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