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