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