1 /*
2 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
3 *
4 * Mostly rewritten, based on driver from Sigma Designs. Original
5 * copyright notice below.
6 *
7 *
8 * Driver for tangox SMP864x/SMP865x/SMP867x/SMP868x builtin Ethernet Mac.
9 *
10 * Copyright (C) 2005 Maxime Bizon <mbizon@freebox.fr>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23 #include <linux/module.h>
24 #include <linux/etherdevice.h>
25 #include <linux/delay.h>
26 #include <linux/ethtool.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
29 #include <linux/of_device.h>
30 #include <linux/of_mdio.h>
31 #include <linux/of_net.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/phy.h>
34 #include <linux/cache.h>
35 #include <linux/jiffies.h>
36 #include <linux/io.h>
37 #include <linux/iopoll.h>
38 #include <asm/barrier.h>
39
40 #include "nb8800.h"
41
42 static void nb8800_tx_done(struct net_device *dev);
43 static int nb8800_dma_stop(struct net_device *dev);
44
nb8800_readb(struct nb8800_priv * priv,int reg)45 static inline u8 nb8800_readb(struct nb8800_priv *priv, int reg)
46 {
47 return readb_relaxed(priv->base + reg);
48 }
49
nb8800_readl(struct nb8800_priv * priv,int reg)50 static inline u32 nb8800_readl(struct nb8800_priv *priv, int reg)
51 {
52 return readl_relaxed(priv->base + reg);
53 }
54
nb8800_writeb(struct nb8800_priv * priv,int reg,u8 val)55 static inline void nb8800_writeb(struct nb8800_priv *priv, int reg, u8 val)
56 {
57 writeb_relaxed(val, priv->base + reg);
58 }
59
nb8800_writew(struct nb8800_priv * priv,int reg,u16 val)60 static inline void nb8800_writew(struct nb8800_priv *priv, int reg, u16 val)
61 {
62 writew_relaxed(val, priv->base + reg);
63 }
64
nb8800_writel(struct nb8800_priv * priv,int reg,u32 val)65 static inline void nb8800_writel(struct nb8800_priv *priv, int reg, u32 val)
66 {
67 writel_relaxed(val, priv->base + reg);
68 }
69
nb8800_maskb(struct nb8800_priv * priv,int reg,u32 mask,u32 val)70 static inline void nb8800_maskb(struct nb8800_priv *priv, int reg,
71 u32 mask, u32 val)
72 {
73 u32 old = nb8800_readb(priv, reg);
74 u32 new = (old & ~mask) | (val & mask);
75
76 if (new != old)
77 nb8800_writeb(priv, reg, new);
78 }
79
nb8800_maskl(struct nb8800_priv * priv,int reg,u32 mask,u32 val)80 static inline void nb8800_maskl(struct nb8800_priv *priv, int reg,
81 u32 mask, u32 val)
82 {
83 u32 old = nb8800_readl(priv, reg);
84 u32 new = (old & ~mask) | (val & mask);
85
86 if (new != old)
87 nb8800_writel(priv, reg, new);
88 }
89
nb8800_modb(struct nb8800_priv * priv,int reg,u8 bits,bool set)90 static inline void nb8800_modb(struct nb8800_priv *priv, int reg, u8 bits,
91 bool set)
92 {
93 nb8800_maskb(priv, reg, bits, set ? bits : 0);
94 }
95
nb8800_setb(struct nb8800_priv * priv,int reg,u8 bits)96 static inline void nb8800_setb(struct nb8800_priv *priv, int reg, u8 bits)
97 {
98 nb8800_maskb(priv, reg, bits, bits);
99 }
100
nb8800_clearb(struct nb8800_priv * priv,int reg,u8 bits)101 static inline void nb8800_clearb(struct nb8800_priv *priv, int reg, u8 bits)
102 {
103 nb8800_maskb(priv, reg, bits, 0);
104 }
105
nb8800_modl(struct nb8800_priv * priv,int reg,u32 bits,bool set)106 static inline void nb8800_modl(struct nb8800_priv *priv, int reg, u32 bits,
107 bool set)
108 {
109 nb8800_maskl(priv, reg, bits, set ? bits : 0);
110 }
111
nb8800_setl(struct nb8800_priv * priv,int reg,u32 bits)112 static inline void nb8800_setl(struct nb8800_priv *priv, int reg, u32 bits)
113 {
114 nb8800_maskl(priv, reg, bits, bits);
115 }
116
nb8800_clearl(struct nb8800_priv * priv,int reg,u32 bits)117 static inline void nb8800_clearl(struct nb8800_priv *priv, int reg, u32 bits)
118 {
119 nb8800_maskl(priv, reg, bits, 0);
120 }
121
nb8800_mdio_wait(struct mii_bus * bus)122 static int nb8800_mdio_wait(struct mii_bus *bus)
123 {
124 struct nb8800_priv *priv = bus->priv;
125 u32 val;
126
127 return readl_poll_timeout_atomic(priv->base + NB8800_MDIO_CMD,
128 val, !(val & MDIO_CMD_GO), 1, 1000);
129 }
130
nb8800_mdio_cmd(struct mii_bus * bus,u32 cmd)131 static int nb8800_mdio_cmd(struct mii_bus *bus, u32 cmd)
132 {
133 struct nb8800_priv *priv = bus->priv;
134 int err;
135
136 err = nb8800_mdio_wait(bus);
137 if (err)
138 return err;
139
140 nb8800_writel(priv, NB8800_MDIO_CMD, cmd);
141 udelay(10);
142 nb8800_writel(priv, NB8800_MDIO_CMD, cmd | MDIO_CMD_GO);
143
144 return nb8800_mdio_wait(bus);
145 }
146
nb8800_mdio_read(struct mii_bus * bus,int phy_id,int reg)147 static int nb8800_mdio_read(struct mii_bus *bus, int phy_id, int reg)
148 {
149 struct nb8800_priv *priv = bus->priv;
150 u32 val;
151 int err;
152
153 err = nb8800_mdio_cmd(bus, MDIO_CMD_ADDR(phy_id) | MDIO_CMD_REG(reg));
154 if (err)
155 return err;
156
157 val = nb8800_readl(priv, NB8800_MDIO_STS);
158 if (val & MDIO_STS_ERR)
159 return 0xffff;
160
161 return val & 0xffff;
162 }
163
nb8800_mdio_write(struct mii_bus * bus,int phy_id,int reg,u16 val)164 static int nb8800_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
165 {
166 u32 cmd = MDIO_CMD_ADDR(phy_id) | MDIO_CMD_REG(reg) |
167 MDIO_CMD_DATA(val) | MDIO_CMD_WR;
168
169 return nb8800_mdio_cmd(bus, cmd);
170 }
171
nb8800_mac_tx(struct net_device * dev,bool enable)172 static void nb8800_mac_tx(struct net_device *dev, bool enable)
173 {
174 struct nb8800_priv *priv = netdev_priv(dev);
175
176 while (nb8800_readl(priv, NB8800_TXC_CR) & TCR_EN)
177 cpu_relax();
178
179 nb8800_modb(priv, NB8800_TX_CTL1, TX_EN, enable);
180 }
181
nb8800_mac_rx(struct net_device * dev,bool enable)182 static void nb8800_mac_rx(struct net_device *dev, bool enable)
183 {
184 nb8800_modb(netdev_priv(dev), NB8800_RX_CTL, RX_EN, enable);
185 }
186
nb8800_mac_af(struct net_device * dev,bool enable)187 static void nb8800_mac_af(struct net_device *dev, bool enable)
188 {
189 nb8800_modb(netdev_priv(dev), NB8800_RX_CTL, RX_AF_EN, enable);
190 }
191
nb8800_start_rx(struct net_device * dev)192 static void nb8800_start_rx(struct net_device *dev)
193 {
194 nb8800_setl(netdev_priv(dev), NB8800_RXC_CR, RCR_EN);
195 }
196
nb8800_alloc_rx(struct net_device * dev,unsigned int i,bool napi)197 static int nb8800_alloc_rx(struct net_device *dev, unsigned int i, bool napi)
198 {
199 struct nb8800_priv *priv = netdev_priv(dev);
200 struct nb8800_rx_desc *rxd = &priv->rx_descs[i];
201 struct nb8800_rx_buf *rxb = &priv->rx_bufs[i];
202 int size = L1_CACHE_ALIGN(RX_BUF_SIZE);
203 dma_addr_t dma_addr;
204 struct page *page;
205 unsigned long offset;
206 void *data;
207
208 data = napi ? napi_alloc_frag(size) : netdev_alloc_frag(size);
209 if (!data)
210 return -ENOMEM;
211
212 page = virt_to_head_page(data);
213 offset = data - page_address(page);
214
215 dma_addr = dma_map_page(&dev->dev, page, offset, RX_BUF_SIZE,
216 DMA_FROM_DEVICE);
217
218 if (dma_mapping_error(&dev->dev, dma_addr)) {
219 skb_free_frag(data);
220 return -ENOMEM;
221 }
222
223 rxb->page = page;
224 rxb->offset = offset;
225 rxd->desc.s_addr = dma_addr;
226
227 return 0;
228 }
229
nb8800_receive(struct net_device * dev,unsigned int i,unsigned int len)230 static void nb8800_receive(struct net_device *dev, unsigned int i,
231 unsigned int len)
232 {
233 struct nb8800_priv *priv = netdev_priv(dev);
234 struct nb8800_rx_desc *rxd = &priv->rx_descs[i];
235 struct page *page = priv->rx_bufs[i].page;
236 int offset = priv->rx_bufs[i].offset;
237 void *data = page_address(page) + offset;
238 dma_addr_t dma = rxd->desc.s_addr;
239 struct sk_buff *skb;
240 unsigned int size;
241 int err;
242
243 size = len <= RX_COPYBREAK ? len : RX_COPYHDR;
244
245 skb = napi_alloc_skb(&priv->napi, size);
246 if (!skb) {
247 netdev_err(dev, "rx skb allocation failed\n");
248 dev->stats.rx_dropped++;
249 return;
250 }
251
252 if (len <= RX_COPYBREAK) {
253 dma_sync_single_for_cpu(&dev->dev, dma, len, DMA_FROM_DEVICE);
254 skb_put_data(skb, data, len);
255 dma_sync_single_for_device(&dev->dev, dma, len,
256 DMA_FROM_DEVICE);
257 } else {
258 err = nb8800_alloc_rx(dev, i, true);
259 if (err) {
260 netdev_err(dev, "rx buffer allocation failed\n");
261 dev->stats.rx_dropped++;
262 dev_kfree_skb(skb);
263 return;
264 }
265
266 dma_unmap_page(&dev->dev, dma, RX_BUF_SIZE, DMA_FROM_DEVICE);
267 skb_put_data(skb, data, RX_COPYHDR);
268 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
269 offset + RX_COPYHDR, len - RX_COPYHDR,
270 RX_BUF_SIZE);
271 }
272
273 skb->protocol = eth_type_trans(skb, dev);
274 napi_gro_receive(&priv->napi, skb);
275 }
276
nb8800_rx_error(struct net_device * dev,u32 report)277 static void nb8800_rx_error(struct net_device *dev, u32 report)
278 {
279 if (report & RX_LENGTH_ERR)
280 dev->stats.rx_length_errors++;
281
282 if (report & RX_FCS_ERR)
283 dev->stats.rx_crc_errors++;
284
285 if (report & RX_FIFO_OVERRUN)
286 dev->stats.rx_fifo_errors++;
287
288 if (report & RX_ALIGNMENT_ERROR)
289 dev->stats.rx_frame_errors++;
290
291 dev->stats.rx_errors++;
292 }
293
nb8800_poll(struct napi_struct * napi,int budget)294 static int nb8800_poll(struct napi_struct *napi, int budget)
295 {
296 struct net_device *dev = napi->dev;
297 struct nb8800_priv *priv = netdev_priv(dev);
298 struct nb8800_rx_desc *rxd;
299 unsigned int last = priv->rx_eoc;
300 unsigned int next;
301 int work = 0;
302
303 nb8800_tx_done(dev);
304
305 again:
306 do {
307 unsigned int len;
308
309 next = (last + 1) % RX_DESC_COUNT;
310
311 rxd = &priv->rx_descs[next];
312
313 if (!rxd->report)
314 break;
315
316 len = RX_BYTES_TRANSFERRED(rxd->report);
317
318 if (IS_RX_ERROR(rxd->report))
319 nb8800_rx_error(dev, rxd->report);
320 else
321 nb8800_receive(dev, next, len);
322
323 dev->stats.rx_packets++;
324 dev->stats.rx_bytes += len;
325
326 if (rxd->report & RX_MULTICAST_PKT)
327 dev->stats.multicast++;
328
329 rxd->report = 0;
330 last = next;
331 work++;
332 } while (work < budget);
333
334 if (work) {
335 priv->rx_descs[last].desc.config |= DESC_EOC;
336 wmb(); /* ensure new EOC is written before clearing old */
337 priv->rx_descs[priv->rx_eoc].desc.config &= ~DESC_EOC;
338 priv->rx_eoc = last;
339 nb8800_start_rx(dev);
340 }
341
342 if (work < budget) {
343 nb8800_writel(priv, NB8800_RX_ITR, priv->rx_itr_irq);
344
345 /* If a packet arrived after we last checked but
346 * before writing RX_ITR, the interrupt will be
347 * delayed, so we retrieve it now.
348 */
349 if (priv->rx_descs[next].report)
350 goto again;
351
352 napi_complete_done(napi, work);
353 }
354
355 return work;
356 }
357
__nb8800_tx_dma_start(struct net_device * dev)358 static void __nb8800_tx_dma_start(struct net_device *dev)
359 {
360 struct nb8800_priv *priv = netdev_priv(dev);
361 struct nb8800_tx_buf *txb;
362 u32 txc_cr;
363
364 txb = &priv->tx_bufs[priv->tx_queue];
365 if (!txb->ready)
366 return;
367
368 txc_cr = nb8800_readl(priv, NB8800_TXC_CR);
369 if (txc_cr & TCR_EN)
370 return;
371
372 nb8800_writel(priv, NB8800_TX_DESC_ADDR, txb->dma_desc);
373 wmb(); /* ensure desc addr is written before starting DMA */
374 nb8800_writel(priv, NB8800_TXC_CR, txc_cr | TCR_EN);
375
376 priv->tx_queue = (priv->tx_queue + txb->chain_len) % TX_DESC_COUNT;
377 }
378
nb8800_tx_dma_start(struct net_device * dev)379 static void nb8800_tx_dma_start(struct net_device *dev)
380 {
381 struct nb8800_priv *priv = netdev_priv(dev);
382
383 spin_lock_irq(&priv->tx_lock);
384 __nb8800_tx_dma_start(dev);
385 spin_unlock_irq(&priv->tx_lock);
386 }
387
nb8800_tx_dma_start_irq(struct net_device * dev)388 static void nb8800_tx_dma_start_irq(struct net_device *dev)
389 {
390 struct nb8800_priv *priv = netdev_priv(dev);
391
392 spin_lock(&priv->tx_lock);
393 __nb8800_tx_dma_start(dev);
394 spin_unlock(&priv->tx_lock);
395 }
396
nb8800_xmit(struct sk_buff * skb,struct net_device * dev)397 static int nb8800_xmit(struct sk_buff *skb, struct net_device *dev)
398 {
399 struct nb8800_priv *priv = netdev_priv(dev);
400 struct nb8800_tx_desc *txd;
401 struct nb8800_tx_buf *txb;
402 struct nb8800_dma_desc *desc;
403 dma_addr_t dma_addr;
404 unsigned int dma_len;
405 unsigned int align;
406 unsigned int next;
407
408 if (atomic_read(&priv->tx_free) <= NB8800_DESC_LOW) {
409 netif_stop_queue(dev);
410 return NETDEV_TX_BUSY;
411 }
412
413 align = (8 - (uintptr_t)skb->data) & 7;
414
415 dma_len = skb->len - align;
416 dma_addr = dma_map_single(&dev->dev, skb->data + align,
417 dma_len, DMA_TO_DEVICE);
418
419 if (dma_mapping_error(&dev->dev, dma_addr)) {
420 netdev_err(dev, "tx dma mapping error\n");
421 kfree_skb(skb);
422 dev->stats.tx_dropped++;
423 return NETDEV_TX_OK;
424 }
425
426 if (atomic_dec_return(&priv->tx_free) <= NB8800_DESC_LOW) {
427 netif_stop_queue(dev);
428 skb->xmit_more = 0;
429 }
430
431 next = priv->tx_next;
432 txb = &priv->tx_bufs[next];
433 txd = &priv->tx_descs[next];
434 desc = &txd->desc[0];
435
436 next = (next + 1) % TX_DESC_COUNT;
437
438 if (align) {
439 memcpy(txd->buf, skb->data, align);
440
441 desc->s_addr =
442 txb->dma_desc + offsetof(struct nb8800_tx_desc, buf);
443 desc->n_addr = txb->dma_desc + sizeof(txd->desc[0]);
444 desc->config = DESC_BTS(2) | DESC_DS | align;
445
446 desc++;
447 }
448
449 desc->s_addr = dma_addr;
450 desc->n_addr = priv->tx_bufs[next].dma_desc;
451 desc->config = DESC_BTS(2) | DESC_DS | DESC_EOF | dma_len;
452
453 if (!skb->xmit_more)
454 desc->config |= DESC_EOC;
455
456 txb->skb = skb;
457 txb->dma_addr = dma_addr;
458 txb->dma_len = dma_len;
459
460 if (!priv->tx_chain) {
461 txb->chain_len = 1;
462 priv->tx_chain = txb;
463 } else {
464 priv->tx_chain->chain_len++;
465 }
466
467 netdev_sent_queue(dev, skb->len);
468
469 priv->tx_next = next;
470
471 if (!skb->xmit_more) {
472 smp_wmb();
473 priv->tx_chain->ready = true;
474 priv->tx_chain = NULL;
475 nb8800_tx_dma_start(dev);
476 }
477
478 return NETDEV_TX_OK;
479 }
480
nb8800_tx_error(struct net_device * dev,u32 report)481 static void nb8800_tx_error(struct net_device *dev, u32 report)
482 {
483 if (report & TX_LATE_COLLISION)
484 dev->stats.collisions++;
485
486 if (report & TX_PACKET_DROPPED)
487 dev->stats.tx_dropped++;
488
489 if (report & TX_FIFO_UNDERRUN)
490 dev->stats.tx_fifo_errors++;
491
492 dev->stats.tx_errors++;
493 }
494
nb8800_tx_done(struct net_device * dev)495 static void nb8800_tx_done(struct net_device *dev)
496 {
497 struct nb8800_priv *priv = netdev_priv(dev);
498 unsigned int limit = priv->tx_next;
499 unsigned int done = priv->tx_done;
500 unsigned int packets = 0;
501 unsigned int len = 0;
502
503 while (done != limit) {
504 struct nb8800_tx_desc *txd = &priv->tx_descs[done];
505 struct nb8800_tx_buf *txb = &priv->tx_bufs[done];
506 struct sk_buff *skb;
507
508 if (!txd->report)
509 break;
510
511 skb = txb->skb;
512 len += skb->len;
513
514 dma_unmap_single(&dev->dev, txb->dma_addr, txb->dma_len,
515 DMA_TO_DEVICE);
516
517 if (IS_TX_ERROR(txd->report)) {
518 nb8800_tx_error(dev, txd->report);
519 kfree_skb(skb);
520 } else {
521 consume_skb(skb);
522 }
523
524 dev->stats.tx_packets++;
525 dev->stats.tx_bytes += TX_BYTES_TRANSFERRED(txd->report);
526 dev->stats.collisions += TX_EARLY_COLLISIONS(txd->report);
527
528 txb->skb = NULL;
529 txb->ready = false;
530 txd->report = 0;
531
532 done = (done + 1) % TX_DESC_COUNT;
533 packets++;
534 }
535
536 if (packets) {
537 smp_mb__before_atomic();
538 atomic_add(packets, &priv->tx_free);
539 netdev_completed_queue(dev, packets, len);
540 netif_wake_queue(dev);
541 priv->tx_done = done;
542 }
543 }
544
nb8800_irq(int irq,void * dev_id)545 static irqreturn_t nb8800_irq(int irq, void *dev_id)
546 {
547 struct net_device *dev = dev_id;
548 struct nb8800_priv *priv = netdev_priv(dev);
549 irqreturn_t ret = IRQ_NONE;
550 u32 val;
551
552 /* tx interrupt */
553 val = nb8800_readl(priv, NB8800_TXC_SR);
554 if (val) {
555 nb8800_writel(priv, NB8800_TXC_SR, val);
556
557 if (val & TSR_DI)
558 nb8800_tx_dma_start_irq(dev);
559
560 if (val & TSR_TI)
561 napi_schedule_irqoff(&priv->napi);
562
563 if (unlikely(val & TSR_DE))
564 netdev_err(dev, "TX DMA error\n");
565
566 /* should never happen with automatic status retrieval */
567 if (unlikely(val & TSR_TO))
568 netdev_err(dev, "TX Status FIFO overflow\n");
569
570 ret = IRQ_HANDLED;
571 }
572
573 /* rx interrupt */
574 val = nb8800_readl(priv, NB8800_RXC_SR);
575 if (val) {
576 nb8800_writel(priv, NB8800_RXC_SR, val);
577
578 if (likely(val & (RSR_RI | RSR_DI))) {
579 nb8800_writel(priv, NB8800_RX_ITR, priv->rx_itr_poll);
580 napi_schedule_irqoff(&priv->napi);
581 }
582
583 if (unlikely(val & RSR_DE))
584 netdev_err(dev, "RX DMA error\n");
585
586 /* should never happen with automatic status retrieval */
587 if (unlikely(val & RSR_RO))
588 netdev_err(dev, "RX Status FIFO overflow\n");
589
590 ret = IRQ_HANDLED;
591 }
592
593 return ret;
594 }
595
nb8800_mac_config(struct net_device * dev)596 static void nb8800_mac_config(struct net_device *dev)
597 {
598 struct nb8800_priv *priv = netdev_priv(dev);
599 bool gigabit = priv->speed == SPEED_1000;
600 u32 mac_mode_mask = RGMII_MODE | HALF_DUPLEX | GMAC_MODE;
601 u32 mac_mode = 0;
602 u32 slot_time;
603 u32 phy_clk;
604 u32 ict;
605
606 if (!priv->duplex)
607 mac_mode |= HALF_DUPLEX;
608
609 if (gigabit) {
610 if (phy_interface_is_rgmii(dev->phydev))
611 mac_mode |= RGMII_MODE;
612
613 mac_mode |= GMAC_MODE;
614 phy_clk = 125000000;
615
616 /* Should be 512 but register is only 8 bits */
617 slot_time = 255;
618 } else {
619 phy_clk = 25000000;
620 slot_time = 128;
621 }
622
623 ict = DIV_ROUND_UP(phy_clk, clk_get_rate(priv->clk));
624
625 nb8800_writeb(priv, NB8800_IC_THRESHOLD, ict);
626 nb8800_writeb(priv, NB8800_SLOT_TIME, slot_time);
627 nb8800_maskb(priv, NB8800_MAC_MODE, mac_mode_mask, mac_mode);
628 }
629
nb8800_pause_config(struct net_device * dev)630 static void nb8800_pause_config(struct net_device *dev)
631 {
632 struct nb8800_priv *priv = netdev_priv(dev);
633 struct phy_device *phydev = dev->phydev;
634 u32 rxcr;
635
636 if (priv->pause_aneg) {
637 if (!phydev || !phydev->link)
638 return;
639
640 priv->pause_rx = phydev->pause;
641 priv->pause_tx = phydev->pause ^ phydev->asym_pause;
642 }
643
644 nb8800_modb(priv, NB8800_RX_CTL, RX_PAUSE_EN, priv->pause_rx);
645
646 rxcr = nb8800_readl(priv, NB8800_RXC_CR);
647 if (!!(rxcr & RCR_FL) == priv->pause_tx)
648 return;
649
650 if (netif_running(dev)) {
651 napi_disable(&priv->napi);
652 netif_tx_lock_bh(dev);
653 nb8800_dma_stop(dev);
654 nb8800_modl(priv, NB8800_RXC_CR, RCR_FL, priv->pause_tx);
655 nb8800_start_rx(dev);
656 netif_tx_unlock_bh(dev);
657 napi_enable(&priv->napi);
658 } else {
659 nb8800_modl(priv, NB8800_RXC_CR, RCR_FL, priv->pause_tx);
660 }
661 }
662
nb8800_link_reconfigure(struct net_device * dev)663 static void nb8800_link_reconfigure(struct net_device *dev)
664 {
665 struct nb8800_priv *priv = netdev_priv(dev);
666 struct phy_device *phydev = dev->phydev;
667 int change = 0;
668
669 if (phydev->link) {
670 if (phydev->speed != priv->speed) {
671 priv->speed = phydev->speed;
672 change = 1;
673 }
674
675 if (phydev->duplex != priv->duplex) {
676 priv->duplex = phydev->duplex;
677 change = 1;
678 }
679
680 if (change)
681 nb8800_mac_config(dev);
682
683 nb8800_pause_config(dev);
684 }
685
686 if (phydev->link != priv->link) {
687 priv->link = phydev->link;
688 change = 1;
689 }
690
691 if (change)
692 phy_print_status(phydev);
693 }
694
nb8800_update_mac_addr(struct net_device * dev)695 static void nb8800_update_mac_addr(struct net_device *dev)
696 {
697 struct nb8800_priv *priv = netdev_priv(dev);
698 int i;
699
700 for (i = 0; i < ETH_ALEN; i++)
701 nb8800_writeb(priv, NB8800_SRC_ADDR(i), dev->dev_addr[i]);
702
703 for (i = 0; i < ETH_ALEN; i++)
704 nb8800_writeb(priv, NB8800_UC_ADDR(i), dev->dev_addr[i]);
705 }
706
nb8800_set_mac_address(struct net_device * dev,void * addr)707 static int nb8800_set_mac_address(struct net_device *dev, void *addr)
708 {
709 struct sockaddr *sock = addr;
710
711 if (netif_running(dev))
712 return -EBUSY;
713
714 ether_addr_copy(dev->dev_addr, sock->sa_data);
715 nb8800_update_mac_addr(dev);
716
717 return 0;
718 }
719
nb8800_mc_init(struct net_device * dev,int val)720 static void nb8800_mc_init(struct net_device *dev, int val)
721 {
722 struct nb8800_priv *priv = netdev_priv(dev);
723
724 nb8800_writeb(priv, NB8800_MC_INIT, val);
725 readb_poll_timeout_atomic(priv->base + NB8800_MC_INIT, val, !val,
726 1, 1000);
727 }
728
nb8800_set_rx_mode(struct net_device * dev)729 static void nb8800_set_rx_mode(struct net_device *dev)
730 {
731 struct nb8800_priv *priv = netdev_priv(dev);
732 struct netdev_hw_addr *ha;
733 int i;
734
735 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
736 nb8800_mac_af(dev, false);
737 return;
738 }
739
740 nb8800_mac_af(dev, true);
741 nb8800_mc_init(dev, 0);
742
743 netdev_for_each_mc_addr(ha, dev) {
744 for (i = 0; i < ETH_ALEN; i++)
745 nb8800_writeb(priv, NB8800_MC_ADDR(i), ha->addr[i]);
746
747 nb8800_mc_init(dev, 0xff);
748 }
749 }
750
751 #define RX_DESC_SIZE (RX_DESC_COUNT * sizeof(struct nb8800_rx_desc))
752 #define TX_DESC_SIZE (TX_DESC_COUNT * sizeof(struct nb8800_tx_desc))
753
nb8800_dma_free(struct net_device * dev)754 static void nb8800_dma_free(struct net_device *dev)
755 {
756 struct nb8800_priv *priv = netdev_priv(dev);
757 unsigned int i;
758
759 if (priv->rx_bufs) {
760 for (i = 0; i < RX_DESC_COUNT; i++)
761 if (priv->rx_bufs[i].page)
762 put_page(priv->rx_bufs[i].page);
763
764 kfree(priv->rx_bufs);
765 priv->rx_bufs = NULL;
766 }
767
768 if (priv->tx_bufs) {
769 for (i = 0; i < TX_DESC_COUNT; i++)
770 kfree_skb(priv->tx_bufs[i].skb);
771
772 kfree(priv->tx_bufs);
773 priv->tx_bufs = NULL;
774 }
775
776 if (priv->rx_descs) {
777 dma_free_coherent(dev->dev.parent, RX_DESC_SIZE, priv->rx_descs,
778 priv->rx_desc_dma);
779 priv->rx_descs = NULL;
780 }
781
782 if (priv->tx_descs) {
783 dma_free_coherent(dev->dev.parent, TX_DESC_SIZE, priv->tx_descs,
784 priv->tx_desc_dma);
785 priv->tx_descs = NULL;
786 }
787 }
788
nb8800_dma_reset(struct net_device * dev)789 static void nb8800_dma_reset(struct net_device *dev)
790 {
791 struct nb8800_priv *priv = netdev_priv(dev);
792 struct nb8800_rx_desc *rxd;
793 struct nb8800_tx_desc *txd;
794 unsigned int i;
795
796 for (i = 0; i < RX_DESC_COUNT; i++) {
797 dma_addr_t rx_dma = priv->rx_desc_dma + i * sizeof(*rxd);
798
799 rxd = &priv->rx_descs[i];
800 rxd->desc.n_addr = rx_dma + sizeof(*rxd);
801 rxd->desc.r_addr =
802 rx_dma + offsetof(struct nb8800_rx_desc, report);
803 rxd->desc.config = priv->rx_dma_config;
804 rxd->report = 0;
805 }
806
807 rxd->desc.n_addr = priv->rx_desc_dma;
808 rxd->desc.config |= DESC_EOC;
809
810 priv->rx_eoc = RX_DESC_COUNT - 1;
811
812 for (i = 0; i < TX_DESC_COUNT; i++) {
813 struct nb8800_tx_buf *txb = &priv->tx_bufs[i];
814 dma_addr_t r_dma = txb->dma_desc +
815 offsetof(struct nb8800_tx_desc, report);
816
817 txd = &priv->tx_descs[i];
818 txd->desc[0].r_addr = r_dma;
819 txd->desc[1].r_addr = r_dma;
820 txd->report = 0;
821 }
822
823 priv->tx_next = 0;
824 priv->tx_queue = 0;
825 priv->tx_done = 0;
826 atomic_set(&priv->tx_free, TX_DESC_COUNT);
827
828 nb8800_writel(priv, NB8800_RX_DESC_ADDR, priv->rx_desc_dma);
829
830 wmb(); /* ensure all setup is written before starting */
831 }
832
nb8800_dma_init(struct net_device * dev)833 static int nb8800_dma_init(struct net_device *dev)
834 {
835 struct nb8800_priv *priv = netdev_priv(dev);
836 unsigned int n_rx = RX_DESC_COUNT;
837 unsigned int n_tx = TX_DESC_COUNT;
838 unsigned int i;
839 int err;
840
841 priv->rx_descs = dma_alloc_coherent(dev->dev.parent, RX_DESC_SIZE,
842 &priv->rx_desc_dma, GFP_KERNEL);
843 if (!priv->rx_descs)
844 goto err_out;
845
846 priv->rx_bufs = kcalloc(n_rx, sizeof(*priv->rx_bufs), GFP_KERNEL);
847 if (!priv->rx_bufs)
848 goto err_out;
849
850 for (i = 0; i < n_rx; i++) {
851 err = nb8800_alloc_rx(dev, i, false);
852 if (err)
853 goto err_out;
854 }
855
856 priv->tx_descs = dma_alloc_coherent(dev->dev.parent, TX_DESC_SIZE,
857 &priv->tx_desc_dma, GFP_KERNEL);
858 if (!priv->tx_descs)
859 goto err_out;
860
861 priv->tx_bufs = kcalloc(n_tx, sizeof(*priv->tx_bufs), GFP_KERNEL);
862 if (!priv->tx_bufs)
863 goto err_out;
864
865 for (i = 0; i < n_tx; i++)
866 priv->tx_bufs[i].dma_desc =
867 priv->tx_desc_dma + i * sizeof(struct nb8800_tx_desc);
868
869 nb8800_dma_reset(dev);
870
871 return 0;
872
873 err_out:
874 nb8800_dma_free(dev);
875
876 return -ENOMEM;
877 }
878
nb8800_dma_stop(struct net_device * dev)879 static int nb8800_dma_stop(struct net_device *dev)
880 {
881 struct nb8800_priv *priv = netdev_priv(dev);
882 struct nb8800_tx_buf *txb = &priv->tx_bufs[0];
883 struct nb8800_tx_desc *txd = &priv->tx_descs[0];
884 int retry = 5;
885 u32 txcr;
886 u32 rxcr;
887 int err;
888 unsigned int i;
889
890 /* wait for tx to finish */
891 err = readl_poll_timeout_atomic(priv->base + NB8800_TXC_CR, txcr,
892 !(txcr & TCR_EN) &&
893 priv->tx_done == priv->tx_next,
894 1000, 1000000);
895 if (err)
896 return err;
897
898 /* The rx DMA only stops if it reaches the end of chain.
899 * To make this happen, we set the EOC flag on all rx
900 * descriptors, put the device in loopback mode, and send
901 * a few dummy frames. The interrupt handler will ignore
902 * these since NAPI is disabled and no real frames are in
903 * the tx queue.
904 */
905
906 for (i = 0; i < RX_DESC_COUNT; i++)
907 priv->rx_descs[i].desc.config |= DESC_EOC;
908
909 txd->desc[0].s_addr =
910 txb->dma_desc + offsetof(struct nb8800_tx_desc, buf);
911 txd->desc[0].config = DESC_BTS(2) | DESC_DS | DESC_EOF | DESC_EOC | 8;
912 memset(txd->buf, 0, sizeof(txd->buf));
913
914 nb8800_mac_af(dev, false);
915 nb8800_setb(priv, NB8800_MAC_MODE, LOOPBACK_EN);
916
917 do {
918 nb8800_writel(priv, NB8800_TX_DESC_ADDR, txb->dma_desc);
919 wmb();
920 nb8800_writel(priv, NB8800_TXC_CR, txcr | TCR_EN);
921
922 err = readl_poll_timeout_atomic(priv->base + NB8800_RXC_CR,
923 rxcr, !(rxcr & RCR_EN),
924 1000, 100000);
925 } while (err && --retry);
926
927 nb8800_mac_af(dev, true);
928 nb8800_clearb(priv, NB8800_MAC_MODE, LOOPBACK_EN);
929 nb8800_dma_reset(dev);
930
931 return retry ? 0 : -ETIMEDOUT;
932 }
933
nb8800_pause_adv(struct net_device * dev)934 static void nb8800_pause_adv(struct net_device *dev)
935 {
936 struct nb8800_priv *priv = netdev_priv(dev);
937 struct phy_device *phydev = dev->phydev;
938 u32 adv = 0;
939
940 if (!phydev)
941 return;
942
943 if (priv->pause_rx)
944 adv |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
945 if (priv->pause_tx)
946 adv ^= ADVERTISED_Asym_Pause;
947
948 phydev->supported |= adv;
949 phydev->advertising |= adv;
950 }
951
nb8800_open(struct net_device * dev)952 static int nb8800_open(struct net_device *dev)
953 {
954 struct nb8800_priv *priv = netdev_priv(dev);
955 struct phy_device *phydev;
956 int err;
957
958 /* clear any pending interrupts */
959 nb8800_writel(priv, NB8800_RXC_SR, 0xf);
960 nb8800_writel(priv, NB8800_TXC_SR, 0xf);
961
962 err = nb8800_dma_init(dev);
963 if (err)
964 return err;
965
966 err = request_irq(dev->irq, nb8800_irq, 0, dev_name(&dev->dev), dev);
967 if (err)
968 goto err_free_dma;
969
970 nb8800_mac_rx(dev, true);
971 nb8800_mac_tx(dev, true);
972
973 phydev = of_phy_connect(dev, priv->phy_node,
974 nb8800_link_reconfigure, 0,
975 priv->phy_mode);
976 if (!phydev) {
977 err = -ENODEV;
978 goto err_free_irq;
979 }
980
981 nb8800_pause_adv(dev);
982
983 netdev_reset_queue(dev);
984 napi_enable(&priv->napi);
985 netif_start_queue(dev);
986
987 nb8800_start_rx(dev);
988 phy_start(phydev);
989
990 return 0;
991
992 err_free_irq:
993 free_irq(dev->irq, dev);
994 err_free_dma:
995 nb8800_dma_free(dev);
996
997 return err;
998 }
999
nb8800_stop(struct net_device * dev)1000 static int nb8800_stop(struct net_device *dev)
1001 {
1002 struct nb8800_priv *priv = netdev_priv(dev);
1003 struct phy_device *phydev = dev->phydev;
1004
1005 phy_stop(phydev);
1006
1007 netif_stop_queue(dev);
1008 napi_disable(&priv->napi);
1009
1010 nb8800_dma_stop(dev);
1011 nb8800_mac_rx(dev, false);
1012 nb8800_mac_tx(dev, false);
1013
1014 phy_disconnect(phydev);
1015
1016 free_irq(dev->irq, dev);
1017
1018 nb8800_dma_free(dev);
1019
1020 return 0;
1021 }
1022
nb8800_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)1023 static int nb8800_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1024 {
1025 return phy_mii_ioctl(dev->phydev, rq, cmd);
1026 }
1027
1028 static const struct net_device_ops nb8800_netdev_ops = {
1029 .ndo_open = nb8800_open,
1030 .ndo_stop = nb8800_stop,
1031 .ndo_start_xmit = nb8800_xmit,
1032 .ndo_set_mac_address = nb8800_set_mac_address,
1033 .ndo_set_rx_mode = nb8800_set_rx_mode,
1034 .ndo_do_ioctl = nb8800_ioctl,
1035 .ndo_validate_addr = eth_validate_addr,
1036 };
1037
nb8800_get_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pp)1038 static void nb8800_get_pauseparam(struct net_device *dev,
1039 struct ethtool_pauseparam *pp)
1040 {
1041 struct nb8800_priv *priv = netdev_priv(dev);
1042
1043 pp->autoneg = priv->pause_aneg;
1044 pp->rx_pause = priv->pause_rx;
1045 pp->tx_pause = priv->pause_tx;
1046 }
1047
nb8800_set_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pp)1048 static int nb8800_set_pauseparam(struct net_device *dev,
1049 struct ethtool_pauseparam *pp)
1050 {
1051 struct nb8800_priv *priv = netdev_priv(dev);
1052 struct phy_device *phydev = dev->phydev;
1053
1054 priv->pause_aneg = pp->autoneg;
1055 priv->pause_rx = pp->rx_pause;
1056 priv->pause_tx = pp->tx_pause;
1057
1058 nb8800_pause_adv(dev);
1059
1060 if (!priv->pause_aneg)
1061 nb8800_pause_config(dev);
1062 else if (phydev)
1063 phy_start_aneg(phydev);
1064
1065 return 0;
1066 }
1067
1068 static const char nb8800_stats_names[][ETH_GSTRING_LEN] = {
1069 "rx_bytes_ok",
1070 "rx_frames_ok",
1071 "rx_undersize_frames",
1072 "rx_fragment_frames",
1073 "rx_64_byte_frames",
1074 "rx_127_byte_frames",
1075 "rx_255_byte_frames",
1076 "rx_511_byte_frames",
1077 "rx_1023_byte_frames",
1078 "rx_max_size_frames",
1079 "rx_oversize_frames",
1080 "rx_bad_fcs_frames",
1081 "rx_broadcast_frames",
1082 "rx_multicast_frames",
1083 "rx_control_frames",
1084 "rx_pause_frames",
1085 "rx_unsup_control_frames",
1086 "rx_align_error_frames",
1087 "rx_overrun_frames",
1088 "rx_jabber_frames",
1089 "rx_bytes",
1090 "rx_frames",
1091
1092 "tx_bytes_ok",
1093 "tx_frames_ok",
1094 "tx_64_byte_frames",
1095 "tx_127_byte_frames",
1096 "tx_255_byte_frames",
1097 "tx_511_byte_frames",
1098 "tx_1023_byte_frames",
1099 "tx_max_size_frames",
1100 "tx_oversize_frames",
1101 "tx_broadcast_frames",
1102 "tx_multicast_frames",
1103 "tx_control_frames",
1104 "tx_pause_frames",
1105 "tx_underrun_frames",
1106 "tx_single_collision_frames",
1107 "tx_multi_collision_frames",
1108 "tx_deferred_collision_frames",
1109 "tx_late_collision_frames",
1110 "tx_excessive_collision_frames",
1111 "tx_bytes",
1112 "tx_frames",
1113 "tx_collisions",
1114 };
1115
1116 #define NB8800_NUM_STATS ARRAY_SIZE(nb8800_stats_names)
1117
nb8800_get_sset_count(struct net_device * dev,int sset)1118 static int nb8800_get_sset_count(struct net_device *dev, int sset)
1119 {
1120 if (sset == ETH_SS_STATS)
1121 return NB8800_NUM_STATS;
1122
1123 return -EOPNOTSUPP;
1124 }
1125
nb8800_get_strings(struct net_device * dev,u32 sset,u8 * buf)1126 static void nb8800_get_strings(struct net_device *dev, u32 sset, u8 *buf)
1127 {
1128 if (sset == ETH_SS_STATS)
1129 memcpy(buf, &nb8800_stats_names, sizeof(nb8800_stats_names));
1130 }
1131
nb8800_read_stat(struct net_device * dev,int index)1132 static u32 nb8800_read_stat(struct net_device *dev, int index)
1133 {
1134 struct nb8800_priv *priv = netdev_priv(dev);
1135
1136 nb8800_writeb(priv, NB8800_STAT_INDEX, index);
1137
1138 return nb8800_readl(priv, NB8800_STAT_DATA);
1139 }
1140
nb8800_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * estats,u64 * st)1141 static void nb8800_get_ethtool_stats(struct net_device *dev,
1142 struct ethtool_stats *estats, u64 *st)
1143 {
1144 unsigned int i;
1145 u32 rx, tx;
1146
1147 for (i = 0; i < NB8800_NUM_STATS / 2; i++) {
1148 rx = nb8800_read_stat(dev, i);
1149 tx = nb8800_read_stat(dev, i | 0x80);
1150 st[i] = rx;
1151 st[i + NB8800_NUM_STATS / 2] = tx;
1152 }
1153 }
1154
1155 static const struct ethtool_ops nb8800_ethtool_ops = {
1156 .nway_reset = phy_ethtool_nway_reset,
1157 .get_link = ethtool_op_get_link,
1158 .get_pauseparam = nb8800_get_pauseparam,
1159 .set_pauseparam = nb8800_set_pauseparam,
1160 .get_sset_count = nb8800_get_sset_count,
1161 .get_strings = nb8800_get_strings,
1162 .get_ethtool_stats = nb8800_get_ethtool_stats,
1163 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1164 .set_link_ksettings = phy_ethtool_set_link_ksettings,
1165 };
1166
nb8800_hw_init(struct net_device * dev)1167 static int nb8800_hw_init(struct net_device *dev)
1168 {
1169 struct nb8800_priv *priv = netdev_priv(dev);
1170 u32 val;
1171
1172 val = TX_RETRY_EN | TX_PAD_EN | TX_APPEND_FCS;
1173 nb8800_writeb(priv, NB8800_TX_CTL1, val);
1174
1175 /* Collision retry count */
1176 nb8800_writeb(priv, NB8800_TX_CTL2, 5);
1177
1178 val = RX_PAD_STRIP | RX_AF_EN;
1179 nb8800_writeb(priv, NB8800_RX_CTL, val);
1180
1181 /* Chosen by fair dice roll */
1182 nb8800_writeb(priv, NB8800_RANDOM_SEED, 4);
1183
1184 /* TX cycles per deferral period */
1185 nb8800_writeb(priv, NB8800_TX_SDP, 12);
1186
1187 /* The following three threshold values have been
1188 * experimentally determined for good results.
1189 */
1190
1191 /* RX/TX FIFO threshold for partial empty (64-bit entries) */
1192 nb8800_writeb(priv, NB8800_PE_THRESHOLD, 0);
1193
1194 /* RX/TX FIFO threshold for partial full (64-bit entries) */
1195 nb8800_writeb(priv, NB8800_PF_THRESHOLD, 255);
1196
1197 /* Buffer size for transmit (64-bit entries) */
1198 nb8800_writeb(priv, NB8800_TX_BUFSIZE, 64);
1199
1200 /* Configure tx DMA */
1201
1202 val = nb8800_readl(priv, NB8800_TXC_CR);
1203 val &= TCR_LE; /* keep endian setting */
1204 val |= TCR_DM; /* DMA descriptor mode */
1205 val |= TCR_RS; /* automatically store tx status */
1206 val |= TCR_DIE; /* interrupt on DMA chain completion */
1207 val |= TCR_TFI(7); /* interrupt after 7 frames transmitted */
1208 val |= TCR_BTS(2); /* 32-byte bus transaction size */
1209 nb8800_writel(priv, NB8800_TXC_CR, val);
1210
1211 /* TX complete interrupt after 10 ms or 7 frames (see above) */
1212 val = clk_get_rate(priv->clk) / 100;
1213 nb8800_writel(priv, NB8800_TX_ITR, val);
1214
1215 /* Configure rx DMA */
1216
1217 val = nb8800_readl(priv, NB8800_RXC_CR);
1218 val &= RCR_LE; /* keep endian setting */
1219 val |= RCR_DM; /* DMA descriptor mode */
1220 val |= RCR_RS; /* automatically store rx status */
1221 val |= RCR_DIE; /* interrupt at end of DMA chain */
1222 val |= RCR_RFI(7); /* interrupt after 7 frames received */
1223 val |= RCR_BTS(2); /* 32-byte bus transaction size */
1224 nb8800_writel(priv, NB8800_RXC_CR, val);
1225
1226 /* The rx interrupt can fire before the DMA has completed
1227 * unless a small delay is added. 50 us is hopefully enough.
1228 */
1229 priv->rx_itr_irq = clk_get_rate(priv->clk) / 20000;
1230
1231 /* In NAPI poll mode we want to disable interrupts, but the
1232 * hardware does not permit this. Delay 10 ms instead.
1233 */
1234 priv->rx_itr_poll = clk_get_rate(priv->clk) / 100;
1235
1236 nb8800_writel(priv, NB8800_RX_ITR, priv->rx_itr_irq);
1237
1238 priv->rx_dma_config = RX_BUF_SIZE | DESC_BTS(2) | DESC_DS | DESC_EOF;
1239
1240 /* Flow control settings */
1241
1242 /* Pause time of 0.1 ms */
1243 val = 100000 / 512;
1244 nb8800_writeb(priv, NB8800_PQ1, val >> 8);
1245 nb8800_writeb(priv, NB8800_PQ2, val & 0xff);
1246
1247 /* Auto-negotiate by default */
1248 priv->pause_aneg = true;
1249 priv->pause_rx = true;
1250 priv->pause_tx = true;
1251
1252 nb8800_mc_init(dev, 0);
1253
1254 return 0;
1255 }
1256
nb8800_tangox_init(struct net_device * dev)1257 static int nb8800_tangox_init(struct net_device *dev)
1258 {
1259 struct nb8800_priv *priv = netdev_priv(dev);
1260 u32 pad_mode = PAD_MODE_MII;
1261
1262 switch (priv->phy_mode) {
1263 case PHY_INTERFACE_MODE_MII:
1264 case PHY_INTERFACE_MODE_GMII:
1265 pad_mode = PAD_MODE_MII;
1266 break;
1267
1268 case PHY_INTERFACE_MODE_RGMII:
1269 case PHY_INTERFACE_MODE_RGMII_ID:
1270 case PHY_INTERFACE_MODE_RGMII_RXID:
1271 case PHY_INTERFACE_MODE_RGMII_TXID:
1272 pad_mode = PAD_MODE_RGMII;
1273 break;
1274
1275 default:
1276 dev_err(dev->dev.parent, "unsupported phy mode %s\n",
1277 phy_modes(priv->phy_mode));
1278 return -EINVAL;
1279 }
1280
1281 nb8800_writeb(priv, NB8800_TANGOX_PAD_MODE, pad_mode);
1282
1283 return 0;
1284 }
1285
nb8800_tangox_reset(struct net_device * dev)1286 static int nb8800_tangox_reset(struct net_device *dev)
1287 {
1288 struct nb8800_priv *priv = netdev_priv(dev);
1289 int clk_div;
1290
1291 nb8800_writeb(priv, NB8800_TANGOX_RESET, 0);
1292 usleep_range(1000, 10000);
1293 nb8800_writeb(priv, NB8800_TANGOX_RESET, 1);
1294
1295 wmb(); /* ensure reset is cleared before proceeding */
1296
1297 clk_div = DIV_ROUND_UP(clk_get_rate(priv->clk), 2 * MAX_MDC_CLOCK);
1298 nb8800_writew(priv, NB8800_TANGOX_MDIO_CLKDIV, clk_div);
1299
1300 return 0;
1301 }
1302
1303 static const struct nb8800_ops nb8800_tangox_ops = {
1304 .init = nb8800_tangox_init,
1305 .reset = nb8800_tangox_reset,
1306 };
1307
nb8800_tango4_init(struct net_device * dev)1308 static int nb8800_tango4_init(struct net_device *dev)
1309 {
1310 struct nb8800_priv *priv = netdev_priv(dev);
1311 int err;
1312
1313 err = nb8800_tangox_init(dev);
1314 if (err)
1315 return err;
1316
1317 /* On tango4 interrupt on DMA completion per frame works and gives
1318 * better performance despite generating more rx interrupts.
1319 */
1320
1321 /* Disable unnecessary interrupt on rx completion */
1322 nb8800_clearl(priv, NB8800_RXC_CR, RCR_RFI(7));
1323
1324 /* Request interrupt on descriptor DMA completion */
1325 priv->rx_dma_config |= DESC_ID;
1326
1327 return 0;
1328 }
1329
1330 static const struct nb8800_ops nb8800_tango4_ops = {
1331 .init = nb8800_tango4_init,
1332 .reset = nb8800_tangox_reset,
1333 };
1334
1335 static const struct of_device_id nb8800_dt_ids[] = {
1336 {
1337 .compatible = "aurora,nb8800",
1338 },
1339 {
1340 .compatible = "sigma,smp8642-ethernet",
1341 .data = &nb8800_tangox_ops,
1342 },
1343 {
1344 .compatible = "sigma,smp8734-ethernet",
1345 .data = &nb8800_tango4_ops,
1346 },
1347 { }
1348 };
1349 MODULE_DEVICE_TABLE(of, nb8800_dt_ids);
1350
nb8800_probe(struct platform_device * pdev)1351 static int nb8800_probe(struct platform_device *pdev)
1352 {
1353 const struct of_device_id *match;
1354 const struct nb8800_ops *ops = NULL;
1355 struct nb8800_priv *priv;
1356 struct resource *res;
1357 struct net_device *dev;
1358 struct mii_bus *bus;
1359 const unsigned char *mac;
1360 void __iomem *base;
1361 int irq;
1362 int ret;
1363
1364 match = of_match_device(nb8800_dt_ids, &pdev->dev);
1365 if (match)
1366 ops = match->data;
1367
1368 irq = platform_get_irq(pdev, 0);
1369 if (irq <= 0) {
1370 dev_err(&pdev->dev, "No IRQ\n");
1371 return -EINVAL;
1372 }
1373
1374 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1375 base = devm_ioremap_resource(&pdev->dev, res);
1376 if (IS_ERR(base))
1377 return PTR_ERR(base);
1378
1379 dev_dbg(&pdev->dev, "AU-NB8800 Ethernet at %pa\n", &res->start);
1380
1381 dev = alloc_etherdev(sizeof(*priv));
1382 if (!dev)
1383 return -ENOMEM;
1384
1385 platform_set_drvdata(pdev, dev);
1386 SET_NETDEV_DEV(dev, &pdev->dev);
1387
1388 priv = netdev_priv(dev);
1389 priv->base = base;
1390
1391 priv->phy_mode = of_get_phy_mode(pdev->dev.of_node);
1392 if (priv->phy_mode < 0)
1393 priv->phy_mode = PHY_INTERFACE_MODE_RGMII;
1394
1395 priv->clk = devm_clk_get(&pdev->dev, NULL);
1396 if (IS_ERR(priv->clk)) {
1397 dev_err(&pdev->dev, "failed to get clock\n");
1398 ret = PTR_ERR(priv->clk);
1399 goto err_free_dev;
1400 }
1401
1402 ret = clk_prepare_enable(priv->clk);
1403 if (ret)
1404 goto err_free_dev;
1405
1406 spin_lock_init(&priv->tx_lock);
1407
1408 if (ops && ops->reset) {
1409 ret = ops->reset(dev);
1410 if (ret)
1411 goto err_disable_clk;
1412 }
1413
1414 bus = devm_mdiobus_alloc(&pdev->dev);
1415 if (!bus) {
1416 ret = -ENOMEM;
1417 goto err_disable_clk;
1418 }
1419
1420 bus->name = "nb8800-mii";
1421 bus->read = nb8800_mdio_read;
1422 bus->write = nb8800_mdio_write;
1423 bus->parent = &pdev->dev;
1424 snprintf(bus->id, MII_BUS_ID_SIZE, "%lx.nb8800-mii",
1425 (unsigned long)res->start);
1426 bus->priv = priv;
1427
1428 ret = of_mdiobus_register(bus, pdev->dev.of_node);
1429 if (ret) {
1430 dev_err(&pdev->dev, "failed to register MII bus\n");
1431 goto err_disable_clk;
1432 }
1433
1434 if (of_phy_is_fixed_link(pdev->dev.of_node)) {
1435 ret = of_phy_register_fixed_link(pdev->dev.of_node);
1436 if (ret < 0) {
1437 dev_err(&pdev->dev, "bad fixed-link spec\n");
1438 goto err_free_bus;
1439 }
1440 priv->phy_node = of_node_get(pdev->dev.of_node);
1441 }
1442
1443 if (!priv->phy_node)
1444 priv->phy_node = of_parse_phandle(pdev->dev.of_node,
1445 "phy-handle", 0);
1446
1447 if (!priv->phy_node) {
1448 dev_err(&pdev->dev, "no PHY specified\n");
1449 ret = -ENODEV;
1450 goto err_free_bus;
1451 }
1452
1453 priv->mii_bus = bus;
1454
1455 ret = nb8800_hw_init(dev);
1456 if (ret)
1457 goto err_deregister_fixed_link;
1458
1459 if (ops && ops->init) {
1460 ret = ops->init(dev);
1461 if (ret)
1462 goto err_deregister_fixed_link;
1463 }
1464
1465 dev->netdev_ops = &nb8800_netdev_ops;
1466 dev->ethtool_ops = &nb8800_ethtool_ops;
1467 dev->flags |= IFF_MULTICAST;
1468 dev->irq = irq;
1469
1470 mac = of_get_mac_address(pdev->dev.of_node);
1471 if (mac)
1472 ether_addr_copy(dev->dev_addr, mac);
1473
1474 if (!is_valid_ether_addr(dev->dev_addr))
1475 eth_hw_addr_random(dev);
1476
1477 nb8800_update_mac_addr(dev);
1478
1479 netif_carrier_off(dev);
1480
1481 ret = register_netdev(dev);
1482 if (ret) {
1483 netdev_err(dev, "failed to register netdev\n");
1484 goto err_free_dma;
1485 }
1486
1487 netif_napi_add(dev, &priv->napi, nb8800_poll, NAPI_POLL_WEIGHT);
1488
1489 netdev_info(dev, "MAC address %pM\n", dev->dev_addr);
1490
1491 return 0;
1492
1493 err_free_dma:
1494 nb8800_dma_free(dev);
1495 err_deregister_fixed_link:
1496 if (of_phy_is_fixed_link(pdev->dev.of_node))
1497 of_phy_deregister_fixed_link(pdev->dev.of_node);
1498 err_free_bus:
1499 of_node_put(priv->phy_node);
1500 mdiobus_unregister(bus);
1501 err_disable_clk:
1502 clk_disable_unprepare(priv->clk);
1503 err_free_dev:
1504 free_netdev(dev);
1505
1506 return ret;
1507 }
1508
nb8800_remove(struct platform_device * pdev)1509 static int nb8800_remove(struct platform_device *pdev)
1510 {
1511 struct net_device *ndev = platform_get_drvdata(pdev);
1512 struct nb8800_priv *priv = netdev_priv(ndev);
1513
1514 unregister_netdev(ndev);
1515 if (of_phy_is_fixed_link(pdev->dev.of_node))
1516 of_phy_deregister_fixed_link(pdev->dev.of_node);
1517 of_node_put(priv->phy_node);
1518
1519 mdiobus_unregister(priv->mii_bus);
1520
1521 clk_disable_unprepare(priv->clk);
1522
1523 nb8800_dma_free(ndev);
1524 free_netdev(ndev);
1525
1526 return 0;
1527 }
1528
1529 static struct platform_driver nb8800_driver = {
1530 .driver = {
1531 .name = "nb8800",
1532 .of_match_table = nb8800_dt_ids,
1533 },
1534 .probe = nb8800_probe,
1535 .remove = nb8800_remove,
1536 };
1537
1538 module_platform_driver(nb8800_driver);
1539
1540 MODULE_DESCRIPTION("Aurora AU-NB8800 Ethernet driver");
1541 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
1542 MODULE_LICENSE("GPL");
1543