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