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