]> Git Repo - linux.git/blob - drivers/net/dsa/microchip/ksz_common.h
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux.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 #define KSZ_MAX_NUM_PORTS 8
19
20 struct ksz_device;
21
22 struct vlan_table {
23         u32 table[3];
24 };
25
26 struct 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
35 struct ksz_mib_names {
36         int index;
37         char string[ETH_GSTRING_LEN];
38 };
39
40 struct 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         const struct ksz_dev_ops *ops;
49         bool phy_errata_9477;
50         bool ksz87xx_eee_link_erratum;
51         const struct ksz_mib_names *mib_names;
52         int mib_cnt;
53         u8 reg_mib_cnt;
54         const u16 *regs;
55         const u32 *masks;
56         const u8 *shifts;
57         const u8 *xmii_ctrl0;
58         const u8 *xmii_ctrl1;
59         int stp_ctrl_reg;
60         int broadcast_ctrl_reg;
61         int multicast_ctrl_reg;
62         int start_ctrl_reg;
63         bool supports_mii[KSZ_MAX_NUM_PORTS];
64         bool supports_rmii[KSZ_MAX_NUM_PORTS];
65         bool supports_rgmii[KSZ_MAX_NUM_PORTS];
66         bool internal_phy[KSZ_MAX_NUM_PORTS];
67         bool gbit_capable[KSZ_MAX_NUM_PORTS];
68         const struct regmap_access_table *wr_table;
69         const struct regmap_access_table *rd_table;
70 };
71
72 struct ksz_irq {
73         u16 masked;
74         struct irq_chip chip;
75         struct irq_domain *domain;
76         int nirqs;
77         char name[16];
78 };
79
80 struct ksz_port {
81         bool remove_tag;                /* Remove Tag flag set, for ksz8795 only */
82         bool learning;
83         int stp_state;
84         struct phy_device phydev;
85
86         u32 on:1;                       /* port is not disabled by hardware */
87         u32 fiber:1;                    /* port is fiber */
88         u32 force:1;
89         u32 read:1;                     /* read MIB counters in background */
90         u32 freeze:1;                   /* MIB counter freeze is enabled */
91
92         struct ksz_port_mib mib;
93         phy_interface_t interface;
94         u16 max_frame;
95         u32 rgmii_tx_val;
96         u32 rgmii_rx_val;
97         struct ksz_device *ksz_dev;
98         struct ksz_irq pirq;
99         u8 num;
100 };
101
102 struct ksz_device {
103         struct dsa_switch *ds;
104         struct ksz_platform_data *pdata;
105         const struct ksz_chip_data *info;
106
107         struct mutex dev_mutex;         /* device access */
108         struct mutex regmap_mutex;      /* regmap access */
109         struct mutex alu_mutex;         /* ALU access */
110         struct mutex vlan_mutex;        /* vlan access */
111         const struct ksz_dev_ops *dev_ops;
112
113         struct device *dev;
114         struct regmap *regmap[3];
115
116         void *priv;
117         int irq;
118
119         struct gpio_desc *reset_gpio;   /* Optional reset GPIO */
120
121         /* chip specific data */
122         u32 chip_id;
123         u8 chip_rev;
124         int cpu_port;                   /* port connected to CPU */
125         int phy_port_cnt;
126         phy_interface_t compat_interface;
127         bool synclko_125;
128         bool synclko_disable;
129
130         struct vlan_table *vlan_cache;
131
132         struct ksz_port *ports;
133         struct delayed_work mib_read;
134         unsigned long mib_read_interval;
135         u16 mirror_rx;
136         u16 mirror_tx;
137         u16 port_mask;
138         struct mutex lock_irq;          /* IRQ Access */
139         struct ksz_irq girq;
140 };
141
142 /* List of supported models */
143 enum ksz_model {
144         KSZ8563,
145         KSZ8795,
146         KSZ8794,
147         KSZ8765,
148         KSZ8830,
149         KSZ9477,
150         KSZ9896,
151         KSZ9897,
152         KSZ9893,
153         KSZ9567,
154         LAN9370,
155         LAN9371,
156         LAN9372,
157         LAN9373,
158         LAN9374,
159 };
160
161 enum ksz_chip_id {
162         KSZ8563_CHIP_ID = 0x8563,
163         KSZ8795_CHIP_ID = 0x8795,
164         KSZ8794_CHIP_ID = 0x8794,
165         KSZ8765_CHIP_ID = 0x8765,
166         KSZ8830_CHIP_ID = 0x8830,
167         KSZ9477_CHIP_ID = 0x00947700,
168         KSZ9896_CHIP_ID = 0x00989600,
169         KSZ9897_CHIP_ID = 0x00989700,
170         KSZ9893_CHIP_ID = 0x00989300,
171         KSZ9567_CHIP_ID = 0x00956700,
172         LAN9370_CHIP_ID = 0x00937000,
173         LAN9371_CHIP_ID = 0x00937100,
174         LAN9372_CHIP_ID = 0x00937200,
175         LAN9373_CHIP_ID = 0x00937300,
176         LAN9374_CHIP_ID = 0x00937400,
177 };
178
179 enum ksz_regs {
180         REG_IND_CTRL_0,
181         REG_IND_DATA_8,
182         REG_IND_DATA_CHECK,
183         REG_IND_DATA_HI,
184         REG_IND_DATA_LO,
185         REG_IND_MIB_CHECK,
186         REG_IND_BYTE,
187         P_FORCE_CTRL,
188         P_LINK_STATUS,
189         P_LOCAL_CTRL,
190         P_NEG_RESTART_CTRL,
191         P_REMOTE_STATUS,
192         P_SPEED_STATUS,
193         S_TAIL_TAG_CTRL,
194         P_STP_CTRL,
195         S_START_CTRL,
196         S_BROADCAST_CTRL,
197         S_MULTICAST_CTRL,
198         P_XMII_CTRL_0,
199         P_XMII_CTRL_1,
200 };
201
202 enum ksz_masks {
203         PORT_802_1P_REMAPPING,
204         SW_TAIL_TAG_ENABLE,
205         MIB_COUNTER_OVERFLOW,
206         MIB_COUNTER_VALID,
207         VLAN_TABLE_FID,
208         VLAN_TABLE_MEMBERSHIP,
209         VLAN_TABLE_VALID,
210         STATIC_MAC_TABLE_VALID,
211         STATIC_MAC_TABLE_USE_FID,
212         STATIC_MAC_TABLE_FID,
213         STATIC_MAC_TABLE_OVERRIDE,
214         STATIC_MAC_TABLE_FWD_PORTS,
215         DYNAMIC_MAC_TABLE_ENTRIES_H,
216         DYNAMIC_MAC_TABLE_MAC_EMPTY,
217         DYNAMIC_MAC_TABLE_NOT_READY,
218         DYNAMIC_MAC_TABLE_ENTRIES,
219         DYNAMIC_MAC_TABLE_FID,
220         DYNAMIC_MAC_TABLE_SRC_PORT,
221         DYNAMIC_MAC_TABLE_TIMESTAMP,
222         ALU_STAT_WRITE,
223         ALU_STAT_READ,
224         P_MII_TX_FLOW_CTRL,
225         P_MII_RX_FLOW_CTRL,
226 };
227
228 enum ksz_shifts {
229         VLAN_TABLE_MEMBERSHIP_S,
230         VLAN_TABLE,
231         STATIC_MAC_FWD_PORTS,
232         STATIC_MAC_FID,
233         DYNAMIC_MAC_ENTRIES_H,
234         DYNAMIC_MAC_ENTRIES,
235         DYNAMIC_MAC_FID,
236         DYNAMIC_MAC_TIMESTAMP,
237         DYNAMIC_MAC_SRC_PORT,
238         ALU_STAT_INDEX,
239 };
240
241 enum ksz_xmii_ctrl0 {
242         P_MII_100MBIT,
243         P_MII_10MBIT,
244         P_MII_FULL_DUPLEX,
245         P_MII_HALF_DUPLEX,
246 };
247
248 enum ksz_xmii_ctrl1 {
249         P_RGMII_SEL,
250         P_RMII_SEL,
251         P_GMII_SEL,
252         P_MII_SEL,
253         P_GMII_1GBIT,
254         P_GMII_NOT_1GBIT,
255 };
256
257 struct alu_struct {
258         /* entry 1 */
259         u8      is_static:1;
260         u8      is_src_filter:1;
261         u8      is_dst_filter:1;
262         u8      prio_age:3;
263         u32     _reserv_0_1:23;
264         u8      mstp:3;
265         /* entry 2 */
266         u8      is_override:1;
267         u8      is_use_fid:1;
268         u32     _reserv_1_1:23;
269         u8      port_forward:7;
270         /* entry 3 & 4*/
271         u32     _reserv_2_1:9;
272         u8      fid:7;
273         u8      mac[ETH_ALEN];
274 };
275
276 struct ksz_dev_ops {
277         int (*setup)(struct dsa_switch *ds);
278         void (*teardown)(struct dsa_switch *ds);
279         u32 (*get_port_addr)(int port, int offset);
280         void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
281         void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
282         void (*port_cleanup)(struct ksz_device *dev, int port);
283         void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
284         int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
285         int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
286         void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
287                           u64 *cnt);
288         void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
289                           u64 *dropped, u64 *cnt);
290         void (*r_mib_stat64)(struct ksz_device *dev, int port);
291         int  (*vlan_filtering)(struct ksz_device *dev, int port,
292                                bool flag, struct netlink_ext_ack *extack);
293         int  (*vlan_add)(struct ksz_device *dev, int port,
294                          const struct switchdev_obj_port_vlan *vlan,
295                          struct netlink_ext_ack *extack);
296         int  (*vlan_del)(struct ksz_device *dev, int port,
297                          const struct switchdev_obj_port_vlan *vlan);
298         int (*mirror_add)(struct ksz_device *dev, int port,
299                           struct dsa_mall_mirror_tc_entry *mirror,
300                           bool ingress, struct netlink_ext_ack *extack);
301         void (*mirror_del)(struct ksz_device *dev, int port,
302                            struct dsa_mall_mirror_tc_entry *mirror);
303         int (*fdb_add)(struct ksz_device *dev, int port,
304                        const unsigned char *addr, u16 vid, struct dsa_db db);
305         int (*fdb_del)(struct ksz_device *dev, int port,
306                        const unsigned char *addr, u16 vid, struct dsa_db db);
307         int (*fdb_dump)(struct ksz_device *dev, int port,
308                         dsa_fdb_dump_cb_t *cb, void *data);
309         int (*mdb_add)(struct ksz_device *dev, int port,
310                        const struct switchdev_obj_port_mdb *mdb,
311                        struct dsa_db db);
312         int (*mdb_del)(struct ksz_device *dev, int port,
313                        const struct switchdev_obj_port_mdb *mdb,
314                        struct dsa_db db);
315         void (*get_caps)(struct ksz_device *dev, int port,
316                          struct phylink_config *config);
317         int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
318         int (*max_mtu)(struct ksz_device *dev, int port);
319         void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
320         void (*port_init_cnt)(struct ksz_device *dev, int port);
321         void (*phylink_mac_config)(struct ksz_device *dev, int port,
322                                    unsigned int mode,
323                                    const struct phylink_link_state *state);
324         void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
325                                     unsigned int mode,
326                                     phy_interface_t interface,
327                                     struct phy_device *phydev, int speed,
328                                     int duplex, bool tx_pause, bool rx_pause);
329         void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
330         void (*config_cpu_port)(struct dsa_switch *ds);
331         int (*enable_stp_addr)(struct ksz_device *dev);
332         int (*reset)(struct ksz_device *dev);
333         int (*init)(struct ksz_device *dev);
334         void (*exit)(struct ksz_device *dev);
335 };
336
337 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
338 int ksz_switch_register(struct ksz_device *dev);
339 void ksz_switch_remove(struct ksz_device *dev);
340
341 void ksz_init_mib_timer(struct ksz_device *dev);
342 void ksz_r_mib_stats64(struct ksz_device *dev, int port);
343 void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
344 bool ksz_get_gbit(struct ksz_device *dev, int port);
345 phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
346 extern const struct ksz_chip_data ksz_switch_chips[];
347
348 /* Common register access functions */
349
350 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
351 {
352         unsigned int value;
353         int ret = regmap_read(dev->regmap[0], reg, &value);
354
355         if (ret)
356                 dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
357                         ERR_PTR(ret));
358
359         *val = value;
360         return ret;
361 }
362
363 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
364 {
365         unsigned int value;
366         int ret = regmap_read(dev->regmap[1], reg, &value);
367
368         if (ret)
369                 dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
370                         ERR_PTR(ret));
371
372         *val = value;
373         return ret;
374 }
375
376 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
377 {
378         unsigned int value;
379         int ret = regmap_read(dev->regmap[2], reg, &value);
380
381         if (ret)
382                 dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
383                         ERR_PTR(ret));
384
385         *val = value;
386         return ret;
387 }
388
389 static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
390 {
391         u32 value[2];
392         int ret;
393
394         ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
395         if (ret)
396                 dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
397                         ERR_PTR(ret));
398         else
399                 *val = (u64)value[0] << 32 | value[1];
400
401         return ret;
402 }
403
404 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
405 {
406         int ret;
407
408         ret = regmap_write(dev->regmap[0], reg, value);
409         if (ret)
410                 dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
411                         ERR_PTR(ret));
412
413         return ret;
414 }
415
416 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
417 {
418         int ret;
419
420         ret = regmap_write(dev->regmap[1], reg, value);
421         if (ret)
422                 dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
423                         ERR_PTR(ret));
424
425         return ret;
426 }
427
428 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
429 {
430         int ret;
431
432         ret = regmap_write(dev->regmap[2], reg, value);
433         if (ret)
434                 dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
435                         ERR_PTR(ret));
436
437         return ret;
438 }
439
440 static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
441 {
442         u32 val[2];
443
444         /* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
445         value = swab64(value);
446         val[0] = swab32(value & 0xffffffffULL);
447         val[1] = swab32(value >> 32ULL);
448
449         return regmap_bulk_write(dev->regmap[2], reg, val, 2);
450 }
451
452 static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
453                              u8 *data)
454 {
455         return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
456 }
457
458 static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
459                               u16 *data)
460 {
461         return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
462 }
463
464 static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
465                               u32 *data)
466 {
467         return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
468 }
469
470 static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
471                               u8 data)
472 {
473         return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
474 }
475
476 static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
477                                u16 data)
478 {
479         return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
480                            data);
481 }
482
483 static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
484                                u32 data)
485 {
486         return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
487                            data);
488 }
489
490 static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
491                              u8 mask, u8 val)
492 {
493         regmap_update_bits(dev->regmap[0],
494                            dev->dev_ops->get_port_addr(port, offset),
495                            mask, val);
496 }
497
498 static inline void ksz_regmap_lock(void *__mtx)
499 {
500         struct mutex *mtx = __mtx;
501         mutex_lock(mtx);
502 }
503
504 static inline void ksz_regmap_unlock(void *__mtx)
505 {
506         struct mutex *mtx = __mtx;
507         mutex_unlock(mtx);
508 }
509
510 static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
511 {
512         return dev->chip_id == KSZ8830_CHIP_ID;
513 }
514
515 static inline int is_lan937x(struct ksz_device *dev)
516 {
517         return dev->chip_id == LAN9370_CHIP_ID ||
518                 dev->chip_id == LAN9371_CHIP_ID ||
519                 dev->chip_id == LAN9372_CHIP_ID ||
520                 dev->chip_id == LAN9373_CHIP_ID ||
521                 dev->chip_id == LAN9374_CHIP_ID;
522 }
523
524 /* STP State Defines */
525 #define PORT_TX_ENABLE                  BIT(2)
526 #define PORT_RX_ENABLE                  BIT(1)
527 #define PORT_LEARN_DISABLE              BIT(0)
528
529 /* Switch ID Defines */
530 #define REG_CHIP_ID0                    0x00
531
532 #define SW_FAMILY_ID_M                  GENMASK(15, 8)
533 #define KSZ87_FAMILY_ID                 0x87
534 #define KSZ88_FAMILY_ID                 0x88
535
536 #define KSZ8_PORT_STATUS_0              0x08
537 #define KSZ8_PORT_FIBER_MODE            BIT(7)
538
539 #define SW_CHIP_ID_M                    GENMASK(7, 4)
540 #define KSZ87_CHIP_ID_94                0x6
541 #define KSZ87_CHIP_ID_95                0x9
542 #define KSZ88_CHIP_ID_63                0x3
543
544 #define SW_REV_ID_M                     GENMASK(7, 4)
545
546 /* KSZ9893, KSZ9563, KSZ8563 specific register  */
547 #define REG_CHIP_ID4                    0x0f
548 #define SKU_ID_KSZ8563                  0x3c
549
550 /* Driver set switch broadcast storm protection at 10% rate. */
551 #define BROADCAST_STORM_PROT_RATE       10
552
553 /* 148,800 frames * 67 ms / 100 */
554 #define BROADCAST_STORM_VALUE           9969
555
556 #define BROADCAST_STORM_RATE_HI         0x07
557 #define BROADCAST_STORM_RATE_LO         0xFF
558 #define BROADCAST_STORM_RATE            0x07FF
559
560 #define MULTICAST_STORM_DISABLE         BIT(6)
561
562 #define SW_START                        0x01
563
564 /* xMII configuration */
565 #define P_MII_DUPLEX_M                  BIT(6)
566 #define P_MII_100MBIT_M                 BIT(4)
567
568 #define P_GMII_1GBIT_M                  BIT(6)
569 #define P_RGMII_ID_IG_ENABLE            BIT(4)
570 #define P_RGMII_ID_EG_ENABLE            BIT(3)
571 #define P_MII_MAC_MODE                  BIT(2)
572 #define P_MII_SEL_M                     0x3
573
574 /* Regmap tables generation */
575 #define KSZ_SPI_OP_RD           3
576 #define KSZ_SPI_OP_WR           2
577
578 #define swabnot_used(x)         0
579
580 #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)              \
581         swab##swp((opcode) << ((regbits) + (regpad)))
582
583 #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)         \
584         {                                                               \
585                 .name = #width,                                         \
586                 .val_bits = (width),                                    \
587                 .reg_stride = 1,                                        \
588                 .reg_bits = (regbits) + (regalign),                     \
589                 .pad_bits = (regpad),                                   \
590                 .max_register = BIT(regbits) - 1,                       \
591                 .cache_type = REGCACHE_NONE,                            \
592                 .read_flag_mask =                                       \
593                         KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,        \
594                                              regbits, regpad),          \
595                 .write_flag_mask =                                      \
596                         KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,        \
597                                              regbits, regpad),          \
598                 .lock = ksz_regmap_lock,                                \
599                 .unlock = ksz_regmap_unlock,                            \
600                 .reg_format_endian = REGMAP_ENDIAN_BIG,                 \
601                 .val_format_endian = REGMAP_ENDIAN_BIG                  \
602         }
603
604 #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)           \
605         static const struct regmap_config ksz##_regmap_config[] = {     \
606                 KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
607                 KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
608                 KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
609         }
610
611 #endif
This page took 0.071989 seconds and 4 git commands to generate.