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