1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/net/ethernet/netx-eth.c
5 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
14 #include <linux/netdevice.h>
15 #include <linux/platform_device.h>
16 #include <linux/etherdevice.h>
17 #include <linux/skbuff.h>
18 #include <linux/mii.h>
21 #include <mach/hardware.h>
22 #include <mach/netx-regs.h>
23 #include <mach/pfifo.h>
25 #include <linux/platform_data/eth-netx.h>
28 #define EMPTY_PTR_FIFO(xcno) (0 + ((xcno) << 3)) /* Index of the empty pointer FIFO */
29 #define IND_FIFO_PORT_HI(xcno) (1 + ((xcno) << 3)) /* Index of the FIFO where received */
30 /* Data packages are indicated by XC */
31 #define IND_FIFO_PORT_LO(xcno) (2 + ((xcno) << 3)) /* Index of the FIFO where received */
32 /* Data packages are indicated by XC */
33 #define REQ_FIFO_PORT_HI(xcno) (3 + ((xcno) << 3)) /* Index of the FIFO where Data packages */
34 /* have to be indicated by ARM which */
36 #define REQ_FIFO_PORT_LO(xcno) (4 + ((xcno) << 3)) /* Index of the FIFO where Data packages */
37 /* have to be indicated by ARM which shall */
39 #define CON_FIFO_PORT_HI(xcno) (5 + ((xcno) << 3)) /* Index of the FIFO where sent Data packages */
41 #define CON_FIFO_PORT_LO(xcno) (6 + ((xcno) << 3)) /* Index of the FIFO where sent Data */
42 /* packages are confirmed */
43 #define PFIFO_MASK(xcno) (0x7f << (xcno*8))
45 #define FIFO_PTR_FRAMELEN_SHIFT 0
46 #define FIFO_PTR_FRAMELEN_MASK (0x7ff << 0)
47 #define FIFO_PTR_FRAMELEN(len) (((len) << 0) & FIFO_PTR_FRAMELEN_MASK)
48 #define FIFO_PTR_TIMETRIG (1<<11)
49 #define FIFO_PTR_MULTI_REQ
50 #define FIFO_PTR_ORIGIN (1<<14)
51 #define FIFO_PTR_VLAN (1<<15)
52 #define FIFO_PTR_FRAMENO_SHIFT 16
53 #define FIFO_PTR_FRAMENO_MASK (0x3f << 16)
54 #define FIFO_PTR_FRAMENO(no) (((no) << 16) & FIFO_PTR_FRAMENO_MASK)
55 #define FIFO_PTR_SEGMENT_SHIFT 22
56 #define FIFO_PTR_SEGMENT_MASK (0xf << 22)
57 #define FIFO_PTR_SEGMENT(seg) (((seg) & 0xf) << 22)
58 #define FIFO_PTR_ERROR_SHIFT 28
59 #define FIFO_PTR_ERROR_MASK (0xf << 28)
61 #define ISR_LINK_STATUS_CHANGE (1<<4)
62 #define ISR_IND_LO (1<<3)
63 #define ISR_CON_LO (1<<2)
64 #define ISR_IND_HI (1<<1)
65 #define ISR_CON_HI (1<<0)
67 #define ETH_MAC_LOCAL_CONFIG 0x1560
68 #define ETH_MAC_4321 0x1564
69 #define ETH_MAC_65 0x1568
71 #define MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT 16
72 #define MAC_TRAFFIC_CLASS_ARRANGEMENT_MASK (0xf<<MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT)
73 #define MAC_TRAFFIC_CLASS_ARRANGEMENT(x) (((x)<<MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT) & MAC_TRAFFIC_CLASS_ARRANGEMENT_MASK)
74 #define LOCAL_CONFIG_LINK_STATUS_IRQ_EN (1<<24)
75 #define LOCAL_CONFIG_CON_LO_IRQ_EN (1<<23)
76 #define LOCAL_CONFIG_CON_HI_IRQ_EN (1<<22)
77 #define LOCAL_CONFIG_IND_LO_IRQ_EN (1<<21)
78 #define LOCAL_CONFIG_IND_HI_IRQ_EN (1<<20)
80 #define CARDNAME "netx-eth"
82 /* LSB must be zero */
83 #define INTERNAL_PHY_ADR 0x1c
85 struct netx_eth_priv {
86 void __iomem *sram_base, *xpec_base, *xmac_base;
88 struct mii_if_info mii;
94 static void netx_eth_set_multicast_list(struct net_device *ndev)
100 netx_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
102 struct netx_eth_priv *priv = netdev_priv(ndev);
103 unsigned char *buf = skb->data;
104 unsigned int len = skb->len;
106 spin_lock_irq(&priv->lock);
107 memcpy_toio(priv->sram_base + 1560, (void *)buf, len);
109 memset_io(priv->sram_base + 1560 + len, 0, 60 - len);
113 pfifo_push(REQ_FIFO_PORT_LO(priv->id),
114 FIFO_PTR_SEGMENT(priv->id) |
115 FIFO_PTR_FRAMENO(1) |
116 FIFO_PTR_FRAMELEN(len));
118 ndev->stats.tx_packets++;
119 ndev->stats.tx_bytes += skb->len;
121 netif_stop_queue(ndev);
122 spin_unlock_irq(&priv->lock);
128 static void netx_eth_receive(struct net_device *ndev)
130 struct netx_eth_priv *priv = netdev_priv(ndev);
131 unsigned int val, frameno, seg, len;
135 val = pfifo_pop(IND_FIFO_PORT_LO(priv->id));
137 frameno = (val & FIFO_PTR_FRAMENO_MASK) >> FIFO_PTR_FRAMENO_SHIFT;
138 seg = (val & FIFO_PTR_SEGMENT_MASK) >> FIFO_PTR_SEGMENT_SHIFT;
139 len = (val & FIFO_PTR_FRAMELEN_MASK) >> FIFO_PTR_FRAMELEN_SHIFT;
141 skb = netdev_alloc_skb(ndev, len);
142 if (unlikely(skb == NULL)) {
143 ndev->stats.rx_dropped++;
147 data = skb_put(skb, len);
149 memcpy_fromio(data, priv->sram_base + frameno * 1560, len);
151 pfifo_push(EMPTY_PTR_FIFO(priv->id),
152 FIFO_PTR_SEGMENT(seg) | FIFO_PTR_FRAMENO(frameno));
154 skb->protocol = eth_type_trans(skb, ndev);
156 ndev->stats.rx_packets++;
157 ndev->stats.rx_bytes += len;
161 netx_eth_interrupt(int irq, void *dev_id)
163 struct net_device *ndev = dev_id;
164 struct netx_eth_priv *priv = netdev_priv(ndev);
168 spin_lock_irqsave(&priv->lock, flags);
170 status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
173 writel(status, NETX_PFIFO_XPEC_ISR(priv->id));
175 if ((status & ISR_CON_HI) || (status & ISR_IND_HI))
176 printk("%s: unexpected status: 0x%08x\n",
180 readl(NETX_PFIFO_FILL_LEVEL(IND_FIFO_PORT_LO(priv->id)));
182 netx_eth_receive(ndev);
184 if (status & ISR_CON_LO)
185 netif_wake_queue(ndev);
187 if (status & ISR_LINK_STATUS_CHANGE)
188 mii_check_media(&priv->mii, netif_msg_link(priv), 1);
190 status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
192 spin_unlock_irqrestore(&priv->lock, flags);
196 static int netx_eth_open(struct net_device *ndev)
198 struct netx_eth_priv *priv = netdev_priv(ndev);
201 (ndev->irq, netx_eth_interrupt, IRQF_SHARED, ndev->name, ndev))
204 writel(ndev->dev_addr[0] |
205 ndev->dev_addr[1]<<8 |
206 ndev->dev_addr[2]<<16 |
207 ndev->dev_addr[3]<<24,
208 priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
209 writel(ndev->dev_addr[4] |
210 ndev->dev_addr[5]<<8,
211 priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
213 writel(LOCAL_CONFIG_LINK_STATUS_IRQ_EN |
214 LOCAL_CONFIG_CON_LO_IRQ_EN |
215 LOCAL_CONFIG_CON_HI_IRQ_EN |
216 LOCAL_CONFIG_IND_LO_IRQ_EN |
217 LOCAL_CONFIG_IND_HI_IRQ_EN,
218 priv->xpec_base + NETX_XPEC_RAM_START_OFS +
219 ETH_MAC_LOCAL_CONFIG);
221 mii_check_media(&priv->mii, netif_msg_link(priv), 1);
222 netif_start_queue(ndev);
227 static int netx_eth_close(struct net_device *ndev)
229 struct netx_eth_priv *priv = netdev_priv(ndev);
231 netif_stop_queue(ndev);
234 priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_LOCAL_CONFIG);
236 free_irq(ndev->irq, ndev);
241 static void netx_eth_timeout(struct net_device *ndev)
243 struct netx_eth_priv *priv = netdev_priv(ndev);
246 printk(KERN_ERR "%s: transmit timed out, resetting\n", ndev->name);
248 spin_lock_irq(&priv->lock);
253 for (i=2; i<=18; i++)
254 pfifo_push(EMPTY_PTR_FIFO(priv->id),
255 FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
257 spin_unlock_irq(&priv->lock);
259 netif_wake_queue(ndev);
263 netx_eth_phy_read(struct net_device *ndev, int phy_id, int reg)
267 val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
268 MIIMU_REGADDR(reg) | MIIMU_PHY_NRES;
270 writel(val, NETX_MIIMU);
271 while (readl(NETX_MIIMU) & MIIMU_SNRDY);
273 return readl(NETX_MIIMU) >> 16;
278 netx_eth_phy_write(struct net_device *ndev, int phy_id, int reg, int value)
282 val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
283 MIIMU_REGADDR(reg) | MIIMU_PHY_NRES | MIIMU_OPMODE_WRITE |
286 writel(val, NETX_MIIMU);
287 while (readl(NETX_MIIMU) & MIIMU_SNRDY);
290 static const struct net_device_ops netx_eth_netdev_ops = {
291 .ndo_open = netx_eth_open,
292 .ndo_stop = netx_eth_close,
293 .ndo_start_xmit = netx_eth_hard_start_xmit,
294 .ndo_tx_timeout = netx_eth_timeout,
295 .ndo_set_rx_mode = netx_eth_set_multicast_list,
296 .ndo_validate_addr = eth_validate_addr,
297 .ndo_set_mac_address = eth_mac_addr,
300 static int netx_eth_enable(struct net_device *ndev)
302 struct netx_eth_priv *priv = netdev_priv(ndev);
303 unsigned int mac4321, mac65;
305 bool inv_mac_addr = false;
307 ndev->netdev_ops = &netx_eth_netdev_ops;
308 ndev->watchdog_timeo = msecs_to_jiffies(5000);
310 priv->msg_enable = NETIF_MSG_LINK;
311 priv->mii.phy_id_mask = 0x1f;
312 priv->mii.reg_num_mask = 0x1f;
313 priv->mii.force_media = 0;
314 priv->mii.full_duplex = 0;
315 priv->mii.dev = ndev;
316 priv->mii.mdio_read = netx_eth_phy_read;
317 priv->mii.mdio_write = netx_eth_phy_write;
318 priv->mii.phy_id = INTERNAL_PHY_ADR + priv->id;
320 running = xc_running(priv->xc);
323 /* if the xc engine is already running, assume the bootloader has
324 * loaded the firmware for us
327 /* get Node Address from hardware */
328 mac4321 = readl(priv->xpec_base +
329 NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
330 mac65 = readl(priv->xpec_base +
331 NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
333 ndev->dev_addr[0] = mac4321 & 0xff;
334 ndev->dev_addr[1] = (mac4321 >> 8) & 0xff;
335 ndev->dev_addr[2] = (mac4321 >> 16) & 0xff;
336 ndev->dev_addr[3] = (mac4321 >> 24) & 0xff;
337 ndev->dev_addr[4] = mac65 & 0xff;
338 ndev->dev_addr[5] = (mac65 >> 8) & 0xff;
340 if (xc_request_firmware(priv->xc)) {
341 printk(CARDNAME ": requesting firmware failed\n");
349 if (!is_valid_ether_addr(ndev->dev_addr))
352 for (i=2; i<=18; i++)
353 pfifo_push(EMPTY_PTR_FIFO(priv->id),
354 FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
356 ret = register_netdev(ndev);
358 printk("%s: Invalid ethernet MAC address. Please set using ip\n",
364 static int netx_eth_drv_probe(struct platform_device *pdev)
366 struct netx_eth_priv *priv;
367 struct net_device *ndev;
368 struct netxeth_platform_data *pdata;
371 ndev = alloc_etherdev(sizeof (struct netx_eth_priv));
376 SET_NETDEV_DEV(ndev, &pdev->dev);
378 platform_set_drvdata(pdev, ndev);
380 priv = netdev_priv(ndev);
382 pdata = dev_get_platdata(&pdev->dev);
383 priv->xc = request_xc(pdata->xcno, &pdev->dev);
385 dev_err(&pdev->dev, "unable to request xc engine\n");
387 goto exit_free_netdev;
390 ndev->irq = priv->xc->irq;
392 priv->xpec_base = priv->xc->xpec_base;
393 priv->xmac_base = priv->xc->xmac_base;
394 priv->sram_base = priv->xc->sram_base;
396 spin_lock_init(&priv->lock);
398 ret = pfifo_request(PFIFO_MASK(priv->id));
400 printk("unable to request PFIFO\n");
404 ret = netx_eth_enable(ndev);
406 goto exit_free_pfifo;
410 pfifo_free(PFIFO_MASK(priv->id));
419 static int netx_eth_drv_remove(struct platform_device *pdev)
421 struct net_device *ndev = platform_get_drvdata(pdev);
422 struct netx_eth_priv *priv = netdev_priv(ndev);
424 unregister_netdev(ndev);
428 pfifo_free(PFIFO_MASK(priv->id));
433 static int netx_eth_drv_suspend(struct platform_device *pdev, pm_message_t state)
435 dev_err(&pdev->dev, "suspend not implemented\n");
439 static int netx_eth_drv_resume(struct platform_device *pdev)
441 dev_err(&pdev->dev, "resume not implemented\n");
445 static struct platform_driver netx_eth_driver = {
446 .probe = netx_eth_drv_probe,
447 .remove = netx_eth_drv_remove,
448 .suspend = netx_eth_drv_suspend,
449 .resume = netx_eth_drv_resume,
455 static int __init netx_eth_init(void)
457 unsigned int phy_control, val;
459 printk("NetX Ethernet driver\n");
461 phy_control = PHY_CONTROL_PHY_ADDRESS(INTERNAL_PHY_ADR>>1) |
462 PHY_CONTROL_PHY1_MODE(PHY_MODE_ALL) |
463 PHY_CONTROL_PHY1_AUTOMDIX |
464 PHY_CONTROL_PHY1_EN |
465 PHY_CONTROL_PHY0_MODE(PHY_MODE_ALL) |
466 PHY_CONTROL_PHY0_AUTOMDIX |
467 PHY_CONTROL_PHY0_EN |
468 PHY_CONTROL_CLK_XLATIN;
470 val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
471 writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
473 writel(phy_control | PHY_CONTROL_RESET, NETX_SYSTEM_PHY_CONTROL);
476 val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
477 writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
479 writel(phy_control, NETX_SYSTEM_PHY_CONTROL);
481 return platform_driver_register(&netx_eth_driver);
484 static void __exit netx_eth_cleanup(void)
486 platform_driver_unregister(&netx_eth_driver);
489 module_init(netx_eth_init);
490 module_exit(netx_eth_cleanup);
492 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS("platform:" CARDNAME);
495 MODULE_FIRMWARE("xc0.bin");
496 MODULE_FIRMWARE("xc1.bin");
497 MODULE_FIRMWARE("xc2.bin");