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