blob: 5c2887ec8ae930c67b979239c6dcb8340ecdce51 [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 Glassc05ed002020-05-10 11:40:11 -060015#include <linux/delay.h>
rickbe71a172017-05-23 13:48:27 +080016#include <linux/io.h>
Po-Yu Chuang750326e2009-08-10 11:00:00 +080017
18#include "ftmac100.h"
rickbe71a172017-05-23 13:48:27 +080019#ifdef CONFIG_DM_ETH
20#include <dm.h>
21DECLARE_GLOBAL_DATA_PTR;
22#endif
Po-Yu Chuang750326e2009-08-10 11:00:00 +080023#define ETH_ZLEN 60
24
25struct ftmac100_data {
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +080026 struct ftmac100_txdes txdes[1];
27 struct ftmac100_rxdes rxdes[PKTBUFSRX];
Po-Yu Chuang750326e2009-08-10 11:00:00 +080028 int rx_index;
rickbe71a172017-05-23 13:48:27 +080029 const char *name;
30 phys_addr_t iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080031};
32
33/*
34 * Reset MAC
35 */
rickbe71a172017-05-23 13:48:27 +080036static void ftmac100_reset(struct ftmac100_data *priv)
Po-Yu Chuang750326e2009-08-10 11:00:00 +080037{
rickbe71a172017-05-23 13:48:27 +080038 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080039
40 debug ("%s()\n", __func__);
41
42 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
43
44 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
rick13414942017-08-29 10:15:05 +080045 mdelay(1);
46 /*
47 * When soft reset complete, write mac address immediately maybe fail somehow
48 * Wait for a while can avoid this problem
49 */
50 mdelay(1);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080051}
52
53/*
54 * Set MAC address
55 */
rickbe71a172017-05-23 13:48:27 +080056static void ftmac100_set_mac(struct ftmac100_data *priv ,
57 const unsigned char *mac)
Po-Yu Chuang750326e2009-08-10 11:00:00 +080058{
rickbe71a172017-05-23 13:48:27 +080059 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080060 unsigned int maddr = mac[0] << 8 | mac[1];
61 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
62
63 debug ("%s(%x %x)\n", __func__, maddr, laddr);
64
65 writel (maddr, &ftmac100->mac_madr);
66 writel (laddr, &ftmac100->mac_ladr);
67}
68
Po-Yu Chuang750326e2009-08-10 11:00:00 +080069/*
rickbe71a172017-05-23 13:48:27 +080070 * Disable MAC
Po-Yu Chuang750326e2009-08-10 11:00:00 +080071 */
rickbe71a172017-05-23 13:48:27 +080072static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang750326e2009-08-10 11:00:00 +080073{
rickbe71a172017-05-23 13:48:27 +080074 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080075 debug ("%s()\n", __func__);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080076 writel (0, &ftmac100->maccr);
77}
78
rickbe71a172017-05-23 13:48:27 +080079/*
80 * Initialize MAC
81 */
82static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang750326e2009-08-10 11:00:00 +080083{
rickbe71a172017-05-23 13:48:27 +080084 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +080085 struct ftmac100_txdes *txdes = priv->txdes;
86 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080087 unsigned int maccr;
88 int i;
Po-Yu Chuang750326e2009-08-10 11:00:00 +080089 debug ("%s()\n", __func__);
90
rickbe71a172017-05-23 13:48:27 +080091 ftmac100_reset(priv);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080092
93 /* set the ethernet address */
rickbe71a172017-05-23 13:48:27 +080094 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang750326e2009-08-10 11:00:00 +080095
Po-Yu Chuang750326e2009-08-10 11:00:00 +080096
97 /* disable all interrupts */
98
99 writel (0, &ftmac100->imr);
100
101 /* initialize descriptors */
102
103 priv->rx_index = 0;
104
105 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
106 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
107
108 for (i = 0; i < PKTBUFSRX; i++) {
109 /* RXBUF_BADR */
Rick Chen28b52a62018-05-29 10:42:19 +0800110 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800111 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
112 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
113 }
114
115 /* transmit ring */
116
Rick Chen28b52a62018-05-29 10:42:19 +0800117 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800118
119 /* receive ring */
120
Rick Chen28b52a62018-05-29 10:42:19 +0800121 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800122
123 /* poll receive descriptor automatically */
124
125 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
126
127 /* enable transmitter, receiver */
128
129 maccr = FTMAC100_MACCR_XMT_EN |
130 FTMAC100_MACCR_RCV_EN |
131 FTMAC100_MACCR_XDMA_EN |
132 FTMAC100_MACCR_RDMA_EN |
133 FTMAC100_MACCR_CRC_APD |
134 FTMAC100_MACCR_ENRX_IN_HALFTX |
135 FTMAC100_MACCR_RX_RUNT |
136 FTMAC100_MACCR_RX_BROADPKT;
137
138 writel (maccr, &ftmac100->maccr);
139
140 return 0;
141}
142
143/*
rickbe71a172017-05-23 13:48:27 +0800144 * Free receiving buffer
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800145 */
rickbe71a172017-05-23 13:48:27 +0800146static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800147{
rickbe71a172017-05-23 13:48:27 +0800148 struct ftmac100_rxdes *curr_des;
149 curr_des = &priv->rxdes[priv->rx_index];
150 /* release buffer to DMA */
151 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
152 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
153 return 0;
154}
155
156/*
157 * Receive a data block via Ethernet
158 */
159static int __ftmac100_recv(struct ftmac100_data *priv)
160{
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +0800161 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800162 unsigned short rxlen;
163
164 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800165 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickbe71a172017-05-23 13:48:27 +0800166 return 0;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800167
168 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
169 FTMAC100_RXDES0_CRC_ERR |
170 FTMAC100_RXDES0_FTL |
171 FTMAC100_RXDES0_RUNT |
172 FTMAC100_RXDES0_RX_ODD_NB)) {
rickbe71a172017-05-23 13:48:27 +0800173 return 0;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800174 }
175
176 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickce4e2372017-08-29 10:09:00 +0800177 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800178 debug ("%s(): RX buffer %d, %x received\n",
179 __func__, priv->rx_index, rxlen);
180
rickbe71a172017-05-23 13:48:27 +0800181 return rxlen;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800182}
183
184/*
185 * Send a data block via Ethernet
186 */
rickbe71a172017-05-23 13:48:27 +0800187static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800188{
rickbe71a172017-05-23 13:48:27 +0800189 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang6f6e6e02011-01-21 15:50:37 +0800190 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang8d8fd5b2011-01-21 15:51:25 +0800191 ulong start;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800192
193 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
194 debug ("%s(): no TX descriptor available\n", __func__);
195 return -1;
196 }
197
Rick Chen28b52a62018-05-29 10:42:19 +0800198 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800199
200 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
201
202 /* initiate a transmit sequence */
203
Rick Chen28b52a62018-05-29 10:42:19 +0800204 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
205 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800206
207 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
208 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
209 FTMAC100_TXDES1_LTS |
210 FTMAC100_TXDES1_TXBUF_SIZE (length);
211
212 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
213
214 /* start transmit */
215
216 writel (1, &ftmac100->txpd);
217
218 /* wait for transfer to succeed */
219
Po-Yu Chuang8d8fd5b2011-01-21 15:51:25 +0800220 start = get_timer(0);
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800221 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang8d8fd5b2011-01-21 15:51:25 +0800222 if (get_timer(start) >= 5) {
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800223 debug ("%s(): timed out\n", __func__);
224 return -1;
225 }
226 }
227
228 debug ("%s(): packet sent\n", __func__);
229
230 return 0;
231}
232
rickbe71a172017-05-23 13:48:27 +0800233#ifndef CONFIG_DM_ETH
234/*
235 * disable transmitter, receiver
236 */
237static void ftmac100_halt(struct eth_device *dev)
238{
239 struct ftmac100_data *priv = dev->priv;
240 return _ftmac100_halt(priv);
241}
242
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900243static int ftmac100_init(struct eth_device *dev, struct bd_info *bd)
rickbe71a172017-05-23 13:48:27 +0800244{
245 struct ftmac100_data *priv = dev->priv;
246 return _ftmac100_init(priv , dev->enetaddr);
247}
248
249static int _ftmac100_recv(struct ftmac100_data *priv)
250{
251 struct ftmac100_rxdes *curr_des;
252 unsigned short len;
253 curr_des = &priv->rxdes[priv->rx_index];
254 len = __ftmac100_recv(priv);
255 if (len) {
256 /* pass the packet up to the protocol layers. */
257 net_process_received_packet((void *)curr_des->rxdes2, len);
258 _ftmac100_free_pkt(priv);
259 }
260 return len ? 1 : 0;
261}
262
263/*
264 * Get a data block via Ethernet
265 */
266static int ftmac100_recv(struct eth_device *dev)
267{
268 struct ftmac100_data *priv = dev->priv;
269 return _ftmac100_recv(priv);
270}
271
272/*
273 * Send a data block via Ethernet
274 */
275static int ftmac100_send(struct eth_device *dev, void *packet, int length)
276{
277 struct ftmac100_data *priv = dev->priv;
278 return _ftmac100_send(priv , packet , length);
279}
280
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900281int ftmac100_initialize (struct bd_info *bd)
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800282{
283 struct eth_device *dev;
284 struct ftmac100_data *priv;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800285 dev = malloc (sizeof *dev);
286 if (!dev) {
287 printf ("%s(): failed to allocate dev\n", __func__);
288 goto out;
289 }
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800290 /* Transmit and receive descriptors should align to 16 bytes */
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800291 priv = memalign (16, sizeof (struct ftmac100_data));
292 if (!priv) {
293 printf ("%s(): failed to allocate priv\n", __func__);
294 goto free_dev;
295 }
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800296 memset (dev, 0, sizeof (*dev));
297 memset (priv, 0, sizeof (*priv));
298
Ben Whitten192bc692015-12-30 13:05:58 +0000299 strcpy(dev->name, "FTMAC100");
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800300 dev->iobase = CONFIG_FTMAC100_BASE;
301 dev->init = ftmac100_init;
302 dev->halt = ftmac100_halt;
303 dev->send = ftmac100_send;
304 dev->recv = ftmac100_recv;
305 dev->priv = priv;
rickbe71a172017-05-23 13:48:27 +0800306 priv->iobase = dev->iobase;
Po-Yu Chuang750326e2009-08-10 11:00:00 +0800307 eth_register (dev);
308
309 return 1;
310
311free_dev:
312 free (dev);
313out:
314 return 0;
315}
rickbe71a172017-05-23 13:48:27 +0800316#endif
317
318#ifdef CONFIG_DM_ETH
319static int ftmac100_start(struct udevice *dev)
320{
Simon Glassc69cda22020-12-03 16:55:20 -0700321 struct eth_pdata *plat = dev_get_plat(dev);
rickbe71a172017-05-23 13:48:27 +0800322 struct ftmac100_data *priv = dev_get_priv(dev);
323
324 return _ftmac100_init(priv, plat->enetaddr);
325}
326
327static void ftmac100_stop(struct udevice *dev)
328{
329 struct ftmac100_data *priv = dev_get_priv(dev);
330 _ftmac100_halt(priv);
331}
332
333static int ftmac100_send(struct udevice *dev, void *packet, int length)
334{
335 struct ftmac100_data *priv = dev_get_priv(dev);
336 int ret;
337 ret = _ftmac100_send(priv , packet , length);
338 return ret ? 0 : -ETIMEDOUT;
339}
340
341static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
342{
343 struct ftmac100_data *priv = dev_get_priv(dev);
344 struct ftmac100_rxdes *curr_des;
345 curr_des = &priv->rxdes[priv->rx_index];
346 int len;
347 len = __ftmac100_recv(priv);
348 if (len)
Rick Chen28b52a62018-05-29 10:42:19 +0800349 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickbe71a172017-05-23 13:48:27 +0800350
351 return len ? len : -EAGAIN;
352}
353
354static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
355{
356 struct ftmac100_data *priv = dev_get_priv(dev);
357 _ftmac100_free_pkt(priv);
358 return 0;
359}
360
361int ftmac100_read_rom_hwaddr(struct udevice *dev)
362{
Simon Glassc69cda22020-12-03 16:55:20 -0700363 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass35affd72017-08-03 12:22:14 -0600364 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickbe71a172017-05-23 13:48:27 +0800365 return 0;
366}
367
368static const char *dtbmacaddr(u32 ifno)
369{
370 int node, len;
371 char enet[16];
372 const char *mac;
373 const char *path;
374 if (gd->fdt_blob == NULL) {
375 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
376 return NULL;
377 }
378 node = fdt_path_offset(gd->fdt_blob, "/aliases");
379 if (node < 0)
380 return NULL;
381
382 sprintf(enet, "ethernet%d", ifno);
383 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
384 if (!path) {
385 printf("no alias for %s\n", enet);
386 return NULL;
387 }
388 node = fdt_path_offset(gd->fdt_blob, path);
389 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
390 if (mac && is_valid_ethaddr((u8 *)mac))
391 return mac;
392
393 return NULL;
394}
395
396static int ftmac100_ofdata_to_platdata(struct udevice *dev)
397{
398 struct ftmac100_data *priv = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700399 struct eth_pdata *pdata = dev_get_plat(dev);
rickbe71a172017-05-23 13:48:27 +0800400 const char *mac;
Masahiro Yamada25484932020-07-17 14:36:48 +0900401 pdata->iobase = dev_read_addr(dev);
rickbe71a172017-05-23 13:48:27 +0800402 priv->iobase = pdata->iobase;
403 mac = dtbmacaddr(0);
404 if (mac)
405 memcpy(pdata->enetaddr , mac , 6);
406
407 return 0;
408}
409
410static int ftmac100_probe(struct udevice *dev)
411{
412 struct ftmac100_data *priv = dev_get_priv(dev);
413 priv->name = dev->name;
414 return 0;
415}
416
417static int ftmac100_bind(struct udevice *dev)
418{
419 return device_set_name(dev, dev->name);
420}
421
422static const struct eth_ops ftmac100_ops = {
423 .start = ftmac100_start,
424 .send = ftmac100_send,
425 .recv = ftmac100_recv,
426 .stop = ftmac100_stop,
427 .free_pkt = ftmac100_free_pkt,
428};
429
430static const struct udevice_id ftmac100_ids[] = {
431 { .compatible = "andestech,atmac100" },
432 { }
433};
434
435U_BOOT_DRIVER(ftmac100) = {
436 .name = "nds32_mac",
437 .id = UCLASS_ETH,
438 .of_match = ftmac100_ids,
439 .bind = ftmac100_bind,
440 .ofdata_to_platdata = ftmac100_ofdata_to_platdata,
441 .probe = ftmac100_probe,
442 .ops = &ftmac100_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700443 .priv_auto = sizeof(struct ftmac100_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700444 .plat_auto = sizeof(struct eth_pdata),
rickbe71a172017-05-23 13:48:27 +0800445 .flags = DM_FLAG_ALLOC_PRIV_DMA,
446};
447#endif