Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.8
  1/* SPDX-License-Identifier: GPL-2.0 */
  2/* Microchip switch driver common header
  3 *
  4 * Copyright (C) 2017-2019 Microchip Technology Inc.
  5 */
  6
  7#ifndef __KSZ_COMMON_H
  8#define __KSZ_COMMON_H
  9
 10#include <linux/etherdevice.h>
 11#include <linux/kernel.h>
 12#include <linux/mutex.h>
 13#include <linux/phy.h>
 14#include <linux/regmap.h>
 15#include <net/dsa.h>
 16#include <linux/irq.h>
 17#include <linux/platform_data/microchip-ksz.h>
 18
 19#include "ksz_ptp.h"
 20
 21#define KSZ_MAX_NUM_PORTS 8
 22
 23struct ksz_device;
 24struct ksz_port;
 25
 26enum ksz_regmap_width {
 27	KSZ_REGMAP_8,
 28	KSZ_REGMAP_16,
 29	KSZ_REGMAP_32,
 30	__KSZ_NUM_REGMAPS,
 31};
 32
 33struct vlan_table {
 34	u32 table[3];
 35};
 36
 37struct ksz_port_mib {
 38	struct mutex cnt_mutex;		/* structure access */
 39	u8 cnt_ptr;
 40	u64 *counters;
 41	struct rtnl_link_stats64 stats64;
 42	struct ethtool_pause_stats pause_stats;
 43	struct spinlock stats64_lock;
 44};
 45
 46struct ksz_mib_names {
 47	int index;
 48	char string[ETH_GSTRING_LEN];
 49};
 50
 51struct ksz_chip_data {
 52	u32 chip_id;
 53	const char *dev_name;
 54	int num_vlans;
 55	int num_alus;
 56	int num_statics;
 57	int cpu_ports;
 58	int port_cnt;
 59	u8 port_nirqs;
 60	u8 num_tx_queues;
 61	bool tc_cbs_supported;
 62	bool tc_ets_supported;
 63	const struct ksz_dev_ops *ops;
 
 64	bool ksz87xx_eee_link_erratum;
 65	const struct ksz_mib_names *mib_names;
 66	int mib_cnt;
 67	u8 reg_mib_cnt;
 68	const u16 *regs;
 69	const u32 *masks;
 70	const u8 *shifts;
 71	const u8 *xmii_ctrl0;
 72	const u8 *xmii_ctrl1;
 73	int stp_ctrl_reg;
 74	int broadcast_ctrl_reg;
 75	int multicast_ctrl_reg;
 76	int start_ctrl_reg;
 77	bool supports_mii[KSZ_MAX_NUM_PORTS];
 78	bool supports_rmii[KSZ_MAX_NUM_PORTS];
 79	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
 80	bool internal_phy[KSZ_MAX_NUM_PORTS];
 81	bool gbit_capable[KSZ_MAX_NUM_PORTS];
 82	const struct regmap_access_table *wr_table;
 83	const struct regmap_access_table *rd_table;
 84};
 85
 86struct ksz_irq {
 87	u16 masked;
 88	u16 reg_mask;
 89	u16 reg_status;
 90	struct irq_domain *domain;
 91	int nirqs;
 92	int irq_num;
 93	char name[16];
 94	struct ksz_device *dev;
 95};
 96
 97struct ksz_ptp_irq {
 98	struct ksz_port *port;
 99	u16 ts_reg;
100	bool ts_en;
101	char name[16];
102	int num;
103};
104
105struct ksz_switch_macaddr {
106	unsigned char addr[ETH_ALEN];
107	refcount_t refcount;
108};
109
110struct ksz_port {
111	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
112	bool learning;
113	int stp_state;
114	struct phy_device phydev;
115
 
116	u32 fiber:1;			/* port is fiber */
117	u32 force:1;
118	u32 read:1;			/* read MIB counters in background */
119	u32 freeze:1;			/* MIB counter freeze is enabled */
120
121	struct ksz_port_mib mib;
122	phy_interface_t interface;
123	u32 rgmii_tx_val;
124	u32 rgmii_rx_val;
125	struct ksz_device *ksz_dev;
126	void *acl_priv;
127	struct ksz_irq pirq;
128	u8 num;
129#if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
130	struct hwtstamp_config tstamp_config;
131	bool hwts_tx_en;
132	bool hwts_rx_en;
133	struct ksz_irq ptpirq;
134	struct ksz_ptp_irq ptpmsg_irq[3];
135	ktime_t tstamp_msg;
136	struct completion tstamp_msg_comp;
137#endif
138	bool manual_flow;
139};
140
141struct ksz_device {
142	struct dsa_switch *ds;
143	struct ksz_platform_data *pdata;
144	const struct ksz_chip_data *info;
145
146	struct mutex dev_mutex;		/* device access */
147	struct mutex regmap_mutex;	/* regmap access */
148	struct mutex alu_mutex;		/* ALU access */
149	struct mutex vlan_mutex;	/* vlan access */
150	const struct ksz_dev_ops *dev_ops;
151
152	struct device *dev;
153	struct regmap *regmap[__KSZ_NUM_REGMAPS];
154
155	void *priv;
156	int irq;
157
158	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
159
160	/* chip specific data */
161	u32 chip_id;
162	u8 chip_rev;
163	int cpu_port;			/* port connected to CPU */
164	int phy_port_cnt;
165	phy_interface_t compat_interface;
166	bool synclko_125;
167	bool synclko_disable;
168	bool wakeup_source;
169
170	struct vlan_table *vlan_cache;
171
172	struct ksz_port *ports;
173	struct delayed_work mib_read;
174	unsigned long mib_read_interval;
175	u16 mirror_rx;
176	u16 mirror_tx;
177	u16 port_mask;
178	struct mutex lock_irq;		/* IRQ Access */
179	struct ksz_irq girq;
180	struct ksz_ptp_data ptp_data;
181
182	struct ksz_switch_macaddr *switch_macaddr;
183	struct net_device *hsr_dev;     /* HSR */
184	u8 hsr_ports;
185};
186
187/* List of supported models */
188enum ksz_model {
189	KSZ8563,
190	KSZ8795,
191	KSZ8794,
192	KSZ8765,
193	KSZ8830,
194	KSZ9477,
195	KSZ9896,
196	KSZ9897,
197	KSZ9893,
198	KSZ9563,
199	KSZ9567,
200	LAN9370,
201	LAN9371,
202	LAN9372,
203	LAN9373,
204	LAN9374,
205};
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207enum ksz_regs {
208	REG_SW_MAC_ADDR,
209	REG_IND_CTRL_0,
210	REG_IND_DATA_8,
211	REG_IND_DATA_CHECK,
212	REG_IND_DATA_HI,
213	REG_IND_DATA_LO,
214	REG_IND_MIB_CHECK,
215	REG_IND_BYTE,
216	P_FORCE_CTRL,
217	P_LINK_STATUS,
218	P_LOCAL_CTRL,
219	P_NEG_RESTART_CTRL,
220	P_REMOTE_STATUS,
221	P_SPEED_STATUS,
222	S_TAIL_TAG_CTRL,
223	P_STP_CTRL,
224	S_START_CTRL,
225	S_BROADCAST_CTRL,
226	S_MULTICAST_CTRL,
227	P_XMII_CTRL_0,
228	P_XMII_CTRL_1,
229};
230
231enum ksz_masks {
232	PORT_802_1P_REMAPPING,
233	SW_TAIL_TAG_ENABLE,
234	MIB_COUNTER_OVERFLOW,
235	MIB_COUNTER_VALID,
236	VLAN_TABLE_FID,
237	VLAN_TABLE_MEMBERSHIP,
238	VLAN_TABLE_VALID,
239	STATIC_MAC_TABLE_VALID,
240	STATIC_MAC_TABLE_USE_FID,
241	STATIC_MAC_TABLE_FID,
242	STATIC_MAC_TABLE_OVERRIDE,
243	STATIC_MAC_TABLE_FWD_PORTS,
244	DYNAMIC_MAC_TABLE_ENTRIES_H,
245	DYNAMIC_MAC_TABLE_MAC_EMPTY,
246	DYNAMIC_MAC_TABLE_NOT_READY,
247	DYNAMIC_MAC_TABLE_ENTRIES,
248	DYNAMIC_MAC_TABLE_FID,
249	DYNAMIC_MAC_TABLE_SRC_PORT,
250	DYNAMIC_MAC_TABLE_TIMESTAMP,
251	ALU_STAT_WRITE,
252	ALU_STAT_READ,
253	P_MII_TX_FLOW_CTRL,
254	P_MII_RX_FLOW_CTRL,
255};
256
257enum ksz_shifts {
258	VLAN_TABLE_MEMBERSHIP_S,
259	VLAN_TABLE,
260	STATIC_MAC_FWD_PORTS,
261	STATIC_MAC_FID,
262	DYNAMIC_MAC_ENTRIES_H,
263	DYNAMIC_MAC_ENTRIES,
264	DYNAMIC_MAC_FID,
265	DYNAMIC_MAC_TIMESTAMP,
266	DYNAMIC_MAC_SRC_PORT,
267	ALU_STAT_INDEX,
268};
269
270enum ksz_xmii_ctrl0 {
271	P_MII_100MBIT,
272	P_MII_10MBIT,
273	P_MII_FULL_DUPLEX,
274	P_MII_HALF_DUPLEX,
275};
276
277enum ksz_xmii_ctrl1 {
278	P_RGMII_SEL,
279	P_RMII_SEL,
280	P_GMII_SEL,
281	P_MII_SEL,
282	P_GMII_1GBIT,
283	P_GMII_NOT_1GBIT,
284};
285
286struct alu_struct {
287	/* entry 1 */
288	u8	is_static:1;
289	u8	is_src_filter:1;
290	u8	is_dst_filter:1;
291	u8	prio_age:3;
292	u32	_reserv_0_1:23;
293	u8	mstp:3;
294	/* entry 2 */
295	u8	is_override:1;
296	u8	is_use_fid:1;
297	u32	_reserv_1_1:23;
298	u8	port_forward:7;
299	/* entry 3 & 4*/
300	u32	_reserv_2_1:9;
301	u8	fid:7;
302	u8	mac[ETH_ALEN];
303};
304
305struct ksz_dev_ops {
306	int (*setup)(struct dsa_switch *ds);
307	void (*teardown)(struct dsa_switch *ds);
308	u32 (*get_port_addr)(int port, int offset);
309	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
310	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
311	void (*port_cleanup)(struct ksz_device *dev, int port);
312	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
313	int (*set_ageing_time)(struct ksz_device *dev, unsigned int msecs);
314	int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
315	int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
316	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
317			  u64 *cnt);
318	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
319			  u64 *dropped, u64 *cnt);
320	void (*r_mib_stat64)(struct ksz_device *dev, int port);
321	int  (*vlan_filtering)(struct ksz_device *dev, int port,
322			       bool flag, struct netlink_ext_ack *extack);
323	int  (*vlan_add)(struct ksz_device *dev, int port,
324			 const struct switchdev_obj_port_vlan *vlan,
325			 struct netlink_ext_ack *extack);
326	int  (*vlan_del)(struct ksz_device *dev, int port,
327			 const struct switchdev_obj_port_vlan *vlan);
328	int (*mirror_add)(struct ksz_device *dev, int port,
329			  struct dsa_mall_mirror_tc_entry *mirror,
330			  bool ingress, struct netlink_ext_ack *extack);
331	void (*mirror_del)(struct ksz_device *dev, int port,
332			   struct dsa_mall_mirror_tc_entry *mirror);
333	int (*fdb_add)(struct ksz_device *dev, int port,
334		       const unsigned char *addr, u16 vid, struct dsa_db db);
335	int (*fdb_del)(struct ksz_device *dev, int port,
336		       const unsigned char *addr, u16 vid, struct dsa_db db);
337	int (*fdb_dump)(struct ksz_device *dev, int port,
338			dsa_fdb_dump_cb_t *cb, void *data);
339	int (*mdb_add)(struct ksz_device *dev, int port,
340		       const struct switchdev_obj_port_mdb *mdb,
341		       struct dsa_db db);
342	int (*mdb_del)(struct ksz_device *dev, int port,
343		       const struct switchdev_obj_port_mdb *mdb,
344		       struct dsa_db db);
345	void (*get_caps)(struct ksz_device *dev, int port,
346			 struct phylink_config *config);
347	int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
348	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
349	void (*port_init_cnt)(struct ksz_device *dev, int port);
350	void (*phylink_mac_config)(struct ksz_device *dev, int port,
351				   unsigned int mode,
352				   const struct phylink_link_state *state);
353	void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
354				    unsigned int mode,
355				    phy_interface_t interface,
356				    struct phy_device *phydev, int speed,
357				    int duplex, bool tx_pause, bool rx_pause);
358	void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
359	int (*tc_cbs_set_cinc)(struct ksz_device *dev, int port, u32 val);
360	void (*get_wol)(struct ksz_device *dev, int port,
361			struct ethtool_wolinfo *wol);
362	int (*set_wol)(struct ksz_device *dev, int port,
363		       struct ethtool_wolinfo *wol);
364	void (*wol_pre_shutdown)(struct ksz_device *dev, bool *wol_enabled);
365	void (*config_cpu_port)(struct dsa_switch *ds);
366	int (*enable_stp_addr)(struct ksz_device *dev);
367	int (*reset)(struct ksz_device *dev);
368	int (*init)(struct ksz_device *dev);
369	void (*exit)(struct ksz_device *dev);
370};
371
372struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
373int ksz_switch_register(struct ksz_device *dev);
374void ksz_switch_remove(struct ksz_device *dev);
375
376void ksz_init_mib_timer(struct ksz_device *dev);
377bool ksz_is_port_mac_global_usable(struct dsa_switch *ds, int port);
378void ksz_r_mib_stats64(struct ksz_device *dev, int port);
379void ksz88xx_r_mib_stats64(struct ksz_device *dev, int port);
380void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
381bool ksz_get_gbit(struct ksz_device *dev, int port);
382phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
383extern const struct ksz_chip_data ksz_switch_chips[];
384int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
385			   struct netlink_ext_ack *extack);
386void ksz_switch_macaddr_put(struct dsa_switch *ds);
387void ksz_switch_shutdown(struct ksz_device *dev);
388
389/* Common register access functions */
390static inline struct regmap *ksz_regmap_8(struct ksz_device *dev)
391{
392	return dev->regmap[KSZ_REGMAP_8];
393}
394
395static inline struct regmap *ksz_regmap_16(struct ksz_device *dev)
396{
397	return dev->regmap[KSZ_REGMAP_16];
398}
399
400static inline struct regmap *ksz_regmap_32(struct ksz_device *dev)
401{
402	return dev->regmap[KSZ_REGMAP_32];
403}
404
405static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
406{
407	unsigned int value;
408	int ret = regmap_read(ksz_regmap_8(dev), reg, &value);
409
410	if (ret)
411		dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
412			ERR_PTR(ret));
413
414	*val = value;
415	return ret;
416}
417
418static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
419{
420	unsigned int value;
421	int ret = regmap_read(ksz_regmap_16(dev), reg, &value);
422
423	if (ret)
424		dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
425			ERR_PTR(ret));
426
427	*val = value;
428	return ret;
429}
430
431static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
432{
433	unsigned int value;
434	int ret = regmap_read(ksz_regmap_32(dev), reg, &value);
435
436	if (ret)
437		dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
438			ERR_PTR(ret));
439
440	*val = value;
441	return ret;
442}
443
444static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
445{
446	u32 value[2];
447	int ret;
448
449	ret = regmap_bulk_read(ksz_regmap_32(dev), reg, value, 2);
450	if (ret)
451		dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
452			ERR_PTR(ret));
453	else
454		*val = (u64)value[0] << 32 | value[1];
455
456	return ret;
457}
458
459static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
460{
461	int ret;
462
463	ret = regmap_write(ksz_regmap_8(dev), reg, value);
464	if (ret)
465		dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
466			ERR_PTR(ret));
467
468	return ret;
469}
470
471static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
472{
473	int ret;
474
475	ret = regmap_write(ksz_regmap_16(dev), reg, value);
476	if (ret)
477		dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
478			ERR_PTR(ret));
479
480	return ret;
481}
482
483static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
484{
485	int ret;
486
487	ret = regmap_write(ksz_regmap_32(dev), reg, value);
488	if (ret)
489		dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
490			ERR_PTR(ret));
491
492	return ret;
493}
494
495static inline int ksz_rmw16(struct ksz_device *dev, u32 reg, u16 mask,
496			    u16 value)
497{
498	int ret;
499
500	ret = regmap_update_bits(ksz_regmap_16(dev), reg, mask, value);
501	if (ret)
502		dev_err(dev->dev, "can't rmw 16bit reg 0x%x: %pe\n", reg,
503			ERR_PTR(ret));
504
505	return ret;
506}
507
508static inline int ksz_rmw32(struct ksz_device *dev, u32 reg, u32 mask,
509			    u32 value)
510{
511	int ret;
512
513	ret = regmap_update_bits(ksz_regmap_32(dev), reg, mask, value);
514	if (ret)
515		dev_err(dev->dev, "can't rmw 32bit reg 0x%x: %pe\n", reg,
516			ERR_PTR(ret));
517
518	return ret;
519}
520
521static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
522{
523	u32 val[2];
524
525	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
526	value = swab64(value);
527	val[0] = swab32(value & 0xffffffffULL);
528	val[1] = swab32(value >> 32ULL);
529
530	return regmap_bulk_write(ksz_regmap_32(dev), reg, val, 2);
531}
532
533static inline int ksz_rmw8(struct ksz_device *dev, int offset, u8 mask, u8 val)
534{
535	int ret;
536
537	ret = regmap_update_bits(ksz_regmap_8(dev), offset, mask, val);
538	if (ret)
539		dev_err(dev->dev, "can't rmw 8bit reg 0x%x: %pe\n", offset,
540			ERR_PTR(ret));
541
542	return ret;
543}
544
545static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
546			     u8 *data)
547{
548	return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
549}
550
551static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
552			      u16 *data)
553{
554	return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
555}
556
557static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
558			      u32 *data)
559{
560	return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
561}
562
563static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
564			      u8 data)
565{
566	return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
567}
568
569static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
570			       u16 data)
571{
572	return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
573			   data);
574}
575
576static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
577			       u32 data)
578{
579	return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
580			   data);
581}
582
583static inline int ksz_prmw8(struct ksz_device *dev, int port, int offset,
584			    u8 mask, u8 val)
585{
586	return ksz_rmw8(dev, dev->dev_ops->get_port_addr(port, offset),
587			mask, val);
588}
589
590static inline int ksz_prmw32(struct ksz_device *dev, int port, int offset,
591			     u32 mask, u32 val)
592{
593	return ksz_rmw32(dev, dev->dev_ops->get_port_addr(port, offset),
594			 mask, val);
 
595}
596
597static inline void ksz_regmap_lock(void *__mtx)
598{
599	struct mutex *mtx = __mtx;
600	mutex_lock(mtx);
601}
602
603static inline void ksz_regmap_unlock(void *__mtx)
604{
605	struct mutex *mtx = __mtx;
606	mutex_unlock(mtx);
607}
608
609static inline bool ksz_is_ksz87xx(struct ksz_device *dev)
610{
611	return dev->chip_id == KSZ8795_CHIP_ID ||
612	       dev->chip_id == KSZ8794_CHIP_ID ||
613	       dev->chip_id == KSZ8765_CHIP_ID;
614}
615
616static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
617{
618	return dev->chip_id == KSZ8830_CHIP_ID;
619}
620
621static inline int is_lan937x(struct ksz_device *dev)
622{
623	return dev->chip_id == LAN9370_CHIP_ID ||
624		dev->chip_id == LAN9371_CHIP_ID ||
625		dev->chip_id == LAN9372_CHIP_ID ||
626		dev->chip_id == LAN9373_CHIP_ID ||
627		dev->chip_id == LAN9374_CHIP_ID;
628}
629
630/* STP State Defines */
631#define PORT_TX_ENABLE			BIT(2)
632#define PORT_RX_ENABLE			BIT(1)
633#define PORT_LEARN_DISABLE		BIT(0)
634
635/* Switch ID Defines */
636#define REG_CHIP_ID0			0x00
637
638#define SW_FAMILY_ID_M			GENMASK(15, 8)
639#define KSZ87_FAMILY_ID			0x87
640#define KSZ88_FAMILY_ID			0x88
641
642#define KSZ8_PORT_STATUS_0		0x08
643#define KSZ8_PORT_FIBER_MODE		BIT(7)
644
645#define SW_CHIP_ID_M			GENMASK(7, 4)
646#define KSZ87_CHIP_ID_94		0x6
647#define KSZ87_CHIP_ID_95		0x9
648#define KSZ88_CHIP_ID_63		0x3
649
650#define SW_REV_ID_M			GENMASK(7, 4)
651
652/* KSZ9893, KSZ9563, KSZ8563 specific register  */
653#define REG_CHIP_ID4			0x0f
654#define SKU_ID_KSZ8563			0x3c
655#define SKU_ID_KSZ9563			0x1c
656
657/* Driver set switch broadcast storm protection at 10% rate. */
658#define BROADCAST_STORM_PROT_RATE	10
659
660/* 148,800 frames * 67 ms / 100 */
661#define BROADCAST_STORM_VALUE		9969
662
663#define BROADCAST_STORM_RATE_HI		0x07
664#define BROADCAST_STORM_RATE_LO		0xFF
665#define BROADCAST_STORM_RATE		0x07FF
666
667#define MULTICAST_STORM_DISABLE		BIT(6)
668
669#define SW_START			0x01
670
671/* xMII configuration */
672#define P_MII_DUPLEX_M			BIT(6)
673#define P_MII_100MBIT_M			BIT(4)
674
675#define P_GMII_1GBIT_M			BIT(6)
676#define P_RGMII_ID_IG_ENABLE		BIT(4)
677#define P_RGMII_ID_EG_ENABLE		BIT(3)
678#define P_MII_MAC_MODE			BIT(2)
679#define P_MII_SEL_M			0x3
680
681/* Interrupt */
682#define REG_SW_PORT_INT_STATUS__1	0x001B
683#define REG_SW_PORT_INT_MASK__1		0x001F
684
685#define REG_PORT_INT_STATUS		0x001B
686#define REG_PORT_INT_MASK		0x001F
687
688#define PORT_SRC_PHY_INT		1
689#define PORT_SRC_PTP_INT		2
690
691#define KSZ8795_HUGE_PACKET_SIZE	2000
692#define KSZ8863_HUGE_PACKET_SIZE	1916
693#define KSZ8863_NORMAL_PACKET_SIZE	1536
694#define KSZ8_LEGAL_PACKET_SIZE		1518
695#define KSZ9477_MAX_FRAME_SIZE		9000
696
697#define KSZ8873_REG_GLOBAL_CTRL_12	0x0e
698/* Drive Strength of I/O Pad
699 * 0: 8mA, 1: 16mA
700 */
701#define KSZ8873_DRIVE_STRENGTH_16MA	BIT(6)
702
703#define KSZ8795_REG_SW_CTRL_20		0xa3
704#define KSZ9477_REG_SW_IO_STRENGTH	0x010d
705#define SW_DRIVE_STRENGTH_M		0x7
706#define SW_DRIVE_STRENGTH_2MA		0
707#define SW_DRIVE_STRENGTH_4MA		1
708#define SW_DRIVE_STRENGTH_8MA		2
709#define SW_DRIVE_STRENGTH_12MA		3
710#define SW_DRIVE_STRENGTH_16MA		4
711#define SW_DRIVE_STRENGTH_20MA		5
712#define SW_DRIVE_STRENGTH_24MA		6
713#define SW_DRIVE_STRENGTH_28MA		7
714#define SW_HI_SPEED_DRIVE_STRENGTH_S	4
715#define SW_LO_SPEED_DRIVE_STRENGTH_S	0
716
717#define KSZ9477_REG_PORT_OUT_RATE_0	0x0420
718#define KSZ9477_OUT_RATE_NO_LIMIT	0
719
720#define KSZ9477_PORT_MRI_TC_MAP__4	0x0808
721
722#define KSZ9477_PORT_TC_MAP_S		4
723#define KSZ9477_MAX_TC_PRIO		7
724
725/* CBS related registers */
726#define REG_PORT_MTI_QUEUE_INDEX__4	0x0900
727
728#define REG_PORT_MTI_QUEUE_CTRL_0	0x0914
729
730#define MTI_SCHEDULE_MODE_M		GENMASK(7, 6)
731#define MTI_SCHEDULE_STRICT_PRIO	0
732#define MTI_SCHEDULE_WRR		2
733#define MTI_SHAPING_M			GENMASK(5, 4)
734#define MTI_SHAPING_OFF			0
735#define MTI_SHAPING_SRP			1
736#define MTI_SHAPING_TIME_AWARE		2
737
738#define KSZ9477_PORT_MTI_QUEUE_CTRL_1	0x0915
739#define KSZ9477_DEFAULT_WRR_WEIGHT	1
740
741#define REG_PORT_MTI_HI_WATER_MARK	0x0916
742#define REG_PORT_MTI_LO_WATER_MARK	0x0918
743
744/* Regmap tables generation */
745#define KSZ_SPI_OP_RD		3
746#define KSZ_SPI_OP_WR		2
747
748#define swabnot_used(x)		0
749
750#define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
751	swab##swp((opcode) << ((regbits) + (regpad)))
752
753#define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
754	{								\
755		.name = #width,						\
756		.val_bits = (width),					\
757		.reg_stride = 1,					\
758		.reg_bits = (regbits) + (regalign),			\
759		.pad_bits = (regpad),					\
760		.max_register = BIT(regbits) - 1,			\
761		.cache_type = REGCACHE_NONE,				\
762		.read_flag_mask =					\
763			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
764					     regbits, regpad),		\
765		.write_flag_mask =					\
766			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
767					     regbits, regpad),		\
768		.lock = ksz_regmap_lock,				\
769		.unlock = ksz_regmap_unlock,				\
770		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
771		.val_format_endian = REGMAP_ENDIAN_BIG			\
772	}
773
774#define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
775	static const struct regmap_config ksz##_regmap_config[] = {	\
776		[KSZ_REGMAP_8] = KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
777		[KSZ_REGMAP_16] = KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
778		[KSZ_REGMAP_32] = KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
779	}
780
781#endif
v6.2
  1/* SPDX-License-Identifier: GPL-2.0 */
  2/* Microchip switch driver common header
  3 *
  4 * Copyright (C) 2017-2019 Microchip Technology Inc.
  5 */
  6
  7#ifndef __KSZ_COMMON_H
  8#define __KSZ_COMMON_H
  9
 10#include <linux/etherdevice.h>
 11#include <linux/kernel.h>
 12#include <linux/mutex.h>
 13#include <linux/phy.h>
 14#include <linux/regmap.h>
 15#include <net/dsa.h>
 16#include <linux/irq.h>
 
 
 
 17
 18#define KSZ_MAX_NUM_PORTS 8
 19
 20struct ksz_device;
 
 
 
 
 
 
 
 
 21
 22struct vlan_table {
 23	u32 table[3];
 24};
 25
 26struct ksz_port_mib {
 27	struct mutex cnt_mutex;		/* structure access */
 28	u8 cnt_ptr;
 29	u64 *counters;
 30	struct rtnl_link_stats64 stats64;
 31	struct ethtool_pause_stats pause_stats;
 32	struct spinlock stats64_lock;
 33};
 34
 35struct ksz_mib_names {
 36	int index;
 37	char string[ETH_GSTRING_LEN];
 38};
 39
 40struct ksz_chip_data {
 41	u32 chip_id;
 42	const char *dev_name;
 43	int num_vlans;
 44	int num_alus;
 45	int num_statics;
 46	int cpu_ports;
 47	int port_cnt;
 48	u8 port_nirqs;
 
 
 
 49	const struct ksz_dev_ops *ops;
 50	bool phy_errata_9477;
 51	bool ksz87xx_eee_link_erratum;
 52	const struct ksz_mib_names *mib_names;
 53	int mib_cnt;
 54	u8 reg_mib_cnt;
 55	const u16 *regs;
 56	const u32 *masks;
 57	const u8 *shifts;
 58	const u8 *xmii_ctrl0;
 59	const u8 *xmii_ctrl1;
 60	int stp_ctrl_reg;
 61	int broadcast_ctrl_reg;
 62	int multicast_ctrl_reg;
 63	int start_ctrl_reg;
 64	bool supports_mii[KSZ_MAX_NUM_PORTS];
 65	bool supports_rmii[KSZ_MAX_NUM_PORTS];
 66	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
 67	bool internal_phy[KSZ_MAX_NUM_PORTS];
 68	bool gbit_capable[KSZ_MAX_NUM_PORTS];
 69	const struct regmap_access_table *wr_table;
 70	const struct regmap_access_table *rd_table;
 71};
 72
 73struct ksz_irq {
 74	u16 masked;
 75	u16 reg_mask;
 76	u16 reg_status;
 77	struct irq_domain *domain;
 78	int nirqs;
 79	int irq_num;
 80	char name[16];
 81	struct ksz_device *dev;
 82};
 83
 
 
 
 
 
 
 
 
 
 
 
 
 
 84struct ksz_port {
 85	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
 86	bool learning;
 87	int stp_state;
 88	struct phy_device phydev;
 89
 90	u32 on:1;			/* port is not disabled by hardware */
 91	u32 fiber:1;			/* port is fiber */
 92	u32 force:1;
 93	u32 read:1;			/* read MIB counters in background */
 94	u32 freeze:1;			/* MIB counter freeze is enabled */
 95
 96	struct ksz_port_mib mib;
 97	phy_interface_t interface;
 98	u32 rgmii_tx_val;
 99	u32 rgmii_rx_val;
100	struct ksz_device *ksz_dev;
 
101	struct ksz_irq pirq;
102	u8 num;
 
 
 
 
 
 
 
 
 
 
103};
104
105struct ksz_device {
106	struct dsa_switch *ds;
107	struct ksz_platform_data *pdata;
108	const struct ksz_chip_data *info;
109
110	struct mutex dev_mutex;		/* device access */
111	struct mutex regmap_mutex;	/* regmap access */
112	struct mutex alu_mutex;		/* ALU access */
113	struct mutex vlan_mutex;	/* vlan access */
114	const struct ksz_dev_ops *dev_ops;
115
116	struct device *dev;
117	struct regmap *regmap[3];
118
119	void *priv;
120	int irq;
121
122	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
123
124	/* chip specific data */
125	u32 chip_id;
126	u8 chip_rev;
127	int cpu_port;			/* port connected to CPU */
128	int phy_port_cnt;
129	phy_interface_t compat_interface;
130	bool synclko_125;
131	bool synclko_disable;
 
132
133	struct vlan_table *vlan_cache;
134
135	struct ksz_port *ports;
136	struct delayed_work mib_read;
137	unsigned long mib_read_interval;
138	u16 mirror_rx;
139	u16 mirror_tx;
140	u16 port_mask;
141	struct mutex lock_irq;		/* IRQ Access */
142	struct ksz_irq girq;
 
 
 
 
 
143};
144
145/* List of supported models */
146enum ksz_model {
147	KSZ8563,
148	KSZ8795,
149	KSZ8794,
150	KSZ8765,
151	KSZ8830,
152	KSZ9477,
153	KSZ9896,
154	KSZ9897,
155	KSZ9893,
156	KSZ9563,
157	KSZ9567,
158	LAN9370,
159	LAN9371,
160	LAN9372,
161	LAN9373,
162	LAN9374,
163};
164
165enum ksz_chip_id {
166	KSZ8563_CHIP_ID = 0x8563,
167	KSZ8795_CHIP_ID = 0x8795,
168	KSZ8794_CHIP_ID = 0x8794,
169	KSZ8765_CHIP_ID = 0x8765,
170	KSZ8830_CHIP_ID = 0x8830,
171	KSZ9477_CHIP_ID = 0x00947700,
172	KSZ9896_CHIP_ID = 0x00989600,
173	KSZ9897_CHIP_ID = 0x00989700,
174	KSZ9893_CHIP_ID = 0x00989300,
175	KSZ9563_CHIP_ID = 0x00956300,
176	KSZ9567_CHIP_ID = 0x00956700,
177	LAN9370_CHIP_ID = 0x00937000,
178	LAN9371_CHIP_ID = 0x00937100,
179	LAN9372_CHIP_ID = 0x00937200,
180	LAN9373_CHIP_ID = 0x00937300,
181	LAN9374_CHIP_ID = 0x00937400,
182};
183
184enum ksz_regs {
 
185	REG_IND_CTRL_0,
186	REG_IND_DATA_8,
187	REG_IND_DATA_CHECK,
188	REG_IND_DATA_HI,
189	REG_IND_DATA_LO,
190	REG_IND_MIB_CHECK,
191	REG_IND_BYTE,
192	P_FORCE_CTRL,
193	P_LINK_STATUS,
194	P_LOCAL_CTRL,
195	P_NEG_RESTART_CTRL,
196	P_REMOTE_STATUS,
197	P_SPEED_STATUS,
198	S_TAIL_TAG_CTRL,
199	P_STP_CTRL,
200	S_START_CTRL,
201	S_BROADCAST_CTRL,
202	S_MULTICAST_CTRL,
203	P_XMII_CTRL_0,
204	P_XMII_CTRL_1,
205};
206
207enum ksz_masks {
208	PORT_802_1P_REMAPPING,
209	SW_TAIL_TAG_ENABLE,
210	MIB_COUNTER_OVERFLOW,
211	MIB_COUNTER_VALID,
212	VLAN_TABLE_FID,
213	VLAN_TABLE_MEMBERSHIP,
214	VLAN_TABLE_VALID,
215	STATIC_MAC_TABLE_VALID,
216	STATIC_MAC_TABLE_USE_FID,
217	STATIC_MAC_TABLE_FID,
218	STATIC_MAC_TABLE_OVERRIDE,
219	STATIC_MAC_TABLE_FWD_PORTS,
220	DYNAMIC_MAC_TABLE_ENTRIES_H,
221	DYNAMIC_MAC_TABLE_MAC_EMPTY,
222	DYNAMIC_MAC_TABLE_NOT_READY,
223	DYNAMIC_MAC_TABLE_ENTRIES,
224	DYNAMIC_MAC_TABLE_FID,
225	DYNAMIC_MAC_TABLE_SRC_PORT,
226	DYNAMIC_MAC_TABLE_TIMESTAMP,
227	ALU_STAT_WRITE,
228	ALU_STAT_READ,
229	P_MII_TX_FLOW_CTRL,
230	P_MII_RX_FLOW_CTRL,
231};
232
233enum ksz_shifts {
234	VLAN_TABLE_MEMBERSHIP_S,
235	VLAN_TABLE,
236	STATIC_MAC_FWD_PORTS,
237	STATIC_MAC_FID,
238	DYNAMIC_MAC_ENTRIES_H,
239	DYNAMIC_MAC_ENTRIES,
240	DYNAMIC_MAC_FID,
241	DYNAMIC_MAC_TIMESTAMP,
242	DYNAMIC_MAC_SRC_PORT,
243	ALU_STAT_INDEX,
244};
245
246enum ksz_xmii_ctrl0 {
247	P_MII_100MBIT,
248	P_MII_10MBIT,
249	P_MII_FULL_DUPLEX,
250	P_MII_HALF_DUPLEX,
251};
252
253enum ksz_xmii_ctrl1 {
254	P_RGMII_SEL,
255	P_RMII_SEL,
256	P_GMII_SEL,
257	P_MII_SEL,
258	P_GMII_1GBIT,
259	P_GMII_NOT_1GBIT,
260};
261
262struct alu_struct {
263	/* entry 1 */
264	u8	is_static:1;
265	u8	is_src_filter:1;
266	u8	is_dst_filter:1;
267	u8	prio_age:3;
268	u32	_reserv_0_1:23;
269	u8	mstp:3;
270	/* entry 2 */
271	u8	is_override:1;
272	u8	is_use_fid:1;
273	u32	_reserv_1_1:23;
274	u8	port_forward:7;
275	/* entry 3 & 4*/
276	u32	_reserv_2_1:9;
277	u8	fid:7;
278	u8	mac[ETH_ALEN];
279};
280
281struct ksz_dev_ops {
282	int (*setup)(struct dsa_switch *ds);
283	void (*teardown)(struct dsa_switch *ds);
284	u32 (*get_port_addr)(int port, int offset);
285	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
286	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
287	void (*port_cleanup)(struct ksz_device *dev, int port);
288	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
289	int (*set_ageing_time)(struct ksz_device *dev, unsigned int msecs);
290	int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
291	int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
292	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
293			  u64 *cnt);
294	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
295			  u64 *dropped, u64 *cnt);
296	void (*r_mib_stat64)(struct ksz_device *dev, int port);
297	int  (*vlan_filtering)(struct ksz_device *dev, int port,
298			       bool flag, struct netlink_ext_ack *extack);
299	int  (*vlan_add)(struct ksz_device *dev, int port,
300			 const struct switchdev_obj_port_vlan *vlan,
301			 struct netlink_ext_ack *extack);
302	int  (*vlan_del)(struct ksz_device *dev, int port,
303			 const struct switchdev_obj_port_vlan *vlan);
304	int (*mirror_add)(struct ksz_device *dev, int port,
305			  struct dsa_mall_mirror_tc_entry *mirror,
306			  bool ingress, struct netlink_ext_ack *extack);
307	void (*mirror_del)(struct ksz_device *dev, int port,
308			   struct dsa_mall_mirror_tc_entry *mirror);
309	int (*fdb_add)(struct ksz_device *dev, int port,
310		       const unsigned char *addr, u16 vid, struct dsa_db db);
311	int (*fdb_del)(struct ksz_device *dev, int port,
312		       const unsigned char *addr, u16 vid, struct dsa_db db);
313	int (*fdb_dump)(struct ksz_device *dev, int port,
314			dsa_fdb_dump_cb_t *cb, void *data);
315	int (*mdb_add)(struct ksz_device *dev, int port,
316		       const struct switchdev_obj_port_mdb *mdb,
317		       struct dsa_db db);
318	int (*mdb_del)(struct ksz_device *dev, int port,
319		       const struct switchdev_obj_port_mdb *mdb,
320		       struct dsa_db db);
321	void (*get_caps)(struct ksz_device *dev, int port,
322			 struct phylink_config *config);
323	int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
324	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
325	void (*port_init_cnt)(struct ksz_device *dev, int port);
326	void (*phylink_mac_config)(struct ksz_device *dev, int port,
327				   unsigned int mode,
328				   const struct phylink_link_state *state);
329	void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
330				    unsigned int mode,
331				    phy_interface_t interface,
332				    struct phy_device *phydev, int speed,
333				    int duplex, bool tx_pause, bool rx_pause);
334	void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
 
 
 
 
 
 
335	void (*config_cpu_port)(struct dsa_switch *ds);
336	int (*enable_stp_addr)(struct ksz_device *dev);
337	int (*reset)(struct ksz_device *dev);
338	int (*init)(struct ksz_device *dev);
339	void (*exit)(struct ksz_device *dev);
340};
341
342struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
343int ksz_switch_register(struct ksz_device *dev);
344void ksz_switch_remove(struct ksz_device *dev);
345
346void ksz_init_mib_timer(struct ksz_device *dev);
 
347void ksz_r_mib_stats64(struct ksz_device *dev, int port);
348void ksz88xx_r_mib_stats64(struct ksz_device *dev, int port);
349void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
350bool ksz_get_gbit(struct ksz_device *dev, int port);
351phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
352extern const struct ksz_chip_data ksz_switch_chips[];
 
 
 
 
353
354/* Common register access functions */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
356static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
357{
358	unsigned int value;
359	int ret = regmap_read(dev->regmap[0], reg, &value);
360
361	if (ret)
362		dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
363			ERR_PTR(ret));
364
365	*val = value;
366	return ret;
367}
368
369static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
370{
371	unsigned int value;
372	int ret = regmap_read(dev->regmap[1], reg, &value);
373
374	if (ret)
375		dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
376			ERR_PTR(ret));
377
378	*val = value;
379	return ret;
380}
381
382static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
383{
384	unsigned int value;
385	int ret = regmap_read(dev->regmap[2], reg, &value);
386
387	if (ret)
388		dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
389			ERR_PTR(ret));
390
391	*val = value;
392	return ret;
393}
394
395static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
396{
397	u32 value[2];
398	int ret;
399
400	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
401	if (ret)
402		dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
403			ERR_PTR(ret));
404	else
405		*val = (u64)value[0] << 32 | value[1];
406
407	return ret;
408}
409
410static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
411{
412	int ret;
413
414	ret = regmap_write(dev->regmap[0], reg, value);
415	if (ret)
416		dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
417			ERR_PTR(ret));
418
419	return ret;
420}
421
422static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
423{
424	int ret;
425
426	ret = regmap_write(dev->regmap[1], reg, value);
427	if (ret)
428		dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
429			ERR_PTR(ret));
430
431	return ret;
432}
433
434static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
435{
436	int ret;
437
438	ret = regmap_write(dev->regmap[2], reg, value);
439	if (ret)
440		dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
441			ERR_PTR(ret));
442
443	return ret;
444}
445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
447{
448	u32 val[2];
449
450	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
451	value = swab64(value);
452	val[0] = swab32(value & 0xffffffffULL);
453	val[1] = swab32(value >> 32ULL);
454
455	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
456}
457
458static inline int ksz_rmw8(struct ksz_device *dev, int offset, u8 mask, u8 val)
459{
460	return regmap_update_bits(dev->regmap[0], offset, mask, val);
 
 
 
 
 
 
 
461}
462
463static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
464			     u8 *data)
465{
466	return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
467}
468
469static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
470			      u16 *data)
471{
472	return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
473}
474
475static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
476			      u32 *data)
477{
478	return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
479}
480
481static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
482			      u8 data)
483{
484	return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
485}
486
487static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
488			       u16 data)
489{
490	return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
491			   data);
492}
493
494static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
495			       u32 data)
496{
497	return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
498			   data);
499}
500
501static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
502			     u8 mask, u8 val)
 
 
 
 
 
 
 
503{
504	regmap_update_bits(dev->regmap[0],
505			   dev->dev_ops->get_port_addr(port, offset),
506			   mask, val);
507}
508
509static inline void ksz_regmap_lock(void *__mtx)
510{
511	struct mutex *mtx = __mtx;
512	mutex_lock(mtx);
513}
514
515static inline void ksz_regmap_unlock(void *__mtx)
516{
517	struct mutex *mtx = __mtx;
518	mutex_unlock(mtx);
519}
520
 
 
 
 
 
 
 
521static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
522{
523	return dev->chip_id == KSZ8830_CHIP_ID;
524}
525
526static inline int is_lan937x(struct ksz_device *dev)
527{
528	return dev->chip_id == LAN9370_CHIP_ID ||
529		dev->chip_id == LAN9371_CHIP_ID ||
530		dev->chip_id == LAN9372_CHIP_ID ||
531		dev->chip_id == LAN9373_CHIP_ID ||
532		dev->chip_id == LAN9374_CHIP_ID;
533}
534
535/* STP State Defines */
536#define PORT_TX_ENABLE			BIT(2)
537#define PORT_RX_ENABLE			BIT(1)
538#define PORT_LEARN_DISABLE		BIT(0)
539
540/* Switch ID Defines */
541#define REG_CHIP_ID0			0x00
542
543#define SW_FAMILY_ID_M			GENMASK(15, 8)
544#define KSZ87_FAMILY_ID			0x87
545#define KSZ88_FAMILY_ID			0x88
546
547#define KSZ8_PORT_STATUS_0		0x08
548#define KSZ8_PORT_FIBER_MODE		BIT(7)
549
550#define SW_CHIP_ID_M			GENMASK(7, 4)
551#define KSZ87_CHIP_ID_94		0x6
552#define KSZ87_CHIP_ID_95		0x9
553#define KSZ88_CHIP_ID_63		0x3
554
555#define SW_REV_ID_M			GENMASK(7, 4)
556
557/* KSZ9893, KSZ9563, KSZ8563 specific register  */
558#define REG_CHIP_ID4			0x0f
559#define SKU_ID_KSZ8563			0x3c
560#define SKU_ID_KSZ9563			0x1c
561
562/* Driver set switch broadcast storm protection at 10% rate. */
563#define BROADCAST_STORM_PROT_RATE	10
564
565/* 148,800 frames * 67 ms / 100 */
566#define BROADCAST_STORM_VALUE		9969
567
568#define BROADCAST_STORM_RATE_HI		0x07
569#define BROADCAST_STORM_RATE_LO		0xFF
570#define BROADCAST_STORM_RATE		0x07FF
571
572#define MULTICAST_STORM_DISABLE		BIT(6)
573
574#define SW_START			0x01
575
576/* xMII configuration */
577#define P_MII_DUPLEX_M			BIT(6)
578#define P_MII_100MBIT_M			BIT(4)
579
580#define P_GMII_1GBIT_M			BIT(6)
581#define P_RGMII_ID_IG_ENABLE		BIT(4)
582#define P_RGMII_ID_EG_ENABLE		BIT(3)
583#define P_MII_MAC_MODE			BIT(2)
584#define P_MII_SEL_M			0x3
585
586/* Interrupt */
587#define REG_SW_PORT_INT_STATUS__1	0x001B
588#define REG_SW_PORT_INT_MASK__1		0x001F
589
590#define REG_PORT_INT_STATUS		0x001B
591#define REG_PORT_INT_MASK		0x001F
592
593#define PORT_SRC_PHY_INT		1
 
594
595#define KSZ8795_HUGE_PACKET_SIZE	2000
596#define KSZ8863_HUGE_PACKET_SIZE	1916
597#define KSZ8863_NORMAL_PACKET_SIZE	1536
598#define KSZ8_LEGAL_PACKET_SIZE		1518
599#define KSZ9477_MAX_FRAME_SIZE		9000
600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
601/* Regmap tables generation */
602#define KSZ_SPI_OP_RD		3
603#define KSZ_SPI_OP_WR		2
604
605#define swabnot_used(x)		0
606
607#define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
608	swab##swp((opcode) << ((regbits) + (regpad)))
609
610#define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
611	{								\
612		.name = #width,						\
613		.val_bits = (width),					\
614		.reg_stride = 1,					\
615		.reg_bits = (regbits) + (regalign),			\
616		.pad_bits = (regpad),					\
617		.max_register = BIT(regbits) - 1,			\
618		.cache_type = REGCACHE_NONE,				\
619		.read_flag_mask =					\
620			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
621					     regbits, regpad),		\
622		.write_flag_mask =					\
623			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
624					     regbits, regpad),		\
625		.lock = ksz_regmap_lock,				\
626		.unlock = ksz_regmap_unlock,				\
627		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
628		.val_format_endian = REGMAP_ENDIAN_BIG			\
629	}
630
631#define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
632	static const struct regmap_config ksz##_regmap_config[] = {	\
633		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
634		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
635		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
636	}
637
638#endif