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