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