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
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>
29 #include <linux/gpio.h>
30 #include <linux/of_gpio.h>
35 * struct ks8851_rxctrl - KS8851 driver rx control
36 * @mchash: Multicast hash-table data.
37 * @rxcr1: KS_RXCR1 register setting
38 * @rxcr2: KS_RXCR2 register setting
40 * Representation of the settings needs to control the receive filtering
41 * such as the multicast hash-filter and the receive register settings. This
42 * is used to make the job of working out if the receive settings change and
43 * then issuing the new settings to the worker that will send the necessary
46 struct ks8851_rxctrl {
53 * union ks8851_tx_hdr - tx header data
54 * @txb: The header as bytes
55 * @txw: The header as 16bit, little-endian words
57 * A dual representation of the tx header data to allow
58 * access to individual bytes, and to allow 16bit accesses
59 * with 16bit alignment.
67 * struct ks8851_net - KS8851 driver private data
68 * @netdev: The network device we're bound to
69 * @spidev: The spi device we're bound to.
70 * @lock: Lock to ensure that the device is not accessed when busy.
71 * @statelock: Lock on this structure for tx list.
72 * @mii: The MII state information for the mii calls.
73 * @rxctrl: RX settings for @rxctrl_work.
74 * @tx_work: Work queue for tx packets
75 * @rxctrl_work: Work queue for updating RX mode and multicast lists
76 * @txq: Queue of packets for transmission.
77 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
78 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
79 * @txh: Space for generating packet TX header in DMA-able data
80 * @rxd: Space for receiving SPI data, in DMA-able space.
81 * @txd: Space for transmitting SPI data, in DMA-able space.
82 * @msg_enable: The message flags controlling driver output (see ethtool).
83 * @fid: Incrementing frame id tag.
84 * @rc_ier: Cached copy of KS_IER.
85 * @rc_ccr: Cached copy of KS_CCR.
86 * @rc_rxqcr: Cached copy of KS_RXQCR.
87 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
88 * @vdd_reg: Optional regulator supplying the chip
89 * @vdd_io: Optional digital power supply for IO
90 * @gpio: Optional reset_n gpio
92 * The @lock ensures that the chip is protected when certain operations are
93 * in progress. When the read or write packet transfer is in progress, most
94 * of the chip registers are not ccessible until the transfer is finished and
95 * the DMA has been de-asserted.
97 * The @statelock is used to protect information in the structure which may
98 * need to be accessed via several sources, such as the network driver layer
99 * or one of the work queues.
101 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
102 * wants to DMA map them, it will not have any problems with data the driver
106 struct net_device *netdev;
107 struct spi_device *spidev;
109 spinlock_t statelock;
111 union ks8851_tx_hdr txh ____cacheline_aligned;
115 u32 msg_enable ____cacheline_aligned;
123 struct mii_if_info mii;
124 struct ks8851_rxctrl rxctrl;
126 struct work_struct tx_work;
127 struct work_struct rxctrl_work;
129 struct sk_buff_head txq;
131 struct spi_message spi_msg1;
132 struct spi_message spi_msg2;
133 struct spi_transfer spi_xfer1;
134 struct spi_transfer spi_xfer2[2];
136 struct eeprom_93cx6 eeprom;
137 struct regulator *vdd_reg;
138 struct regulator *vdd_io;
142 static int msg_enable;
144 /* shift for byte-enable data */
145 #define BYTE_EN(_x) ((_x) << 2)
147 /* turn register number and byte-enable mask into data for start of packet */
148 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
150 /* SPI register read/write calls.
152 * All these calls issue SPI transactions to access the chip's registers. They
153 * all require that the necessary lock is held to prevent accesses when the
154 * chip is busy transferring packet data (RX/TX FIFO accesses).
158 * ks8851_wrreg16 - write 16bit register value to chip
159 * @ks: The chip state
160 * @reg: The register address
161 * @val: The value to write
163 * Issue a write to put the value @val into the register specified in @reg.
165 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
167 struct spi_transfer *xfer = &ks->spi_xfer1;
168 struct spi_message *msg = &ks->spi_msg1;
172 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
173 txb[1] = cpu_to_le16(val);
179 ret = spi_sync(ks->spidev, msg);
181 netdev_err(ks->netdev, "spi_sync() failed\n");
185 * ks8851_wrreg8 - write 8bit register value to chip
186 * @ks: The chip state
187 * @reg: The register address
188 * @val: The value to write
190 * Issue a write to put the value @val into the register specified in @reg.
192 static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
194 struct spi_transfer *xfer = &ks->spi_xfer1;
195 struct spi_message *msg = &ks->spi_msg1;
200 bit = 1 << (reg & 3);
202 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
209 ret = spi_sync(ks->spidev, msg);
211 netdev_err(ks->netdev, "spi_sync() failed\n");
215 * ks8851_rdreg - issue read register command and return the data
216 * @ks: The device state
217 * @op: The register address and byte enables in message format.
218 * @rxb: The RX buffer to return the result into
219 * @rxl: The length of data expected.
221 * This is the low level read call that issues the necessary spi message(s)
222 * to read data from the register specified in @op.
224 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
225 u8 *rxb, unsigned rxl)
227 struct spi_transfer *xfer;
228 struct spi_message *msg;
229 __le16 *txb = (__le16 *)ks->txd;
233 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
235 if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
237 xfer = ks->spi_xfer2;
249 xfer = &ks->spi_xfer1;
256 ret = spi_sync(ks->spidev, msg);
258 netdev_err(ks->netdev, "read: spi_sync() failed\n");
259 else if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
260 memcpy(rxb, trx, rxl);
262 memcpy(rxb, trx + 2, rxl);
266 * ks8851_rdreg8 - read 8 bit register from device
267 * @ks: The chip information
268 * @reg: The register address
270 * Read a 8bit register from the chip, returning the result
272 static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
276 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
281 * ks8851_rdreg16 - read 16 bit register from device
282 * @ks: The chip information
283 * @reg: The register address
285 * Read a 16bit register from the chip, returning the result
287 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
291 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
292 return le16_to_cpu(rx);
296 * ks8851_rdreg32 - read 32 bit register from device
297 * @ks: The chip information
298 * @reg: The register address
300 * Read a 32bit register from the chip.
302 * Note, this read requires the address be aligned to 4 bytes.
304 static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
310 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
311 return le32_to_cpu(rx);
315 * ks8851_soft_reset - issue one of the soft reset to the device
316 * @ks: The device state.
317 * @op: The bit(s) to set in the GRR
319 * Issue the relevant soft-reset command to the device's GRR register
322 * Note, the delays are in there as a caution to ensure that the reset
323 * has time to take effect and then complete. Since the datasheet does
324 * not currently specify the exact sequence, we have chosen something
325 * that seems to work with our device.
327 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
329 ks8851_wrreg16(ks, KS_GRR, op);
330 mdelay(1); /* wait a short time to effect reset */
331 ks8851_wrreg16(ks, KS_GRR, 0);
332 mdelay(1); /* wait for condition to clear */
336 * ks8851_set_powermode - set power mode of the device
337 * @ks: The device state
338 * @pwrmode: The power mode value to write to KS_PMECR.
340 * Change the power mode of the chip.
342 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
346 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
348 pmecr = ks8851_rdreg16(ks, KS_PMECR);
349 pmecr &= ~PMECR_PM_MASK;
352 ks8851_wrreg16(ks, KS_PMECR, pmecr);
356 * ks8851_write_mac_addr - write mac address to device registers
357 * @dev: The network device
359 * Update the KS8851 MAC address registers from the address in @dev.
361 * This call assumes that the chip is not running, so there is no need to
362 * shutdown the RXQ process whilst setting this.
364 static int ks8851_write_mac_addr(struct net_device *dev)
366 struct ks8851_net *ks = netdev_priv(dev);
369 mutex_lock(&ks->lock);
372 * Wake up chip in case it was powered off when stopped; otherwise,
373 * the first write to the MAC address does not take effect.
375 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
376 for (i = 0; i < ETH_ALEN; i++)
377 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
378 if (!netif_running(dev))
379 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
381 mutex_unlock(&ks->lock);
387 * ks8851_read_mac_addr - read mac address from device registers
388 * @dev: The network device
390 * Update our copy of the KS8851 MAC address from the registers of @dev.
392 static void ks8851_read_mac_addr(struct net_device *dev)
394 struct ks8851_net *ks = netdev_priv(dev);
397 mutex_lock(&ks->lock);
399 for (i = 0; i < ETH_ALEN; i++)
400 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
402 mutex_unlock(&ks->lock);
406 * ks8851_init_mac - initialise the mac address
407 * @ks: The device structure
409 * Get or create the initial mac address for the device and then set that
410 * into the station address register. If there is an EEPROM present, then
411 * we try that. If no valid mac address is found we use eth_random_addr()
412 * to create a new one.
414 static void ks8851_init_mac(struct ks8851_net *ks)
416 struct net_device *dev = ks->netdev;
418 /* first, try reading what we've got already */
419 if (ks->rc_ccr & CCR_EEPROM) {
420 ks8851_read_mac_addr(dev);
421 if (is_valid_ether_addr(dev->dev_addr))
424 netdev_err(ks->netdev, "invalid mac address read %pM\n",
428 eth_hw_addr_random(dev);
429 ks8851_write_mac_addr(dev);
433 * ks8851_rdfifo - read data from the receive fifo
434 * @ks: The device state.
435 * @buff: The buffer address
436 * @len: The length of the data to read
438 * Issue an RXQ FIFO read command and read the @len amount of data from
439 * the FIFO into the buffer specified by @buff.
441 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
443 struct spi_transfer *xfer = ks->spi_xfer2;
444 struct spi_message *msg = &ks->spi_msg2;
448 netif_dbg(ks, rx_status, ks->netdev,
449 "%s: %d@%p\n", __func__, len, buff);
451 /* set the operation we're issuing */
452 txb[0] = KS_SPIOP_RXFIFO;
463 ret = spi_sync(ks->spidev, msg);
465 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
469 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
470 * @ks: The device state
471 * @rxpkt: The data for the received packet
473 * Dump the initial data from the packet to dev_dbg().
475 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
477 netdev_dbg(ks->netdev,
478 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
479 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
480 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
481 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
485 * ks8851_rx_pkts - receive packets from the host
486 * @ks: The device information.
488 * This is called from the IRQ work queue when the system detects that there
489 * are packets in the receive queue. Find out how many packets there are and
490 * read them from the FIFO.
492 static void ks8851_rx_pkts(struct ks8851_net *ks)
501 rxfc = ks8851_rdreg8(ks, KS_RXFC);
503 netif_dbg(ks, rx_status, ks->netdev,
504 "%s: %d packets\n", __func__, rxfc);
506 /* Currently we're issuing a read per packet, but we could possibly
507 * improve the code by issuing a single read, getting the receive
508 * header, allocating the packet and then reading the packet data
511 * This form of operation would require us to hold the SPI bus'
512 * chipselect low during the entie transaction to avoid any
513 * reset to the data stream coming from the chip.
516 for (; rxfc != 0; rxfc--) {
517 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
518 rxstat = rxh & 0xffff;
519 rxlen = (rxh >> 16) & 0xfff;
521 netif_dbg(ks, rx_status, ks->netdev,
522 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
524 /* the length of the packet includes the 32bit CRC */
526 /* set dma read address */
527 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
529 /* start DMA access */
530 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
533 unsigned int rxalign;
536 rxalign = ALIGN(rxlen, 4);
537 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
540 /* 4 bytes of status header + 4 bytes of
541 * garbage: we put them before ethernet
542 * header, so that they are copied,
546 rxpkt = skb_put(skb, rxlen) - 8;
548 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
550 if (netif_msg_pktdata(ks))
551 ks8851_dbg_dumpkkt(ks, rxpkt);
553 skb->protocol = eth_type_trans(skb, ks->netdev);
556 ks->netdev->stats.rx_packets++;
557 ks->netdev->stats.rx_bytes += rxlen;
561 /* end DMA access and dequeue packet */
562 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
567 * ks8851_irq - IRQ handler for dealing with interrupt requests
571 * This handler is invoked when the IRQ line asserts to find out what happened.
572 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
575 * Read the interrupt status, work out what needs to be done and then clear
576 * any of the interrupts that are not needed.
578 static irqreturn_t ks8851_irq(int irq, void *_ks)
580 struct ks8851_net *ks = _ks;
582 unsigned handled = 0;
584 mutex_lock(&ks->lock);
586 status = ks8851_rdreg16(ks, KS_ISR);
588 netif_dbg(ks, intr, ks->netdev,
589 "%s: status 0x%04x\n", __func__, status);
591 if (status & IRQ_LCI)
594 if (status & IRQ_LDI) {
595 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
596 pmecr &= ~PMECR_WKEVT_MASK;
597 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
602 if (status & IRQ_RXPSI)
603 handled |= IRQ_RXPSI;
605 if (status & IRQ_TXI) {
608 /* no lock here, tx queue should have been stopped */
610 /* update our idea of how much tx space is available to the
612 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
614 netif_dbg(ks, intr, ks->netdev,
615 "%s: txspace %d\n", __func__, ks->tx_space);
618 if (status & IRQ_RXI)
621 if (status & IRQ_SPIBEI) {
622 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
623 handled |= IRQ_SPIBEI;
626 ks8851_wrreg16(ks, KS_ISR, handled);
628 if (status & IRQ_RXI) {
629 /* the datasheet says to disable the rx interrupt during
630 * packet read-out, however we're masking the interrupt
631 * from the device so do not bother masking just the RX
632 * from the device. */
637 /* if something stopped the rx process, probably due to wanting
638 * to change the rx settings, then do something about restarting
640 if (status & IRQ_RXPSI) {
641 struct ks8851_rxctrl *rxc = &ks->rxctrl;
643 /* update the multicast hash table */
644 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
645 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
646 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
647 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
649 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
650 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
653 mutex_unlock(&ks->lock);
655 if (status & IRQ_LCI)
656 mii_check_link(&ks->mii);
658 if (status & IRQ_TXI)
659 netif_wake_queue(ks->netdev);
665 * calc_txlen - calculate size of message to send packet
666 * @len: Length of data
668 * Returns the size of the TXFIFO message needed to send
671 static inline unsigned calc_txlen(unsigned len)
673 return ALIGN(len + 4, 4);
677 * ks8851_wrpkt - write packet to TX FIFO
678 * @ks: The device state.
679 * @txp: The sk_buff to transmit.
680 * @irq: IRQ on completion of the packet.
682 * Send the @txp to the chip. This means creating the relevant packet header
683 * specifying the length of the packet and the other information the chip
684 * needs, such as IRQ on completion. Send the header and the packet data to
687 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
689 struct spi_transfer *xfer = ks->spi_xfer2;
690 struct spi_message *msg = &ks->spi_msg2;
694 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
695 __func__, txp, txp->len, txp->data, irq);
698 fid &= TXFR_TXFID_MASK;
701 fid |= TXFR_TXIC; /* irq on completion */
703 /* start header at txb[1] to align txw entries */
704 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
705 ks->txh.txw[1] = cpu_to_le16(fid);
706 ks->txh.txw[2] = cpu_to_le16(txp->len);
708 xfer->tx_buf = &ks->txh.txb[1];
713 xfer->tx_buf = txp->data;
715 xfer->len = ALIGN(txp->len, 4);
717 ret = spi_sync(ks->spidev, msg);
719 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
723 * ks8851_done_tx - update and then free skbuff after transmitting
724 * @ks: The device state
725 * @txb: The buffer transmitted
727 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
729 struct net_device *dev = ks->netdev;
731 dev->stats.tx_bytes += txb->len;
732 dev->stats.tx_packets++;
738 * ks8851_tx_work - process tx packet(s)
739 * @work: The work strucutre what was scheduled.
741 * This is called when a number of packets have been scheduled for
742 * transmission and need to be sent to the device.
744 static void ks8851_tx_work(struct work_struct *work)
746 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
748 bool last = skb_queue_empty(&ks->txq);
750 mutex_lock(&ks->lock);
753 txb = skb_dequeue(&ks->txq);
754 last = skb_queue_empty(&ks->txq);
757 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
758 ks8851_wrpkt(ks, txb, last);
759 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
760 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
762 ks8851_done_tx(ks, txb);
766 mutex_unlock(&ks->lock);
770 * ks8851_net_open - open network device
771 * @dev: The network device being opened.
773 * Called when the network device is marked active, such as a user executing
774 * 'ifconfig up' on the device.
776 static int ks8851_net_open(struct net_device *dev)
778 struct ks8851_net *ks = netdev_priv(dev);
781 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
782 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
785 netdev_err(dev, "failed to get irq\n");
789 /* lock the card, even if we may not actually be doing anything
790 * else at the moment */
791 mutex_lock(&ks->lock);
793 netif_dbg(ks, ifup, ks->netdev, "opening\n");
795 /* bring chip out of any power saving mode it was in */
796 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
798 /* issue a soft reset to the RX/TX QMU to put it into a known
800 ks8851_soft_reset(ks, GRR_QMU);
802 /* setup transmission parameters */
804 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
805 TXCR_TXPE | /* pad to min length */
806 TXCR_TXCRC | /* add CRC */
807 TXCR_TXFCE)); /* enable flow control */
809 /* auto-increment tx data, reset tx pointer */
810 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
812 /* setup receiver control */
814 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
815 RXCR1_RXFCE | /* enable flow control */
816 RXCR1_RXBE | /* broadcast enable */
817 RXCR1_RXUE | /* unicast enable */
818 RXCR1_RXE)); /* enable rx block */
820 /* transfer entire frames out in one go */
821 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
823 /* set receive counter timeouts */
824 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
825 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
826 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
828 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
829 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
830 RXQCR_RXDTTE); /* IRQ on time exceeded */
832 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
834 /* clear then enable interrupts */
836 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
837 IRQ_TXI | /* TX done */ \
838 IRQ_RXI | /* RX done */ \
839 IRQ_SPIBEI | /* SPI bus error */ \
840 IRQ_TXPSI | /* TX process stop */ \
841 IRQ_RXPSI) /* RX process stop */
843 ks->rc_ier = STD_IRQ;
844 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
845 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
847 netif_start_queue(ks->netdev);
849 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
851 mutex_unlock(&ks->lock);
852 mii_check_link(&ks->mii);
857 * ks8851_net_stop - close network device
858 * @dev: The device being closed.
860 * Called to close down a network device which has been active. Cancell any
861 * work, shutdown the RX and TX process and then place the chip into a low
862 * power state whilst it is not being used.
864 static int ks8851_net_stop(struct net_device *dev)
866 struct ks8851_net *ks = netdev_priv(dev);
868 netif_info(ks, ifdown, dev, "shutting down\n");
870 netif_stop_queue(dev);
872 mutex_lock(&ks->lock);
873 /* turn off the IRQs and ack any outstanding */
874 ks8851_wrreg16(ks, KS_IER, 0x0000);
875 ks8851_wrreg16(ks, KS_ISR, 0xffff);
876 mutex_unlock(&ks->lock);
878 /* stop any outstanding work */
879 flush_work(&ks->tx_work);
880 flush_work(&ks->rxctrl_work);
882 mutex_lock(&ks->lock);
883 /* shutdown RX process */
884 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
886 /* shutdown TX process */
887 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
889 /* set powermode to soft power down to save power */
890 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
891 mutex_unlock(&ks->lock);
893 /* ensure any queued tx buffers are dumped */
894 while (!skb_queue_empty(&ks->txq)) {
895 struct sk_buff *txb = skb_dequeue(&ks->txq);
897 netif_dbg(ks, ifdown, ks->netdev,
898 "%s: freeing txb %p\n", __func__, txb);
903 free_irq(dev->irq, ks);
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
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;
937 ks->tx_space -= needed;
938 skb_queue_tail(&ks->txq, skb);
941 spin_unlock(&ks->statelock);
942 schedule_work(&ks->tx_work);
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;
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;
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))
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))
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_validate_addr = eth_validate_addr,
1060 /* ethtool support */
1062 static void ks8851_get_drvinfo(struct net_device *dev,
1063 struct ethtool_drvinfo *di)
1065 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1066 strlcpy(di->version, "1.00", sizeof(di->version));
1067 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1070 static u32 ks8851_get_msglevel(struct net_device *dev)
1072 struct ks8851_net *ks = netdev_priv(dev);
1073 return ks->msg_enable;
1076 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1078 struct ks8851_net *ks = netdev_priv(dev);
1079 ks->msg_enable = to;
1082 static int ks8851_get_link_ksettings(struct net_device *dev,
1083 struct ethtool_link_ksettings *cmd)
1085 struct ks8851_net *ks = netdev_priv(dev);
1087 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1092 static int ks8851_set_link_ksettings(struct net_device *dev,
1093 const struct ethtool_link_ksettings *cmd)
1095 struct ks8851_net *ks = netdev_priv(dev);
1096 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1099 static u32 ks8851_get_link(struct net_device *dev)
1101 struct ks8851_net *ks = netdev_priv(dev);
1102 return mii_link_ok(&ks->mii);
1105 static int ks8851_nway_reset(struct net_device *dev)
1107 struct ks8851_net *ks = netdev_priv(dev);
1108 return mii_nway_restart(&ks->mii);
1111 /* EEPROM support */
1113 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1115 struct ks8851_net *ks = ee->data;
1118 val = ks8851_rdreg16(ks, KS_EEPCR);
1120 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1121 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1122 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1125 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1127 struct ks8851_net *ks = ee->data;
1128 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1131 val |= EEPCR_EESRWA;
1132 if (ee->reg_data_in)
1134 if (ee->reg_data_clock)
1136 if (ee->reg_chip_select)
1139 ks8851_wrreg16(ks, KS_EEPCR, val);
1143 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1144 * @ks: The network device state.
1146 * Check for the presence of an EEPROM, and then activate software access
1149 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1151 if (!(ks->rc_ccr & CCR_EEPROM))
1154 mutex_lock(&ks->lock);
1156 /* start with clock low, cs high */
1157 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1162 * ks8851_eeprom_release - release the EEPROM interface
1163 * @ks: The device state
1165 * Release the software access to the device EEPROM
1167 static void ks8851_eeprom_release(struct ks8851_net *ks)
1169 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1171 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1172 mutex_unlock(&ks->lock);
1175 #define KS_EEPROM_MAGIC (0x00008851)
1177 static int ks8851_set_eeprom(struct net_device *dev,
1178 struct ethtool_eeprom *ee, u8 *data)
1180 struct ks8851_net *ks = netdev_priv(dev);
1181 int offset = ee->offset;
1185 /* currently only support byte writing */
1189 if (ee->magic != KS_EEPROM_MAGIC)
1192 if (ks8851_eeprom_claim(ks))
1195 eeprom_93cx6_wren(&ks->eeprom, true);
1197 /* ethtool currently only supports writing bytes, which means
1198 * we have to read/modify/write our 16bit EEPROMs */
1200 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1210 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1211 eeprom_93cx6_wren(&ks->eeprom, false);
1213 ks8851_eeprom_release(ks);
1218 static int ks8851_get_eeprom(struct net_device *dev,
1219 struct ethtool_eeprom *ee, u8 *data)
1221 struct ks8851_net *ks = netdev_priv(dev);
1222 int offset = ee->offset;
1225 /* must be 2 byte aligned */
1226 if (len & 1 || offset & 1)
1229 if (ks8851_eeprom_claim(ks))
1232 ee->magic = KS_EEPROM_MAGIC;
1234 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1235 ks8851_eeprom_release(ks);
1240 static int ks8851_get_eeprom_len(struct net_device *dev)
1242 struct ks8851_net *ks = netdev_priv(dev);
1244 /* currently, we assume it is an 93C46 attached, so return 128 */
1245 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1248 static const struct ethtool_ops ks8851_ethtool_ops = {
1249 .get_drvinfo = ks8851_get_drvinfo,
1250 .get_msglevel = ks8851_get_msglevel,
1251 .set_msglevel = ks8851_set_msglevel,
1252 .get_link = ks8851_get_link,
1253 .nway_reset = ks8851_nway_reset,
1254 .get_eeprom_len = ks8851_get_eeprom_len,
1255 .get_eeprom = ks8851_get_eeprom,
1256 .set_eeprom = ks8851_set_eeprom,
1257 .get_link_ksettings = ks8851_get_link_ksettings,
1258 .set_link_ksettings = ks8851_set_link_ksettings,
1261 /* MII interface controls */
1264 * ks8851_phy_reg - convert MII register into a KS8851 register
1265 * @reg: MII register number.
1267 * Return the KS8851 register number for the corresponding MII PHY register
1268 * if possible. Return zero if the MII register has no direct mapping to the
1269 * KS8851 register set.
1271 static int ks8851_phy_reg(int reg)
1292 * ks8851_phy_read - MII interface PHY register read.
1293 * @dev: The network device the PHY is on.
1294 * @phy_addr: Address of PHY (ignored as we only have one)
1295 * @reg: The register to read.
1297 * This call reads data from the PHY register specified in @reg. Since the
1298 * device does not support all the MII registers, the non-existent values
1299 * are always returned as zero.
1301 * We return zero for unsupported registers as the MII code does not check
1302 * the value returned for any error status, and simply returns it to the
1303 * caller. The mii-tool that the driver was tested with takes any -ve error
1304 * as real PHY capabilities, thus displaying incorrect data to the user.
1306 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1308 struct ks8851_net *ks = netdev_priv(dev);
1312 ksreg = ks8851_phy_reg(reg);
1314 return 0x0; /* no error return allowed, so use zero */
1316 mutex_lock(&ks->lock);
1317 result = ks8851_rdreg16(ks, ksreg);
1318 mutex_unlock(&ks->lock);
1323 static void ks8851_phy_write(struct net_device *dev,
1324 int phy, int reg, int value)
1326 struct ks8851_net *ks = netdev_priv(dev);
1329 ksreg = ks8851_phy_reg(reg);
1331 mutex_lock(&ks->lock);
1332 ks8851_wrreg16(ks, ksreg, value);
1333 mutex_unlock(&ks->lock);
1338 * ks8851_read_selftest - read the selftest memory info.
1339 * @ks: The device state
1341 * Read and check the TX/RX memory selftest information.
1343 static int ks8851_read_selftest(struct ks8851_net *ks)
1345 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1349 rd = ks8851_rdreg16(ks, KS_MBIR);
1351 if ((rd & both_done) != both_done) {
1352 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1356 if (rd & MBIR_TXMBFA) {
1357 netdev_err(ks->netdev, "TX memory selftest fail\n");
1361 if (rd & MBIR_RXMBFA) {
1362 netdev_err(ks->netdev, "RX memory selftest fail\n");
1369 /* driver bus management functions */
1371 #ifdef CONFIG_PM_SLEEP
1373 static int ks8851_suspend(struct device *dev)
1375 struct ks8851_net *ks = dev_get_drvdata(dev);
1376 struct net_device *netdev = ks->netdev;
1378 if (netif_running(netdev)) {
1379 netif_device_detach(netdev);
1380 ks8851_net_stop(netdev);
1386 static int ks8851_resume(struct device *dev)
1388 struct ks8851_net *ks = dev_get_drvdata(dev);
1389 struct net_device *netdev = ks->netdev;
1391 if (netif_running(netdev)) {
1392 ks8851_net_open(netdev);
1393 netif_device_attach(netdev);
1400 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1402 static int ks8851_probe(struct spi_device *spi)
1404 struct net_device *ndev;
1405 struct ks8851_net *ks;
1410 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1414 spi->bits_per_word = 8;
1416 ks = netdev_priv(ndev);
1420 ks->tx_space = 6144;
1422 gpio = of_get_named_gpio_flags(spi->dev.of_node, "reset-gpios",
1424 if (gpio == -EPROBE_DEFER) {
1430 if (gpio_is_valid(gpio)) {
1431 ret = devm_gpio_request_one(&spi->dev, gpio,
1432 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1434 dev_err(&spi->dev, "reset gpio request failed\n");
1439 ks->vdd_io = devm_regulator_get(&spi->dev, "vdd-io");
1440 if (IS_ERR(ks->vdd_io)) {
1441 ret = PTR_ERR(ks->vdd_io);
1445 ret = regulator_enable(ks->vdd_io);
1447 dev_err(&spi->dev, "regulator vdd_io enable fail: %d\n",
1452 ks->vdd_reg = devm_regulator_get(&spi->dev, "vdd");
1453 if (IS_ERR(ks->vdd_reg)) {
1454 ret = PTR_ERR(ks->vdd_reg);
1458 ret = regulator_enable(ks->vdd_reg);
1460 dev_err(&spi->dev, "regulator vdd enable fail: %d\n",
1465 if (gpio_is_valid(gpio)) {
1466 usleep_range(10000, 11000);
1467 gpio_set_value(gpio, 1);
1470 mutex_init(&ks->lock);
1471 spin_lock_init(&ks->statelock);
1473 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1474 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1476 /* initialise pre-made spi transfer messages */
1478 spi_message_init(&ks->spi_msg1);
1479 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1481 spi_message_init(&ks->spi_msg2);
1482 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1483 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1485 /* setup EEPROM state */
1487 ks->eeprom.data = ks;
1488 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1489 ks->eeprom.register_read = ks8851_eeprom_regread;
1490 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1492 /* setup mii state */
1495 ks->mii.phy_id_mask = 1;
1496 ks->mii.reg_num_mask = 0xf;
1497 ks->mii.mdio_read = ks8851_phy_read;
1498 ks->mii.mdio_write = ks8851_phy_write;
1500 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1502 /* set the default message enable */
1503 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1507 skb_queue_head_init(&ks->txq);
1509 ndev->ethtool_ops = &ks8851_ethtool_ops;
1510 SET_NETDEV_DEV(ndev, &spi->dev);
1512 spi_set_drvdata(spi, ks);
1514 netif_carrier_off(ks->netdev);
1515 ndev->if_port = IF_PORT_100BASET;
1516 ndev->netdev_ops = &ks8851_netdev_ops;
1517 ndev->irq = spi->irq;
1519 /* issue a global soft reset to reset the device. */
1520 ks8851_soft_reset(ks, GRR_GSR);
1522 /* simple check for a valid chip being connected to the bus */
1523 cider = ks8851_rdreg16(ks, KS_CIDER);
1524 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1525 dev_err(&spi->dev, "failed to read device ID\n");
1530 /* cache the contents of the CCR register for EEPROM, etc. */
1531 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1533 ks8851_read_selftest(ks);
1534 ks8851_init_mac(ks);
1536 ret = register_netdev(ndev);
1538 dev_err(&spi->dev, "failed to register network device\n");
1542 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1543 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
1544 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1550 if (gpio_is_valid(gpio))
1551 gpio_set_value(gpio, 0);
1552 regulator_disable(ks->vdd_reg);
1554 regulator_disable(ks->vdd_io);
1561 static int ks8851_remove(struct spi_device *spi)
1563 struct ks8851_net *priv = spi_get_drvdata(spi);
1565 if (netif_msg_drv(priv))
1566 dev_info(&spi->dev, "remove\n");
1568 unregister_netdev(priv->netdev);
1569 if (gpio_is_valid(priv->gpio))
1570 gpio_set_value(priv->gpio, 0);
1571 regulator_disable(priv->vdd_reg);
1572 regulator_disable(priv->vdd_io);
1573 free_netdev(priv->netdev);
1578 static const struct of_device_id ks8851_match_table[] = {
1579 { .compatible = "micrel,ks8851" },
1582 MODULE_DEVICE_TABLE(of, ks8851_match_table);
1584 static struct spi_driver ks8851_driver = {
1587 .of_match_table = ks8851_match_table,
1588 .pm = &ks8851_pm_ops,
1590 .probe = ks8851_probe,
1591 .remove = ks8851_remove,
1593 module_spi_driver(ks8851_driver);
1595 MODULE_DESCRIPTION("KS8851 Network driver");
1596 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1597 MODULE_LICENSE("GPL");
1599 module_param_named(message, msg_enable, int, 0);
1600 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1601 MODULE_ALIAS("spi:ks8851");