net: ks8851: Factor out TX work flush function
[linux-2.6-microblaze.git] / drivers / net / ethernet / micrel / ks8851.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/net/ethernet/micrel/ks8851.c
3  *
4  * Copyright 2009 Simtec Electronics
5  *      http://www.simtec.co.uk/
6  *      Ben Dooks <ben@simtec.co.uk>
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #define DEBUG
12
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>
24
25 #include <linux/spi/spi.h>
26 #include <linux/gpio.h>
27 #include <linux/of_gpio.h>
28 #include <linux/of_net.h>
29
30 #include "ks8851.h"
31
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
37  *
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.
43  */
44 struct ks8851_rxctrl {
45         u16     mchash[4];
46         u16     rxcr1;
47         u16     rxcr2;
48 };
49
50 /**
51  * union ks8851_tx_hdr - tx header data
52  * @txb: The header as bytes
53  * @txw: The header as 16bit, little-endian words
54  *
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.
58  */
59 union ks8851_tx_hdr {
60         u8      txb[6];
61         __le16  txw[3];
62 };
63
64 /**
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
84  *
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.
88  *
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
91  * modifies.
92  */
93 struct ks8851_net {
94         struct net_device       *netdev;
95         spinlock_t              statelock;
96
97         union ks8851_tx_hdr     txh ____cacheline_aligned;
98         u8                      rxd[8];
99         u8                      txd[8];
100
101         u32                     msg_enable ____cacheline_aligned;
102         u16                     tx_space;
103         u8                      fid;
104
105         u16                     rc_ier;
106         u16                     rc_rxqcr;
107         u16                     rc_ccr;
108
109         struct mii_if_info      mii;
110         struct ks8851_rxctrl    rxctrl;
111
112         struct work_struct      rxctrl_work;
113
114         struct sk_buff_head     txq;
115
116         struct eeprom_93cx6     eeprom;
117         struct regulator        *vdd_reg;
118         struct regulator        *vdd_io;
119         int                     gpio;
120 };
121
122 /**
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.
130  *
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.
135  */
136 struct ks8851_net_spi {
137         struct ks8851_net       ks8851;
138         struct mutex            lock;
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];
145 };
146
147 #define to_ks8851_spi(ks) container_of((ks), struct ks8851_net_spi, ks8851)
148
149 static int msg_enable;
150
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)
156
157 /* shift for byte-enable data */
158 #define BYTE_EN(_x)     ((_x) << 2)
159
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)
162
163 /**
164  * ks8851_lock - register access lock
165  * @ks: The chip state
166  * @flags: Spinlock flags
167  *
168  * Claim chip register access lock
169  */
170 static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
171 {
172         struct ks8851_net_spi *kss = to_ks8851_spi(ks);
173
174         mutex_lock(&kss->lock);
175 }
176
177 /**
178  * ks8851_unlock - register access unlock
179  * @ks: The chip state
180  * @flags: Spinlock flags
181  *
182  * Release chip register access lock
183  */
184 static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
185 {
186         struct ks8851_net_spi *kss = to_ks8851_spi(ks);
187
188         mutex_unlock(&kss->lock);
189 }
190
191 /* SPI register read/write calls.
192  *
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).
196  */
197
198 /**
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
203  *
204  * Issue a write to put the value @val into the register specified in @reg.
205  */
206 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
207 {
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;
211         __le16 txb[2];
212         int ret;
213
214         txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
215         txb[1] = cpu_to_le16(val);
216
217         xfer->tx_buf = txb;
218         xfer->rx_buf = NULL;
219         xfer->len = 4;
220
221         ret = spi_sync(kss->spidev, msg);
222         if (ret < 0)
223                 netdev_err(ks->netdev, "spi_sync() failed\n");
224 }
225
226 /**
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.
232  *
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.
235  */
236 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
237                          u8 *rxb, unsigned rxl)
238 {
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;
243         u8 *trx = ks->rxd;
244         int ret;
245
246         txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
247
248         if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
249                 msg = &kss->spi_msg2;
250                 xfer = kss->spi_xfer2;
251
252                 xfer->tx_buf = txb;
253                 xfer->rx_buf = NULL;
254                 xfer->len = 2;
255
256                 xfer++;
257                 xfer->tx_buf = NULL;
258                 xfer->rx_buf = trx;
259                 xfer->len = rxl;
260         } else {
261                 msg = &kss->spi_msg1;
262                 xfer = &kss->spi_xfer1;
263
264                 xfer->tx_buf = txb;
265                 xfer->rx_buf = trx;
266                 xfer->len = rxl + 2;
267         }
268
269         ret = spi_sync(kss->spidev, msg);
270         if (ret < 0)
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);
274         else
275                 memcpy(rxb, trx + 2, rxl);
276 }
277
278 /**
279  * ks8851_rdreg16 - read 16 bit register from device
280  * @ks: The chip information
281  * @reg: The register address
282  *
283  * Read a 16bit register from the chip, returning the result
284 */
285 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
286 {
287         __le16 rx = 0;
288
289         ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
290         return le16_to_cpu(rx);
291 }
292
293 /**
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
297  *
298  * Issue the relevant soft-reset command to the device's GRR register
299  * specified by @op.
300  *
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.
305  */
306 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
307 {
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 */
312 }
313
314 /**
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.
318  *
319  * Change the power mode of the chip.
320  */
321 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
322 {
323         unsigned pmecr;
324
325         netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
326
327         pmecr = ks8851_rdreg16(ks, KS_PMECR);
328         pmecr &= ~PMECR_PM_MASK;
329         pmecr |= pwrmode;
330
331         ks8851_wrreg16(ks, KS_PMECR, pmecr);
332 }
333
334 /**
335  * ks8851_write_mac_addr - write mac address to device registers
336  * @dev: The network device
337  *
338  * Update the KS8851 MAC address registers from the address in @dev.
339  *
340  * This call assumes that the chip is not running, so there is no need to
341  * shutdown the RXQ process whilst setting this.
342 */
343 static int ks8851_write_mac_addr(struct net_device *dev)
344 {
345         struct ks8851_net *ks = netdev_priv(dev);
346         unsigned long flags;
347         u16 val;
348         int i;
349
350         ks8851_lock(ks, &flags);
351
352         /*
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.
355          */
356         ks8851_set_powermode(ks, PMECR_PM_NORMAL);
357
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);
361         }
362
363         if (!netif_running(dev))
364                 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
365
366         ks8851_unlock(ks, &flags);
367
368         return 0;
369 }
370
371 /**
372  * ks8851_read_mac_addr - read mac address from device registers
373  * @dev: The network device
374  *
375  * Update our copy of the KS8851 MAC address from the registers of @dev.
376 */
377 static void ks8851_read_mac_addr(struct net_device *dev)
378 {
379         struct ks8851_net *ks = netdev_priv(dev);
380         unsigned long flags;
381         u16 reg;
382         int i;
383
384         ks8851_lock(ks, &flags);
385
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;
390         }
391
392         ks8851_unlock(ks, &flags);
393 }
394
395 /**
396  * ks8851_init_mac - initialise the mac address
397  * @ks: The device structure
398  * @np: The device node pointer
399  *
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.
405  */
406 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
407 {
408         struct net_device *dev = ks->netdev;
409         const u8 *mac_addr;
410
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);
415                 return;
416         }
417
418         if (ks->rc_ccr & CCR_EEPROM) {
419                 ks8851_read_mac_addr(dev);
420                 if (is_valid_ether_addr(dev->dev_addr))
421                         return;
422
423                 netdev_err(ks->netdev, "invalid mac address read %pM\n",
424                                 dev->dev_addr);
425         }
426
427         eth_hw_addr_random(dev);
428         ks8851_write_mac_addr(dev);
429 }
430
431 /**
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
436  *
437  * Issue an RXQ FIFO read command and read the @len amount of data from
438  * the FIFO into the buffer specified by @buff.
439  */
440 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
441 {
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;
445         u8 txb[1];
446         int ret;
447
448         netif_dbg(ks, rx_status, ks->netdev,
449                   "%s: %d@%p\n", __func__, len, buff);
450
451         /* set the operation we're issuing */
452         txb[0] = KS_SPIOP_RXFIFO;
453
454         xfer->tx_buf = txb;
455         xfer->rx_buf = NULL;
456         xfer->len = 1;
457
458         xfer++;
459         xfer->rx_buf = buff;
460         xfer->tx_buf = NULL;
461         xfer->len = len;
462
463         ret = spi_sync(kss->spidev, msg);
464         if (ret < 0)
465                 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
466 }
467
468 /**
469  * ks8851_dbg_dumpkkt - dump initial packet contents to debug
470  * @ks: The device state
471  * @rxpkt: The data for the received packet
472  *
473  * Dump the initial data from the packet to dev_dbg().
474 */
475 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
476 {
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]);
482 }
483
484 /**
485  * ks8851_rx_skb - receive skbuff
486  * @skb: The skbuff
487  */
488 static void ks8851_rx_skb(struct sk_buff *skb)
489 {
490         netif_rx_ni(skb);
491 }
492
493 /**
494  * ks8851_rx_pkts - receive packets from the host
495  * @ks: The device information.
496  *
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.
500  */
501 static void ks8851_rx_pkts(struct ks8851_net *ks)
502 {
503         struct sk_buff *skb;
504         unsigned rxfc;
505         unsigned rxlen;
506         unsigned rxstat;
507         u8 *rxpkt;
508
509         rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
510
511         netif_dbg(ks, rx_status, ks->netdev,
512                   "%s: %d packets\n", __func__, rxfc);
513
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
517          * out in one go.
518          *
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.
522          */
523
524         for (; rxfc != 0; rxfc--) {
525                 rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
526                 rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
527
528                 netif_dbg(ks, rx_status, ks->netdev,
529                           "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
530
531                 /* the length of the packet includes the 32bit CRC */
532
533                 /* set dma read address */
534                 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
535
536                 /* start DMA access */
537                 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
538
539                 if (rxlen > 4) {
540                         unsigned int rxalign;
541
542                         rxlen -= 4;
543                         rxalign = ALIGN(rxlen, 4);
544                         skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
545                         if (skb) {
546
547                                 /* 4 bytes of status header + 4 bytes of
548                                  * garbage: we put them before ethernet
549                                  * header, so that they are copied,
550                                  * but ignored.
551                                  */
552
553                                 rxpkt = skb_put(skb, rxlen) - 8;
554
555                                 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
556
557                                 if (netif_msg_pktdata(ks))
558                                         ks8851_dbg_dumpkkt(ks, rxpkt);
559
560                                 skb->protocol = eth_type_trans(skb, ks->netdev);
561                                 ks8851_rx_skb(skb);
562
563                                 ks->netdev->stats.rx_packets++;
564                                 ks->netdev->stats.rx_bytes += rxlen;
565                         }
566                 }
567
568                 /* end DMA access and dequeue packet */
569                 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
570         }
571 }
572
573 /**
574  * ks8851_irq - IRQ handler for dealing with interrupt requests
575  * @irq: IRQ number
576  * @_ks: cookie
577  *
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
580  * in thread context.
581  *
582  * Read the interrupt status, work out what needs to be done and then clear
583  * any of the interrupts that are not needed.
584  */
585 static irqreturn_t ks8851_irq(int irq, void *_ks)
586 {
587         struct ks8851_net *ks = _ks;
588         unsigned handled = 0;
589         unsigned long flags;
590         unsigned int status;
591
592         ks8851_lock(ks, &flags);
593
594         status = ks8851_rdreg16(ks, KS_ISR);
595
596         netif_dbg(ks, intr, ks->netdev,
597                   "%s: status 0x%04x\n", __func__, status);
598
599         if (status & IRQ_LCI)
600                 handled |= IRQ_LCI;
601
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);
606
607                 handled |= IRQ_LDI;
608         }
609
610         if (status & IRQ_RXPSI)
611                 handled |= IRQ_RXPSI;
612
613         if (status & IRQ_TXI) {
614                 handled |= IRQ_TXI;
615
616                 /* no lock here, tx queue should have been stopped */
617
618                 /* update our idea of how much tx space is available to the
619                  * system */
620                 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
621
622                 netif_dbg(ks, intr, ks->netdev,
623                           "%s: txspace %d\n", __func__, ks->tx_space);
624         }
625
626         if (status & IRQ_RXI)
627                 handled |= IRQ_RXI;
628
629         if (status & IRQ_SPIBEI) {
630                 netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
631                 handled |= IRQ_SPIBEI;
632         }
633
634         ks8851_wrreg16(ks, KS_ISR, handled);
635
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. */
641
642                 ks8851_rx_pkts(ks);
643         }
644
645         /* if something stopped the rx process, probably due to wanting
646          * to change the rx settings, then do something about restarting
647          * it. */
648         if (status & IRQ_RXPSI) {
649                 struct ks8851_rxctrl *rxc = &ks->rxctrl;
650
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]);
656
657                 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
658                 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
659         }
660
661         ks8851_unlock(ks, &flags);
662
663         if (status & IRQ_LCI)
664                 mii_check_link(&ks->mii);
665
666         if (status & IRQ_TXI)
667                 netif_wake_queue(ks->netdev);
668
669         return IRQ_HANDLED;
670 }
671
672 /**
673  * calc_txlen - calculate size of message to send packet
674  * @len: Length of data
675  *
676  * Returns the size of the TXFIFO message needed to send
677  * this packet.
678  */
679 static inline unsigned calc_txlen(unsigned len)
680 {
681         return ALIGN(len + 4, 4);
682 }
683
684 /**
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.
689  *
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
693  * the device.
694  */
695 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
696 {
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;
700         unsigned fid = 0;
701         int ret;
702
703         netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
704                   __func__, txp, txp->len, txp->data, irq);
705
706         fid = ks->fid++;
707         fid &= TXFR_TXFID_MASK;
708
709         if (irq)
710                 fid |= TXFR_TXIC;       /* irq on completion */
711
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);
716
717         xfer->tx_buf = &ks->txh.txb[1];
718         xfer->rx_buf = NULL;
719         xfer->len = 5;
720
721         xfer++;
722         xfer->tx_buf = txp->data;
723         xfer->rx_buf = NULL;
724         xfer->len = ALIGN(txp->len, 4);
725
726         ret = spi_sync(kss->spidev, msg);
727         if (ret < 0)
728                 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
729 }
730
731 /**
732  * ks8851_done_tx - update and then free skbuff after transmitting
733  * @ks: The device state
734  * @txb: The buffer transmitted
735  */
736 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
737 {
738         struct net_device *dev = ks->netdev;
739
740         dev->stats.tx_bytes += txb->len;
741         dev->stats.tx_packets++;
742
743         dev_kfree_skb(txb);
744 }
745
746 /**
747  * ks8851_tx_work - process tx packet(s)
748  * @work: The work strucutre what was scheduled.
749  *
750  * This is called when a number of packets have been scheduled for
751  * transmission and need to be sent to the device.
752  */
753 static void ks8851_tx_work(struct work_struct *work)
754 {
755         struct ks8851_net_spi *kss;
756         struct ks8851_net *ks;
757         unsigned long flags;
758         struct sk_buff *txb;
759         bool last;
760
761         kss = container_of(work, struct ks8851_net_spi, tx_work);
762         ks = &kss->ks8851;
763         last = skb_queue_empty(&ks->txq);
764
765         ks8851_lock(ks, &flags);
766
767         while (!last) {
768                 txb = skb_dequeue(&ks->txq);
769                 last = skb_queue_empty(&ks->txq);
770
771                 if (txb != NULL) {
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);
776
777                         ks8851_done_tx(ks, txb);
778                 }
779         }
780
781         ks8851_unlock(ks, &flags);
782 }
783
784 /**
785  * ks8851_flush_tx_work - flush outstanding TX work
786  * @ks: The device state
787  */
788 static void ks8851_flush_tx_work(struct ks8851_net *ks)
789 {
790         struct ks8851_net_spi *kss = to_ks8851_spi(ks);
791
792         flush_work(&kss->tx_work);
793 }
794
795 /**
796  * ks8851_net_open - open network device
797  * @dev: The network device being opened.
798  *
799  * Called when the network device is marked active, such as a user executing
800  * 'ifconfig up' on the device.
801  */
802 static int ks8851_net_open(struct net_device *dev)
803 {
804         struct ks8851_net *ks = netdev_priv(dev);
805         unsigned long flags;
806         int ret;
807
808         ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
809                                    IRQF_TRIGGER_LOW | IRQF_ONESHOT,
810                                    dev->name, ks);
811         if (ret < 0) {
812                 netdev_err(dev, "failed to get irq\n");
813                 return ret;
814         }
815
816         /* lock the card, even if we may not actually be doing anything
817          * else at the moment */
818         ks8851_lock(ks, &flags);
819
820         netif_dbg(ks, ifup, ks->netdev, "opening\n");
821
822         /* bring chip out of any power saving mode it was in */
823         ks8851_set_powermode(ks, PMECR_PM_NORMAL);
824
825         /* issue a soft reset to the RX/TX QMU to put it into a known
826          * state. */
827         ks8851_soft_reset(ks, GRR_QMU);
828
829         /* setup transmission parameters */
830
831         ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
832                                      TXCR_TXPE | /* pad to min length */
833                                      TXCR_TXCRC | /* add CRC */
834                                      TXCR_TXFCE)); /* enable flow control */
835
836         /* auto-increment tx data, reset tx pointer */
837         ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
838
839         /* setup receiver control */
840
841         ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /*  from mac filter */
842                                       RXCR1_RXFCE | /* enable flow control */
843                                       RXCR1_RXBE | /* broadcast enable */
844                                       RXCR1_RXUE | /* unicast enable */
845                                       RXCR1_RXE)); /* enable rx block */
846
847         /* transfer entire frames out in one go */
848         ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
849
850         /* set receive counter timeouts */
851         ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
852         ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
853         ks8851_wrreg16(ks, KS_RXFCTR, 10);  /* 10 frames to IRQ */
854
855         ks->rc_rxqcr = (RXQCR_RXFCTE |  /* IRQ on frame count exceeded */
856                         RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
857                         RXQCR_RXDTTE);  /* IRQ on time exceeded */
858
859         ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
860
861         /* clear then enable interrupts */
862
863 #define STD_IRQ (IRQ_LCI |      /* Link Change */       \
864                  IRQ_TXI |      /* TX done */           \
865                  IRQ_RXI |      /* RX done */           \
866                  IRQ_SPIBEI |   /* SPI bus error */     \
867                  IRQ_TXPSI |    /* TX process stop */   \
868                  IRQ_RXPSI)     /* RX process stop */
869
870         ks->rc_ier = STD_IRQ;
871         ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
872         ks8851_wrreg16(ks, KS_IER, STD_IRQ);
873
874         netif_start_queue(ks->netdev);
875
876         netif_dbg(ks, ifup, ks->netdev, "network device up\n");
877
878         ks8851_unlock(ks, &flags);
879         mii_check_link(&ks->mii);
880         return 0;
881 }
882
883 /**
884  * ks8851_net_stop - close network device
885  * @dev: The device being closed.
886  *
887  * Called to close down a network device which has been active. Cancell any
888  * work, shutdown the RX and TX process and then place the chip into a low
889  * power state whilst it is not being used.
890  */
891 static int ks8851_net_stop(struct net_device *dev)
892 {
893         struct ks8851_net *ks = netdev_priv(dev);
894         unsigned long flags;
895
896         netif_info(ks, ifdown, dev, "shutting down\n");
897
898         netif_stop_queue(dev);
899
900         ks8851_lock(ks, &flags);
901         /* turn off the IRQs and ack any outstanding */
902         ks8851_wrreg16(ks, KS_IER, 0x0000);
903         ks8851_wrreg16(ks, KS_ISR, 0xffff);
904         ks8851_unlock(ks, &flags);
905
906         /* stop any outstanding work */
907         ks8851_flush_tx_work(ks);
908         flush_work(&ks->rxctrl_work);
909
910         ks8851_lock(ks, &flags);
911         /* shutdown RX process */
912         ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
913
914         /* shutdown TX process */
915         ks8851_wrreg16(ks, KS_TXCR, 0x0000);
916
917         /* set powermode to soft power down to save power */
918         ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
919         ks8851_unlock(ks, &flags);
920
921         /* ensure any queued tx buffers are dumped */
922         while (!skb_queue_empty(&ks->txq)) {
923                 struct sk_buff *txb = skb_dequeue(&ks->txq);
924
925                 netif_dbg(ks, ifdown, ks->netdev,
926                           "%s: freeing txb %p\n", __func__, txb);
927
928                 dev_kfree_skb(txb);
929         }
930
931         free_irq(dev->irq, ks);
932
933         return 0;
934 }
935
936 /**
937  * ks8851_start_xmit - transmit packet
938  * @skb: The buffer to transmit
939  * @dev: The device used to transmit the packet.
940  *
941  * Called by the network layer to transmit the @skb. Queue the packet for
942  * the device and schedule the necessary work to transmit the packet when
943  * it is free.
944  *
945  * We do this to firstly avoid sleeping with the network device locked,
946  * and secondly so we can round up more than one packet to transmit which
947  * means we can try and avoid generating too many transmit done interrupts.
948  */
949 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
950                                      struct net_device *dev)
951 {
952         struct ks8851_net *ks = netdev_priv(dev);
953         unsigned needed = calc_txlen(skb->len);
954         netdev_tx_t ret = NETDEV_TX_OK;
955         struct ks8851_net_spi *kss;
956
957         kss = to_ks8851_spi(ks);
958
959         netif_dbg(ks, tx_queued, ks->netdev,
960                   "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
961
962         spin_lock(&ks->statelock);
963
964         if (needed > ks->tx_space) {
965                 netif_stop_queue(dev);
966                 ret = NETDEV_TX_BUSY;
967         } else {
968                 ks->tx_space -= needed;
969                 skb_queue_tail(&ks->txq, skb);
970         }
971
972         spin_unlock(&ks->statelock);
973         schedule_work(&kss->tx_work);
974
975         return ret;
976 }
977
978 /**
979  * ks8851_rxctrl_work - work handler to change rx mode
980  * @work: The work structure this belongs to.
981  *
982  * Lock the device and issue the necessary changes to the receive mode from
983  * the network device layer. This is done so that we can do this without
984  * having to sleep whilst holding the network device lock.
985  *
986  * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
987  * receive parameters are programmed, we issue a write to disable the RXQ and
988  * then wait for the interrupt handler to be triggered once the RXQ shutdown is
989  * complete. The interrupt handler then writes the new values into the chip.
990  */
991 static void ks8851_rxctrl_work(struct work_struct *work)
992 {
993         struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
994         unsigned long flags;
995
996         ks8851_lock(ks, &flags);
997
998         /* need to shutdown RXQ before modifying filter parameters */
999         ks8851_wrreg16(ks, KS_RXCR1, 0x00);
1000
1001         ks8851_unlock(ks, &flags);
1002 }
1003
1004 static void ks8851_set_rx_mode(struct net_device *dev)
1005 {
1006         struct ks8851_net *ks = netdev_priv(dev);
1007         struct ks8851_rxctrl rxctrl;
1008
1009         memset(&rxctrl, 0, sizeof(rxctrl));
1010
1011         if (dev->flags & IFF_PROMISC) {
1012                 /* interface to receive everything */
1013
1014                 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
1015         } else if (dev->flags & IFF_ALLMULTI) {
1016                 /* accept all multicast packets */
1017
1018                 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1019                                 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
1020         } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
1021                 struct netdev_hw_addr *ha;
1022                 u32 crc;
1023
1024                 /* accept some multicast */
1025
1026                 netdev_for_each_mc_addr(ha, dev) {
1027                         crc = ether_crc(ETH_ALEN, ha->addr);
1028                         crc >>= (32 - 6);  /* get top six bits */
1029
1030                         rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1031                 }
1032
1033                 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1034         } else {
1035                 /* just accept broadcast / unicast */
1036                 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1037         }
1038
1039         rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1040                          RXCR1_RXBE | /* broadcast enable */
1041                          RXCR1_RXE | /* RX process enable */
1042                          RXCR1_RXFCE); /* enable flow control */
1043
1044         rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1045
1046         /* schedule work to do the actual set of the data if needed */
1047
1048         spin_lock(&ks->statelock);
1049
1050         if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1051                 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1052                 schedule_work(&ks->rxctrl_work);
1053         }
1054
1055         spin_unlock(&ks->statelock);
1056 }
1057
1058 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1059 {
1060         struct sockaddr *sa = addr;
1061
1062         if (netif_running(dev))
1063                 return -EBUSY;
1064
1065         if (!is_valid_ether_addr(sa->sa_data))
1066                 return -EADDRNOTAVAIL;
1067
1068         memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1069         return ks8851_write_mac_addr(dev);
1070 }
1071
1072 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1073 {
1074         struct ks8851_net *ks = netdev_priv(dev);
1075
1076         if (!netif_running(dev))
1077                 return -EINVAL;
1078
1079         return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1080 }
1081
1082 static const struct net_device_ops ks8851_netdev_ops = {
1083         .ndo_open               = ks8851_net_open,
1084         .ndo_stop               = ks8851_net_stop,
1085         .ndo_do_ioctl           = ks8851_net_ioctl,
1086         .ndo_start_xmit         = ks8851_start_xmit,
1087         .ndo_set_mac_address    = ks8851_set_mac_address,
1088         .ndo_set_rx_mode        = ks8851_set_rx_mode,
1089         .ndo_validate_addr      = eth_validate_addr,
1090 };
1091
1092 /* ethtool support */
1093
1094 static void ks8851_get_drvinfo(struct net_device *dev,
1095                                struct ethtool_drvinfo *di)
1096 {
1097         strlcpy(di->driver, "KS8851", sizeof(di->driver));
1098         strlcpy(di->version, "1.00", sizeof(di->version));
1099         strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1100 }
1101
1102 static u32 ks8851_get_msglevel(struct net_device *dev)
1103 {
1104         struct ks8851_net *ks = netdev_priv(dev);
1105         return ks->msg_enable;
1106 }
1107
1108 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1109 {
1110         struct ks8851_net *ks = netdev_priv(dev);
1111         ks->msg_enable = to;
1112 }
1113
1114 static int ks8851_get_link_ksettings(struct net_device *dev,
1115                                      struct ethtool_link_ksettings *cmd)
1116 {
1117         struct ks8851_net *ks = netdev_priv(dev);
1118
1119         mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1120
1121         return 0;
1122 }
1123
1124 static int ks8851_set_link_ksettings(struct net_device *dev,
1125                                      const struct ethtool_link_ksettings *cmd)
1126 {
1127         struct ks8851_net *ks = netdev_priv(dev);
1128         return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1129 }
1130
1131 static u32 ks8851_get_link(struct net_device *dev)
1132 {
1133         struct ks8851_net *ks = netdev_priv(dev);
1134         return mii_link_ok(&ks->mii);
1135 }
1136
1137 static int ks8851_nway_reset(struct net_device *dev)
1138 {
1139         struct ks8851_net *ks = netdev_priv(dev);
1140         return mii_nway_restart(&ks->mii);
1141 }
1142
1143 /* EEPROM support */
1144
1145 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1146 {
1147         struct ks8851_net *ks = ee->data;
1148         unsigned val;
1149
1150         val = ks8851_rdreg16(ks, KS_EEPCR);
1151
1152         ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1153         ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1154         ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1155 }
1156
1157 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1158 {
1159         struct ks8851_net *ks = ee->data;
1160         unsigned val = EEPCR_EESA;      /* default - eeprom access on */
1161
1162         if (ee->drive_data)
1163                 val |= EEPCR_EESRWA;
1164         if (ee->reg_data_in)
1165                 val |= EEPCR_EEDO;
1166         if (ee->reg_data_clock)
1167                 val |= EEPCR_EESCK;
1168         if (ee->reg_chip_select)
1169                 val |= EEPCR_EECS;
1170
1171         ks8851_wrreg16(ks, KS_EEPCR, val);
1172 }
1173
1174 /**
1175  * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1176  * @ks: The network device state.
1177  *
1178  * Check for the presence of an EEPROM, and then activate software access
1179  * to the device.
1180  */
1181 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1182 {
1183         /* start with clock low, cs high */
1184         ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1185         return 0;
1186 }
1187
1188 /**
1189  * ks8851_eeprom_release - release the EEPROM interface
1190  * @ks: The device state
1191  *
1192  * Release the software access to the device EEPROM
1193  */
1194 static void ks8851_eeprom_release(struct ks8851_net *ks)
1195 {
1196         unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1197
1198         ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1199 }
1200
1201 #define KS_EEPROM_MAGIC (0x00008851)
1202
1203 static int ks8851_set_eeprom(struct net_device *dev,
1204                              struct ethtool_eeprom *ee, u8 *data)
1205 {
1206         struct ks8851_net *ks = netdev_priv(dev);
1207         int offset = ee->offset;
1208         unsigned long flags;
1209         int len = ee->len;
1210         u16 tmp;
1211
1212         /* currently only support byte writing */
1213         if (len != 1)
1214                 return -EINVAL;
1215
1216         if (ee->magic != KS_EEPROM_MAGIC)
1217                 return -EINVAL;
1218
1219         if (!(ks->rc_ccr & CCR_EEPROM))
1220                 return -ENOENT;
1221
1222         ks8851_lock(ks, &flags);
1223
1224         ks8851_eeprom_claim(ks);
1225
1226         eeprom_93cx6_wren(&ks->eeprom, true);
1227
1228         /* ethtool currently only supports writing bytes, which means
1229          * we have to read/modify/write our 16bit EEPROMs */
1230
1231         eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1232
1233         if (offset & 1) {
1234                 tmp &= 0xff;
1235                 tmp |= *data << 8;
1236         } else {
1237                 tmp &= 0xff00;
1238                 tmp |= *data;
1239         }
1240
1241         eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1242         eeprom_93cx6_wren(&ks->eeprom, false);
1243
1244         ks8851_eeprom_release(ks);
1245         ks8851_unlock(ks, &flags);
1246
1247         return 0;
1248 }
1249
1250 static int ks8851_get_eeprom(struct net_device *dev,
1251                              struct ethtool_eeprom *ee, u8 *data)
1252 {
1253         struct ks8851_net *ks = netdev_priv(dev);
1254         int offset = ee->offset;
1255         unsigned long flags;
1256         int len = ee->len;
1257
1258         /* must be 2 byte aligned */
1259         if (len & 1 || offset & 1)
1260                 return -EINVAL;
1261
1262         if (!(ks->rc_ccr & CCR_EEPROM))
1263                 return -ENOENT;
1264
1265         ks8851_lock(ks, &flags);
1266
1267         ks8851_eeprom_claim(ks);
1268
1269         ee->magic = KS_EEPROM_MAGIC;
1270
1271         eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1272         ks8851_eeprom_release(ks);
1273         ks8851_unlock(ks, &flags);
1274
1275         return 0;
1276 }
1277
1278 static int ks8851_get_eeprom_len(struct net_device *dev)
1279 {
1280         struct ks8851_net *ks = netdev_priv(dev);
1281
1282         /* currently, we assume it is an 93C46 attached, so return 128 */
1283         return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1284 }
1285
1286 static const struct ethtool_ops ks8851_ethtool_ops = {
1287         .get_drvinfo    = ks8851_get_drvinfo,
1288         .get_msglevel   = ks8851_get_msglevel,
1289         .set_msglevel   = ks8851_set_msglevel,
1290         .get_link       = ks8851_get_link,
1291         .nway_reset     = ks8851_nway_reset,
1292         .get_eeprom_len = ks8851_get_eeprom_len,
1293         .get_eeprom     = ks8851_get_eeprom,
1294         .set_eeprom     = ks8851_set_eeprom,
1295         .get_link_ksettings = ks8851_get_link_ksettings,
1296         .set_link_ksettings = ks8851_set_link_ksettings,
1297 };
1298
1299 /* MII interface controls */
1300
1301 /**
1302  * ks8851_phy_reg - convert MII register into a KS8851 register
1303  * @reg: MII register number.
1304  *
1305  * Return the KS8851 register number for the corresponding MII PHY register
1306  * if possible. Return zero if the MII register has no direct mapping to the
1307  * KS8851 register set.
1308  */
1309 static int ks8851_phy_reg(int reg)
1310 {
1311         switch (reg) {
1312         case MII_BMCR:
1313                 return KS_P1MBCR;
1314         case MII_BMSR:
1315                 return KS_P1MBSR;
1316         case MII_PHYSID1:
1317                 return KS_PHY1ILR;
1318         case MII_PHYSID2:
1319                 return KS_PHY1IHR;
1320         case MII_ADVERTISE:
1321                 return KS_P1ANAR;
1322         case MII_LPA:
1323                 return KS_P1ANLPR;
1324         }
1325
1326         return 0x0;
1327 }
1328
1329 /**
1330  * ks8851_phy_read - MII interface PHY register read.
1331  * @dev: The network device the PHY is on.
1332  * @phy_addr: Address of PHY (ignored as we only have one)
1333  * @reg: The register to read.
1334  *
1335  * This call reads data from the PHY register specified in @reg. Since the
1336  * device does not support all the MII registers, the non-existent values
1337  * are always returned as zero.
1338  *
1339  * We return zero for unsupported registers as the MII code does not check
1340  * the value returned for any error status, and simply returns it to the
1341  * caller. The mii-tool that the driver was tested with takes any -ve error
1342  * as real PHY capabilities, thus displaying incorrect data to the user.
1343  */
1344 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1345 {
1346         struct ks8851_net *ks = netdev_priv(dev);
1347         unsigned long flags;
1348         int ksreg;
1349         int result;
1350
1351         ksreg = ks8851_phy_reg(reg);
1352         if (!ksreg)
1353                 return 0x0;     /* no error return allowed, so use zero */
1354
1355         ks8851_lock(ks, &flags);
1356         result = ks8851_rdreg16(ks, ksreg);
1357         ks8851_unlock(ks, &flags);
1358
1359         return result;
1360 }
1361
1362 static void ks8851_phy_write(struct net_device *dev,
1363                              int phy, int reg, int value)
1364 {
1365         struct ks8851_net *ks = netdev_priv(dev);
1366         unsigned long flags;
1367         int ksreg;
1368
1369         ksreg = ks8851_phy_reg(reg);
1370         if (ksreg) {
1371                 ks8851_lock(ks, &flags);
1372                 ks8851_wrreg16(ks, ksreg, value);
1373                 ks8851_unlock(ks, &flags);
1374         }
1375 }
1376
1377 /**
1378  * ks8851_read_selftest - read the selftest memory info.
1379  * @ks: The device state
1380  *
1381  * Read and check the TX/RX memory selftest information.
1382  */
1383 static int ks8851_read_selftest(struct ks8851_net *ks)
1384 {
1385         unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1386         int ret = 0;
1387         unsigned rd;
1388
1389         rd = ks8851_rdreg16(ks, KS_MBIR);
1390
1391         if ((rd & both_done) != both_done) {
1392                 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1393                 return 0;
1394         }
1395
1396         if (rd & MBIR_TXMBFA) {
1397                 netdev_err(ks->netdev, "TX memory selftest fail\n");
1398                 ret |= 1;
1399         }
1400
1401         if (rd & MBIR_RXMBFA) {
1402                 netdev_err(ks->netdev, "RX memory selftest fail\n");
1403                 ret |= 2;
1404         }
1405
1406         return 0;
1407 }
1408
1409 /* driver bus management functions */
1410
1411 #ifdef CONFIG_PM_SLEEP
1412
1413 static int ks8851_suspend(struct device *dev)
1414 {
1415         struct ks8851_net *ks = dev_get_drvdata(dev);
1416         struct net_device *netdev = ks->netdev;
1417
1418         if (netif_running(netdev)) {
1419                 netif_device_detach(netdev);
1420                 ks8851_net_stop(netdev);
1421         }
1422
1423         return 0;
1424 }
1425
1426 static int ks8851_resume(struct device *dev)
1427 {
1428         struct ks8851_net *ks = dev_get_drvdata(dev);
1429         struct net_device *netdev = ks->netdev;
1430
1431         if (netif_running(netdev)) {
1432                 ks8851_net_open(netdev);
1433                 netif_device_attach(netdev);
1434         }
1435
1436         return 0;
1437 }
1438 #endif
1439
1440 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1441
1442 static int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1443                                int msg_en)
1444 {
1445         struct ks8851_net *ks = netdev_priv(netdev);
1446         unsigned cider;
1447         int gpio;
1448         int ret;
1449
1450         ks->netdev = netdev;
1451         ks->tx_space = 6144;
1452
1453         gpio = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0, NULL);
1454         if (gpio == -EPROBE_DEFER)
1455                 return gpio;
1456
1457         ks->gpio = gpio;
1458         if (gpio_is_valid(gpio)) {
1459                 ret = devm_gpio_request_one(dev, gpio,
1460                                             GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1461                 if (ret) {
1462                         dev_err(dev, "reset gpio request failed\n");
1463                         return ret;
1464                 }
1465         }
1466
1467         ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1468         if (IS_ERR(ks->vdd_io)) {
1469                 ret = PTR_ERR(ks->vdd_io);
1470                 goto err_reg_io;
1471         }
1472
1473         ret = regulator_enable(ks->vdd_io);
1474         if (ret) {
1475                 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1476                 goto err_reg_io;
1477         }
1478
1479         ks->vdd_reg = devm_regulator_get(dev, "vdd");
1480         if (IS_ERR(ks->vdd_reg)) {
1481                 ret = PTR_ERR(ks->vdd_reg);
1482                 goto err_reg;
1483         }
1484
1485         ret = regulator_enable(ks->vdd_reg);
1486         if (ret) {
1487                 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1488                 goto err_reg;
1489         }
1490
1491         if (gpio_is_valid(gpio)) {
1492                 usleep_range(10000, 11000);
1493                 gpio_set_value(gpio, 1);
1494         }
1495
1496         spin_lock_init(&ks->statelock);
1497
1498         INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1499
1500         /* setup EEPROM state */
1501         ks->eeprom.data = ks;
1502         ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1503         ks->eeprom.register_read = ks8851_eeprom_regread;
1504         ks->eeprom.register_write = ks8851_eeprom_regwrite;
1505
1506         /* setup mii state */
1507         ks->mii.dev             = netdev;
1508         ks->mii.phy_id          = 1,
1509         ks->mii.phy_id_mask     = 1;
1510         ks->mii.reg_num_mask    = 0xf;
1511         ks->mii.mdio_read       = ks8851_phy_read;
1512         ks->mii.mdio_write      = ks8851_phy_write;
1513
1514         dev_info(dev, "message enable is %d\n", msg_en);
1515
1516         /* set the default message enable */
1517         ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1518                                                 NETIF_MSG_PROBE |
1519                                                 NETIF_MSG_LINK);
1520
1521         skb_queue_head_init(&ks->txq);
1522
1523         netdev->ethtool_ops = &ks8851_ethtool_ops;
1524         SET_NETDEV_DEV(netdev, dev);
1525
1526         dev_set_drvdata(dev, ks);
1527
1528         netif_carrier_off(ks->netdev);
1529         netdev->if_port = IF_PORT_100BASET;
1530         netdev->netdev_ops = &ks8851_netdev_ops;
1531
1532         /* issue a global soft reset to reset the device. */
1533         ks8851_soft_reset(ks, GRR_GSR);
1534
1535         /* simple check for a valid chip being connected to the bus */
1536         cider = ks8851_rdreg16(ks, KS_CIDER);
1537         if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1538                 dev_err(dev, "failed to read device ID\n");
1539                 ret = -ENODEV;
1540                 goto err_id;
1541         }
1542
1543         /* cache the contents of the CCR register for EEPROM, etc. */
1544         ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1545
1546         ks8851_read_selftest(ks);
1547         ks8851_init_mac(ks, dev->of_node);
1548
1549         ret = register_netdev(netdev);
1550         if (ret) {
1551                 dev_err(dev, "failed to register network device\n");
1552                 goto err_netdev;
1553         }
1554
1555         netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1556                     CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1557                     ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1558
1559         return 0;
1560
1561 err_netdev:
1562 err_id:
1563         if (gpio_is_valid(gpio))
1564                 gpio_set_value(gpio, 0);
1565         regulator_disable(ks->vdd_reg);
1566 err_reg:
1567         regulator_disable(ks->vdd_io);
1568 err_reg_io:
1569         return ret;
1570 }
1571
1572 static int ks8851_remove_common(struct device *dev)
1573 {
1574         struct ks8851_net *priv = dev_get_drvdata(dev);
1575
1576         if (netif_msg_drv(priv))
1577                 dev_info(dev, "remove\n");
1578
1579         unregister_netdev(priv->netdev);
1580         if (gpio_is_valid(priv->gpio))
1581                 gpio_set_value(priv->gpio, 0);
1582         regulator_disable(priv->vdd_reg);
1583         regulator_disable(priv->vdd_io);
1584
1585         return 0;
1586 }
1587
1588 static int ks8851_probe(struct spi_device *spi)
1589 {
1590         struct device *dev = &spi->dev;
1591         struct ks8851_net_spi *kss;
1592         struct net_device *netdev;
1593         struct ks8851_net *ks;
1594
1595         netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_spi));
1596         if (!netdev)
1597                 return -ENOMEM;
1598
1599         spi->bits_per_word = 8;
1600
1601         ks = netdev_priv(netdev);
1602         kss = to_ks8851_spi(ks);
1603
1604         kss->spidev = spi;
1605         mutex_init(&kss->lock);
1606         INIT_WORK(&kss->tx_work, ks8851_tx_work);
1607
1608         /* initialise pre-made spi transfer messages */
1609         spi_message_init(&kss->spi_msg1);
1610         spi_message_add_tail(&kss->spi_xfer1, &kss->spi_msg1);
1611
1612         spi_message_init(&kss->spi_msg2);
1613         spi_message_add_tail(&kss->spi_xfer2[0], &kss->spi_msg2);
1614         spi_message_add_tail(&kss->spi_xfer2[1], &kss->spi_msg2);
1615
1616         netdev->irq = spi->irq;
1617
1618         return ks8851_probe_common(netdev, dev, msg_enable);
1619 }
1620
1621 static int ks8851_remove(struct spi_device *spi)
1622 {
1623         return ks8851_remove_common(&spi->dev);
1624 }
1625
1626 static const struct of_device_id ks8851_match_table[] = {
1627         { .compatible = "micrel,ks8851" },
1628         { }
1629 };
1630 MODULE_DEVICE_TABLE(of, ks8851_match_table);
1631
1632 static struct spi_driver ks8851_driver = {
1633         .driver = {
1634                 .name = "ks8851",
1635                 .of_match_table = ks8851_match_table,
1636                 .pm = &ks8851_pm_ops,
1637         },
1638         .probe = ks8851_probe,
1639         .remove = ks8851_remove,
1640 };
1641 module_spi_driver(ks8851_driver);
1642
1643 MODULE_DESCRIPTION("KS8851 Network driver");
1644 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1645 MODULE_LICENSE("GPL");
1646
1647 module_param_named(message, msg_enable, int, 0);
1648 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1649 MODULE_ALIAS("spi:ks8851");