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.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/cache.h>
20 #include <linux/crc32.h>
21 #include <linux/mii.h>
23 #include <linux/spi/spi.h>
28 * struct ks8851_rxctrl - KS8851 driver rx control
29 * @mchash: Multicast hash-table data.
30 * @rxcr1: KS_RXCR1 register setting
31 * @rxcr2: KS_RXCR2 register setting
33 * Representation of the settings needs to control the receive filtering
34 * such as the multicast hash-filter and the receive register settings. This
35 * is used to make the job of working out if the receive settings change and
36 * then issuing the new settings to the worker that will send the necessary
39 struct ks8851_rxctrl
{
46 * union ks8851_tx_hdr - tx header data
47 * @txb: The header as bytes
48 * @txw: The header as 16bit, little-endian words
50 * A dual representation of the tx header data to allow
51 * access to individual bytes, and to allow 16bit accesses
52 * with 16bit alignment.
60 * struct ks8851_net - KS8851 driver private data
61 * @netdev: The network device we're bound to
62 * @spidev: The spi device we're bound to.
63 * @lock: Lock to ensure that the device is not accessed when busy.
64 * @statelock: Lock on this structure for tx list.
65 * @mii: The MII state information for the mii calls.
66 * @rxctrl: RX settings for @rxctrl_work.
67 * @tx_work: Work queue for tx packets
68 * @irq_work: Work queue for servicing interrupts
69 * @rxctrl_work: Work queue for updating RX mode and multicast lists
70 * @txq: Queue of packets for transmission.
71 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
72 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
73 * @txh: Space for generating packet TX header in DMA-able data
74 * @rxd: Space for receiving SPI data, in DMA-able space.
75 * @txd: Space for transmitting SPI data, in DMA-able space.
76 * @msg_enable: The message flags controlling driver output (see ethtool).
77 * @fid: Incrementing frame id tag.
78 * @rc_ier: Cached copy of KS_IER.
79 * @rc_rxqcr: Cached copy of KS_RXQCR.
81 * The @lock ensures that the chip is protected when certain operations are
82 * in progress. When the read or write packet transfer is in progress, most
83 * of the chip registers are not ccessible until the transfer is finished and
84 * the DMA has been de-asserted.
86 * The @statelock is used to protect information in the structure which may
87 * need to be accessed via several sources, such as the network driver layer
88 * or one of the work queues.
90 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
91 * wants to DMA map them, it will not have any problems with data the driver
95 struct net_device
*netdev
;
96 struct spi_device
*spidev
;
100 union ks8851_tx_hdr txh ____cacheline_aligned
;
104 u32 msg_enable ____cacheline_aligned
;
111 struct mii_if_info mii
;
112 struct ks8851_rxctrl rxctrl
;
114 struct work_struct tx_work
;
115 struct work_struct irq_work
;
116 struct work_struct rxctrl_work
;
118 struct sk_buff_head txq
;
120 struct spi_message spi_msg1
;
121 struct spi_message spi_msg2
;
122 struct spi_transfer spi_xfer1
;
123 struct spi_transfer spi_xfer2
[2];
126 static int msg_enable
;
128 #define ks_info(_ks, _msg...) dev_info(&(_ks)->spidev->dev, _msg)
129 #define ks_warn(_ks, _msg...) dev_warn(&(_ks)->spidev->dev, _msg)
130 #define ks_dbg(_ks, _msg...) dev_dbg(&(_ks)->spidev->dev, _msg)
131 #define ks_err(_ks, _msg...) dev_err(&(_ks)->spidev->dev, _msg)
133 /* shift for byte-enable data */
134 #define BYTE_EN(_x) ((_x) << 2)
136 /* turn register number and byte-enable mask into data for start of packet */
137 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
139 /* SPI register read/write calls.
141 * All these calls issue SPI transactions to access the chip's registers. They
142 * all require that the necessary lock is held to prevent accesses when the
143 * chip is busy transfering packet data (RX/TX FIFO accesses).
147 * ks8851_wrreg16 - write 16bit register value to chip
148 * @ks: The chip state
149 * @reg: The register address
150 * @val: The value to write
152 * Issue a write to put the value @val into the register specified in @reg.
154 static void ks8851_wrreg16(struct ks8851_net
*ks
, unsigned reg
, unsigned val
)
156 struct spi_transfer
*xfer
= &ks
->spi_xfer1
;
157 struct spi_message
*msg
= &ks
->spi_msg1
;
161 txb
[0] = cpu_to_le16(MK_OP(reg
& 2 ? 0xC : 0x03, reg
) | KS_SPIOP_WR
);
162 txb
[1] = cpu_to_le16(val
);
168 ret
= spi_sync(ks
->spidev
, msg
);
170 ks_err(ks
, "spi_sync() failed\n");
174 * ks8851_wrreg8 - write 8bit register value to chip
175 * @ks: The chip state
176 * @reg: The register address
177 * @val: The value to write
179 * Issue a write to put the value @val into the register specified in @reg.
181 static void ks8851_wrreg8(struct ks8851_net
*ks
, unsigned reg
, unsigned val
)
183 struct spi_transfer
*xfer
= &ks
->spi_xfer1
;
184 struct spi_message
*msg
= &ks
->spi_msg1
;
189 bit
= 1 << (reg
& 3);
191 txb
[0] = cpu_to_le16(MK_OP(bit
, reg
) | KS_SPIOP_WR
);
198 ret
= spi_sync(ks
->spidev
, msg
);
200 ks_err(ks
, "spi_sync() failed\n");
204 * ks8851_rx_1msg - select whether to use one or two messages for spi read
205 * @ks: The device structure
207 * Return whether to generate a single message with a tx and rx buffer
208 * supplied to spi_sync(), or alternatively send the tx and rx buffers
209 * as separate messages.
211 * Depending on the hardware in use, a single message may be more efficient
212 * on interrupts or work done by the driver.
214 * This currently always returns true until we add some per-device data passed
215 * from the platform code to specify which mode is better.
217 static inline bool ks8851_rx_1msg(struct ks8851_net
*ks
)
223 * ks8851_rdreg - issue read register command and return the data
224 * @ks: The device state
225 * @op: The register address and byte enables in message format.
226 * @rxb: The RX buffer to return the result into
227 * @rxl: The length of data expected.
229 * This is the low level read call that issues the necessary spi message(s)
230 * to read data from the register specified in @op.
232 static void ks8851_rdreg(struct ks8851_net
*ks
, unsigned op
,
233 u8
*rxb
, unsigned rxl
)
235 struct spi_transfer
*xfer
;
236 struct spi_message
*msg
;
237 __le16
*txb
= (__le16
*)ks
->txd
;
241 txb
[0] = cpu_to_le16(op
| KS_SPIOP_RD
);
243 if (ks8851_rx_1msg(ks
)) {
245 xfer
= &ks
->spi_xfer1
;
252 xfer
= ks
->spi_xfer2
;
264 ret
= spi_sync(ks
->spidev
, msg
);
266 ks_err(ks
, "read: spi_sync() failed\n");
267 else if (ks8851_rx_1msg(ks
))
268 memcpy(rxb
, trx
+ 2, rxl
);
270 memcpy(rxb
, trx
, rxl
);
274 * ks8851_rdreg8 - read 8 bit register from device
275 * @ks: The chip information
276 * @reg: The register address
278 * Read a 8bit register from the chip, returning the result
280 static unsigned ks8851_rdreg8(struct ks8851_net
*ks
, unsigned reg
)
284 ks8851_rdreg(ks
, MK_OP(1 << (reg
& 3), reg
), rxb
, 1);
289 * ks8851_rdreg16 - read 16 bit register from device
290 * @ks: The chip information
291 * @reg: The register address
293 * Read a 16bit register from the chip, returning the result
295 static unsigned ks8851_rdreg16(struct ks8851_net
*ks
, unsigned reg
)
299 ks8851_rdreg(ks
, MK_OP(reg
& 2 ? 0xC : 0x3, reg
), (u8
*)&rx
, 2);
300 return le16_to_cpu(rx
);
304 * ks8851_rdreg32 - read 32 bit register from device
305 * @ks: The chip information
306 * @reg: The register address
308 * Read a 32bit register from the chip.
310 * Note, this read requires the address be aligned to 4 bytes.
312 static unsigned ks8851_rdreg32(struct ks8851_net
*ks
, unsigned reg
)
318 ks8851_rdreg(ks
, MK_OP(0xf, reg
), (u8
*)&rx
, 4);
319 return le32_to_cpu(rx
);
323 * ks8851_soft_reset - issue one of the soft reset to the device
324 * @ks: The device state.
325 * @op: The bit(s) to set in the GRR
327 * Issue the relevant soft-reset command to the device's GRR register
330 * Note, the delays are in there as a caution to ensure that the reset
331 * has time to take effect and then complete. Since the datasheet does
332 * not currently specify the exact sequence, we have chosen something
333 * that seems to work with our device.
335 static void ks8851_soft_reset(struct ks8851_net
*ks
, unsigned op
)
337 ks8851_wrreg16(ks
, KS_GRR
, op
);
338 mdelay(1); /* wait a short time to effect reset */
339 ks8851_wrreg16(ks
, KS_GRR
, 0);
340 mdelay(1); /* wait for condition to clear */
344 * ks8851_write_mac_addr - write mac address to device registers
345 * @dev: The network device
347 * Update the KS8851 MAC address registers from the address in @dev.
349 * This call assumes that the chip is not running, so there is no need to
350 * shutdown the RXQ process whilst setting this.
352 static int ks8851_write_mac_addr(struct net_device
*dev
)
354 struct ks8851_net
*ks
= netdev_priv(dev
);
357 mutex_lock(&ks
->lock
);
359 for (i
= 0; i
< ETH_ALEN
; i
++)
360 ks8851_wrreg8(ks
, KS_MAR(i
), dev
->dev_addr
[i
]);
362 mutex_unlock(&ks
->lock
);
368 * ks8851_init_mac - initialise the mac address
369 * @ks: The device structure
371 * Get or create the initial mac address for the device and then set that
372 * into the station address register. Currently we assume that the device
373 * does not have a valid mac address in it, and so we use random_ether_addr()
374 * to create a new one.
376 * In future, the driver should check to see if the device has an EEPROM
377 * attached and whether that has a valid ethernet address in it.
379 static void ks8851_init_mac(struct ks8851_net
*ks
)
381 struct net_device
*dev
= ks
->netdev
;
383 random_ether_addr(dev
->dev_addr
);
384 ks8851_write_mac_addr(dev
);
388 * ks8851_irq - device interrupt handler
389 * @irq: Interrupt number passed from the IRQ hnalder.
390 * @pw: The private word passed to register_irq(), our struct ks8851_net.
392 * Disable the interrupt from happening again until we've processed the
393 * current status by scheduling ks8851_irq_work().
395 static irqreturn_t
ks8851_irq(int irq
, void *pw
)
397 struct ks8851_net
*ks
= pw
;
399 disable_irq_nosync(irq
);
400 schedule_work(&ks
->irq_work
);
405 * ks8851_rdfifo - read data from the receive fifo
406 * @ks: The device state.
407 * @buff: The buffer address
408 * @len: The length of the data to read
410 * Issue an RXQ FIFO read command and read the @len ammount of data from
411 * the FIFO into the buffer specified by @buff.
413 static void ks8851_rdfifo(struct ks8851_net
*ks
, u8
*buff
, unsigned len
)
415 struct spi_transfer
*xfer
= ks
->spi_xfer2
;
416 struct spi_message
*msg
= &ks
->spi_msg2
;
420 if (netif_msg_rx_status(ks
))
421 ks_dbg(ks
, "%s: %d@%p\n", __func__
, len
, buff
);
423 /* set the operation we're issuing */
424 txb
[0] = KS_SPIOP_RXFIFO
;
435 ret
= spi_sync(ks
->spidev
, msg
);
437 ks_err(ks
, "%s: spi_sync() failed\n", __func__
);
441 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
442 * @ks: The device state
443 * @rxpkt: The data for the received packet
445 * Dump the initial data from the packet to dev_dbg().
447 static void ks8851_dbg_dumpkkt(struct ks8851_net
*ks
, u8
*rxpkt
)
449 ks_dbg(ks
, "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
450 rxpkt
[4], rxpkt
[5], rxpkt
[6], rxpkt
[7],
451 rxpkt
[8], rxpkt
[9], rxpkt
[10], rxpkt
[11],
452 rxpkt
[12], rxpkt
[13], rxpkt
[14], rxpkt
[15]);
456 * ks8851_rx_pkts - receive packets from the host
457 * @ks: The device information.
459 * This is called from the IRQ work queue when the system detects that there
460 * are packets in the receive queue. Find out how many packets there are and
461 * read them from the FIFO.
463 static void ks8851_rx_pkts(struct ks8851_net
*ks
)
472 rxfc
= ks8851_rdreg8(ks
, KS_RXFC
);
474 if (netif_msg_rx_status(ks
))
475 ks_dbg(ks
, "%s: %d packets\n", __func__
, rxfc
);
477 /* Currently we're issuing a read per packet, but we could possibly
478 * improve the code by issuing a single read, getting the receive
479 * header, allocating the packet and then reading the packet data
482 * This form of operation would require us to hold the SPI bus'
483 * chipselect low during the entie transaction to avoid any
484 * reset to the data stream comming from the chip.
487 for (; rxfc
!= 0; rxfc
--) {
488 rxh
= ks8851_rdreg32(ks
, KS_RXFHSR
);
489 rxstat
= rxh
& 0xffff;
492 if (netif_msg_rx_status(ks
))
493 ks_dbg(ks
, "rx: stat 0x%04x, len 0x%04x\n",
496 /* the length of the packet includes the 32bit CRC */
498 /* set dma read address */
499 ks8851_wrreg16(ks
, KS_RXFDPR
, RXFDPR_RXFPAI
| 0x00);
501 /* start the packet dma process, and set auto-dequeue rx */
502 ks8851_wrreg16(ks
, KS_RXQCR
,
503 ks
->rc_rxqcr
| RXQCR_SDA
| RXQCR_ADRFE
);
506 skb
= netdev_alloc_skb(ks
->netdev
, rxlen
+ 2 + 8);
508 /* todo - dump frame and move on */
511 /* two bytes to ensure ip is aligned, and four bytes
512 * for the status header and 4 bytes of garbage */
513 skb_reserve(skb
, 2 + 4 + 4);
515 rxpkt
= skb_put(skb
, rxlen
- 4) - 8;
517 /* align the packet length to 4 bytes, and add 4 bytes
518 * as we're getting the rx status header as well */
519 ks8851_rdfifo(ks
, rxpkt
, ALIGN(rxlen
, 4) + 8);
521 if (netif_msg_pktdata(ks
))
522 ks8851_dbg_dumpkkt(ks
, rxpkt
);
524 skb
->protocol
= eth_type_trans(skb
, ks
->netdev
);
527 ks
->netdev
->stats
.rx_packets
++;
528 ks
->netdev
->stats
.rx_bytes
+= rxlen
- 4;
531 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
);
536 * ks8851_irq_work - work queue handler for dealing with interrupt requests
537 * @work: The work structure that was scheduled by schedule_work()
539 * This is the handler invoked when the ks8851_irq() is called to find out
540 * what happened, as we cannot allow ourselves to sleep whilst waiting for
541 * anything other process has the chip's lock.
543 * Read the interrupt status, work out what needs to be done and then clear
544 * any of the interrupts that are not needed.
546 static void ks8851_irq_work(struct work_struct
*work
)
548 struct ks8851_net
*ks
= container_of(work
, struct ks8851_net
, irq_work
);
550 unsigned handled
= 0;
552 mutex_lock(&ks
->lock
);
554 status
= ks8851_rdreg16(ks
, KS_ISR
);
556 if (netif_msg_intr(ks
))
557 dev_dbg(&ks
->spidev
->dev
, "%s: status 0x%04x\n",
560 if (status
& IRQ_LCI
) {
561 /* should do something about checking link status */
565 if (status
& IRQ_LDI
) {
566 u16 pmecr
= ks8851_rdreg16(ks
, KS_PMECR
);
567 pmecr
&= ~PMECR_WKEVT_MASK
;
568 ks8851_wrreg16(ks
, KS_PMECR
, pmecr
| PMECR_WKEVT_LINK
);
573 if (status
& IRQ_RXPSI
)
574 handled
|= IRQ_RXPSI
;
576 if (status
& IRQ_TXI
) {
579 /* no lock here, tx queue should have been stopped */
581 /* update our idea of how much tx space is available to the
583 ks
->tx_space
= ks8851_rdreg16(ks
, KS_TXMIR
);
585 if (netif_msg_intr(ks
))
586 ks_dbg(ks
, "%s: txspace %d\n", __func__
, ks
->tx_space
);
589 if (status
& IRQ_RXI
)
592 if (status
& IRQ_SPIBEI
) {
593 dev_err(&ks
->spidev
->dev
, "%s: spi bus error\n", __func__
);
594 handled
|= IRQ_SPIBEI
;
597 ks8851_wrreg16(ks
, KS_ISR
, handled
);
599 if (status
& IRQ_RXI
) {
600 /* the datasheet says to disable the rx interrupt during
601 * packet read-out, however we're masking the interrupt
602 * from the device so do not bother masking just the RX
603 * from the device. */
608 /* if something stopped the rx process, probably due to wanting
609 * to change the rx settings, then do something about restarting
611 if (status
& IRQ_RXPSI
) {
612 struct ks8851_rxctrl
*rxc
= &ks
->rxctrl
;
614 /* update the multicast hash table */
615 ks8851_wrreg16(ks
, KS_MAHTR0
, rxc
->mchash
[0]);
616 ks8851_wrreg16(ks
, KS_MAHTR1
, rxc
->mchash
[1]);
617 ks8851_wrreg16(ks
, KS_MAHTR2
, rxc
->mchash
[2]);
618 ks8851_wrreg16(ks
, KS_MAHTR3
, rxc
->mchash
[3]);
620 ks8851_wrreg16(ks
, KS_RXCR2
, rxc
->rxcr2
);
621 ks8851_wrreg16(ks
, KS_RXCR1
, rxc
->rxcr1
);
624 mutex_unlock(&ks
->lock
);
626 if (status
& IRQ_TXI
)
627 netif_wake_queue(ks
->netdev
);
629 enable_irq(ks
->netdev
->irq
);
633 * calc_txlen - calculate size of message to send packet
634 * @len: Lenght of data
636 * Returns the size of the TXFIFO message needed to send
639 static inline unsigned calc_txlen(unsigned len
)
641 return ALIGN(len
+ 4, 4);
645 * ks8851_wrpkt - write packet to TX FIFO
646 * @ks: The device state.
647 * @txp: The sk_buff to transmit.
648 * @irq: IRQ on completion of the packet.
650 * Send the @txp to the chip. This means creating the relevant packet header
651 * specifying the length of the packet and the other information the chip
652 * needs, such as IRQ on completion. Send the header and the packet data to
655 static void ks8851_wrpkt(struct ks8851_net
*ks
, struct sk_buff
*txp
, bool irq
)
657 struct spi_transfer
*xfer
= ks
->spi_xfer2
;
658 struct spi_message
*msg
= &ks
->spi_msg2
;
662 if (netif_msg_tx_queued(ks
))
663 dev_dbg(&ks
->spidev
->dev
, "%s: skb %p, %d@%p, irq %d\n",
664 __func__
, txp
, txp
->len
, txp
->data
, irq
);
667 fid
&= TXFR_TXFID_MASK
;
670 fid
|= TXFR_TXIC
; /* irq on completion */
672 /* start header at txb[1] to align txw entries */
673 ks
->txh
.txb
[1] = KS_SPIOP_TXFIFO
;
674 ks
->txh
.txw
[1] = cpu_to_le16(fid
);
675 ks
->txh
.txw
[2] = cpu_to_le16(txp
->len
);
677 xfer
->tx_buf
= &ks
->txh
.txb
[1];
682 xfer
->tx_buf
= txp
->data
;
684 xfer
->len
= ALIGN(txp
->len
, 4);
686 ret
= spi_sync(ks
->spidev
, msg
);
688 ks_err(ks
, "%s: spi_sync() failed\n", __func__
);
692 * ks8851_done_tx - update and then free skbuff after transmitting
693 * @ks: The device state
694 * @txb: The buffer transmitted
696 static void ks8851_done_tx(struct ks8851_net
*ks
, struct sk_buff
*txb
)
698 struct net_device
*dev
= ks
->netdev
;
700 dev
->stats
.tx_bytes
+= txb
->len
;
701 dev
->stats
.tx_packets
++;
707 * ks8851_tx_work - process tx packet(s)
708 * @work: The work strucutre what was scheduled.
710 * This is called when a number of packets have been scheduled for
711 * transmission and need to be sent to the device.
713 static void ks8851_tx_work(struct work_struct
*work
)
715 struct ks8851_net
*ks
= container_of(work
, struct ks8851_net
, tx_work
);
717 bool last
= skb_queue_empty(&ks
->txq
);
719 mutex_lock(&ks
->lock
);
722 txb
= skb_dequeue(&ks
->txq
);
723 last
= skb_queue_empty(&ks
->txq
);
725 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
| RXQCR_SDA
);
726 ks8851_wrpkt(ks
, txb
, last
);
727 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
);
728 ks8851_wrreg16(ks
, KS_TXQCR
, TXQCR_METFE
);
730 ks8851_done_tx(ks
, txb
);
733 mutex_unlock(&ks
->lock
);
737 * ks8851_set_powermode - set power mode of the device
738 * @ks: The device state
739 * @pwrmode: The power mode value to write to KS_PMECR.
741 * Change the power mode of the chip.
743 static void ks8851_set_powermode(struct ks8851_net
*ks
, unsigned pwrmode
)
747 if (netif_msg_hw(ks
))
748 ks_dbg(ks
, "setting power mode %d\n", pwrmode
);
750 pmecr
= ks8851_rdreg16(ks
, KS_PMECR
);
751 pmecr
&= ~PMECR_PM_MASK
;
754 ks8851_wrreg16(ks
, KS_PMECR
, pmecr
);
758 * ks8851_net_open - open network device
759 * @dev: The network device being opened.
761 * Called when the network device is marked active, such as a user executing
762 * 'ifconfig up' on the device.
764 static int ks8851_net_open(struct net_device
*dev
)
766 struct ks8851_net
*ks
= netdev_priv(dev
);
768 /* lock the card, even if we may not actually be doing anything
769 * else at the moment */
770 mutex_lock(&ks
->lock
);
772 if (netif_msg_ifup(ks
))
773 ks_dbg(ks
, "opening %s\n", dev
->name
);
775 /* bring chip out of any power saving mode it was in */
776 ks8851_set_powermode(ks
, PMECR_PM_NORMAL
);
778 /* issue a soft reset to the RX/TX QMU to put it into a known
780 ks8851_soft_reset(ks
, GRR_QMU
);
782 /* setup transmission parameters */
784 ks8851_wrreg16(ks
, KS_TXCR
, (TXCR_TXE
| /* enable transmit process */
785 TXCR_TXPE
| /* pad to min length */
786 TXCR_TXCRC
| /* add CRC */
787 TXCR_TXFCE
)); /* enable flow control */
789 /* auto-increment tx data, reset tx pointer */
790 ks8851_wrreg16(ks
, KS_TXFDPR
, TXFDPR_TXFPAI
);
792 /* setup receiver control */
794 ks8851_wrreg16(ks
, KS_RXCR1
, (RXCR1_RXPAFMA
| /* from mac filter */
795 RXCR1_RXFCE
| /* enable flow control */
796 RXCR1_RXBE
| /* broadcast enable */
797 RXCR1_RXUE
| /* unicast enable */
798 RXCR1_RXE
)); /* enable rx block */
800 /* transfer entire frames out in one go */
801 ks8851_wrreg16(ks
, KS_RXCR2
, RXCR2_SRDBL_FRAME
);
803 /* set receive counter timeouts */
804 ks8851_wrreg16(ks
, KS_RXDTTR
, 1000); /* 1ms after first frame to IRQ */
805 ks8851_wrreg16(ks
, KS_RXDBCTR
, 4096); /* >4Kbytes in buffer to IRQ */
806 ks8851_wrreg16(ks
, KS_RXFCTR
, 10); /* 10 frames to IRQ */
808 ks
->rc_rxqcr
= (RXQCR_RXFCTE
| /* IRQ on frame count exceeded */
809 RXQCR_RXDBCTE
| /* IRQ on byte count exceeded */
810 RXQCR_RXDTTE
); /* IRQ on time exceeded */
812 ks8851_wrreg16(ks
, KS_RXQCR
, ks
->rc_rxqcr
);
814 /* clear then enable interrupts */
816 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
817 IRQ_TXI | /* TX done */ \
818 IRQ_RXI | /* RX done */ \
819 IRQ_SPIBEI | /* SPI bus error */ \
820 IRQ_TXPSI | /* TX process stop */ \
821 IRQ_RXPSI) /* RX process stop */
823 ks
->rc_ier
= STD_IRQ
;
824 ks8851_wrreg16(ks
, KS_ISR
, STD_IRQ
);
825 ks8851_wrreg16(ks
, KS_IER
, STD_IRQ
);
827 netif_start_queue(ks
->netdev
);
829 if (netif_msg_ifup(ks
))
830 ks_dbg(ks
, "network device %s up\n", dev
->name
);
832 mutex_unlock(&ks
->lock
);
837 * ks8851_net_stop - close network device
838 * @dev: The device being closed.
840 * Called to close down a network device which has been active. Cancell any
841 * work, shutdown the RX and TX process and then place the chip into a low
842 * power state whilst it is not being used.
844 static int ks8851_net_stop(struct net_device
*dev
)
846 struct ks8851_net
*ks
= netdev_priv(dev
);
848 if (netif_msg_ifdown(ks
))
849 ks_info(ks
, "%s: shutting down\n", dev
->name
);
851 netif_stop_queue(dev
);
853 mutex_lock(&ks
->lock
);
855 /* stop any outstanding work */
856 flush_work(&ks
->irq_work
);
857 flush_work(&ks
->tx_work
);
858 flush_work(&ks
->rxctrl_work
);
860 /* turn off the IRQs and ack any outstanding */
861 ks8851_wrreg16(ks
, KS_IER
, 0x0000);
862 ks8851_wrreg16(ks
, KS_ISR
, 0xffff);
864 /* shutdown RX process */
865 ks8851_wrreg16(ks
, KS_RXCR1
, 0x0000);
867 /* shutdown TX process */
868 ks8851_wrreg16(ks
, KS_TXCR
, 0x0000);
870 /* set powermode to soft power down to save power */
871 ks8851_set_powermode(ks
, PMECR_PM_SOFTDOWN
);
873 /* ensure any queued tx buffers are dumped */
874 while (!skb_queue_empty(&ks
->txq
)) {
875 struct sk_buff
*txb
= skb_dequeue(&ks
->txq
);
877 if (netif_msg_ifdown(ks
))
878 ks_dbg(ks
, "%s: freeing txb %p\n", __func__
, txb
);
883 mutex_unlock(&ks
->lock
);
888 * ks8851_start_xmit - transmit packet
889 * @skb: The buffer to transmit
890 * @dev: The device used to transmit the packet.
892 * Called by the network layer to transmit the @skb. Queue the packet for
893 * the device and schedule the necessary work to transmit the packet when
896 * We do this to firstly avoid sleeping with the network device locked,
897 * and secondly so we can round up more than one packet to transmit which
898 * means we can try and avoid generating too many transmit done interrupts.
900 static netdev_tx_t
ks8851_start_xmit(struct sk_buff
*skb
,
901 struct net_device
*dev
)
903 struct ks8851_net
*ks
= netdev_priv(dev
);
904 unsigned needed
= calc_txlen(skb
->len
);
905 netdev_tx_t ret
= NETDEV_TX_OK
;
907 if (netif_msg_tx_queued(ks
))
908 ks_dbg(ks
, "%s: skb %p, %d@%p\n", __func__
,
909 skb
, skb
->len
, skb
->data
);
911 spin_lock(&ks
->statelock
);
913 if (needed
> ks
->tx_space
) {
914 netif_stop_queue(dev
);
915 ret
= NETDEV_TX_BUSY
;
917 ks
->tx_space
-= needed
;
918 skb_queue_tail(&ks
->txq
, skb
);
921 spin_unlock(&ks
->statelock
);
922 schedule_work(&ks
->tx_work
);
928 * ks8851_rxctrl_work - work handler to change rx mode
929 * @work: The work structure this belongs to.
931 * Lock the device and issue the necessary changes to the receive mode from
932 * the network device layer. This is done so that we can do this without
933 * having to sleep whilst holding the network device lock.
935 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
936 * receive parameters are programmed, we issue a write to disable the RXQ and
937 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
938 * complete. The interrupt handler then writes the new values into the chip.
940 static void ks8851_rxctrl_work(struct work_struct
*work
)
942 struct ks8851_net
*ks
= container_of(work
, struct ks8851_net
, rxctrl_work
);
944 mutex_lock(&ks
->lock
);
946 /* need to shutdown RXQ before modifying filter parameters */
947 ks8851_wrreg16(ks
, KS_RXCR1
, 0x00);
949 mutex_unlock(&ks
->lock
);
952 static void ks8851_set_rx_mode(struct net_device
*dev
)
954 struct ks8851_net
*ks
= netdev_priv(dev
);
955 struct ks8851_rxctrl rxctrl
;
957 memset(&rxctrl
, 0, sizeof(rxctrl
));
959 if (dev
->flags
& IFF_PROMISC
) {
960 /* interface to receive everything */
962 rxctrl
.rxcr1
= RXCR1_RXAE
| RXCR1_RXINVF
;
963 } else if (dev
->flags
& IFF_ALLMULTI
) {
964 /* accept all multicast packets */
966 rxctrl
.rxcr1
= (RXCR1_RXME
| RXCR1_RXAE
|
967 RXCR1_RXPAFMA
| RXCR1_RXMAFMA
);
968 } else if (dev
->flags
& IFF_MULTICAST
&& dev
->mc_count
> 0) {
969 struct dev_mc_list
*mcptr
= dev
->mc_list
;
973 /* accept some multicast */
975 for (i
= dev
->mc_count
; i
> 0; i
--) {
976 crc
= ether_crc(ETH_ALEN
, mcptr
->dmi_addr
);
977 crc
>>= (32 - 6); /* get top six bits */
979 rxctrl
.mchash
[crc
>> 4] |= (1 << (crc
& 0xf));
983 rxctrl
.rxcr1
= RXCR1_RXME
| RXCR1_RXPAFMA
;
985 /* just accept broadcast / unicast */
986 rxctrl
.rxcr1
= RXCR1_RXPAFMA
;
989 rxctrl
.rxcr1
|= (RXCR1_RXUE
| /* unicast enable */
990 RXCR1_RXBE
| /* broadcast enable */
991 RXCR1_RXE
| /* RX process enable */
992 RXCR1_RXFCE
); /* enable flow control */
994 rxctrl
.rxcr2
|= RXCR2_SRDBL_FRAME
;
996 /* schedule work to do the actual set of the data if needed */
998 spin_lock(&ks
->statelock
);
1000 if (memcmp(&rxctrl
, &ks
->rxctrl
, sizeof(rxctrl
)) != 0) {
1001 memcpy(&ks
->rxctrl
, &rxctrl
, sizeof(ks
->rxctrl
));
1002 schedule_work(&ks
->rxctrl_work
);
1005 spin_unlock(&ks
->statelock
);
1008 static int ks8851_set_mac_address(struct net_device
*dev
, void *addr
)
1010 struct sockaddr
*sa
= addr
;
1012 if (netif_running(dev
))
1015 if (!is_valid_ether_addr(sa
->sa_data
))
1016 return -EADDRNOTAVAIL
;
1018 memcpy(dev
->dev_addr
, sa
->sa_data
, ETH_ALEN
);
1019 return ks8851_write_mac_addr(dev
);
1022 static int ks8851_net_ioctl(struct net_device
*dev
, struct ifreq
*req
, int cmd
)
1024 struct ks8851_net
*ks
= netdev_priv(dev
);
1026 if (!netif_running(dev
))
1029 return generic_mii_ioctl(&ks
->mii
, if_mii(req
), cmd
, NULL
);
1032 static const struct net_device_ops ks8851_netdev_ops
= {
1033 .ndo_open
= ks8851_net_open
,
1034 .ndo_stop
= ks8851_net_stop
,
1035 .ndo_do_ioctl
= ks8851_net_ioctl
,
1036 .ndo_start_xmit
= ks8851_start_xmit
,
1037 .ndo_set_mac_address
= ks8851_set_mac_address
,
1038 .ndo_set_rx_mode
= ks8851_set_rx_mode
,
1039 .ndo_change_mtu
= eth_change_mtu
,
1040 .ndo_validate_addr
= eth_validate_addr
,
1043 /* ethtool support */
1045 static void ks8851_get_drvinfo(struct net_device
*dev
,
1046 struct ethtool_drvinfo
*di
)
1048 strlcpy(di
->driver
, "KS8851", sizeof(di
->driver
));
1049 strlcpy(di
->version
, "1.00", sizeof(di
->version
));
1050 strlcpy(di
->bus_info
, dev_name(dev
->dev
.parent
), sizeof(di
->bus_info
));
1053 static u32
ks8851_get_msglevel(struct net_device
*dev
)
1055 struct ks8851_net
*ks
= netdev_priv(dev
);
1056 return ks
->msg_enable
;
1059 static void ks8851_set_msglevel(struct net_device
*dev
, u32 to
)
1061 struct ks8851_net
*ks
= netdev_priv(dev
);
1062 ks
->msg_enable
= to
;
1065 static int ks8851_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1067 struct ks8851_net
*ks
= netdev_priv(dev
);
1068 return mii_ethtool_gset(&ks
->mii
, cmd
);
1071 static int ks8851_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1073 struct ks8851_net
*ks
= netdev_priv(dev
);
1074 return mii_ethtool_sset(&ks
->mii
, cmd
);
1077 static u32
ks8851_get_link(struct net_device
*dev
)
1079 struct ks8851_net
*ks
= netdev_priv(dev
);
1080 return mii_link_ok(&ks
->mii
);
1083 static int ks8851_nway_reset(struct net_device
*dev
)
1085 struct ks8851_net
*ks
= netdev_priv(dev
);
1086 return mii_nway_restart(&ks
->mii
);
1089 static const struct ethtool_ops ks8851_ethtool_ops
= {
1090 .get_drvinfo
= ks8851_get_drvinfo
,
1091 .get_msglevel
= ks8851_get_msglevel
,
1092 .set_msglevel
= ks8851_set_msglevel
,
1093 .get_settings
= ks8851_get_settings
,
1094 .set_settings
= ks8851_set_settings
,
1095 .get_link
= ks8851_get_link
,
1096 .nway_reset
= ks8851_nway_reset
,
1099 /* MII interface controls */
1102 * ks8851_phy_reg - convert MII register into a KS8851 register
1103 * @reg: MII register number.
1105 * Return the KS8851 register number for the corresponding MII PHY register
1106 * if possible. Return zero if the MII register has no direct mapping to the
1107 * KS8851 register set.
1109 static int ks8851_phy_reg(int reg
)
1130 * ks8851_phy_read - MII interface PHY register read.
1131 * @dev: The network device the PHY is on.
1132 * @phy_addr: Address of PHY (ignored as we only have one)
1133 * @reg: The register to read.
1135 * This call reads data from the PHY register specified in @reg. Since the
1136 * device does not support all the MII registers, the non-existant values
1137 * are always returned as zero.
1139 * We return zero for unsupported registers as the MII code does not check
1140 * the value returned for any error status, and simply returns it to the
1141 * caller. The mii-tool that the driver was tested with takes any -ve error
1142 * as real PHY capabilities, thus displaying incorrect data to the user.
1144 static int ks8851_phy_read(struct net_device
*dev
, int phy_addr
, int reg
)
1146 struct ks8851_net
*ks
= netdev_priv(dev
);
1150 ksreg
= ks8851_phy_reg(reg
);
1152 return 0x0; /* no error return allowed, so use zero */
1154 mutex_lock(&ks
->lock
);
1155 result
= ks8851_rdreg16(ks
, ksreg
);
1156 mutex_unlock(&ks
->lock
);
1161 static void ks8851_phy_write(struct net_device
*dev
,
1162 int phy
, int reg
, int value
)
1164 struct ks8851_net
*ks
= netdev_priv(dev
);
1167 ksreg
= ks8851_phy_reg(reg
);
1169 mutex_lock(&ks
->lock
);
1170 ks8851_wrreg16(ks
, ksreg
, value
);
1171 mutex_unlock(&ks
->lock
);
1176 * ks8851_read_selftest - read the selftest memory info.
1177 * @ks: The device state
1179 * Read and check the TX/RX memory selftest information.
1181 static int ks8851_read_selftest(struct ks8851_net
*ks
)
1183 unsigned both_done
= MBIR_TXMBF
| MBIR_RXMBF
;
1187 rd
= ks8851_rdreg16(ks
, KS_MBIR
);
1189 if ((rd
& both_done
) != both_done
) {
1190 ks_warn(ks
, "Memory selftest not finished\n");
1194 if (rd
& MBIR_TXMBFA
) {
1195 ks_err(ks
, "TX memory selftest fail\n");
1199 if (rd
& MBIR_RXMBFA
) {
1200 ks_err(ks
, "RX memory selftest fail\n");
1207 /* driver bus management functions */
1209 static int __devinit
ks8851_probe(struct spi_device
*spi
)
1211 struct net_device
*ndev
;
1212 struct ks8851_net
*ks
;
1215 ndev
= alloc_etherdev(sizeof(struct ks8851_net
));
1217 dev_err(&spi
->dev
, "failed to alloc ethernet device\n");
1221 spi
->bits_per_word
= 8;
1223 ks
= netdev_priv(ndev
);
1227 ks
->tx_space
= 6144;
1229 mutex_init(&ks
->lock
);
1230 spin_lock_init(&ks
->statelock
);
1232 INIT_WORK(&ks
->tx_work
, ks8851_tx_work
);
1233 INIT_WORK(&ks
->irq_work
, ks8851_irq_work
);
1234 INIT_WORK(&ks
->rxctrl_work
, ks8851_rxctrl_work
);
1236 /* initialise pre-made spi transfer messages */
1238 spi_message_init(&ks
->spi_msg1
);
1239 spi_message_add_tail(&ks
->spi_xfer1
, &ks
->spi_msg1
);
1241 spi_message_init(&ks
->spi_msg2
);
1242 spi_message_add_tail(&ks
->spi_xfer2
[0], &ks
->spi_msg2
);
1243 spi_message_add_tail(&ks
->spi_xfer2
[1], &ks
->spi_msg2
);
1245 /* setup mii state */
1248 ks
->mii
.phy_id_mask
= 1;
1249 ks
->mii
.reg_num_mask
= 0xf;
1250 ks
->mii
.mdio_read
= ks8851_phy_read
;
1251 ks
->mii
.mdio_write
= ks8851_phy_write
;
1253 dev_info(&spi
->dev
, "message enable is %d\n", msg_enable
);
1255 /* set the default message enable */
1256 ks
->msg_enable
= netif_msg_init(msg_enable
, (NETIF_MSG_DRV
|
1260 skb_queue_head_init(&ks
->txq
);
1262 SET_ETHTOOL_OPS(ndev
, &ks8851_ethtool_ops
);
1263 SET_NETDEV_DEV(ndev
, &spi
->dev
);
1265 dev_set_drvdata(&spi
->dev
, ks
);
1267 ndev
->if_port
= IF_PORT_100BASET
;
1268 ndev
->netdev_ops
= &ks8851_netdev_ops
;
1269 ndev
->irq
= spi
->irq
;
1271 /* issue a global soft reset to reset the device. */
1272 ks8851_soft_reset(ks
, GRR_GSR
);
1274 /* simple check for a valid chip being connected to the bus */
1276 if ((ks8851_rdreg16(ks
, KS_CIDER
) & ~CIDER_REV_MASK
) != CIDER_ID
) {
1277 dev_err(&spi
->dev
, "failed to read device ID\n");
1282 ks8851_read_selftest(ks
);
1283 ks8851_init_mac(ks
);
1285 ret
= request_irq(spi
->irq
, ks8851_irq
, IRQF_TRIGGER_LOW
,
1288 dev_err(&spi
->dev
, "failed to get irq\n");
1292 ret
= register_netdev(ndev
);
1294 dev_err(&spi
->dev
, "failed to register network device\n");
1298 dev_info(&spi
->dev
, "revision %d, MAC %pM, IRQ %d\n",
1299 CIDER_REV_GET(ks8851_rdreg16(ks
, KS_CIDER
)),
1300 ndev
->dev_addr
, ndev
->irq
);
1306 free_irq(ndev
->irq
, ndev
);
1314 static int __devexit
ks8851_remove(struct spi_device
*spi
)
1316 struct ks8851_net
*priv
= dev_get_drvdata(&spi
->dev
);
1318 if (netif_msg_drv(priv
))
1319 dev_info(&spi
->dev
, "remove");
1321 unregister_netdev(priv
->netdev
);
1322 free_irq(spi
->irq
, priv
);
1323 free_netdev(priv
->netdev
);
1328 static struct spi_driver ks8851_driver
= {
1331 .owner
= THIS_MODULE
,
1333 .probe
= ks8851_probe
,
1334 .remove
= __devexit_p(ks8851_remove
),
1337 static int __init
ks8851_init(void)
1339 return spi_register_driver(&ks8851_driver
);
1342 static void __exit
ks8851_exit(void)
1344 spi_unregister_driver(&ks8851_driver
);
1347 module_init(ks8851_init
);
1348 module_exit(ks8851_exit
);
1350 MODULE_DESCRIPTION("KS8851 Network driver");
1351 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1352 MODULE_LICENSE("GPL");
1354 module_param_named(message
, msg_enable
, int, 0);
1355 MODULE_PARM_DESC(message
, "Message verbosity level (0=none, 31=all)");
1356 MODULE_ALIAS("spi:ks8851");