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