1 /* drivers/net/ethernet/micrel/ks8851.c
3 * Copyright 2009 Simtec Electronics
4 * http://www.simtec.co.uk/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/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>
26 #include <linux/regulator/consumer.h>
28 #include <linux/spi/spi.h>
33 * struct ks8851_rxctrl - KS8851 driver rx control
34 * @mchash: Multicast hash-table data.
35 * @rxcr1: KS_RXCR1 register setting
36 * @rxcr2: KS_RXCR2 register setting
38 * Representation of the settings needs to control the receive filtering
39 * such as the multicast hash-filter and the receive register settings. This
40 * is used to make the job of working out if the receive settings change and
41 * then issuing the new settings to the worker that will send the necessary
44 struct ks8851_rxctrl {
51 * union ks8851_tx_hdr - tx header data
52 * @txb: The header as bytes
53 * @txw: The header as 16bit, little-endian words
55 * A dual representation of the tx header data to allow
56 * access to individual bytes, and to allow 16bit accesses
57 * with 16bit alignment.
65 * struct ks8851_net - KS8851 driver private data
66 * @netdev: The network device we're bound to
67 * @spidev: The spi device we're bound to.
68 * @lock: Lock to ensure that the device is not accessed when busy.
69 * @statelock: Lock on this structure for tx list.
70 * @mii: The MII state information for the mii calls.
71 * @rxctrl: RX settings for @rxctrl_work.
72 * @tx_work: Work queue for tx packets
73 * @rxctrl_work: Work queue for updating RX mode and multicast lists
74 * @txq: Queue of packets for transmission.
75 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
76 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
77 * @txh: Space for generating packet TX header in DMA-able data
78 * @rxd: Space for receiving SPI data, in DMA-able space.
79 * @txd: Space for transmitting SPI data, in DMA-able space.
80 * @msg_enable: The message flags controlling driver output (see ethtool).
81 * @fid: Incrementing frame id tag.
82 * @rc_ier: Cached copy of KS_IER.
83 * @rc_ccr: Cached copy of KS_CCR.
84 * @rc_rxqcr: Cached copy of KS_RXQCR.
85 * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
86 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
87 * @vdd_reg: Optional regulator supplying the chip
89 * The @lock ensures that the chip is protected when certain operations are
90 * in progress. When the read or write packet transfer is in progress, most
91 * of the chip registers are not ccessible until the transfer is finished and
92 * the DMA has been de-asserted.
94 * The @statelock is used to protect information in the structure which may
95 * need to be accessed via several sources, such as the network driver layer
96 * or one of the work queues.
98 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
99 * wants to DMA map them, it will not have any problems with data the driver
103 struct net_device *netdev;
104 struct spi_device *spidev;
106 spinlock_t statelock;
108 union ks8851_tx_hdr txh ____cacheline_aligned;
112 u32 msg_enable ____cacheline_aligned;
121 struct mii_if_info mii;
122 struct ks8851_rxctrl rxctrl;
124 struct work_struct tx_work;
125 struct work_struct rxctrl_work;
127 struct sk_buff_head txq;
129 struct spi_message spi_msg1;
130 struct spi_message spi_msg2;
131 struct spi_transfer spi_xfer1;
132 struct spi_transfer spi_xfer2[2];
134 struct eeprom_93cx6 eeprom;
135 struct regulator *vdd_reg;
138 static int msg_enable;
140 /* shift for byte-enable data */
141 #define BYTE_EN(_x) ((_x) << 2)
143 /* turn register number and byte-enable mask into data for start of packet */
144 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
146 /* SPI register read/write calls.
148 * All these calls issue SPI transactions to access the chip's registers. They
149 * all require that the necessary lock is held to prevent accesses when the
150 * chip is busy transferring packet data (RX/TX FIFO accesses).
154 * ks8851_wrreg16 - write 16bit register value to chip
155 * @ks: The chip state
156 * @reg: The register address
157 * @val: The value to write
159 * Issue a write to put the value @val into the register specified in @reg.
161 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
163 struct spi_transfer *xfer = &ks->spi_xfer1;
164 struct spi_message *msg = &ks->spi_msg1;
168 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
169 txb[1] = cpu_to_le16(val);
175 ret = spi_sync(ks->spidev, msg);
177 netdev_err(ks->netdev, "spi_sync() failed\n");
181 * ks8851_wrreg8 - write 8bit register value to chip
182 * @ks: The chip state
183 * @reg: The register address
184 * @val: The value to write
186 * Issue a write to put the value @val into the register specified in @reg.
188 static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
190 struct spi_transfer *xfer = &ks->spi_xfer1;
191 struct spi_message *msg = &ks->spi_msg1;
196 bit = 1 << (reg & 3);
198 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
205 ret = spi_sync(ks->spidev, msg);
207 netdev_err(ks->netdev, "spi_sync() failed\n");
211 * ks8851_rx_1msg - select whether to use one or two messages for spi read
212 * @ks: The device structure
214 * Return whether to generate a single message with a tx and rx buffer
215 * supplied to spi_sync(), or alternatively send the tx and rx buffers
216 * as separate messages.
218 * Depending on the hardware in use, a single message may be more efficient
219 * on interrupts or work done by the driver.
221 * This currently always returns true until we add some per-device data passed
222 * from the platform code to specify which mode is better.
224 static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
230 * ks8851_rdreg - issue read register command and return the data
231 * @ks: The device state
232 * @op: The register address and byte enables in message format.
233 * @rxb: The RX buffer to return the result into
234 * @rxl: The length of data expected.
236 * This is the low level read call that issues the necessary spi message(s)
237 * to read data from the register specified in @op.
239 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
240 u8 *rxb, unsigned rxl)
242 struct spi_transfer *xfer;
243 struct spi_message *msg;
244 __le16 *txb = (__le16 *)ks->txd;
248 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
250 if (ks8851_rx_1msg(ks)) {
252 xfer = &ks->spi_xfer1;
259 xfer = ks->spi_xfer2;
271 ret = spi_sync(ks->spidev, msg);
273 netdev_err(ks->netdev, "read: spi_sync() failed\n");
274 else if (ks8851_rx_1msg(ks))
275 memcpy(rxb, trx + 2, rxl);
277 memcpy(rxb, trx, rxl);
281 * ks8851_rdreg8 - read 8 bit register from device
282 * @ks: The chip information
283 * @reg: The register address
285 * Read a 8bit register from the chip, returning the result
287 static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
291 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
296 * ks8851_rdreg16 - read 16 bit register from device
297 * @ks: The chip information
298 * @reg: The register address
300 * Read a 16bit register from the chip, returning the result
302 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
306 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
307 return le16_to_cpu(rx);
311 * ks8851_rdreg32 - read 32 bit register from device
312 * @ks: The chip information
313 * @reg: The register address
315 * Read a 32bit register from the chip.
317 * Note, this read requires the address be aligned to 4 bytes.
319 static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
325 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
326 return le32_to_cpu(rx);
330 * ks8851_soft_reset - issue one of the soft reset to the device
331 * @ks: The device state.
332 * @op: The bit(s) to set in the GRR
334 * Issue the relevant soft-reset command to the device's GRR register
337 * Note, the delays are in there as a caution to ensure that the reset
338 * has time to take effect and then complete. Since the datasheet does
339 * not currently specify the exact sequence, we have chosen something
340 * that seems to work with our device.
342 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
344 ks8851_wrreg16(ks, KS_GRR, op);
345 mdelay(1); /* wait a short time to effect reset */
346 ks8851_wrreg16(ks, KS_GRR, 0);
347 mdelay(1); /* wait for condition to clear */
351 * ks8851_set_powermode - set power mode of the device
352 * @ks: The device state
353 * @pwrmode: The power mode value to write to KS_PMECR.
355 * Change the power mode of the chip.
357 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
361 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
363 pmecr = ks8851_rdreg16(ks, KS_PMECR);
364 pmecr &= ~PMECR_PM_MASK;
367 ks8851_wrreg16(ks, KS_PMECR, pmecr);
371 * ks8851_write_mac_addr - write mac address to device registers
372 * @dev: The network device
374 * Update the KS8851 MAC address registers from the address in @dev.
376 * This call assumes that the chip is not running, so there is no need to
377 * shutdown the RXQ process whilst setting this.
379 static int ks8851_write_mac_addr(struct net_device *dev)
381 struct ks8851_net *ks = netdev_priv(dev);
384 mutex_lock(&ks->lock);
387 * Wake up chip in case it was powered off when stopped; otherwise,
388 * the first write to the MAC address does not take effect.
390 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
391 for (i = 0; i < ETH_ALEN; i++)
392 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
393 if (!netif_running(dev))
394 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
396 mutex_unlock(&ks->lock);
402 * ks8851_read_mac_addr - read mac address from device registers
403 * @dev: The network device
405 * Update our copy of the KS8851 MAC address from the registers of @dev.
407 static void ks8851_read_mac_addr(struct net_device *dev)
409 struct ks8851_net *ks = netdev_priv(dev);
412 mutex_lock(&ks->lock);
414 for (i = 0; i < ETH_ALEN; i++)
415 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
417 mutex_unlock(&ks->lock);
421 * ks8851_init_mac - initialise the mac address
422 * @ks: The device structure
424 * Get or create the initial mac address for the device and then set that
425 * into the station address register. If there is an EEPROM present, then
426 * we try that. If no valid mac address is found we use eth_random_addr()
427 * to create a new one.
429 static void ks8851_init_mac(struct ks8851_net *ks)
431 struct net_device *dev = ks->netdev;
433 /* first, try reading what we've got already */
434 if (ks->rc_ccr & CCR_EEPROM) {
435 ks8851_read_mac_addr(dev);
436 if (is_valid_ether_addr(dev->dev_addr))
439 netdev_err(ks->netdev, "invalid mac address read %pM\n",
443 eth_hw_addr_random(dev);
444 ks8851_write_mac_addr(dev);
448 * ks8851_rdfifo - read data from the receive fifo
449 * @ks: The device state.
450 * @buff: The buffer address
451 * @len: The length of the data to read
453 * Issue an RXQ FIFO read command and read the @len amount of data from
454 * the FIFO into the buffer specified by @buff.
456 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
458 struct spi_transfer *xfer = ks->spi_xfer2;
459 struct spi_message *msg = &ks->spi_msg2;
463 netif_dbg(ks, rx_status, ks->netdev,
464 "%s: %d@%p\n", __func__, len, buff);
466 /* set the operation we're issuing */
467 txb[0] = KS_SPIOP_RXFIFO;
478 ret = spi_sync(ks->spidev, msg);
480 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
484 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
485 * @ks: The device state
486 * @rxpkt: The data for the received packet
488 * Dump the initial data from the packet to dev_dbg().
490 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
492 netdev_dbg(ks->netdev,
493 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
494 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
495 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
496 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
500 * ks8851_rx_pkts - receive packets from the host
501 * @ks: The device information.
503 * This is called from the IRQ work queue when the system detects that there
504 * are packets in the receive queue. Find out how many packets there are and
505 * read them from the FIFO.
507 static void ks8851_rx_pkts(struct ks8851_net *ks)
516 rxfc = ks8851_rdreg8(ks, KS_RXFC);
518 netif_dbg(ks, rx_status, ks->netdev,
519 "%s: %d packets\n", __func__, rxfc);
521 /* Currently we're issuing a read per packet, but we could possibly
522 * improve the code by issuing a single read, getting the receive
523 * header, allocating the packet and then reading the packet data
526 * This form of operation would require us to hold the SPI bus'
527 * chipselect low during the entie transaction to avoid any
528 * reset to the data stream coming from the chip.
531 for (; rxfc != 0; rxfc--) {
532 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
533 rxstat = rxh & 0xffff;
534 rxlen = (rxh >> 16) & 0xfff;
536 netif_dbg(ks, rx_status, ks->netdev,
537 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
539 /* the length of the packet includes the 32bit CRC */
541 /* set dma read address */
542 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
544 /* start the packet dma process, and set auto-dequeue rx */
545 ks8851_wrreg16(ks, KS_RXQCR,
546 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
549 unsigned int rxalign;
552 rxalign = ALIGN(rxlen, 4);
553 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
556 /* 4 bytes of status header + 4 bytes of
557 * garbage: we put them before ethernet
558 * header, so that they are copied,
562 rxpkt = skb_put(skb, rxlen) - 8;
564 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
566 if (netif_msg_pktdata(ks))
567 ks8851_dbg_dumpkkt(ks, rxpkt);
569 skb->protocol = eth_type_trans(skb, ks->netdev);
572 ks->netdev->stats.rx_packets++;
573 ks->netdev->stats.rx_bytes += rxlen;
577 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
582 * ks8851_irq - IRQ handler for dealing with interrupt requests
586 * This handler is invoked when the IRQ line asserts to find out what happened.
587 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
590 * Read the interrupt status, work out what needs to be done and then clear
591 * any of the interrupts that are not needed.
593 static irqreturn_t ks8851_irq(int irq, void *_ks)
595 struct ks8851_net *ks = _ks;
597 unsigned handled = 0;
599 mutex_lock(&ks->lock);
601 status = ks8851_rdreg16(ks, KS_ISR);
603 netif_dbg(ks, intr, ks->netdev,
604 "%s: status 0x%04x\n", __func__, status);
606 if (status & IRQ_LCI)
609 if (status & IRQ_LDI) {
610 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
611 pmecr &= ~PMECR_WKEVT_MASK;
612 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
617 if (status & IRQ_RXPSI)
618 handled |= IRQ_RXPSI;
620 if (status & IRQ_TXI) {
623 /* no lock here, tx queue should have been stopped */
625 /* update our idea of how much tx space is available to the
627 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
629 netif_dbg(ks, intr, ks->netdev,
630 "%s: txspace %d\n", __func__, ks->tx_space);
633 if (status & IRQ_RXI)
636 if (status & IRQ_SPIBEI) {
637 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
638 handled |= IRQ_SPIBEI;
641 ks8851_wrreg16(ks, KS_ISR, handled);
643 if (status & IRQ_RXI) {
644 /* the datasheet says to disable the rx interrupt during
645 * packet read-out, however we're masking the interrupt
646 * from the device so do not bother masking just the RX
647 * from the device. */
652 /* if something stopped the rx process, probably due to wanting
653 * to change the rx settings, then do something about restarting
655 if (status & IRQ_RXPSI) {
656 struct ks8851_rxctrl *rxc = &ks->rxctrl;
658 /* update the multicast hash table */
659 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
660 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
661 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
662 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
664 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
665 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
668 mutex_unlock(&ks->lock);
670 if (status & IRQ_LCI)
671 mii_check_link(&ks->mii);
673 if (status & IRQ_TXI)
674 netif_wake_queue(ks->netdev);
680 * calc_txlen - calculate size of message to send packet
681 * @len: Length of data
683 * Returns the size of the TXFIFO message needed to send
686 static inline unsigned calc_txlen(unsigned len)
688 return ALIGN(len + 4, 4);
692 * ks8851_wrpkt - write packet to TX FIFO
693 * @ks: The device state.
694 * @txp: The sk_buff to transmit.
695 * @irq: IRQ on completion of the packet.
697 * Send the @txp to the chip. This means creating the relevant packet header
698 * specifying the length of the packet and the other information the chip
699 * needs, such as IRQ on completion. Send the header and the packet data to
702 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
704 struct spi_transfer *xfer = ks->spi_xfer2;
705 struct spi_message *msg = &ks->spi_msg2;
709 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
710 __func__, txp, txp->len, txp->data, irq);
713 fid &= TXFR_TXFID_MASK;
716 fid |= TXFR_TXIC; /* irq on completion */
718 /* start header at txb[1] to align txw entries */
719 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
720 ks->txh.txw[1] = cpu_to_le16(fid);
721 ks->txh.txw[2] = cpu_to_le16(txp->len);
723 xfer->tx_buf = &ks->txh.txb[1];
728 xfer->tx_buf = txp->data;
730 xfer->len = ALIGN(txp->len, 4);
732 ret = spi_sync(ks->spidev, msg);
734 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
738 * ks8851_done_tx - update and then free skbuff after transmitting
739 * @ks: The device state
740 * @txb: The buffer transmitted
742 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
744 struct net_device *dev = ks->netdev;
746 dev->stats.tx_bytes += txb->len;
747 dev->stats.tx_packets++;
753 * ks8851_tx_work - process tx packet(s)
754 * @work: The work strucutre what was scheduled.
756 * This is called when a number of packets have been scheduled for
757 * transmission and need to be sent to the device.
759 static void ks8851_tx_work(struct work_struct *work)
761 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
763 bool last = skb_queue_empty(&ks->txq);
765 mutex_lock(&ks->lock);
768 txb = skb_dequeue(&ks->txq);
769 last = skb_queue_empty(&ks->txq);
772 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
773 ks8851_wrpkt(ks, txb, last);
774 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
775 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
777 ks8851_done_tx(ks, txb);
781 mutex_unlock(&ks->lock);
785 * ks8851_net_open - open network device
786 * @dev: The network device being opened.
788 * Called when the network device is marked active, such as a user executing
789 * 'ifconfig up' on the device.
791 static int ks8851_net_open(struct net_device *dev)
793 struct ks8851_net *ks = netdev_priv(dev);
795 /* lock the card, even if we may not actually be doing anything
796 * else at the moment */
797 mutex_lock(&ks->lock);
799 netif_dbg(ks, ifup, ks->netdev, "opening\n");
801 /* bring chip out of any power saving mode it was in */
802 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
804 /* issue a soft reset to the RX/TX QMU to put it into a known
806 ks8851_soft_reset(ks, GRR_QMU);
808 /* setup transmission parameters */
810 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
811 TXCR_TXPE | /* pad to min length */
812 TXCR_TXCRC | /* add CRC */
813 TXCR_TXFCE)); /* enable flow control */
815 /* auto-increment tx data, reset tx pointer */
816 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
818 /* setup receiver control */
820 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
821 RXCR1_RXFCE | /* enable flow control */
822 RXCR1_RXBE | /* broadcast enable */
823 RXCR1_RXUE | /* unicast enable */
824 RXCR1_RXE)); /* enable rx block */
826 /* transfer entire frames out in one go */
827 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
829 /* set receive counter timeouts */
830 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
831 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
832 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
834 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
835 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
836 RXQCR_RXDTTE); /* IRQ on time exceeded */
838 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
840 /* clear then enable interrupts */
842 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
843 IRQ_TXI | /* TX done */ \
844 IRQ_RXI | /* RX done */ \
845 IRQ_SPIBEI | /* SPI bus error */ \
846 IRQ_TXPSI | /* TX process stop */ \
847 IRQ_RXPSI) /* RX process stop */
849 ks->rc_ier = STD_IRQ;
850 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
851 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
853 netif_start_queue(ks->netdev);
855 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
857 mutex_unlock(&ks->lock);
862 * ks8851_net_stop - close network device
863 * @dev: The device being closed.
865 * Called to close down a network device which has been active. Cancell any
866 * work, shutdown the RX and TX process and then place the chip into a low
867 * power state whilst it is not being used.
869 static int ks8851_net_stop(struct net_device *dev)
871 struct ks8851_net *ks = netdev_priv(dev);
873 netif_info(ks, ifdown, dev, "shutting down\n");
875 netif_stop_queue(dev);
877 mutex_lock(&ks->lock);
878 /* turn off the IRQs and ack any outstanding */
879 ks8851_wrreg16(ks, KS_IER, 0x0000);
880 ks8851_wrreg16(ks, KS_ISR, 0xffff);
881 mutex_unlock(&ks->lock);
883 /* stop any outstanding work */
884 flush_work(&ks->tx_work);
885 flush_work(&ks->rxctrl_work);
887 mutex_lock(&ks->lock);
888 /* shutdown RX process */
889 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
891 /* shutdown TX process */
892 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
894 /* set powermode to soft power down to save power */
895 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
896 mutex_unlock(&ks->lock);
898 /* ensure any queued tx buffers are dumped */
899 while (!skb_queue_empty(&ks->txq)) {
900 struct sk_buff *txb = skb_dequeue(&ks->txq);
902 netif_dbg(ks, ifdown, ks->netdev,
903 "%s: freeing txb %p\n", __func__, txb);
912 * ks8851_start_xmit - transmit packet
913 * @skb: The buffer to transmit
914 * @dev: The device used to transmit the packet.
916 * Called by the network layer to transmit the @skb. Queue the packet for
917 * the device and schedule the necessary work to transmit the packet when
920 * We do this to firstly avoid sleeping with the network device locked,
921 * and secondly so we can round up more than one packet to transmit which
922 * means we can try and avoid generating too many transmit done interrupts.
924 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
925 struct net_device *dev)
927 struct ks8851_net *ks = netdev_priv(dev);
928 unsigned needed = calc_txlen(skb->len);
929 netdev_tx_t ret = NETDEV_TX_OK;
931 netif_dbg(ks, tx_queued, ks->netdev,
932 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
934 spin_lock(&ks->statelock);
936 if (needed > ks->tx_space) {
937 netif_stop_queue(dev);
938 ret = NETDEV_TX_BUSY;
940 ks->tx_space -= needed;
941 skb_queue_tail(&ks->txq, skb);
944 spin_unlock(&ks->statelock);
945 schedule_work(&ks->tx_work);
951 * ks8851_rxctrl_work - work handler to change rx mode
952 * @work: The work structure this belongs to.
954 * Lock the device and issue the necessary changes to the receive mode from
955 * the network device layer. This is done so that we can do this without
956 * having to sleep whilst holding the network device lock.
958 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
959 * receive parameters are programmed, we issue a write to disable the RXQ and
960 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
961 * complete. The interrupt handler then writes the new values into the chip.
963 static void ks8851_rxctrl_work(struct work_struct *work)
965 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
967 mutex_lock(&ks->lock);
969 /* need to shutdown RXQ before modifying filter parameters */
970 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
972 mutex_unlock(&ks->lock);
975 static void ks8851_set_rx_mode(struct net_device *dev)
977 struct ks8851_net *ks = netdev_priv(dev);
978 struct ks8851_rxctrl rxctrl;
980 memset(&rxctrl, 0, sizeof(rxctrl));
982 if (dev->flags & IFF_PROMISC) {
983 /* interface to receive everything */
985 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
986 } else if (dev->flags & IFF_ALLMULTI) {
987 /* accept all multicast packets */
989 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
990 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
991 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
992 struct netdev_hw_addr *ha;
995 /* accept some multicast */
997 netdev_for_each_mc_addr(ha, dev) {
998 crc = ether_crc(ETH_ALEN, ha->addr);
999 crc >>= (32 - 6); /* get top six bits */
1001 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1004 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1006 /* just accept broadcast / unicast */
1007 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1010 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1011 RXCR1_RXBE | /* broadcast enable */
1012 RXCR1_RXE | /* RX process enable */
1013 RXCR1_RXFCE); /* enable flow control */
1015 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1017 /* schedule work to do the actual set of the data if needed */
1019 spin_lock(&ks->statelock);
1021 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1022 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1023 schedule_work(&ks->rxctrl_work);
1026 spin_unlock(&ks->statelock);
1029 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1031 struct sockaddr *sa = addr;
1033 if (netif_running(dev))
1036 if (!is_valid_ether_addr(sa->sa_data))
1037 return -EADDRNOTAVAIL;
1039 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1040 return ks8851_write_mac_addr(dev);
1043 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1045 struct ks8851_net *ks = netdev_priv(dev);
1047 if (!netif_running(dev))
1050 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1053 static const struct net_device_ops ks8851_netdev_ops = {
1054 .ndo_open = ks8851_net_open,
1055 .ndo_stop = ks8851_net_stop,
1056 .ndo_do_ioctl = ks8851_net_ioctl,
1057 .ndo_start_xmit = ks8851_start_xmit,
1058 .ndo_set_mac_address = ks8851_set_mac_address,
1059 .ndo_set_rx_mode = ks8851_set_rx_mode,
1060 .ndo_change_mtu = eth_change_mtu,
1061 .ndo_validate_addr = eth_validate_addr,
1064 /* ethtool support */
1066 static void ks8851_get_drvinfo(struct net_device *dev,
1067 struct ethtool_drvinfo *di)
1069 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1070 strlcpy(di->version, "1.00", sizeof(di->version));
1071 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1074 static u32 ks8851_get_msglevel(struct net_device *dev)
1076 struct ks8851_net *ks = netdev_priv(dev);
1077 return ks->msg_enable;
1080 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1082 struct ks8851_net *ks = netdev_priv(dev);
1083 ks->msg_enable = to;
1086 static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1088 struct ks8851_net *ks = netdev_priv(dev);
1089 return mii_ethtool_gset(&ks->mii, cmd);
1092 static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1094 struct ks8851_net *ks = netdev_priv(dev);
1095 return mii_ethtool_sset(&ks->mii, cmd);
1098 static u32 ks8851_get_link(struct net_device *dev)
1100 struct ks8851_net *ks = netdev_priv(dev);
1101 return mii_link_ok(&ks->mii);
1104 static int ks8851_nway_reset(struct net_device *dev)
1106 struct ks8851_net *ks = netdev_priv(dev);
1107 return mii_nway_restart(&ks->mii);
1110 /* EEPROM support */
1112 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1114 struct ks8851_net *ks = ee->data;
1117 val = ks8851_rdreg16(ks, KS_EEPCR);
1119 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1120 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1121 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1124 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1126 struct ks8851_net *ks = ee->data;
1127 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1130 val |= EEPCR_EESRWA;
1131 if (ee->reg_data_in)
1133 if (ee->reg_data_clock)
1135 if (ee->reg_chip_select)
1138 ks8851_wrreg16(ks, KS_EEPCR, val);
1142 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1143 * @ks: The network device state.
1145 * Check for the presence of an EEPROM, and then activate software access
1148 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1150 if (!(ks->rc_ccr & CCR_EEPROM))
1153 mutex_lock(&ks->lock);
1155 /* start with clock low, cs high */
1156 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1161 * ks8851_eeprom_release - release the EEPROM interface
1162 * @ks: The device state
1164 * Release the software access to the device EEPROM
1166 static void ks8851_eeprom_release(struct ks8851_net *ks)
1168 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1170 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1171 mutex_unlock(&ks->lock);
1174 #define KS_EEPROM_MAGIC (0x00008851)
1176 static int ks8851_set_eeprom(struct net_device *dev,
1177 struct ethtool_eeprom *ee, u8 *data)
1179 struct ks8851_net *ks = netdev_priv(dev);
1180 int offset = ee->offset;
1184 /* currently only support byte writing */
1188 if (ee->magic != KS_EEPROM_MAGIC)
1191 if (ks8851_eeprom_claim(ks))
1194 eeprom_93cx6_wren(&ks->eeprom, true);
1196 /* ethtool currently only supports writing bytes, which means
1197 * we have to read/modify/write our 16bit EEPROMs */
1199 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1209 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1210 eeprom_93cx6_wren(&ks->eeprom, false);
1212 ks8851_eeprom_release(ks);
1217 static int ks8851_get_eeprom(struct net_device *dev,
1218 struct ethtool_eeprom *ee, u8 *data)
1220 struct ks8851_net *ks = netdev_priv(dev);
1221 int offset = ee->offset;
1224 /* must be 2 byte aligned */
1225 if (len & 1 || offset & 1)
1228 if (ks8851_eeprom_claim(ks))
1231 ee->magic = KS_EEPROM_MAGIC;
1233 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1234 ks8851_eeprom_release(ks);
1239 static int ks8851_get_eeprom_len(struct net_device *dev)
1241 struct ks8851_net *ks = netdev_priv(dev);
1243 /* currently, we assume it is an 93C46 attached, so return 128 */
1244 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1247 static const struct ethtool_ops ks8851_ethtool_ops = {
1248 .get_drvinfo = ks8851_get_drvinfo,
1249 .get_msglevel = ks8851_get_msglevel,
1250 .set_msglevel = ks8851_set_msglevel,
1251 .get_settings = ks8851_get_settings,
1252 .set_settings = ks8851_set_settings,
1253 .get_link = ks8851_get_link,
1254 .nway_reset = ks8851_nway_reset,
1255 .get_eeprom_len = ks8851_get_eeprom_len,
1256 .get_eeprom = ks8851_get_eeprom,
1257 .set_eeprom = ks8851_set_eeprom,
1260 /* MII interface controls */
1263 * ks8851_phy_reg - convert MII register into a KS8851 register
1264 * @reg: MII register number.
1266 * Return the KS8851 register number for the corresponding MII PHY register
1267 * if possible. Return zero if the MII register has no direct mapping to the
1268 * KS8851 register set.
1270 static int ks8851_phy_reg(int reg)
1291 * ks8851_phy_read - MII interface PHY register read.
1292 * @dev: The network device the PHY is on.
1293 * @phy_addr: Address of PHY (ignored as we only have one)
1294 * @reg: The register to read.
1296 * This call reads data from the PHY register specified in @reg. Since the
1297 * device does not support all the MII registers, the non-existent values
1298 * are always returned as zero.
1300 * We return zero for unsupported registers as the MII code does not check
1301 * the value returned for any error status, and simply returns it to the
1302 * caller. The mii-tool that the driver was tested with takes any -ve error
1303 * as real PHY capabilities, thus displaying incorrect data to the user.
1305 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1307 struct ks8851_net *ks = netdev_priv(dev);
1311 ksreg = ks8851_phy_reg(reg);
1313 return 0x0; /* no error return allowed, so use zero */
1315 mutex_lock(&ks->lock);
1316 result = ks8851_rdreg16(ks, ksreg);
1317 mutex_unlock(&ks->lock);
1322 static void ks8851_phy_write(struct net_device *dev,
1323 int phy, int reg, int value)
1325 struct ks8851_net *ks = netdev_priv(dev);
1328 ksreg = ks8851_phy_reg(reg);
1330 mutex_lock(&ks->lock);
1331 ks8851_wrreg16(ks, ksreg, value);
1332 mutex_unlock(&ks->lock);
1337 * ks8851_read_selftest - read the selftest memory info.
1338 * @ks: The device state
1340 * Read and check the TX/RX memory selftest information.
1342 static int ks8851_read_selftest(struct ks8851_net *ks)
1344 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1348 rd = ks8851_rdreg16(ks, KS_MBIR);
1350 if ((rd & both_done) != both_done) {
1351 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1355 if (rd & MBIR_TXMBFA) {
1356 netdev_err(ks->netdev, "TX memory selftest fail\n");
1360 if (rd & MBIR_RXMBFA) {
1361 netdev_err(ks->netdev, "RX memory selftest fail\n");
1368 /* driver bus management functions */
1370 #ifdef CONFIG_PM_SLEEP
1372 static int ks8851_suspend(struct device *dev)
1374 struct ks8851_net *ks = dev_get_drvdata(dev);
1375 struct net_device *netdev = ks->netdev;
1377 if (netif_running(netdev)) {
1378 netif_device_detach(netdev);
1379 ks8851_net_stop(netdev);
1385 static int ks8851_resume(struct device *dev)
1387 struct ks8851_net *ks = dev_get_drvdata(dev);
1388 struct net_device *netdev = ks->netdev;
1390 if (netif_running(netdev)) {
1391 ks8851_net_open(netdev);
1392 netif_device_attach(netdev);
1399 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1401 static int ks8851_probe(struct spi_device *spi)
1403 struct net_device *ndev;
1404 struct ks8851_net *ks;
1408 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1412 spi->bits_per_word = 8;
1414 ks = netdev_priv(ndev);
1418 ks->tx_space = 6144;
1420 ks->vdd_reg = regulator_get_optional(&spi->dev, "vdd");
1421 if (IS_ERR(ks->vdd_reg)) {
1422 ret = PTR_ERR(ks->vdd_reg);
1423 if (ret == -EPROBE_DEFER)
1426 ret = regulator_enable(ks->vdd_reg);
1428 dev_err(&spi->dev, "regulator enable fail: %d\n",
1435 mutex_init(&ks->lock);
1436 spin_lock_init(&ks->statelock);
1438 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1439 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1441 /* initialise pre-made spi transfer messages */
1443 spi_message_init(&ks->spi_msg1);
1444 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1446 spi_message_init(&ks->spi_msg2);
1447 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1448 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1450 /* setup EEPROM state */
1452 ks->eeprom.data = ks;
1453 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1454 ks->eeprom.register_read = ks8851_eeprom_regread;
1455 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1457 /* setup mii state */
1460 ks->mii.phy_id_mask = 1;
1461 ks->mii.reg_num_mask = 0xf;
1462 ks->mii.mdio_read = ks8851_phy_read;
1463 ks->mii.mdio_write = ks8851_phy_write;
1465 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1467 /* set the default message enable */
1468 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1472 skb_queue_head_init(&ks->txq);
1474 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1475 SET_NETDEV_DEV(ndev, &spi->dev);
1477 spi_set_drvdata(spi, ks);
1479 ndev->if_port = IF_PORT_100BASET;
1480 ndev->netdev_ops = &ks8851_netdev_ops;
1481 ndev->irq = spi->irq;
1483 /* issue a global soft reset to reset the device. */
1484 ks8851_soft_reset(ks, GRR_GSR);
1486 /* simple check for a valid chip being connected to the bus */
1487 cider = ks8851_rdreg16(ks, KS_CIDER);
1488 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1489 dev_err(&spi->dev, "failed to read device ID\n");
1494 /* cache the contents of the CCR register for EEPROM, etc. */
1495 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1497 if (ks->rc_ccr & CCR_EEPROM)
1498 ks->eeprom_size = 128;
1500 ks->eeprom_size = 0;
1502 ks8851_read_selftest(ks);
1503 ks8851_init_mac(ks);
1505 ret = request_threaded_irq(spi->irq, NULL, ks8851_irq,
1506 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1509 dev_err(&spi->dev, "failed to get irq\n");
1513 ret = register_netdev(ndev);
1515 dev_err(&spi->dev, "failed to register network device\n");
1519 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1520 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
1521 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1527 free_irq(ndev->irq, ks);
1531 if (!IS_ERR(ks->vdd_reg))
1532 regulator_disable(ks->vdd_reg);
1534 if (!IS_ERR(ks->vdd_reg))
1535 regulator_put(ks->vdd_reg);
1541 static int ks8851_remove(struct spi_device *spi)
1543 struct ks8851_net *priv = spi_get_drvdata(spi);
1545 if (netif_msg_drv(priv))
1546 dev_info(&spi->dev, "remove\n");
1548 unregister_netdev(priv->netdev);
1549 free_irq(spi->irq, priv);
1550 if (!IS_ERR(priv->vdd_reg)) {
1551 regulator_disable(priv->vdd_reg);
1552 regulator_put(priv->vdd_reg);
1554 free_netdev(priv->netdev);
1559 static struct spi_driver ks8851_driver = {
1562 .owner = THIS_MODULE,
1563 .pm = &ks8851_pm_ops,
1565 .probe = ks8851_probe,
1566 .remove = ks8851_remove,
1568 module_spi_driver(ks8851_driver);
1570 MODULE_DESCRIPTION("KS8851 Network driver");
1572 MODULE_LICENSE("GPL");
1574 module_param_named(message, msg_enable, int, 0);
1575 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1576 MODULE_ALIAS("spi:ks8851");