blob: 183fa37954d5096229499241487dff87dd14d182 [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/**
Ben Dooksff47cb02010-04-29 13:16:26 +0000373 * ks8851_read_mac_addr - read mac address from device registers
374 * @dev: The network device
Ben Dooks3ba81f32009-07-16 05:24:08 +0000375 *
Ben Dooksff47cb02010-04-29 13:16:26 +0000376 * Update our copy of the KS8851 MAC address from the registers of @dev.
377*/
378static void ks8851_read_mac_addr(struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000379{
Ben Dooksff47cb02010-04-29 13:16:26 +0000380 struct ks8851_net *ks = netdev_priv(dev);
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700381 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000382
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700383 mutex_lock(&ks->lock);
384
385 for (i = 0; i < ETH_ALEN; i++)
386 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
387
388 mutex_unlock(&ks->lock);
Ben Dooksff47cb02010-04-29 13:16:26 +0000389}
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700390
Ben Dooksff47cb02010-04-29 13:16:26 +0000391/**
392 * ks8851_init_mac - initialise the mac address
393 * @ks: The device structure
394 *
395 * Get or create the initial mac address for the device and then set that
396 * into the station address register. If there is an EEPROM present, then
397 * we try that. If no valid mac address is found we use random_ether_addr()
398 * to create a new one.
399 */
400static void ks8851_init_mac(struct ks8851_net *ks)
401{
402 struct net_device *dev = ks->netdev;
403
404 /* first, try reading what we've got already */
405 if (ks->rc_ccr & CCR_EEPROM) {
406 ks8851_read_mac_addr(dev);
407 if (is_valid_ether_addr(dev->dev_addr))
408 return;
409
410 netdev_err(ks->netdev, "invalid mac address read %pM\n",
411 dev->dev_addr);
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700412 }
Ben Dooksff47cb02010-04-29 13:16:26 +0000413
414 random_ether_addr(dev->dev_addr);
415 ks8851_write_mac_addr(dev);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000416}
417
418/**
419 * ks8851_irq - device interrupt handler
420 * @irq: Interrupt number passed from the IRQ hnalder.
421 * @pw: The private word passed to register_irq(), our struct ks8851_net.
422 *
423 * Disable the interrupt from happening again until we've processed the
424 * current status by scheduling ks8851_irq_work().
425 */
426static irqreturn_t ks8851_irq(int irq, void *pw)
427{
428 struct ks8851_net *ks = pw;
429
430 disable_irq_nosync(irq);
431 schedule_work(&ks->irq_work);
432 return IRQ_HANDLED;
433}
434
435/**
436 * ks8851_rdfifo - read data from the receive fifo
437 * @ks: The device state.
438 * @buff: The buffer address
439 * @len: The length of the data to read
440 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100441 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000442 * the FIFO into the buffer specified by @buff.
443 */
444static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
445{
446 struct spi_transfer *xfer = ks->spi_xfer2;
447 struct spi_message *msg = &ks->spi_msg2;
448 u8 txb[1];
449 int ret;
450
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000451 netif_dbg(ks, rx_status, ks->netdev,
452 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000453
454 /* set the operation we're issuing */
455 txb[0] = KS_SPIOP_RXFIFO;
456
457 xfer->tx_buf = txb;
458 xfer->rx_buf = NULL;
459 xfer->len = 1;
460
461 xfer++;
462 xfer->rx_buf = buff;
463 xfer->tx_buf = NULL;
464 xfer->len = len;
465
466 ret = spi_sync(ks->spidev, msg);
467 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000468 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000469}
470
471/**
472 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
473 * @ks: The device state
474 * @rxpkt: The data for the received packet
475 *
476 * Dump the initial data from the packet to dev_dbg().
477*/
478static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
479{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000480 netdev_dbg(ks->netdev,
481 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
482 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
483 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
484 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000485}
486
487/**
488 * ks8851_rx_pkts - receive packets from the host
489 * @ks: The device information.
490 *
491 * This is called from the IRQ work queue when the system detects that there
492 * are packets in the receive queue. Find out how many packets there are and
493 * read them from the FIFO.
494 */
495static void ks8851_rx_pkts(struct ks8851_net *ks)
496{
497 struct sk_buff *skb;
498 unsigned rxfc;
499 unsigned rxlen;
500 unsigned rxstat;
501 u32 rxh;
502 u8 *rxpkt;
503
504 rxfc = ks8851_rdreg8(ks, KS_RXFC);
505
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000506 netif_dbg(ks, rx_status, ks->netdev,
507 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000508
509 /* Currently we're issuing a read per packet, but we could possibly
510 * improve the code by issuing a single read, getting the receive
511 * header, allocating the packet and then reading the packet data
512 * out in one go.
513 *
514 * This form of operation would require us to hold the SPI bus'
515 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300516 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000517 */
518
519 for (; rxfc != 0; rxfc--) {
520 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
521 rxstat = rxh & 0xffff;
522 rxlen = rxh >> 16;
523
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000524 netif_dbg(ks, rx_status, ks->netdev,
525 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000526
527 /* the length of the packet includes the 32bit CRC */
528
529 /* set dma read address */
530 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
531
532 /* start the packet dma process, and set auto-dequeue rx */
533 ks8851_wrreg16(ks, KS_RXQCR,
534 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
535
Eric Dumazet972c40b2010-09-08 13:26:55 +0000536 if (rxlen > 4) {
537 unsigned int rxalign;
538
539 rxlen -= 4;
540 rxalign = ALIGN(rxlen, 4);
541 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
542 if (skb) {
543
544 /* 4 bytes of status header + 4 bytes of
545 * garbage: we put them before ethernet
546 * header, so that they are copied,
547 * but ignored.
548 */
549
550 rxpkt = skb_put(skb, rxlen) - 8;
551
552 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
553
554 if (netif_msg_pktdata(ks))
555 ks8851_dbg_dumpkkt(ks, rxpkt);
556
557 skb->protocol = eth_type_trans(skb, ks->netdev);
558 netif_rx(skb);
559
560 ks->netdev->stats.rx_packets++;
561 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000562 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000563 }
564
565 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
566 }
567}
568
569/**
570 * ks8851_irq_work - work queue handler for dealing with interrupt requests
571 * @work: The work structure that was scheduled by schedule_work()
572 *
573 * This is the handler invoked when the ks8851_irq() is called to find out
574 * what happened, as we cannot allow ourselves to sleep whilst waiting for
575 * anything other process has the chip's lock.
576 *
577 * Read the interrupt status, work out what needs to be done and then clear
578 * any of the interrupts that are not needed.
579 */
580static void ks8851_irq_work(struct work_struct *work)
581{
582 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
583 unsigned status;
584 unsigned handled = 0;
585
586 mutex_lock(&ks->lock);
587
588 status = ks8851_rdreg16(ks, KS_ISR);
589
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000590 netif_dbg(ks, intr, ks->netdev,
591 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000592
593 if (status & IRQ_LCI) {
594 /* should do something about checking link status */
595 handled |= IRQ_LCI;
596 }
597
598 if (status & IRQ_LDI) {
599 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
600 pmecr &= ~PMECR_WKEVT_MASK;
601 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
602
603 handled |= IRQ_LDI;
604 }
605
606 if (status & IRQ_RXPSI)
607 handled |= IRQ_RXPSI;
608
609 if (status & IRQ_TXI) {
610 handled |= IRQ_TXI;
611
612 /* no lock here, tx queue should have been stopped */
613
614 /* update our idea of how much tx space is available to the
615 * system */
616 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
617
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000618 netif_dbg(ks, intr, ks->netdev,
619 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000620 }
621
622 if (status & IRQ_RXI)
623 handled |= IRQ_RXI;
624
625 if (status & IRQ_SPIBEI) {
626 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
627 handled |= IRQ_SPIBEI;
628 }
629
630 ks8851_wrreg16(ks, KS_ISR, handled);
631
632 if (status & IRQ_RXI) {
633 /* the datasheet says to disable the rx interrupt during
634 * packet read-out, however we're masking the interrupt
635 * from the device so do not bother masking just the RX
636 * from the device. */
637
638 ks8851_rx_pkts(ks);
639 }
640
641 /* if something stopped the rx process, probably due to wanting
642 * to change the rx settings, then do something about restarting
643 * it. */
644 if (status & IRQ_RXPSI) {
645 struct ks8851_rxctrl *rxc = &ks->rxctrl;
646
647 /* update the multicast hash table */
648 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
649 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
650 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
651 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
652
653 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
654 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
655 }
656
657 mutex_unlock(&ks->lock);
658
659 if (status & IRQ_TXI)
660 netif_wake_queue(ks->netdev);
661
662 enable_irq(ks->netdev->irq);
663}
664
665/**
666 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300667 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000668 *
669 * Returns the size of the TXFIFO message needed to send
670 * this packet.
671 */
672static inline unsigned calc_txlen(unsigned len)
673{
674 return ALIGN(len + 4, 4);
675}
676
677/**
678 * ks8851_wrpkt - write packet to TX FIFO
679 * @ks: The device state.
680 * @txp: The sk_buff to transmit.
681 * @irq: IRQ on completion of the packet.
682 *
683 * Send the @txp to the chip. This means creating the relevant packet header
684 * specifying the length of the packet and the other information the chip
685 * needs, such as IRQ on completion. Send the header and the packet data to
686 * the device.
687 */
688static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
689{
690 struct spi_transfer *xfer = ks->spi_xfer2;
691 struct spi_message *msg = &ks->spi_msg2;
692 unsigned fid = 0;
693 int ret;
694
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000695 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
696 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000697
698 fid = ks->fid++;
699 fid &= TXFR_TXFID_MASK;
700
701 if (irq)
702 fid |= TXFR_TXIC; /* irq on completion */
703
704 /* start header at txb[1] to align txw entries */
705 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
706 ks->txh.txw[1] = cpu_to_le16(fid);
707 ks->txh.txw[2] = cpu_to_le16(txp->len);
708
709 xfer->tx_buf = &ks->txh.txb[1];
710 xfer->rx_buf = NULL;
711 xfer->len = 5;
712
713 xfer++;
714 xfer->tx_buf = txp->data;
715 xfer->rx_buf = NULL;
716 xfer->len = ALIGN(txp->len, 4);
717
718 ret = spi_sync(ks->spidev, msg);
719 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000720 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000721}
722
723/**
724 * ks8851_done_tx - update and then free skbuff after transmitting
725 * @ks: The device state
726 * @txb: The buffer transmitted
727 */
728static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
729{
730 struct net_device *dev = ks->netdev;
731
732 dev->stats.tx_bytes += txb->len;
733 dev->stats.tx_packets++;
734
735 dev_kfree_skb(txb);
736}
737
738/**
739 * ks8851_tx_work - process tx packet(s)
740 * @work: The work strucutre what was scheduled.
741 *
742 * This is called when a number of packets have been scheduled for
743 * transmission and need to be sent to the device.
744 */
745static void ks8851_tx_work(struct work_struct *work)
746{
747 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
748 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000749 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000750
751 mutex_lock(&ks->lock);
752
753 while (!last) {
754 txb = skb_dequeue(&ks->txq);
755 last = skb_queue_empty(&ks->txq);
756
Abraham Arce761172f2010-04-16 14:48:43 +0000757 if (txb != NULL) {
758 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
759 ks8851_wrpkt(ks, txb, last);
760 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
761 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000762
Abraham Arce761172f2010-04-16 14:48:43 +0000763 ks8851_done_tx(ks, txb);
764 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000765 }
766
767 mutex_unlock(&ks->lock);
768}
769
770/**
771 * ks8851_set_powermode - set power mode of the device
772 * @ks: The device state
773 * @pwrmode: The power mode value to write to KS_PMECR.
774 *
775 * Change the power mode of the chip.
776 */
777static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
778{
779 unsigned pmecr;
780
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000781 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000782
783 pmecr = ks8851_rdreg16(ks, KS_PMECR);
784 pmecr &= ~PMECR_PM_MASK;
785 pmecr |= pwrmode;
786
787 ks8851_wrreg16(ks, KS_PMECR, pmecr);
788}
789
790/**
791 * ks8851_net_open - open network device
792 * @dev: The network device being opened.
793 *
794 * Called when the network device is marked active, such as a user executing
795 * 'ifconfig up' on the device.
796 */
797static int ks8851_net_open(struct net_device *dev)
798{
799 struct ks8851_net *ks = netdev_priv(dev);
800
801 /* lock the card, even if we may not actually be doing anything
802 * else at the moment */
803 mutex_lock(&ks->lock);
804
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000805 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000806
807 /* bring chip out of any power saving mode it was in */
808 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
809
810 /* issue a soft reset to the RX/TX QMU to put it into a known
811 * state. */
812 ks8851_soft_reset(ks, GRR_QMU);
813
814 /* setup transmission parameters */
815
816 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
817 TXCR_TXPE | /* pad to min length */
818 TXCR_TXCRC | /* add CRC */
819 TXCR_TXFCE)); /* enable flow control */
820
821 /* auto-increment tx data, reset tx pointer */
822 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
823
824 /* setup receiver control */
825
826 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
827 RXCR1_RXFCE | /* enable flow control */
828 RXCR1_RXBE | /* broadcast enable */
829 RXCR1_RXUE | /* unicast enable */
830 RXCR1_RXE)); /* enable rx block */
831
832 /* transfer entire frames out in one go */
833 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
834
835 /* set receive counter timeouts */
836 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
837 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
838 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
839
840 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
841 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
842 RXQCR_RXDTTE); /* IRQ on time exceeded */
843
844 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
845
846 /* clear then enable interrupts */
847
848#define STD_IRQ (IRQ_LCI | /* Link Change */ \
849 IRQ_TXI | /* TX done */ \
850 IRQ_RXI | /* RX done */ \
851 IRQ_SPIBEI | /* SPI bus error */ \
852 IRQ_TXPSI | /* TX process stop */ \
853 IRQ_RXPSI) /* RX process stop */
854
855 ks->rc_ier = STD_IRQ;
856 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
857 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
858
859 netif_start_queue(ks->netdev);
860
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000861 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000862
863 mutex_unlock(&ks->lock);
864 return 0;
865}
866
867/**
868 * ks8851_net_stop - close network device
869 * @dev: The device being closed.
870 *
871 * Called to close down a network device which has been active. Cancell any
872 * work, shutdown the RX and TX process and then place the chip into a low
873 * power state whilst it is not being used.
874 */
875static int ks8851_net_stop(struct net_device *dev)
876{
877 struct ks8851_net *ks = netdev_priv(dev);
878
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000879 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000880
881 netif_stop_queue(dev);
882
883 mutex_lock(&ks->lock);
884
885 /* stop any outstanding work */
886 flush_work(&ks->irq_work);
887 flush_work(&ks->tx_work);
888 flush_work(&ks->rxctrl_work);
889
890 /* turn off the IRQs and ack any outstanding */
891 ks8851_wrreg16(ks, KS_IER, 0x0000);
892 ks8851_wrreg16(ks, KS_ISR, 0xffff);
893
894 /* shutdown RX process */
895 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
896
897 /* shutdown TX process */
898 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
899
900 /* set powermode to soft power down to save power */
901 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
902
903 /* ensure any queued tx buffers are dumped */
904 while (!skb_queue_empty(&ks->txq)) {
905 struct sk_buff *txb = skb_dequeue(&ks->txq);
906
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000907 netif_dbg(ks, ifdown, ks->netdev,
908 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000909
910 dev_kfree_skb(txb);
911 }
912
913 mutex_unlock(&ks->lock);
914 return 0;
915}
916
917/**
918 * ks8851_start_xmit - transmit packet
919 * @skb: The buffer to transmit
920 * @dev: The device used to transmit the packet.
921 *
922 * Called by the network layer to transmit the @skb. Queue the packet for
923 * the device and schedule the necessary work to transmit the packet when
924 * it is free.
925 *
926 * We do this to firstly avoid sleeping with the network device locked,
927 * and secondly so we can round up more than one packet to transmit which
928 * means we can try and avoid generating too many transmit done interrupts.
929 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000930static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
931 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000932{
933 struct ks8851_net *ks = netdev_priv(dev);
934 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000935 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000936
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000937 netif_dbg(ks, tx_queued, ks->netdev,
938 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000939
940 spin_lock(&ks->statelock);
941
942 if (needed > ks->tx_space) {
943 netif_stop_queue(dev);
944 ret = NETDEV_TX_BUSY;
945 } else {
946 ks->tx_space -= needed;
947 skb_queue_tail(&ks->txq, skb);
948 }
949
950 spin_unlock(&ks->statelock);
951 schedule_work(&ks->tx_work);
952
953 return ret;
954}
955
956/**
957 * ks8851_rxctrl_work - work handler to change rx mode
958 * @work: The work structure this belongs to.
959 *
960 * Lock the device and issue the necessary changes to the receive mode from
961 * the network device layer. This is done so that we can do this without
962 * having to sleep whilst holding the network device lock.
963 *
964 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
965 * receive parameters are programmed, we issue a write to disable the RXQ and
966 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
967 * complete. The interrupt handler then writes the new values into the chip.
968 */
969static void ks8851_rxctrl_work(struct work_struct *work)
970{
971 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
972
973 mutex_lock(&ks->lock);
974
975 /* need to shutdown RXQ before modifying filter parameters */
976 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
977
978 mutex_unlock(&ks->lock);
979}
980
981static void ks8851_set_rx_mode(struct net_device *dev)
982{
983 struct ks8851_net *ks = netdev_priv(dev);
984 struct ks8851_rxctrl rxctrl;
985
986 memset(&rxctrl, 0, sizeof(rxctrl));
987
988 if (dev->flags & IFF_PROMISC) {
989 /* interface to receive everything */
990
991 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
992 } else if (dev->flags & IFF_ALLMULTI) {
993 /* accept all multicast packets */
994
995 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
996 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000997 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000998 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000999 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001000
1001 /* accept some multicast */
1002
Jiri Pirko22bedad2010-04-01 21:22:57 +00001003 netdev_for_each_mc_addr(ha, dev) {
1004 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001005 crc >>= (32 - 6); /* get top six bits */
1006
1007 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001008 }
1009
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001010 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001011 } else {
1012 /* just accept broadcast / unicast */
1013 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1014 }
1015
1016 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1017 RXCR1_RXBE | /* broadcast enable */
1018 RXCR1_RXE | /* RX process enable */
1019 RXCR1_RXFCE); /* enable flow control */
1020
1021 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1022
1023 /* schedule work to do the actual set of the data if needed */
1024
1025 spin_lock(&ks->statelock);
1026
1027 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1028 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1029 schedule_work(&ks->rxctrl_work);
1030 }
1031
1032 spin_unlock(&ks->statelock);
1033}
1034
1035static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1036{
1037 struct sockaddr *sa = addr;
1038
1039 if (netif_running(dev))
1040 return -EBUSY;
1041
1042 if (!is_valid_ether_addr(sa->sa_data))
1043 return -EADDRNOTAVAIL;
1044
1045 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1046 return ks8851_write_mac_addr(dev);
1047}
1048
1049static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1050{
1051 struct ks8851_net *ks = netdev_priv(dev);
1052
1053 if (!netif_running(dev))
1054 return -EINVAL;
1055
1056 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1057}
1058
1059static const struct net_device_ops ks8851_netdev_ops = {
1060 .ndo_open = ks8851_net_open,
1061 .ndo_stop = ks8851_net_stop,
1062 .ndo_do_ioctl = ks8851_net_ioctl,
1063 .ndo_start_xmit = ks8851_start_xmit,
1064 .ndo_set_mac_address = ks8851_set_mac_address,
1065 .ndo_set_rx_mode = ks8851_set_rx_mode,
1066 .ndo_change_mtu = eth_change_mtu,
1067 .ndo_validate_addr = eth_validate_addr,
1068};
1069
Sebastien Jana4bdfff2010-05-05 08:45:53 +00001070/* Companion eeprom access */
1071
1072enum { /* EEPROM programming states */
1073 EEPROM_CONTROL,
1074 EEPROM_ADDRESS,
1075 EEPROM_DATA,
1076 EEPROM_COMPLETE
1077};
1078
1079/**
1080 * ks8851_eeprom_read - read a 16bits word in ks8851 companion EEPROM
1081 * @dev: The network device the PHY is on.
1082 * @addr: EEPROM address to read
1083 *
1084 * eeprom_size: used to define the data coding length. Can be changed
1085 * through debug-fs.
1086 *
1087 * Programs a read on the EEPROM using ks8851 EEPROM SW access feature.
1088 * Warning: The READ feature is not supported on ks8851 revision 0.
1089 *
1090 * Rough programming model:
1091 * - on period start: set clock high and read value on bus
1092 * - on period / 2: set clock low and program value on bus
1093 * - start on period / 2
1094 */
1095unsigned int ks8851_eeprom_read(struct net_device *dev, unsigned int addr)
1096{
1097 struct ks8851_net *ks = netdev_priv(dev);
1098 int eepcr;
1099 int ctrl = EEPROM_OP_READ;
1100 int state = EEPROM_CONTROL;
1101 int bit_count = EEPROM_OP_LEN - 1;
1102 unsigned int data = 0;
1103 int dummy;
1104 unsigned int addr_len;
1105
1106 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1107
1108 /* start transaction: chip select high, authorize write */
1109 mutex_lock(&ks->lock);
1110 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1111 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1112 eepcr |= EEPCR_EECS;
1113 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1114 mutex_unlock(&ks->lock);
1115
1116 while (state != EEPROM_COMPLETE) {
1117 /* falling clock period starts... */
1118 /* set EED_IO pin for control and address */
1119 eepcr &= ~EEPCR_EEDO;
1120 switch (state) {
1121 case EEPROM_CONTROL:
1122 eepcr |= ((ctrl >> bit_count) & 1) << 2;
1123 if (bit_count-- <= 0) {
1124 bit_count = addr_len - 1;
1125 state = EEPROM_ADDRESS;
1126 }
1127 break;
1128 case EEPROM_ADDRESS:
1129 eepcr |= ((addr >> bit_count) & 1) << 2;
1130 bit_count--;
1131 break;
1132 case EEPROM_DATA:
1133 /* Change to receive mode */
1134 eepcr &= ~EEPCR_EESRWA;
1135 break;
1136 }
1137
1138 /* lower clock */
1139 eepcr &= ~EEPCR_EESCK;
1140
1141 mutex_lock(&ks->lock);
1142 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1143 mutex_unlock(&ks->lock);
1144
1145 /* waitread period / 2 */
1146 udelay(EEPROM_SK_PERIOD / 2);
1147
1148 /* rising clock period starts... */
1149
1150 /* raise clock */
1151 mutex_lock(&ks->lock);
1152 eepcr |= EEPCR_EESCK;
1153 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1154 mutex_unlock(&ks->lock);
1155
1156 /* Manage read */
1157 switch (state) {
1158 case EEPROM_ADDRESS:
1159 if (bit_count < 0) {
1160 bit_count = EEPROM_DATA_LEN - 1;
1161 state = EEPROM_DATA;
1162 }
1163 break;
1164 case EEPROM_DATA:
1165 mutex_lock(&ks->lock);
1166 dummy = ks8851_rdreg16(ks, KS_EEPCR);
1167 mutex_unlock(&ks->lock);
1168 data |= ((dummy >> EEPCR_EESB_OFFSET) & 1) << bit_count;
1169 if (bit_count-- <= 0)
1170 state = EEPROM_COMPLETE;
1171 break;
1172 }
1173
1174 /* wait period / 2 */
1175 udelay(EEPROM_SK_PERIOD / 2);
1176 }
1177
1178 /* close transaction */
1179 mutex_lock(&ks->lock);
1180 eepcr &= ~EEPCR_EECS;
1181 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1182 eepcr = 0;
1183 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1184 mutex_unlock(&ks->lock);
1185
1186 return data;
1187}
1188
1189/**
1190 * ks8851_eeprom_write - write a 16bits word in ks8851 companion EEPROM
1191 * @dev: The network device the PHY is on.
1192 * @op: operand (can be WRITE, EWEN, EWDS)
1193 * @addr: EEPROM address to write
1194 * @data: data to write
1195 *
1196 * eeprom_size: used to define the data coding length. Can be changed
1197 * through debug-fs.
1198 *
1199 * Programs a write on the EEPROM using ks8851 EEPROM SW access feature.
1200 *
1201 * Note that a write enable is required before writing data.
1202 *
1203 * Rough programming model:
1204 * - on period start: set clock high
1205 * - on period / 2: set clock low and program value on bus
1206 * - start on period / 2
1207 */
1208void ks8851_eeprom_write(struct net_device *dev, unsigned int op,
1209 unsigned int addr, unsigned int data)
1210{
1211 struct ks8851_net *ks = netdev_priv(dev);
1212 int eepcr;
1213 int state = EEPROM_CONTROL;
1214 int bit_count = EEPROM_OP_LEN - 1;
1215 unsigned int addr_len;
1216
1217 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1218
1219 switch (op) {
1220 case EEPROM_OP_EWEN:
1221 addr = 0x30;
1222 break;
1223 case EEPROM_OP_EWDS:
1224 addr = 0;
1225 break;
1226 }
1227
1228 /* start transaction: chip select high, authorize write */
1229 mutex_lock(&ks->lock);
1230 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1231 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1232 eepcr |= EEPCR_EECS;
1233 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1234 mutex_unlock(&ks->lock);
1235
1236 while (state != EEPROM_COMPLETE) {
1237 /* falling clock period starts... */
1238 /* set EED_IO pin for control and address */
1239 eepcr &= ~EEPCR_EEDO;
1240 switch (state) {
1241 case EEPROM_CONTROL:
1242 eepcr |= ((op >> bit_count) & 1) << 2;
1243 if (bit_count-- <= 0) {
1244 bit_count = addr_len - 1;
1245 state = EEPROM_ADDRESS;
1246 }
1247 break;
1248 case EEPROM_ADDRESS:
1249 eepcr |= ((addr >> bit_count) & 1) << 2;
1250 if (bit_count-- <= 0) {
1251 if (op == EEPROM_OP_WRITE) {
1252 bit_count = EEPROM_DATA_LEN - 1;
1253 state = EEPROM_DATA;
1254 } else {
1255 state = EEPROM_COMPLETE;
1256 }
1257 }
1258 break;
1259 case EEPROM_DATA:
1260 eepcr |= ((data >> bit_count) & 1) << 2;
1261 if (bit_count-- <= 0)
1262 state = EEPROM_COMPLETE;
1263 break;
1264 }
1265
1266 /* lower clock */
1267 eepcr &= ~EEPCR_EESCK;
1268
1269 mutex_lock(&ks->lock);
1270 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1271 mutex_unlock(&ks->lock);
1272
1273 /* wait period / 2 */
1274 udelay(EEPROM_SK_PERIOD / 2);
1275
1276 /* rising clock period starts... */
1277
1278 /* raise clock */
1279 eepcr |= EEPCR_EESCK;
1280 mutex_lock(&ks->lock);
1281 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1282 mutex_unlock(&ks->lock);
1283
1284 /* wait period / 2 */
1285 udelay(EEPROM_SK_PERIOD / 2);
1286 }
1287
1288 /* close transaction */
1289 mutex_lock(&ks->lock);
1290 eepcr &= ~EEPCR_EECS;
1291 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1292 eepcr = 0;
1293 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1294 mutex_unlock(&ks->lock);
1295
1296}
1297
Ben Dooks3ba81f32009-07-16 05:24:08 +00001298/* ethtool support */
1299
1300static void ks8851_get_drvinfo(struct net_device *dev,
1301 struct ethtool_drvinfo *di)
1302{
1303 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1304 strlcpy(di->version, "1.00", sizeof(di->version));
1305 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1306}
1307
1308static u32 ks8851_get_msglevel(struct net_device *dev)
1309{
1310 struct ks8851_net *ks = netdev_priv(dev);
1311 return ks->msg_enable;
1312}
1313
1314static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1315{
1316 struct ks8851_net *ks = netdev_priv(dev);
1317 ks->msg_enable = to;
1318}
1319
1320static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1321{
1322 struct ks8851_net *ks = netdev_priv(dev);
1323 return mii_ethtool_gset(&ks->mii, cmd);
1324}
1325
1326static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1327{
1328 struct ks8851_net *ks = netdev_priv(dev);
1329 return mii_ethtool_sset(&ks->mii, cmd);
1330}
1331
1332static u32 ks8851_get_link(struct net_device *dev)
1333{
1334 struct ks8851_net *ks = netdev_priv(dev);
1335 return mii_link_ok(&ks->mii);
1336}
1337
1338static int ks8851_nway_reset(struct net_device *dev)
1339{
1340 struct ks8851_net *ks = netdev_priv(dev);
1341 return mii_nway_restart(&ks->mii);
1342}
1343
Sebastien Jana84afa42010-05-05 08:45:54 +00001344static int ks8851_get_eeprom_len(struct net_device *dev)
1345{
1346 struct ks8851_net *ks = netdev_priv(dev);
1347 return ks->eeprom_size;
1348}
1349
1350static int ks8851_get_eeprom(struct net_device *dev,
1351 struct ethtool_eeprom *eeprom, u8 *bytes)
1352{
1353 struct ks8851_net *ks = netdev_priv(dev);
1354 u16 *eeprom_buff;
1355 int first_word;
1356 int last_word;
1357 int ret_val = 0;
1358 u16 i;
1359
1360 if (eeprom->len == 0)
1361 return -EINVAL;
1362
1363 if (eeprom->len > ks->eeprom_size)
1364 return -EINVAL;
1365
1366 eeprom->magic = ks8851_rdreg16(ks, KS_CIDER);
1367
1368 first_word = eeprom->offset >> 1;
1369 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1370
1371 eeprom_buff = kmalloc(sizeof(u16) *
1372 (last_word - first_word + 1), GFP_KERNEL);
1373 if (!eeprom_buff)
1374 return -ENOMEM;
1375
1376 for (i = 0; i < last_word - first_word + 1; i++)
1377 eeprom_buff[i] = ks8851_eeprom_read(dev, first_word + 1);
1378
1379 /* Device's eeprom is little-endian, word addressable */
1380 for (i = 0; i < last_word - first_word + 1; i++)
1381 le16_to_cpus(&eeprom_buff[i]);
1382
1383 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
1384 kfree(eeprom_buff);
1385
1386 return ret_val;
1387}
1388
1389static int ks8851_set_eeprom(struct net_device *dev,
1390 struct ethtool_eeprom *eeprom, u8 *bytes)
1391{
1392 struct ks8851_net *ks = netdev_priv(dev);
1393 u16 *eeprom_buff;
1394 void *ptr;
1395 int max_len;
1396 int first_word;
1397 int last_word;
1398 int ret_val = 0;
1399 u16 i;
1400
1401 if (eeprom->len == 0)
1402 return -EOPNOTSUPP;
1403
1404 if (eeprom->len > ks->eeprom_size)
1405 return -EINVAL;
1406
1407 if (eeprom->magic != ks8851_rdreg16(ks, KS_CIDER))
1408 return -EFAULT;
1409
1410 first_word = eeprom->offset >> 1;
1411 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1412 max_len = (last_word - first_word + 1) * 2;
1413 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
1414 if (!eeprom_buff)
1415 return -ENOMEM;
1416
1417 ptr = (void *)eeprom_buff;
1418
1419 if (eeprom->offset & 1) {
1420 /* need read/modify/write of first changed EEPROM word */
1421 /* only the second byte of the word is being modified */
1422 eeprom_buff[0] = ks8851_eeprom_read(dev, first_word);
1423 ptr++;
1424 }
1425 if ((eeprom->offset + eeprom->len) & 1)
1426 /* need read/modify/write of last changed EEPROM word */
1427 /* only the first byte of the word is being modified */
1428 eeprom_buff[last_word - first_word] =
1429 ks8851_eeprom_read(dev, last_word);
1430
1431
1432 /* Device's eeprom is little-endian, word addressable */
1433 le16_to_cpus(&eeprom_buff[0]);
1434 le16_to_cpus(&eeprom_buff[last_word - first_word]);
1435
1436 memcpy(ptr, bytes, eeprom->len);
1437
1438 for (i = 0; i < last_word - first_word + 1; i++)
1439 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
1440
1441 ks8851_eeprom_write(dev, EEPROM_OP_EWEN, 0, 0);
1442
1443 for (i = 0; i < last_word - first_word + 1; i++) {
1444 ks8851_eeprom_write(dev, EEPROM_OP_WRITE, first_word + i,
1445 eeprom_buff[i]);
1446 mdelay(EEPROM_WRITE_TIME);
1447 }
1448
1449 ks8851_eeprom_write(dev, EEPROM_OP_EWDS, 0, 0);
1450
1451 kfree(eeprom_buff);
1452 return ret_val;
1453}
1454
Ben Dooks3ba81f32009-07-16 05:24:08 +00001455static const struct ethtool_ops ks8851_ethtool_ops = {
1456 .get_drvinfo = ks8851_get_drvinfo,
1457 .get_msglevel = ks8851_get_msglevel,
1458 .set_msglevel = ks8851_set_msglevel,
1459 .get_settings = ks8851_get_settings,
1460 .set_settings = ks8851_set_settings,
1461 .get_link = ks8851_get_link,
1462 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001463 .get_eeprom_len = ks8851_get_eeprom_len,
1464 .get_eeprom = ks8851_get_eeprom,
1465 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001466};
1467
1468/* MII interface controls */
1469
1470/**
1471 * ks8851_phy_reg - convert MII register into a KS8851 register
1472 * @reg: MII register number.
1473 *
1474 * Return the KS8851 register number for the corresponding MII PHY register
1475 * if possible. Return zero if the MII register has no direct mapping to the
1476 * KS8851 register set.
1477 */
1478static int ks8851_phy_reg(int reg)
1479{
1480 switch (reg) {
1481 case MII_BMCR:
1482 return KS_P1MBCR;
1483 case MII_BMSR:
1484 return KS_P1MBSR;
1485 case MII_PHYSID1:
1486 return KS_PHY1ILR;
1487 case MII_PHYSID2:
1488 return KS_PHY1IHR;
1489 case MII_ADVERTISE:
1490 return KS_P1ANAR;
1491 case MII_LPA:
1492 return KS_P1ANLPR;
1493 }
1494
1495 return 0x0;
1496}
1497
1498/**
1499 * ks8851_phy_read - MII interface PHY register read.
1500 * @dev: The network device the PHY is on.
1501 * @phy_addr: Address of PHY (ignored as we only have one)
1502 * @reg: The register to read.
1503 *
1504 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001505 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001506 * are always returned as zero.
1507 *
1508 * We return zero for unsupported registers as the MII code does not check
1509 * the value returned for any error status, and simply returns it to the
1510 * caller. The mii-tool that the driver was tested with takes any -ve error
1511 * as real PHY capabilities, thus displaying incorrect data to the user.
1512 */
1513static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1514{
1515 struct ks8851_net *ks = netdev_priv(dev);
1516 int ksreg;
1517 int result;
1518
1519 ksreg = ks8851_phy_reg(reg);
1520 if (!ksreg)
1521 return 0x0; /* no error return allowed, so use zero */
1522
1523 mutex_lock(&ks->lock);
1524 result = ks8851_rdreg16(ks, ksreg);
1525 mutex_unlock(&ks->lock);
1526
1527 return result;
1528}
1529
1530static void ks8851_phy_write(struct net_device *dev,
1531 int phy, int reg, int value)
1532{
1533 struct ks8851_net *ks = netdev_priv(dev);
1534 int ksreg;
1535
1536 ksreg = ks8851_phy_reg(reg);
1537 if (ksreg) {
1538 mutex_lock(&ks->lock);
1539 ks8851_wrreg16(ks, ksreg, value);
1540 mutex_unlock(&ks->lock);
1541 }
1542}
1543
1544/**
1545 * ks8851_read_selftest - read the selftest memory info.
1546 * @ks: The device state
1547 *
1548 * Read and check the TX/RX memory selftest information.
1549 */
1550static int ks8851_read_selftest(struct ks8851_net *ks)
1551{
1552 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1553 int ret = 0;
1554 unsigned rd;
1555
1556 rd = ks8851_rdreg16(ks, KS_MBIR);
1557
1558 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001559 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001560 return 0;
1561 }
1562
1563 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001564 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001565 ret |= 1;
1566 }
1567
1568 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001569 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001570 ret |= 2;
1571 }
1572
1573 return 0;
1574}
1575
1576/* driver bus management functions */
1577
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001578#ifdef CONFIG_PM
1579static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1580{
1581 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1582 struct net_device *dev = ks->netdev;
1583
1584 if (netif_running(dev)) {
1585 netif_device_detach(dev);
1586 ks8851_net_stop(dev);
1587 }
1588
1589 return 0;
1590}
1591
1592static int ks8851_resume(struct spi_device *spi)
1593{
1594 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1595 struct net_device *dev = ks->netdev;
1596
1597 if (netif_running(dev)) {
1598 ks8851_net_open(dev);
1599 netif_device_attach(dev);
1600 }
1601
1602 return 0;
1603}
1604#else
1605#define ks8851_suspend NULL
1606#define ks8851_resume NULL
1607#endif
1608
Ben Dooks3ba81f32009-07-16 05:24:08 +00001609static int __devinit ks8851_probe(struct spi_device *spi)
1610{
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001611 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001612 struct net_device *ndev;
1613 struct ks8851_net *ks;
1614 int ret;
1615
1616 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1617 if (!ndev) {
1618 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1619 return -ENOMEM;
1620 }
1621
1622 spi->bits_per_word = 8;
1623
1624 ks = netdev_priv(ndev);
1625
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001626 ks->vdd_io = regulator_get(&spi->dev, "vdd_io");
1627 ks->vdd_phy = regulator_get(&spi->dev, "vdd_phy");
1628
1629 if (!IS_ERR(ks->vdd_io))
1630 regulator_enable(ks->vdd_io);
1631
1632 if (!IS_ERR(ks->vdd_phy))
1633 regulator_enable(ks->vdd_phy);
1634
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001635 if (pdata && gpio_is_valid(pdata->irq_gpio)) {
1636 ret = gpio_request(pdata->irq_gpio, "ks8851_irq");
1637 if (ret) {
1638 pr_err("ks8851 gpio_request failed: %d\n", ret);
1639 goto err_irq_gpio;
1640 }
1641 }
1642
1643 if (pdata && gpio_is_valid(pdata->rst_gpio)) {
1644 ret = gpio_request(pdata->rst_gpio, "ks8851_rst");
1645 if (ret) {
1646 pr_err("ks8851 gpio_request failed: %d\n", ret);
1647 goto err_rst_gpio;
1648 }
1649 gpio_direction_output(pdata->rst_gpio, 1);
1650 }
1651
Ben Dooks3ba81f32009-07-16 05:24:08 +00001652 ks->netdev = ndev;
1653 ks->spidev = spi;
1654 ks->tx_space = 6144;
1655
1656 mutex_init(&ks->lock);
1657 spin_lock_init(&ks->statelock);
1658
1659 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1660 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1661 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1662
1663 /* initialise pre-made spi transfer messages */
1664
1665 spi_message_init(&ks->spi_msg1);
1666 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1667
1668 spi_message_init(&ks->spi_msg2);
1669 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1670 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1671
1672 /* setup mii state */
1673 ks->mii.dev = ndev;
1674 ks->mii.phy_id = 1,
1675 ks->mii.phy_id_mask = 1;
1676 ks->mii.reg_num_mask = 0xf;
1677 ks->mii.mdio_read = ks8851_phy_read;
1678 ks->mii.mdio_write = ks8851_phy_write;
1679
1680 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1681
1682 /* set the default message enable */
1683 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1684 NETIF_MSG_PROBE |
1685 NETIF_MSG_LINK));
1686
1687 skb_queue_head_init(&ks->txq);
1688
1689 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1690 SET_NETDEV_DEV(ndev, &spi->dev);
1691
1692 dev_set_drvdata(&spi->dev, ks);
1693
1694 ndev->if_port = IF_PORT_100BASET;
1695 ndev->netdev_ops = &ks8851_netdev_ops;
1696 ndev->irq = spi->irq;
1697
Ben Dooks57dada62009-10-19 23:49:03 +00001698 /* issue a global soft reset to reset the device. */
1699 ks8851_soft_reset(ks, GRR_GSR);
1700
Ben Dooks3ba81f32009-07-16 05:24:08 +00001701 /* simple check for a valid chip being connected to the bus */
1702
1703 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1704 dev_err(&spi->dev, "failed to read device ID\n");
1705 ret = -ENODEV;
1706 goto err_id;
1707 }
1708
Sebastien Jan7d997462010-05-05 08:45:52 +00001709 /* cache the contents of the CCR register for EEPROM, etc. */
1710 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1711
1712 if (ks->rc_ccr & CCR_EEPROM)
1713 ks->eeprom_size = 128;
1714 else
1715 ks->eeprom_size = 0;
1716
Ben Dooks3ba81f32009-07-16 05:24:08 +00001717 ks8851_read_selftest(ks);
1718 ks8851_init_mac(ks);
1719
1720 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1721 ndev->name, ks);
1722 if (ret < 0) {
1723 dev_err(&spi->dev, "failed to get irq\n");
1724 goto err_irq;
1725 }
1726
1727 ret = register_netdev(ndev);
1728 if (ret) {
1729 dev_err(&spi->dev, "failed to register network device\n");
1730 goto err_netdev;
1731 }
1732
Ben Dooksff47cb02010-04-29 13:16:26 +00001733 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001734 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
Ben Dooksff47cb02010-04-29 13:16:26 +00001735 ndev->dev_addr, ndev->irq,
1736 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001737
1738 return 0;
1739
1740
1741err_netdev:
1742 free_irq(ndev->irq, ndev);
1743
1744err_id:
1745err_irq:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 if (!IS_ERR(ks->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001747 regulator_disable(ks->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 regulator_put(ks->vdd_io);
1749 }
1750
1751 if (!IS_ERR(ks->vdd_phy)) {
1752 regulator_disable(ks->vdd_phy);
1753 regulator_put(ks->vdd_phy);
1754 }
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001755
Stepan Moskovchenko79b444f2011-10-12 13:47:39 -07001756 free_netdev(ndev);
1757
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001758 if (pdata && gpio_is_valid(pdata->rst_gpio))
1759 gpio_free(pdata->rst_gpio);
1760
1761err_rst_gpio:
1762 if (pdata && gpio_is_valid(pdata->irq_gpio))
1763 gpio_free(pdata->irq_gpio);
1764
1765err_irq_gpio:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001766 return ret;
1767}
1768
1769static int __devexit ks8851_remove(struct spi_device *spi)
1770{
1771 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001772 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001773
1774 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001775 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 if (!IS_ERR(priv->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001778 regulator_disable(priv->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779 regulator_put(priv->vdd_io);
1780 }
1781
1782 if (!IS_ERR(priv->vdd_phy)) {
1783 regulator_disable(priv->vdd_phy);
1784 regulator_put(priv->vdd_phy);
1785 }
1786
Ben Dooks3ba81f32009-07-16 05:24:08 +00001787 unregister_netdev(priv->netdev);
1788 free_irq(spi->irq, priv);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001789
1790 if (pdata && gpio_is_valid(pdata->irq_gpio))
1791 gpio_free(pdata->irq_gpio);
1792
1793 if (pdata && gpio_is_valid(pdata->rst_gpio))
1794 gpio_free(pdata->rst_gpio);
1795
Ben Dooks3ba81f32009-07-16 05:24:08 +00001796 free_netdev(priv->netdev);
1797
1798 return 0;
1799}
1800
1801static struct spi_driver ks8851_driver = {
1802 .driver = {
1803 .name = "ks8851",
1804 .owner = THIS_MODULE,
1805 },
1806 .probe = ks8851_probe,
1807 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001808 .suspend = ks8851_suspend,
1809 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001810};
1811
1812static int __init ks8851_init(void)
1813{
1814 return spi_register_driver(&ks8851_driver);
1815}
1816
1817static void __exit ks8851_exit(void)
1818{
1819 spi_unregister_driver(&ks8851_driver);
1820}
1821
1822module_init(ks8851_init);
1823module_exit(ks8851_exit);
1824
1825MODULE_DESCRIPTION("KS8851 Network driver");
1826MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1827MODULE_LICENSE("GPL");
1828
1829module_param_named(message, msg_enable, int, 0);
1830MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001831MODULE_ALIAS("spi:ks8851");