blob: d8f1dde657974f97aa16cb8af0da3d04ddd13d7b [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 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>
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)
rick54966612017-08-29 10:15:05 +080043 mdelay(1);
44 /*
45 * When soft reset complete, write mac address immediately maybe fail somehow
46 * Wait for a while can avoid this problem
47 */
48 mdelay(1);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080049}
50
51/*
52 * Set MAC address
53 */
rickdc24dac2017-05-23 13:48:27 +080054static void ftmac100_set_mac(struct ftmac100_data *priv ,
55 const unsigned char *mac)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080056{
rickdc24dac2017-05-23 13:48:27 +080057 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080058 unsigned int maddr = mac[0] << 8 | mac[1];
59 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
60
61 debug ("%s(%x %x)\n", __func__, maddr, laddr);
62
63 writel (maddr, &ftmac100->mac_madr);
64 writel (laddr, &ftmac100->mac_ladr);
65}
66
Po-Yu Chuang50253b82009-08-10 11:00:00 +080067/*
rickdc24dac2017-05-23 13:48:27 +080068 * Disable MAC
Po-Yu Chuang50253b82009-08-10 11:00:00 +080069 */
rickdc24dac2017-05-23 13:48:27 +080070static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080071{
rickdc24dac2017-05-23 13:48:27 +080072 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080073 debug ("%s()\n", __func__);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080074 writel (0, &ftmac100->maccr);
75}
76
rickdc24dac2017-05-23 13:48:27 +080077/*
78 * Initialize MAC
79 */
80static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang50253b82009-08-10 11:00:00 +080081{
rickdc24dac2017-05-23 13:48:27 +080082 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080083 struct ftmac100_txdes *txdes = priv->txdes;
84 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080085 unsigned int maccr;
86 int i;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080087 debug ("%s()\n", __func__);
88
rickdc24dac2017-05-23 13:48:27 +080089 ftmac100_reset(priv);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080090
91 /* set the ethernet address */
rickdc24dac2017-05-23 13:48:27 +080092 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080093
Po-Yu Chuang50253b82009-08-10 11:00:00 +080094
95 /* disable all interrupts */
96
97 writel (0, &ftmac100->imr);
98
99 /* initialize descriptors */
100
101 priv->rx_index = 0;
102
103 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
104 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
105
106 for (i = 0; i < PKTBUFSRX; i++) {
107 /* RXBUF_BADR */
Rick Chen59d15262018-05-29 10:42:19 +0800108 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800109 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
110 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
111 }
112
113 /* transmit ring */
114
Rick Chen59d15262018-05-29 10:42:19 +0800115 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800116
117 /* receive ring */
118
Rick Chen59d15262018-05-29 10:42:19 +0800119 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800120
121 /* poll receive descriptor automatically */
122
123 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
124
125 /* enable transmitter, receiver */
126
127 maccr = FTMAC100_MACCR_XMT_EN |
128 FTMAC100_MACCR_RCV_EN |
129 FTMAC100_MACCR_XDMA_EN |
130 FTMAC100_MACCR_RDMA_EN |
131 FTMAC100_MACCR_CRC_APD |
132 FTMAC100_MACCR_ENRX_IN_HALFTX |
133 FTMAC100_MACCR_RX_RUNT |
134 FTMAC100_MACCR_RX_BROADPKT;
135
136 writel (maccr, &ftmac100->maccr);
137
138 return 0;
139}
140
141/*
rickdc24dac2017-05-23 13:48:27 +0800142 * Free receiving buffer
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800143 */
rickdc24dac2017-05-23 13:48:27 +0800144static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800145{
rickdc24dac2017-05-23 13:48:27 +0800146 struct ftmac100_rxdes *curr_des;
147 curr_des = &priv->rxdes[priv->rx_index];
148 /* release buffer to DMA */
149 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
150 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
151 return 0;
152}
153
154/*
155 * Receive a data block via Ethernet
156 */
157static int __ftmac100_recv(struct ftmac100_data *priv)
158{
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800159 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800160 unsigned short rxlen;
161
162 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800163 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickdc24dac2017-05-23 13:48:27 +0800164 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800165
166 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
167 FTMAC100_RXDES0_CRC_ERR |
168 FTMAC100_RXDES0_FTL |
169 FTMAC100_RXDES0_RUNT |
170 FTMAC100_RXDES0_RX_ODD_NB)) {
rickdc24dac2017-05-23 13:48:27 +0800171 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800172 }
173
174 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickb0ce1822017-08-29 10:09:00 +0800175 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800176 debug ("%s(): RX buffer %d, %x received\n",
177 __func__, priv->rx_index, rxlen);
178
rickdc24dac2017-05-23 13:48:27 +0800179 return rxlen;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800180}
181
182/*
183 * Send a data block via Ethernet
184 */
rickdc24dac2017-05-23 13:48:27 +0800185static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800186{
rickdc24dac2017-05-23 13:48:27 +0800187 struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800188 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800189 ulong start;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800190
191 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
192 debug ("%s(): no TX descriptor available\n", __func__);
193 return -1;
194 }
195
Rick Chen59d15262018-05-29 10:42:19 +0800196 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800197
198 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
199
200 /* initiate a transmit sequence */
201
Rick Chen59d15262018-05-29 10:42:19 +0800202 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
203 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800204
205 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
206 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
207 FTMAC100_TXDES1_LTS |
208 FTMAC100_TXDES1_TXBUF_SIZE (length);
209
210 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
211
212 /* start transmit */
213
214 writel (1, &ftmac100->txpd);
215
216 /* wait for transfer to succeed */
217
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800218 start = get_timer(0);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800219 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800220 if (get_timer(start) >= 5) {
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800221 debug ("%s(): timed out\n", __func__);
222 return -1;
223 }
224 }
225
226 debug ("%s(): packet sent\n", __func__);
227
228 return 0;
229}
230
rickdc24dac2017-05-23 13:48:27 +0800231#ifndef CONFIG_DM_ETH
232/*
233 * disable transmitter, receiver
234 */
235static void ftmac100_halt(struct eth_device *dev)
236{
237 struct ftmac100_data *priv = dev->priv;
238 return _ftmac100_halt(priv);
239}
240
241static int ftmac100_init(struct eth_device *dev, bd_t *bd)
242{
243 struct ftmac100_data *priv = dev->priv;
244 return _ftmac100_init(priv , dev->enetaddr);
245}
246
247static int _ftmac100_recv(struct ftmac100_data *priv)
248{
249 struct ftmac100_rxdes *curr_des;
250 unsigned short len;
251 curr_des = &priv->rxdes[priv->rx_index];
252 len = __ftmac100_recv(priv);
253 if (len) {
254 /* pass the packet up to the protocol layers. */
255 net_process_received_packet((void *)curr_des->rxdes2, len);
256 _ftmac100_free_pkt(priv);
257 }
258 return len ? 1 : 0;
259}
260
261/*
262 * Get a data block via Ethernet
263 */
264static int ftmac100_recv(struct eth_device *dev)
265{
266 struct ftmac100_data *priv = dev->priv;
267 return _ftmac100_recv(priv);
268}
269
270/*
271 * Send a data block via Ethernet
272 */
273static int ftmac100_send(struct eth_device *dev, void *packet, int length)
274{
275 struct ftmac100_data *priv = dev->priv;
276 return _ftmac100_send(priv , packet , length);
277}
278
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800279int ftmac100_initialize (bd_t *bd)
280{
281 struct eth_device *dev;
282 struct ftmac100_data *priv;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800283 dev = malloc (sizeof *dev);
284 if (!dev) {
285 printf ("%s(): failed to allocate dev\n", __func__);
286 goto out;
287 }
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800288 /* Transmit and receive descriptors should align to 16 bytes */
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800289 priv = memalign (16, sizeof (struct ftmac100_data));
290 if (!priv) {
291 printf ("%s(): failed to allocate priv\n", __func__);
292 goto free_dev;
293 }
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800294 memset (dev, 0, sizeof (*dev));
295 memset (priv, 0, sizeof (*priv));
296
Ben Whitten34fd6c92015-12-30 13:05:58 +0000297 strcpy(dev->name, "FTMAC100");
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800298 dev->iobase = CONFIG_FTMAC100_BASE;
299 dev->init = ftmac100_init;
300 dev->halt = ftmac100_halt;
301 dev->send = ftmac100_send;
302 dev->recv = ftmac100_recv;
303 dev->priv = priv;
rickdc24dac2017-05-23 13:48:27 +0800304 priv->iobase = dev->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800305 eth_register (dev);
306
307 return 1;
308
309free_dev:
310 free (dev);
311out:
312 return 0;
313}
rickdc24dac2017-05-23 13:48:27 +0800314#endif
315
316#ifdef CONFIG_DM_ETH
317static int ftmac100_start(struct udevice *dev)
318{
319 struct eth_pdata *plat = dev_get_platdata(dev);
320 struct ftmac100_data *priv = dev_get_priv(dev);
321
322 return _ftmac100_init(priv, plat->enetaddr);
323}
324
325static void ftmac100_stop(struct udevice *dev)
326{
327 struct ftmac100_data *priv = dev_get_priv(dev);
328 _ftmac100_halt(priv);
329}
330
331static int ftmac100_send(struct udevice *dev, void *packet, int length)
332{
333 struct ftmac100_data *priv = dev_get_priv(dev);
334 int ret;
335 ret = _ftmac100_send(priv , packet , length);
336 return ret ? 0 : -ETIMEDOUT;
337}
338
339static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
340{
341 struct ftmac100_data *priv = dev_get_priv(dev);
342 struct ftmac100_rxdes *curr_des;
343 curr_des = &priv->rxdes[priv->rx_index];
344 int len;
345 len = __ftmac100_recv(priv);
346 if (len)
Rick Chen59d15262018-05-29 10:42:19 +0800347 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickdc24dac2017-05-23 13:48:27 +0800348
349 return len ? len : -EAGAIN;
350}
351
352static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
353{
354 struct ftmac100_data *priv = dev_get_priv(dev);
355 _ftmac100_free_pkt(priv);
356 return 0;
357}
358
359int ftmac100_read_rom_hwaddr(struct udevice *dev)
360{
361 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glass399a9ce2017-08-03 12:22:14 -0600362 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickdc24dac2017-05-23 13:48:27 +0800363 return 0;
364}
365
366static const char *dtbmacaddr(u32 ifno)
367{
368 int node, len;
369 char enet[16];
370 const char *mac;
371 const char *path;
372 if (gd->fdt_blob == NULL) {
373 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
374 return NULL;
375 }
376 node = fdt_path_offset(gd->fdt_blob, "/aliases");
377 if (node < 0)
378 return NULL;
379
380 sprintf(enet, "ethernet%d", ifno);
381 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
382 if (!path) {
383 printf("no alias for %s\n", enet);
384 return NULL;
385 }
386 node = fdt_path_offset(gd->fdt_blob, path);
387 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
388 if (mac && is_valid_ethaddr((u8 *)mac))
389 return mac;
390
391 return NULL;
392}
393
394static int ftmac100_ofdata_to_platdata(struct udevice *dev)
395{
396 struct ftmac100_data *priv = dev_get_priv(dev);
397 struct eth_pdata *pdata = dev_get_platdata(dev);
398 const char *mac;
Simon Glassba1dea42017-05-17 17:18:05 -0600399 pdata->iobase = devfdt_get_addr(dev);
rickdc24dac2017-05-23 13:48:27 +0800400 priv->iobase = pdata->iobase;
401 mac = dtbmacaddr(0);
402 if (mac)
403 memcpy(pdata->enetaddr , mac , 6);
404
405 return 0;
406}
407
408static int ftmac100_probe(struct udevice *dev)
409{
410 struct ftmac100_data *priv = dev_get_priv(dev);
411 priv->name = dev->name;
412 return 0;
413}
414
415static int ftmac100_bind(struct udevice *dev)
416{
417 return device_set_name(dev, dev->name);
418}
419
420static const struct eth_ops ftmac100_ops = {
421 .start = ftmac100_start,
422 .send = ftmac100_send,
423 .recv = ftmac100_recv,
424 .stop = ftmac100_stop,
425 .free_pkt = ftmac100_free_pkt,
426};
427
428static const struct udevice_id ftmac100_ids[] = {
429 { .compatible = "andestech,atmac100" },
430 { }
431};
432
433U_BOOT_DRIVER(ftmac100) = {
434 .name = "nds32_mac",
435 .id = UCLASS_ETH,
436 .of_match = ftmac100_ids,
437 .bind = ftmac100_bind,
438 .ofdata_to_platdata = ftmac100_ofdata_to_platdata,
439 .probe = ftmac100_probe,
440 .ops = &ftmac100_ops,
441 .priv_auto_alloc_size = sizeof(struct ftmac100_data),
442 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
443 .flags = DM_FLAG_ALLOC_PRIV_DMA,
444};
445#endif