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