blob: e338aed9cb1cf0941e9cdcfacf85add9bb868693 [file] [log] [blame]
Tristram Ha3320eae2009-12-03 11:06:42 +00001/* drivers/net/ks8851.c
Ben Dooks3ba81f32009-07-16 05:24:08 +00002 *
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
Joe Perches0dc7d2b2010-02-27 14:43:51 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Ben Dooks3ba81f32009-07-16 05:24:08 +000014#define DEBUG
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/ethtool.h>
21#include <linux/cache.h>
22#include <linux/crc32.h>
23#include <linux/mii.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/regulator/consumer.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000025#include <linux/spi/spi.h>
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -070026#include <linux/ks8851.h>
27#include <linux/gpio.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000028
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 */
43struct 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 */
58union 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 * @irq_work: Work queue for servicing interrupts
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.
Sebastien Jan7d997462010-05-05 08:45:52 +000083 * @rc_ccr: Cached copy of KS_CCR.
Ben Dooks3ba81f32009-07-16 05:24:08 +000084 * @rc_rxqcr: Cached copy of KS_RXQCR.
Sebastien Jan7d997462010-05-05 08:45:52 +000085 * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
Ben Dooks3ba81f32009-07-16 05:24:08 +000086 *
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 */
100struct 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;
Sebastien Jan7d997462010-05-05 08:45:52 +0000116 u16 rc_ccr;
117 u16 eeprom_size;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000118
119 struct mii_if_info mii;
120 struct ks8851_rxctrl rxctrl;
121
122 struct work_struct tx_work;
123 struct work_struct irq_work;
124 struct work_struct rxctrl_work;
125
126 struct sk_buff_head txq;
127
128 struct spi_message spi_msg1;
129 struct spi_message spi_msg2;
130 struct spi_transfer spi_xfer1;
131 struct spi_transfer spi_xfer2[2];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 struct regulator *vdd_io;
133 struct regulator *vdd_phy;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000134};
135
136static int msg_enable;
137
Ben Dooks3ba81f32009-07-16 05:24:08 +0000138/* shift for byte-enable data */
139#define BYTE_EN(_x) ((_x) << 2)
140
141/* turn register number and byte-enable mask into data for start of packet */
142#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
143
144/* SPI register read/write calls.
145 *
146 * All these calls issue SPI transactions to access the chip's registers. They
147 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300148 * chip is busy transferring packet data (RX/TX FIFO accesses).
Ben Dooks3ba81f32009-07-16 05:24:08 +0000149 */
150
151/**
152 * ks8851_wrreg16 - write 16bit register value to chip
153 * @ks: The chip state
154 * @reg: The register address
155 * @val: The value to write
156 *
157 * Issue a write to put the value @val into the register specified in @reg.
158 */
159static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
160{
161 struct spi_transfer *xfer = &ks->spi_xfer1;
162 struct spi_message *msg = &ks->spi_msg1;
163 __le16 txb[2];
164 int ret;
165
166 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
167 txb[1] = cpu_to_le16(val);
168
169 xfer->tx_buf = txb;
170 xfer->rx_buf = NULL;
171 xfer->len = 4;
172
173 ret = spi_sync(ks->spidev, msg);
174 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000175 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000176}
177
178/**
Ben Dooks160d0fa2009-10-19 23:49:04 +0000179 * ks8851_wrreg8 - write 8bit register value to chip
180 * @ks: The chip state
181 * @reg: The register address
182 * @val: The value to write
183 *
184 * Issue a write to put the value @val into the register specified in @reg.
185 */
186static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
187{
188 struct spi_transfer *xfer = &ks->spi_xfer1;
189 struct spi_message *msg = &ks->spi_msg1;
190 __le16 txb[2];
191 int ret;
192 int bit;
193
194 bit = 1 << (reg & 3);
195
196 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
197 txb[1] = val;
198
199 xfer->tx_buf = txb;
200 xfer->rx_buf = NULL;
201 xfer->len = 3;
202
203 ret = spi_sync(ks->spidev, msg);
204 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000205 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks160d0fa2009-10-19 23:49:04 +0000206}
207
208/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000209 * ks8851_rx_1msg - select whether to use one or two messages for spi read
210 * @ks: The device structure
211 *
212 * Return whether to generate a single message with a tx and rx buffer
213 * supplied to spi_sync(), or alternatively send the tx and rx buffers
214 * as separate messages.
215 *
216 * Depending on the hardware in use, a single message may be more efficient
217 * on interrupts or work done by the driver.
218 *
219 * This currently always returns true until we add some per-device data passed
220 * from the platform code to specify which mode is better.
221 */
222static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
223{
224 return true;
225}
226
227/**
228 * ks8851_rdreg - issue read register command and return the data
229 * @ks: The device state
230 * @op: The register address and byte enables in message format.
231 * @rxb: The RX buffer to return the result into
232 * @rxl: The length of data expected.
233 *
234 * This is the low level read call that issues the necessary spi message(s)
235 * to read data from the register specified in @op.
236 */
237static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
238 u8 *rxb, unsigned rxl)
239{
240 struct spi_transfer *xfer;
241 struct spi_message *msg;
242 __le16 *txb = (__le16 *)ks->txd;
243 u8 *trx = ks->rxd;
244 int ret;
245
246 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
247
248 if (ks8851_rx_1msg(ks)) {
249 msg = &ks->spi_msg1;
250 xfer = &ks->spi_xfer1;
251
252 xfer->tx_buf = txb;
253 xfer->rx_buf = trx;
254 xfer->len = rxl + 2;
255 } else {
256 msg = &ks->spi_msg2;
257 xfer = ks->spi_xfer2;
258
259 xfer->tx_buf = txb;
260 xfer->rx_buf = NULL;
261 xfer->len = 2;
262
263 xfer++;
264 xfer->tx_buf = NULL;
265 xfer->rx_buf = trx;
266 xfer->len = rxl;
267 }
268
269 ret = spi_sync(ks->spidev, msg);
270 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000271 netdev_err(ks->netdev, "read: spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000272 else if (ks8851_rx_1msg(ks))
273 memcpy(rxb, trx + 2, rxl);
274 else
275 memcpy(rxb, trx, rxl);
276}
277
278/**
279 * ks8851_rdreg8 - read 8 bit register from device
280 * @ks: The chip information
281 * @reg: The register address
282 *
283 * Read a 8bit register from the chip, returning the result
284*/
285static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
286{
287 u8 rxb[1];
288
289 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
290 return rxb[0];
291}
292
293/**
294 * ks8851_rdreg16 - read 16 bit register from device
295 * @ks: The chip information
296 * @reg: The register address
297 *
298 * Read a 16bit register from the chip, returning the result
299*/
300static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
301{
302 __le16 rx = 0;
303
304 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
305 return le16_to_cpu(rx);
306}
307
308/**
309 * ks8851_rdreg32 - read 32 bit register from device
310 * @ks: The chip information
311 * @reg: The register address
312 *
313 * Read a 32bit register from the chip.
314 *
315 * Note, this read requires the address be aligned to 4 bytes.
316*/
317static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
318{
319 __le32 rx = 0;
320
321 WARN_ON(reg & 3);
322
323 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
324 return le32_to_cpu(rx);
325}
326
327/**
328 * ks8851_soft_reset - issue one of the soft reset to the device
329 * @ks: The device state.
330 * @op: The bit(s) to set in the GRR
331 *
332 * Issue the relevant soft-reset command to the device's GRR register
333 * specified by @op.
334 *
335 * Note, the delays are in there as a caution to ensure that the reset
336 * has time to take effect and then complete. Since the datasheet does
337 * not currently specify the exact sequence, we have chosen something
338 * that seems to work with our device.
339 */
340static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
341{
342 ks8851_wrreg16(ks, KS_GRR, op);
343 mdelay(1); /* wait a short time to effect reset */
344 ks8851_wrreg16(ks, KS_GRR, 0);
345 mdelay(1); /* wait for condition to clear */
346}
347
348/**
349 * ks8851_write_mac_addr - write mac address to device registers
350 * @dev: The network device
351 *
352 * Update the KS8851 MAC address registers from the address in @dev.
353 *
354 * This call assumes that the chip is not running, so there is no need to
355 * shutdown the RXQ process whilst setting this.
356*/
357static int ks8851_write_mac_addr(struct net_device *dev)
358{
359 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000360 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000361
362 mutex_lock(&ks->lock);
363
Ben Dooks160d0fa2009-10-19 23:49:04 +0000364 for (i = 0; i < ETH_ALEN; i++)
365 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000366
367 mutex_unlock(&ks->lock);
368
369 return 0;
370}
371
372/**
373 * ks8851_init_mac - initialise the mac address
374 * @ks: The device structure
375 *
376 * Get or create the initial mac address for the device and then set that
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700377 * into the station address register. The device will try to read a MAC address
378 * from the EEPROM and program it into the MARs. We use random_ether_addr()
379 * if the EEPROM is not present or if the address in the MARs appears invalid.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000380 */
381static void ks8851_init_mac(struct ks8851_net *ks)
382{
383 struct net_device *dev = ks->netdev;
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700384 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000385
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700386 mutex_lock(&ks->lock);
387
388 for (i = 0; i < ETH_ALEN; i++)
389 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
390
391 mutex_unlock(&ks->lock);
392
393 if (!(ks->rc_ccr & CCR_EEPROM) || !is_valid_ether_addr(dev->dev_addr)) {
394 random_ether_addr(dev->dev_addr);
395 ks8851_write_mac_addr(dev);
396 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000397}
398
399/**
400 * ks8851_irq - device interrupt handler
401 * @irq: Interrupt number passed from the IRQ hnalder.
402 * @pw: The private word passed to register_irq(), our struct ks8851_net.
403 *
404 * Disable the interrupt from happening again until we've processed the
405 * current status by scheduling ks8851_irq_work().
406 */
407static irqreturn_t ks8851_irq(int irq, void *pw)
408{
409 struct ks8851_net *ks = pw;
410
411 disable_irq_nosync(irq);
412 schedule_work(&ks->irq_work);
413 return IRQ_HANDLED;
414}
415
416/**
417 * ks8851_rdfifo - read data from the receive fifo
418 * @ks: The device state.
419 * @buff: The buffer address
420 * @len: The length of the data to read
421 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100422 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000423 * the FIFO into the buffer specified by @buff.
424 */
425static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
426{
427 struct spi_transfer *xfer = ks->spi_xfer2;
428 struct spi_message *msg = &ks->spi_msg2;
429 u8 txb[1];
430 int ret;
431
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000432 netif_dbg(ks, rx_status, ks->netdev,
433 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000434
435 /* set the operation we're issuing */
436 txb[0] = KS_SPIOP_RXFIFO;
437
438 xfer->tx_buf = txb;
439 xfer->rx_buf = NULL;
440 xfer->len = 1;
441
442 xfer++;
443 xfer->rx_buf = buff;
444 xfer->tx_buf = NULL;
445 xfer->len = len;
446
447 ret = spi_sync(ks->spidev, msg);
448 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000449 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000450}
451
452/**
453 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
454 * @ks: The device state
455 * @rxpkt: The data for the received packet
456 *
457 * Dump the initial data from the packet to dev_dbg().
458*/
459static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
460{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000461 netdev_dbg(ks->netdev,
462 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
463 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
464 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
465 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000466}
467
468/**
469 * ks8851_rx_pkts - receive packets from the host
470 * @ks: The device information.
471 *
472 * This is called from the IRQ work queue when the system detects that there
473 * are packets in the receive queue. Find out how many packets there are and
474 * read them from the FIFO.
475 */
476static void ks8851_rx_pkts(struct ks8851_net *ks)
477{
478 struct sk_buff *skb;
479 unsigned rxfc;
480 unsigned rxlen;
481 unsigned rxstat;
482 u32 rxh;
483 u8 *rxpkt;
484
485 rxfc = ks8851_rdreg8(ks, KS_RXFC);
486
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000487 netif_dbg(ks, rx_status, ks->netdev,
488 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000489
490 /* Currently we're issuing a read per packet, but we could possibly
491 * improve the code by issuing a single read, getting the receive
492 * header, allocating the packet and then reading the packet data
493 * out in one go.
494 *
495 * This form of operation would require us to hold the SPI bus'
496 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300497 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000498 */
499
500 for (; rxfc != 0; rxfc--) {
501 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
502 rxstat = rxh & 0xffff;
503 rxlen = rxh >> 16;
504
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000505 netif_dbg(ks, rx_status, ks->netdev,
506 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000507
508 /* the length of the packet includes the 32bit CRC */
509
510 /* set dma read address */
511 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
512
513 /* start the packet dma process, and set auto-dequeue rx */
514 ks8851_wrreg16(ks, KS_RXQCR,
515 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
516
Eric Dumazet972c40b2010-09-08 13:26:55 +0000517 if (rxlen > 4) {
518 unsigned int rxalign;
519
520 rxlen -= 4;
521 rxalign = ALIGN(rxlen, 4);
522 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
523 if (skb) {
524
525 /* 4 bytes of status header + 4 bytes of
526 * garbage: we put them before ethernet
527 * header, so that they are copied,
528 * but ignored.
529 */
530
531 rxpkt = skb_put(skb, rxlen) - 8;
532
533 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
534
535 if (netif_msg_pktdata(ks))
536 ks8851_dbg_dumpkkt(ks, rxpkt);
537
538 skb->protocol = eth_type_trans(skb, ks->netdev);
539 netif_rx(skb);
540
541 ks->netdev->stats.rx_packets++;
542 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000543 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000544 }
545
546 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
547 }
548}
549
550/**
551 * ks8851_irq_work - work queue handler for dealing with interrupt requests
552 * @work: The work structure that was scheduled by schedule_work()
553 *
554 * This is the handler invoked when the ks8851_irq() is called to find out
555 * what happened, as we cannot allow ourselves to sleep whilst waiting for
556 * anything other process has the chip's lock.
557 *
558 * Read the interrupt status, work out what needs to be done and then clear
559 * any of the interrupts that are not needed.
560 */
561static void ks8851_irq_work(struct work_struct *work)
562{
563 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
564 unsigned status;
565 unsigned handled = 0;
566
567 mutex_lock(&ks->lock);
568
569 status = ks8851_rdreg16(ks, KS_ISR);
570
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000571 netif_dbg(ks, intr, ks->netdev,
572 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000573
574 if (status & IRQ_LCI) {
575 /* should do something about checking link status */
576 handled |= IRQ_LCI;
577 }
578
579 if (status & IRQ_LDI) {
580 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
581 pmecr &= ~PMECR_WKEVT_MASK;
582 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
583
584 handled |= IRQ_LDI;
585 }
586
587 if (status & IRQ_RXPSI)
588 handled |= IRQ_RXPSI;
589
590 if (status & IRQ_TXI) {
591 handled |= IRQ_TXI;
592
593 /* no lock here, tx queue should have been stopped */
594
595 /* update our idea of how much tx space is available to the
596 * system */
597 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
598
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000599 netif_dbg(ks, intr, ks->netdev,
600 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000601 }
602
603 if (status & IRQ_RXI)
604 handled |= IRQ_RXI;
605
606 if (status & IRQ_SPIBEI) {
607 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
608 handled |= IRQ_SPIBEI;
609 }
610
611 ks8851_wrreg16(ks, KS_ISR, handled);
612
613 if (status & IRQ_RXI) {
614 /* the datasheet says to disable the rx interrupt during
615 * packet read-out, however we're masking the interrupt
616 * from the device so do not bother masking just the RX
617 * from the device. */
618
619 ks8851_rx_pkts(ks);
620 }
621
622 /* if something stopped the rx process, probably due to wanting
623 * to change the rx settings, then do something about restarting
624 * it. */
625 if (status & IRQ_RXPSI) {
626 struct ks8851_rxctrl *rxc = &ks->rxctrl;
627
628 /* update the multicast hash table */
629 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
630 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
631 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
632 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
633
634 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
635 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
636 }
637
638 mutex_unlock(&ks->lock);
639
640 if (status & IRQ_TXI)
641 netif_wake_queue(ks->netdev);
642
643 enable_irq(ks->netdev->irq);
644}
645
646/**
647 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300648 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000649 *
650 * Returns the size of the TXFIFO message needed to send
651 * this packet.
652 */
653static inline unsigned calc_txlen(unsigned len)
654{
655 return ALIGN(len + 4, 4);
656}
657
658/**
659 * ks8851_wrpkt - write packet to TX FIFO
660 * @ks: The device state.
661 * @txp: The sk_buff to transmit.
662 * @irq: IRQ on completion of the packet.
663 *
664 * Send the @txp to the chip. This means creating the relevant packet header
665 * specifying the length of the packet and the other information the chip
666 * needs, such as IRQ on completion. Send the header and the packet data to
667 * the device.
668 */
669static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
670{
671 struct spi_transfer *xfer = ks->spi_xfer2;
672 struct spi_message *msg = &ks->spi_msg2;
673 unsigned fid = 0;
674 int ret;
675
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000676 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
677 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000678
679 fid = ks->fid++;
680 fid &= TXFR_TXFID_MASK;
681
682 if (irq)
683 fid |= TXFR_TXIC; /* irq on completion */
684
685 /* start header at txb[1] to align txw entries */
686 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
687 ks->txh.txw[1] = cpu_to_le16(fid);
688 ks->txh.txw[2] = cpu_to_le16(txp->len);
689
690 xfer->tx_buf = &ks->txh.txb[1];
691 xfer->rx_buf = NULL;
692 xfer->len = 5;
693
694 xfer++;
695 xfer->tx_buf = txp->data;
696 xfer->rx_buf = NULL;
697 xfer->len = ALIGN(txp->len, 4);
698
699 ret = spi_sync(ks->spidev, msg);
700 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000701 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000702}
703
704/**
705 * ks8851_done_tx - update and then free skbuff after transmitting
706 * @ks: The device state
707 * @txb: The buffer transmitted
708 */
709static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
710{
711 struct net_device *dev = ks->netdev;
712
713 dev->stats.tx_bytes += txb->len;
714 dev->stats.tx_packets++;
715
716 dev_kfree_skb(txb);
717}
718
719/**
720 * ks8851_tx_work - process tx packet(s)
721 * @work: The work strucutre what was scheduled.
722 *
723 * This is called when a number of packets have been scheduled for
724 * transmission and need to be sent to the device.
725 */
726static void ks8851_tx_work(struct work_struct *work)
727{
728 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
729 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000730 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000731
732 mutex_lock(&ks->lock);
733
734 while (!last) {
735 txb = skb_dequeue(&ks->txq);
736 last = skb_queue_empty(&ks->txq);
737
Abraham Arce761172f2010-04-16 14:48:43 +0000738 if (txb != NULL) {
739 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
740 ks8851_wrpkt(ks, txb, last);
741 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
742 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000743
Abraham Arce761172f2010-04-16 14:48:43 +0000744 ks8851_done_tx(ks, txb);
745 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000746 }
747
748 mutex_unlock(&ks->lock);
749}
750
751/**
752 * ks8851_set_powermode - set power mode of the device
753 * @ks: The device state
754 * @pwrmode: The power mode value to write to KS_PMECR.
755 *
756 * Change the power mode of the chip.
757 */
758static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
759{
760 unsigned pmecr;
761
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000762 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000763
764 pmecr = ks8851_rdreg16(ks, KS_PMECR);
765 pmecr &= ~PMECR_PM_MASK;
766 pmecr |= pwrmode;
767
768 ks8851_wrreg16(ks, KS_PMECR, pmecr);
769}
770
771/**
772 * ks8851_net_open - open network device
773 * @dev: The network device being opened.
774 *
775 * Called when the network device is marked active, such as a user executing
776 * 'ifconfig up' on the device.
777 */
778static int ks8851_net_open(struct net_device *dev)
779{
780 struct ks8851_net *ks = netdev_priv(dev);
781
782 /* lock the card, even if we may not actually be doing anything
783 * else at the moment */
784 mutex_lock(&ks->lock);
785
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000786 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000787
788 /* bring chip out of any power saving mode it was in */
789 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
790
791 /* issue a soft reset to the RX/TX QMU to put it into a known
792 * state. */
793 ks8851_soft_reset(ks, GRR_QMU);
794
795 /* setup transmission parameters */
796
797 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
798 TXCR_TXPE | /* pad to min length */
799 TXCR_TXCRC | /* add CRC */
800 TXCR_TXFCE)); /* enable flow control */
801
802 /* auto-increment tx data, reset tx pointer */
803 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
804
805 /* setup receiver control */
806
807 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
808 RXCR1_RXFCE | /* enable flow control */
809 RXCR1_RXBE | /* broadcast enable */
810 RXCR1_RXUE | /* unicast enable */
811 RXCR1_RXE)); /* enable rx block */
812
813 /* transfer entire frames out in one go */
814 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
815
816 /* set receive counter timeouts */
817 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
818 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
819 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
820
821 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
822 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
823 RXQCR_RXDTTE); /* IRQ on time exceeded */
824
825 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
826
827 /* clear then enable interrupts */
828
829#define STD_IRQ (IRQ_LCI | /* Link Change */ \
830 IRQ_TXI | /* TX done */ \
831 IRQ_RXI | /* RX done */ \
832 IRQ_SPIBEI | /* SPI bus error */ \
833 IRQ_TXPSI | /* TX process stop */ \
834 IRQ_RXPSI) /* RX process stop */
835
836 ks->rc_ier = STD_IRQ;
837 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
838 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
839
840 netif_start_queue(ks->netdev);
841
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000842 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000843
844 mutex_unlock(&ks->lock);
845 return 0;
846}
847
848/**
849 * ks8851_net_stop - close network device
850 * @dev: The device being closed.
851 *
852 * Called to close down a network device which has been active. Cancell any
853 * work, shutdown the RX and TX process and then place the chip into a low
854 * power state whilst it is not being used.
855 */
856static int ks8851_net_stop(struct net_device *dev)
857{
858 struct ks8851_net *ks = netdev_priv(dev);
859
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000860 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000861
862 netif_stop_queue(dev);
863
864 mutex_lock(&ks->lock);
865
866 /* stop any outstanding work */
867 flush_work(&ks->irq_work);
868 flush_work(&ks->tx_work);
869 flush_work(&ks->rxctrl_work);
870
871 /* turn off the IRQs and ack any outstanding */
872 ks8851_wrreg16(ks, KS_IER, 0x0000);
873 ks8851_wrreg16(ks, KS_ISR, 0xffff);
874
875 /* shutdown RX process */
876 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
877
878 /* shutdown TX process */
879 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
880
881 /* set powermode to soft power down to save power */
882 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
883
884 /* ensure any queued tx buffers are dumped */
885 while (!skb_queue_empty(&ks->txq)) {
886 struct sk_buff *txb = skb_dequeue(&ks->txq);
887
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000888 netif_dbg(ks, ifdown, ks->netdev,
889 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000890
891 dev_kfree_skb(txb);
892 }
893
894 mutex_unlock(&ks->lock);
895 return 0;
896}
897
898/**
899 * ks8851_start_xmit - transmit packet
900 * @skb: The buffer to transmit
901 * @dev: The device used to transmit the packet.
902 *
903 * Called by the network layer to transmit the @skb. Queue the packet for
904 * the device and schedule the necessary work to transmit the packet when
905 * it is free.
906 *
907 * We do this to firstly avoid sleeping with the network device locked,
908 * and secondly so we can round up more than one packet to transmit which
909 * means we can try and avoid generating too many transmit done interrupts.
910 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000911static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
912 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000913{
914 struct ks8851_net *ks = netdev_priv(dev);
915 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000916 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000917
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000918 netif_dbg(ks, tx_queued, ks->netdev,
919 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000920
921 spin_lock(&ks->statelock);
922
923 if (needed > ks->tx_space) {
924 netif_stop_queue(dev);
925 ret = NETDEV_TX_BUSY;
926 } else {
927 ks->tx_space -= needed;
928 skb_queue_tail(&ks->txq, skb);
929 }
930
931 spin_unlock(&ks->statelock);
932 schedule_work(&ks->tx_work);
933
934 return ret;
935}
936
937/**
938 * ks8851_rxctrl_work - work handler to change rx mode
939 * @work: The work structure this belongs to.
940 *
941 * Lock the device and issue the necessary changes to the receive mode from
942 * the network device layer. This is done so that we can do this without
943 * having to sleep whilst holding the network device lock.
944 *
945 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
946 * receive parameters are programmed, we issue a write to disable the RXQ and
947 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
948 * complete. The interrupt handler then writes the new values into the chip.
949 */
950static void ks8851_rxctrl_work(struct work_struct *work)
951{
952 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
953
954 mutex_lock(&ks->lock);
955
956 /* need to shutdown RXQ before modifying filter parameters */
957 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
958
959 mutex_unlock(&ks->lock);
960}
961
962static void ks8851_set_rx_mode(struct net_device *dev)
963{
964 struct ks8851_net *ks = netdev_priv(dev);
965 struct ks8851_rxctrl rxctrl;
966
967 memset(&rxctrl, 0, sizeof(rxctrl));
968
969 if (dev->flags & IFF_PROMISC) {
970 /* interface to receive everything */
971
972 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
973 } else if (dev->flags & IFF_ALLMULTI) {
974 /* accept all multicast packets */
975
976 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
977 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000978 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000979 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000980 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000981
982 /* accept some multicast */
983
Jiri Pirko22bedad2010-04-01 21:22:57 +0000984 netdev_for_each_mc_addr(ha, dev) {
985 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000986 crc >>= (32 - 6); /* get top six bits */
987
988 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +0000989 }
990
Ben Dooksb6a71bf2009-10-19 23:49:05 +0000991 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000992 } else {
993 /* just accept broadcast / unicast */
994 rxctrl.rxcr1 = RXCR1_RXPAFMA;
995 }
996
997 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
998 RXCR1_RXBE | /* broadcast enable */
999 RXCR1_RXE | /* RX process enable */
1000 RXCR1_RXFCE); /* enable flow control */
1001
1002 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1003
1004 /* schedule work to do the actual set of the data if needed */
1005
1006 spin_lock(&ks->statelock);
1007
1008 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1009 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1010 schedule_work(&ks->rxctrl_work);
1011 }
1012
1013 spin_unlock(&ks->statelock);
1014}
1015
1016static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1017{
1018 struct sockaddr *sa = addr;
1019
1020 if (netif_running(dev))
1021 return -EBUSY;
1022
1023 if (!is_valid_ether_addr(sa->sa_data))
1024 return -EADDRNOTAVAIL;
1025
1026 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1027 return ks8851_write_mac_addr(dev);
1028}
1029
1030static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1031{
1032 struct ks8851_net *ks = netdev_priv(dev);
1033
1034 if (!netif_running(dev))
1035 return -EINVAL;
1036
1037 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1038}
1039
1040static const struct net_device_ops ks8851_netdev_ops = {
1041 .ndo_open = ks8851_net_open,
1042 .ndo_stop = ks8851_net_stop,
1043 .ndo_do_ioctl = ks8851_net_ioctl,
1044 .ndo_start_xmit = ks8851_start_xmit,
1045 .ndo_set_mac_address = ks8851_set_mac_address,
1046 .ndo_set_rx_mode = ks8851_set_rx_mode,
1047 .ndo_change_mtu = eth_change_mtu,
1048 .ndo_validate_addr = eth_validate_addr,
1049};
1050
Sebastien Jana4bdfff2010-05-05 08:45:53 +00001051/* Companion eeprom access */
1052
1053enum { /* EEPROM programming states */
1054 EEPROM_CONTROL,
1055 EEPROM_ADDRESS,
1056 EEPROM_DATA,
1057 EEPROM_COMPLETE
1058};
1059
1060/**
1061 * ks8851_eeprom_read - read a 16bits word in ks8851 companion EEPROM
1062 * @dev: The network device the PHY is on.
1063 * @addr: EEPROM address to read
1064 *
1065 * eeprom_size: used to define the data coding length. Can be changed
1066 * through debug-fs.
1067 *
1068 * Programs a read on the EEPROM using ks8851 EEPROM SW access feature.
1069 * Warning: The READ feature is not supported on ks8851 revision 0.
1070 *
1071 * Rough programming model:
1072 * - on period start: set clock high and read value on bus
1073 * - on period / 2: set clock low and program value on bus
1074 * - start on period / 2
1075 */
1076unsigned int ks8851_eeprom_read(struct net_device *dev, unsigned int addr)
1077{
1078 struct ks8851_net *ks = netdev_priv(dev);
1079 int eepcr;
1080 int ctrl = EEPROM_OP_READ;
1081 int state = EEPROM_CONTROL;
1082 int bit_count = EEPROM_OP_LEN - 1;
1083 unsigned int data = 0;
1084 int dummy;
1085 unsigned int addr_len;
1086
1087 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1088
1089 /* start transaction: chip select high, authorize write */
1090 mutex_lock(&ks->lock);
1091 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1092 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1093 eepcr |= EEPCR_EECS;
1094 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1095 mutex_unlock(&ks->lock);
1096
1097 while (state != EEPROM_COMPLETE) {
1098 /* falling clock period starts... */
1099 /* set EED_IO pin for control and address */
1100 eepcr &= ~EEPCR_EEDO;
1101 switch (state) {
1102 case EEPROM_CONTROL:
1103 eepcr |= ((ctrl >> bit_count) & 1) << 2;
1104 if (bit_count-- <= 0) {
1105 bit_count = addr_len - 1;
1106 state = EEPROM_ADDRESS;
1107 }
1108 break;
1109 case EEPROM_ADDRESS:
1110 eepcr |= ((addr >> bit_count) & 1) << 2;
1111 bit_count--;
1112 break;
1113 case EEPROM_DATA:
1114 /* Change to receive mode */
1115 eepcr &= ~EEPCR_EESRWA;
1116 break;
1117 }
1118
1119 /* lower clock */
1120 eepcr &= ~EEPCR_EESCK;
1121
1122 mutex_lock(&ks->lock);
1123 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1124 mutex_unlock(&ks->lock);
1125
1126 /* waitread period / 2 */
1127 udelay(EEPROM_SK_PERIOD / 2);
1128
1129 /* rising clock period starts... */
1130
1131 /* raise clock */
1132 mutex_lock(&ks->lock);
1133 eepcr |= EEPCR_EESCK;
1134 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1135 mutex_unlock(&ks->lock);
1136
1137 /* Manage read */
1138 switch (state) {
1139 case EEPROM_ADDRESS:
1140 if (bit_count < 0) {
1141 bit_count = EEPROM_DATA_LEN - 1;
1142 state = EEPROM_DATA;
1143 }
1144 break;
1145 case EEPROM_DATA:
1146 mutex_lock(&ks->lock);
1147 dummy = ks8851_rdreg16(ks, KS_EEPCR);
1148 mutex_unlock(&ks->lock);
1149 data |= ((dummy >> EEPCR_EESB_OFFSET) & 1) << bit_count;
1150 if (bit_count-- <= 0)
1151 state = EEPROM_COMPLETE;
1152 break;
1153 }
1154
1155 /* wait period / 2 */
1156 udelay(EEPROM_SK_PERIOD / 2);
1157 }
1158
1159 /* close transaction */
1160 mutex_lock(&ks->lock);
1161 eepcr &= ~EEPCR_EECS;
1162 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1163 eepcr = 0;
1164 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1165 mutex_unlock(&ks->lock);
1166
1167 return data;
1168}
1169
1170/**
1171 * ks8851_eeprom_write - write a 16bits word in ks8851 companion EEPROM
1172 * @dev: The network device the PHY is on.
1173 * @op: operand (can be WRITE, EWEN, EWDS)
1174 * @addr: EEPROM address to write
1175 * @data: data to write
1176 *
1177 * eeprom_size: used to define the data coding length. Can be changed
1178 * through debug-fs.
1179 *
1180 * Programs a write on the EEPROM using ks8851 EEPROM SW access feature.
1181 *
1182 * Note that a write enable is required before writing data.
1183 *
1184 * Rough programming model:
1185 * - on period start: set clock high
1186 * - on period / 2: set clock low and program value on bus
1187 * - start on period / 2
1188 */
1189void ks8851_eeprom_write(struct net_device *dev, unsigned int op,
1190 unsigned int addr, unsigned int data)
1191{
1192 struct ks8851_net *ks = netdev_priv(dev);
1193 int eepcr;
1194 int state = EEPROM_CONTROL;
1195 int bit_count = EEPROM_OP_LEN - 1;
1196 unsigned int addr_len;
1197
1198 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1199
1200 switch (op) {
1201 case EEPROM_OP_EWEN:
1202 addr = 0x30;
1203 break;
1204 case EEPROM_OP_EWDS:
1205 addr = 0;
1206 break;
1207 }
1208
1209 /* start transaction: chip select high, authorize write */
1210 mutex_lock(&ks->lock);
1211 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1212 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1213 eepcr |= EEPCR_EECS;
1214 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1215 mutex_unlock(&ks->lock);
1216
1217 while (state != EEPROM_COMPLETE) {
1218 /* falling clock period starts... */
1219 /* set EED_IO pin for control and address */
1220 eepcr &= ~EEPCR_EEDO;
1221 switch (state) {
1222 case EEPROM_CONTROL:
1223 eepcr |= ((op >> bit_count) & 1) << 2;
1224 if (bit_count-- <= 0) {
1225 bit_count = addr_len - 1;
1226 state = EEPROM_ADDRESS;
1227 }
1228 break;
1229 case EEPROM_ADDRESS:
1230 eepcr |= ((addr >> bit_count) & 1) << 2;
1231 if (bit_count-- <= 0) {
1232 if (op == EEPROM_OP_WRITE) {
1233 bit_count = EEPROM_DATA_LEN - 1;
1234 state = EEPROM_DATA;
1235 } else {
1236 state = EEPROM_COMPLETE;
1237 }
1238 }
1239 break;
1240 case EEPROM_DATA:
1241 eepcr |= ((data >> bit_count) & 1) << 2;
1242 if (bit_count-- <= 0)
1243 state = EEPROM_COMPLETE;
1244 break;
1245 }
1246
1247 /* lower clock */
1248 eepcr &= ~EEPCR_EESCK;
1249
1250 mutex_lock(&ks->lock);
1251 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1252 mutex_unlock(&ks->lock);
1253
1254 /* wait period / 2 */
1255 udelay(EEPROM_SK_PERIOD / 2);
1256
1257 /* rising clock period starts... */
1258
1259 /* raise clock */
1260 eepcr |= EEPCR_EESCK;
1261 mutex_lock(&ks->lock);
1262 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1263 mutex_unlock(&ks->lock);
1264
1265 /* wait period / 2 */
1266 udelay(EEPROM_SK_PERIOD / 2);
1267 }
1268
1269 /* close transaction */
1270 mutex_lock(&ks->lock);
1271 eepcr &= ~EEPCR_EECS;
1272 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1273 eepcr = 0;
1274 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1275 mutex_unlock(&ks->lock);
1276
1277}
1278
Ben Dooks3ba81f32009-07-16 05:24:08 +00001279/* ethtool support */
1280
1281static void ks8851_get_drvinfo(struct net_device *dev,
1282 struct ethtool_drvinfo *di)
1283{
1284 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1285 strlcpy(di->version, "1.00", sizeof(di->version));
1286 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1287}
1288
1289static u32 ks8851_get_msglevel(struct net_device *dev)
1290{
1291 struct ks8851_net *ks = netdev_priv(dev);
1292 return ks->msg_enable;
1293}
1294
1295static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1296{
1297 struct ks8851_net *ks = netdev_priv(dev);
1298 ks->msg_enable = to;
1299}
1300
1301static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1302{
1303 struct ks8851_net *ks = netdev_priv(dev);
1304 return mii_ethtool_gset(&ks->mii, cmd);
1305}
1306
1307static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1308{
1309 struct ks8851_net *ks = netdev_priv(dev);
1310 return mii_ethtool_sset(&ks->mii, cmd);
1311}
1312
1313static u32 ks8851_get_link(struct net_device *dev)
1314{
1315 struct ks8851_net *ks = netdev_priv(dev);
1316 return mii_link_ok(&ks->mii);
1317}
1318
1319static int ks8851_nway_reset(struct net_device *dev)
1320{
1321 struct ks8851_net *ks = netdev_priv(dev);
1322 return mii_nway_restart(&ks->mii);
1323}
1324
Sebastien Jana84afa42010-05-05 08:45:54 +00001325static int ks8851_get_eeprom_len(struct net_device *dev)
1326{
1327 struct ks8851_net *ks = netdev_priv(dev);
1328 return ks->eeprom_size;
1329}
1330
1331static int ks8851_get_eeprom(struct net_device *dev,
1332 struct ethtool_eeprom *eeprom, u8 *bytes)
1333{
1334 struct ks8851_net *ks = netdev_priv(dev);
1335 u16 *eeprom_buff;
1336 int first_word;
1337 int last_word;
1338 int ret_val = 0;
1339 u16 i;
1340
1341 if (eeprom->len == 0)
1342 return -EINVAL;
1343
1344 if (eeprom->len > ks->eeprom_size)
1345 return -EINVAL;
1346
1347 eeprom->magic = ks8851_rdreg16(ks, KS_CIDER);
1348
1349 first_word = eeprom->offset >> 1;
1350 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1351
1352 eeprom_buff = kmalloc(sizeof(u16) *
1353 (last_word - first_word + 1), GFP_KERNEL);
1354 if (!eeprom_buff)
1355 return -ENOMEM;
1356
1357 for (i = 0; i < last_word - first_word + 1; i++)
1358 eeprom_buff[i] = ks8851_eeprom_read(dev, first_word + 1);
1359
1360 /* Device's eeprom is little-endian, word addressable */
1361 for (i = 0; i < last_word - first_word + 1; i++)
1362 le16_to_cpus(&eeprom_buff[i]);
1363
1364 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
1365 kfree(eeprom_buff);
1366
1367 return ret_val;
1368}
1369
1370static int ks8851_set_eeprom(struct net_device *dev,
1371 struct ethtool_eeprom *eeprom, u8 *bytes)
1372{
1373 struct ks8851_net *ks = netdev_priv(dev);
1374 u16 *eeprom_buff;
1375 void *ptr;
1376 int max_len;
1377 int first_word;
1378 int last_word;
1379 int ret_val = 0;
1380 u16 i;
1381
1382 if (eeprom->len == 0)
1383 return -EOPNOTSUPP;
1384
1385 if (eeprom->len > ks->eeprom_size)
1386 return -EINVAL;
1387
1388 if (eeprom->magic != ks8851_rdreg16(ks, KS_CIDER))
1389 return -EFAULT;
1390
1391 first_word = eeprom->offset >> 1;
1392 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1393 max_len = (last_word - first_word + 1) * 2;
1394 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
1395 if (!eeprom_buff)
1396 return -ENOMEM;
1397
1398 ptr = (void *)eeprom_buff;
1399
1400 if (eeprom->offset & 1) {
1401 /* need read/modify/write of first changed EEPROM word */
1402 /* only the second byte of the word is being modified */
1403 eeprom_buff[0] = ks8851_eeprom_read(dev, first_word);
1404 ptr++;
1405 }
1406 if ((eeprom->offset + eeprom->len) & 1)
1407 /* need read/modify/write of last changed EEPROM word */
1408 /* only the first byte of the word is being modified */
1409 eeprom_buff[last_word - first_word] =
1410 ks8851_eeprom_read(dev, last_word);
1411
1412
1413 /* Device's eeprom is little-endian, word addressable */
1414 le16_to_cpus(&eeprom_buff[0]);
1415 le16_to_cpus(&eeprom_buff[last_word - first_word]);
1416
1417 memcpy(ptr, bytes, eeprom->len);
1418
1419 for (i = 0; i < last_word - first_word + 1; i++)
1420 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
1421
1422 ks8851_eeprom_write(dev, EEPROM_OP_EWEN, 0, 0);
1423
1424 for (i = 0; i < last_word - first_word + 1; i++) {
1425 ks8851_eeprom_write(dev, EEPROM_OP_WRITE, first_word + i,
1426 eeprom_buff[i]);
1427 mdelay(EEPROM_WRITE_TIME);
1428 }
1429
1430 ks8851_eeprom_write(dev, EEPROM_OP_EWDS, 0, 0);
1431
1432 kfree(eeprom_buff);
1433 return ret_val;
1434}
1435
Ben Dooks3ba81f32009-07-16 05:24:08 +00001436static const struct ethtool_ops ks8851_ethtool_ops = {
1437 .get_drvinfo = ks8851_get_drvinfo,
1438 .get_msglevel = ks8851_get_msglevel,
1439 .set_msglevel = ks8851_set_msglevel,
1440 .get_settings = ks8851_get_settings,
1441 .set_settings = ks8851_set_settings,
1442 .get_link = ks8851_get_link,
1443 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001444 .get_eeprom_len = ks8851_get_eeprom_len,
1445 .get_eeprom = ks8851_get_eeprom,
1446 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001447};
1448
1449/* MII interface controls */
1450
1451/**
1452 * ks8851_phy_reg - convert MII register into a KS8851 register
1453 * @reg: MII register number.
1454 *
1455 * Return the KS8851 register number for the corresponding MII PHY register
1456 * if possible. Return zero if the MII register has no direct mapping to the
1457 * KS8851 register set.
1458 */
1459static int ks8851_phy_reg(int reg)
1460{
1461 switch (reg) {
1462 case MII_BMCR:
1463 return KS_P1MBCR;
1464 case MII_BMSR:
1465 return KS_P1MBSR;
1466 case MII_PHYSID1:
1467 return KS_PHY1ILR;
1468 case MII_PHYSID2:
1469 return KS_PHY1IHR;
1470 case MII_ADVERTISE:
1471 return KS_P1ANAR;
1472 case MII_LPA:
1473 return KS_P1ANLPR;
1474 }
1475
1476 return 0x0;
1477}
1478
1479/**
1480 * ks8851_phy_read - MII interface PHY register read.
1481 * @dev: The network device the PHY is on.
1482 * @phy_addr: Address of PHY (ignored as we only have one)
1483 * @reg: The register to read.
1484 *
1485 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001486 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001487 * are always returned as zero.
1488 *
1489 * We return zero for unsupported registers as the MII code does not check
1490 * the value returned for any error status, and simply returns it to the
1491 * caller. The mii-tool that the driver was tested with takes any -ve error
1492 * as real PHY capabilities, thus displaying incorrect data to the user.
1493 */
1494static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1495{
1496 struct ks8851_net *ks = netdev_priv(dev);
1497 int ksreg;
1498 int result;
1499
1500 ksreg = ks8851_phy_reg(reg);
1501 if (!ksreg)
1502 return 0x0; /* no error return allowed, so use zero */
1503
1504 mutex_lock(&ks->lock);
1505 result = ks8851_rdreg16(ks, ksreg);
1506 mutex_unlock(&ks->lock);
1507
1508 return result;
1509}
1510
1511static void ks8851_phy_write(struct net_device *dev,
1512 int phy, int reg, int value)
1513{
1514 struct ks8851_net *ks = netdev_priv(dev);
1515 int ksreg;
1516
1517 ksreg = ks8851_phy_reg(reg);
1518 if (ksreg) {
1519 mutex_lock(&ks->lock);
1520 ks8851_wrreg16(ks, ksreg, value);
1521 mutex_unlock(&ks->lock);
1522 }
1523}
1524
1525/**
1526 * ks8851_read_selftest - read the selftest memory info.
1527 * @ks: The device state
1528 *
1529 * Read and check the TX/RX memory selftest information.
1530 */
1531static int ks8851_read_selftest(struct ks8851_net *ks)
1532{
1533 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1534 int ret = 0;
1535 unsigned rd;
1536
1537 rd = ks8851_rdreg16(ks, KS_MBIR);
1538
1539 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001540 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001541 return 0;
1542 }
1543
1544 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001545 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001546 ret |= 1;
1547 }
1548
1549 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001550 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001551 ret |= 2;
1552 }
1553
1554 return 0;
1555}
1556
1557/* driver bus management functions */
1558
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001559#ifdef CONFIG_PM
1560static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1561{
1562 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1563 struct net_device *dev = ks->netdev;
1564
1565 if (netif_running(dev)) {
1566 netif_device_detach(dev);
1567 ks8851_net_stop(dev);
1568 }
1569
1570 return 0;
1571}
1572
1573static int ks8851_resume(struct spi_device *spi)
1574{
1575 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1576 struct net_device *dev = ks->netdev;
1577
1578 if (netif_running(dev)) {
1579 ks8851_net_open(dev);
1580 netif_device_attach(dev);
1581 }
1582
1583 return 0;
1584}
1585#else
1586#define ks8851_suspend NULL
1587#define ks8851_resume NULL
1588#endif
1589
Ben Dooks3ba81f32009-07-16 05:24:08 +00001590static int __devinit ks8851_probe(struct spi_device *spi)
1591{
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001592 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001593 struct net_device *ndev;
1594 struct ks8851_net *ks;
1595 int ret;
1596
1597 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1598 if (!ndev) {
1599 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1600 return -ENOMEM;
1601 }
1602
1603 spi->bits_per_word = 8;
1604
1605 ks = netdev_priv(ndev);
1606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001607 ks->vdd_io = regulator_get(&spi->dev, "vdd_io");
1608 ks->vdd_phy = regulator_get(&spi->dev, "vdd_phy");
1609
1610 if (!IS_ERR(ks->vdd_io))
1611 regulator_enable(ks->vdd_io);
1612
1613 if (!IS_ERR(ks->vdd_phy))
1614 regulator_enable(ks->vdd_phy);
1615
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001616 if (pdata && gpio_is_valid(pdata->irq_gpio)) {
1617 ret = gpio_request(pdata->irq_gpio, "ks8851_irq");
1618 if (ret) {
1619 pr_err("ks8851 gpio_request failed: %d\n", ret);
1620 goto err_irq_gpio;
1621 }
1622 }
1623
1624 if (pdata && gpio_is_valid(pdata->rst_gpio)) {
1625 ret = gpio_request(pdata->rst_gpio, "ks8851_rst");
1626 if (ret) {
1627 pr_err("ks8851 gpio_request failed: %d\n", ret);
1628 goto err_rst_gpio;
1629 }
1630 gpio_direction_output(pdata->rst_gpio, 1);
1631 }
1632
Ben Dooks3ba81f32009-07-16 05:24:08 +00001633 ks->netdev = ndev;
1634 ks->spidev = spi;
1635 ks->tx_space = 6144;
1636
1637 mutex_init(&ks->lock);
1638 spin_lock_init(&ks->statelock);
1639
1640 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1641 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1642 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1643
1644 /* initialise pre-made spi transfer messages */
1645
1646 spi_message_init(&ks->spi_msg1);
1647 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1648
1649 spi_message_init(&ks->spi_msg2);
1650 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1651 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1652
1653 /* setup mii state */
1654 ks->mii.dev = ndev;
1655 ks->mii.phy_id = 1,
1656 ks->mii.phy_id_mask = 1;
1657 ks->mii.reg_num_mask = 0xf;
1658 ks->mii.mdio_read = ks8851_phy_read;
1659 ks->mii.mdio_write = ks8851_phy_write;
1660
1661 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1662
1663 /* set the default message enable */
1664 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1665 NETIF_MSG_PROBE |
1666 NETIF_MSG_LINK));
1667
1668 skb_queue_head_init(&ks->txq);
1669
1670 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1671 SET_NETDEV_DEV(ndev, &spi->dev);
1672
1673 dev_set_drvdata(&spi->dev, ks);
1674
1675 ndev->if_port = IF_PORT_100BASET;
1676 ndev->netdev_ops = &ks8851_netdev_ops;
1677 ndev->irq = spi->irq;
1678
Ben Dooks57dada62009-10-19 23:49:03 +00001679 /* issue a global soft reset to reset the device. */
1680 ks8851_soft_reset(ks, GRR_GSR);
1681
Ben Dooks3ba81f32009-07-16 05:24:08 +00001682 /* simple check for a valid chip being connected to the bus */
1683
1684 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1685 dev_err(&spi->dev, "failed to read device ID\n");
1686 ret = -ENODEV;
1687 goto err_id;
1688 }
1689
Sebastien Jan7d997462010-05-05 08:45:52 +00001690 /* cache the contents of the CCR register for EEPROM, etc. */
1691 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1692
1693 if (ks->rc_ccr & CCR_EEPROM)
1694 ks->eeprom_size = 128;
1695 else
1696 ks->eeprom_size = 0;
1697
Ben Dooks3ba81f32009-07-16 05:24:08 +00001698 ks8851_read_selftest(ks);
1699 ks8851_init_mac(ks);
1700
1701 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1702 ndev->name, ks);
1703 if (ret < 0) {
1704 dev_err(&spi->dev, "failed to get irq\n");
1705 goto err_irq;
1706 }
1707
1708 ret = register_netdev(ndev);
1709 if (ret) {
1710 dev_err(&spi->dev, "failed to register network device\n");
1711 goto err_netdev;
1712 }
1713
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001714 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d\n",
1715 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
1716 ndev->dev_addr, ndev->irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001717
1718 return 0;
1719
1720
1721err_netdev:
1722 free_irq(ndev->irq, ndev);
1723
1724err_id:
1725err_irq:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001726 if (!IS_ERR(ks->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001727 regulator_disable(ks->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728 regulator_put(ks->vdd_io);
1729 }
1730
1731 if (!IS_ERR(ks->vdd_phy)) {
1732 regulator_disable(ks->vdd_phy);
1733 regulator_put(ks->vdd_phy);
1734 }
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001735
Stepan Moskovchenko79b444f2011-10-12 13:47:39 -07001736 free_netdev(ndev);
1737
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001738 if (pdata && gpio_is_valid(pdata->rst_gpio))
1739 gpio_free(pdata->rst_gpio);
1740
1741err_rst_gpio:
1742 if (pdata && gpio_is_valid(pdata->irq_gpio))
1743 gpio_free(pdata->irq_gpio);
1744
1745err_irq_gpio:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001746 return ret;
1747}
1748
1749static int __devexit ks8851_remove(struct spi_device *spi)
1750{
1751 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001752 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001753
1754 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001755 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001756
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001757 if (!IS_ERR(priv->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001758 regulator_disable(priv->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001759 regulator_put(priv->vdd_io);
1760 }
1761
1762 if (!IS_ERR(priv->vdd_phy)) {
1763 regulator_disable(priv->vdd_phy);
1764 regulator_put(priv->vdd_phy);
1765 }
1766
Ben Dooks3ba81f32009-07-16 05:24:08 +00001767 unregister_netdev(priv->netdev);
1768 free_irq(spi->irq, priv);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001769
1770 if (pdata && gpio_is_valid(pdata->irq_gpio))
1771 gpio_free(pdata->irq_gpio);
1772
1773 if (pdata && gpio_is_valid(pdata->rst_gpio))
1774 gpio_free(pdata->rst_gpio);
1775
Ben Dooks3ba81f32009-07-16 05:24:08 +00001776 free_netdev(priv->netdev);
1777
1778 return 0;
1779}
1780
1781static struct spi_driver ks8851_driver = {
1782 .driver = {
1783 .name = "ks8851",
1784 .owner = THIS_MODULE,
1785 },
1786 .probe = ks8851_probe,
1787 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001788 .suspend = ks8851_suspend,
1789 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001790};
1791
1792static int __init ks8851_init(void)
1793{
1794 return spi_register_driver(&ks8851_driver);
1795}
1796
1797static void __exit ks8851_exit(void)
1798{
1799 spi_unregister_driver(&ks8851_driver);
1800}
1801
1802module_init(ks8851_init);
1803module_exit(ks8851_exit);
1804
1805MODULE_DESCRIPTION("KS8851 Network driver");
1806MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1807MODULE_LICENSE("GPL");
1808
1809module_param_named(message, msg_enable, int, 0);
1810MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001811MODULE_ALIAS("spi:ks8851");