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