Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / tundra / tsi108_eth.c
blobf076050c8ad37fe11c4a5bb37a2481c4c7561859
1 /*******************************************************************************
3 Copyright(c) 2006 Tundra Semiconductor Corporation.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *******************************************************************************/
21 /* This driver is based on the driver code originally developed
22 * for the Intel IOC80314 (ForestLake) Gigabit Ethernet by
23 * scott.wood@timesys.com * Copyright (C) 2003 TimeSys Corporation
25 * Currently changes from original version are:
26 * - porting to Tsi108-based platform and kernel 2.6 (kong.lai@tundra.com)
27 * - modifications to handle two ports independently and support for
28 * additional PHY devices (alexandre.bounine@tundra.com)
29 * - Get hardware information from platform device. (tie-fei.zang@freescale.com)
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/interrupt.h>
36 #include <linux/net.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ethtool.h>
40 #include <linux/skbuff.h>
41 #include <linux/spinlock.h>
42 #include <linux/delay.h>
43 #include <linux/crc32.h>
44 #include <linux/mii.h>
45 #include <linux/device.h>
46 #include <linux/pci.h>
47 #include <linux/rtnetlink.h>
48 #include <linux/timer.h>
49 #include <linux/platform_device.h>
50 #include <linux/gfp.h>
52 #include <asm/io.h>
53 #include <asm/tsi108.h>
55 #include "tsi108_eth.h"
57 #define MII_READ_DELAY 10000 /* max link wait time in msec */
59 #define TSI108_RXRING_LEN 256
61 /* NOTE: The driver currently does not support receiving packets
62 * larger than the buffer size, so don't decrease this (unless you
63 * want to add such support).
65 #define TSI108_RXBUF_SIZE 1536
67 #define TSI108_TXRING_LEN 256
69 #define TSI108_TX_INT_FREQ 64
71 /* Check the phy status every half a second. */
72 #define CHECK_PHY_INTERVAL (HZ/2)
74 static int tsi108_init_one(struct platform_device *pdev);
75 static int tsi108_ether_remove(struct platform_device *pdev);
77 struct tsi108_prv_data {
78 void __iomem *regs; /* Base of normal regs */
79 void __iomem *phyregs; /* Base of register bank used for PHY access */
81 struct net_device *dev;
82 struct napi_struct napi;
84 unsigned int phy; /* Index of PHY for this interface */
85 unsigned int irq_num;
86 unsigned int id;
87 unsigned int phy_type;
89 struct timer_list timer;/* Timer that triggers the check phy function */
90 unsigned int rxtail; /* Next entry in rxring to read */
91 unsigned int rxhead; /* Next entry in rxring to give a new buffer */
92 unsigned int rxfree; /* Number of free, allocated RX buffers */
94 unsigned int rxpending; /* Non-zero if there are still descriptors
95 * to be processed from a previous descriptor
96 * interrupt condition that has been cleared */
98 unsigned int txtail; /* Next TX descriptor to check status on */
99 unsigned int txhead; /* Next TX descriptor to use */
101 /* Number of free TX descriptors. This could be calculated from
102 * rxhead and rxtail if one descriptor were left unused to disambiguate
103 * full and empty conditions, but it's simpler to just keep track
104 * explicitly. */
106 unsigned int txfree;
108 unsigned int phy_ok; /* The PHY is currently powered on. */
110 /* PHY status (duplex is 1 for half, 2 for full,
111 * so that the default 0 indicates that neither has
112 * yet been configured). */
114 unsigned int link_up;
115 unsigned int speed;
116 unsigned int duplex;
118 tx_desc *txring;
119 rx_desc *rxring;
120 struct sk_buff *txskbs[TSI108_TXRING_LEN];
121 struct sk_buff *rxskbs[TSI108_RXRING_LEN];
123 dma_addr_t txdma, rxdma;
125 /* txlock nests in misclock and phy_lock */
127 spinlock_t txlock, misclock;
129 /* stats is used to hold the upper bits of each hardware counter,
130 * and tmpstats is used to hold the full values for returning
131 * to the caller of get_stats(). They must be separate in case
132 * an overflow interrupt occurs before the stats are consumed.
135 struct net_device_stats stats;
136 struct net_device_stats tmpstats;
138 /* These stats are kept separate in hardware, thus require individual
139 * fields for handling carry. They are combined in get_stats.
142 unsigned long rx_fcs; /* Add to rx_frame_errors */
143 unsigned long rx_short_fcs; /* Add to rx_frame_errors */
144 unsigned long rx_long_fcs; /* Add to rx_frame_errors */
145 unsigned long rx_underruns; /* Add to rx_length_errors */
146 unsigned long rx_overruns; /* Add to rx_length_errors */
148 unsigned long tx_coll_abort; /* Add to tx_aborted_errors/collisions */
149 unsigned long tx_pause_drop; /* Add to tx_aborted_errors */
151 unsigned long mc_hash[16];
152 u32 msg_enable; /* debug message level */
153 struct mii_if_info mii_if;
154 unsigned int init_media;
156 struct platform_device *pdev;
159 /* Structure for a device driver */
161 static struct platform_driver tsi_eth_driver = {
162 .probe = tsi108_init_one,
163 .remove = tsi108_ether_remove,
164 .driver = {
165 .name = "tsi-ethernet",
169 static void tsi108_timed_checker(struct timer_list *t);
171 #ifdef DEBUG
172 static void dump_eth_one(struct net_device *dev)
174 struct tsi108_prv_data *data = netdev_priv(dev);
176 printk("Dumping %s...\n", dev->name);
177 printk("intstat %x intmask %x phy_ok %d"
178 " link %d speed %d duplex %d\n",
179 TSI_READ(TSI108_EC_INTSTAT),
180 TSI_READ(TSI108_EC_INTMASK), data->phy_ok,
181 data->link_up, data->speed, data->duplex);
183 printk("TX: head %d, tail %d, free %d, stat %x, estat %x, err %x\n",
184 data->txhead, data->txtail, data->txfree,
185 TSI_READ(TSI108_EC_TXSTAT),
186 TSI_READ(TSI108_EC_TXESTAT),
187 TSI_READ(TSI108_EC_TXERR));
189 printk("RX: head %d, tail %d, free %d, stat %x,"
190 " estat %x, err %x, pending %d\n\n",
191 data->rxhead, data->rxtail, data->rxfree,
192 TSI_READ(TSI108_EC_RXSTAT),
193 TSI_READ(TSI108_EC_RXESTAT),
194 TSI_READ(TSI108_EC_RXERR), data->rxpending);
196 #endif
198 /* Synchronization is needed between the thread and up/down events.
199 * Note that the PHY is accessed through the same registers for both
200 * interfaces, so this can't be made interface-specific.
203 static DEFINE_SPINLOCK(phy_lock);
205 static int tsi108_read_mii(struct tsi108_prv_data *data, int reg)
207 unsigned i;
209 TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
210 (data->phy << TSI108_MAC_MII_ADDR_PHY) |
211 (reg << TSI108_MAC_MII_ADDR_REG));
212 TSI_WRITE_PHY(TSI108_MAC_MII_CMD, 0);
213 TSI_WRITE_PHY(TSI108_MAC_MII_CMD, TSI108_MAC_MII_CMD_READ);
214 for (i = 0; i < 100; i++) {
215 if (!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
216 (TSI108_MAC_MII_IND_NOTVALID | TSI108_MAC_MII_IND_BUSY)))
217 break;
218 udelay(10);
221 if (i == 100)
222 return 0xffff;
223 else
224 return TSI_READ_PHY(TSI108_MAC_MII_DATAIN);
227 static void tsi108_write_mii(struct tsi108_prv_data *data,
228 int reg, u16 val)
230 unsigned i = 100;
231 TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
232 (data->phy << TSI108_MAC_MII_ADDR_PHY) |
233 (reg << TSI108_MAC_MII_ADDR_REG));
234 TSI_WRITE_PHY(TSI108_MAC_MII_DATAOUT, val);
235 while (i--) {
236 if(!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
237 TSI108_MAC_MII_IND_BUSY))
238 break;
239 udelay(10);
243 static int tsi108_mdio_read(struct net_device *dev, int addr, int reg)
245 struct tsi108_prv_data *data = netdev_priv(dev);
246 return tsi108_read_mii(data, reg);
249 static void tsi108_mdio_write(struct net_device *dev, int addr, int reg, int val)
251 struct tsi108_prv_data *data = netdev_priv(dev);
252 tsi108_write_mii(data, reg, val);
255 static inline void tsi108_write_tbi(struct tsi108_prv_data *data,
256 int reg, u16 val)
258 unsigned i = 1000;
259 TSI_WRITE(TSI108_MAC_MII_ADDR,
260 (0x1e << TSI108_MAC_MII_ADDR_PHY)
261 | (reg << TSI108_MAC_MII_ADDR_REG));
262 TSI_WRITE(TSI108_MAC_MII_DATAOUT, val);
263 while(i--) {
264 if(!(TSI_READ(TSI108_MAC_MII_IND) & TSI108_MAC_MII_IND_BUSY))
265 return;
266 udelay(10);
268 printk(KERN_ERR "%s function time out\n", __func__);
271 static int mii_speed(struct mii_if_info *mii)
273 int advert, lpa, val, media;
274 int lpa2 = 0;
275 int speed;
277 if (!mii_link_ok(mii))
278 return 0;
280 val = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_BMSR);
281 if ((val & BMSR_ANEGCOMPLETE) == 0)
282 return 0;
284 advert = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_ADVERTISE);
285 lpa = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_LPA);
286 media = mii_nway_result(advert & lpa);
288 if (mii->supports_gmii)
289 lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
291 speed = lpa2 & (LPA_1000FULL | LPA_1000HALF) ? 1000 :
292 (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 100 : 10);
293 return speed;
296 static void tsi108_check_phy(struct net_device *dev)
298 struct tsi108_prv_data *data = netdev_priv(dev);
299 u32 mac_cfg2_reg, portctrl_reg;
300 u32 duplex;
301 u32 speed;
302 unsigned long flags;
304 spin_lock_irqsave(&phy_lock, flags);
306 if (!data->phy_ok)
307 goto out;
309 duplex = mii_check_media(&data->mii_if, netif_msg_link(data), data->init_media);
310 data->init_media = 0;
312 if (netif_carrier_ok(dev)) {
314 speed = mii_speed(&data->mii_if);
316 if ((speed != data->speed) || duplex) {
318 mac_cfg2_reg = TSI_READ(TSI108_MAC_CFG2);
319 portctrl_reg = TSI_READ(TSI108_EC_PORTCTRL);
321 mac_cfg2_reg &= ~TSI108_MAC_CFG2_IFACE_MASK;
323 if (speed == 1000) {
324 mac_cfg2_reg |= TSI108_MAC_CFG2_GIG;
325 portctrl_reg &= ~TSI108_EC_PORTCTRL_NOGIG;
326 } else {
327 mac_cfg2_reg |= TSI108_MAC_CFG2_NOGIG;
328 portctrl_reg |= TSI108_EC_PORTCTRL_NOGIG;
331 data->speed = speed;
333 if (data->mii_if.full_duplex) {
334 mac_cfg2_reg |= TSI108_MAC_CFG2_FULLDUPLEX;
335 portctrl_reg &= ~TSI108_EC_PORTCTRL_HALFDUPLEX;
336 data->duplex = 2;
337 } else {
338 mac_cfg2_reg &= ~TSI108_MAC_CFG2_FULLDUPLEX;
339 portctrl_reg |= TSI108_EC_PORTCTRL_HALFDUPLEX;
340 data->duplex = 1;
343 TSI_WRITE(TSI108_MAC_CFG2, mac_cfg2_reg);
344 TSI_WRITE(TSI108_EC_PORTCTRL, portctrl_reg);
347 if (data->link_up == 0) {
348 /* The manual says it can take 3-4 usecs for the speed change
349 * to take effect.
351 udelay(5);
353 spin_lock(&data->txlock);
354 if (is_valid_ether_addr(dev->dev_addr) && data->txfree)
355 netif_wake_queue(dev);
357 data->link_up = 1;
358 spin_unlock(&data->txlock);
360 } else {
361 if (data->link_up == 1) {
362 netif_stop_queue(dev);
363 data->link_up = 0;
364 printk(KERN_NOTICE "%s : link is down\n", dev->name);
367 goto out;
371 out:
372 spin_unlock_irqrestore(&phy_lock, flags);
375 static inline void
376 tsi108_stat_carry_one(int carry, int carry_bit, int carry_shift,
377 unsigned long *upper)
379 if (carry & carry_bit)
380 *upper += carry_shift;
383 static void tsi108_stat_carry(struct net_device *dev)
385 struct tsi108_prv_data *data = netdev_priv(dev);
386 unsigned long flags;
387 u32 carry1, carry2;
389 spin_lock_irqsave(&data->misclock, flags);
391 carry1 = TSI_READ(TSI108_STAT_CARRY1);
392 carry2 = TSI_READ(TSI108_STAT_CARRY2);
394 TSI_WRITE(TSI108_STAT_CARRY1, carry1);
395 TSI_WRITE(TSI108_STAT_CARRY2, carry2);
397 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXBYTES,
398 TSI108_STAT_RXBYTES_CARRY, &data->stats.rx_bytes);
400 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXPKTS,
401 TSI108_STAT_RXPKTS_CARRY,
402 &data->stats.rx_packets);
404 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXFCS,
405 TSI108_STAT_RXFCS_CARRY, &data->rx_fcs);
407 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXMCAST,
408 TSI108_STAT_RXMCAST_CARRY,
409 &data->stats.multicast);
411 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXALIGN,
412 TSI108_STAT_RXALIGN_CARRY,
413 &data->stats.rx_frame_errors);
415 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXLENGTH,
416 TSI108_STAT_RXLENGTH_CARRY,
417 &data->stats.rx_length_errors);
419 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXRUNT,
420 TSI108_STAT_RXRUNT_CARRY, &data->rx_underruns);
422 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXJUMBO,
423 TSI108_STAT_RXJUMBO_CARRY, &data->rx_overruns);
425 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXFRAG,
426 TSI108_STAT_RXFRAG_CARRY, &data->rx_short_fcs);
428 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXJABBER,
429 TSI108_STAT_RXJABBER_CARRY, &data->rx_long_fcs);
431 tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXDROP,
432 TSI108_STAT_RXDROP_CARRY,
433 &data->stats.rx_missed_errors);
435 tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXBYTES,
436 TSI108_STAT_TXBYTES_CARRY, &data->stats.tx_bytes);
438 tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXPKTS,
439 TSI108_STAT_TXPKTS_CARRY,
440 &data->stats.tx_packets);
442 tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXEXDEF,
443 TSI108_STAT_TXEXDEF_CARRY,
444 &data->stats.tx_aborted_errors);
446 tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXEXCOL,
447 TSI108_STAT_TXEXCOL_CARRY, &data->tx_coll_abort);
449 tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXTCOL,
450 TSI108_STAT_TXTCOL_CARRY,
451 &data->stats.collisions);
453 tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXPAUSE,
454 TSI108_STAT_TXPAUSEDROP_CARRY,
455 &data->tx_pause_drop);
457 spin_unlock_irqrestore(&data->misclock, flags);
460 /* Read a stat counter atomically with respect to carries.
461 * data->misclock must be held.
463 static inline unsigned long
464 tsi108_read_stat(struct tsi108_prv_data * data, int reg, int carry_bit,
465 int carry_shift, unsigned long *upper)
467 int carryreg;
468 unsigned long val;
470 if (reg < 0xb0)
471 carryreg = TSI108_STAT_CARRY1;
472 else
473 carryreg = TSI108_STAT_CARRY2;
475 again:
476 val = TSI_READ(reg) | *upper;
478 /* Check to see if it overflowed, but the interrupt hasn't
479 * been serviced yet. If so, handle the carry here, and
480 * try again.
483 if (unlikely(TSI_READ(carryreg) & carry_bit)) {
484 *upper += carry_shift;
485 TSI_WRITE(carryreg, carry_bit);
486 goto again;
489 return val;
492 static struct net_device_stats *tsi108_get_stats(struct net_device *dev)
494 unsigned long excol;
496 struct tsi108_prv_data *data = netdev_priv(dev);
497 spin_lock_irq(&data->misclock);
499 data->tmpstats.rx_packets =
500 tsi108_read_stat(data, TSI108_STAT_RXPKTS,
501 TSI108_STAT_CARRY1_RXPKTS,
502 TSI108_STAT_RXPKTS_CARRY, &data->stats.rx_packets);
504 data->tmpstats.tx_packets =
505 tsi108_read_stat(data, TSI108_STAT_TXPKTS,
506 TSI108_STAT_CARRY2_TXPKTS,
507 TSI108_STAT_TXPKTS_CARRY, &data->stats.tx_packets);
509 data->tmpstats.rx_bytes =
510 tsi108_read_stat(data, TSI108_STAT_RXBYTES,
511 TSI108_STAT_CARRY1_RXBYTES,
512 TSI108_STAT_RXBYTES_CARRY, &data->stats.rx_bytes);
514 data->tmpstats.tx_bytes =
515 tsi108_read_stat(data, TSI108_STAT_TXBYTES,
516 TSI108_STAT_CARRY2_TXBYTES,
517 TSI108_STAT_TXBYTES_CARRY, &data->stats.tx_bytes);
519 data->tmpstats.multicast =
520 tsi108_read_stat(data, TSI108_STAT_RXMCAST,
521 TSI108_STAT_CARRY1_RXMCAST,
522 TSI108_STAT_RXMCAST_CARRY, &data->stats.multicast);
524 excol = tsi108_read_stat(data, TSI108_STAT_TXEXCOL,
525 TSI108_STAT_CARRY2_TXEXCOL,
526 TSI108_STAT_TXEXCOL_CARRY,
527 &data->tx_coll_abort);
529 data->tmpstats.collisions =
530 tsi108_read_stat(data, TSI108_STAT_TXTCOL,
531 TSI108_STAT_CARRY2_TXTCOL,
532 TSI108_STAT_TXTCOL_CARRY, &data->stats.collisions);
534 data->tmpstats.collisions += excol;
536 data->tmpstats.rx_length_errors =
537 tsi108_read_stat(data, TSI108_STAT_RXLENGTH,
538 TSI108_STAT_CARRY1_RXLENGTH,
539 TSI108_STAT_RXLENGTH_CARRY,
540 &data->stats.rx_length_errors);
542 data->tmpstats.rx_length_errors +=
543 tsi108_read_stat(data, TSI108_STAT_RXRUNT,
544 TSI108_STAT_CARRY1_RXRUNT,
545 TSI108_STAT_RXRUNT_CARRY, &data->rx_underruns);
547 data->tmpstats.rx_length_errors +=
548 tsi108_read_stat(data, TSI108_STAT_RXJUMBO,
549 TSI108_STAT_CARRY1_RXJUMBO,
550 TSI108_STAT_RXJUMBO_CARRY, &data->rx_overruns);
552 data->tmpstats.rx_frame_errors =
553 tsi108_read_stat(data, TSI108_STAT_RXALIGN,
554 TSI108_STAT_CARRY1_RXALIGN,
555 TSI108_STAT_RXALIGN_CARRY,
556 &data->stats.rx_frame_errors);
558 data->tmpstats.rx_frame_errors +=
559 tsi108_read_stat(data, TSI108_STAT_RXFCS,
560 TSI108_STAT_CARRY1_RXFCS, TSI108_STAT_RXFCS_CARRY,
561 &data->rx_fcs);
563 data->tmpstats.rx_frame_errors +=
564 tsi108_read_stat(data, TSI108_STAT_RXFRAG,
565 TSI108_STAT_CARRY1_RXFRAG,
566 TSI108_STAT_RXFRAG_CARRY, &data->rx_short_fcs);
568 data->tmpstats.rx_missed_errors =
569 tsi108_read_stat(data, TSI108_STAT_RXDROP,
570 TSI108_STAT_CARRY1_RXDROP,
571 TSI108_STAT_RXDROP_CARRY,
572 &data->stats.rx_missed_errors);
574 /* These three are maintained by software. */
575 data->tmpstats.rx_fifo_errors = data->stats.rx_fifo_errors;
576 data->tmpstats.rx_crc_errors = data->stats.rx_crc_errors;
578 data->tmpstats.tx_aborted_errors =
579 tsi108_read_stat(data, TSI108_STAT_TXEXDEF,
580 TSI108_STAT_CARRY2_TXEXDEF,
581 TSI108_STAT_TXEXDEF_CARRY,
582 &data->stats.tx_aborted_errors);
584 data->tmpstats.tx_aborted_errors +=
585 tsi108_read_stat(data, TSI108_STAT_TXPAUSEDROP,
586 TSI108_STAT_CARRY2_TXPAUSE,
587 TSI108_STAT_TXPAUSEDROP_CARRY,
588 &data->tx_pause_drop);
590 data->tmpstats.tx_aborted_errors += excol;
592 data->tmpstats.tx_errors = data->tmpstats.tx_aborted_errors;
593 data->tmpstats.rx_errors = data->tmpstats.rx_length_errors +
594 data->tmpstats.rx_crc_errors +
595 data->tmpstats.rx_frame_errors +
596 data->tmpstats.rx_fifo_errors + data->tmpstats.rx_missed_errors;
598 spin_unlock_irq(&data->misclock);
599 return &data->tmpstats;
602 static void tsi108_restart_rx(struct tsi108_prv_data * data, struct net_device *dev)
604 TSI_WRITE(TSI108_EC_RXQ_PTRHIGH,
605 TSI108_EC_RXQ_PTRHIGH_VALID);
607 TSI_WRITE(TSI108_EC_RXCTRL, TSI108_EC_RXCTRL_GO
608 | TSI108_EC_RXCTRL_QUEUE0);
611 static void tsi108_restart_tx(struct tsi108_prv_data * data)
613 TSI_WRITE(TSI108_EC_TXQ_PTRHIGH,
614 TSI108_EC_TXQ_PTRHIGH_VALID);
616 TSI_WRITE(TSI108_EC_TXCTRL, TSI108_EC_TXCTRL_IDLEINT |
617 TSI108_EC_TXCTRL_GO | TSI108_EC_TXCTRL_QUEUE0);
620 /* txlock must be held by caller, with IRQs disabled, and
621 * with permission to re-enable them when the lock is dropped.
623 static void tsi108_complete_tx(struct net_device *dev)
625 struct tsi108_prv_data *data = netdev_priv(dev);
626 int tx;
627 struct sk_buff *skb;
628 int release = 0;
630 while (!data->txfree || data->txhead != data->txtail) {
631 tx = data->txtail;
633 if (data->txring[tx].misc & TSI108_TX_OWN)
634 break;
636 skb = data->txskbs[tx];
638 if (!(data->txring[tx].misc & TSI108_TX_OK))
639 printk("%s: bad tx packet, misc %x\n",
640 dev->name, data->txring[tx].misc);
642 data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
643 data->txfree++;
645 if (data->txring[tx].misc & TSI108_TX_EOF) {
646 dev_kfree_skb_any(skb);
647 release++;
651 if (release) {
652 if (is_valid_ether_addr(dev->dev_addr) && data->link_up)
653 netif_wake_queue(dev);
657 static int tsi108_send_packet(struct sk_buff * skb, struct net_device *dev)
659 struct tsi108_prv_data *data = netdev_priv(dev);
660 int frags = skb_shinfo(skb)->nr_frags + 1;
661 int i;
663 if (!data->phy_ok && net_ratelimit())
664 printk(KERN_ERR "%s: Transmit while PHY is down!\n", dev->name);
666 if (!data->link_up) {
667 printk(KERN_ERR "%s: Transmit while link is down!\n",
668 dev->name);
669 netif_stop_queue(dev);
670 return NETDEV_TX_BUSY;
673 if (data->txfree < MAX_SKB_FRAGS + 1) {
674 netif_stop_queue(dev);
676 if (net_ratelimit())
677 printk(KERN_ERR "%s: Transmit with full tx ring!\n",
678 dev->name);
679 return NETDEV_TX_BUSY;
682 if (data->txfree - frags < MAX_SKB_FRAGS + 1) {
683 netif_stop_queue(dev);
686 spin_lock_irq(&data->txlock);
688 for (i = 0; i < frags; i++) {
689 int misc = 0;
690 int tx = data->txhead;
692 /* This is done to mark every TSI108_TX_INT_FREQ tx buffers with
693 * the interrupt bit. TX descriptor-complete interrupts are
694 * enabled when the queue fills up, and masked when there is
695 * still free space. This way, when saturating the outbound
696 * link, the tx interrupts are kept to a reasonable level.
697 * When the queue is not full, reclamation of skbs still occurs
698 * as new packets are transmitted, or on a queue-empty
699 * interrupt.
702 if ((tx % TSI108_TX_INT_FREQ == 0) &&
703 ((TSI108_TXRING_LEN - data->txfree) >= TSI108_TX_INT_FREQ))
704 misc = TSI108_TX_INT;
706 data->txskbs[tx] = skb;
708 if (i == 0) {
709 data->txring[tx].buf0 = dma_map_single(&data->pdev->dev,
710 skb->data, skb_headlen(skb),
711 DMA_TO_DEVICE);
712 data->txring[tx].len = skb_headlen(skb);
713 misc |= TSI108_TX_SOF;
714 } else {
715 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
717 data->txring[tx].buf0 =
718 skb_frag_dma_map(&data->pdev->dev, frag,
719 0, skb_frag_size(frag),
720 DMA_TO_DEVICE);
721 data->txring[tx].len = skb_frag_size(frag);
724 if (i == frags - 1)
725 misc |= TSI108_TX_EOF;
727 if (netif_msg_pktdata(data)) {
728 int i;
729 printk("%s: Tx Frame contents (%d)\n", dev->name,
730 skb->len);
731 for (i = 0; i < skb->len; i++)
732 printk(" %2.2x", skb->data[i]);
733 printk(".\n");
735 data->txring[tx].misc = misc | TSI108_TX_OWN;
737 data->txhead = (data->txhead + 1) % TSI108_TXRING_LEN;
738 data->txfree--;
741 tsi108_complete_tx(dev);
743 /* This must be done after the check for completed tx descriptors,
744 * so that the tail pointer is correct.
747 if (!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_QUEUE0))
748 tsi108_restart_tx(data);
750 spin_unlock_irq(&data->txlock);
751 return NETDEV_TX_OK;
754 static int tsi108_complete_rx(struct net_device *dev, int budget)
756 struct tsi108_prv_data *data = netdev_priv(dev);
757 int done = 0;
759 while (data->rxfree && done != budget) {
760 int rx = data->rxtail;
761 struct sk_buff *skb;
763 if (data->rxring[rx].misc & TSI108_RX_OWN)
764 break;
766 skb = data->rxskbs[rx];
767 data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
768 data->rxfree--;
769 done++;
771 if (data->rxring[rx].misc & TSI108_RX_BAD) {
772 spin_lock_irq(&data->misclock);
774 if (data->rxring[rx].misc & TSI108_RX_CRC)
775 data->stats.rx_crc_errors++;
776 if (data->rxring[rx].misc & TSI108_RX_OVER)
777 data->stats.rx_fifo_errors++;
779 spin_unlock_irq(&data->misclock);
781 dev_kfree_skb_any(skb);
782 continue;
784 if (netif_msg_pktdata(data)) {
785 int i;
786 printk("%s: Rx Frame contents (%d)\n",
787 dev->name, data->rxring[rx].len);
788 for (i = 0; i < data->rxring[rx].len; i++)
789 printk(" %2.2x", skb->data[i]);
790 printk(".\n");
793 skb_put(skb, data->rxring[rx].len);
794 skb->protocol = eth_type_trans(skb, dev);
795 netif_receive_skb(skb);
798 return done;
801 static int tsi108_refill_rx(struct net_device *dev, int budget)
803 struct tsi108_prv_data *data = netdev_priv(dev);
804 int done = 0;
806 while (data->rxfree != TSI108_RXRING_LEN && done != budget) {
807 int rx = data->rxhead;
808 struct sk_buff *skb;
810 skb = netdev_alloc_skb_ip_align(dev, TSI108_RXBUF_SIZE);
811 data->rxskbs[rx] = skb;
812 if (!skb)
813 break;
815 data->rxring[rx].buf0 = dma_map_single(&data->pdev->dev,
816 skb->data, TSI108_RX_SKB_SIZE,
817 DMA_FROM_DEVICE);
819 /* Sometimes the hardware sets blen to zero after packet
820 * reception, even though the manual says that it's only ever
821 * modified by the driver.
824 data->rxring[rx].blen = TSI108_RX_SKB_SIZE;
825 data->rxring[rx].misc = TSI108_RX_OWN | TSI108_RX_INT;
827 data->rxhead = (data->rxhead + 1) % TSI108_RXRING_LEN;
828 data->rxfree++;
829 done++;
832 if (done != 0 && !(TSI_READ(TSI108_EC_RXSTAT) &
833 TSI108_EC_RXSTAT_QUEUE0))
834 tsi108_restart_rx(data, dev);
836 return done;
839 static int tsi108_poll(struct napi_struct *napi, int budget)
841 struct tsi108_prv_data *data = container_of(napi, struct tsi108_prv_data, napi);
842 struct net_device *dev = data->dev;
843 u32 estat = TSI_READ(TSI108_EC_RXESTAT);
844 u32 intstat = TSI_READ(TSI108_EC_INTSTAT);
845 int num_received = 0, num_filled = 0;
847 intstat &= TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
848 TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR | TSI108_INT_RXWAIT;
850 TSI_WRITE(TSI108_EC_RXESTAT, estat);
851 TSI_WRITE(TSI108_EC_INTSTAT, intstat);
853 if (data->rxpending || (estat & TSI108_EC_RXESTAT_Q0_DESCINT))
854 num_received = tsi108_complete_rx(dev, budget);
856 /* This should normally fill no more slots than the number of
857 * packets received in tsi108_complete_rx(). The exception
858 * is when we previously ran out of memory for RX SKBs. In that
859 * case, it's helpful to obey the budget, not only so that the
860 * CPU isn't hogged, but so that memory (which may still be low)
861 * is not hogged by one device.
863 * A work unit is considered to be two SKBs to allow us to catch
864 * up when the ring has shrunk due to out-of-memory but we're
865 * still removing the full budget's worth of packets each time.
868 if (data->rxfree < TSI108_RXRING_LEN)
869 num_filled = tsi108_refill_rx(dev, budget * 2);
871 if (intstat & TSI108_INT_RXERROR) {
872 u32 err = TSI_READ(TSI108_EC_RXERR);
873 TSI_WRITE(TSI108_EC_RXERR, err);
875 if (err) {
876 if (net_ratelimit())
877 printk(KERN_DEBUG "%s: RX error %x\n",
878 dev->name, err);
880 if (!(TSI_READ(TSI108_EC_RXSTAT) &
881 TSI108_EC_RXSTAT_QUEUE0))
882 tsi108_restart_rx(data, dev);
886 if (intstat & TSI108_INT_RXOVERRUN) {
887 spin_lock_irq(&data->misclock);
888 data->stats.rx_fifo_errors++;
889 spin_unlock_irq(&data->misclock);
892 if (num_received < budget) {
893 data->rxpending = 0;
894 napi_complete_done(napi, num_received);
896 TSI_WRITE(TSI108_EC_INTMASK,
897 TSI_READ(TSI108_EC_INTMASK)
898 & ~(TSI108_INT_RXQUEUE0
899 | TSI108_INT_RXTHRESH |
900 TSI108_INT_RXOVERRUN |
901 TSI108_INT_RXERROR |
902 TSI108_INT_RXWAIT));
903 } else {
904 data->rxpending = 1;
907 return num_received;
910 static void tsi108_rx_int(struct net_device *dev)
912 struct tsi108_prv_data *data = netdev_priv(dev);
914 /* A race could cause dev to already be scheduled, so it's not an
915 * error if that happens (and interrupts shouldn't be re-masked,
916 * because that can cause harmful races, if poll has already
917 * unmasked them but not cleared LINK_STATE_SCHED).
919 * This can happen if this code races with tsi108_poll(), which masks
920 * the interrupts after tsi108_irq_one() read the mask, but before
921 * napi_schedule is called. It could also happen due to calls
922 * from tsi108_check_rxring().
925 if (napi_schedule_prep(&data->napi)) {
926 /* Mask, rather than ack, the receive interrupts. The ack
927 * will happen in tsi108_poll().
930 TSI_WRITE(TSI108_EC_INTMASK,
931 TSI_READ(TSI108_EC_INTMASK) |
932 TSI108_INT_RXQUEUE0
933 | TSI108_INT_RXTHRESH |
934 TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR |
935 TSI108_INT_RXWAIT);
936 __napi_schedule(&data->napi);
937 } else {
938 if (!netif_running(dev)) {
939 /* This can happen if an interrupt occurs while the
940 * interface is being brought down, as the START
941 * bit is cleared before the stop function is called.
943 * In this case, the interrupts must be masked, or
944 * they will continue indefinitely.
946 * There's a race here if the interface is brought down
947 * and then up in rapid succession, as the device could
948 * be made running after the above check and before
949 * the masking below. This will only happen if the IRQ
950 * thread has a lower priority than the task brining
951 * up the interface. Fixing this race would likely
952 * require changes in generic code.
955 TSI_WRITE(TSI108_EC_INTMASK,
956 TSI_READ
957 (TSI108_EC_INTMASK) |
958 TSI108_INT_RXQUEUE0 |
959 TSI108_INT_RXTHRESH |
960 TSI108_INT_RXOVERRUN |
961 TSI108_INT_RXERROR |
962 TSI108_INT_RXWAIT);
967 /* If the RX ring has run out of memory, try periodically
968 * to allocate some more, as otherwise poll would never
969 * get called (apart from the initial end-of-queue condition).
971 * This is called once per second (by default) from the thread.
974 static void tsi108_check_rxring(struct net_device *dev)
976 struct tsi108_prv_data *data = netdev_priv(dev);
978 /* A poll is scheduled, as opposed to caling tsi108_refill_rx
979 * directly, so as to keep the receive path single-threaded
980 * (and thus not needing a lock).
983 if (netif_running(dev) && data->rxfree < TSI108_RXRING_LEN / 4)
984 tsi108_rx_int(dev);
987 static void tsi108_tx_int(struct net_device *dev)
989 struct tsi108_prv_data *data = netdev_priv(dev);
990 u32 estat = TSI_READ(TSI108_EC_TXESTAT);
992 TSI_WRITE(TSI108_EC_TXESTAT, estat);
993 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_TXQUEUE0 |
994 TSI108_INT_TXIDLE | TSI108_INT_TXERROR);
995 if (estat & TSI108_EC_TXESTAT_Q0_ERR) {
996 u32 err = TSI_READ(TSI108_EC_TXERR);
997 TSI_WRITE(TSI108_EC_TXERR, err);
999 if (err && net_ratelimit())
1000 printk(KERN_ERR "%s: TX error %x\n", dev->name, err);
1003 if (estat & (TSI108_EC_TXESTAT_Q0_DESCINT | TSI108_EC_TXESTAT_Q0_EOQ)) {
1004 spin_lock(&data->txlock);
1005 tsi108_complete_tx(dev);
1006 spin_unlock(&data->txlock);
1011 static irqreturn_t tsi108_irq(int irq, void *dev_id)
1013 struct net_device *dev = dev_id;
1014 struct tsi108_prv_data *data = netdev_priv(dev);
1015 u32 stat = TSI_READ(TSI108_EC_INTSTAT);
1017 if (!(stat & TSI108_INT_ANY))
1018 return IRQ_NONE; /* Not our interrupt */
1020 stat &= ~TSI_READ(TSI108_EC_INTMASK);
1022 if (stat & (TSI108_INT_TXQUEUE0 | TSI108_INT_TXIDLE |
1023 TSI108_INT_TXERROR))
1024 tsi108_tx_int(dev);
1025 if (stat & (TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
1026 TSI108_INT_RXWAIT | TSI108_INT_RXOVERRUN |
1027 TSI108_INT_RXERROR))
1028 tsi108_rx_int(dev);
1030 if (stat & TSI108_INT_SFN) {
1031 if (net_ratelimit())
1032 printk(KERN_DEBUG "%s: SFN error\n", dev->name);
1033 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_SFN);
1036 if (stat & TSI108_INT_STATCARRY) {
1037 tsi108_stat_carry(dev);
1038 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_STATCARRY);
1041 return IRQ_HANDLED;
1044 static void tsi108_stop_ethernet(struct net_device *dev)
1046 struct tsi108_prv_data *data = netdev_priv(dev);
1047 int i = 1000;
1048 /* Disable all TX and RX queues ... */
1049 TSI_WRITE(TSI108_EC_TXCTRL, 0);
1050 TSI_WRITE(TSI108_EC_RXCTRL, 0);
1052 /* ...and wait for them to become idle */
1053 while(i--) {
1054 if(!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_ACTIVE))
1055 break;
1056 udelay(10);
1058 i = 1000;
1059 while(i--){
1060 if(!(TSI_READ(TSI108_EC_RXSTAT) & TSI108_EC_RXSTAT_ACTIVE))
1061 return;
1062 udelay(10);
1064 printk(KERN_ERR "%s function time out\n", __func__);
1067 static void tsi108_reset_ether(struct tsi108_prv_data * data)
1069 TSI_WRITE(TSI108_MAC_CFG1, TSI108_MAC_CFG1_SOFTRST);
1070 udelay(100);
1071 TSI_WRITE(TSI108_MAC_CFG1, 0);
1073 TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATRST);
1074 udelay(100);
1075 TSI_WRITE(TSI108_EC_PORTCTRL,
1076 TSI_READ(TSI108_EC_PORTCTRL) &
1077 ~TSI108_EC_PORTCTRL_STATRST);
1079 TSI_WRITE(TSI108_EC_TXCFG, TSI108_EC_TXCFG_RST);
1080 udelay(100);
1081 TSI_WRITE(TSI108_EC_TXCFG,
1082 TSI_READ(TSI108_EC_TXCFG) &
1083 ~TSI108_EC_TXCFG_RST);
1085 TSI_WRITE(TSI108_EC_RXCFG, TSI108_EC_RXCFG_RST);
1086 udelay(100);
1087 TSI_WRITE(TSI108_EC_RXCFG,
1088 TSI_READ(TSI108_EC_RXCFG) &
1089 ~TSI108_EC_RXCFG_RST);
1091 TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
1092 TSI_READ(TSI108_MAC_MII_MGMT_CFG) |
1093 TSI108_MAC_MII_MGMT_RST);
1094 udelay(100);
1095 TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
1096 (TSI_READ(TSI108_MAC_MII_MGMT_CFG) &
1097 ~(TSI108_MAC_MII_MGMT_RST |
1098 TSI108_MAC_MII_MGMT_CLK)) | 0x07);
1101 static int tsi108_get_mac(struct net_device *dev)
1103 struct tsi108_prv_data *data = netdev_priv(dev);
1104 u32 word1 = TSI_READ(TSI108_MAC_ADDR1);
1105 u32 word2 = TSI_READ(TSI108_MAC_ADDR2);
1107 /* Note that the octets are reversed from what the manual says,
1108 * producing an even weirder ordering...
1110 if (word2 == 0 && word1 == 0) {
1111 dev->dev_addr[0] = 0x00;
1112 dev->dev_addr[1] = 0x06;
1113 dev->dev_addr[2] = 0xd2;
1114 dev->dev_addr[3] = 0x00;
1115 dev->dev_addr[4] = 0x00;
1116 if (0x8 == data->phy)
1117 dev->dev_addr[5] = 0x01;
1118 else
1119 dev->dev_addr[5] = 0x02;
1121 word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
1123 word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
1124 (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
1126 TSI_WRITE(TSI108_MAC_ADDR1, word1);
1127 TSI_WRITE(TSI108_MAC_ADDR2, word2);
1128 } else {
1129 dev->dev_addr[0] = (word2 >> 16) & 0xff;
1130 dev->dev_addr[1] = (word2 >> 24) & 0xff;
1131 dev->dev_addr[2] = (word1 >> 0) & 0xff;
1132 dev->dev_addr[3] = (word1 >> 8) & 0xff;
1133 dev->dev_addr[4] = (word1 >> 16) & 0xff;
1134 dev->dev_addr[5] = (word1 >> 24) & 0xff;
1137 if (!is_valid_ether_addr(dev->dev_addr)) {
1138 printk(KERN_ERR
1139 "%s: Invalid MAC address. word1: %08x, word2: %08x\n",
1140 dev->name, word1, word2);
1141 return -EINVAL;
1144 return 0;
1147 static int tsi108_set_mac(struct net_device *dev, void *addr)
1149 struct tsi108_prv_data *data = netdev_priv(dev);
1150 u32 word1, word2;
1151 int i;
1153 if (!is_valid_ether_addr(addr))
1154 return -EADDRNOTAVAIL;
1156 for (i = 0; i < 6; i++)
1157 /* +2 is for the offset of the HW addr type */
1158 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1160 word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
1162 word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
1163 (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
1165 spin_lock_irq(&data->misclock);
1166 TSI_WRITE(TSI108_MAC_ADDR1, word1);
1167 TSI_WRITE(TSI108_MAC_ADDR2, word2);
1168 spin_lock(&data->txlock);
1170 if (data->txfree && data->link_up)
1171 netif_wake_queue(dev);
1173 spin_unlock(&data->txlock);
1174 spin_unlock_irq(&data->misclock);
1175 return 0;
1178 /* Protected by dev->xmit_lock. */
1179 static void tsi108_set_rx_mode(struct net_device *dev)
1181 struct tsi108_prv_data *data = netdev_priv(dev);
1182 u32 rxcfg = TSI_READ(TSI108_EC_RXCFG);
1184 if (dev->flags & IFF_PROMISC) {
1185 rxcfg &= ~(TSI108_EC_RXCFG_UC_HASH | TSI108_EC_RXCFG_MC_HASH);
1186 rxcfg |= TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE;
1187 goto out;
1190 rxcfg &= ~(TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE);
1192 if (dev->flags & IFF_ALLMULTI || !netdev_mc_empty(dev)) {
1193 int i;
1194 struct netdev_hw_addr *ha;
1195 rxcfg |= TSI108_EC_RXCFG_MFE | TSI108_EC_RXCFG_MC_HASH;
1197 memset(data->mc_hash, 0, sizeof(data->mc_hash));
1199 netdev_for_each_mc_addr(ha, dev) {
1200 u32 hash, crc;
1202 crc = ether_crc(6, ha->addr);
1203 hash = crc >> 23;
1204 __set_bit(hash, &data->mc_hash[0]);
1207 TSI_WRITE(TSI108_EC_HASHADDR,
1208 TSI108_EC_HASHADDR_AUTOINC |
1209 TSI108_EC_HASHADDR_MCAST);
1211 for (i = 0; i < 16; i++) {
1212 /* The manual says that the hardware may drop
1213 * back-to-back writes to the data register.
1215 udelay(1);
1216 TSI_WRITE(TSI108_EC_HASHDATA,
1217 data->mc_hash[i]);
1221 out:
1222 TSI_WRITE(TSI108_EC_RXCFG, rxcfg);
1225 static void tsi108_init_phy(struct net_device *dev)
1227 struct tsi108_prv_data *data = netdev_priv(dev);
1228 u32 i = 0;
1229 u16 phyval = 0;
1230 unsigned long flags;
1232 spin_lock_irqsave(&phy_lock, flags);
1234 tsi108_write_mii(data, MII_BMCR, BMCR_RESET);
1235 while (--i) {
1236 if(!(tsi108_read_mii(data, MII_BMCR) & BMCR_RESET))
1237 break;
1238 udelay(10);
1240 if (i == 0)
1241 printk(KERN_ERR "%s function time out\n", __func__);
1243 if (data->phy_type == TSI108_PHY_BCM54XX) {
1244 tsi108_write_mii(data, 0x09, 0x0300);
1245 tsi108_write_mii(data, 0x10, 0x1020);
1246 tsi108_write_mii(data, 0x1c, 0x8c00);
1249 tsi108_write_mii(data,
1250 MII_BMCR,
1251 BMCR_ANENABLE | BMCR_ANRESTART);
1252 while (tsi108_read_mii(data, MII_BMCR) & BMCR_ANRESTART)
1253 cpu_relax();
1255 /* Set G/MII mode and receive clock select in TBI control #2. The
1256 * second port won't work if this isn't done, even though we don't
1257 * use TBI mode.
1260 tsi108_write_tbi(data, 0x11, 0x30);
1262 /* FIXME: It seems to take more than 2 back-to-back reads to the
1263 * PHY_STAT register before the link up status bit is set.
1266 data->link_up = 0;
1268 while (!((phyval = tsi108_read_mii(data, MII_BMSR)) &
1269 BMSR_LSTATUS)) {
1270 if (i++ > (MII_READ_DELAY / 10)) {
1271 break;
1273 spin_unlock_irqrestore(&phy_lock, flags);
1274 msleep(10);
1275 spin_lock_irqsave(&phy_lock, flags);
1278 data->mii_if.supports_gmii = mii_check_gmii_support(&data->mii_if);
1279 printk(KERN_DEBUG "PHY_STAT reg contains %08x\n", phyval);
1280 data->phy_ok = 1;
1281 data->init_media = 1;
1282 spin_unlock_irqrestore(&phy_lock, flags);
1285 static void tsi108_kill_phy(struct net_device *dev)
1287 struct tsi108_prv_data *data = netdev_priv(dev);
1288 unsigned long flags;
1290 spin_lock_irqsave(&phy_lock, flags);
1291 tsi108_write_mii(data, MII_BMCR, BMCR_PDOWN);
1292 data->phy_ok = 0;
1293 spin_unlock_irqrestore(&phy_lock, flags);
1296 static int tsi108_open(struct net_device *dev)
1298 int i;
1299 struct tsi108_prv_data *data = netdev_priv(dev);
1300 unsigned int rxring_size = TSI108_RXRING_LEN * sizeof(rx_desc);
1301 unsigned int txring_size = TSI108_TXRING_LEN * sizeof(tx_desc);
1303 i = request_irq(data->irq_num, tsi108_irq, 0, dev->name, dev);
1304 if (i != 0) {
1305 printk(KERN_ERR "tsi108_eth%d: Could not allocate IRQ%d.\n",
1306 data->id, data->irq_num);
1307 return i;
1308 } else {
1309 dev->irq = data->irq_num;
1310 printk(KERN_NOTICE
1311 "tsi108_open : Port %d Assigned IRQ %d to %s\n",
1312 data->id, dev->irq, dev->name);
1315 data->rxring = dma_zalloc_coherent(&data->pdev->dev, rxring_size,
1316 &data->rxdma, GFP_KERNEL);
1317 if (!data->rxring)
1318 return -ENOMEM;
1320 data->txring = dma_zalloc_coherent(&data->pdev->dev, txring_size,
1321 &data->txdma, GFP_KERNEL);
1322 if (!data->txring) {
1323 dma_free_coherent(&data->pdev->dev, rxring_size, data->rxring,
1324 data->rxdma);
1325 return -ENOMEM;
1328 for (i = 0; i < TSI108_RXRING_LEN; i++) {
1329 data->rxring[i].next0 = data->rxdma + (i + 1) * sizeof(rx_desc);
1330 data->rxring[i].blen = TSI108_RXBUF_SIZE;
1331 data->rxring[i].vlan = 0;
1334 data->rxring[TSI108_RXRING_LEN - 1].next0 = data->rxdma;
1336 data->rxtail = 0;
1337 data->rxhead = 0;
1339 for (i = 0; i < TSI108_RXRING_LEN; i++) {
1340 struct sk_buff *skb;
1342 skb = netdev_alloc_skb_ip_align(dev, TSI108_RXBUF_SIZE);
1343 if (!skb) {
1344 /* Bah. No memory for now, but maybe we'll get
1345 * some more later.
1346 * For now, we'll live with the smaller ring.
1348 printk(KERN_WARNING
1349 "%s: Could only allocate %d receive skb(s).\n",
1350 dev->name, i);
1351 data->rxhead = i;
1352 break;
1355 data->rxskbs[i] = skb;
1356 data->rxring[i].buf0 = virt_to_phys(data->rxskbs[i]->data);
1357 data->rxring[i].misc = TSI108_RX_OWN | TSI108_RX_INT;
1360 data->rxfree = i;
1361 TSI_WRITE(TSI108_EC_RXQ_PTRLOW, data->rxdma);
1363 for (i = 0; i < TSI108_TXRING_LEN; i++) {
1364 data->txring[i].next0 = data->txdma + (i + 1) * sizeof(tx_desc);
1365 data->txring[i].misc = 0;
1368 data->txring[TSI108_TXRING_LEN - 1].next0 = data->txdma;
1369 data->txtail = 0;
1370 data->txhead = 0;
1371 data->txfree = TSI108_TXRING_LEN;
1372 TSI_WRITE(TSI108_EC_TXQ_PTRLOW, data->txdma);
1373 tsi108_init_phy(dev);
1375 napi_enable(&data->napi);
1377 timer_setup(&data->timer, tsi108_timed_checker, 0);
1378 mod_timer(&data->timer, jiffies + 1);
1380 tsi108_restart_rx(data, dev);
1382 TSI_WRITE(TSI108_EC_INTSTAT, ~0);
1384 TSI_WRITE(TSI108_EC_INTMASK,
1385 ~(TSI108_INT_TXQUEUE0 | TSI108_INT_RXERROR |
1386 TSI108_INT_RXTHRESH | TSI108_INT_RXQUEUE0 |
1387 TSI108_INT_RXOVERRUN | TSI108_INT_RXWAIT |
1388 TSI108_INT_SFN | TSI108_INT_STATCARRY));
1390 TSI_WRITE(TSI108_MAC_CFG1,
1391 TSI108_MAC_CFG1_RXEN | TSI108_MAC_CFG1_TXEN);
1392 netif_start_queue(dev);
1393 return 0;
1396 static int tsi108_close(struct net_device *dev)
1398 struct tsi108_prv_data *data = netdev_priv(dev);
1400 netif_stop_queue(dev);
1401 napi_disable(&data->napi);
1403 del_timer_sync(&data->timer);
1405 tsi108_stop_ethernet(dev);
1406 tsi108_kill_phy(dev);
1407 TSI_WRITE(TSI108_EC_INTMASK, ~0);
1408 TSI_WRITE(TSI108_MAC_CFG1, 0);
1410 /* Check for any pending TX packets, and drop them. */
1412 while (!data->txfree || data->txhead != data->txtail) {
1413 int tx = data->txtail;
1414 struct sk_buff *skb;
1415 skb = data->txskbs[tx];
1416 data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
1417 data->txfree++;
1418 dev_kfree_skb(skb);
1421 free_irq(data->irq_num, dev);
1423 /* Discard the RX ring. */
1425 while (data->rxfree) {
1426 int rx = data->rxtail;
1427 struct sk_buff *skb;
1429 skb = data->rxskbs[rx];
1430 data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
1431 data->rxfree--;
1432 dev_kfree_skb(skb);
1435 dma_free_coherent(&data->pdev->dev,
1436 TSI108_RXRING_LEN * sizeof(rx_desc),
1437 data->rxring, data->rxdma);
1438 dma_free_coherent(&data->pdev->dev,
1439 TSI108_TXRING_LEN * sizeof(tx_desc),
1440 data->txring, data->txdma);
1442 return 0;
1445 static void tsi108_init_mac(struct net_device *dev)
1447 struct tsi108_prv_data *data = netdev_priv(dev);
1449 TSI_WRITE(TSI108_MAC_CFG2, TSI108_MAC_CFG2_DFLT_PREAMBLE |
1450 TSI108_MAC_CFG2_PADCRC);
1452 TSI_WRITE(TSI108_EC_TXTHRESH,
1453 (192 << TSI108_EC_TXTHRESH_STARTFILL) |
1454 (192 << TSI108_EC_TXTHRESH_STOPFILL));
1456 TSI_WRITE(TSI108_STAT_CARRYMASK1,
1457 ~(TSI108_STAT_CARRY1_RXBYTES |
1458 TSI108_STAT_CARRY1_RXPKTS |
1459 TSI108_STAT_CARRY1_RXFCS |
1460 TSI108_STAT_CARRY1_RXMCAST |
1461 TSI108_STAT_CARRY1_RXALIGN |
1462 TSI108_STAT_CARRY1_RXLENGTH |
1463 TSI108_STAT_CARRY1_RXRUNT |
1464 TSI108_STAT_CARRY1_RXJUMBO |
1465 TSI108_STAT_CARRY1_RXFRAG |
1466 TSI108_STAT_CARRY1_RXJABBER |
1467 TSI108_STAT_CARRY1_RXDROP));
1469 TSI_WRITE(TSI108_STAT_CARRYMASK2,
1470 ~(TSI108_STAT_CARRY2_TXBYTES |
1471 TSI108_STAT_CARRY2_TXPKTS |
1472 TSI108_STAT_CARRY2_TXEXDEF |
1473 TSI108_STAT_CARRY2_TXEXCOL |
1474 TSI108_STAT_CARRY2_TXTCOL |
1475 TSI108_STAT_CARRY2_TXPAUSE));
1477 TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATEN);
1478 TSI_WRITE(TSI108_MAC_CFG1, 0);
1480 TSI_WRITE(TSI108_EC_RXCFG,
1481 TSI108_EC_RXCFG_SE | TSI108_EC_RXCFG_BFE);
1483 TSI_WRITE(TSI108_EC_TXQ_CFG, TSI108_EC_TXQ_CFG_DESC_INT |
1484 TSI108_EC_TXQ_CFG_EOQ_OWN_INT |
1485 TSI108_EC_TXQ_CFG_WSWP | (TSI108_PBM_PORT <<
1486 TSI108_EC_TXQ_CFG_SFNPORT));
1488 TSI_WRITE(TSI108_EC_RXQ_CFG, TSI108_EC_RXQ_CFG_DESC_INT |
1489 TSI108_EC_RXQ_CFG_EOQ_OWN_INT |
1490 TSI108_EC_RXQ_CFG_WSWP | (TSI108_PBM_PORT <<
1491 TSI108_EC_RXQ_CFG_SFNPORT));
1493 TSI_WRITE(TSI108_EC_TXQ_BUFCFG,
1494 TSI108_EC_TXQ_BUFCFG_BURST256 |
1495 TSI108_EC_TXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
1496 TSI108_EC_TXQ_BUFCFG_SFNPORT));
1498 TSI_WRITE(TSI108_EC_RXQ_BUFCFG,
1499 TSI108_EC_RXQ_BUFCFG_BURST256 |
1500 TSI108_EC_RXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
1501 TSI108_EC_RXQ_BUFCFG_SFNPORT));
1503 TSI_WRITE(TSI108_EC_INTMASK, ~0);
1506 static int tsi108_get_link_ksettings(struct net_device *dev,
1507 struct ethtool_link_ksettings *cmd)
1509 struct tsi108_prv_data *data = netdev_priv(dev);
1510 unsigned long flags;
1512 spin_lock_irqsave(&data->txlock, flags);
1513 mii_ethtool_get_link_ksettings(&data->mii_if, cmd);
1514 spin_unlock_irqrestore(&data->txlock, flags);
1516 return 0;
1519 static int tsi108_set_link_ksettings(struct net_device *dev,
1520 const struct ethtool_link_ksettings *cmd)
1522 struct tsi108_prv_data *data = netdev_priv(dev);
1523 unsigned long flags;
1524 int rc;
1526 spin_lock_irqsave(&data->txlock, flags);
1527 rc = mii_ethtool_set_link_ksettings(&data->mii_if, cmd);
1528 spin_unlock_irqrestore(&data->txlock, flags);
1530 return rc;
1533 static int tsi108_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1535 struct tsi108_prv_data *data = netdev_priv(dev);
1536 if (!netif_running(dev))
1537 return -EINVAL;
1538 return generic_mii_ioctl(&data->mii_if, if_mii(rq), cmd, NULL);
1541 static const struct ethtool_ops tsi108_ethtool_ops = {
1542 .get_link = ethtool_op_get_link,
1543 .get_link_ksettings = tsi108_get_link_ksettings,
1544 .set_link_ksettings = tsi108_set_link_ksettings,
1547 static const struct net_device_ops tsi108_netdev_ops = {
1548 .ndo_open = tsi108_open,
1549 .ndo_stop = tsi108_close,
1550 .ndo_start_xmit = tsi108_send_packet,
1551 .ndo_set_rx_mode = tsi108_set_rx_mode,
1552 .ndo_get_stats = tsi108_get_stats,
1553 .ndo_do_ioctl = tsi108_do_ioctl,
1554 .ndo_set_mac_address = tsi108_set_mac,
1555 .ndo_validate_addr = eth_validate_addr,
1558 static int
1559 tsi108_init_one(struct platform_device *pdev)
1561 struct net_device *dev = NULL;
1562 struct tsi108_prv_data *data = NULL;
1563 hw_info *einfo;
1564 int err = 0;
1566 einfo = dev_get_platdata(&pdev->dev);
1568 if (NULL == einfo) {
1569 printk(KERN_ERR "tsi-eth %d: Missing additional data!\n",
1570 pdev->id);
1571 return -ENODEV;
1574 /* Create an ethernet device instance */
1576 dev = alloc_etherdev(sizeof(struct tsi108_prv_data));
1577 if (!dev)
1578 return -ENOMEM;
1580 printk("tsi108_eth%d: probe...\n", pdev->id);
1581 data = netdev_priv(dev);
1582 data->dev = dev;
1583 data->pdev = pdev;
1585 pr_debug("tsi108_eth%d:regs:phyresgs:phy:irq_num=0x%x:0x%x:0x%x:0x%x\n",
1586 pdev->id, einfo->regs, einfo->phyregs,
1587 einfo->phy, einfo->irq_num);
1589 data->regs = ioremap(einfo->regs, 0x400);
1590 if (NULL == data->regs) {
1591 err = -ENOMEM;
1592 goto regs_fail;
1595 data->phyregs = ioremap(einfo->phyregs, 0x400);
1596 if (NULL == data->phyregs) {
1597 err = -ENOMEM;
1598 goto phyregs_fail;
1600 /* MII setup */
1601 data->mii_if.dev = dev;
1602 data->mii_if.mdio_read = tsi108_mdio_read;
1603 data->mii_if.mdio_write = tsi108_mdio_write;
1604 data->mii_if.phy_id = einfo->phy;
1605 data->mii_if.phy_id_mask = 0x1f;
1606 data->mii_if.reg_num_mask = 0x1f;
1608 data->phy = einfo->phy;
1609 data->phy_type = einfo->phy_type;
1610 data->irq_num = einfo->irq_num;
1611 data->id = pdev->id;
1612 netif_napi_add(dev, &data->napi, tsi108_poll, 64);
1613 dev->netdev_ops = &tsi108_netdev_ops;
1614 dev->ethtool_ops = &tsi108_ethtool_ops;
1616 /* Apparently, the Linux networking code won't use scatter-gather
1617 * if the hardware doesn't do checksums. However, it's faster
1618 * to checksum in place and use SG, as (among other reasons)
1619 * the cache won't be dirtied (which then has to be flushed
1620 * before DMA). The checksumming is done by the driver (via
1621 * a new function skb_csum_dev() in net/core/skbuff.c).
1624 dev->features = NETIF_F_HIGHDMA;
1626 spin_lock_init(&data->txlock);
1627 spin_lock_init(&data->misclock);
1629 tsi108_reset_ether(data);
1630 tsi108_kill_phy(dev);
1632 if ((err = tsi108_get_mac(dev)) != 0) {
1633 printk(KERN_ERR "%s: Invalid MAC address. Please correct.\n",
1634 dev->name);
1635 goto register_fail;
1638 tsi108_init_mac(dev);
1639 err = register_netdev(dev);
1640 if (err) {
1641 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
1642 dev->name);
1643 goto register_fail;
1646 platform_set_drvdata(pdev, dev);
1647 printk(KERN_INFO "%s: Tsi108 Gigabit Ethernet, MAC: %pM\n",
1648 dev->name, dev->dev_addr);
1649 #ifdef DEBUG
1650 data->msg_enable = DEBUG;
1651 dump_eth_one(dev);
1652 #endif
1654 return 0;
1656 register_fail:
1657 iounmap(data->phyregs);
1659 phyregs_fail:
1660 iounmap(data->regs);
1662 regs_fail:
1663 free_netdev(dev);
1664 return err;
1667 /* There's no way to either get interrupts from the PHY when
1668 * something changes, or to have the Tsi108 automatically communicate
1669 * with the PHY to reconfigure itself.
1671 * Thus, we have to do it using a timer.
1674 static void tsi108_timed_checker(struct timer_list *t)
1676 struct tsi108_prv_data *data = from_timer(data, t, timer);
1677 struct net_device *dev = data->dev;
1679 tsi108_check_phy(dev);
1680 tsi108_check_rxring(dev);
1681 mod_timer(&data->timer, jiffies + CHECK_PHY_INTERVAL);
1684 static int tsi108_ether_remove(struct platform_device *pdev)
1686 struct net_device *dev = platform_get_drvdata(pdev);
1687 struct tsi108_prv_data *priv = netdev_priv(dev);
1689 unregister_netdev(dev);
1690 tsi108_stop_ethernet(dev);
1691 iounmap(priv->regs);
1692 iounmap(priv->phyregs);
1693 free_netdev(dev);
1695 return 0;
1697 module_platform_driver(tsi_eth_driver);
1699 MODULE_AUTHOR("Tundra Semiconductor Corporation");
1700 MODULE_DESCRIPTION("Tsi108 Gigabit Ethernet driver");
1701 MODULE_LICENSE("GPL");
1702 MODULE_ALIAS("platform:tsi-ethernet");