1 // SPDX-License-Identifier: GPL-2.0+
3 * CPSW Ethernet Switch Driver
5 * Copyright (C) 2010-2018 Texas Instruments Incorporated - http://www.ti.com/
16 #include <linux/errno.h>
20 #include <asm/arch/cpu.h>
22 #include <fdt_support.h>
24 #include "cpsw_mdio.h"
26 DECLARE_GLOBAL_DATA_PTR;
28 #define BITMASK(bits) (BIT(bits) - 1)
29 #define NUM_DESCS (PKTBUFSRX * 2)
31 #define PKT_MAX (1500 + 14 + 4 + 4)
33 #define GIGABITEN BIT(7)
34 #define FULLDUPLEXEN BIT(0)
38 #define CPSW_HOST_PORT_OFFSET 0x108
39 #define CPSW_SLAVE0_OFFSET 0x208
40 #define CPSW_SLAVE1_OFFSET 0x308
41 #define CPSW_SLAVE_SIZE 0x100
42 #define CPSW_CPDMA_OFFSET 0x800
43 #define CPSW_HW_STATS 0x900
44 #define CPSW_STATERAM_OFFSET 0xa00
45 #define CPSW_CPTS_OFFSET 0xc00
46 #define CPSW_ALE_OFFSET 0xd00
47 #define CPSW_SLIVER0_OFFSET 0xd80
48 #define CPSW_SLIVER1_OFFSET 0xdc0
49 #define CPSW_BD_OFFSET 0x2000
50 #define CPSW_MDIO_DIV 0xff
52 #define AM335X_GMII_SEL_OFFSET 0x630
55 #define CPDMA_TXCONTROL 0x004
56 #define CPDMA_RXCONTROL 0x014
57 #define CPDMA_SOFTRESET 0x01c
58 #define CPDMA_RXFREE 0x0e0
59 #define CPDMA_TXHDP_VER1 0x100
60 #define CPDMA_TXHDP_VER2 0x200
61 #define CPDMA_RXHDP_VER1 0x120
62 #define CPDMA_RXHDP_VER2 0x220
63 #define CPDMA_TXCP_VER1 0x140
64 #define CPDMA_TXCP_VER2 0x240
65 #define CPDMA_RXCP_VER1 0x160
66 #define CPDMA_RXCP_VER2 0x260
68 /* Descriptor mode bits */
69 #define CPDMA_DESC_SOP BIT(31)
70 #define CPDMA_DESC_EOP BIT(30)
71 #define CPDMA_DESC_OWNER BIT(29)
72 #define CPDMA_DESC_EOQ BIT(28)
75 * This timeout definition is a worst-case ultra defensive measure against
76 * unexpected controller lock ups. Ideally, we should never ever hit this
77 * scenario in practice.
79 #define CPDMA_TIMEOUT 100 /* msecs */
89 struct cpsw_slave_regs {
97 #elif defined(CONFIG_TI814X)
106 struct cpsw_host_regs {
112 u32 cpdma_tx_pri_map;
113 u32 cpdma_rx_chan_map;
116 struct cpsw_sliver_regs {
129 #define ALE_ENTRY_BITS 68
130 #define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
133 #define ALE_CONTROL 0x08
134 #define ALE_UNKNOWNVLAN 0x18
135 #define ALE_TABLE_CONTROL 0x20
136 #define ALE_TABLE 0x34
137 #define ALE_PORTCTL 0x40
139 #define ALE_TABLE_WRITE BIT(31)
141 #define ALE_TYPE_FREE 0
142 #define ALE_TYPE_ADDR 1
143 #define ALE_TYPE_VLAN 2
144 #define ALE_TYPE_VLAN_ADDR 3
146 #define ALE_UCAST_PERSISTANT 0
147 #define ALE_UCAST_UNTOUCHED 1
148 #define ALE_UCAST_OUI 2
149 #define ALE_UCAST_TOUCHED 3
151 #define ALE_MCAST_FWD 0
152 #define ALE_MCAST_BLOCK_LEARN_FWD 1
153 #define ALE_MCAST_FWD_LEARN 2
154 #define ALE_MCAST_FWD_2 3
156 enum cpsw_ale_port_state {
157 ALE_PORT_STATE_DISABLE = 0x00,
158 ALE_PORT_STATE_BLOCK = 0x01,
159 ALE_PORT_STATE_LEARN = 0x02,
160 ALE_PORT_STATE_FORWARD = 0x03,
163 /* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
165 #define ALE_BLOCKED 2
168 struct cpsw_slave_regs *regs;
169 struct cpsw_sliver_regs *sliver;
172 struct cpsw_slave_data *data;
176 /* hardware fields */
181 /* software fields */
187 struct cpdma_desc *head, *tail;
188 void *hdp, *cp, *rxfree;
191 /* AM33xx SoC specific definitions for the CONTROL port */
192 #define AM33XX_GMII_SEL_MODE_MII 0
193 #define AM33XX_GMII_SEL_MODE_RMII 1
194 #define AM33XX_GMII_SEL_MODE_RGMII 2
196 #define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
197 #define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
198 #define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
199 #define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
201 #define GMII_SEL_MODE_MASK 0x3
203 #define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
204 #define desc_read(desc, fld) __raw_readl(&(desc)->fld)
205 #define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
207 #define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
208 #define chan_read(chan, fld) __raw_readl((chan)->fld)
209 #define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
211 #define for_active_slave(slave, priv) \
212 slave = (priv)->slaves + (priv)->data.active_slave; if (slave)
213 #define for_each_slave(slave, priv) \
214 for (slave = (priv)->slaves; slave != (priv)->slaves + \
215 (priv)->data.slaves; slave++)
221 struct eth_device *dev;
223 struct cpsw_platform_data data;
226 struct cpsw_regs *regs;
228 struct cpsw_host_regs *host_port_regs;
231 struct cpdma_desc *descs;
232 struct cpdma_desc *desc_free;
233 struct cpdma_chan rx_chan, tx_chan;
235 struct cpsw_slave *slaves;
236 struct phy_device *phydev;
242 static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
248 idx = 2 - idx; /* flip */
249 return (ale_entry[idx] >> start) & BITMASK(bits);
252 static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
257 value &= BITMASK(bits);
260 idx = 2 - idx; /* flip */
261 ale_entry[idx] &= ~(BITMASK(bits) << start);
262 ale_entry[idx] |= (value << start);
265 #define DEFINE_ALE_FIELD(name, start, bits) \
266 static inline int cpsw_ale_get_##name(u32 *ale_entry) \
268 return cpsw_ale_get_field(ale_entry, start, bits); \
270 static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
272 cpsw_ale_set_field(ale_entry, start, bits, value); \
275 DEFINE_ALE_FIELD(entry_type, 60, 2)
276 DEFINE_ALE_FIELD(mcast_state, 62, 2)
277 DEFINE_ALE_FIELD(port_mask, 66, 3)
278 DEFINE_ALE_FIELD(ucast_type, 62, 2)
279 DEFINE_ALE_FIELD(port_num, 66, 2)
280 DEFINE_ALE_FIELD(blocked, 65, 1)
281 DEFINE_ALE_FIELD(secure, 64, 1)
282 DEFINE_ALE_FIELD(mcast, 40, 1)
284 /* The MAC address field in the ALE entry cannot be macroized as above */
285 static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
289 for (i = 0; i < 6; i++)
290 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
293 static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
297 for (i = 0; i < 6; i++)
298 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
301 static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
305 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
307 for (i = 0; i < ALE_ENTRY_WORDS; i++)
308 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
313 static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
317 for (i = 0; i < ALE_ENTRY_WORDS; i++)
318 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
320 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
325 static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
327 u32 ale_entry[ALE_ENTRY_WORDS];
330 for (idx = 0; idx < priv->data.ale_entries; idx++) {
333 cpsw_ale_read(priv, idx, ale_entry);
334 type = cpsw_ale_get_entry_type(ale_entry);
335 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
337 cpsw_ale_get_addr(ale_entry, entry_addr);
338 if (memcmp(entry_addr, addr, 6) == 0)
344 static int cpsw_ale_match_free(struct cpsw_priv *priv)
346 u32 ale_entry[ALE_ENTRY_WORDS];
349 for (idx = 0; idx < priv->data.ale_entries; idx++) {
350 cpsw_ale_read(priv, idx, ale_entry);
351 type = cpsw_ale_get_entry_type(ale_entry);
352 if (type == ALE_TYPE_FREE)
358 static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
360 u32 ale_entry[ALE_ENTRY_WORDS];
363 for (idx = 0; idx < priv->data.ale_entries; idx++) {
364 cpsw_ale_read(priv, idx, ale_entry);
365 type = cpsw_ale_get_entry_type(ale_entry);
366 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
368 if (cpsw_ale_get_mcast(ale_entry))
370 type = cpsw_ale_get_ucast_type(ale_entry);
371 if (type != ALE_UCAST_PERSISTANT &&
372 type != ALE_UCAST_OUI)
378 static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
381 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
384 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
385 cpsw_ale_set_addr(ale_entry, addr);
386 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
387 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
388 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
389 cpsw_ale_set_port_num(ale_entry, port);
391 idx = cpsw_ale_match_addr(priv, addr);
393 idx = cpsw_ale_match_free(priv);
395 idx = cpsw_ale_find_ageable(priv);
399 cpsw_ale_write(priv, idx, ale_entry);
403 static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
406 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
409 idx = cpsw_ale_match_addr(priv, addr);
411 cpsw_ale_read(priv, idx, ale_entry);
413 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
414 cpsw_ale_set_addr(ale_entry, addr);
415 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
417 mask = cpsw_ale_get_port_mask(ale_entry);
419 cpsw_ale_set_port_mask(ale_entry, port_mask);
422 idx = cpsw_ale_match_free(priv);
424 idx = cpsw_ale_find_ageable(priv);
428 cpsw_ale_write(priv, idx, ale_entry);
432 static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
434 u32 tmp, mask = BIT(bit);
436 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
438 tmp |= val ? mask : 0;
439 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
442 #define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
443 #define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
444 #define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
446 static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
449 int offset = ALE_PORTCTL + 4 * port;
452 tmp = __raw_readl(priv->ale_regs + offset);
455 __raw_writel(tmp, priv->ale_regs + offset);
458 /* Set a self-clearing bit in a register, and wait for it to clear */
459 static inline void setbit_and_wait_for_clear32(void *addr)
461 __raw_writel(CLEAR_BIT, addr);
462 while (__raw_readl(addr) & CLEAR_BIT)
466 #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
467 ((mac)[2] << 16) | ((mac)[3] << 24))
468 #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
470 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
471 struct cpsw_priv *priv)
474 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
476 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
477 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
479 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
480 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
484 static int cpsw_slave_update_link(struct cpsw_slave *slave,
485 struct cpsw_priv *priv, int *link)
487 struct phy_device *phy;
495 ret = phy_startup(phy);
502 if (phy->link) { /* link up */
503 mac_control = priv->data.mac_control;
504 if (phy->speed == 1000)
505 mac_control |= GIGABITEN;
506 if (phy->duplex == DUPLEX_FULL)
507 mac_control |= FULLDUPLEXEN;
508 if (phy->speed == 100)
509 mac_control |= MIIEN;
512 if (mac_control == slave->mac_control)
516 printf("link up on port %d, speed %d, %s duplex\n",
517 slave->slave_num, phy->speed,
518 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
520 printf("link down on port %d\n", slave->slave_num);
523 __raw_writel(mac_control, &slave->sliver->mac_control);
524 slave->mac_control = mac_control;
530 static int cpsw_update_link(struct cpsw_priv *priv)
533 struct cpsw_slave *slave;
535 for_active_slave(slave, priv)
536 ret = cpsw_slave_update_link(slave, priv, NULL);
541 static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
543 if (priv->host_port == 0)
544 return slave_num + 1;
549 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
553 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
555 /* setup priority mapping */
556 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
557 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
559 /* setup max packet size, and mac address */
560 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
561 cpsw_set_slave_mac(slave, priv);
563 slave->mac_control = 0; /* no link yet */
565 /* enable forwarding */
566 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
567 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
569 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
571 priv->phy_mask |= 1 << slave->data->phy_addr;
574 static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
576 struct cpdma_desc *desc = priv->desc_free;
579 priv->desc_free = desc_read_ptr(desc, hw_next);
583 static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
586 desc_write(desc, hw_next, priv->desc_free);
587 priv->desc_free = desc;
591 static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
592 void *buffer, int len)
594 struct cpdma_desc *desc, *prev;
597 desc = cpdma_desc_alloc(priv);
604 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
606 desc_write(desc, hw_next, 0);
607 desc_write(desc, hw_buffer, buffer);
608 desc_write(desc, hw_len, len);
609 desc_write(desc, hw_mode, mode | len);
610 desc_write(desc, sw_buffer, buffer);
611 desc_write(desc, sw_len, len);
614 /* simple case - first packet enqueued */
617 chan_write(chan, hdp, desc);
621 /* not the first packet - enqueue at the tail */
623 desc_write(prev, hw_next, desc);
626 /* next check if EOQ has been triggered already */
627 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
628 chan_write(chan, hdp, desc);
632 chan_write(chan, rxfree, 1);
636 static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
637 void **buffer, int *len)
639 struct cpdma_desc *desc = chan->head;
645 status = desc_read(desc, hw_mode);
648 *len = status & 0x7ff;
651 *buffer = desc_read_ptr(desc, sw_buffer);
653 if (status & CPDMA_DESC_OWNER) {
654 if (chan_read(chan, hdp) == 0) {
655 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
656 chan_write(chan, hdp, desc);
662 chan->head = desc_read_ptr(desc, hw_next);
663 chan_write(chan, cp, desc);
665 cpdma_desc_free(priv, desc);
669 static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
671 struct cpsw_slave *slave;
674 /* soft reset the controller and initialize priv */
675 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
677 /* initialize and reset the address lookup engine */
678 cpsw_ale_enable(priv, 1);
679 cpsw_ale_clear(priv, 1);
680 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
682 /* setup host port priority mapping */
683 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
684 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
686 /* disable priority elevation and enable statistics on all ports */
687 __raw_writel(0, &priv->regs->ptype);
689 /* enable statistics collection only on the host port */
690 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
691 __raw_writel(0x7, &priv->regs->stat_port_en);
693 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
695 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
696 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
698 for_active_slave(slave, priv)
699 cpsw_slave_init(slave, priv);
701 ret = cpsw_update_link(priv);
705 /* init descriptor pool */
706 for (i = 0; i < NUM_DESCS; i++) {
707 desc_write(&priv->descs[i], hw_next,
708 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
710 priv->desc_free = &priv->descs[0];
712 /* initialize channels */
713 if (priv->data.version == CPSW_CTRL_VERSION_2) {
714 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
715 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
716 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
717 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
719 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
720 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
721 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
723 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
724 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
725 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
726 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
728 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
729 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
730 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
733 /* clear dma state */
734 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
736 if (priv->data.version == CPSW_CTRL_VERSION_2) {
737 for (i = 0; i < priv->data.channels; i++) {
738 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
740 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
742 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
744 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
746 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
750 for (i = 0; i < priv->data.channels; i++) {
751 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
753 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
755 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
757 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
759 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
765 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
766 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
768 /* submit rx descs */
769 for (i = 0; i < PKTBUFSRX; i++) {
770 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
773 printf("error %d submitting rx desc\n", ret);
782 static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
784 int timeout = CPDMA_TIMEOUT;
786 /* reap completed packets */
788 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
794 static void _cpsw_halt(struct cpsw_priv *priv)
796 cpsw_reap_completed_packets(priv);
798 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
799 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
801 /* soft reset the controller and initialize priv */
802 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
804 /* clear dma state */
805 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
809 static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
813 flush_dcache_range((unsigned long)packet,
814 (unsigned long)packet + ALIGN(length, PKTALIGN));
816 timeout = cpsw_reap_completed_packets(priv);
818 printf("cpdma_process timeout\n");
822 return cpdma_submit(priv, &priv->tx_chan, packet, length);
825 static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
831 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
835 invalidate_dcache_range((unsigned long)buffer,
836 (unsigned long)buffer + PKTSIZE_ALIGN);
842 static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
843 struct cpsw_priv *priv)
845 void *regs = priv->regs;
846 struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
847 slave->slave_num = slave_num;
849 slave->regs = regs + data->slave_reg_ofs;
850 slave->sliver = regs + data->sliver_reg_ofs;
853 static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
855 struct phy_device *phydev;
856 u32 supported = PHY_GBIT_FEATURES;
858 phydev = phy_connect(priv->bus,
859 slave->data->phy_addr,
861 slave->data->phy_if);
866 phydev->supported &= supported;
867 phydev->advertising = phydev->supported;
870 if (slave->data->phy_of_handle)
871 phydev->node = offset_to_ofnode(slave->data->phy_of_handle);
874 priv->phydev = phydev;
880 static void cpsw_phy_addr_update(struct cpsw_priv *priv)
882 struct cpsw_platform_data *data = &priv->data;
883 u16 alive = cpsw_mdio_get_alive(priv->bus);
884 int active = data->active_slave;
885 int new_addr = ffs(alive) - 1;
888 * If there is only one phy alive and its address does not match
889 * that of active slave, then phy address can safely be updated.
891 if (hweight16(alive) == 1 &&
892 data->slave_data[active].phy_addr != new_addr) {
893 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
894 active, data->slave_data[active].phy_addr, new_addr);
895 data->slave_data[active].phy_addr = new_addr;
899 int _cpsw_register(struct cpsw_priv *priv)
901 struct cpsw_slave *slave;
902 struct cpsw_platform_data *data = &priv->data;
903 void *regs = (void *)data->cpsw_base;
905 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
910 priv->host_port = data->host_port_num;
912 priv->host_port_regs = regs + data->host_port_reg_ofs;
913 priv->dma_regs = regs + data->cpdma_reg_ofs;
914 priv->ale_regs = regs + data->ale_reg_ofs;
915 priv->descs = (void *)regs + data->bd_ram_ofs;
919 for_each_slave(slave, priv) {
920 cpsw_slave_setup(slave, idx, priv);
924 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
928 cpsw_phy_addr_update(priv);
930 for_active_slave(slave, priv)
931 cpsw_phy_init(priv, slave);
936 #ifndef CONFIG_DM_ETH
937 static int cpsw_init(struct eth_device *dev, bd_t *bis)
939 struct cpsw_priv *priv = dev->priv;
941 return _cpsw_init(priv, dev->enetaddr);
944 static void cpsw_halt(struct eth_device *dev)
946 struct cpsw_priv *priv = dev->priv;
948 return _cpsw_halt(priv);
951 static int cpsw_send(struct eth_device *dev, void *packet, int length)
953 struct cpsw_priv *priv = dev->priv;
955 return _cpsw_send(priv, packet, length);
958 static int cpsw_recv(struct eth_device *dev)
960 struct cpsw_priv *priv = dev->priv;
964 len = _cpsw_recv(priv, &pkt);
967 net_process_received_packet(pkt, len);
968 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
974 int cpsw_register(struct cpsw_platform_data *data)
976 struct cpsw_priv *priv;
977 struct eth_device *dev;
980 dev = calloc(sizeof(*dev), 1);
984 priv = calloc(sizeof(*priv), 1);
993 strcpy(dev->name, "cpsw");
995 dev->init = cpsw_init;
996 dev->halt = cpsw_halt;
997 dev->send = cpsw_send;
998 dev->recv = cpsw_recv;
1003 ret = _cpsw_register(priv);
1005 eth_unregister(dev);
1014 static int cpsw_eth_start(struct udevice *dev)
1016 struct eth_pdata *pdata = dev_get_platdata(dev);
1017 struct cpsw_priv *priv = dev_get_priv(dev);
1019 return _cpsw_init(priv, pdata->enetaddr);
1022 static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1024 struct cpsw_priv *priv = dev_get_priv(dev);
1026 return _cpsw_send(priv, packet, length);
1029 static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1031 struct cpsw_priv *priv = dev_get_priv(dev);
1033 return _cpsw_recv(priv, packetp);
1036 static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1039 struct cpsw_priv *priv = dev_get_priv(dev);
1041 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1044 static void cpsw_eth_stop(struct udevice *dev)
1046 struct cpsw_priv *priv = dev_get_priv(dev);
1048 return _cpsw_halt(priv);
1052 static int cpsw_eth_probe(struct udevice *dev)
1054 struct cpsw_priv *priv = dev_get_priv(dev);
1058 return _cpsw_register(priv);
1061 static const struct eth_ops cpsw_eth_ops = {
1062 .start = cpsw_eth_start,
1063 .send = cpsw_eth_send,
1064 .recv = cpsw_eth_recv,
1065 .free_pkt = cpsw_eth_free_pkt,
1066 .stop = cpsw_eth_stop,
1069 static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node)
1071 return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL,
1075 static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1076 phy_interface_t phy_mode)
1081 bool rgmii_id = false;
1082 int slave = priv->data.active_slave;
1084 reg = readl(priv->data.gmii_sel);
1087 case PHY_INTERFACE_MODE_RMII:
1088 mode = AM33XX_GMII_SEL_MODE_RMII;
1091 case PHY_INTERFACE_MODE_RGMII:
1092 mode = AM33XX_GMII_SEL_MODE_RGMII;
1094 case PHY_INTERFACE_MODE_RGMII_ID:
1095 case PHY_INTERFACE_MODE_RGMII_RXID:
1096 case PHY_INTERFACE_MODE_RGMII_TXID:
1097 mode = AM33XX_GMII_SEL_MODE_RGMII;
1101 case PHY_INTERFACE_MODE_MII:
1103 mode = AM33XX_GMII_SEL_MODE_MII;
1107 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1110 if (priv->data.rmii_clock_external) {
1112 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1114 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1119 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1121 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1127 writel(reg, priv->data.gmii_sel);
1130 static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1131 phy_interface_t phy_mode)
1136 int slave = priv->data.active_slave;
1138 reg = readl(priv->data.gmii_sel);
1141 case PHY_INTERFACE_MODE_RMII:
1142 mode = AM33XX_GMII_SEL_MODE_RMII;
1145 case PHY_INTERFACE_MODE_RGMII:
1146 case PHY_INTERFACE_MODE_RGMII_ID:
1147 case PHY_INTERFACE_MODE_RGMII_RXID:
1148 case PHY_INTERFACE_MODE_RGMII_TXID:
1149 mode = AM33XX_GMII_SEL_MODE_RGMII;
1152 case PHY_INTERFACE_MODE_MII:
1154 mode = AM33XX_GMII_SEL_MODE_MII;
1160 mask = GMII_SEL_MODE_MASK;
1163 mask = GMII_SEL_MODE_MASK << 4;
1167 dev_err(priv->dev, "invalid slave number...\n");
1171 if (priv->data.rmii_clock_external)
1172 dev_err(priv->dev, "RMII External clock is not supported\n");
1177 writel(reg, priv->data.gmii_sel);
1180 static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1181 phy_interface_t phy_mode)
1183 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1184 cpsw_gmii_sel_am3352(priv, phy_mode);
1185 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1186 cpsw_gmii_sel_am3352(priv, phy_mode);
1187 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1188 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1191 static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1193 struct eth_pdata *pdata = dev_get_platdata(dev);
1194 struct cpsw_priv *priv = dev_get_priv(dev);
1195 struct gpio_desc *mode_gpios;
1196 const char *phy_mode;
1197 const char *phy_sel_compat = NULL;
1198 const void *fdt = gd->fdt_blob;
1199 int node = dev_of_offset(dev);
1201 int slave_index = 0;
1206 pdata->iobase = devfdt_get_addr(dev);
1207 priv->data.version = CPSW_CTRL_VERSION_2;
1208 priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
1209 priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
1210 priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1211 priv->data.mdio_div = CPSW_MDIO_DIV;
1212 priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
1214 pdata->phy_interface = -1;
1216 priv->data.cpsw_base = pdata->iobase;
1217 priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
1218 if (priv->data.channels <= 0) {
1219 printf("error: cpdma_channels not found in dt\n");
1223 priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
1224 if (priv->data.slaves <= 0) {
1225 printf("error: slaves not found in dt\n");
1228 priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
1231 priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
1232 if (priv->data.ale_entries <= 0) {
1233 printf("error: ale_entries not found in dt\n");
1237 priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
1238 if (priv->data.bd_ram_ofs <= 0) {
1239 printf("error: bd_ram_size not found in dt\n");
1243 priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
1244 if (priv->data.mac_control <= 0) {
1245 printf("error: ale_entries not found in dt\n");
1249 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1250 if (num_mode_gpios > 0) {
1251 mode_gpios = malloc(sizeof(struct gpio_desc) *
1253 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1254 num_mode_gpios, GPIOD_IS_OUT);
1258 active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
1259 priv->data.active_slave = active_slave;
1261 fdt_for_each_subnode(subnode, fdt, node) {
1265 name = fdt_get_name(fdt, subnode, &len);
1266 if (!strncmp(name, "mdio", 4)) {
1269 mdio_base = cpsw_get_addr_by_node(fdt, subnode);
1270 if (mdio_base == FDT_ADDR_T_NONE) {
1271 pr_err("Not able to get MDIO address space\n");
1274 priv->data.mdio_base = mdio_base;
1277 if (!strncmp(name, "slave", 5)) {
1280 if (slave_index >= priv->data.slaves)
1282 phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
1284 priv->data.slave_data[slave_index].phy_if =
1285 phy_get_interface_by_name(phy_mode);
1287 priv->data.slave_data[slave_index].phy_of_handle =
1288 fdtdec_lookup_phandle(fdt, subnode,
1291 if (priv->data.slave_data[slave_index].phy_of_handle >= 0) {
1292 priv->data.slave_data[slave_index].phy_addr =
1293 fdtdec_get_int(gd->fdt_blob,
1294 priv->data.slave_data[slave_index].phy_of_handle,
1297 fdtdec_get_int_array(fdt, subnode, "phy_id",
1299 priv->data.slave_data[slave_index].phy_addr =
1305 if (!strncmp(name, "cpsw-phy-sel", 12)) {
1306 priv->data.gmii_sel = cpsw_get_addr_by_node(fdt,
1309 if (priv->data.gmii_sel == FDT_ADDR_T_NONE) {
1310 pr_err("Not able to get gmii_sel reg address\n");
1314 if (fdt_get_property(fdt, subnode, "rmii-clock-ext",
1316 priv->data.rmii_clock_external = true;
1318 phy_sel_compat = fdt_getprop(fdt, subnode, "compatible",
1320 if (!phy_sel_compat) {
1321 pr_err("Not able to get gmii_sel compatible\n");
1327 priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1328 priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
1330 if (priv->data.slaves == 2) {
1331 priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1332 priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
1335 ret = ti_cm_get_macid(dev, active_slave, pdata->enetaddr);
1337 pr_err("cpsw read efuse mac failed\n");
1341 pdata->phy_interface = priv->data.slave_data[active_slave].phy_if;
1342 if (pdata->phy_interface == -1) {
1343 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1347 /* Select phy interface in control module */
1348 cpsw_phy_sel(priv, phy_sel_compat, pdata->phy_interface);
1353 int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1355 struct cpsw_priv *priv = dev_get_priv(dev);
1356 struct cpsw_platform_data *data = &priv->data;
1358 return data->slave_data[slave].phy_addr;
1361 static const struct udevice_id cpsw_eth_ids[] = {
1362 { .compatible = "ti,cpsw" },
1363 { .compatible = "ti,am335x-cpsw" },
1367 U_BOOT_DRIVER(eth_cpsw) = {
1370 .of_match = cpsw_eth_ids,
1371 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
1372 .probe = cpsw_eth_probe,
1373 .ops = &cpsw_eth_ops,
1374 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
1375 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1376 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1378 #endif /* CONFIG_DM_ETH */