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/gpio/consumer.h>
15 #include <linux/if_bridge.h>
16 #include <linux/interrupt.h>
17 #include <linux/iopoll.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 <linux/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
= {0};
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_atomic(adin1110_read_mdio_acc
, priv
, val
,
468 (val
& ADIN1110_MDIO_TRDONE
),
473 return (val
& ADIN1110_MDIO_DATA
);
476 static int adin1110_mdio_write(struct mii_bus
*bus
, int phy_id
,
477 int reg
, u16 reg_val
)
479 struct adin1110_priv
*priv
= bus
->priv
;
483 if (mdio_phy_id_is_c45(phy_id
))
486 val
|= FIELD_PREP(ADIN1110_MDIO_OP
, ADIN1110_MDIO_OP_WR
);
487 val
|= FIELD_PREP(ADIN1110_MDIO_ST
, 0x1);
488 val
|= FIELD_PREP(ADIN1110_MDIO_PRTAD
, phy_id
);
489 val
|= FIELD_PREP(ADIN1110_MDIO_DEVAD
, reg
);
490 val
|= FIELD_PREP(ADIN1110_MDIO_DATA
, reg_val
);
492 /* write the clause 22 write command to the chip */
493 mutex_lock(&priv
->lock
);
494 ret
= adin1110_write_reg(priv
, ADIN1110_MDIOACC
, val
);
495 mutex_unlock(&priv
->lock
);
499 return readx_poll_timeout_atomic(adin1110_read_mdio_acc
, priv
, val
,
500 (val
& ADIN1110_MDIO_TRDONE
),
504 /* ADIN1110 MAC-PHY contains an ADIN1100 PHY.
505 * ADIN2111 MAC-PHY contains two ADIN1100 PHYs.
506 * By registering a new MDIO bus we allow the PAL to discover
507 * the encapsulated PHY and probe the ADIN1100 driver.
509 static int adin1110_register_mdiobus(struct adin1110_priv
*priv
,
512 struct mii_bus
*mii_bus
;
515 mii_bus
= devm_mdiobus_alloc(dev
);
519 snprintf(priv
->mii_bus_name
, MII_BUS_ID_SIZE
, "%s-%u",
520 priv
->cfg
->name
, spi_get_chipselect(priv
->spidev
, 0));
522 mii_bus
->name
= priv
->mii_bus_name
;
523 mii_bus
->read
= adin1110_mdio_read
;
524 mii_bus
->write
= adin1110_mdio_write
;
525 mii_bus
->priv
= priv
;
526 mii_bus
->parent
= dev
;
527 mii_bus
->phy_mask
= ~((u32
)GENMASK(2, 0));
528 snprintf(mii_bus
->id
, MII_BUS_ID_SIZE
, "%s", dev_name(dev
));
530 ret
= devm_mdiobus_register(dev
, mii_bus
);
534 priv
->mii_bus
= mii_bus
;
539 static bool adin1110_port_rx_ready(struct adin1110_port_priv
*port_priv
,
542 if (!netif_oper_up(port_priv
->netdev
))
546 return !!(status
& ADIN1110_RX_RDY
);
548 return !!(status
& ADIN2111_P2_RX_RDY
);
551 static void adin1110_read_frames(struct adin1110_port_priv
*port_priv
,
554 struct adin1110_priv
*priv
= port_priv
->priv
;
559 ret
= adin1110_read_reg(priv
, ADIN1110_STATUS1
, &status1
);
563 if (!adin1110_port_rx_ready(port_priv
, status1
))
566 ret
= adin1110_read_fifo(port_priv
);
574 static void adin1110_wake_queues(struct adin1110_priv
*priv
)
578 for (i
= 0; i
< priv
->cfg
->ports_nr
; i
++)
579 netif_wake_queue(priv
->ports
[i
]->netdev
);
582 static irqreturn_t
adin1110_irq(int irq
, void *p
)
584 struct adin1110_priv
*priv
= p
;
590 mutex_lock(&priv
->lock
);
592 ret
= adin1110_read_reg(priv
, ADIN1110_STATUS1
, &status1
);
596 if (priv
->append_crc
&& (status1
& ADIN1110_SPI_ERR
))
597 dev_warn_ratelimited(&priv
->spidev
->dev
,
598 "SPI CRC error on write.\n");
600 ret
= adin1110_read_reg(priv
, ADIN1110_TX_SPACE
, &val
);
604 /* TX FIFO space is expressed in half-words */
605 priv
->tx_space
= 2 * val
;
607 for (i
= 0; i
< priv
->cfg
->ports_nr
; i
++) {
608 if (adin1110_port_rx_ready(priv
->ports
[i
], status1
))
609 adin1110_read_frames(priv
->ports
[i
],
610 ADIN1110_MAX_FRAMES_READ
);
613 /* clear IRQ sources */
614 adin1110_write_reg(priv
, ADIN1110_STATUS0
, ADIN1110_CLEAR_STATUS0
);
615 adin1110_write_reg(priv
, ADIN1110_STATUS1
, priv
->irq_mask
);
618 mutex_unlock(&priv
->lock
);
620 if (priv
->tx_space
> 0 && ret
>= 0)
621 adin1110_wake_queues(priv
);
626 /* ADIN1110 can filter up to 16 MAC addresses, mac_nr here is the slot used */
627 static int adin1110_write_mac_address(struct adin1110_port_priv
*port_priv
,
628 int mac_nr
, const u8
*addr
,
629 u8
*mask
, u32 port_rules
)
631 struct adin1110_priv
*priv
= port_priv
->priv
;
632 u32 offset
= mac_nr
* 2;
638 port_rules_mask
= ADIN1110_MAC_ADDR_APPLY2PORT
;
640 port_rules_mask
= ADIN2111_MAC_ADDR_APPLY2PORT2
;
642 if (port_rules
& port_rules_mask
)
643 port_rules_mask
|= ADIN1110_MAC_ADDR_TO_HOST
| ADIN2111_MAC_ADDR_TO_OTHER_PORT
;
645 port_rules_mask
|= GENMASK(15, 0);
646 val
= port_rules
| get_unaligned_be16(&addr
[0]);
647 ret
= adin1110_set_bits(priv
, ADIN1110_MAC_ADDR_FILTER_UPR
+ offset
,
648 port_rules_mask
, val
);
652 val
= get_unaligned_be32(&addr
[2]);
653 ret
= adin1110_write_reg(priv
,
654 ADIN1110_MAC_ADDR_FILTER_LWR
+ offset
, val
);
658 /* Only the first two MAC address slots support masking. */
659 if (mac_nr
< ADIN_MAC_P1_ADDR_SLOT
) {
660 val
= get_unaligned_be16(&mask
[0]);
661 ret
= adin1110_write_reg(priv
,
662 ADIN1110_MAC_ADDR_MASK_UPR
+ offset
,
667 val
= get_unaligned_be32(&mask
[2]);
668 return adin1110_write_reg(priv
,
669 ADIN1110_MAC_ADDR_MASK_LWR
+ offset
,
676 static int adin1110_clear_mac_address(struct adin1110_priv
*priv
, int mac_nr
)
678 u32 offset
= mac_nr
* 2;
681 ret
= adin1110_write_reg(priv
, ADIN1110_MAC_ADDR_FILTER_UPR
+ offset
, 0);
685 ret
= adin1110_write_reg(priv
, ADIN1110_MAC_ADDR_FILTER_LWR
+ offset
, 0);
689 /* only the first two MAC address slots are maskable */
691 ret
= adin1110_write_reg(priv
, ADIN1110_MAC_ADDR_MASK_UPR
+ offset
, 0);
695 ret
= adin1110_write_reg(priv
, ADIN1110_MAC_ADDR_MASK_LWR
+ offset
, 0);
701 static u32
adin1110_port_rules(struct adin1110_port_priv
*port_priv
,
703 bool fw_to_other_port
)
708 port_rules
|= ADIN1110_MAC_ADDR_APPLY2PORT
;
710 port_rules
|= ADIN2111_MAC_ADDR_APPLY2PORT2
;
713 port_rules
|= ADIN1110_MAC_ADDR_TO_HOST
;
715 if (fw_to_other_port
&& port_priv
->priv
->forwarding
)
716 port_rules
|= ADIN2111_MAC_ADDR_TO_OTHER_PORT
;
721 static int adin1110_multicast_filter(struct adin1110_port_priv
*port_priv
,
722 int mac_nr
, bool accept_multicast
)
724 u8 mask
[ETH_ALEN
] = {0};
725 u8 mac
[ETH_ALEN
] = {0};
731 if (accept_multicast
&& port_priv
->state
== BR_STATE_FORWARDING
)
732 port_rules
= adin1110_port_rules(port_priv
, true, true);
734 return adin1110_write_mac_address(port_priv
, mac_nr
, mac
,
738 static int adin1110_broadcasts_filter(struct adin1110_port_priv
*port_priv
,
739 int mac_nr
, bool accept_broadcast
)
744 eth_broadcast_addr(mask
);
746 if (accept_broadcast
&& port_priv
->state
== BR_STATE_FORWARDING
)
747 port_rules
= adin1110_port_rules(port_priv
, true, true);
749 return adin1110_write_mac_address(port_priv
, mac_nr
, mask
,
753 static int adin1110_set_mac_address(struct net_device
*netdev
,
754 const unsigned char *dev_addr
)
756 struct adin1110_port_priv
*port_priv
= netdev_priv(netdev
);
761 if (!is_valid_ether_addr(dev_addr
))
762 return -EADDRNOTAVAIL
;
764 eth_hw_addr_set(netdev
, dev_addr
);
765 eth_broadcast_addr(mask
);
767 mac_slot
= (!port_priv
->nr
) ? ADIN_MAC_P1_ADDR_SLOT
: ADIN_MAC_P2_ADDR_SLOT
;
768 port_rules
= adin1110_port_rules(port_priv
, true, false);
770 return adin1110_write_mac_address(port_priv
, mac_slot
, netdev
->dev_addr
,
774 static int adin1110_ndo_set_mac_address(struct net_device
*netdev
, void *addr
)
776 struct sockaddr
*sa
= addr
;
779 ret
= eth_prepare_mac_addr_change(netdev
, addr
);
783 return adin1110_set_mac_address(netdev
, sa
->sa_data
);
786 static int adin1110_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
788 if (!netif_running(netdev
))
791 return phy_do_ioctl(netdev
, rq
, cmd
);
794 static int adin1110_set_promisc_mode(struct adin1110_port_priv
*port_priv
,
797 struct adin1110_priv
*priv
= port_priv
->priv
;
800 if (port_priv
->state
!= BR_STATE_FORWARDING
)
804 mask
= ADIN1110_FWD_UNK2HOST
;
806 mask
= ADIN2111_P2_FWD_UNK2HOST
;
808 return adin1110_set_bits(priv
, ADIN1110_CONFIG2
,
809 mask
, promisc
? mask
: 0);
812 static int adin1110_setup_rx_mode(struct adin1110_port_priv
*port_priv
)
816 ret
= adin1110_set_promisc_mode(port_priv
,
817 !!(port_priv
->flags
& IFF_PROMISC
));
821 ret
= adin1110_multicast_filter(port_priv
, ADIN_MAC_MULTICAST_ADDR_SLOT
,
822 !!(port_priv
->flags
& IFF_ALLMULTI
));
826 ret
= adin1110_broadcasts_filter(port_priv
,
827 ADIN_MAC_BROADCAST_ADDR_SLOT
,
828 !!(port_priv
->flags
& IFF_BROADCAST
));
832 return adin1110_set_bits(port_priv
->priv
, ADIN1110_CONFIG1
,
833 ADIN1110_CONFIG1_SYNC
, ADIN1110_CONFIG1_SYNC
);
836 static bool adin1110_can_offload_forwarding(struct adin1110_priv
*priv
)
840 if (priv
->cfg
->id
!= ADIN2111_MAC
)
843 /* Can't enable forwarding if ports do not belong to the same bridge */
844 if (priv
->ports
[0]->bridge
!= priv
->ports
[1]->bridge
|| !priv
->ports
[0]->bridge
)
847 /* Can't enable forwarding if there is a port
848 * that has been blocked by STP.
850 for (i
= 0; i
< priv
->cfg
->ports_nr
; i
++) {
851 if (priv
->ports
[i
]->state
!= BR_STATE_FORWARDING
)
858 static void adin1110_rx_mode_work(struct work_struct
*work
)
860 struct adin1110_port_priv
*port_priv
;
861 struct adin1110_priv
*priv
;
863 port_priv
= container_of(work
, struct adin1110_port_priv
, rx_mode_work
);
864 priv
= port_priv
->priv
;
866 mutex_lock(&priv
->lock
);
867 adin1110_setup_rx_mode(port_priv
);
868 mutex_unlock(&priv
->lock
);
871 static void adin1110_set_rx_mode(struct net_device
*dev
)
873 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
874 struct adin1110_priv
*priv
= port_priv
->priv
;
876 spin_lock(&priv
->state_lock
);
878 port_priv
->flags
= dev
->flags
;
879 schedule_work(&port_priv
->rx_mode_work
);
881 spin_unlock(&priv
->state_lock
);
884 static int adin1110_net_open(struct net_device
*net_dev
)
886 struct adin1110_port_priv
*port_priv
= netdev_priv(net_dev
);
887 struct adin1110_priv
*priv
= port_priv
->priv
;
891 mutex_lock(&priv
->lock
);
893 /* Configure MAC to compute and append the FCS itself. */
894 ret
= adin1110_write_reg(priv
, ADIN1110_CONFIG2
, ADIN1110_CRC_APPEND
);
898 val
= ADIN1110_TX_RDY_IRQ
| ADIN1110_RX_RDY_IRQ
| ADIN1110_SPI_ERR_IRQ
;
899 if (priv
->cfg
->id
== ADIN2111_MAC
)
900 val
|= ADIN2111_RX_RDY_IRQ
;
902 priv
->irq_mask
= val
;
903 ret
= adin1110_write_reg(priv
, ADIN1110_IMASK1
, ~val
);
905 netdev_err(net_dev
, "Failed to enable chip IRQs: %d\n", ret
);
909 ret
= adin1110_read_reg(priv
, ADIN1110_TX_SPACE
, &val
);
911 netdev_err(net_dev
, "Failed to read TX FIFO space: %d\n", ret
);
915 priv
->tx_space
= 2 * val
;
917 port_priv
->state
= BR_STATE_FORWARDING
;
918 ret
= adin1110_set_mac_address(net_dev
, net_dev
->dev_addr
);
920 netdev_err(net_dev
, "Could not set MAC address: %pM, %d\n",
921 net_dev
->dev_addr
, ret
);
925 ret
= adin1110_set_bits(priv
, ADIN1110_CONFIG1
, ADIN1110_CONFIG1_SYNC
,
926 ADIN1110_CONFIG1_SYNC
);
929 mutex_unlock(&priv
->lock
);
934 phy_start(port_priv
->phydev
);
936 netif_start_queue(net_dev
);
941 static int adin1110_net_stop(struct net_device
*net_dev
)
943 struct adin1110_port_priv
*port_priv
= netdev_priv(net_dev
);
944 struct adin1110_priv
*priv
= port_priv
->priv
;
948 mask
= !port_priv
->nr
? ADIN2111_RX_RDY_IRQ
: ADIN1110_RX_RDY_IRQ
;
950 /* Disable RX RDY IRQs */
951 mutex_lock(&priv
->lock
);
952 ret
= adin1110_set_bits(priv
, ADIN1110_IMASK1
, mask
, mask
);
953 mutex_unlock(&priv
->lock
);
957 netif_stop_queue(port_priv
->netdev
);
958 flush_work(&port_priv
->tx_work
);
959 phy_stop(port_priv
->phydev
);
964 static void adin1110_tx_work(struct work_struct
*work
)
966 struct adin1110_port_priv
*port_priv
;
967 struct adin1110_priv
*priv
;
971 port_priv
= container_of(work
, struct adin1110_port_priv
, tx_work
);
972 priv
= port_priv
->priv
;
974 mutex_lock(&priv
->lock
);
976 while ((txb
= skb_dequeue(&port_priv
->txq
))) {
977 ret
= adin1110_write_fifo(port_priv
, txb
);
979 dev_err_ratelimited(&priv
->spidev
->dev
,
980 "Frame write error: %d\n", ret
);
985 mutex_unlock(&priv
->lock
);
988 static netdev_tx_t
adin1110_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
990 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
991 struct adin1110_priv
*priv
= port_priv
->priv
;
992 netdev_tx_t netdev_ret
= NETDEV_TX_OK
;
995 tx_space_needed
= skb
->len
+ ADIN1110_FRAME_HEADER_LEN
+ ADIN1110_INTERNAL_SIZE_HEADER_LEN
;
996 if (tx_space_needed
> priv
->tx_space
) {
997 netif_stop_queue(dev
);
998 netdev_ret
= NETDEV_TX_BUSY
;
1000 priv
->tx_space
-= tx_space_needed
;
1001 skb_queue_tail(&port_priv
->txq
, skb
);
1004 schedule_work(&port_priv
->tx_work
);
1009 static void adin1110_ndo_get_stats64(struct net_device
*dev
,
1010 struct rtnl_link_stats64
*storage
)
1012 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
1014 storage
->rx_packets
= port_priv
->rx_packets
;
1015 storage
->tx_packets
= port_priv
->tx_packets
;
1017 storage
->rx_bytes
= port_priv
->rx_bytes
;
1018 storage
->tx_bytes
= port_priv
->tx_bytes
;
1021 static int adin1110_port_get_port_parent_id(struct net_device
*dev
,
1022 struct netdev_phys_item_id
*ppid
)
1024 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
1025 struct adin1110_priv
*priv
= port_priv
->priv
;
1027 ppid
->id_len
= strnlen(priv
->mii_bus_name
, MAX_PHYS_ITEM_ID_LEN
);
1028 memcpy(ppid
->id
, priv
->mii_bus_name
, ppid
->id_len
);
1033 static int adin1110_ndo_get_phys_port_name(struct net_device
*dev
,
1034 char *name
, size_t len
)
1036 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
1039 err
= snprintf(name
, len
, "p%d", port_priv
->nr
);
1046 static const struct net_device_ops adin1110_netdev_ops
= {
1047 .ndo_open
= adin1110_net_open
,
1048 .ndo_stop
= adin1110_net_stop
,
1049 .ndo_eth_ioctl
= adin1110_ioctl
,
1050 .ndo_start_xmit
= adin1110_start_xmit
,
1051 .ndo_set_mac_address
= adin1110_ndo_set_mac_address
,
1052 .ndo_set_rx_mode
= adin1110_set_rx_mode
,
1053 .ndo_validate_addr
= eth_validate_addr
,
1054 .ndo_get_stats64
= adin1110_ndo_get_stats64
,
1055 .ndo_get_port_parent_id
= adin1110_port_get_port_parent_id
,
1056 .ndo_get_phys_port_name
= adin1110_ndo_get_phys_port_name
,
1059 static void adin1110_get_drvinfo(struct net_device
*dev
,
1060 struct ethtool_drvinfo
*di
)
1062 strscpy(di
->driver
, "ADIN1110", sizeof(di
->driver
));
1063 strscpy(di
->bus_info
, dev_name(dev
->dev
.parent
), sizeof(di
->bus_info
));
1066 static const struct ethtool_ops adin1110_ethtool_ops
= {
1067 .get_drvinfo
= adin1110_get_drvinfo
,
1068 .get_link
= ethtool_op_get_link
,
1069 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
1070 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
1073 static void adin1110_adjust_link(struct net_device
*dev
)
1075 struct phy_device
*phydev
= dev
->phydev
;
1078 phy_print_status(phydev
);
1081 /* PHY ID is stored in the MAC registers too,
1082 * check spi connection by reading it.
1084 static int adin1110_check_spi(struct adin1110_priv
*priv
)
1086 struct gpio_desc
*reset_gpio
;
1090 reset_gpio
= devm_gpiod_get_optional(&priv
->spidev
->dev
, "reset",
1093 /* MISO pin is used for internal configuration, can't have
1094 * anyone else disturbing the SDO line.
1096 spi_bus_lock(priv
->spidev
->controller
);
1098 gpiod_set_value(reset_gpio
, 1);
1100 gpiod_set_value(reset_gpio
, 0);
1102 /* Need to wait 90 ms before interacting with
1103 * the MAC after a HW reset.
1107 spi_bus_unlock(priv
->spidev
->controller
);
1110 ret
= adin1110_read_reg(priv
, ADIN1110_PHY_ID
, &val
);
1114 if (val
!= priv
->cfg
->phy_id_val
) {
1115 dev_err(&priv
->spidev
->dev
, "PHY ID expected: %x, read: %x\n",
1116 priv
->cfg
->phy_id_val
, val
);
1123 static int adin1110_hw_forwarding(struct adin1110_priv
*priv
, bool enable
)
1128 priv
->forwarding
= enable
;
1130 if (!priv
->forwarding
) {
1131 for (i
= ADIN_MAC_FDB_ADDR_SLOT
; i
< ADIN_MAC_MAX_ADDR_SLOTS
; i
++) {
1132 ret
= adin1110_clear_mac_address(priv
, i
);
1138 /* Forwarding is optimised when MAC runs in Cut Through mode. */
1139 ret
= adin1110_set_bits(priv
, ADIN1110_CONFIG2
,
1140 ADIN2111_PORT_CUT_THRU_EN
,
1141 priv
->forwarding
? ADIN2111_PORT_CUT_THRU_EN
: 0);
1145 for (i
= 0; i
< priv
->cfg
->ports_nr
; i
++) {
1146 ret
= adin1110_setup_rx_mode(priv
->ports
[i
]);
1154 static int adin1110_port_bridge_join(struct adin1110_port_priv
*port_priv
,
1155 struct net_device
*bridge
)
1157 struct adin1110_priv
*priv
= port_priv
->priv
;
1160 port_priv
->bridge
= bridge
;
1162 if (adin1110_can_offload_forwarding(priv
)) {
1163 mutex_lock(&priv
->lock
);
1164 ret
= adin1110_hw_forwarding(priv
, true);
1165 mutex_unlock(&priv
->lock
);
1171 return adin1110_set_mac_address(port_priv
->netdev
, bridge
->dev_addr
);
1174 static int adin1110_port_bridge_leave(struct adin1110_port_priv
*port_priv
,
1175 struct net_device
*bridge
)
1177 struct adin1110_priv
*priv
= port_priv
->priv
;
1180 port_priv
->bridge
= NULL
;
1182 mutex_lock(&priv
->lock
);
1183 ret
= adin1110_hw_forwarding(priv
, false);
1184 mutex_unlock(&priv
->lock
);
1189 static bool adin1110_port_dev_check(const struct net_device
*dev
)
1191 return dev
->netdev_ops
== &adin1110_netdev_ops
;
1194 static int adin1110_netdevice_event(struct notifier_block
*unused
,
1195 unsigned long event
, void *ptr
)
1197 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1198 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
1199 struct netdev_notifier_changeupper_info
*info
= ptr
;
1202 if (!adin1110_port_dev_check(dev
))
1206 case NETDEV_CHANGEUPPER
:
1207 if (netif_is_bridge_master(info
->upper_dev
)) {
1209 ret
= adin1110_port_bridge_join(port_priv
, info
->upper_dev
);
1211 ret
= adin1110_port_bridge_leave(port_priv
, info
->upper_dev
);
1218 return notifier_from_errno(ret
);
1221 static struct notifier_block adin1110_netdevice_nb
= {
1222 .notifier_call
= adin1110_netdevice_event
,
1225 static void adin1110_disconnect_phy(void *data
)
1227 phy_disconnect(data
);
1230 static int adin1110_port_set_forwarding_state(struct adin1110_port_priv
*port_priv
)
1232 struct adin1110_priv
*priv
= port_priv
->priv
;
1235 port_priv
->state
= BR_STATE_FORWARDING
;
1237 mutex_lock(&priv
->lock
);
1238 ret
= adin1110_set_mac_address(port_priv
->netdev
,
1239 port_priv
->netdev
->dev_addr
);
1243 if (adin1110_can_offload_forwarding(priv
))
1244 ret
= adin1110_hw_forwarding(priv
, true);
1246 ret
= adin1110_setup_rx_mode(port_priv
);
1248 mutex_unlock(&priv
->lock
);
1253 static int adin1110_port_set_blocking_state(struct adin1110_port_priv
*port_priv
)
1255 u8 mac
[ETH_ALEN
] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x00};
1256 struct adin1110_priv
*priv
= port_priv
->priv
;
1262 port_priv
->state
= BR_STATE_BLOCKING
;
1264 mutex_lock(&priv
->lock
);
1266 mac_slot
= (!port_priv
->nr
) ? ADIN_MAC_P1_ADDR_SLOT
: ADIN_MAC_P2_ADDR_SLOT
;
1267 ret
= adin1110_clear_mac_address(priv
, mac_slot
);
1271 ret
= adin1110_hw_forwarding(priv
, false);
1275 /* Allow only BPDUs to be passed to the CPU */
1276 eth_broadcast_addr(mask
);
1277 port_rules
= adin1110_port_rules(port_priv
, true, false);
1278 ret
= adin1110_write_mac_address(port_priv
, mac_slot
, mac
,
1281 mutex_unlock(&priv
->lock
);
1286 /* ADIN1110/2111 does not have any native STP support.
1287 * Listen for bridge core state changes and
1288 * allow all frames to pass or only the BPDUs.
1290 static int adin1110_port_attr_stp_state_set(struct adin1110_port_priv
*port_priv
,
1294 case BR_STATE_FORWARDING
:
1295 return adin1110_port_set_forwarding_state(port_priv
);
1296 case BR_STATE_LEARNING
:
1297 case BR_STATE_LISTENING
:
1298 case BR_STATE_DISABLED
:
1299 case BR_STATE_BLOCKING
:
1300 return adin1110_port_set_blocking_state(port_priv
);
1306 static int adin1110_port_attr_set(struct net_device
*dev
, const void *ctx
,
1307 const struct switchdev_attr
*attr
,
1308 struct netlink_ext_ack
*extack
)
1310 struct adin1110_port_priv
*port_priv
= netdev_priv(dev
);
1313 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
1314 return adin1110_port_attr_stp_state_set(port_priv
,
1321 static int adin1110_switchdev_blocking_event(struct notifier_block
*unused
,
1322 unsigned long event
,
1325 struct net_device
*netdev
= switchdev_notifier_info_to_dev(ptr
);
1328 if (event
== SWITCHDEV_PORT_ATTR_SET
) {
1329 ret
= switchdev_handle_port_attr_set(netdev
, ptr
,
1330 adin1110_port_dev_check
,
1331 adin1110_port_attr_set
);
1333 return notifier_from_errno(ret
);
1339 static struct notifier_block adin1110_switchdev_blocking_notifier
= {
1340 .notifier_call
= adin1110_switchdev_blocking_event
,
1343 static void adin1110_fdb_offload_notify(struct net_device
*netdev
,
1344 struct switchdev_notifier_fdb_info
*rcv
)
1346 struct switchdev_notifier_fdb_info info
= {};
1348 info
.addr
= rcv
->addr
;
1349 info
.vid
= rcv
->vid
;
1350 info
.offloaded
= true;
1351 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED
,
1352 netdev
, &info
.info
, NULL
);
1355 static int adin1110_fdb_add(struct adin1110_port_priv
*port_priv
,
1356 struct switchdev_notifier_fdb_info
*fdb
)
1358 struct adin1110_priv
*priv
= port_priv
->priv
;
1359 struct adin1110_port_priv
*other_port
;
1366 netdev_dbg(port_priv
->netdev
,
1367 "DEBUG: %s: MACID = %pM vid = %u flags = %u %u -- port %d\n",
1368 __func__
, fdb
->addr
, fdb
->vid
, fdb
->added_by_user
,
1369 fdb
->offloaded
, port_priv
->nr
);
1371 if (!priv
->forwarding
)
1377 /* Find free FDB slot on device. */
1378 for (mac_nr
= ADIN_MAC_FDB_ADDR_SLOT
; mac_nr
< ADIN_MAC_MAX_ADDR_SLOTS
; mac_nr
++) {
1379 ret
= adin1110_read_reg(priv
, ADIN1110_MAC_ADDR_FILTER_UPR
+ (mac_nr
* 2), &val
);
1386 if (mac_nr
== ADIN_MAC_MAX_ADDR_SLOTS
)
1389 other_port
= priv
->ports
[!port_priv
->nr
];
1390 port_rules
= adin1110_port_rules(other_port
, false, true);
1391 eth_broadcast_addr(mask
);
1393 return adin1110_write_mac_address(other_port
, mac_nr
, (u8
*)fdb
->addr
,
1397 static int adin1110_read_mac(struct adin1110_priv
*priv
, int mac_nr
, u8
*addr
)
1402 ret
= adin1110_read_reg(priv
, ADIN1110_MAC_ADDR_FILTER_UPR
+ (mac_nr
* 2), &val
);
1406 put_unaligned_be16(val
, addr
);
1408 ret
= adin1110_read_reg(priv
, ADIN1110_MAC_ADDR_FILTER_LWR
+ (mac_nr
* 2), &val
);
1412 put_unaligned_be32(val
, addr
+ 2);
1417 static int adin1110_fdb_del(struct adin1110_port_priv
*port_priv
,
1418 struct switchdev_notifier_fdb_info
*fdb
)
1420 struct adin1110_priv
*priv
= port_priv
->priv
;
1425 netdev_dbg(port_priv
->netdev
,
1426 "DEBUG: %s: MACID = %pM vid = %u flags = %u %u -- port %d\n",
1427 __func__
, fdb
->addr
, fdb
->vid
, fdb
->added_by_user
,
1428 fdb
->offloaded
, port_priv
->nr
);
1433 for (mac_nr
= ADIN_MAC_FDB_ADDR_SLOT
; mac_nr
< ADIN_MAC_MAX_ADDR_SLOTS
; mac_nr
++) {
1434 ret
= adin1110_read_mac(priv
, mac_nr
, addr
);
1438 if (ether_addr_equal(addr
, fdb
->addr
)) {
1439 ret
= adin1110_clear_mac_address(priv
, mac_nr
);
1448 static void adin1110_switchdev_event_work(struct work_struct
*work
)
1450 struct adin1110_switchdev_event_work
*switchdev_work
;
1451 struct adin1110_port_priv
*port_priv
;
1454 switchdev_work
= container_of(work
, struct adin1110_switchdev_event_work
, work
);
1455 port_priv
= switchdev_work
->port_priv
;
1457 mutex_lock(&port_priv
->priv
->lock
);
1459 switch (switchdev_work
->event
) {
1460 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
1461 ret
= adin1110_fdb_add(port_priv
, &switchdev_work
->fdb_info
);
1463 adin1110_fdb_offload_notify(port_priv
->netdev
,
1464 &switchdev_work
->fdb_info
);
1466 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1467 adin1110_fdb_del(port_priv
, &switchdev_work
->fdb_info
);
1473 mutex_unlock(&port_priv
->priv
->lock
);
1475 kfree(switchdev_work
->fdb_info
.addr
);
1476 kfree(switchdev_work
);
1477 dev_put(port_priv
->netdev
);
1480 /* called under rcu_read_lock() */
1481 static int adin1110_switchdev_event(struct notifier_block
*unused
,
1482 unsigned long event
, void *ptr
)
1484 struct net_device
*netdev
= switchdev_notifier_info_to_dev(ptr
);
1485 struct adin1110_port_priv
*port_priv
= netdev_priv(netdev
);
1486 struct adin1110_switchdev_event_work
*switchdev_work
;
1487 struct switchdev_notifier_fdb_info
*fdb_info
= ptr
;
1489 if (!adin1110_port_dev_check(netdev
))
1492 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
1493 if (WARN_ON(!switchdev_work
))
1496 INIT_WORK(&switchdev_work
->work
, adin1110_switchdev_event_work
);
1497 switchdev_work
->port_priv
= port_priv
;
1498 switchdev_work
->event
= event
;
1501 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
1502 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
1503 memcpy(&switchdev_work
->fdb_info
, ptr
,
1504 sizeof(switchdev_work
->fdb_info
));
1505 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
1507 if (!switchdev_work
->fdb_info
.addr
)
1508 goto err_addr_alloc
;
1510 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
1515 kfree(switchdev_work
);
1519 queue_work(system_long_wq
, &switchdev_work
->work
);
1524 kfree(switchdev_work
);
1528 static struct notifier_block adin1110_switchdev_notifier
= {
1529 .notifier_call
= adin1110_switchdev_event
,
1532 static void adin1110_unregister_notifiers(void)
1534 unregister_switchdev_blocking_notifier(&adin1110_switchdev_blocking_notifier
);
1535 unregister_switchdev_notifier(&adin1110_switchdev_notifier
);
1536 unregister_netdevice_notifier(&adin1110_netdevice_nb
);
1539 static int adin1110_setup_notifiers(void)
1543 ret
= register_netdevice_notifier(&adin1110_netdevice_nb
);
1547 ret
= register_switchdev_notifier(&adin1110_switchdev_notifier
);
1551 ret
= register_switchdev_blocking_notifier(&adin1110_switchdev_blocking_notifier
);
1558 unregister_switchdev_notifier(&adin1110_switchdev_notifier
);
1561 unregister_netdevice_notifier(&adin1110_netdevice_nb
);
1566 static int adin1110_probe_netdevs(struct adin1110_priv
*priv
)
1568 struct device
*dev
= &priv
->spidev
->dev
;
1569 struct adin1110_port_priv
*port_priv
;
1570 struct net_device
*netdev
;
1574 for (i
= 0; i
< priv
->cfg
->ports_nr
; i
++) {
1575 netdev
= devm_alloc_etherdev(dev
, sizeof(*port_priv
));
1579 port_priv
= netdev_priv(netdev
);
1580 port_priv
->netdev
= netdev
;
1581 port_priv
->priv
= priv
;
1582 port_priv
->cfg
= priv
->cfg
;
1584 priv
->ports
[i
] = port_priv
;
1585 SET_NETDEV_DEV(netdev
, dev
);
1587 ret
= device_get_ethdev_address(dev
, netdev
);
1591 netdev
->irq
= priv
->spidev
->irq
;
1592 INIT_WORK(&port_priv
->tx_work
, adin1110_tx_work
);
1593 INIT_WORK(&port_priv
->rx_mode_work
, adin1110_rx_mode_work
);
1594 skb_queue_head_init(&port_priv
->txq
);
1596 netif_carrier_off(netdev
);
1598 netdev
->if_port
= IF_PORT_10BASET
;
1599 netdev
->netdev_ops
= &adin1110_netdev_ops
;
1600 netdev
->ethtool_ops
= &adin1110_ethtool_ops
;
1601 netdev
->priv_flags
|= IFF_UNICAST_FLT
;
1602 netdev
->netns_local
= true;
1604 port_priv
->phydev
= get_phy_device(priv
->mii_bus
, i
+ 1, false);
1605 if (IS_ERR(port_priv
->phydev
)) {
1606 netdev_err(netdev
, "Could not find PHY with device address: %d.\n", i
);
1607 return PTR_ERR(port_priv
->phydev
);
1610 port_priv
->phydev
= phy_connect(netdev
,
1611 phydev_name(port_priv
->phydev
),
1612 adin1110_adjust_link
,
1613 PHY_INTERFACE_MODE_INTERNAL
);
1614 if (IS_ERR(port_priv
->phydev
)) {
1615 netdev_err(netdev
, "Could not connect PHY with device address: %d.\n", i
);
1616 return PTR_ERR(port_priv
->phydev
);
1619 ret
= devm_add_action_or_reset(dev
, adin1110_disconnect_phy
,
1625 /* ADIN1110 INT_N pin will be used to signal the host */
1626 ret
= devm_request_threaded_irq(dev
, priv
->spidev
->irq
, NULL
,
1628 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1629 dev_name(dev
), priv
);
1633 for (i
= 0; i
< priv
->cfg
->ports_nr
; i
++) {
1634 ret
= devm_register_netdev(dev
, priv
->ports
[i
]->netdev
);
1636 dev_err(dev
, "Failed to register network device.\n");
1644 static int adin1110_probe(struct spi_device
*spi
)
1646 const struct spi_device_id
*dev_id
= spi_get_device_id(spi
);
1647 struct device
*dev
= &spi
->dev
;
1648 struct adin1110_priv
*priv
;
1651 priv
= devm_kzalloc(dev
, sizeof(struct adin1110_priv
), GFP_KERNEL
);
1656 priv
->cfg
= &adin1110_cfgs
[dev_id
->driver_data
];
1657 spi
->bits_per_word
= 8;
1658 spi
->mode
= SPI_MODE_0
;
1660 mutex_init(&priv
->lock
);
1661 spin_lock_init(&priv
->state_lock
);
1663 /* use of CRC on control and data transactions is pin dependent */
1664 priv
->append_crc
= device_property_read_bool(dev
, "adi,spi-crc");
1665 if (priv
->append_crc
)
1666 crc8_populate_msb(adin1110_crc_table
, 0x7);
1668 ret
= adin1110_check_spi(priv
);
1670 dev_err(dev
, "Probe SPI Read check failed: %d\n", ret
);
1674 ret
= adin1110_write_reg(priv
, ADIN1110_RESET
, ADIN1110_SWRESET
);
1678 ret
= adin1110_register_mdiobus(priv
, dev
);
1680 dev_err(dev
, "Could not register MDIO bus %d\n", ret
);
1684 return adin1110_probe_netdevs(priv
);
1687 static const struct of_device_id adin1110_match_table
[] = {
1688 { .compatible
= "adi,adin1110" },
1689 { .compatible
= "adi,adin2111" },
1692 MODULE_DEVICE_TABLE(of
, adin1110_match_table
);
1694 static const struct spi_device_id adin1110_spi_id
[] = {
1695 { .name
= "adin1110", .driver_data
= ADIN1110_MAC
},
1696 { .name
= "adin2111", .driver_data
= ADIN2111_MAC
},
1699 MODULE_DEVICE_TABLE(spi
, adin1110_spi_id
);
1701 static struct spi_driver adin1110_driver
= {
1704 .of_match_table
= adin1110_match_table
,
1706 .probe
= adin1110_probe
,
1707 .id_table
= adin1110_spi_id
,
1710 static int __init
adin1110_driver_init(void)
1714 ret
= adin1110_setup_notifiers();
1718 ret
= spi_register_driver(&adin1110_driver
);
1720 adin1110_unregister_notifiers();
1727 static void __exit
adin1110_exit(void)
1729 adin1110_unregister_notifiers();
1730 spi_unregister_driver(&adin1110_driver
);
1732 module_init(adin1110_driver_init
);
1733 module_exit(adin1110_exit
);
1735 MODULE_DESCRIPTION("ADIN1110 Network driver");
1736 MODULE_AUTHOR("Alexandru Tachici <alexandru.tachici@analog.com>");
1737 MODULE_LICENSE("Dual BSD/GPL");