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