blob: 31dabd53004a0c98610f730be742da92b6f3a980 [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 Dooks1741a392010-04-29 13:16:28 +000025#include <linux/eeprom_93cx6.h>
26
Ben Dooks3ba81f32009-07-16 05:24:08 +000027#include <linux/spi/spi.h>
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -070028#include <linux/ks8851.h>
29#include <linux/gpio.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000030
31#include "ks8851.h"
32
33/**
34 * struct ks8851_rxctrl - KS8851 driver rx control
35 * @mchash: Multicast hash-table data.
36 * @rxcr1: KS_RXCR1 register setting
37 * @rxcr2: KS_RXCR2 register setting
38 *
39 * Representation of the settings needs to control the receive filtering
40 * such as the multicast hash-filter and the receive register settings. This
41 * is used to make the job of working out if the receive settings change and
42 * then issuing the new settings to the worker that will send the necessary
43 * commands.
44 */
45struct ks8851_rxctrl {
46 u16 mchash[4];
47 u16 rxcr1;
48 u16 rxcr2;
49};
50
51/**
52 * union ks8851_tx_hdr - tx header data
53 * @txb: The header as bytes
54 * @txw: The header as 16bit, little-endian words
55 *
56 * A dual representation of the tx header data to allow
57 * access to individual bytes, and to allow 16bit accesses
58 * with 16bit alignment.
59 */
60union ks8851_tx_hdr {
61 u8 txb[6];
62 __le16 txw[3];
63};
64
65/**
66 * struct ks8851_net - KS8851 driver private data
67 * @netdev: The network device we're bound to
68 * @spidev: The spi device we're bound to.
69 * @lock: Lock to ensure that the device is not accessed when busy.
70 * @statelock: Lock on this structure for tx list.
71 * @mii: The MII state information for the mii calls.
72 * @rxctrl: RX settings for @rxctrl_work.
73 * @tx_work: Work queue for tx packets
74 * @irq_work: Work queue for servicing interrupts
75 * @rxctrl_work: Work queue for updating RX mode and multicast lists
76 * @txq: Queue of packets for transmission.
77 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
78 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
79 * @txh: Space for generating packet TX header in DMA-able data
80 * @rxd: Space for receiving SPI data, in DMA-able space.
81 * @txd: Space for transmitting SPI data, in DMA-able space.
82 * @msg_enable: The message flags controlling driver output (see ethtool).
83 * @fid: Incrementing frame id tag.
84 * @rc_ier: Cached copy of KS_IER.
Sebastien Jan7d997462010-05-05 08:45:52 +000085 * @rc_ccr: Cached copy of KS_CCR.
Ben Dooks3ba81f32009-07-16 05:24:08 +000086 * @rc_rxqcr: Cached copy of KS_RXQCR.
Sebastien Jan7d997462010-05-05 08:45:52 +000087 * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
Ben Dooks1741a392010-04-29 13:16:28 +000088 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
Ben Dooks3ba81f32009-07-16 05:24:08 +000089 *
90 * The @lock ensures that the chip is protected when certain operations are
91 * in progress. When the read or write packet transfer is in progress, most
92 * of the chip registers are not ccessible until the transfer is finished and
93 * the DMA has been de-asserted.
94 *
95 * The @statelock is used to protect information in the structure which may
96 * need to be accessed via several sources, such as the network driver layer
97 * or one of the work queues.
98 *
99 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
100 * wants to DMA map them, it will not have any problems with data the driver
101 * modifies.
102 */
103struct ks8851_net {
104 struct net_device *netdev;
105 struct spi_device *spidev;
106 struct mutex lock;
107 spinlock_t statelock;
108
109 union ks8851_tx_hdr txh ____cacheline_aligned;
110 u8 rxd[8];
111 u8 txd[8];
112
113 u32 msg_enable ____cacheline_aligned;
114 u16 tx_space;
115 u8 fid;
116
117 u16 rc_ier;
118 u16 rc_rxqcr;
Sebastien Jan7d997462010-05-05 08:45:52 +0000119 u16 rc_ccr;
120 u16 eeprom_size;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000121
122 struct mii_if_info mii;
123 struct ks8851_rxctrl rxctrl;
124
125 struct work_struct tx_work;
126 struct work_struct irq_work;
127 struct work_struct rxctrl_work;
128
129 struct sk_buff_head txq;
130
131 struct spi_message spi_msg1;
132 struct spi_message spi_msg2;
133 struct spi_transfer spi_xfer1;
134 struct spi_transfer spi_xfer2[2];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 struct regulator *vdd_io;
136 struct regulator *vdd_phy;
Ben Dooks1741a392010-04-29 13:16:28 +0000137
138 struct eeprom_93cx6 eeprom;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000139};
140
141static int msg_enable;
142
Ben Dooks3ba81f32009-07-16 05:24:08 +0000143/* shift for byte-enable data */
144#define BYTE_EN(_x) ((_x) << 2)
145
146/* turn register number and byte-enable mask into data for start of packet */
147#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
148
149/* SPI register read/write calls.
150 *
151 * All these calls issue SPI transactions to access the chip's registers. They
152 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300153 * chip is busy transferring packet data (RX/TX FIFO accesses).
Ben Dooks3ba81f32009-07-16 05:24:08 +0000154 */
155
156/**
157 * ks8851_wrreg16 - write 16bit register value to chip
158 * @ks: The chip state
159 * @reg: The register address
160 * @val: The value to write
161 *
162 * Issue a write to put the value @val into the register specified in @reg.
163 */
164static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
165{
166 struct spi_transfer *xfer = &ks->spi_xfer1;
167 struct spi_message *msg = &ks->spi_msg1;
168 __le16 txb[2];
169 int ret;
170
171 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
172 txb[1] = cpu_to_le16(val);
173
174 xfer->tx_buf = txb;
175 xfer->rx_buf = NULL;
176 xfer->len = 4;
177
178 ret = spi_sync(ks->spidev, msg);
179 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000180 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000181}
182
183/**
Ben Dooks160d0fa2009-10-19 23:49:04 +0000184 * ks8851_wrreg8 - write 8bit register value to chip
185 * @ks: The chip state
186 * @reg: The register address
187 * @val: The value to write
188 *
189 * Issue a write to put the value @val into the register specified in @reg.
190 */
191static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
192{
193 struct spi_transfer *xfer = &ks->spi_xfer1;
194 struct spi_message *msg = &ks->spi_msg1;
195 __le16 txb[2];
196 int ret;
197 int bit;
198
199 bit = 1 << (reg & 3);
200
201 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
202 txb[1] = val;
203
204 xfer->tx_buf = txb;
205 xfer->rx_buf = NULL;
206 xfer->len = 3;
207
208 ret = spi_sync(ks->spidev, msg);
209 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000210 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks160d0fa2009-10-19 23:49:04 +0000211}
212
213/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000214 * ks8851_rx_1msg - select whether to use one or two messages for spi read
215 * @ks: The device structure
216 *
217 * Return whether to generate a single message with a tx and rx buffer
218 * supplied to spi_sync(), or alternatively send the tx and rx buffers
219 * as separate messages.
220 *
221 * Depending on the hardware in use, a single message may be more efficient
222 * on interrupts or work done by the driver.
223 *
224 * This currently always returns true until we add some per-device data passed
225 * from the platform code to specify which mode is better.
226 */
227static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
228{
229 return true;
230}
231
232/**
233 * ks8851_rdreg - issue read register command and return the data
234 * @ks: The device state
235 * @op: The register address and byte enables in message format.
236 * @rxb: The RX buffer to return the result into
237 * @rxl: The length of data expected.
238 *
239 * This is the low level read call that issues the necessary spi message(s)
240 * to read data from the register specified in @op.
241 */
242static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
243 u8 *rxb, unsigned rxl)
244{
245 struct spi_transfer *xfer;
246 struct spi_message *msg;
247 __le16 *txb = (__le16 *)ks->txd;
248 u8 *trx = ks->rxd;
249 int ret;
250
251 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
252
253 if (ks8851_rx_1msg(ks)) {
254 msg = &ks->spi_msg1;
255 xfer = &ks->spi_xfer1;
256
257 xfer->tx_buf = txb;
258 xfer->rx_buf = trx;
259 xfer->len = rxl + 2;
260 } else {
261 msg = &ks->spi_msg2;
262 xfer = ks->spi_xfer2;
263
264 xfer->tx_buf = txb;
265 xfer->rx_buf = NULL;
266 xfer->len = 2;
267
268 xfer++;
269 xfer->tx_buf = NULL;
270 xfer->rx_buf = trx;
271 xfer->len = rxl;
272 }
273
274 ret = spi_sync(ks->spidev, msg);
275 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000276 netdev_err(ks->netdev, "read: spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000277 else if (ks8851_rx_1msg(ks))
278 memcpy(rxb, trx + 2, rxl);
279 else
280 memcpy(rxb, trx, rxl);
281}
282
283/**
284 * ks8851_rdreg8 - read 8 bit register from device
285 * @ks: The chip information
286 * @reg: The register address
287 *
288 * Read a 8bit register from the chip, returning the result
289*/
290static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
291{
292 u8 rxb[1];
293
294 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
295 return rxb[0];
296}
297
298/**
299 * ks8851_rdreg16 - read 16 bit register from device
300 * @ks: The chip information
301 * @reg: The register address
302 *
303 * Read a 16bit register from the chip, returning the result
304*/
305static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
306{
307 __le16 rx = 0;
308
309 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
310 return le16_to_cpu(rx);
311}
312
313/**
314 * ks8851_rdreg32 - read 32 bit register from device
315 * @ks: The chip information
316 * @reg: The register address
317 *
318 * Read a 32bit register from the chip.
319 *
320 * Note, this read requires the address be aligned to 4 bytes.
321*/
322static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
323{
324 __le32 rx = 0;
325
326 WARN_ON(reg & 3);
327
328 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
329 return le32_to_cpu(rx);
330}
331
332/**
333 * ks8851_soft_reset - issue one of the soft reset to the device
334 * @ks: The device state.
335 * @op: The bit(s) to set in the GRR
336 *
337 * Issue the relevant soft-reset command to the device's GRR register
338 * specified by @op.
339 *
340 * Note, the delays are in there as a caution to ensure that the reset
341 * has time to take effect and then complete. Since the datasheet does
342 * not currently specify the exact sequence, we have chosen something
343 * that seems to work with our device.
344 */
345static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
346{
347 ks8851_wrreg16(ks, KS_GRR, op);
348 mdelay(1); /* wait a short time to effect reset */
349 ks8851_wrreg16(ks, KS_GRR, 0);
350 mdelay(1); /* wait for condition to clear */
351}
352
353/**
Tristram Ha9bf885e2010-04-29 13:16:27 +0000354 * ks8851_set_powermode - set power mode of the device
355 * @ks: The device state
356 * @pwrmode: The power mode value to write to KS_PMECR.
357 *
358 * Change the power mode of the chip.
359 */
360static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
361{
362 unsigned pmecr;
363
364 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
365
366 pmecr = ks8851_rdreg16(ks, KS_PMECR);
367 pmecr &= ~PMECR_PM_MASK;
368 pmecr |= pwrmode;
369
370 ks8851_wrreg16(ks, KS_PMECR, pmecr);
371}
372
373/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000374 * ks8851_write_mac_addr - write mac address to device registers
375 * @dev: The network device
376 *
377 * Update the KS8851 MAC address registers from the address in @dev.
378 *
379 * This call assumes that the chip is not running, so there is no need to
380 * shutdown the RXQ process whilst setting this.
381*/
382static int ks8851_write_mac_addr(struct net_device *dev)
383{
384 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000385 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000386
387 mutex_lock(&ks->lock);
388
Tristram Ha9bf885e2010-04-29 13:16:27 +0000389 /*
390 * Wake up chip in case it was powered off when stopped; otherwise,
391 * the first write to the MAC address does not take effect.
392 */
393 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000394 for (i = 0; i < ETH_ALEN; i++)
395 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
Tristram Ha9bf885e2010-04-29 13:16:27 +0000396 if (!netif_running(dev))
397 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000398
399 mutex_unlock(&ks->lock);
400
401 return 0;
402}
403
404/**
Ben Dooksff47cb02010-04-29 13:16:26 +0000405 * ks8851_read_mac_addr - read mac address from device registers
406 * @dev: The network device
Ben Dooks3ba81f32009-07-16 05:24:08 +0000407 *
Ben Dooksff47cb02010-04-29 13:16:26 +0000408 * Update our copy of the KS8851 MAC address from the registers of @dev.
409*/
410static void ks8851_read_mac_addr(struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000411{
Ben Dooksff47cb02010-04-29 13:16:26 +0000412 struct ks8851_net *ks = netdev_priv(dev);
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700413 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000414
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700415 mutex_lock(&ks->lock);
416
417 for (i = 0; i < ETH_ALEN; i++)
418 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
419
420 mutex_unlock(&ks->lock);
Ben Dooksff47cb02010-04-29 13:16:26 +0000421}
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700422
Ben Dooksff47cb02010-04-29 13:16:26 +0000423/**
424 * ks8851_init_mac - initialise the mac address
425 * @ks: The device structure
426 *
427 * Get or create the initial mac address for the device and then set that
428 * into the station address register. If there is an EEPROM present, then
429 * we try that. If no valid mac address is found we use random_ether_addr()
430 * to create a new one.
431 */
432static void ks8851_init_mac(struct ks8851_net *ks)
433{
434 struct net_device *dev = ks->netdev;
435
436 /* first, try reading what we've got already */
437 if (ks->rc_ccr & CCR_EEPROM) {
438 ks8851_read_mac_addr(dev);
439 if (is_valid_ether_addr(dev->dev_addr))
440 return;
441
442 netdev_err(ks->netdev, "invalid mac address read %pM\n",
443 dev->dev_addr);
Stepan Moskovchenkodd58a032011-09-20 19:53:41 -0700444 }
Ben Dooksff47cb02010-04-29 13:16:26 +0000445
446 random_ether_addr(dev->dev_addr);
447 ks8851_write_mac_addr(dev);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000448}
449
450/**
451 * ks8851_irq - device interrupt handler
452 * @irq: Interrupt number passed from the IRQ hnalder.
453 * @pw: The private word passed to register_irq(), our struct ks8851_net.
454 *
455 * Disable the interrupt from happening again until we've processed the
456 * current status by scheduling ks8851_irq_work().
457 */
458static irqreturn_t ks8851_irq(int irq, void *pw)
459{
460 struct ks8851_net *ks = pw;
461
462 disable_irq_nosync(irq);
463 schedule_work(&ks->irq_work);
464 return IRQ_HANDLED;
465}
466
467/**
468 * ks8851_rdfifo - read data from the receive fifo
469 * @ks: The device state.
470 * @buff: The buffer address
471 * @len: The length of the data to read
472 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100473 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000474 * the FIFO into the buffer specified by @buff.
475 */
476static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
477{
478 struct spi_transfer *xfer = ks->spi_xfer2;
479 struct spi_message *msg = &ks->spi_msg2;
480 u8 txb[1];
481 int ret;
482
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000483 netif_dbg(ks, rx_status, ks->netdev,
484 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000485
486 /* set the operation we're issuing */
487 txb[0] = KS_SPIOP_RXFIFO;
488
489 xfer->tx_buf = txb;
490 xfer->rx_buf = NULL;
491 xfer->len = 1;
492
493 xfer++;
494 xfer->rx_buf = buff;
495 xfer->tx_buf = NULL;
496 xfer->len = len;
497
498 ret = spi_sync(ks->spidev, msg);
499 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000500 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000501}
502
503/**
504 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
505 * @ks: The device state
506 * @rxpkt: The data for the received packet
507 *
508 * Dump the initial data from the packet to dev_dbg().
509*/
510static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
511{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000512 netdev_dbg(ks->netdev,
513 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
514 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
515 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
516 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000517}
518
519/**
520 * ks8851_rx_pkts - receive packets from the host
521 * @ks: The device information.
522 *
523 * This is called from the IRQ work queue when the system detects that there
524 * are packets in the receive queue. Find out how many packets there are and
525 * read them from the FIFO.
526 */
527static void ks8851_rx_pkts(struct ks8851_net *ks)
528{
529 struct sk_buff *skb;
530 unsigned rxfc;
531 unsigned rxlen;
532 unsigned rxstat;
533 u32 rxh;
534 u8 *rxpkt;
535
536 rxfc = ks8851_rdreg8(ks, KS_RXFC);
537
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000538 netif_dbg(ks, rx_status, ks->netdev,
539 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000540
541 /* Currently we're issuing a read per packet, but we could possibly
542 * improve the code by issuing a single read, getting the receive
543 * header, allocating the packet and then reading the packet data
544 * out in one go.
545 *
546 * This form of operation would require us to hold the SPI bus'
547 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300548 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000549 */
550
551 for (; rxfc != 0; rxfc--) {
552 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
553 rxstat = rxh & 0xffff;
554 rxlen = rxh >> 16;
555
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000556 netif_dbg(ks, rx_status, ks->netdev,
557 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000558
559 /* the length of the packet includes the 32bit CRC */
560
561 /* set dma read address */
562 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
563
564 /* start the packet dma process, and set auto-dequeue rx */
565 ks8851_wrreg16(ks, KS_RXQCR,
566 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
567
Eric Dumazet972c40b2010-09-08 13:26:55 +0000568 if (rxlen > 4) {
569 unsigned int rxalign;
570
571 rxlen -= 4;
572 rxalign = ALIGN(rxlen, 4);
573 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
574 if (skb) {
575
576 /* 4 bytes of status header + 4 bytes of
577 * garbage: we put them before ethernet
578 * header, so that they are copied,
579 * but ignored.
580 */
581
582 rxpkt = skb_put(skb, rxlen) - 8;
583
584 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
585
586 if (netif_msg_pktdata(ks))
587 ks8851_dbg_dumpkkt(ks, rxpkt);
588
589 skb->protocol = eth_type_trans(skb, ks->netdev);
590 netif_rx(skb);
591
592 ks->netdev->stats.rx_packets++;
593 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000594 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000595 }
596
597 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
598 }
599}
600
601/**
602 * ks8851_irq_work - work queue handler for dealing with interrupt requests
603 * @work: The work structure that was scheduled by schedule_work()
604 *
605 * This is the handler invoked when the ks8851_irq() is called to find out
606 * what happened, as we cannot allow ourselves to sleep whilst waiting for
607 * anything other process has the chip's lock.
608 *
609 * Read the interrupt status, work out what needs to be done and then clear
610 * any of the interrupts that are not needed.
611 */
612static void ks8851_irq_work(struct work_struct *work)
613{
614 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
615 unsigned status;
616 unsigned handled = 0;
617
618 mutex_lock(&ks->lock);
619
620 status = ks8851_rdreg16(ks, KS_ISR);
621
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000622 netif_dbg(ks, intr, ks->netdev,
623 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000624
Stephen Boyd0397ad22012-05-08 18:39:43 -0700625 if (status & IRQ_LCI)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000626 handled |= IRQ_LCI;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000627
628 if (status & IRQ_LDI) {
629 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
630 pmecr &= ~PMECR_WKEVT_MASK;
631 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
632
633 handled |= IRQ_LDI;
634 }
635
636 if (status & IRQ_RXPSI)
637 handled |= IRQ_RXPSI;
638
639 if (status & IRQ_TXI) {
640 handled |= IRQ_TXI;
641
642 /* no lock here, tx queue should have been stopped */
643
644 /* update our idea of how much tx space is available to the
645 * system */
646 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
647
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000648 netif_dbg(ks, intr, ks->netdev,
649 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000650 }
651
652 if (status & IRQ_RXI)
653 handled |= IRQ_RXI;
654
655 if (status & IRQ_SPIBEI) {
656 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
657 handled |= IRQ_SPIBEI;
658 }
659
660 ks8851_wrreg16(ks, KS_ISR, handled);
661
662 if (status & IRQ_RXI) {
663 /* the datasheet says to disable the rx interrupt during
664 * packet read-out, however we're masking the interrupt
665 * from the device so do not bother masking just the RX
666 * from the device. */
667
668 ks8851_rx_pkts(ks);
669 }
670
671 /* if something stopped the rx process, probably due to wanting
672 * to change the rx settings, then do something about restarting
673 * it. */
674 if (status & IRQ_RXPSI) {
675 struct ks8851_rxctrl *rxc = &ks->rxctrl;
676
677 /* update the multicast hash table */
678 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
679 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
680 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
681 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
682
683 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
684 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
685 }
686
687 mutex_unlock(&ks->lock);
688
Stephen Boyd0397ad22012-05-08 18:39:43 -0700689 if (status & IRQ_LCI)
690 mii_check_link(&ks->mii);
691
Ben Dooks3ba81f32009-07-16 05:24:08 +0000692 if (status & IRQ_TXI)
693 netif_wake_queue(ks->netdev);
694
695 enable_irq(ks->netdev->irq);
696}
697
698/**
699 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300700 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000701 *
702 * Returns the size of the TXFIFO message needed to send
703 * this packet.
704 */
705static inline unsigned calc_txlen(unsigned len)
706{
707 return ALIGN(len + 4, 4);
708}
709
710/**
711 * ks8851_wrpkt - write packet to TX FIFO
712 * @ks: The device state.
713 * @txp: The sk_buff to transmit.
714 * @irq: IRQ on completion of the packet.
715 *
716 * Send the @txp to the chip. This means creating the relevant packet header
717 * specifying the length of the packet and the other information the chip
718 * needs, such as IRQ on completion. Send the header and the packet data to
719 * the device.
720 */
721static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
722{
723 struct spi_transfer *xfer = ks->spi_xfer2;
724 struct spi_message *msg = &ks->spi_msg2;
725 unsigned fid = 0;
726 int ret;
727
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000728 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
729 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000730
731 fid = ks->fid++;
732 fid &= TXFR_TXFID_MASK;
733
734 if (irq)
735 fid |= TXFR_TXIC; /* irq on completion */
736
737 /* start header at txb[1] to align txw entries */
738 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
739 ks->txh.txw[1] = cpu_to_le16(fid);
740 ks->txh.txw[2] = cpu_to_le16(txp->len);
741
742 xfer->tx_buf = &ks->txh.txb[1];
743 xfer->rx_buf = NULL;
744 xfer->len = 5;
745
746 xfer++;
747 xfer->tx_buf = txp->data;
748 xfer->rx_buf = NULL;
749 xfer->len = ALIGN(txp->len, 4);
750
751 ret = spi_sync(ks->spidev, msg);
752 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000753 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000754}
755
756/**
757 * ks8851_done_tx - update and then free skbuff after transmitting
758 * @ks: The device state
759 * @txb: The buffer transmitted
760 */
761static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
762{
763 struct net_device *dev = ks->netdev;
764
765 dev->stats.tx_bytes += txb->len;
766 dev->stats.tx_packets++;
767
768 dev_kfree_skb(txb);
769}
770
771/**
772 * ks8851_tx_work - process tx packet(s)
773 * @work: The work strucutre what was scheduled.
774 *
775 * This is called when a number of packets have been scheduled for
776 * transmission and need to be sent to the device.
777 */
778static void ks8851_tx_work(struct work_struct *work)
779{
780 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
781 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000782 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000783
784 mutex_lock(&ks->lock);
785
786 while (!last) {
787 txb = skb_dequeue(&ks->txq);
788 last = skb_queue_empty(&ks->txq);
789
Abraham Arce761172f2010-04-16 14:48:43 +0000790 if (txb != NULL) {
791 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
792 ks8851_wrpkt(ks, txb, last);
793 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
794 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000795
Abraham Arce761172f2010-04-16 14:48:43 +0000796 ks8851_done_tx(ks, txb);
797 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000798 }
799
800 mutex_unlock(&ks->lock);
801}
802
803/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000804 * ks8851_net_open - open network device
805 * @dev: The network device being opened.
806 *
807 * Called when the network device is marked active, such as a user executing
808 * 'ifconfig up' on the device.
809 */
810static int ks8851_net_open(struct net_device *dev)
811{
812 struct ks8851_net *ks = netdev_priv(dev);
813
814 /* lock the card, even if we may not actually be doing anything
815 * else at the moment */
816 mutex_lock(&ks->lock);
817
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000818 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000819
820 /* bring chip out of any power saving mode it was in */
821 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
822
823 /* issue a soft reset to the RX/TX QMU to put it into a known
824 * state. */
825 ks8851_soft_reset(ks, GRR_QMU);
826
827 /* setup transmission parameters */
828
829 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
830 TXCR_TXPE | /* pad to min length */
831 TXCR_TXCRC | /* add CRC */
832 TXCR_TXFCE)); /* enable flow control */
833
834 /* auto-increment tx data, reset tx pointer */
835 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
836
837 /* setup receiver control */
838
839 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
840 RXCR1_RXFCE | /* enable flow control */
841 RXCR1_RXBE | /* broadcast enable */
842 RXCR1_RXUE | /* unicast enable */
843 RXCR1_RXE)); /* enable rx block */
844
845 /* transfer entire frames out in one go */
846 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
847
848 /* set receive counter timeouts */
849 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
850 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
851 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
852
853 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
854 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
855 RXQCR_RXDTTE); /* IRQ on time exceeded */
856
857 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
858
859 /* clear then enable interrupts */
860
861#define STD_IRQ (IRQ_LCI | /* Link Change */ \
862 IRQ_TXI | /* TX done */ \
863 IRQ_RXI | /* RX done */ \
864 IRQ_SPIBEI | /* SPI bus error */ \
865 IRQ_TXPSI | /* TX process stop */ \
866 IRQ_RXPSI) /* RX process stop */
867
868 ks->rc_ier = STD_IRQ;
869 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
870 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
871
872 netif_start_queue(ks->netdev);
873
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000874 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000875
876 mutex_unlock(&ks->lock);
877 return 0;
878}
879
880/**
881 * ks8851_net_stop - close network device
882 * @dev: The device being closed.
883 *
884 * Called to close down a network device which has been active. Cancell any
885 * work, shutdown the RX and TX process and then place the chip into a low
886 * power state whilst it is not being used.
887 */
888static int ks8851_net_stop(struct net_device *dev)
889{
890 struct ks8851_net *ks = netdev_priv(dev);
891
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000892 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000893
894 netif_stop_queue(dev);
895
896 mutex_lock(&ks->lock);
897
898 /* stop any outstanding work */
899 flush_work(&ks->irq_work);
900 flush_work(&ks->tx_work);
901 flush_work(&ks->rxctrl_work);
902
903 /* turn off the IRQs and ack any outstanding */
904 ks8851_wrreg16(ks, KS_IER, 0x0000);
905 ks8851_wrreg16(ks, KS_ISR, 0xffff);
906
907 /* shutdown RX process */
908 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
909
910 /* shutdown TX process */
911 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
912
913 /* set powermode to soft power down to save power */
914 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
915
916 /* ensure any queued tx buffers are dumped */
917 while (!skb_queue_empty(&ks->txq)) {
918 struct sk_buff *txb = skb_dequeue(&ks->txq);
919
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000920 netif_dbg(ks, ifdown, ks->netdev,
921 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000922
923 dev_kfree_skb(txb);
924 }
925
926 mutex_unlock(&ks->lock);
927 return 0;
928}
929
930/**
931 * ks8851_start_xmit - transmit packet
932 * @skb: The buffer to transmit
933 * @dev: The device used to transmit the packet.
934 *
935 * Called by the network layer to transmit the @skb. Queue the packet for
936 * the device and schedule the necessary work to transmit the packet when
937 * it is free.
938 *
939 * We do this to firstly avoid sleeping with the network device locked,
940 * and secondly so we can round up more than one packet to transmit which
941 * means we can try and avoid generating too many transmit done interrupts.
942 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000943static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
944 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000945{
946 struct ks8851_net *ks = netdev_priv(dev);
947 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000948 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000949
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000950 netif_dbg(ks, tx_queued, ks->netdev,
951 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000952
953 spin_lock(&ks->statelock);
954
955 if (needed > ks->tx_space) {
956 netif_stop_queue(dev);
957 ret = NETDEV_TX_BUSY;
958 } else {
959 ks->tx_space -= needed;
960 skb_queue_tail(&ks->txq, skb);
961 }
962
963 spin_unlock(&ks->statelock);
964 schedule_work(&ks->tx_work);
965
966 return ret;
967}
968
969/**
970 * ks8851_rxctrl_work - work handler to change rx mode
971 * @work: The work structure this belongs to.
972 *
973 * Lock the device and issue the necessary changes to the receive mode from
974 * the network device layer. This is done so that we can do this without
975 * having to sleep whilst holding the network device lock.
976 *
977 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
978 * receive parameters are programmed, we issue a write to disable the RXQ and
979 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
980 * complete. The interrupt handler then writes the new values into the chip.
981 */
982static void ks8851_rxctrl_work(struct work_struct *work)
983{
984 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
985
986 mutex_lock(&ks->lock);
987
988 /* need to shutdown RXQ before modifying filter parameters */
989 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
990
991 mutex_unlock(&ks->lock);
992}
993
994static void ks8851_set_rx_mode(struct net_device *dev)
995{
996 struct ks8851_net *ks = netdev_priv(dev);
997 struct ks8851_rxctrl rxctrl;
998
999 memset(&rxctrl, 0, sizeof(rxctrl));
1000
1001 if (dev->flags & IFF_PROMISC) {
1002 /* interface to receive everything */
1003
1004 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
1005 } else if (dev->flags & IFF_ALLMULTI) {
1006 /* accept all multicast packets */
1007
1008 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1009 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001010 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001011 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001012 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001013
1014 /* accept some multicast */
1015
Jiri Pirko22bedad2010-04-01 21:22:57 +00001016 netdev_for_each_mc_addr(ha, dev) {
1017 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001018 crc >>= (32 - 6); /* get top six bits */
1019
1020 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001021 }
1022
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001023 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001024 } else {
1025 /* just accept broadcast / unicast */
1026 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1027 }
1028
1029 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1030 RXCR1_RXBE | /* broadcast enable */
1031 RXCR1_RXE | /* RX process enable */
1032 RXCR1_RXFCE); /* enable flow control */
1033
1034 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1035
1036 /* schedule work to do the actual set of the data if needed */
1037
1038 spin_lock(&ks->statelock);
1039
1040 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1041 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1042 schedule_work(&ks->rxctrl_work);
1043 }
1044
1045 spin_unlock(&ks->statelock);
1046}
1047
1048static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1049{
1050 struct sockaddr *sa = addr;
1051
1052 if (netif_running(dev))
1053 return -EBUSY;
1054
1055 if (!is_valid_ether_addr(sa->sa_data))
1056 return -EADDRNOTAVAIL;
1057
1058 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1059 return ks8851_write_mac_addr(dev);
1060}
1061
1062static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1063{
1064 struct ks8851_net *ks = netdev_priv(dev);
1065
1066 if (!netif_running(dev))
1067 return -EINVAL;
1068
1069 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1070}
1071
1072static const struct net_device_ops ks8851_netdev_ops = {
1073 .ndo_open = ks8851_net_open,
1074 .ndo_stop = ks8851_net_stop,
1075 .ndo_do_ioctl = ks8851_net_ioctl,
1076 .ndo_start_xmit = ks8851_start_xmit,
1077 .ndo_set_mac_address = ks8851_set_mac_address,
1078 .ndo_set_rx_mode = ks8851_set_rx_mode,
1079 .ndo_change_mtu = eth_change_mtu,
1080 .ndo_validate_addr = eth_validate_addr,
1081};
1082
1083/* ethtool support */
1084
1085static void ks8851_get_drvinfo(struct net_device *dev,
1086 struct ethtool_drvinfo *di)
1087{
1088 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1089 strlcpy(di->version, "1.00", sizeof(di->version));
1090 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1091}
1092
1093static u32 ks8851_get_msglevel(struct net_device *dev)
1094{
1095 struct ks8851_net *ks = netdev_priv(dev);
1096 return ks->msg_enable;
1097}
1098
1099static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1100{
1101 struct ks8851_net *ks = netdev_priv(dev);
1102 ks->msg_enable = to;
1103}
1104
1105static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1106{
1107 struct ks8851_net *ks = netdev_priv(dev);
1108 return mii_ethtool_gset(&ks->mii, cmd);
1109}
1110
1111static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1112{
1113 struct ks8851_net *ks = netdev_priv(dev);
1114 return mii_ethtool_sset(&ks->mii, cmd);
1115}
1116
1117static u32 ks8851_get_link(struct net_device *dev)
1118{
1119 struct ks8851_net *ks = netdev_priv(dev);
1120 return mii_link_ok(&ks->mii);
1121}
1122
1123static int ks8851_nway_reset(struct net_device *dev)
1124{
1125 struct ks8851_net *ks = netdev_priv(dev);
1126 return mii_nway_restart(&ks->mii);
1127}
1128
Ben Dooks1741a392010-04-29 13:16:28 +00001129/* EEPROM support */
1130
1131static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1132{
1133 struct ks8851_net *ks = ee->data;
1134 unsigned val;
1135
1136 val = ks8851_rdreg16(ks, KS_EEPCR);
1137
1138 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1139 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1140 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1141}
1142
1143static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1144{
1145 struct ks8851_net *ks = ee->data;
1146 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1147
1148 if (ee->drive_data)
1149 val |= EEPCR_EESRWA;
1150 if (ee->reg_data_in)
1151 val |= EEPCR_EEDO;
1152 if (ee->reg_data_clock)
1153 val |= EEPCR_EESCK;
1154 if (ee->reg_chip_select)
1155 val |= EEPCR_EECS;
1156
1157 ks8851_wrreg16(ks, KS_EEPCR, val);
1158}
1159
1160/**
1161 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1162 * @ks: The network deice state.
1163 *
1164 * Check for the presence of an EEPROM, and then activate software access
1165 * to the device.
1166 */
1167static int ks8851_eeprom_claim(struct ks8851_net *ks)
1168{
1169 if (!(ks->rc_ccr & CCR_EEPROM))
1170 return -ENOENT;
1171
1172 mutex_lock(&ks->lock);
1173
1174 /* start with clock low, cs high */
1175 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1176 return 0;
1177}
1178
1179/**
1180 * ks8851_eeprom_release - release the EEPROM interface
1181 * @ks: The device state
1182 *
1183 * Release the software access to the device EEPROM
1184 */
1185static void ks8851_eeprom_release(struct ks8851_net *ks)
1186{
1187 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1188
1189 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1190 mutex_unlock(&ks->lock);
1191}
1192
1193#define KS_EEPROM_MAGIC (0x00008851)
1194
1195static int ks8851_set_eeprom(struct net_device *dev,
1196 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001197{
1198 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks1741a392010-04-29 13:16:28 +00001199 int offset = ee->offset;
1200 int len = ee->len;
1201 u16 tmp;
1202
1203 /* currently only support byte writing */
1204 if (len != 1)
1205 return -EINVAL;
1206
1207 if (ee->magic != KS_EEPROM_MAGIC)
1208 return -EINVAL;
1209
1210 if (ks8851_eeprom_claim(ks))
1211 return -ENOENT;
1212
1213 eeprom_93cx6_wren(&ks->eeprom, true);
1214
1215 /* ethtool currently only supports writing bytes, which means
1216 * we have to read/modify/write our 16bit EEPROMs */
1217
1218 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1219
1220 if (offset & 1) {
1221 tmp &= 0xff;
1222 tmp |= *data << 8;
1223 } else {
1224 tmp &= 0xff00;
1225 tmp |= *data;
1226 }
1227
1228 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1229 eeprom_93cx6_wren(&ks->eeprom, false);
1230
1231 ks8851_eeprom_release(ks);
1232
1233 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001234}
1235
1236static int ks8851_get_eeprom(struct net_device *dev,
Ben Dooks1741a392010-04-29 13:16:28 +00001237 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001238{
1239 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks1741a392010-04-29 13:16:28 +00001240 int offset = ee->offset;
1241 int len = ee->len;
Sebastien Jana84afa42010-05-05 08:45:54 +00001242
Ben Dooks1741a392010-04-29 13:16:28 +00001243 /* must be 2 byte aligned */
1244 if (len & 1 || offset & 1)
Sebastien Jana84afa42010-05-05 08:45:54 +00001245 return -EINVAL;
1246
Ben Dooks1741a392010-04-29 13:16:28 +00001247 if (ks8851_eeprom_claim(ks))
1248 return -ENOENT;
Sebastien Jana84afa42010-05-05 08:45:54 +00001249
Ben Dooks1741a392010-04-29 13:16:28 +00001250 ee->magic = KS_EEPROM_MAGIC;
Sebastien Jana84afa42010-05-05 08:45:54 +00001251
Ben Dooks1741a392010-04-29 13:16:28 +00001252 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1253 ks8851_eeprom_release(ks);
Sebastien Jana84afa42010-05-05 08:45:54 +00001254
Ben Dooks1741a392010-04-29 13:16:28 +00001255 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001256}
1257
Ben Dooks1741a392010-04-29 13:16:28 +00001258static int ks8851_get_eeprom_len(struct net_device *dev)
Sebastien Jana84afa42010-05-05 08:45:54 +00001259{
1260 struct ks8851_net *ks = netdev_priv(dev);
Sebastien Jana84afa42010-05-05 08:45:54 +00001261
Ben Dooks1741a392010-04-29 13:16:28 +00001262 /* currently, we assume it is an 93C46 attached, so return 128 */
1263 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001264}
1265
Ben Dooks3ba81f32009-07-16 05:24:08 +00001266static const struct ethtool_ops ks8851_ethtool_ops = {
1267 .get_drvinfo = ks8851_get_drvinfo,
1268 .get_msglevel = ks8851_get_msglevel,
1269 .set_msglevel = ks8851_set_msglevel,
1270 .get_settings = ks8851_get_settings,
1271 .set_settings = ks8851_set_settings,
1272 .get_link = ks8851_get_link,
1273 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001274 .get_eeprom_len = ks8851_get_eeprom_len,
1275 .get_eeprom = ks8851_get_eeprom,
1276 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001277};
1278
1279/* MII interface controls */
1280
1281/**
1282 * ks8851_phy_reg - convert MII register into a KS8851 register
1283 * @reg: MII register number.
1284 *
1285 * Return the KS8851 register number for the corresponding MII PHY register
1286 * if possible. Return zero if the MII register has no direct mapping to the
1287 * KS8851 register set.
1288 */
1289static int ks8851_phy_reg(int reg)
1290{
1291 switch (reg) {
1292 case MII_BMCR:
1293 return KS_P1MBCR;
1294 case MII_BMSR:
1295 return KS_P1MBSR;
1296 case MII_PHYSID1:
1297 return KS_PHY1ILR;
1298 case MII_PHYSID2:
1299 return KS_PHY1IHR;
1300 case MII_ADVERTISE:
1301 return KS_P1ANAR;
1302 case MII_LPA:
1303 return KS_P1ANLPR;
1304 }
1305
1306 return 0x0;
1307}
1308
1309/**
1310 * ks8851_phy_read - MII interface PHY register read.
1311 * @dev: The network device the PHY is on.
1312 * @phy_addr: Address of PHY (ignored as we only have one)
1313 * @reg: The register to read.
1314 *
1315 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001316 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001317 * are always returned as zero.
1318 *
1319 * We return zero for unsupported registers as the MII code does not check
1320 * the value returned for any error status, and simply returns it to the
1321 * caller. The mii-tool that the driver was tested with takes any -ve error
1322 * as real PHY capabilities, thus displaying incorrect data to the user.
1323 */
1324static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1325{
1326 struct ks8851_net *ks = netdev_priv(dev);
1327 int ksreg;
1328 int result;
1329
1330 ksreg = ks8851_phy_reg(reg);
1331 if (!ksreg)
1332 return 0x0; /* no error return allowed, so use zero */
1333
1334 mutex_lock(&ks->lock);
1335 result = ks8851_rdreg16(ks, ksreg);
1336 mutex_unlock(&ks->lock);
1337
1338 return result;
1339}
1340
1341static void ks8851_phy_write(struct net_device *dev,
1342 int phy, int reg, int value)
1343{
1344 struct ks8851_net *ks = netdev_priv(dev);
1345 int ksreg;
1346
1347 ksreg = ks8851_phy_reg(reg);
1348 if (ksreg) {
1349 mutex_lock(&ks->lock);
1350 ks8851_wrreg16(ks, ksreg, value);
1351 mutex_unlock(&ks->lock);
1352 }
1353}
1354
1355/**
1356 * ks8851_read_selftest - read the selftest memory info.
1357 * @ks: The device state
1358 *
1359 * Read and check the TX/RX memory selftest information.
1360 */
1361static int ks8851_read_selftest(struct ks8851_net *ks)
1362{
1363 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1364 int ret = 0;
1365 unsigned rd;
1366
1367 rd = ks8851_rdreg16(ks, KS_MBIR);
1368
1369 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001370 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001371 return 0;
1372 }
1373
1374 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001375 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001376 ret |= 1;
1377 }
1378
1379 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001380 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001381 ret |= 2;
1382 }
1383
1384 return 0;
1385}
1386
1387/* driver bus management functions */
1388
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001389#ifdef CONFIG_PM
1390static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1391{
1392 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1393 struct net_device *dev = ks->netdev;
1394
1395 if (netif_running(dev)) {
1396 netif_device_detach(dev);
1397 ks8851_net_stop(dev);
1398 }
1399
1400 return 0;
1401}
1402
1403static int ks8851_resume(struct spi_device *spi)
1404{
1405 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1406 struct net_device *dev = ks->netdev;
1407
1408 if (netif_running(dev)) {
1409 ks8851_net_open(dev);
1410 netif_device_attach(dev);
1411 }
1412
1413 return 0;
1414}
1415#else
1416#define ks8851_suspend NULL
1417#define ks8851_resume NULL
1418#endif
1419
Ben Dooks3ba81f32009-07-16 05:24:08 +00001420static int __devinit ks8851_probe(struct spi_device *spi)
1421{
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001422 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001423 struct net_device *ndev;
1424 struct ks8851_net *ks;
1425 int ret;
1426
1427 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1428 if (!ndev) {
1429 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1430 return -ENOMEM;
1431 }
1432
1433 spi->bits_per_word = 8;
1434
1435 ks = netdev_priv(ndev);
1436
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437 ks->vdd_io = regulator_get(&spi->dev, "vdd_io");
1438 ks->vdd_phy = regulator_get(&spi->dev, "vdd_phy");
1439
1440 if (!IS_ERR(ks->vdd_io))
1441 regulator_enable(ks->vdd_io);
1442
1443 if (!IS_ERR(ks->vdd_phy))
1444 regulator_enable(ks->vdd_phy);
1445
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001446 if (pdata && gpio_is_valid(pdata->irq_gpio)) {
1447 ret = gpio_request(pdata->irq_gpio, "ks8851_irq");
1448 if (ret) {
1449 pr_err("ks8851 gpio_request failed: %d\n", ret);
1450 goto err_irq_gpio;
1451 }
1452 }
1453
1454 if (pdata && gpio_is_valid(pdata->rst_gpio)) {
1455 ret = gpio_request(pdata->rst_gpio, "ks8851_rst");
1456 if (ret) {
1457 pr_err("ks8851 gpio_request failed: %d\n", ret);
1458 goto err_rst_gpio;
1459 }
1460 gpio_direction_output(pdata->rst_gpio, 1);
1461 }
1462
Ben Dooks3ba81f32009-07-16 05:24:08 +00001463 ks->netdev = ndev;
1464 ks->spidev = spi;
1465 ks->tx_space = 6144;
1466
1467 mutex_init(&ks->lock);
1468 spin_lock_init(&ks->statelock);
1469
1470 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1471 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1472 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1473
1474 /* initialise pre-made spi transfer messages */
1475
1476 spi_message_init(&ks->spi_msg1);
1477 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1478
1479 spi_message_init(&ks->spi_msg2);
1480 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1481 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1482
Ben Dooks1741a392010-04-29 13:16:28 +00001483 /* setup EEPROM state */
1484
1485 ks->eeprom.data = ks;
1486 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1487 ks->eeprom.register_read = ks8851_eeprom_regread;
1488 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1489
Ben Dooks3ba81f32009-07-16 05:24:08 +00001490 /* setup mii state */
1491 ks->mii.dev = ndev;
1492 ks->mii.phy_id = 1,
1493 ks->mii.phy_id_mask = 1;
1494 ks->mii.reg_num_mask = 0xf;
1495 ks->mii.mdio_read = ks8851_phy_read;
1496 ks->mii.mdio_write = ks8851_phy_write;
1497
1498 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1499
1500 /* set the default message enable */
1501 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1502 NETIF_MSG_PROBE |
1503 NETIF_MSG_LINK));
1504
1505 skb_queue_head_init(&ks->txq);
1506
1507 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1508 SET_NETDEV_DEV(ndev, &spi->dev);
1509
1510 dev_set_drvdata(&spi->dev, ks);
1511
1512 ndev->if_port = IF_PORT_100BASET;
1513 ndev->netdev_ops = &ks8851_netdev_ops;
1514 ndev->irq = spi->irq;
1515
Ben Dooks57dada62009-10-19 23:49:03 +00001516 /* issue a global soft reset to reset the device. */
1517 ks8851_soft_reset(ks, GRR_GSR);
1518
Ben Dooks3ba81f32009-07-16 05:24:08 +00001519 /* simple check for a valid chip being connected to the bus */
1520
1521 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1522 dev_err(&spi->dev, "failed to read device ID\n");
1523 ret = -ENODEV;
1524 goto err_id;
1525 }
1526
Sebastien Jan7d997462010-05-05 08:45:52 +00001527 /* cache the contents of the CCR register for EEPROM, etc. */
1528 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1529
1530 if (ks->rc_ccr & CCR_EEPROM)
1531 ks->eeprom_size = 128;
1532 else
1533 ks->eeprom_size = 0;
1534
Ben Dooks3ba81f32009-07-16 05:24:08 +00001535 ks8851_read_selftest(ks);
1536 ks8851_init_mac(ks);
1537
1538 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1539 ndev->name, ks);
1540 if (ret < 0) {
1541 dev_err(&spi->dev, "failed to get irq\n");
1542 goto err_irq;
1543 }
1544
1545 ret = register_netdev(ndev);
1546 if (ret) {
1547 dev_err(&spi->dev, "failed to register network device\n");
1548 goto err_netdev;
1549 }
1550
Ben Dooksff47cb02010-04-29 13:16:26 +00001551 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001552 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
Ben Dooksff47cb02010-04-29 13:16:26 +00001553 ndev->dev_addr, ndev->irq,
1554 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001555
1556 return 0;
1557
1558
1559err_netdev:
1560 free_irq(ndev->irq, ndev);
1561
1562err_id:
1563err_irq:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564 if (!IS_ERR(ks->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001565 regulator_disable(ks->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 regulator_put(ks->vdd_io);
1567 }
1568
1569 if (!IS_ERR(ks->vdd_phy)) {
1570 regulator_disable(ks->vdd_phy);
1571 regulator_put(ks->vdd_phy);
1572 }
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001573
Stepan Moskovchenko79b444f2011-10-12 13:47:39 -07001574 free_netdev(ndev);
1575
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001576 if (pdata && gpio_is_valid(pdata->rst_gpio))
1577 gpio_free(pdata->rst_gpio);
1578
1579err_rst_gpio:
1580 if (pdata && gpio_is_valid(pdata->irq_gpio))
1581 gpio_free(pdata->irq_gpio);
1582
1583err_irq_gpio:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001584 return ret;
1585}
1586
1587static int __devexit ks8851_remove(struct spi_device *spi)
1588{
1589 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001590 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001591
1592 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001593 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595 if (!IS_ERR(priv->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001596 regulator_disable(priv->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001597 regulator_put(priv->vdd_io);
1598 }
1599
1600 if (!IS_ERR(priv->vdd_phy)) {
1601 regulator_disable(priv->vdd_phy);
1602 regulator_put(priv->vdd_phy);
1603 }
1604
Ben Dooks3ba81f32009-07-16 05:24:08 +00001605 unregister_netdev(priv->netdev);
1606 free_irq(spi->irq, priv);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001607
1608 if (pdata && gpio_is_valid(pdata->irq_gpio))
1609 gpio_free(pdata->irq_gpio);
1610
1611 if (pdata && gpio_is_valid(pdata->rst_gpio))
1612 gpio_free(pdata->rst_gpio);
1613
Ben Dooks3ba81f32009-07-16 05:24:08 +00001614 free_netdev(priv->netdev);
1615
1616 return 0;
1617}
1618
1619static struct spi_driver ks8851_driver = {
1620 .driver = {
1621 .name = "ks8851",
1622 .owner = THIS_MODULE,
1623 },
1624 .probe = ks8851_probe,
1625 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001626 .suspend = ks8851_suspend,
1627 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001628};
1629
1630static int __init ks8851_init(void)
1631{
1632 return spi_register_driver(&ks8851_driver);
1633}
1634
1635static void __exit ks8851_exit(void)
1636{
1637 spi_unregister_driver(&ks8851_driver);
1638}
1639
1640module_init(ks8851_init);
1641module_exit(ks8851_exit);
1642
1643MODULE_DESCRIPTION("KS8851 Network driver");
1644MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1645MODULE_LICENSE("GPL");
1646
1647module_param_named(message, msg_enable, int, 0);
1648MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001649MODULE_ALIAS("spi:ks8851");