1 // SPDX-License-Identifier: GPL-2.0+
16 #include <asm/global_data.h>
18 #include <linux/delay.h>
19 #include <linux/mii.h>
21 #include "pic32_eth.h"
23 #define MAX_RX_BUF_SIZE 1536
24 #define MAX_RX_DESCR PKTBUFSRX
25 #define MAX_TX_DESCR 2
27 DECLARE_GLOBAL_DATA_PTR;
30 struct eth_dma_desc rxd_ring[MAX_RX_DESCR];
31 struct eth_dma_desc txd_ring[MAX_TX_DESCR];
32 u32 rxd_idx; /* index of RX desc to read */
34 struct pic32_ectl_regs *ectl_regs;
35 struct pic32_emac_regs *emac_regs;
37 struct phy_device *phydev;
38 phy_interface_t phyif;
40 struct gpio_desc rst_gpio;
43 void __weak board_netphy_reset(void *dev)
45 struct pic32eth_dev *priv = dev;
47 if (!dm_gpio_is_valid(&priv->rst_gpio))
51 dm_gpio_set_value(&priv->rst_gpio, 0);
53 dm_gpio_set_value(&priv->rst_gpio, 1);
57 /* Initialize mii(MDIO) interface, discover which PHY is
58 * attached to the device, and configure it properly.
60 static int pic32_mii_init(struct pic32eth_dev *priv)
62 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
63 struct pic32_emac_regs *emac_p = priv->emac_regs;
66 board_netphy_reset(priv);
68 /* disable RX, TX & all transactions */
69 writel(ETHCON_ON | ETHCON_TXRTS | ETHCON_RXEN, &ectl_p->con1.clr);
72 wait_for_bit_le32(&ectl_p->stat.raw, ETHSTAT_BUSY, false,
73 CONFIG_SYS_HZ, false);
75 /* turn controller ON to access PHY over MII */
76 writel(ETHCON_ON, &ectl_p->con1.set);
81 writel(EMAC_SOFTRESET, &emac_p->cfg1.set); /* reset assert */
83 writel(EMAC_SOFTRESET, &emac_p->cfg1.clr); /* reset deassert */
85 /* initialize MDIO/MII */
86 if (priv->phyif == PHY_INTERFACE_MODE_RMII) {
87 writel(EMAC_RMII_RESET, &emac_p->supp.set);
89 writel(EMAC_RMII_RESET, &emac_p->supp.clr);
92 return pic32_mdio_init(PIC32_MDIO_NAME, (ulong)&emac_p->mii);
95 static int pic32_phy_init(struct pic32eth_dev *priv, struct udevice *dev)
99 mii = miiphy_get_dev_by_name(PIC32_MDIO_NAME);
101 /* find & connect PHY */
102 priv->phydev = phy_connect(mii, priv->phy_addr,
105 printf("%s: %s: Error, PHY connect\n", __FILE__, __func__);
109 /* Wait for phy to complete reset */
112 /* configure supported modes */
113 priv->phydev->supported = SUPPORTED_10baseT_Half |
114 SUPPORTED_10baseT_Full |
115 SUPPORTED_100baseT_Half |
116 SUPPORTED_100baseT_Full |
119 priv->phydev->advertising = ADVERTISED_10baseT_Half |
120 ADVERTISED_10baseT_Full |
121 ADVERTISED_100baseT_Half |
122 ADVERTISED_100baseT_Full |
125 priv->phydev->autoneg = AUTONEG_ENABLE;
130 /* Configure MAC based on negotiated speed and duplex
133 static int pic32_mac_adjust_link(struct pic32eth_dev *priv)
135 struct phy_device *phydev = priv->phydev;
136 struct pic32_emac_regs *emac_p = priv->emac_regs;
139 printf("%s: No link.\n", phydev->dev->name);
143 if (phydev->duplex) {
144 writel(EMAC_FULLDUP, &emac_p->cfg2.set);
145 writel(FULLDUP_GAP_TIME, &emac_p->ipgt.raw);
147 writel(EMAC_FULLDUP, &emac_p->cfg2.clr);
148 writel(HALFDUP_GAP_TIME, &emac_p->ipgt.raw);
151 switch (phydev->speed) {
153 writel(EMAC_RMII_SPD100, &emac_p->supp.set);
156 writel(EMAC_RMII_SPD100, &emac_p->supp.clr);
159 printf("%s: Speed was bad\n", phydev->dev->name);
163 printf("pic32eth: PHY is %s with %dbase%s, %s\n",
164 phydev->drv->name, phydev->speed,
165 (phydev->port == PORT_TP) ? "T" : "X",
166 (phydev->duplex) ? "full" : "half");
171 static void pic32_mac_init(struct pic32eth_dev *priv, u8 *macaddr)
173 struct pic32_emac_regs *emac_p = priv->emac_regs;
177 v = EMAC_TXPAUSE | EMAC_RXPAUSE | EMAC_RXENABLE;
178 writel(v, &emac_p->cfg1.raw);
180 v = EMAC_EXCESS | EMAC_AUTOPAD | EMAC_PADENABLE |
181 EMAC_CRCENABLE | EMAC_LENGTHCK | EMAC_FULLDUP;
182 writel(v, &emac_p->cfg2.raw);
184 /* recommended back-to-back inter-packet gap for 10 Mbps half duplex */
185 writel(HALFDUP_GAP_TIME, &emac_p->ipgt.raw);
187 /* recommended non-back-to-back interpacket gap is 0xc12 */
188 writel(0xc12, &emac_p->ipgr.raw);
190 /* recommended collision window retry limit is 0x370F */
191 writel(0x370f, &emac_p->clrt.raw);
193 /* set maximum frame length: allow VLAN tagged frame */
194 writel(0x600, &emac_p->maxf.raw);
196 /* set the mac address */
197 writel(macaddr[0] | (macaddr[1] << 8), &emac_p->sa2.raw);
198 writel(macaddr[2] | (macaddr[3] << 8), &emac_p->sa1.raw);
199 writel(macaddr[4] | (macaddr[5] << 8), &emac_p->sa0.raw);
201 /* default, enable 10 Mbps operation */
202 writel(EMAC_RMII_SPD100, &emac_p->supp.clr);
204 /* wait until link status UP or deadline elapsed */
205 expire = get_ticks() + get_tbclk() * 2;
206 for (; get_ticks() < expire;) {
207 stat = phy_read(priv->phydev, priv->phy_addr, MII_BMSR);
208 if (stat & BMSR_LSTATUS)
212 if (!(stat & BMSR_LSTATUS))
213 printf("MAC: Link is DOWN!\n");
215 /* delay to stabilize before any tx/rx */
219 static void pic32_mac_reset(struct pic32eth_dev *priv)
221 struct pic32_emac_regs *emac_p = priv->emac_regs;
225 writel(EMAC_SOFTRESET, &emac_p->cfg1.raw);
229 writel(0, &emac_p->cfg1.raw);
232 mii = priv->phydev->bus;
233 if (mii && mii->reset)
237 /* initializes the MAC and PHY, then establishes a link */
238 static void pic32_ctrl_reset(struct pic32eth_dev *priv)
240 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
243 /* disable RX, TX & any other transactions */
244 writel(ETHCON_ON | ETHCON_TXRTS | ETHCON_RXEN, &ectl_p->con1.clr);
247 wait_for_bit_le32(&ectl_p->stat.raw, ETHSTAT_BUSY, false,
248 CONFIG_SYS_HZ, false);
249 /* decrement received buffcnt to zero. */
250 while (readl(&ectl_p->stat.raw) & ETHSTAT_BUFCNT)
251 writel(ETHCON_BUFCDEC, &ectl_p->con1.set);
253 /* clear any existing interrupt event */
254 writel(0xffffffff, &ectl_p->irq.clr);
256 /* clear RX/TX start address */
257 writel(0xffffffff, &ectl_p->txst.clr);
258 writel(0xffffffff, &ectl_p->rxst.clr);
260 /* clear the receive filters */
261 writel(0x00ff, &ectl_p->rxfc.clr);
263 /* set the receive filters
264 * ETH_FILT_CRC_ERR_REJECT
265 * ETH_FILT_RUNT_REJECT
266 * ETH_FILT_UCAST_ACCEPT
267 * ETH_FILT_MCAST_ACCEPT
268 * ETH_FILT_BCAST_ACCEPT
270 v = ETHRXFC_BCEN | ETHRXFC_MCEN | ETHRXFC_UCEN |
271 ETHRXFC_RUNTEN | ETHRXFC_CRCOKEN;
272 writel(v, &ectl_p->rxfc.set);
274 /* turn controller ON to access PHY over MII */
275 writel(ETHCON_ON, &ectl_p->con1.set);
278 static void pic32_rx_desc_init(struct pic32eth_dev *priv)
280 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
281 struct eth_dma_desc *rxd;
285 for (idx = 0; idx < MAX_RX_DESCR; idx++) {
286 rxd = &priv->rxd_ring[idx];
289 rxd->hdr = EDH_NPV | EDH_EOWN | EDH_STICKY;
291 /* packet buffer address */
292 rxd->data_buff = virt_to_phys(net_rx_packets[idx]);
294 /* link to next desc */
295 rxd->next_ed = virt_to_phys(rxd + 1);
301 /* decrement bufcnt */
302 writel(ETHCON_BUFCDEC, &ectl_p->con1.set);
305 /* link last descr to beginning of list */
306 rxd->next_ed = virt_to_phys(&priv->rxd_ring[0]);
309 flush_dcache_range((ulong)priv->rxd_ring,
310 (ulong)priv->rxd_ring + sizeof(priv->rxd_ring));
312 /* set rx desc-ring start address */
313 writel((ulong)virt_to_phys(&priv->rxd_ring[0]), &ectl_p->rxst.raw);
316 bufsz = readl(&ectl_p->con2.raw);
317 bufsz &= ~(ETHCON_RXBUFSZ << ETHCON_RXBUFSZ_SHFT);
318 bufsz |= ((MAX_RX_BUF_SIZE / 16) << ETHCON_RXBUFSZ_SHFT);
319 writel(bufsz, &ectl_p->con2.raw);
321 /* enable the receiver in hardware which allows hardware
322 * to DMA received pkts to the descriptor pointer address.
324 writel(ETHCON_RXEN, &ectl_p->con1.set);
327 static int pic32_eth_start(struct udevice *dev)
329 struct eth_pdata *pdata = dev_get_plat(dev);
330 struct pic32eth_dev *priv = dev_get_priv(dev);
333 pic32_ctrl_reset(priv);
336 pic32_mac_reset(priv);
339 phy_config(priv->phydev);
342 pic32_mac_init(priv, &pdata->enetaddr[0]);
344 /* init RX descriptor; TX descriptors are handled in xmit */
345 pic32_rx_desc_init(priv);
347 /* Start up & update link status of PHY */
348 phy_startup(priv->phydev);
350 /* adjust mac with phy link status */
351 return pic32_mac_adjust_link(priv);
354 static void pic32_eth_stop(struct udevice *dev)
356 struct pic32eth_dev *priv = dev_get_priv(dev);
357 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
358 struct pic32_emac_regs *emac_p = priv->emac_regs;
360 /* Reset the phy if the controller is enabled */
361 if (readl(&ectl_p->con1.raw) & ETHCON_ON)
362 phy_reset(priv->phydev);
364 /* Shut down the PHY */
365 phy_shutdown(priv->phydev);
368 writel(ETHCON_TXRTS | ETHCON_RXEN, &ectl_p->con1.clr);
372 writel(EMAC_SOFTRESET, &emac_p->cfg1.raw);
375 writel(0, &emac_p->cfg1.raw);
378 /* disable controller */
379 writel(ETHCON_ON, &ectl_p->con1.clr);
382 /* wait until everything is down */
383 wait_for_bit_le32(&ectl_p->stat.raw, ETHSTAT_BUSY, false,
384 2 * CONFIG_SYS_HZ, false);
386 /* clear any existing interrupt event */
387 writel(0xffffffff, &ectl_p->irq.clr);
390 static int pic32_eth_send(struct udevice *dev, void *packet, int length)
392 struct pic32eth_dev *priv = dev_get_priv(dev);
393 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
394 struct eth_dma_desc *txd;
397 txd = &priv->txd_ring[0];
399 /* set proper flags & length in descriptor header */
400 txd->hdr = EDH_SOP | EDH_EOP | EDH_EOWN | EDH_BCOUNT(length);
402 /* pass buffer address to hardware */
403 txd->data_buff = virt_to_phys(packet);
405 debug("%s: %d / .hdr %x, .data_buff %x, .stat %x, .nexted %x\n",
406 __func__, __LINE__, txd->hdr, txd->data_buff, txd->stat2,
409 /* cache flush (packet) */
410 flush_dcache_range((ulong)packet, (ulong)packet + length);
412 /* cache flush (txd) */
413 flush_dcache_range((ulong)txd, (ulong)txd + sizeof(*txd));
415 /* pass descriptor table base to h/w */
416 writel(virt_to_phys(txd), &ectl_p->txst.raw);
418 /* ready to send enabled, hardware can now send the packet(s) */
419 writel(ETHCON_TXRTS | ETHCON_ON, &ectl_p->con1.set);
421 /* wait until tx has completed and h/w has released ownership
422 * of the tx descriptor or timeout elapsed.
424 deadline = get_ticks() + get_tbclk();
427 if (get_ticks() > deadline)
434 if (readl(&ectl_p->con1.raw) & ETHCON_TXRTS) {
439 /* h/w not released ownership yet? */
440 invalidate_dcache_range((ulong)txd, (ulong)txd + sizeof(*txd));
441 if (!(txd->hdr & EDH_EOWN))
448 static int pic32_eth_recv(struct udevice *dev, int flags, uchar **packetp)
450 struct pic32eth_dev *priv = dev_get_priv(dev);
451 struct eth_dma_desc *rxd;
452 u32 idx = priv->rxd_idx;
455 /* find the next ready to receive */
456 rxd = &priv->rxd_ring[idx];
458 invalidate_dcache_range((ulong)rxd, (ulong)rxd + sizeof(*rxd));
459 /* check if owned by MAC */
460 if (rxd->hdr & EDH_EOWN)
463 /* Sanity check on header: SOP and EOP */
464 if ((rxd->hdr & (EDH_SOP | EDH_EOP)) != (EDH_SOP | EDH_EOP)) {
465 printf("%s: %s, rx pkt across multiple descr\n",
470 debug("%s: %d /idx %i, hdr=%x, data_buff %x, stat %x, nexted %x\n",
471 __func__, __LINE__, idx, rxd->hdr,
472 rxd->data_buff, rxd->stat2, rxd->next_ed);
474 /* Sanity check on rx_stat: OK, CRC */
475 if (!RSV_RX_OK(rxd->stat2) || RSV_CRC_ERR(rxd->stat2)) {
476 debug("%s: %s: Error, rx problem detected\n",
481 /* invalidate dcache */
482 rx_count = RSV_RX_COUNT(rxd->stat2);
483 invalidate_dcache_range((ulong)net_rx_packets[idx],
484 (ulong)net_rx_packets[idx] + rx_count);
486 /* Pass the packet to protocol layer */
487 *packetp = net_rx_packets[idx];
489 /* increment number of bytes rcvd (ignore CRC) */
493 static int pic32_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
495 struct pic32eth_dev *priv = dev_get_priv(dev);
496 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
497 struct eth_dma_desc *rxd;
498 int idx = priv->rxd_idx;
501 if (packet != net_rx_packets[idx]) {
502 printf("rxd_id %d: packet is not matched,\n", idx);
506 /* prepare for receive */
507 rxd = &priv->rxd_ring[idx];
508 rxd->hdr = EDH_STICKY | EDH_NPV | EDH_EOWN;
510 flush_dcache_range((ulong)rxd, (ulong)rxd + sizeof(*rxd));
512 /* decrement rx pkt count */
513 writel(ETHCON_BUFCDEC, &ectl_p->con1.set);
515 debug("%s: %d / idx %i, hdr %x, data_buff %x, stat %x, nexted %x\n",
516 __func__, __LINE__, idx, rxd->hdr, rxd->data_buff,
517 rxd->stat2, rxd->next_ed);
519 priv->rxd_idx = (priv->rxd_idx + 1) % MAX_RX_DESCR;
524 static const struct eth_ops pic32_eth_ops = {
525 .start = pic32_eth_start,
526 .send = pic32_eth_send,
527 .recv = pic32_eth_recv,
528 .free_pkt = pic32_eth_free_pkt,
529 .stop = pic32_eth_stop,
532 static int pic32_eth_probe(struct udevice *dev)
534 struct eth_pdata *pdata = dev_get_plat(dev);
535 struct pic32eth_dev *priv = dev_get_priv(dev);
536 void __iomem *iobase;
542 addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
544 if (addr == FDT_ADDR_T_NONE)
547 iobase = ioremap(addr, size);
548 pdata->iobase = (phys_addr_t)addr;
551 pdata->phy_interface = dev_read_phy_mode(dev);
552 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
556 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
559 phy_addr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
562 gpio_request_by_name_nodev(dev_ofnode(dev), "reset-gpios", 0,
563 &priv->rst_gpio, GPIOD_IS_OUT);
565 priv->phyif = pdata->phy_interface;
566 priv->phy_addr = phy_addr;
567 priv->ectl_regs = iobase;
568 priv->emac_regs = iobase + PIC32_EMAC1CFG1;
570 pic32_mii_init(priv);
572 return pic32_phy_init(priv, dev);
575 static int pic32_eth_remove(struct udevice *dev)
577 struct pic32eth_dev *priv = dev_get_priv(dev);
580 dm_gpio_free(dev, &priv->rst_gpio);
581 phy_shutdown(priv->phydev);
583 bus = miiphy_get_dev_by_name(PIC32_MDIO_NAME);
584 mdio_unregister(bus);
586 iounmap(priv->ectl_regs);
590 static const struct udevice_id pic32_eth_ids[] = {
591 { .compatible = "microchip,pic32mzda-eth" },
595 U_BOOT_DRIVER(pic32_ethernet) = {
596 .name = "pic32_ethernet",
598 .of_match = pic32_eth_ids,
599 .probe = pic32_eth_probe,
600 .remove = pic32_eth_remove,
601 .ops = &pic32_eth_ops,
602 .priv_auto = sizeof(struct pic32eth_dev),
603 .plat_auto = sizeof(struct eth_pdata),