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