4283ba5bee81607909e9d64d96c8fcd3b52eeeb4
[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_net_open - open network device
786  * @dev: The network device being opened.
787  *
788  * Called when the network device is marked active, such as a user executing
789  * 'ifconfig up' on the device.
790  */
791 static int ks8851_net_open(struct net_device *dev)
792 {
793         struct ks8851_net *ks = netdev_priv(dev);
794         unsigned long flags;
795         int ret;
796
797         ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
798                                    IRQF_TRIGGER_LOW | IRQF_ONESHOT,
799                                    dev->name, ks);
800         if (ret < 0) {
801                 netdev_err(dev, "failed to get irq\n");
802                 return ret;
803         }
804
805         /* lock the card, even if we may not actually be doing anything
806          * else at the moment */
807         ks8851_lock(ks, &flags);
808
809         netif_dbg(ks, ifup, ks->netdev, "opening\n");
810
811         /* bring chip out of any power saving mode it was in */
812         ks8851_set_powermode(ks, PMECR_PM_NORMAL);
813
814         /* issue a soft reset to the RX/TX QMU to put it into a known
815          * state. */
816         ks8851_soft_reset(ks, GRR_QMU);
817
818         /* setup transmission parameters */
819
820         ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
821                                      TXCR_TXPE | /* pad to min length */
822                                      TXCR_TXCRC | /* add CRC */
823                                      TXCR_TXFCE)); /* enable flow control */
824
825         /* auto-increment tx data, reset tx pointer */
826         ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
827
828         /* setup receiver control */
829
830         ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /*  from mac filter */
831                                       RXCR1_RXFCE | /* enable flow control */
832                                       RXCR1_RXBE | /* broadcast enable */
833                                       RXCR1_RXUE | /* unicast enable */
834                                       RXCR1_RXE)); /* enable rx block */
835
836         /* transfer entire frames out in one go */
837         ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
838
839         /* set receive counter timeouts */
840         ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
841         ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
842         ks8851_wrreg16(ks, KS_RXFCTR, 10);  /* 10 frames to IRQ */
843
844         ks->rc_rxqcr = (RXQCR_RXFCTE |  /* IRQ on frame count exceeded */
845                         RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
846                         RXQCR_RXDTTE);  /* IRQ on time exceeded */
847
848         ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
849
850         /* clear then enable interrupts */
851
852 #define STD_IRQ (IRQ_LCI |      /* Link Change */       \
853                  IRQ_TXI |      /* TX done */           \
854                  IRQ_RXI |      /* RX done */           \
855                  IRQ_SPIBEI |   /* SPI bus error */     \
856                  IRQ_TXPSI |    /* TX process stop */   \
857                  IRQ_RXPSI)     /* RX process stop */
858
859         ks->rc_ier = STD_IRQ;
860         ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
861         ks8851_wrreg16(ks, KS_IER, STD_IRQ);
862
863         netif_start_queue(ks->netdev);
864
865         netif_dbg(ks, ifup, ks->netdev, "network device up\n");
866
867         ks8851_unlock(ks, &flags);
868         mii_check_link(&ks->mii);
869         return 0;
870 }
871
872 /**
873  * ks8851_net_stop - close network device
874  * @dev: The device being closed.
875  *
876  * Called to close down a network device which has been active. Cancell any
877  * work, shutdown the RX and TX process and then place the chip into a low
878  * power state whilst it is not being used.
879  */
880 static int ks8851_net_stop(struct net_device *dev)
881 {
882         struct ks8851_net *ks = netdev_priv(dev);
883         struct ks8851_net_spi *kss;
884         unsigned long flags;
885
886         kss = to_ks8851_spi(ks);
887
888         netif_info(ks, ifdown, dev, "shutting down\n");
889
890         netif_stop_queue(dev);
891
892         ks8851_lock(ks, &flags);
893         /* turn off the IRQs and ack any outstanding */
894         ks8851_wrreg16(ks, KS_IER, 0x0000);
895         ks8851_wrreg16(ks, KS_ISR, 0xffff);
896         ks8851_unlock(ks, &flags);
897
898         /* stop any outstanding work */
899         flush_work(&kss->tx_work);
900         flush_work(&ks->rxctrl_work);
901
902         ks8851_lock(ks, &flags);
903         /* shutdown RX process */
904         ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
905
906         /* shutdown TX process */
907         ks8851_wrreg16(ks, KS_TXCR, 0x0000);
908
909         /* set powermode to soft power down to save power */
910         ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
911         ks8851_unlock(ks, &flags);
912
913         /* ensure any queued tx buffers are dumped */
914         while (!skb_queue_empty(&ks->txq)) {
915                 struct sk_buff *txb = skb_dequeue(&ks->txq);
916
917                 netif_dbg(ks, ifdown, ks->netdev,
918                           "%s: freeing txb %p\n", __func__, txb);
919
920                 dev_kfree_skb(txb);
921         }
922
923         free_irq(dev->irq, ks);
924
925         return 0;
926 }
927
928 /**
929  * ks8851_start_xmit - transmit packet
930  * @skb: The buffer to transmit
931  * @dev: The device used to transmit the packet.
932  *
933  * Called by the network layer to transmit the @skb. Queue the packet for
934  * the device and schedule the necessary work to transmit the packet when
935  * it is free.
936  *
937  * We do this to firstly avoid sleeping with the network device locked,
938  * and secondly so we can round up more than one packet to transmit which
939  * means we can try and avoid generating too many transmit done interrupts.
940  */
941 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
942                                      struct net_device *dev)
943 {
944         struct ks8851_net *ks = netdev_priv(dev);
945         unsigned needed = calc_txlen(skb->len);
946         netdev_tx_t ret = NETDEV_TX_OK;
947         struct ks8851_net_spi *kss;
948
949         kss = to_ks8851_spi(ks);
950
951         netif_dbg(ks, tx_queued, ks->netdev,
952                   "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
953
954         spin_lock(&ks->statelock);
955
956         if (needed > ks->tx_space) {
957                 netif_stop_queue(dev);
958                 ret = NETDEV_TX_BUSY;
959         } else {
960                 ks->tx_space -= needed;
961                 skb_queue_tail(&ks->txq, skb);
962         }
963
964         spin_unlock(&ks->statelock);
965         schedule_work(&kss->tx_work);
966
967         return ret;
968 }
969
970 /**
971  * ks8851_rxctrl_work - work handler to change rx mode
972  * @work: The work structure this belongs to.
973  *
974  * Lock the device and issue the necessary changes to the receive mode from
975  * the network device layer. This is done so that we can do this without
976  * having to sleep whilst holding the network device lock.
977  *
978  * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
979  * receive parameters are programmed, we issue a write to disable the RXQ and
980  * then wait for the interrupt handler to be triggered once the RXQ shutdown is
981  * complete. The interrupt handler then writes the new values into the chip.
982  */
983 static void ks8851_rxctrl_work(struct work_struct *work)
984 {
985         struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
986         unsigned long flags;
987
988         ks8851_lock(ks, &flags);
989
990         /* need to shutdown RXQ before modifying filter parameters */
991         ks8851_wrreg16(ks, KS_RXCR1, 0x00);
992
993         ks8851_unlock(ks, &flags);
994 }
995
996 static void ks8851_set_rx_mode(struct net_device *dev)
997 {
998         struct ks8851_net *ks = netdev_priv(dev);
999         struct ks8851_rxctrl rxctrl;
1000
1001         memset(&rxctrl, 0, sizeof(rxctrl));
1002
1003         if (dev->flags & IFF_PROMISC) {
1004                 /* interface to receive everything */
1005
1006                 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
1007         } else if (dev->flags & IFF_ALLMULTI) {
1008                 /* accept all multicast packets */
1009
1010                 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1011                                 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
1012         } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
1013                 struct netdev_hw_addr *ha;
1014                 u32 crc;
1015
1016                 /* accept some multicast */
1017
1018                 netdev_for_each_mc_addr(ha, dev) {
1019                         crc = ether_crc(ETH_ALEN, ha->addr);
1020                         crc >>= (32 - 6);  /* get top six bits */
1021
1022                         rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1023                 }
1024
1025                 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1026         } else {
1027                 /* just accept broadcast / unicast */
1028                 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1029         }
1030
1031         rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1032                          RXCR1_RXBE | /* broadcast enable */
1033                          RXCR1_RXE | /* RX process enable */
1034                          RXCR1_RXFCE); /* enable flow control */
1035
1036         rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1037
1038         /* schedule work to do the actual set of the data if needed */
1039
1040         spin_lock(&ks->statelock);
1041
1042         if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1043                 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1044                 schedule_work(&ks->rxctrl_work);
1045         }
1046
1047         spin_unlock(&ks->statelock);
1048 }
1049
1050 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1051 {
1052         struct sockaddr *sa = addr;
1053
1054         if (netif_running(dev))
1055                 return -EBUSY;
1056
1057         if (!is_valid_ether_addr(sa->sa_data))
1058                 return -EADDRNOTAVAIL;
1059
1060         memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1061         return ks8851_write_mac_addr(dev);
1062 }
1063
1064 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1065 {
1066         struct ks8851_net *ks = netdev_priv(dev);
1067
1068         if (!netif_running(dev))
1069                 return -EINVAL;
1070
1071         return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1072 }
1073
1074 static const struct net_device_ops ks8851_netdev_ops = {
1075         .ndo_open               = ks8851_net_open,
1076         .ndo_stop               = ks8851_net_stop,
1077         .ndo_do_ioctl           = ks8851_net_ioctl,
1078         .ndo_start_xmit         = ks8851_start_xmit,
1079         .ndo_set_mac_address    = ks8851_set_mac_address,
1080         .ndo_set_rx_mode        = ks8851_set_rx_mode,
1081         .ndo_validate_addr      = eth_validate_addr,
1082 };
1083
1084 /* ethtool support */
1085
1086 static void ks8851_get_drvinfo(struct net_device *dev,
1087                                struct ethtool_drvinfo *di)
1088 {
1089         strlcpy(di->driver, "KS8851", sizeof(di->driver));
1090         strlcpy(di->version, "1.00", sizeof(di->version));
1091         strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1092 }
1093
1094 static u32 ks8851_get_msglevel(struct net_device *dev)
1095 {
1096         struct ks8851_net *ks = netdev_priv(dev);
1097         return ks->msg_enable;
1098 }
1099
1100 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1101 {
1102         struct ks8851_net *ks = netdev_priv(dev);
1103         ks->msg_enable = to;
1104 }
1105
1106 static int ks8851_get_link_ksettings(struct net_device *dev,
1107                                      struct ethtool_link_ksettings *cmd)
1108 {
1109         struct ks8851_net *ks = netdev_priv(dev);
1110
1111         mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1112
1113         return 0;
1114 }
1115
1116 static int ks8851_set_link_ksettings(struct net_device *dev,
1117                                      const struct ethtool_link_ksettings *cmd)
1118 {
1119         struct ks8851_net *ks = netdev_priv(dev);
1120         return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1121 }
1122
1123 static u32 ks8851_get_link(struct net_device *dev)
1124 {
1125         struct ks8851_net *ks = netdev_priv(dev);
1126         return mii_link_ok(&ks->mii);
1127 }
1128
1129 static int ks8851_nway_reset(struct net_device *dev)
1130 {
1131         struct ks8851_net *ks = netdev_priv(dev);
1132         return mii_nway_restart(&ks->mii);
1133 }
1134
1135 /* EEPROM support */
1136
1137 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1138 {
1139         struct ks8851_net *ks = ee->data;
1140         unsigned val;
1141
1142         val = ks8851_rdreg16(ks, KS_EEPCR);
1143
1144         ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1145         ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1146         ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1147 }
1148
1149 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1150 {
1151         struct ks8851_net *ks = ee->data;
1152         unsigned val = EEPCR_EESA;      /* default - eeprom access on */
1153
1154         if (ee->drive_data)
1155                 val |= EEPCR_EESRWA;
1156         if (ee->reg_data_in)
1157                 val |= EEPCR_EEDO;
1158         if (ee->reg_data_clock)
1159                 val |= EEPCR_EESCK;
1160         if (ee->reg_chip_select)
1161                 val |= EEPCR_EECS;
1162
1163         ks8851_wrreg16(ks, KS_EEPCR, val);
1164 }
1165
1166 /**
1167  * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1168  * @ks: The network device state.
1169  *
1170  * Check for the presence of an EEPROM, and then activate software access
1171  * to the device.
1172  */
1173 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1174 {
1175         /* start with clock low, cs high */
1176         ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1177         return 0;
1178 }
1179
1180 /**
1181  * ks8851_eeprom_release - release the EEPROM interface
1182  * @ks: The device state
1183  *
1184  * Release the software access to the device EEPROM
1185  */
1186 static void ks8851_eeprom_release(struct ks8851_net *ks)
1187 {
1188         unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1189
1190         ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1191 }
1192
1193 #define KS_EEPROM_MAGIC (0x00008851)
1194
1195 static int ks8851_set_eeprom(struct net_device *dev,
1196                              struct ethtool_eeprom *ee, u8 *data)
1197 {
1198         struct ks8851_net *ks = netdev_priv(dev);
1199         int offset = ee->offset;
1200         unsigned long flags;
1201         int len = ee->len;
1202         u16 tmp;
1203
1204         /* currently only support byte writing */
1205         if (len != 1)
1206                 return -EINVAL;
1207
1208         if (ee->magic != KS_EEPROM_MAGIC)
1209                 return -EINVAL;
1210
1211         if (!(ks->rc_ccr & CCR_EEPROM))
1212                 return -ENOENT;
1213
1214         ks8851_lock(ks, &flags);
1215
1216         ks8851_eeprom_claim(ks);
1217
1218         eeprom_93cx6_wren(&ks->eeprom, true);
1219
1220         /* ethtool currently only supports writing bytes, which means
1221          * we have to read/modify/write our 16bit EEPROMs */
1222
1223         eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1224
1225         if (offset & 1) {
1226                 tmp &= 0xff;
1227                 tmp |= *data << 8;
1228         } else {
1229                 tmp &= 0xff00;
1230                 tmp |= *data;
1231         }
1232
1233         eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1234         eeprom_93cx6_wren(&ks->eeprom, false);
1235
1236         ks8851_eeprom_release(ks);
1237         ks8851_unlock(ks, &flags);
1238
1239         return 0;
1240 }
1241
1242 static int ks8851_get_eeprom(struct net_device *dev,
1243                              struct ethtool_eeprom *ee, u8 *data)
1244 {
1245         struct ks8851_net *ks = netdev_priv(dev);
1246         int offset = ee->offset;
1247         unsigned long flags;
1248         int len = ee->len;
1249
1250         /* must be 2 byte aligned */
1251         if (len & 1 || offset & 1)
1252                 return -EINVAL;
1253
1254         if (!(ks->rc_ccr & CCR_EEPROM))
1255                 return -ENOENT;
1256
1257         ks8851_lock(ks, &flags);
1258
1259         ks8851_eeprom_claim(ks);
1260
1261         ee->magic = KS_EEPROM_MAGIC;
1262
1263         eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1264         ks8851_eeprom_release(ks);
1265         ks8851_unlock(ks, &flags);
1266
1267         return 0;
1268 }
1269
1270 static int ks8851_get_eeprom_len(struct net_device *dev)
1271 {
1272         struct ks8851_net *ks = netdev_priv(dev);
1273
1274         /* currently, we assume it is an 93C46 attached, so return 128 */
1275         return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1276 }
1277
1278 static const struct ethtool_ops ks8851_ethtool_ops = {
1279         .get_drvinfo    = ks8851_get_drvinfo,
1280         .get_msglevel   = ks8851_get_msglevel,
1281         .set_msglevel   = ks8851_set_msglevel,
1282         .get_link       = ks8851_get_link,
1283         .nway_reset     = ks8851_nway_reset,
1284         .get_eeprom_len = ks8851_get_eeprom_len,
1285         .get_eeprom     = ks8851_get_eeprom,
1286         .set_eeprom     = ks8851_set_eeprom,
1287         .get_link_ksettings = ks8851_get_link_ksettings,
1288         .set_link_ksettings = ks8851_set_link_ksettings,
1289 };
1290
1291 /* MII interface controls */
1292
1293 /**
1294  * ks8851_phy_reg - convert MII register into a KS8851 register
1295  * @reg: MII register number.
1296  *
1297  * Return the KS8851 register number for the corresponding MII PHY register
1298  * if possible. Return zero if the MII register has no direct mapping to the
1299  * KS8851 register set.
1300  */
1301 static int ks8851_phy_reg(int reg)
1302 {
1303         switch (reg) {
1304         case MII_BMCR:
1305                 return KS_P1MBCR;
1306         case MII_BMSR:
1307                 return KS_P1MBSR;
1308         case MII_PHYSID1:
1309                 return KS_PHY1ILR;
1310         case MII_PHYSID2:
1311                 return KS_PHY1IHR;
1312         case MII_ADVERTISE:
1313                 return KS_P1ANAR;
1314         case MII_LPA:
1315                 return KS_P1ANLPR;
1316         }
1317
1318         return 0x0;
1319 }
1320
1321 /**
1322  * ks8851_phy_read - MII interface PHY register read.
1323  * @dev: The network device the PHY is on.
1324  * @phy_addr: Address of PHY (ignored as we only have one)
1325  * @reg: The register to read.
1326  *
1327  * This call reads data from the PHY register specified in @reg. Since the
1328  * device does not support all the MII registers, the non-existent values
1329  * are always returned as zero.
1330  *
1331  * We return zero for unsupported registers as the MII code does not check
1332  * the value returned for any error status, and simply returns it to the
1333  * caller. The mii-tool that the driver was tested with takes any -ve error
1334  * as real PHY capabilities, thus displaying incorrect data to the user.
1335  */
1336 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1337 {
1338         struct ks8851_net *ks = netdev_priv(dev);
1339         unsigned long flags;
1340         int ksreg;
1341         int result;
1342
1343         ksreg = ks8851_phy_reg(reg);
1344         if (!ksreg)
1345                 return 0x0;     /* no error return allowed, so use zero */
1346
1347         ks8851_lock(ks, &flags);
1348         result = ks8851_rdreg16(ks, ksreg);
1349         ks8851_unlock(ks, &flags);
1350
1351         return result;
1352 }
1353
1354 static void ks8851_phy_write(struct net_device *dev,
1355                              int phy, int reg, int value)
1356 {
1357         struct ks8851_net *ks = netdev_priv(dev);
1358         unsigned long flags;
1359         int ksreg;
1360
1361         ksreg = ks8851_phy_reg(reg);
1362         if (ksreg) {
1363                 ks8851_lock(ks, &flags);
1364                 ks8851_wrreg16(ks, ksreg, value);
1365                 ks8851_unlock(ks, &flags);
1366         }
1367 }
1368
1369 /**
1370  * ks8851_read_selftest - read the selftest memory info.
1371  * @ks: The device state
1372  *
1373  * Read and check the TX/RX memory selftest information.
1374  */
1375 static int ks8851_read_selftest(struct ks8851_net *ks)
1376 {
1377         unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1378         int ret = 0;
1379         unsigned rd;
1380
1381         rd = ks8851_rdreg16(ks, KS_MBIR);
1382
1383         if ((rd & both_done) != both_done) {
1384                 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1385                 return 0;
1386         }
1387
1388         if (rd & MBIR_TXMBFA) {
1389                 netdev_err(ks->netdev, "TX memory selftest fail\n");
1390                 ret |= 1;
1391         }
1392
1393         if (rd & MBIR_RXMBFA) {
1394                 netdev_err(ks->netdev, "RX memory selftest fail\n");
1395                 ret |= 2;
1396         }
1397
1398         return 0;
1399 }
1400
1401 /* driver bus management functions */
1402
1403 #ifdef CONFIG_PM_SLEEP
1404
1405 static int ks8851_suspend(struct device *dev)
1406 {
1407         struct ks8851_net *ks = dev_get_drvdata(dev);
1408         struct net_device *netdev = ks->netdev;
1409
1410         if (netif_running(netdev)) {
1411                 netif_device_detach(netdev);
1412                 ks8851_net_stop(netdev);
1413         }
1414
1415         return 0;
1416 }
1417
1418 static int ks8851_resume(struct device *dev)
1419 {
1420         struct ks8851_net *ks = dev_get_drvdata(dev);
1421         struct net_device *netdev = ks->netdev;
1422
1423         if (netif_running(netdev)) {
1424                 ks8851_net_open(netdev);
1425                 netif_device_attach(netdev);
1426         }
1427
1428         return 0;
1429 }
1430 #endif
1431
1432 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1433
1434 static int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1435                                int msg_en)
1436 {
1437         struct ks8851_net *ks = netdev_priv(netdev);
1438         unsigned cider;
1439         int gpio;
1440         int ret;
1441
1442         ks->netdev = netdev;
1443         ks->tx_space = 6144;
1444
1445         gpio = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0, NULL);
1446         if (gpio == -EPROBE_DEFER)
1447                 return gpio;
1448
1449         ks->gpio = gpio;
1450         if (gpio_is_valid(gpio)) {
1451                 ret = devm_gpio_request_one(dev, gpio,
1452                                             GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1453                 if (ret) {
1454                         dev_err(dev, "reset gpio request failed\n");
1455                         return ret;
1456                 }
1457         }
1458
1459         ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1460         if (IS_ERR(ks->vdd_io)) {
1461                 ret = PTR_ERR(ks->vdd_io);
1462                 goto err_reg_io;
1463         }
1464
1465         ret = regulator_enable(ks->vdd_io);
1466         if (ret) {
1467                 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1468                 goto err_reg_io;
1469         }
1470
1471         ks->vdd_reg = devm_regulator_get(dev, "vdd");
1472         if (IS_ERR(ks->vdd_reg)) {
1473                 ret = PTR_ERR(ks->vdd_reg);
1474                 goto err_reg;
1475         }
1476
1477         ret = regulator_enable(ks->vdd_reg);
1478         if (ret) {
1479                 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1480                 goto err_reg;
1481         }
1482
1483         if (gpio_is_valid(gpio)) {
1484                 usleep_range(10000, 11000);
1485                 gpio_set_value(gpio, 1);
1486         }
1487
1488         spin_lock_init(&ks->statelock);
1489
1490         INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1491
1492         /* setup EEPROM state */
1493         ks->eeprom.data = ks;
1494         ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1495         ks->eeprom.register_read = ks8851_eeprom_regread;
1496         ks->eeprom.register_write = ks8851_eeprom_regwrite;
1497
1498         /* setup mii state */
1499         ks->mii.dev             = netdev;
1500         ks->mii.phy_id          = 1,
1501         ks->mii.phy_id_mask     = 1;
1502         ks->mii.reg_num_mask    = 0xf;
1503         ks->mii.mdio_read       = ks8851_phy_read;
1504         ks->mii.mdio_write      = ks8851_phy_write;
1505
1506         dev_info(dev, "message enable is %d\n", msg_en);
1507
1508         /* set the default message enable */
1509         ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1510                                                 NETIF_MSG_PROBE |
1511                                                 NETIF_MSG_LINK);
1512
1513         skb_queue_head_init(&ks->txq);
1514
1515         netdev->ethtool_ops = &ks8851_ethtool_ops;
1516         SET_NETDEV_DEV(netdev, dev);
1517
1518         dev_set_drvdata(dev, ks);
1519
1520         netif_carrier_off(ks->netdev);
1521         netdev->if_port = IF_PORT_100BASET;
1522         netdev->netdev_ops = &ks8851_netdev_ops;
1523
1524         /* issue a global soft reset to reset the device. */
1525         ks8851_soft_reset(ks, GRR_GSR);
1526
1527         /* simple check for a valid chip being connected to the bus */
1528         cider = ks8851_rdreg16(ks, KS_CIDER);
1529         if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1530                 dev_err(dev, "failed to read device ID\n");
1531                 ret = -ENODEV;
1532                 goto err_id;
1533         }
1534
1535         /* cache the contents of the CCR register for EEPROM, etc. */
1536         ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1537
1538         ks8851_read_selftest(ks);
1539         ks8851_init_mac(ks, dev->of_node);
1540
1541         ret = register_netdev(netdev);
1542         if (ret) {
1543                 dev_err(dev, "failed to register network device\n");
1544                 goto err_netdev;
1545         }
1546
1547         netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1548                     CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1549                     ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1550
1551         return 0;
1552
1553 err_netdev:
1554 err_id:
1555         if (gpio_is_valid(gpio))
1556                 gpio_set_value(gpio, 0);
1557         regulator_disable(ks->vdd_reg);
1558 err_reg:
1559         regulator_disable(ks->vdd_io);
1560 err_reg_io:
1561         return ret;
1562 }
1563
1564 static int ks8851_remove_common(struct device *dev)
1565 {
1566         struct ks8851_net *priv = dev_get_drvdata(dev);
1567
1568         if (netif_msg_drv(priv))
1569                 dev_info(dev, "remove\n");
1570
1571         unregister_netdev(priv->netdev);
1572         if (gpio_is_valid(priv->gpio))
1573                 gpio_set_value(priv->gpio, 0);
1574         regulator_disable(priv->vdd_reg);
1575         regulator_disable(priv->vdd_io);
1576
1577         return 0;
1578 }
1579
1580 static int ks8851_probe(struct spi_device *spi)
1581 {
1582         struct device *dev = &spi->dev;
1583         struct ks8851_net_spi *kss;
1584         struct net_device *netdev;
1585         struct ks8851_net *ks;
1586
1587         netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_spi));
1588         if (!netdev)
1589                 return -ENOMEM;
1590
1591         spi->bits_per_word = 8;
1592
1593         ks = netdev_priv(netdev);
1594         kss = to_ks8851_spi(ks);
1595
1596         kss->spidev = spi;
1597         mutex_init(&kss->lock);
1598         INIT_WORK(&kss->tx_work, ks8851_tx_work);
1599
1600         /* initialise pre-made spi transfer messages */
1601         spi_message_init(&kss->spi_msg1);
1602         spi_message_add_tail(&kss->spi_xfer1, &kss->spi_msg1);
1603
1604         spi_message_init(&kss->spi_msg2);
1605         spi_message_add_tail(&kss->spi_xfer2[0], &kss->spi_msg2);
1606         spi_message_add_tail(&kss->spi_xfer2[1], &kss->spi_msg2);
1607
1608         netdev->irq = spi->irq;
1609
1610         return ks8851_probe_common(netdev, dev, msg_enable);
1611 }
1612
1613 static int ks8851_remove(struct spi_device *spi)
1614 {
1615         return ks8851_remove_common(&spi->dev);
1616 }
1617
1618 static const struct of_device_id ks8851_match_table[] = {
1619         { .compatible = "micrel,ks8851" },
1620         { }
1621 };
1622 MODULE_DEVICE_TABLE(of, ks8851_match_table);
1623
1624 static struct spi_driver ks8851_driver = {
1625         .driver = {
1626                 .name = "ks8851",
1627                 .of_match_table = ks8851_match_table,
1628                 .pm = &ks8851_pm_ops,
1629         },
1630         .probe = ks8851_probe,
1631         .remove = ks8851_remove,
1632 };
1633 module_spi_driver(ks8851_driver);
1634
1635 MODULE_DESCRIPTION("KS8851 Network driver");
1636 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1637 MODULE_LICENSE("GPL");
1638
1639 module_param_named(message, msg_enable, int, 0);
1640 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1641 MODULE_ALIAS("spi:ks8851");