1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
2 /* ADIN1110 Low Power 10BASE-T1L Ethernet MAC-PHY
3 * ADIN2111 2-Port Ethernet Switch with Integrated 10BASE-T1L PHY
5 * Copyright 2021 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/cache.h>
11 #include <linux/crc8.h>
12 #include <linux/etherdevice.h>
13 #include <linux/ethtool.h>
14 #include <linux/if_bridge.h>
15 #include <linux/interrupt.h>
16 #include <linux/iopoll.h>
17 #include <linux/gpio.h>
18 #include <linux/kernel.h>
19 #include <linux/mii.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/phy.h>
24 #include <linux/property.h>
25 #include <linux/spi/spi.h>
27 #include <net/switchdev.h>
29 #include <asm/unaligned.h>
31 #define ADIN1110_PHY_ID 0x1
33 #define ADIN1110_RESET 0x03
34 #define ADIN1110_SWRESET BIT(0)
36 #define ADIN1110_CONFIG1 0x04
37 #define ADIN1110_CONFIG1_SYNC BIT(15)
39 #define ADIN1110_CONFIG2 0x06
40 #define ADIN2111_P2_FWD_UNK2HOST BIT(12)
41 #define ADIN2111_PORT_CUT_THRU_EN BIT(11)
42 #define ADIN1110_CRC_APPEND BIT(5)
43 #define ADIN1110_FWD_UNK2HOST BIT(2)
45 #define ADIN1110_STATUS0 0x08
47 #define ADIN1110_STATUS1 0x09
48 #define ADIN2111_P2_RX_RDY BIT(17)
49 #define ADIN1110_SPI_ERR BIT(10)
50 #define ADIN1110_RX_RDY BIT(4)
52 #define ADIN1110_IMASK1 0x0D
53 #define ADIN2111_RX_RDY_IRQ BIT(17)
54 #define ADIN1110_SPI_ERR_IRQ BIT(10)
55 #define ADIN1110_RX_RDY_IRQ BIT(4)
56 #define ADIN1110_TX_RDY_IRQ BIT(3)
58 #define ADIN1110_MDIOACC 0x20
59 #define ADIN1110_MDIO_TRDONE BIT(31)
60 #define ADIN1110_MDIO_ST GENMASK(29, 28)
61 #define ADIN1110_MDIO_OP GENMASK(27, 26)
62 #define ADIN1110_MDIO_PRTAD GENMASK(25, 21)
63 #define ADIN1110_MDIO_DEVAD GENMASK(20, 16)
64 #define ADIN1110_MDIO_DATA GENMASK(15, 0)
66 #define ADIN1110_TX_FSIZE 0x30
67 #define ADIN1110_TX 0x31
68 #define ADIN1110_TX_SPACE 0x32
70 #define ADIN1110_MAC_ADDR_FILTER_UPR 0x50
71 #define ADIN2111_MAC_ADDR_APPLY2PORT2 BIT(31)
72 #define ADIN1110_MAC_ADDR_APPLY2PORT BIT(30)
73 #define ADIN2111_MAC_ADDR_TO_OTHER_PORT BIT(17)
74 #define ADIN1110_MAC_ADDR_TO_HOST BIT(16)
76 #define ADIN1110_MAC_ADDR_FILTER_LWR 0x51
78 #define ADIN1110_MAC_ADDR_MASK_UPR 0x70
79 #define ADIN1110_MAC_ADDR_MASK_LWR 0x71
81 #define ADIN1110_RX_FSIZE 0x90
82 #define ADIN1110_RX 0x91
84 #define ADIN2111_RX_P2_FSIZE 0xC0
85 #define ADIN2111_RX_P2 0xC1
87 #define ADIN1110_CLEAR_STATUS0 0xFFF
90 #define ADIN1110_MDIO_OP_WR 0x1
91 #define ADIN1110_MDIO_OP_RD 0x3
93 #define ADIN1110_CD BIT(7)
94 #define ADIN1110_WRITE BIT(5)
96 #define ADIN1110_MAX_BUFF 2048
97 #define ADIN1110_MAX_FRAMES_READ 64
98 #define ADIN1110_WR_HEADER_LEN 2
99 #define ADIN1110_FRAME_HEADER_LEN 2
100 #define ADIN1110_INTERNAL_SIZE_HEADER_LEN 2
101 #define ADIN1110_RD_HEADER_LEN 3
102 #define ADIN1110_REG_LEN 4
103 #define ADIN1110_FEC_LEN 4
105 #define ADIN1110_PHY_ID_VAL 0x0283BC91
106 #define ADIN2111_PHY_ID_VAL 0x0283BCA1
108 #define ADIN_MAC_MAX_PORTS 2
109 #define ADIN_MAC_MAX_ADDR_SLOTS 16
111 #define ADIN_MAC_MULTICAST_ADDR_SLOT 0
112 #define ADIN_MAC_BROADCAST_ADDR_SLOT 1
113 #define ADIN_MAC_P1_ADDR_SLOT 2
114 #define ADIN_MAC_P2_ADDR_SLOT 3
115 #define ADIN_MAC_FDB_ADDR_SLOT 4
117 DECLARE_CRC8_TABLE(adin1110_crc_table);
119 enum adin1110_chips_id {
124 struct adin1110_cfg {
125 enum adin1110_chips_id id;
126 char name[MDIO_NAME_SIZE];
127 u32 phy_ids[PHY_MAX_ADDR];
132 struct adin1110_port_priv {
133 struct adin1110_priv *priv;
134 struct net_device *netdev;
135 struct net_device *bridge;
136 struct phy_device *phydev;
137 struct work_struct tx_work;
142 struct work_struct rx_mode_work;
144 struct sk_buff_head txq;
147 struct adin1110_cfg *cfg;
150 struct adin1110_priv {
151 struct mutex lock; /* protect spi */
152 spinlock_t state_lock; /* protect RX mode */
153 struct mii_bus *mii_bus;
154 struct spi_device *spidev;
156 struct adin1110_cfg *cfg;
161 struct adin1110_port_priv *ports[ADIN_MAC_MAX_PORTS];
162 char mii_bus_name[MII_BUS_ID_SIZE];
163 u8 data[ADIN1110_MAX_BUFF] ____cacheline_aligned;
166 struct adin1110_switchdev_event_work {
167 struct work_struct work;
168 struct switchdev_notifier_fdb_info fdb_info;
169 struct adin1110_port_priv *port_priv;
173 static struct adin1110_cfg adin1110_cfgs[] = {
179 .phy_id_val = ADIN1110_PHY_ID_VAL,
186 .phy_id_val = ADIN2111_PHY_ID_VAL,
190 static u8 adin1110_crc_data(u8 *data, u32 len)
192 return crc8(adin1110_crc_table, data, len, 0);
195 static int adin1110_read_reg(struct adin1110_priv *priv, u16 reg, u32 *val)
197 u32 header_len = ADIN1110_RD_HEADER_LEN;
198 u32 read_len = ADIN1110_REG_LEN;
199 struct spi_transfer t = {0};
202 priv->data[0] = ADIN1110_CD | FIELD_GET(GENMASK(12, 8), reg);
203 priv->data[1] = FIELD_GET(GENMASK(7, 0), reg);
204 priv->data[2] = 0x00;
206 if (priv->append_crc) {
207 priv->data[2] = adin1110_crc_data(&priv->data[0], 2);
208 priv->data[3] = 0x00;
212 if (priv->append_crc)
215 memset(&priv->data[header_len], 0, read_len);
216 t.tx_buf = &priv->data[0];
217 t.rx_buf = &priv->data[0];
218 t.len = read_len + header_len;
220 ret = spi_sync_transfer(priv->spidev, &t, 1);
224 if (priv->append_crc) {
228 crc = adin1110_crc_data(&priv->data[header_len],
230 recv_crc = priv->data[header_len + ADIN1110_REG_LEN];
232 if (crc != recv_crc) {
233 dev_err_ratelimited(&priv->spidev->dev, "CRC error.");
238 *val = get_unaligned_be32(&priv->data[header_len]);
243 static int adin1110_write_reg(struct adin1110_priv *priv, u16 reg, u32 val)
245 u32 header_len = ADIN1110_WR_HEADER_LEN;
246 u32 write_len = ADIN1110_REG_LEN;
248 priv->data[0] = ADIN1110_CD | ADIN1110_WRITE | FIELD_GET(GENMASK(12, 8), reg);
249 priv->data[1] = FIELD_GET(GENMASK(7, 0), reg);
251 if (priv->append_crc) {
252 priv->data[2] = adin1110_crc_data(&priv->data[0], header_len);
256 put_unaligned_be32(val, &priv->data[header_len]);
257 if (priv->append_crc) {
258 priv->data[header_len + write_len] = adin1110_crc_data(&priv->data[header_len],
263 return spi_write(priv->spidev, &priv->data[0], header_len + write_len);
266 static int adin1110_set_bits(struct adin1110_priv *priv, u16 reg,
267 unsigned long mask, unsigned long val)
272 ret = adin1110_read_reg(priv, reg, &write_val);
276 set_mask_bits(&write_val, mask, val);
278 return adin1110_write_reg(priv, reg, write_val);
281 static int adin1110_round_len(int len)
283 /* can read/write only mutiples of 4 bytes of payload */
286 /* NOTE: ADIN1110_WR_HEADER_LEN should be used for write ops. */
287 if (len + ADIN1110_RD_HEADER_LEN > ADIN1110_MAX_BUFF)
293 static int adin1110_read_fifo(struct adin1110_port_priv *port_priv)
295 struct adin1110_priv *priv = port_priv->priv;
296 u32 header_len = ADIN1110_RD_HEADER_LEN;
297 struct spi_transfer t;
298 u32 frame_size_no_fcs;
305 if (!port_priv->nr) {
307 ret = adin1110_read_reg(priv, ADIN1110_RX_FSIZE, &frame_size);
309 reg = ADIN2111_RX_P2;
310 ret = adin1110_read_reg(priv, ADIN2111_RX_P2_FSIZE,
317 /* The read frame size includes the extra 2 bytes
318 * from the ADIN1110 frame header.
320 if (frame_size < ADIN1110_FRAME_HEADER_LEN + ADIN1110_FEC_LEN)
323 round_len = adin1110_round_len(frame_size);
327 frame_size_no_fcs = frame_size - ADIN1110_FRAME_HEADER_LEN - ADIN1110_FEC_LEN;
328 memset(priv->data, 0, ADIN1110_RD_HEADER_LEN);
330 priv->data[0] = ADIN1110_CD | FIELD_GET(GENMASK(12, 8), reg);
331 priv->data[1] = FIELD_GET(GENMASK(7, 0), reg);
333 if (priv->append_crc) {
334 priv->data[2] = adin1110_crc_data(&priv->data[0], 2);
338 rxb = netdev_alloc_skb(port_priv->netdev, round_len + header_len);
342 skb_put(rxb, frame_size_no_fcs + header_len + ADIN1110_FRAME_HEADER_LEN);
344 t.tx_buf = &priv->data[0];
345 t.rx_buf = &rxb->data[0];
346 t.len = header_len + round_len;
348 ret = spi_sync_transfer(priv->spidev, &t, 1);
354 skb_pull(rxb, header_len + ADIN1110_FRAME_HEADER_LEN);
355 rxb->protocol = eth_type_trans(rxb, port_priv->netdev);
357 if ((port_priv->flags & IFF_ALLMULTI && rxb->pkt_type == PACKET_MULTICAST) ||
358 (port_priv->flags & IFF_BROADCAST && rxb->pkt_type == PACKET_BROADCAST))
359 rxb->offload_fwd_mark = port_priv->priv->forwarding;
363 port_priv->rx_bytes += frame_size - ADIN1110_FRAME_HEADER_LEN;
364 port_priv->rx_packets++;
369 static int adin1110_write_fifo(struct adin1110_port_priv *port_priv,
372 struct adin1110_priv *priv = port_priv->priv;
373 u32 header_len = ADIN1110_WR_HEADER_LEN;
380 /* Pad frame to 64 byte length,
381 * MAC nor PHY will otherwise add the
383 * The FEC will be added by the MAC internally.
385 if (txb->len + ADIN1110_FEC_LEN < 64)
386 padding = 64 - (txb->len + ADIN1110_FEC_LEN);
388 padded_len = txb->len + padding + ADIN1110_FRAME_HEADER_LEN;
390 round_len = adin1110_round_len(padded_len);
394 ret = adin1110_write_reg(priv, ADIN1110_TX_FSIZE, padded_len);
398 memset(priv->data, 0, round_len + ADIN1110_WR_HEADER_LEN);
400 priv->data[0] = ADIN1110_CD | ADIN1110_WRITE;
401 priv->data[0] |= FIELD_GET(GENMASK(12, 8), ADIN1110_TX);
402 priv->data[1] = FIELD_GET(GENMASK(7, 0), ADIN1110_TX);
403 if (priv->append_crc) {
404 priv->data[2] = adin1110_crc_data(&priv->data[0], 2);
408 /* mention the port on which to send the frame in the frame header */
409 frame_header = cpu_to_be16(port_priv->nr);
410 memcpy(&priv->data[header_len], &frame_header,
411 ADIN1110_FRAME_HEADER_LEN);
413 memcpy(&priv->data[header_len + ADIN1110_FRAME_HEADER_LEN],
414 txb->data, txb->len);
416 ret = spi_write(priv->spidev, &priv->data[0], round_len + header_len);
420 port_priv->tx_bytes += txb->len;
421 port_priv->tx_packets++;
426 static int adin1110_read_mdio_acc(struct adin1110_priv *priv)
431 mutex_lock(&priv->lock);
432 ret = adin1110_read_reg(priv, ADIN1110_MDIOACC, &val);
433 mutex_unlock(&priv->lock);
440 static int adin1110_mdio_read(struct mii_bus *bus, int phy_id, int reg)
442 struct adin1110_priv *priv = bus->priv;
446 if (mdio_phy_id_is_c45(phy_id))
449 val |= FIELD_PREP(ADIN1110_MDIO_OP, ADIN1110_MDIO_OP_RD);
450 val |= FIELD_PREP(ADIN1110_MDIO_ST, 0x1);
451 val |= FIELD_PREP(ADIN1110_MDIO_PRTAD, phy_id);
452 val |= FIELD_PREP(ADIN1110_MDIO_DEVAD, reg);
454 /* write the clause 22 read command to the chip */
455 mutex_lock(&priv->lock);
456 ret = adin1110_write_reg(priv, ADIN1110_MDIOACC, val);
457 mutex_unlock(&priv->lock);
461 /* ADIN1110_MDIO_TRDONE BIT of the ADIN1110_MDIOACC
462 * register is set when the read is done.
463 * After the transaction is done, ADIN1110_MDIO_DATA
464 * bitfield of ADIN1110_MDIOACC register will contain
465 * the requested register value.
467 ret = readx_poll_timeout(adin1110_read_mdio_acc, priv, val,
468 (val & ADIN1110_MDIO_TRDONE), 10000, 30000);
472 return (val & ADIN1110_MDIO_DATA);
475 static int adin1110_mdio_write(struct mii_bus *bus, int phy_id,
476 int reg, u16 reg_val)
478 struct adin1110_priv *priv = bus->priv;
482 if (mdio_phy_id_is_c45(phy_id))
485 val |= FIELD_PREP(ADIN1110_MDIO_OP, ADIN1110_MDIO_OP_WR);
486 val |= FIELD_PREP(ADIN1110_MDIO_ST, 0x1);
487 val |= FIELD_PREP(ADIN1110_MDIO_PRTAD, phy_id);
488 val |= FIELD_PREP(ADIN1110_MDIO_DEVAD, reg);
489 val |= FIELD_PREP(ADIN1110_MDIO_DATA, reg_val);
491 /* write the clause 22 write command to the chip */
492 mutex_lock(&priv->lock);
493 ret = adin1110_write_reg(priv, ADIN1110_MDIOACC, val);
494 mutex_unlock(&priv->lock);
498 return readx_poll_timeout(adin1110_read_mdio_acc, priv, val,
499 (val & ADIN1110_MDIO_TRDONE), 10000, 30000);
502 /* ADIN1110 MAC-PHY contains an ADIN1100 PHY.
503 * ADIN2111 MAC-PHY contains two ADIN1100 PHYs.
504 * By registering a new MDIO bus we allow the PAL to discover
505 * the encapsulated PHY and probe the ADIN1100 driver.
507 static int adin1110_register_mdiobus(struct adin1110_priv *priv,
510 struct mii_bus *mii_bus;
513 mii_bus = devm_mdiobus_alloc(dev);
517 snprintf(priv->mii_bus_name, MII_BUS_ID_SIZE, "%s-%u",
518 priv->cfg->name, spi_get_chipselect(priv->spidev, 0));
520 mii_bus->name = priv->mii_bus_name;
521 mii_bus->read = adin1110_mdio_read;
522 mii_bus->write = adin1110_mdio_write;
523 mii_bus->priv = priv;
524 mii_bus->parent = dev;
525 mii_bus->phy_mask = ~((u32)GENMASK(2, 0));
526 snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
528 ret = devm_mdiobus_register(dev, mii_bus);
532 priv->mii_bus = mii_bus;
537 static bool adin1110_port_rx_ready(struct adin1110_port_priv *port_priv,
540 if (!netif_oper_up(port_priv->netdev))
544 return !!(status & ADIN1110_RX_RDY);
546 return !!(status & ADIN2111_P2_RX_RDY);
549 static void adin1110_read_frames(struct adin1110_port_priv *port_priv,
552 struct adin1110_priv *priv = port_priv->priv;
557 ret = adin1110_read_reg(priv, ADIN1110_STATUS1, &status1);
561 if (!adin1110_port_rx_ready(port_priv, status1))
564 ret = adin1110_read_fifo(port_priv);
572 static void adin1110_wake_queues(struct adin1110_priv *priv)
576 for (i = 0; i < priv->cfg->ports_nr; i++)
577 netif_wake_queue(priv->ports[i]->netdev);
580 static irqreturn_t adin1110_irq(int irq, void *p)
582 struct adin1110_priv *priv = p;
588 mutex_lock(&priv->lock);
590 ret = adin1110_read_reg(priv, ADIN1110_STATUS1, &status1);
594 if (priv->append_crc && (status1 & ADIN1110_SPI_ERR))
595 dev_warn_ratelimited(&priv->spidev->dev,
596 "SPI CRC error on write.\n");
598 ret = adin1110_read_reg(priv, ADIN1110_TX_SPACE, &val);
602 /* TX FIFO space is expressed in half-words */
603 priv->tx_space = 2 * val;
605 for (i = 0; i < priv->cfg->ports_nr; i++) {
606 if (adin1110_port_rx_ready(priv->ports[i], status1))
607 adin1110_read_frames(priv->ports[i],
608 ADIN1110_MAX_FRAMES_READ);
611 /* clear IRQ sources */
612 adin1110_write_reg(priv, ADIN1110_STATUS0, ADIN1110_CLEAR_STATUS0);
613 adin1110_write_reg(priv, ADIN1110_STATUS1, priv->irq_mask);
616 mutex_unlock(&priv->lock);
618 if (priv->tx_space > 0 && ret >= 0)
619 adin1110_wake_queues(priv);
624 /* ADIN1110 can filter up to 16 MAC addresses, mac_nr here is the slot used */
625 static int adin1110_write_mac_address(struct adin1110_port_priv *port_priv,
626 int mac_nr, const u8 *addr,
627 u8 *mask, u32 port_rules)
629 struct adin1110_priv *priv = port_priv->priv;
630 u32 offset = mac_nr * 2;
636 port_rules_mask = ADIN1110_MAC_ADDR_APPLY2PORT;
638 port_rules_mask = ADIN2111_MAC_ADDR_APPLY2PORT2;
640 if (port_rules & port_rules_mask)
641 port_rules_mask |= ADIN1110_MAC_ADDR_TO_HOST | ADIN2111_MAC_ADDR_TO_OTHER_PORT;
643 port_rules_mask |= GENMASK(15, 0);
644 val = port_rules | get_unaligned_be16(&addr[0]);
645 ret = adin1110_set_bits(priv, ADIN1110_MAC_ADDR_FILTER_UPR + offset,
646 port_rules_mask, val);
650 val = get_unaligned_be32(&addr[2]);
651 ret = adin1110_write_reg(priv,
652 ADIN1110_MAC_ADDR_FILTER_LWR + offset, val);
656 /* Only the first two MAC address slots support masking. */
657 if (mac_nr < ADIN_MAC_P1_ADDR_SLOT) {
658 val = get_unaligned_be16(&mask[0]);
659 ret = adin1110_write_reg(priv,
660 ADIN1110_MAC_ADDR_MASK_UPR + offset,
665 val = get_unaligned_be32(&mask[2]);
666 return adin1110_write_reg(priv,
667 ADIN1110_MAC_ADDR_MASK_LWR + offset,
674 static int adin1110_clear_mac_address(struct adin1110_priv *priv, int mac_nr)
676 u32 offset = mac_nr * 2;
679 ret = adin1110_write_reg(priv, ADIN1110_MAC_ADDR_FILTER_UPR + offset, 0);
683 ret = adin1110_write_reg(priv, ADIN1110_MAC_ADDR_FILTER_LWR + offset, 0);
687 /* only the first two MAC address slots are maskable */
689 ret = adin1110_write_reg(priv, ADIN1110_MAC_ADDR_MASK_UPR + offset, 0);
693 ret = adin1110_write_reg(priv, ADIN1110_MAC_ADDR_MASK_LWR + offset, 0);
699 static u32 adin1110_port_rules(struct adin1110_port_priv *port_priv,
701 bool fw_to_other_port)
706 port_rules |= ADIN1110_MAC_ADDR_APPLY2PORT;
708 port_rules |= ADIN2111_MAC_ADDR_APPLY2PORT2;
711 port_rules |= ADIN1110_MAC_ADDR_TO_HOST;
713 if (fw_to_other_port && port_priv->priv->forwarding)
714 port_rules |= ADIN2111_MAC_ADDR_TO_OTHER_PORT;
719 static int adin1110_multicast_filter(struct adin1110_port_priv *port_priv,
720 int mac_nr, bool accept_multicast)
722 u8 mask[ETH_ALEN] = {0};
723 u8 mac[ETH_ALEN] = {0};
729 if (accept_multicast && port_priv->state == BR_STATE_FORWARDING)
730 port_rules = adin1110_port_rules(port_priv, true, true);
732 return adin1110_write_mac_address(port_priv, mac_nr, mac,
736 static int adin1110_broadcasts_filter(struct adin1110_port_priv *port_priv,
737 int mac_nr, bool accept_broadcast)
742 eth_broadcast_addr(mask);
744 if (accept_broadcast && port_priv->state == BR_STATE_FORWARDING)
745 port_rules = adin1110_port_rules(port_priv, true, true);
747 return adin1110_write_mac_address(port_priv, mac_nr, mask,
751 static int adin1110_set_mac_address(struct net_device *netdev,
752 const unsigned char *dev_addr)
754 struct adin1110_port_priv *port_priv = netdev_priv(netdev);
759 if (!is_valid_ether_addr(dev_addr))
760 return -EADDRNOTAVAIL;
762 eth_hw_addr_set(netdev, dev_addr);
763 eth_broadcast_addr(mask);
765 mac_slot = (!port_priv->nr) ? ADIN_MAC_P1_ADDR_SLOT : ADIN_MAC_P2_ADDR_SLOT;
766 port_rules = adin1110_port_rules(port_priv, true, false);
768 return adin1110_write_mac_address(port_priv, mac_slot, netdev->dev_addr,
772 static int adin1110_ndo_set_mac_address(struct net_device *netdev, void *addr)
774 struct sockaddr *sa = addr;
777 ret = eth_prepare_mac_addr_change(netdev, addr);
781 return adin1110_set_mac_address(netdev, sa->sa_data);
784 static int adin1110_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
786 if (!netif_running(netdev))
789 return phy_do_ioctl(netdev, rq, cmd);
792 static int adin1110_set_promisc_mode(struct adin1110_port_priv *port_priv,
795 struct adin1110_priv *priv = port_priv->priv;
798 if (port_priv->state != BR_STATE_FORWARDING)
802 mask = ADIN1110_FWD_UNK2HOST;
804 mask = ADIN2111_P2_FWD_UNK2HOST;
806 return adin1110_set_bits(priv, ADIN1110_CONFIG2,
807 mask, promisc ? mask : 0);
810 static int adin1110_setup_rx_mode(struct adin1110_port_priv *port_priv)
814 ret = adin1110_set_promisc_mode(port_priv,
815 !!(port_priv->flags & IFF_PROMISC));
819 ret = adin1110_multicast_filter(port_priv, ADIN_MAC_MULTICAST_ADDR_SLOT,
820 !!(port_priv->flags & IFF_ALLMULTI));
824 ret = adin1110_broadcasts_filter(port_priv,
825 ADIN_MAC_BROADCAST_ADDR_SLOT,
826 !!(port_priv->flags & IFF_BROADCAST));
830 return adin1110_set_bits(port_priv->priv, ADIN1110_CONFIG1,
831 ADIN1110_CONFIG1_SYNC, ADIN1110_CONFIG1_SYNC);
834 static bool adin1110_can_offload_forwarding(struct adin1110_priv *priv)
838 if (priv->cfg->id != ADIN2111_MAC)
841 /* Can't enable forwarding if ports do not belong to the same bridge */
842 if (priv->ports[0]->bridge != priv->ports[1]->bridge || !priv->ports[0]->bridge)
845 /* Can't enable forwarding if there is a port
846 * that has been blocked by STP.
848 for (i = 0; i < priv->cfg->ports_nr; i++) {
849 if (priv->ports[i]->state != BR_STATE_FORWARDING)
856 static void adin1110_rx_mode_work(struct work_struct *work)
858 struct adin1110_port_priv *port_priv;
859 struct adin1110_priv *priv;
861 port_priv = container_of(work, struct adin1110_port_priv, rx_mode_work);
862 priv = port_priv->priv;
864 mutex_lock(&priv->lock);
865 adin1110_setup_rx_mode(port_priv);
866 mutex_unlock(&priv->lock);
869 static void adin1110_set_rx_mode(struct net_device *dev)
871 struct adin1110_port_priv *port_priv = netdev_priv(dev);
872 struct adin1110_priv *priv = port_priv->priv;
874 spin_lock(&priv->state_lock);
876 port_priv->flags = dev->flags;
877 schedule_work(&port_priv->rx_mode_work);
879 spin_unlock(&priv->state_lock);
882 static int adin1110_net_open(struct net_device *net_dev)
884 struct adin1110_port_priv *port_priv = netdev_priv(net_dev);
885 struct adin1110_priv *priv = port_priv->priv;
889 mutex_lock(&priv->lock);
891 /* Configure MAC to compute and append the FCS itself. */
892 ret = adin1110_write_reg(priv, ADIN1110_CONFIG2, ADIN1110_CRC_APPEND);
896 val = ADIN1110_TX_RDY_IRQ | ADIN1110_RX_RDY_IRQ | ADIN1110_SPI_ERR_IRQ;
897 if (priv->cfg->id == ADIN2111_MAC)
898 val |= ADIN2111_RX_RDY_IRQ;
900 priv->irq_mask = val;
901 ret = adin1110_write_reg(priv, ADIN1110_IMASK1, ~val);
903 netdev_err(net_dev, "Failed to enable chip IRQs: %d\n", ret);
907 ret = adin1110_read_reg(priv, ADIN1110_TX_SPACE, &val);
909 netdev_err(net_dev, "Failed to read TX FIFO space: %d\n", ret);
913 priv->tx_space = 2 * val;
915 port_priv->state = BR_STATE_FORWARDING;
916 ret = adin1110_set_mac_address(net_dev, net_dev->dev_addr);
918 netdev_err(net_dev, "Could not set MAC address: %pM, %d\n",
919 net_dev->dev_addr, ret);
923 ret = adin1110_set_bits(priv, ADIN1110_CONFIG1, ADIN1110_CONFIG1_SYNC,
924 ADIN1110_CONFIG1_SYNC);
927 mutex_unlock(&priv->lock);
932 phy_start(port_priv->phydev);
934 netif_start_queue(net_dev);
939 static int adin1110_net_stop(struct net_device *net_dev)
941 struct adin1110_port_priv *port_priv = netdev_priv(net_dev);
942 struct adin1110_priv *priv = port_priv->priv;
946 mask = !port_priv->nr ? ADIN2111_RX_RDY_IRQ : ADIN1110_RX_RDY_IRQ;
948 /* Disable RX RDY IRQs */
949 mutex_lock(&priv->lock);
950 ret = adin1110_set_bits(priv, ADIN1110_IMASK1, mask, mask);
951 mutex_unlock(&priv->lock);
955 netif_stop_queue(port_priv->netdev);
956 flush_work(&port_priv->tx_work);
957 phy_stop(port_priv->phydev);
962 static void adin1110_tx_work(struct work_struct *work)
964 struct adin1110_port_priv *port_priv;
965 struct adin1110_priv *priv;
969 port_priv = container_of(work, struct adin1110_port_priv, tx_work);
970 priv = port_priv->priv;
972 mutex_lock(&priv->lock);
974 while ((txb = skb_dequeue(&port_priv->txq))) {
975 ret = adin1110_write_fifo(port_priv, txb);
977 dev_err_ratelimited(&priv->spidev->dev,
978 "Frame write error: %d\n", ret);
983 mutex_unlock(&priv->lock);
986 static netdev_tx_t adin1110_start_xmit(struct sk_buff *skb, struct net_device *dev)
988 struct adin1110_port_priv *port_priv = netdev_priv(dev);
989 struct adin1110_priv *priv = port_priv->priv;
990 netdev_tx_t netdev_ret = NETDEV_TX_OK;
993 tx_space_needed = skb->len + ADIN1110_FRAME_HEADER_LEN + ADIN1110_INTERNAL_SIZE_HEADER_LEN;
994 if (tx_space_needed > priv->tx_space) {
995 netif_stop_queue(dev);
996 netdev_ret = NETDEV_TX_BUSY;
998 priv->tx_space -= tx_space_needed;
999 skb_queue_tail(&port_priv->txq, skb);
1002 schedule_work(&port_priv->tx_work);
1007 static void adin1110_ndo_get_stats64(struct net_device *dev,
1008 struct rtnl_link_stats64 *storage)
1010 struct adin1110_port_priv *port_priv = netdev_priv(dev);
1012 storage->rx_packets = port_priv->rx_packets;
1013 storage->tx_packets = port_priv->tx_packets;
1015 storage->rx_bytes = port_priv->rx_bytes;
1016 storage->tx_bytes = port_priv->tx_bytes;
1019 static int adin1110_port_get_port_parent_id(struct net_device *dev,
1020 struct netdev_phys_item_id *ppid)
1022 struct adin1110_port_priv *port_priv = netdev_priv(dev);
1023 struct adin1110_priv *priv = port_priv->priv;
1025 ppid->id_len = strnlen(priv->mii_bus_name, MAX_PHYS_ITEM_ID_LEN);
1026 memcpy(ppid->id, priv->mii_bus_name, ppid->id_len);
1031 static int adin1110_ndo_get_phys_port_name(struct net_device *dev,
1032 char *name, size_t len)
1034 struct adin1110_port_priv *port_priv = netdev_priv(dev);
1037 err = snprintf(name, len, "p%d", port_priv->nr);
1044 static const struct net_device_ops adin1110_netdev_ops = {
1045 .ndo_open = adin1110_net_open,
1046 .ndo_stop = adin1110_net_stop,
1047 .ndo_eth_ioctl = adin1110_ioctl,
1048 .ndo_start_xmit = adin1110_start_xmit,
1049 .ndo_set_mac_address = adin1110_ndo_set_mac_address,
1050 .ndo_set_rx_mode = adin1110_set_rx_mode,
1051 .ndo_validate_addr = eth_validate_addr,
1052 .ndo_get_stats64 = adin1110_ndo_get_stats64,
1053 .ndo_get_port_parent_id = adin1110_port_get_port_parent_id,
1054 .ndo_get_phys_port_name = adin1110_ndo_get_phys_port_name,
1057 static void adin1110_get_drvinfo(struct net_device *dev,
1058 struct ethtool_drvinfo *di)
1060 strscpy(di->driver, "ADIN1110", sizeof(di->driver));
1061 strscpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1064 static const struct ethtool_ops adin1110_ethtool_ops = {
1065 .get_drvinfo = adin1110_get_drvinfo,
1066 .get_link = ethtool_op_get_link,
1067 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1068 .set_link_ksettings = phy_ethtool_set_link_ksettings,
1071 static void adin1110_adjust_link(struct net_device *dev)
1073 struct phy_device *phydev = dev->phydev;
1076 phy_print_status(phydev);
1079 /* PHY ID is stored in the MAC registers too,
1080 * check spi connection by reading it.
1082 static int adin1110_check_spi(struct adin1110_priv *priv)
1084 struct gpio_desc *reset_gpio;
1088 reset_gpio = devm_gpiod_get_optional(&priv->spidev->dev, "reset",
1091 /* MISO pin is used for internal configuration, can't have
1092 * anyone else disturbing the SDO line.
1094 spi_bus_lock(priv->spidev->controller);
1096 gpiod_set_value(reset_gpio, 1);
1098 gpiod_set_value(reset_gpio, 0);
1100 /* Need to wait 90 ms before interacting with
1101 * the MAC after a HW reset.
1105 spi_bus_unlock(priv->spidev->controller);
1108 ret = adin1110_read_reg(priv, ADIN1110_PHY_ID, &val);
1112 if (val != priv->cfg->phy_id_val) {
1113 dev_err(&priv->spidev->dev, "PHY ID expected: %x, read: %x\n",
1114 priv->cfg->phy_id_val, val);
1121 static int adin1110_hw_forwarding(struct adin1110_priv *priv, bool enable)
1126 priv->forwarding = enable;
1128 if (!priv->forwarding) {
1129 for (i = ADIN_MAC_FDB_ADDR_SLOT; i < ADIN_MAC_MAX_ADDR_SLOTS; i++) {
1130 ret = adin1110_clear_mac_address(priv, i);
1136 /* Forwarding is optimised when MAC runs in Cut Through mode. */
1137 ret = adin1110_set_bits(priv, ADIN1110_CONFIG2,
1138 ADIN2111_PORT_CUT_THRU_EN,
1139 priv->forwarding ? ADIN2111_PORT_CUT_THRU_EN : 0);
1143 for (i = 0; i < priv->cfg->ports_nr; i++) {
1144 ret = adin1110_setup_rx_mode(priv->ports[i]);
1152 static int adin1110_port_bridge_join(struct adin1110_port_priv *port_priv,
1153 struct net_device *bridge)
1155 struct adin1110_priv *priv = port_priv->priv;
1158 port_priv->bridge = bridge;
1160 if (adin1110_can_offload_forwarding(priv)) {
1161 mutex_lock(&priv->lock);
1162 ret = adin1110_hw_forwarding(priv, true);
1163 mutex_unlock(&priv->lock);
1169 return adin1110_set_mac_address(port_priv->netdev, bridge->dev_addr);
1172 static int adin1110_port_bridge_leave(struct adin1110_port_priv *port_priv,
1173 struct net_device *bridge)
1175 struct adin1110_priv *priv = port_priv->priv;
1178 port_priv->bridge = NULL;
1180 mutex_lock(&priv->lock);
1181 ret = adin1110_hw_forwarding(priv, false);
1182 mutex_unlock(&priv->lock);
1187 static bool adin1110_port_dev_check(const struct net_device *dev)
1189 return dev->netdev_ops == &adin1110_netdev_ops;
1192 static int adin1110_netdevice_event(struct notifier_block *unused,
1193 unsigned long event, void *ptr)
1195 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1196 struct adin1110_port_priv *port_priv = netdev_priv(dev);
1197 struct netdev_notifier_changeupper_info *info = ptr;
1200 if (!adin1110_port_dev_check(dev))
1204 case NETDEV_CHANGEUPPER:
1205 if (netif_is_bridge_master(info->upper_dev)) {
1207 ret = adin1110_port_bridge_join(port_priv, info->upper_dev);
1209 ret = adin1110_port_bridge_leave(port_priv, info->upper_dev);
1216 return notifier_from_errno(ret);
1219 static struct notifier_block adin1110_netdevice_nb = {
1220 .notifier_call = adin1110_netdevice_event,
1223 static void adin1110_disconnect_phy(void *data)
1225 phy_disconnect(data);
1228 static int adin1110_port_set_forwarding_state(struct adin1110_port_priv *port_priv)
1230 struct adin1110_priv *priv = port_priv->priv;
1233 port_priv->state = BR_STATE_FORWARDING;
1235 mutex_lock(&priv->lock);
1236 ret = adin1110_set_mac_address(port_priv->netdev,
1237 port_priv->netdev->dev_addr);
1241 if (adin1110_can_offload_forwarding(priv))
1242 ret = adin1110_hw_forwarding(priv, true);
1244 ret = adin1110_setup_rx_mode(port_priv);
1246 mutex_unlock(&priv->lock);
1251 static int adin1110_port_set_blocking_state(struct adin1110_port_priv *port_priv)
1253 u8 mac[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x00};
1254 struct adin1110_priv *priv = port_priv->priv;
1260 port_priv->state = BR_STATE_BLOCKING;
1262 mutex_lock(&priv->lock);
1264 mac_slot = (!port_priv->nr) ? ADIN_MAC_P1_ADDR_SLOT : ADIN_MAC_P2_ADDR_SLOT;
1265 ret = adin1110_clear_mac_address(priv, mac_slot);
1269 ret = adin1110_hw_forwarding(priv, false);
1273 /* Allow only BPDUs to be passed to the CPU */
1274 eth_broadcast_addr(mask);
1275 port_rules = adin1110_port_rules(port_priv, true, false);
1276 ret = adin1110_write_mac_address(port_priv, mac_slot, mac,
1279 mutex_unlock(&priv->lock);
1284 /* ADIN1110/2111 does not have any native STP support.
1285 * Listen for bridge core state changes and
1286 * allow all frames to pass or only the BPDUs.
1288 static int adin1110_port_attr_stp_state_set(struct adin1110_port_priv *port_priv,
1292 case BR_STATE_FORWARDING:
1293 return adin1110_port_set_forwarding_state(port_priv);
1294 case BR_STATE_LEARNING:
1295 case BR_STATE_LISTENING:
1296 case BR_STATE_DISABLED:
1297 case BR_STATE_BLOCKING:
1298 return adin1110_port_set_blocking_state(port_priv);
1304 static int adin1110_port_attr_set(struct net_device *dev, const void *ctx,
1305 const struct switchdev_attr *attr,
1306 struct netlink_ext_ack *extack)
1308 struct adin1110_port_priv *port_priv = netdev_priv(dev);
1311 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1312 return adin1110_port_attr_stp_state_set(port_priv,
1319 static int adin1110_switchdev_blocking_event(struct notifier_block *unused,
1320 unsigned long event,
1323 struct net_device *netdev = switchdev_notifier_info_to_dev(ptr);
1326 if (event == SWITCHDEV_PORT_ATTR_SET) {
1327 ret = switchdev_handle_port_attr_set(netdev, ptr,
1328 adin1110_port_dev_check,
1329 adin1110_port_attr_set);
1331 return notifier_from_errno(ret);
1337 static struct notifier_block adin1110_switchdev_blocking_notifier = {
1338 .notifier_call = adin1110_switchdev_blocking_event,
1341 static void adin1110_fdb_offload_notify(struct net_device *netdev,
1342 struct switchdev_notifier_fdb_info *rcv)
1344 struct switchdev_notifier_fdb_info info = {};
1346 info.addr = rcv->addr;
1347 info.vid = rcv->vid;
1348 info.offloaded = true;
1349 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
1350 netdev, &info.info, NULL);
1353 static int adin1110_fdb_add(struct adin1110_port_priv *port_priv,
1354 struct switchdev_notifier_fdb_info *fdb)
1356 struct adin1110_priv *priv = port_priv->priv;
1357 struct adin1110_port_priv *other_port;
1364 netdev_dbg(port_priv->netdev,
1365 "DEBUG: %s: MACID = %pM vid = %u flags = %u %u -- port %d\n",
1366 __func__, fdb->addr, fdb->vid, fdb->added_by_user,
1367 fdb->offloaded, port_priv->nr);
1369 if (!priv->forwarding)
1375 /* Find free FDB slot on device. */
1376 for (mac_nr = ADIN_MAC_FDB_ADDR_SLOT; mac_nr < ADIN_MAC_MAX_ADDR_SLOTS; mac_nr++) {
1377 ret = adin1110_read_reg(priv, ADIN1110_MAC_ADDR_FILTER_UPR + (mac_nr * 2), &val);
1384 if (mac_nr == ADIN_MAC_MAX_ADDR_SLOTS)
1387 other_port = priv->ports[!port_priv->nr];
1388 port_rules = adin1110_port_rules(other_port, false, true);
1389 eth_broadcast_addr(mask);
1391 return adin1110_write_mac_address(other_port, mac_nr, (u8 *)fdb->addr,
1395 static int adin1110_read_mac(struct adin1110_priv *priv, int mac_nr, u8 *addr)
1400 ret = adin1110_read_reg(priv, ADIN1110_MAC_ADDR_FILTER_UPR + (mac_nr * 2), &val);
1404 put_unaligned_be16(val, addr);
1406 ret = adin1110_read_reg(priv, ADIN1110_MAC_ADDR_FILTER_LWR + (mac_nr * 2), &val);
1410 put_unaligned_be32(val, addr + 2);
1415 static int adin1110_fdb_del(struct adin1110_port_priv *port_priv,
1416 struct switchdev_notifier_fdb_info *fdb)
1418 struct adin1110_priv *priv = port_priv->priv;
1423 netdev_dbg(port_priv->netdev,
1424 "DEBUG: %s: MACID = %pM vid = %u flags = %u %u -- port %d\n",
1425 __func__, fdb->addr, fdb->vid, fdb->added_by_user,
1426 fdb->offloaded, port_priv->nr);
1431 for (mac_nr = ADIN_MAC_FDB_ADDR_SLOT; mac_nr < ADIN_MAC_MAX_ADDR_SLOTS; mac_nr++) {
1432 ret = adin1110_read_mac(priv, mac_nr, addr);
1436 if (ether_addr_equal(addr, fdb->addr)) {
1437 ret = adin1110_clear_mac_address(priv, mac_nr);
1446 static void adin1110_switchdev_event_work(struct work_struct *work)
1448 struct adin1110_switchdev_event_work *switchdev_work;
1449 struct adin1110_port_priv *port_priv;
1452 switchdev_work = container_of(work, struct adin1110_switchdev_event_work, work);
1453 port_priv = switchdev_work->port_priv;
1455 mutex_lock(&port_priv->priv->lock);
1457 switch (switchdev_work->event) {
1458 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1459 ret = adin1110_fdb_add(port_priv, &switchdev_work->fdb_info);
1461 adin1110_fdb_offload_notify(port_priv->netdev,
1462 &switchdev_work->fdb_info);
1464 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1465 adin1110_fdb_del(port_priv, &switchdev_work->fdb_info);
1471 mutex_unlock(&port_priv->priv->lock);
1473 kfree(switchdev_work->fdb_info.addr);
1474 kfree(switchdev_work);
1475 dev_put(port_priv->netdev);
1478 /* called under rcu_read_lock() */
1479 static int adin1110_switchdev_event(struct notifier_block *unused,
1480 unsigned long event, void *ptr)
1482 struct net_device *netdev = switchdev_notifier_info_to_dev(ptr);
1483 struct adin1110_port_priv *port_priv = netdev_priv(netdev);
1484 struct adin1110_switchdev_event_work *switchdev_work;
1485 struct switchdev_notifier_fdb_info *fdb_info = ptr;
1487 if (!adin1110_port_dev_check(netdev))
1490 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1491 if (WARN_ON(!switchdev_work))
1494 INIT_WORK(&switchdev_work->work, adin1110_switchdev_event_work);
1495 switchdev_work->port_priv = port_priv;
1496 switchdev_work->event = event;
1499 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1500 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1501 memcpy(&switchdev_work->fdb_info, ptr,
1502 sizeof(switchdev_work->fdb_info));
1503 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1505 if (!switchdev_work->fdb_info.addr)
1506 goto err_addr_alloc;
1508 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1513 kfree(switchdev_work);
1517 queue_work(system_long_wq, &switchdev_work->work);
1522 kfree(switchdev_work);
1526 static struct notifier_block adin1110_switchdev_notifier = {
1527 .notifier_call = adin1110_switchdev_event,
1530 static void adin1110_unregister_notifiers(void)
1532 unregister_switchdev_blocking_notifier(&adin1110_switchdev_blocking_notifier);
1533 unregister_switchdev_notifier(&adin1110_switchdev_notifier);
1534 unregister_netdevice_notifier(&adin1110_netdevice_nb);
1537 static int adin1110_setup_notifiers(void)
1541 ret = register_netdevice_notifier(&adin1110_netdevice_nb);
1545 ret = register_switchdev_notifier(&adin1110_switchdev_notifier);
1549 ret = register_switchdev_blocking_notifier(&adin1110_switchdev_blocking_notifier);
1556 unregister_switchdev_notifier(&adin1110_switchdev_notifier);
1559 unregister_netdevice_notifier(&adin1110_netdevice_nb);
1564 static int adin1110_probe_netdevs(struct adin1110_priv *priv)
1566 struct device *dev = &priv->spidev->dev;
1567 struct adin1110_port_priv *port_priv;
1568 struct net_device *netdev;
1572 for (i = 0; i < priv->cfg->ports_nr; i++) {
1573 netdev = devm_alloc_etherdev(dev, sizeof(*port_priv));
1577 port_priv = netdev_priv(netdev);
1578 port_priv->netdev = netdev;
1579 port_priv->priv = priv;
1580 port_priv->cfg = priv->cfg;
1582 priv->ports[i] = port_priv;
1583 SET_NETDEV_DEV(netdev, dev);
1585 ret = device_get_ethdev_address(dev, netdev);
1589 netdev->irq = priv->spidev->irq;
1590 INIT_WORK(&port_priv->tx_work, adin1110_tx_work);
1591 INIT_WORK(&port_priv->rx_mode_work, adin1110_rx_mode_work);
1592 skb_queue_head_init(&port_priv->txq);
1594 netif_carrier_off(netdev);
1596 netdev->if_port = IF_PORT_10BASET;
1597 netdev->netdev_ops = &adin1110_netdev_ops;
1598 netdev->ethtool_ops = &adin1110_ethtool_ops;
1599 netdev->priv_flags |= IFF_UNICAST_FLT;
1600 netdev->features |= NETIF_F_NETNS_LOCAL;
1602 port_priv->phydev = get_phy_device(priv->mii_bus, i + 1, false);
1603 if (IS_ERR(port_priv->phydev)) {
1604 netdev_err(netdev, "Could not find PHY with device address: %d.\n", i);
1605 return PTR_ERR(port_priv->phydev);
1608 port_priv->phydev = phy_connect(netdev,
1609 phydev_name(port_priv->phydev),
1610 adin1110_adjust_link,
1611 PHY_INTERFACE_MODE_INTERNAL);
1612 if (IS_ERR(port_priv->phydev)) {
1613 netdev_err(netdev, "Could not connect PHY with device address: %d.\n", i);
1614 return PTR_ERR(port_priv->phydev);
1617 ret = devm_add_action_or_reset(dev, adin1110_disconnect_phy,
1623 /* ADIN1110 INT_N pin will be used to signal the host */
1624 ret = devm_request_threaded_irq(dev, priv->spidev->irq, NULL,
1626 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1627 dev_name(dev), priv);
1631 for (i = 0; i < priv->cfg->ports_nr; i++) {
1632 ret = devm_register_netdev(dev, priv->ports[i]->netdev);
1634 dev_err(dev, "Failed to register network device.\n");
1642 static int adin1110_probe(struct spi_device *spi)
1644 const struct spi_device_id *dev_id = spi_get_device_id(spi);
1645 struct device *dev = &spi->dev;
1646 struct adin1110_priv *priv;
1649 priv = devm_kzalloc(dev, sizeof(struct adin1110_priv), GFP_KERNEL);
1654 priv->cfg = &adin1110_cfgs[dev_id->driver_data];
1655 spi->bits_per_word = 8;
1656 spi->mode = SPI_MODE_0;
1658 mutex_init(&priv->lock);
1659 spin_lock_init(&priv->state_lock);
1661 /* use of CRC on control and data transactions is pin dependent */
1662 priv->append_crc = device_property_read_bool(dev, "adi,spi-crc");
1663 if (priv->append_crc)
1664 crc8_populate_msb(adin1110_crc_table, 0x7);
1666 ret = adin1110_check_spi(priv);
1668 dev_err(dev, "Probe SPI Read check failed: %d\n", ret);
1672 ret = adin1110_write_reg(priv, ADIN1110_RESET, ADIN1110_SWRESET);
1676 ret = adin1110_register_mdiobus(priv, dev);
1678 dev_err(dev, "Could not register MDIO bus %d\n", ret);
1682 return adin1110_probe_netdevs(priv);
1685 static const struct of_device_id adin1110_match_table[] = {
1686 { .compatible = "adi,adin1110" },
1687 { .compatible = "adi,adin2111" },
1690 MODULE_DEVICE_TABLE(of, adin1110_match_table);
1692 static const struct spi_device_id adin1110_spi_id[] = {
1693 { .name = "adin1110", .driver_data = ADIN1110_MAC },
1694 { .name = "adin2111", .driver_data = ADIN2111_MAC },
1697 MODULE_DEVICE_TABLE(spi, adin1110_spi_id);
1699 static struct spi_driver adin1110_driver = {
1702 .of_match_table = adin1110_match_table,
1704 .probe = adin1110_probe,
1705 .id_table = adin1110_spi_id,
1708 static int __init adin1110_driver_init(void)
1712 ret = adin1110_setup_notifiers();
1716 ret = spi_register_driver(&adin1110_driver);
1718 adin1110_unregister_notifiers();
1725 static void __exit adin1110_exit(void)
1727 adin1110_unregister_notifiers();
1728 spi_unregister_driver(&adin1110_driver);
1730 module_init(adin1110_driver_init);
1731 module_exit(adin1110_exit);
1733 MODULE_DESCRIPTION("ADIN1110 Network driver");
1734 MODULE_AUTHOR("Alexandru Tachici <alexandru.tachici@analog.com>");
1735 MODULE_LICENSE("Dual BSD/GPL");