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