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