1 // SPDX-License-Identifier: GPL-2.0+
3 * Faraday FTMAC100 Ethernet
5 * (C) Copyright 2009 Faraday Technology
17 #include <dm/device_compat.h>
18 #include <asm/global_data.h>
19 #include <linux/delay.h>
21 #include <linux/iopoll.h>
26 DECLARE_GLOBAL_DATA_PTR;
30 /* Timeout for a mdio read/write operation */
31 #define FTMAC100_MDIO_TIMEOUT_USEC 10000
33 struct ftmac100_data {
34 struct ftmac100_txdes txdes[1];
35 struct ftmac100_rxdes rxdes[PKTBUFSRX];
38 struct ftmac100 *ftmac100;
45 static void ftmac100_reset(struct ftmac100_data *priv)
47 struct ftmac100 *ftmac100 = priv->ftmac100;
49 debug ("%s()\n", __func__);
51 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
53 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
56 * When soft reset complete, write mac address immediately maybe fail somehow
57 * Wait for a while can avoid this problem
65 static void ftmac100_set_mac(struct ftmac100_data *priv ,
66 const unsigned char *mac)
68 struct ftmac100 *ftmac100 = priv->ftmac100;
69 unsigned int maddr = mac[0] << 8 | mac[1];
70 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
72 debug ("%s(%x %x)\n", __func__, maddr, laddr);
74 writel (maddr, &ftmac100->mac_madr);
75 writel (laddr, &ftmac100->mac_ladr);
81 static void _ftmac100_halt(struct ftmac100_data *priv)
83 struct ftmac100 *ftmac100 = priv->ftmac100;
84 debug ("%s()\n", __func__);
85 writel (0, &ftmac100->maccr);
91 static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
93 struct ftmac100 *ftmac100 = priv->ftmac100;
94 struct ftmac100_txdes *txdes = priv->txdes;
95 struct ftmac100_rxdes *rxdes = priv->rxdes;
98 debug ("%s()\n", __func__);
100 ftmac100_reset(priv);
102 /* set the ethernet address */
103 ftmac100_set_mac(priv, enetaddr);
106 /* disable all interrupts */
108 writel (0, &ftmac100->imr);
110 /* initialize descriptors */
114 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
115 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
117 for (i = 0; i < PKTBUFSRX; i++) {
119 rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
120 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
121 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
126 writel ((unsigned long)txdes, &ftmac100->txr_badr);
130 writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
132 /* poll receive descriptor automatically */
134 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
136 /* enable transmitter, receiver */
138 maccr = FTMAC100_MACCR_XMT_EN |
139 FTMAC100_MACCR_RCV_EN |
140 FTMAC100_MACCR_XDMA_EN |
141 FTMAC100_MACCR_RDMA_EN |
142 FTMAC100_MACCR_CRC_APD |
143 FTMAC100_MACCR_ENRX_IN_HALFTX |
144 FTMAC100_MACCR_RX_RUNT |
145 FTMAC100_MACCR_RX_BROADPKT;
147 writel (maccr, &ftmac100->maccr);
153 * Free receiving buffer
155 static int _ftmac100_free_pkt(struct ftmac100_data *priv)
157 struct ftmac100_rxdes *curr_des;
158 curr_des = &priv->rxdes[priv->rx_index];
159 /* release buffer to DMA */
160 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
161 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
166 * Receive a data block via Ethernet
168 static int __ftmac100_recv(struct ftmac100_data *priv)
170 struct ftmac100_rxdes *curr_des;
171 unsigned short rxlen;
173 curr_des = &priv->rxdes[priv->rx_index];
174 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
177 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
178 FTMAC100_RXDES0_CRC_ERR |
179 FTMAC100_RXDES0_FTL |
180 FTMAC100_RXDES0_RUNT |
181 FTMAC100_RXDES0_RX_ODD_NB)) {
185 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
186 invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
187 debug ("%s(): RX buffer %d, %x received\n",
188 __func__, priv->rx_index, rxlen);
194 * Send a data block via Ethernet
196 static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
198 struct ftmac100 *ftmac100 = priv->ftmac100;
199 struct ftmac100_txdes *curr_des = priv->txdes;
202 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
203 debug ("%s(): no TX descriptor available\n", __func__);
207 debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
209 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
211 /* initiate a transmit sequence */
213 flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
214 curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
216 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
217 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
218 FTMAC100_TXDES1_LTS |
219 FTMAC100_TXDES1_TXBUF_SIZE (length);
221 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
225 writel (1, &ftmac100->txpd);
227 /* wait for transfer to succeed */
229 start = get_timer(0);
230 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
231 if (get_timer(start) >= 5) {
232 debug ("%s(): timed out\n", __func__);
237 debug ("%s(): packet sent\n", __func__);
242 static int ftmac100_start(struct udevice *dev)
244 struct eth_pdata *plat = dev_get_plat(dev);
245 struct ftmac100_data *priv = dev_get_priv(dev);
247 return _ftmac100_init(priv, plat->enetaddr);
250 static void ftmac100_stop(struct udevice *dev)
252 struct ftmac100_data *priv = dev_get_priv(dev);
253 _ftmac100_halt(priv);
256 static int ftmac100_send(struct udevice *dev, void *packet, int length)
258 struct ftmac100_data *priv = dev_get_priv(dev);
260 ret = _ftmac100_send(priv , packet , length);
261 return ret ? 0 : -ETIMEDOUT;
264 static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
266 struct ftmac100_data *priv = dev_get_priv(dev);
267 struct ftmac100_rxdes *curr_des;
268 curr_des = &priv->rxdes[priv->rx_index];
270 len = __ftmac100_recv(priv);
272 *packetp = (uchar *)(unsigned long)curr_des->rxdes2;
274 return len ? len : -EAGAIN;
277 static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
279 struct ftmac100_data *priv = dev_get_priv(dev);
280 _ftmac100_free_pkt(priv);
284 int ftmac100_read_rom_hwaddr(struct udevice *dev)
286 struct eth_pdata *pdata = dev_get_plat(dev);
287 eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
291 static const char *dtbmacaddr(u32 ifno)
297 if (gd->fdt_blob == NULL) {
298 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
301 node = fdt_path_offset(gd->fdt_blob, "/aliases");
305 sprintf(enet, "ethernet%d", ifno);
306 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
308 printf("no alias for %s\n", enet);
311 node = fdt_path_offset(gd->fdt_blob, path);
312 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
313 if (mac && is_valid_ethaddr((u8 *)mac))
319 static int ftmac100_of_to_plat(struct udevice *dev)
321 struct ftmac100_data *priv = dev_get_priv(dev);
322 struct eth_pdata *pdata = dev_get_plat(dev);
324 pdata->iobase = dev_read_addr(dev);
325 priv->ftmac100 = phys_to_virt(pdata->iobase);
328 memcpy(pdata->enetaddr , mac , 6);
334 * struct mii_bus functions
336 static int ftmac100_mdio_read(struct mii_dev *bus, int addr, int devad,
339 struct ftmac100_data *priv = bus->priv;
340 struct ftmac100 *ftmac100 = priv->ftmac100;
341 int phycr = FTMAC100_PHYCR_PHYAD(addr) |
342 FTMAC100_PHYCR_REGAD(reg) |
343 FTMAC100_PHYCR_MIIRD;
346 writel(phycr, &ftmac100->phycr);
348 ret = readl_poll_timeout(&ftmac100->phycr, phycr,
349 !(phycr & FTMAC100_PHYCR_MIIRD),
350 FTMAC100_MDIO_TIMEOUT_USEC);
352 pr_err("%s: mdio read failed (addr=0x%x reg=0x%x)\n",
353 bus->name, addr, reg);
355 ret = phycr & FTMAC100_PHYCR_MIIRDATA;
360 static int ftmac100_mdio_write(struct mii_dev *bus, int addr, int devad,
363 struct ftmac100_data *priv = bus->priv;
364 struct ftmac100 *ftmac100 = priv->ftmac100;
365 int phycr = FTMAC100_PHYCR_PHYAD(addr) |
366 FTMAC100_PHYCR_REGAD(reg) |
367 FTMAC100_PHYCR_MIIWR;
370 writel(value, &ftmac100->phywdata);
371 writel(phycr, &ftmac100->phycr);
373 ret = readl_poll_timeout(&ftmac100->phycr, phycr,
374 !(phycr & FTMAC100_PHYCR_MIIWR),
375 FTMAC100_MDIO_TIMEOUT_USEC);
377 pr_err("%s: mdio write failed (addr=0x%x reg=0x%x)\n",
378 bus->name, addr, reg);
383 static int ftmac100_mdio_init(struct udevice *dev)
385 struct ftmac100_data *priv = dev_get_priv(dev);
393 bus->read = ftmac100_mdio_read;
394 bus->write = ftmac100_mdio_write;
397 ret = mdio_register_seq(bus, dev_seq(dev));
408 static int ftmac100_probe(struct udevice *dev)
410 struct ftmac100_data *priv = dev_get_priv(dev);
411 priv->name = dev->name;
414 ret = ftmac100_mdio_init(dev);
416 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
424 static int ftmac100_remove(struct udevice *dev)
426 struct ftmac100_data *priv = dev_get_priv(dev);
428 mdio_unregister(priv->bus);
429 mdio_free(priv->bus);
434 static int ftmac100_bind(struct udevice *dev)
436 return device_set_name(dev, dev->name);
439 static const struct eth_ops ftmac100_ops = {
440 .start = ftmac100_start,
441 .send = ftmac100_send,
442 .recv = ftmac100_recv,
443 .stop = ftmac100_stop,
444 .free_pkt = ftmac100_free_pkt,
447 static const struct udevice_id ftmac100_ids[] = {
448 { .compatible = "andestech,atmac100" },
452 U_BOOT_DRIVER(ftmac100) = {
455 .of_match = ftmac100_ids,
456 .bind = ftmac100_bind,
457 .of_to_plat = ftmac100_of_to_plat,
458 .probe = ftmac100_probe,
459 .remove = ftmac100_remove,
460 .ops = &ftmac100_ops,
461 .priv_auto = sizeof(struct ftmac100_data),
462 .plat_auto = sizeof(struct eth_pdata),
463 .flags = DM_FLAG_ALLOC_PRIV_DMA,