blob: fa0b3dbb6d1787a93e1989b06898f1b3f4487cce [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Po-Yu Chuang50253b82009-08-10 11:00:00 +08002/*
3 * Faraday FTMAC100 Ethernet
4 *
5 * (C) Copyright 2009 Faraday Technology
6 * Po-Yu Chuang <ratbert@faraday-tech.com>
Po-Yu Chuang50253b82009-08-10 11:00:00 +08007 */
8
9#include <config.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Po-Yu Chuang50253b82009-08-10 11:00:00 +080012#include <malloc.h>
13#include <net.h>
Sergei Antonovb8a0ab92023-02-03 22:09:04 +030014#include <phy.h>
15#include <miiphy.h>
16#include <dm/device_compat.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
rickdc24dac2017-05-23 13:48:27 +080019#include <linux/io.h>
Sergei Antonovb8a0ab92023-02-03 22:09:04 +030020#include <linux/iopoll.h>
Po-Yu Chuang50253b82009-08-10 11:00:00 +080021
22#include "ftmac100.h"
rickdc24dac2017-05-23 13:48:27 +080023#include <dm.h>
Tom Rinic8c4c562022-11-19 18:45:40 -050024
rickdc24dac2017-05-23 13:48:27 +080025DECLARE_GLOBAL_DATA_PTR;
Tom Rinic8c4c562022-11-19 18:45:40 -050026
Po-Yu Chuang50253b82009-08-10 11:00:00 +080027#define ETH_ZLEN 60
28
Sergei Antonovb8a0ab92023-02-03 22:09:04 +030029/* Timeout for a mdio read/write operation */
30#define FTMAC100_MDIO_TIMEOUT_USEC 10000
31
Po-Yu Chuang50253b82009-08-10 11:00:00 +080032struct ftmac100_data {
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080033 struct ftmac100_txdes txdes[1];
34 struct ftmac100_rxdes rxdes[PKTBUFSRX];
Po-Yu Chuang50253b82009-08-10 11:00:00 +080035 int rx_index;
rickdc24dac2017-05-23 13:48:27 +080036 const char *name;
Sergei Antonov2f69cb02023-02-03 22:09:03 +030037 struct ftmac100 *ftmac100;
Sergei Antonovb8a0ab92023-02-03 22:09:04 +030038 struct mii_dev *bus;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080039};
40
41/*
42 * Reset MAC
43 */
rickdc24dac2017-05-23 13:48:27 +080044static void ftmac100_reset(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080045{
Sergei Antonov2f69cb02023-02-03 22:09:03 +030046 struct ftmac100 *ftmac100 = priv->ftmac100;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080047
48 debug ("%s()\n", __func__);
49
50 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
51
52 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
rick54966612017-08-29 10:15:05 +080053 mdelay(1);
54 /*
55 * When soft reset complete, write mac address immediately maybe fail somehow
56 * Wait for a while can avoid this problem
57 */
58 mdelay(1);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080059}
60
61/*
62 * Set MAC address
63 */
rickdc24dac2017-05-23 13:48:27 +080064static void ftmac100_set_mac(struct ftmac100_data *priv ,
65 const unsigned char *mac)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080066{
Sergei Antonov2f69cb02023-02-03 22:09:03 +030067 struct ftmac100 *ftmac100 = priv->ftmac100;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080068 unsigned int maddr = mac[0] << 8 | mac[1];
69 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
70
71 debug ("%s(%x %x)\n", __func__, maddr, laddr);
72
73 writel (maddr, &ftmac100->mac_madr);
74 writel (laddr, &ftmac100->mac_ladr);
75}
76
Po-Yu Chuang50253b82009-08-10 11:00:00 +080077/*
rickdc24dac2017-05-23 13:48:27 +080078 * Disable MAC
Po-Yu Chuang50253b82009-08-10 11:00:00 +080079 */
rickdc24dac2017-05-23 13:48:27 +080080static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080081{
Sergei Antonov2f69cb02023-02-03 22:09:03 +030082 struct ftmac100 *ftmac100 = priv->ftmac100;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080083 debug ("%s()\n", __func__);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080084 writel (0, &ftmac100->maccr);
85}
86
rickdc24dac2017-05-23 13:48:27 +080087/*
88 * Initialize MAC
89 */
90static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang50253b82009-08-10 11:00:00 +080091{
Sergei Antonov2f69cb02023-02-03 22:09:03 +030092 struct ftmac100 *ftmac100 = priv->ftmac100;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080093 struct ftmac100_txdes *txdes = priv->txdes;
94 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080095 unsigned int maccr;
96 int i;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080097 debug ("%s()\n", __func__);
98
rickdc24dac2017-05-23 13:48:27 +080099 ftmac100_reset(priv);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800100
101 /* set the ethernet address */
rickdc24dac2017-05-23 13:48:27 +0800102 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800103
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800104 /* disable all interrupts */
105
106 writel (0, &ftmac100->imr);
107
108 /* initialize descriptors */
109
110 priv->rx_index = 0;
111
112 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
113 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
114
115 for (i = 0; i < PKTBUFSRX; i++) {
116 /* RXBUF_BADR */
Rick Chen59d15262018-05-29 10:42:19 +0800117 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800118 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
119 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
120 }
121
122 /* transmit ring */
123
Rick Chen59d15262018-05-29 10:42:19 +0800124 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800125
126 /* receive ring */
127
Rick Chen59d15262018-05-29 10:42:19 +0800128 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800129
130 /* poll receive descriptor automatically */
131
132 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
133
134 /* enable transmitter, receiver */
135
136 maccr = FTMAC100_MACCR_XMT_EN |
137 FTMAC100_MACCR_RCV_EN |
138 FTMAC100_MACCR_XDMA_EN |
139 FTMAC100_MACCR_RDMA_EN |
140 FTMAC100_MACCR_CRC_APD |
141 FTMAC100_MACCR_ENRX_IN_HALFTX |
142 FTMAC100_MACCR_RX_RUNT |
143 FTMAC100_MACCR_RX_BROADPKT;
144
145 writel (maccr, &ftmac100->maccr);
146
147 return 0;
148}
149
150/*
rickdc24dac2017-05-23 13:48:27 +0800151 * Free receiving buffer
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800152 */
rickdc24dac2017-05-23 13:48:27 +0800153static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800154{
rickdc24dac2017-05-23 13:48:27 +0800155 struct ftmac100_rxdes *curr_des;
156 curr_des = &priv->rxdes[priv->rx_index];
157 /* release buffer to DMA */
158 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
159 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
160 return 0;
161}
162
163/*
164 * Receive a data block via Ethernet
165 */
166static int __ftmac100_recv(struct ftmac100_data *priv)
167{
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800168 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800169 unsigned short rxlen;
170
171 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800172 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickdc24dac2017-05-23 13:48:27 +0800173 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800174
175 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
176 FTMAC100_RXDES0_CRC_ERR |
177 FTMAC100_RXDES0_FTL |
178 FTMAC100_RXDES0_RUNT |
179 FTMAC100_RXDES0_RX_ODD_NB)) {
rickdc24dac2017-05-23 13:48:27 +0800180 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800181 }
182
183 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickb0ce1822017-08-29 10:09:00 +0800184 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800185 debug ("%s(): RX buffer %d, %x received\n",
186 __func__, priv->rx_index, rxlen);
187
rickdc24dac2017-05-23 13:48:27 +0800188 return rxlen;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800189}
190
191/*
192 * Send a data block via Ethernet
193 */
rickdc24dac2017-05-23 13:48:27 +0800194static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800195{
Sergei Antonov2f69cb02023-02-03 22:09:03 +0300196 struct ftmac100 *ftmac100 = priv->ftmac100;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800197 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800198 ulong start;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800199
200 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
201 debug ("%s(): no TX descriptor available\n", __func__);
202 return -1;
203 }
204
Rick Chen59d15262018-05-29 10:42:19 +0800205 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800206
207 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
208
209 /* initiate a transmit sequence */
210
Rick Chen59d15262018-05-29 10:42:19 +0800211 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
212 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800213
214 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
215 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
216 FTMAC100_TXDES1_LTS |
217 FTMAC100_TXDES1_TXBUF_SIZE (length);
218
219 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
220
221 /* start transmit */
222
223 writel (1, &ftmac100->txpd);
224
225 /* wait for transfer to succeed */
226
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800227 start = get_timer(0);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800228 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800229 if (get_timer(start) >= 5) {
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800230 debug ("%s(): timed out\n", __func__);
231 return -1;
232 }
233 }
234
235 debug ("%s(): packet sent\n", __func__);
236
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800237 return 0;
238}
rickdc24dac2017-05-23 13:48:27 +0800239
rickdc24dac2017-05-23 13:48:27 +0800240static int ftmac100_start(struct udevice *dev)
241{
Simon Glassfa20e932020-12-03 16:55:20 -0700242 struct eth_pdata *plat = dev_get_plat(dev);
rickdc24dac2017-05-23 13:48:27 +0800243 struct ftmac100_data *priv = dev_get_priv(dev);
244
245 return _ftmac100_init(priv, plat->enetaddr);
246}
247
248static void ftmac100_stop(struct udevice *dev)
249{
250 struct ftmac100_data *priv = dev_get_priv(dev);
251 _ftmac100_halt(priv);
252}
253
254static int ftmac100_send(struct udevice *dev, void *packet, int length)
255{
256 struct ftmac100_data *priv = dev_get_priv(dev);
257 int ret;
258 ret = _ftmac100_send(priv , packet , length);
259 return ret ? 0 : -ETIMEDOUT;
260}
261
262static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
263{
264 struct ftmac100_data *priv = dev_get_priv(dev);
265 struct ftmac100_rxdes *curr_des;
266 curr_des = &priv->rxdes[priv->rx_index];
267 int len;
268 len = __ftmac100_recv(priv);
269 if (len)
Rick Chen59d15262018-05-29 10:42:19 +0800270 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickdc24dac2017-05-23 13:48:27 +0800271
272 return len ? len : -EAGAIN;
273}
274
275static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
276{
277 struct ftmac100_data *priv = dev_get_priv(dev);
278 _ftmac100_free_pkt(priv);
279 return 0;
280}
281
282int ftmac100_read_rom_hwaddr(struct udevice *dev)
283{
Simon Glassfa20e932020-12-03 16:55:20 -0700284 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass399a9ce2017-08-03 12:22:14 -0600285 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickdc24dac2017-05-23 13:48:27 +0800286 return 0;
287}
288
289static const char *dtbmacaddr(u32 ifno)
290{
291 int node, len;
292 char enet[16];
293 const char *mac;
294 const char *path;
295 if (gd->fdt_blob == NULL) {
296 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
297 return NULL;
298 }
299 node = fdt_path_offset(gd->fdt_blob, "/aliases");
300 if (node < 0)
301 return NULL;
302
303 sprintf(enet, "ethernet%d", ifno);
304 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
305 if (!path) {
306 printf("no alias for %s\n", enet);
307 return NULL;
308 }
309 node = fdt_path_offset(gd->fdt_blob, path);
310 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
311 if (mac && is_valid_ethaddr((u8 *)mac))
312 return mac;
313
314 return NULL;
315}
316
Simon Glassaad29ae2020-12-03 16:55:21 -0700317static int ftmac100_of_to_plat(struct udevice *dev)
rickdc24dac2017-05-23 13:48:27 +0800318{
319 struct ftmac100_data *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700320 struct eth_pdata *pdata = dev_get_plat(dev);
rickdc24dac2017-05-23 13:48:27 +0800321 const char *mac;
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900322 pdata->iobase = dev_read_addr(dev);
Sergei Antonov2f69cb02023-02-03 22:09:03 +0300323 priv->ftmac100 = phys_to_virt(pdata->iobase);
rickdc24dac2017-05-23 13:48:27 +0800324 mac = dtbmacaddr(0);
325 if (mac)
326 memcpy(pdata->enetaddr , mac , 6);
327
328 return 0;
329}
330
Sergei Antonovb8a0ab92023-02-03 22:09:04 +0300331/*
332 * struct mii_bus functions
333 */
334static int ftmac100_mdio_read(struct mii_dev *bus, int addr, int devad,
335 int reg)
336{
337 struct ftmac100_data *priv = bus->priv;
338 struct ftmac100 *ftmac100 = priv->ftmac100;
339 int phycr = FTMAC100_PHYCR_PHYAD(addr) |
340 FTMAC100_PHYCR_REGAD(reg) |
341 FTMAC100_PHYCR_MIIRD;
342 int ret;
343
344 writel(phycr, &ftmac100->phycr);
345
346 ret = readl_poll_timeout(&ftmac100->phycr, phycr,
347 !(phycr & FTMAC100_PHYCR_MIIRD),
348 FTMAC100_MDIO_TIMEOUT_USEC);
349 if (ret)
350 pr_err("%s: mdio read failed (addr=0x%x reg=0x%x)\n",
351 bus->name, addr, reg);
352 else
353 ret = phycr & FTMAC100_PHYCR_MIIRDATA;
354
355 return ret;
356}
357
358static int ftmac100_mdio_write(struct mii_dev *bus, int addr, int devad,
359 int reg, u16 value)
360{
361 struct ftmac100_data *priv = bus->priv;
362 struct ftmac100 *ftmac100 = priv->ftmac100;
363 int phycr = FTMAC100_PHYCR_PHYAD(addr) |
364 FTMAC100_PHYCR_REGAD(reg) |
365 FTMAC100_PHYCR_MIIWR;
366 int ret;
367
368 writel(value, &ftmac100->phywdata);
369 writel(phycr, &ftmac100->phycr);
370
371 ret = readl_poll_timeout(&ftmac100->phycr, phycr,
372 !(phycr & FTMAC100_PHYCR_MIIWR),
373 FTMAC100_MDIO_TIMEOUT_USEC);
374 if (ret)
375 pr_err("%s: mdio write failed (addr=0x%x reg=0x%x)\n",
376 bus->name, addr, reg);
377
378 return ret;
379}
380
381static int ftmac100_mdio_init(struct udevice *dev)
382{
383 struct ftmac100_data *priv = dev_get_priv(dev);
384 struct mii_dev *bus;
385 int ret;
386
387 bus = mdio_alloc();
388 if (!bus)
389 return -ENOMEM;
390
391 bus->read = ftmac100_mdio_read;
392 bus->write = ftmac100_mdio_write;
393 bus->priv = priv;
394
395 ret = mdio_register_seq(bus, dev_seq(dev));
396 if (ret) {
397 mdio_free(bus);
398 return ret;
399 }
400
401 priv->bus = bus;
402
403 return 0;
404}
405
rickdc24dac2017-05-23 13:48:27 +0800406static int ftmac100_probe(struct udevice *dev)
407{
408 struct ftmac100_data *priv = dev_get_priv(dev);
409 priv->name = dev->name;
Sergei Antonovb8a0ab92023-02-03 22:09:04 +0300410 int ret = 0;
411
412 ret = ftmac100_mdio_init(dev);
413 if (ret) {
414 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
415 goto out;
416 }
417
418out:
419 return ret;
420}
421
422static int ftmac100_remove(struct udevice *dev)
423{
424 struct ftmac100_data *priv = dev_get_priv(dev);
425
426 mdio_unregister(priv->bus);
427 mdio_free(priv->bus);
428
rickdc24dac2017-05-23 13:48:27 +0800429 return 0;
430}
431
432static int ftmac100_bind(struct udevice *dev)
433{
434 return device_set_name(dev, dev->name);
435}
436
437static const struct eth_ops ftmac100_ops = {
438 .start = ftmac100_start,
439 .send = ftmac100_send,
440 .recv = ftmac100_recv,
441 .stop = ftmac100_stop,
442 .free_pkt = ftmac100_free_pkt,
443};
444
445static const struct udevice_id ftmac100_ids[] = {
446 { .compatible = "andestech,atmac100" },
447 { }
448};
449
450U_BOOT_DRIVER(ftmac100) = {
Sergei Antonov0e15edb2023-02-03 22:09:02 +0300451 .name = "ftmac100",
rickdc24dac2017-05-23 13:48:27 +0800452 .id = UCLASS_ETH,
453 .of_match = ftmac100_ids,
454 .bind = ftmac100_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700455 .of_to_plat = ftmac100_of_to_plat,
rickdc24dac2017-05-23 13:48:27 +0800456 .probe = ftmac100_probe,
Sergei Antonovb8a0ab92023-02-03 22:09:04 +0300457 .remove = ftmac100_remove,
rickdc24dac2017-05-23 13:48:27 +0800458 .ops = &ftmac100_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700459 .priv_auto = sizeof(struct ftmac100_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700460 .plat_auto = sizeof(struct eth_pdata),
rickdc24dac2017-05-23 13:48:27 +0800461 .flags = DM_FLAG_ALLOC_PRIV_DMA,
462};