blob: cd24a21f04f1e45c843222384a10a51ac2283d5b [file] [log] [blame]
Po-Yu Chuang50253b82009-08-10 11:00:00 +08001/*
2 * Faraday FTMAC100 Ethernet
3 *
4 * (C) Copyright 2009 Faraday Technology
5 * Po-Yu Chuang <ratbert@faraday-tech.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Po-Yu Chuang50253b82009-08-10 11:00:00 +08008 */
9
10#include <config.h>
11#include <common.h>
12#include <malloc.h>
13#include <net.h>
rickdc24dac2017-05-23 13:48:27 +080014#include <linux/io.h>
Po-Yu Chuang50253b82009-08-10 11:00:00 +080015
16#include "ftmac100.h"
rickdc24dac2017-05-23 13:48:27 +080017#ifdef CONFIG_DM_ETH
18#include <dm.h>
19DECLARE_GLOBAL_DATA_PTR;
20#endif
Po-Yu Chuang50253b82009-08-10 11:00:00 +080021#define ETH_ZLEN 60
22
23struct ftmac100_data {
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080024 struct ftmac100_txdes txdes[1];
25 struct ftmac100_rxdes rxdes[PKTBUFSRX];
Po-Yu Chuang50253b82009-08-10 11:00:00 +080026 int rx_index;
rickdc24dac2017-05-23 13:48:27 +080027 const char *name;
28 phys_addr_t iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080029};
30
31/*
32 * Reset MAC
33 */
rickdc24dac2017-05-23 13:48:27 +080034static void ftmac100_reset(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080035{
rickdc24dac2017-05-23 13:48:27 +080036 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080037
38 debug ("%s()\n", __func__);
39
40 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
41
42 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
43 ;
44}
45
46/*
47 * Set MAC address
48 */
rickdc24dac2017-05-23 13:48:27 +080049static void ftmac100_set_mac(struct ftmac100_data *priv ,
50 const unsigned char *mac)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080051{
rickdc24dac2017-05-23 13:48:27 +080052 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080053 unsigned int maddr = mac[0] << 8 | mac[1];
54 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
55
56 debug ("%s(%x %x)\n", __func__, maddr, laddr);
57
58 writel (maddr, &ftmac100->mac_madr);
59 writel (laddr, &ftmac100->mac_ladr);
60}
61
Po-Yu Chuang50253b82009-08-10 11:00:00 +080062/*
rickdc24dac2017-05-23 13:48:27 +080063 * Disable MAC
Po-Yu Chuang50253b82009-08-10 11:00:00 +080064 */
rickdc24dac2017-05-23 13:48:27 +080065static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080066{
rickdc24dac2017-05-23 13:48:27 +080067 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080068 debug ("%s()\n", __func__);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080069 writel (0, &ftmac100->maccr);
70}
71
rickdc24dac2017-05-23 13:48:27 +080072/*
73 * Initialize MAC
74 */
75static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang50253b82009-08-10 11:00:00 +080076{
rickdc24dac2017-05-23 13:48:27 +080077 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080078 struct ftmac100_txdes *txdes = priv->txdes;
79 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080080 unsigned int maccr;
81 int i;
82
83 debug ("%s()\n", __func__);
84
rickdc24dac2017-05-23 13:48:27 +080085 ftmac100_reset(priv);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080086
87 /* set the ethernet address */
rickdc24dac2017-05-23 13:48:27 +080088 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080089
Po-Yu Chuang50253b82009-08-10 11:00:00 +080090
91 /* disable all interrupts */
92
93 writel (0, &ftmac100->imr);
94
95 /* initialize descriptors */
96
97 priv->rx_index = 0;
98
99 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
100 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
101
102 for (i = 0; i < PKTBUFSRX; i++) {
103 /* RXBUF_BADR */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500104 rxdes[i].rxdes2 = (unsigned int)net_rx_packets[i];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800105 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
106 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
107 }
108
109 /* transmit ring */
110
111 writel ((unsigned int)txdes, &ftmac100->txr_badr);
112
113 /* receive ring */
114
115 writel ((unsigned int)rxdes, &ftmac100->rxr_badr);
116
117 /* poll receive descriptor automatically */
118
119 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
120
121 /* enable transmitter, receiver */
122
123 maccr = FTMAC100_MACCR_XMT_EN |
124 FTMAC100_MACCR_RCV_EN |
125 FTMAC100_MACCR_XDMA_EN |
126 FTMAC100_MACCR_RDMA_EN |
127 FTMAC100_MACCR_CRC_APD |
128 FTMAC100_MACCR_ENRX_IN_HALFTX |
129 FTMAC100_MACCR_RX_RUNT |
130 FTMAC100_MACCR_RX_BROADPKT;
131
132 writel (maccr, &ftmac100->maccr);
133
134 return 0;
135}
136
137/*
rickdc24dac2017-05-23 13:48:27 +0800138 * Free receiving buffer
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800139 */
rickdc24dac2017-05-23 13:48:27 +0800140static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800141{
rickdc24dac2017-05-23 13:48:27 +0800142 struct ftmac100_rxdes *curr_des;
143 curr_des = &priv->rxdes[priv->rx_index];
144 /* release buffer to DMA */
145 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
146 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
147 return 0;
148}
149
150/*
151 * Receive a data block via Ethernet
152 */
153static int __ftmac100_recv(struct ftmac100_data *priv)
154{
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800155 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800156 unsigned short rxlen;
157
158 curr_des = &priv->rxdes[priv->rx_index];
159
160 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickdc24dac2017-05-23 13:48:27 +0800161 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800162
163 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
164 FTMAC100_RXDES0_CRC_ERR |
165 FTMAC100_RXDES0_FTL |
166 FTMAC100_RXDES0_RUNT |
167 FTMAC100_RXDES0_RX_ODD_NB)) {
rickdc24dac2017-05-23 13:48:27 +0800168 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800169 }
170
171 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
172
173 debug ("%s(): RX buffer %d, %x received\n",
174 __func__, priv->rx_index, rxlen);
175
rickdc24dac2017-05-23 13:48:27 +0800176 return rxlen;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800177}
178
179/*
180 * Send a data block via Ethernet
181 */
rickdc24dac2017-05-23 13:48:27 +0800182static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800183{
rickdc24dac2017-05-23 13:48:27 +0800184 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800185 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800186 ulong start;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800187
188 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
189 debug ("%s(): no TX descriptor available\n", __func__);
190 return -1;
191 }
192
193 debug ("%s(%x, %x)\n", __func__, (int)packet, length);
194
195 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
196
197 /* initiate a transmit sequence */
198
199 curr_des->txdes2 = (unsigned int)packet; /* TXBUF_BADR */
200
201 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
202 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
203 FTMAC100_TXDES1_LTS |
204 FTMAC100_TXDES1_TXBUF_SIZE (length);
205
206 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
207
208 /* start transmit */
209
210 writel (1, &ftmac100->txpd);
211
212 /* wait for transfer to succeed */
213
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800214 start = get_timer(0);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800215 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800216 if (get_timer(start) >= 5) {
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800217 debug ("%s(): timed out\n", __func__);
218 return -1;
219 }
220 }
221
222 debug ("%s(): packet sent\n", __func__);
223
224 return 0;
225}
226
rickdc24dac2017-05-23 13:48:27 +0800227#ifndef CONFIG_DM_ETH
228/*
229 * disable transmitter, receiver
230 */
231static void ftmac100_halt(struct eth_device *dev)
232{
233 struct ftmac100_data *priv = dev->priv;
234 return _ftmac100_halt(priv);
235}
236
237static int ftmac100_init(struct eth_device *dev, bd_t *bd)
238{
239 struct ftmac100_data *priv = dev->priv;
240 return _ftmac100_init(priv , dev->enetaddr);
241}
242
243static int _ftmac100_recv(struct ftmac100_data *priv)
244{
245 struct ftmac100_rxdes *curr_des;
246 unsigned short len;
247 curr_des = &priv->rxdes[priv->rx_index];
248 len = __ftmac100_recv(priv);
249 if (len) {
250 /* pass the packet up to the protocol layers. */
251 net_process_received_packet((void *)curr_des->rxdes2, len);
252 _ftmac100_free_pkt(priv);
253 }
254 return len ? 1 : 0;
255}
256
257/*
258 * Get a data block via Ethernet
259 */
260static int ftmac100_recv(struct eth_device *dev)
261{
262 struct ftmac100_data *priv = dev->priv;
263 return _ftmac100_recv(priv);
264}
265
266/*
267 * Send a data block via Ethernet
268 */
269static int ftmac100_send(struct eth_device *dev, void *packet, int length)
270{
271 struct ftmac100_data *priv = dev->priv;
272 return _ftmac100_send(priv , packet , length);
273}
274
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800275int ftmac100_initialize (bd_t *bd)
276{
277 struct eth_device *dev;
278 struct ftmac100_data *priv;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800279 dev = malloc (sizeof *dev);
280 if (!dev) {
281 printf ("%s(): failed to allocate dev\n", __func__);
282 goto out;
283 }
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800284 /* Transmit and receive descriptors should align to 16 bytes */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800285 priv = memalign (16, sizeof (struct ftmac100_data));
286 if (!priv) {
287 printf ("%s(): failed to allocate priv\n", __func__);
288 goto free_dev;
289 }
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800290 memset (dev, 0, sizeof (*dev));
291 memset (priv, 0, sizeof (*priv));
292
Ben Whitten34fd6c92015-12-30 13:05:58 +0000293 strcpy(dev->name, "FTMAC100");
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800294 dev->iobase = CONFIG_FTMAC100_BASE;
295 dev->init = ftmac100_init;
296 dev->halt = ftmac100_halt;
297 dev->send = ftmac100_send;
298 dev->recv = ftmac100_recv;
299 dev->priv = priv;
rickdc24dac2017-05-23 13:48:27 +0800300 priv->iobase = dev->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800301 eth_register (dev);
302
303 return 1;
304
305free_dev:
306 free (dev);
307out:
308 return 0;
309}
rickdc24dac2017-05-23 13:48:27 +0800310#endif
311
312#ifdef CONFIG_DM_ETH
313static int ftmac100_start(struct udevice *dev)
314{
315 struct eth_pdata *plat = dev_get_platdata(dev);
316 struct ftmac100_data *priv = dev_get_priv(dev);
317
318 return _ftmac100_init(priv, plat->enetaddr);
319}
320
321static void ftmac100_stop(struct udevice *dev)
322{
323 struct ftmac100_data *priv = dev_get_priv(dev);
324 _ftmac100_halt(priv);
325}
326
327static int ftmac100_send(struct udevice *dev, void *packet, int length)
328{
329 struct ftmac100_data *priv = dev_get_priv(dev);
330 int ret;
331 ret = _ftmac100_send(priv , packet , length);
332 return ret ? 0 : -ETIMEDOUT;
333}
334
335static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
336{
337 struct ftmac100_data *priv = dev_get_priv(dev);
338 struct ftmac100_rxdes *curr_des;
339 curr_des = &priv->rxdes[priv->rx_index];
340 int len;
341 len = __ftmac100_recv(priv);
342 if (len)
343 *packetp = (void *)curr_des->rxdes2;
344
345 return len ? len : -EAGAIN;
346}
347
348static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
349{
350 struct ftmac100_data *priv = dev_get_priv(dev);
351 _ftmac100_free_pkt(priv);
352 return 0;
353}
354
355int ftmac100_read_rom_hwaddr(struct udevice *dev)
356{
357 struct eth_pdata *pdata = dev_get_platdata(dev);
358 eth_getenv_enetaddr("ethaddr", pdata->enetaddr);
359 return 0;
360}
361
362static const char *dtbmacaddr(u32 ifno)
363{
364 int node, len;
365 char enet[16];
366 const char *mac;
367 const char *path;
368 if (gd->fdt_blob == NULL) {
369 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
370 return NULL;
371 }
372 node = fdt_path_offset(gd->fdt_blob, "/aliases");
373 if (node < 0)
374 return NULL;
375
376 sprintf(enet, "ethernet%d", ifno);
377 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
378 if (!path) {
379 printf("no alias for %s\n", enet);
380 return NULL;
381 }
382 node = fdt_path_offset(gd->fdt_blob, path);
383 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
384 if (mac && is_valid_ethaddr((u8 *)mac))
385 return mac;
386
387 return NULL;
388}
389
390static int ftmac100_ofdata_to_platdata(struct udevice *dev)
391{
392 struct ftmac100_data *priv = dev_get_priv(dev);
393 struct eth_pdata *pdata = dev_get_platdata(dev);
394 const char *mac;
Simon Glassba1dea42017-05-17 17:18:05 -0600395 pdata->iobase = devfdt_get_addr(dev);
rickdc24dac2017-05-23 13:48:27 +0800396 priv->iobase = pdata->iobase;
397 mac = dtbmacaddr(0);
398 if (mac)
399 memcpy(pdata->enetaddr , mac , 6);
400
401 return 0;
402}
403
404static int ftmac100_probe(struct udevice *dev)
405{
406 struct ftmac100_data *priv = dev_get_priv(dev);
407 priv->name = dev->name;
408 return 0;
409}
410
411static int ftmac100_bind(struct udevice *dev)
412{
413 return device_set_name(dev, dev->name);
414}
415
416static const struct eth_ops ftmac100_ops = {
417 .start = ftmac100_start,
418 .send = ftmac100_send,
419 .recv = ftmac100_recv,
420 .stop = ftmac100_stop,
421 .free_pkt = ftmac100_free_pkt,
422};
423
424static const struct udevice_id ftmac100_ids[] = {
425 { .compatible = "andestech,atmac100" },
426 { }
427};
428
429U_BOOT_DRIVER(ftmac100) = {
430 .name = "nds32_mac",
431 .id = UCLASS_ETH,
432 .of_match = ftmac100_ids,
433 .bind = ftmac100_bind,
434 .ofdata_to_platdata = ftmac100_ofdata_to_platdata,
435 .probe = ftmac100_probe,
436 .ops = &ftmac100_ops,
437 .priv_auto_alloc_size = sizeof(struct ftmac100_data),
438 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
439 .flags = DM_FLAG_ALLOC_PRIV_DMA,
440};
441#endif