blob: 199a0723b8437d1d0b41f7719352b5af14a4e652 [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
105 /* disable all interrupts */
106
107 writel (0, &ftmac100->imr);
108
109 /* initialize descriptors */
110
111 priv->rx_index = 0;
112
113 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
114 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
115
116 for (i = 0; i < PKTBUFSRX; i++) {
117 /* RXBUF_BADR */
Rick Chen59d15262018-05-29 10:42:19 +0800118 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800119 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
120 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
121 }
122
123 /* transmit ring */
124
Rick Chen59d15262018-05-29 10:42:19 +0800125 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800126
127 /* receive ring */
128
Rick Chen59d15262018-05-29 10:42:19 +0800129 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800130
131 /* poll receive descriptor automatically */
132
133 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
134
135 /* enable transmitter, receiver */
136
137 maccr = FTMAC100_MACCR_XMT_EN |
138 FTMAC100_MACCR_RCV_EN |
139 FTMAC100_MACCR_XDMA_EN |
140 FTMAC100_MACCR_RDMA_EN |
141 FTMAC100_MACCR_CRC_APD |
142 FTMAC100_MACCR_ENRX_IN_HALFTX |
143 FTMAC100_MACCR_RX_RUNT |
144 FTMAC100_MACCR_RX_BROADPKT;
145
146 writel (maccr, &ftmac100->maccr);
147
148 return 0;
149}
150
151/*
rickdc24dac2017-05-23 13:48:27 +0800152 * Free receiving buffer
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800153 */
rickdc24dac2017-05-23 13:48:27 +0800154static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800155{
rickdc24dac2017-05-23 13:48:27 +0800156 struct ftmac100_rxdes *curr_des;
157 curr_des = &priv->rxdes[priv->rx_index];
158 /* release buffer to DMA */
159 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
160 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
161 return 0;
162}
163
164/*
165 * Receive a data block via Ethernet
166 */
167static int __ftmac100_recv(struct ftmac100_data *priv)
168{
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800169 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800170 unsigned short rxlen;
171
172 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800173 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickdc24dac2017-05-23 13:48:27 +0800174 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800175
176 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
177 FTMAC100_RXDES0_CRC_ERR |
178 FTMAC100_RXDES0_FTL |
179 FTMAC100_RXDES0_RUNT |
180 FTMAC100_RXDES0_RX_ODD_NB)) {
rickdc24dac2017-05-23 13:48:27 +0800181 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800182 }
183
184 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickb0ce1822017-08-29 10:09:00 +0800185 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800186 debug ("%s(): RX buffer %d, %x received\n",
187 __func__, priv->rx_index, rxlen);
188
rickdc24dac2017-05-23 13:48:27 +0800189 return rxlen;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800190}
191
192/*
193 * Send a data block via Ethernet
194 */
rickdc24dac2017-05-23 13:48:27 +0800195static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800196{
Sergei Antonov2f69cb02023-02-03 22:09:03 +0300197 struct ftmac100 *ftmac100 = priv->ftmac100;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800198 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800199 ulong start;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800200
201 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
202 debug ("%s(): no TX descriptor available\n", __func__);
203 return -1;
204 }
205
Rick Chen59d15262018-05-29 10:42:19 +0800206 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800207
208 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
209
210 /* initiate a transmit sequence */
211
Rick Chen59d15262018-05-29 10:42:19 +0800212 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
213 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800214
215 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
216 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
217 FTMAC100_TXDES1_LTS |
218 FTMAC100_TXDES1_TXBUF_SIZE (length);
219
220 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
221
222 /* start transmit */
223
224 writel (1, &ftmac100->txpd);
225
226 /* wait for transfer to succeed */
227
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800228 start = get_timer(0);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800229 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800230 if (get_timer(start) >= 5) {
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800231 debug ("%s(): timed out\n", __func__);
232 return -1;
233 }
234 }
235
236 debug ("%s(): packet sent\n", __func__);
237
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800238 return 0;
239}
rickdc24dac2017-05-23 13:48:27 +0800240
rickdc24dac2017-05-23 13:48:27 +0800241static int ftmac100_start(struct udevice *dev)
242{
Simon Glassfa20e932020-12-03 16:55:20 -0700243 struct eth_pdata *plat = dev_get_plat(dev);
rickdc24dac2017-05-23 13:48:27 +0800244 struct ftmac100_data *priv = dev_get_priv(dev);
245
246 return _ftmac100_init(priv, plat->enetaddr);
247}
248
249static void ftmac100_stop(struct udevice *dev)
250{
251 struct ftmac100_data *priv = dev_get_priv(dev);
252 _ftmac100_halt(priv);
253}
254
255static int ftmac100_send(struct udevice *dev, void *packet, int length)
256{
257 struct ftmac100_data *priv = dev_get_priv(dev);
258 int ret;
259 ret = _ftmac100_send(priv , packet , length);
260 return ret ? 0 : -ETIMEDOUT;
261}
262
263static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
264{
265 struct ftmac100_data *priv = dev_get_priv(dev);
266 struct ftmac100_rxdes *curr_des;
267 curr_des = &priv->rxdes[priv->rx_index];
268 int len;
269 len = __ftmac100_recv(priv);
270 if (len)
Rick Chen59d15262018-05-29 10:42:19 +0800271 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickdc24dac2017-05-23 13:48:27 +0800272
273 return len ? len : -EAGAIN;
274}
275
276static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
277{
278 struct ftmac100_data *priv = dev_get_priv(dev);
279 _ftmac100_free_pkt(priv);
280 return 0;
281}
282
283int ftmac100_read_rom_hwaddr(struct udevice *dev)
284{
Simon Glassfa20e932020-12-03 16:55:20 -0700285 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass399a9ce2017-08-03 12:22:14 -0600286 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickdc24dac2017-05-23 13:48:27 +0800287 return 0;
288}
289
290static const char *dtbmacaddr(u32 ifno)
291{
292 int node, len;
293 char enet[16];
294 const char *mac;
295 const char *path;
296 if (gd->fdt_blob == NULL) {
297 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
298 return NULL;
299 }
300 node = fdt_path_offset(gd->fdt_blob, "/aliases");
301 if (node < 0)
302 return NULL;
303
304 sprintf(enet, "ethernet%d", ifno);
305 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
306 if (!path) {
307 printf("no alias for %s\n", enet);
308 return NULL;
309 }
310 node = fdt_path_offset(gd->fdt_blob, path);
311 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
312 if (mac && is_valid_ethaddr((u8 *)mac))
313 return mac;
314
315 return NULL;
316}
317
Simon Glassaad29ae2020-12-03 16:55:21 -0700318static int ftmac100_of_to_plat(struct udevice *dev)
rickdc24dac2017-05-23 13:48:27 +0800319{
320 struct ftmac100_data *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700321 struct eth_pdata *pdata = dev_get_plat(dev);
rickdc24dac2017-05-23 13:48:27 +0800322 const char *mac;
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900323 pdata->iobase = dev_read_addr(dev);
Sergei Antonov2f69cb02023-02-03 22:09:03 +0300324 priv->ftmac100 = phys_to_virt(pdata->iobase);
rickdc24dac2017-05-23 13:48:27 +0800325 mac = dtbmacaddr(0);
326 if (mac)
327 memcpy(pdata->enetaddr , mac , 6);
328
329 return 0;
330}
331
Sergei Antonovb8a0ab92023-02-03 22:09:04 +0300332/*
333 * struct mii_bus functions
334 */
335static int ftmac100_mdio_read(struct mii_dev *bus, int addr, int devad,
336 int reg)
337{
338 struct ftmac100_data *priv = bus->priv;
339 struct ftmac100 *ftmac100 = priv->ftmac100;
340 int phycr = FTMAC100_PHYCR_PHYAD(addr) |
341 FTMAC100_PHYCR_REGAD(reg) |
342 FTMAC100_PHYCR_MIIRD;
343 int ret;
344
345 writel(phycr, &ftmac100->phycr);
346
347 ret = readl_poll_timeout(&ftmac100->phycr, phycr,
348 !(phycr & FTMAC100_PHYCR_MIIRD),
349 FTMAC100_MDIO_TIMEOUT_USEC);
350 if (ret)
351 pr_err("%s: mdio read failed (addr=0x%x reg=0x%x)\n",
352 bus->name, addr, reg);
353 else
354 ret = phycr & FTMAC100_PHYCR_MIIRDATA;
355
356 return ret;
357}
358
359static int ftmac100_mdio_write(struct mii_dev *bus, int addr, int devad,
360 int reg, u16 value)
361{
362 struct ftmac100_data *priv = bus->priv;
363 struct ftmac100 *ftmac100 = priv->ftmac100;
364 int phycr = FTMAC100_PHYCR_PHYAD(addr) |
365 FTMAC100_PHYCR_REGAD(reg) |
366 FTMAC100_PHYCR_MIIWR;
367 int ret;
368
369 writel(value, &ftmac100->phywdata);
370 writel(phycr, &ftmac100->phycr);
371
372 ret = readl_poll_timeout(&ftmac100->phycr, phycr,
373 !(phycr & FTMAC100_PHYCR_MIIWR),
374 FTMAC100_MDIO_TIMEOUT_USEC);
375 if (ret)
376 pr_err("%s: mdio write failed (addr=0x%x reg=0x%x)\n",
377 bus->name, addr, reg);
378
379 return ret;
380}
381
382static int ftmac100_mdio_init(struct udevice *dev)
383{
384 struct ftmac100_data *priv = dev_get_priv(dev);
385 struct mii_dev *bus;
386 int ret;
387
388 bus = mdio_alloc();
389 if (!bus)
390 return -ENOMEM;
391
392 bus->read = ftmac100_mdio_read;
393 bus->write = ftmac100_mdio_write;
394 bus->priv = priv;
395
396 ret = mdio_register_seq(bus, dev_seq(dev));
397 if (ret) {
398 mdio_free(bus);
399 return ret;
400 }
401
402 priv->bus = bus;
403
404 return 0;
405}
406
rickdc24dac2017-05-23 13:48:27 +0800407static int ftmac100_probe(struct udevice *dev)
408{
409 struct ftmac100_data *priv = dev_get_priv(dev);
410 priv->name = dev->name;
Sergei Antonovb8a0ab92023-02-03 22:09:04 +0300411 int ret = 0;
412
413 ret = ftmac100_mdio_init(dev);
414 if (ret) {
415 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
416 goto out;
417 }
418
419out:
420 return ret;
421}
422
423static int ftmac100_remove(struct udevice *dev)
424{
425 struct ftmac100_data *priv = dev_get_priv(dev);
426
427 mdio_unregister(priv->bus);
428 mdio_free(priv->bus);
429
rickdc24dac2017-05-23 13:48:27 +0800430 return 0;
431}
432
433static int ftmac100_bind(struct udevice *dev)
434{
435 return device_set_name(dev, dev->name);
436}
437
438static const struct eth_ops ftmac100_ops = {
439 .start = ftmac100_start,
440 .send = ftmac100_send,
441 .recv = ftmac100_recv,
442 .stop = ftmac100_stop,
443 .free_pkt = ftmac100_free_pkt,
444};
445
446static const struct udevice_id ftmac100_ids[] = {
447 { .compatible = "andestech,atmac100" },
448 { }
449};
450
451U_BOOT_DRIVER(ftmac100) = {
Sergei Antonov0e15edb2023-02-03 22:09:02 +0300452 .name = "ftmac100",
rickdc24dac2017-05-23 13:48:27 +0800453 .id = UCLASS_ETH,
454 .of_match = ftmac100_ids,
455 .bind = ftmac100_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700456 .of_to_plat = ftmac100_of_to_plat,
rickdc24dac2017-05-23 13:48:27 +0800457 .probe = ftmac100_probe,
Sergei Antonovb8a0ab92023-02-03 22:09:04 +0300458 .remove = ftmac100_remove,
rickdc24dac2017-05-23 13:48:27 +0800459 .ops = &ftmac100_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700460 .priv_auto = sizeof(struct ftmac100_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700461 .plat_auto = sizeof(struct eth_pdata),
rickdc24dac2017-05-23 13:48:27 +0800462 .flags = DM_FLAG_ALLOC_PRIV_DMA,
463};