Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2005-2006 Atmel Corporation |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 6 | #include <clk.h> |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 8 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 9 | /* |
| 10 | * The u-boot networking stack is a little weird. It seems like the |
| 11 | * networking core allocates receive buffers up front without any |
| 12 | * regard to the hardware that's supposed to actually receive those |
| 13 | * packets. |
| 14 | * |
| 15 | * The MACB receives packets into 128-byte receive buffers, so the |
| 16 | * buffers allocated by the core isn't very practical to use. We'll |
| 17 | * allocate our own, but we need one such buffer in case a packet |
| 18 | * wraps around the DMA ring so that we have to copy it. |
| 19 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 20 | * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 21 | * configuration header. This way, the core allocates one RX buffer |
| 22 | * and one TX buffer, each of which can hold a ethernet packet of |
| 23 | * maximum size. |
| 24 | * |
| 25 | * For some reason, the networking core unconditionally specifies a |
| 26 | * 32-byte packet "alignment" (which really should be called |
| 27 | * "padding"). MACB shouldn't need that, but we'll refrain from any |
| 28 | * core modifications here... |
| 29 | */ |
| 30 | |
| 31 | #include <net.h> |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 32 | #ifndef CONFIG_DM_ETH |
Ben Warren | 89973f8 | 2008-08-31 22:22:04 -0700 | [diff] [blame] | 33 | #include <netdev.h> |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 34 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 35 | #include <malloc.h> |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 36 | #include <miiphy.h> |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 37 | |
| 38 | #include <linux/mii.h> |
| 39 | #include <asm/io.h> |
| 40 | #include <asm/dma-mapping.h> |
| 41 | #include <asm/arch/clk.h> |
Masahiro Yamada | 5d97dff | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 42 | #include <linux/errno.h> |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 43 | |
| 44 | #include "macb.h" |
| 45 | |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 46 | DECLARE_GLOBAL_DATA_PTR; |
| 47 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 48 | #define MACB_RX_BUFFER_SIZE 4096 |
| 49 | #define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128) |
| 50 | #define MACB_TX_RING_SIZE 16 |
| 51 | #define MACB_TX_TIMEOUT 1000 |
| 52 | #define MACB_AUTONEG_TIMEOUT 5000000 |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 53 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 54 | #ifdef CONFIG_MACB_ZYNQ |
| 55 | /* INCR4 AHB bursts */ |
| 56 | #define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004 |
| 57 | /* Use full configured addressable space (8 Kb) */ |
| 58 | #define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300 |
| 59 | /* Use full configured addressable space (4 Kb) */ |
| 60 | #define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400 |
| 61 | /* Set RXBUF with use of 128 byte */ |
| 62 | #define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000 |
| 63 | #define MACB_ZYNQ_GEM_DMACR_INIT \ |
| 64 | (MACB_ZYNQ_GEM_DMACR_BLENGTH | \ |
| 65 | MACB_ZYNQ_GEM_DMACR_RXSIZE | \ |
| 66 | MACB_ZYNQ_GEM_DMACR_TXSIZE | \ |
| 67 | MACB_ZYNQ_GEM_DMACR_RXBUF) |
| 68 | #endif |
| 69 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 70 | struct macb_dma_desc { |
| 71 | u32 addr; |
| 72 | u32 ctrl; |
| 73 | }; |
| 74 | |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 75 | #define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc)) |
| 76 | #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE)) |
| 77 | #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE)) |
Wu, Josh | ade4ea4 | 2015-06-03 16:45:44 +0800 | [diff] [blame] | 78 | #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1)) |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 79 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 80 | #define RXADDR_USED 0x00000001 |
| 81 | #define RXADDR_WRAP 0x00000002 |
| 82 | |
| 83 | #define RXBUF_FRMLEN_MASK 0x00000fff |
| 84 | #define RXBUF_FRAME_START 0x00004000 |
| 85 | #define RXBUF_FRAME_END 0x00008000 |
| 86 | #define RXBUF_TYPEID_MATCH 0x00400000 |
| 87 | #define RXBUF_ADDR4_MATCH 0x00800000 |
| 88 | #define RXBUF_ADDR3_MATCH 0x01000000 |
| 89 | #define RXBUF_ADDR2_MATCH 0x02000000 |
| 90 | #define RXBUF_ADDR1_MATCH 0x04000000 |
| 91 | #define RXBUF_BROADCAST 0x80000000 |
| 92 | |
| 93 | #define TXBUF_FRMLEN_MASK 0x000007ff |
| 94 | #define TXBUF_FRAME_END 0x00008000 |
| 95 | #define TXBUF_NOCRC 0x00010000 |
| 96 | #define TXBUF_EXHAUSTED 0x08000000 |
| 97 | #define TXBUF_UNDERRUN 0x10000000 |
| 98 | #define TXBUF_MAXRETRY 0x20000000 |
| 99 | #define TXBUF_WRAP 0x40000000 |
| 100 | #define TXBUF_USED 0x80000000 |
| 101 | |
| 102 | struct macb_device { |
| 103 | void *regs; |
| 104 | |
| 105 | unsigned int rx_tail; |
| 106 | unsigned int tx_head; |
| 107 | unsigned int tx_tail; |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 108 | unsigned int next_rx_tail; |
| 109 | bool wrapped; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 110 | |
| 111 | void *rx_buffer; |
| 112 | void *tx_buffer; |
| 113 | struct macb_dma_desc *rx_ring; |
| 114 | struct macb_dma_desc *tx_ring; |
| 115 | |
| 116 | unsigned long rx_buffer_dma; |
| 117 | unsigned long rx_ring_dma; |
| 118 | unsigned long tx_ring_dma; |
| 119 | |
Wu, Josh | ade4ea4 | 2015-06-03 16:45:44 +0800 | [diff] [blame] | 120 | struct macb_dma_desc *dummy_desc; |
| 121 | unsigned long dummy_desc_dma; |
| 122 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 123 | const struct device *dev; |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 124 | #ifndef CONFIG_DM_ETH |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 125 | struct eth_device netdev; |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 126 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 127 | unsigned short phy_addr; |
Bo Shen | b1a0006 | 2013-04-24 15:59:27 +0800 | [diff] [blame] | 128 | struct mii_dev *bus; |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 129 | #ifdef CONFIG_PHYLIB |
| 130 | struct phy_device *phydev; |
| 131 | #endif |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 132 | |
| 133 | #ifdef CONFIG_DM_ETH |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 134 | #ifdef CONFIG_CLK |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 135 | unsigned long pclk_rate; |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 136 | #endif |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 137 | phy_interface_t phy_interface; |
| 138 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 139 | }; |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 140 | #ifndef CONFIG_DM_ETH |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 141 | #define to_macb(_nd) container_of(_nd, struct macb_device, netdev) |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 142 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 143 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 144 | static int macb_is_gem(struct macb_device *macb) |
| 145 | { |
Atish Patra | fbcaa26 | 2019-02-25 08:14:42 +0000 | [diff] [blame] | 146 | return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2; |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Gregory CLEMENT | 75b03cf | 2015-12-16 14:50:34 +0100 | [diff] [blame] | 149 | #ifndef cpu_is_sama5d2 |
| 150 | #define cpu_is_sama5d2() 0 |
| 151 | #endif |
| 152 | |
| 153 | #ifndef cpu_is_sama5d4 |
| 154 | #define cpu_is_sama5d4() 0 |
| 155 | #endif |
| 156 | |
| 157 | static int gem_is_gigabit_capable(struct macb_device *macb) |
| 158 | { |
| 159 | /* |
Robert P. J. Day | 1cc0a9f | 2016-05-04 04:47:31 -0400 | [diff] [blame] | 160 | * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are |
Gregory CLEMENT | 75b03cf | 2015-12-16 14:50:34 +0100 | [diff] [blame] | 161 | * configured to support only 10/100. |
| 162 | */ |
| 163 | return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4(); |
| 164 | } |
| 165 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 166 | static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value) |
| 167 | { |
| 168 | unsigned long netctl; |
| 169 | unsigned long netstat; |
| 170 | unsigned long frame; |
| 171 | |
| 172 | netctl = macb_readl(macb, NCR); |
| 173 | netctl |= MACB_BIT(MPE); |
| 174 | macb_writel(macb, NCR, netctl); |
| 175 | |
| 176 | frame = (MACB_BF(SOF, 1) |
| 177 | | MACB_BF(RW, 1) |
| 178 | | MACB_BF(PHYA, macb->phy_addr) |
| 179 | | MACB_BF(REGA, reg) |
| 180 | | MACB_BF(CODE, 2) |
| 181 | | MACB_BF(DATA, value)); |
| 182 | macb_writel(macb, MAN, frame); |
| 183 | |
| 184 | do { |
| 185 | netstat = macb_readl(macb, NSR); |
| 186 | } while (!(netstat & MACB_BIT(IDLE))); |
| 187 | |
| 188 | netctl = macb_readl(macb, NCR); |
| 189 | netctl &= ~MACB_BIT(MPE); |
| 190 | macb_writel(macb, NCR, netctl); |
| 191 | } |
| 192 | |
| 193 | static u16 macb_mdio_read(struct macb_device *macb, u8 reg) |
| 194 | { |
| 195 | unsigned long netctl; |
| 196 | unsigned long netstat; |
| 197 | unsigned long frame; |
| 198 | |
| 199 | netctl = macb_readl(macb, NCR); |
| 200 | netctl |= MACB_BIT(MPE); |
| 201 | macb_writel(macb, NCR, netctl); |
| 202 | |
| 203 | frame = (MACB_BF(SOF, 1) |
| 204 | | MACB_BF(RW, 2) |
| 205 | | MACB_BF(PHYA, macb->phy_addr) |
| 206 | | MACB_BF(REGA, reg) |
| 207 | | MACB_BF(CODE, 2)); |
| 208 | macb_writel(macb, MAN, frame); |
| 209 | |
| 210 | do { |
| 211 | netstat = macb_readl(macb, NSR); |
| 212 | } while (!(netstat & MACB_BIT(IDLE))); |
| 213 | |
| 214 | frame = macb_readl(macb, MAN); |
| 215 | |
| 216 | netctl = macb_readl(macb, NCR); |
| 217 | netctl &= ~MACB_BIT(MPE); |
| 218 | macb_writel(macb, NCR, netctl); |
| 219 | |
| 220 | return MACB_BFEXT(DATA, frame); |
| 221 | } |
| 222 | |
Joe Hershberger | 1b8c18b | 2013-06-24 19:06:38 -0500 | [diff] [blame] | 223 | void __weak arch_get_mdio_control(const char *name) |
Shiraz Hashim | 416ce62 | 2012-12-13 17:22:52 +0530 | [diff] [blame] | 224 | { |
| 225 | return; |
| 226 | } |
| 227 | |
Bo Shen | b1a0006 | 2013-04-24 15:59:27 +0800 | [diff] [blame] | 228 | #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 229 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 230 | int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg) |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 231 | { |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 232 | u16 value = 0; |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 233 | #ifdef CONFIG_DM_ETH |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 234 | struct udevice *dev = eth_get_dev_by_name(bus->name); |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 235 | struct macb_device *macb = dev_get_priv(dev); |
| 236 | #else |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 237 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 238 | struct macb_device *macb = to_macb(dev); |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 239 | #endif |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 240 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 241 | if (macb->phy_addr != phy_adr) |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 242 | return -1; |
| 243 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 244 | arch_get_mdio_control(bus->name); |
| 245 | value = macb_mdio_read(macb, reg); |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 246 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 247 | return value; |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 250 | int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg, |
| 251 | u16 value) |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 252 | { |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 253 | #ifdef CONFIG_DM_ETH |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 254 | struct udevice *dev = eth_get_dev_by_name(bus->name); |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 255 | struct macb_device *macb = dev_get_priv(dev); |
| 256 | #else |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 257 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 258 | struct macb_device *macb = to_macb(dev); |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 259 | #endif |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 260 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 261 | if (macb->phy_addr != phy_adr) |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 262 | return -1; |
| 263 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 264 | arch_get_mdio_control(bus->name); |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 265 | macb_mdio_write(macb, reg, value); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | #endif |
| 270 | |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 271 | #define RX 1 |
| 272 | #define TX 0 |
| 273 | static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx) |
| 274 | { |
| 275 | if (rx) |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 276 | invalidate_dcache_range(macb->rx_ring_dma, |
| 277 | ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE, |
| 278 | PKTALIGN)); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 279 | else |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 280 | invalidate_dcache_range(macb->tx_ring_dma, |
| 281 | ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE, |
| 282 | PKTALIGN)); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx) |
| 286 | { |
| 287 | if (rx) |
| 288 | flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma + |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 289 | ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN)); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 290 | else |
| 291 | flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma + |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 292 | ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN)); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static inline void macb_flush_rx_buffer(struct macb_device *macb) |
| 296 | { |
| 297 | flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma + |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 298 | ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN)); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static inline void macb_invalidate_rx_buffer(struct macb_device *macb) |
| 302 | { |
| 303 | invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma + |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 304 | ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN)); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 305 | } |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 306 | |
Jon Loeliger | 07d38a1 | 2007-07-09 17:30:01 -0500 | [diff] [blame] | 307 | #if defined(CONFIG_CMD_NET) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 308 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 309 | static int _macb_send(struct macb_device *macb, const char *name, void *packet, |
| 310 | int length) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 311 | { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 312 | unsigned long paddr, ctrl; |
| 313 | unsigned int tx_head = macb->tx_head; |
| 314 | int i; |
| 315 | |
| 316 | paddr = dma_map_single(packet, length, DMA_TO_DEVICE); |
| 317 | |
| 318 | ctrl = length & TXBUF_FRMLEN_MASK; |
| 319 | ctrl |= TXBUF_FRAME_END; |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 320 | if (tx_head == (MACB_TX_RING_SIZE - 1)) { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 321 | ctrl |= TXBUF_WRAP; |
| 322 | macb->tx_head = 0; |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 323 | } else { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 324 | macb->tx_head++; |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 325 | } |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 326 | |
| 327 | macb->tx_ring[tx_head].ctrl = ctrl; |
| 328 | macb->tx_ring[tx_head].addr = paddr; |
Haavard Skinnemoen | 04fcb5d | 2007-05-02 13:22:38 +0200 | [diff] [blame] | 329 | barrier(); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 330 | macb_flush_ring_desc(macb, TX); |
| 331 | /* Do we need check paddr and length is dcache line aligned? */ |
Simon Glass | f589f8c | 2016-05-05 07:28:10 -0600 | [diff] [blame] | 332 | flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN)); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 333 | macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART)); |
| 334 | |
| 335 | /* |
| 336 | * I guess this is necessary because the networking core may |
| 337 | * re-use the transmit buffer as soon as we return... |
| 338 | */ |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 339 | for (i = 0; i <= MACB_TX_TIMEOUT; i++) { |
Haavard Skinnemoen | 04fcb5d | 2007-05-02 13:22:38 +0200 | [diff] [blame] | 340 | barrier(); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 341 | macb_invalidate_ring_desc(macb, TX); |
Haavard Skinnemoen | 04fcb5d | 2007-05-02 13:22:38 +0200 | [diff] [blame] | 342 | ctrl = macb->tx_ring[tx_head].ctrl; |
| 343 | if (ctrl & TXBUF_USED) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 344 | break; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 345 | udelay(1); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | dma_unmap_single(packet, length, paddr); |
| 349 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 350 | if (i <= MACB_TX_TIMEOUT) { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 351 | if (ctrl & TXBUF_UNDERRUN) |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 352 | printf("%s: TX underrun\n", name); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 353 | if (ctrl & TXBUF_EXHAUSTED) |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 354 | printf("%s: TX buffers exhausted in mid frame\n", name); |
Haavard Skinnemoen | 04fcb5d | 2007-05-02 13:22:38 +0200 | [diff] [blame] | 355 | } else { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 356 | printf("%s: TX timeout\n", name); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* No one cares anyway */ |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static void reclaim_rx_buffers(struct macb_device *macb, |
| 364 | unsigned int new_tail) |
| 365 | { |
| 366 | unsigned int i; |
| 367 | |
| 368 | i = macb->rx_tail; |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 369 | |
| 370 | macb_invalidate_ring_desc(macb, RX); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 371 | while (i > new_tail) { |
| 372 | macb->rx_ring[i].addr &= ~RXADDR_USED; |
| 373 | i++; |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 374 | if (i > MACB_RX_RING_SIZE) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 375 | i = 0; |
| 376 | } |
| 377 | |
| 378 | while (i < new_tail) { |
| 379 | macb->rx_ring[i].addr &= ~RXADDR_USED; |
| 380 | i++; |
| 381 | } |
| 382 | |
Haavard Skinnemoen | 04fcb5d | 2007-05-02 13:22:38 +0200 | [diff] [blame] | 383 | barrier(); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 384 | macb_flush_ring_desc(macb, RX); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 385 | macb->rx_tail = new_tail; |
| 386 | } |
| 387 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 388 | static int _macb_recv(struct macb_device *macb, uchar **packetp) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 389 | { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 390 | unsigned int next_rx_tail = macb->next_rx_tail; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 391 | void *buffer; |
| 392 | int length; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 393 | u32 status; |
| 394 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 395 | macb->wrapped = false; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 396 | for (;;) { |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 397 | macb_invalidate_ring_desc(macb, RX); |
| 398 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 399 | if (!(macb->rx_ring[next_rx_tail].addr & RXADDR_USED)) |
| 400 | return -EAGAIN; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 401 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 402 | status = macb->rx_ring[next_rx_tail].ctrl; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 403 | if (status & RXBUF_FRAME_START) { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 404 | if (next_rx_tail != macb->rx_tail) |
| 405 | reclaim_rx_buffers(macb, next_rx_tail); |
| 406 | macb->wrapped = false; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (status & RXBUF_FRAME_END) { |
| 410 | buffer = macb->rx_buffer + 128 * macb->rx_tail; |
| 411 | length = status & RXBUF_FRMLEN_MASK; |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 412 | |
| 413 | macb_invalidate_rx_buffer(macb); |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 414 | if (macb->wrapped) { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 415 | unsigned int headlen, taillen; |
| 416 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 417 | headlen = 128 * (MACB_RX_RING_SIZE |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 418 | - macb->rx_tail); |
| 419 | taillen = length - headlen; |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 420 | memcpy((void *)net_rx_packets[0], |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 421 | buffer, headlen); |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 422 | memcpy((void *)net_rx_packets[0] + headlen, |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 423 | macb->rx_buffer, taillen); |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 424 | *packetp = (void *)net_rx_packets[0]; |
| 425 | } else { |
| 426 | *packetp = buffer; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 429 | if (++next_rx_tail >= MACB_RX_RING_SIZE) |
| 430 | next_rx_tail = 0; |
| 431 | macb->next_rx_tail = next_rx_tail; |
| 432 | return length; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 433 | } else { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 434 | if (++next_rx_tail >= MACB_RX_RING_SIZE) { |
| 435 | macb->wrapped = true; |
| 436 | next_rx_tail = 0; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 437 | } |
| 438 | } |
Haavard Skinnemoen | 04fcb5d | 2007-05-02 13:22:38 +0200 | [diff] [blame] | 439 | barrier(); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 440 | } |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 443 | static void macb_phy_reset(struct macb_device *macb, const char *name) |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 444 | { |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 445 | int i; |
| 446 | u16 status, adv; |
| 447 | |
| 448 | adv = ADVERTISE_CSMA | ADVERTISE_ALL; |
| 449 | macb_mdio_write(macb, MII_ADVERTISE, adv); |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 450 | printf("%s: Starting autonegotiation...\n", name); |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 451 | macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE |
| 452 | | BMCR_ANRESTART)); |
| 453 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 454 | for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) { |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 455 | status = macb_mdio_read(macb, MII_BMSR); |
| 456 | if (status & BMSR_ANEGCOMPLETE) |
| 457 | break; |
| 458 | udelay(100); |
| 459 | } |
| 460 | |
| 461 | if (status & BMSR_ANEGCOMPLETE) |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 462 | printf("%s: Autonegotiation complete\n", name); |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 463 | else |
| 464 | printf("%s: Autonegotiation timed out (status=0x%04x)\n", |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 465 | name, status); |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 468 | static int macb_phy_find(struct macb_device *macb, const char *name) |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 469 | { |
| 470 | int i; |
| 471 | u16 phy_id; |
| 472 | |
| 473 | /* Search for PHY... */ |
| 474 | for (i = 0; i < 32; i++) { |
| 475 | macb->phy_addr = i; |
| 476 | phy_id = macb_mdio_read(macb, MII_PHYSID1); |
| 477 | if (phy_id != 0xffff) { |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 478 | printf("%s: PHY present at %d\n", name, i); |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 479 | return 0; |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | /* PHY isn't up to snuff */ |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 484 | printf("%s: PHY not found\n", name); |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 485 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 486 | return -ENODEV; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * macb_linkspd_cb - Linkspeed change callback function |
| 491 | * @regs: Base Register of MACB devices |
| 492 | * @speed: Linkspeed |
| 493 | * Returns 0 when operation success and negative errno number |
| 494 | * when operation failed. |
| 495 | */ |
| 496 | int __weak macb_linkspd_cb(void *regs, unsigned int speed) |
| 497 | { |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | } |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 500 | |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 501 | #ifdef CONFIG_DM_ETH |
| 502 | static int macb_phy_init(struct udevice *dev, const char *name) |
| 503 | #else |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 504 | static int macb_phy_init(struct macb_device *macb, const char *name) |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 505 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 506 | { |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 507 | #ifdef CONFIG_DM_ETH |
| 508 | struct macb_device *macb = dev_get_priv(dev); |
| 509 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 510 | u32 ncfgr; |
| 511 | u16 phy_id, status, adv, lpa; |
| 512 | int media, speed, duplex; |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 513 | int ret; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 514 | int i; |
| 515 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 516 | arch_get_mdio_control(name); |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 517 | /* Auto-detect phy_addr */ |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 518 | ret = macb_phy_find(macb, name); |
| 519 | if (ret) |
| 520 | return ret; |
Gunnar Rangoy | fc01ea1 | 2009-01-23 12:56:31 +0100 | [diff] [blame] | 521 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 522 | /* Check if the PHY is up to snuff... */ |
| 523 | phy_id = macb_mdio_read(macb, MII_PHYSID1); |
| 524 | if (phy_id == 0xffff) { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 525 | printf("%s: No PHY present\n", name); |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 526 | return -ENODEV; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Bo Shen | b1a0006 | 2013-04-24 15:59:27 +0800 | [diff] [blame] | 529 | #ifdef CONFIG_PHYLIB |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 530 | #ifdef CONFIG_DM_ETH |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 531 | macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev, |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 532 | macb->phy_interface); |
| 533 | #else |
Bo Shen | 8314ccd | 2013-08-19 10:35:47 +0800 | [diff] [blame] | 534 | /* need to consider other phy interface mode */ |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 535 | macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev, |
Bo Shen | 8314ccd | 2013-08-19 10:35:47 +0800 | [diff] [blame] | 536 | PHY_INTERFACE_MODE_RGMII); |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 537 | #endif |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 538 | if (!macb->phydev) { |
Bo Shen | 8314ccd | 2013-08-19 10:35:47 +0800 | [diff] [blame] | 539 | printf("phy_connect failed\n"); |
| 540 | return -ENODEV; |
| 541 | } |
| 542 | |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 543 | phy_config(macb->phydev); |
Bo Shen | b1a0006 | 2013-04-24 15:59:27 +0800 | [diff] [blame] | 544 | #endif |
| 545 | |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 546 | status = macb_mdio_read(macb, MII_BMSR); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 547 | if (!(status & BMSR_LSTATUS)) { |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 548 | /* Try to re-negotiate if we don't have link already. */ |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 549 | macb_phy_reset(macb, name); |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 550 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 551 | for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 552 | status = macb_mdio_read(macb, MII_BMSR); |
Stefan Roese | 7bf9bca | 2019-03-27 11:20:19 +0100 | [diff] [blame] | 553 | if (status & BMSR_LSTATUS) { |
| 554 | /* |
| 555 | * Delay a bit after the link is established, |
| 556 | * so that the next xfer does not fail |
| 557 | */ |
| 558 | mdelay(10); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 559 | break; |
Stefan Roese | 7bf9bca | 2019-03-27 11:20:19 +0100 | [diff] [blame] | 560 | } |
Haavard Skinnemoen | f2134f8 | 2007-05-02 13:31:53 +0200 | [diff] [blame] | 561 | udelay(100); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | if (!(status & BMSR_LSTATUS)) { |
| 566 | printf("%s: link down (status: 0x%04x)\n", |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 567 | name, status); |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 568 | return -ENETDOWN; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 569 | } |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 570 | |
Gregory CLEMENT | 75b03cf | 2015-12-16 14:50:34 +0100 | [diff] [blame] | 571 | /* First check for GMAC and that it is GiB capable */ |
| 572 | if (gem_is_gigabit_capable(macb)) { |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 573 | lpa = macb_mdio_read(macb, MII_STAT1000); |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 574 | |
Andreas Bießmann | 4760957 | 2014-09-18 23:46:48 +0200 | [diff] [blame] | 575 | if (lpa & (LPA_1000FULL | LPA_1000HALF)) { |
| 576 | duplex = ((lpa & LPA_1000FULL) ? 1 : 0); |
| 577 | |
| 578 | printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n", |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 579 | name, |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 580 | duplex ? "full" : "half", |
| 581 | lpa); |
| 582 | |
| 583 | ncfgr = macb_readl(macb, NCFGR); |
Andreas Bießmann | 4760957 | 2014-09-18 23:46:48 +0200 | [diff] [blame] | 584 | ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); |
| 585 | ncfgr |= GEM_BIT(GBE); |
| 586 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 587 | if (duplex) |
| 588 | ncfgr |= MACB_BIT(FD); |
Andreas Bießmann | 4760957 | 2014-09-18 23:46:48 +0200 | [diff] [blame] | 589 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 590 | macb_writel(macb, NCFGR, ncfgr); |
| 591 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 592 | ret = macb_linkspd_cb(macb->regs, _1000BASET); |
| 593 | if (ret) |
| 594 | return ret; |
| 595 | |
| 596 | return 0; |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | /* fall back for EMAC checking */ |
| 601 | adv = macb_mdio_read(macb, MII_ADVERTISE); |
| 602 | lpa = macb_mdio_read(macb, MII_LPA); |
| 603 | media = mii_nway_result(lpa & adv); |
| 604 | speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) |
| 605 | ? 1 : 0); |
| 606 | duplex = (media & ADVERTISE_FULL) ? 1 : 0; |
| 607 | printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n", |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 608 | name, |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 609 | speed ? "100" : "10", |
| 610 | duplex ? "full" : "half", |
| 611 | lpa); |
| 612 | |
| 613 | ncfgr = macb_readl(macb, NCFGR); |
Bo Shen | c83cb5f | 2015-03-04 13:35:16 +0800 | [diff] [blame] | 614 | ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE)); |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 615 | if (speed) { |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 616 | ncfgr |= MACB_BIT(SPD); |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 617 | ret = macb_linkspd_cb(macb->regs, _100BASET); |
| 618 | } else { |
| 619 | ret = macb_linkspd_cb(macb->regs, _10BASET); |
| 620 | } |
| 621 | |
| 622 | if (ret) |
| 623 | return ret; |
| 624 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 625 | if (duplex) |
| 626 | ncfgr |= MACB_BIT(FD); |
| 627 | macb_writel(macb, NCFGR, ncfgr); |
| 628 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 629 | return 0; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Wu, Josh | ade4ea4 | 2015-06-03 16:45:44 +0800 | [diff] [blame] | 632 | static int gmac_init_multi_queues(struct macb_device *macb) |
| 633 | { |
| 634 | int i, num_queues = 1; |
| 635 | u32 queue_mask; |
| 636 | |
| 637 | /* bit 0 is never set but queue 0 always exists */ |
| 638 | queue_mask = gem_readl(macb, DCFG6) & 0xff; |
| 639 | queue_mask |= 0x1; |
| 640 | |
| 641 | for (i = 1; i < MACB_MAX_QUEUES; i++) |
| 642 | if (queue_mask & (1 << i)) |
| 643 | num_queues++; |
| 644 | |
| 645 | macb->dummy_desc->ctrl = TXBUF_USED; |
| 646 | macb->dummy_desc->addr = 0; |
| 647 | flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma + |
Heiko Schocher | 592a749 | 2016-08-29 07:46:11 +0200 | [diff] [blame] | 648 | ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN)); |
Wu, Josh | ade4ea4 | 2015-06-03 16:45:44 +0800 | [diff] [blame] | 649 | |
| 650 | for (i = 1; i < num_queues; i++) |
| 651 | gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 656 | #ifdef CONFIG_DM_ETH |
| 657 | static int _macb_init(struct udevice *dev, const char *name) |
| 658 | #else |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 659 | static int _macb_init(struct macb_device *macb, const char *name) |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 660 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 661 | { |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 662 | #ifdef CONFIG_DM_ETH |
| 663 | struct macb_device *macb = dev_get_priv(dev); |
| 664 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 665 | unsigned long paddr; |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 666 | int ret; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 667 | int i; |
| 668 | |
| 669 | /* |
| 670 | * macb_halt should have been called at some point before now, |
| 671 | * so we'll assume the controller is idle. |
| 672 | */ |
| 673 | |
| 674 | /* initialize DMA descriptors */ |
| 675 | paddr = macb->rx_buffer_dma; |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 676 | for (i = 0; i < MACB_RX_RING_SIZE; i++) { |
| 677 | if (i == (MACB_RX_RING_SIZE - 1)) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 678 | paddr |= RXADDR_WRAP; |
| 679 | macb->rx_ring[i].addr = paddr; |
| 680 | macb->rx_ring[i].ctrl = 0; |
| 681 | paddr += 128; |
| 682 | } |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 683 | macb_flush_ring_desc(macb, RX); |
| 684 | macb_flush_rx_buffer(macb); |
| 685 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 686 | for (i = 0; i < MACB_TX_RING_SIZE; i++) { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 687 | macb->tx_ring[i].addr = 0; |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 688 | if (i == (MACB_TX_RING_SIZE - 1)) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 689 | macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP; |
| 690 | else |
| 691 | macb->tx_ring[i].ctrl = TXBUF_USED; |
| 692 | } |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 693 | macb_flush_ring_desc(macb, TX); |
| 694 | |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 695 | macb->rx_tail = 0; |
| 696 | macb->tx_head = 0; |
| 697 | macb->tx_tail = 0; |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 698 | macb->next_rx_tail = 0; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 699 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 700 | #ifdef CONFIG_MACB_ZYNQ |
| 701 | macb_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT); |
| 702 | #endif |
| 703 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 704 | macb_writel(macb, RBQP, macb->rx_ring_dma); |
| 705 | macb_writel(macb, TBQP, macb->tx_ring_dma); |
| 706 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 707 | if (macb_is_gem(macb)) { |
Wu, Josh | ade4ea4 | 2015-06-03 16:45:44 +0800 | [diff] [blame] | 708 | /* Check the multi queue and initialize the queue for tx */ |
| 709 | gmac_init_multi_queues(macb); |
| 710 | |
Bo Shen | cabf61c | 2014-11-10 15:24:01 +0800 | [diff] [blame] | 711 | /* |
| 712 | * When the GMAC IP with GE feature, this bit is used to |
| 713 | * select interface between RGMII and GMII. |
| 714 | * When the GMAC IP without GE feature, this bit is used |
| 715 | * to select interface between RMII and MII. |
| 716 | */ |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 717 | #ifdef CONFIG_DM_ETH |
Wenyou Yang | 6de046e | 2017-04-20 11:13:13 +0800 | [diff] [blame] | 718 | if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) || |
| 719 | (macb->phy_interface == PHY_INTERFACE_MODE_RGMII)) |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 720 | gem_writel(macb, UR, GEM_BIT(RGMII)); |
| 721 | else |
| 722 | gem_writel(macb, UR, 0); |
| 723 | #else |
Bo Shen | cabf61c | 2014-11-10 15:24:01 +0800 | [diff] [blame] | 724 | #if defined(CONFIG_RGMII) || defined(CONFIG_RMII) |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 725 | gem_writel(macb, UR, GEM_BIT(RGMII)); |
| 726 | #else |
| 727 | gem_writel(macb, UR, 0); |
| 728 | #endif |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 729 | #endif |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 730 | } else { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 731 | /* choose RMII or MII mode. This depends on the board */ |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 732 | #ifdef CONFIG_DM_ETH |
| 733 | #ifdef CONFIG_AT91FAMILY |
| 734 | if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) { |
| 735 | macb_writel(macb, USRIO, |
| 736 | MACB_BIT(RMII) | MACB_BIT(CLKEN)); |
| 737 | } else { |
| 738 | macb_writel(macb, USRIO, MACB_BIT(CLKEN)); |
| 739 | } |
| 740 | #else |
| 741 | if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) |
| 742 | macb_writel(macb, USRIO, 0); |
| 743 | else |
| 744 | macb_writel(macb, USRIO, MACB_BIT(MII)); |
| 745 | #endif |
| 746 | #else |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 747 | #ifdef CONFIG_RMII |
Bo Shen | d8f64b4 | 2013-04-24 15:59:26 +0800 | [diff] [blame] | 748 | #ifdef CONFIG_AT91FAMILY |
Stelian Pop | 7263ef1 | 2008-01-03 21:15:56 +0000 | [diff] [blame] | 749 | macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN)); |
| 750 | #else |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 751 | macb_writel(macb, USRIO, 0); |
Stelian Pop | 7263ef1 | 2008-01-03 21:15:56 +0000 | [diff] [blame] | 752 | #endif |
| 753 | #else |
Bo Shen | d8f64b4 | 2013-04-24 15:59:26 +0800 | [diff] [blame] | 754 | #ifdef CONFIG_AT91FAMILY |
Stelian Pop | 7263ef1 | 2008-01-03 21:15:56 +0000 | [diff] [blame] | 755 | macb_writel(macb, USRIO, MACB_BIT(CLKEN)); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 756 | #else |
| 757 | macb_writel(macb, USRIO, MACB_BIT(MII)); |
| 758 | #endif |
Stelian Pop | 7263ef1 | 2008-01-03 21:15:56 +0000 | [diff] [blame] | 759 | #endif /* CONFIG_RMII */ |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 760 | #endif |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 761 | } |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 762 | |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 763 | #ifdef CONFIG_DM_ETH |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 764 | ret = macb_phy_init(dev, name); |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 765 | #else |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 766 | ret = macb_phy_init(macb, name); |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 767 | #endif |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 768 | if (ret) |
| 769 | return ret; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 770 | |
| 771 | /* Enable TX and RX */ |
| 772 | macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE)); |
| 773 | |
Ben Warren | 422b1a0 | 2008-01-09 18:15:53 -0500 | [diff] [blame] | 774 | return 0; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 777 | static void _macb_halt(struct macb_device *macb) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 778 | { |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 779 | u32 ncr, tsr; |
| 780 | |
| 781 | /* Halt the controller and wait for any ongoing transmission to end. */ |
| 782 | ncr = macb_readl(macb, NCR); |
| 783 | ncr |= MACB_BIT(THALT); |
| 784 | macb_writel(macb, NCR, ncr); |
| 785 | |
| 786 | do { |
| 787 | tsr = macb_readl(macb, TSR); |
| 788 | } while (tsr & MACB_BIT(TGO)); |
| 789 | |
| 790 | /* Disable TX and RX, and clear statistics */ |
| 791 | macb_writel(macb, NCR, MACB_BIT(CLRSTAT)); |
| 792 | } |
| 793 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 794 | static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr) |
Ben Warren | 6bb4679 | 2010-06-01 11:55:42 -0700 | [diff] [blame] | 795 | { |
Ben Warren | 6bb4679 | 2010-06-01 11:55:42 -0700 | [diff] [blame] | 796 | u32 hwaddr_bottom; |
| 797 | u16 hwaddr_top; |
| 798 | |
| 799 | /* set hardware address */ |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 800 | hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 | |
| 801 | enetaddr[2] << 16 | enetaddr[3] << 24; |
Ben Warren | 6bb4679 | 2010-06-01 11:55:42 -0700 | [diff] [blame] | 802 | macb_writel(macb, SA1B, hwaddr_bottom); |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 803 | hwaddr_top = enetaddr[4] | enetaddr[5] << 8; |
Ben Warren | 6bb4679 | 2010-06-01 11:55:42 -0700 | [diff] [blame] | 804 | macb_writel(macb, SA1T, hwaddr_top); |
| 805 | return 0; |
| 806 | } |
| 807 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 808 | static u32 macb_mdc_clk_div(int id, struct macb_device *macb) |
| 809 | { |
| 810 | u32 config; |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 811 | #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK) |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 812 | unsigned long macb_hz = macb->pclk_rate; |
| 813 | #else |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 814 | unsigned long macb_hz = get_macb_pclk_rate(id); |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 815 | #endif |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 816 | |
| 817 | if (macb_hz < 20000000) |
| 818 | config = MACB_BF(CLK, MACB_CLK_DIV8); |
| 819 | else if (macb_hz < 40000000) |
| 820 | config = MACB_BF(CLK, MACB_CLK_DIV16); |
| 821 | else if (macb_hz < 80000000) |
| 822 | config = MACB_BF(CLK, MACB_CLK_DIV32); |
| 823 | else |
| 824 | config = MACB_BF(CLK, MACB_CLK_DIV64); |
| 825 | |
| 826 | return config; |
| 827 | } |
| 828 | |
| 829 | static u32 gem_mdc_clk_div(int id, struct macb_device *macb) |
| 830 | { |
| 831 | u32 config; |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 832 | |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 833 | #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK) |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 834 | unsigned long macb_hz = macb->pclk_rate; |
| 835 | #else |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 836 | unsigned long macb_hz = get_macb_pclk_rate(id); |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 837 | #endif |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 838 | |
| 839 | if (macb_hz < 20000000) |
| 840 | config = GEM_BF(CLK, GEM_CLK_DIV8); |
| 841 | else if (macb_hz < 40000000) |
| 842 | config = GEM_BF(CLK, GEM_CLK_DIV16); |
| 843 | else if (macb_hz < 80000000) |
| 844 | config = GEM_BF(CLK, GEM_CLK_DIV32); |
| 845 | else if (macb_hz < 120000000) |
| 846 | config = GEM_BF(CLK, GEM_CLK_DIV48); |
| 847 | else if (macb_hz < 160000000) |
| 848 | config = GEM_BF(CLK, GEM_CLK_DIV64); |
| 849 | else |
| 850 | config = GEM_BF(CLK, GEM_CLK_DIV96); |
| 851 | |
| 852 | return config; |
| 853 | } |
| 854 | |
Bo Shen | 32e4f6b | 2013-09-18 15:07:44 +0800 | [diff] [blame] | 855 | /* |
| 856 | * Get the DMA bus width field of the network configuration register that we |
| 857 | * should program. We find the width from decoding the design configuration |
| 858 | * register to find the maximum supported data bus width. |
| 859 | */ |
| 860 | static u32 macb_dbw(struct macb_device *macb) |
| 861 | { |
| 862 | switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) { |
| 863 | case 4: |
| 864 | return GEM_BF(DBW, GEM_DBW128); |
| 865 | case 2: |
| 866 | return GEM_BF(DBW, GEM_DBW64); |
| 867 | case 1: |
| 868 | default: |
| 869 | return GEM_BF(DBW, GEM_DBW32); |
| 870 | } |
| 871 | } |
| 872 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 873 | static void _macb_eth_initialize(struct macb_device *macb) |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 874 | { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 875 | int id = 0; /* This is not used by functions we call */ |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 876 | u32 ncfgr; |
| 877 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 878 | /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */ |
Andreas Bießmann | ceef983 | 2014-05-26 22:55:18 +0200 | [diff] [blame] | 879 | macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE, |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 880 | &macb->rx_buffer_dma); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 881 | macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE, |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 882 | &macb->rx_ring_dma); |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 883 | macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE, |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 884 | &macb->tx_ring_dma); |
Wu, Josh | ade4ea4 | 2015-06-03 16:45:44 +0800 | [diff] [blame] | 885 | macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE, |
| 886 | &macb->dummy_desc_dma); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 887 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 888 | /* |
| 889 | * Do some basic initialization so that we at least can talk |
| 890 | * to the PHY |
| 891 | */ |
| 892 | if (macb_is_gem(macb)) { |
| 893 | ncfgr = gem_mdc_clk_div(id, macb); |
| 894 | ncfgr |= macb_dbw(macb); |
| 895 | } else { |
| 896 | ncfgr = macb_mdc_clk_div(id, macb); |
| 897 | } |
| 898 | |
| 899 | macb_writel(macb, NCFGR, ncfgr); |
| 900 | } |
| 901 | |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 902 | #ifndef CONFIG_DM_ETH |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 903 | static int macb_send(struct eth_device *netdev, void *packet, int length) |
| 904 | { |
| 905 | struct macb_device *macb = to_macb(netdev); |
| 906 | |
| 907 | return _macb_send(macb, netdev->name, packet, length); |
| 908 | } |
| 909 | |
| 910 | static int macb_recv(struct eth_device *netdev) |
| 911 | { |
| 912 | struct macb_device *macb = to_macb(netdev); |
| 913 | uchar *packet; |
| 914 | int length; |
| 915 | |
| 916 | macb->wrapped = false; |
| 917 | for (;;) { |
| 918 | macb->next_rx_tail = macb->rx_tail; |
| 919 | length = _macb_recv(macb, &packet); |
| 920 | if (length >= 0) { |
| 921 | net_process_received_packet(packet, length); |
| 922 | reclaim_rx_buffers(macb, macb->next_rx_tail); |
Heinrich Schuchardt | 6cdf072 | 2018-03-18 11:32:53 +0100 | [diff] [blame] | 923 | } else { |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 924 | return length; |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | static int macb_init(struct eth_device *netdev, bd_t *bd) |
| 930 | { |
| 931 | struct macb_device *macb = to_macb(netdev); |
| 932 | |
| 933 | return _macb_init(macb, netdev->name); |
| 934 | } |
| 935 | |
| 936 | static void macb_halt(struct eth_device *netdev) |
| 937 | { |
| 938 | struct macb_device *macb = to_macb(netdev); |
| 939 | |
| 940 | return _macb_halt(macb); |
| 941 | } |
| 942 | |
| 943 | static int macb_write_hwaddr(struct eth_device *netdev) |
| 944 | { |
| 945 | struct macb_device *macb = to_macb(netdev); |
| 946 | |
| 947 | return _macb_write_hwaddr(macb, netdev->enetaddr); |
| 948 | } |
| 949 | |
| 950 | int macb_eth_initialize(int id, void *regs, unsigned int phy_addr) |
| 951 | { |
| 952 | struct macb_device *macb; |
| 953 | struct eth_device *netdev; |
| 954 | |
| 955 | macb = malloc(sizeof(struct macb_device)); |
| 956 | if (!macb) { |
| 957 | printf("Error: Failed to allocate memory for MACB%d\n", id); |
| 958 | return -1; |
| 959 | } |
| 960 | memset(macb, 0, sizeof(struct macb_device)); |
| 961 | |
| 962 | netdev = &macb->netdev; |
Wu, Josh | 5ae0e38 | 2014-05-27 16:31:05 +0800 | [diff] [blame] | 963 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 964 | macb->regs = regs; |
| 965 | macb->phy_addr = phy_addr; |
| 966 | |
Bo Shen | d256be2 | 2013-04-24 15:59:28 +0800 | [diff] [blame] | 967 | if (macb_is_gem(macb)) |
| 968 | sprintf(netdev->name, "gmac%d", id); |
| 969 | else |
| 970 | sprintf(netdev->name, "macb%d", id); |
| 971 | |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 972 | netdev->init = macb_init; |
| 973 | netdev->halt = macb_halt; |
| 974 | netdev->send = macb_send; |
| 975 | netdev->recv = macb_recv; |
Ben Warren | 6bb4679 | 2010-06-01 11:55:42 -0700 | [diff] [blame] | 976 | netdev->write_hwaddr = macb_write_hwaddr; |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 977 | |
Simon Glass | d5555b7 | 2016-05-05 07:28:09 -0600 | [diff] [blame] | 978 | _macb_eth_initialize(macb); |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 979 | |
| 980 | eth_register(netdev); |
| 981 | |
Bo Shen | b1a0006 | 2013-04-24 15:59:27 +0800 | [diff] [blame] | 982 | #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 983 | int retval; |
| 984 | struct mii_dev *mdiodev = mdio_alloc(); |
| 985 | if (!mdiodev) |
| 986 | return -ENOMEM; |
| 987 | strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN); |
| 988 | mdiodev->read = macb_miiphy_read; |
| 989 | mdiodev->write = macb_miiphy_write; |
| 990 | |
| 991 | retval = mdio_register(mdiodev); |
| 992 | if (retval < 0) |
| 993 | return retval; |
Bo Shen | b1a0006 | 2013-04-24 15:59:27 +0800 | [diff] [blame] | 994 | macb->bus = miiphy_get_dev_by_name(netdev->name); |
Semih Hazar | 0f751d6 | 2009-12-17 15:07:15 +0200 | [diff] [blame] | 995 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 996 | return 0; |
| 997 | } |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 998 | #endif /* !CONFIG_DM_ETH */ |
| 999 | |
| 1000 | #ifdef CONFIG_DM_ETH |
| 1001 | |
| 1002 | static int macb_start(struct udevice *dev) |
| 1003 | { |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 1004 | return _macb_init(dev, dev->name); |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static int macb_send(struct udevice *dev, void *packet, int length) |
| 1008 | { |
| 1009 | struct macb_device *macb = dev_get_priv(dev); |
| 1010 | |
| 1011 | return _macb_send(macb, dev->name, packet, length); |
| 1012 | } |
| 1013 | |
| 1014 | static int macb_recv(struct udevice *dev, int flags, uchar **packetp) |
| 1015 | { |
| 1016 | struct macb_device *macb = dev_get_priv(dev); |
| 1017 | |
| 1018 | macb->next_rx_tail = macb->rx_tail; |
| 1019 | macb->wrapped = false; |
| 1020 | |
| 1021 | return _macb_recv(macb, packetp); |
| 1022 | } |
| 1023 | |
| 1024 | static int macb_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 1025 | { |
| 1026 | struct macb_device *macb = dev_get_priv(dev); |
| 1027 | |
| 1028 | reclaim_rx_buffers(macb, macb->next_rx_tail); |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static void macb_stop(struct udevice *dev) |
| 1034 | { |
| 1035 | struct macb_device *macb = dev_get_priv(dev); |
| 1036 | |
| 1037 | _macb_halt(macb); |
| 1038 | } |
| 1039 | |
| 1040 | static int macb_write_hwaddr(struct udevice *dev) |
| 1041 | { |
| 1042 | struct eth_pdata *plat = dev_get_platdata(dev); |
| 1043 | struct macb_device *macb = dev_get_priv(dev); |
| 1044 | |
| 1045 | return _macb_write_hwaddr(macb, plat->enetaddr); |
| 1046 | } |
| 1047 | |
| 1048 | static const struct eth_ops macb_eth_ops = { |
| 1049 | .start = macb_start, |
| 1050 | .send = macb_send, |
| 1051 | .recv = macb_recv, |
| 1052 | .stop = macb_stop, |
| 1053 | .free_pkt = macb_free_pkt, |
| 1054 | .write_hwaddr = macb_write_hwaddr, |
| 1055 | }; |
| 1056 | |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 1057 | #ifdef CONFIG_CLK |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1058 | static int macb_enable_clk(struct udevice *dev) |
| 1059 | { |
| 1060 | struct macb_device *macb = dev_get_priv(dev); |
| 1061 | struct clk clk; |
| 1062 | ulong clk_rate; |
| 1063 | int ret; |
| 1064 | |
| 1065 | ret = clk_get_by_index(dev, 0, &clk); |
| 1066 | if (ret) |
| 1067 | return -EINVAL; |
| 1068 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 1069 | /* |
Anup Patel | 2e242f5 | 2019-02-25 08:14:36 +0000 | [diff] [blame] | 1070 | * If clock driver didn't support enable or disable then |
| 1071 | * we get -ENOSYS from clk_enable(). To handle this, we |
| 1072 | * don't fail for ret == -ENOSYS. |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 1073 | */ |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1074 | ret = clk_enable(&clk); |
Anup Patel | 2e242f5 | 2019-02-25 08:14:36 +0000 | [diff] [blame] | 1075 | if (ret && ret != -ENOSYS) |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1076 | return ret; |
| 1077 | |
| 1078 | clk_rate = clk_get_rate(&clk); |
| 1079 | if (!clk_rate) |
| 1080 | return -EINVAL; |
| 1081 | |
| 1082 | macb->pclk_rate = clk_rate; |
| 1083 | |
| 1084 | return 0; |
| 1085 | } |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 1086 | #endif |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1087 | |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1088 | static int macb_eth_probe(struct udevice *dev) |
| 1089 | { |
| 1090 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 1091 | struct macb_device *macb = dev_get_priv(dev); |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 1092 | const char *phy_mode; |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1093 | __maybe_unused int ret; |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 1094 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 1095 | phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode", |
| 1096 | NULL); |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 1097 | if (phy_mode) |
| 1098 | macb->phy_interface = phy_get_interface_by_name(phy_mode); |
| 1099 | if (macb->phy_interface == -1) { |
| 1100 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 1101 | return -EINVAL; |
| 1102 | } |
Wenyou Yang | a212b66 | 2016-05-17 13:11:35 +0800 | [diff] [blame] | 1103 | |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1104 | macb->regs = (void *)pdata->iobase; |
| 1105 | |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 1106 | #ifdef CONFIG_CLK |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1107 | ret = macb_enable_clk(dev); |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1108 | if (ret) |
| 1109 | return ret; |
Wenyou Yang | 3fd2b3a | 2017-02-14 16:24:40 +0800 | [diff] [blame] | 1110 | #endif |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1111 | |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1112 | _macb_eth_initialize(macb); |
Wenyou Yang | 577aa3b | 2016-11-02 10:06:56 +0800 | [diff] [blame] | 1113 | |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1114 | #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1115 | macb->bus = mdio_alloc(); |
| 1116 | if (!macb->bus) |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 1117 | return -ENOMEM; |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1118 | strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN); |
| 1119 | macb->bus->read = macb_miiphy_read; |
| 1120 | macb->bus->write = macb_miiphy_write; |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 1121 | |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1122 | ret = mdio_register(macb->bus); |
| 1123 | if (ret < 0) |
| 1124 | return ret; |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1125 | macb->bus = miiphy_get_dev_by_name(dev->name); |
| 1126 | #endif |
| 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1131 | static int macb_eth_remove(struct udevice *dev) |
| 1132 | { |
| 1133 | struct macb_device *macb = dev_get_priv(dev); |
| 1134 | |
| 1135 | #ifdef CONFIG_PHYLIB |
| 1136 | free(macb->phydev); |
| 1137 | #endif |
| 1138 | mdio_unregister(macb->bus); |
| 1139 | mdio_free(macb->bus); |
| 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 1144 | /** |
| 1145 | * macb_late_eth_ofdata_to_platdata |
| 1146 | * @dev: udevice struct |
| 1147 | * Returns 0 when operation success and negative errno number |
| 1148 | * when operation failed. |
| 1149 | */ |
| 1150 | int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev) |
| 1151 | { |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1155 | static int macb_eth_ofdata_to_platdata(struct udevice *dev) |
| 1156 | { |
| 1157 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 1158 | |
Ramon Fried | 9043c4e | 2018-12-27 19:58:42 +0200 | [diff] [blame] | 1159 | pdata->iobase = (phys_addr_t)dev_remap_addr(dev); |
| 1160 | if (!pdata->iobase) |
| 1161 | return -EINVAL; |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 1162 | |
| 1163 | return macb_late_eth_ofdata_to_platdata(dev); |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | static const struct udevice_id macb_eth_ids[] = { |
| 1167 | { .compatible = "cdns,macb" }, |
Wenyou Yang | 7546025 | 2017-04-14 14:36:05 +0800 | [diff] [blame] | 1168 | { .compatible = "cdns,at91sam9260-macb" }, |
| 1169 | { .compatible = "atmel,sama5d2-gem" }, |
| 1170 | { .compatible = "atmel,sama5d3-gem" }, |
| 1171 | { .compatible = "atmel,sama5d4-gem" }, |
Wilson Lee | 4bf5691 | 2017-08-22 20:25:07 -0700 | [diff] [blame] | 1172 | { .compatible = "cdns,zynq-gem" }, |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1173 | { } |
| 1174 | }; |
| 1175 | |
| 1176 | U_BOOT_DRIVER(eth_macb) = { |
| 1177 | .name = "eth_macb", |
| 1178 | .id = UCLASS_ETH, |
| 1179 | .of_match = macb_eth_ids, |
| 1180 | .ofdata_to_platdata = macb_eth_ofdata_to_platdata, |
| 1181 | .probe = macb_eth_probe, |
Wenyou Yang | 1870d4d | 2017-04-14 14:36:04 +0800 | [diff] [blame] | 1182 | .remove = macb_eth_remove, |
Simon Glass | f1dcc19 | 2016-05-05 07:28:11 -0600 | [diff] [blame] | 1183 | .ops = &macb_eth_ops, |
| 1184 | .priv_auto_alloc_size = sizeof(struct macb_device), |
| 1185 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 1186 | }; |
| 1187 | #endif |
Haavard Skinnemoen | 5c1fe1f | 2006-01-20 10:03:34 +0100 | [diff] [blame] | 1188 | |
Jon Loeliger | 07d38a1 | 2007-07-09 17:30:01 -0500 | [diff] [blame] | 1189 | #endif |