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