r8169: WakeOnLan fix for the 8168
[linux-2.6/mini2440.git] / drivers / net / ks8851.c
blob9a1dea60c1c41c62a4632d5bd9647cefa2fd6b9f
1 /* drivers/net/ks8651.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 DEBUG
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>
25 #include "ks8851.h"
27 /**
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
37 * commands.
39 struct ks8851_rxctrl {
40 u16 mchash[4];
41 u16 rxcr1;
42 u16 rxcr2;
45 /**
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.
54 union ks8851_tx_hdr {
55 u8 txb[6];
56 __le16 txw[3];
59 /**
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
92 * modifies.
94 struct ks8851_net {
95 struct net_device *netdev;
96 struct spi_device *spidev;
97 struct mutex lock;
98 spinlock_t statelock;
100 union ks8851_tx_hdr txh ____cacheline_aligned;
101 u8 rxd[8];
102 u8 txd[8];
104 u32 msg_enable ____cacheline_aligned;
105 u16 tx_space;
106 u8 fid;
108 u16 rc_ier;
109 u16 rc_rxqcr;
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;
158 __le16 txb[2];
159 int ret;
161 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
162 txb[1] = cpu_to_le16(val);
164 xfer->tx_buf = txb;
165 xfer->rx_buf = NULL;
166 xfer->len = 4;
168 ret = spi_sync(ks->spidev, msg);
169 if (ret < 0)
170 ks_err(ks, "spi_sync() failed\n");
174 * ks8851_rx_1msg - select whether to use one or two messages for spi read
175 * @ks: The device structure
177 * Return whether to generate a single message with a tx and rx buffer
178 * supplied to spi_sync(), or alternatively send the tx and rx buffers
179 * as separate messages.
181 * Depending on the hardware in use, a single message may be more efficient
182 * on interrupts or work done by the driver.
184 * This currently always returns true until we add some per-device data passed
185 * from the platform code to specify which mode is better.
187 static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
189 return true;
193 * ks8851_rdreg - issue read register command and return the data
194 * @ks: The device state
195 * @op: The register address and byte enables in message format.
196 * @rxb: The RX buffer to return the result into
197 * @rxl: The length of data expected.
199 * This is the low level read call that issues the necessary spi message(s)
200 * to read data from the register specified in @op.
202 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
203 u8 *rxb, unsigned rxl)
205 struct spi_transfer *xfer;
206 struct spi_message *msg;
207 __le16 *txb = (__le16 *)ks->txd;
208 u8 *trx = ks->rxd;
209 int ret;
211 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
213 if (ks8851_rx_1msg(ks)) {
214 msg = &ks->spi_msg1;
215 xfer = &ks->spi_xfer1;
217 xfer->tx_buf = txb;
218 xfer->rx_buf = trx;
219 xfer->len = rxl + 2;
220 } else {
221 msg = &ks->spi_msg2;
222 xfer = ks->spi_xfer2;
224 xfer->tx_buf = txb;
225 xfer->rx_buf = NULL;
226 xfer->len = 2;
228 xfer++;
229 xfer->tx_buf = NULL;
230 xfer->rx_buf = trx;
231 xfer->len = rxl;
234 ret = spi_sync(ks->spidev, msg);
235 if (ret < 0)
236 ks_err(ks, "read: spi_sync() failed\n");
237 else if (ks8851_rx_1msg(ks))
238 memcpy(rxb, trx + 2, rxl);
239 else
240 memcpy(rxb, trx, rxl);
244 * ks8851_rdreg8 - read 8 bit register from device
245 * @ks: The chip information
246 * @reg: The register address
248 * Read a 8bit register from the chip, returning the result
250 static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
252 u8 rxb[1];
254 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
255 return rxb[0];
259 * ks8851_rdreg16 - read 16 bit register from device
260 * @ks: The chip information
261 * @reg: The register address
263 * Read a 16bit register from the chip, returning the result
265 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
267 __le16 rx = 0;
269 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
270 return le16_to_cpu(rx);
274 * ks8851_rdreg32 - read 32 bit register from device
275 * @ks: The chip information
276 * @reg: The register address
278 * Read a 32bit register from the chip.
280 * Note, this read requires the address be aligned to 4 bytes.
282 static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
284 __le32 rx = 0;
286 WARN_ON(reg & 3);
288 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
289 return le32_to_cpu(rx);
293 * ks8851_soft_reset - issue one of the soft reset to the device
294 * @ks: The device state.
295 * @op: The bit(s) to set in the GRR
297 * Issue the relevant soft-reset command to the device's GRR register
298 * specified by @op.
300 * Note, the delays are in there as a caution to ensure that the reset
301 * has time to take effect and then complete. Since the datasheet does
302 * not currently specify the exact sequence, we have chosen something
303 * that seems to work with our device.
305 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
307 ks8851_wrreg16(ks, KS_GRR, op);
308 mdelay(1); /* wait a short time to effect reset */
309 ks8851_wrreg16(ks, KS_GRR, 0);
310 mdelay(1); /* wait for condition to clear */
314 * ks8851_write_mac_addr - write mac address to device registers
315 * @dev: The network device
317 * Update the KS8851 MAC address registers from the address in @dev.
319 * This call assumes that the chip is not running, so there is no need to
320 * shutdown the RXQ process whilst setting this.
322 static int ks8851_write_mac_addr(struct net_device *dev)
324 struct ks8851_net *ks = netdev_priv(dev);
325 u16 *mcp = (u16 *)dev->dev_addr;
327 mutex_lock(&ks->lock);
329 ks8851_wrreg16(ks, KS_MARL, mcp[0]);
330 ks8851_wrreg16(ks, KS_MARM, mcp[1]);
331 ks8851_wrreg16(ks, KS_MARH, mcp[2]);
333 mutex_unlock(&ks->lock);
335 return 0;
339 * ks8851_init_mac - initialise the mac address
340 * @ks: The device structure
342 * Get or create the initial mac address for the device and then set that
343 * into the station address register. Currently we assume that the device
344 * does not have a valid mac address in it, and so we use random_ether_addr()
345 * to create a new one.
347 * In future, the driver should check to see if the device has an EEPROM
348 * attached and whether that has a valid ethernet address in it.
350 static void ks8851_init_mac(struct ks8851_net *ks)
352 struct net_device *dev = ks->netdev;
354 random_ether_addr(dev->dev_addr);
355 ks8851_write_mac_addr(dev);
359 * ks8851_irq - device interrupt handler
360 * @irq: Interrupt number passed from the IRQ hnalder.
361 * @pw: The private word passed to register_irq(), our struct ks8851_net.
363 * Disable the interrupt from happening again until we've processed the
364 * current status by scheduling ks8851_irq_work().
366 static irqreturn_t ks8851_irq(int irq, void *pw)
368 struct ks8851_net *ks = pw;
370 disable_irq_nosync(irq);
371 schedule_work(&ks->irq_work);
372 return IRQ_HANDLED;
376 * ks8851_rdfifo - read data from the receive fifo
377 * @ks: The device state.
378 * @buff: The buffer address
379 * @len: The length of the data to read
381 * Issue an RXQ FIFO read command and read the @len ammount of data from
382 * the FIFO into the buffer specified by @buff.
384 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
386 struct spi_transfer *xfer = ks->spi_xfer2;
387 struct spi_message *msg = &ks->spi_msg2;
388 u8 txb[1];
389 int ret;
391 if (netif_msg_rx_status(ks))
392 ks_dbg(ks, "%s: %d@%p\n", __func__, len, buff);
394 /* set the operation we're issuing */
395 txb[0] = KS_SPIOP_RXFIFO;
397 xfer->tx_buf = txb;
398 xfer->rx_buf = NULL;
399 xfer->len = 1;
401 xfer++;
402 xfer->rx_buf = buff;
403 xfer->tx_buf = NULL;
404 xfer->len = len;
406 ret = spi_sync(ks->spidev, msg);
407 if (ret < 0)
408 ks_err(ks, "%s: spi_sync() failed\n", __func__);
412 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
413 * @ks: The device state
414 * @rxpkt: The data for the received packet
416 * Dump the initial data from the packet to dev_dbg().
418 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
420 ks_dbg(ks, "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
421 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
422 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
423 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
427 * ks8851_rx_pkts - receive packets from the host
428 * @ks: The device information.
430 * This is called from the IRQ work queue when the system detects that there
431 * are packets in the receive queue. Find out how many packets there are and
432 * read them from the FIFO.
434 static void ks8851_rx_pkts(struct ks8851_net *ks)
436 struct sk_buff *skb;
437 unsigned rxfc;
438 unsigned rxlen;
439 unsigned rxstat;
440 u32 rxh;
441 u8 *rxpkt;
443 rxfc = ks8851_rdreg8(ks, KS_RXFC);
445 if (netif_msg_rx_status(ks))
446 ks_dbg(ks, "%s: %d packets\n", __func__, rxfc);
448 /* Currently we're issuing a read per packet, but we could possibly
449 * improve the code by issuing a single read, getting the receive
450 * header, allocating the packet and then reading the packet data
451 * out in one go.
453 * This form of operation would require us to hold the SPI bus'
454 * chipselect low during the entie transaction to avoid any
455 * reset to the data stream comming from the chip.
458 for (; rxfc != 0; rxfc--) {
459 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
460 rxstat = rxh & 0xffff;
461 rxlen = rxh >> 16;
463 if (netif_msg_rx_status(ks))
464 ks_dbg(ks, "rx: stat 0x%04x, len 0x%04x\n",
465 rxstat, rxlen);
467 /* the length of the packet includes the 32bit CRC */
469 /* set dma read address */
470 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
472 /* start the packet dma process, and set auto-dequeue rx */
473 ks8851_wrreg16(ks, KS_RXQCR,
474 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
476 if (rxlen > 0) {
477 skb = netdev_alloc_skb(ks->netdev, rxlen + 2 + 8);
478 if (!skb) {
479 /* todo - dump frame and move on */
482 /* two bytes to ensure ip is aligned, and four bytes
483 * for the status header and 4 bytes of garbage */
484 skb_reserve(skb, 2 + 4 + 4);
486 rxpkt = skb_put(skb, rxlen - 4) - 8;
488 /* align the packet length to 4 bytes, and add 4 bytes
489 * as we're getting the rx status header as well */
490 ks8851_rdfifo(ks, rxpkt, ALIGN(rxlen, 4) + 8);
492 if (netif_msg_pktdata(ks))
493 ks8851_dbg_dumpkkt(ks, rxpkt);
495 skb->protocol = eth_type_trans(skb, ks->netdev);
496 netif_rx(skb);
498 ks->netdev->stats.rx_packets++;
499 ks->netdev->stats.rx_bytes += rxlen - 4;
502 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
507 * ks8851_irq_work - work queue handler for dealing with interrupt requests
508 * @work: The work structure that was scheduled by schedule_work()
510 * This is the handler invoked when the ks8851_irq() is called to find out
511 * what happened, as we cannot allow ourselves to sleep whilst waiting for
512 * anything other process has the chip's lock.
514 * Read the interrupt status, work out what needs to be done and then clear
515 * any of the interrupts that are not needed.
517 static void ks8851_irq_work(struct work_struct *work)
519 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
520 unsigned status;
521 unsigned handled = 0;
523 mutex_lock(&ks->lock);
525 status = ks8851_rdreg16(ks, KS_ISR);
527 if (netif_msg_intr(ks))
528 dev_dbg(&ks->spidev->dev, "%s: status 0x%04x\n",
529 __func__, status);
531 if (status & IRQ_LCI) {
532 /* should do something about checking link status */
533 handled |= IRQ_LCI;
536 if (status & IRQ_LDI) {
537 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
538 pmecr &= ~PMECR_WKEVT_MASK;
539 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
541 handled |= IRQ_LDI;
544 if (status & IRQ_RXPSI)
545 handled |= IRQ_RXPSI;
547 if (status & IRQ_TXI) {
548 handled |= IRQ_TXI;
550 /* no lock here, tx queue should have been stopped */
552 /* update our idea of how much tx space is available to the
553 * system */
554 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
556 if (netif_msg_intr(ks))
557 ks_dbg(ks, "%s: txspace %d\n", __func__, ks->tx_space);
560 if (status & IRQ_RXI)
561 handled |= IRQ_RXI;
563 if (status & IRQ_SPIBEI) {
564 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
565 handled |= IRQ_SPIBEI;
568 ks8851_wrreg16(ks, KS_ISR, handled);
570 if (status & IRQ_RXI) {
571 /* the datasheet says to disable the rx interrupt during
572 * packet read-out, however we're masking the interrupt
573 * from the device so do not bother masking just the RX
574 * from the device. */
576 ks8851_rx_pkts(ks);
579 /* if something stopped the rx process, probably due to wanting
580 * to change the rx settings, then do something about restarting
581 * it. */
582 if (status & IRQ_RXPSI) {
583 struct ks8851_rxctrl *rxc = &ks->rxctrl;
585 /* update the multicast hash table */
586 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
587 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
588 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
589 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
591 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
592 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
595 mutex_unlock(&ks->lock);
597 if (status & IRQ_TXI)
598 netif_wake_queue(ks->netdev);
600 enable_irq(ks->netdev->irq);
604 * calc_txlen - calculate size of message to send packet
605 * @len: Lenght of data
607 * Returns the size of the TXFIFO message needed to send
608 * this packet.
610 static inline unsigned calc_txlen(unsigned len)
612 return ALIGN(len + 4, 4);
616 * ks8851_wrpkt - write packet to TX FIFO
617 * @ks: The device state.
618 * @txp: The sk_buff to transmit.
619 * @irq: IRQ on completion of the packet.
621 * Send the @txp to the chip. This means creating the relevant packet header
622 * specifying the length of the packet and the other information the chip
623 * needs, such as IRQ on completion. Send the header and the packet data to
624 * the device.
626 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
628 struct spi_transfer *xfer = ks->spi_xfer2;
629 struct spi_message *msg = &ks->spi_msg2;
630 unsigned fid = 0;
631 int ret;
633 if (netif_msg_tx_queued(ks))
634 dev_dbg(&ks->spidev->dev, "%s: skb %p, %d@%p, irq %d\n",
635 __func__, txp, txp->len, txp->data, irq);
637 fid = ks->fid++;
638 fid &= TXFR_TXFID_MASK;
640 if (irq)
641 fid |= TXFR_TXIC; /* irq on completion */
643 /* start header at txb[1] to align txw entries */
644 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
645 ks->txh.txw[1] = cpu_to_le16(fid);
646 ks->txh.txw[2] = cpu_to_le16(txp->len);
648 xfer->tx_buf = &ks->txh.txb[1];
649 xfer->rx_buf = NULL;
650 xfer->len = 5;
652 xfer++;
653 xfer->tx_buf = txp->data;
654 xfer->rx_buf = NULL;
655 xfer->len = ALIGN(txp->len, 4);
657 ret = spi_sync(ks->spidev, msg);
658 if (ret < 0)
659 ks_err(ks, "%s: spi_sync() failed\n", __func__);
663 * ks8851_done_tx - update and then free skbuff after transmitting
664 * @ks: The device state
665 * @txb: The buffer transmitted
667 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
669 struct net_device *dev = ks->netdev;
671 dev->stats.tx_bytes += txb->len;
672 dev->stats.tx_packets++;
674 dev_kfree_skb(txb);
678 * ks8851_tx_work - process tx packet(s)
679 * @work: The work strucutre what was scheduled.
681 * This is called when a number of packets have been scheduled for
682 * transmission and need to be sent to the device.
684 static void ks8851_tx_work(struct work_struct *work)
686 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
687 struct sk_buff *txb;
688 bool last = false;
690 mutex_lock(&ks->lock);
692 while (!last) {
693 txb = skb_dequeue(&ks->txq);
694 last = skb_queue_empty(&ks->txq);
696 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
697 ks8851_wrpkt(ks, txb, last);
698 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
699 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
701 ks8851_done_tx(ks, txb);
704 mutex_unlock(&ks->lock);
708 * ks8851_set_powermode - set power mode of the device
709 * @ks: The device state
710 * @pwrmode: The power mode value to write to KS_PMECR.
712 * Change the power mode of the chip.
714 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
716 unsigned pmecr;
718 if (netif_msg_hw(ks))
719 ks_dbg(ks, "setting power mode %d\n", pwrmode);
721 pmecr = ks8851_rdreg16(ks, KS_PMECR);
722 pmecr &= ~PMECR_PM_MASK;
723 pmecr |= pwrmode;
725 ks8851_wrreg16(ks, KS_PMECR, pmecr);
729 * ks8851_net_open - open network device
730 * @dev: The network device being opened.
732 * Called when the network device is marked active, such as a user executing
733 * 'ifconfig up' on the device.
735 static int ks8851_net_open(struct net_device *dev)
737 struct ks8851_net *ks = netdev_priv(dev);
739 /* lock the card, even if we may not actually be doing anything
740 * else at the moment */
741 mutex_lock(&ks->lock);
743 if (netif_msg_ifup(ks))
744 ks_dbg(ks, "opening %s\n", dev->name);
746 /* bring chip out of any power saving mode it was in */
747 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
749 /* issue a soft reset to the RX/TX QMU to put it into a known
750 * state. */
751 ks8851_soft_reset(ks, GRR_QMU);
753 /* setup transmission parameters */
755 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
756 TXCR_TXPE | /* pad to min length */
757 TXCR_TXCRC | /* add CRC */
758 TXCR_TXFCE)); /* enable flow control */
760 /* auto-increment tx data, reset tx pointer */
761 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
763 /* setup receiver control */
765 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
766 RXCR1_RXFCE | /* enable flow control */
767 RXCR1_RXBE | /* broadcast enable */
768 RXCR1_RXUE | /* unicast enable */
769 RXCR1_RXE)); /* enable rx block */
771 /* transfer entire frames out in one go */
772 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
774 /* set receive counter timeouts */
775 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
776 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
777 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
779 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
780 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
781 RXQCR_RXDTTE); /* IRQ on time exceeded */
783 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
785 /* clear then enable interrupts */
787 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
788 IRQ_TXI | /* TX done */ \
789 IRQ_RXI | /* RX done */ \
790 IRQ_SPIBEI | /* SPI bus error */ \
791 IRQ_TXPSI | /* TX process stop */ \
792 IRQ_RXPSI) /* RX process stop */
794 ks->rc_ier = STD_IRQ;
795 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
796 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
798 netif_start_queue(ks->netdev);
800 if (netif_msg_ifup(ks))
801 ks_dbg(ks, "network device %s up\n", dev->name);
803 mutex_unlock(&ks->lock);
804 return 0;
808 * ks8851_net_stop - close network device
809 * @dev: The device being closed.
811 * Called to close down a network device which has been active. Cancell any
812 * work, shutdown the RX and TX process and then place the chip into a low
813 * power state whilst it is not being used.
815 static int ks8851_net_stop(struct net_device *dev)
817 struct ks8851_net *ks = netdev_priv(dev);
819 if (netif_msg_ifdown(ks))
820 ks_info(ks, "%s: shutting down\n", dev->name);
822 netif_stop_queue(dev);
824 mutex_lock(&ks->lock);
826 /* stop any outstanding work */
827 flush_work(&ks->irq_work);
828 flush_work(&ks->tx_work);
829 flush_work(&ks->rxctrl_work);
831 /* turn off the IRQs and ack any outstanding */
832 ks8851_wrreg16(ks, KS_IER, 0x0000);
833 ks8851_wrreg16(ks, KS_ISR, 0xffff);
835 /* shutdown RX process */
836 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
838 /* shutdown TX process */
839 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
841 /* set powermode to soft power down to save power */
842 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
844 /* ensure any queued tx buffers are dumped */
845 while (!skb_queue_empty(&ks->txq)) {
846 struct sk_buff *txb = skb_dequeue(&ks->txq);
848 if (netif_msg_ifdown(ks))
849 ks_dbg(ks, "%s: freeing txb %p\n", __func__, txb);
851 dev_kfree_skb(txb);
854 mutex_unlock(&ks->lock);
855 return 0;
859 * ks8851_start_xmit - transmit packet
860 * @skb: The buffer to transmit
861 * @dev: The device used to transmit the packet.
863 * Called by the network layer to transmit the @skb. Queue the packet for
864 * the device and schedule the necessary work to transmit the packet when
865 * it is free.
867 * We do this to firstly avoid sleeping with the network device locked,
868 * and secondly so we can round up more than one packet to transmit which
869 * means we can try and avoid generating too many transmit done interrupts.
871 static int ks8851_start_xmit(struct sk_buff *skb, struct net_device *dev)
873 struct ks8851_net *ks = netdev_priv(dev);
874 unsigned needed = calc_txlen(skb->len);
875 int ret = NETDEV_TX_OK;
877 if (netif_msg_tx_queued(ks))
878 ks_dbg(ks, "%s: skb %p, %d@%p\n", __func__,
879 skb, skb->len, skb->data);
881 spin_lock(&ks->statelock);
883 if (needed > ks->tx_space) {
884 netif_stop_queue(dev);
885 ret = NETDEV_TX_BUSY;
886 } else {
887 ks->tx_space -= needed;
888 skb_queue_tail(&ks->txq, skb);
891 spin_unlock(&ks->statelock);
892 schedule_work(&ks->tx_work);
894 return ret;
898 * ks8851_rxctrl_work - work handler to change rx mode
899 * @work: The work structure this belongs to.
901 * Lock the device and issue the necessary changes to the receive mode from
902 * the network device layer. This is done so that we can do this without
903 * having to sleep whilst holding the network device lock.
905 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
906 * receive parameters are programmed, we issue a write to disable the RXQ and
907 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
908 * complete. The interrupt handler then writes the new values into the chip.
910 static void ks8851_rxctrl_work(struct work_struct *work)
912 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
914 mutex_lock(&ks->lock);
916 /* need to shutdown RXQ before modifying filter parameters */
917 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
919 mutex_unlock(&ks->lock);
922 static void ks8851_set_rx_mode(struct net_device *dev)
924 struct ks8851_net *ks = netdev_priv(dev);
925 struct ks8851_rxctrl rxctrl;
927 memset(&rxctrl, 0, sizeof(rxctrl));
929 if (dev->flags & IFF_PROMISC) {
930 /* interface to receive everything */
932 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
933 } else if (dev->flags & IFF_ALLMULTI) {
934 /* accept all multicast packets */
936 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
937 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
938 } else if (dev->flags & IFF_MULTICAST && dev->mc_count > 0) {
939 struct dev_mc_list *mcptr = dev->mc_list;
940 u32 crc;
941 int i;
943 /* accept some multicast */
945 for (i = dev->mc_count; i > 0; i--) {
946 crc = ether_crc(ETH_ALEN, mcptr->dmi_addr);
947 crc >>= (32 - 6); /* get top six bits */
949 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
950 mcptr = mcptr->next;
953 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXAE | RXCR1_RXPAFMA;
954 } else {
955 /* just accept broadcast / unicast */
956 rxctrl.rxcr1 = RXCR1_RXPAFMA;
959 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
960 RXCR1_RXBE | /* broadcast enable */
961 RXCR1_RXE | /* RX process enable */
962 RXCR1_RXFCE); /* enable flow control */
964 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
966 /* schedule work to do the actual set of the data if needed */
968 spin_lock(&ks->statelock);
970 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
971 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
972 schedule_work(&ks->rxctrl_work);
975 spin_unlock(&ks->statelock);
978 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
980 struct sockaddr *sa = addr;
982 if (netif_running(dev))
983 return -EBUSY;
985 if (!is_valid_ether_addr(sa->sa_data))
986 return -EADDRNOTAVAIL;
988 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
989 return ks8851_write_mac_addr(dev);
992 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
994 struct ks8851_net *ks = netdev_priv(dev);
996 if (!netif_running(dev))
997 return -EINVAL;
999 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1002 static const struct net_device_ops ks8851_netdev_ops = {
1003 .ndo_open = ks8851_net_open,
1004 .ndo_stop = ks8851_net_stop,
1005 .ndo_do_ioctl = ks8851_net_ioctl,
1006 .ndo_start_xmit = ks8851_start_xmit,
1007 .ndo_set_mac_address = ks8851_set_mac_address,
1008 .ndo_set_rx_mode = ks8851_set_rx_mode,
1009 .ndo_change_mtu = eth_change_mtu,
1010 .ndo_validate_addr = eth_validate_addr,
1013 /* ethtool support */
1015 static void ks8851_get_drvinfo(struct net_device *dev,
1016 struct ethtool_drvinfo *di)
1018 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1019 strlcpy(di->version, "1.00", sizeof(di->version));
1020 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1023 static u32 ks8851_get_msglevel(struct net_device *dev)
1025 struct ks8851_net *ks = netdev_priv(dev);
1026 return ks->msg_enable;
1029 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1031 struct ks8851_net *ks = netdev_priv(dev);
1032 ks->msg_enable = to;
1035 static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1037 struct ks8851_net *ks = netdev_priv(dev);
1038 return mii_ethtool_gset(&ks->mii, cmd);
1041 static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1043 struct ks8851_net *ks = netdev_priv(dev);
1044 return mii_ethtool_sset(&ks->mii, cmd);
1047 static u32 ks8851_get_link(struct net_device *dev)
1049 struct ks8851_net *ks = netdev_priv(dev);
1050 return mii_link_ok(&ks->mii);
1053 static int ks8851_nway_reset(struct net_device *dev)
1055 struct ks8851_net *ks = netdev_priv(dev);
1056 return mii_nway_restart(&ks->mii);
1059 static const struct ethtool_ops ks8851_ethtool_ops = {
1060 .get_drvinfo = ks8851_get_drvinfo,
1061 .get_msglevel = ks8851_get_msglevel,
1062 .set_msglevel = ks8851_set_msglevel,
1063 .get_settings = ks8851_get_settings,
1064 .set_settings = ks8851_set_settings,
1065 .get_link = ks8851_get_link,
1066 .nway_reset = ks8851_nway_reset,
1069 /* MII interface controls */
1072 * ks8851_phy_reg - convert MII register into a KS8851 register
1073 * @reg: MII register number.
1075 * Return the KS8851 register number for the corresponding MII PHY register
1076 * if possible. Return zero if the MII register has no direct mapping to the
1077 * KS8851 register set.
1079 static int ks8851_phy_reg(int reg)
1081 switch (reg) {
1082 case MII_BMCR:
1083 return KS_P1MBCR;
1084 case MII_BMSR:
1085 return KS_P1MBSR;
1086 case MII_PHYSID1:
1087 return KS_PHY1ILR;
1088 case MII_PHYSID2:
1089 return KS_PHY1IHR;
1090 case MII_ADVERTISE:
1091 return KS_P1ANAR;
1092 case MII_LPA:
1093 return KS_P1ANLPR;
1096 return 0x0;
1100 * ks8851_phy_read - MII interface PHY register read.
1101 * @dev: The network device the PHY is on.
1102 * @phy_addr: Address of PHY (ignored as we only have one)
1103 * @reg: The register to read.
1105 * This call reads data from the PHY register specified in @reg. Since the
1106 * device does not support all the MII registers, the non-existant values
1107 * are always returned as zero.
1109 * We return zero for unsupported registers as the MII code does not check
1110 * the value returned for any error status, and simply returns it to the
1111 * caller. The mii-tool that the driver was tested with takes any -ve error
1112 * as real PHY capabilities, thus displaying incorrect data to the user.
1114 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1116 struct ks8851_net *ks = netdev_priv(dev);
1117 int ksreg;
1118 int result;
1120 ksreg = ks8851_phy_reg(reg);
1121 if (!ksreg)
1122 return 0x0; /* no error return allowed, so use zero */
1124 mutex_lock(&ks->lock);
1125 result = ks8851_rdreg16(ks, ksreg);
1126 mutex_unlock(&ks->lock);
1128 return result;
1131 static void ks8851_phy_write(struct net_device *dev,
1132 int phy, int reg, int value)
1134 struct ks8851_net *ks = netdev_priv(dev);
1135 int ksreg;
1137 ksreg = ks8851_phy_reg(reg);
1138 if (ksreg) {
1139 mutex_lock(&ks->lock);
1140 ks8851_wrreg16(ks, ksreg, value);
1141 mutex_unlock(&ks->lock);
1146 * ks8851_read_selftest - read the selftest memory info.
1147 * @ks: The device state
1149 * Read and check the TX/RX memory selftest information.
1151 static int ks8851_read_selftest(struct ks8851_net *ks)
1153 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1154 int ret = 0;
1155 unsigned rd;
1157 rd = ks8851_rdreg16(ks, KS_MBIR);
1159 if ((rd & both_done) != both_done) {
1160 ks_warn(ks, "Memory selftest not finished\n");
1161 return 0;
1164 if (rd & MBIR_TXMBFA) {
1165 ks_err(ks, "TX memory selftest fail\n");
1166 ret |= 1;
1169 if (rd & MBIR_RXMBFA) {
1170 ks_err(ks, "RX memory selftest fail\n");
1171 ret |= 2;
1174 return 0;
1177 /* driver bus management functions */
1179 static int __devinit ks8851_probe(struct spi_device *spi)
1181 struct net_device *ndev;
1182 struct ks8851_net *ks;
1183 int ret;
1185 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1186 if (!ndev) {
1187 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1188 return -ENOMEM;
1191 spi->bits_per_word = 8;
1193 ks = netdev_priv(ndev);
1195 ks->netdev = ndev;
1196 ks->spidev = spi;
1197 ks->tx_space = 6144;
1199 mutex_init(&ks->lock);
1200 spin_lock_init(&ks->statelock);
1202 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1203 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1204 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1206 /* initialise pre-made spi transfer messages */
1208 spi_message_init(&ks->spi_msg1);
1209 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1211 spi_message_init(&ks->spi_msg2);
1212 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1213 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1215 /* setup mii state */
1216 ks->mii.dev = ndev;
1217 ks->mii.phy_id = 1,
1218 ks->mii.phy_id_mask = 1;
1219 ks->mii.reg_num_mask = 0xf;
1220 ks->mii.mdio_read = ks8851_phy_read;
1221 ks->mii.mdio_write = ks8851_phy_write;
1223 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1225 /* set the default message enable */
1226 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1227 NETIF_MSG_PROBE |
1228 NETIF_MSG_LINK));
1230 skb_queue_head_init(&ks->txq);
1232 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1233 SET_NETDEV_DEV(ndev, &spi->dev);
1235 dev_set_drvdata(&spi->dev, ks);
1237 ndev->if_port = IF_PORT_100BASET;
1238 ndev->netdev_ops = &ks8851_netdev_ops;
1239 ndev->irq = spi->irq;
1241 /* simple check for a valid chip being connected to the bus */
1243 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1244 dev_err(&spi->dev, "failed to read device ID\n");
1245 ret = -ENODEV;
1246 goto err_id;
1249 ks8851_read_selftest(ks);
1250 ks8851_init_mac(ks);
1252 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1253 ndev->name, ks);
1254 if (ret < 0) {
1255 dev_err(&spi->dev, "failed to get irq\n");
1256 goto err_irq;
1259 ret = register_netdev(ndev);
1260 if (ret) {
1261 dev_err(&spi->dev, "failed to register network device\n");
1262 goto err_netdev;
1265 dev_info(&spi->dev, "revision %d, MAC %pM, IRQ %d\n",
1266 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
1267 ndev->dev_addr, ndev->irq);
1269 return 0;
1272 err_netdev:
1273 free_irq(ndev->irq, ndev);
1275 err_id:
1276 err_irq:
1277 free_netdev(ndev);
1278 return ret;
1281 static int __devexit ks8851_remove(struct spi_device *spi)
1283 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
1285 if (netif_msg_drv(priv))
1286 dev_info(&spi->dev, "remove");
1288 unregister_netdev(priv->netdev);
1289 free_irq(spi->irq, priv);
1290 free_netdev(priv->netdev);
1292 return 0;
1295 static struct spi_driver ks8851_driver = {
1296 .driver = {
1297 .name = "ks8851",
1298 .owner = THIS_MODULE,
1300 .probe = ks8851_probe,
1301 .remove = __devexit_p(ks8851_remove),
1304 static int __init ks8851_init(void)
1306 return spi_register_driver(&ks8851_driver);
1309 static void __exit ks8851_exit(void)
1311 spi_unregister_driver(&ks8851_driver);
1314 module_init(ks8851_init);
1315 module_exit(ks8851_exit);
1317 MODULE_DESCRIPTION("KS8851 Network driver");
1318 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1319 MODULE_LICENSE("GPL");
1321 module_param_named(message, msg_enable, int, 0);
1322 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");