1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/net/ethernet/micrel/ks8851.c
4 * Copyright 2009 Simtec Electronics
5 * http://www.simtec.co.uk/
6 * Ben Dooks <ben@simtec.co.uk>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/interrupt.h>
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>
22 #include <linux/eeprom_93cx6.h>
23 #include <linux/regulator/consumer.h>
25 #include <linux/spi/spi.h>
26 #include <linux/gpio.h>
27 #include <linux/of_gpio.h>
28 #include <linux/of_net.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: 93CX6 EEPROM state for accessing on-board EEPROM.
86 * @vdd_reg: Optional regulator supplying the chip
87 * @vdd_io: Optional digital power supply for IO
88 * @gpio: Optional reset_n gpio
90 * The @lock ensures that the chip is protected when certain operations are
91 * in progress. When the read or write packet transfer is in progress, most
92 * of the chip registers are not ccessible until the transfer is finished and
93 * the DMA has been de-asserted.
95 * The @statelock is used to protect information in the structure which may
96 * need to be accessed via several sources, such as the network driver layer
97 * or one of the work queues.
99 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
100 * wants to DMA map them, it will not have any problems with data the driver
104 struct net_device *netdev;
105 struct spi_device *spidev;
107 spinlock_t statelock;
109 union ks8851_tx_hdr txh ____cacheline_aligned;
113 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;
136 struct regulator *vdd_io;
140 static int msg_enable;
142 /* SPI frame opcodes */
143 #define KS_SPIOP_RD (0x00)
144 #define KS_SPIOP_WR (0x40)
145 #define KS_SPIOP_RXFIFO (0x80)
146 #define KS_SPIOP_TXFIFO (0xC0)
148 /* shift for byte-enable data */
149 #define BYTE_EN(_x) ((_x) << 2)
151 /* turn register number and byte-enable mask into data for start of packet */
152 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
154 /* SPI register read/write calls.
156 * All these calls issue SPI transactions to access the chip's registers. They
157 * all require that the necessary lock is held to prevent accesses when the
158 * chip is busy transferring packet data (RX/TX FIFO accesses).
162 * ks8851_wrreg16 - write 16bit register value to chip
163 * @ks: The chip state
164 * @reg: The register address
165 * @val: The value to write
167 * Issue a write to put the value @val into the register specified in @reg.
169 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
171 struct spi_transfer *xfer = &ks->spi_xfer1;
172 struct spi_message *msg = &ks->spi_msg1;
176 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
177 txb[1] = cpu_to_le16(val);
183 ret = spi_sync(ks->spidev, msg);
185 netdev_err(ks->netdev, "spi_sync() failed\n");
189 * ks8851_wrreg8 - write 8bit register value to chip
190 * @ks: The chip state
191 * @reg: The register address
192 * @val: The value to write
194 * Issue a write to put the value @val into the register specified in @reg.
196 static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
198 struct spi_transfer *xfer = &ks->spi_xfer1;
199 struct spi_message *msg = &ks->spi_msg1;
204 bit = 1 << (reg & 3);
206 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
213 ret = spi_sync(ks->spidev, msg);
215 netdev_err(ks->netdev, "spi_sync() failed\n");
219 * ks8851_rdreg - issue read register command and return the data
220 * @ks: The device state
221 * @op: The register address and byte enables in message format.
222 * @rxb: The RX buffer to return the result into
223 * @rxl: The length of data expected.
225 * This is the low level read call that issues the necessary spi message(s)
226 * to read data from the register specified in @op.
228 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
229 u8 *rxb, unsigned rxl)
231 struct spi_transfer *xfer;
232 struct spi_message *msg;
233 __le16 *txb = (__le16 *)ks->txd;
237 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
239 if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
241 xfer = ks->spi_xfer2;
253 xfer = &ks->spi_xfer1;
260 ret = spi_sync(ks->spidev, msg);
262 netdev_err(ks->netdev, "read: spi_sync() failed\n");
263 else if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
264 memcpy(rxb, trx, rxl);
266 memcpy(rxb, trx + 2, rxl);
270 * ks8851_rdreg8 - read 8 bit register from device
271 * @ks: The chip information
272 * @reg: The register address
274 * Read a 8bit register from the chip, returning the result
276 static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
280 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
285 * ks8851_rdreg16 - read 16 bit register from device
286 * @ks: The chip information
287 * @reg: The register address
289 * Read a 16bit register from the chip, returning the result
291 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
295 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
296 return le16_to_cpu(rx);
300 * ks8851_rdreg32 - read 32 bit register from device
301 * @ks: The chip information
302 * @reg: The register address
304 * Read a 32bit register from the chip.
306 * Note, this read requires the address be aligned to 4 bytes.
308 static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
314 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
315 return le32_to_cpu(rx);
319 * ks8851_soft_reset - issue one of the soft reset to the device
320 * @ks: The device state.
321 * @op: The bit(s) to set in the GRR
323 * Issue the relevant soft-reset command to the device's GRR register
326 * Note, the delays are in there as a caution to ensure that the reset
327 * has time to take effect and then complete. Since the datasheet does
328 * not currently specify the exact sequence, we have chosen something
329 * that seems to work with our device.
331 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
333 ks8851_wrreg16(ks, KS_GRR, op);
334 mdelay(1); /* wait a short time to effect reset */
335 ks8851_wrreg16(ks, KS_GRR, 0);
336 mdelay(1); /* wait for condition to clear */
340 * ks8851_set_powermode - set power mode of the device
341 * @ks: The device state
342 * @pwrmode: The power mode value to write to KS_PMECR.
344 * Change the power mode of the chip.
346 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
350 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
352 pmecr = ks8851_rdreg16(ks, KS_PMECR);
353 pmecr &= ~PMECR_PM_MASK;
356 ks8851_wrreg16(ks, KS_PMECR, pmecr);
360 * ks8851_write_mac_addr - write mac address to device registers
361 * @dev: The network device
363 * Update the KS8851 MAC address registers from the address in @dev.
365 * This call assumes that the chip is not running, so there is no need to
366 * shutdown the RXQ process whilst setting this.
368 static int ks8851_write_mac_addr(struct net_device *dev)
370 struct ks8851_net *ks = netdev_priv(dev);
373 mutex_lock(&ks->lock);
376 * Wake up chip in case it was powered off when stopped; otherwise,
377 * the first write to the MAC address does not take effect.
379 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
380 for (i = 0; i < ETH_ALEN; i++)
381 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
382 if (!netif_running(dev))
383 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
385 mutex_unlock(&ks->lock);
391 * ks8851_read_mac_addr - read mac address from device registers
392 * @dev: The network device
394 * Update our copy of the KS8851 MAC address from the registers of @dev.
396 static void ks8851_read_mac_addr(struct net_device *dev)
398 struct ks8851_net *ks = netdev_priv(dev);
401 mutex_lock(&ks->lock);
403 for (i = 0; i < ETH_ALEN; i++)
404 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
406 mutex_unlock(&ks->lock);
410 * ks8851_init_mac - initialise the mac address
411 * @ks: The device structure
412 * @np: The device node pointer
414 * Get or create the initial mac address for the device and then set that
415 * into the station address register. A mac address supplied in the device
416 * tree takes precedence. Otherwise, if there is an EEPROM present, then
417 * we try that. If no valid mac address is found we use eth_random_addr()
418 * to create a new one.
420 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
422 struct net_device *dev = ks->netdev;
425 mac_addr = of_get_mac_address(np);
426 if (!IS_ERR(mac_addr)) {
427 ether_addr_copy(dev->dev_addr, mac_addr);
428 ks8851_write_mac_addr(dev);
432 if (ks->rc_ccr & CCR_EEPROM) {
433 ks8851_read_mac_addr(dev);
434 if (is_valid_ether_addr(dev->dev_addr))
437 netdev_err(ks->netdev, "invalid mac address read %pM\n",
441 eth_hw_addr_random(dev);
442 ks8851_write_mac_addr(dev);
446 * ks8851_rdfifo - read data from the receive fifo
447 * @ks: The device state.
448 * @buff: The buffer address
449 * @len: The length of the data to read
451 * Issue an RXQ FIFO read command and read the @len amount of data from
452 * the FIFO into the buffer specified by @buff.
454 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
456 struct spi_transfer *xfer = ks->spi_xfer2;
457 struct spi_message *msg = &ks->spi_msg2;
461 netif_dbg(ks, rx_status, ks->netdev,
462 "%s: %d@%p\n", __func__, len, buff);
464 /* set the operation we're issuing */
465 txb[0] = KS_SPIOP_RXFIFO;
476 ret = spi_sync(ks->spidev, msg);
478 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
482 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
483 * @ks: The device state
484 * @rxpkt: The data for the received packet
486 * Dump the initial data from the packet to dev_dbg().
488 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
490 netdev_dbg(ks->netdev,
491 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
492 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
493 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
494 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
498 * ks8851_rx_pkts - receive packets from the host
499 * @ks: The device information.
501 * This is called from the IRQ work queue when the system detects that there
502 * are packets in the receive queue. Find out how many packets there are and
503 * read them from the FIFO.
505 static void ks8851_rx_pkts(struct ks8851_net *ks)
514 rxfc = ks8851_rdreg8(ks, KS_RXFC);
516 netif_dbg(ks, rx_status, ks->netdev,
517 "%s: %d packets\n", __func__, rxfc);
519 /* Currently we're issuing a read per packet, but we could possibly
520 * improve the code by issuing a single read, getting the receive
521 * header, allocating the packet and then reading the packet data
524 * This form of operation would require us to hold the SPI bus'
525 * chipselect low during the entie transaction to avoid any
526 * reset to the data stream coming from the chip.
529 for (; rxfc != 0; rxfc--) {
530 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
531 rxstat = rxh & 0xffff;
532 rxlen = (rxh >> 16) & 0xfff;
534 netif_dbg(ks, rx_status, ks->netdev,
535 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
537 /* the length of the packet includes the 32bit CRC */
539 /* set dma read address */
540 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
542 /* start DMA access */
543 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
546 unsigned int rxalign;
549 rxalign = ALIGN(rxlen, 4);
550 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
553 /* 4 bytes of status header + 4 bytes of
554 * garbage: we put them before ethernet
555 * header, so that they are copied,
559 rxpkt = skb_put(skb, rxlen) - 8;
561 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
563 if (netif_msg_pktdata(ks))
564 ks8851_dbg_dumpkkt(ks, rxpkt);
566 skb->protocol = eth_type_trans(skb, ks->netdev);
569 ks->netdev->stats.rx_packets++;
570 ks->netdev->stats.rx_bytes += rxlen;
574 /* end DMA access and dequeue packet */
575 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
580 * ks8851_irq - IRQ handler for dealing with interrupt requests
584 * This handler is invoked when the IRQ line asserts to find out what happened.
585 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
588 * Read the interrupt status, work out what needs to be done and then clear
589 * any of the interrupts that are not needed.
591 static irqreturn_t ks8851_irq(int irq, void *_ks)
593 struct ks8851_net *ks = _ks;
595 unsigned handled = 0;
597 mutex_lock(&ks->lock);
599 status = ks8851_rdreg16(ks, KS_ISR);
601 netif_dbg(ks, intr, ks->netdev,
602 "%s: status 0x%04x\n", __func__, status);
604 if (status & IRQ_LCI)
607 if (status & IRQ_LDI) {
608 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
609 pmecr &= ~PMECR_WKEVT_MASK;
610 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
615 if (status & IRQ_RXPSI)
616 handled |= IRQ_RXPSI;
618 if (status & IRQ_TXI) {
621 /* no lock here, tx queue should have been stopped */
623 /* update our idea of how much tx space is available to the
625 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
627 netif_dbg(ks, intr, ks->netdev,
628 "%s: txspace %d\n", __func__, ks->tx_space);
631 if (status & IRQ_RXI)
634 if (status & IRQ_SPIBEI) {
635 netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
636 handled |= IRQ_SPIBEI;
639 ks8851_wrreg16(ks, KS_ISR, handled);
641 if (status & IRQ_RXI) {
642 /* the datasheet says to disable the rx interrupt during
643 * packet read-out, however we're masking the interrupt
644 * from the device so do not bother masking just the RX
645 * from the device. */
650 /* if something stopped the rx process, probably due to wanting
651 * to change the rx settings, then do something about restarting
653 if (status & IRQ_RXPSI) {
654 struct ks8851_rxctrl *rxc = &ks->rxctrl;
656 /* update the multicast hash table */
657 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
658 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
659 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
660 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
662 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
663 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
666 mutex_unlock(&ks->lock);
668 if (status & IRQ_LCI)
669 mii_check_link(&ks->mii);
671 if (status & IRQ_TXI)
672 netif_wake_queue(ks->netdev);
678 * calc_txlen - calculate size of message to send packet
679 * @len: Length of data
681 * Returns the size of the TXFIFO message needed to send
684 static inline unsigned calc_txlen(unsigned len)
686 return ALIGN(len + 4, 4);
690 * ks8851_wrpkt - write packet to TX FIFO
691 * @ks: The device state.
692 * @txp: The sk_buff to transmit.
693 * @irq: IRQ on completion of the packet.
695 * Send the @txp to the chip. This means creating the relevant packet header
696 * specifying the length of the packet and the other information the chip
697 * needs, such as IRQ on completion. Send the header and the packet data to
700 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
702 struct spi_transfer *xfer = ks->spi_xfer2;
703 struct spi_message *msg = &ks->spi_msg2;
707 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
708 __func__, txp, txp->len, txp->data, irq);
711 fid &= TXFR_TXFID_MASK;
714 fid |= TXFR_TXIC; /* irq on completion */
716 /* start header at txb[1] to align txw entries */
717 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
718 ks->txh.txw[1] = cpu_to_le16(fid);
719 ks->txh.txw[2] = cpu_to_le16(txp->len);
721 xfer->tx_buf = &ks->txh.txb[1];
726 xfer->tx_buf = txp->data;
728 xfer->len = ALIGN(txp->len, 4);
730 ret = spi_sync(ks->spidev, msg);
732 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
736 * ks8851_done_tx - update and then free skbuff after transmitting
737 * @ks: The device state
738 * @txb: The buffer transmitted
740 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
742 struct net_device *dev = ks->netdev;
744 dev->stats.tx_bytes += txb->len;
745 dev->stats.tx_packets++;
751 * ks8851_tx_work - process tx packet(s)
752 * @work: The work strucutre what was scheduled.
754 * This is called when a number of packets have been scheduled for
755 * transmission and need to be sent to the device.
757 static void ks8851_tx_work(struct work_struct *work)
759 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
761 bool last = skb_queue_empty(&ks->txq);
763 mutex_lock(&ks->lock);
766 txb = skb_dequeue(&ks->txq);
767 last = skb_queue_empty(&ks->txq);
770 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
771 ks8851_wrpkt(ks, txb, last);
772 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
773 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
775 ks8851_done_tx(ks, txb);
779 mutex_unlock(&ks->lock);
783 * ks8851_net_open - open network device
784 * @dev: The network device being opened.
786 * Called when the network device is marked active, such as a user executing
787 * 'ifconfig up' on the device.
789 static int ks8851_net_open(struct net_device *dev)
791 struct ks8851_net *ks = netdev_priv(dev);
794 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
795 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
798 netdev_err(dev, "failed to get irq\n");
802 /* lock the card, even if we may not actually be doing anything
803 * else at the moment */
804 mutex_lock(&ks->lock);
806 netif_dbg(ks, ifup, ks->netdev, "opening\n");
808 /* bring chip out of any power saving mode it was in */
809 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
811 /* issue a soft reset to the RX/TX QMU to put it into a known
813 ks8851_soft_reset(ks, GRR_QMU);
815 /* setup transmission parameters */
817 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
818 TXCR_TXPE | /* pad to min length */
819 TXCR_TXCRC | /* add CRC */
820 TXCR_TXFCE)); /* enable flow control */
822 /* auto-increment tx data, reset tx pointer */
823 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
825 /* setup receiver control */
827 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
828 RXCR1_RXFCE | /* enable flow control */
829 RXCR1_RXBE | /* broadcast enable */
830 RXCR1_RXUE | /* unicast enable */
831 RXCR1_RXE)); /* enable rx block */
833 /* transfer entire frames out in one go */
834 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
836 /* set receive counter timeouts */
837 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
838 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
839 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
841 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
842 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
843 RXQCR_RXDTTE); /* IRQ on time exceeded */
845 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
847 /* clear then enable interrupts */
849 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
850 IRQ_TXI | /* TX done */ \
851 IRQ_RXI | /* RX done */ \
852 IRQ_SPIBEI | /* SPI bus error */ \
853 IRQ_TXPSI | /* TX process stop */ \
854 IRQ_RXPSI) /* RX process stop */
856 ks->rc_ier = STD_IRQ;
857 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
858 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
860 netif_start_queue(ks->netdev);
862 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
864 mutex_unlock(&ks->lock);
865 mii_check_link(&ks->mii);
870 * ks8851_net_stop - close network device
871 * @dev: The device being closed.
873 * Called to close down a network device which has been active. Cancell any
874 * work, shutdown the RX and TX process and then place the chip into a low
875 * power state whilst it is not being used.
877 static int ks8851_net_stop(struct net_device *dev)
879 struct ks8851_net *ks = netdev_priv(dev);
881 netif_info(ks, ifdown, dev, "shutting down\n");
883 netif_stop_queue(dev);
885 mutex_lock(&ks->lock);
886 /* turn off the IRQs and ack any outstanding */
887 ks8851_wrreg16(ks, KS_IER, 0x0000);
888 ks8851_wrreg16(ks, KS_ISR, 0xffff);
889 mutex_unlock(&ks->lock);
891 /* stop any outstanding work */
892 flush_work(&ks->tx_work);
893 flush_work(&ks->rxctrl_work);
895 mutex_lock(&ks->lock);
896 /* shutdown RX process */
897 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
899 /* shutdown TX process */
900 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
902 /* set powermode to soft power down to save power */
903 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
904 mutex_unlock(&ks->lock);
906 /* ensure any queued tx buffers are dumped */
907 while (!skb_queue_empty(&ks->txq)) {
908 struct sk_buff *txb = skb_dequeue(&ks->txq);
910 netif_dbg(ks, ifdown, ks->netdev,
911 "%s: freeing txb %p\n", __func__, txb);
916 free_irq(dev->irq, ks);
922 * ks8851_start_xmit - transmit packet
923 * @skb: The buffer to transmit
924 * @dev: The device used to transmit the packet.
926 * Called by the network layer to transmit the @skb. Queue the packet for
927 * the device and schedule the necessary work to transmit the packet when
930 * We do this to firstly avoid sleeping with the network device locked,
931 * and secondly so we can round up more than one packet to transmit which
932 * means we can try and avoid generating too many transmit done interrupts.
934 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
935 struct net_device *dev)
937 struct ks8851_net *ks = netdev_priv(dev);
938 unsigned needed = calc_txlen(skb->len);
939 netdev_tx_t ret = NETDEV_TX_OK;
941 netif_dbg(ks, tx_queued, ks->netdev,
942 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
944 spin_lock(&ks->statelock);
946 if (needed > ks->tx_space) {
947 netif_stop_queue(dev);
948 ret = NETDEV_TX_BUSY;
950 ks->tx_space -= needed;
951 skb_queue_tail(&ks->txq, skb);
954 spin_unlock(&ks->statelock);
955 schedule_work(&ks->tx_work);
961 * ks8851_rxctrl_work - work handler to change rx mode
962 * @work: The work structure this belongs to.
964 * Lock the device and issue the necessary changes to the receive mode from
965 * the network device layer. This is done so that we can do this without
966 * having to sleep whilst holding the network device lock.
968 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
969 * receive parameters are programmed, we issue a write to disable the RXQ and
970 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
971 * complete. The interrupt handler then writes the new values into the chip.
973 static void ks8851_rxctrl_work(struct work_struct *work)
975 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
977 mutex_lock(&ks->lock);
979 /* need to shutdown RXQ before modifying filter parameters */
980 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
982 mutex_unlock(&ks->lock);
985 static void ks8851_set_rx_mode(struct net_device *dev)
987 struct ks8851_net *ks = netdev_priv(dev);
988 struct ks8851_rxctrl rxctrl;
990 memset(&rxctrl, 0, sizeof(rxctrl));
992 if (dev->flags & IFF_PROMISC) {
993 /* interface to receive everything */
995 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
996 } else if (dev->flags & IFF_ALLMULTI) {
997 /* accept all multicast packets */
999 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1000 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
1001 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
1002 struct netdev_hw_addr *ha;
1005 /* accept some multicast */
1007 netdev_for_each_mc_addr(ha, dev) {
1008 crc = ether_crc(ETH_ALEN, ha->addr);
1009 crc >>= (32 - 6); /* get top six bits */
1011 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1014 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1016 /* just accept broadcast / unicast */
1017 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1020 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1021 RXCR1_RXBE | /* broadcast enable */
1022 RXCR1_RXE | /* RX process enable */
1023 RXCR1_RXFCE); /* enable flow control */
1025 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1027 /* schedule work to do the actual set of the data if needed */
1029 spin_lock(&ks->statelock);
1031 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1032 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1033 schedule_work(&ks->rxctrl_work);
1036 spin_unlock(&ks->statelock);
1039 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1041 struct sockaddr *sa = addr;
1043 if (netif_running(dev))
1046 if (!is_valid_ether_addr(sa->sa_data))
1047 return -EADDRNOTAVAIL;
1049 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1050 return ks8851_write_mac_addr(dev);
1053 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1055 struct ks8851_net *ks = netdev_priv(dev);
1057 if (!netif_running(dev))
1060 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1063 static const struct net_device_ops ks8851_netdev_ops = {
1064 .ndo_open = ks8851_net_open,
1065 .ndo_stop = ks8851_net_stop,
1066 .ndo_do_ioctl = ks8851_net_ioctl,
1067 .ndo_start_xmit = ks8851_start_xmit,
1068 .ndo_set_mac_address = ks8851_set_mac_address,
1069 .ndo_set_rx_mode = ks8851_set_rx_mode,
1070 .ndo_validate_addr = eth_validate_addr,
1073 /* ethtool support */
1075 static void ks8851_get_drvinfo(struct net_device *dev,
1076 struct ethtool_drvinfo *di)
1078 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1079 strlcpy(di->version, "1.00", sizeof(di->version));
1080 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1083 static u32 ks8851_get_msglevel(struct net_device *dev)
1085 struct ks8851_net *ks = netdev_priv(dev);
1086 return ks->msg_enable;
1089 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1091 struct ks8851_net *ks = netdev_priv(dev);
1092 ks->msg_enable = to;
1095 static int ks8851_get_link_ksettings(struct net_device *dev,
1096 struct ethtool_link_ksettings *cmd)
1098 struct ks8851_net *ks = netdev_priv(dev);
1100 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1105 static int ks8851_set_link_ksettings(struct net_device *dev,
1106 const struct ethtool_link_ksettings *cmd)
1108 struct ks8851_net *ks = netdev_priv(dev);
1109 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1112 static u32 ks8851_get_link(struct net_device *dev)
1114 struct ks8851_net *ks = netdev_priv(dev);
1115 return mii_link_ok(&ks->mii);
1118 static int ks8851_nway_reset(struct net_device *dev)
1120 struct ks8851_net *ks = netdev_priv(dev);
1121 return mii_nway_restart(&ks->mii);
1124 /* EEPROM support */
1126 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1128 struct ks8851_net *ks = ee->data;
1131 val = ks8851_rdreg16(ks, KS_EEPCR);
1133 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1134 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1135 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1138 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1140 struct ks8851_net *ks = ee->data;
1141 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1144 val |= EEPCR_EESRWA;
1145 if (ee->reg_data_in)
1147 if (ee->reg_data_clock)
1149 if (ee->reg_chip_select)
1152 ks8851_wrreg16(ks, KS_EEPCR, val);
1156 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1157 * @ks: The network device state.
1159 * Check for the presence of an EEPROM, and then activate software access
1162 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1164 if (!(ks->rc_ccr & CCR_EEPROM))
1167 mutex_lock(&ks->lock);
1169 /* start with clock low, cs high */
1170 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1175 * ks8851_eeprom_release - release the EEPROM interface
1176 * @ks: The device state
1178 * Release the software access to the device EEPROM
1180 static void ks8851_eeprom_release(struct ks8851_net *ks)
1182 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1184 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1185 mutex_unlock(&ks->lock);
1188 #define KS_EEPROM_MAGIC (0x00008851)
1190 static int ks8851_set_eeprom(struct net_device *dev,
1191 struct ethtool_eeprom *ee, u8 *data)
1193 struct ks8851_net *ks = netdev_priv(dev);
1194 int offset = ee->offset;
1198 /* currently only support byte writing */
1202 if (ee->magic != KS_EEPROM_MAGIC)
1205 if (ks8851_eeprom_claim(ks))
1208 eeprom_93cx6_wren(&ks->eeprom, true);
1210 /* ethtool currently only supports writing bytes, which means
1211 * we have to read/modify/write our 16bit EEPROMs */
1213 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1223 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1224 eeprom_93cx6_wren(&ks->eeprom, false);
1226 ks8851_eeprom_release(ks);
1231 static int ks8851_get_eeprom(struct net_device *dev,
1232 struct ethtool_eeprom *ee, u8 *data)
1234 struct ks8851_net *ks = netdev_priv(dev);
1235 int offset = ee->offset;
1238 /* must be 2 byte aligned */
1239 if (len & 1 || offset & 1)
1242 if (ks8851_eeprom_claim(ks))
1245 ee->magic = KS_EEPROM_MAGIC;
1247 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1248 ks8851_eeprom_release(ks);
1253 static int ks8851_get_eeprom_len(struct net_device *dev)
1255 struct ks8851_net *ks = netdev_priv(dev);
1257 /* currently, we assume it is an 93C46 attached, so return 128 */
1258 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1261 static const struct ethtool_ops ks8851_ethtool_ops = {
1262 .get_drvinfo = ks8851_get_drvinfo,
1263 .get_msglevel = ks8851_get_msglevel,
1264 .set_msglevel = ks8851_set_msglevel,
1265 .get_link = ks8851_get_link,
1266 .nway_reset = ks8851_nway_reset,
1267 .get_eeprom_len = ks8851_get_eeprom_len,
1268 .get_eeprom = ks8851_get_eeprom,
1269 .set_eeprom = ks8851_set_eeprom,
1270 .get_link_ksettings = ks8851_get_link_ksettings,
1271 .set_link_ksettings = ks8851_set_link_ksettings,
1274 /* MII interface controls */
1277 * ks8851_phy_reg - convert MII register into a KS8851 register
1278 * @reg: MII register number.
1280 * Return the KS8851 register number for the corresponding MII PHY register
1281 * if possible. Return zero if the MII register has no direct mapping to the
1282 * KS8851 register set.
1284 static int ks8851_phy_reg(int reg)
1305 * ks8851_phy_read - MII interface PHY register read.
1306 * @dev: The network device the PHY is on.
1307 * @phy_addr: Address of PHY (ignored as we only have one)
1308 * @reg: The register to read.
1310 * This call reads data from the PHY register specified in @reg. Since the
1311 * device does not support all the MII registers, the non-existent values
1312 * are always returned as zero.
1314 * We return zero for unsupported registers as the MII code does not check
1315 * the value returned for any error status, and simply returns it to the
1316 * caller. The mii-tool that the driver was tested with takes any -ve error
1317 * as real PHY capabilities, thus displaying incorrect data to the user.
1319 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1321 struct ks8851_net *ks = netdev_priv(dev);
1325 ksreg = ks8851_phy_reg(reg);
1327 return 0x0; /* no error return allowed, so use zero */
1329 mutex_lock(&ks->lock);
1330 result = ks8851_rdreg16(ks, ksreg);
1331 mutex_unlock(&ks->lock);
1336 static void ks8851_phy_write(struct net_device *dev,
1337 int phy, int reg, int value)
1339 struct ks8851_net *ks = netdev_priv(dev);
1342 ksreg = ks8851_phy_reg(reg);
1344 mutex_lock(&ks->lock);
1345 ks8851_wrreg16(ks, ksreg, value);
1346 mutex_unlock(&ks->lock);
1351 * ks8851_read_selftest - read the selftest memory info.
1352 * @ks: The device state
1354 * Read and check the TX/RX memory selftest information.
1356 static int ks8851_read_selftest(struct ks8851_net *ks)
1358 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1362 rd = ks8851_rdreg16(ks, KS_MBIR);
1364 if ((rd & both_done) != both_done) {
1365 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1369 if (rd & MBIR_TXMBFA) {
1370 netdev_err(ks->netdev, "TX memory selftest fail\n");
1374 if (rd & MBIR_RXMBFA) {
1375 netdev_err(ks->netdev, "RX memory selftest fail\n");
1382 /* driver bus management functions */
1384 #ifdef CONFIG_PM_SLEEP
1386 static int ks8851_suspend(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 netif_device_detach(netdev);
1393 ks8851_net_stop(netdev);
1399 static int ks8851_resume(struct device *dev)
1401 struct ks8851_net *ks = dev_get_drvdata(dev);
1402 struct net_device *netdev = ks->netdev;
1404 if (netif_running(netdev)) {
1405 ks8851_net_open(netdev);
1406 netif_device_attach(netdev);
1413 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1415 static int ks8851_probe(struct spi_device *spi)
1417 struct device *dev = &spi->dev;
1418 struct net_device *netdev;
1419 struct ks8851_net *ks;
1424 netdev = alloc_etherdev(sizeof(struct ks8851_net));
1428 spi->bits_per_word = 8;
1430 ks = netdev_priv(netdev);
1432 ks->netdev = netdev;
1434 ks->tx_space = 6144;
1436 gpio = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0, NULL);
1437 if (gpio == -EPROBE_DEFER) {
1443 if (gpio_is_valid(gpio)) {
1444 ret = devm_gpio_request_one(dev, gpio,
1445 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1447 dev_err(dev, "reset gpio request failed\n");
1452 ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1453 if (IS_ERR(ks->vdd_io)) {
1454 ret = PTR_ERR(ks->vdd_io);
1458 ret = regulator_enable(ks->vdd_io);
1460 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1464 ks->vdd_reg = devm_regulator_get(dev, "vdd");
1465 if (IS_ERR(ks->vdd_reg)) {
1466 ret = PTR_ERR(ks->vdd_reg);
1470 ret = regulator_enable(ks->vdd_reg);
1472 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1476 if (gpio_is_valid(gpio)) {
1477 usleep_range(10000, 11000);
1478 gpio_set_value(gpio, 1);
1481 mutex_init(&ks->lock);
1482 spin_lock_init(&ks->statelock);
1484 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1485 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1487 /* initialise pre-made spi transfer messages */
1489 spi_message_init(&ks->spi_msg1);
1490 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1492 spi_message_init(&ks->spi_msg2);
1493 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1494 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1496 /* setup EEPROM state */
1498 ks->eeprom.data = ks;
1499 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1500 ks->eeprom.register_read = ks8851_eeprom_regread;
1501 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1503 /* setup mii state */
1504 ks->mii.dev = netdev;
1506 ks->mii.phy_id_mask = 1;
1507 ks->mii.reg_num_mask = 0xf;
1508 ks->mii.mdio_read = ks8851_phy_read;
1509 ks->mii.mdio_write = ks8851_phy_write;
1511 dev_info(dev, "message enable is %d\n", msg_enable);
1513 /* set the default message enable */
1514 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1518 skb_queue_head_init(&ks->txq);
1520 netdev->ethtool_ops = &ks8851_ethtool_ops;
1521 SET_NETDEV_DEV(netdev, dev);
1523 spi_set_drvdata(spi, ks);
1525 netif_carrier_off(ks->netdev);
1526 netdev->if_port = IF_PORT_100BASET;
1527 netdev->netdev_ops = &ks8851_netdev_ops;
1528 netdev->irq = spi->irq;
1530 /* issue a global soft reset to reset the device. */
1531 ks8851_soft_reset(ks, GRR_GSR);
1533 /* simple check for a valid chip being connected to the bus */
1534 cider = ks8851_rdreg16(ks, KS_CIDER);
1535 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1536 dev_err(dev, "failed to read device ID\n");
1541 /* cache the contents of the CCR register for EEPROM, etc. */
1542 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1544 ks8851_read_selftest(ks);
1545 ks8851_init_mac(ks, dev->of_node);
1547 ret = register_netdev(netdev);
1549 dev_err(dev, "failed to register network device\n");
1553 netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1554 CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1555 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1561 if (gpio_is_valid(gpio))
1562 gpio_set_value(gpio, 0);
1563 regulator_disable(ks->vdd_reg);
1565 regulator_disable(ks->vdd_io);
1568 free_netdev(netdev);
1572 static int ks8851_remove(struct spi_device *spi)
1574 struct ks8851_net *priv = spi_get_drvdata(spi);
1575 struct device *dev = &spi->dev;
1577 if (netif_msg_drv(priv))
1578 dev_info(dev, "remove\n");
1580 unregister_netdev(priv->netdev);
1581 if (gpio_is_valid(priv->gpio))
1582 gpio_set_value(priv->gpio, 0);
1583 regulator_disable(priv->vdd_reg);
1584 regulator_disable(priv->vdd_io);
1585 free_netdev(priv->netdev);
1590 static const struct of_device_id ks8851_match_table[] = {
1591 { .compatible = "micrel,ks8851" },
1594 MODULE_DEVICE_TABLE(of, ks8851_match_table);
1596 static struct spi_driver ks8851_driver = {
1599 .of_match_table = ks8851_match_table,
1600 .pm = &ks8851_pm_ops,
1602 .probe = ks8851_probe,
1603 .remove = ks8851_remove,
1605 module_spi_driver(ks8851_driver);
1607 MODULE_DESCRIPTION("KS8851 Network driver");
1608 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1609 MODULE_LICENSE("GPL");
1611 module_param_named(message, msg_enable, int, 0);
1612 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1613 MODULE_ALIAS("spi:ks8851");