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