1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * LiteX Liteeth Ethernet
5 * Copyright 2017 Joel Stanley <joel@jms.id.au>
9 #include <linux/etherdevice.h>
10 #include <linux/interrupt.h>
11 #include <linux/litex.h>
12 #include <linux/module.h>
13 #include <linux/of_net.h>
14 #include <linux/platform_device.h>
16 #define LITEETH_WRITER_SLOT 0x00
17 #define LITEETH_WRITER_LENGTH 0x04
18 #define LITEETH_WRITER_ERRORS 0x08
19 #define LITEETH_WRITER_EV_STATUS 0x0C
20 #define LITEETH_WRITER_EV_PENDING 0x10
21 #define LITEETH_WRITER_EV_ENABLE 0x14
22 #define LITEETH_READER_START 0x18
23 #define LITEETH_READER_READY 0x1C
24 #define LITEETH_READER_LEVEL 0x20
25 #define LITEETH_READER_SLOT 0x24
26 #define LITEETH_READER_LENGTH 0x28
27 #define LITEETH_READER_EV_STATUS 0x2C
28 #define LITEETH_READER_EV_PENDING 0x30
29 #define LITEETH_READER_EV_ENABLE 0x34
30 #define LITEETH_PREAMBLE_CRC 0x38
31 #define LITEETH_PREAMBLE_ERRORS 0x3C
32 #define LITEETH_CRC_ERRORS 0x40
34 #define LITEETH_PHY_CRG_RESET 0x00
35 #define LITEETH_MDIO_W 0x04
36 #define LITEETH_MDIO_R 0x0C
38 #define DRV_NAME "liteeth"
42 struct net_device *netdev;
49 void __iomem *tx_base;
54 void __iomem *rx_base;
57 static int liteeth_rx(struct net_device *netdev)
59 struct liteeth *priv = netdev_priv(netdev);
65 rx_slot = litex_read8(priv->base + LITEETH_WRITER_SLOT);
66 len = litex_read32(priv->base + LITEETH_WRITER_LENGTH);
68 if (len == 0 || len > 2048)
71 skb = netdev_alloc_skb_ip_align(netdev, len);
73 netdev_err(netdev, "couldn't get memory\n");
77 data = skb_put(skb, len);
78 memcpy_fromio(data, priv->rx_base + rx_slot * priv->slot_size, len);
79 skb->protocol = eth_type_trans(skb, netdev);
81 netdev->stats.rx_packets++;
82 netdev->stats.rx_bytes += len;
87 netdev->stats.rx_dropped++;
88 netdev->stats.rx_errors++;
93 static irqreturn_t liteeth_interrupt(int irq, void *dev_id)
95 struct net_device *netdev = dev_id;
96 struct liteeth *priv = netdev_priv(netdev);
99 reg = litex_read8(priv->base + LITEETH_READER_EV_PENDING);
101 if (netif_queue_stopped(netdev))
102 netif_wake_queue(netdev);
103 litex_write8(priv->base + LITEETH_READER_EV_PENDING, reg);
106 reg = litex_read8(priv->base + LITEETH_WRITER_EV_PENDING);
109 litex_write8(priv->base + LITEETH_WRITER_EV_PENDING, reg);
115 static int liteeth_open(struct net_device *netdev)
117 struct liteeth *priv = netdev_priv(netdev);
120 /* Clear pending events */
121 litex_write8(priv->base + LITEETH_WRITER_EV_PENDING, 1);
122 litex_write8(priv->base + LITEETH_READER_EV_PENDING, 1);
124 err = request_irq(netdev->irq, liteeth_interrupt, 0, netdev->name, netdev);
126 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
131 litex_write8(priv->base + LITEETH_WRITER_EV_ENABLE, 1);
132 litex_write8(priv->base + LITEETH_READER_EV_ENABLE, 1);
134 netif_carrier_on(netdev);
135 netif_start_queue(netdev);
140 static int liteeth_stop(struct net_device *netdev)
142 struct liteeth *priv = netdev_priv(netdev);
144 netif_stop_queue(netdev);
145 netif_carrier_off(netdev);
147 litex_write8(priv->base + LITEETH_WRITER_EV_ENABLE, 0);
148 litex_write8(priv->base + LITEETH_READER_EV_ENABLE, 0);
150 free_irq(netdev->irq, netdev);
155 static int liteeth_start_xmit(struct sk_buff *skb, struct net_device *netdev)
157 struct liteeth *priv = netdev_priv(netdev);
158 void __iomem *txbuffer;
160 if (!litex_read8(priv->base + LITEETH_READER_READY)) {
162 netdev_err(netdev, "LITEETH_READER_READY not ready\n");
164 netif_stop_queue(netdev);
166 return NETDEV_TX_BUSY;
169 /* Reject oversize packets */
170 if (unlikely(skb->len > priv->slot_size)) {
172 netdev_err(netdev, "tx packet too big\n");
174 dev_kfree_skb_any(skb);
175 netdev->stats.tx_dropped++;
176 netdev->stats.tx_errors++;
181 txbuffer = priv->tx_base + priv->tx_slot * priv->slot_size;
182 memcpy_toio(txbuffer, skb->data, skb->len);
183 litex_write8(priv->base + LITEETH_READER_SLOT, priv->tx_slot);
184 litex_write16(priv->base + LITEETH_READER_LENGTH, skb->len);
185 litex_write8(priv->base + LITEETH_READER_START, 1);
187 netdev->stats.tx_bytes += skb->len;
188 netdev->stats.tx_packets++;
190 priv->tx_slot = (priv->tx_slot + 1) % priv->num_tx_slots;
191 dev_kfree_skb_any(skb);
196 static const struct net_device_ops liteeth_netdev_ops = {
197 .ndo_open = liteeth_open,
198 .ndo_stop = liteeth_stop,
199 .ndo_start_xmit = liteeth_start_xmit,
202 static void liteeth_setup_slots(struct liteeth *priv)
204 struct device_node *np = priv->dev->of_node;
207 err = of_property_read_u32(np, "litex,rx-slots", &priv->num_rx_slots);
209 dev_dbg(priv->dev, "unable to get litex,rx-slots, using 2\n");
210 priv->num_rx_slots = 2;
213 err = of_property_read_u32(np, "litex,tx-slots", &priv->num_tx_slots);
215 dev_dbg(priv->dev, "unable to get litex,tx-slots, using 2\n");
216 priv->num_tx_slots = 2;
219 err = of_property_read_u32(np, "litex,slot-size", &priv->slot_size);
221 dev_dbg(priv->dev, "unable to get litex,slot-size, using 0x800\n");
222 priv->slot_size = 0x800;
226 static int liteeth_probe(struct platform_device *pdev)
228 struct net_device *netdev;
229 void __iomem *buf_base;
230 struct liteeth *priv;
233 netdev = devm_alloc_etherdev(&pdev->dev, sizeof(*priv));
237 SET_NETDEV_DEV(netdev, &pdev->dev);
238 platform_set_drvdata(pdev, netdev);
240 priv = netdev_priv(netdev);
241 priv->netdev = netdev;
242 priv->dev = &pdev->dev;
244 irq = platform_get_irq(pdev, 0);
249 priv->base = devm_platform_ioremap_resource_byname(pdev, "mac");
250 if (IS_ERR(priv->base))
251 return PTR_ERR(priv->base);
253 buf_base = devm_platform_ioremap_resource_byname(pdev, "buffer");
254 if (IS_ERR(buf_base))
255 return PTR_ERR(buf_base);
257 liteeth_setup_slots(priv);
260 priv->rx_base = buf_base;
263 /* Tx slots come after Rx slots */
264 priv->tx_base = buf_base + priv->num_rx_slots * priv->slot_size;
267 err = of_get_ethdev_address(pdev->dev.of_node, netdev);
269 eth_hw_addr_random(netdev);
271 netdev->netdev_ops = &liteeth_netdev_ops;
273 err = register_netdev(netdev);
275 dev_err(&pdev->dev, "Failed to register netdev %d\n", err);
279 netdev_info(netdev, "irq %d slots: tx %d rx %d size %d\n",
280 netdev->irq, priv->num_tx_slots, priv->num_rx_slots, priv->slot_size);
285 static int liteeth_remove(struct platform_device *pdev)
287 struct net_device *netdev = platform_get_drvdata(pdev);
289 unregister_netdev(netdev);
294 static const struct of_device_id liteeth_of_match[] = {
295 { .compatible = "litex,liteeth" },
298 MODULE_DEVICE_TABLE(of, liteeth_of_match);
300 static struct platform_driver liteeth_driver = {
301 .probe = liteeth_probe,
302 .remove = liteeth_remove,
305 .of_match_table = liteeth_of_match,
308 module_platform_driver(liteeth_driver);
310 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
311 MODULE_LICENSE("GPL");