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