gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / micrel / ks8851.c
blob33305c9c5a62df45a5c582c6baf320cc81b4bb0f
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>
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define DEBUG
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>
30 #include "ks8851.h"
32 /**
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
42 * commands.
44 struct ks8851_rxctrl {
45 u16 mchash[4];
46 u16 rxcr1;
47 u16 rxcr2;
50 /**
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.
59 union ks8851_tx_hdr {
60 u8 txb[6];
61 __le16 txw[3];
64 /**
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
101 * modifies.
103 struct ks8851_net {
104 struct net_device *netdev;
105 struct spi_device *spidev;
106 struct mutex lock;
107 spinlock_t statelock;
109 union ks8851_tx_hdr txh ____cacheline_aligned;
110 u8 rxd[8];
111 u8 txd[8];
113 u32 msg_enable ____cacheline_aligned;
114 u16 tx_space;
115 u8 fid;
117 u16 rc_ier;
118 u16 rc_rxqcr;
119 u16 rc_ccr;
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;
137 int gpio;
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;
173 __le16 txb[2];
174 int ret;
176 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
177 txb[1] = cpu_to_le16(val);
179 xfer->tx_buf = txb;
180 xfer->rx_buf = NULL;
181 xfer->len = 4;
183 ret = spi_sync(ks->spidev, msg);
184 if (ret < 0)
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;
200 __le16 txb[2];
201 int ret;
202 int bit;
204 bit = 1 << (reg & 3);
206 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
207 txb[1] = val;
209 xfer->tx_buf = txb;
210 xfer->rx_buf = NULL;
211 xfer->len = 3;
213 ret = spi_sync(ks->spidev, msg);
214 if (ret < 0)
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;
234 u8 *trx = ks->rxd;
235 int ret;
237 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
239 if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
240 msg = &ks->spi_msg2;
241 xfer = ks->spi_xfer2;
243 xfer->tx_buf = txb;
244 xfer->rx_buf = NULL;
245 xfer->len = 2;
247 xfer++;
248 xfer->tx_buf = NULL;
249 xfer->rx_buf = trx;
250 xfer->len = rxl;
251 } else {
252 msg = &ks->spi_msg1;
253 xfer = &ks->spi_xfer1;
255 xfer->tx_buf = txb;
256 xfer->rx_buf = trx;
257 xfer->len = rxl + 2;
260 ret = spi_sync(ks->spidev, msg);
261 if (ret < 0)
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);
265 else
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)
278 u8 rxb[1];
280 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
281 return rxb[0];
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)
293 __le16 rx = 0;
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)
310 __le32 rx = 0;
312 WARN_ON(reg & 3);
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
324 * specified by @op.
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)
348 unsigned pmecr;
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;
354 pmecr |= pwrmode;
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);
371 int i;
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);
387 return 0;
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);
399 int i;
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
413 * Get or create the initial mac address for the device and then set that
414 * into the station address register. A mac address supplied in the device
415 * tree takes precedence. Otherwise, if there is an EEPROM present, then
416 * we try that. If no valid mac address is found we use eth_random_addr()
417 * to create a new one.
419 static void ks8851_init_mac(struct ks8851_net *ks)
421 struct net_device *dev = ks->netdev;
422 const u8 *mac_addr;
424 mac_addr = of_get_mac_address(ks->spidev->dev.of_node);
425 if (!IS_ERR(mac_addr)) {
426 ether_addr_copy(dev->dev_addr, mac_addr);
427 ks8851_write_mac_addr(dev);
428 return;
431 if (ks->rc_ccr & CCR_EEPROM) {
432 ks8851_read_mac_addr(dev);
433 if (is_valid_ether_addr(dev->dev_addr))
434 return;
436 netdev_err(ks->netdev, "invalid mac address read %pM\n",
437 dev->dev_addr);
440 eth_hw_addr_random(dev);
441 ks8851_write_mac_addr(dev);
445 * ks8851_rdfifo - read data from the receive fifo
446 * @ks: The device state.
447 * @buff: The buffer address
448 * @len: The length of the data to read
450 * Issue an RXQ FIFO read command and read the @len amount of data from
451 * the FIFO into the buffer specified by @buff.
453 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
455 struct spi_transfer *xfer = ks->spi_xfer2;
456 struct spi_message *msg = &ks->spi_msg2;
457 u8 txb[1];
458 int ret;
460 netif_dbg(ks, rx_status, ks->netdev,
461 "%s: %d@%p\n", __func__, len, buff);
463 /* set the operation we're issuing */
464 txb[0] = KS_SPIOP_RXFIFO;
466 xfer->tx_buf = txb;
467 xfer->rx_buf = NULL;
468 xfer->len = 1;
470 xfer++;
471 xfer->rx_buf = buff;
472 xfer->tx_buf = NULL;
473 xfer->len = len;
475 ret = spi_sync(ks->spidev, msg);
476 if (ret < 0)
477 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
481 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
482 * @ks: The device state
483 * @rxpkt: The data for the received packet
485 * Dump the initial data from the packet to dev_dbg().
487 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
489 netdev_dbg(ks->netdev,
490 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
491 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
492 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
493 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
497 * ks8851_rx_pkts - receive packets from the host
498 * @ks: The device information.
500 * This is called from the IRQ work queue when the system detects that there
501 * are packets in the receive queue. Find out how many packets there are and
502 * read them from the FIFO.
504 static void ks8851_rx_pkts(struct ks8851_net *ks)
506 struct sk_buff *skb;
507 unsigned rxfc;
508 unsigned rxlen;
509 unsigned rxstat;
510 u32 rxh;
511 u8 *rxpkt;
513 rxfc = ks8851_rdreg8(ks, KS_RXFC);
515 netif_dbg(ks, rx_status, ks->netdev,
516 "%s: %d packets\n", __func__, rxfc);
518 /* Currently we're issuing a read per packet, but we could possibly
519 * improve the code by issuing a single read, getting the receive
520 * header, allocating the packet and then reading the packet data
521 * out in one go.
523 * This form of operation would require us to hold the SPI bus'
524 * chipselect low during the entie transaction to avoid any
525 * reset to the data stream coming from the chip.
528 for (; rxfc != 0; rxfc--) {
529 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
530 rxstat = rxh & 0xffff;
531 rxlen = (rxh >> 16) & 0xfff;
533 netif_dbg(ks, rx_status, ks->netdev,
534 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
536 /* the length of the packet includes the 32bit CRC */
538 /* set dma read address */
539 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
541 /* start DMA access */
542 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
544 if (rxlen > 4) {
545 unsigned int rxalign;
547 rxlen -= 4;
548 rxalign = ALIGN(rxlen, 4);
549 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
550 if (skb) {
552 /* 4 bytes of status header + 4 bytes of
553 * garbage: we put them before ethernet
554 * header, so that they are copied,
555 * but ignored.
558 rxpkt = skb_put(skb, rxlen) - 8;
560 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
562 if (netif_msg_pktdata(ks))
563 ks8851_dbg_dumpkkt(ks, rxpkt);
565 skb->protocol = eth_type_trans(skb, ks->netdev);
566 netif_rx_ni(skb);
568 ks->netdev->stats.rx_packets++;
569 ks->netdev->stats.rx_bytes += rxlen;
573 /* end DMA access and dequeue packet */
574 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
579 * ks8851_irq - IRQ handler for dealing with interrupt requests
580 * @irq: IRQ number
581 * @_ks: cookie
583 * This handler is invoked when the IRQ line asserts to find out what happened.
584 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
585 * in thread context.
587 * Read the interrupt status, work out what needs to be done and then clear
588 * any of the interrupts that are not needed.
590 static irqreturn_t ks8851_irq(int irq, void *_ks)
592 struct ks8851_net *ks = _ks;
593 unsigned status;
594 unsigned handled = 0;
596 mutex_lock(&ks->lock);
598 status = ks8851_rdreg16(ks, KS_ISR);
600 netif_dbg(ks, intr, ks->netdev,
601 "%s: status 0x%04x\n", __func__, status);
603 if (status & IRQ_LCI)
604 handled |= IRQ_LCI;
606 if (status & IRQ_LDI) {
607 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
608 pmecr &= ~PMECR_WKEVT_MASK;
609 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
611 handled |= IRQ_LDI;
614 if (status & IRQ_RXPSI)
615 handled |= IRQ_RXPSI;
617 if (status & IRQ_TXI) {
618 handled |= IRQ_TXI;
620 /* no lock here, tx queue should have been stopped */
622 /* update our idea of how much tx space is available to the
623 * system */
624 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
626 netif_dbg(ks, intr, ks->netdev,
627 "%s: txspace %d\n", __func__, ks->tx_space);
630 if (status & IRQ_RXI)
631 handled |= IRQ_RXI;
633 if (status & IRQ_SPIBEI) {
634 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
635 handled |= IRQ_SPIBEI;
638 ks8851_wrreg16(ks, KS_ISR, handled);
640 if (status & IRQ_RXI) {
641 /* the datasheet says to disable the rx interrupt during
642 * packet read-out, however we're masking the interrupt
643 * from the device so do not bother masking just the RX
644 * from the device. */
646 ks8851_rx_pkts(ks);
649 /* if something stopped the rx process, probably due to wanting
650 * to change the rx settings, then do something about restarting
651 * it. */
652 if (status & IRQ_RXPSI) {
653 struct ks8851_rxctrl *rxc = &ks->rxctrl;
655 /* update the multicast hash table */
656 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
657 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
658 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
659 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
661 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
662 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
665 mutex_unlock(&ks->lock);
667 if (status & IRQ_LCI)
668 mii_check_link(&ks->mii);
670 if (status & IRQ_TXI)
671 netif_wake_queue(ks->netdev);
673 return IRQ_HANDLED;
677 * calc_txlen - calculate size of message to send packet
678 * @len: Length of data
680 * Returns the size of the TXFIFO message needed to send
681 * this packet.
683 static inline unsigned calc_txlen(unsigned len)
685 return ALIGN(len + 4, 4);
689 * ks8851_wrpkt - write packet to TX FIFO
690 * @ks: The device state.
691 * @txp: The sk_buff to transmit.
692 * @irq: IRQ on completion of the packet.
694 * Send the @txp to the chip. This means creating the relevant packet header
695 * specifying the length of the packet and the other information the chip
696 * needs, such as IRQ on completion. Send the header and the packet data to
697 * the device.
699 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
701 struct spi_transfer *xfer = ks->spi_xfer2;
702 struct spi_message *msg = &ks->spi_msg2;
703 unsigned fid = 0;
704 int ret;
706 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
707 __func__, txp, txp->len, txp->data, irq);
709 fid = ks->fid++;
710 fid &= TXFR_TXFID_MASK;
712 if (irq)
713 fid |= TXFR_TXIC; /* irq on completion */
715 /* start header at txb[1] to align txw entries */
716 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
717 ks->txh.txw[1] = cpu_to_le16(fid);
718 ks->txh.txw[2] = cpu_to_le16(txp->len);
720 xfer->tx_buf = &ks->txh.txb[1];
721 xfer->rx_buf = NULL;
722 xfer->len = 5;
724 xfer++;
725 xfer->tx_buf = txp->data;
726 xfer->rx_buf = NULL;
727 xfer->len = ALIGN(txp->len, 4);
729 ret = spi_sync(ks->spidev, msg);
730 if (ret < 0)
731 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
735 * ks8851_done_tx - update and then free skbuff after transmitting
736 * @ks: The device state
737 * @txb: The buffer transmitted
739 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
741 struct net_device *dev = ks->netdev;
743 dev->stats.tx_bytes += txb->len;
744 dev->stats.tx_packets++;
746 dev_kfree_skb(txb);
750 * ks8851_tx_work - process tx packet(s)
751 * @work: The work strucutre what was scheduled.
753 * This is called when a number of packets have been scheduled for
754 * transmission and need to be sent to the device.
756 static void ks8851_tx_work(struct work_struct *work)
758 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
759 struct sk_buff *txb;
760 bool last = skb_queue_empty(&ks->txq);
762 mutex_lock(&ks->lock);
764 while (!last) {
765 txb = skb_dequeue(&ks->txq);
766 last = skb_queue_empty(&ks->txq);
768 if (txb != NULL) {
769 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
770 ks8851_wrpkt(ks, txb, last);
771 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
772 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
774 ks8851_done_tx(ks, txb);
778 mutex_unlock(&ks->lock);
782 * ks8851_net_open - open network device
783 * @dev: The network device being opened.
785 * Called when the network device is marked active, such as a user executing
786 * 'ifconfig up' on the device.
788 static int ks8851_net_open(struct net_device *dev)
790 struct ks8851_net *ks = netdev_priv(dev);
791 int ret;
793 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
794 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
795 dev->name, ks);
796 if (ret < 0) {
797 netdev_err(dev, "failed to get irq\n");
798 return ret;
801 /* lock the card, even if we may not actually be doing anything
802 * else at the moment */
803 mutex_lock(&ks->lock);
805 netif_dbg(ks, ifup, ks->netdev, "opening\n");
807 /* bring chip out of any power saving mode it was in */
808 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
810 /* issue a soft reset to the RX/TX QMU to put it into a known
811 * state. */
812 ks8851_soft_reset(ks, GRR_QMU);
814 /* setup transmission parameters */
816 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
817 TXCR_TXPE | /* pad to min length */
818 TXCR_TXCRC | /* add CRC */
819 TXCR_TXFCE)); /* enable flow control */
821 /* auto-increment tx data, reset tx pointer */
822 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
824 /* setup receiver control */
826 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
827 RXCR1_RXFCE | /* enable flow control */
828 RXCR1_RXBE | /* broadcast enable */
829 RXCR1_RXUE | /* unicast enable */
830 RXCR1_RXE)); /* enable rx block */
832 /* transfer entire frames out in one go */
833 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
835 /* set receive counter timeouts */
836 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
837 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
838 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
840 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
841 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
842 RXQCR_RXDTTE); /* IRQ on time exceeded */
844 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
846 /* clear then enable interrupts */
848 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
849 IRQ_TXI | /* TX done */ \
850 IRQ_RXI | /* RX done */ \
851 IRQ_SPIBEI | /* SPI bus error */ \
852 IRQ_TXPSI | /* TX process stop */ \
853 IRQ_RXPSI) /* RX process stop */
855 ks->rc_ier = STD_IRQ;
856 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
857 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
859 netif_start_queue(ks->netdev);
861 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
863 mutex_unlock(&ks->lock);
864 mii_check_link(&ks->mii);
865 return 0;
869 * ks8851_net_stop - close network device
870 * @dev: The device being closed.
872 * Called to close down a network device which has been active. Cancell any
873 * work, shutdown the RX and TX process and then place the chip into a low
874 * power state whilst it is not being used.
876 static int ks8851_net_stop(struct net_device *dev)
878 struct ks8851_net *ks = netdev_priv(dev);
880 netif_info(ks, ifdown, dev, "shutting down\n");
882 netif_stop_queue(dev);
884 mutex_lock(&ks->lock);
885 /* turn off the IRQs and ack any outstanding */
886 ks8851_wrreg16(ks, KS_IER, 0x0000);
887 ks8851_wrreg16(ks, KS_ISR, 0xffff);
888 mutex_unlock(&ks->lock);
890 /* stop any outstanding work */
891 flush_work(&ks->tx_work);
892 flush_work(&ks->rxctrl_work);
894 mutex_lock(&ks->lock);
895 /* shutdown RX process */
896 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
898 /* shutdown TX process */
899 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
901 /* set powermode to soft power down to save power */
902 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
903 mutex_unlock(&ks->lock);
905 /* ensure any queued tx buffers are dumped */
906 while (!skb_queue_empty(&ks->txq)) {
907 struct sk_buff *txb = skb_dequeue(&ks->txq);
909 netif_dbg(ks, ifdown, ks->netdev,
910 "%s: freeing txb %p\n", __func__, txb);
912 dev_kfree_skb(txb);
915 free_irq(dev->irq, ks);
917 return 0;
921 * ks8851_start_xmit - transmit packet
922 * @skb: The buffer to transmit
923 * @dev: The device used to transmit the packet.
925 * Called by the network layer to transmit the @skb. Queue the packet for
926 * the device and schedule the necessary work to transmit the packet when
927 * it is free.
929 * We do this to firstly avoid sleeping with the network device locked,
930 * and secondly so we can round up more than one packet to transmit which
931 * means we can try and avoid generating too many transmit done interrupts.
933 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
934 struct net_device *dev)
936 struct ks8851_net *ks = netdev_priv(dev);
937 unsigned needed = calc_txlen(skb->len);
938 netdev_tx_t ret = NETDEV_TX_OK;
940 netif_dbg(ks, tx_queued, ks->netdev,
941 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
943 spin_lock(&ks->statelock);
945 if (needed > ks->tx_space) {
946 netif_stop_queue(dev);
947 ret = NETDEV_TX_BUSY;
948 } else {
949 ks->tx_space -= needed;
950 skb_queue_tail(&ks->txq, skb);
953 spin_unlock(&ks->statelock);
954 schedule_work(&ks->tx_work);
956 return ret;
960 * ks8851_rxctrl_work - work handler to change rx mode
961 * @work: The work structure this belongs to.
963 * Lock the device and issue the necessary changes to the receive mode from
964 * the network device layer. This is done so that we can do this without
965 * having to sleep whilst holding the network device lock.
967 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
968 * receive parameters are programmed, we issue a write to disable the RXQ and
969 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
970 * complete. The interrupt handler then writes the new values into the chip.
972 static void ks8851_rxctrl_work(struct work_struct *work)
974 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
976 mutex_lock(&ks->lock);
978 /* need to shutdown RXQ before modifying filter parameters */
979 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
981 mutex_unlock(&ks->lock);
984 static void ks8851_set_rx_mode(struct net_device *dev)
986 struct ks8851_net *ks = netdev_priv(dev);
987 struct ks8851_rxctrl rxctrl;
989 memset(&rxctrl, 0, sizeof(rxctrl));
991 if (dev->flags & IFF_PROMISC) {
992 /* interface to receive everything */
994 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
995 } else if (dev->flags & IFF_ALLMULTI) {
996 /* accept all multicast packets */
998 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
999 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
1000 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
1001 struct netdev_hw_addr *ha;
1002 u32 crc;
1004 /* accept some multicast */
1006 netdev_for_each_mc_addr(ha, dev) {
1007 crc = ether_crc(ETH_ALEN, ha->addr);
1008 crc >>= (32 - 6); /* get top six bits */
1010 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1013 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1014 } else {
1015 /* just accept broadcast / unicast */
1016 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1019 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1020 RXCR1_RXBE | /* broadcast enable */
1021 RXCR1_RXE | /* RX process enable */
1022 RXCR1_RXFCE); /* enable flow control */
1024 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1026 /* schedule work to do the actual set of the data if needed */
1028 spin_lock(&ks->statelock);
1030 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1031 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1032 schedule_work(&ks->rxctrl_work);
1035 spin_unlock(&ks->statelock);
1038 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1040 struct sockaddr *sa = addr;
1042 if (netif_running(dev))
1043 return -EBUSY;
1045 if (!is_valid_ether_addr(sa->sa_data))
1046 return -EADDRNOTAVAIL;
1048 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1049 return ks8851_write_mac_addr(dev);
1052 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1054 struct ks8851_net *ks = netdev_priv(dev);
1056 if (!netif_running(dev))
1057 return -EINVAL;
1059 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1062 static const struct net_device_ops ks8851_netdev_ops = {
1063 .ndo_open = ks8851_net_open,
1064 .ndo_stop = ks8851_net_stop,
1065 .ndo_do_ioctl = ks8851_net_ioctl,
1066 .ndo_start_xmit = ks8851_start_xmit,
1067 .ndo_set_mac_address = ks8851_set_mac_address,
1068 .ndo_set_rx_mode = ks8851_set_rx_mode,
1069 .ndo_validate_addr = eth_validate_addr,
1072 /* ethtool support */
1074 static void ks8851_get_drvinfo(struct net_device *dev,
1075 struct ethtool_drvinfo *di)
1077 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1078 strlcpy(di->version, "1.00", sizeof(di->version));
1079 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1082 static u32 ks8851_get_msglevel(struct net_device *dev)
1084 struct ks8851_net *ks = netdev_priv(dev);
1085 return ks->msg_enable;
1088 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1090 struct ks8851_net *ks = netdev_priv(dev);
1091 ks->msg_enable = to;
1094 static int ks8851_get_link_ksettings(struct net_device *dev,
1095 struct ethtool_link_ksettings *cmd)
1097 struct ks8851_net *ks = netdev_priv(dev);
1099 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1101 return 0;
1104 static int ks8851_set_link_ksettings(struct net_device *dev,
1105 const struct ethtool_link_ksettings *cmd)
1107 struct ks8851_net *ks = netdev_priv(dev);
1108 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1111 static u32 ks8851_get_link(struct net_device *dev)
1113 struct ks8851_net *ks = netdev_priv(dev);
1114 return mii_link_ok(&ks->mii);
1117 static int ks8851_nway_reset(struct net_device *dev)
1119 struct ks8851_net *ks = netdev_priv(dev);
1120 return mii_nway_restart(&ks->mii);
1123 /* EEPROM support */
1125 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1127 struct ks8851_net *ks = ee->data;
1128 unsigned val;
1130 val = ks8851_rdreg16(ks, KS_EEPCR);
1132 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1133 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1134 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1137 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1139 struct ks8851_net *ks = ee->data;
1140 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1142 if (ee->drive_data)
1143 val |= EEPCR_EESRWA;
1144 if (ee->reg_data_in)
1145 val |= EEPCR_EEDO;
1146 if (ee->reg_data_clock)
1147 val |= EEPCR_EESCK;
1148 if (ee->reg_chip_select)
1149 val |= EEPCR_EECS;
1151 ks8851_wrreg16(ks, KS_EEPCR, val);
1155 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1156 * @ks: The network device state.
1158 * Check for the presence of an EEPROM, and then activate software access
1159 * to the device.
1161 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1163 if (!(ks->rc_ccr & CCR_EEPROM))
1164 return -ENOENT;
1166 mutex_lock(&ks->lock);
1168 /* start with clock low, cs high */
1169 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1170 return 0;
1174 * ks8851_eeprom_release - release the EEPROM interface
1175 * @ks: The device state
1177 * Release the software access to the device EEPROM
1179 static void ks8851_eeprom_release(struct ks8851_net *ks)
1181 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1183 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1184 mutex_unlock(&ks->lock);
1187 #define KS_EEPROM_MAGIC (0x00008851)
1189 static int ks8851_set_eeprom(struct net_device *dev,
1190 struct ethtool_eeprom *ee, u8 *data)
1192 struct ks8851_net *ks = netdev_priv(dev);
1193 int offset = ee->offset;
1194 int len = ee->len;
1195 u16 tmp;
1197 /* currently only support byte writing */
1198 if (len != 1)
1199 return -EINVAL;
1201 if (ee->magic != KS_EEPROM_MAGIC)
1202 return -EINVAL;
1204 if (ks8851_eeprom_claim(ks))
1205 return -ENOENT;
1207 eeprom_93cx6_wren(&ks->eeprom, true);
1209 /* ethtool currently only supports writing bytes, which means
1210 * we have to read/modify/write our 16bit EEPROMs */
1212 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1214 if (offset & 1) {
1215 tmp &= 0xff;
1216 tmp |= *data << 8;
1217 } else {
1218 tmp &= 0xff00;
1219 tmp |= *data;
1222 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1223 eeprom_93cx6_wren(&ks->eeprom, false);
1225 ks8851_eeprom_release(ks);
1227 return 0;
1230 static int ks8851_get_eeprom(struct net_device *dev,
1231 struct ethtool_eeprom *ee, u8 *data)
1233 struct ks8851_net *ks = netdev_priv(dev);
1234 int offset = ee->offset;
1235 int len = ee->len;
1237 /* must be 2 byte aligned */
1238 if (len & 1 || offset & 1)
1239 return -EINVAL;
1241 if (ks8851_eeprom_claim(ks))
1242 return -ENOENT;
1244 ee->magic = KS_EEPROM_MAGIC;
1246 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1247 ks8851_eeprom_release(ks);
1249 return 0;
1252 static int ks8851_get_eeprom_len(struct net_device *dev)
1254 struct ks8851_net *ks = netdev_priv(dev);
1256 /* currently, we assume it is an 93C46 attached, so return 128 */
1257 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1260 static const struct ethtool_ops ks8851_ethtool_ops = {
1261 .get_drvinfo = ks8851_get_drvinfo,
1262 .get_msglevel = ks8851_get_msglevel,
1263 .set_msglevel = ks8851_set_msglevel,
1264 .get_link = ks8851_get_link,
1265 .nway_reset = ks8851_nway_reset,
1266 .get_eeprom_len = ks8851_get_eeprom_len,
1267 .get_eeprom = ks8851_get_eeprom,
1268 .set_eeprom = ks8851_set_eeprom,
1269 .get_link_ksettings = ks8851_get_link_ksettings,
1270 .set_link_ksettings = ks8851_set_link_ksettings,
1273 /* MII interface controls */
1276 * ks8851_phy_reg - convert MII register into a KS8851 register
1277 * @reg: MII register number.
1279 * Return the KS8851 register number for the corresponding MII PHY register
1280 * if possible. Return zero if the MII register has no direct mapping to the
1281 * KS8851 register set.
1283 static int ks8851_phy_reg(int reg)
1285 switch (reg) {
1286 case MII_BMCR:
1287 return KS_P1MBCR;
1288 case MII_BMSR:
1289 return KS_P1MBSR;
1290 case MII_PHYSID1:
1291 return KS_PHY1ILR;
1292 case MII_PHYSID2:
1293 return KS_PHY1IHR;
1294 case MII_ADVERTISE:
1295 return KS_P1ANAR;
1296 case MII_LPA:
1297 return KS_P1ANLPR;
1300 return 0x0;
1304 * ks8851_phy_read - MII interface PHY register read.
1305 * @dev: The network device the PHY is on.
1306 * @phy_addr: Address of PHY (ignored as we only have one)
1307 * @reg: The register to read.
1309 * This call reads data from the PHY register specified in @reg. Since the
1310 * device does not support all the MII registers, the non-existent values
1311 * are always returned as zero.
1313 * We return zero for unsupported registers as the MII code does not check
1314 * the value returned for any error status, and simply returns it to the
1315 * caller. The mii-tool that the driver was tested with takes any -ve error
1316 * as real PHY capabilities, thus displaying incorrect data to the user.
1318 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1320 struct ks8851_net *ks = netdev_priv(dev);
1321 int ksreg;
1322 int result;
1324 ksreg = ks8851_phy_reg(reg);
1325 if (!ksreg)
1326 return 0x0; /* no error return allowed, so use zero */
1328 mutex_lock(&ks->lock);
1329 result = ks8851_rdreg16(ks, ksreg);
1330 mutex_unlock(&ks->lock);
1332 return result;
1335 static void ks8851_phy_write(struct net_device *dev,
1336 int phy, int reg, int value)
1338 struct ks8851_net *ks = netdev_priv(dev);
1339 int ksreg;
1341 ksreg = ks8851_phy_reg(reg);
1342 if (ksreg) {
1343 mutex_lock(&ks->lock);
1344 ks8851_wrreg16(ks, ksreg, value);
1345 mutex_unlock(&ks->lock);
1350 * ks8851_read_selftest - read the selftest memory info.
1351 * @ks: The device state
1353 * Read and check the TX/RX memory selftest information.
1355 static int ks8851_read_selftest(struct ks8851_net *ks)
1357 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1358 int ret = 0;
1359 unsigned rd;
1361 rd = ks8851_rdreg16(ks, KS_MBIR);
1363 if ((rd & both_done) != both_done) {
1364 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1365 return 0;
1368 if (rd & MBIR_TXMBFA) {
1369 netdev_err(ks->netdev, "TX memory selftest fail\n");
1370 ret |= 1;
1373 if (rd & MBIR_RXMBFA) {
1374 netdev_err(ks->netdev, "RX memory selftest fail\n");
1375 ret |= 2;
1378 return 0;
1381 /* driver bus management functions */
1383 #ifdef CONFIG_PM_SLEEP
1385 static int ks8851_suspend(struct device *dev)
1387 struct ks8851_net *ks = dev_get_drvdata(dev);
1388 struct net_device *netdev = ks->netdev;
1390 if (netif_running(netdev)) {
1391 netif_device_detach(netdev);
1392 ks8851_net_stop(netdev);
1395 return 0;
1398 static int ks8851_resume(struct device *dev)
1400 struct ks8851_net *ks = dev_get_drvdata(dev);
1401 struct net_device *netdev = ks->netdev;
1403 if (netif_running(netdev)) {
1404 ks8851_net_open(netdev);
1405 netif_device_attach(netdev);
1408 return 0;
1410 #endif
1412 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1414 static int ks8851_probe(struct spi_device *spi)
1416 struct net_device *ndev;
1417 struct ks8851_net *ks;
1418 int ret;
1419 unsigned cider;
1420 int gpio;
1422 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1423 if (!ndev)
1424 return -ENOMEM;
1426 spi->bits_per_word = 8;
1428 ks = netdev_priv(ndev);
1430 ks->netdev = ndev;
1431 ks->spidev = spi;
1432 ks->tx_space = 6144;
1434 gpio = of_get_named_gpio_flags(spi->dev.of_node, "reset-gpios",
1435 0, NULL);
1436 if (gpio == -EPROBE_DEFER) {
1437 ret = gpio;
1438 goto err_gpio;
1441 ks->gpio = gpio;
1442 if (gpio_is_valid(gpio)) {
1443 ret = devm_gpio_request_one(&spi->dev, gpio,
1444 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1445 if (ret) {
1446 dev_err(&spi->dev, "reset gpio request failed\n");
1447 goto err_gpio;
1451 ks->vdd_io = devm_regulator_get(&spi->dev, "vdd-io");
1452 if (IS_ERR(ks->vdd_io)) {
1453 ret = PTR_ERR(ks->vdd_io);
1454 goto err_reg_io;
1457 ret = regulator_enable(ks->vdd_io);
1458 if (ret) {
1459 dev_err(&spi->dev, "regulator vdd_io enable fail: %d\n",
1460 ret);
1461 goto err_reg_io;
1464 ks->vdd_reg = devm_regulator_get(&spi->dev, "vdd");
1465 if (IS_ERR(ks->vdd_reg)) {
1466 ret = PTR_ERR(ks->vdd_reg);
1467 goto err_reg;
1470 ret = regulator_enable(ks->vdd_reg);
1471 if (ret) {
1472 dev_err(&spi->dev, "regulator vdd enable fail: %d\n",
1473 ret);
1474 goto err_reg;
1477 if (gpio_is_valid(gpio)) {
1478 usleep_range(10000, 11000);
1479 gpio_set_value(gpio, 1);
1482 mutex_init(&ks->lock);
1483 spin_lock_init(&ks->statelock);
1485 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1486 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1488 /* initialise pre-made spi transfer messages */
1490 spi_message_init(&ks->spi_msg1);
1491 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1493 spi_message_init(&ks->spi_msg2);
1494 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1495 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1497 /* setup EEPROM state */
1499 ks->eeprom.data = ks;
1500 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1501 ks->eeprom.register_read = ks8851_eeprom_regread;
1502 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1504 /* setup mii state */
1505 ks->mii.dev = ndev;
1506 ks->mii.phy_id = 1,
1507 ks->mii.phy_id_mask = 1;
1508 ks->mii.reg_num_mask = 0xf;
1509 ks->mii.mdio_read = ks8851_phy_read;
1510 ks->mii.mdio_write = ks8851_phy_write;
1512 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1514 /* set the default message enable */
1515 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1516 NETIF_MSG_PROBE |
1517 NETIF_MSG_LINK));
1519 skb_queue_head_init(&ks->txq);
1521 ndev->ethtool_ops = &ks8851_ethtool_ops;
1522 SET_NETDEV_DEV(ndev, &spi->dev);
1524 spi_set_drvdata(spi, ks);
1526 netif_carrier_off(ks->netdev);
1527 ndev->if_port = IF_PORT_100BASET;
1528 ndev->netdev_ops = &ks8851_netdev_ops;
1529 ndev->irq = spi->irq;
1531 /* issue a global soft reset to reset the device. */
1532 ks8851_soft_reset(ks, GRR_GSR);
1534 /* simple check for a valid chip being connected to the bus */
1535 cider = ks8851_rdreg16(ks, KS_CIDER);
1536 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1537 dev_err(&spi->dev, "failed to read device ID\n");
1538 ret = -ENODEV;
1539 goto err_id;
1542 /* cache the contents of the CCR register for EEPROM, etc. */
1543 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1545 ks8851_read_selftest(ks);
1546 ks8851_init_mac(ks);
1548 ret = register_netdev(ndev);
1549 if (ret) {
1550 dev_err(&spi->dev, "failed to register network device\n");
1551 goto err_netdev;
1554 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1555 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
1556 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1558 return 0;
1560 err_netdev:
1561 err_id:
1562 if (gpio_is_valid(gpio))
1563 gpio_set_value(gpio, 0);
1564 regulator_disable(ks->vdd_reg);
1565 err_reg:
1566 regulator_disable(ks->vdd_io);
1567 err_reg_io:
1568 err_gpio:
1569 free_netdev(ndev);
1570 return ret;
1573 static int ks8851_remove(struct spi_device *spi)
1575 struct ks8851_net *priv = spi_get_drvdata(spi);
1577 if (netif_msg_drv(priv))
1578 dev_info(&spi->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);
1587 return 0;
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 = {
1597 .driver = {
1598 .name = "ks8851",
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");