blob: 24bb45f351530f424f5452609f638ed00cc2d7de [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>
10#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Po-Yu Chuang50253b82009-08-10 11:00:00 +080013#include <malloc.h>
14#include <net.h>
rickdc24dac2017-05-23 13:48:27 +080015#include <linux/io.h>
Po-Yu Chuang50253b82009-08-10 11:00:00 +080016
17#include "ftmac100.h"
rickdc24dac2017-05-23 13:48:27 +080018#ifdef CONFIG_DM_ETH
19#include <dm.h>
20DECLARE_GLOBAL_DATA_PTR;
21#endif
Po-Yu Chuang50253b82009-08-10 11:00:00 +080022#define ETH_ZLEN 60
23
24struct ftmac100_data {
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080025 struct ftmac100_txdes txdes[1];
26 struct ftmac100_rxdes rxdes[PKTBUFSRX];
Po-Yu Chuang50253b82009-08-10 11:00:00 +080027 int rx_index;
rickdc24dac2017-05-23 13:48:27 +080028 const char *name;
29 phys_addr_t iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080030};
31
32/*
33 * Reset MAC
34 */
rickdc24dac2017-05-23 13:48:27 +080035static void ftmac100_reset(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080036{
rickdc24dac2017-05-23 13:48:27 +080037 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080038
39 debug ("%s()\n", __func__);
40
41 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
42
43 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
rick54966612017-08-29 10:15:05 +080044 mdelay(1);
45 /*
46 * When soft reset complete, write mac address immediately maybe fail somehow
47 * Wait for a while can avoid this problem
48 */
49 mdelay(1);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080050}
51
52/*
53 * Set MAC address
54 */
rickdc24dac2017-05-23 13:48:27 +080055static void ftmac100_set_mac(struct ftmac100_data *priv ,
56 const unsigned char *mac)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080057{
rickdc24dac2017-05-23 13:48:27 +080058 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080059 unsigned int maddr = mac[0] << 8 | mac[1];
60 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
61
62 debug ("%s(%x %x)\n", __func__, maddr, laddr);
63
64 writel (maddr, &ftmac100->mac_madr);
65 writel (laddr, &ftmac100->mac_ladr);
66}
67
Po-Yu Chuang50253b82009-08-10 11:00:00 +080068/*
rickdc24dac2017-05-23 13:48:27 +080069 * Disable MAC
Po-Yu Chuang50253b82009-08-10 11:00:00 +080070 */
rickdc24dac2017-05-23 13:48:27 +080071static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080072{
rickdc24dac2017-05-23 13:48:27 +080073 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080074 debug ("%s()\n", __func__);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080075 writel (0, &ftmac100->maccr);
76}
77
rickdc24dac2017-05-23 13:48:27 +080078/*
79 * Initialize MAC
80 */
81static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang50253b82009-08-10 11:00:00 +080082{
rickdc24dac2017-05-23 13:48:27 +080083 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080084 struct ftmac100_txdes *txdes = priv->txdes;
85 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080086 unsigned int maccr;
87 int i;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080088 debug ("%s()\n", __func__);
89
rickdc24dac2017-05-23 13:48:27 +080090 ftmac100_reset(priv);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080091
92 /* set the ethernet address */
rickdc24dac2017-05-23 13:48:27 +080093 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080094
Po-Yu Chuang50253b82009-08-10 11:00:00 +080095
96 /* disable all interrupts */
97
98 writel (0, &ftmac100->imr);
99
100 /* initialize descriptors */
101
102 priv->rx_index = 0;
103
104 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
105 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
106
107 for (i = 0; i < PKTBUFSRX; i++) {
108 /* RXBUF_BADR */
Rick Chen59d15262018-05-29 10:42:19 +0800109 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800110 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
111 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
112 }
113
114 /* transmit ring */
115
Rick Chen59d15262018-05-29 10:42:19 +0800116 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800117
118 /* receive ring */
119
Rick Chen59d15262018-05-29 10:42:19 +0800120 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800121
122 /* poll receive descriptor automatically */
123
124 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
125
126 /* enable transmitter, receiver */
127
128 maccr = FTMAC100_MACCR_XMT_EN |
129 FTMAC100_MACCR_RCV_EN |
130 FTMAC100_MACCR_XDMA_EN |
131 FTMAC100_MACCR_RDMA_EN |
132 FTMAC100_MACCR_CRC_APD |
133 FTMAC100_MACCR_ENRX_IN_HALFTX |
134 FTMAC100_MACCR_RX_RUNT |
135 FTMAC100_MACCR_RX_BROADPKT;
136
137 writel (maccr, &ftmac100->maccr);
138
139 return 0;
140}
141
142/*
rickdc24dac2017-05-23 13:48:27 +0800143 * Free receiving buffer
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800144 */
rickdc24dac2017-05-23 13:48:27 +0800145static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800146{
rickdc24dac2017-05-23 13:48:27 +0800147 struct ftmac100_rxdes *curr_des;
148 curr_des = &priv->rxdes[priv->rx_index];
149 /* release buffer to DMA */
150 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
151 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
152 return 0;
153}
154
155/*
156 * Receive a data block via Ethernet
157 */
158static int __ftmac100_recv(struct ftmac100_data *priv)
159{
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800160 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800161 unsigned short rxlen;
162
163 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800164 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickdc24dac2017-05-23 13:48:27 +0800165 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800166
167 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
168 FTMAC100_RXDES0_CRC_ERR |
169 FTMAC100_RXDES0_FTL |
170 FTMAC100_RXDES0_RUNT |
171 FTMAC100_RXDES0_RX_ODD_NB)) {
rickdc24dac2017-05-23 13:48:27 +0800172 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800173 }
174
175 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickb0ce1822017-08-29 10:09:00 +0800176 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800177 debug ("%s(): RX buffer %d, %x received\n",
178 __func__, priv->rx_index, rxlen);
179
rickdc24dac2017-05-23 13:48:27 +0800180 return rxlen;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800181}
182
183/*
184 * Send a data block via Ethernet
185 */
rickdc24dac2017-05-23 13:48:27 +0800186static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800187{
rickdc24dac2017-05-23 13:48:27 +0800188 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800189 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800190 ulong start;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800191
192 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
193 debug ("%s(): no TX descriptor available\n", __func__);
194 return -1;
195 }
196
Rick Chen59d15262018-05-29 10:42:19 +0800197 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800198
199 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
200
201 /* initiate a transmit sequence */
202
Rick Chen59d15262018-05-29 10:42:19 +0800203 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
204 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800205
206 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
207 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
208 FTMAC100_TXDES1_LTS |
209 FTMAC100_TXDES1_TXBUF_SIZE (length);
210
211 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
212
213 /* start transmit */
214
215 writel (1, &ftmac100->txpd);
216
217 /* wait for transfer to succeed */
218
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800219 start = get_timer(0);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800220 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800221 if (get_timer(start) >= 5) {
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800222 debug ("%s(): timed out\n", __func__);
223 return -1;
224 }
225 }
226
227 debug ("%s(): packet sent\n", __func__);
228
229 return 0;
230}
231
rickdc24dac2017-05-23 13:48:27 +0800232#ifndef CONFIG_DM_ETH
233/*
234 * disable transmitter, receiver
235 */
236static void ftmac100_halt(struct eth_device *dev)
237{
238 struct ftmac100_data *priv = dev->priv;
239 return _ftmac100_halt(priv);
240}
241
242static int ftmac100_init(struct eth_device *dev, bd_t *bd)
243{
244 struct ftmac100_data *priv = dev->priv;
245 return _ftmac100_init(priv , dev->enetaddr);
246}
247
248static int _ftmac100_recv(struct ftmac100_data *priv)
249{
250 struct ftmac100_rxdes *curr_des;
251 unsigned short len;
252 curr_des = &priv->rxdes[priv->rx_index];
253 len = __ftmac100_recv(priv);
254 if (len) {
255 /* pass the packet up to the protocol layers. */
256 net_process_received_packet((void *)curr_des->rxdes2, len);
257 _ftmac100_free_pkt(priv);
258 }
259 return len ? 1 : 0;
260}
261
262/*
263 * Get a data block via Ethernet
264 */
265static int ftmac100_recv(struct eth_device *dev)
266{
267 struct ftmac100_data *priv = dev->priv;
268 return _ftmac100_recv(priv);
269}
270
271/*
272 * Send a data block via Ethernet
273 */
274static int ftmac100_send(struct eth_device *dev, void *packet, int length)
275{
276 struct ftmac100_data *priv = dev->priv;
277 return _ftmac100_send(priv , packet , length);
278}
279
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800280int ftmac100_initialize (bd_t *bd)
281{
282 struct eth_device *dev;
283 struct ftmac100_data *priv;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800284 dev = malloc (sizeof *dev);
285 if (!dev) {
286 printf ("%s(): failed to allocate dev\n", __func__);
287 goto out;
288 }
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800289 /* Transmit and receive descriptors should align to 16 bytes */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800290 priv = memalign (16, sizeof (struct ftmac100_data));
291 if (!priv) {
292 printf ("%s(): failed to allocate priv\n", __func__);
293 goto free_dev;
294 }
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800295 memset (dev, 0, sizeof (*dev));
296 memset (priv, 0, sizeof (*priv));
297
Ben Whitten34fd6c92015-12-30 13:05:58 +0000298 strcpy(dev->name, "FTMAC100");
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800299 dev->iobase = CONFIG_FTMAC100_BASE;
300 dev->init = ftmac100_init;
301 dev->halt = ftmac100_halt;
302 dev->send = ftmac100_send;
303 dev->recv = ftmac100_recv;
304 dev->priv = priv;
rickdc24dac2017-05-23 13:48:27 +0800305 priv->iobase = dev->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800306 eth_register (dev);
307
308 return 1;
309
310free_dev:
311 free (dev);
312out:
313 return 0;
314}
rickdc24dac2017-05-23 13:48:27 +0800315#endif
316
317#ifdef CONFIG_DM_ETH
318static int ftmac100_start(struct udevice *dev)
319{
320 struct eth_pdata *plat = dev_get_platdata(dev);
321 struct ftmac100_data *priv = dev_get_priv(dev);
322
323 return _ftmac100_init(priv, plat->enetaddr);
324}
325
326static void ftmac100_stop(struct udevice *dev)
327{
328 struct ftmac100_data *priv = dev_get_priv(dev);
329 _ftmac100_halt(priv);
330}
331
332static int ftmac100_send(struct udevice *dev, void *packet, int length)
333{
334 struct ftmac100_data *priv = dev_get_priv(dev);
335 int ret;
336 ret = _ftmac100_send(priv , packet , length);
337 return ret ? 0 : -ETIMEDOUT;
338}
339
340static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
341{
342 struct ftmac100_data *priv = dev_get_priv(dev);
343 struct ftmac100_rxdes *curr_des;
344 curr_des = &priv->rxdes[priv->rx_index];
345 int len;
346 len = __ftmac100_recv(priv);
347 if (len)
Rick Chen59d15262018-05-29 10:42:19 +0800348 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickdc24dac2017-05-23 13:48:27 +0800349
350 return len ? len : -EAGAIN;
351}
352
353static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
354{
355 struct ftmac100_data *priv = dev_get_priv(dev);
356 _ftmac100_free_pkt(priv);
357 return 0;
358}
359
360int ftmac100_read_rom_hwaddr(struct udevice *dev)
361{
362 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glass399a9ce2017-08-03 12:22:14 -0600363 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickdc24dac2017-05-23 13:48:27 +0800364 return 0;
365}
366
367static const char *dtbmacaddr(u32 ifno)
368{
369 int node, len;
370 char enet[16];
371 const char *mac;
372 const char *path;
373 if (gd->fdt_blob == NULL) {
374 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
375 return NULL;
376 }
377 node = fdt_path_offset(gd->fdt_blob, "/aliases");
378 if (node < 0)
379 return NULL;
380
381 sprintf(enet, "ethernet%d", ifno);
382 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
383 if (!path) {
384 printf("no alias for %s\n", enet);
385 return NULL;
386 }
387 node = fdt_path_offset(gd->fdt_blob, path);
388 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
389 if (mac && is_valid_ethaddr((u8 *)mac))
390 return mac;
391
392 return NULL;
393}
394
395static int ftmac100_ofdata_to_platdata(struct udevice *dev)
396{
397 struct ftmac100_data *priv = dev_get_priv(dev);
398 struct eth_pdata *pdata = dev_get_platdata(dev);
399 const char *mac;
Simon Glassba1dea42017-05-17 17:18:05 -0600400 pdata->iobase = devfdt_get_addr(dev);
rickdc24dac2017-05-23 13:48:27 +0800401 priv->iobase = pdata->iobase;
402 mac = dtbmacaddr(0);
403 if (mac)
404 memcpy(pdata->enetaddr , mac , 6);
405
406 return 0;
407}
408
409static int ftmac100_probe(struct udevice *dev)
410{
411 struct ftmac100_data *priv = dev_get_priv(dev);
412 priv->name = dev->name;
413 return 0;
414}
415
416static int ftmac100_bind(struct udevice *dev)
417{
418 return device_set_name(dev, dev->name);
419}
420
421static const struct eth_ops ftmac100_ops = {
422 .start = ftmac100_start,
423 .send = ftmac100_send,
424 .recv = ftmac100_recv,
425 .stop = ftmac100_stop,
426 .free_pkt = ftmac100_free_pkt,
427};
428
429static const struct udevice_id ftmac100_ids[] = {
430 { .compatible = "andestech,atmac100" },
431 { }
432};
433
434U_BOOT_DRIVER(ftmac100) = {
435 .name = "nds32_mac",
436 .id = UCLASS_ETH,
437 .of_match = ftmac100_ids,
438 .bind = ftmac100_bind,
439 .ofdata_to_platdata = ftmac100_ofdata_to_platdata,
440 .probe = ftmac100_probe,
441 .ops = &ftmac100_ops,
442 .priv_auto_alloc_size = sizeof(struct ftmac100_data),
443 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
444 .flags = DM_FLAG_ALLOC_PRIV_DMA,
445};
446#endif