net: dsa: rzn1-a5psw: fix STP states handling
[platform/kernel/linux-rpi.git] / drivers / net / dsa / rzn1_a5psw.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 Schneider-Electric
4  *
5  * Clément Léger <clement.leger@bootlin.com>
6  */
7
8 #include <linux/clk.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_bridge.h>
11 #include <linux/if_ether.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_mdio.h>
16 #include <net/dsa.h>
17
18 #include "rzn1_a5psw.h"
19
20 struct a5psw_stats {
21         u16 offset;
22         const char name[ETH_GSTRING_LEN];
23 };
24
25 #define STAT_DESC(_offset) {    \
26         .offset = A5PSW_##_offset,      \
27         .name = __stringify(_offset),   \
28 }
29
30 static const struct a5psw_stats a5psw_stats[] = {
31         STAT_DESC(aFramesTransmittedOK),
32         STAT_DESC(aFramesReceivedOK),
33         STAT_DESC(aFrameCheckSequenceErrors),
34         STAT_DESC(aAlignmentErrors),
35         STAT_DESC(aOctetsTransmittedOK),
36         STAT_DESC(aOctetsReceivedOK),
37         STAT_DESC(aTxPAUSEMACCtrlFrames),
38         STAT_DESC(aRxPAUSEMACCtrlFrames),
39         STAT_DESC(ifInErrors),
40         STAT_DESC(ifOutErrors),
41         STAT_DESC(ifInUcastPkts),
42         STAT_DESC(ifInMulticastPkts),
43         STAT_DESC(ifInBroadcastPkts),
44         STAT_DESC(ifOutDiscards),
45         STAT_DESC(ifOutUcastPkts),
46         STAT_DESC(ifOutMulticastPkts),
47         STAT_DESC(ifOutBroadcastPkts),
48         STAT_DESC(etherStatsDropEvents),
49         STAT_DESC(etherStatsOctets),
50         STAT_DESC(etherStatsPkts),
51         STAT_DESC(etherStatsUndersizePkts),
52         STAT_DESC(etherStatsOversizePkts),
53         STAT_DESC(etherStatsPkts64Octets),
54         STAT_DESC(etherStatsPkts65to127Octets),
55         STAT_DESC(etherStatsPkts128to255Octets),
56         STAT_DESC(etherStatsPkts256to511Octets),
57         STAT_DESC(etherStatsPkts1024to1518Octets),
58         STAT_DESC(etherStatsPkts1519toXOctets),
59         STAT_DESC(etherStatsJabbers),
60         STAT_DESC(etherStatsFragments),
61         STAT_DESC(VLANReceived),
62         STAT_DESC(VLANTransmitted),
63         STAT_DESC(aDeferred),
64         STAT_DESC(aMultipleCollisions),
65         STAT_DESC(aSingleCollisions),
66         STAT_DESC(aLateCollisions),
67         STAT_DESC(aExcessiveCollisions),
68         STAT_DESC(aCarrierSenseErrors),
69 };
70
71 static void a5psw_reg_writel(struct a5psw *a5psw, int offset, u32 value)
72 {
73         writel(value, a5psw->base + offset);
74 }
75
76 static u32 a5psw_reg_readl(struct a5psw *a5psw, int offset)
77 {
78         return readl(a5psw->base + offset);
79 }
80
81 static void a5psw_reg_rmw(struct a5psw *a5psw, int offset, u32 mask, u32 val)
82 {
83         u32 reg;
84
85         spin_lock(&a5psw->reg_lock);
86
87         reg = a5psw_reg_readl(a5psw, offset);
88         reg &= ~mask;
89         reg |= val;
90         a5psw_reg_writel(a5psw, offset, reg);
91
92         spin_unlock(&a5psw->reg_lock);
93 }
94
95 static enum dsa_tag_protocol a5psw_get_tag_protocol(struct dsa_switch *ds,
96                                                     int port,
97                                                     enum dsa_tag_protocol mp)
98 {
99         return DSA_TAG_PROTO_RZN1_A5PSW;
100 }
101
102 static void a5psw_port_pattern_set(struct a5psw *a5psw, int port, int pattern,
103                                    bool enable)
104 {
105         u32 rx_match = 0;
106
107         if (enable)
108                 rx_match |= A5PSW_RXMATCH_CONFIG_PATTERN(pattern);
109
110         a5psw_reg_rmw(a5psw, A5PSW_RXMATCH_CONFIG(port),
111                       A5PSW_RXMATCH_CONFIG_PATTERN(pattern), rx_match);
112 }
113
114 static void a5psw_port_mgmtfwd_set(struct a5psw *a5psw, int port, bool enable)
115 {
116         /* Enable "management forward" pattern matching, this will forward
117          * packets from this port only towards the management port and thus
118          * isolate the port.
119          */
120         a5psw_port_pattern_set(a5psw, port, A5PSW_PATTERN_MGMTFWD, enable);
121 }
122
123 static void a5psw_port_tx_enable(struct a5psw *a5psw, int port, bool enable)
124 {
125         u32 mask = A5PSW_PORT_ENA_TX(port);
126         u32 reg = enable ? mask : 0;
127
128         /* Even though the port TX is disabled through TXENA bit in the
129          * PORT_ENA register, it can still send BPDUs. This depends on the tag
130          * configuration added when sending packets from the CPU port to the
131          * switch port. Indeed, when using forced forwarding without filtering,
132          * even disabled ports will be able to send packets that are tagged.
133          * This allows to implement STP support when ports are in a state where
134          * forwarding traffic should be stopped but BPDUs should still be sent.
135          */
136         a5psw_reg_rmw(a5psw, A5PSW_PORT_ENA, mask, reg);
137 }
138
139 static void a5psw_port_enable_set(struct a5psw *a5psw, int port, bool enable)
140 {
141         u32 port_ena = 0;
142
143         if (enable)
144                 port_ena |= A5PSW_PORT_ENA_TX_RX(port);
145
146         a5psw_reg_rmw(a5psw, A5PSW_PORT_ENA, A5PSW_PORT_ENA_TX_RX(port),
147                       port_ena);
148 }
149
150 static int a5psw_lk_execute_ctrl(struct a5psw *a5psw, u32 *ctrl)
151 {
152         int ret;
153
154         a5psw_reg_writel(a5psw, A5PSW_LK_ADDR_CTRL, *ctrl);
155
156         ret = readl_poll_timeout(a5psw->base + A5PSW_LK_ADDR_CTRL, *ctrl,
157                                  !(*ctrl & A5PSW_LK_ADDR_CTRL_BUSY),
158                                  A5PSW_LK_BUSY_USEC_POLL, A5PSW_CTRL_TIMEOUT);
159         if (ret)
160                 dev_err(a5psw->dev, "LK_CTRL timeout waiting for BUSY bit\n");
161
162         return ret;
163 }
164
165 static void a5psw_port_fdb_flush(struct a5psw *a5psw, int port)
166 {
167         u32 ctrl = A5PSW_LK_ADDR_CTRL_DELETE_PORT | BIT(port);
168
169         mutex_lock(&a5psw->lk_lock);
170         a5psw_lk_execute_ctrl(a5psw, &ctrl);
171         mutex_unlock(&a5psw->lk_lock);
172 }
173
174 static void a5psw_port_authorize_set(struct a5psw *a5psw, int port,
175                                      bool authorize)
176 {
177         u32 reg = a5psw_reg_readl(a5psw, A5PSW_AUTH_PORT(port));
178
179         if (authorize)
180                 reg |= A5PSW_AUTH_PORT_AUTHORIZED;
181         else
182                 reg &= ~A5PSW_AUTH_PORT_AUTHORIZED;
183
184         a5psw_reg_writel(a5psw, A5PSW_AUTH_PORT(port), reg);
185 }
186
187 static void a5psw_port_disable(struct dsa_switch *ds, int port)
188 {
189         struct a5psw *a5psw = ds->priv;
190
191         a5psw_port_authorize_set(a5psw, port, false);
192         a5psw_port_enable_set(a5psw, port, false);
193 }
194
195 static int a5psw_port_enable(struct dsa_switch *ds, int port,
196                              struct phy_device *phy)
197 {
198         struct a5psw *a5psw = ds->priv;
199
200         a5psw_port_authorize_set(a5psw, port, true);
201         a5psw_port_enable_set(a5psw, port, true);
202
203         return 0;
204 }
205
206 static int a5psw_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
207 {
208         struct a5psw *a5psw = ds->priv;
209
210         new_mtu += ETH_HLEN + A5PSW_EXTRA_MTU_LEN + ETH_FCS_LEN;
211         a5psw_reg_writel(a5psw, A5PSW_FRM_LENGTH(port), new_mtu);
212
213         return 0;
214 }
215
216 static int a5psw_port_max_mtu(struct dsa_switch *ds, int port)
217 {
218         return A5PSW_MAX_MTU;
219 }
220
221 static void a5psw_phylink_get_caps(struct dsa_switch *ds, int port,
222                                    struct phylink_config *config)
223 {
224         unsigned long *intf = config->supported_interfaces;
225
226         config->mac_capabilities = MAC_1000FD;
227
228         if (dsa_is_cpu_port(ds, port)) {
229                 /* GMII is used internally and GMAC2 is connected to the switch
230                  * using 1000Mbps Full-Duplex mode only (cf ethernet manual)
231                  */
232                 __set_bit(PHY_INTERFACE_MODE_GMII, intf);
233         } else {
234                 config->mac_capabilities |= MAC_100 | MAC_10;
235                 phy_interface_set_rgmii(intf);
236                 __set_bit(PHY_INTERFACE_MODE_RMII, intf);
237                 __set_bit(PHY_INTERFACE_MODE_MII, intf);
238         }
239 }
240
241 static struct phylink_pcs *
242 a5psw_phylink_mac_select_pcs(struct dsa_switch *ds, int port,
243                              phy_interface_t interface)
244 {
245         struct dsa_port *dp = dsa_to_port(ds, port);
246         struct a5psw *a5psw = ds->priv;
247
248         if (!dsa_port_is_cpu(dp) && a5psw->pcs[port])
249                 return a5psw->pcs[port];
250
251         return NULL;
252 }
253
254 static void a5psw_phylink_mac_link_down(struct dsa_switch *ds, int port,
255                                         unsigned int mode,
256                                         phy_interface_t interface)
257 {
258         struct a5psw *a5psw = ds->priv;
259         u32 cmd_cfg;
260
261         cmd_cfg = a5psw_reg_readl(a5psw, A5PSW_CMD_CFG(port));
262         cmd_cfg &= ~(A5PSW_CMD_CFG_RX_ENA | A5PSW_CMD_CFG_TX_ENA);
263         a5psw_reg_writel(a5psw, A5PSW_CMD_CFG(port), cmd_cfg);
264 }
265
266 static void a5psw_phylink_mac_link_up(struct dsa_switch *ds, int port,
267                                       unsigned int mode,
268                                       phy_interface_t interface,
269                                       struct phy_device *phydev, int speed,
270                                       int duplex, bool tx_pause, bool rx_pause)
271 {
272         u32 cmd_cfg = A5PSW_CMD_CFG_RX_ENA | A5PSW_CMD_CFG_TX_ENA |
273                       A5PSW_CMD_CFG_TX_CRC_APPEND;
274         struct a5psw *a5psw = ds->priv;
275
276         if (speed == SPEED_1000)
277                 cmd_cfg |= A5PSW_CMD_CFG_ETH_SPEED;
278
279         if (duplex == DUPLEX_HALF)
280                 cmd_cfg |= A5PSW_CMD_CFG_HD_ENA;
281
282         cmd_cfg |= A5PSW_CMD_CFG_CNTL_FRM_ENA;
283
284         if (!rx_pause)
285                 cmd_cfg &= ~A5PSW_CMD_CFG_PAUSE_IGNORE;
286
287         a5psw_reg_writel(a5psw, A5PSW_CMD_CFG(port), cmd_cfg);
288 }
289
290 static int a5psw_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
291 {
292         struct a5psw *a5psw = ds->priv;
293         unsigned long rate;
294         u64 max, tmp;
295         u32 agetime;
296
297         rate = clk_get_rate(a5psw->clk);
298         max = div64_ul(((u64)A5PSW_LK_AGETIME_MASK * A5PSW_TABLE_ENTRIES * 1024),
299                        rate) * 1000;
300         if (msecs > max)
301                 return -EINVAL;
302
303         tmp = div_u64(rate, MSEC_PER_SEC);
304         agetime = div_u64(msecs * tmp, 1024 * A5PSW_TABLE_ENTRIES);
305
306         a5psw_reg_writel(a5psw, A5PSW_LK_AGETIME, agetime);
307
308         return 0;
309 }
310
311 static void a5psw_port_learning_set(struct a5psw *a5psw, int port, bool learn)
312 {
313         u32 mask = A5PSW_INPUT_LEARN_DIS(port);
314         u32 reg = !learn ? mask : 0;
315
316         a5psw_reg_rmw(a5psw, A5PSW_INPUT_LEARN, mask, reg);
317 }
318
319 static void a5psw_port_rx_block_set(struct a5psw *a5psw, int port, bool block)
320 {
321         u32 mask = A5PSW_INPUT_LEARN_BLOCK(port);
322         u32 reg = block ? mask : 0;
323
324         a5psw_reg_rmw(a5psw, A5PSW_INPUT_LEARN, mask, reg);
325 }
326
327 static void a5psw_flooding_set_resolution(struct a5psw *a5psw, int port,
328                                           bool set)
329 {
330         u8 offsets[] = {A5PSW_UCAST_DEF_MASK, A5PSW_BCAST_DEF_MASK,
331                         A5PSW_MCAST_DEF_MASK};
332         int i;
333
334         if (set)
335                 a5psw->bridged_ports |= BIT(port);
336         else
337                 a5psw->bridged_ports &= ~BIT(port);
338
339         for (i = 0; i < ARRAY_SIZE(offsets); i++)
340                 a5psw_reg_writel(a5psw, offsets[i], a5psw->bridged_ports);
341 }
342
343 static int a5psw_port_bridge_join(struct dsa_switch *ds, int port,
344                                   struct dsa_bridge bridge,
345                                   bool *tx_fwd_offload,
346                                   struct netlink_ext_ack *extack)
347 {
348         struct a5psw *a5psw = ds->priv;
349
350         /* We only support 1 bridge device */
351         if (a5psw->br_dev && bridge.dev != a5psw->br_dev) {
352                 NL_SET_ERR_MSG_MOD(extack,
353                                    "Forwarding offload supported for a single bridge");
354                 return -EOPNOTSUPP;
355         }
356
357         a5psw->br_dev = bridge.dev;
358         a5psw_flooding_set_resolution(a5psw, port, true);
359         a5psw_port_mgmtfwd_set(a5psw, port, false);
360
361         return 0;
362 }
363
364 static void a5psw_port_bridge_leave(struct dsa_switch *ds, int port,
365                                     struct dsa_bridge bridge)
366 {
367         struct a5psw *a5psw = ds->priv;
368
369         a5psw_flooding_set_resolution(a5psw, port, false);
370         a5psw_port_mgmtfwd_set(a5psw, port, true);
371
372         /* No more ports bridged */
373         if (a5psw->bridged_ports == BIT(A5PSW_CPU_PORT))
374                 a5psw->br_dev = NULL;
375 }
376
377 static void a5psw_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
378 {
379         bool learning_enabled, rx_enabled, tx_enabled;
380         struct a5psw *a5psw = ds->priv;
381
382         switch (state) {
383         case BR_STATE_DISABLED:
384         case BR_STATE_BLOCKING:
385         case BR_STATE_LISTENING:
386                 rx_enabled = false;
387                 tx_enabled = false;
388                 learning_enabled = false;
389                 break;
390         case BR_STATE_LEARNING:
391                 rx_enabled = false;
392                 tx_enabled = false;
393                 learning_enabled = true;
394                 break;
395         case BR_STATE_FORWARDING:
396                 rx_enabled = true;
397                 tx_enabled = true;
398                 learning_enabled = true;
399                 break;
400         default:
401                 dev_err(ds->dev, "invalid STP state: %d\n", state);
402                 return;
403         }
404
405         a5psw_port_learning_set(a5psw, port, learning_enabled);
406         a5psw_port_rx_block_set(a5psw, port, !rx_enabled);
407         a5psw_port_tx_enable(a5psw, port, tx_enabled);
408 }
409
410 static void a5psw_port_fast_age(struct dsa_switch *ds, int port)
411 {
412         struct a5psw *a5psw = ds->priv;
413
414         a5psw_port_fdb_flush(a5psw, port);
415 }
416
417 static int a5psw_lk_execute_lookup(struct a5psw *a5psw, union lk_data *lk_data,
418                                    u16 *entry)
419 {
420         u32 ctrl;
421         int ret;
422
423         a5psw_reg_writel(a5psw, A5PSW_LK_DATA_LO, lk_data->lo);
424         a5psw_reg_writel(a5psw, A5PSW_LK_DATA_HI, lk_data->hi);
425
426         ctrl = A5PSW_LK_ADDR_CTRL_LOOKUP;
427         ret = a5psw_lk_execute_ctrl(a5psw, &ctrl);
428         if (ret)
429                 return ret;
430
431         *entry = ctrl & A5PSW_LK_ADDR_CTRL_ADDRESS;
432
433         return 0;
434 }
435
436 static int a5psw_port_fdb_add(struct dsa_switch *ds, int port,
437                               const unsigned char *addr, u16 vid,
438                               struct dsa_db db)
439 {
440         struct a5psw *a5psw = ds->priv;
441         union lk_data lk_data = {0};
442         bool inc_learncount = false;
443         int ret = 0;
444         u16 entry;
445         u32 reg;
446
447         ether_addr_copy(lk_data.entry.mac, addr);
448         lk_data.entry.port_mask = BIT(port);
449
450         mutex_lock(&a5psw->lk_lock);
451
452         /* Set the value to be written in the lookup table */
453         ret = a5psw_lk_execute_lookup(a5psw, &lk_data, &entry);
454         if (ret)
455                 goto lk_unlock;
456
457         lk_data.hi = a5psw_reg_readl(a5psw, A5PSW_LK_DATA_HI);
458         if (!lk_data.entry.valid) {
459                 inc_learncount = true;
460                 /* port_mask set to 0x1f when entry is not valid, clear it */
461                 lk_data.entry.port_mask = 0;
462                 lk_data.entry.prio = 0;
463         }
464
465         lk_data.entry.port_mask |= BIT(port);
466         lk_data.entry.is_static = 1;
467         lk_data.entry.valid = 1;
468
469         a5psw_reg_writel(a5psw, A5PSW_LK_DATA_HI, lk_data.hi);
470
471         reg = A5PSW_LK_ADDR_CTRL_WRITE | entry;
472         ret = a5psw_lk_execute_ctrl(a5psw, &reg);
473         if (ret)
474                 goto lk_unlock;
475
476         if (inc_learncount) {
477                 reg = A5PSW_LK_LEARNCOUNT_MODE_INC;
478                 a5psw_reg_writel(a5psw, A5PSW_LK_LEARNCOUNT, reg);
479         }
480
481 lk_unlock:
482         mutex_unlock(&a5psw->lk_lock);
483
484         return ret;
485 }
486
487 static int a5psw_port_fdb_del(struct dsa_switch *ds, int port,
488                               const unsigned char *addr, u16 vid,
489                               struct dsa_db db)
490 {
491         struct a5psw *a5psw = ds->priv;
492         union lk_data lk_data = {0};
493         bool clear = false;
494         u16 entry;
495         u32 reg;
496         int ret;
497
498         ether_addr_copy(lk_data.entry.mac, addr);
499
500         mutex_lock(&a5psw->lk_lock);
501
502         ret = a5psw_lk_execute_lookup(a5psw, &lk_data, &entry);
503         if (ret)
504                 goto lk_unlock;
505
506         lk_data.hi = a5psw_reg_readl(a5psw, A5PSW_LK_DATA_HI);
507
508         /* Our hardware does not associate any VID to the FDB entries so this
509          * means that if two entries were added for the same mac but for
510          * different VID, then, on the deletion of the first one, we would also
511          * delete the second one. Since there is unfortunately nothing we can do
512          * about that, do not return an error...
513          */
514         if (!lk_data.entry.valid)
515                 goto lk_unlock;
516
517         lk_data.entry.port_mask &= ~BIT(port);
518         /* If there is no more port in the mask, clear the entry */
519         if (lk_data.entry.port_mask == 0)
520                 clear = true;
521
522         a5psw_reg_writel(a5psw, A5PSW_LK_DATA_HI, lk_data.hi);
523
524         reg = entry;
525         if (clear)
526                 reg |= A5PSW_LK_ADDR_CTRL_CLEAR;
527         else
528                 reg |= A5PSW_LK_ADDR_CTRL_WRITE;
529
530         ret = a5psw_lk_execute_ctrl(a5psw, &reg);
531         if (ret)
532                 goto lk_unlock;
533
534         /* Decrement LEARNCOUNT */
535         if (clear) {
536                 reg = A5PSW_LK_LEARNCOUNT_MODE_DEC;
537                 a5psw_reg_writel(a5psw, A5PSW_LK_LEARNCOUNT, reg);
538         }
539
540 lk_unlock:
541         mutex_unlock(&a5psw->lk_lock);
542
543         return ret;
544 }
545
546 static int a5psw_port_fdb_dump(struct dsa_switch *ds, int port,
547                                dsa_fdb_dump_cb_t *cb, void *data)
548 {
549         struct a5psw *a5psw = ds->priv;
550         union lk_data lk_data;
551         int i = 0, ret = 0;
552         u32 reg;
553
554         mutex_lock(&a5psw->lk_lock);
555
556         for (i = 0; i < A5PSW_TABLE_ENTRIES; i++) {
557                 reg = A5PSW_LK_ADDR_CTRL_READ | A5PSW_LK_ADDR_CTRL_WAIT | i;
558
559                 ret = a5psw_lk_execute_ctrl(a5psw, &reg);
560                 if (ret)
561                         goto out_unlock;
562
563                 lk_data.hi = a5psw_reg_readl(a5psw, A5PSW_LK_DATA_HI);
564                 /* If entry is not valid or does not contain the port, skip */
565                 if (!lk_data.entry.valid ||
566                     !(lk_data.entry.port_mask & BIT(port)))
567                         continue;
568
569                 lk_data.lo = a5psw_reg_readl(a5psw, A5PSW_LK_DATA_LO);
570
571                 ret = cb(lk_data.entry.mac, 0, lk_data.entry.is_static, data);
572                 if (ret)
573                         goto out_unlock;
574         }
575
576 out_unlock:
577         mutex_unlock(&a5psw->lk_lock);
578
579         return ret;
580 }
581
582 static u64 a5psw_read_stat(struct a5psw *a5psw, u32 offset, int port)
583 {
584         u32 reg_lo, reg_hi;
585
586         reg_lo = a5psw_reg_readl(a5psw, offset + A5PSW_PORT_OFFSET(port));
587         /* A5PSW_STATS_HIWORD is latched on stat read */
588         reg_hi = a5psw_reg_readl(a5psw, A5PSW_STATS_HIWORD);
589
590         return ((u64)reg_hi << 32) | reg_lo;
591 }
592
593 static void a5psw_get_strings(struct dsa_switch *ds, int port, u32 stringset,
594                               uint8_t *data)
595 {
596         unsigned int u;
597
598         if (stringset != ETH_SS_STATS)
599                 return;
600
601         for (u = 0; u < ARRAY_SIZE(a5psw_stats); u++) {
602                 memcpy(data + u * ETH_GSTRING_LEN, a5psw_stats[u].name,
603                        ETH_GSTRING_LEN);
604         }
605 }
606
607 static void a5psw_get_ethtool_stats(struct dsa_switch *ds, int port,
608                                     uint64_t *data)
609 {
610         struct a5psw *a5psw = ds->priv;
611         unsigned int u;
612
613         for (u = 0; u < ARRAY_SIZE(a5psw_stats); u++)
614                 data[u] = a5psw_read_stat(a5psw, a5psw_stats[u].offset, port);
615 }
616
617 static int a5psw_get_sset_count(struct dsa_switch *ds, int port, int sset)
618 {
619         if (sset != ETH_SS_STATS)
620                 return 0;
621
622         return ARRAY_SIZE(a5psw_stats);
623 }
624
625 static void a5psw_get_eth_mac_stats(struct dsa_switch *ds, int port,
626                                     struct ethtool_eth_mac_stats *mac_stats)
627 {
628         struct a5psw *a5psw = ds->priv;
629
630 #define RD(name) a5psw_read_stat(a5psw, A5PSW_##name, port)
631         mac_stats->FramesTransmittedOK = RD(aFramesTransmittedOK);
632         mac_stats->SingleCollisionFrames = RD(aSingleCollisions);
633         mac_stats->MultipleCollisionFrames = RD(aMultipleCollisions);
634         mac_stats->FramesReceivedOK = RD(aFramesReceivedOK);
635         mac_stats->FrameCheckSequenceErrors = RD(aFrameCheckSequenceErrors);
636         mac_stats->AlignmentErrors = RD(aAlignmentErrors);
637         mac_stats->OctetsTransmittedOK = RD(aOctetsTransmittedOK);
638         mac_stats->FramesWithDeferredXmissions = RD(aDeferred);
639         mac_stats->LateCollisions = RD(aLateCollisions);
640         mac_stats->FramesAbortedDueToXSColls = RD(aExcessiveCollisions);
641         mac_stats->FramesLostDueToIntMACXmitError = RD(ifOutErrors);
642         mac_stats->CarrierSenseErrors = RD(aCarrierSenseErrors);
643         mac_stats->OctetsReceivedOK = RD(aOctetsReceivedOK);
644         mac_stats->FramesLostDueToIntMACRcvError = RD(ifInErrors);
645         mac_stats->MulticastFramesXmittedOK = RD(ifOutMulticastPkts);
646         mac_stats->BroadcastFramesXmittedOK = RD(ifOutBroadcastPkts);
647         mac_stats->FramesWithExcessiveDeferral = RD(aDeferred);
648         mac_stats->MulticastFramesReceivedOK = RD(ifInMulticastPkts);
649         mac_stats->BroadcastFramesReceivedOK = RD(ifInBroadcastPkts);
650 #undef RD
651 }
652
653 static const struct ethtool_rmon_hist_range a5psw_rmon_ranges[] = {
654         { 0, 64 },
655         { 65, 127 },
656         { 128, 255 },
657         { 256, 511 },
658         { 512, 1023 },
659         { 1024, 1518 },
660         { 1519, A5PSW_MAX_MTU },
661         {}
662 };
663
664 static void a5psw_get_rmon_stats(struct dsa_switch *ds, int port,
665                                  struct ethtool_rmon_stats *rmon_stats,
666                                  const struct ethtool_rmon_hist_range **ranges)
667 {
668         struct a5psw *a5psw = ds->priv;
669
670 #define RD(name) a5psw_read_stat(a5psw, A5PSW_##name, port)
671         rmon_stats->undersize_pkts = RD(etherStatsUndersizePkts);
672         rmon_stats->oversize_pkts = RD(etherStatsOversizePkts);
673         rmon_stats->fragments = RD(etherStatsFragments);
674         rmon_stats->jabbers = RD(etherStatsJabbers);
675         rmon_stats->hist[0] = RD(etherStatsPkts64Octets);
676         rmon_stats->hist[1] = RD(etherStatsPkts65to127Octets);
677         rmon_stats->hist[2] = RD(etherStatsPkts128to255Octets);
678         rmon_stats->hist[3] = RD(etherStatsPkts256to511Octets);
679         rmon_stats->hist[4] = RD(etherStatsPkts512to1023Octets);
680         rmon_stats->hist[5] = RD(etherStatsPkts1024to1518Octets);
681         rmon_stats->hist[6] = RD(etherStatsPkts1519toXOctets);
682 #undef RD
683
684         *ranges = a5psw_rmon_ranges;
685 }
686
687 static void a5psw_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
688                                      struct ethtool_eth_ctrl_stats *ctrl_stats)
689 {
690         struct a5psw *a5psw = ds->priv;
691         u64 stat;
692
693         stat = a5psw_read_stat(a5psw, A5PSW_aTxPAUSEMACCtrlFrames, port);
694         ctrl_stats->MACControlFramesTransmitted = stat;
695         stat = a5psw_read_stat(a5psw, A5PSW_aRxPAUSEMACCtrlFrames, port);
696         ctrl_stats->MACControlFramesReceived = stat;
697 }
698
699 static int a5psw_setup(struct dsa_switch *ds)
700 {
701         struct a5psw *a5psw = ds->priv;
702         int port, vlan, ret;
703         struct dsa_port *dp;
704         u32 reg;
705
706         /* Validate that there is only 1 CPU port with index A5PSW_CPU_PORT */
707         dsa_switch_for_each_cpu_port(dp, ds) {
708                 if (dp->index != A5PSW_CPU_PORT) {
709                         dev_err(a5psw->dev, "Invalid CPU port\n");
710                         return -EINVAL;
711                 }
712         }
713
714         /* Configure management port */
715         reg = A5PSW_CPU_PORT | A5PSW_MGMT_CFG_ENABLE;
716         a5psw_reg_writel(a5psw, A5PSW_MGMT_CFG, reg);
717
718         /* Set pattern 0 to forward all frame to mgmt port */
719         a5psw_reg_writel(a5psw, A5PSW_PATTERN_CTRL(A5PSW_PATTERN_MGMTFWD),
720                          A5PSW_PATTERN_CTRL_MGMTFWD);
721
722         /* Enable port tagging */
723         reg = FIELD_PREP(A5PSW_MGMT_TAG_CFG_TAGFIELD, ETH_P_DSA_A5PSW);
724         reg |= A5PSW_MGMT_TAG_CFG_ENABLE | A5PSW_MGMT_TAG_CFG_ALL_FRAMES;
725         a5psw_reg_writel(a5psw, A5PSW_MGMT_TAG_CFG, reg);
726
727         /* Enable normal switch operation */
728         reg = A5PSW_LK_ADDR_CTRL_BLOCKING | A5PSW_LK_ADDR_CTRL_LEARNING |
729               A5PSW_LK_ADDR_CTRL_AGEING | A5PSW_LK_ADDR_CTRL_ALLOW_MIGR |
730               A5PSW_LK_ADDR_CTRL_CLEAR_TABLE;
731         a5psw_reg_writel(a5psw, A5PSW_LK_CTRL, reg);
732
733         ret = readl_poll_timeout(a5psw->base + A5PSW_LK_CTRL, reg,
734                                  !(reg & A5PSW_LK_ADDR_CTRL_CLEAR_TABLE),
735                                  A5PSW_LK_BUSY_USEC_POLL, A5PSW_CTRL_TIMEOUT);
736         if (ret) {
737                 dev_err(a5psw->dev, "Failed to clear lookup table\n");
738                 return ret;
739         }
740
741         /* Reset learn count to 0 */
742         reg = A5PSW_LK_LEARNCOUNT_MODE_SET;
743         a5psw_reg_writel(a5psw, A5PSW_LK_LEARNCOUNT, reg);
744
745         /* Clear VLAN resource table */
746         reg = A5PSW_VLAN_RES_WR_PORTMASK | A5PSW_VLAN_RES_WR_TAGMASK;
747         for (vlan = 0; vlan < A5PSW_VLAN_COUNT; vlan++)
748                 a5psw_reg_writel(a5psw, A5PSW_VLAN_RES(vlan), reg);
749
750         /* Reset all ports */
751         dsa_switch_for_each_port(dp, ds) {
752                 port = dp->index;
753
754                 /* Reset the port */
755                 a5psw_reg_writel(a5psw, A5PSW_CMD_CFG(port),
756                                  A5PSW_CMD_CFG_SW_RESET);
757
758                 /* Enable only CPU port */
759                 a5psw_port_enable_set(a5psw, port, dsa_port_is_cpu(dp));
760
761                 if (dsa_port_is_unused(dp))
762                         continue;
763
764                 /* Enable egress flooding for CPU port */
765                 if (dsa_port_is_cpu(dp))
766                         a5psw_flooding_set_resolution(a5psw, port, true);
767
768                 /* Enable management forward only for user ports */
769                 if (dsa_port_is_user(dp))
770                         a5psw_port_mgmtfwd_set(a5psw, port, true);
771         }
772
773         return 0;
774 }
775
776 static const struct dsa_switch_ops a5psw_switch_ops = {
777         .get_tag_protocol = a5psw_get_tag_protocol,
778         .setup = a5psw_setup,
779         .port_disable = a5psw_port_disable,
780         .port_enable = a5psw_port_enable,
781         .phylink_get_caps = a5psw_phylink_get_caps,
782         .phylink_mac_select_pcs = a5psw_phylink_mac_select_pcs,
783         .phylink_mac_link_down = a5psw_phylink_mac_link_down,
784         .phylink_mac_link_up = a5psw_phylink_mac_link_up,
785         .port_change_mtu = a5psw_port_change_mtu,
786         .port_max_mtu = a5psw_port_max_mtu,
787         .get_sset_count = a5psw_get_sset_count,
788         .get_strings = a5psw_get_strings,
789         .get_ethtool_stats = a5psw_get_ethtool_stats,
790         .get_eth_mac_stats = a5psw_get_eth_mac_stats,
791         .get_eth_ctrl_stats = a5psw_get_eth_ctrl_stats,
792         .get_rmon_stats = a5psw_get_rmon_stats,
793         .set_ageing_time = a5psw_set_ageing_time,
794         .port_bridge_join = a5psw_port_bridge_join,
795         .port_bridge_leave = a5psw_port_bridge_leave,
796         .port_stp_state_set = a5psw_port_stp_state_set,
797         .port_fast_age = a5psw_port_fast_age,
798         .port_fdb_add = a5psw_port_fdb_add,
799         .port_fdb_del = a5psw_port_fdb_del,
800         .port_fdb_dump = a5psw_port_fdb_dump,
801 };
802
803 static int a5psw_mdio_wait_busy(struct a5psw *a5psw)
804 {
805         u32 status;
806         int err;
807
808         err = readl_poll_timeout(a5psw->base + A5PSW_MDIO_CFG_STATUS, status,
809                                  !(status & A5PSW_MDIO_CFG_STATUS_BUSY), 10,
810                                  1000 * USEC_PER_MSEC);
811         if (err)
812                 dev_err(a5psw->dev, "MDIO command timeout\n");
813
814         return err;
815 }
816
817 static int a5psw_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
818 {
819         struct a5psw *a5psw = bus->priv;
820         u32 cmd, status;
821         int ret;
822
823         cmd = A5PSW_MDIO_COMMAND_READ;
824         cmd |= FIELD_PREP(A5PSW_MDIO_COMMAND_REG_ADDR, phy_reg);
825         cmd |= FIELD_PREP(A5PSW_MDIO_COMMAND_PHY_ADDR, phy_id);
826
827         a5psw_reg_writel(a5psw, A5PSW_MDIO_COMMAND, cmd);
828
829         ret = a5psw_mdio_wait_busy(a5psw);
830         if (ret)
831                 return ret;
832
833         ret = a5psw_reg_readl(a5psw, A5PSW_MDIO_DATA) & A5PSW_MDIO_DATA_MASK;
834
835         status = a5psw_reg_readl(a5psw, A5PSW_MDIO_CFG_STATUS);
836         if (status & A5PSW_MDIO_CFG_STATUS_READERR)
837                 return -EIO;
838
839         return ret;
840 }
841
842 static int a5psw_mdio_write(struct mii_bus *bus, int phy_id, int phy_reg,
843                             u16 phy_data)
844 {
845         struct a5psw *a5psw = bus->priv;
846         u32 cmd;
847
848         cmd = FIELD_PREP(A5PSW_MDIO_COMMAND_REG_ADDR, phy_reg);
849         cmd |= FIELD_PREP(A5PSW_MDIO_COMMAND_PHY_ADDR, phy_id);
850
851         a5psw_reg_writel(a5psw, A5PSW_MDIO_COMMAND, cmd);
852         a5psw_reg_writel(a5psw, A5PSW_MDIO_DATA, phy_data);
853
854         return a5psw_mdio_wait_busy(a5psw);
855 }
856
857 static int a5psw_mdio_config(struct a5psw *a5psw, u32 mdio_freq)
858 {
859         unsigned long rate;
860         unsigned long div;
861         u32 cfgstatus;
862
863         rate = clk_get_rate(a5psw->hclk);
864         div = ((rate / mdio_freq) / 2);
865         if (div > FIELD_MAX(A5PSW_MDIO_CFG_STATUS_CLKDIV) ||
866             div < A5PSW_MDIO_CLK_DIV_MIN) {
867                 dev_err(a5psw->dev, "MDIO clock div %ld out of range\n", div);
868                 return -ERANGE;
869         }
870
871         cfgstatus = FIELD_PREP(A5PSW_MDIO_CFG_STATUS_CLKDIV, div);
872
873         a5psw_reg_writel(a5psw, A5PSW_MDIO_CFG_STATUS, cfgstatus);
874
875         return 0;
876 }
877
878 static int a5psw_probe_mdio(struct a5psw *a5psw, struct device_node *node)
879 {
880         struct device *dev = a5psw->dev;
881         struct mii_bus *bus;
882         u32 mdio_freq;
883         int ret;
884
885         if (of_property_read_u32(node, "clock-frequency", &mdio_freq))
886                 mdio_freq = A5PSW_MDIO_DEF_FREQ;
887
888         ret = a5psw_mdio_config(a5psw, mdio_freq);
889         if (ret)
890                 return ret;
891
892         bus = devm_mdiobus_alloc(dev);
893         if (!bus)
894                 return -ENOMEM;
895
896         bus->name = "a5psw_mdio";
897         bus->read = a5psw_mdio_read;
898         bus->write = a5psw_mdio_write;
899         bus->priv = a5psw;
900         bus->parent = dev;
901         snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
902
903         a5psw->mii_bus = bus;
904
905         return devm_of_mdiobus_register(dev, bus, node);
906 }
907
908 static void a5psw_pcs_free(struct a5psw *a5psw)
909 {
910         int i;
911
912         for (i = 0; i < ARRAY_SIZE(a5psw->pcs); i++) {
913                 if (a5psw->pcs[i])
914                         miic_destroy(a5psw->pcs[i]);
915         }
916 }
917
918 static int a5psw_pcs_get(struct a5psw *a5psw)
919 {
920         struct device_node *ports, *port, *pcs_node;
921         struct phylink_pcs *pcs;
922         int ret;
923         u32 reg;
924
925         ports = of_get_child_by_name(a5psw->dev->of_node, "ethernet-ports");
926         if (!ports)
927                 return -EINVAL;
928
929         for_each_available_child_of_node(ports, port) {
930                 pcs_node = of_parse_phandle(port, "pcs-handle", 0);
931                 if (!pcs_node)
932                         continue;
933
934                 if (of_property_read_u32(port, "reg", &reg)) {
935                         ret = -EINVAL;
936                         goto free_pcs;
937                 }
938
939                 if (reg >= ARRAY_SIZE(a5psw->pcs)) {
940                         ret = -ENODEV;
941                         goto free_pcs;
942                 }
943
944                 pcs = miic_create(a5psw->dev, pcs_node);
945                 if (IS_ERR(pcs)) {
946                         dev_err(a5psw->dev, "Failed to create PCS for port %d\n",
947                                 reg);
948                         ret = PTR_ERR(pcs);
949                         goto free_pcs;
950                 }
951
952                 a5psw->pcs[reg] = pcs;
953                 of_node_put(pcs_node);
954         }
955         of_node_put(ports);
956
957         return 0;
958
959 free_pcs:
960         of_node_put(pcs_node);
961         of_node_put(port);
962         of_node_put(ports);
963         a5psw_pcs_free(a5psw);
964
965         return ret;
966 }
967
968 static int a5psw_probe(struct platform_device *pdev)
969 {
970         struct device *dev = &pdev->dev;
971         struct device_node *mdio;
972         struct dsa_switch *ds;
973         struct a5psw *a5psw;
974         int ret;
975
976         a5psw = devm_kzalloc(dev, sizeof(*a5psw), GFP_KERNEL);
977         if (!a5psw)
978                 return -ENOMEM;
979
980         a5psw->dev = dev;
981         mutex_init(&a5psw->lk_lock);
982         spin_lock_init(&a5psw->reg_lock);
983         a5psw->base = devm_platform_ioremap_resource(pdev, 0);
984         if (IS_ERR(a5psw->base))
985                 return PTR_ERR(a5psw->base);
986
987         ret = a5psw_pcs_get(a5psw);
988         if (ret)
989                 return ret;
990
991         a5psw->hclk = devm_clk_get(dev, "hclk");
992         if (IS_ERR(a5psw->hclk)) {
993                 dev_err(dev, "failed get hclk clock\n");
994                 ret = PTR_ERR(a5psw->hclk);
995                 goto free_pcs;
996         }
997
998         a5psw->clk = devm_clk_get(dev, "clk");
999         if (IS_ERR(a5psw->clk)) {
1000                 dev_err(dev, "failed get clk_switch clock\n");
1001                 ret = PTR_ERR(a5psw->clk);
1002                 goto free_pcs;
1003         }
1004
1005         ret = clk_prepare_enable(a5psw->clk);
1006         if (ret)
1007                 goto free_pcs;
1008
1009         ret = clk_prepare_enable(a5psw->hclk);
1010         if (ret)
1011                 goto clk_disable;
1012
1013         mdio = of_get_child_by_name(dev->of_node, "mdio");
1014         if (of_device_is_available(mdio)) {
1015                 ret = a5psw_probe_mdio(a5psw, mdio);
1016                 if (ret) {
1017                         of_node_put(mdio);
1018                         dev_err(dev, "Failed to register MDIO: %d\n", ret);
1019                         goto hclk_disable;
1020                 }
1021         }
1022
1023         of_node_put(mdio);
1024
1025         ds = &a5psw->ds;
1026         ds->dev = dev;
1027         ds->num_ports = A5PSW_PORTS_NUM;
1028         ds->ops = &a5psw_switch_ops;
1029         ds->priv = a5psw;
1030
1031         ret = dsa_register_switch(ds);
1032         if (ret) {
1033                 dev_err(dev, "Failed to register DSA switch: %d\n", ret);
1034                 goto hclk_disable;
1035         }
1036
1037         return 0;
1038
1039 hclk_disable:
1040         clk_disable_unprepare(a5psw->hclk);
1041 clk_disable:
1042         clk_disable_unprepare(a5psw->clk);
1043 free_pcs:
1044         a5psw_pcs_free(a5psw);
1045
1046         return ret;
1047 }
1048
1049 static int a5psw_remove(struct platform_device *pdev)
1050 {
1051         struct a5psw *a5psw = platform_get_drvdata(pdev);
1052
1053         if (!a5psw)
1054                 return 0;
1055
1056         dsa_unregister_switch(&a5psw->ds);
1057         a5psw_pcs_free(a5psw);
1058         clk_disable_unprepare(a5psw->hclk);
1059         clk_disable_unprepare(a5psw->clk);
1060
1061         return 0;
1062 }
1063
1064 static void a5psw_shutdown(struct platform_device *pdev)
1065 {
1066         struct a5psw *a5psw = platform_get_drvdata(pdev);
1067
1068         if (!a5psw)
1069                 return;
1070
1071         dsa_switch_shutdown(&a5psw->ds);
1072
1073         platform_set_drvdata(pdev, NULL);
1074 }
1075
1076 static const struct of_device_id a5psw_of_mtable[] = {
1077         { .compatible = "renesas,rzn1-a5psw", },
1078         { /* sentinel */ },
1079 };
1080 MODULE_DEVICE_TABLE(of, a5psw_of_mtable);
1081
1082 static struct platform_driver a5psw_driver = {
1083         .driver = {
1084                 .name    = "rzn1_a5psw",
1085                 .of_match_table = of_match_ptr(a5psw_of_mtable),
1086         },
1087         .probe = a5psw_probe,
1088         .remove = a5psw_remove,
1089         .shutdown = a5psw_shutdown,
1090 };
1091 module_platform_driver(a5psw_driver);
1092
1093 MODULE_LICENSE("GPL");
1094 MODULE_DESCRIPTION("Renesas RZ/N1 Advanced 5-port Switch driver");
1095 MODULE_AUTHOR("Clément Léger <clement.leger@bootlin.com>");