1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) 2015 Purna Chandra Mandal <purna.mandal@microchip.com>
15 #include "pic32_eth.h"
17 #define MAX_RX_BUF_SIZE 1536
18 #define MAX_RX_DESCR PKTBUFSRX
19 #define MAX_TX_DESCR 2
21 DECLARE_GLOBAL_DATA_PTR;
24 struct eth_dma_desc rxd_ring[MAX_RX_DESCR];
25 struct eth_dma_desc txd_ring[MAX_TX_DESCR];
26 u32 rxd_idx; /* index of RX desc to read */
28 struct pic32_ectl_regs *ectl_regs;
29 struct pic32_emac_regs *emac_regs;
31 struct phy_device *phydev;
32 phy_interface_t phyif;
34 struct gpio_desc rst_gpio;
37 void __weak board_netphy_reset(void *dev)
39 struct pic32eth_dev *priv = dev;
41 if (!dm_gpio_is_valid(&priv->rst_gpio))
45 dm_gpio_set_value(&priv->rst_gpio, 0);
47 dm_gpio_set_value(&priv->rst_gpio, 1);
51 /* Initialize mii(MDIO) interface, discover which PHY is
52 * attached to the device, and configure it properly.
54 static int pic32_mii_init(struct pic32eth_dev *priv)
56 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
57 struct pic32_emac_regs *emac_p = priv->emac_regs;
60 board_netphy_reset(priv);
62 /* disable RX, TX & all transactions */
63 writel(ETHCON_ON | ETHCON_TXRTS | ETHCON_RXEN, &ectl_p->con1.clr);
66 wait_for_bit_le32(&ectl_p->stat.raw, ETHSTAT_BUSY, false,
67 CONFIG_SYS_HZ, false);
69 /* turn controller ON to access PHY over MII */
70 writel(ETHCON_ON, &ectl_p->con1.set);
75 writel(EMAC_SOFTRESET, &emac_p->cfg1.set); /* reset assert */
77 writel(EMAC_SOFTRESET, &emac_p->cfg1.clr); /* reset deassert */
79 /* initialize MDIO/MII */
80 if (priv->phyif == PHY_INTERFACE_MODE_RMII) {
81 writel(EMAC_RMII_RESET, &emac_p->supp.set);
83 writel(EMAC_RMII_RESET, &emac_p->supp.clr);
86 return pic32_mdio_init(PIC32_MDIO_NAME, (ulong)&emac_p->mii);
89 static int pic32_phy_init(struct pic32eth_dev *priv, struct udevice *dev)
93 mii = miiphy_get_dev_by_name(PIC32_MDIO_NAME);
95 /* find & connect PHY */
96 priv->phydev = phy_connect(mii, priv->phy_addr,
99 printf("%s: %s: Error, PHY connect\n", __FILE__, __func__);
103 /* Wait for phy to complete reset */
106 /* configure supported modes */
107 priv->phydev->supported = SUPPORTED_10baseT_Half |
108 SUPPORTED_10baseT_Full |
109 SUPPORTED_100baseT_Half |
110 SUPPORTED_100baseT_Full |
113 priv->phydev->advertising = ADVERTISED_10baseT_Half |
114 ADVERTISED_10baseT_Full |
115 ADVERTISED_100baseT_Half |
116 ADVERTISED_100baseT_Full |
119 priv->phydev->autoneg = AUTONEG_ENABLE;
124 /* Configure MAC based on negotiated speed and duplex
127 static int pic32_mac_adjust_link(struct pic32eth_dev *priv)
129 struct phy_device *phydev = priv->phydev;
130 struct pic32_emac_regs *emac_p = priv->emac_regs;
133 printf("%s: No link.\n", phydev->dev->name);
137 if (phydev->duplex) {
138 writel(EMAC_FULLDUP, &emac_p->cfg2.set);
139 writel(FULLDUP_GAP_TIME, &emac_p->ipgt.raw);
141 writel(EMAC_FULLDUP, &emac_p->cfg2.clr);
142 writel(HALFDUP_GAP_TIME, &emac_p->ipgt.raw);
145 switch (phydev->speed) {
147 writel(EMAC_RMII_SPD100, &emac_p->supp.set);
150 writel(EMAC_RMII_SPD100, &emac_p->supp.clr);
153 printf("%s: Speed was bad\n", phydev->dev->name);
157 printf("pic32eth: PHY is %s with %dbase%s, %s\n",
158 phydev->drv->name, phydev->speed,
159 (phydev->port == PORT_TP) ? "T" : "X",
160 (phydev->duplex) ? "full" : "half");
165 static void pic32_mac_init(struct pic32eth_dev *priv, u8 *macaddr)
167 struct pic32_emac_regs *emac_p = priv->emac_regs;
171 v = EMAC_TXPAUSE | EMAC_RXPAUSE | EMAC_RXENABLE;
172 writel(v, &emac_p->cfg1.raw);
174 v = EMAC_EXCESS | EMAC_AUTOPAD | EMAC_PADENABLE |
175 EMAC_CRCENABLE | EMAC_LENGTHCK | EMAC_FULLDUP;
176 writel(v, &emac_p->cfg2.raw);
178 /* recommended back-to-back inter-packet gap for 10 Mbps half duplex */
179 writel(HALFDUP_GAP_TIME, &emac_p->ipgt.raw);
181 /* recommended non-back-to-back interpacket gap is 0xc12 */
182 writel(0xc12, &emac_p->ipgr.raw);
184 /* recommended collision window retry limit is 0x370F */
185 writel(0x370f, &emac_p->clrt.raw);
187 /* set maximum frame length: allow VLAN tagged frame */
188 writel(0x600, &emac_p->maxf.raw);
190 /* set the mac address */
191 writel(macaddr[0] | (macaddr[1] << 8), &emac_p->sa2.raw);
192 writel(macaddr[2] | (macaddr[3] << 8), &emac_p->sa1.raw);
193 writel(macaddr[4] | (macaddr[5] << 8), &emac_p->sa0.raw);
195 /* default, enable 10 Mbps operation */
196 writel(EMAC_RMII_SPD100, &emac_p->supp.clr);
198 /* wait until link status UP or deadline elapsed */
199 expire = get_ticks() + get_tbclk() * 2;
200 for (; get_ticks() < expire;) {
201 stat = phy_read(priv->phydev, priv->phy_addr, MII_BMSR);
202 if (stat & BMSR_LSTATUS)
206 if (!(stat & BMSR_LSTATUS))
207 printf("MAC: Link is DOWN!\n");
209 /* delay to stabilize before any tx/rx */
213 static void pic32_mac_reset(struct pic32eth_dev *priv)
215 struct pic32_emac_regs *emac_p = priv->emac_regs;
219 writel(EMAC_SOFTRESET, &emac_p->cfg1.raw);
223 writel(0, &emac_p->cfg1.raw);
226 mii = priv->phydev->bus;
227 if (mii && mii->reset)
231 /* initializes the MAC and PHY, then establishes a link */
232 static void pic32_ctrl_reset(struct pic32eth_dev *priv)
234 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
237 /* disable RX, TX & any other transactions */
238 writel(ETHCON_ON | ETHCON_TXRTS | ETHCON_RXEN, &ectl_p->con1.clr);
241 wait_for_bit_le32(&ectl_p->stat.raw, ETHSTAT_BUSY, false,
242 CONFIG_SYS_HZ, false);
243 /* decrement received buffcnt to zero. */
244 while (readl(&ectl_p->stat.raw) & ETHSTAT_BUFCNT)
245 writel(ETHCON_BUFCDEC, &ectl_p->con1.set);
247 /* clear any existing interrupt event */
248 writel(0xffffffff, &ectl_p->irq.clr);
250 /* clear RX/TX start address */
251 writel(0xffffffff, &ectl_p->txst.clr);
252 writel(0xffffffff, &ectl_p->rxst.clr);
254 /* clear the receive filters */
255 writel(0x00ff, &ectl_p->rxfc.clr);
257 /* set the receive filters
258 * ETH_FILT_CRC_ERR_REJECT
259 * ETH_FILT_RUNT_REJECT
260 * ETH_FILT_UCAST_ACCEPT
261 * ETH_FILT_MCAST_ACCEPT
262 * ETH_FILT_BCAST_ACCEPT
264 v = ETHRXFC_BCEN | ETHRXFC_MCEN | ETHRXFC_UCEN |
265 ETHRXFC_RUNTEN | ETHRXFC_CRCOKEN;
266 writel(v, &ectl_p->rxfc.set);
268 /* turn controller ON to access PHY over MII */
269 writel(ETHCON_ON, &ectl_p->con1.set);
272 static void pic32_rx_desc_init(struct pic32eth_dev *priv)
274 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
275 struct eth_dma_desc *rxd;
279 for (idx = 0; idx < MAX_RX_DESCR; idx++) {
280 rxd = &priv->rxd_ring[idx];
283 rxd->hdr = EDH_NPV | EDH_EOWN | EDH_STICKY;
285 /* packet buffer address */
286 rxd->data_buff = virt_to_phys(net_rx_packets[idx]);
288 /* link to next desc */
289 rxd->next_ed = virt_to_phys(rxd + 1);
295 /* decrement bufcnt */
296 writel(ETHCON_BUFCDEC, &ectl_p->con1.set);
299 /* link last descr to beginning of list */
300 rxd->next_ed = virt_to_phys(&priv->rxd_ring[0]);
303 flush_dcache_range((ulong)priv->rxd_ring,
304 (ulong)priv->rxd_ring + sizeof(priv->rxd_ring));
306 /* set rx desc-ring start address */
307 writel((ulong)virt_to_phys(&priv->rxd_ring[0]), &ectl_p->rxst.raw);
310 bufsz = readl(&ectl_p->con2.raw);
311 bufsz &= ~(ETHCON_RXBUFSZ << ETHCON_RXBUFSZ_SHFT);
312 bufsz |= ((MAX_RX_BUF_SIZE / 16) << ETHCON_RXBUFSZ_SHFT);
313 writel(bufsz, &ectl_p->con2.raw);
315 /* enable the receiver in hardware which allows hardware
316 * to DMA received pkts to the descriptor pointer address.
318 writel(ETHCON_RXEN, &ectl_p->con1.set);
321 static int pic32_eth_start(struct udevice *dev)
323 struct eth_pdata *pdata = dev_get_platdata(dev);
324 struct pic32eth_dev *priv = dev_get_priv(dev);
327 pic32_ctrl_reset(priv);
330 pic32_mac_reset(priv);
333 phy_config(priv->phydev);
336 pic32_mac_init(priv, &pdata->enetaddr[0]);
338 /* init RX descriptor; TX descriptors are handled in xmit */
339 pic32_rx_desc_init(priv);
341 /* Start up & update link status of PHY */
342 phy_startup(priv->phydev);
344 /* adjust mac with phy link status */
345 return pic32_mac_adjust_link(priv);
348 static void pic32_eth_stop(struct udevice *dev)
350 struct pic32eth_dev *priv = dev_get_priv(dev);
351 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
352 struct pic32_emac_regs *emac_p = priv->emac_regs;
354 /* Reset the phy if the controller is enabled */
355 if (readl(&ectl_p->con1.raw) & ETHCON_ON)
356 phy_reset(priv->phydev);
358 /* Shut down the PHY */
359 phy_shutdown(priv->phydev);
362 writel(ETHCON_TXRTS | ETHCON_RXEN, &ectl_p->con1.clr);
366 writel(EMAC_SOFTRESET, &emac_p->cfg1.raw);
369 writel(0, &emac_p->cfg1.raw);
372 /* disable controller */
373 writel(ETHCON_ON, &ectl_p->con1.clr);
376 /* wait until everything is down */
377 wait_for_bit_le32(&ectl_p->stat.raw, ETHSTAT_BUSY, false,
378 2 * CONFIG_SYS_HZ, false);
380 /* clear any existing interrupt event */
381 writel(0xffffffff, &ectl_p->irq.clr);
384 static int pic32_eth_send(struct udevice *dev, void *packet, int length)
386 struct pic32eth_dev *priv = dev_get_priv(dev);
387 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
388 struct eth_dma_desc *txd;
391 txd = &priv->txd_ring[0];
393 /* set proper flags & length in descriptor header */
394 txd->hdr = EDH_SOP | EDH_EOP | EDH_EOWN | EDH_BCOUNT(length);
396 /* pass buffer address to hardware */
397 txd->data_buff = virt_to_phys(packet);
399 debug("%s: %d / .hdr %x, .data_buff %x, .stat %x, .nexted %x\n",
400 __func__, __LINE__, txd->hdr, txd->data_buff, txd->stat2,
403 /* cache flush (packet) */
404 flush_dcache_range((ulong)packet, (ulong)packet + length);
406 /* cache flush (txd) */
407 flush_dcache_range((ulong)txd, (ulong)txd + sizeof(*txd));
409 /* pass descriptor table base to h/w */
410 writel(virt_to_phys(txd), &ectl_p->txst.raw);
412 /* ready to send enabled, hardware can now send the packet(s) */
413 writel(ETHCON_TXRTS | ETHCON_ON, &ectl_p->con1.set);
415 /* wait until tx has completed and h/w has released ownership
416 * of the tx descriptor or timeout elapsed.
418 deadline = get_ticks() + get_tbclk();
421 if (get_ticks() > deadline)
428 if (readl(&ectl_p->con1.raw) & ETHCON_TXRTS) {
433 /* h/w not released ownership yet? */
434 invalidate_dcache_range((ulong)txd, (ulong)txd + sizeof(*txd));
435 if (!(txd->hdr & EDH_EOWN))
442 static int pic32_eth_recv(struct udevice *dev, int flags, uchar **packetp)
444 struct pic32eth_dev *priv = dev_get_priv(dev);
445 struct eth_dma_desc *rxd;
446 u32 idx = priv->rxd_idx;
449 /* find the next ready to receive */
450 rxd = &priv->rxd_ring[idx];
452 invalidate_dcache_range((ulong)rxd, (ulong)rxd + sizeof(*rxd));
453 /* check if owned by MAC */
454 if (rxd->hdr & EDH_EOWN)
457 /* Sanity check on header: SOP and EOP */
458 if ((rxd->hdr & (EDH_SOP | EDH_EOP)) != (EDH_SOP | EDH_EOP)) {
459 printf("%s: %s, rx pkt across multiple descr\n",
464 debug("%s: %d /idx %i, hdr=%x, data_buff %x, stat %x, nexted %x\n",
465 __func__, __LINE__, idx, rxd->hdr,
466 rxd->data_buff, rxd->stat2, rxd->next_ed);
468 /* Sanity check on rx_stat: OK, CRC */
469 if (!RSV_RX_OK(rxd->stat2) || RSV_CRC_ERR(rxd->stat2)) {
470 debug("%s: %s: Error, rx problem detected\n",
475 /* invalidate dcache */
476 rx_count = RSV_RX_COUNT(rxd->stat2);
477 invalidate_dcache_range((ulong)net_rx_packets[idx],
478 (ulong)net_rx_packets[idx] + rx_count);
480 /* Pass the packet to protocol layer */
481 *packetp = net_rx_packets[idx];
483 /* increment number of bytes rcvd (ignore CRC) */
487 static int pic32_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
489 struct pic32eth_dev *priv = dev_get_priv(dev);
490 struct pic32_ectl_regs *ectl_p = priv->ectl_regs;
491 struct eth_dma_desc *rxd;
492 int idx = priv->rxd_idx;
495 if (packet != net_rx_packets[idx]) {
496 printf("rxd_id %d: packet is not matched,\n", idx);
500 /* prepare for receive */
501 rxd = &priv->rxd_ring[idx];
502 rxd->hdr = EDH_STICKY | EDH_NPV | EDH_EOWN;
504 flush_dcache_range((ulong)rxd, (ulong)rxd + sizeof(*rxd));
506 /* decrement rx pkt count */
507 writel(ETHCON_BUFCDEC, &ectl_p->con1.set);
509 debug("%s: %d / idx %i, hdr %x, data_buff %x, stat %x, nexted %x\n",
510 __func__, __LINE__, idx, rxd->hdr, rxd->data_buff,
511 rxd->stat2, rxd->next_ed);
513 priv->rxd_idx = (priv->rxd_idx + 1) % MAX_RX_DESCR;
518 static const struct eth_ops pic32_eth_ops = {
519 .start = pic32_eth_start,
520 .send = pic32_eth_send,
521 .recv = pic32_eth_recv,
522 .free_pkt = pic32_eth_free_pkt,
523 .stop = pic32_eth_stop,
526 static int pic32_eth_probe(struct udevice *dev)
528 struct eth_pdata *pdata = dev_get_platdata(dev);
529 struct pic32eth_dev *priv = dev_get_priv(dev);
530 const char *phy_mode;
531 void __iomem *iobase;
537 addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
539 if (addr == FDT_ADDR_T_NONE)
542 iobase = ioremap(addr, size);
543 pdata->iobase = (phys_addr_t)addr;
546 pdata->phy_interface = -1;
547 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
550 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
551 if (pdata->phy_interface == -1) {
552 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
557 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
560 phy_addr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
563 gpio_request_by_name_nodev(dev_ofnode(dev), "reset-gpios", 0,
564 &priv->rst_gpio, GPIOD_IS_OUT);
566 priv->phyif = pdata->phy_interface;
567 priv->phy_addr = phy_addr;
568 priv->ectl_regs = iobase;
569 priv->emac_regs = iobase + PIC32_EMAC1CFG1;
571 pic32_mii_init(priv);
573 return pic32_phy_init(priv, dev);
576 static int pic32_eth_remove(struct udevice *dev)
578 struct pic32eth_dev *priv = dev_get_priv(dev);
581 dm_gpio_free(dev, &priv->rst_gpio);
582 phy_shutdown(priv->phydev);
584 bus = miiphy_get_dev_by_name(PIC32_MDIO_NAME);
585 mdio_unregister(bus);
587 iounmap(priv->ectl_regs);
591 static const struct udevice_id pic32_eth_ids[] = {
592 { .compatible = "microchip,pic32mzda-eth" },
596 U_BOOT_DRIVER(pic32_ethernet) = {
597 .name = "pic32_ethernet",
599 .of_match = pic32_eth_ids,
600 .probe = pic32_eth_probe,
601 .remove = pic32_eth_remove,
602 .ops = &pic32_eth_ops,
603 .priv_auto_alloc_size = sizeof(struct pic32eth_dev),
604 .platdata_auto_alloc_size = sizeof(struct eth_pdata),