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