blob: 9a1b2eb712451e8aa487d7b21bba40a26b321f52 [file] [log] [blame]
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +09001// SPDX-License-Identifier: GPL-2.0+
2/**
3 * sni_ave.c - Socionext UniPhier AVE ethernet driver
4 * Copyright 2016-2018 Socionext inc.
5 */
6
7#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +09009#include <dm.h>
10#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +090013#include <miiphy.h>
14#include <net.h>
15#include <regmap.h>
16#include <reset.h>
17#include <syscon.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <linux/err.h>
23#include <linux/io.h>
24#include <linux/iopoll.h>
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +090025
26#define AVE_GRST_DELAY_MSEC 40
27#define AVE_MIN_XMITSIZE 60
28#define AVE_SEND_TIMEOUT_COUNT 1000
29#define AVE_MDIO_TIMEOUT_USEC 10000
30#define AVE_HALT_TIMEOUT_USEC 10000
31
32/* General Register Group */
33#define AVE_IDR 0x000 /* ID */
34#define AVE_VR 0x004 /* Version */
35#define AVE_GRR 0x008 /* Global Reset */
36#define AVE_CFGR 0x00c /* Configuration */
37
38/* Interrupt Register Group */
39#define AVE_GIMR 0x100 /* Global Interrupt Mask */
40#define AVE_GISR 0x104 /* Global Interrupt Status */
41
42/* MAC Register Group */
43#define AVE_TXCR 0x200 /* TX Setup */
44#define AVE_RXCR 0x204 /* RX Setup */
45#define AVE_RXMAC1R 0x208 /* MAC address (lower) */
46#define AVE_RXMAC2R 0x20c /* MAC address (upper) */
47#define AVE_MDIOCTR 0x214 /* MDIO Control */
48#define AVE_MDIOAR 0x218 /* MDIO Address */
49#define AVE_MDIOWDR 0x21c /* MDIO Data */
50#define AVE_MDIOSR 0x220 /* MDIO Status */
51#define AVE_MDIORDR 0x224 /* MDIO Rd Data */
52
53/* Descriptor Control Register Group */
54#define AVE_DESCC 0x300 /* Descriptor Control */
55#define AVE_TXDC 0x304 /* TX Descriptor Configuration */
56#define AVE_RXDC 0x308 /* RX Descriptor Ring0 Configuration */
57#define AVE_IIRQC 0x34c /* Interval IRQ Control */
58
59/* 64bit descriptor memory */
60#define AVE_DESC_SIZE_64 12 /* Descriptor Size */
61#define AVE_TXDM_64 0x1000 /* Tx Descriptor Memory */
62#define AVE_RXDM_64 0x1c00 /* Rx Descriptor Memory */
63
64/* 32bit descriptor memory */
65#define AVE_DESC_SIZE_32 8 /* Descriptor Size */
66#define AVE_TXDM_32 0x1000 /* Tx Descriptor Memory */
67#define AVE_RXDM_32 0x1800 /* Rx Descriptor Memory */
68
69/* RMII Bridge Register Group */
70#define AVE_RSTCTRL 0x8028 /* Reset control */
71#define AVE_RSTCTRL_RMIIRST BIT(16)
72#define AVE_LINKSEL 0x8034 /* Link speed setting */
73#define AVE_LINKSEL_100M BIT(0)
74
75/* AVE_GRR */
76#define AVE_GRR_PHYRST BIT(4) /* Reset external PHY */
77#define AVE_GRR_GRST BIT(0) /* Reset all MAC */
78
79/* AVE_CFGR */
80#define AVE_CFGR_MII BIT(27) /* Func mode (1:MII/RMII, 0:RGMII) */
81
82/* AVE_GISR (common with GIMR) */
83#define AVE_GIMR_CLR 0
84#define AVE_GISR_CLR GENMASK(31, 0)
85
86/* AVE_TXCR */
87#define AVE_TXCR_FLOCTR BIT(18) /* Flow control */
88#define AVE_TXCR_TXSPD_1G BIT(17)
89#define AVE_TXCR_TXSPD_100 BIT(16)
90
91/* AVE_RXCR */
92#define AVE_RXCR_RXEN BIT(30) /* Rx enable */
93#define AVE_RXCR_FDUPEN BIT(22) /* Interface mode */
94#define AVE_RXCR_FLOCTR BIT(21) /* Flow control */
95
96/* AVE_MDIOCTR */
97#define AVE_MDIOCTR_RREQ BIT(3) /* Read request */
98#define AVE_MDIOCTR_WREQ BIT(2) /* Write request */
99
100/* AVE_MDIOSR */
101#define AVE_MDIOSR_STS BIT(0) /* access status */
102
103/* AVE_DESCC */
104#define AVE_DESCC_RXDSTPSTS BIT(20)
105#define AVE_DESCC_RD0 BIT(8) /* Enable Rx descriptor Ring0 */
106#define AVE_DESCC_RXDSTP BIT(4) /* Pause Rx descriptor */
107#define AVE_DESCC_TD BIT(0) /* Enable Tx descriptor */
108
109/* AVE_TXDC/RXDC */
110#define AVE_DESC_SIZE(priv, num) \
111 ((num) * ((priv)->data->is_desc_64bit ? AVE_DESC_SIZE_64 : \
112 AVE_DESC_SIZE_32))
113
114/* Command status for descriptor */
115#define AVE_STS_OWN BIT(31) /* Descriptor ownership */
116#define AVE_STS_OK BIT(27) /* Normal transmit */
117#define AVE_STS_1ST BIT(26) /* Head of buffer chain */
118#define AVE_STS_LAST BIT(25) /* Tail of buffer chain */
119#define AVE_STS_PKTLEN_TX_MASK GENMASK(15, 0)
120#define AVE_STS_PKTLEN_RX_MASK GENMASK(10, 0)
121
122#define AVE_DESC_OFS_CMDSTS 0
123#define AVE_DESC_OFS_ADDRL 4
124#define AVE_DESC_OFS_ADDRU 8
125
126/* Parameter for ethernet frame */
127#define AVE_RXCR_MTU 1518
128
129/* SG */
130#define SG_ETPINMODE 0x540
131#define SG_ETPINMODE_EXTPHY BIT(1) /* for LD11 */
132#define SG_ETPINMODE_RMII(ins) BIT(ins)
133
134#define AVE_MAX_CLKS 4
135#define AVE_MAX_RSTS 2
136
137enum desc_id {
138 AVE_DESCID_TX,
139 AVE_DESCID_RX,
140};
141
142struct ave_private {
143 phys_addr_t iobase;
144 unsigned int nclks;
145 struct clk clk[AVE_MAX_CLKS];
146 unsigned int nrsts;
147 struct reset_ctl rst[AVE_MAX_RSTS];
148 struct regmap *regmap;
149 unsigned int regmap_arg;
150
151 struct mii_dev *bus;
152 struct phy_device *phydev;
153 int phy_mode;
154 int max_speed;
155
156 int rx_pos;
157 int rx_siz;
158 int rx_off;
159 int tx_num;
160
161 u8 tx_adj_packetbuf[PKTSIZE_ALIGN + PKTALIGN];
162 void *tx_adj_buf;
163
164 const struct ave_soc_data *data;
165};
166
167struct ave_soc_data {
168 bool is_desc_64bit;
169 const char *clock_names[AVE_MAX_CLKS];
170 const char *reset_names[AVE_MAX_RSTS];
171 int (*get_pinmode)(struct ave_private *priv);
172};
173
174static u32 ave_desc_read(struct ave_private *priv, enum desc_id id, int entry,
175 int offset)
176{
177 int desc_size;
178 u32 addr;
179
180 if (priv->data->is_desc_64bit) {
181 desc_size = AVE_DESC_SIZE_64;
182 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_64 : AVE_RXDM_64;
183 } else {
184 desc_size = AVE_DESC_SIZE_32;
185 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_32 : AVE_RXDM_32;
186 }
187
188 addr += entry * desc_size + offset;
189
190 return readl(priv->iobase + addr);
191}
192
193static u32 ave_desc_read_cmdsts(struct ave_private *priv, enum desc_id id,
194 int entry)
195{
196 return ave_desc_read(priv, id, entry, AVE_DESC_OFS_CMDSTS);
197}
198
199static void ave_desc_write(struct ave_private *priv, enum desc_id id,
200 int entry, int offset, u32 val)
201{
202 int desc_size;
203 u32 addr;
204
205 if (priv->data->is_desc_64bit) {
206 desc_size = AVE_DESC_SIZE_64;
207 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_64 : AVE_RXDM_64;
208 } else {
209 desc_size = AVE_DESC_SIZE_32;
210 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_32 : AVE_RXDM_32;
211 }
212
213 addr += entry * desc_size + offset;
214 writel(val, priv->iobase + addr);
215}
216
217static void ave_desc_write_cmdsts(struct ave_private *priv, enum desc_id id,
218 int entry, u32 val)
219{
220 ave_desc_write(priv, id, entry, AVE_DESC_OFS_CMDSTS, val);
221}
222
223static void ave_desc_write_addr(struct ave_private *priv, enum desc_id id,
224 int entry, uintptr_t paddr)
225{
226 ave_desc_write(priv, id, entry,
227 AVE_DESC_OFS_ADDRL, lower_32_bits(paddr));
228 if (priv->data->is_desc_64bit)
229 ave_desc_write(priv, id, entry,
230 AVE_DESC_OFS_ADDRU, upper_32_bits(paddr));
231}
232
233static void ave_cache_invalidate(uintptr_t vaddr, int len)
234{
235 invalidate_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
236 roundup(vaddr + len, ARCH_DMA_MINALIGN));
237}
238
239static void ave_cache_flush(uintptr_t vaddr, int len)
240{
241 flush_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
242 roundup(vaddr + len, ARCH_DMA_MINALIGN));
243}
244
245static int ave_mdiobus_read(struct mii_dev *bus,
246 int phyid, int devad, int regnum)
247{
248 struct ave_private *priv = bus->priv;
249 u32 mdioctl, mdiosr;
250 int ret;
251
252 /* write address */
253 writel((phyid << 8) | regnum, priv->iobase + AVE_MDIOAR);
254
255 /* read request */
256 mdioctl = readl(priv->iobase + AVE_MDIOCTR);
257 writel(mdioctl | AVE_MDIOCTR_RREQ, priv->iobase + AVE_MDIOCTR);
258
259 ret = readl_poll_timeout(priv->iobase + AVE_MDIOSR, mdiosr,
260 !(mdiosr & AVE_MDIOSR_STS),
261 AVE_MDIO_TIMEOUT_USEC);
262 if (ret) {
263 pr_err("%s: failed to read from mdio (phy:%d reg:%x)\n",
264 priv->phydev->dev->name, phyid, regnum);
265 return ret;
266 }
267
268 return readl(priv->iobase + AVE_MDIORDR) & GENMASK(15, 0);
269}
270
271static int ave_mdiobus_write(struct mii_dev *bus,
272 int phyid, int devad, int regnum, u16 val)
273{
274 struct ave_private *priv = bus->priv;
275 u32 mdioctl, mdiosr;
276 int ret;
277
278 /* write address */
279 writel((phyid << 8) | regnum, priv->iobase + AVE_MDIOAR);
280
281 /* write data */
282 writel(val, priv->iobase + AVE_MDIOWDR);
283
284 /* write request */
285 mdioctl = readl(priv->iobase + AVE_MDIOCTR);
286 writel((mdioctl | AVE_MDIOCTR_WREQ) & ~AVE_MDIOCTR_RREQ,
287 priv->iobase + AVE_MDIOCTR);
288
289 ret = readl_poll_timeout(priv->iobase + AVE_MDIOSR, mdiosr,
290 !(mdiosr & AVE_MDIOSR_STS),
291 AVE_MDIO_TIMEOUT_USEC);
292 if (ret)
293 pr_err("%s: failed to write to mdio (phy:%d reg:%x)\n",
294 priv->phydev->dev->name, phyid, regnum);
295
296 return ret;
297}
298
299static int ave_adjust_link(struct ave_private *priv)
300{
301 struct phy_device *phydev = priv->phydev;
Simon Glassfa20e932020-12-03 16:55:20 -0700302 struct eth_pdata *pdata = dev_get_plat(phydev->dev);
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +0900303 u32 val, txcr, rxcr, rxcr_org;
304 u16 rmt_adv = 0, lcl_adv = 0;
305 u8 cap;
306
307 /* set RGMII speed */
308 val = readl(priv->iobase + AVE_TXCR);
309 val &= ~(AVE_TXCR_TXSPD_100 | AVE_TXCR_TXSPD_1G);
310
311 if (phy_interface_is_rgmii(phydev) && phydev->speed == SPEED_1000)
312 val |= AVE_TXCR_TXSPD_1G;
313 else if (phydev->speed == SPEED_100)
314 val |= AVE_TXCR_TXSPD_100;
315
316 writel(val, priv->iobase + AVE_TXCR);
317
318 /* set RMII speed (100M/10M only) */
319 if (!phy_interface_is_rgmii(phydev)) {
320 val = readl(priv->iobase + AVE_LINKSEL);
321 if (phydev->speed == SPEED_10)
322 val &= ~AVE_LINKSEL_100M;
323 else
324 val |= AVE_LINKSEL_100M;
325 writel(val, priv->iobase + AVE_LINKSEL);
326 }
327
328 /* check current RXCR/TXCR */
329 rxcr = readl(priv->iobase + AVE_RXCR);
330 txcr = readl(priv->iobase + AVE_TXCR);
331 rxcr_org = rxcr;
332
333 if (phydev->duplex) {
334 rxcr |= AVE_RXCR_FDUPEN;
335
336 if (phydev->pause)
337 rmt_adv |= LPA_PAUSE_CAP;
338 if (phydev->asym_pause)
339 rmt_adv |= LPA_PAUSE_ASYM;
340 if (phydev->advertising & ADVERTISED_Pause)
341 lcl_adv |= ADVERTISE_PAUSE_CAP;
342 if (phydev->advertising & ADVERTISED_Asym_Pause)
343 lcl_adv |= ADVERTISE_PAUSE_ASYM;
344
345 cap = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
346 if (cap & FLOW_CTRL_TX)
347 txcr |= AVE_TXCR_FLOCTR;
348 else
349 txcr &= ~AVE_TXCR_FLOCTR;
350 if (cap & FLOW_CTRL_RX)
351 rxcr |= AVE_RXCR_FLOCTR;
352 else
353 rxcr &= ~AVE_RXCR_FLOCTR;
354 } else {
355 rxcr &= ~AVE_RXCR_FDUPEN;
356 rxcr &= ~AVE_RXCR_FLOCTR;
357 txcr &= ~AVE_TXCR_FLOCTR;
358 }
359
360 if (rxcr_org != rxcr) {
361 /* disable Rx mac */
362 writel(rxcr & ~AVE_RXCR_RXEN, priv->iobase + AVE_RXCR);
363 /* change and enable TX/Rx mac */
364 writel(txcr, priv->iobase + AVE_TXCR);
365 writel(rxcr, priv->iobase + AVE_RXCR);
366 }
367
368 pr_notice("%s: phy:%s speed:%d mac:%pM\n",
369 phydev->dev->name, phydev->drv->name, phydev->speed,
370 pdata->enetaddr);
371
372 return phydev->link;
373}
374
375static int ave_mdiobus_init(struct ave_private *priv, const char *name)
376{
377 struct mii_dev *bus = mdio_alloc();
378
379 if (!bus)
380 return -ENOMEM;
381
382 bus->read = ave_mdiobus_read;
383 bus->write = ave_mdiobus_write;
384 snprintf(bus->name, sizeof(bus->name), "%s", name);
385 bus->priv = priv;
386
387 return mdio_register(bus);
388}
389
390static int ave_phy_init(struct ave_private *priv, void *dev)
391{
392 struct phy_device *phydev;
393 int mask = GENMASK(31, 0), ret;
394
395 phydev = phy_find_by_mask(priv->bus, mask, priv->phy_mode);
396 if (!phydev)
397 return -ENODEV;
398
399 phy_connect_dev(phydev, dev);
400
401 phydev->supported &= PHY_GBIT_FEATURES;
402 if (priv->max_speed) {
403 ret = phy_set_supported(phydev, priv->max_speed);
404 if (ret)
405 return ret;
406 }
407 phydev->advertising = phydev->supported;
408
409 priv->phydev = phydev;
410 phy_config(phydev);
411
412 return 0;
413}
414
415static void ave_stop(struct udevice *dev)
416{
417 struct ave_private *priv = dev_get_priv(dev);
418 u32 val;
419 int ret;
420
421 val = readl(priv->iobase + AVE_GRR);
422 if (val)
423 return;
424
425 val = readl(priv->iobase + AVE_RXCR);
426 val &= ~AVE_RXCR_RXEN;
427 writel(val, priv->iobase + AVE_RXCR);
428
429 writel(0, priv->iobase + AVE_DESCC);
430 ret = readl_poll_timeout(priv->iobase + AVE_DESCC, val, !val,
431 AVE_HALT_TIMEOUT_USEC);
432 if (ret)
433 pr_warn("%s: halt timeout\n", priv->phydev->dev->name);
434
435 writel(AVE_GRR_GRST, priv->iobase + AVE_GRR);
436
437 phy_shutdown(priv->phydev);
438}
439
440static void ave_reset(struct ave_private *priv)
441{
442 u32 val;
443
444 /* reset RMII register */
445 val = readl(priv->iobase + AVE_RSTCTRL);
446 val &= ~AVE_RSTCTRL_RMIIRST;
447 writel(val, priv->iobase + AVE_RSTCTRL);
448
449 /* assert reset */
450 writel(AVE_GRR_GRST | AVE_GRR_PHYRST, priv->iobase + AVE_GRR);
451 mdelay(AVE_GRST_DELAY_MSEC);
452
453 /* 1st, negate PHY reset only */
454 writel(AVE_GRR_GRST, priv->iobase + AVE_GRR);
455 mdelay(AVE_GRST_DELAY_MSEC);
456
457 /* negate reset */
458 writel(0, priv->iobase + AVE_GRR);
459 mdelay(AVE_GRST_DELAY_MSEC);
460
461 /* negate RMII register */
462 val = readl(priv->iobase + AVE_RSTCTRL);
463 val |= AVE_RSTCTRL_RMIIRST;
464 writel(val, priv->iobase + AVE_RSTCTRL);
465}
466
467static int ave_start(struct udevice *dev)
468{
469 struct ave_private *priv = dev_get_priv(dev);
470 uintptr_t paddr;
471 u32 val;
472 int i;
473
474 ave_reset(priv);
475
476 priv->rx_pos = 0;
477 priv->rx_off = 2; /* RX data has 2byte offsets */
478 priv->tx_num = 0;
479 priv->tx_adj_buf =
480 (void *)roundup((uintptr_t)&priv->tx_adj_packetbuf[0],
481 PKTALIGN);
482 priv->rx_siz = (PKTSIZE_ALIGN - priv->rx_off);
483
484 val = 0;
485 if (priv->phy_mode != PHY_INTERFACE_MODE_RGMII)
486 val |= AVE_CFGR_MII;
487 writel(val, priv->iobase + AVE_CFGR);
488
489 /* use one descriptor for Tx */
490 writel(AVE_DESC_SIZE(priv, 1) << 16, priv->iobase + AVE_TXDC);
491 ave_desc_write_cmdsts(priv, AVE_DESCID_TX, 0, 0);
492 ave_desc_write_addr(priv, AVE_DESCID_TX, 0, 0);
493
494 /* use PKTBUFSRX descriptors for Rx */
495 writel(AVE_DESC_SIZE(priv, PKTBUFSRX) << 16, priv->iobase + AVE_RXDC);
496 for (i = 0; i < PKTBUFSRX; i++) {
497 paddr = (uintptr_t)net_rx_packets[i];
498 ave_cache_flush(paddr, priv->rx_siz + priv->rx_off);
499 ave_desc_write_addr(priv, AVE_DESCID_RX, i, paddr);
500 ave_desc_write_cmdsts(priv, AVE_DESCID_RX, i, priv->rx_siz);
501 }
502
503 writel(AVE_GISR_CLR, priv->iobase + AVE_GISR);
504 writel(AVE_GIMR_CLR, priv->iobase + AVE_GIMR);
505
506 writel(AVE_RXCR_RXEN | AVE_RXCR_FDUPEN | AVE_RXCR_FLOCTR | AVE_RXCR_MTU,
507 priv->iobase + AVE_RXCR);
508 writel(AVE_DESCC_RD0 | AVE_DESCC_TD, priv->iobase + AVE_DESCC);
509
510 phy_startup(priv->phydev);
511 ave_adjust_link(priv);
512
513 return 0;
514}
515
516static int ave_write_hwaddr(struct udevice *dev)
517{
518 struct ave_private *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700519 struct eth_pdata *pdata = dev_get_plat(dev);
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +0900520 u8 *mac = pdata->enetaddr;
521
522 writel(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
523 priv->iobase + AVE_RXMAC1R);
524 writel(mac[4] | mac[5] << 8, priv->iobase + AVE_RXMAC2R);
525
526 return 0;
527}
528
529static int ave_send(struct udevice *dev, void *packet, int length)
530{
531 struct ave_private *priv = dev_get_priv(dev);
532 u32 val;
533 void *ptr = packet;
534 int count;
535
536 /* adjust alignment for descriptor */
537 if ((uintptr_t)ptr & 0x3) {
538 memcpy(priv->tx_adj_buf, (const void *)ptr, length);
539 ptr = priv->tx_adj_buf;
540 }
541
542 /* padding for minimum length */
543 if (length < AVE_MIN_XMITSIZE) {
544 memset(ptr + length, 0, AVE_MIN_XMITSIZE - length);
545 length = AVE_MIN_XMITSIZE;
546 }
547
548 /* check ownership and wait for previous xmit done */
549 count = AVE_SEND_TIMEOUT_COUNT;
550 do {
551 val = ave_desc_read_cmdsts(priv, AVE_DESCID_TX, 0);
552 } while ((val & AVE_STS_OWN) && --count);
553 if (!count)
554 return -ETIMEDOUT;
555
556 ave_cache_flush((uintptr_t)ptr, length);
557 ave_desc_write_addr(priv, AVE_DESCID_TX, 0, (uintptr_t)ptr);
558
559 val = AVE_STS_OWN | AVE_STS_1ST | AVE_STS_LAST |
560 (length & AVE_STS_PKTLEN_TX_MASK);
561 ave_desc_write_cmdsts(priv, AVE_DESCID_TX, 0, val);
562 priv->tx_num++;
563
564 count = AVE_SEND_TIMEOUT_COUNT;
565 do {
566 val = ave_desc_read_cmdsts(priv, AVE_DESCID_TX, 0);
567 } while ((val & AVE_STS_OWN) && --count);
568 if (!count)
569 return -ETIMEDOUT;
570
571 if (!(val & AVE_STS_OK))
572 pr_warn("%s: bad send packet status:%08x\n",
573 priv->phydev->dev->name, le32_to_cpu(val));
574
575 return 0;
576}
577
578static int ave_recv(struct udevice *dev, int flags, uchar **packetp)
579{
580 struct ave_private *priv = dev_get_priv(dev);
581 uchar *ptr;
582 int length = 0;
583 u32 cmdsts;
584
585 while (1) {
586 cmdsts = ave_desc_read_cmdsts(priv, AVE_DESCID_RX,
587 priv->rx_pos);
588 if (!(cmdsts & AVE_STS_OWN))
589 /* hardware ownership, no received packets */
590 return -EAGAIN;
591
592 ptr = net_rx_packets[priv->rx_pos] + priv->rx_off;
593 if (cmdsts & AVE_STS_OK)
594 break;
595
596 pr_warn("%s: bad packet[%d] status:%08x ptr:%p\n",
597 priv->phydev->dev->name, priv->rx_pos,
598 le32_to_cpu(cmdsts), ptr);
599 }
600
601 length = cmdsts & AVE_STS_PKTLEN_RX_MASK;
602
603 /* invalidate after DMA is done */
604 ave_cache_invalidate((uintptr_t)ptr, length);
605 *packetp = ptr;
606
607 return length;
608}
609
610static int ave_free_packet(struct udevice *dev, uchar *packet, int length)
611{
612 struct ave_private *priv = dev_get_priv(dev);
613
614 ave_cache_flush((uintptr_t)net_rx_packets[priv->rx_pos],
615 priv->rx_siz + priv->rx_off);
616
617 ave_desc_write_cmdsts(priv, AVE_DESCID_RX,
618 priv->rx_pos, priv->rx_siz);
619
620 if (++priv->rx_pos >= PKTBUFSRX)
621 priv->rx_pos = 0;
622
623 return 0;
624}
625
626static int ave_pro4_get_pinmode(struct ave_private *priv)
627{
628 u32 reg, mask, val = 0;
629
630 if (priv->regmap_arg > 0)
631 return -EINVAL;
632
633 mask = SG_ETPINMODE_RMII(0);
634
635 switch (priv->phy_mode) {
636 case PHY_INTERFACE_MODE_RMII:
637 val = SG_ETPINMODE_RMII(0);
638 break;
639 case PHY_INTERFACE_MODE_MII:
640 case PHY_INTERFACE_MODE_RGMII:
641 break;
642 default:
643 return -EINVAL;
644 }
645
646 regmap_read(priv->regmap, SG_ETPINMODE, &reg);
647 reg &= ~mask;
648 reg |= val;
649 regmap_write(priv->regmap, SG_ETPINMODE, reg);
650
651 return 0;
652}
653
654static int ave_ld11_get_pinmode(struct ave_private *priv)
655{
656 u32 reg, mask, val = 0;
657
658 if (priv->regmap_arg > 0)
659 return -EINVAL;
660
661 mask = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
662
663 switch (priv->phy_mode) {
664 case PHY_INTERFACE_MODE_INTERNAL:
665 break;
666 case PHY_INTERFACE_MODE_RMII:
667 val = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
668 break;
669 default:
670 return -EINVAL;
671 }
672
673 regmap_read(priv->regmap, SG_ETPINMODE, &reg);
674 reg &= ~mask;
675 reg |= val;
676 regmap_write(priv->regmap, SG_ETPINMODE, reg);
677
678 return 0;
679}
680
681static int ave_ld20_get_pinmode(struct ave_private *priv)
682{
683 u32 reg, mask, val = 0;
684
685 if (priv->regmap_arg > 0)
686 return -EINVAL;
687
688 mask = SG_ETPINMODE_RMII(0);
689
690 switch (priv->phy_mode) {
691 case PHY_INTERFACE_MODE_RMII:
692 val = SG_ETPINMODE_RMII(0);
693 break;
694 case PHY_INTERFACE_MODE_RGMII:
695 break;
696 default:
697 return -EINVAL;
698 }
699
700 regmap_read(priv->regmap, SG_ETPINMODE, &reg);
701 reg &= ~mask;
702 reg |= val;
703 regmap_write(priv->regmap, SG_ETPINMODE, reg);
704
705 return 0;
706}
707
708static int ave_pxs3_get_pinmode(struct ave_private *priv)
709{
710 u32 reg, mask, val = 0;
711
712 if (priv->regmap_arg > 1)
713 return -EINVAL;
714
715 mask = SG_ETPINMODE_RMII(priv->regmap_arg);
716
717 switch (priv->phy_mode) {
718 case PHY_INTERFACE_MODE_RMII:
719 val = SG_ETPINMODE_RMII(priv->regmap_arg);
720 break;
721 case PHY_INTERFACE_MODE_RGMII:
722 break;
723 default:
724 return -EINVAL;
725 }
726
727 regmap_read(priv->regmap, SG_ETPINMODE, &reg);
728 reg &= ~mask;
729 reg |= val;
730 regmap_write(priv->regmap, SG_ETPINMODE, reg);
731
732 return 0;
733}
734
735static int ave_ofdata_to_platdata(struct udevice *dev)
736{
Simon Glassfa20e932020-12-03 16:55:20 -0700737 struct eth_pdata *pdata = dev_get_plat(dev);
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +0900738 struct ave_private *priv = dev_get_priv(dev);
739 struct ofnode_phandle_args args;
740 const char *phy_mode;
741 const u32 *valp;
742 int ret, nc, nr;
743 const char *name;
744
745 priv->data = (const struct ave_soc_data *)dev_get_driver_data(dev);
746 if (!priv->data)
747 return -EINVAL;
748
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900749 pdata->iobase = dev_read_addr(dev);
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +0900750 pdata->phy_interface = -1;
751 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
752 NULL);
753 if (phy_mode)
754 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
755 if (pdata->phy_interface == -1) {
756 dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
757 return -EINVAL;
758 }
759
760 pdata->max_speed = 0;
761 valp = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed",
762 NULL);
763 if (valp)
764 pdata->max_speed = fdt32_to_cpu(*valp);
765
766 for (nc = 0; nc < AVE_MAX_CLKS; nc++) {
767 name = priv->data->clock_names[nc];
768 if (!name)
769 break;
770 ret = clk_get_by_name(dev, name, &priv->clk[nc]);
771 if (ret) {
772 dev_err(dev, "Failed to get clocks property: %d\n",
773 ret);
774 goto out_clk_free;
775 }
776 priv->nclks++;
777 }
778
779 for (nr = 0; nr < AVE_MAX_RSTS; nr++) {
780 name = priv->data->reset_names[nr];
781 if (!name)
782 break;
783 ret = reset_get_by_name(dev, name, &priv->rst[nr]);
784 if (ret) {
785 dev_err(dev, "Failed to get resets property: %d\n",
786 ret);
787 goto out_reset_free;
788 }
789 priv->nrsts++;
790 }
791
792 ret = dev_read_phandle_with_args(dev, "socionext,syscon-phy-mode",
793 NULL, 1, 0, &args);
794 if (ret) {
795 dev_err(dev, "Failed to get syscon-phy-mode property: %d\n",
796 ret);
797 goto out_reset_free;
798 }
799
800 priv->regmap = syscon_node_to_regmap(args.node);
801 if (IS_ERR(priv->regmap)) {
802 ret = PTR_ERR(priv->regmap);
803 dev_err(dev, "can't get syscon: %d\n", ret);
804 goto out_reset_free;
805 }
806
807 if (args.args_count != 1) {
808 ret = -EINVAL;
809 dev_err(dev, "Invalid argument of syscon-phy-mode\n");
810 goto out_reset_free;
811 }
812
813 priv->regmap_arg = args.args[0];
814
815 return 0;
816
817out_reset_free:
818 while (--nr >= 0)
819 reset_free(&priv->rst[nr]);
820out_clk_free:
821 while (--nc >= 0)
822 clk_free(&priv->clk[nc]);
823
824 return ret;
825}
826
827static int ave_probe(struct udevice *dev)
828{
Simon Glassfa20e932020-12-03 16:55:20 -0700829 struct eth_pdata *pdata = dev_get_plat(dev);
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +0900830 struct ave_private *priv = dev_get_priv(dev);
831 int ret, nc, nr;
832
833 priv->data = (const struct ave_soc_data *)dev_get_driver_data(dev);
834 if (!priv->data)
835 return -EINVAL;
836
837 priv->iobase = pdata->iobase;
838 priv->phy_mode = pdata->phy_interface;
839 priv->max_speed = pdata->max_speed;
840
841 ret = priv->data->get_pinmode(priv);
842 if (ret) {
843 dev_err(dev, "Invalid phy-mode\n");
844 return -EINVAL;
845 }
846
847 for (nc = 0; nc < priv->nclks; nc++) {
848 ret = clk_enable(&priv->clk[nc]);
849 if (ret) {
850 dev_err(dev, "Failed to enable clk: %d\n", ret);
851 goto out_clk_release;
852 }
853 }
854
855 for (nr = 0; nr < priv->nrsts; nr++) {
856 ret = reset_deassert(&priv->rst[nr]);
857 if (ret) {
858 dev_err(dev, "Failed to deassert reset: %d\n", ret);
859 goto out_reset_release;
860 }
861 }
862
863 ave_reset(priv);
864
865 ret = ave_mdiobus_init(priv, dev->name);
866 if (ret) {
867 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
868 goto out_reset_release;
869 }
870
871 priv->bus = miiphy_get_dev_by_name(dev->name);
872
873 ret = ave_phy_init(priv, dev);
874 if (ret) {
875 dev_err(dev, "Failed to initialize phy: %d\n", ret);
876 goto out_mdiobus_release;
877 }
878
879 return 0;
880
881out_mdiobus_release:
882 mdio_unregister(priv->bus);
883 mdio_free(priv->bus);
884out_reset_release:
885 reset_release_all(priv->rst, nr);
886out_clk_release:
887 clk_release_all(priv->clk, nc);
888
889 return ret;
890}
891
892static int ave_remove(struct udevice *dev)
893{
894 struct ave_private *priv = dev_get_priv(dev);
895
896 free(priv->phydev);
897 mdio_unregister(priv->bus);
898 mdio_free(priv->bus);
899 reset_release_all(priv->rst, priv->nrsts);
900 clk_release_all(priv->clk, priv->nclks);
901
902 return 0;
903}
904
905static const struct eth_ops ave_ops = {
906 .start = ave_start,
907 .stop = ave_stop,
908 .send = ave_send,
909 .recv = ave_recv,
910 .free_pkt = ave_free_packet,
911 .write_hwaddr = ave_write_hwaddr,
912};
913
914static const struct ave_soc_data ave_pro4_data = {
915 .is_desc_64bit = false,
916 .clock_names = {
917 "gio", "ether", "ether-gb", "ether-phy",
918 },
919 .reset_names = {
920 "gio", "ether",
921 },
922 .get_pinmode = ave_pro4_get_pinmode,
923};
924
925static const struct ave_soc_data ave_pxs2_data = {
926 .is_desc_64bit = false,
927 .clock_names = {
928 "ether",
929 },
930 .reset_names = {
931 "ether",
932 },
933 .get_pinmode = ave_pro4_get_pinmode,
934};
935
936static const struct ave_soc_data ave_ld11_data = {
937 .is_desc_64bit = false,
938 .clock_names = {
939 "ether",
940 },
941 .reset_names = {
942 "ether",
943 },
944 .get_pinmode = ave_ld11_get_pinmode,
945};
946
947static const struct ave_soc_data ave_ld20_data = {
948 .is_desc_64bit = true,
949 .clock_names = {
950 "ether",
951 },
952 .reset_names = {
953 "ether",
954 },
955 .get_pinmode = ave_ld20_get_pinmode,
956};
957
958static const struct ave_soc_data ave_pxs3_data = {
959 .is_desc_64bit = false,
960 .clock_names = {
961 "ether",
962 },
963 .reset_names = {
964 "ether",
965 },
966 .get_pinmode = ave_pxs3_get_pinmode,
967};
968
969static const struct udevice_id ave_ids[] = {
970 {
971 .compatible = "socionext,uniphier-pro4-ave4",
972 .data = (ulong)&ave_pro4_data,
973 },
974 {
975 .compatible = "socionext,uniphier-pxs2-ave4",
976 .data = (ulong)&ave_pxs2_data,
977 },
978 {
979 .compatible = "socionext,uniphier-ld11-ave4",
980 .data = (ulong)&ave_ld11_data,
981 },
982 {
983 .compatible = "socionext,uniphier-ld20-ave4",
984 .data = (ulong)&ave_ld20_data,
985 },
986 {
987 .compatible = "socionext,uniphier-pxs3-ave4",
988 .data = (ulong)&ave_pxs3_data,
989 },
990 { /* Sentinel */ }
991};
992
993U_BOOT_DRIVER(ave) = {
994 .name = "ave",
995 .id = UCLASS_ETH,
996 .of_match = ave_ids,
997 .probe = ave_probe,
998 .remove = ave_remove,
999 .ofdata_to_platdata = ave_ofdata_to_platdata,
1000 .ops = &ave_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001001 .priv_auto = sizeof(struct ave_private),
Simon Glass71fa5b42020-12-03 16:55:18 -07001002 .plat_auto = sizeof(struct eth_pdata),
Kunihiko Hayashi7dcdc222018-05-24 19:24:37 +09001003};