xref: /openbmc/u-boot/drivers/net/ftmac100.c (revision a3b36c84)
1 /*
2  * Faraday FTMAC100 Ethernet
3  *
4  * (C) Copyright 2009 Faraday Technology
5  * Po-Yu Chuang <ratbert@faraday-tech.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <config.h>
11 #include <common.h>
12 #include <malloc.h>
13 #include <net.h>
14 #include <linux/io.h>
15 
16 #include "ftmac100.h"
17 #ifdef CONFIG_DM_ETH
18 #include <dm.h>
19 DECLARE_GLOBAL_DATA_PTR;
20 #endif
21 #define ETH_ZLEN	60
22 
23 struct ftmac100_data {
24 	struct ftmac100_txdes txdes[1];
25 	struct ftmac100_rxdes rxdes[PKTBUFSRX];
26 	int rx_index;
27 	const char *name;
28 	phys_addr_t iobase;
29 };
30 
31 /*
32  * Reset MAC
33  */
34 static void ftmac100_reset(struct ftmac100_data *priv)
35 {
36 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
37 
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 		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);
49 }
50 
51 /*
52  * Set MAC address
53  */
54 static void ftmac100_set_mac(struct ftmac100_data *priv ,
55 	const unsigned char *mac)
56 {
57 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
58 	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 
67 /*
68  * Disable MAC
69  */
70 static void _ftmac100_halt(struct ftmac100_data *priv)
71 {
72 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
73 	debug ("%s()\n", __func__);
74 	writel (0, &ftmac100->maccr);
75 }
76 
77 /*
78  * Initialize MAC
79  */
80 static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
81 {
82 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
83 	struct ftmac100_txdes *txdes = priv->txdes;
84 	struct ftmac100_rxdes *rxdes = priv->rxdes;
85 	unsigned int maccr;
86 	int i;
87 	debug ("%s()\n", __func__);
88 
89 	ftmac100_reset(priv);
90 
91 	/* set the ethernet address */
92 	ftmac100_set_mac(priv, enetaddr);
93 
94 
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 */
108 		rxdes[i].rxdes2 = (unsigned int)net_rx_packets[i];
109 		rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
110 		rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
111 	}
112 
113 	/* transmit ring */
114 
115 	writel ((unsigned int)txdes, &ftmac100->txr_badr);
116 
117 	/* receive ring */
118 
119 	writel ((unsigned int)rxdes, &ftmac100->rxr_badr);
120 
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 /*
142  * Free receiving buffer
143  */
144 static int _ftmac100_free_pkt(struct ftmac100_data *priv)
145 {
146 	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  */
157 static int __ftmac100_recv(struct ftmac100_data *priv)
158 {
159 	struct ftmac100_rxdes *curr_des;
160 	unsigned short rxlen;
161 
162 	curr_des = &priv->rxdes[priv->rx_index];
163 	if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
164 		return 0;
165 
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)) {
171 		return 0;
172 	}
173 
174 	rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
175 	invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
176 	debug ("%s(): RX buffer %d, %x received\n",
177 	       __func__, priv->rx_index, rxlen);
178 
179 	return rxlen;
180 }
181 
182 /*
183  * Send a data block via Ethernet
184  */
185 static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
186 {
187 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
188 	struct ftmac100_txdes *curr_des = priv->txdes;
189 	ulong start;
190 
191 	if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
192 		debug ("%s(): no TX descriptor available\n", __func__);
193 		return -1;
194 	}
195 
196 	debug ("%s(%x, %x)\n", __func__, (int)packet, length);
197 
198 	length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
199 
200 	/* initiate a transmit sequence */
201 
202 	flush_dcache_range((u32)packet,(u32)packet+length);
203 	curr_des->txdes2 = (unsigned int)packet;	/* TXBUF_BADR */
204 
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 
218 	start = get_timer(0);
219 	while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
220 		if (get_timer(start) >= 5) {
221 			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 
231 #ifndef CONFIG_DM_ETH
232 /*
233  * disable transmitter, receiver
234  */
235 static void ftmac100_halt(struct eth_device *dev)
236 {
237 	struct ftmac100_data *priv = dev->priv;
238 	return _ftmac100_halt(priv);
239 }
240 
241 static 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 
247 static 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  */
264 static 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  */
273 static 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 
279 int ftmac100_initialize (bd_t *bd)
280 {
281 	struct eth_device *dev;
282 	struct ftmac100_data *priv;
283 	dev = malloc (sizeof *dev);
284 	if (!dev) {
285 		printf ("%s(): failed to allocate dev\n", __func__);
286 		goto out;
287 	}
288 	/* Transmit and receive descriptors should align to 16 bytes */
289 	priv = memalign (16, sizeof (struct ftmac100_data));
290 	if (!priv) {
291 		printf ("%s(): failed to allocate priv\n", __func__);
292 		goto free_dev;
293 	}
294 	memset (dev, 0, sizeof (*dev));
295 	memset (priv, 0, sizeof (*priv));
296 
297 	strcpy(dev->name, "FTMAC100");
298 	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;
304 	priv->iobase	= dev->iobase;
305 	eth_register (dev);
306 
307 	return 1;
308 
309 free_dev:
310 	free (dev);
311 out:
312 	return 0;
313 }
314 #endif
315 
316 #ifdef CONFIG_DM_ETH
317 static 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 
325 static void ftmac100_stop(struct udevice *dev)
326 {
327 	struct ftmac100_data *priv = dev_get_priv(dev);
328 	_ftmac100_halt(priv);
329 }
330 
331 static 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 
339 static 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)
347 		*packetp = (void *)curr_des->rxdes2;
348 
349 	return len ? len : -EAGAIN;
350 }
351 
352 static 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 
359 int ftmac100_read_rom_hwaddr(struct udevice *dev)
360 {
361 	struct eth_pdata *pdata = dev_get_platdata(dev);
362 	eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
363 	return 0;
364 }
365 
366 static 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 
394 static 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;
399 	pdata->iobase = devfdt_get_addr(dev);
400 	priv->iobase = pdata->iobase;
401 	mac = dtbmacaddr(0);
402 	if (mac)
403 		memcpy(pdata->enetaddr , mac , 6);
404 
405 	return 0;
406 }
407 
408 static 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 
415 static int ftmac100_bind(struct udevice *dev)
416 {
417 	return device_set_name(dev, dev->name);
418 }
419 
420 static 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 
428 static const struct udevice_id ftmac100_ids[] = {
429 	{ .compatible = "andestech,atmac100" },
430 	{ }
431 };
432 
433 U_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
446