xref: /openbmc/u-boot/drivers/net/ftmac100.c (revision 6183b295)
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 		;
44 }
45 
46 /*
47  * Set MAC address
48  */
49 static void ftmac100_set_mac(struct ftmac100_data *priv ,
50 	const unsigned char *mac)
51 {
52 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
53 	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 
62 /*
63  * Disable MAC
64  */
65 static void _ftmac100_halt(struct ftmac100_data *priv)
66 {
67 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
68 	debug ("%s()\n", __func__);
69 	writel (0, &ftmac100->maccr);
70 }
71 
72 /*
73  * Initialize MAC
74  */
75 static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
76 {
77 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
78 	struct ftmac100_txdes *txdes = priv->txdes;
79 	struct ftmac100_rxdes *rxdes = priv->rxdes;
80 	unsigned int maccr;
81 	int i;
82 
83 	debug ("%s()\n", __func__);
84 
85 	ftmac100_reset(priv);
86 
87 	/* set the ethernet address */
88 	ftmac100_set_mac(priv, enetaddr);
89 
90 
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 */
104 		rxdes[i].rxdes2 = (unsigned int)net_rx_packets[i];
105 		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 /*
138  * Free receiving buffer
139  */
140 static int _ftmac100_free_pkt(struct ftmac100_data *priv)
141 {
142 	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  */
153 static int __ftmac100_recv(struct ftmac100_data *priv)
154 {
155 	struct ftmac100_rxdes *curr_des;
156 	unsigned short rxlen;
157 
158 	curr_des = &priv->rxdes[priv->rx_index];
159 
160 	if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
161 		return 0;
162 
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)) {
168 		return 0;
169 	}
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 
176 	return rxlen;
177 }
178 
179 /*
180  * Send a data block via Ethernet
181  */
182 static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
183 {
184 	struct ftmac100 *ftmac100 = (struct ftmac100 *)priv->iobase;
185 	struct ftmac100_txdes *curr_des = priv->txdes;
186 	ulong start;
187 
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 
214 	start = get_timer(0);
215 	while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
216 		if (get_timer(start) >= 5) {
217 			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 
227 #ifndef CONFIG_DM_ETH
228 /*
229  * disable transmitter, receiver
230  */
231 static void ftmac100_halt(struct eth_device *dev)
232 {
233 	struct ftmac100_data *priv = dev->priv;
234 	return _ftmac100_halt(priv);
235 }
236 
237 static 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 
243 static 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  */
260 static 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  */
269 static 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 
275 int ftmac100_initialize (bd_t *bd)
276 {
277 	struct eth_device *dev;
278 	struct ftmac100_data *priv;
279 	dev = malloc (sizeof *dev);
280 	if (!dev) {
281 		printf ("%s(): failed to allocate dev\n", __func__);
282 		goto out;
283 	}
284 	/* Transmit and receive descriptors should align to 16 bytes */
285 	priv = memalign (16, sizeof (struct ftmac100_data));
286 	if (!priv) {
287 		printf ("%s(): failed to allocate priv\n", __func__);
288 		goto free_dev;
289 	}
290 	memset (dev, 0, sizeof (*dev));
291 	memset (priv, 0, sizeof (*priv));
292 
293 	strcpy(dev->name, "FTMAC100");
294 	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;
300 	priv->iobase	= dev->iobase;
301 	eth_register (dev);
302 
303 	return 1;
304 
305 free_dev:
306 	free (dev);
307 out:
308 	return 0;
309 }
310 #endif
311 
312 #ifdef CONFIG_DM_ETH
313 static 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 
321 static void ftmac100_stop(struct udevice *dev)
322 {
323 	struct ftmac100_data *priv = dev_get_priv(dev);
324 	_ftmac100_halt(priv);
325 }
326 
327 static 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 
335 static 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 
348 static 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 
355 int 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 
362 static 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 
390 static 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;
395 	pdata->iobase = dev_get_addr(dev);
396 	priv->iobase = pdata->iobase;
397 	mac = dtbmacaddr(0);
398 	if (mac)
399 		memcpy(pdata->enetaddr , mac , 6);
400 
401 	return 0;
402 }
403 
404 static 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 
411 static int ftmac100_bind(struct udevice *dev)
412 {
413 	return device_set_name(dev, dev->name);
414 }
415 
416 static 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 
424 static const struct udevice_id ftmac100_ids[] = {
425 	{ .compatible = "andestech,atmac100" },
426 	{ }
427 };
428 
429 U_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
442