1 // SPDX-License-Identifier: GPL-2.0+
3 * Faraday FTGMAC100 Ethernet
5 * (C) Copyright 2009 Faraday Technology
6 * Po-Yu Chuang <ratbert@faraday-tech.com>
8 * (C) Copyright 2010 Andes Technology
9 * Macpaul Lin <macpaul@andestech.com>
11 * Copyright (C) 2018, IBM Corporation.
20 #include <linux/iopoll.h>
22 #include "ftgmac100.h"
24 /* Min frame ethernet frame size without FCS */
27 /* Receive Buffer Size Register - HW default is 0x640 */
28 #define FTGMAC100_RBSR_DEFAULT 0x640
30 /* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
31 #define PKTBUFSTX 4 /* must be power of 2 */
33 /* Timeout for transmit */
34 #define FTGMAC100_TX_TIMEOUT_MS 1000
36 /* Timeout for a mdio read/write operation */
37 #define FTGMAC100_MDIO_TIMEOUT_USEC 10000
40 * MDC clock cycle threshold
42 * 20us * 100 = 2ms > (1 / 2.5Mhz) * 0x34
44 #define MDC_CYCTHR 0x34
47 * ftgmac100 model variants
49 enum ftgmac100_model {
50 FTGMAC100_MODEL_FARADAY,
51 FTGMAC100_MODEL_ASPEED,
55 * struct ftgmac100_data - private data for the FTGMAC100 driver
57 * @iobase: The base address of the hardware registers
58 * @txdes: The array of transmit descriptors
59 * @rxdes: The array of receive descriptors
60 * @tx_index: Transmit descriptor index in @txdes
61 * @rx_index: Receive descriptor index in @rxdes
62 * @phy_addr: The PHY interface address to use
63 * @phydev: The PHY device backing the MAC
65 * @phy_mode: The mode of the PHY interface (rgmii, rmii, ...)
66 * @max_speed: Maximum speed of Ethernet connection supported by MAC
67 * @clks: The bulk of clocks assigned to the device in the DT
68 * @rxdes0_edorr_mask: The bit number identifying the end of the RX ring buffer
69 * @txdes0_edotr_mask: The bit number identifying the end of the TX ring buffer
71 struct ftgmac100_data {
72 struct ftgmac100 *iobase;
74 struct ftgmac100_txdes txdes[PKTBUFSTX];
75 struct ftgmac100_rxdes rxdes[PKTBUFSRX];
80 struct phy_device *phydev;
87 /* End of RX/TX ring buffer bits. Depend on model */
88 u32 rxdes0_edorr_mask;
89 u32 txdes0_edotr_mask;
93 * struct mii_bus functions
95 static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
98 struct ftgmac100_data *priv = bus->priv;
99 struct ftgmac100 *ftgmac100 = priv->iobase;
104 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
105 FTGMAC100_PHYCR_PHYAD(phy_addr) |
106 FTGMAC100_PHYCR_REGAD(reg_addr) |
107 FTGMAC100_PHYCR_MIIRD;
108 writel(phycr, &ftgmac100->phycr);
110 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
111 !(phycr & FTGMAC100_PHYCR_MIIRD),
112 FTGMAC100_MDIO_TIMEOUT_USEC);
114 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
115 priv->phydev->dev->name, phy_addr, reg_addr);
119 data = readl(&ftgmac100->phydata);
121 return FTGMAC100_PHYDATA_MIIRDATA(data);
124 static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
125 int reg_addr, u16 value)
127 struct ftgmac100_data *priv = bus->priv;
128 struct ftgmac100 *ftgmac100 = priv->iobase;
133 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
134 FTGMAC100_PHYCR_PHYAD(phy_addr) |
135 FTGMAC100_PHYCR_REGAD(reg_addr) |
136 FTGMAC100_PHYCR_MIIWR;
137 data = FTGMAC100_PHYDATA_MIIWDATA(value);
139 writel(data, &ftgmac100->phydata);
140 writel(phycr, &ftgmac100->phycr);
142 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
143 !(phycr & FTGMAC100_PHYCR_MIIWR),
144 FTGMAC100_MDIO_TIMEOUT_USEC);
146 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
147 priv->phydev->dev->name, phy_addr, reg_addr);
153 static int ftgmac100_mdio_init(struct udevice *dev)
155 struct ftgmac100_data *priv = dev_get_priv(dev);
163 bus->read = ftgmac100_mdio_read;
164 bus->write = ftgmac100_mdio_write;
167 ret = mdio_register_seq(bus, dev->seq);
178 static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
180 struct ftgmac100 *ftgmac100 = priv->iobase;
181 struct phy_device *phydev = priv->phydev;
185 dev_err(phydev->dev, "No link\n");
189 /* read MAC control register and clear related bits */
190 maccr = readl(&ftgmac100->maccr) &
191 ~(FTGMAC100_MACCR_GIGA_MODE |
192 FTGMAC100_MACCR_FAST_MODE |
193 FTGMAC100_MACCR_FULLDUP);
195 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
196 maccr |= FTGMAC100_MACCR_GIGA_MODE;
198 if (phydev->speed == 100)
199 maccr |= FTGMAC100_MACCR_FAST_MODE;
202 maccr |= FTGMAC100_MACCR_FULLDUP;
204 /* update MII config into maccr */
205 writel(maccr, &ftgmac100->maccr);
210 static int ftgmac100_phy_init(struct udevice *dev)
212 struct ftgmac100_data *priv = dev_get_priv(dev);
213 struct phy_device *phydev;
216 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
220 phydev->supported &= PHY_GBIT_FEATURES;
221 if (priv->max_speed) {
222 ret = phy_set_supported(phydev, priv->max_speed);
226 phydev->advertising = phydev->supported;
227 priv->phydev = phydev;
236 static void ftgmac100_reset(struct ftgmac100_data *priv)
238 struct ftgmac100 *ftgmac100 = priv->iobase;
240 debug("%s()\n", __func__);
242 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
244 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
251 static int ftgmac100_set_mac(struct ftgmac100_data *priv,
252 const unsigned char *mac)
254 struct ftgmac100 *ftgmac100 = priv->iobase;
255 unsigned int maddr = mac[0] << 8 | mac[1];
256 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
258 debug("%s(%x %x)\n", __func__, maddr, laddr);
260 writel(maddr, &ftgmac100->mac_madr);
261 writel(laddr, &ftgmac100->mac_ladr);
267 * disable transmitter, receiver
269 static void ftgmac100_stop(struct udevice *dev)
271 struct ftgmac100_data *priv = dev_get_priv(dev);
272 struct ftgmac100 *ftgmac100 = priv->iobase;
274 debug("%s()\n", __func__);
276 writel(0, &ftgmac100->maccr);
278 phy_shutdown(priv->phydev);
281 static int ftgmac100_start(struct udevice *dev)
283 struct eth_pdata *plat = dev_get_platdata(dev);
284 struct ftgmac100_data *priv = dev_get_priv(dev);
285 struct ftgmac100 *ftgmac100 = priv->iobase;
286 struct phy_device *phydev = priv->phydev;
292 debug("%s()\n", __func__);
294 ftgmac100_reset(priv);
296 /* set the ethernet address */
297 ftgmac100_set_mac(priv, plat->enetaddr);
299 /* disable all interrupts */
300 writel(0, &ftgmac100->ier);
302 /* initialize descriptors */
306 for (i = 0; i < PKTBUFSTX; i++) {
307 priv->txdes[i].txdes3 = 0;
308 priv->txdes[i].txdes0 = 0;
310 priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask;
312 start = (ulong)&priv->txdes[0];
313 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
314 flush_dcache_range(start, end);
316 for (i = 0; i < PKTBUFSRX; i++) {
317 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
318 priv->rxdes[i].rxdes0 = 0;
320 priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask;
322 start = (ulong)&priv->rxdes[0];
323 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
324 flush_dcache_range(start, end);
327 writel((u32)priv->txdes, &ftgmac100->txr_badr);
330 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
332 /* poll receive descriptor automatically */
333 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
335 /* config receive buffer size register */
336 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
338 /* enable transmitter, receiver */
339 maccr = FTGMAC100_MACCR_TXMAC_EN |
340 FTGMAC100_MACCR_RXMAC_EN |
341 FTGMAC100_MACCR_TXDMA_EN |
342 FTGMAC100_MACCR_RXDMA_EN |
343 FTGMAC100_MACCR_CRC_APD |
344 FTGMAC100_MACCR_FULLDUP |
345 FTGMAC100_MACCR_RX_RUNT |
346 FTGMAC100_MACCR_RX_BROADPKT;
348 writel(maccr, &ftgmac100->maccr);
350 ret = phy_startup(phydev);
352 dev_err(phydev->dev, "Could not start PHY\n");
356 ret = ftgmac100_phy_adjust_link(priv);
358 dev_err(phydev->dev, "Could not adjust link\n");
362 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
363 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
368 static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
370 struct ftgmac100_data *priv = dev_get_priv(dev);
371 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
372 ulong des_start = (ulong)curr_des;
373 ulong des_end = des_start +
374 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
376 /* Release buffer to DMA and flush descriptor */
377 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
378 flush_dcache_range(des_start, des_end);
380 /* Move to next descriptor */
381 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
387 * Get a data block via Ethernet
389 static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
391 struct ftgmac100_data *priv = dev_get_priv(dev);
392 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
393 unsigned short rxlen;
394 ulong des_start = (ulong)curr_des;
395 ulong des_end = des_start +
396 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
397 ulong data_start = curr_des->rxdes3;
400 invalidate_dcache_range(des_start, des_end);
402 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
405 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
406 FTGMAC100_RXDES0_CRC_ERR |
407 FTGMAC100_RXDES0_FTL |
408 FTGMAC100_RXDES0_RUNT |
409 FTGMAC100_RXDES0_RX_ODD_NB)) {
413 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
415 debug("%s(): RX buffer %d, %x received\n",
416 __func__, priv->rx_index, rxlen);
418 /* Invalidate received data */
419 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
420 invalidate_dcache_range(data_start, data_end);
421 *packetp = (uchar *)data_start;
426 static u32 ftgmac100_read_txdesc(const void *desc)
428 const struct ftgmac100_txdes *txdes = desc;
429 ulong des_start = (ulong)txdes;
430 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
432 invalidate_dcache_range(des_start, des_end);
434 return txdes->txdes0;
437 BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
440 * Send a data block via Ethernet
442 static int ftgmac100_send(struct udevice *dev, void *packet, int length)
444 struct ftgmac100_data *priv = dev_get_priv(dev);
445 struct ftgmac100 *ftgmac100 = priv->iobase;
446 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
447 ulong des_start = (ulong)curr_des;
448 ulong des_end = des_start +
449 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
454 invalidate_dcache_range(des_start, des_end);
456 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
457 dev_err(dev, "no TX descriptor available\n");
461 debug("%s(%x, %x)\n", __func__, (int)packet, length);
463 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
465 curr_des->txdes3 = (unsigned int)packet;
467 /* Flush data to be sent */
468 data_start = curr_des->txdes3;
469 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
470 flush_dcache_range(data_start, data_end);
472 /* Only one segment on TXBUF */
473 curr_des->txdes0 &= priv->txdes0_edotr_mask;
474 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
475 FTGMAC100_TXDES0_LTS |
476 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
477 FTGMAC100_TXDES0_TXDMA_OWN ;
479 /* Flush modified buffer descriptor */
480 flush_dcache_range(des_start, des_end);
483 writel(1, &ftgmac100->txpd);
485 rc = wait_for_bit_ftgmac100_txdone(curr_des,
486 FTGMAC100_TXDES0_TXDMA_OWN, false,
487 FTGMAC100_TX_TIMEOUT_MS, true);
491 debug("%s(): packet sent\n", __func__);
493 /* Move to next descriptor */
494 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
499 static int ftgmac100_write_hwaddr(struct udevice *dev)
501 struct eth_pdata *pdata = dev_get_platdata(dev);
502 struct ftgmac100_data *priv = dev_get_priv(dev);
504 return ftgmac100_set_mac(priv, pdata->enetaddr);
507 static int ftgmac100_ofdata_to_platdata(struct udevice *dev)
509 struct eth_pdata *pdata = dev_get_platdata(dev);
510 struct ftgmac100_data *priv = dev_get_priv(dev);
511 const char *phy_mode;
513 pdata->iobase = devfdt_get_addr(dev);
514 pdata->phy_interface = -1;
515 phy_mode = dev_read_string(dev, "phy-mode");
517 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
518 if (pdata->phy_interface == -1) {
519 dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
523 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
525 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
526 priv->rxdes0_edorr_mask = BIT(30);
527 priv->txdes0_edotr_mask = BIT(30);
529 priv->rxdes0_edorr_mask = BIT(15);
530 priv->txdes0_edotr_mask = BIT(15);
533 return clk_get_bulk(dev, &priv->clks);
536 static int ftgmac100_probe(struct udevice *dev)
538 struct eth_pdata *pdata = dev_get_platdata(dev);
539 struct ftgmac100_data *priv = dev_get_priv(dev);
542 priv->iobase = (struct ftgmac100 *)pdata->iobase;
543 priv->phy_mode = pdata->phy_interface;
544 priv->max_speed = pdata->max_speed;
547 ret = clk_enable_bulk(&priv->clks);
551 ret = ftgmac100_mdio_init(dev);
553 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
557 ret = ftgmac100_phy_init(dev);
559 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
565 clk_release_bulk(&priv->clks);
570 static int ftgmac100_remove(struct udevice *dev)
572 struct ftgmac100_data *priv = dev_get_priv(dev);
575 mdio_unregister(priv->bus);
576 mdio_free(priv->bus);
577 clk_release_bulk(&priv->clks);
582 static const struct eth_ops ftgmac100_ops = {
583 .start = ftgmac100_start,
584 .send = ftgmac100_send,
585 .recv = ftgmac100_recv,
586 .stop = ftgmac100_stop,
587 .free_pkt = ftgmac100_free_pkt,
588 .write_hwaddr = ftgmac100_write_hwaddr,
591 static const struct udevice_id ftgmac100_ids[] = {
592 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
593 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
597 U_BOOT_DRIVER(ftgmac100) = {
600 .of_match = ftgmac100_ids,
601 .ofdata_to_platdata = ftgmac100_ofdata_to_platdata,
602 .probe = ftgmac100_probe,
603 .remove = ftgmac100_remove,
604 .ops = &ftgmac100_ops,
605 .priv_auto_alloc_size = sizeof(struct ftgmac100_data),
606 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
607 .flags = DM_FLAG_ALLOC_PRIV_DMA,