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 * @statelock: Lock on this structure for tx list.
68 * @mii: The MII state information for the mii calls.
69 * @rxctrl: RX settings for @rxctrl_work.
70 * @rxctrl_work: Work queue for updating RX mode and multicast lists
71 * @txq: Queue of packets for transmission.
72 * @txh: Space for generating packet TX header in DMA-able data
73 * @rxd: Space for receiving SPI data, in DMA-able space.
74 * @txd: Space for transmitting SPI data, in DMA-able space.
75 * @msg_enable: The message flags controlling driver output (see ethtool).
76 * @fid: Incrementing frame id tag.
77 * @rc_ier: Cached copy of KS_IER.
78 * @rc_ccr: Cached copy of KS_CCR.
79 * @rc_rxqcr: Cached copy of KS_RXQCR.
80 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
81 * @vdd_reg: Optional regulator supplying the chip
82 * @vdd_io: Optional digital power supply for IO
83 * @gpio: Optional reset_n gpio
85 * The @statelock is used to protect information in the structure which may
86 * need to be accessed via several sources, such as the network driver layer
87 * or one of the work queues.
89 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
90 * wants to DMA map them, it will not have any problems with data the driver
94 struct net_device *netdev;
97 union ks8851_tx_hdr txh ____cacheline_aligned;
101 u32 msg_enable ____cacheline_aligned;
109 struct mii_if_info mii;
110 struct ks8851_rxctrl rxctrl;
112 struct work_struct rxctrl_work;
114 struct sk_buff_head txq;
116 struct eeprom_93cx6 eeprom;
117 struct regulator *vdd_reg;
118 struct regulator *vdd_io;
123 * struct ks8851_net_spi - KS8851 SPI driver private data
124 * @ks8851: KS8851 driver common private data
125 * @lock: Lock to ensure that the device is not accessed when busy.
126 * @tx_work: Work queue for tx packets
127 * @spidev: The spi device we're bound to.
128 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
129 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
131 * The @lock ensures that the chip is protected when certain operations are
132 * in progress. When the read or write packet transfer is in progress, most
133 * of the chip registers are not ccessible until the transfer is finished and
134 * the DMA has been de-asserted.
136 struct ks8851_net_spi {
137 struct ks8851_net ks8851;
139 struct work_struct tx_work;
140 struct spi_device *spidev;
141 struct spi_message spi_msg1;
142 struct spi_message spi_msg2;
143 struct spi_transfer spi_xfer1;
144 struct spi_transfer spi_xfer2[2];
147 #define to_ks8851_spi(ks) container_of((ks), struct ks8851_net_spi, ks8851)
149 static int msg_enable;
151 /* SPI frame opcodes */
152 #define KS_SPIOP_RD (0x00)
153 #define KS_SPIOP_WR (0x40)
154 #define KS_SPIOP_RXFIFO (0x80)
155 #define KS_SPIOP_TXFIFO (0xC0)
157 /* shift for byte-enable data */
158 #define BYTE_EN(_x) ((_x) << 2)
160 /* turn register number and byte-enable mask into data for start of packet */
161 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
164 * ks8851_lock - register access lock
165 * @ks: The chip state
166 * @flags: Spinlock flags
168 * Claim chip register access lock
170 static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
172 struct ks8851_net_spi *kss = to_ks8851_spi(ks);
174 mutex_lock(&kss->lock);
178 * ks8851_unlock - register access unlock
179 * @ks: The chip state
180 * @flags: Spinlock flags
182 * Release chip register access lock
184 static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
186 struct ks8851_net_spi *kss = to_ks8851_spi(ks);
188 mutex_unlock(&kss->lock);
191 /* SPI register read/write calls.
193 * All these calls issue SPI transactions to access the chip's registers. They
194 * all require that the necessary lock is held to prevent accesses when the
195 * chip is busy transferring packet data (RX/TX FIFO accesses).
199 * ks8851_wrreg16 - write 16bit register value to chip
200 * @ks: The chip state
201 * @reg: The register address
202 * @val: The value to write
204 * Issue a write to put the value @val into the register specified in @reg.
206 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
208 struct ks8851_net_spi *kss = to_ks8851_spi(ks);
209 struct spi_transfer *xfer = &kss->spi_xfer1;
210 struct spi_message *msg = &kss->spi_msg1;
214 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
215 txb[1] = cpu_to_le16(val);
221 ret = spi_sync(kss->spidev, msg);
223 netdev_err(ks->netdev, "spi_sync() failed\n");
227 * ks8851_rdreg - issue read register command and return the data
228 * @ks: The device state
229 * @op: The register address and byte enables in message format.
230 * @rxb: The RX buffer to return the result into
231 * @rxl: The length of data expected.
233 * This is the low level read call that issues the necessary spi message(s)
234 * to read data from the register specified in @op.
236 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
237 u8 *rxb, unsigned rxl)
239 struct ks8851_net_spi *kss = to_ks8851_spi(ks);
240 struct spi_transfer *xfer;
241 struct spi_message *msg;
242 __le16 *txb = (__le16 *)ks->txd;
246 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
248 if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
249 msg = &kss->spi_msg2;
250 xfer = kss->spi_xfer2;
261 msg = &kss->spi_msg1;
262 xfer = &kss->spi_xfer1;
269 ret = spi_sync(kss->spidev, msg);
271 netdev_err(ks->netdev, "read: spi_sync() failed\n");
272 else if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
273 memcpy(rxb, trx, rxl);
275 memcpy(rxb, trx + 2, rxl);
279 * ks8851_rdreg16 - read 16 bit register from device
280 * @ks: The chip information
281 * @reg: The register address
283 * Read a 16bit register from the chip, returning the result
285 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
289 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
290 return le16_to_cpu(rx);
294 * ks8851_soft_reset - issue one of the soft reset to the device
295 * @ks: The device state.
296 * @op: The bit(s) to set in the GRR
298 * Issue the relevant soft-reset command to the device's GRR register
301 * Note, the delays are in there as a caution to ensure that the reset
302 * has time to take effect and then complete. Since the datasheet does
303 * not currently specify the exact sequence, we have chosen something
304 * that seems to work with our device.
306 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
308 ks8851_wrreg16(ks, KS_GRR, op);
309 mdelay(1); /* wait a short time to effect reset */
310 ks8851_wrreg16(ks, KS_GRR, 0);
311 mdelay(1); /* wait for condition to clear */
315 * ks8851_set_powermode - set power mode of the device
316 * @ks: The device state
317 * @pwrmode: The power mode value to write to KS_PMECR.
319 * Change the power mode of the chip.
321 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
325 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
327 pmecr = ks8851_rdreg16(ks, KS_PMECR);
328 pmecr &= ~PMECR_PM_MASK;
331 ks8851_wrreg16(ks, KS_PMECR, pmecr);
335 * ks8851_write_mac_addr - write mac address to device registers
336 * @dev: The network device
338 * Update the KS8851 MAC address registers from the address in @dev.
340 * This call assumes that the chip is not running, so there is no need to
341 * shutdown the RXQ process whilst setting this.
343 static int ks8851_write_mac_addr(struct net_device *dev)
345 struct ks8851_net *ks = netdev_priv(dev);
350 ks8851_lock(ks, &flags);
353 * Wake up chip in case it was powered off when stopped; otherwise,
354 * the first write to the MAC address does not take effect.
356 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
358 for (i = 0; i < ETH_ALEN; i += 2) {
359 val = (dev->dev_addr[i] << 8) | dev->dev_addr[i + 1];
360 ks8851_wrreg16(ks, KS_MAR(i), val);
363 if (!netif_running(dev))
364 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
366 ks8851_unlock(ks, &flags);
372 * ks8851_read_mac_addr - read mac address from device registers
373 * @dev: The network device
375 * Update our copy of the KS8851 MAC address from the registers of @dev.
377 static void ks8851_read_mac_addr(struct net_device *dev)
379 struct ks8851_net *ks = netdev_priv(dev);
384 ks8851_lock(ks, &flags);
386 for (i = 0; i < ETH_ALEN; i += 2) {
387 reg = ks8851_rdreg16(ks, KS_MAR(i));
388 dev->dev_addr[i] = reg >> 8;
389 dev->dev_addr[i + 1] = reg & 0xff;
392 ks8851_unlock(ks, &flags);
396 * ks8851_init_mac - initialise the mac address
397 * @ks: The device structure
398 * @np: The device node pointer
400 * Get or create the initial mac address for the device and then set that
401 * into the station address register. A mac address supplied in the device
402 * tree takes precedence. Otherwise, if there is an EEPROM present, then
403 * we try that. If no valid mac address is found we use eth_random_addr()
404 * to create a new one.
406 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
408 struct net_device *dev = ks->netdev;
411 mac_addr = of_get_mac_address(np);
412 if (!IS_ERR(mac_addr)) {
413 ether_addr_copy(dev->dev_addr, mac_addr);
414 ks8851_write_mac_addr(dev);
418 if (ks->rc_ccr & CCR_EEPROM) {
419 ks8851_read_mac_addr(dev);
420 if (is_valid_ether_addr(dev->dev_addr))
423 netdev_err(ks->netdev, "invalid mac address read %pM\n",
427 eth_hw_addr_random(dev);
428 ks8851_write_mac_addr(dev);
432 * ks8851_rdfifo - read data from the receive fifo
433 * @ks: The device state.
434 * @buff: The buffer address
435 * @len: The length of the data to read
437 * Issue an RXQ FIFO read command and read the @len amount of data from
438 * the FIFO into the buffer specified by @buff.
440 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
442 struct ks8851_net_spi *kss = to_ks8851_spi(ks);
443 struct spi_transfer *xfer = kss->spi_xfer2;
444 struct spi_message *msg = &kss->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(kss->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_skb - receive skbuff
488 static void ks8851_rx_skb(struct sk_buff *skb)
494 * ks8851_rx_pkts - receive packets from the host
495 * @ks: The device information.
497 * This is called from the IRQ work queue when the system detects that there
498 * are packets in the receive queue. Find out how many packets there are and
499 * read them from the FIFO.
501 static void ks8851_rx_pkts(struct ks8851_net *ks)
509 rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
511 netif_dbg(ks, rx_status, ks->netdev,
512 "%s: %d packets\n", __func__, rxfc);
514 /* Currently we're issuing a read per packet, but we could possibly
515 * improve the code by issuing a single read, getting the receive
516 * header, allocating the packet and then reading the packet data
519 * This form of operation would require us to hold the SPI bus'
520 * chipselect low during the entie transaction to avoid any
521 * reset to the data stream coming from the chip.
524 for (; rxfc != 0; rxfc--) {
525 rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
526 rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
528 netif_dbg(ks, rx_status, ks->netdev,
529 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
531 /* the length of the packet includes the 32bit CRC */
533 /* set dma read address */
534 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
536 /* start DMA access */
537 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
540 unsigned int rxalign;
543 rxalign = ALIGN(rxlen, 4);
544 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
547 /* 4 bytes of status header + 4 bytes of
548 * garbage: we put them before ethernet
549 * header, so that they are copied,
553 rxpkt = skb_put(skb, rxlen) - 8;
555 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
557 if (netif_msg_pktdata(ks))
558 ks8851_dbg_dumpkkt(ks, rxpkt);
560 skb->protocol = eth_type_trans(skb, ks->netdev);
563 ks->netdev->stats.rx_packets++;
564 ks->netdev->stats.rx_bytes += rxlen;
568 /* end DMA access and dequeue packet */
569 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
574 * ks8851_irq - IRQ handler for dealing with interrupt requests
578 * This handler is invoked when the IRQ line asserts to find out what happened.
579 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
582 * Read the interrupt status, work out what needs to be done and then clear
583 * any of the interrupts that are not needed.
585 static irqreturn_t ks8851_irq(int irq, void *_ks)
587 struct ks8851_net *ks = _ks;
588 unsigned handled = 0;
592 ks8851_lock(ks, &flags);
594 status = ks8851_rdreg16(ks, KS_ISR);
596 netif_dbg(ks, intr, ks->netdev,
597 "%s: status 0x%04x\n", __func__, status);
599 if (status & IRQ_LCI)
602 if (status & IRQ_LDI) {
603 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
604 pmecr &= ~PMECR_WKEVT_MASK;
605 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
610 if (status & IRQ_RXPSI)
611 handled |= IRQ_RXPSI;
613 if (status & IRQ_TXI) {
616 /* no lock here, tx queue should have been stopped */
618 /* update our idea of how much tx space is available to the
620 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
622 netif_dbg(ks, intr, ks->netdev,
623 "%s: txspace %d\n", __func__, ks->tx_space);
626 if (status & IRQ_RXI)
629 if (status & IRQ_SPIBEI) {
630 netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
631 handled |= IRQ_SPIBEI;
634 ks8851_wrreg16(ks, KS_ISR, handled);
636 if (status & IRQ_RXI) {
637 /* the datasheet says to disable the rx interrupt during
638 * packet read-out, however we're masking the interrupt
639 * from the device so do not bother masking just the RX
640 * from the device. */
645 /* if something stopped the rx process, probably due to wanting
646 * to change the rx settings, then do something about restarting
648 if (status & IRQ_RXPSI) {
649 struct ks8851_rxctrl *rxc = &ks->rxctrl;
651 /* update the multicast hash table */
652 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
653 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
654 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
655 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
657 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
658 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
661 ks8851_unlock(ks, &flags);
663 if (status & IRQ_LCI)
664 mii_check_link(&ks->mii);
666 if (status & IRQ_TXI)
667 netif_wake_queue(ks->netdev);
673 * calc_txlen - calculate size of message to send packet
674 * @len: Length of data
676 * Returns the size of the TXFIFO message needed to send
679 static inline unsigned calc_txlen(unsigned len)
681 return ALIGN(len + 4, 4);
685 * ks8851_wrpkt - write packet to TX FIFO
686 * @ks: The device state.
687 * @txp: The sk_buff to transmit.
688 * @irq: IRQ on completion of the packet.
690 * Send the @txp to the chip. This means creating the relevant packet header
691 * specifying the length of the packet and the other information the chip
692 * needs, such as IRQ on completion. Send the header and the packet data to
695 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
697 struct ks8851_net_spi *kss = to_ks8851_spi(ks);
698 struct spi_transfer *xfer = kss->spi_xfer2;
699 struct spi_message *msg = &kss->spi_msg2;
703 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
704 __func__, txp, txp->len, txp->data, irq);
707 fid &= TXFR_TXFID_MASK;
710 fid |= TXFR_TXIC; /* irq on completion */
712 /* start header at txb[1] to align txw entries */
713 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
714 ks->txh.txw[1] = cpu_to_le16(fid);
715 ks->txh.txw[2] = cpu_to_le16(txp->len);
717 xfer->tx_buf = &ks->txh.txb[1];
722 xfer->tx_buf = txp->data;
724 xfer->len = ALIGN(txp->len, 4);
726 ret = spi_sync(kss->spidev, msg);
728 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
732 * ks8851_done_tx - update and then free skbuff after transmitting
733 * @ks: The device state
734 * @txb: The buffer transmitted
736 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
738 struct net_device *dev = ks->netdev;
740 dev->stats.tx_bytes += txb->len;
741 dev->stats.tx_packets++;
747 * ks8851_tx_work - process tx packet(s)
748 * @work: The work strucutre what was scheduled.
750 * This is called when a number of packets have been scheduled for
751 * transmission and need to be sent to the device.
753 static void ks8851_tx_work(struct work_struct *work)
755 struct ks8851_net_spi *kss;
756 struct ks8851_net *ks;
761 kss = container_of(work, struct ks8851_net_spi, tx_work);
763 last = skb_queue_empty(&ks->txq);
765 ks8851_lock(ks, &flags);
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 ks8851_unlock(ks, &flags);
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);
797 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
798 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
801 netdev_err(dev, "failed to get irq\n");
805 /* lock the card, even if we may not actually be doing anything
806 * else at the moment */
807 ks8851_lock(ks, &flags);
809 netif_dbg(ks, ifup, ks->netdev, "opening\n");
811 /* bring chip out of any power saving mode it was in */
812 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
814 /* issue a soft reset to the RX/TX QMU to put it into a known
816 ks8851_soft_reset(ks, GRR_QMU);
818 /* setup transmission parameters */
820 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
821 TXCR_TXPE | /* pad to min length */
822 TXCR_TXCRC | /* add CRC */
823 TXCR_TXFCE)); /* enable flow control */
825 /* auto-increment tx data, reset tx pointer */
826 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
828 /* setup receiver control */
830 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
831 RXCR1_RXFCE | /* enable flow control */
832 RXCR1_RXBE | /* broadcast enable */
833 RXCR1_RXUE | /* unicast enable */
834 RXCR1_RXE)); /* enable rx block */
836 /* transfer entire frames out in one go */
837 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
839 /* set receive counter timeouts */
840 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
841 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
842 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
844 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
845 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
846 RXQCR_RXDTTE); /* IRQ on time exceeded */
848 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
850 /* clear then enable interrupts */
852 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
853 IRQ_TXI | /* TX done */ \
854 IRQ_RXI | /* RX done */ \
855 IRQ_SPIBEI | /* SPI bus error */ \
856 IRQ_TXPSI | /* TX process stop */ \
857 IRQ_RXPSI) /* RX process stop */
859 ks->rc_ier = STD_IRQ;
860 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
861 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
863 netif_start_queue(ks->netdev);
865 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
867 ks8851_unlock(ks, &flags);
868 mii_check_link(&ks->mii);
873 * ks8851_net_stop - close network device
874 * @dev: The device being closed.
876 * Called to close down a network device which has been active. Cancell any
877 * work, shutdown the RX and TX process and then place the chip into a low
878 * power state whilst it is not being used.
880 static int ks8851_net_stop(struct net_device *dev)
882 struct ks8851_net *ks = netdev_priv(dev);
883 struct ks8851_net_spi *kss;
886 kss = to_ks8851_spi(ks);
888 netif_info(ks, ifdown, dev, "shutting down\n");
890 netif_stop_queue(dev);
892 ks8851_lock(ks, &flags);
893 /* turn off the IRQs and ack any outstanding */
894 ks8851_wrreg16(ks, KS_IER, 0x0000);
895 ks8851_wrreg16(ks, KS_ISR, 0xffff);
896 ks8851_unlock(ks, &flags);
898 /* stop any outstanding work */
899 flush_work(&kss->tx_work);
900 flush_work(&ks->rxctrl_work);
902 ks8851_lock(ks, &flags);
903 /* shutdown RX process */
904 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
906 /* shutdown TX process */
907 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
909 /* set powermode to soft power down to save power */
910 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
911 ks8851_unlock(ks, &flags);
913 /* ensure any queued tx buffers are dumped */
914 while (!skb_queue_empty(&ks->txq)) {
915 struct sk_buff *txb = skb_dequeue(&ks->txq);
917 netif_dbg(ks, ifdown, ks->netdev,
918 "%s: freeing txb %p\n", __func__, txb);
923 free_irq(dev->irq, ks);
929 * ks8851_start_xmit - transmit packet
930 * @skb: The buffer to transmit
931 * @dev: The device used to transmit the packet.
933 * Called by the network layer to transmit the @skb. Queue the packet for
934 * the device and schedule the necessary work to transmit the packet when
937 * We do this to firstly avoid sleeping with the network device locked,
938 * and secondly so we can round up more than one packet to transmit which
939 * means we can try and avoid generating too many transmit done interrupts.
941 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
942 struct net_device *dev)
944 struct ks8851_net *ks = netdev_priv(dev);
945 unsigned needed = calc_txlen(skb->len);
946 netdev_tx_t ret = NETDEV_TX_OK;
947 struct ks8851_net_spi *kss;
949 kss = to_ks8851_spi(ks);
951 netif_dbg(ks, tx_queued, ks->netdev,
952 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
954 spin_lock(&ks->statelock);
956 if (needed > ks->tx_space) {
957 netif_stop_queue(dev);
958 ret = NETDEV_TX_BUSY;
960 ks->tx_space -= needed;
961 skb_queue_tail(&ks->txq, skb);
964 spin_unlock(&ks->statelock);
965 schedule_work(&kss->tx_work);
971 * ks8851_rxctrl_work - work handler to change rx mode
972 * @work: The work structure this belongs to.
974 * Lock the device and issue the necessary changes to the receive mode from
975 * the network device layer. This is done so that we can do this without
976 * having to sleep whilst holding the network device lock.
978 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
979 * receive parameters are programmed, we issue a write to disable the RXQ and
980 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
981 * complete. The interrupt handler then writes the new values into the chip.
983 static void ks8851_rxctrl_work(struct work_struct *work)
985 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
988 ks8851_lock(ks, &flags);
990 /* need to shutdown RXQ before modifying filter parameters */
991 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
993 ks8851_unlock(ks, &flags);
996 static void ks8851_set_rx_mode(struct net_device *dev)
998 struct ks8851_net *ks = netdev_priv(dev);
999 struct ks8851_rxctrl rxctrl;
1001 memset(&rxctrl, 0, sizeof(rxctrl));
1003 if (dev->flags & IFF_PROMISC) {
1004 /* interface to receive everything */
1006 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
1007 } else if (dev->flags & IFF_ALLMULTI) {
1008 /* accept all multicast packets */
1010 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1011 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
1012 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
1013 struct netdev_hw_addr *ha;
1016 /* accept some multicast */
1018 netdev_for_each_mc_addr(ha, dev) {
1019 crc = ether_crc(ETH_ALEN, ha->addr);
1020 crc >>= (32 - 6); /* get top six bits */
1022 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1025 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1027 /* just accept broadcast / unicast */
1028 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1031 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1032 RXCR1_RXBE | /* broadcast enable */
1033 RXCR1_RXE | /* RX process enable */
1034 RXCR1_RXFCE); /* enable flow control */
1036 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1038 /* schedule work to do the actual set of the data if needed */
1040 spin_lock(&ks->statelock);
1042 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1043 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1044 schedule_work(&ks->rxctrl_work);
1047 spin_unlock(&ks->statelock);
1050 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1052 struct sockaddr *sa = addr;
1054 if (netif_running(dev))
1057 if (!is_valid_ether_addr(sa->sa_data))
1058 return -EADDRNOTAVAIL;
1060 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1061 return ks8851_write_mac_addr(dev);
1064 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1066 struct ks8851_net *ks = netdev_priv(dev);
1068 if (!netif_running(dev))
1071 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1074 static const struct net_device_ops ks8851_netdev_ops = {
1075 .ndo_open = ks8851_net_open,
1076 .ndo_stop = ks8851_net_stop,
1077 .ndo_do_ioctl = ks8851_net_ioctl,
1078 .ndo_start_xmit = ks8851_start_xmit,
1079 .ndo_set_mac_address = ks8851_set_mac_address,
1080 .ndo_set_rx_mode = ks8851_set_rx_mode,
1081 .ndo_validate_addr = eth_validate_addr,
1084 /* ethtool support */
1086 static void ks8851_get_drvinfo(struct net_device *dev,
1087 struct ethtool_drvinfo *di)
1089 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1090 strlcpy(di->version, "1.00", sizeof(di->version));
1091 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1094 static u32 ks8851_get_msglevel(struct net_device *dev)
1096 struct ks8851_net *ks = netdev_priv(dev);
1097 return ks->msg_enable;
1100 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1102 struct ks8851_net *ks = netdev_priv(dev);
1103 ks->msg_enable = to;
1106 static int ks8851_get_link_ksettings(struct net_device *dev,
1107 struct ethtool_link_ksettings *cmd)
1109 struct ks8851_net *ks = netdev_priv(dev);
1111 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1116 static int ks8851_set_link_ksettings(struct net_device *dev,
1117 const struct ethtool_link_ksettings *cmd)
1119 struct ks8851_net *ks = netdev_priv(dev);
1120 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1123 static u32 ks8851_get_link(struct net_device *dev)
1125 struct ks8851_net *ks = netdev_priv(dev);
1126 return mii_link_ok(&ks->mii);
1129 static int ks8851_nway_reset(struct net_device *dev)
1131 struct ks8851_net *ks = netdev_priv(dev);
1132 return mii_nway_restart(&ks->mii);
1135 /* EEPROM support */
1137 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1139 struct ks8851_net *ks = ee->data;
1142 val = ks8851_rdreg16(ks, KS_EEPCR);
1144 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1145 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1146 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1149 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1151 struct ks8851_net *ks = ee->data;
1152 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1155 val |= EEPCR_EESRWA;
1156 if (ee->reg_data_in)
1158 if (ee->reg_data_clock)
1160 if (ee->reg_chip_select)
1163 ks8851_wrreg16(ks, KS_EEPCR, val);
1167 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1168 * @ks: The network device state.
1170 * Check for the presence of an EEPROM, and then activate software access
1173 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1175 /* start with clock low, cs high */
1176 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1181 * ks8851_eeprom_release - release the EEPROM interface
1182 * @ks: The device state
1184 * Release the software access to the device EEPROM
1186 static void ks8851_eeprom_release(struct ks8851_net *ks)
1188 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1190 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1193 #define KS_EEPROM_MAGIC (0x00008851)
1195 static int ks8851_set_eeprom(struct net_device *dev,
1196 struct ethtool_eeprom *ee, u8 *data)
1198 struct ks8851_net *ks = netdev_priv(dev);
1199 int offset = ee->offset;
1200 unsigned long flags;
1204 /* currently only support byte writing */
1208 if (ee->magic != KS_EEPROM_MAGIC)
1211 if (!(ks->rc_ccr & CCR_EEPROM))
1214 ks8851_lock(ks, &flags);
1216 ks8851_eeprom_claim(ks);
1218 eeprom_93cx6_wren(&ks->eeprom, true);
1220 /* ethtool currently only supports writing bytes, which means
1221 * we have to read/modify/write our 16bit EEPROMs */
1223 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1233 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1234 eeprom_93cx6_wren(&ks->eeprom, false);
1236 ks8851_eeprom_release(ks);
1237 ks8851_unlock(ks, &flags);
1242 static int ks8851_get_eeprom(struct net_device *dev,
1243 struct ethtool_eeprom *ee, u8 *data)
1245 struct ks8851_net *ks = netdev_priv(dev);
1246 int offset = ee->offset;
1247 unsigned long flags;
1250 /* must be 2 byte aligned */
1251 if (len & 1 || offset & 1)
1254 if (!(ks->rc_ccr & CCR_EEPROM))
1257 ks8851_lock(ks, &flags);
1259 ks8851_eeprom_claim(ks);
1261 ee->magic = KS_EEPROM_MAGIC;
1263 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1264 ks8851_eeprom_release(ks);
1265 ks8851_unlock(ks, &flags);
1270 static int ks8851_get_eeprom_len(struct net_device *dev)
1272 struct ks8851_net *ks = netdev_priv(dev);
1274 /* currently, we assume it is an 93C46 attached, so return 128 */
1275 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1278 static const struct ethtool_ops ks8851_ethtool_ops = {
1279 .get_drvinfo = ks8851_get_drvinfo,
1280 .get_msglevel = ks8851_get_msglevel,
1281 .set_msglevel = ks8851_set_msglevel,
1282 .get_link = ks8851_get_link,
1283 .nway_reset = ks8851_nway_reset,
1284 .get_eeprom_len = ks8851_get_eeprom_len,
1285 .get_eeprom = ks8851_get_eeprom,
1286 .set_eeprom = ks8851_set_eeprom,
1287 .get_link_ksettings = ks8851_get_link_ksettings,
1288 .set_link_ksettings = ks8851_set_link_ksettings,
1291 /* MII interface controls */
1294 * ks8851_phy_reg - convert MII register into a KS8851 register
1295 * @reg: MII register number.
1297 * Return the KS8851 register number for the corresponding MII PHY register
1298 * if possible. Return zero if the MII register has no direct mapping to the
1299 * KS8851 register set.
1301 static int ks8851_phy_reg(int reg)
1322 * ks8851_phy_read - MII interface PHY register read.
1323 * @dev: The network device the PHY is on.
1324 * @phy_addr: Address of PHY (ignored as we only have one)
1325 * @reg: The register to read.
1327 * This call reads data from the PHY register specified in @reg. Since the
1328 * device does not support all the MII registers, the non-existent values
1329 * are always returned as zero.
1331 * We return zero for unsupported registers as the MII code does not check
1332 * the value returned for any error status, and simply returns it to the
1333 * caller. The mii-tool that the driver was tested with takes any -ve error
1334 * as real PHY capabilities, thus displaying incorrect data to the user.
1336 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1338 struct ks8851_net *ks = netdev_priv(dev);
1339 unsigned long flags;
1343 ksreg = ks8851_phy_reg(reg);
1345 return 0x0; /* no error return allowed, so use zero */
1347 ks8851_lock(ks, &flags);
1348 result = ks8851_rdreg16(ks, ksreg);
1349 ks8851_unlock(ks, &flags);
1354 static void ks8851_phy_write(struct net_device *dev,
1355 int phy, int reg, int value)
1357 struct ks8851_net *ks = netdev_priv(dev);
1358 unsigned long flags;
1361 ksreg = ks8851_phy_reg(reg);
1363 ks8851_lock(ks, &flags);
1364 ks8851_wrreg16(ks, ksreg, value);
1365 ks8851_unlock(ks, &flags);
1370 * ks8851_read_selftest - read the selftest memory info.
1371 * @ks: The device state
1373 * Read and check the TX/RX memory selftest information.
1375 static int ks8851_read_selftest(struct ks8851_net *ks)
1377 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1381 rd = ks8851_rdreg16(ks, KS_MBIR);
1383 if ((rd & both_done) != both_done) {
1384 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1388 if (rd & MBIR_TXMBFA) {
1389 netdev_err(ks->netdev, "TX memory selftest fail\n");
1393 if (rd & MBIR_RXMBFA) {
1394 netdev_err(ks->netdev, "RX memory selftest fail\n");
1401 /* driver bus management functions */
1403 #ifdef CONFIG_PM_SLEEP
1405 static int ks8851_suspend(struct device *dev)
1407 struct ks8851_net *ks = dev_get_drvdata(dev);
1408 struct net_device *netdev = ks->netdev;
1410 if (netif_running(netdev)) {
1411 netif_device_detach(netdev);
1412 ks8851_net_stop(netdev);
1418 static int ks8851_resume(struct device *dev)
1420 struct ks8851_net *ks = dev_get_drvdata(dev);
1421 struct net_device *netdev = ks->netdev;
1423 if (netif_running(netdev)) {
1424 ks8851_net_open(netdev);
1425 netif_device_attach(netdev);
1432 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1434 static int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1437 struct ks8851_net *ks = netdev_priv(netdev);
1442 ks->netdev = netdev;
1443 ks->tx_space = 6144;
1445 gpio = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0, NULL);
1446 if (gpio == -EPROBE_DEFER)
1450 if (gpio_is_valid(gpio)) {
1451 ret = devm_gpio_request_one(dev, gpio,
1452 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1454 dev_err(dev, "reset gpio request failed\n");
1459 ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1460 if (IS_ERR(ks->vdd_io)) {
1461 ret = PTR_ERR(ks->vdd_io);
1465 ret = regulator_enable(ks->vdd_io);
1467 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1471 ks->vdd_reg = devm_regulator_get(dev, "vdd");
1472 if (IS_ERR(ks->vdd_reg)) {
1473 ret = PTR_ERR(ks->vdd_reg);
1477 ret = regulator_enable(ks->vdd_reg);
1479 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1483 if (gpio_is_valid(gpio)) {
1484 usleep_range(10000, 11000);
1485 gpio_set_value(gpio, 1);
1488 spin_lock_init(&ks->statelock);
1490 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1492 /* setup EEPROM state */
1493 ks->eeprom.data = ks;
1494 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1495 ks->eeprom.register_read = ks8851_eeprom_regread;
1496 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1498 /* setup mii state */
1499 ks->mii.dev = netdev;
1501 ks->mii.phy_id_mask = 1;
1502 ks->mii.reg_num_mask = 0xf;
1503 ks->mii.mdio_read = ks8851_phy_read;
1504 ks->mii.mdio_write = ks8851_phy_write;
1506 dev_info(dev, "message enable is %d\n", msg_en);
1508 /* set the default message enable */
1509 ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1513 skb_queue_head_init(&ks->txq);
1515 netdev->ethtool_ops = &ks8851_ethtool_ops;
1516 SET_NETDEV_DEV(netdev, dev);
1518 dev_set_drvdata(dev, ks);
1520 netif_carrier_off(ks->netdev);
1521 netdev->if_port = IF_PORT_100BASET;
1522 netdev->netdev_ops = &ks8851_netdev_ops;
1524 /* issue a global soft reset to reset the device. */
1525 ks8851_soft_reset(ks, GRR_GSR);
1527 /* simple check for a valid chip being connected to the bus */
1528 cider = ks8851_rdreg16(ks, KS_CIDER);
1529 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1530 dev_err(dev, "failed to read device ID\n");
1535 /* cache the contents of the CCR register for EEPROM, etc. */
1536 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1538 ks8851_read_selftest(ks);
1539 ks8851_init_mac(ks, dev->of_node);
1541 ret = register_netdev(netdev);
1543 dev_err(dev, "failed to register network device\n");
1547 netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1548 CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1549 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1555 if (gpio_is_valid(gpio))
1556 gpio_set_value(gpio, 0);
1557 regulator_disable(ks->vdd_reg);
1559 regulator_disable(ks->vdd_io);
1564 static int ks8851_remove_common(struct device *dev)
1566 struct ks8851_net *priv = dev_get_drvdata(dev);
1568 if (netif_msg_drv(priv))
1569 dev_info(dev, "remove\n");
1571 unregister_netdev(priv->netdev);
1572 if (gpio_is_valid(priv->gpio))
1573 gpio_set_value(priv->gpio, 0);
1574 regulator_disable(priv->vdd_reg);
1575 regulator_disable(priv->vdd_io);
1580 static int ks8851_probe(struct spi_device *spi)
1582 struct device *dev = &spi->dev;
1583 struct ks8851_net_spi *kss;
1584 struct net_device *netdev;
1585 struct ks8851_net *ks;
1587 netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_spi));
1591 spi->bits_per_word = 8;
1593 ks = netdev_priv(netdev);
1594 kss = to_ks8851_spi(ks);
1597 mutex_init(&kss->lock);
1598 INIT_WORK(&kss->tx_work, ks8851_tx_work);
1600 /* initialise pre-made spi transfer messages */
1601 spi_message_init(&kss->spi_msg1);
1602 spi_message_add_tail(&kss->spi_xfer1, &kss->spi_msg1);
1604 spi_message_init(&kss->spi_msg2);
1605 spi_message_add_tail(&kss->spi_xfer2[0], &kss->spi_msg2);
1606 spi_message_add_tail(&kss->spi_xfer2[1], &kss->spi_msg2);
1608 netdev->irq = spi->irq;
1610 return ks8851_probe_common(netdev, dev, msg_enable);
1613 static int ks8851_remove(struct spi_device *spi)
1615 return ks8851_remove_common(&spi->dev);
1618 static const struct of_device_id ks8851_match_table[] = {
1619 { .compatible = "micrel,ks8851" },
1622 MODULE_DEVICE_TABLE(of, ks8851_match_table);
1624 static struct spi_driver ks8851_driver = {
1627 .of_match_table = ks8851_match_table,
1628 .pm = &ks8851_pm_ops,
1630 .probe = ks8851_probe,
1631 .remove = ks8851_remove,
1633 module_spi_driver(ks8851_driver);
1635 MODULE_DESCRIPTION("KS8851 Network driver");
1636 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1637 MODULE_LICENSE("GPL");
1639 module_param_named(message, msg_enable, int, 0);
1640 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1641 MODULE_ALIAS("spi:ks8851");