net: lantiq_xrx200: fix lock under memory pressure
[linux-block.git] / drivers / net / ethernet / lantiq_xrx200.c
CommitLineData
fe1a5642
HM
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Lantiq / Intel PMAC driver for XRX200 SoCs
4 *
5 * Copyright (C) 2010 Lantiq Deutschland
6 * Copyright (C) 2012 John Crispin <john@phrozen.org>
7 * Copyright (C) 2017 - 2018 Hauke Mehrtens <hauke@hauke-m.de>
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13#include <linux/interrupt.h>
14#include <linux/clk.h>
15#include <linux/delay.h>
16
998ac358
AJB
17#include <linux/if_vlan.h>
18
fe1a5642
HM
19#include <linux/of_net.h>
20#include <linux/of_platform.h>
21
22#include <xway_dma.h>
23
24/* DMA */
998ac358 25#define XRX200_DMA_DATA_LEN (SZ_64K - 1)
fe1a5642
HM
26#define XRX200_DMA_RX 0
27#define XRX200_DMA_TX 1
7e553c44 28#define XRX200_DMA_BURST_LEN 8
fe1a5642 29
c3e6b2c3
AJB
30#define XRX200_DMA_PACKET_COMPLETE 0
31#define XRX200_DMA_PACKET_IN_PROGRESS 1
32
fe1a5642
HM
33/* cpu port mac */
34#define PMAC_RX_IPG 0x0024
35#define PMAC_RX_IPG_MASK 0xf
36
37#define PMAC_HD_CTL 0x0000
38/* Add Ethernet header to packets from DMA to PMAC */
39#define PMAC_HD_CTL_ADD BIT(0)
40/* Add VLAN tag to Packets from DMA to PMAC */
41#define PMAC_HD_CTL_TAG BIT(1)
42/* Add CRC to packets from DMA to PMAC */
43#define PMAC_HD_CTL_AC BIT(2)
44/* Add status header to packets from PMAC to DMA */
45#define PMAC_HD_CTL_AS BIT(3)
46/* Remove CRC from packets from PMAC to DMA */
47#define PMAC_HD_CTL_RC BIT(4)
48/* Remove Layer-2 header from packets from PMAC to DMA */
49#define PMAC_HD_CTL_RL2 BIT(5)
50/* Status header is present from DMA to PMAC */
51#define PMAC_HD_CTL_RXSH BIT(6)
52/* Add special tag from PMAC to switch */
53#define PMAC_HD_CTL_AST BIT(7)
54/* Remove specail Tag from PMAC to DMA */
55#define PMAC_HD_CTL_RST BIT(8)
56/* Check CRC from DMA to PMAC */
57#define PMAC_HD_CTL_CCRC BIT(9)
58/* Enable reaction to Pause frames in the PMAC */
59#define PMAC_HD_CTL_FC BIT(10)
60
61struct xrx200_chan {
62 int tx_free;
63
64 struct napi_struct napi;
65 struct ltq_dma_channel dma;
e0155935
AJB
66
67 union {
68 struct sk_buff *skb[LTQ_DESC_NUM];
69 void *rx_buff[LTQ_DESC_NUM];
70 };
fe1a5642 71
c3e6b2c3
AJB
72 struct sk_buff *skb_head;
73 struct sk_buff *skb_tail;
74
fe1a5642
HM
75 struct xrx200_priv *priv;
76};
77
78struct xrx200_priv {
79 struct clk *clk;
80
81 struct xrx200_chan chan_tx;
82 struct xrx200_chan chan_rx;
83
1488fc20 84 u16 rx_buf_size;
e0155935 85 u16 rx_skb_size;
1488fc20 86
fe1a5642
HM
87 struct net_device *net_dev;
88 struct device *dev;
89
90 __iomem void *pmac_reg;
91};
92
93static u32 xrx200_pmac_r32(struct xrx200_priv *priv, u32 offset)
94{
95 return __raw_readl(priv->pmac_reg + offset);
96}
97
98static void xrx200_pmac_w32(struct xrx200_priv *priv, u32 val, u32 offset)
99{
100 __raw_writel(val, priv->pmac_reg + offset);
101}
102
103static void xrx200_pmac_mask(struct xrx200_priv *priv, u32 clear, u32 set,
104 u32 offset)
105{
106 u32 val = xrx200_pmac_r32(priv, offset);
107
108 val &= ~(clear);
109 val |= set;
110 xrx200_pmac_w32(priv, val, offset);
111}
112
1488fc20
AJB
113static int xrx200_max_frame_len(int mtu)
114{
115 return VLAN_ETH_HLEN + mtu;
116}
117
118static int xrx200_buffer_size(int mtu)
119{
120 return round_up(xrx200_max_frame_len(mtu), 4 * XRX200_DMA_BURST_LEN);
121}
122
e0155935
AJB
123static int xrx200_skb_size(u16 buf_size)
124{
125 return SKB_DATA_ALIGN(buf_size + NET_SKB_PAD + NET_IP_ALIGN) +
126 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
127}
128
fe1a5642
HM
129/* drop all the packets from the DMA ring */
130static void xrx200_flush_dma(struct xrx200_chan *ch)
131{
132 int i;
133
134 for (i = 0; i < LTQ_DESC_NUM; i++) {
135 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
136
137 if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) != LTQ_DMA_C)
138 break;
139
140 desc->ctl = LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) |
1488fc20 141 ch->priv->rx_buf_size;
fe1a5642
HM
142 ch->dma.desc++;
143 ch->dma.desc %= LTQ_DESC_NUM;
144 }
145}
146
147static int xrx200_open(struct net_device *net_dev)
148{
149 struct xrx200_priv *priv = netdev_priv(net_dev);
fe1a5642
HM
150
151 napi_enable(&priv->chan_tx.napi);
152 ltq_dma_open(&priv->chan_tx.dma);
153 ltq_dma_enable_irq(&priv->chan_tx.dma);
154
155 napi_enable(&priv->chan_rx.napi);
156 ltq_dma_open(&priv->chan_rx.dma);
157 /* The boot loader does not always deactivate the receiving of frames
158 * on the ports and then some packets queue up in the PPE buffers.
159 * They already passed the PMAC so they do not have the tags
160 * configured here. Read the these packets here and drop them.
161 * The HW should have written them into memory after 10us
162 */
163 usleep_range(20, 40);
164 xrx200_flush_dma(&priv->chan_rx);
165 ltq_dma_enable_irq(&priv->chan_rx.dma);
166
167 netif_wake_queue(net_dev);
168
169 return 0;
170}
171
172static int xrx200_close(struct net_device *net_dev)
173{
174 struct xrx200_priv *priv = netdev_priv(net_dev);
175
176 netif_stop_queue(net_dev);
177
178 napi_disable(&priv->chan_rx.napi);
179 ltq_dma_close(&priv->chan_rx.dma);
180
181 napi_disable(&priv->chan_tx.napi);
182 ltq_dma_close(&priv->chan_tx.dma);
183
fe1a5642
HM
184 return 0;
185}
186
e0155935 187static int xrx200_alloc_buf(struct xrx200_chan *ch, void *(*alloc)(unsigned int size))
fe1a5642 188{
e0155935 189 void *buf = ch->rx_buff[ch->dma.desc];
1488fc20 190 struct xrx200_priv *priv = ch->priv;
c7718ee9 191 dma_addr_t mapping;
fe1a5642
HM
192 int ret = 0;
193
e0155935
AJB
194 ch->rx_buff[ch->dma.desc] = alloc(priv->rx_skb_size);
195 if (!ch->rx_buff[ch->dma.desc]) {
fe1a5642
HM
196 ret = -ENOMEM;
197 goto skip;
198 }
199
e0155935 200 mapping = dma_map_single(priv->dev, ch->rx_buff[ch->dma.desc],
1488fc20
AJB
201 priv->rx_buf_size, DMA_FROM_DEVICE);
202 if (unlikely(dma_mapping_error(priv->dev, mapping))) {
e0155935
AJB
203 skb_free_frag(ch->rx_buff[ch->dma.desc]);
204 ch->rx_buff[ch->dma.desc] = buf;
fe1a5642
HM
205 ret = -ENOMEM;
206 goto skip;
207 }
208
e0155935 209 ch->dma.desc_base[ch->dma.desc].addr = mapping + NET_SKB_PAD + NET_IP_ALIGN;
c7718ee9
AJB
210 /* Make sure the address is written before we give it to HW */
211 wmb();
fe1a5642
HM
212skip:
213 ch->dma.desc_base[ch->dma.desc].ctl =
1488fc20 214 LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) | priv->rx_buf_size;
fe1a5642
HM
215
216 return ret;
217}
218
219static int xrx200_hw_receive(struct xrx200_chan *ch)
220{
221 struct xrx200_priv *priv = ch->priv;
222 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
e0155935 223 void *buf = ch->rx_buff[ch->dma.desc];
c3e6b2c3
AJB
224 u32 ctl = desc->ctl;
225 int len = (ctl & LTQ_DMA_SIZE_MASK);
fe1a5642 226 struct net_device *net_dev = priv->net_dev;
e0155935 227 struct sk_buff *skb;
fe1a5642
HM
228 int ret;
229
e0155935 230 ret = xrx200_alloc_buf(ch, napi_alloc_frag);
fe1a5642
HM
231
232 ch->dma.desc++;
233 ch->dma.desc %= LTQ_DESC_NUM;
234
235 if (ret) {
c7718ee9 236 net_dev->stats.rx_dropped++;
fe1a5642
HM
237 netdev_err(net_dev, "failed to allocate new rx buffer\n");
238 return ret;
239 }
240
e0155935 241 skb = build_skb(buf, priv->rx_skb_size);
c8b04370
AJB
242 if (!skb) {
243 skb_free_frag(buf);
244 net_dev->stats.rx_dropped++;
245 return -ENOMEM;
246 }
247
e0155935 248 skb_reserve(skb, NET_SKB_PAD);
fe1a5642 249 skb_put(skb, len);
fe1a5642 250
c3e6b2c3
AJB
251 /* add buffers to skb via skb->frag_list */
252 if (ctl & LTQ_DMA_SOP) {
253 ch->skb_head = skb;
254 ch->skb_tail = skb;
e0155935 255 skb_reserve(skb, NET_IP_ALIGN);
c3e6b2c3
AJB
256 } else if (ch->skb_head) {
257 if (ch->skb_head == ch->skb_tail)
258 skb_shinfo(ch->skb_tail)->frag_list = skb;
259 else
260 ch->skb_tail->next = skb;
261 ch->skb_tail = skb;
c3e6b2c3
AJB
262 ch->skb_head->len += skb->len;
263 ch->skb_head->data_len += skb->len;
264 ch->skb_head->truesize += skb->truesize;
265 }
266
267 if (ctl & LTQ_DMA_EOP) {
268 ch->skb_head->protocol = eth_type_trans(ch->skb_head, net_dev);
c3e6b2c3
AJB
269 net_dev->stats.rx_packets++;
270 net_dev->stats.rx_bytes += ch->skb_head->len;
dd830aed 271 netif_receive_skb(ch->skb_head);
c3e6b2c3
AJB
272 ch->skb_head = NULL;
273 ch->skb_tail = NULL;
274 ret = XRX200_DMA_PACKET_COMPLETE;
275 } else {
276 ret = XRX200_DMA_PACKET_IN_PROGRESS;
277 }
278
279 return ret;
fe1a5642
HM
280}
281
282static int xrx200_poll_rx(struct napi_struct *napi, int budget)
283{
284 struct xrx200_chan *ch = container_of(napi,
285 struct xrx200_chan, napi);
286 int rx = 0;
287 int ret;
288
289 while (rx < budget) {
290 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
291
292 if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
293 ret = xrx200_hw_receive(ch);
c3e6b2c3
AJB
294 if (ret == XRX200_DMA_PACKET_IN_PROGRESS)
295 continue;
296 if (ret != XRX200_DMA_PACKET_COMPLETE)
c4b6e934 297 break;
fe1a5642
HM
298 rx++;
299 } else {
300 break;
301 }
302 }
303
304 if (rx < budget) {
c582a7fe
HM
305 if (napi_complete_done(&ch->napi, rx))
306 ltq_dma_enable_irq(&ch->dma);
fe1a5642
HM
307 }
308
309 return rx;
310}
311
312static int xrx200_tx_housekeeping(struct napi_struct *napi, int budget)
313{
314 struct xrx200_chan *ch = container_of(napi,
315 struct xrx200_chan, napi);
316 struct net_device *net_dev = ch->priv->net_dev;
317 int pkts = 0;
318 int bytes = 0;
319
f9317ae5 320 netif_tx_lock(net_dev);
fe1a5642
HM
321 while (pkts < budget) {
322 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->tx_free];
323
324 if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
325 struct sk_buff *skb = ch->skb[ch->tx_free];
326
327 pkts++;
328 bytes += skb->len;
329 ch->skb[ch->tx_free] = NULL;
330 consume_skb(skb);
331 memset(&ch->dma.desc_base[ch->tx_free], 0,
332 sizeof(struct ltq_dma_desc));
333 ch->tx_free++;
334 ch->tx_free %= LTQ_DESC_NUM;
335 } else {
336 break;
337 }
338 }
339
340 net_dev->stats.tx_packets += pkts;
341 net_dev->stats.tx_bytes += bytes;
342 netdev_completed_queue(ch->priv->net_dev, pkts, bytes);
343
f9317ae5 344 netif_tx_unlock(net_dev);
dea36631
HM
345 if (netif_queue_stopped(net_dev))
346 netif_wake_queue(net_dev);
347
fe1a5642 348 if (pkts < budget) {
c582a7fe
HM
349 if (napi_complete_done(&ch->napi, pkts))
350 ltq_dma_enable_irq(&ch->dma);
fe1a5642
HM
351 }
352
353 return pkts;
354}
355
1cfecc23
YW
356static netdev_tx_t xrx200_start_xmit(struct sk_buff *skb,
357 struct net_device *net_dev)
fe1a5642
HM
358{
359 struct xrx200_priv *priv = netdev_priv(net_dev);
360 struct xrx200_chan *ch = &priv->chan_tx;
361 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
362 u32 byte_offset;
363 dma_addr_t mapping;
364 int len;
365
366 skb->dev = net_dev;
367 if (skb_put_padto(skb, ETH_ZLEN)) {
368 net_dev->stats.tx_dropped++;
369 return NETDEV_TX_OK;
370 }
371
372 len = skb->len;
373
374 if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) || ch->skb[ch->dma.desc]) {
375 netdev_err(net_dev, "tx ring full\n");
376 netif_stop_queue(net_dev);
377 return NETDEV_TX_BUSY;
378 }
379
380 ch->skb[ch->dma.desc] = skb;
381
382 mapping = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
383 if (unlikely(dma_mapping_error(priv->dev, mapping)))
384 goto err_drop;
385
14d4e308 386 /* dma needs to start on a burst length value aligned address */
7e553c44 387 byte_offset = mapping % (XRX200_DMA_BURST_LEN * 4);
fe1a5642
HM
388
389 desc->addr = mapping - byte_offset;
390 /* Make sure the address is written before we give it to HW */
391 wmb();
392 desc->ctl = LTQ_DMA_OWN | LTQ_DMA_SOP | LTQ_DMA_EOP |
393 LTQ_DMA_TX_OFFSET(byte_offset) | (len & LTQ_DMA_SIZE_MASK);
394 ch->dma.desc++;
395 ch->dma.desc %= LTQ_DESC_NUM;
396 if (ch->dma.desc == ch->tx_free)
397 netif_stop_queue(net_dev);
398
399 netdev_sent_queue(net_dev, len);
400
401 return NETDEV_TX_OK;
402
403err_drop:
404 dev_kfree_skb(skb);
405 net_dev->stats.tx_dropped++;
406 net_dev->stats.tx_errors++;
407 return NETDEV_TX_OK;
408}
409
998ac358
AJB
410static int
411xrx200_change_mtu(struct net_device *net_dev, int new_mtu)
412{
413 struct xrx200_priv *priv = netdev_priv(net_dev);
414 struct xrx200_chan *ch_rx = &priv->chan_rx;
415 int old_mtu = net_dev->mtu;
416 bool running = false;
e0155935 417 void *buff;
998ac358
AJB
418 int curr_desc;
419 int ret = 0;
420
421 net_dev->mtu = new_mtu;
1488fc20 422 priv->rx_buf_size = xrx200_buffer_size(new_mtu);
e0155935 423 priv->rx_skb_size = xrx200_skb_size(priv->rx_buf_size);
998ac358
AJB
424
425 if (new_mtu <= old_mtu)
426 return ret;
427
428 running = netif_running(net_dev);
429 if (running) {
430 napi_disable(&ch_rx->napi);
431 ltq_dma_close(&ch_rx->dma);
432 }
433
434 xrx200_poll_rx(&ch_rx->napi, LTQ_DESC_NUM);
435 curr_desc = ch_rx->dma.desc;
436
437 for (ch_rx->dma.desc = 0; ch_rx->dma.desc < LTQ_DESC_NUM;
438 ch_rx->dma.desc++) {
e0155935
AJB
439 buff = ch_rx->rx_buff[ch_rx->dma.desc];
440 ret = xrx200_alloc_buf(ch_rx, netdev_alloc_frag);
998ac358
AJB
441 if (ret) {
442 net_dev->mtu = old_mtu;
1488fc20 443 priv->rx_buf_size = xrx200_buffer_size(old_mtu);
e0155935 444 priv->rx_skb_size = xrx200_skb_size(priv->rx_buf_size);
998ac358
AJB
445 break;
446 }
e0155935 447 skb_free_frag(buff);
998ac358
AJB
448 }
449
450 ch_rx->dma.desc = curr_desc;
451 if (running) {
452 napi_enable(&ch_rx->napi);
453 ltq_dma_open(&ch_rx->dma);
454 ltq_dma_enable_irq(&ch_rx->dma);
455 }
456
457 return ret;
458}
459
fe1a5642
HM
460static const struct net_device_ops xrx200_netdev_ops = {
461 .ndo_open = xrx200_open,
462 .ndo_stop = xrx200_close,
463 .ndo_start_xmit = xrx200_start_xmit,
998ac358 464 .ndo_change_mtu = xrx200_change_mtu,
fe1a5642
HM
465 .ndo_set_mac_address = eth_mac_addr,
466 .ndo_validate_addr = eth_validate_addr,
fe1a5642
HM
467};
468
469static irqreturn_t xrx200_dma_irq(int irq, void *ptr)
470{
471 struct xrx200_chan *ch = ptr;
472
9423361d 473 if (napi_schedule_prep(&ch->napi)) {
9423361d 474 ltq_dma_disable_irq(&ch->dma);
f2386cf7 475 __napi_schedule(&ch->napi);
9423361d 476 }
fe1a5642 477
9423361d 478 ltq_dma_ack_irq(&ch->dma);
fe1a5642
HM
479
480 return IRQ_HANDLED;
481}
482
483static int xrx200_dma_init(struct xrx200_priv *priv)
484{
485 struct xrx200_chan *ch_rx = &priv->chan_rx;
486 struct xrx200_chan *ch_tx = &priv->chan_tx;
487 int ret = 0;
488 int i;
489
7e553c44
AJB
490 ltq_dma_init_port(DMA_PORT_ETOP, XRX200_DMA_BURST_LEN,
491 XRX200_DMA_BURST_LEN);
fe1a5642
HM
492
493 ch_rx->dma.nr = XRX200_DMA_RX;
494 ch_rx->dma.dev = priv->dev;
495 ch_rx->priv = priv;
496
497 ltq_dma_alloc_rx(&ch_rx->dma);
498 for (ch_rx->dma.desc = 0; ch_rx->dma.desc < LTQ_DESC_NUM;
499 ch_rx->dma.desc++) {
e0155935 500 ret = xrx200_alloc_buf(ch_rx, netdev_alloc_frag);
fe1a5642
HM
501 if (ret)
502 goto rx_free;
503 }
504 ch_rx->dma.desc = 0;
505 ret = devm_request_irq(priv->dev, ch_rx->dma.irq, xrx200_dma_irq, 0,
506 "xrx200_net_rx", &priv->chan_rx);
507 if (ret) {
508 dev_err(priv->dev, "failed to request RX irq %d\n",
509 ch_rx->dma.irq);
510 goto rx_ring_free;
511 }
512
513 ch_tx->dma.nr = XRX200_DMA_TX;
514 ch_tx->dma.dev = priv->dev;
515 ch_tx->priv = priv;
516
517 ltq_dma_alloc_tx(&ch_tx->dma);
518 ret = devm_request_irq(priv->dev, ch_tx->dma.irq, xrx200_dma_irq, 0,
519 "xrx200_net_tx", &priv->chan_tx);
520 if (ret) {
521 dev_err(priv->dev, "failed to request TX irq %d\n",
522 ch_tx->dma.irq);
523 goto tx_free;
524 }
525
526 return ret;
527
528tx_free:
529 ltq_dma_free(&ch_tx->dma);
530
531rx_ring_free:
532 /* free the allocated RX ring */
533 for (i = 0; i < LTQ_DESC_NUM; i++) {
534 if (priv->chan_rx.skb[i])
e0155935 535 skb_free_frag(priv->chan_rx.rx_buff[i]);
fe1a5642
HM
536 }
537
538rx_free:
539 ltq_dma_free(&ch_rx->dma);
540 return ret;
541}
542
543static void xrx200_hw_cleanup(struct xrx200_priv *priv)
544{
545 int i;
546
547 ltq_dma_free(&priv->chan_tx.dma);
548 ltq_dma_free(&priv->chan_rx.dma);
549
550 /* free the allocated RX ring */
551 for (i = 0; i < LTQ_DESC_NUM; i++)
e0155935 552 skb_free_frag(priv->chan_rx.rx_buff[i]);
fe1a5642
HM
553}
554
555static int xrx200_probe(struct platform_device *pdev)
556{
557 struct device *dev = &pdev->dev;
558 struct device_node *np = dev->of_node;
fe1a5642
HM
559 struct xrx200_priv *priv;
560 struct net_device *net_dev;
fe1a5642
HM
561 int err;
562
563 /* alloc the network device */
564 net_dev = devm_alloc_etherdev(dev, sizeof(struct xrx200_priv));
565 if (!net_dev)
566 return -ENOMEM;
567
568 priv = netdev_priv(net_dev);
569 priv->net_dev = net_dev;
570 priv->dev = dev;
571
572 net_dev->netdev_ops = &xrx200_netdev_ops;
573 SET_NETDEV_DEV(net_dev, dev);
574 net_dev->min_mtu = ETH_ZLEN;
1488fc20
AJB
575 net_dev->max_mtu = XRX200_DMA_DATA_LEN - xrx200_max_frame_len(0);
576 priv->rx_buf_size = xrx200_buffer_size(ETH_DATA_LEN);
e0155935 577 priv->rx_skb_size = xrx200_skb_size(priv->rx_buf_size);
fe1a5642
HM
578
579 /* load the memory ranges */
d402af20 580 priv->pmac_reg = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
d759c1bd 581 if (IS_ERR(priv->pmac_reg))
b8b2de91 582 return PTR_ERR(priv->pmac_reg);
fe1a5642
HM
583
584 priv->chan_rx.dma.irq = platform_get_irq_byname(pdev, "rx");
d1a55841 585 if (priv->chan_rx.dma.irq < 0)
fe1a5642 586 return -ENOENT;
fe1a5642 587 priv->chan_tx.dma.irq = platform_get_irq_byname(pdev, "tx");
d1a55841 588 if (priv->chan_tx.dma.irq < 0)
fe1a5642 589 return -ENOENT;
fe1a5642
HM
590
591 /* get the clock */
592 priv->clk = devm_clk_get(dev, NULL);
593 if (IS_ERR(priv->clk)) {
594 dev_err(dev, "failed to get clock\n");
595 return PTR_ERR(priv->clk);
596 }
597
9ca01b25 598 err = of_get_ethdev_address(np, net_dev);
83216e39 599 if (err)
fe1a5642
HM
600 eth_hw_addr_random(net_dev);
601
602 /* bring up the dma engine and IP core */
603 err = xrx200_dma_init(priv);
604 if (err)
605 return err;
606
a44ecfbd
HM
607 /* enable clock gate */
608 err = clk_prepare_enable(priv->clk);
609 if (err)
610 goto err_uninit_dma;
611
fe1a5642
HM
612 /* set IPG to 12 */
613 xrx200_pmac_mask(priv, PMAC_RX_IPG_MASK, 0xb, PMAC_RX_IPG);
614
615 /* enable status header, enable CRC */
616 xrx200_pmac_mask(priv, 0,
617 PMAC_HD_CTL_RST | PMAC_HD_CTL_AST | PMAC_HD_CTL_RXSH |
618 PMAC_HD_CTL_AS | PMAC_HD_CTL_AC | PMAC_HD_CTL_RC,
619 PMAC_HD_CTL);
620
621 /* setup NAPI */
768818d7
AJB
622 netif_napi_add(net_dev, &priv->chan_rx.napi, xrx200_poll_rx,
623 NAPI_POLL_WEIGHT);
16d083e2
JK
624 netif_napi_add_tx(net_dev, &priv->chan_tx.napi,
625 xrx200_tx_housekeeping);
fe1a5642
HM
626
627 platform_set_drvdata(pdev, priv);
628
629 err = register_netdev(net_dev);
630 if (err)
a44ecfbd 631 goto err_unprepare_clk;
06bc4d00
CJ
632
633 return 0;
fe1a5642 634
a44ecfbd
HM
635err_unprepare_clk:
636 clk_disable_unprepare(priv->clk);
637
fe1a5642
HM
638err_uninit_dma:
639 xrx200_hw_cleanup(priv);
640
06bc4d00 641 return err;
fe1a5642
HM
642}
643
644static int xrx200_remove(struct platform_device *pdev)
645{
646 struct xrx200_priv *priv = platform_get_drvdata(pdev);
647 struct net_device *net_dev = priv->net_dev;
648
649 /* free stack related instances */
650 netif_stop_queue(net_dev);
651 netif_napi_del(&priv->chan_tx.napi);
652 netif_napi_del(&priv->chan_rx.napi);
653
654 /* remove the actual device */
655 unregister_netdev(net_dev);
656
a44ecfbd
HM
657 /* release the clock */
658 clk_disable_unprepare(priv->clk);
659
fe1a5642
HM
660 /* shut down hardware */
661 xrx200_hw_cleanup(priv);
662
663 return 0;
664}
665
666static const struct of_device_id xrx200_match[] = {
667 { .compatible = "lantiq,xrx200-net" },
668 {},
669};
670MODULE_DEVICE_TABLE(of, xrx200_match);
671
672static struct platform_driver xrx200_driver = {
673 .probe = xrx200_probe,
674 .remove = xrx200_remove,
675 .driver = {
676 .name = "lantiq,xrx200-net",
677 .of_match_table = xrx200_match,
678 },
679};
680
681module_platform_driver(xrx200_driver);
682
683MODULE_AUTHOR("John Crispin <john@phrozen.org>");
684MODULE_DESCRIPTION("Lantiq SoC XRX200 ethernet");
685MODULE_LICENSE("GPL");