blob: 7c89d7f67b8daf259938ae8a2d3b4baf1ad75a63 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
rickdc24dac2017-05-23 13:48:27 +080017#include <linux/io.h>
Po-Yu Chuang50253b82009-08-10 11:00:00 +080018
19#include "ftmac100.h"
rickdc24dac2017-05-23 13:48:27 +080020#include <dm.h>
Tom Rinic8c4c562022-11-19 18:45:40 -050021
rickdc24dac2017-05-23 13:48:27 +080022DECLARE_GLOBAL_DATA_PTR;
Tom Rinic8c4c562022-11-19 18:45:40 -050023
Po-Yu Chuang50253b82009-08-10 11:00:00 +080024#define ETH_ZLEN 60
25
26struct ftmac100_data {
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080027 struct ftmac100_txdes txdes[1];
28 struct ftmac100_rxdes rxdes[PKTBUFSRX];
Po-Yu Chuang50253b82009-08-10 11:00:00 +080029 int rx_index;
rickdc24dac2017-05-23 13:48:27 +080030 const char *name;
31 phys_addr_t iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080032};
33
34/*
35 * Reset MAC
36 */
rickdc24dac2017-05-23 13:48:27 +080037static void ftmac100_reset(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080038{
Bin Mengff7607c2021-01-31 20:36:02 +080039 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang50253b82009-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)
rick54966612017-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 Chuang50253b82009-08-10 11:00:00 +080052}
53
54/*
55 * Set MAC address
56 */
rickdc24dac2017-05-23 13:48:27 +080057static void ftmac100_set_mac(struct ftmac100_data *priv ,
58 const unsigned char *mac)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080059{
Bin Mengff7607c2021-01-31 20:36:02 +080060 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang50253b82009-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 Chuang50253b82009-08-10 11:00:00 +080070/*
rickdc24dac2017-05-23 13:48:27 +080071 * Disable MAC
Po-Yu Chuang50253b82009-08-10 11:00:00 +080072 */
rickdc24dac2017-05-23 13:48:27 +080073static void _ftmac100_halt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +080074{
Bin Mengff7607c2021-01-31 20:36:02 +080075 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080076 debug ("%s()\n", __func__);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080077 writel (0, &ftmac100->maccr);
78}
79
rickdc24dac2017-05-23 13:48:27 +080080/*
81 * Initialize MAC
82 */
83static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
Po-Yu Chuang50253b82009-08-10 11:00:00 +080084{
Bin Mengff7607c2021-01-31 20:36:02 +080085 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +080086 struct ftmac100_txdes *txdes = priv->txdes;
87 struct ftmac100_rxdes *rxdes = priv->rxdes;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080088 unsigned int maccr;
89 int i;
Po-Yu Chuang50253b82009-08-10 11:00:00 +080090 debug ("%s()\n", __func__);
91
rickdc24dac2017-05-23 13:48:27 +080092 ftmac100_reset(priv);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080093
94 /* set the ethernet address */
rickdc24dac2017-05-23 13:48:27 +080095 ftmac100_set_mac(priv, enetaddr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +080096
Po-Yu Chuang50253b82009-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 Chen59d15262018-05-29 10:42:19 +0800111 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
Po-Yu Chuang50253b82009-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 Chen59d15262018-05-29 10:42:19 +0800118 writel ((unsigned long)txdes, &ftmac100->txr_badr);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800119
120 /* receive ring */
121
Rick Chen59d15262018-05-29 10:42:19 +0800122 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
Po-Yu Chuang50253b82009-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/*
rickdc24dac2017-05-23 13:48:27 +0800145 * Free receiving buffer
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800146 */
rickdc24dac2017-05-23 13:48:27 +0800147static int _ftmac100_free_pkt(struct ftmac100_data *priv)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800148{
rickdc24dac2017-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 Chuang767ba6f2011-01-21 15:50:37 +0800162 struct ftmac100_rxdes *curr_des;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800163 unsigned short rxlen;
164
165 curr_des = &priv->rxdes[priv->rx_index];
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800166 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
rickdc24dac2017-05-23 13:48:27 +0800167 return 0;
Po-Yu Chuang50253b82009-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)) {
rickdc24dac2017-05-23 13:48:27 +0800174 return 0;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800175 }
176
177 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
rickb0ce1822017-08-29 10:09:00 +0800178 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800179 debug ("%s(): RX buffer %d, %x received\n",
180 __func__, priv->rx_index, rxlen);
181
rickdc24dac2017-05-23 13:48:27 +0800182 return rxlen;
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800183}
184
185/*
186 * Send a data block via Ethernet
187 */
rickdc24dac2017-05-23 13:48:27 +0800188static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800189{
Bin Mengff7607c2021-01-31 20:36:02 +0800190 struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
Po-Yu Chuang767ba6f2011-01-21 15:50:37 +0800191 struct ftmac100_txdes *curr_des = priv->txdes;
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800192 ulong start;
Po-Yu Chuang50253b82009-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 Chen59d15262018-05-29 10:42:19 +0800199 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800200
201 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
202
203 /* initiate a transmit sequence */
204
Rick Chen59d15262018-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 Chuang50253b82009-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 Chuang0bed9172011-01-21 15:51:25 +0800221 start = get_timer(0);
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800222 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
Po-Yu Chuang0bed9172011-01-21 15:51:25 +0800223 if (get_timer(start) >= 5) {
Po-Yu Chuang50253b82009-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
Po-Yu Chuang50253b82009-08-10 11:00:00 +0800231 return 0;
232}
rickdc24dac2017-05-23 13:48:27 +0800233
rickdc24dac2017-05-23 13:48:27 +0800234static int ftmac100_start(struct udevice *dev)
235{
Simon Glassfa20e932020-12-03 16:55:20 -0700236 struct eth_pdata *plat = dev_get_plat(dev);
rickdc24dac2017-05-23 13:48:27 +0800237 struct ftmac100_data *priv = dev_get_priv(dev);
238
239 return _ftmac100_init(priv, plat->enetaddr);
240}
241
242static void ftmac100_stop(struct udevice *dev)
243{
244 struct ftmac100_data *priv = dev_get_priv(dev);
245 _ftmac100_halt(priv);
246}
247
248static int ftmac100_send(struct udevice *dev, void *packet, int length)
249{
250 struct ftmac100_data *priv = dev_get_priv(dev);
251 int ret;
252 ret = _ftmac100_send(priv , packet , length);
253 return ret ? 0 : -ETIMEDOUT;
254}
255
256static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
257{
258 struct ftmac100_data *priv = dev_get_priv(dev);
259 struct ftmac100_rxdes *curr_des;
260 curr_des = &priv->rxdes[priv->rx_index];
261 int len;
262 len = __ftmac100_recv(priv);
263 if (len)
Rick Chen59d15262018-05-29 10:42:19 +0800264 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
rickdc24dac2017-05-23 13:48:27 +0800265
266 return len ? len : -EAGAIN;
267}
268
269static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
270{
271 struct ftmac100_data *priv = dev_get_priv(dev);
272 _ftmac100_free_pkt(priv);
273 return 0;
274}
275
276int ftmac100_read_rom_hwaddr(struct udevice *dev)
277{
Simon Glassfa20e932020-12-03 16:55:20 -0700278 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass399a9ce2017-08-03 12:22:14 -0600279 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
rickdc24dac2017-05-23 13:48:27 +0800280 return 0;
281}
282
283static const char *dtbmacaddr(u32 ifno)
284{
285 int node, len;
286 char enet[16];
287 const char *mac;
288 const char *path;
289 if (gd->fdt_blob == NULL) {
290 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
291 return NULL;
292 }
293 node = fdt_path_offset(gd->fdt_blob, "/aliases");
294 if (node < 0)
295 return NULL;
296
297 sprintf(enet, "ethernet%d", ifno);
298 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
299 if (!path) {
300 printf("no alias for %s\n", enet);
301 return NULL;
302 }
303 node = fdt_path_offset(gd->fdt_blob, path);
304 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
305 if (mac && is_valid_ethaddr((u8 *)mac))
306 return mac;
307
308 return NULL;
309}
310
Simon Glassaad29ae2020-12-03 16:55:21 -0700311static int ftmac100_of_to_plat(struct udevice *dev)
rickdc24dac2017-05-23 13:48:27 +0800312{
313 struct ftmac100_data *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700314 struct eth_pdata *pdata = dev_get_plat(dev);
rickdc24dac2017-05-23 13:48:27 +0800315 const char *mac;
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900316 pdata->iobase = dev_read_addr(dev);
rickdc24dac2017-05-23 13:48:27 +0800317 priv->iobase = pdata->iobase;
318 mac = dtbmacaddr(0);
319 if (mac)
320 memcpy(pdata->enetaddr , mac , 6);
321
322 return 0;
323}
324
325static int ftmac100_probe(struct udevice *dev)
326{
327 struct ftmac100_data *priv = dev_get_priv(dev);
328 priv->name = dev->name;
329 return 0;
330}
331
332static int ftmac100_bind(struct udevice *dev)
333{
334 return device_set_name(dev, dev->name);
335}
336
337static const struct eth_ops ftmac100_ops = {
338 .start = ftmac100_start,
339 .send = ftmac100_send,
340 .recv = ftmac100_recv,
341 .stop = ftmac100_stop,
342 .free_pkt = ftmac100_free_pkt,
343};
344
345static const struct udevice_id ftmac100_ids[] = {
346 { .compatible = "andestech,atmac100" },
347 { }
348};
349
350U_BOOT_DRIVER(ftmac100) = {
Sergei Antonov0e15edb2023-02-03 22:09:02 +0300351 .name = "ftmac100",
rickdc24dac2017-05-23 13:48:27 +0800352 .id = UCLASS_ETH,
353 .of_match = ftmac100_ids,
354 .bind = ftmac100_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700355 .of_to_plat = ftmac100_of_to_plat,
rickdc24dac2017-05-23 13:48:27 +0800356 .probe = ftmac100_probe,
357 .ops = &ftmac100_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700358 .priv_auto = sizeof(struct ftmac100_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700359 .plat_auto = sizeof(struct eth_pdata),
rickdc24dac2017-05-23 13:48:27 +0800360 .flags = DM_FLAG_ALLOC_PRIV_DMA,
361};