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