ramips: raeth: use 're' instead of 'priv' everywhere
[openwrt.git] / target / linux / ramips / files / drivers / net / ramips.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 *
15 * Copyright (C) 2009 John Crispin <blogic@openwrt.org>
16 */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/platform_device.h>
27 #include <linux/phy.h>
28
29 #include <ramips_eth_platform.h>
30 #include "ramips_eth.h"
31
32 #define TX_TIMEOUT (20 * HZ / 100)
33 #define MAX_RX_LENGTH 1600
34
35 #ifdef CONFIG_RALINK_RT305X
36 #include "ramips_esw.c"
37 #else
38 static inline int rt305x_esw_init(void) { return 0; }
39 static inline void rt305x_esw_exit(void) { }
40 #endif
41
42 #define phys_to_bus(a) (a & 0x1FFFFFFF)
43
44 #ifdef CONFIG_RAMIPS_ETH_DEBUG
45 #define RADEBUG(fmt, args...) printk(KERN_DEBUG fmt, ## args)
46 #else
47 #define RADEBUG(fmt, args...) do {} while (0)
48 #endif
49
50 static struct net_device * ramips_dev;
51 static void __iomem *ramips_fe_base = 0;
52
53 static inline void
54 ramips_fe_wr(u32 val, unsigned reg)
55 {
56 __raw_writel(val, ramips_fe_base + reg);
57 }
58
59 static inline u32
60 ramips_fe_rr(unsigned reg)
61 {
62 return __raw_readl(ramips_fe_base + reg);
63 }
64
65 static inline void
66 ramips_fe_int_disable(u32 mask)
67 {
68 ramips_fe_wr(ramips_fe_rr(RAMIPS_FE_INT_ENABLE) & ~mask,
69 RAMIPS_FE_INT_ENABLE);
70 /* flush write */
71 ramips_fe_rr(RAMIPS_FE_INT_ENABLE);
72 }
73
74 static inline void
75 ramips_fe_int_enable(u32 mask)
76 {
77 ramips_fe_wr(ramips_fe_rr(RAMIPS_FE_INT_ENABLE) | mask,
78 RAMIPS_FE_INT_ENABLE);
79 /* flush write */
80 ramips_fe_rr(RAMIPS_FE_INT_ENABLE);
81 }
82
83 static inline void
84 ramips_hw_set_macaddr(unsigned char *mac)
85 {
86 ramips_fe_wr((mac[0] << 8) | mac[1], RAMIPS_GDMA1_MAC_ADRH);
87 ramips_fe_wr((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
88 RAMIPS_GDMA1_MAC_ADRL);
89 }
90
91 #if defined(CONFIG_RALINK_RT288X) || defined(CONFIG_RALINK_RT3883)
92
93 #define RAMIPS_MDIO_RETRY 1000
94
95 static unsigned char *ramips_speed_str(struct raeth_priv *re)
96 {
97 switch (re->speed) {
98 case SPEED_1000:
99 return "1000";
100 case SPEED_100:
101 return "100";
102 case SPEED_10:
103 return "10";
104 }
105
106 return "?";
107 }
108
109 static void ramips_link_adjust(struct raeth_priv *re)
110 {
111 struct ramips_eth_platform_data *pdata;
112 u32 mdio_cfg;
113
114 pdata = re->parent->platform_data;
115 if (!re->link) {
116 netif_carrier_off(re->netdev);
117 netdev_info(re->netdev, "link down\n");
118 return;
119 }
120
121 mdio_cfg = RAMIPS_MDIO_CFG_TX_CLK_SKEW_200 |
122 RAMIPS_MDIO_CFG_TX_CLK_SKEW_200 |
123 RAMIPS_MDIO_CFG_GP1_FRC_EN;
124
125 if (re->duplex == DUPLEX_FULL)
126 mdio_cfg |= RAMIPS_MDIO_CFG_GP1_DUPLEX;
127
128 if (re->tx_fc)
129 mdio_cfg |= RAMIPS_MDIO_CFG_GP1_FC_TX;
130
131 if (re->rx_fc)
132 mdio_cfg |= RAMIPS_MDIO_CFG_GP1_FC_RX;
133
134 switch (re->speed) {
135 case SPEED_10:
136 mdio_cfg |= RAMIPS_MDIO_CFG_GP1_SPEED_10;
137 break;
138 case SPEED_100:
139 mdio_cfg |= RAMIPS_MDIO_CFG_GP1_SPEED_100;
140 break;
141 case SPEED_1000:
142 mdio_cfg |= RAMIPS_MDIO_CFG_GP1_SPEED_1000;
143 break;
144 default:
145 BUG();
146 }
147
148 ramips_fe_wr(mdio_cfg, RAMIPS_MDIO_CFG);
149
150 netif_carrier_on(re->netdev);
151 netdev_info(re->netdev, "link up (%sMbps/%s duplex)\n",
152 ramips_speed_str(re),
153 (DUPLEX_FULL == re->duplex) ? "Full" : "Half");
154 }
155
156 static int
157 ramips_mdio_wait_ready(struct raeth_priv *re)
158 {
159 int retries;
160
161 retries = RAMIPS_MDIO_RETRY;
162 while (1) {
163 u32 t;
164
165 t = ramips_fe_rr(RAMIPS_MDIO_ACCESS);
166 if ((t & (0x1 << 31)) == 0)
167 return 0;
168
169 if (retries-- == 0)
170 break;
171
172 udelay(1);
173 }
174
175 dev_err(re->parent, "MDIO operation timed out\n");
176 return -ETIMEDOUT;
177 }
178
179 static int
180 ramips_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
181 {
182 struct raeth_priv *re = bus->priv;
183 int err;
184 u32 t;
185
186 err = ramips_mdio_wait_ready(re);
187 if (err)
188 return 0xffff;
189
190 t = (phy_addr << 24) | (phy_reg << 16);
191 ramips_fe_wr(t, RAMIPS_MDIO_ACCESS);
192 t |= (1 << 31);
193 ramips_fe_wr(t, RAMIPS_MDIO_ACCESS);
194
195 err = ramips_mdio_wait_ready(re);
196 if (err)
197 return 0xffff;
198
199 RADEBUG("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
200 phy_addr, phy_reg, ramips_fe_rr(RAMIPS_MDIO_ACCESS) & 0xffff);
201
202 return ramips_fe_rr(RAMIPS_MDIO_ACCESS) & 0xffff;
203 }
204
205 static int
206 ramips_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
207 {
208 struct raeth_priv *re = bus->priv;
209 int err;
210 u32 t;
211
212 RADEBUG("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
213 phy_addr, phy_reg, ramips_fe_rr(RAMIPS_MDIO_ACCESS) & 0xffff);
214
215 err = ramips_mdio_wait_ready(re);
216 if (err)
217 return err;
218
219 t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
220 ramips_fe_wr(t, RAMIPS_MDIO_ACCESS);
221 t |= (1 << 31);
222 ramips_fe_wr(t, RAMIPS_MDIO_ACCESS);
223
224 return ramips_mdio_wait_ready(re);
225 }
226
227 static int
228 ramips_mdio_reset(struct mii_bus *bus)
229 {
230 /* TODO */
231 return 0;
232 }
233
234 static int
235 ramips_mdio_init(struct raeth_priv *re)
236 {
237 int err;
238 int i;
239
240 re->mii_bus = mdiobus_alloc();
241 if (re->mii_bus == NULL)
242 return -ENOMEM;
243
244 re->mii_bus->name = "ramips_mdio";
245 re->mii_bus->read = ramips_mdio_read;
246 re->mii_bus->write = ramips_mdio_write;
247 re->mii_bus->reset = ramips_mdio_reset;
248 re->mii_bus->irq = re->mii_irq;
249 re->mii_bus->priv = re;
250 re->mii_bus->parent = re->parent;
251
252 snprintf(re->mii_bus->id, MII_BUS_ID_SIZE, "%s", "ramips_mdio");
253 re->mii_bus->phy_mask = 0;
254
255 for (i = 0; i < PHY_MAX_ADDR; i++)
256 re->mii_irq[i] = PHY_POLL;
257
258 err = mdiobus_register(re->mii_bus);
259 if (err)
260 goto err_free_bus;
261
262 return 0;
263
264 err_free_bus:
265 kfree(re->mii_bus);
266 return err;
267 }
268
269 static void
270 ramips_mdio_cleanup(struct raeth_priv *re)
271 {
272 mdiobus_unregister(re->mii_bus);
273 kfree(re->mii_bus);
274 }
275
276 static void
277 ramips_phy_link_adjust(struct net_device *dev)
278 {
279 struct raeth_priv *re = netdev_priv(dev);
280 struct phy_device *phydev = re->phy_dev;
281 unsigned long flags;
282 int status_change = 0;
283
284 spin_lock_irqsave(&re->phy_lock, flags);
285
286 if (phydev->link)
287 if (re->duplex != phydev->duplex ||
288 re->speed != phydev->speed)
289 status_change = 1;
290
291 if (phydev->link != re->link)
292 status_change = 1;
293
294 re->link = phydev->link;
295 re->duplex = phydev->duplex;
296 re->speed = phydev->speed;
297
298 if (status_change)
299 ramips_link_adjust(re);
300
301 spin_unlock_irqrestore(&re->phy_lock, flags);
302 }
303
304 static int
305 ramips_phy_connect_multi(struct raeth_priv *re)
306 {
307 struct net_device *netdev = re->netdev;
308 struct ramips_eth_platform_data *pdata;
309 struct phy_device *phydev = NULL;
310 int phy_addr;
311 int ret = 0;
312
313 pdata = re->parent->platform_data;
314 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
315 if (!(pdata->phy_mask & (1 << phy_addr)))
316 continue;
317
318 if (re->mii_bus->phy_map[phy_addr] == NULL)
319 continue;
320
321 RADEBUG("%s: PHY found at %s, uid=%08x\n",
322 netdev->name,
323 dev_name(&re->mii_bus->phy_map[phy_addr]->dev),
324 re->mii_bus->phy_map[phy_addr]->phy_id);
325
326 if (phydev == NULL)
327 phydev = re->mii_bus->phy_map[phy_addr];
328 }
329
330 if (!phydev) {
331 netdev_err(netdev, "no PHY found with phy_mask=%08x\n",
332 pdata->phy_mask);
333 return -ENODEV;
334 }
335
336 re->phy_dev = phy_connect(netdev, dev_name(&phydev->dev),
337 ramips_phy_link_adjust, 0,
338 pdata->phy_if_mode);
339
340 if (IS_ERR(re->phy_dev)) {
341 netdev_err(netdev, "could not connect to PHY at %s\n",
342 dev_name(&phydev->dev));
343 return PTR_ERR(re->phy_dev);
344 }
345
346 phydev->supported &= PHY_GBIT_FEATURES;
347 phydev->advertising = phydev->supported;
348
349 RADEBUG("%s: connected to PHY at %s [uid=%08x, driver=%s]\n",
350 netdev->name, dev_name(&phydev->dev),
351 phydev->phy_id, phydev->drv->name);
352
353 re->link = 0;
354 re->speed = 0;
355 re->duplex = -1;
356 re->rx_fc = 0;
357 re->tx_fc = 0;
358
359 return ret;
360 }
361
362 static int
363 ramips_phy_connect_fixed(struct raeth_priv *re)
364 {
365 struct ramips_eth_platform_data *pdata;
366
367 pdata = re->parent->platform_data;
368 switch (pdata->speed) {
369 case SPEED_10:
370 case SPEED_100:
371 case SPEED_1000:
372 break;
373 default:
374 netdev_err(re->netdev, "invalid speed specified\n");
375 return -EINVAL;
376 }
377
378 RADEBUG("%s: using fixed link parameters\n", re->netdev->name);
379
380 re->speed = pdata->speed;
381 re->duplex = pdata->duplex;
382 re->tx_fc = pdata->tx_fc;
383 re->rx_fc = pdata->tx_fc;
384
385 return 0;
386 }
387
388 static int
389 ramips_phy_connect(struct raeth_priv *re)
390 {
391 struct ramips_eth_platform_data *pdata;
392
393 pdata = re->parent->platform_data;
394 if (pdata->phy_mask)
395 return ramips_phy_connect_multi(re);
396
397 return ramips_phy_connect_fixed(re);
398 }
399
400 static void
401 ramips_phy_disconnect(struct raeth_priv *re)
402 {
403 if (re->phy_dev)
404 phy_disconnect(re->phy_dev);
405 }
406
407 static void
408 ramips_phy_start(struct raeth_priv *re)
409 {
410 unsigned long flags;
411
412 if (re->phy_dev) {
413 phy_start(re->phy_dev);
414 } else {
415 spin_lock_irqsave(&re->phy_lock, flags);
416 re->link = 1;
417 ramips_link_adjust(re);
418 spin_unlock_irqrestore(&re->phy_lock, flags);
419 }
420 }
421
422 static void
423 ramips_phy_stop(struct raeth_priv *re)
424 {
425 unsigned long flags;
426
427 if (re->phy_dev)
428 phy_stop(re->phy_dev);
429
430 spin_lock_irqsave(&re->phy_lock, flags);
431 re->link = 0;
432 ramips_link_adjust(re);
433 spin_unlock_irqrestore(&re->phy_lock, flags);
434 }
435 #else
436 static inline int
437 ramips_mdio_init(struct raeth_priv *re)
438 {
439 return 0;
440 }
441
442 static inline void
443 ramips_mdio_cleanup(struct raeth_priv *re)
444 {
445 }
446
447 static inline int
448 ramips_phy_connect(struct raeth_priv *re)
449 {
450 return 0;
451 }
452
453 static inline void
454 ramips_phy_disconnect(struct raeth_priv *re)
455 {
456 }
457
458 static inline void
459 ramips_phy_start(struct raeth_priv *re)
460 {
461 }
462
463 static inline void
464 ramips_phy_stop(struct raeth_priv *re)
465 {
466 }
467 #endif /* CONFIG_RALINK_RT288X || CONFIG_RALINK_RT3883 */
468
469 static void
470 ramips_cleanup_dma(struct raeth_priv *re)
471 {
472 int i;
473
474 for (i = 0; i < NUM_RX_DESC; i++)
475 if (re->rx_skb[i]) {
476 dma_unmap_single(&re->netdev->dev, re->rx_dma[i],
477 MAX_RX_LENGTH, DMA_FROM_DEVICE);
478 dev_kfree_skb_any(re->rx_skb[i]);
479 }
480
481 if (re->rx)
482 dma_free_coherent(&re->netdev->dev,
483 NUM_RX_DESC * sizeof(struct ramips_rx_dma),
484 re->rx, re->rx_desc_dma);
485
486 if (re->tx)
487 dma_free_coherent(&re->netdev->dev,
488 NUM_TX_DESC * sizeof(struct ramips_tx_dma),
489 re->tx, re->tx_desc_dma);
490 }
491
492 static int
493 ramips_alloc_dma(struct raeth_priv *re)
494 {
495 int err = -ENOMEM;
496 int i;
497
498 re->skb_free_idx = 0;
499
500 /* setup tx ring */
501 re->tx = dma_alloc_coherent(&re->netdev->dev,
502 NUM_TX_DESC * sizeof(struct ramips_tx_dma),
503 &re->tx_desc_dma, GFP_ATOMIC);
504 if (!re->tx)
505 goto err_cleanup;
506
507 memset(re->tx, 0, NUM_TX_DESC * sizeof(struct ramips_tx_dma));
508 for (i = 0; i < NUM_TX_DESC; i++) {
509 re->tx[i].txd2 = TX_DMA_LSO | TX_DMA_DONE;
510 re->tx[i].txd4 = TX_DMA_QN(3) | TX_DMA_PN(1);
511 }
512
513 /* setup rx ring */
514 re->rx = dma_alloc_coherent(&re->netdev->dev,
515 NUM_RX_DESC * sizeof(struct ramips_rx_dma),
516 &re->rx_desc_dma, GFP_ATOMIC);
517 if (!re->rx)
518 goto err_cleanup;
519
520 memset(re->rx, 0, sizeof(struct ramips_rx_dma) * NUM_RX_DESC);
521 for (i = 0; i < NUM_RX_DESC; i++) {
522 dma_addr_t dma_addr;
523 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_LENGTH +
524 NET_IP_ALIGN);
525
526 if (!new_skb)
527 goto err_cleanup;
528
529 skb_reserve(new_skb, NET_IP_ALIGN);
530
531 dma_addr = dma_map_single(&re->netdev->dev, new_skb->data,
532 MAX_RX_LENGTH, DMA_FROM_DEVICE);
533 re->rx_dma[i] = dma_addr;
534 re->rx[i].rxd1 = (unsigned int) re->rx_dma[i];
535 re->rx[i].rxd2 |= RX_DMA_LSO;
536 re->rx_skb[i] = new_skb;
537 }
538
539 return 0;
540
541 err_cleanup:
542 ramips_cleanup_dma(re);
543 return err;
544 }
545
546 static void
547 ramips_setup_dma(struct raeth_priv *re)
548 {
549 ramips_fe_wr(re->tx_desc_dma, RAMIPS_TX_BASE_PTR0);
550 ramips_fe_wr(NUM_TX_DESC, RAMIPS_TX_MAX_CNT0);
551 ramips_fe_wr(0, RAMIPS_TX_CTX_IDX0);
552 ramips_fe_wr(RAMIPS_PST_DTX_IDX0, RAMIPS_PDMA_RST_CFG);
553
554 ramips_fe_wr(re->rx_desc_dma, RAMIPS_RX_BASE_PTR0);
555 ramips_fe_wr(NUM_RX_DESC, RAMIPS_RX_MAX_CNT0);
556 ramips_fe_wr((NUM_RX_DESC - 1), RAMIPS_RX_CALC_IDX0);
557 ramips_fe_wr(RAMIPS_PST_DRX_IDX0, RAMIPS_PDMA_RST_CFG);
558 }
559
560 static int
561 ramips_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
562 {
563 struct raeth_priv *re = netdev_priv(dev);
564 unsigned long tx;
565 unsigned int tx_next;
566 dma_addr_t mapped_addr;
567
568 if (re->plat->min_pkt_len) {
569 if (skb->len < re->plat->min_pkt_len) {
570 if (skb_padto(skb, re->plat->min_pkt_len)) {
571 printk(KERN_ERR
572 "ramips_eth: skb_padto failed\n");
573 kfree_skb(skb);
574 return 0;
575 }
576 skb_put(skb, re->plat->min_pkt_len - skb->len);
577 }
578 }
579
580 dev->trans_start = jiffies;
581 mapped_addr = dma_map_single(&re->netdev->dev, skb->data, skb->len,
582 DMA_TO_DEVICE);
583
584 spin_lock(&re->page_lock);
585 tx = ramips_fe_rr(RAMIPS_TX_CTX_IDX0);
586 tx_next = (tx + 1) % NUM_TX_DESC;
587
588 if ((re->tx_skb[tx]) || (re->tx_skb[tx_next]) ||
589 !(re->tx[tx].txd2 & TX_DMA_DONE) ||
590 !(re->tx[tx_next].txd2 & TX_DMA_DONE))
591 goto out;
592
593 re->tx[tx].txd1 = (unsigned int) mapped_addr;
594 re->tx[tx].txd2 &= ~(TX_DMA_PLEN0_MASK | TX_DMA_DONE);
595 re->tx[tx].txd2 |= TX_DMA_PLEN0(skb->len);
596 dev->stats.tx_packets++;
597 dev->stats.tx_bytes += skb->len;
598 re->tx_skb[tx] = skb;
599 wmb();
600 ramips_fe_wr(tx_next, RAMIPS_TX_CTX_IDX0);
601 spin_unlock(&re->page_lock);
602 return NETDEV_TX_OK;
603
604 out:
605 spin_unlock(&re->page_lock);
606 dev->stats.tx_dropped++;
607 kfree_skb(skb);
608 return NETDEV_TX_OK;
609 }
610
611 static void
612 ramips_eth_rx_hw(unsigned long ptr)
613 {
614 struct net_device *dev = (struct net_device *) ptr;
615 struct raeth_priv *re = netdev_priv(dev);
616 int rx;
617 int max_rx = 16;
618
619 while (max_rx) {
620 struct sk_buff *rx_skb, *new_skb;
621 int pktlen;
622
623 rx = (ramips_fe_rr(RAMIPS_RX_CALC_IDX0) + 1) % NUM_RX_DESC;
624 if (!(re->rx[rx].rxd2 & RX_DMA_DONE))
625 break;
626 max_rx--;
627
628 rx_skb = re->rx_skb[rx];
629 pktlen = RX_DMA_PLEN0(re->rx[rx].rxd2);
630
631 new_skb = netdev_alloc_skb(dev, MAX_RX_LENGTH + NET_IP_ALIGN);
632 /* Reuse the buffer on allocation failures */
633 if (new_skb) {
634 dma_addr_t dma_addr;
635
636 dma_unmap_single(&re->netdev->dev, re->rx_dma[rx],
637 MAX_RX_LENGTH, DMA_FROM_DEVICE);
638
639 skb_put(rx_skb, pktlen);
640 rx_skb->dev = dev;
641 rx_skb->protocol = eth_type_trans(rx_skb, dev);
642 rx_skb->ip_summed = CHECKSUM_NONE;
643 dev->stats.rx_packets++;
644 dev->stats.rx_bytes += pktlen;
645 netif_rx(rx_skb);
646
647 re->rx_skb[rx] = new_skb;
648 skb_reserve(new_skb, NET_IP_ALIGN);
649
650 dma_addr = dma_map_single(&re->netdev->dev,
651 new_skb->data,
652 MAX_RX_LENGTH,
653 DMA_FROM_DEVICE);
654 re->rx_dma[rx] = dma_addr;
655 re->rx[rx].rxd1 = (unsigned int) dma_addr;
656 } else {
657 dev->stats.rx_dropped++;
658 }
659
660 re->rx[rx].rxd2 &= ~RX_DMA_DONE;
661 wmb();
662 ramips_fe_wr(rx, RAMIPS_RX_CALC_IDX0);
663 }
664
665 if (max_rx == 0)
666 tasklet_schedule(&re->rx_tasklet);
667 else
668 ramips_fe_int_enable(RAMIPS_RX_DLY_INT);
669 }
670
671 static void
672 ramips_eth_tx_housekeeping(unsigned long ptr)
673 {
674 struct net_device *dev = (struct net_device*)ptr;
675 struct raeth_priv *re = netdev_priv(dev);
676
677 spin_lock(&re->page_lock);
678 while ((re->tx[re->skb_free_idx].txd2 & TX_DMA_DONE) &&
679 (re->tx_skb[re->skb_free_idx])) {
680 dev_kfree_skb_irq(re->tx_skb[re->skb_free_idx]);
681 re->tx_skb[re->skb_free_idx] = 0;
682 re->skb_free_idx++;
683 if (re->skb_free_idx >= NUM_TX_DESC)
684 re->skb_free_idx = 0;
685 }
686 spin_unlock(&re->page_lock);
687
688 ramips_fe_int_enable(RAMIPS_TX_DLY_INT);
689 }
690
691 static void
692 ramips_eth_timeout(struct net_device *dev)
693 {
694 struct raeth_priv *re = netdev_priv(dev);
695
696 tasklet_schedule(&re->tx_housekeeping_tasklet);
697 }
698
699 static irqreturn_t
700 ramips_eth_irq(int irq, void *dev)
701 {
702 struct raeth_priv *re = netdev_priv(dev);
703 unsigned long fe_int = ramips_fe_rr(RAMIPS_FE_INT_STATUS);
704
705 ramips_fe_wr(0xFFFFFFFF, RAMIPS_FE_INT_STATUS);
706
707 if (fe_int & RAMIPS_RX_DLY_INT) {
708 ramips_fe_int_disable(RAMIPS_RX_DLY_INT);
709 tasklet_schedule(&re->rx_tasklet);
710 }
711
712 if (fe_int & RAMIPS_TX_DLY_INT) {
713 ramips_fe_int_disable(RAMIPS_TX_DLY_INT);
714 tasklet_schedule(&re->tx_housekeeping_tasklet);
715 }
716
717 return IRQ_HANDLED;
718 }
719
720 static int
721 ramips_eth_open(struct net_device *dev)
722 {
723 struct raeth_priv *re = netdev_priv(dev);
724 int err;
725
726 err = request_irq(dev->irq, ramips_eth_irq, IRQF_DISABLED,
727 dev->name, dev);
728 if (err)
729 return err;
730
731 err = ramips_alloc_dma(re);
732 if (err)
733 goto err_free_irq;
734
735 ramips_hw_set_macaddr(dev->dev_addr);
736
737 ramips_setup_dma(re);
738 ramips_fe_wr((ramips_fe_rr(RAMIPS_PDMA_GLO_CFG) & 0xff) |
739 (RAMIPS_TX_WB_DDONE | RAMIPS_RX_DMA_EN |
740 RAMIPS_TX_DMA_EN | RAMIPS_PDMA_SIZE_4DWORDS),
741 RAMIPS_PDMA_GLO_CFG);
742 ramips_fe_wr((ramips_fe_rr(RAMIPS_FE_GLO_CFG) &
743 ~(RAMIPS_US_CYC_CNT_MASK << RAMIPS_US_CYC_CNT_SHIFT)) |
744 ((re->plat->sys_freq / RAMIPS_US_CYC_CNT_DIVISOR) << RAMIPS_US_CYC_CNT_SHIFT),
745 RAMIPS_FE_GLO_CFG);
746
747 tasklet_init(&re->tx_housekeeping_tasklet, ramips_eth_tx_housekeeping,
748 (unsigned long)dev);
749 tasklet_init(&re->rx_tasklet, ramips_eth_rx_hw, (unsigned long)dev);
750
751 ramips_phy_start(re);
752
753 ramips_fe_wr(RAMIPS_DELAY_INIT, RAMIPS_DLY_INT_CFG);
754 ramips_fe_wr(RAMIPS_TX_DLY_INT | RAMIPS_RX_DLY_INT, RAMIPS_FE_INT_ENABLE);
755 ramips_fe_wr(ramips_fe_rr(RAMIPS_GDMA1_FWD_CFG) &
756 ~(RAMIPS_GDM1_ICS_EN | RAMIPS_GDM1_TCS_EN | RAMIPS_GDM1_UCS_EN | 0xffff),
757 RAMIPS_GDMA1_FWD_CFG);
758 ramips_fe_wr(ramips_fe_rr(RAMIPS_CDMA_CSG_CFG) &
759 ~(RAMIPS_ICS_GEN_EN | RAMIPS_TCS_GEN_EN | RAMIPS_UCS_GEN_EN),
760 RAMIPS_CDMA_CSG_CFG);
761 ramips_fe_wr(RAMIPS_PSE_FQFC_CFG_INIT, RAMIPS_PSE_FQ_CFG);
762 ramips_fe_wr(1, RAMIPS_FE_RST_GL);
763 ramips_fe_wr(0, RAMIPS_FE_RST_GL);
764
765 netif_start_queue(dev);
766 return 0;
767
768 err_free_irq:
769 free_irq(dev->irq, dev);
770 return err;
771 }
772
773 static int
774 ramips_eth_stop(struct net_device *dev)
775 {
776 struct raeth_priv *re = netdev_priv(dev);
777
778 ramips_fe_wr(ramips_fe_rr(RAMIPS_PDMA_GLO_CFG) &
779 ~(RAMIPS_TX_WB_DDONE | RAMIPS_RX_DMA_EN | RAMIPS_TX_DMA_EN),
780 RAMIPS_PDMA_GLO_CFG);
781
782 /* disable all interrupts in the hw */
783 ramips_fe_wr(0, RAMIPS_FE_INT_ENABLE);
784
785 ramips_phy_stop(re);
786 free_irq(dev->irq, dev);
787 netif_stop_queue(dev);
788 tasklet_kill(&re->tx_housekeeping_tasklet);
789 tasklet_kill(&re->rx_tasklet);
790 ramips_cleanup_dma(re);
791 RADEBUG("ramips_eth: stopped\n");
792 return 0;
793 }
794
795 static int __init
796 ramips_eth_probe(struct net_device *dev)
797 {
798 struct raeth_priv *re = netdev_priv(dev);
799 int err;
800
801 BUG_ON(!re->plat->reset_fe);
802 re->plat->reset_fe();
803 net_srandom(jiffies);
804 memcpy(dev->dev_addr, re->plat->mac, ETH_ALEN);
805
806 ether_setup(dev);
807 dev->mtu = 1500;
808 dev->watchdog_timeo = TX_TIMEOUT;
809 spin_lock_init(&re->page_lock);
810 spin_lock_init(&re->phy_lock);
811
812 err = ramips_mdio_init(re);
813 if (err)
814 return err;
815
816 err = ramips_phy_connect(re);
817 if (err)
818 goto err_mdio_cleanup;
819
820 return 0;
821
822 err_mdio_cleanup:
823 ramips_mdio_cleanup(re);
824 return err;
825 }
826
827 static void
828 ramips_eth_uninit(struct net_device *dev)
829 {
830 struct raeth_priv *re = netdev_priv(dev);
831
832 ramips_phy_disconnect(re);
833 ramips_mdio_cleanup(re);
834 }
835
836 static const struct net_device_ops ramips_eth_netdev_ops = {
837 .ndo_init = ramips_eth_probe,
838 .ndo_uninit = ramips_eth_uninit,
839 .ndo_open = ramips_eth_open,
840 .ndo_stop = ramips_eth_stop,
841 .ndo_start_xmit = ramips_eth_hard_start_xmit,
842 .ndo_tx_timeout = ramips_eth_timeout,
843 .ndo_change_mtu = eth_change_mtu,
844 .ndo_set_mac_address = eth_mac_addr,
845 .ndo_validate_addr = eth_validate_addr,
846 };
847
848 static int
849 ramips_eth_plat_probe(struct platform_device *plat)
850 {
851 struct raeth_priv *re;
852 struct ramips_eth_platform_data *data = plat->dev.platform_data;
853 struct resource *res;
854 int err;
855
856 if (!data) {
857 dev_err(&plat->dev, "no platform data specified\n");
858 return -EINVAL;
859 }
860
861 res = platform_get_resource(plat, IORESOURCE_MEM, 0);
862 if (!res) {
863 dev_err(&plat->dev, "no memory resource found\n");
864 return -ENXIO;
865 }
866
867 ramips_fe_base = ioremap_nocache(res->start, res->end - res->start + 1);
868 if (!ramips_fe_base)
869 return -ENOMEM;
870
871 ramips_dev = alloc_etherdev(sizeof(struct raeth_priv));
872 if (!ramips_dev) {
873 dev_err(&plat->dev, "alloc_etherdev failed\n");
874 err = -ENOMEM;
875 goto err_unmap;
876 }
877
878 strcpy(ramips_dev->name, "eth%d");
879 ramips_dev->irq = platform_get_irq(plat, 0);
880 if (ramips_dev->irq < 0) {
881 dev_err(&plat->dev, "no IRQ resource found\n");
882 err = -ENXIO;
883 goto err_free_dev;
884 }
885 ramips_dev->addr_len = ETH_ALEN;
886 ramips_dev->base_addr = (unsigned long)ramips_fe_base;
887 ramips_dev->netdev_ops = &ramips_eth_netdev_ops;
888
889 re = netdev_priv(ramips_dev);
890
891 re->netdev = ramips_dev;
892 re->parent = &plat->dev;
893 re->speed = data->speed;
894 re->duplex = data->duplex;
895 re->rx_fc = data->rx_fc;
896 re->tx_fc = data->tx_fc;
897 re->plat = data;
898
899 err = register_netdev(ramips_dev);
900 if (err) {
901 dev_err(&plat->dev, "error bringing up device\n");
902 goto err_free_dev;
903 }
904
905 RADEBUG("ramips_eth: loaded\n");
906 return 0;
907
908 err_free_dev:
909 kfree(ramips_dev);
910 err_unmap:
911 iounmap(ramips_fe_base);
912 return err;
913 }
914
915 static int
916 ramips_eth_plat_remove(struct platform_device *plat)
917 {
918 unregister_netdev(ramips_dev);
919 free_netdev(ramips_dev);
920 RADEBUG("ramips_eth: unloaded\n");
921 return 0;
922 }
923
924 static struct platform_driver ramips_eth_driver = {
925 .probe = ramips_eth_plat_probe,
926 .remove = ramips_eth_plat_remove,
927 .driver = {
928 .name = "ramips_eth",
929 .owner = THIS_MODULE,
930 },
931 };
932
933 static int __init
934 ramips_eth_init(void)
935 {
936 int ret;
937
938 ret = rt305x_esw_init();
939 if (ret)
940 return ret;
941
942 ret = platform_driver_register(&ramips_eth_driver);
943 if (ret) {
944 printk(KERN_ERR
945 "ramips_eth: Error registering platfom driver!\n");
946 goto esw_cleanup;
947 }
948
949 return 0;
950
951 esw_cleanup:
952 rt305x_esw_exit();
953 return ret;
954 }
955
956 static void __exit
957 ramips_eth_cleanup(void)
958 {
959 platform_driver_unregister(&ramips_eth_driver);
960 rt305x_esw_exit();
961 }
962
963 module_init(ramips_eth_init);
964 module_exit(ramips_eth_cleanup);
965
966 MODULE_LICENSE("GPL");
967 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
968 MODULE_DESCRIPTION("ethernet driver for ramips boards");
This page took 0.129212 seconds and 5 git commands to generate.