1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2019 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
4 * +----------------------+
5 * GMAC1----RGMII----|--MAC0 |
6 * \---MDIO1----|--REGs |----MDIO3----\
9 * | MAC1-|----RMII--M-----| PHY0 |-o P0
12 * | MAC2-|----RMII--------| PHY1 |-o P1
15 * | MAC3-|----RMII--------| PHY2 |-o P2
18 * | MAC4-|----RMII--------| PHY3 |-o P3
21 * | MAC5-|--+-RMII--M-----|-PHY4-|-o P4
23 * +----------------------+ | \--CFG_SW_PHY_SWAP
24 * GMAC0---------------RMII--------------------/ \-CFG_SW_PHY_ADDR_SWAP
27 * GMAC0 and MAC5 are connected together and use same PHY. Depending on
28 * configuration it can be PHY4 (default) or PHY0. Only GMAC0 or MAC5 can be
29 * used at same time. If GMAC0 is used (default) then MAC5 should be disabled.
31 * CFG_SW_PHY_SWAP - swap connections of PHY0 and PHY4. If this bit is not set
32 * PHY4 is connected to GMAC0/MAC5 bundle and PHY0 is connected to MAC1. If this
33 * bit is set, PHY4 is connected to MAC1 and PHY0 is connected to GMAC0/MAC5
36 * CFG_SW_PHY_ADDR_SWAP - swap addresses of PHY0 and PHY4
38 * CFG_SW_PHY_SWAP and CFG_SW_PHY_ADDR_SWAP are part of SoC specific register
39 * set and not related to switch internal registers.
42 #include <linux/bitfield.h>
43 #include <linux/module.h>
44 #include <linux/of_irq.h>
45 #include <linux/of_mdio.h>
46 #include <linux/regmap.h>
47 #include <linux/reset.h>
50 #define AR9331_SW_NAME "ar9331_switch"
51 #define AR9331_SW_PORTS 6
53 /* dummy reg to change page */
54 #define AR9331_SW_REG_PAGE 0x40000
56 /* Global Interrupt */
57 #define AR9331_SW_REG_GINT 0x10
58 #define AR9331_SW_REG_GINT_MASK 0x14
59 #define AR9331_SW_GINT_PHY_INT BIT(2)
61 #define AR9331_SW_REG_FLOOD_MASK 0x2c
62 #define AR9331_SW_FLOOD_MASK_BROAD_TO_CPU BIT(26)
64 #define AR9331_SW_REG_GLOBAL_CTRL 0x30
65 #define AR9331_SW_GLOBAL_CTRL_MFS_M GENMASK(13, 0)
67 #define AR9331_SW_REG_MDIO_CTRL 0x98
68 #define AR9331_SW_MDIO_CTRL_BUSY BIT(31)
69 #define AR9331_SW_MDIO_CTRL_MASTER_EN BIT(30)
70 #define AR9331_SW_MDIO_CTRL_CMD_READ BIT(27)
71 #define AR9331_SW_MDIO_CTRL_PHY_ADDR_M GENMASK(25, 21)
72 #define AR9331_SW_MDIO_CTRL_REG_ADDR_M GENMASK(20, 16)
73 #define AR9331_SW_MDIO_CTRL_DATA_M GENMASK(16, 0)
75 #define AR9331_SW_REG_PORT_STATUS(_port) (0x100 + (_port) * 0x100)
77 /* FLOW_LINK_EN - enable mac flow control config auto-neg with phy.
78 * If not set, mac can be config by software.
80 #define AR9331_SW_PORT_STATUS_FLOW_LINK_EN BIT(12)
82 /* LINK_EN - If set, MAC is configured from PHY link status.
83 * If not set, MAC should be configured by software.
85 #define AR9331_SW_PORT_STATUS_LINK_EN BIT(9)
86 #define AR9331_SW_PORT_STATUS_DUPLEX_MODE BIT(6)
87 #define AR9331_SW_PORT_STATUS_RX_FLOW_EN BIT(5)
88 #define AR9331_SW_PORT_STATUS_TX_FLOW_EN BIT(4)
89 #define AR9331_SW_PORT_STATUS_RXMAC BIT(3)
90 #define AR9331_SW_PORT_STATUS_TXMAC BIT(2)
91 #define AR9331_SW_PORT_STATUS_SPEED_M GENMASK(1, 0)
92 #define AR9331_SW_PORT_STATUS_SPEED_1000 2
93 #define AR9331_SW_PORT_STATUS_SPEED_100 1
94 #define AR9331_SW_PORT_STATUS_SPEED_10 0
96 #define AR9331_SW_PORT_STATUS_MAC_MASK \
97 (AR9331_SW_PORT_STATUS_TXMAC | AR9331_SW_PORT_STATUS_RXMAC)
99 #define AR9331_SW_PORT_STATUS_LINK_MASK \
100 (AR9331_SW_PORT_STATUS_DUPLEX_MODE | \
101 AR9331_SW_PORT_STATUS_RX_FLOW_EN | AR9331_SW_PORT_STATUS_TX_FLOW_EN | \
102 AR9331_SW_PORT_STATUS_SPEED_M)
105 #define AR9331_MIB_COUNTER(x) (0x20000 + ((x) * 0x100))
108 * ------------------------------------------------------------------------
109 * Bit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |
111 * real | start | OP | PhyAddr | Reg Addr | TA |
112 * atheros| start | OP | 2'b00 |PhyAdd[2:0]| Reg Addr[4:0] | TA |
115 * Bit: |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
119 * ------------------------------------------------------------------------
121 * ------------------------------------------------------------------------
122 * Bit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |
123 * real | start | OP | PhyAddr | Reg Addr | TA |
124 * atheros| start | OP | 2'b11 | 8'b0 | TA |
126 * Bit: |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
128 * atheros| | Page [9:0] |
130 /* In case of Page Address mode, Bit[18:9] of 32 bit register address should be
131 * written to bits[9:0] of mdio data register.
133 #define AR9331_SW_ADDR_PAGE GENMASK(18, 9)
135 /* ------------------------------------------------------------------------
136 * Normal register access mode
137 * ------------------------------------------------------------------------
138 * Bit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |
139 * real | start | OP | PhyAddr | Reg Addr | TA |
140 * atheros| start | OP | 2'b10 | low_addr[7:0] | TA |
142 * Bit: |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
145 * ------------------------------------------------------------------------
147 #define AR9331_SW_LOW_ADDR_PHY GENMASK(8, 6)
148 #define AR9331_SW_LOW_ADDR_REG GENMASK(5, 1)
150 #define AR9331_SW_MDIO_PHY_MODE_M GENMASK(4, 3)
151 #define AR9331_SW_MDIO_PHY_MODE_PAGE 3
152 #define AR9331_SW_MDIO_PHY_MODE_REG 2
153 #define AR9331_SW_MDIO_PHY_MODE_BYPASS 0
154 #define AR9331_SW_MDIO_PHY_ADDR_M GENMASK(2, 0)
156 /* Empirical determined values */
157 #define AR9331_SW_MDIO_POLL_SLEEP_US 1
158 #define AR9331_SW_MDIO_POLL_TIMEOUT_US 20
160 /* The interval should be small enough to avoid overflow of 32bit MIBs */
162 * FIXME: until we can read MIBs from stats64 call directly (i.e. sleep
163 * there), we have to poll stats more frequently then it is actually needed.
164 * For overflow protection, normally, 100 sec interval should have been OK.
166 #define STATS_INTERVAL_JIFFIES (3 * HZ)
168 struct ar9331_sw_stats_raw {
169 u32 rxbroad; /* 0x00 */
170 u32 rxpause; /* 0x04 */
171 u32 rxmulti; /* 0x08 */
172 u32 rxfcserr; /* 0x0c */
173 u32 rxalignerr; /* 0x10 */
174 u32 rxrunt; /* 0x14 */
175 u32 rxfragment; /* 0x18 */
176 u32 rx64byte; /* 0x1c */
177 u32 rx128byte; /* 0x20 */
178 u32 rx256byte; /* 0x24 */
179 u32 rx512byte; /* 0x28 */
180 u32 rx1024byte; /* 0x2c */
181 u32 rx1518byte; /* 0x30 */
182 u32 rxmaxbyte; /* 0x34 */
183 u32 rxtoolong; /* 0x38 */
184 u32 rxgoodbyte; /* 0x3c */
186 u32 rxbadbyte; /* 0x44 */
188 u32 rxoverflow; /* 0x4c */
189 u32 filtered; /* 0x50 */
190 u32 txbroad; /* 0x54 */
191 u32 txpause; /* 0x58 */
192 u32 txmulti; /* 0x5c */
193 u32 txunderrun; /* 0x60 */
194 u32 tx64byte; /* 0x64 */
195 u32 tx128byte; /* 0x68 */
196 u32 tx256byte; /* 0x6c */
197 u32 tx512byte; /* 0x70 */
198 u32 tx1024byte; /* 0x74 */
199 u32 tx1518byte; /* 0x78 */
200 u32 txmaxbyte; /* 0x7c */
201 u32 txoversize; /* 0x80 */
202 u32 txbyte; /* 0x84 */
204 u32 txcollision; /* 0x8c */
205 u32 txabortcol; /* 0x90 */
206 u32 txmulticol; /* 0x94 */
207 u32 txsinglecol; /* 0x98 */
208 u32 txexcdefer; /* 0x9c */
209 u32 txdefer; /* 0xa0 */
210 u32 txlatecol; /* 0xa4 */
213 struct ar9331_sw_port {
215 struct delayed_work mib_read;
216 struct rtnl_link_stats64 stats;
217 struct spinlock stats_lock;
220 struct ar9331_sw_priv {
222 struct dsa_switch ds;
223 struct dsa_switch_ops ops;
224 struct irq_domain *irqdomain;
226 struct mutex lock_irq;
227 struct mii_bus *mbus; /* mdio master */
228 struct mii_bus *sbus; /* mdio slave */
229 struct regmap *regmap;
230 struct reset_control *sw_reset;
231 struct ar9331_sw_port port[AR9331_SW_PORTS];
234 static struct ar9331_sw_priv *ar9331_sw_port_to_priv(struct ar9331_sw_port *port)
236 struct ar9331_sw_port *p = port - port->idx;
238 return (struct ar9331_sw_priv *)((void *)p -
239 offsetof(struct ar9331_sw_priv, port));
242 /* Warning: switch reset will reset last AR9331_SW_MDIO_PHY_MODE_PAGE request
243 * If some kind of optimization is used, the request should be repeated.
245 static int ar9331_sw_reset(struct ar9331_sw_priv *priv)
249 ret = reset_control_assert(priv->sw_reset);
253 /* AR9331 doc do not provide any information about proper reset
254 * sequence. The AR8136 (the closes switch to the AR9331) doc says:
255 * reset duration should be greater than 10ms. So, let's use this value
258 usleep_range(10000, 15000);
259 ret = reset_control_deassert(priv->sw_reset);
262 /* There is no information on how long should we wait after reset.
263 * AR8136 has an EEPROM and there is an Interrupt for EEPROM load
264 * status. AR9331 has no EEPROM support.
265 * For now, do not wait. In case AR8136 will be needed, the after
266 * reset delay can be added as well.
271 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
275 static int ar9331_sw_mbus_write(struct mii_bus *mbus, int port, int regnum,
278 struct ar9331_sw_priv *priv = mbus->priv;
279 struct regmap *regmap = priv->regmap;
283 ret = regmap_write(regmap, AR9331_SW_REG_MDIO_CTRL,
284 AR9331_SW_MDIO_CTRL_BUSY |
285 AR9331_SW_MDIO_CTRL_MASTER_EN |
286 FIELD_PREP(AR9331_SW_MDIO_CTRL_PHY_ADDR_M, port) |
287 FIELD_PREP(AR9331_SW_MDIO_CTRL_REG_ADDR_M, regnum) |
288 FIELD_PREP(AR9331_SW_MDIO_CTRL_DATA_M, data));
292 ret = regmap_read_poll_timeout(regmap, AR9331_SW_REG_MDIO_CTRL, val,
293 !(val & AR9331_SW_MDIO_CTRL_BUSY),
294 AR9331_SW_MDIO_POLL_SLEEP_US,
295 AR9331_SW_MDIO_POLL_TIMEOUT_US);
301 dev_err_ratelimited(priv->dev, "PHY write error: %i\n", ret);
305 static int ar9331_sw_mbus_read(struct mii_bus *mbus, int port, int regnum)
307 struct ar9331_sw_priv *priv = mbus->priv;
308 struct regmap *regmap = priv->regmap;
312 ret = regmap_write(regmap, AR9331_SW_REG_MDIO_CTRL,
313 AR9331_SW_MDIO_CTRL_BUSY |
314 AR9331_SW_MDIO_CTRL_MASTER_EN |
315 AR9331_SW_MDIO_CTRL_CMD_READ |
316 FIELD_PREP(AR9331_SW_MDIO_CTRL_PHY_ADDR_M, port) |
317 FIELD_PREP(AR9331_SW_MDIO_CTRL_REG_ADDR_M, regnum));
321 ret = regmap_read_poll_timeout(regmap, AR9331_SW_REG_MDIO_CTRL, val,
322 !(val & AR9331_SW_MDIO_CTRL_BUSY),
323 AR9331_SW_MDIO_POLL_SLEEP_US,
324 AR9331_SW_MDIO_POLL_TIMEOUT_US);
328 ret = regmap_read(regmap, AR9331_SW_REG_MDIO_CTRL, &val);
332 return FIELD_GET(AR9331_SW_MDIO_CTRL_DATA_M, val);
335 dev_err_ratelimited(priv->dev, "PHY read error: %i\n", ret);
339 static int ar9331_sw_mbus_init(struct ar9331_sw_priv *priv)
341 struct device *dev = priv->dev;
342 struct mii_bus *mbus;
343 struct device_node *np, *mnp;
348 mbus = devm_mdiobus_alloc(dev);
352 mbus->name = np->full_name;
353 snprintf(mbus->id, MII_BUS_ID_SIZE, "%pOF", np);
355 mbus->read = ar9331_sw_mbus_read;
356 mbus->write = ar9331_sw_mbus_write;
360 mnp = of_get_child_by_name(np, "mdio");
364 ret = of_mdiobus_register(mbus, mnp);
374 static int ar9331_sw_setup(struct dsa_switch *ds)
376 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ds->priv;
377 struct regmap *regmap = priv->regmap;
380 ret = ar9331_sw_reset(priv);
384 /* Reset will set proper defaults. CPU - Port0 will be enabled and
385 * configured. All other ports (ports 1 - 5) are disabled
387 ret = ar9331_sw_mbus_init(priv);
391 /* Do not drop broadcast frames */
392 ret = regmap_write_bits(regmap, AR9331_SW_REG_FLOOD_MASK,
393 AR9331_SW_FLOOD_MASK_BROAD_TO_CPU,
394 AR9331_SW_FLOOD_MASK_BROAD_TO_CPU);
398 /* Set max frame size to the maximum supported value */
399 ret = regmap_write_bits(regmap, AR9331_SW_REG_GLOBAL_CTRL,
400 AR9331_SW_GLOBAL_CTRL_MFS_M,
401 AR9331_SW_GLOBAL_CTRL_MFS_M);
405 ds->configure_vlan_while_not_filtering = false;
409 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
413 static void ar9331_sw_port_disable(struct dsa_switch *ds, int port)
415 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ds->priv;
416 struct regmap *regmap = priv->regmap;
419 ret = regmap_write(regmap, AR9331_SW_REG_PORT_STATUS(port), 0);
421 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
424 static enum dsa_tag_protocol ar9331_sw_get_tag_protocol(struct dsa_switch *ds,
426 enum dsa_tag_protocol m)
428 return DSA_TAG_PROTO_AR9331;
431 static void ar9331_sw_phylink_validate(struct dsa_switch *ds, int port,
432 unsigned long *supported,
433 struct phylink_link_state *state)
435 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
439 if (state->interface != PHY_INTERFACE_MODE_GMII)
442 phylink_set(mask, 1000baseT_Full);
443 phylink_set(mask, 1000baseT_Half);
450 if (state->interface != PHY_INTERFACE_MODE_INTERNAL)
454 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
455 dev_err(ds->dev, "Unsupported port: %i\n", port);
459 phylink_set_port_modes(mask);
460 phylink_set(mask, Pause);
461 phylink_set(mask, Asym_Pause);
463 phylink_set(mask, 10baseT_Half);
464 phylink_set(mask, 10baseT_Full);
465 phylink_set(mask, 100baseT_Half);
466 phylink_set(mask, 100baseT_Full);
468 bitmap_and(supported, supported, mask,
469 __ETHTOOL_LINK_MODE_MASK_NBITS);
470 bitmap_and(state->advertising, state->advertising, mask,
471 __ETHTOOL_LINK_MODE_MASK_NBITS);
476 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
477 dev_err(ds->dev, "Unsupported interface: %d, port: %d\n",
478 state->interface, port);
481 static void ar9331_sw_phylink_mac_config(struct dsa_switch *ds, int port,
483 const struct phylink_link_state *state)
485 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ds->priv;
486 struct regmap *regmap = priv->regmap;
489 ret = regmap_update_bits(regmap, AR9331_SW_REG_PORT_STATUS(port),
490 AR9331_SW_PORT_STATUS_LINK_EN |
491 AR9331_SW_PORT_STATUS_FLOW_LINK_EN, 0);
493 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
496 static void ar9331_sw_phylink_mac_link_down(struct dsa_switch *ds, int port,
498 phy_interface_t interface)
500 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ds->priv;
501 struct ar9331_sw_port *p = &priv->port[port];
502 struct regmap *regmap = priv->regmap;
505 ret = regmap_update_bits(regmap, AR9331_SW_REG_PORT_STATUS(port),
506 AR9331_SW_PORT_STATUS_MAC_MASK, 0);
508 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
510 cancel_delayed_work_sync(&p->mib_read);
513 static void ar9331_sw_phylink_mac_link_up(struct dsa_switch *ds, int port,
515 phy_interface_t interface,
516 struct phy_device *phydev,
517 int speed, int duplex,
518 bool tx_pause, bool rx_pause)
520 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ds->priv;
521 struct ar9331_sw_port *p = &priv->port[port];
522 struct regmap *regmap = priv->regmap;
526 schedule_delayed_work(&p->mib_read, 0);
528 val = AR9331_SW_PORT_STATUS_MAC_MASK;
531 val |= AR9331_SW_PORT_STATUS_SPEED_1000;
534 val |= AR9331_SW_PORT_STATUS_SPEED_100;
537 val |= AR9331_SW_PORT_STATUS_SPEED_10;
544 val |= AR9331_SW_PORT_STATUS_DUPLEX_MODE;
547 val |= AR9331_SW_PORT_STATUS_TX_FLOW_EN;
550 val |= AR9331_SW_PORT_STATUS_RX_FLOW_EN;
552 ret = regmap_update_bits(regmap, AR9331_SW_REG_PORT_STATUS(port),
553 AR9331_SW_PORT_STATUS_MAC_MASK |
554 AR9331_SW_PORT_STATUS_LINK_MASK,
557 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
560 static void ar9331_read_stats(struct ar9331_sw_port *port)
562 struct ar9331_sw_priv *priv = ar9331_sw_port_to_priv(port);
563 struct rtnl_link_stats64 *stats = &port->stats;
564 struct ar9331_sw_stats_raw raw;
567 /* Do the slowest part first, to avoid needless locking for long time */
568 ret = regmap_bulk_read(priv->regmap, AR9331_MIB_COUNTER(port->idx),
569 &raw, sizeof(raw) / sizeof(u32));
571 dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
574 /* All MIB counters are cleared automatically on read */
576 spin_lock(&port->stats_lock);
578 stats->rx_bytes += raw.rxgoodbyte;
579 stats->tx_bytes += raw.txbyte;
581 stats->rx_packets += raw.rx64byte + raw.rx128byte + raw.rx256byte +
582 raw.rx512byte + raw.rx1024byte + raw.rx1518byte + raw.rxmaxbyte;
583 stats->tx_packets += raw.tx64byte + raw.tx128byte + raw.tx256byte +
584 raw.tx512byte + raw.tx1024byte + raw.tx1518byte + raw.txmaxbyte;
586 stats->rx_length_errors += raw.rxrunt + raw.rxfragment + raw.rxtoolong;
587 stats->rx_crc_errors += raw.rxfcserr;
588 stats->rx_frame_errors += raw.rxalignerr;
589 stats->rx_missed_errors += raw.rxoverflow;
590 stats->rx_dropped += raw.filtered;
591 stats->rx_errors += raw.rxfcserr + raw.rxalignerr + raw.rxrunt +
592 raw.rxfragment + raw.rxoverflow + raw.rxtoolong;
594 stats->tx_window_errors += raw.txlatecol;
595 stats->tx_fifo_errors += raw.txunderrun;
596 stats->tx_aborted_errors += raw.txabortcol;
597 stats->tx_errors += raw.txoversize + raw.txabortcol + raw.txunderrun +
600 stats->multicast += raw.rxmulti;
601 stats->collisions += raw.txcollision;
603 spin_unlock(&port->stats_lock);
606 static void ar9331_do_stats_poll(struct work_struct *work)
608 struct ar9331_sw_port *port = container_of(work, struct ar9331_sw_port,
611 ar9331_read_stats(port);
613 schedule_delayed_work(&port->mib_read, STATS_INTERVAL_JIFFIES);
616 static void ar9331_get_stats64(struct dsa_switch *ds, int port,
617 struct rtnl_link_stats64 *s)
619 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ds->priv;
620 struct ar9331_sw_port *p = &priv->port[port];
622 spin_lock(&p->stats_lock);
623 memcpy(s, &p->stats, sizeof(*s));
624 spin_unlock(&p->stats_lock);
627 static const struct dsa_switch_ops ar9331_sw_ops = {
628 .get_tag_protocol = ar9331_sw_get_tag_protocol,
629 .setup = ar9331_sw_setup,
630 .port_disable = ar9331_sw_port_disable,
631 .phylink_validate = ar9331_sw_phylink_validate,
632 .phylink_mac_config = ar9331_sw_phylink_mac_config,
633 .phylink_mac_link_down = ar9331_sw_phylink_mac_link_down,
634 .phylink_mac_link_up = ar9331_sw_phylink_mac_link_up,
635 .get_stats64 = ar9331_get_stats64,
638 static irqreturn_t ar9331_sw_irq(int irq, void *data)
640 struct ar9331_sw_priv *priv = data;
641 struct regmap *regmap = priv->regmap;
645 ret = regmap_read(regmap, AR9331_SW_REG_GINT, &stat);
647 dev_err(priv->dev, "can't read interrupt status\n");
654 if (stat & AR9331_SW_GINT_PHY_INT) {
657 child_irq = irq_find_mapping(priv->irqdomain, 0);
658 handle_nested_irq(child_irq);
661 ret = regmap_write(regmap, AR9331_SW_REG_GINT, stat);
663 dev_err(priv->dev, "can't write interrupt status\n");
670 static void ar9331_sw_mask_irq(struct irq_data *d)
672 struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
677 static void ar9331_sw_unmask_irq(struct irq_data *d)
679 struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
681 priv->irq_mask = AR9331_SW_GINT_PHY_INT;
684 static void ar9331_sw_irq_bus_lock(struct irq_data *d)
686 struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
688 mutex_lock(&priv->lock_irq);
691 static void ar9331_sw_irq_bus_sync_unlock(struct irq_data *d)
693 struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
694 struct regmap *regmap = priv->regmap;
697 ret = regmap_update_bits(regmap, AR9331_SW_REG_GINT_MASK,
698 AR9331_SW_GINT_PHY_INT, priv->irq_mask);
700 dev_err(priv->dev, "failed to change IRQ mask\n");
702 mutex_unlock(&priv->lock_irq);
705 static struct irq_chip ar9331_sw_irq_chip = {
706 .name = AR9331_SW_NAME,
707 .irq_mask = ar9331_sw_mask_irq,
708 .irq_unmask = ar9331_sw_unmask_irq,
709 .irq_bus_lock = ar9331_sw_irq_bus_lock,
710 .irq_bus_sync_unlock = ar9331_sw_irq_bus_sync_unlock,
713 static int ar9331_sw_irq_map(struct irq_domain *domain, unsigned int irq,
714 irq_hw_number_t hwirq)
716 irq_set_chip_data(irq, domain->host_data);
717 irq_set_chip_and_handler(irq, &ar9331_sw_irq_chip, handle_simple_irq);
718 irq_set_nested_thread(irq, 1);
719 irq_set_noprobe(irq);
724 static void ar9331_sw_irq_unmap(struct irq_domain *d, unsigned int irq)
726 irq_set_nested_thread(irq, 0);
727 irq_set_chip_and_handler(irq, NULL, NULL);
728 irq_set_chip_data(irq, NULL);
731 static const struct irq_domain_ops ar9331_sw_irqdomain_ops = {
732 .map = ar9331_sw_irq_map,
733 .unmap = ar9331_sw_irq_unmap,
734 .xlate = irq_domain_xlate_onecell,
737 static int ar9331_sw_irq_init(struct ar9331_sw_priv *priv)
739 struct device_node *np = priv->dev->of_node;
740 struct device *dev = priv->dev;
743 irq = of_irq_get(np, 0);
745 dev_err(dev, "failed to get parent IRQ\n");
746 return irq ? irq : -EINVAL;
749 mutex_init(&priv->lock_irq);
750 ret = devm_request_threaded_irq(dev, irq, NULL, ar9331_sw_irq,
751 IRQF_ONESHOT, AR9331_SW_NAME, priv);
753 dev_err(dev, "unable to request irq: %d\n", ret);
757 priv->irqdomain = irq_domain_add_linear(np, 1, &ar9331_sw_irqdomain_ops,
759 if (!priv->irqdomain) {
760 dev_err(dev, "failed to create IRQ domain\n");
764 irq_set_parent(irq_create_mapping(priv->irqdomain, 0), irq);
769 static int __ar9331_mdio_write(struct mii_bus *sbus, u8 mode, u16 reg, u16 val)
773 p = FIELD_PREP(AR9331_SW_MDIO_PHY_MODE_M, mode) |
774 FIELD_GET(AR9331_SW_LOW_ADDR_PHY, reg);
775 r = FIELD_GET(AR9331_SW_LOW_ADDR_REG, reg);
777 return mdiobus_write(sbus, p, r, val);
780 static int __ar9331_mdio_read(struct mii_bus *sbus, u16 reg)
784 p = FIELD_PREP(AR9331_SW_MDIO_PHY_MODE_M, AR9331_SW_MDIO_PHY_MODE_REG) |
785 FIELD_GET(AR9331_SW_LOW_ADDR_PHY, reg);
786 r = FIELD_GET(AR9331_SW_LOW_ADDR_REG, reg);
788 return mdiobus_read(sbus, p, r);
791 static int ar9331_mdio_read(void *ctx, const void *reg_buf, size_t reg_len,
792 void *val_buf, size_t val_len)
794 struct ar9331_sw_priv *priv = ctx;
795 struct mii_bus *sbus = priv->sbus;
796 u32 reg = *(u32 *)reg_buf;
799 if (reg == AR9331_SW_REG_PAGE) {
800 /* We cannot read the page selector register from hardware and
801 * we cache its value in regmap. Return all bits set here,
802 * that regmap will always write the page on first use.
804 *(u32 *)val_buf = GENMASK(9, 0);
808 ret = __ar9331_mdio_read(sbus, reg);
812 *(u32 *)val_buf = ret;
813 ret = __ar9331_mdio_read(sbus, reg + 2);
817 *(u32 *)val_buf |= ret << 16;
821 dev_err_ratelimited(&sbus->dev, "Bus error. Failed to read register.\n");
825 static int ar9331_mdio_write(void *ctx, u32 reg, u32 val)
827 struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ctx;
828 struct mii_bus *sbus = priv->sbus;
831 if (reg == AR9331_SW_REG_PAGE) {
832 ret = __ar9331_mdio_write(sbus, AR9331_SW_MDIO_PHY_MODE_PAGE,
840 /* In case of this switch we work with 32bit registers on top of 16bit
841 * bus. Some registers (for example access to forwarding database) have
842 * trigger bit on the first 16bit half of request, the result and
843 * configuration of request in the second half.
844 * To make it work properly, we should do the second part of transfer
845 * before the first one is done.
847 ret = __ar9331_mdio_write(sbus, AR9331_SW_MDIO_PHY_MODE_REG, reg + 2,
852 ret = __ar9331_mdio_write(sbus, AR9331_SW_MDIO_PHY_MODE_REG, reg, val);
859 dev_err_ratelimited(&sbus->dev, "Bus error. Failed to write register.\n");
863 static int ar9331_sw_bus_write(void *context, const void *data, size_t count)
865 u32 reg = *(u32 *)data;
866 u32 val = *((u32 *)data + 1);
868 return ar9331_mdio_write(context, reg, val);
871 static const struct regmap_range ar9331_valid_regs[] = {
872 regmap_reg_range(0x0, 0x0),
873 regmap_reg_range(0x10, 0x14),
874 regmap_reg_range(0x20, 0x24),
875 regmap_reg_range(0x2c, 0x30),
876 regmap_reg_range(0x40, 0x44),
877 regmap_reg_range(0x50, 0x78),
878 regmap_reg_range(0x80, 0x98),
880 regmap_reg_range(0x100, 0x120),
881 regmap_reg_range(0x200, 0x220),
882 regmap_reg_range(0x300, 0x320),
883 regmap_reg_range(0x400, 0x420),
884 regmap_reg_range(0x500, 0x520),
885 regmap_reg_range(0x600, 0x620),
887 regmap_reg_range(0x20000, 0x200a4),
888 regmap_reg_range(0x20100, 0x201a4),
889 regmap_reg_range(0x20200, 0x202a4),
890 regmap_reg_range(0x20300, 0x203a4),
891 regmap_reg_range(0x20400, 0x204a4),
892 regmap_reg_range(0x20500, 0x205a4),
894 /* dummy page selector reg */
895 regmap_reg_range(AR9331_SW_REG_PAGE, AR9331_SW_REG_PAGE),
898 static const struct regmap_range ar9331_nonvolatile_regs[] = {
899 regmap_reg_range(AR9331_SW_REG_PAGE, AR9331_SW_REG_PAGE),
902 static const struct regmap_range_cfg ar9331_regmap_range[] = {
904 .selector_reg = AR9331_SW_REG_PAGE,
905 .selector_mask = GENMASK(9, 0),
912 .range_max = AR9331_SW_REG_PAGE - 4,
916 static const struct regmap_access_table ar9331_register_set = {
917 .yes_ranges = ar9331_valid_regs,
918 .n_yes_ranges = ARRAY_SIZE(ar9331_valid_regs),
921 static const struct regmap_access_table ar9331_volatile_set = {
922 .no_ranges = ar9331_nonvolatile_regs,
923 .n_no_ranges = ARRAY_SIZE(ar9331_nonvolatile_regs),
926 static const struct regmap_config ar9331_mdio_regmap_config = {
930 .max_register = AR9331_SW_REG_PAGE,
932 .ranges = ar9331_regmap_range,
933 .num_ranges = ARRAY_SIZE(ar9331_regmap_range),
935 .volatile_table = &ar9331_volatile_set,
936 .wr_table = &ar9331_register_set,
937 .rd_table = &ar9331_register_set,
939 .cache_type = REGCACHE_RBTREE,
942 static struct regmap_bus ar9331_sw_bus = {
943 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
944 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
945 .read = ar9331_mdio_read,
946 .write = ar9331_sw_bus_write,
951 static int ar9331_sw_probe(struct mdio_device *mdiodev)
953 struct ar9331_sw_priv *priv;
954 struct dsa_switch *ds;
957 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
961 priv->regmap = devm_regmap_init(&mdiodev->dev, &ar9331_sw_bus, priv,
962 &ar9331_mdio_regmap_config);
963 if (IS_ERR(priv->regmap)) {
964 ret = PTR_ERR(priv->regmap);
965 dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
969 priv->sw_reset = devm_reset_control_get(&mdiodev->dev, "switch");
970 if (IS_ERR(priv->sw_reset)) {
971 dev_err(&mdiodev->dev, "missing switch reset\n");
972 return PTR_ERR(priv->sw_reset);
975 priv->sbus = mdiodev->bus;
976 priv->dev = &mdiodev->dev;
978 ret = ar9331_sw_irq_init(priv);
983 ds->dev = &mdiodev->dev;
984 ds->num_ports = AR9331_SW_PORTS;
986 priv->ops = ar9331_sw_ops;
987 ds->ops = &priv->ops;
988 dev_set_drvdata(&mdiodev->dev, priv);
990 for (i = 0; i < ARRAY_SIZE(priv->port); i++) {
991 struct ar9331_sw_port *port = &priv->port[i];
994 spin_lock_init(&port->stats_lock);
995 INIT_DELAYED_WORK(&port->mib_read, ar9331_do_stats_poll);
998 ret = dsa_register_switch(ds);
1000 goto err_remove_irq;
1005 irq_domain_remove(priv->irqdomain);
1010 static void ar9331_sw_remove(struct mdio_device *mdiodev)
1012 struct ar9331_sw_priv *priv = dev_get_drvdata(&mdiodev->dev);
1015 for (i = 0; i < ARRAY_SIZE(priv->port); i++) {
1016 struct ar9331_sw_port *port = &priv->port[i];
1018 cancel_delayed_work_sync(&port->mib_read);
1021 irq_domain_remove(priv->irqdomain);
1022 mdiobus_unregister(priv->mbus);
1023 dsa_unregister_switch(&priv->ds);
1025 reset_control_assert(priv->sw_reset);
1028 static const struct of_device_id ar9331_sw_of_match[] = {
1029 { .compatible = "qca,ar9331-switch" },
1033 static struct mdio_driver ar9331_sw_mdio_driver = {
1034 .probe = ar9331_sw_probe,
1035 .remove = ar9331_sw_remove,
1037 .name = AR9331_SW_NAME,
1038 .of_match_table = ar9331_sw_of_match,
1042 mdio_module_driver(ar9331_sw_mdio_driver);
1044 MODULE_AUTHOR("Oleksij Rempel <kernel@pengutronix.de>");
1045 MODULE_DESCRIPTION("Driver for Atheros AR9331 switch");
1046 MODULE_LICENSE("GPL v2");