Linux 3.11-rc3
[cris-mirror.git] / drivers / net / ethernet / micrel / ks8851.c
blob727b546a9eb844c909dd8cd145aacfab014c9f99
1 /* drivers/net/ethernet/micrel/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
14 #define DEBUG
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/cache.h>
23 #include <linux/crc32.h>
24 #include <linux/mii.h>
25 #include <linux/eeprom_93cx6.h>
27 #include <linux/spi/spi.h>
29 #include "ks8851.h"
31 /**
32 * struct ks8851_rxctrl - KS8851 driver rx control
33 * @mchash: Multicast hash-table data.
34 * @rxcr1: KS_RXCR1 register setting
35 * @rxcr2: KS_RXCR2 register setting
37 * Representation of the settings needs to control the receive filtering
38 * such as the multicast hash-filter and the receive register settings. This
39 * is used to make the job of working out if the receive settings change and
40 * then issuing the new settings to the worker that will send the necessary
41 * commands.
43 struct ks8851_rxctrl {
44 u16 mchash[4];
45 u16 rxcr1;
46 u16 rxcr2;
49 /**
50 * union ks8851_tx_hdr - tx header data
51 * @txb: The header as bytes
52 * @txw: The header as 16bit, little-endian words
54 * A dual representation of the tx header data to allow
55 * access to individual bytes, and to allow 16bit accesses
56 * with 16bit alignment.
58 union ks8851_tx_hdr {
59 u8 txb[6];
60 __le16 txw[3];
63 /**
64 * struct ks8851_net - KS8851 driver private data
65 * @netdev: The network device we're bound to
66 * @spidev: The spi device we're bound to.
67 * @lock: Lock to ensure that the device is not accessed when busy.
68 * @statelock: Lock on this structure for tx list.
69 * @mii: The MII state information for the mii calls.
70 * @rxctrl: RX settings for @rxctrl_work.
71 * @tx_work: Work queue for tx packets
72 * @rxctrl_work: Work queue for updating RX mode and multicast lists
73 * @txq: Queue of packets for transmission.
74 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
75 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
76 * @txh: Space for generating packet TX header in DMA-able data
77 * @rxd: Space for receiving SPI data, in DMA-able space.
78 * @txd: Space for transmitting SPI data, in DMA-able space.
79 * @msg_enable: The message flags controlling driver output (see ethtool).
80 * @fid: Incrementing frame id tag.
81 * @rc_ier: Cached copy of KS_IER.
82 * @rc_ccr: Cached copy of KS_CCR.
83 * @rc_rxqcr: Cached copy of KS_RXQCR.
84 * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
85 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
87 * The @lock ensures that the chip is protected when certain operations are
88 * in progress. When the read or write packet transfer is in progress, most
89 * of the chip registers are not ccessible until the transfer is finished and
90 * the DMA has been de-asserted.
92 * The @statelock is used to protect information in the structure which may
93 * need to be accessed via several sources, such as the network driver layer
94 * or one of the work queues.
96 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
97 * wants to DMA map them, it will not have any problems with data the driver
98 * modifies.
100 struct ks8851_net {
101 struct net_device *netdev;
102 struct spi_device *spidev;
103 struct mutex lock;
104 spinlock_t statelock;
106 union ks8851_tx_hdr txh ____cacheline_aligned;
107 u8 rxd[8];
108 u8 txd[8];
110 u32 msg_enable ____cacheline_aligned;
111 u16 tx_space;
112 u8 fid;
114 u16 rc_ier;
115 u16 rc_rxqcr;
116 u16 rc_ccr;
117 u16 eeprom_size;
119 struct mii_if_info mii;
120 struct ks8851_rxctrl rxctrl;
122 struct work_struct tx_work;
123 struct work_struct rxctrl_work;
125 struct sk_buff_head txq;
127 struct spi_message spi_msg1;
128 struct spi_message spi_msg2;
129 struct spi_transfer spi_xfer1;
130 struct spi_transfer spi_xfer2[2];
132 struct eeprom_93cx6 eeprom;
135 static int msg_enable;
137 /* shift for byte-enable data */
138 #define BYTE_EN(_x) ((_x) << 2)
140 /* turn register number and byte-enable mask into data for start of packet */
141 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
143 /* SPI register read/write calls.
145 * All these calls issue SPI transactions to access the chip's registers. They
146 * all require that the necessary lock is held to prevent accesses when the
147 * chip is busy transferring packet data (RX/TX FIFO accesses).
151 * ks8851_wrreg16 - write 16bit register value to chip
152 * @ks: The chip state
153 * @reg: The register address
154 * @val: The value to write
156 * Issue a write to put the value @val into the register specified in @reg.
158 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
160 struct spi_transfer *xfer = &ks->spi_xfer1;
161 struct spi_message *msg = &ks->spi_msg1;
162 __le16 txb[2];
163 int ret;
165 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
166 txb[1] = cpu_to_le16(val);
168 xfer->tx_buf = txb;
169 xfer->rx_buf = NULL;
170 xfer->len = 4;
172 ret = spi_sync(ks->spidev, msg);
173 if (ret < 0)
174 netdev_err(ks->netdev, "spi_sync() failed\n");
178 * ks8851_wrreg8 - write 8bit register value to chip
179 * @ks: The chip state
180 * @reg: The register address
181 * @val: The value to write
183 * Issue a write to put the value @val into the register specified in @reg.
185 static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
187 struct spi_transfer *xfer = &ks->spi_xfer1;
188 struct spi_message *msg = &ks->spi_msg1;
189 __le16 txb[2];
190 int ret;
191 int bit;
193 bit = 1 << (reg & 3);
195 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
196 txb[1] = val;
198 xfer->tx_buf = txb;
199 xfer->rx_buf = NULL;
200 xfer->len = 3;
202 ret = spi_sync(ks->spidev, msg);
203 if (ret < 0)
204 netdev_err(ks->netdev, "spi_sync() failed\n");
208 * ks8851_rx_1msg - select whether to use one or two messages for spi read
209 * @ks: The device structure
211 * Return whether to generate a single message with a tx and rx buffer
212 * supplied to spi_sync(), or alternatively send the tx and rx buffers
213 * as separate messages.
215 * Depending on the hardware in use, a single message may be more efficient
216 * on interrupts or work done by the driver.
218 * This currently always returns true until we add some per-device data passed
219 * from the platform code to specify which mode is better.
221 static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
223 return true;
227 * ks8851_rdreg - issue read register command and return the data
228 * @ks: The device state
229 * @op: The register address and byte enables in message format.
230 * @rxb: The RX buffer to return the result into
231 * @rxl: The length of data expected.
233 * This is the low level read call that issues the necessary spi message(s)
234 * to read data from the register specified in @op.
236 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
237 u8 *rxb, unsigned rxl)
239 struct spi_transfer *xfer;
240 struct spi_message *msg;
241 __le16 *txb = (__le16 *)ks->txd;
242 u8 *trx = ks->rxd;
243 int ret;
245 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
247 if (ks8851_rx_1msg(ks)) {
248 msg = &ks->spi_msg1;
249 xfer = &ks->spi_xfer1;
251 xfer->tx_buf = txb;
252 xfer->rx_buf = trx;
253 xfer->len = rxl + 2;
254 } else {
255 msg = &ks->spi_msg2;
256 xfer = ks->spi_xfer2;
258 xfer->tx_buf = txb;
259 xfer->rx_buf = NULL;
260 xfer->len = 2;
262 xfer++;
263 xfer->tx_buf = NULL;
264 xfer->rx_buf = trx;
265 xfer->len = rxl;
268 ret = spi_sync(ks->spidev, msg);
269 if (ret < 0)
270 netdev_err(ks->netdev, "read: spi_sync() failed\n");
271 else if (ks8851_rx_1msg(ks))
272 memcpy(rxb, trx + 2, rxl);
273 else
274 memcpy(rxb, trx, rxl);
278 * ks8851_rdreg8 - read 8 bit register from device
279 * @ks: The chip information
280 * @reg: The register address
282 * Read a 8bit register from the chip, returning the result
284 static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
286 u8 rxb[1];
288 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
289 return rxb[0];
293 * ks8851_rdreg16 - read 16 bit register from device
294 * @ks: The chip information
295 * @reg: The register address
297 * Read a 16bit register from the chip, returning the result
299 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
301 __le16 rx = 0;
303 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
304 return le16_to_cpu(rx);
308 * ks8851_rdreg32 - read 32 bit register from device
309 * @ks: The chip information
310 * @reg: The register address
312 * Read a 32bit register from the chip.
314 * Note, this read requires the address be aligned to 4 bytes.
316 static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
318 __le32 rx = 0;
320 WARN_ON(reg & 3);
322 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
323 return le32_to_cpu(rx);
327 * ks8851_soft_reset - issue one of the soft reset to the device
328 * @ks: The device state.
329 * @op: The bit(s) to set in the GRR
331 * Issue the relevant soft-reset command to the device's GRR register
332 * specified by @op.
334 * Note, the delays are in there as a caution to ensure that the reset
335 * has time to take effect and then complete. Since the datasheet does
336 * not currently specify the exact sequence, we have chosen something
337 * that seems to work with our device.
339 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
341 ks8851_wrreg16(ks, KS_GRR, op);
342 mdelay(1); /* wait a short time to effect reset */
343 ks8851_wrreg16(ks, KS_GRR, 0);
344 mdelay(1); /* wait for condition to clear */
348 * ks8851_set_powermode - set power mode of the device
349 * @ks: The device state
350 * @pwrmode: The power mode value to write to KS_PMECR.
352 * Change the power mode of the chip.
354 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
356 unsigned pmecr;
358 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
360 pmecr = ks8851_rdreg16(ks, KS_PMECR);
361 pmecr &= ~PMECR_PM_MASK;
362 pmecr |= pwrmode;
364 ks8851_wrreg16(ks, KS_PMECR, pmecr);
368 * ks8851_write_mac_addr - write mac address to device registers
369 * @dev: The network device
371 * Update the KS8851 MAC address registers from the address in @dev.
373 * This call assumes that the chip is not running, so there is no need to
374 * shutdown the RXQ process whilst setting this.
376 static int ks8851_write_mac_addr(struct net_device *dev)
378 struct ks8851_net *ks = netdev_priv(dev);
379 int i;
381 mutex_lock(&ks->lock);
384 * Wake up chip in case it was powered off when stopped; otherwise,
385 * the first write to the MAC address does not take effect.
387 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
388 for (i = 0; i < ETH_ALEN; i++)
389 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
390 if (!netif_running(dev))
391 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
393 mutex_unlock(&ks->lock);
395 return 0;
399 * ks8851_read_mac_addr - read mac address from device registers
400 * @dev: The network device
402 * Update our copy of the KS8851 MAC address from the registers of @dev.
404 static void ks8851_read_mac_addr(struct net_device *dev)
406 struct ks8851_net *ks = netdev_priv(dev);
407 int i;
409 mutex_lock(&ks->lock);
411 for (i = 0; i < ETH_ALEN; i++)
412 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
414 mutex_unlock(&ks->lock);
418 * ks8851_init_mac - initialise the mac address
419 * @ks: The device structure
421 * Get or create the initial mac address for the device and then set that
422 * into the station address register. If there is an EEPROM present, then
423 * we try that. If no valid mac address is found we use eth_random_addr()
424 * to create a new one.
426 static void ks8851_init_mac(struct ks8851_net *ks)
428 struct net_device *dev = ks->netdev;
430 /* first, try reading what we've got already */
431 if (ks->rc_ccr & CCR_EEPROM) {
432 ks8851_read_mac_addr(dev);
433 if (is_valid_ether_addr(dev->dev_addr))
434 return;
436 netdev_err(ks->netdev, "invalid mac address read %pM\n",
437 dev->dev_addr);
440 eth_hw_addr_random(dev);
441 ks8851_write_mac_addr(dev);
445 * ks8851_rdfifo - read data from the receive fifo
446 * @ks: The device state.
447 * @buff: The buffer address
448 * @len: The length of the data to read
450 * Issue an RXQ FIFO read command and read the @len amount of data from
451 * the FIFO into the buffer specified by @buff.
453 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
455 struct spi_transfer *xfer = ks->spi_xfer2;
456 struct spi_message *msg = &ks->spi_msg2;
457 u8 txb[1];
458 int ret;
460 netif_dbg(ks, rx_status, ks->netdev,
461 "%s: %d@%p\n", __func__, len, buff);
463 /* set the operation we're issuing */
464 txb[0] = KS_SPIOP_RXFIFO;
466 xfer->tx_buf = txb;
467 xfer->rx_buf = NULL;
468 xfer->len = 1;
470 xfer++;
471 xfer->rx_buf = buff;
472 xfer->tx_buf = NULL;
473 xfer->len = len;
475 ret = spi_sync(ks->spidev, msg);
476 if (ret < 0)
477 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
481 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
482 * @ks: The device state
483 * @rxpkt: The data for the received packet
485 * Dump the initial data from the packet to dev_dbg().
487 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
489 netdev_dbg(ks->netdev,
490 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
491 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
492 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
493 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
497 * ks8851_rx_pkts - receive packets from the host
498 * @ks: The device information.
500 * This is called from the IRQ work queue when the system detects that there
501 * are packets in the receive queue. Find out how many packets there are and
502 * read them from the FIFO.
504 static void ks8851_rx_pkts(struct ks8851_net *ks)
506 struct sk_buff *skb;
507 unsigned rxfc;
508 unsigned rxlen;
509 unsigned rxstat;
510 u32 rxh;
511 u8 *rxpkt;
513 rxfc = ks8851_rdreg8(ks, KS_RXFC);
515 netif_dbg(ks, rx_status, ks->netdev,
516 "%s: %d packets\n", __func__, rxfc);
518 /* Currently we're issuing a read per packet, but we could possibly
519 * improve the code by issuing a single read, getting the receive
520 * header, allocating the packet and then reading the packet data
521 * out in one go.
523 * This form of operation would require us to hold the SPI bus'
524 * chipselect low during the entie transaction to avoid any
525 * reset to the data stream coming from the chip.
528 for (; rxfc != 0; rxfc--) {
529 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
530 rxstat = rxh & 0xffff;
531 rxlen = (rxh >> 16) & 0xfff;
533 netif_dbg(ks, rx_status, ks->netdev,
534 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
536 /* the length of the packet includes the 32bit CRC */
538 /* set dma read address */
539 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
541 /* start the packet dma process, and set auto-dequeue rx */
542 ks8851_wrreg16(ks, KS_RXQCR,
543 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
545 if (rxlen > 4) {
546 unsigned int rxalign;
548 rxlen -= 4;
549 rxalign = ALIGN(rxlen, 4);
550 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
551 if (skb) {
553 /* 4 bytes of status header + 4 bytes of
554 * garbage: we put them before ethernet
555 * header, so that they are copied,
556 * but ignored.
559 rxpkt = skb_put(skb, rxlen) - 8;
561 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
563 if (netif_msg_pktdata(ks))
564 ks8851_dbg_dumpkkt(ks, rxpkt);
566 skb->protocol = eth_type_trans(skb, ks->netdev);
567 netif_rx_ni(skb);
569 ks->netdev->stats.rx_packets++;
570 ks->netdev->stats.rx_bytes += rxlen;
574 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
579 * ks8851_irq - IRQ handler for dealing with interrupt requests
580 * @irq: IRQ number
581 * @_ks: cookie
583 * This handler is invoked when the IRQ line asserts to find out what happened.
584 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
585 * in thread context.
587 * Read the interrupt status, work out what needs to be done and then clear
588 * any of the interrupts that are not needed.
590 static irqreturn_t ks8851_irq(int irq, void *_ks)
592 struct ks8851_net *ks = _ks;
593 unsigned status;
594 unsigned handled = 0;
596 mutex_lock(&ks->lock);
598 status = ks8851_rdreg16(ks, KS_ISR);
600 netif_dbg(ks, intr, ks->netdev,
601 "%s: status 0x%04x\n", __func__, status);
603 if (status & IRQ_LCI)
604 handled |= IRQ_LCI;
606 if (status & IRQ_LDI) {
607 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
608 pmecr &= ~PMECR_WKEVT_MASK;
609 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
611 handled |= IRQ_LDI;
614 if (status & IRQ_RXPSI)
615 handled |= IRQ_RXPSI;
617 if (status & IRQ_TXI) {
618 handled |= IRQ_TXI;
620 /* no lock here, tx queue should have been stopped */
622 /* update our idea of how much tx space is available to the
623 * system */
624 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
626 netif_dbg(ks, intr, ks->netdev,
627 "%s: txspace %d\n", __func__, ks->tx_space);
630 if (status & IRQ_RXI)
631 handled |= IRQ_RXI;
633 if (status & IRQ_SPIBEI) {
634 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
635 handled |= IRQ_SPIBEI;
638 ks8851_wrreg16(ks, KS_ISR, handled);
640 if (status & IRQ_RXI) {
641 /* the datasheet says to disable the rx interrupt during
642 * packet read-out, however we're masking the interrupt
643 * from the device so do not bother masking just the RX
644 * from the device. */
646 ks8851_rx_pkts(ks);
649 /* if something stopped the rx process, probably due to wanting
650 * to change the rx settings, then do something about restarting
651 * it. */
652 if (status & IRQ_RXPSI) {
653 struct ks8851_rxctrl *rxc = &ks->rxctrl;
655 /* update the multicast hash table */
656 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
657 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
658 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
659 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
661 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
662 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
665 mutex_unlock(&ks->lock);
667 if (status & IRQ_LCI)
668 mii_check_link(&ks->mii);
670 if (status & IRQ_TXI)
671 netif_wake_queue(ks->netdev);
673 return IRQ_HANDLED;
677 * calc_txlen - calculate size of message to send packet
678 * @len: Length of data
680 * Returns the size of the TXFIFO message needed to send
681 * this packet.
683 static inline unsigned calc_txlen(unsigned len)
685 return ALIGN(len + 4, 4);
689 * ks8851_wrpkt - write packet to TX FIFO
690 * @ks: The device state.
691 * @txp: The sk_buff to transmit.
692 * @irq: IRQ on completion of the packet.
694 * Send the @txp to the chip. This means creating the relevant packet header
695 * specifying the length of the packet and the other information the chip
696 * needs, such as IRQ on completion. Send the header and the packet data to
697 * the device.
699 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
701 struct spi_transfer *xfer = ks->spi_xfer2;
702 struct spi_message *msg = &ks->spi_msg2;
703 unsigned fid = 0;
704 int ret;
706 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
707 __func__, txp, txp->len, txp->data, irq);
709 fid = ks->fid++;
710 fid &= TXFR_TXFID_MASK;
712 if (irq)
713 fid |= TXFR_TXIC; /* irq on completion */
715 /* start header at txb[1] to align txw entries */
716 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
717 ks->txh.txw[1] = cpu_to_le16(fid);
718 ks->txh.txw[2] = cpu_to_le16(txp->len);
720 xfer->tx_buf = &ks->txh.txb[1];
721 xfer->rx_buf = NULL;
722 xfer->len = 5;
724 xfer++;
725 xfer->tx_buf = txp->data;
726 xfer->rx_buf = NULL;
727 xfer->len = ALIGN(txp->len, 4);
729 ret = spi_sync(ks->spidev, msg);
730 if (ret < 0)
731 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
735 * ks8851_done_tx - update and then free skbuff after transmitting
736 * @ks: The device state
737 * @txb: The buffer transmitted
739 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
741 struct net_device *dev = ks->netdev;
743 dev->stats.tx_bytes += txb->len;
744 dev->stats.tx_packets++;
746 dev_kfree_skb(txb);
750 * ks8851_tx_work - process tx packet(s)
751 * @work: The work strucutre what was scheduled.
753 * This is called when a number of packets have been scheduled for
754 * transmission and need to be sent to the device.
756 static void ks8851_tx_work(struct work_struct *work)
758 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
759 struct sk_buff *txb;
760 bool last = skb_queue_empty(&ks->txq);
762 mutex_lock(&ks->lock);
764 while (!last) {
765 txb = skb_dequeue(&ks->txq);
766 last = skb_queue_empty(&ks->txq);
768 if (txb != NULL) {
769 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
770 ks8851_wrpkt(ks, txb, last);
771 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
772 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
774 ks8851_done_tx(ks, txb);
778 mutex_unlock(&ks->lock);
782 * ks8851_net_open - open network device
783 * @dev: The network device being opened.
785 * Called when the network device is marked active, such as a user executing
786 * 'ifconfig up' on the device.
788 static int ks8851_net_open(struct net_device *dev)
790 struct ks8851_net *ks = netdev_priv(dev);
792 /* lock the card, even if we may not actually be doing anything
793 * else at the moment */
794 mutex_lock(&ks->lock);
796 netif_dbg(ks, ifup, ks->netdev, "opening\n");
798 /* bring chip out of any power saving mode it was in */
799 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
801 /* issue a soft reset to the RX/TX QMU to put it into a known
802 * state. */
803 ks8851_soft_reset(ks, GRR_QMU);
805 /* setup transmission parameters */
807 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
808 TXCR_TXPE | /* pad to min length */
809 TXCR_TXCRC | /* add CRC */
810 TXCR_TXFCE)); /* enable flow control */
812 /* auto-increment tx data, reset tx pointer */
813 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
815 /* setup receiver control */
817 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
818 RXCR1_RXFCE | /* enable flow control */
819 RXCR1_RXBE | /* broadcast enable */
820 RXCR1_RXUE | /* unicast enable */
821 RXCR1_RXE)); /* enable rx block */
823 /* transfer entire frames out in one go */
824 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
826 /* set receive counter timeouts */
827 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
828 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
829 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
831 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
832 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
833 RXQCR_RXDTTE); /* IRQ on time exceeded */
835 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
837 /* clear then enable interrupts */
839 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
840 IRQ_TXI | /* TX done */ \
841 IRQ_RXI | /* RX done */ \
842 IRQ_SPIBEI | /* SPI bus error */ \
843 IRQ_TXPSI | /* TX process stop */ \
844 IRQ_RXPSI) /* RX process stop */
846 ks->rc_ier = STD_IRQ;
847 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
848 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
850 netif_start_queue(ks->netdev);
852 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
854 mutex_unlock(&ks->lock);
855 return 0;
859 * ks8851_net_stop - close network device
860 * @dev: The device being closed.
862 * Called to close down a network device which has been active. Cancell any
863 * work, shutdown the RX and TX process and then place the chip into a low
864 * power state whilst it is not being used.
866 static int ks8851_net_stop(struct net_device *dev)
868 struct ks8851_net *ks = netdev_priv(dev);
870 netif_info(ks, ifdown, dev, "shutting down\n");
872 netif_stop_queue(dev);
874 mutex_lock(&ks->lock);
875 /* turn off the IRQs and ack any outstanding */
876 ks8851_wrreg16(ks, KS_IER, 0x0000);
877 ks8851_wrreg16(ks, KS_ISR, 0xffff);
878 mutex_unlock(&ks->lock);
880 /* stop any outstanding work */
881 flush_work(&ks->tx_work);
882 flush_work(&ks->rxctrl_work);
884 mutex_lock(&ks->lock);
885 /* shutdown RX process */
886 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
888 /* shutdown TX process */
889 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
891 /* set powermode to soft power down to save power */
892 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
893 mutex_unlock(&ks->lock);
895 /* ensure any queued tx buffers are dumped */
896 while (!skb_queue_empty(&ks->txq)) {
897 struct sk_buff *txb = skb_dequeue(&ks->txq);
899 netif_dbg(ks, ifdown, ks->netdev,
900 "%s: freeing txb %p\n", __func__, txb);
902 dev_kfree_skb(txb);
905 return 0;
909 * ks8851_start_xmit - transmit packet
910 * @skb: The buffer to transmit
911 * @dev: The device used to transmit the packet.
913 * Called by the network layer to transmit the @skb. Queue the packet for
914 * the device and schedule the necessary work to transmit the packet when
915 * it is free.
917 * We do this to firstly avoid sleeping with the network device locked,
918 * and secondly so we can round up more than one packet to transmit which
919 * means we can try and avoid generating too many transmit done interrupts.
921 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
922 struct net_device *dev)
924 struct ks8851_net *ks = netdev_priv(dev);
925 unsigned needed = calc_txlen(skb->len);
926 netdev_tx_t ret = NETDEV_TX_OK;
928 netif_dbg(ks, tx_queued, ks->netdev,
929 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
931 spin_lock(&ks->statelock);
933 if (needed > ks->tx_space) {
934 netif_stop_queue(dev);
935 ret = NETDEV_TX_BUSY;
936 } else {
937 ks->tx_space -= needed;
938 skb_queue_tail(&ks->txq, skb);
941 spin_unlock(&ks->statelock);
942 schedule_work(&ks->tx_work);
944 return ret;
948 * ks8851_rxctrl_work - work handler to change rx mode
949 * @work: The work structure this belongs to.
951 * Lock the device and issue the necessary changes to the receive mode from
952 * the network device layer. This is done so that we can do this without
953 * having to sleep whilst holding the network device lock.
955 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
956 * receive parameters are programmed, we issue a write to disable the RXQ and
957 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
958 * complete. The interrupt handler then writes the new values into the chip.
960 static void ks8851_rxctrl_work(struct work_struct *work)
962 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
964 mutex_lock(&ks->lock);
966 /* need to shutdown RXQ before modifying filter parameters */
967 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
969 mutex_unlock(&ks->lock);
972 static void ks8851_set_rx_mode(struct net_device *dev)
974 struct ks8851_net *ks = netdev_priv(dev);
975 struct ks8851_rxctrl rxctrl;
977 memset(&rxctrl, 0, sizeof(rxctrl));
979 if (dev->flags & IFF_PROMISC) {
980 /* interface to receive everything */
982 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
983 } else if (dev->flags & IFF_ALLMULTI) {
984 /* accept all multicast packets */
986 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
987 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
988 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
989 struct netdev_hw_addr *ha;
990 u32 crc;
992 /* accept some multicast */
994 netdev_for_each_mc_addr(ha, dev) {
995 crc = ether_crc(ETH_ALEN, ha->addr);
996 crc >>= (32 - 6); /* get top six bits */
998 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1001 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1002 } else {
1003 /* just accept broadcast / unicast */
1004 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1007 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1008 RXCR1_RXBE | /* broadcast enable */
1009 RXCR1_RXE | /* RX process enable */
1010 RXCR1_RXFCE); /* enable flow control */
1012 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1014 /* schedule work to do the actual set of the data if needed */
1016 spin_lock(&ks->statelock);
1018 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1019 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1020 schedule_work(&ks->rxctrl_work);
1023 spin_unlock(&ks->statelock);
1026 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1028 struct sockaddr *sa = addr;
1030 if (netif_running(dev))
1031 return -EBUSY;
1033 if (!is_valid_ether_addr(sa->sa_data))
1034 return -EADDRNOTAVAIL;
1036 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1037 return ks8851_write_mac_addr(dev);
1040 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1042 struct ks8851_net *ks = netdev_priv(dev);
1044 if (!netif_running(dev))
1045 return -EINVAL;
1047 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1050 static const struct net_device_ops ks8851_netdev_ops = {
1051 .ndo_open = ks8851_net_open,
1052 .ndo_stop = ks8851_net_stop,
1053 .ndo_do_ioctl = ks8851_net_ioctl,
1054 .ndo_start_xmit = ks8851_start_xmit,
1055 .ndo_set_mac_address = ks8851_set_mac_address,
1056 .ndo_set_rx_mode = ks8851_set_rx_mode,
1057 .ndo_change_mtu = eth_change_mtu,
1058 .ndo_validate_addr = eth_validate_addr,
1061 /* ethtool support */
1063 static void ks8851_get_drvinfo(struct net_device *dev,
1064 struct ethtool_drvinfo *di)
1066 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1067 strlcpy(di->version, "1.00", sizeof(di->version));
1068 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1071 static u32 ks8851_get_msglevel(struct net_device *dev)
1073 struct ks8851_net *ks = netdev_priv(dev);
1074 return ks->msg_enable;
1077 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1079 struct ks8851_net *ks = netdev_priv(dev);
1080 ks->msg_enable = to;
1083 static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1085 struct ks8851_net *ks = netdev_priv(dev);
1086 return mii_ethtool_gset(&ks->mii, cmd);
1089 static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1091 struct ks8851_net *ks = netdev_priv(dev);
1092 return mii_ethtool_sset(&ks->mii, cmd);
1095 static u32 ks8851_get_link(struct net_device *dev)
1097 struct ks8851_net *ks = netdev_priv(dev);
1098 return mii_link_ok(&ks->mii);
1101 static int ks8851_nway_reset(struct net_device *dev)
1103 struct ks8851_net *ks = netdev_priv(dev);
1104 return mii_nway_restart(&ks->mii);
1107 /* EEPROM support */
1109 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1111 struct ks8851_net *ks = ee->data;
1112 unsigned val;
1114 val = ks8851_rdreg16(ks, KS_EEPCR);
1116 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1117 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1118 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1121 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1123 struct ks8851_net *ks = ee->data;
1124 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1126 if (ee->drive_data)
1127 val |= EEPCR_EESRWA;
1128 if (ee->reg_data_in)
1129 val |= EEPCR_EEDO;
1130 if (ee->reg_data_clock)
1131 val |= EEPCR_EESCK;
1132 if (ee->reg_chip_select)
1133 val |= EEPCR_EECS;
1135 ks8851_wrreg16(ks, KS_EEPCR, val);
1139 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1140 * @ks: The network device state.
1142 * Check for the presence of an EEPROM, and then activate software access
1143 * to the device.
1145 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1147 if (!(ks->rc_ccr & CCR_EEPROM))
1148 return -ENOENT;
1150 mutex_lock(&ks->lock);
1152 /* start with clock low, cs high */
1153 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1154 return 0;
1158 * ks8851_eeprom_release - release the EEPROM interface
1159 * @ks: The device state
1161 * Release the software access to the device EEPROM
1163 static void ks8851_eeprom_release(struct ks8851_net *ks)
1165 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1167 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1168 mutex_unlock(&ks->lock);
1171 #define KS_EEPROM_MAGIC (0x00008851)
1173 static int ks8851_set_eeprom(struct net_device *dev,
1174 struct ethtool_eeprom *ee, u8 *data)
1176 struct ks8851_net *ks = netdev_priv(dev);
1177 int offset = ee->offset;
1178 int len = ee->len;
1179 u16 tmp;
1181 /* currently only support byte writing */
1182 if (len != 1)
1183 return -EINVAL;
1185 if (ee->magic != KS_EEPROM_MAGIC)
1186 return -EINVAL;
1188 if (ks8851_eeprom_claim(ks))
1189 return -ENOENT;
1191 eeprom_93cx6_wren(&ks->eeprom, true);
1193 /* ethtool currently only supports writing bytes, which means
1194 * we have to read/modify/write our 16bit EEPROMs */
1196 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1198 if (offset & 1) {
1199 tmp &= 0xff;
1200 tmp |= *data << 8;
1201 } else {
1202 tmp &= 0xff00;
1203 tmp |= *data;
1206 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1207 eeprom_93cx6_wren(&ks->eeprom, false);
1209 ks8851_eeprom_release(ks);
1211 return 0;
1214 static int ks8851_get_eeprom(struct net_device *dev,
1215 struct ethtool_eeprom *ee, u8 *data)
1217 struct ks8851_net *ks = netdev_priv(dev);
1218 int offset = ee->offset;
1219 int len = ee->len;
1221 /* must be 2 byte aligned */
1222 if (len & 1 || offset & 1)
1223 return -EINVAL;
1225 if (ks8851_eeprom_claim(ks))
1226 return -ENOENT;
1228 ee->magic = KS_EEPROM_MAGIC;
1230 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1231 ks8851_eeprom_release(ks);
1233 return 0;
1236 static int ks8851_get_eeprom_len(struct net_device *dev)
1238 struct ks8851_net *ks = netdev_priv(dev);
1240 /* currently, we assume it is an 93C46 attached, so return 128 */
1241 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1244 static const struct ethtool_ops ks8851_ethtool_ops = {
1245 .get_drvinfo = ks8851_get_drvinfo,
1246 .get_msglevel = ks8851_get_msglevel,
1247 .set_msglevel = ks8851_set_msglevel,
1248 .get_settings = ks8851_get_settings,
1249 .set_settings = ks8851_set_settings,
1250 .get_link = ks8851_get_link,
1251 .nway_reset = ks8851_nway_reset,
1252 .get_eeprom_len = ks8851_get_eeprom_len,
1253 .get_eeprom = ks8851_get_eeprom,
1254 .set_eeprom = ks8851_set_eeprom,
1257 /* MII interface controls */
1260 * ks8851_phy_reg - convert MII register into a KS8851 register
1261 * @reg: MII register number.
1263 * Return the KS8851 register number for the corresponding MII PHY register
1264 * if possible. Return zero if the MII register has no direct mapping to the
1265 * KS8851 register set.
1267 static int ks8851_phy_reg(int reg)
1269 switch (reg) {
1270 case MII_BMCR:
1271 return KS_P1MBCR;
1272 case MII_BMSR:
1273 return KS_P1MBSR;
1274 case MII_PHYSID1:
1275 return KS_PHY1ILR;
1276 case MII_PHYSID2:
1277 return KS_PHY1IHR;
1278 case MII_ADVERTISE:
1279 return KS_P1ANAR;
1280 case MII_LPA:
1281 return KS_P1ANLPR;
1284 return 0x0;
1288 * ks8851_phy_read - MII interface PHY register read.
1289 * @dev: The network device the PHY is on.
1290 * @phy_addr: Address of PHY (ignored as we only have one)
1291 * @reg: The register to read.
1293 * This call reads data from the PHY register specified in @reg. Since the
1294 * device does not support all the MII registers, the non-existent values
1295 * are always returned as zero.
1297 * We return zero for unsupported registers as the MII code does not check
1298 * the value returned for any error status, and simply returns it to the
1299 * caller. The mii-tool that the driver was tested with takes any -ve error
1300 * as real PHY capabilities, thus displaying incorrect data to the user.
1302 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1304 struct ks8851_net *ks = netdev_priv(dev);
1305 int ksreg;
1306 int result;
1308 ksreg = ks8851_phy_reg(reg);
1309 if (!ksreg)
1310 return 0x0; /* no error return allowed, so use zero */
1312 mutex_lock(&ks->lock);
1313 result = ks8851_rdreg16(ks, ksreg);
1314 mutex_unlock(&ks->lock);
1316 return result;
1319 static void ks8851_phy_write(struct net_device *dev,
1320 int phy, int reg, int value)
1322 struct ks8851_net *ks = netdev_priv(dev);
1323 int ksreg;
1325 ksreg = ks8851_phy_reg(reg);
1326 if (ksreg) {
1327 mutex_lock(&ks->lock);
1328 ks8851_wrreg16(ks, ksreg, value);
1329 mutex_unlock(&ks->lock);
1334 * ks8851_read_selftest - read the selftest memory info.
1335 * @ks: The device state
1337 * Read and check the TX/RX memory selftest information.
1339 static int ks8851_read_selftest(struct ks8851_net *ks)
1341 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1342 int ret = 0;
1343 unsigned rd;
1345 rd = ks8851_rdreg16(ks, KS_MBIR);
1347 if ((rd & both_done) != both_done) {
1348 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1349 return 0;
1352 if (rd & MBIR_TXMBFA) {
1353 netdev_err(ks->netdev, "TX memory selftest fail\n");
1354 ret |= 1;
1357 if (rd & MBIR_RXMBFA) {
1358 netdev_err(ks->netdev, "RX memory selftest fail\n");
1359 ret |= 2;
1362 return 0;
1365 /* driver bus management functions */
1367 #ifdef CONFIG_PM_SLEEP
1369 static int ks8851_suspend(struct device *dev)
1371 struct ks8851_net *ks = dev_get_drvdata(dev);
1372 struct net_device *netdev = ks->netdev;
1374 if (netif_running(netdev)) {
1375 netif_device_detach(netdev);
1376 ks8851_net_stop(netdev);
1379 return 0;
1382 static int ks8851_resume(struct device *dev)
1384 struct ks8851_net *ks = dev_get_drvdata(dev);
1385 struct net_device *netdev = ks->netdev;
1387 if (netif_running(netdev)) {
1388 ks8851_net_open(netdev);
1389 netif_device_attach(netdev);
1392 return 0;
1394 #endif
1396 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1398 static int ks8851_probe(struct spi_device *spi)
1400 struct net_device *ndev;
1401 struct ks8851_net *ks;
1402 int ret;
1403 unsigned cider;
1405 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1406 if (!ndev)
1407 return -ENOMEM;
1409 spi->bits_per_word = 8;
1411 ks = netdev_priv(ndev);
1413 ks->netdev = ndev;
1414 ks->spidev = spi;
1415 ks->tx_space = 6144;
1417 mutex_init(&ks->lock);
1418 spin_lock_init(&ks->statelock);
1420 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1421 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1423 /* initialise pre-made spi transfer messages */
1425 spi_message_init(&ks->spi_msg1);
1426 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1428 spi_message_init(&ks->spi_msg2);
1429 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1430 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1432 /* setup EEPROM state */
1434 ks->eeprom.data = ks;
1435 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1436 ks->eeprom.register_read = ks8851_eeprom_regread;
1437 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1439 /* setup mii state */
1440 ks->mii.dev = ndev;
1441 ks->mii.phy_id = 1,
1442 ks->mii.phy_id_mask = 1;
1443 ks->mii.reg_num_mask = 0xf;
1444 ks->mii.mdio_read = ks8851_phy_read;
1445 ks->mii.mdio_write = ks8851_phy_write;
1447 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1449 /* set the default message enable */
1450 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1451 NETIF_MSG_PROBE |
1452 NETIF_MSG_LINK));
1454 skb_queue_head_init(&ks->txq);
1456 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1457 SET_NETDEV_DEV(ndev, &spi->dev);
1459 spi_set_drvdata(spi, ks);
1461 ndev->if_port = IF_PORT_100BASET;
1462 ndev->netdev_ops = &ks8851_netdev_ops;
1463 ndev->irq = spi->irq;
1465 /* issue a global soft reset to reset the device. */
1466 ks8851_soft_reset(ks, GRR_GSR);
1468 /* simple check for a valid chip being connected to the bus */
1469 cider = ks8851_rdreg16(ks, KS_CIDER);
1470 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1471 dev_err(&spi->dev, "failed to read device ID\n");
1472 ret = -ENODEV;
1473 goto err_id;
1476 /* cache the contents of the CCR register for EEPROM, etc. */
1477 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1479 if (ks->rc_ccr & CCR_EEPROM)
1480 ks->eeprom_size = 128;
1481 else
1482 ks->eeprom_size = 0;
1484 ks8851_read_selftest(ks);
1485 ks8851_init_mac(ks);
1487 ret = request_threaded_irq(spi->irq, NULL, ks8851_irq,
1488 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1489 ndev->name, ks);
1490 if (ret < 0) {
1491 dev_err(&spi->dev, "failed to get irq\n");
1492 goto err_irq;
1495 ret = register_netdev(ndev);
1496 if (ret) {
1497 dev_err(&spi->dev, "failed to register network device\n");
1498 goto err_netdev;
1501 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1502 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
1503 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1505 return 0;
1508 err_netdev:
1509 free_irq(ndev->irq, ks);
1511 err_id:
1512 err_irq:
1513 free_netdev(ndev);
1514 return ret;
1517 static int ks8851_remove(struct spi_device *spi)
1519 struct ks8851_net *priv = spi_get_drvdata(spi);
1521 if (netif_msg_drv(priv))
1522 dev_info(&spi->dev, "remove\n");
1524 unregister_netdev(priv->netdev);
1525 free_irq(spi->irq, priv);
1526 free_netdev(priv->netdev);
1528 return 0;
1531 static struct spi_driver ks8851_driver = {
1532 .driver = {
1533 .name = "ks8851",
1534 .owner = THIS_MODULE,
1535 .pm = &ks8851_pm_ops,
1537 .probe = ks8851_probe,
1538 .remove = ks8851_remove,
1540 module_spi_driver(ks8851_driver);
1542 MODULE_DESCRIPTION("KS8851 Network driver");
1543 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1544 MODULE_LICENSE("GPL");
1546 module_param_named(message, msg_enable, int, 0);
1547 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1548 MODULE_ALIAS("spi:ks8851");