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