[NETLINK]: Introduce nlmsg_hdr() helper
[linux-2.6/libata-dev.git] / drivers / net / tsi108_eth.c
blob0bfc2c9c1c083223820102cad66f1e747c3b3e05
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/init.h>
36 #include <linux/net.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/skbuff.h>
40 #include <linux/slab.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/etherdevice.h>
52 #include <asm/system.h>
53 #include <asm/io.h>
54 #include <asm/tsi108.h>
56 #include "tsi108_eth.h"
58 #define MII_READ_DELAY 10000 /* max link wait time in msec */
60 #define TSI108_RXRING_LEN 256
62 /* NOTE: The driver currently does not support receiving packets
63 * larger than the buffer size, so don't decrease this (unless you
64 * want to add such support).
66 #define TSI108_RXBUF_SIZE 1536
68 #define TSI108_TXRING_LEN 256
70 #define TSI108_TX_INT_FREQ 64
72 /* Check the phy status every half a second. */
73 #define CHECK_PHY_INTERVAL (HZ/2)
75 static int tsi108_init_one(struct platform_device *pdev);
76 static int tsi108_ether_remove(struct platform_device *pdev);
78 struct tsi108_prv_data {
79 void __iomem *regs; /* Base of normal regs */
80 void __iomem *phyregs; /* Base of register bank used for PHY access */
82 unsigned int phy; /* Index of PHY for this interface */
83 unsigned int irq_num;
84 unsigned int id;
86 struct timer_list timer;/* Timer that triggers the check phy function */
87 unsigned int rxtail; /* Next entry in rxring to read */
88 unsigned int rxhead; /* Next entry in rxring to give a new buffer */
89 unsigned int rxfree; /* Number of free, allocated RX buffers */
91 unsigned int rxpending; /* Non-zero if there are still descriptors
92 * to be processed from a previous descriptor
93 * interrupt condition that has been cleared */
95 unsigned int txtail; /* Next TX descriptor to check status on */
96 unsigned int txhead; /* Next TX descriptor to use */
98 /* Number of free TX descriptors. This could be calculated from
99 * rxhead and rxtail if one descriptor were left unused to disambiguate
100 * full and empty conditions, but it's simpler to just keep track
101 * explicitly. */
103 unsigned int txfree;
105 unsigned int phy_ok; /* The PHY is currently powered on. */
107 /* PHY status (duplex is 1 for half, 2 for full,
108 * so that the default 0 indicates that neither has
109 * yet been configured). */
111 unsigned int link_up;
112 unsigned int speed;
113 unsigned int duplex;
115 tx_desc *txring;
116 rx_desc *rxring;
117 struct sk_buff *txskbs[TSI108_TXRING_LEN];
118 struct sk_buff *rxskbs[TSI108_RXRING_LEN];
120 dma_addr_t txdma, rxdma;
122 /* txlock nests in misclock and phy_lock */
124 spinlock_t txlock, misclock;
126 /* stats is used to hold the upper bits of each hardware counter,
127 * and tmpstats is used to hold the full values for returning
128 * to the caller of get_stats(). They must be separate in case
129 * an overflow interrupt occurs before the stats are consumed.
132 struct net_device_stats stats;
133 struct net_device_stats tmpstats;
135 /* These stats are kept separate in hardware, thus require individual
136 * fields for handling carry. They are combined in get_stats.
139 unsigned long rx_fcs; /* Add to rx_frame_errors */
140 unsigned long rx_short_fcs; /* Add to rx_frame_errors */
141 unsigned long rx_long_fcs; /* Add to rx_frame_errors */
142 unsigned long rx_underruns; /* Add to rx_length_errors */
143 unsigned long rx_overruns; /* Add to rx_length_errors */
145 unsigned long tx_coll_abort; /* Add to tx_aborted_errors/collisions */
146 unsigned long tx_pause_drop; /* Add to tx_aborted_errors */
148 unsigned long mc_hash[16];
149 u32 msg_enable; /* debug message level */
150 struct mii_if_info mii_if;
151 unsigned int init_media;
154 /* Structure for a device driver */
156 static struct platform_driver tsi_eth_driver = {
157 .probe = tsi108_init_one,
158 .remove = tsi108_ether_remove,
159 .driver = {
160 .name = "tsi-ethernet",
164 static void tsi108_timed_checker(unsigned long dev_ptr);
166 static void dump_eth_one(struct net_device *dev)
168 struct tsi108_prv_data *data = netdev_priv(dev);
170 printk("Dumping %s...\n", dev->name);
171 printk("intstat %x intmask %x phy_ok %d"
172 " link %d speed %d duplex %d\n",
173 TSI_READ(TSI108_EC_INTSTAT),
174 TSI_READ(TSI108_EC_INTMASK), data->phy_ok,
175 data->link_up, data->speed, data->duplex);
177 printk("TX: head %d, tail %d, free %d, stat %x, estat %x, err %x\n",
178 data->txhead, data->txtail, data->txfree,
179 TSI_READ(TSI108_EC_TXSTAT),
180 TSI_READ(TSI108_EC_TXESTAT),
181 TSI_READ(TSI108_EC_TXERR));
183 printk("RX: head %d, tail %d, free %d, stat %x,"
184 " estat %x, err %x, pending %d\n\n",
185 data->rxhead, data->rxtail, data->rxfree,
186 TSI_READ(TSI108_EC_RXSTAT),
187 TSI_READ(TSI108_EC_RXESTAT),
188 TSI_READ(TSI108_EC_RXERR), data->rxpending);
191 /* Synchronization is needed between the thread and up/down events.
192 * Note that the PHY is accessed through the same registers for both
193 * interfaces, so this can't be made interface-specific.
196 static DEFINE_SPINLOCK(phy_lock);
198 static int tsi108_read_mii(struct tsi108_prv_data *data, int reg)
200 unsigned i;
202 TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
203 (data->phy << TSI108_MAC_MII_ADDR_PHY) |
204 (reg << TSI108_MAC_MII_ADDR_REG));
205 TSI_WRITE_PHY(TSI108_MAC_MII_CMD, 0);
206 TSI_WRITE_PHY(TSI108_MAC_MII_CMD, TSI108_MAC_MII_CMD_READ);
207 for (i = 0; i < 100; i++) {
208 if (!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
209 (TSI108_MAC_MII_IND_NOTVALID | TSI108_MAC_MII_IND_BUSY)))
210 break;
211 udelay(10);
214 if (i == 100)
215 return 0xffff;
216 else
217 return (TSI_READ_PHY(TSI108_MAC_MII_DATAIN));
220 static void tsi108_write_mii(struct tsi108_prv_data *data,
221 int reg, u16 val)
223 unsigned i = 100;
224 TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
225 (data->phy << TSI108_MAC_MII_ADDR_PHY) |
226 (reg << TSI108_MAC_MII_ADDR_REG));
227 TSI_WRITE_PHY(TSI108_MAC_MII_DATAOUT, val);
228 while (i--) {
229 if(!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
230 TSI108_MAC_MII_IND_BUSY))
231 break;
232 udelay(10);
236 static int tsi108_mdio_read(struct net_device *dev, int addr, int reg)
238 struct tsi108_prv_data *data = netdev_priv(dev);
239 return tsi108_read_mii(data, reg);
242 static void tsi108_mdio_write(struct net_device *dev, int addr, int reg, int val)
244 struct tsi108_prv_data *data = netdev_priv(dev);
245 tsi108_write_mii(data, reg, val);
248 static inline void tsi108_write_tbi(struct tsi108_prv_data *data,
249 int reg, u16 val)
251 unsigned i = 1000;
252 TSI_WRITE(TSI108_MAC_MII_ADDR,
253 (0x1e << TSI108_MAC_MII_ADDR_PHY)
254 | (reg << TSI108_MAC_MII_ADDR_REG));
255 TSI_WRITE(TSI108_MAC_MII_DATAOUT, val);
256 while(i--) {
257 if(!(TSI_READ(TSI108_MAC_MII_IND) & TSI108_MAC_MII_IND_BUSY))
258 return;
259 udelay(10);
261 printk(KERN_ERR "%s function time out \n", __FUNCTION__);
264 static int mii_speed(struct mii_if_info *mii)
266 int advert, lpa, val, media;
267 int lpa2 = 0;
268 int speed;
270 if (!mii_link_ok(mii))
271 return 0;
273 val = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_BMSR);
274 if ((val & BMSR_ANEGCOMPLETE) == 0)
275 return 0;
277 advert = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_ADVERTISE);
278 lpa = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_LPA);
279 media = mii_nway_result(advert & lpa);
281 if (mii->supports_gmii)
282 lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
284 speed = lpa2 & (LPA_1000FULL | LPA_1000HALF) ? 1000 :
285 (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 100 : 10);
286 return speed;
289 static void tsi108_check_phy(struct net_device *dev)
291 struct tsi108_prv_data *data = netdev_priv(dev);
292 u32 mac_cfg2_reg, portctrl_reg;
293 u32 duplex;
294 u32 speed;
295 unsigned long flags;
297 /* Do a dummy read, as for some reason the first read
298 * after a link becomes up returns link down, even if
299 * it's been a while since the link came up.
302 spin_lock_irqsave(&phy_lock, flags);
304 if (!data->phy_ok)
305 goto out;
307 tsi108_read_mii(data, MII_BMSR);
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);
346 if (data->link_up == 0) {
347 /* The manual says it can take 3-4 usecs for the speed change
348 * to take effect.
350 udelay(5);
352 spin_lock(&data->txlock);
353 if (is_valid_ether_addr(dev->dev_addr) && data->txfree)
354 netif_wake_queue(dev);
356 data->link_up = 1;
357 spin_unlock(&data->txlock);
361 } else {
362 if (data->link_up == 1) {
363 netif_stop_queue(dev);
364 data->link_up = 0;
365 printk(KERN_NOTICE "%s : link is down\n", dev->name);
368 goto out;
372 out:
373 spin_unlock_irqrestore(&phy_lock, flags);
376 static inline void
377 tsi108_stat_carry_one(int carry, int carry_bit, int carry_shift,
378 unsigned long *upper)
380 if (carry & carry_bit)
381 *upper += carry_shift;
384 static void tsi108_stat_carry(struct net_device *dev)
386 struct tsi108_prv_data *data = netdev_priv(dev);
387 u32 carry1, carry2;
389 spin_lock_irq(&data->misclock);
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_irq(&data->misclock);
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(NULL, skb->data,
710 skb->len - skb->data_len, DMA_TO_DEVICE);
711 data->txring[tx].len = skb->len - skb->data_len;
712 misc |= TSI108_TX_SOF;
713 } else {
714 skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
716 data->txring[tx].buf0 =
717 dma_map_page(NULL, frag->page, frag->page_offset,
718 frag->size, DMA_TO_DEVICE);
719 data->txring[tx].len = frag->size;
722 if (i == frags - 1)
723 misc |= TSI108_TX_EOF;
725 if (netif_msg_pktdata(data)) {
726 int i;
727 printk("%s: Tx Frame contents (%d)\n", dev->name,
728 skb->len);
729 for (i = 0; i < skb->len; i++)
730 printk(" %2.2x", skb->data[i]);
731 printk(".\n");
733 data->txring[tx].misc = misc | TSI108_TX_OWN;
735 data->txhead = (data->txhead + 1) % TSI108_TXRING_LEN;
736 data->txfree--;
739 tsi108_complete_tx(dev);
741 /* This must be done after the check for completed tx descriptors,
742 * so that the tail pointer is correct.
745 if (!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_QUEUE0))
746 tsi108_restart_tx(data);
748 spin_unlock_irq(&data->txlock);
749 return NETDEV_TX_OK;
752 static int tsi108_complete_rx(struct net_device *dev, int budget)
754 struct tsi108_prv_data *data = netdev_priv(dev);
755 int done = 0;
757 while (data->rxfree && done != budget) {
758 int rx = data->rxtail;
759 struct sk_buff *skb;
761 if (data->rxring[rx].misc & TSI108_RX_OWN)
762 break;
764 skb = data->rxskbs[rx];
765 data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
766 data->rxfree--;
767 done++;
769 if (data->rxring[rx].misc & TSI108_RX_BAD) {
770 spin_lock_irq(&data->misclock);
772 if (data->rxring[rx].misc & TSI108_RX_CRC)
773 data->stats.rx_crc_errors++;
774 if (data->rxring[rx].misc & TSI108_RX_OVER)
775 data->stats.rx_fifo_errors++;
777 spin_unlock_irq(&data->misclock);
779 dev_kfree_skb_any(skb);
780 continue;
782 if (netif_msg_pktdata(data)) {
783 int i;
784 printk("%s: Rx Frame contents (%d)\n",
785 dev->name, data->rxring[rx].len);
786 for (i = 0; i < data->rxring[rx].len; i++)
787 printk(" %2.2x", skb->data[i]);
788 printk(".\n");
791 skb_put(skb, data->rxring[rx].len);
792 skb->protocol = eth_type_trans(skb, dev);
793 netif_receive_skb(skb);
794 dev->last_rx = jiffies;
797 return done;
800 static int tsi108_refill_rx(struct net_device *dev, int budget)
802 struct tsi108_prv_data *data = netdev_priv(dev);
803 int done = 0;
805 while (data->rxfree != TSI108_RXRING_LEN && done != budget) {
806 int rx = data->rxhead;
807 struct sk_buff *skb;
809 data->rxskbs[rx] = skb = dev_alloc_skb(TSI108_RXBUF_SIZE + 2);
810 if (!skb)
811 break;
813 skb_reserve(skb, 2); /* Align the data on a 4-byte boundary. */
815 data->rxring[rx].buf0 = dma_map_single(NULL, skb->data,
816 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 net_device *dev, int *budget)
841 struct tsi108_prv_data *data = netdev_priv(dev);
842 u32 estat = TSI_READ(TSI108_EC_RXESTAT);
843 u32 intstat = TSI_READ(TSI108_EC_INTSTAT);
844 int total_budget = min(*budget, dev->quota);
845 int num_received = 0, num_filled = 0, budget_used;
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, total_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, total_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 budget_used = max(num_received, num_filled / 2);
894 *budget -= budget_used;
895 dev->quota -= budget_used;
897 if (budget_used != total_budget) {
898 data->rxpending = 0;
899 netif_rx_complete(dev);
901 TSI_WRITE(TSI108_EC_INTMASK,
902 TSI_READ(TSI108_EC_INTMASK)
903 & ~(TSI108_INT_RXQUEUE0
904 | TSI108_INT_RXTHRESH |
905 TSI108_INT_RXOVERRUN |
906 TSI108_INT_RXERROR |
907 TSI108_INT_RXWAIT));
909 /* IRQs are level-triggered, so no need to re-check */
910 return 0;
911 } else {
912 data->rxpending = 1;
915 return 1;
918 static void tsi108_rx_int(struct net_device *dev)
920 struct tsi108_prv_data *data = netdev_priv(dev);
922 /* A race could cause dev to already be scheduled, so it's not an
923 * error if that happens (and interrupts shouldn't be re-masked,
924 * because that can cause harmful races, if poll has already
925 * unmasked them but not cleared LINK_STATE_SCHED).
927 * This can happen if this code races with tsi108_poll(), which masks
928 * the interrupts after tsi108_irq_one() read the mask, but before
929 * netif_rx_schedule is called. It could also happen due to calls
930 * from tsi108_check_rxring().
933 if (netif_rx_schedule_prep(dev)) {
934 /* Mask, rather than ack, the receive interrupts. The ack
935 * will happen in tsi108_poll().
938 TSI_WRITE(TSI108_EC_INTMASK,
939 TSI_READ(TSI108_EC_INTMASK) |
940 TSI108_INT_RXQUEUE0
941 | TSI108_INT_RXTHRESH |
942 TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR |
943 TSI108_INT_RXWAIT);
944 __netif_rx_schedule(dev);
945 } else {
946 if (!netif_running(dev)) {
947 /* This can happen if an interrupt occurs while the
948 * interface is being brought down, as the START
949 * bit is cleared before the stop function is called.
951 * In this case, the interrupts must be masked, or
952 * they will continue indefinitely.
954 * There's a race here if the interface is brought down
955 * and then up in rapid succession, as the device could
956 * be made running after the above check and before
957 * the masking below. This will only happen if the IRQ
958 * thread has a lower priority than the task brining
959 * up the interface. Fixing this race would likely
960 * require changes in generic code.
963 TSI_WRITE(TSI108_EC_INTMASK,
964 TSI_READ
965 (TSI108_EC_INTMASK) |
966 TSI108_INT_RXQUEUE0 |
967 TSI108_INT_RXTHRESH |
968 TSI108_INT_RXOVERRUN |
969 TSI108_INT_RXERROR |
970 TSI108_INT_RXWAIT);
975 /* If the RX ring has run out of memory, try periodically
976 * to allocate some more, as otherwise poll would never
977 * get called (apart from the initial end-of-queue condition).
979 * This is called once per second (by default) from the thread.
982 static void tsi108_check_rxring(struct net_device *dev)
984 struct tsi108_prv_data *data = netdev_priv(dev);
986 /* A poll is scheduled, as opposed to caling tsi108_refill_rx
987 * directly, so as to keep the receive path single-threaded
988 * (and thus not needing a lock).
991 if (netif_running(dev) && data->rxfree < TSI108_RXRING_LEN / 4)
992 tsi108_rx_int(dev);
995 static void tsi108_tx_int(struct net_device *dev)
997 struct tsi108_prv_data *data = netdev_priv(dev);
998 u32 estat = TSI_READ(TSI108_EC_TXESTAT);
1000 TSI_WRITE(TSI108_EC_TXESTAT, estat);
1001 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_TXQUEUE0 |
1002 TSI108_INT_TXIDLE | TSI108_INT_TXERROR);
1003 if (estat & TSI108_EC_TXESTAT_Q0_ERR) {
1004 u32 err = TSI_READ(TSI108_EC_TXERR);
1005 TSI_WRITE(TSI108_EC_TXERR, err);
1007 if (err && net_ratelimit())
1008 printk(KERN_ERR "%s: TX error %x\n", dev->name, err);
1011 if (estat & (TSI108_EC_TXESTAT_Q0_DESCINT | TSI108_EC_TXESTAT_Q0_EOQ)) {
1012 spin_lock(&data->txlock);
1013 tsi108_complete_tx(dev);
1014 spin_unlock(&data->txlock);
1019 static irqreturn_t tsi108_irq(int irq, void *dev_id)
1021 struct net_device *dev = dev_id;
1022 struct tsi108_prv_data *data = netdev_priv(dev);
1023 u32 stat = TSI_READ(TSI108_EC_INTSTAT);
1025 if (!(stat & TSI108_INT_ANY))
1026 return IRQ_NONE; /* Not our interrupt */
1028 stat &= ~TSI_READ(TSI108_EC_INTMASK);
1030 if (stat & (TSI108_INT_TXQUEUE0 | TSI108_INT_TXIDLE |
1031 TSI108_INT_TXERROR))
1032 tsi108_tx_int(dev);
1033 if (stat & (TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
1034 TSI108_INT_RXWAIT | TSI108_INT_RXOVERRUN |
1035 TSI108_INT_RXERROR))
1036 tsi108_rx_int(dev);
1038 if (stat & TSI108_INT_SFN) {
1039 if (net_ratelimit())
1040 printk(KERN_DEBUG "%s: SFN error\n", dev->name);
1041 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_SFN);
1044 if (stat & TSI108_INT_STATCARRY) {
1045 tsi108_stat_carry(dev);
1046 TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_STATCARRY);
1049 return IRQ_HANDLED;
1052 static void tsi108_stop_ethernet(struct net_device *dev)
1054 struct tsi108_prv_data *data = netdev_priv(dev);
1055 int i = 1000;
1056 /* Disable all TX and RX queues ... */
1057 TSI_WRITE(TSI108_EC_TXCTRL, 0);
1058 TSI_WRITE(TSI108_EC_RXCTRL, 0);
1060 /* ...and wait for them to become idle */
1061 while(i--) {
1062 if(!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_ACTIVE))
1063 break;
1064 udelay(10);
1066 i = 1000;
1067 while(i--){
1068 if(!(TSI_READ(TSI108_EC_RXSTAT) & TSI108_EC_RXSTAT_ACTIVE))
1069 return;
1070 udelay(10);
1072 printk(KERN_ERR "%s function time out \n", __FUNCTION__);
1075 static void tsi108_reset_ether(struct tsi108_prv_data * data)
1077 TSI_WRITE(TSI108_MAC_CFG1, TSI108_MAC_CFG1_SOFTRST);
1078 udelay(100);
1079 TSI_WRITE(TSI108_MAC_CFG1, 0);
1081 TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATRST);
1082 udelay(100);
1083 TSI_WRITE(TSI108_EC_PORTCTRL,
1084 TSI_READ(TSI108_EC_PORTCTRL) &
1085 ~TSI108_EC_PORTCTRL_STATRST);
1087 TSI_WRITE(TSI108_EC_TXCFG, TSI108_EC_TXCFG_RST);
1088 udelay(100);
1089 TSI_WRITE(TSI108_EC_TXCFG,
1090 TSI_READ(TSI108_EC_TXCFG) &
1091 ~TSI108_EC_TXCFG_RST);
1093 TSI_WRITE(TSI108_EC_RXCFG, TSI108_EC_RXCFG_RST);
1094 udelay(100);
1095 TSI_WRITE(TSI108_EC_RXCFG,
1096 TSI_READ(TSI108_EC_RXCFG) &
1097 ~TSI108_EC_RXCFG_RST);
1099 TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
1100 TSI_READ(TSI108_MAC_MII_MGMT_CFG) |
1101 TSI108_MAC_MII_MGMT_RST);
1102 udelay(100);
1103 TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
1104 (TSI_READ(TSI108_MAC_MII_MGMT_CFG) &
1105 ~(TSI108_MAC_MII_MGMT_RST |
1106 TSI108_MAC_MII_MGMT_CLK)) | 0x07);
1109 static int tsi108_get_mac(struct net_device *dev)
1111 struct tsi108_prv_data *data = netdev_priv(dev);
1112 u32 word1 = TSI_READ(TSI108_MAC_ADDR1);
1113 u32 word2 = TSI_READ(TSI108_MAC_ADDR2);
1115 /* Note that the octets are reversed from what the manual says,
1116 * producing an even weirder ordering...
1118 if (word2 == 0 && word1 == 0) {
1119 dev->dev_addr[0] = 0x00;
1120 dev->dev_addr[1] = 0x06;
1121 dev->dev_addr[2] = 0xd2;
1122 dev->dev_addr[3] = 0x00;
1123 dev->dev_addr[4] = 0x00;
1124 if (0x8 == data->phy)
1125 dev->dev_addr[5] = 0x01;
1126 else
1127 dev->dev_addr[5] = 0x02;
1129 word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
1131 word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
1132 (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
1134 TSI_WRITE(TSI108_MAC_ADDR1, word1);
1135 TSI_WRITE(TSI108_MAC_ADDR2, word2);
1136 } else {
1137 dev->dev_addr[0] = (word2 >> 16) & 0xff;
1138 dev->dev_addr[1] = (word2 >> 24) & 0xff;
1139 dev->dev_addr[2] = (word1 >> 0) & 0xff;
1140 dev->dev_addr[3] = (word1 >> 8) & 0xff;
1141 dev->dev_addr[4] = (word1 >> 16) & 0xff;
1142 dev->dev_addr[5] = (word1 >> 24) & 0xff;
1145 if (!is_valid_ether_addr(dev->dev_addr)) {
1146 printk("KERN_ERR: word1: %08x, word2: %08x\n", word1, word2);
1147 return -EINVAL;
1150 return 0;
1153 static int tsi108_set_mac(struct net_device *dev, void *addr)
1155 struct tsi108_prv_data *data = netdev_priv(dev);
1156 u32 word1, word2;
1157 int i;
1159 if (!is_valid_ether_addr(addr))
1160 return -EINVAL;
1162 for (i = 0; i < 6; i++)
1163 /* +2 is for the offset of the HW addr type */
1164 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1166 word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
1168 word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
1169 (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
1171 spin_lock_irq(&data->misclock);
1172 TSI_WRITE(TSI108_MAC_ADDR1, word1);
1173 TSI_WRITE(TSI108_MAC_ADDR2, word2);
1174 spin_lock(&data->txlock);
1176 if (data->txfree && data->link_up)
1177 netif_wake_queue(dev);
1179 spin_unlock(&data->txlock);
1180 spin_unlock_irq(&data->misclock);
1181 return 0;
1184 /* Protected by dev->xmit_lock. */
1185 static void tsi108_set_rx_mode(struct net_device *dev)
1187 struct tsi108_prv_data *data = netdev_priv(dev);
1188 u32 rxcfg = TSI_READ(TSI108_EC_RXCFG);
1190 if (dev->flags & IFF_PROMISC) {
1191 rxcfg &= ~(TSI108_EC_RXCFG_UC_HASH | TSI108_EC_RXCFG_MC_HASH);
1192 rxcfg |= TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE;
1193 goto out;
1196 rxcfg &= ~(TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE);
1198 if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
1199 int i;
1200 struct dev_mc_list *mc = dev->mc_list;
1201 rxcfg |= TSI108_EC_RXCFG_MFE | TSI108_EC_RXCFG_MC_HASH;
1203 memset(data->mc_hash, 0, sizeof(data->mc_hash));
1205 while (mc) {
1206 u32 hash, crc;
1208 if (mc->dmi_addrlen == 6) {
1209 crc = ether_crc(6, mc->dmi_addr);
1210 hash = crc >> 23;
1212 __set_bit(hash, &data->mc_hash[0]);
1213 } else {
1214 printk(KERN_ERR
1215 "%s: got multicast address of length %d "
1216 "instead of 6.\n", dev->name,
1217 mc->dmi_addrlen);
1220 mc = mc->next;
1223 TSI_WRITE(TSI108_EC_HASHADDR,
1224 TSI108_EC_HASHADDR_AUTOINC |
1225 TSI108_EC_HASHADDR_MCAST);
1227 for (i = 0; i < 16; i++) {
1228 /* The manual says that the hardware may drop
1229 * back-to-back writes to the data register.
1231 udelay(1);
1232 TSI_WRITE(TSI108_EC_HASHDATA,
1233 data->mc_hash[i]);
1237 out:
1238 TSI_WRITE(TSI108_EC_RXCFG, rxcfg);
1241 static void tsi108_init_phy(struct net_device *dev)
1243 struct tsi108_prv_data *data = netdev_priv(dev);
1244 u32 i = 0;
1245 u16 phyval = 0;
1246 unsigned long flags;
1248 spin_lock_irqsave(&phy_lock, flags);
1250 tsi108_write_mii(data, MII_BMCR, BMCR_RESET);
1251 while (i--){
1252 if(!(tsi108_read_mii(data, MII_BMCR) & BMCR_RESET))
1253 break;
1254 udelay(10);
1256 if (i == 0)
1257 printk(KERN_ERR "%s function time out \n", __FUNCTION__);
1259 #if (TSI108_PHY_TYPE == PHY_BCM54XX) /* Broadcom BCM54xx PHY */
1260 tsi108_write_mii(data, 0x09, 0x0300);
1261 tsi108_write_mii(data, 0x10, 0x1020);
1262 tsi108_write_mii(data, 0x1c, 0x8c00);
1263 #endif
1265 tsi108_write_mii(data,
1266 MII_BMCR,
1267 BMCR_ANENABLE | BMCR_ANRESTART);
1268 while (tsi108_read_mii(data, MII_BMCR) & BMCR_ANRESTART)
1269 cpu_relax();
1271 /* Set G/MII mode and receive clock select in TBI control #2. The
1272 * second port won't work if this isn't done, even though we don't
1273 * use TBI mode.
1276 tsi108_write_tbi(data, 0x11, 0x30);
1278 /* FIXME: It seems to take more than 2 back-to-back reads to the
1279 * PHY_STAT register before the link up status bit is set.
1282 data->link_up = 1;
1284 while (!((phyval = tsi108_read_mii(data, MII_BMSR)) &
1285 BMSR_LSTATUS)) {
1286 if (i++ > (MII_READ_DELAY / 10)) {
1287 data->link_up = 0;
1288 break;
1290 spin_unlock_irqrestore(&phy_lock, flags);
1291 msleep(10);
1292 spin_lock_irqsave(&phy_lock, flags);
1295 printk(KERN_DEBUG "PHY_STAT reg contains %08x\n", phyval);
1296 data->phy_ok = 1;
1297 data->init_media = 1;
1298 spin_unlock_irqrestore(&phy_lock, flags);
1301 static void tsi108_kill_phy(struct net_device *dev)
1303 struct tsi108_prv_data *data = netdev_priv(dev);
1304 unsigned long flags;
1306 spin_lock_irqsave(&phy_lock, flags);
1307 tsi108_write_mii(data, MII_BMCR, BMCR_PDOWN);
1308 data->phy_ok = 0;
1309 spin_unlock_irqrestore(&phy_lock, flags);
1312 static int tsi108_open(struct net_device *dev)
1314 int i;
1315 struct tsi108_prv_data *data = netdev_priv(dev);
1316 unsigned int rxring_size = TSI108_RXRING_LEN * sizeof(rx_desc);
1317 unsigned int txring_size = TSI108_TXRING_LEN * sizeof(tx_desc);
1319 i = request_irq(data->irq_num, tsi108_irq, 0, dev->name, dev);
1320 if (i != 0) {
1321 printk(KERN_ERR "tsi108_eth%d: Could not allocate IRQ%d.\n",
1322 data->id, data->irq_num);
1323 return i;
1324 } else {
1325 dev->irq = data->irq_num;
1326 printk(KERN_NOTICE
1327 "tsi108_open : Port %d Assigned IRQ %d to %s\n",
1328 data->id, dev->irq, dev->name);
1331 data->rxring = dma_alloc_coherent(NULL, rxring_size,
1332 &data->rxdma, GFP_KERNEL);
1334 if (!data->rxring) {
1335 printk(KERN_DEBUG
1336 "TSI108_ETH: failed to allocate memory for rxring!\n");
1337 return -ENOMEM;
1338 } else {
1339 memset(data->rxring, 0, rxring_size);
1342 data->txring = dma_alloc_coherent(NULL, txring_size,
1343 &data->txdma, GFP_KERNEL);
1345 if (!data->txring) {
1346 printk(KERN_DEBUG
1347 "TSI108_ETH: failed to allocate memory for txring!\n");
1348 pci_free_consistent(0, rxring_size, data->rxring, data->rxdma);
1349 return -ENOMEM;
1350 } else {
1351 memset(data->txring, 0, txring_size);
1354 for (i = 0; i < TSI108_RXRING_LEN; i++) {
1355 data->rxring[i].next0 = data->rxdma + (i + 1) * sizeof(rx_desc);
1356 data->rxring[i].blen = TSI108_RXBUF_SIZE;
1357 data->rxring[i].vlan = 0;
1360 data->rxring[TSI108_RXRING_LEN - 1].next0 = data->rxdma;
1362 data->rxtail = 0;
1363 data->rxhead = 0;
1365 for (i = 0; i < TSI108_RXRING_LEN; i++) {
1366 struct sk_buff *skb = dev_alloc_skb(TSI108_RXBUF_SIZE + NET_IP_ALIGN);
1368 if (!skb) {
1369 /* Bah. No memory for now, but maybe we'll get
1370 * some more later.
1371 * For now, we'll live with the smaller ring.
1373 printk(KERN_WARNING
1374 "%s: Could only allocate %d receive skb(s).\n",
1375 dev->name, i);
1376 data->rxhead = i;
1377 break;
1380 data->rxskbs[i] = skb;
1381 /* Align the payload on a 4-byte boundary */
1382 skb_reserve(skb, 2);
1383 data->rxskbs[i] = skb;
1384 data->rxring[i].buf0 = virt_to_phys(data->rxskbs[i]->data);
1385 data->rxring[i].misc = TSI108_RX_OWN | TSI108_RX_INT;
1388 data->rxfree = i;
1389 TSI_WRITE(TSI108_EC_RXQ_PTRLOW, data->rxdma);
1391 for (i = 0; i < TSI108_TXRING_LEN; i++) {
1392 data->txring[i].next0 = data->txdma + (i + 1) * sizeof(tx_desc);
1393 data->txring[i].misc = 0;
1396 data->txring[TSI108_TXRING_LEN - 1].next0 = data->txdma;
1397 data->txtail = 0;
1398 data->txhead = 0;
1399 data->txfree = TSI108_TXRING_LEN;
1400 TSI_WRITE(TSI108_EC_TXQ_PTRLOW, data->txdma);
1401 tsi108_init_phy(dev);
1403 setup_timer(&data->timer, tsi108_timed_checker, (unsigned long)dev);
1404 mod_timer(&data->timer, jiffies + 1);
1406 tsi108_restart_rx(data, dev);
1408 TSI_WRITE(TSI108_EC_INTSTAT, ~0);
1410 TSI_WRITE(TSI108_EC_INTMASK,
1411 ~(TSI108_INT_TXQUEUE0 | TSI108_INT_RXERROR |
1412 TSI108_INT_RXTHRESH | TSI108_INT_RXQUEUE0 |
1413 TSI108_INT_RXOVERRUN | TSI108_INT_RXWAIT |
1414 TSI108_INT_SFN | TSI108_INT_STATCARRY));
1416 TSI_WRITE(TSI108_MAC_CFG1,
1417 TSI108_MAC_CFG1_RXEN | TSI108_MAC_CFG1_TXEN);
1418 netif_start_queue(dev);
1419 return 0;
1422 static int tsi108_close(struct net_device *dev)
1424 struct tsi108_prv_data *data = netdev_priv(dev);
1426 netif_stop_queue(dev);
1428 del_timer_sync(&data->timer);
1430 tsi108_stop_ethernet(dev);
1431 tsi108_kill_phy(dev);
1432 TSI_WRITE(TSI108_EC_INTMASK, ~0);
1433 TSI_WRITE(TSI108_MAC_CFG1, 0);
1435 /* Check for any pending TX packets, and drop them. */
1437 while (!data->txfree || data->txhead != data->txtail) {
1438 int tx = data->txtail;
1439 struct sk_buff *skb;
1440 skb = data->txskbs[tx];
1441 data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
1442 data->txfree++;
1443 dev_kfree_skb(skb);
1446 synchronize_irq(data->irq_num);
1447 free_irq(data->irq_num, dev);
1449 /* Discard the RX ring. */
1451 while (data->rxfree) {
1452 int rx = data->rxtail;
1453 struct sk_buff *skb;
1455 skb = data->rxskbs[rx];
1456 data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
1457 data->rxfree--;
1458 dev_kfree_skb(skb);
1461 dma_free_coherent(0,
1462 TSI108_RXRING_LEN * sizeof(rx_desc),
1463 data->rxring, data->rxdma);
1464 dma_free_coherent(0,
1465 TSI108_TXRING_LEN * sizeof(tx_desc),
1466 data->txring, data->txdma);
1468 return 0;
1471 static void tsi108_init_mac(struct net_device *dev)
1473 struct tsi108_prv_data *data = netdev_priv(dev);
1475 TSI_WRITE(TSI108_MAC_CFG2, TSI108_MAC_CFG2_DFLT_PREAMBLE |
1476 TSI108_MAC_CFG2_PADCRC);
1478 TSI_WRITE(TSI108_EC_TXTHRESH,
1479 (192 << TSI108_EC_TXTHRESH_STARTFILL) |
1480 (192 << TSI108_EC_TXTHRESH_STOPFILL));
1482 TSI_WRITE(TSI108_STAT_CARRYMASK1,
1483 ~(TSI108_STAT_CARRY1_RXBYTES |
1484 TSI108_STAT_CARRY1_RXPKTS |
1485 TSI108_STAT_CARRY1_RXFCS |
1486 TSI108_STAT_CARRY1_RXMCAST |
1487 TSI108_STAT_CARRY1_RXALIGN |
1488 TSI108_STAT_CARRY1_RXLENGTH |
1489 TSI108_STAT_CARRY1_RXRUNT |
1490 TSI108_STAT_CARRY1_RXJUMBO |
1491 TSI108_STAT_CARRY1_RXFRAG |
1492 TSI108_STAT_CARRY1_RXJABBER |
1493 TSI108_STAT_CARRY1_RXDROP));
1495 TSI_WRITE(TSI108_STAT_CARRYMASK2,
1496 ~(TSI108_STAT_CARRY2_TXBYTES |
1497 TSI108_STAT_CARRY2_TXPKTS |
1498 TSI108_STAT_CARRY2_TXEXDEF |
1499 TSI108_STAT_CARRY2_TXEXCOL |
1500 TSI108_STAT_CARRY2_TXTCOL |
1501 TSI108_STAT_CARRY2_TXPAUSE));
1503 TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATEN);
1504 TSI_WRITE(TSI108_MAC_CFG1, 0);
1506 TSI_WRITE(TSI108_EC_RXCFG,
1507 TSI108_EC_RXCFG_SE | TSI108_EC_RXCFG_BFE);
1509 TSI_WRITE(TSI108_EC_TXQ_CFG, TSI108_EC_TXQ_CFG_DESC_INT |
1510 TSI108_EC_TXQ_CFG_EOQ_OWN_INT |
1511 TSI108_EC_TXQ_CFG_WSWP | (TSI108_PBM_PORT <<
1512 TSI108_EC_TXQ_CFG_SFNPORT));
1514 TSI_WRITE(TSI108_EC_RXQ_CFG, TSI108_EC_RXQ_CFG_DESC_INT |
1515 TSI108_EC_RXQ_CFG_EOQ_OWN_INT |
1516 TSI108_EC_RXQ_CFG_WSWP | (TSI108_PBM_PORT <<
1517 TSI108_EC_RXQ_CFG_SFNPORT));
1519 TSI_WRITE(TSI108_EC_TXQ_BUFCFG,
1520 TSI108_EC_TXQ_BUFCFG_BURST256 |
1521 TSI108_EC_TXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
1522 TSI108_EC_TXQ_BUFCFG_SFNPORT));
1524 TSI_WRITE(TSI108_EC_RXQ_BUFCFG,
1525 TSI108_EC_RXQ_BUFCFG_BURST256 |
1526 TSI108_EC_RXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
1527 TSI108_EC_RXQ_BUFCFG_SFNPORT));
1529 TSI_WRITE(TSI108_EC_INTMASK, ~0);
1532 static int tsi108_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1534 struct tsi108_prv_data *data = netdev_priv(dev);
1535 return generic_mii_ioctl(&data->mii_if, if_mii(rq), cmd, NULL);
1538 static int
1539 tsi108_init_one(struct platform_device *pdev)
1541 struct net_device *dev = NULL;
1542 struct tsi108_prv_data *data = NULL;
1543 hw_info *einfo;
1544 int err = 0;
1546 einfo = pdev->dev.platform_data;
1548 if (NULL == einfo) {
1549 printk(KERN_ERR "tsi-eth %d: Missing additional data!\n",
1550 pdev->id);
1551 return -ENODEV;
1554 /* Create an ethernet device instance */
1556 dev = alloc_etherdev(sizeof(struct tsi108_prv_data));
1557 if (!dev) {
1558 printk("tsi108_eth: Could not allocate a device structure\n");
1559 return -ENOMEM;
1562 printk("tsi108_eth%d: probe...\n", pdev->id);
1563 data = netdev_priv(dev);
1565 pr_debug("tsi108_eth%d:regs:phyresgs:phy:irq_num=0x%x:0x%x:0x%x:0x%x\n",
1566 pdev->id, einfo->regs, einfo->phyregs,
1567 einfo->phy, einfo->irq_num);
1569 data->regs = ioremap(einfo->regs, 0x400);
1570 if (NULL == data->regs) {
1571 err = -ENOMEM;
1572 goto regs_fail;
1575 data->phyregs = ioremap(einfo->phyregs, 0x400);
1576 if (NULL == data->phyregs) {
1577 err = -ENOMEM;
1578 goto regs_fail;
1580 /* MII setup */
1581 data->mii_if.dev = dev;
1582 data->mii_if.mdio_read = tsi108_mdio_read;
1583 data->mii_if.mdio_write = tsi108_mdio_write;
1584 data->mii_if.phy_id = einfo->phy;
1585 data->mii_if.phy_id_mask = 0x1f;
1586 data->mii_if.reg_num_mask = 0x1f;
1587 data->mii_if.supports_gmii = mii_check_gmii_support(&data->mii_if);
1589 data->phy = einfo->phy;
1590 data->irq_num = einfo->irq_num;
1591 data->id = pdev->id;
1592 dev->open = tsi108_open;
1593 dev->stop = tsi108_close;
1594 dev->hard_start_xmit = tsi108_send_packet;
1595 dev->set_mac_address = tsi108_set_mac;
1596 dev->set_multicast_list = tsi108_set_rx_mode;
1597 dev->get_stats = tsi108_get_stats;
1598 dev->poll = tsi108_poll;
1599 dev->do_ioctl = tsi108_do_ioctl;
1600 dev->weight = 64; /* 64 is more suitable for GigE interface - klai */
1602 /* Apparently, the Linux networking code won't use scatter-gather
1603 * if the hardware doesn't do checksums. However, it's faster
1604 * to checksum in place and use SG, as (among other reasons)
1605 * the cache won't be dirtied (which then has to be flushed
1606 * before DMA). The checksumming is done by the driver (via
1607 * a new function skb_csum_dev() in net/core/skbuff.c).
1610 dev->features = NETIF_F_HIGHDMA;
1611 SET_MODULE_OWNER(dev);
1613 spin_lock_init(&data->txlock);
1614 spin_lock_init(&data->misclock);
1616 tsi108_reset_ether(data);
1617 tsi108_kill_phy(dev);
1619 if ((err = tsi108_get_mac(dev)) != 0) {
1620 printk(KERN_ERR "%s: Invalid MAC address. Please correct.\n",
1621 dev->name);
1622 goto register_fail;
1625 tsi108_init_mac(dev);
1626 err = register_netdev(dev);
1627 if (err) {
1628 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
1629 dev->name);
1630 goto register_fail;
1633 printk(KERN_INFO "%s: Tsi108 Gigabit Ethernet, MAC: "
1634 "%02x:%02x:%02x:%02x:%02x:%02x\n", dev->name,
1635 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1636 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1637 #ifdef DEBUG
1638 data->msg_enable = DEBUG;
1639 dump_eth_one(dev);
1640 #endif
1642 return 0;
1644 register_fail:
1645 iounmap(data->regs);
1646 iounmap(data->phyregs);
1648 regs_fail:
1649 free_netdev(dev);
1650 return err;
1653 /* There's no way to either get interrupts from the PHY when
1654 * something changes, or to have the Tsi108 automatically communicate
1655 * with the PHY to reconfigure itself.
1657 * Thus, we have to do it using a timer.
1660 static void tsi108_timed_checker(unsigned long dev_ptr)
1662 struct net_device *dev = (struct net_device *)dev_ptr;
1663 struct tsi108_prv_data *data = netdev_priv(dev);
1665 tsi108_check_phy(dev);
1666 tsi108_check_rxring(dev);
1667 mod_timer(&data->timer, jiffies + CHECK_PHY_INTERVAL);
1670 static int tsi108_ether_init(void)
1672 int ret;
1673 ret = platform_driver_register (&tsi_eth_driver);
1674 if (ret < 0){
1675 printk("tsi108_ether_init: error initializing ethernet "
1676 "device\n");
1677 return ret;
1679 return 0;
1682 static int tsi108_ether_remove(struct platform_device *pdev)
1684 struct net_device *dev = platform_get_drvdata(pdev);
1685 struct tsi108_prv_data *priv = netdev_priv(dev);
1687 unregister_netdev(dev);
1688 tsi108_stop_ethernet(dev);
1689 platform_set_drvdata(pdev, NULL);
1690 iounmap(priv->regs);
1691 iounmap(priv->phyregs);
1692 free_netdev(dev);
1694 return 0;
1696 static void tsi108_ether_exit(void)
1698 platform_driver_unregister(&tsi_eth_driver);
1701 module_init(tsi108_ether_init);
1702 module_exit(tsi108_ether_exit);
1704 MODULE_AUTHOR("Tundra Semiconductor Corporation");
1705 MODULE_DESCRIPTION("Tsi108 Gigabit Ethernet driver");
1706 MODULE_LICENSE("GPL");