1 /* drivers/net/ks8851.c
3 * Copyright 2009 Simtec Electronics
4 * http://www.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ethtool.h>
21 #include <linux/cache.h>
22 #include <linux/crc32.h>
23 #include <linux/mii.h>
25 #include <linux/spi/spi.h>
30 * struct ks8851_rxctrl - KS8851 driver rx control
31 * @mchash: Multicast hash-table data.
32 * @rxcr1: KS_RXCR1 register setting
33 * @rxcr2: KS_RXCR2 register setting
35 * Representation of the settings needs to control the receive filtering
36 * such as the multicast hash-filter and the receive register settings. This
37 * is used to make the job of working out if the receive settings change and
38 * then issuing the new settings to the worker that will send the necessary
41 struct ks8851_rxctrl
{
48 * union ks8851_tx_hdr - tx header data
49 * @txb: The header as bytes
50 * @txw: The header as 16bit, little-endian words
52 * A dual representation of the tx header data to allow
53 * access to individual bytes, and to allow 16bit accesses
54 * with 16bit alignment.
62 * struct ks8851_net - KS8851 driver private data
63 * @netdev: The network device we're bound to
64 * @spidev: The spi device we're bound to.
65 * @lock: Lock to ensure that the device is not accessed when busy.
66 * @statelock: Lock on this structure for tx list.
67 * @mii: The MII state information for the mii calls.
68 * @rxctrl: RX settings for @rxctrl_work.
69 * @tx_work: Work queue for tx packets
70 * @irq_work: Work queue for servicing interrupts
71 * @rxctrl_work: Work queue for updating RX mode and multicast lists
72 * @txq: Queue of packets for transmission.
73 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
74 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
75 * @txh: Space for generating packet TX header in DMA-able data
76 * @rxd: Space for receiving SPI data, in DMA-able space.
77 * @txd: Space for transmitting SPI data, in DMA-able space.
78 * @msg_enable: The message flags controlling driver output (see ethtool).
79 * @fid: Incrementing frame id tag.
80 * @rc_ier: Cached copy of KS_IER.
81 * @rc_rxqcr: Cached copy of KS_RXQCR.
83 * The @lock ensures that the chip is protected when certain operations are
84 * in progress. When the read or write packet transfer is in progress, most
85 * of the chip registers are not ccessible until the transfer is finished and
86 * the DMA has been de-asserted.
88 * The @statelock is used to protect information in the structure which may
89 * need to be accessed via several sources, such as the network driver layer
90 * or one of the work queues.
92 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
93 * wants to DMA map them, it will not have any problems with data the driver
97 struct net_device
*netdev
;
98 struct spi_device
*spidev
;
100 spinlock_t statelock
;
102 union ks8851_tx_hdr txh ____cacheline_aligned
;
106 u32 msg_enable ____cacheline_aligned
;
113 struct mii_if_info mii
;
114 struct ks8851_rxctrl rxctrl
;
116 struct work_struct tx_work
;
117 struct work_struct irq_work
;
118 struct work_struct rxctrl_work
;
120 struct sk_buff_head txq
;
122 struct spi_message spi_msg1
;
123 struct spi_message spi_msg2
;
124 struct spi_transfer spi_xfer1
;
125 struct spi_transfer spi_xfer2
[2];
128 static int msg_enable
;
130 /* shift for byte-enable data */
131 #define BYTE_EN(_x) ((_x) << 2)
133 /* turn register number and byte-enable mask into data for start of packet */
134 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
136 /* SPI register read/write calls.
138 * All these calls issue SPI transactions to access the chip's registers. They
139 * all require that the necessary lock is held to prevent accesses when the
140 * chip is busy transfering packet data (RX/TX FIFO accesses).
144 * ks8851_wrreg16 - write 16bit register value to chip
145 * @ks: The chip state
146 * @reg: The register address
147 * @val: The value to write
149 * Issue a write to put the value @val into the register specified in @reg.
151 static void ks8851_wrreg16(struct ks8851_net
*ks
, unsigned reg
, unsigned val
)
153 struct spi_transfer
*xfer
= &ks
->spi_xfer1
;
154 struct spi_message
*msg
= &ks
->spi_msg1
;
158 txb
[0] = cpu_to_le16(MK_OP(reg
& 2 ? 0xC : 0x03, reg
) | KS_SPIOP_WR
);
159 txb
[1] = cpu_to_le16(val
);
165 ret
= spi_sync(ks
->spidev
, msg
);
167 netdev_err(ks
->netdev
, "spi_sync() failed\n");
171 * ks8851_wrreg8 - write 8bit register value to chip
172 * @ks: The chip state
173 * @reg: The register address
174 * @val: The value to write
176 * Issue a write to put the value @val into the register specified in @reg.
178 static void ks8851_wrreg8(struct ks8851_net
*ks
, unsigned reg
, unsigned val
)
180 struct spi_transfer
*xfer
= &ks
->spi_xfer1
;
181 struct spi_message
*msg
= &ks
->spi_msg1
;
186 bit
= 1 << (reg
& 3);
188 txb
[0] = cpu_to_le16(MK_OP(bit
, reg
) | KS_SPIOP_WR
);
195 ret
= spi_sync(ks
->spidev
, msg
);
197 netdev_err(ks
->netdev
, "spi_sync() failed\n");
201 * ks8851_rx_1msg - select whether to use one or two messages for spi read
202 * @ks: The device structure
204 * Return whether to generate a single message with a tx and rx buffer
205 * supplied to spi_sync(), or alternatively send the tx and rx buffers
206 * as separate messages.
208 * Depending on the hardware in use, a single message may be more efficient
209 * on interrupts or work done by the driver.
211 * This currently always returns true until we add some per-device data passed
212 * from the platform code to specify which mode is better.
214 static inline bool ks8851_rx_1msg(struct ks8851_net
*ks
)
220 * ks8851_rdreg - issue read register command and return the data
221 * @ks: The device state
222 * @op: The register address and byte enables in message format.
223 * @rxb: The RX buffer to return the result into
224 * @rxl: The length of data expected.
226 * This is the low level read call that issues the necessary spi message(s)
227 * to read data from the register specified in @op.
229 static void ks8851_rdreg(struct ks8851_net
*ks
, unsigned op
,
230 u8
*rxb
, unsigned rxl
)
232 struct spi_transfer
*xfer
;
233 struct spi_message
*msg
;
234 __le16
*txb
= (__le16
*)ks
->txd
;
238 txb
[0] = cpu_to_le16(op
| KS_SPIOP_RD
);
240 if (ks8851_rx_1msg(ks
)) {
242 xfer
= &ks
->spi_xfer1
;
249 xfer
= ks
->spi_xfer2
;
261 ret
= spi_sync(ks
->spidev
, msg
);
263 netdev_err(ks
->netdev
, "read: spi_sync() failed\n");
264 else if (ks8851_rx_1msg(ks
))
265 memcpy(rxb
, trx
+ 2, rxl
);
267 memcpy(rxb
, trx
, rxl
);
271 * ks8851_rdreg8 - read 8 bit register from device
272 * @ks: The chip information
273 * @reg: The register address
275 * Read a 8bit register from the chip, returning the result
277 static unsigned ks8851_rdreg8(struct ks8851_net
*ks
, unsigned reg
)
281 ks8851_rdreg(ks
, MK_OP(1 << (reg
& 3), reg
), rxb
, 1);
286 * ks8851_rdreg16 - read 16 bit register from device
287 * @ks: The chip information
288 * @reg: The register address
290 * Read a 16bit register from the chip, returning the result
292 static unsigned ks8851_rdreg16(struct ks8851_net
*ks
, unsigned reg
)
296 ks8851_rdreg(ks
, MK_OP(reg
& 2 ? 0xC : 0x3, reg
), (u8
*)&rx
, 2);
297 return le16_to_cpu(rx
);
301 * ks8851_rdreg32 - read 32 bit register from device
302 * @ks: The chip information
303 * @reg: The register address
305 * Read a 32bit register from the chip.
307 * Note, this read requires the address be aligned to 4 bytes.
309 static unsigned ks8851_rdreg32(struct ks8851_net
*ks
, unsigned reg
)
315 ks8851_rdreg(ks
, MK_OP(0xf, reg
), (u8
*)&rx
, 4);
316 return le32_to_cpu(rx
);
320 * ks8851_soft_reset - issue one of the soft reset to the device
321 * @ks: The device state.
322 * @op: The bit(s) to set in the GRR
324 * Issue the relevant soft-reset command to the device's GRR register
327 * Note, the delays are in there as a caution to ensure that the reset
328 * has time to take effect and then complete. Since the datasheet does
329 * not currently specify the exact sequence, we have chosen something
330 * that seems to work with our device.
332 static void ks8851_soft_reset(struct ks8851_net
*ks
, unsigned op
)
334 ks8851_wrreg16(ks
, KS_GRR
, op
);
335 mdelay(1); /* wait a short time to effect reset */
336 ks8851_wrreg16(ks
, KS_GRR
, 0);
337 mdelay(1); /* wait for condition to clear */
341 * ks8851_write_mac_addr - write mac address to device registers
342 * @dev: The network device
344 * Update the KS8851 MAC address registers from the address in @dev.
346 * This call assumes that the chip is not running, so there is no need to
347 * shutdown the RXQ process whilst setting this.
349 static int ks8851_write_mac_addr(struct net_device
*dev
)
351 struct ks8851_net
*ks
= netdev_priv(dev
);
354 mutex_lock(&ks
->lock
);
356 for (i
= 0; i
< ETH_ALEN
; i
++)
357 ks8851_wrreg8(ks
, KS_MAR(i
), dev
->dev_addr
[i
]);
359 mutex_unlock(&ks
->lock
);
365 * ks8851_init_mac - initialise the mac address
366 * @ks: The device structure
368 * Get or create the initial mac address for the device and then set that
369 * into the station address register. Currently we assume that the device
370 * does not have a valid mac address in it, and so we use random_ether_addr()
371 * to create a new one.
373 * In future, the driver should check to see if the device has an EEPROM
374 * attached and whether that has a valid ethernet address in it.
376 static void ks8851_init_mac(struct ks8851_net
*ks
)
378 struct net_device
*dev
= ks
->netdev
;
380 random_ether_addr(dev
->dev_addr
);
381 ks8851_write_mac_addr(dev
);
385 * ks8851_irq - device interrupt handler
386 * @irq: Interrupt number passed from the IRQ hnalder.
387 * @pw: The private word passed to register_irq(), our struct ks8851_net.
389 * Disable the interrupt from happening again until we've processed the
390 * current status by scheduling ks8851_irq_work().
392 static irqreturn_t
ks8851_irq(int irq
, void *pw
)
394 struct ks8851_net
*ks
= pw
;
396 disable_irq_nosync(irq
);
397 schedule_work(&ks
->irq_work
);
402 * ks8851_rdfifo - read data from the receive fifo
403 * @ks: The device state.
404 * @buff: The buffer address
405 * @len: The length of the data to read
407 * Issue an RXQ FIFO read command and read the @len amount of data from
408 * the FIFO into the buffer specified by @buff.
410 static void ks8851_rdfifo(struct ks8851_net
*ks
, u8
*buff
, unsigned len
)
412 struct spi_transfer
*xfer
= ks
->spi_xfer2
;
413 struct spi_message
*msg
= &ks
->spi_msg2
;
417 netif_dbg(ks
, rx_status
, ks
->netdev
,
418 "%s: %d@%p\n", __func__
, len
, buff
);
420 /* set the operation we're issuing */
421 txb
[0] = KS_SPIOP_RXFIFO
;
432 ret
= spi_sync(ks
->spidev
, msg
);
434 netdev_err(ks
->netdev
, "%s: spi_sync() failed\n", __func__
);
438 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
439 * @ks: The device state
440 * @rxpkt: The data for the received packet
442 * Dump the initial data from the packet to dev_dbg().
444 static void ks8851_dbg_dumpkkt(struct ks8851_net
*ks
, u8
*rxpkt
)
446 netdev_dbg(ks
->netdev
,
447 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
448 rxpkt
[4], rxpkt
[5], rxpkt
[6], rxpkt
[7],
449 rxpkt
[8], rxpkt
[9], rxpkt
[10], rxpkt
[11],
450 rxpkt
[12], rxpkt
[13], rxpkt
[14], rxpkt
[15]);
454 * ks8851_rx_pkts - receive packets from the host
455 * @ks: The device information.
457 * This is called from the IRQ work queue when the system detects that there
458 * are packets in the receive queue. Find out how many packets there are and
459 * read them from the FIFO.
461 static void ks8851_rx_pkts(struct ks8851_net
*ks
)
470 rxfc
= ks8851_rdreg8(ks
, KS_RXFC
);
472 netif_dbg(ks
, rx_status
, ks
->netdev
,
473 "%s: %d packets\n", __func__
, rxfc
);
475 /* Currently we're issuing a read per packet, but we could possibly
476 * improve the code by issuing a single read, getting the receive
477 * header, allocating the packet and then reading the packet data
480 * This form of operation would require us to hold the SPI bus'
481 * chipselect low during the entie transaction to avoid any
482 * reset to the data stream comming from the chip.
485 for (; rxfc
!= 0; rxfc
--) {
486 rxh
= ks8851_rdreg32(ks
, KS_RXFHSR
);
487 rxstat
= rxh
& 0xffff;
490 netif_dbg(ks
, rx_status
, ks
->netdev
,
491 "rx: stat 0x%04x, len 0x%04x\n", rxstat
, rxlen
);
493 /* the length of the packet includes the 32bit CRC */
495 /* set dma read address */
496 ks8851_wrreg16(ks
, KS_RXFDPR
, RXFDPR_RXFPAI
| 0x00);
498 /* start the packet dma process, and set auto-dequeue rx */
499 ks8851_wrreg16(ks
, KS_RXQCR
,
500 ks
->rc_rxqcr
| RXQCR_SDA
| RXQCR_ADRFE
);
503 skb
= netdev_alloc_skb(ks
->netdev
, rxlen
+ 2 + 8);
505 /* todo - dump frame and move on */
508 /* two bytes to ensure ip is aligned, and four bytes
509 * for the status header and 4 bytes of garbage */
510 skb_reserve(skb
, 2 + 4 + 4);
512 rxpkt
= skb_put(skb
, rxlen
- 4) - 8;
514 /* align the packet length to 4 bytes, and add 4 bytes
515 * as we're getting the rx status header as well */
516 ks8851_rdfifo(ks
, rxpkt
, ALIGN(rxlen
, 4) + 8);
518 if (netif_msg_pktdata(ks
))
519 ks8851_dbg_dumpkkt(ks
, rxpkt
);
521 skb
->protocol
= eth_type_trans(skb
, ks
->netdev
);
524 ks
->netdev
->stats
.rx_packets
++;
525 ks
->netdev
->stats
.rx_bytes
+= rxlen
- 4;
528 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
);
533 * ks8851_irq_work - work queue handler for dealing with interrupt requests
534 * @work: The work structure that was scheduled by schedule_work()
536 * This is the handler invoked when the ks8851_irq() is called to find out
537 * what happened, as we cannot allow ourselves to sleep whilst waiting for
538 * anything other process has the chip's lock.
540 * Read the interrupt status, work out what needs to be done and then clear
541 * any of the interrupts that are not needed.
543 static void ks8851_irq_work(struct work_struct
*work
)
545 struct ks8851_net
*ks
= container_of(work
, struct ks8851_net
, irq_work
);
547 unsigned handled
= 0;
549 mutex_lock(&ks
->lock
);
551 status
= ks8851_rdreg16(ks
, KS_ISR
);
553 netif_dbg(ks
, intr
, ks
->netdev
,
554 "%s: status 0x%04x\n", __func__
, status
);
556 if (status
& IRQ_LCI
) {
557 /* should do something about checking link status */
561 if (status
& IRQ_LDI
) {
562 u16 pmecr
= ks8851_rdreg16(ks
, KS_PMECR
);
563 pmecr
&= ~PMECR_WKEVT_MASK
;
564 ks8851_wrreg16(ks
, KS_PMECR
, pmecr
| PMECR_WKEVT_LINK
);
569 if (status
& IRQ_RXPSI
)
570 handled
|= IRQ_RXPSI
;
572 if (status
& IRQ_TXI
) {
575 /* no lock here, tx queue should have been stopped */
577 /* update our idea of how much tx space is available to the
579 ks
->tx_space
= ks8851_rdreg16(ks
, KS_TXMIR
);
581 netif_dbg(ks
, intr
, ks
->netdev
,
582 "%s: txspace %d\n", __func__
, ks
->tx_space
);
585 if (status
& IRQ_RXI
)
588 if (status
& IRQ_SPIBEI
) {
589 dev_err(&ks
->spidev
->dev
, "%s: spi bus error\n", __func__
);
590 handled
|= IRQ_SPIBEI
;
593 ks8851_wrreg16(ks
, KS_ISR
, handled
);
595 if (status
& IRQ_RXI
) {
596 /* the datasheet says to disable the rx interrupt during
597 * packet read-out, however we're masking the interrupt
598 * from the device so do not bother masking just the RX
599 * from the device. */
604 /* if something stopped the rx process, probably due to wanting
605 * to change the rx settings, then do something about restarting
607 if (status
& IRQ_RXPSI
) {
608 struct ks8851_rxctrl
*rxc
= &ks
->rxctrl
;
610 /* update the multicast hash table */
611 ks8851_wrreg16(ks
, KS_MAHTR0
, rxc
->mchash
[0]);
612 ks8851_wrreg16(ks
, KS_MAHTR1
, rxc
->mchash
[1]);
613 ks8851_wrreg16(ks
, KS_MAHTR2
, rxc
->mchash
[2]);
614 ks8851_wrreg16(ks
, KS_MAHTR3
, rxc
->mchash
[3]);
616 ks8851_wrreg16(ks
, KS_RXCR2
, rxc
->rxcr2
);
617 ks8851_wrreg16(ks
, KS_RXCR1
, rxc
->rxcr1
);
620 mutex_unlock(&ks
->lock
);
622 if (status
& IRQ_TXI
)
623 netif_wake_queue(ks
->netdev
);
625 enable_irq(ks
->netdev
->irq
);
629 * calc_txlen - calculate size of message to send packet
630 * @len: Lenght of data
632 * Returns the size of the TXFIFO message needed to send
635 static inline unsigned calc_txlen(unsigned len
)
637 return ALIGN(len
+ 4, 4);
641 * ks8851_wrpkt - write packet to TX FIFO
642 * @ks: The device state.
643 * @txp: The sk_buff to transmit.
644 * @irq: IRQ on completion of the packet.
646 * Send the @txp to the chip. This means creating the relevant packet header
647 * specifying the length of the packet and the other information the chip
648 * needs, such as IRQ on completion. Send the header and the packet data to
651 static void ks8851_wrpkt(struct ks8851_net
*ks
, struct sk_buff
*txp
, bool irq
)
653 struct spi_transfer
*xfer
= ks
->spi_xfer2
;
654 struct spi_message
*msg
= &ks
->spi_msg2
;
658 netif_dbg(ks
, tx_queued
, ks
->netdev
, "%s: skb %p, %d@%p, irq %d\n",
659 __func__
, txp
, txp
->len
, txp
->data
, irq
);
662 fid
&= TXFR_TXFID_MASK
;
665 fid
|= TXFR_TXIC
; /* irq on completion */
667 /* start header at txb[1] to align txw entries */
668 ks
->txh
.txb
[1] = KS_SPIOP_TXFIFO
;
669 ks
->txh
.txw
[1] = cpu_to_le16(fid
);
670 ks
->txh
.txw
[2] = cpu_to_le16(txp
->len
);
672 xfer
->tx_buf
= &ks
->txh
.txb
[1];
677 xfer
->tx_buf
= txp
->data
;
679 xfer
->len
= ALIGN(txp
->len
, 4);
681 ret
= spi_sync(ks
->spidev
, msg
);
683 netdev_err(ks
->netdev
, "%s: spi_sync() failed\n", __func__
);
687 * ks8851_done_tx - update and then free skbuff after transmitting
688 * @ks: The device state
689 * @txb: The buffer transmitted
691 static void ks8851_done_tx(struct ks8851_net
*ks
, struct sk_buff
*txb
)
693 struct net_device
*dev
= ks
->netdev
;
695 dev
->stats
.tx_bytes
+= txb
->len
;
696 dev
->stats
.tx_packets
++;
702 * ks8851_tx_work - process tx packet(s)
703 * @work: The work strucutre what was scheduled.
705 * This is called when a number of packets have been scheduled for
706 * transmission and need to be sent to the device.
708 static void ks8851_tx_work(struct work_struct
*work
)
710 struct ks8851_net
*ks
= container_of(work
, struct ks8851_net
, tx_work
);
712 bool last
= skb_queue_empty(&ks
->txq
);
714 mutex_lock(&ks
->lock
);
717 txb
= skb_dequeue(&ks
->txq
);
718 last
= skb_queue_empty(&ks
->txq
);
721 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
| RXQCR_SDA
);
722 ks8851_wrpkt(ks
, txb
, last
);
723 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
);
724 ks8851_wrreg16(ks
, KS_TXQCR
, TXQCR_METFE
);
726 ks8851_done_tx(ks
, txb
);
730 mutex_unlock(&ks
->lock
);
734 * ks8851_set_powermode - set power mode of the device
735 * @ks: The device state
736 * @pwrmode: The power mode value to write to KS_PMECR.
738 * Change the power mode of the chip.
740 static void ks8851_set_powermode(struct ks8851_net
*ks
, unsigned pwrmode
)
744 netif_dbg(ks
, hw
, ks
->netdev
, "setting power mode %d\n", pwrmode
);
746 pmecr
= ks8851_rdreg16(ks
, KS_PMECR
);
747 pmecr
&= ~PMECR_PM_MASK
;
750 ks8851_wrreg16(ks
, KS_PMECR
, pmecr
);
754 * ks8851_net_open - open network device
755 * @dev: The network device being opened.
757 * Called when the network device is marked active, such as a user executing
758 * 'ifconfig up' on the device.
760 static int ks8851_net_open(struct net_device
*dev
)
762 struct ks8851_net
*ks
= netdev_priv(dev
);
764 /* lock the card, even if we may not actually be doing anything
765 * else at the moment */
766 mutex_lock(&ks
->lock
);
768 netif_dbg(ks
, ifup
, ks
->netdev
, "opening\n");
770 /* bring chip out of any power saving mode it was in */
771 ks8851_set_powermode(ks
, PMECR_PM_NORMAL
);
773 /* issue a soft reset to the RX/TX QMU to put it into a known
775 ks8851_soft_reset(ks
, GRR_QMU
);
777 /* setup transmission parameters */
779 ks8851_wrreg16(ks
, KS_TXCR
, (TXCR_TXE
| /* enable transmit process */
780 TXCR_TXPE
| /* pad to min length */
781 TXCR_TXCRC
| /* add CRC */
782 TXCR_TXFCE
)); /* enable flow control */
784 /* auto-increment tx data, reset tx pointer */
785 ks8851_wrreg16(ks
, KS_TXFDPR
, TXFDPR_TXFPAI
);
787 /* setup receiver control */
789 ks8851_wrreg16(ks
, KS_RXCR1
, (RXCR1_RXPAFMA
| /* from mac filter */
790 RXCR1_RXFCE
| /* enable flow control */
791 RXCR1_RXBE
| /* broadcast enable */
792 RXCR1_RXUE
| /* unicast enable */
793 RXCR1_RXE
)); /* enable rx block */
795 /* transfer entire frames out in one go */
796 ks8851_wrreg16(ks
, KS_RXCR2
, RXCR2_SRDBL_FRAME
);
798 /* set receive counter timeouts */
799 ks8851_wrreg16(ks
, KS_RXDTTR
, 1000); /* 1ms after first frame to IRQ */
800 ks8851_wrreg16(ks
, KS_RXDBCTR
, 4096); /* >4Kbytes in buffer to IRQ */
801 ks8851_wrreg16(ks
, KS_RXFCTR
, 10); /* 10 frames to IRQ */
803 ks
->rc_rxqcr
= (RXQCR_RXFCTE
| /* IRQ on frame count exceeded */
804 RXQCR_RXDBCTE
| /* IRQ on byte count exceeded */
805 RXQCR_RXDTTE
); /* IRQ on time exceeded */
807 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
);
809 /* clear then enable interrupts */
811 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
812 IRQ_TXI | /* TX done */ \
813 IRQ_RXI | /* RX done */ \
814 IRQ_SPIBEI | /* SPI bus error */ \
815 IRQ_TXPSI | /* TX process stop */ \
816 IRQ_RXPSI) /* RX process stop */
818 ks
->rc_ier
= STD_IRQ
;
819 ks8851_wrreg16(ks
, KS_ISR
, STD_IRQ
);
820 ks8851_wrreg16(ks
, KS_IER
, STD_IRQ
);
822 netif_start_queue(ks
->netdev
);
824 netif_dbg(ks
, ifup
, ks
->netdev
, "network device up\n");
826 mutex_unlock(&ks
->lock
);
831 * ks8851_net_stop - close network device
832 * @dev: The device being closed.
834 * Called to close down a network device which has been active. Cancell any
835 * work, shutdown the RX and TX process and then place the chip into a low
836 * power state whilst it is not being used.
838 static int ks8851_net_stop(struct net_device
*dev
)
840 struct ks8851_net
*ks
= netdev_priv(dev
);
842 netif_info(ks
, ifdown
, dev
, "shutting down\n");
844 netif_stop_queue(dev
);
846 mutex_lock(&ks
->lock
);
848 /* stop any outstanding work */
849 flush_work(&ks
->irq_work
);
850 flush_work(&ks
->tx_work
);
851 flush_work(&ks
->rxctrl_work
);
853 /* turn off the IRQs and ack any outstanding */
854 ks8851_wrreg16(ks
, KS_IER
, 0x0000);
855 ks8851_wrreg16(ks
, KS_ISR
, 0xffff);
857 /* shutdown RX process */
858 ks8851_wrreg16(ks
, KS_RXCR1
, 0x0000);
860 /* shutdown TX process */
861 ks8851_wrreg16(ks
, KS_TXCR
, 0x0000);
863 /* set powermode to soft power down to save power */
864 ks8851_set_powermode(ks
, PMECR_PM_SOFTDOWN
);
866 /* ensure any queued tx buffers are dumped */
867 while (!skb_queue_empty(&ks
->txq
)) {
868 struct sk_buff
*txb
= skb_dequeue(&ks
->txq
);
870 netif_dbg(ks
, ifdown
, ks
->netdev
,
871 "%s: freeing txb %p\n", __func__
, txb
);
876 mutex_unlock(&ks
->lock
);
881 * ks8851_start_xmit - transmit packet
882 * @skb: The buffer to transmit
883 * @dev: The device used to transmit the packet.
885 * Called by the network layer to transmit the @skb. Queue the packet for
886 * the device and schedule the necessary work to transmit the packet when
889 * We do this to firstly avoid sleeping with the network device locked,
890 * and secondly so we can round up more than one packet to transmit which
891 * means we can try and avoid generating too many transmit done interrupts.
893 static netdev_tx_t
ks8851_start_xmit(struct sk_buff
*skb
,
894 struct net_device
*dev
)
896 struct ks8851_net
*ks
= netdev_priv(dev
);
897 unsigned needed
= calc_txlen(skb
->len
);
898 netdev_tx_t ret
= NETDEV_TX_OK
;
900 netif_dbg(ks
, tx_queued
, ks
->netdev
,
901 "%s: skb %p, %d@%p\n", __func__
, skb
, skb
->len
, skb
->data
);
903 spin_lock(&ks
->statelock
);
905 if (needed
> ks
->tx_space
) {
906 netif_stop_queue(dev
);
907 ret
= NETDEV_TX_BUSY
;
909 ks
->tx_space
-= needed
;
910 skb_queue_tail(&ks
->txq
, skb
);
913 spin_unlock(&ks
->statelock
);
914 schedule_work(&ks
->tx_work
);
920 * ks8851_rxctrl_work - work handler to change rx mode
921 * @work: The work structure this belongs to.
923 * Lock the device and issue the necessary changes to the receive mode from
924 * the network device layer. This is done so that we can do this without
925 * having to sleep whilst holding the network device lock.
927 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
928 * receive parameters are programmed, we issue a write to disable the RXQ and
929 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
930 * complete. The interrupt handler then writes the new values into the chip.
932 static void ks8851_rxctrl_work(struct work_struct
*work
)
934 struct ks8851_net
*ks
= container_of(work
, struct ks8851_net
, rxctrl_work
);
936 mutex_lock(&ks
->lock
);
938 /* need to shutdown RXQ before modifying filter parameters */
939 ks8851_wrreg16(ks
, KS_RXCR1
, 0x00);
941 mutex_unlock(&ks
->lock
);
944 static void ks8851_set_rx_mode(struct net_device
*dev
)
946 struct ks8851_net
*ks
= netdev_priv(dev
);
947 struct ks8851_rxctrl rxctrl
;
949 memset(&rxctrl
, 0, sizeof(rxctrl
));
951 if (dev
->flags
& IFF_PROMISC
) {
952 /* interface to receive everything */
954 rxctrl
.rxcr1
= RXCR1_RXAE
| RXCR1_RXINVF
;
955 } else if (dev
->flags
& IFF_ALLMULTI
) {
956 /* accept all multicast packets */
958 rxctrl
.rxcr1
= (RXCR1_RXME
| RXCR1_RXAE
|
959 RXCR1_RXPAFMA
| RXCR1_RXMAFMA
);
960 } else if (dev
->flags
& IFF_MULTICAST
&& !netdev_mc_empty(dev
)) {
961 struct netdev_hw_addr
*ha
;
964 /* accept some multicast */
966 netdev_for_each_mc_addr(ha
, dev
) {
967 crc
= ether_crc(ETH_ALEN
, ha
->addr
);
968 crc
>>= (32 - 6); /* get top six bits */
970 rxctrl
.mchash
[crc
>> 4] |= (1 << (crc
& 0xf));
973 rxctrl
.rxcr1
= RXCR1_RXME
| RXCR1_RXPAFMA
;
975 /* just accept broadcast / unicast */
976 rxctrl
.rxcr1
= RXCR1_RXPAFMA
;
979 rxctrl
.rxcr1
|= (RXCR1_RXUE
| /* unicast enable */
980 RXCR1_RXBE
| /* broadcast enable */
981 RXCR1_RXE
| /* RX process enable */
982 RXCR1_RXFCE
); /* enable flow control */
984 rxctrl
.rxcr2
|= RXCR2_SRDBL_FRAME
;
986 /* schedule work to do the actual set of the data if needed */
988 spin_lock(&ks
->statelock
);
990 if (memcmp(&rxctrl
, &ks
->rxctrl
, sizeof(rxctrl
)) != 0) {
991 memcpy(&ks
->rxctrl
, &rxctrl
, sizeof(ks
->rxctrl
));
992 schedule_work(&ks
->rxctrl_work
);
995 spin_unlock(&ks
->statelock
);
998 static int ks8851_set_mac_address(struct net_device
*dev
, void *addr
)
1000 struct sockaddr
*sa
= addr
;
1002 if (netif_running(dev
))
1005 if (!is_valid_ether_addr(sa
->sa_data
))
1006 return -EADDRNOTAVAIL
;
1008 memcpy(dev
->dev_addr
, sa
->sa_data
, ETH_ALEN
);
1009 return ks8851_write_mac_addr(dev
);
1012 static int ks8851_net_ioctl(struct net_device
*dev
, struct ifreq
*req
, int cmd
)
1014 struct ks8851_net
*ks
= netdev_priv(dev
);
1016 if (!netif_running(dev
))
1019 return generic_mii_ioctl(&ks
->mii
, if_mii(req
), cmd
, NULL
);
1022 static const struct net_device_ops ks8851_netdev_ops
= {
1023 .ndo_open
= ks8851_net_open
,
1024 .ndo_stop
= ks8851_net_stop
,
1025 .ndo_do_ioctl
= ks8851_net_ioctl
,
1026 .ndo_start_xmit
= ks8851_start_xmit
,
1027 .ndo_set_mac_address
= ks8851_set_mac_address
,
1028 .ndo_set_rx_mode
= ks8851_set_rx_mode
,
1029 .ndo_change_mtu
= eth_change_mtu
,
1030 .ndo_validate_addr
= eth_validate_addr
,
1033 /* ethtool support */
1035 static void ks8851_get_drvinfo(struct net_device
*dev
,
1036 struct ethtool_drvinfo
*di
)
1038 strlcpy(di
->driver
, "KS8851", sizeof(di
->driver
));
1039 strlcpy(di
->version
, "1.00", sizeof(di
->version
));
1040 strlcpy(di
->bus_info
, dev_name(dev
->dev
.parent
), sizeof(di
->bus_info
));
1043 static u32
ks8851_get_msglevel(struct net_device
*dev
)
1045 struct ks8851_net
*ks
= netdev_priv(dev
);
1046 return ks
->msg_enable
;
1049 static void ks8851_set_msglevel(struct net_device
*dev
, u32 to
)
1051 struct ks8851_net
*ks
= netdev_priv(dev
);
1052 ks
->msg_enable
= to
;
1055 static int ks8851_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1057 struct ks8851_net
*ks
= netdev_priv(dev
);
1058 return mii_ethtool_gset(&ks
->mii
, cmd
);
1061 static int ks8851_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1063 struct ks8851_net
*ks
= netdev_priv(dev
);
1064 return mii_ethtool_sset(&ks
->mii
, cmd
);
1067 static u32
ks8851_get_link(struct net_device
*dev
)
1069 struct ks8851_net
*ks
= netdev_priv(dev
);
1070 return mii_link_ok(&ks
->mii
);
1073 static int ks8851_nway_reset(struct net_device
*dev
)
1075 struct ks8851_net
*ks
= netdev_priv(dev
);
1076 return mii_nway_restart(&ks
->mii
);
1079 static const struct ethtool_ops ks8851_ethtool_ops
= {
1080 .get_drvinfo
= ks8851_get_drvinfo
,
1081 .get_msglevel
= ks8851_get_msglevel
,
1082 .set_msglevel
= ks8851_set_msglevel
,
1083 .get_settings
= ks8851_get_settings
,
1084 .set_settings
= ks8851_set_settings
,
1085 .get_link
= ks8851_get_link
,
1086 .nway_reset
= ks8851_nway_reset
,
1089 /* MII interface controls */
1092 * ks8851_phy_reg - convert MII register into a KS8851 register
1093 * @reg: MII register number.
1095 * Return the KS8851 register number for the corresponding MII PHY register
1096 * if possible. Return zero if the MII register has no direct mapping to the
1097 * KS8851 register set.
1099 static int ks8851_phy_reg(int reg
)
1120 * ks8851_phy_read - MII interface PHY register read.
1121 * @dev: The network device the PHY is on.
1122 * @phy_addr: Address of PHY (ignored as we only have one)
1123 * @reg: The register to read.
1125 * This call reads data from the PHY register specified in @reg. Since the
1126 * device does not support all the MII registers, the non-existant values
1127 * are always returned as zero.
1129 * We return zero for unsupported registers as the MII code does not check
1130 * the value returned for any error status, and simply returns it to the
1131 * caller. The mii-tool that the driver was tested with takes any -ve error
1132 * as real PHY capabilities, thus displaying incorrect data to the user.
1134 static int ks8851_phy_read(struct net_device
*dev
, int phy_addr
, int reg
)
1136 struct ks8851_net
*ks
= netdev_priv(dev
);
1140 ksreg
= ks8851_phy_reg(reg
);
1142 return 0x0; /* no error return allowed, so use zero */
1144 mutex_lock(&ks
->lock
);
1145 result
= ks8851_rdreg16(ks
, ksreg
);
1146 mutex_unlock(&ks
->lock
);
1151 static void ks8851_phy_write(struct net_device
*dev
,
1152 int phy
, int reg
, int value
)
1154 struct ks8851_net
*ks
= netdev_priv(dev
);
1157 ksreg
= ks8851_phy_reg(reg
);
1159 mutex_lock(&ks
->lock
);
1160 ks8851_wrreg16(ks
, ksreg
, value
);
1161 mutex_unlock(&ks
->lock
);
1166 * ks8851_read_selftest - read the selftest memory info.
1167 * @ks: The device state
1169 * Read and check the TX/RX memory selftest information.
1171 static int ks8851_read_selftest(struct ks8851_net
*ks
)
1173 unsigned both_done
= MBIR_TXMBF
| MBIR_RXMBF
;
1177 rd
= ks8851_rdreg16(ks
, KS_MBIR
);
1179 if ((rd
& both_done
) != both_done
) {
1180 netdev_warn(ks
->netdev
, "Memory selftest not finished\n");
1184 if (rd
& MBIR_TXMBFA
) {
1185 netdev_err(ks
->netdev
, "TX memory selftest fail\n");
1189 if (rd
& MBIR_RXMBFA
) {
1190 netdev_err(ks
->netdev
, "RX memory selftest fail\n");
1197 /* driver bus management functions */
1199 static int __devinit
ks8851_probe(struct spi_device
*spi
)
1201 struct net_device
*ndev
;
1202 struct ks8851_net
*ks
;
1205 ndev
= alloc_etherdev(sizeof(struct ks8851_net
));
1207 dev_err(&spi
->dev
, "failed to alloc ethernet device\n");
1211 spi
->bits_per_word
= 8;
1213 ks
= netdev_priv(ndev
);
1217 ks
->tx_space
= 6144;
1219 mutex_init(&ks
->lock
);
1220 spin_lock_init(&ks
->statelock
);
1222 INIT_WORK(&ks
->tx_work
, ks8851_tx_work
);
1223 INIT_WORK(&ks
->irq_work
, ks8851_irq_work
);
1224 INIT_WORK(&ks
->rxctrl_work
, ks8851_rxctrl_work
);
1226 /* initialise pre-made spi transfer messages */
1228 spi_message_init(&ks
->spi_msg1
);
1229 spi_message_add_tail(&ks
->spi_xfer1
, &ks
->spi_msg1
);
1231 spi_message_init(&ks
->spi_msg2
);
1232 spi_message_add_tail(&ks
->spi_xfer2
[0], &ks
->spi_msg2
);
1233 spi_message_add_tail(&ks
->spi_xfer2
[1], &ks
->spi_msg2
);
1235 /* setup mii state */
1238 ks
->mii
.phy_id_mask
= 1;
1239 ks
->mii
.reg_num_mask
= 0xf;
1240 ks
->mii
.mdio_read
= ks8851_phy_read
;
1241 ks
->mii
.mdio_write
= ks8851_phy_write
;
1243 dev_info(&spi
->dev
, "message enable is %d\n", msg_enable
);
1245 /* set the default message enable */
1246 ks
->msg_enable
= netif_msg_init(msg_enable
, (NETIF_MSG_DRV
|
1250 skb_queue_head_init(&ks
->txq
);
1252 SET_ETHTOOL_OPS(ndev
, &ks8851_ethtool_ops
);
1253 SET_NETDEV_DEV(ndev
, &spi
->dev
);
1255 dev_set_drvdata(&spi
->dev
, ks
);
1257 ndev
->if_port
= IF_PORT_100BASET
;
1258 ndev
->netdev_ops
= &ks8851_netdev_ops
;
1259 ndev
->irq
= spi
->irq
;
1261 /* issue a global soft reset to reset the device. */
1262 ks8851_soft_reset(ks
, GRR_GSR
);
1264 /* simple check for a valid chip being connected to the bus */
1266 if ((ks8851_rdreg16(ks
, KS_CIDER
) & ~CIDER_REV_MASK
) != CIDER_ID
) {
1267 dev_err(&spi
->dev
, "failed to read device ID\n");
1272 ks8851_read_selftest(ks
);
1273 ks8851_init_mac(ks
);
1275 ret
= request_irq(spi
->irq
, ks8851_irq
, IRQF_TRIGGER_LOW
,
1278 dev_err(&spi
->dev
, "failed to get irq\n");
1282 ret
= register_netdev(ndev
);
1284 dev_err(&spi
->dev
, "failed to register network device\n");
1288 netdev_info(ndev
, "revision %d, MAC %pM, IRQ %d\n",
1289 CIDER_REV_GET(ks8851_rdreg16(ks
, KS_CIDER
)),
1290 ndev
->dev_addr
, ndev
->irq
);
1296 free_irq(ndev
->irq
, ndev
);
1304 static int __devexit
ks8851_remove(struct spi_device
*spi
)
1306 struct ks8851_net
*priv
= dev_get_drvdata(&spi
->dev
);
1308 if (netif_msg_drv(priv
))
1309 dev_info(&spi
->dev
, "remove\n");
1311 unregister_netdev(priv
->netdev
);
1312 free_irq(spi
->irq
, priv
);
1313 free_netdev(priv
->netdev
);
1318 static struct spi_driver ks8851_driver
= {
1321 .owner
= THIS_MODULE
,
1323 .probe
= ks8851_probe
,
1324 .remove
= __devexit_p(ks8851_remove
),
1327 static int __init
ks8851_init(void)
1329 return spi_register_driver(&ks8851_driver
);
1332 static void __exit
ks8851_exit(void)
1334 spi_unregister_driver(&ks8851_driver
);
1337 module_init(ks8851_init
);
1338 module_exit(ks8851_exit
);
1340 MODULE_DESCRIPTION("KS8851 Network driver");
1341 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1342 MODULE_LICENSE("GPL");
1344 module_param_named(message
, msg_enable
, int, 0);
1345 MODULE_PARM_DESC(message
, "Message verbosity level (0=none, 31=all)");
1346 MODULE_ALIAS("spi:ks8851");