blob: c30ace96bb1346baef1687873dc69aa7965de5f4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Po-Yu Chuang750326e2009-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 Chuang750326e2009-08-10 11:00:00 +08007 */
8
9#include <config.h>
10#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass7b51b572019-08-01 09:46:52 -060012#include <env.h>
Po-Yu Chuang750326e2009-08-10 11:00:00 +080013#include <malloc.h>
14#include <net.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
rickbe71a172017-05-23 13:48:27 +080017#include <linux/io.h>
Po-Yu Chuang750326e2009-08-10 11:00:00 +080018
19#include "ftmac100.h"
rickbe71a172017-05-23 13:48:27 +080020#ifdef CONFIG_DM_ETH
21#include <dm.h>
22DECLARE_GLOBAL_DATA_PTR;
23#endif
Po-Yu Chuang750326e2009-08-10 11:00:00 +080024#define ETH_ZLEN 60
25
26struct ftmac100_data {
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +080027 struct ftmac100_txdes txdes[1];
28 struct ftmac100_rxdes rxdes[PKTBUFSRX];
Po-Yu Chuang750326e2009-08-10 11:00:00 +080029 int rx_index;
rickbe71a172017-05-23 13:48:27 +080030 const char *name;
31 phys_addr_t iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080032};
33
34/*
35 * Reset MAC
36 */
rickbe71a172017-05-23 13:48:27 +080037static void ftmac100_reset(struct ftmac100_data *priv)
Po-Yu Chuang750326e2009-08-10 11:00:00 +080038{
Bin Meng673625c2021-01-31 20:36:02 +080039 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080040
41 debug ("%s()\n", __func__);
42
43 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
44
45 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
rick13414942017-08-29 10:15:05 +080046 mdelay(1);
47 /*
48 * When soft reset complete, write mac address immediately maybe fail somehow
49 * Wait for a while can avoid this problem
50 */
51 mdelay(1);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080052}
53
54/*
55 * Set MAC address
56 */
rickbe71a172017-05-23 13:48:27 +080057static void ftmac100_set_mac(struct ftmac100_data *priv ,
58 const unsigned char *mac)
Po-Yu Chuang750326e2009-08-10 11:00:00 +080059{
Bin Meng673625c2021-01-31 20:36:02 +080060 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080061 unsigned int maddr = mac[0] << 8 | mac[1];
62 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
63
64 debug ("%s(%x %x)\n", __func__, maddr, laddr);
65
66 writel (maddr, &ftmac100->mac_madr);
67 writel (laddr, &ftmac100->mac_ladr);
68}
69
Po-Yu Chuang750326e2009-08-10 11:00:00 +080070/*
rickbe71a172017-05-23 13:48:27 +080071 * Disable MAC
Po-Yu Chuang750326e2009-08-10 11:00:00 +080072 */
rickbe71a172017-05-23 13:48:27 +080073static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang750326e2009-08-10 11:00:00 +080074{
Bin Meng673625c2021-01-31 20:36:02 +080075 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080076 debug ("%s()\n", __func__);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080077 writel (0, &ftmac100->maccr);
78}
79
rickbe71a172017-05-23 13:48:27 +080080/*
81 * Initialize MAC
82 */
83static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang750326e2009-08-10 11:00:00 +080084{
Bin Meng673625c2021-01-31 20:36:02 +080085 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +080086 struct ftmac100_txdes *txdes = priv->txdes;
87 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080088 unsigned int maccr;
89 int i;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080090 debug ("%s()\n", __func__);
91
rickbe71a172017-05-23 13:48:27 +080092 ftmac100_reset(priv);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080093
94 /* set the ethernet address */
rickbe71a172017-05-23 13:48:27 +080095 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080096
Po-Yu Chuang750326e2009-08-10 11:00:00 +080097
98 /* disable all interrupts */
99
100 writel (0, &ftmac100->imr);
101
102 /* initialize descriptors */
103
104 priv->rx_index = 0;
105
106 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
107 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
108
109 for (i = 0; i < PKTBUFSRX; i++) {
110 /* RXBUF_BADR */
Rick Chen28b52a62018-05-29 10:42:19 +0800111 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800112 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
113 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
114 }
115
116 /* transmit ring */
117
Rick Chen28b52a62018-05-29 10:42:19 +0800118 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800119
120 /* receive ring */
121
Rick Chen28b52a62018-05-29 10:42:19 +0800122 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800123
124 /* poll receive descriptor automatically */
125
126 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
127
128 /* enable transmitter, receiver */
129
130 maccr = FTMAC100_MACCR_XMT_EN |
131 FTMAC100_MACCR_RCV_EN |
132 FTMAC100_MACCR_XDMA_EN |
133 FTMAC100_MACCR_RDMA_EN |
134 FTMAC100_MACCR_CRC_APD |
135 FTMAC100_MACCR_ENRX_IN_HALFTX |
136 FTMAC100_MACCR_RX_RUNT |
137 FTMAC100_MACCR_RX_BROADPKT;
138
139 writel (maccr, &ftmac100->maccr);
140
141 return 0;
142}
143
144/*
rickbe71a172017-05-23 13:48:27 +0800145 * Free receiving buffer
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800146 */
rickbe71a172017-05-23 13:48:27 +0800147static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800148{
rickbe71a172017-05-23 13:48:27 +0800149 struct ftmac100_rxdes *curr_des;
150 curr_des = &priv->rxdes[priv->rx_index];
151 /* release buffer to DMA */
152 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
153 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
154 return 0;
155}
156
157/*
158 * Receive a data block via Ethernet
159 */
160static int __ftmac100_recv(struct ftmac100_data *priv)
161{
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +0800162 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800163 unsigned short rxlen;
164
165 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800166 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickbe71a172017-05-23 13:48:27 +0800167 return 0;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800168
169 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
170 FTMAC100_RXDES0_CRC_ERR |
171 FTMAC100_RXDES0_FTL |
172 FTMAC100_RXDES0_RUNT |
173 FTMAC100_RXDES0_RX_ODD_NB)) {
rickbe71a172017-05-23 13:48:27 +0800174 return 0;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800175 }
176
177 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickce4e2372017-08-29 10:09:00 +0800178 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800179 debug ("%s(): RX buffer %d, %x received\n",
180 __func__, priv->rx_index, rxlen);
181
rickbe71a172017-05-23 13:48:27 +0800182 return rxlen;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800183}
184
185/*
186 * Send a data block via Ethernet
187 */
rickbe71a172017-05-23 13:48:27 +0800188static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800189{
Bin Meng673625c2021-01-31 20:36:02 +0800190 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +0800191 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang8d8fd5b2011-01-21 15:51:25 +0800192 ulong start;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800193
194 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
195 debug ("%s(): no TX descriptor available\n", __func__);
196 return -1;
197 }
198
Rick Chen28b52a62018-05-29 10:42:19 +0800199 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800200
201 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
202
203 /* initiate a transmit sequence */
204
Rick Chen28b52a62018-05-29 10:42:19 +0800205 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
206 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800207
208 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
209 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
210 FTMAC100_TXDES1_LTS |
211 FTMAC100_TXDES1_TXBUF_SIZE (length);
212
213 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
214
215 /* start transmit */
216
217 writel (1, &ftmac100->txpd);
218
219 /* wait for transfer to succeed */
220
Po-Yu Chuang8d8fd5b2011-01-21 15:51:25 +0800221 start = get_timer(0);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800222 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang8d8fd5b2011-01-21 15:51:25 +0800223 if (get_timer(start) >= 5) {
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800224 debug ("%s(): timed out\n", __func__);
225 return -1;
226 }
227 }
228
229 debug ("%s(): packet sent\n", __func__);
230
231 return 0;
232}
233
rickbe71a172017-05-23 13:48:27 +0800234#ifndef CONFIG_DM_ETH
235/*
236 * disable transmitter, receiver
237 */
238static void ftmac100_halt(struct eth_device *dev)
239{
240 struct ftmac100_data *priv = dev->priv;
241 return _ftmac100_halt(priv);
242}
243
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900244static int ftmac100_init(struct eth_device *dev, struct bd_info *bd)
rickbe71a172017-05-23 13:48:27 +0800245{
246 struct ftmac100_data *priv = dev->priv;
247 return _ftmac100_init(priv , dev->enetaddr);
248}
249
250static int _ftmac100_recv(struct ftmac100_data *priv)
251{
252 struct ftmac100_rxdes *curr_des;
253 unsigned short len;
254 curr_des = &priv->rxdes[priv->rx_index];
255 len = __ftmac100_recv(priv);
256 if (len) {
257 /* pass the packet up to the protocol layers. */
258 net_process_received_packet((void *)curr_des->rxdes2, len);
259 _ftmac100_free_pkt(priv);
260 }
261 return len ? 1 : 0;
262}
263
264/*
265 * Get a data block via Ethernet
266 */
267static int ftmac100_recv(struct eth_device *dev)
268{
269 struct ftmac100_data *priv = dev->priv;
270 return _ftmac100_recv(priv);
271}
272
273/*
274 * Send a data block via Ethernet
275 */
276static int ftmac100_send(struct eth_device *dev, void *packet, int length)
277{
278 struct ftmac100_data *priv = dev->priv;
279 return _ftmac100_send(priv , packet , length);
280}
281
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900282int ftmac100_initialize (struct bd_info *bd)
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800283{
284 struct eth_device *dev;
285 struct ftmac100_data *priv;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800286 dev = malloc (sizeof *dev);
287 if (!dev) {
288 printf ("%s(): failed to allocate dev\n", __func__);
289 goto out;
290 }
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800291 /* Transmit and receive descriptors should align to 16 bytes */
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800292 priv = memalign (16, sizeof (struct ftmac100_data));
293 if (!priv) {
294 printf ("%s(): failed to allocate priv\n", __func__);
295 goto free_dev;
296 }
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800297 memset (dev, 0, sizeof (*dev));
298 memset (priv, 0, sizeof (*priv));
299
Ben Whitten192bc692015-12-30 13:05:58 +0000300 strcpy(dev->name, "FTMAC100");
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800301 dev->iobase = CONFIG_FTMAC100_BASE;
302 dev->init = ftmac100_init;
303 dev->halt = ftmac100_halt;
304 dev->send = ftmac100_send;
305 dev->recv = ftmac100_recv;
306 dev->priv = priv;
rickbe71a172017-05-23 13:48:27 +0800307 priv->iobase = dev->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800308 eth_register (dev);
309
310 return 1;
311
312free_dev:
313 free (dev);
314out:
315 return 0;
316}
rickbe71a172017-05-23 13:48:27 +0800317#endif
318
319#ifdef CONFIG_DM_ETH
320static int ftmac100_start(struct udevice *dev)
321{
Simon Glassc69cda22020-12-03 16:55:20 -0700322 struct eth_pdata *plat = dev_get_plat(dev);
rickbe71a172017-05-23 13:48:27 +0800323 struct ftmac100_data *priv = dev_get_priv(dev);
324
325 return _ftmac100_init(priv, plat->enetaddr);
326}
327
328static void ftmac100_stop(struct udevice *dev)
329{
330 struct ftmac100_data *priv = dev_get_priv(dev);
331 _ftmac100_halt(priv);
332}
333
334static int ftmac100_send(struct udevice *dev, void *packet, int length)
335{
336 struct ftmac100_data *priv = dev_get_priv(dev);
337 int ret;
338 ret = _ftmac100_send(priv , packet , length);
339 return ret ? 0 : -ETIMEDOUT;
340}
341
342static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
343{
344 struct ftmac100_data *priv = dev_get_priv(dev);
345 struct ftmac100_rxdes *curr_des;
346 curr_des = &priv->rxdes[priv->rx_index];
347 int len;
348 len = __ftmac100_recv(priv);
349 if (len)
Rick Chen28b52a62018-05-29 10:42:19 +0800350 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickbe71a172017-05-23 13:48:27 +0800351
352 return len ? len : -EAGAIN;
353}
354
355static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
356{
357 struct ftmac100_data *priv = dev_get_priv(dev);
358 _ftmac100_free_pkt(priv);
359 return 0;
360}
361
362int ftmac100_read_rom_hwaddr(struct udevice *dev)
363{
Simon Glassc69cda22020-12-03 16:55:20 -0700364 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass35affd72017-08-03 12:22:14 -0600365 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickbe71a172017-05-23 13:48:27 +0800366 return 0;
367}
368
369static const char *dtbmacaddr(u32 ifno)
370{
371 int node, len;
372 char enet[16];
373 const char *mac;
374 const char *path;
375 if (gd->fdt_blob == NULL) {
376 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
377 return NULL;
378 }
379 node = fdt_path_offset(gd->fdt_blob, "/aliases");
380 if (node < 0)
381 return NULL;
382
383 sprintf(enet, "ethernet%d", ifno);
384 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
385 if (!path) {
386 printf("no alias for %s\n", enet);
387 return NULL;
388 }
389 node = fdt_path_offset(gd->fdt_blob, path);
390 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
391 if (mac && is_valid_ethaddr((u8 *)mac))
392 return mac;
393
394 return NULL;
395}
396
Simon Glassd1998a92020-12-03 16:55:21 -0700397static int ftmac100_of_to_plat(struct udevice *dev)
rickbe71a172017-05-23 13:48:27 +0800398{
399 struct ftmac100_data *priv = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700400 struct eth_pdata *pdata = dev_get_plat(dev);
rickbe71a172017-05-23 13:48:27 +0800401 const char *mac;
Masahiro Yamada25484932020-07-17 14:36:48 +0900402 pdata->iobase = dev_read_addr(dev);
rickbe71a172017-05-23 13:48:27 +0800403 priv->iobase = pdata->iobase;
404 mac = dtbmacaddr(0);
405 if (mac)
406 memcpy(pdata->enetaddr , mac , 6);
407
408 return 0;
409}
410
411static int ftmac100_probe(struct udevice *dev)
412{
413 struct ftmac100_data *priv = dev_get_priv(dev);
414 priv->name = dev->name;
415 return 0;
416}
417
418static int ftmac100_bind(struct udevice *dev)
419{
420 return device_set_name(dev, dev->name);
421}
422
423static const struct eth_ops ftmac100_ops = {
424 .start = ftmac100_start,
425 .send = ftmac100_send,
426 .recv = ftmac100_recv,
427 .stop = ftmac100_stop,
428 .free_pkt = ftmac100_free_pkt,
429};
430
431static const struct udevice_id ftmac100_ids[] = {
432 { .compatible = "andestech,atmac100" },
433 { }
434};
435
436U_BOOT_DRIVER(ftmac100) = {
437 .name = "nds32_mac",
438 .id = UCLASS_ETH,
439 .of_match = ftmac100_ids,
440 .bind = ftmac100_bind,
Simon Glassd1998a92020-12-03 16:55:21 -0700441 .of_to_plat = ftmac100_of_to_plat,
rickbe71a172017-05-23 13:48:27 +0800442 .probe = ftmac100_probe,
443 .ops = &ftmac100_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700444 .priv_auto = sizeof(struct ftmac100_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700445 .plat_auto = sizeof(struct eth_pdata),
rickbe71a172017-05-23 13:48:27 +0800446 .flags = DM_FLAG_ALLOC_PRIV_DMA,
447};
448#endif