Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * drivers/net/ravb.c |
| 4 | * This file is driver for Renesas Ethernet AVB. |
| 5 | * |
| 6 | * Copyright (C) 2015-2017 Renesas Electronics Corporation |
| 7 | * |
| 8 | * Based on the SuperH Ethernet driver. |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Marek Vasut | c9746c6 | 2017-07-21 23:20:35 +0200 | [diff] [blame] | 12 | #include <clk.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 13 | #include <cpu_func.h> |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include <errno.h> |
| 16 | #include <miiphy.h> |
| 17 | #include <malloc.h> |
| 18 | #include <linux/mii.h> |
| 19 | #include <wait_bit.h> |
| 20 | #include <asm/io.h> |
Marek Vasut | 58f49d6 | 2017-09-15 21:11:15 +0200 | [diff] [blame] | 21 | #include <asm/gpio.h> |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 22 | |
| 23 | /* Registers */ |
| 24 | #define RAVB_REG_CCC 0x000 |
| 25 | #define RAVB_REG_DBAT 0x004 |
| 26 | #define RAVB_REG_CSR 0x00C |
| 27 | #define RAVB_REG_APSR 0x08C |
| 28 | #define RAVB_REG_RCR 0x090 |
| 29 | #define RAVB_REG_TGC 0x300 |
| 30 | #define RAVB_REG_TCCR 0x304 |
| 31 | #define RAVB_REG_RIC0 0x360 |
| 32 | #define RAVB_REG_RIC1 0x368 |
| 33 | #define RAVB_REG_RIC2 0x370 |
| 34 | #define RAVB_REG_TIC 0x378 |
| 35 | #define RAVB_REG_ECMR 0x500 |
| 36 | #define RAVB_REG_RFLR 0x508 |
| 37 | #define RAVB_REG_ECSIPR 0x518 |
| 38 | #define RAVB_REG_PIR 0x520 |
| 39 | #define RAVB_REG_GECMR 0x5b0 |
| 40 | #define RAVB_REG_MAHR 0x5c0 |
| 41 | #define RAVB_REG_MALR 0x5c8 |
| 42 | |
| 43 | #define CCC_OPC_CONFIG BIT(0) |
| 44 | #define CCC_OPC_OPERATION BIT(1) |
| 45 | #define CCC_BOC BIT(20) |
| 46 | |
| 47 | #define CSR_OPS 0x0000000F |
| 48 | #define CSR_OPS_CONFIG BIT(1) |
| 49 | |
Marek Vasut | 4185512 | 2019-04-13 11:42:34 +0200 | [diff] [blame] | 50 | #define APSR_TDM BIT(14) |
| 51 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 52 | #define TCCR_TSRQ0 BIT(0) |
| 53 | |
| 54 | #define RFLR_RFL_MIN 0x05EE |
| 55 | |
| 56 | #define PIR_MDI BIT(3) |
| 57 | #define PIR_MDO BIT(2) |
| 58 | #define PIR_MMD BIT(1) |
| 59 | #define PIR_MDC BIT(0) |
| 60 | |
| 61 | #define ECMR_TRCCM BIT(26) |
| 62 | #define ECMR_RZPF BIT(20) |
| 63 | #define ECMR_PFR BIT(18) |
| 64 | #define ECMR_RXF BIT(17) |
| 65 | #define ECMR_RE BIT(6) |
| 66 | #define ECMR_TE BIT(5) |
| 67 | #define ECMR_DM BIT(1) |
| 68 | #define ECMR_CHG_DM (ECMR_TRCCM | ECMR_RZPF | ECMR_PFR | ECMR_RXF) |
| 69 | |
| 70 | /* DMA Descriptors */ |
| 71 | #define RAVB_NUM_BASE_DESC 16 |
| 72 | #define RAVB_NUM_TX_DESC 8 |
| 73 | #define RAVB_NUM_RX_DESC 8 |
| 74 | |
| 75 | #define RAVB_TX_QUEUE_OFFSET 0 |
| 76 | #define RAVB_RX_QUEUE_OFFSET 4 |
| 77 | |
| 78 | #define RAVB_DESC_DT(n) ((n) << 28) |
| 79 | #define RAVB_DESC_DT_FSINGLE RAVB_DESC_DT(0x7) |
| 80 | #define RAVB_DESC_DT_LINKFIX RAVB_DESC_DT(0x9) |
| 81 | #define RAVB_DESC_DT_EOS RAVB_DESC_DT(0xa) |
| 82 | #define RAVB_DESC_DT_FEMPTY RAVB_DESC_DT(0xc) |
| 83 | #define RAVB_DESC_DT_EEMPTY RAVB_DESC_DT(0x3) |
| 84 | #define RAVB_DESC_DT_MASK RAVB_DESC_DT(0xf) |
| 85 | |
| 86 | #define RAVB_DESC_DS(n) (((n) & 0xfff) << 0) |
| 87 | #define RAVB_DESC_DS_MASK 0xfff |
| 88 | |
| 89 | #define RAVB_RX_DESC_MSC_MC BIT(23) |
| 90 | #define RAVB_RX_DESC_MSC_CEEF BIT(22) |
| 91 | #define RAVB_RX_DESC_MSC_CRL BIT(21) |
| 92 | #define RAVB_RX_DESC_MSC_FRE BIT(20) |
| 93 | #define RAVB_RX_DESC_MSC_RTLF BIT(19) |
| 94 | #define RAVB_RX_DESC_MSC_RTSF BIT(18) |
| 95 | #define RAVB_RX_DESC_MSC_RFE BIT(17) |
| 96 | #define RAVB_RX_DESC_MSC_CRC BIT(16) |
| 97 | #define RAVB_RX_DESC_MSC_MASK (0xff << 16) |
| 98 | |
| 99 | #define RAVB_RX_DESC_MSC_RX_ERR_MASK \ |
| 100 | (RAVB_RX_DESC_MSC_CRC | RAVB_RX_DESC_MSC_RFE | RAVB_RX_DESC_MSC_RTLF | \ |
| 101 | RAVB_RX_DESC_MSC_RTSF | RAVB_RX_DESC_MSC_CEEF) |
| 102 | |
| 103 | #define RAVB_TX_TIMEOUT_MS 1000 |
| 104 | |
| 105 | struct ravb_desc { |
| 106 | u32 ctrl; |
| 107 | u32 dptr; |
| 108 | }; |
| 109 | |
| 110 | struct ravb_rxdesc { |
| 111 | struct ravb_desc data; |
| 112 | struct ravb_desc link; |
| 113 | u8 __pad[48]; |
| 114 | u8 packet[PKTSIZE_ALIGN]; |
| 115 | }; |
| 116 | |
| 117 | struct ravb_priv { |
| 118 | struct ravb_desc base_desc[RAVB_NUM_BASE_DESC]; |
| 119 | struct ravb_desc tx_desc[RAVB_NUM_TX_DESC]; |
| 120 | struct ravb_rxdesc rx_desc[RAVB_NUM_RX_DESC]; |
| 121 | u32 rx_desc_idx; |
| 122 | u32 tx_desc_idx; |
| 123 | |
| 124 | struct phy_device *phydev; |
| 125 | struct mii_dev *bus; |
| 126 | void __iomem *iobase; |
Marek Vasut | c9746c6 | 2017-07-21 23:20:35 +0200 | [diff] [blame] | 127 | struct clk clk; |
Marek Vasut | 58f49d6 | 2017-09-15 21:11:15 +0200 | [diff] [blame] | 128 | struct gpio_desc reset_gpio; |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static inline void ravb_flush_dcache(u32 addr, u32 len) |
| 132 | { |
| 133 | flush_dcache_range(addr, addr + len); |
| 134 | } |
| 135 | |
| 136 | static inline void ravb_invalidate_dcache(u32 addr, u32 len) |
| 137 | { |
| 138 | u32 start = addr & ~((uintptr_t)ARCH_DMA_MINALIGN - 1); |
| 139 | u32 end = roundup(addr + len, ARCH_DMA_MINALIGN); |
| 140 | invalidate_dcache_range(start, end); |
| 141 | } |
| 142 | |
| 143 | static int ravb_send(struct udevice *dev, void *packet, int len) |
| 144 | { |
| 145 | struct ravb_priv *eth = dev_get_priv(dev); |
| 146 | struct ravb_desc *desc = ð->tx_desc[eth->tx_desc_idx]; |
| 147 | unsigned int start; |
| 148 | |
| 149 | /* Update TX descriptor */ |
| 150 | ravb_flush_dcache((uintptr_t)packet, len); |
| 151 | memset(desc, 0x0, sizeof(*desc)); |
| 152 | desc->ctrl = RAVB_DESC_DT_FSINGLE | RAVB_DESC_DS(len); |
| 153 | desc->dptr = (uintptr_t)packet; |
| 154 | ravb_flush_dcache((uintptr_t)desc, sizeof(*desc)); |
| 155 | |
| 156 | /* Restart the transmitter if disabled */ |
| 157 | if (!(readl(eth->iobase + RAVB_REG_TCCR) & TCCR_TSRQ0)) |
| 158 | setbits_le32(eth->iobase + RAVB_REG_TCCR, TCCR_TSRQ0); |
| 159 | |
| 160 | /* Wait until packet is transmitted */ |
| 161 | start = get_timer(0); |
| 162 | while (get_timer(start) < RAVB_TX_TIMEOUT_MS) { |
| 163 | ravb_invalidate_dcache((uintptr_t)desc, sizeof(*desc)); |
| 164 | if ((desc->ctrl & RAVB_DESC_DT_MASK) != RAVB_DESC_DT_FSINGLE) |
| 165 | break; |
| 166 | udelay(10); |
| 167 | }; |
| 168 | |
| 169 | if (get_timer(start) >= RAVB_TX_TIMEOUT_MS) |
| 170 | return -ETIMEDOUT; |
| 171 | |
| 172 | eth->tx_desc_idx = (eth->tx_desc_idx + 1) % (RAVB_NUM_TX_DESC - 1); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int ravb_recv(struct udevice *dev, int flags, uchar **packetp) |
| 177 | { |
| 178 | struct ravb_priv *eth = dev_get_priv(dev); |
| 179 | struct ravb_rxdesc *desc = ð->rx_desc[eth->rx_desc_idx]; |
| 180 | int len; |
| 181 | u8 *packet; |
| 182 | |
| 183 | /* Check if the rx descriptor is ready */ |
| 184 | ravb_invalidate_dcache((uintptr_t)desc, sizeof(*desc)); |
| 185 | if ((desc->data.ctrl & RAVB_DESC_DT_MASK) == RAVB_DESC_DT_FEMPTY) |
| 186 | return -EAGAIN; |
| 187 | |
| 188 | /* Check for errors */ |
| 189 | if (desc->data.ctrl & RAVB_RX_DESC_MSC_RX_ERR_MASK) { |
| 190 | desc->data.ctrl &= ~RAVB_RX_DESC_MSC_MASK; |
| 191 | return -EAGAIN; |
| 192 | } |
| 193 | |
| 194 | len = desc->data.ctrl & RAVB_DESC_DS_MASK; |
| 195 | packet = (u8 *)(uintptr_t)desc->data.dptr; |
| 196 | ravb_invalidate_dcache((uintptr_t)packet, len); |
| 197 | |
| 198 | *packetp = packet; |
| 199 | return len; |
| 200 | } |
| 201 | |
| 202 | static int ravb_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 203 | { |
| 204 | struct ravb_priv *eth = dev_get_priv(dev); |
| 205 | struct ravb_rxdesc *desc = ð->rx_desc[eth->rx_desc_idx]; |
| 206 | |
| 207 | /* Make current descriptor available again */ |
| 208 | desc->data.ctrl = RAVB_DESC_DT_FEMPTY | RAVB_DESC_DS(PKTSIZE_ALIGN); |
| 209 | ravb_flush_dcache((uintptr_t)desc, sizeof(*desc)); |
| 210 | |
| 211 | /* Point to the next descriptor */ |
| 212 | eth->rx_desc_idx = (eth->rx_desc_idx + 1) % RAVB_NUM_RX_DESC; |
| 213 | desc = ð->rx_desc[eth->rx_desc_idx]; |
| 214 | ravb_invalidate_dcache((uintptr_t)desc, sizeof(*desc)); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int ravb_reset(struct udevice *dev) |
| 220 | { |
| 221 | struct ravb_priv *eth = dev_get_priv(dev); |
| 222 | |
| 223 | /* Set config mode */ |
| 224 | writel(CCC_OPC_CONFIG, eth->iobase + RAVB_REG_CCC); |
| 225 | |
| 226 | /* Check the operating mode is changed to the config mode. */ |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 227 | return wait_for_bit_le32(eth->iobase + RAVB_REG_CSR, |
| 228 | CSR_OPS_CONFIG, true, 100, true); |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void ravb_base_desc_init(struct ravb_priv *eth) |
| 232 | { |
| 233 | const u32 desc_size = RAVB_NUM_BASE_DESC * sizeof(struct ravb_desc); |
| 234 | int i; |
| 235 | |
| 236 | /* Initialize all descriptors */ |
| 237 | memset(eth->base_desc, 0x0, desc_size); |
| 238 | |
| 239 | for (i = 0; i < RAVB_NUM_BASE_DESC; i++) |
| 240 | eth->base_desc[i].ctrl = RAVB_DESC_DT_EOS; |
| 241 | |
| 242 | ravb_flush_dcache((uintptr_t)eth->base_desc, desc_size); |
| 243 | |
| 244 | /* Register the descriptor base address table */ |
| 245 | writel((uintptr_t)eth->base_desc, eth->iobase + RAVB_REG_DBAT); |
| 246 | } |
| 247 | |
| 248 | static void ravb_tx_desc_init(struct ravb_priv *eth) |
| 249 | { |
| 250 | const u32 desc_size = RAVB_NUM_TX_DESC * sizeof(struct ravb_desc); |
| 251 | int i; |
| 252 | |
| 253 | /* Initialize all descriptors */ |
| 254 | memset(eth->tx_desc, 0x0, desc_size); |
| 255 | eth->tx_desc_idx = 0; |
| 256 | |
| 257 | for (i = 0; i < RAVB_NUM_TX_DESC; i++) |
| 258 | eth->tx_desc[i].ctrl = RAVB_DESC_DT_EEMPTY; |
| 259 | |
| 260 | /* Mark the end of the descriptors */ |
| 261 | eth->tx_desc[RAVB_NUM_TX_DESC - 1].ctrl = RAVB_DESC_DT_LINKFIX; |
| 262 | eth->tx_desc[RAVB_NUM_TX_DESC - 1].dptr = (uintptr_t)eth->tx_desc; |
| 263 | ravb_flush_dcache((uintptr_t)eth->tx_desc, desc_size); |
| 264 | |
| 265 | /* Point the controller to the TX descriptor list. */ |
| 266 | eth->base_desc[RAVB_TX_QUEUE_OFFSET].ctrl = RAVB_DESC_DT_LINKFIX; |
| 267 | eth->base_desc[RAVB_TX_QUEUE_OFFSET].dptr = (uintptr_t)eth->tx_desc; |
| 268 | ravb_flush_dcache((uintptr_t)ð->base_desc[RAVB_TX_QUEUE_OFFSET], |
| 269 | sizeof(struct ravb_desc)); |
| 270 | } |
| 271 | |
| 272 | static void ravb_rx_desc_init(struct ravb_priv *eth) |
| 273 | { |
| 274 | const u32 desc_size = RAVB_NUM_RX_DESC * sizeof(struct ravb_rxdesc); |
| 275 | int i; |
| 276 | |
| 277 | /* Initialize all descriptors */ |
| 278 | memset(eth->rx_desc, 0x0, desc_size); |
| 279 | eth->rx_desc_idx = 0; |
| 280 | |
| 281 | for (i = 0; i < RAVB_NUM_RX_DESC; i++) { |
| 282 | eth->rx_desc[i].data.ctrl = RAVB_DESC_DT_EEMPTY | |
| 283 | RAVB_DESC_DS(PKTSIZE_ALIGN); |
| 284 | eth->rx_desc[i].data.dptr = (uintptr_t)eth->rx_desc[i].packet; |
| 285 | |
| 286 | eth->rx_desc[i].link.ctrl = RAVB_DESC_DT_LINKFIX; |
| 287 | eth->rx_desc[i].link.dptr = (uintptr_t)ð->rx_desc[i + 1]; |
| 288 | } |
| 289 | |
| 290 | /* Mark the end of the descriptors */ |
| 291 | eth->rx_desc[RAVB_NUM_RX_DESC - 1].link.ctrl = RAVB_DESC_DT_LINKFIX; |
| 292 | eth->rx_desc[RAVB_NUM_RX_DESC - 1].link.dptr = (uintptr_t)eth->rx_desc; |
| 293 | ravb_flush_dcache((uintptr_t)eth->rx_desc, desc_size); |
| 294 | |
| 295 | /* Point the controller to the rx descriptor list */ |
| 296 | eth->base_desc[RAVB_RX_QUEUE_OFFSET].ctrl = RAVB_DESC_DT_LINKFIX; |
| 297 | eth->base_desc[RAVB_RX_QUEUE_OFFSET].dptr = (uintptr_t)eth->rx_desc; |
| 298 | ravb_flush_dcache((uintptr_t)ð->base_desc[RAVB_RX_QUEUE_OFFSET], |
| 299 | sizeof(struct ravb_desc)); |
| 300 | } |
| 301 | |
| 302 | static int ravb_phy_config(struct udevice *dev) |
| 303 | { |
| 304 | struct ravb_priv *eth = dev_get_priv(dev); |
| 305 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 306 | struct phy_device *phydev; |
Marek Vasut | fbde41d | 2017-07-21 23:20:34 +0200 | [diff] [blame] | 307 | int mask = 0xffffffff, reg; |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 308 | |
Marek Vasut | 58f49d6 | 2017-09-15 21:11:15 +0200 | [diff] [blame] | 309 | if (dm_gpio_is_valid(ð->reset_gpio)) { |
| 310 | dm_gpio_set_value(ð->reset_gpio, 1); |
| 311 | mdelay(20); |
| 312 | dm_gpio_set_value(ð->reset_gpio, 0); |
| 313 | mdelay(1); |
| 314 | } |
| 315 | |
Marek Vasut | fbde41d | 2017-07-21 23:20:34 +0200 | [diff] [blame] | 316 | phydev = phy_find_by_mask(eth->bus, mask, pdata->phy_interface); |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 317 | if (!phydev) |
| 318 | return -ENODEV; |
| 319 | |
Marek Vasut | fbde41d | 2017-07-21 23:20:34 +0200 | [diff] [blame] | 320 | phy_connect_dev(phydev, dev); |
| 321 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 322 | eth->phydev = phydev; |
| 323 | |
Marek Vasut | 882294d | 2018-06-18 05:44:53 +0200 | [diff] [blame] | 324 | phydev->supported &= SUPPORTED_100baseT_Full | |
| 325 | SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | |
| 326 | SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_Pause | |
| 327 | SUPPORTED_Asym_Pause; |
| 328 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 329 | if (pdata->max_speed != 1000) { |
Marek Vasut | 882294d | 2018-06-18 05:44:53 +0200 | [diff] [blame] | 330 | phydev->supported &= ~SUPPORTED_1000baseT_Full; |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 331 | reg = phy_read(phydev, -1, MII_CTRL1000); |
| 332 | reg &= ~(BIT(9) | BIT(8)); |
| 333 | phy_write(phydev, -1, MII_CTRL1000, reg); |
| 334 | } |
| 335 | |
| 336 | phy_config(phydev); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | /* Set Mac address */ |
| 342 | static int ravb_write_hwaddr(struct udevice *dev) |
| 343 | { |
| 344 | struct ravb_priv *eth = dev_get_priv(dev); |
| 345 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 346 | unsigned char *mac = pdata->enetaddr; |
| 347 | |
| 348 | writel((mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3], |
| 349 | eth->iobase + RAVB_REG_MAHR); |
| 350 | |
| 351 | writel((mac[4] << 8) | mac[5], eth->iobase + RAVB_REG_MALR); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* E-MAC init function */ |
| 357 | static int ravb_mac_init(struct ravb_priv *eth) |
| 358 | { |
| 359 | /* Disable MAC Interrupt */ |
| 360 | writel(0, eth->iobase + RAVB_REG_ECSIPR); |
| 361 | |
| 362 | /* Recv frame limit set register */ |
| 363 | writel(RFLR_RFL_MIN, eth->iobase + RAVB_REG_RFLR); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* AVB-DMAC init function */ |
| 369 | static int ravb_dmac_init(struct udevice *dev) |
| 370 | { |
| 371 | struct ravb_priv *eth = dev_get_priv(dev); |
| 372 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 373 | int ret = 0; |
| 374 | |
| 375 | /* Set CONFIG mode */ |
| 376 | ret = ravb_reset(dev); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | |
| 380 | /* Disable all interrupts */ |
| 381 | writel(0, eth->iobase + RAVB_REG_RIC0); |
| 382 | writel(0, eth->iobase + RAVB_REG_RIC1); |
| 383 | writel(0, eth->iobase + RAVB_REG_RIC2); |
| 384 | writel(0, eth->iobase + RAVB_REG_TIC); |
| 385 | |
| 386 | /* Set little endian */ |
| 387 | clrbits_le32(eth->iobase + RAVB_REG_CCC, CCC_BOC); |
| 388 | |
| 389 | /* AVB rx set */ |
| 390 | writel(0x18000001, eth->iobase + RAVB_REG_RCR); |
| 391 | |
| 392 | /* FIFO size set */ |
| 393 | writel(0x00222210, eth->iobase + RAVB_REG_TGC); |
| 394 | |
Marek Vasut | 4185512 | 2019-04-13 11:42:34 +0200 | [diff] [blame] | 395 | /* Delay CLK: 2ns (not applicable on R-Car E3/D3) */ |
| 396 | if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) || |
| 397 | (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77995)) |
| 398 | return 0; |
| 399 | |
| 400 | if ((pdata->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) || |
| 401 | (pdata->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)) |
| 402 | writel(APSR_TDM, eth->iobase + RAVB_REG_APSR); |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int ravb_config(struct udevice *dev) |
| 408 | { |
| 409 | struct ravb_priv *eth = dev_get_priv(dev); |
Marek Vasut | 3364d7a | 2018-02-13 17:21:15 +0100 | [diff] [blame] | 410 | struct phy_device *phy = eth->phydev; |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 411 | u32 mask = ECMR_CHG_DM | ECMR_RE | ECMR_TE; |
| 412 | int ret; |
| 413 | |
| 414 | /* Configure AVB-DMAC register */ |
| 415 | ravb_dmac_init(dev); |
| 416 | |
| 417 | /* Configure E-MAC registers */ |
| 418 | ravb_mac_init(eth); |
| 419 | ravb_write_hwaddr(dev); |
| 420 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 421 | ret = phy_startup(phy); |
| 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | /* Set the transfer speed */ |
| 426 | if (phy->speed == 100) |
| 427 | writel(0, eth->iobase + RAVB_REG_GECMR); |
| 428 | else if (phy->speed == 1000) |
| 429 | writel(1, eth->iobase + RAVB_REG_GECMR); |
| 430 | |
| 431 | /* Check if full duplex mode is supported by the phy */ |
| 432 | if (phy->duplex) |
| 433 | mask |= ECMR_DM; |
| 434 | |
| 435 | writel(mask, eth->iobase + RAVB_REG_ECMR); |
| 436 | |
| 437 | phy->drv->writeext(phy, -1, 0x02, 0x08, (0x0f << 5) | 0x19); |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
Marek Vasut | 7457ce9 | 2018-01-19 23:58:32 +0100 | [diff] [blame] | 442 | static int ravb_start(struct udevice *dev) |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 443 | { |
| 444 | struct ravb_priv *eth = dev_get_priv(dev); |
| 445 | int ret; |
| 446 | |
Marek Vasut | c9746c6 | 2017-07-21 23:20:35 +0200 | [diff] [blame] | 447 | ret = ravb_reset(dev); |
| 448 | if (ret) |
Marek Vasut | 597e007 | 2018-06-18 09:35:45 +0200 | [diff] [blame] | 449 | return ret; |
Marek Vasut | c9746c6 | 2017-07-21 23:20:35 +0200 | [diff] [blame] | 450 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 451 | ravb_base_desc_init(eth); |
| 452 | ravb_tx_desc_init(eth); |
| 453 | ravb_rx_desc_init(eth); |
| 454 | |
| 455 | ret = ravb_config(dev); |
| 456 | if (ret) |
Marek Vasut | 597e007 | 2018-06-18 09:35:45 +0200 | [diff] [blame] | 457 | return ret; |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 458 | |
| 459 | /* Setting the control will start the AVB-DMAC process. */ |
| 460 | writel(CCC_OPC_OPERATION, eth->iobase + RAVB_REG_CCC); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static void ravb_stop(struct udevice *dev) |
| 466 | { |
Marek Vasut | c9746c6 | 2017-07-21 23:20:35 +0200 | [diff] [blame] | 467 | struct ravb_priv *eth = dev_get_priv(dev); |
| 468 | |
Marek Vasut | 3364d7a | 2018-02-13 17:21:15 +0100 | [diff] [blame] | 469 | phy_shutdown(eth->phydev); |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 470 | ravb_reset(dev); |
| 471 | } |
| 472 | |
| 473 | static int ravb_probe(struct udevice *dev) |
| 474 | { |
| 475 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 476 | struct ravb_priv *eth = dev_get_priv(dev); |
Marek Vasut | d422d5c | 2018-06-18 04:02:15 +0200 | [diff] [blame] | 477 | struct ofnode_phandle_args phandle_args; |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 478 | struct mii_dev *mdiodev; |
| 479 | void __iomem *iobase; |
| 480 | int ret; |
| 481 | |
| 482 | iobase = map_physmem(pdata->iobase, 0x1000, MAP_NOCACHE); |
| 483 | eth->iobase = iobase; |
| 484 | |
Marek Vasut | c9746c6 | 2017-07-21 23:20:35 +0200 | [diff] [blame] | 485 | ret = clk_get_by_index(dev, 0, ð->clk); |
| 486 | if (ret < 0) |
| 487 | goto err_mdio_alloc; |
| 488 | |
Marek Vasut | d422d5c | 2018-06-18 04:02:15 +0200 | [diff] [blame] | 489 | ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, &phandle_args); |
| 490 | if (!ret) { |
| 491 | gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0, |
| 492 | ð->reset_gpio, GPIOD_IS_OUT); |
| 493 | } |
| 494 | |
| 495 | if (!dm_gpio_is_valid(ð->reset_gpio)) { |
| 496 | gpio_request_by_name(dev, "reset-gpios", 0, ð->reset_gpio, |
| 497 | GPIOD_IS_OUT); |
| 498 | } |
Marek Vasut | 58f49d6 | 2017-09-15 21:11:15 +0200 | [diff] [blame] | 499 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 500 | mdiodev = mdio_alloc(); |
| 501 | if (!mdiodev) { |
| 502 | ret = -ENOMEM; |
| 503 | goto err_mdio_alloc; |
| 504 | } |
| 505 | |
| 506 | mdiodev->read = bb_miiphy_read; |
| 507 | mdiodev->write = bb_miiphy_write; |
| 508 | bb_miiphy_buses[0].priv = eth; |
| 509 | snprintf(mdiodev->name, sizeof(mdiodev->name), dev->name); |
| 510 | |
| 511 | ret = mdio_register(mdiodev); |
| 512 | if (ret < 0) |
| 513 | goto err_mdio_register; |
| 514 | |
| 515 | eth->bus = miiphy_get_dev_by_name(dev->name); |
| 516 | |
Marek Vasut | 3364d7a | 2018-02-13 17:21:15 +0100 | [diff] [blame] | 517 | /* Bring up PHY */ |
| 518 | ret = clk_enable(ð->clk); |
| 519 | if (ret) |
| 520 | goto err_mdio_register; |
| 521 | |
| 522 | ret = ravb_reset(dev); |
| 523 | if (ret) |
| 524 | goto err_mdio_reset; |
| 525 | |
| 526 | ret = ravb_phy_config(dev); |
| 527 | if (ret) |
| 528 | goto err_mdio_reset; |
| 529 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 530 | return 0; |
| 531 | |
Marek Vasut | 3364d7a | 2018-02-13 17:21:15 +0100 | [diff] [blame] | 532 | err_mdio_reset: |
| 533 | clk_disable(ð->clk); |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 534 | err_mdio_register: |
| 535 | mdio_free(mdiodev); |
| 536 | err_mdio_alloc: |
| 537 | unmap_physmem(eth->iobase, MAP_NOCACHE); |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | static int ravb_remove(struct udevice *dev) |
| 542 | { |
| 543 | struct ravb_priv *eth = dev_get_priv(dev); |
| 544 | |
Marek Vasut | 3364d7a | 2018-02-13 17:21:15 +0100 | [diff] [blame] | 545 | clk_disable(ð->clk); |
| 546 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 547 | free(eth->phydev); |
| 548 | mdio_unregister(eth->bus); |
| 549 | mdio_free(eth->bus); |
Marek Vasut | eb7c782 | 2017-11-09 22:49:19 +0100 | [diff] [blame] | 550 | if (dm_gpio_is_valid(ð->reset_gpio)) |
| 551 | dm_gpio_free(dev, ð->reset_gpio); |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 552 | unmap_physmem(eth->iobase, MAP_NOCACHE); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | int ravb_bb_init(struct bb_miiphy_bus *bus) |
| 558 | { |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | int ravb_bb_mdio_active(struct bb_miiphy_bus *bus) |
| 563 | { |
| 564 | struct ravb_priv *eth = bus->priv; |
| 565 | |
| 566 | setbits_le32(eth->iobase + RAVB_REG_PIR, PIR_MMD); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | int ravb_bb_mdio_tristate(struct bb_miiphy_bus *bus) |
| 572 | { |
| 573 | struct ravb_priv *eth = bus->priv; |
| 574 | |
| 575 | clrbits_le32(eth->iobase + RAVB_REG_PIR, PIR_MMD); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | int ravb_bb_set_mdio(struct bb_miiphy_bus *bus, int v) |
| 581 | { |
| 582 | struct ravb_priv *eth = bus->priv; |
| 583 | |
| 584 | if (v) |
| 585 | setbits_le32(eth->iobase + RAVB_REG_PIR, PIR_MDO); |
| 586 | else |
| 587 | clrbits_le32(eth->iobase + RAVB_REG_PIR, PIR_MDO); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | int ravb_bb_get_mdio(struct bb_miiphy_bus *bus, int *v) |
| 593 | { |
| 594 | struct ravb_priv *eth = bus->priv; |
| 595 | |
| 596 | *v = (readl(eth->iobase + RAVB_REG_PIR) & PIR_MDI) >> 3; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | int ravb_bb_set_mdc(struct bb_miiphy_bus *bus, int v) |
| 602 | { |
| 603 | struct ravb_priv *eth = bus->priv; |
| 604 | |
| 605 | if (v) |
| 606 | setbits_le32(eth->iobase + RAVB_REG_PIR, PIR_MDC); |
| 607 | else |
| 608 | clrbits_le32(eth->iobase + RAVB_REG_PIR, PIR_MDC); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | int ravb_bb_delay(struct bb_miiphy_bus *bus) |
| 614 | { |
| 615 | udelay(10); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | struct bb_miiphy_bus bb_miiphy_buses[] = { |
| 621 | { |
| 622 | .name = "ravb", |
| 623 | .init = ravb_bb_init, |
| 624 | .mdio_active = ravb_bb_mdio_active, |
| 625 | .mdio_tristate = ravb_bb_mdio_tristate, |
| 626 | .set_mdio = ravb_bb_set_mdio, |
| 627 | .get_mdio = ravb_bb_get_mdio, |
| 628 | .set_mdc = ravb_bb_set_mdc, |
| 629 | .delay = ravb_bb_delay, |
| 630 | }, |
| 631 | }; |
| 632 | int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses); |
| 633 | |
| 634 | static const struct eth_ops ravb_ops = { |
| 635 | .start = ravb_start, |
| 636 | .send = ravb_send, |
| 637 | .recv = ravb_recv, |
| 638 | .free_pkt = ravb_free_pkt, |
| 639 | .stop = ravb_stop, |
| 640 | .write_hwaddr = ravb_write_hwaddr, |
| 641 | }; |
| 642 | |
Marek Vasut | 934fd3b | 2017-07-21 23:20:33 +0200 | [diff] [blame] | 643 | int ravb_ofdata_to_platdata(struct udevice *dev) |
| 644 | { |
| 645 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 646 | const char *phy_mode; |
| 647 | const fdt32_t *cell; |
| 648 | int ret = 0; |
| 649 | |
| 650 | pdata->iobase = devfdt_get_addr(dev); |
| 651 | pdata->phy_interface = -1; |
| 652 | phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode", |
| 653 | NULL); |
| 654 | if (phy_mode) |
| 655 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 656 | if (pdata->phy_interface == -1) { |
| 657 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | |
| 661 | pdata->max_speed = 1000; |
| 662 | cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL); |
| 663 | if (cell) |
| 664 | pdata->max_speed = fdt32_to_cpu(*cell); |
| 665 | |
| 666 | sprintf(bb_miiphy_buses[0].name, dev->name); |
| 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | static const struct udevice_id ravb_ids[] = { |
| 672 | { .compatible = "renesas,etheravb-r8a7795" }, |
| 673 | { .compatible = "renesas,etheravb-r8a7796" }, |
Marek Vasut | 070782f | 2018-02-26 10:35:15 +0100 | [diff] [blame] | 674 | { .compatible = "renesas,etheravb-r8a77965" }, |
Marek Vasut | 2d3ec64 | 2017-10-21 11:33:17 +0200 | [diff] [blame] | 675 | { .compatible = "renesas,etheravb-r8a77970" }, |
Marek Vasut | 1c44017 | 2018-04-26 13:20:10 +0200 | [diff] [blame] | 676 | { .compatible = "renesas,etheravb-r8a77990" }, |
Marek Vasut | 42832fe | 2017-10-21 11:35:49 +0200 | [diff] [blame] | 677 | { .compatible = "renesas,etheravb-r8a77995" }, |
Marek Vasut | 934fd3b | 2017-07-21 23:20:33 +0200 | [diff] [blame] | 678 | { .compatible = "renesas,etheravb-rcar-gen3" }, |
| 679 | { } |
| 680 | }; |
| 681 | |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 682 | U_BOOT_DRIVER(eth_ravb) = { |
| 683 | .name = "ravb", |
| 684 | .id = UCLASS_ETH, |
Marek Vasut | 934fd3b | 2017-07-21 23:20:33 +0200 | [diff] [blame] | 685 | .of_match = ravb_ids, |
| 686 | .ofdata_to_platdata = ravb_ofdata_to_platdata, |
Marek Vasut | 17714cb | 2017-05-13 15:54:28 +0200 | [diff] [blame] | 687 | .probe = ravb_probe, |
| 688 | .remove = ravb_remove, |
| 689 | .ops = &ravb_ops, |
| 690 | .priv_auto_alloc_size = sizeof(struct ravb_priv), |
| 691 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 692 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 693 | }; |