blob: 0bf972abdcab192c1c6553bef699e4ae089f79df [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
625 if (status & IRQ_LCI) {
626 /* should do something about checking link status */
627 handled |= IRQ_LCI;
628 }
629
630 if (status & IRQ_LDI) {
631 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
632 pmecr &= ~PMECR_WKEVT_MASK;
633 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
634
635 handled |= IRQ_LDI;
636 }
637
638 if (status & IRQ_RXPSI)
639 handled |= IRQ_RXPSI;
640
641 if (status & IRQ_TXI) {
642 handled |= IRQ_TXI;
643
644 /* no lock here, tx queue should have been stopped */
645
646 /* update our idea of how much tx space is available to the
647 * system */
648 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
649
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000650 netif_dbg(ks, intr, ks->netdev,
651 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000652 }
653
654 if (status & IRQ_RXI)
655 handled |= IRQ_RXI;
656
657 if (status & IRQ_SPIBEI) {
658 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
659 handled |= IRQ_SPIBEI;
660 }
661
662 ks8851_wrreg16(ks, KS_ISR, handled);
663
664 if (status & IRQ_RXI) {
665 /* the datasheet says to disable the rx interrupt during
666 * packet read-out, however we're masking the interrupt
667 * from the device so do not bother masking just the RX
668 * from the device. */
669
670 ks8851_rx_pkts(ks);
671 }
672
673 /* if something stopped the rx process, probably due to wanting
674 * to change the rx settings, then do something about restarting
675 * it. */
676 if (status & IRQ_RXPSI) {
677 struct ks8851_rxctrl *rxc = &ks->rxctrl;
678
679 /* update the multicast hash table */
680 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
681 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
682 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
683 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
684
685 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
686 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
687 }
688
689 mutex_unlock(&ks->lock);
690
691 if (status & IRQ_TXI)
692 netif_wake_queue(ks->netdev);
693
694 enable_irq(ks->netdev->irq);
695}
696
697/**
698 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300699 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000700 *
701 * Returns the size of the TXFIFO message needed to send
702 * this packet.
703 */
704static inline unsigned calc_txlen(unsigned len)
705{
706 return ALIGN(len + 4, 4);
707}
708
709/**
710 * ks8851_wrpkt - write packet to TX FIFO
711 * @ks: The device state.
712 * @txp: The sk_buff to transmit.
713 * @irq: IRQ on completion of the packet.
714 *
715 * Send the @txp to the chip. This means creating the relevant packet header
716 * specifying the length of the packet and the other information the chip
717 * needs, such as IRQ on completion. Send the header and the packet data to
718 * the device.
719 */
720static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
721{
722 struct spi_transfer *xfer = ks->spi_xfer2;
723 struct spi_message *msg = &ks->spi_msg2;
724 unsigned fid = 0;
725 int ret;
726
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000727 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
728 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000729
730 fid = ks->fid++;
731 fid &= TXFR_TXFID_MASK;
732
733 if (irq)
734 fid |= TXFR_TXIC; /* irq on completion */
735
736 /* start header at txb[1] to align txw entries */
737 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
738 ks->txh.txw[1] = cpu_to_le16(fid);
739 ks->txh.txw[2] = cpu_to_le16(txp->len);
740
741 xfer->tx_buf = &ks->txh.txb[1];
742 xfer->rx_buf = NULL;
743 xfer->len = 5;
744
745 xfer++;
746 xfer->tx_buf = txp->data;
747 xfer->rx_buf = NULL;
748 xfer->len = ALIGN(txp->len, 4);
749
750 ret = spi_sync(ks->spidev, msg);
751 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000752 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000753}
754
755/**
756 * ks8851_done_tx - update and then free skbuff after transmitting
757 * @ks: The device state
758 * @txb: The buffer transmitted
759 */
760static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
761{
762 struct net_device *dev = ks->netdev;
763
764 dev->stats.tx_bytes += txb->len;
765 dev->stats.tx_packets++;
766
767 dev_kfree_skb(txb);
768}
769
770/**
771 * ks8851_tx_work - process tx packet(s)
772 * @work: The work strucutre what was scheduled.
773 *
774 * This is called when a number of packets have been scheduled for
775 * transmission and need to be sent to the device.
776 */
777static void ks8851_tx_work(struct work_struct *work)
778{
779 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
780 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000781 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000782
783 mutex_lock(&ks->lock);
784
785 while (!last) {
786 txb = skb_dequeue(&ks->txq);
787 last = skb_queue_empty(&ks->txq);
788
Abraham Arce761172f2010-04-16 14:48:43 +0000789 if (txb != NULL) {
790 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
791 ks8851_wrpkt(ks, txb, last);
792 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
793 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000794
Abraham Arce761172f2010-04-16 14:48:43 +0000795 ks8851_done_tx(ks, txb);
796 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000797 }
798
799 mutex_unlock(&ks->lock);
800}
801
802/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000803 * ks8851_net_open - open network device
804 * @dev: The network device being opened.
805 *
806 * Called when the network device is marked active, such as a user executing
807 * 'ifconfig up' on the device.
808 */
809static int ks8851_net_open(struct net_device *dev)
810{
811 struct ks8851_net *ks = netdev_priv(dev);
812
813 /* lock the card, even if we may not actually be doing anything
814 * else at the moment */
815 mutex_lock(&ks->lock);
816
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000817 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000818
819 /* bring chip out of any power saving mode it was in */
820 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
821
822 /* issue a soft reset to the RX/TX QMU to put it into a known
823 * state. */
824 ks8851_soft_reset(ks, GRR_QMU);
825
826 /* setup transmission parameters */
827
828 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
829 TXCR_TXPE | /* pad to min length */
830 TXCR_TXCRC | /* add CRC */
831 TXCR_TXFCE)); /* enable flow control */
832
833 /* auto-increment tx data, reset tx pointer */
834 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
835
836 /* setup receiver control */
837
838 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
839 RXCR1_RXFCE | /* enable flow control */
840 RXCR1_RXBE | /* broadcast enable */
841 RXCR1_RXUE | /* unicast enable */
842 RXCR1_RXE)); /* enable rx block */
843
844 /* transfer entire frames out in one go */
845 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
846
847 /* set receive counter timeouts */
848 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
849 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
850 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
851
852 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
853 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
854 RXQCR_RXDTTE); /* IRQ on time exceeded */
855
856 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
857
858 /* clear then enable interrupts */
859
860#define STD_IRQ (IRQ_LCI | /* Link Change */ \
861 IRQ_TXI | /* TX done */ \
862 IRQ_RXI | /* RX done */ \
863 IRQ_SPIBEI | /* SPI bus error */ \
864 IRQ_TXPSI | /* TX process stop */ \
865 IRQ_RXPSI) /* RX process stop */
866
867 ks->rc_ier = STD_IRQ;
868 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
869 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
870
871 netif_start_queue(ks->netdev);
872
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000873 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000874
875 mutex_unlock(&ks->lock);
876 return 0;
877}
878
879/**
880 * ks8851_net_stop - close network device
881 * @dev: The device being closed.
882 *
883 * Called to close down a network device which has been active. Cancell any
884 * work, shutdown the RX and TX process and then place the chip into a low
885 * power state whilst it is not being used.
886 */
887static int ks8851_net_stop(struct net_device *dev)
888{
889 struct ks8851_net *ks = netdev_priv(dev);
890
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000891 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000892
893 netif_stop_queue(dev);
894
895 mutex_lock(&ks->lock);
896
897 /* stop any outstanding work */
898 flush_work(&ks->irq_work);
899 flush_work(&ks->tx_work);
900 flush_work(&ks->rxctrl_work);
901
902 /* turn off the IRQs and ack any outstanding */
903 ks8851_wrreg16(ks, KS_IER, 0x0000);
904 ks8851_wrreg16(ks, KS_ISR, 0xffff);
905
906 /* shutdown RX process */
907 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
908
909 /* shutdown TX process */
910 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
911
912 /* set powermode to soft power down to save power */
913 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
914
915 /* ensure any queued tx buffers are dumped */
916 while (!skb_queue_empty(&ks->txq)) {
917 struct sk_buff *txb = skb_dequeue(&ks->txq);
918
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000919 netif_dbg(ks, ifdown, ks->netdev,
920 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000921
922 dev_kfree_skb(txb);
923 }
924
925 mutex_unlock(&ks->lock);
926 return 0;
927}
928
929/**
930 * ks8851_start_xmit - transmit packet
931 * @skb: The buffer to transmit
932 * @dev: The device used to transmit the packet.
933 *
934 * Called by the network layer to transmit the @skb. Queue the packet for
935 * the device and schedule the necessary work to transmit the packet when
936 * it is free.
937 *
938 * We do this to firstly avoid sleeping with the network device locked,
939 * and secondly so we can round up more than one packet to transmit which
940 * means we can try and avoid generating too many transmit done interrupts.
941 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000942static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
943 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000944{
945 struct ks8851_net *ks = netdev_priv(dev);
946 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000947 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000948
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000949 netif_dbg(ks, tx_queued, ks->netdev,
950 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000951
952 spin_lock(&ks->statelock);
953
954 if (needed > ks->tx_space) {
955 netif_stop_queue(dev);
956 ret = NETDEV_TX_BUSY;
957 } else {
958 ks->tx_space -= needed;
959 skb_queue_tail(&ks->txq, skb);
960 }
961
962 spin_unlock(&ks->statelock);
963 schedule_work(&ks->tx_work);
964
965 return ret;
966}
967
968/**
969 * ks8851_rxctrl_work - work handler to change rx mode
970 * @work: The work structure this belongs to.
971 *
972 * Lock the device and issue the necessary changes to the receive mode from
973 * the network device layer. This is done so that we can do this without
974 * having to sleep whilst holding the network device lock.
975 *
976 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
977 * receive parameters are programmed, we issue a write to disable the RXQ and
978 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
979 * complete. The interrupt handler then writes the new values into the chip.
980 */
981static void ks8851_rxctrl_work(struct work_struct *work)
982{
983 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
984
985 mutex_lock(&ks->lock);
986
987 /* need to shutdown RXQ before modifying filter parameters */
988 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
989
990 mutex_unlock(&ks->lock);
991}
992
993static void ks8851_set_rx_mode(struct net_device *dev)
994{
995 struct ks8851_net *ks = netdev_priv(dev);
996 struct ks8851_rxctrl rxctrl;
997
998 memset(&rxctrl, 0, sizeof(rxctrl));
999
1000 if (dev->flags & IFF_PROMISC) {
1001 /* interface to receive everything */
1002
1003 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
1004 } else if (dev->flags & IFF_ALLMULTI) {
1005 /* accept all multicast packets */
1006
1007 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1008 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001009 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001010 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001011 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001012
1013 /* accept some multicast */
1014
Jiri Pirko22bedad2010-04-01 21:22:57 +00001015 netdev_for_each_mc_addr(ha, dev) {
1016 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001017 crc >>= (32 - 6); /* get top six bits */
1018
1019 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001020 }
1021
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001022 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001023 } else {
1024 /* just accept broadcast / unicast */
1025 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1026 }
1027
1028 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1029 RXCR1_RXBE | /* broadcast enable */
1030 RXCR1_RXE | /* RX process enable */
1031 RXCR1_RXFCE); /* enable flow control */
1032
1033 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1034
1035 /* schedule work to do the actual set of the data if needed */
1036
1037 spin_lock(&ks->statelock);
1038
1039 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1040 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1041 schedule_work(&ks->rxctrl_work);
1042 }
1043
1044 spin_unlock(&ks->statelock);
1045}
1046
1047static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1048{
1049 struct sockaddr *sa = addr;
1050
1051 if (netif_running(dev))
1052 return -EBUSY;
1053
1054 if (!is_valid_ether_addr(sa->sa_data))
1055 return -EADDRNOTAVAIL;
1056
1057 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1058 return ks8851_write_mac_addr(dev);
1059}
1060
1061static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1062{
1063 struct ks8851_net *ks = netdev_priv(dev);
1064
1065 if (!netif_running(dev))
1066 return -EINVAL;
1067
1068 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1069}
1070
1071static const struct net_device_ops ks8851_netdev_ops = {
1072 .ndo_open = ks8851_net_open,
1073 .ndo_stop = ks8851_net_stop,
1074 .ndo_do_ioctl = ks8851_net_ioctl,
1075 .ndo_start_xmit = ks8851_start_xmit,
1076 .ndo_set_mac_address = ks8851_set_mac_address,
1077 .ndo_set_rx_mode = ks8851_set_rx_mode,
1078 .ndo_change_mtu = eth_change_mtu,
1079 .ndo_validate_addr = eth_validate_addr,
1080};
1081
1082/* ethtool support */
1083
1084static void ks8851_get_drvinfo(struct net_device *dev,
1085 struct ethtool_drvinfo *di)
1086{
1087 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1088 strlcpy(di->version, "1.00", sizeof(di->version));
1089 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1090}
1091
1092static u32 ks8851_get_msglevel(struct net_device *dev)
1093{
1094 struct ks8851_net *ks = netdev_priv(dev);
1095 return ks->msg_enable;
1096}
1097
1098static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1099{
1100 struct ks8851_net *ks = netdev_priv(dev);
1101 ks->msg_enable = to;
1102}
1103
1104static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1105{
1106 struct ks8851_net *ks = netdev_priv(dev);
1107 return mii_ethtool_gset(&ks->mii, cmd);
1108}
1109
1110static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1111{
1112 struct ks8851_net *ks = netdev_priv(dev);
1113 return mii_ethtool_sset(&ks->mii, cmd);
1114}
1115
1116static u32 ks8851_get_link(struct net_device *dev)
1117{
1118 struct ks8851_net *ks = netdev_priv(dev);
1119 return mii_link_ok(&ks->mii);
1120}
1121
1122static int ks8851_nway_reset(struct net_device *dev)
1123{
1124 struct ks8851_net *ks = netdev_priv(dev);
1125 return mii_nway_restart(&ks->mii);
1126}
1127
Ben Dooks1741a392010-04-29 13:16:28 +00001128/* EEPROM support */
1129
1130static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1131{
1132 struct ks8851_net *ks = ee->data;
1133 unsigned val;
1134
1135 val = ks8851_rdreg16(ks, KS_EEPCR);
1136
1137 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1138 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1139 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1140}
1141
1142static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1143{
1144 struct ks8851_net *ks = ee->data;
1145 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1146
1147 if (ee->drive_data)
1148 val |= EEPCR_EESRWA;
1149 if (ee->reg_data_in)
1150 val |= EEPCR_EEDO;
1151 if (ee->reg_data_clock)
1152 val |= EEPCR_EESCK;
1153 if (ee->reg_chip_select)
1154 val |= EEPCR_EECS;
1155
1156 ks8851_wrreg16(ks, KS_EEPCR, val);
1157}
1158
1159/**
1160 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1161 * @ks: The network deice state.
1162 *
1163 * Check for the presence of an EEPROM, and then activate software access
1164 * to the device.
1165 */
1166static int ks8851_eeprom_claim(struct ks8851_net *ks)
1167{
1168 if (!(ks->rc_ccr & CCR_EEPROM))
1169 return -ENOENT;
1170
1171 mutex_lock(&ks->lock);
1172
1173 /* start with clock low, cs high */
1174 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1175 return 0;
1176}
1177
1178/**
1179 * ks8851_eeprom_release - release the EEPROM interface
1180 * @ks: The device state
1181 *
1182 * Release the software access to the device EEPROM
1183 */
1184static void ks8851_eeprom_release(struct ks8851_net *ks)
1185{
1186 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1187
1188 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1189 mutex_unlock(&ks->lock);
1190}
1191
1192#define KS_EEPROM_MAGIC (0x00008851)
1193
1194static int ks8851_set_eeprom(struct net_device *dev,
1195 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001196{
1197 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks1741a392010-04-29 13:16:28 +00001198 int offset = ee->offset;
1199 int len = ee->len;
1200 u16 tmp;
1201
1202 /* currently only support byte writing */
1203 if (len != 1)
1204 return -EINVAL;
1205
1206 if (ee->magic != KS_EEPROM_MAGIC)
1207 return -EINVAL;
1208
1209 if (ks8851_eeprom_claim(ks))
1210 return -ENOENT;
1211
1212 eeprom_93cx6_wren(&ks->eeprom, true);
1213
1214 /* ethtool currently only supports writing bytes, which means
1215 * we have to read/modify/write our 16bit EEPROMs */
1216
1217 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1218
1219 if (offset & 1) {
1220 tmp &= 0xff;
1221 tmp |= *data << 8;
1222 } else {
1223 tmp &= 0xff00;
1224 tmp |= *data;
1225 }
1226
1227 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1228 eeprom_93cx6_wren(&ks->eeprom, false);
1229
1230 ks8851_eeprom_release(ks);
1231
1232 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001233}
1234
1235static int ks8851_get_eeprom(struct net_device *dev,
Ben Dooks1741a392010-04-29 13:16:28 +00001236 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001237{
1238 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks1741a392010-04-29 13:16:28 +00001239 int offset = ee->offset;
1240 int len = ee->len;
Sebastien Jana84afa42010-05-05 08:45:54 +00001241
Ben Dooks1741a392010-04-29 13:16:28 +00001242 /* must be 2 byte aligned */
1243 if (len & 1 || offset & 1)
Sebastien Jana84afa42010-05-05 08:45:54 +00001244 return -EINVAL;
1245
Ben Dooks1741a392010-04-29 13:16:28 +00001246 if (ks8851_eeprom_claim(ks))
1247 return -ENOENT;
Sebastien Jana84afa42010-05-05 08:45:54 +00001248
Ben Dooks1741a392010-04-29 13:16:28 +00001249 ee->magic = KS_EEPROM_MAGIC;
Sebastien Jana84afa42010-05-05 08:45:54 +00001250
Ben Dooks1741a392010-04-29 13:16:28 +00001251 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1252 ks8851_eeprom_release(ks);
Sebastien Jana84afa42010-05-05 08:45:54 +00001253
Ben Dooks1741a392010-04-29 13:16:28 +00001254 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001255}
1256
Ben Dooks1741a392010-04-29 13:16:28 +00001257static int ks8851_get_eeprom_len(struct net_device *dev)
Sebastien Jana84afa42010-05-05 08:45:54 +00001258{
1259 struct ks8851_net *ks = netdev_priv(dev);
Sebastien Jana84afa42010-05-05 08:45:54 +00001260
Ben Dooks1741a392010-04-29 13:16:28 +00001261 /* currently, we assume it is an 93C46 attached, so return 128 */
1262 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001263}
1264
Ben Dooks3ba81f32009-07-16 05:24:08 +00001265static const struct ethtool_ops ks8851_ethtool_ops = {
1266 .get_drvinfo = ks8851_get_drvinfo,
1267 .get_msglevel = ks8851_get_msglevel,
1268 .set_msglevel = ks8851_set_msglevel,
1269 .get_settings = ks8851_get_settings,
1270 .set_settings = ks8851_set_settings,
1271 .get_link = ks8851_get_link,
1272 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001273 .get_eeprom_len = ks8851_get_eeprom_len,
1274 .get_eeprom = ks8851_get_eeprom,
1275 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001276};
1277
1278/* MII interface controls */
1279
1280/**
1281 * ks8851_phy_reg - convert MII register into a KS8851 register
1282 * @reg: MII register number.
1283 *
1284 * Return the KS8851 register number for the corresponding MII PHY register
1285 * if possible. Return zero if the MII register has no direct mapping to the
1286 * KS8851 register set.
1287 */
1288static int ks8851_phy_reg(int reg)
1289{
1290 switch (reg) {
1291 case MII_BMCR:
1292 return KS_P1MBCR;
1293 case MII_BMSR:
1294 return KS_P1MBSR;
1295 case MII_PHYSID1:
1296 return KS_PHY1ILR;
1297 case MII_PHYSID2:
1298 return KS_PHY1IHR;
1299 case MII_ADVERTISE:
1300 return KS_P1ANAR;
1301 case MII_LPA:
1302 return KS_P1ANLPR;
1303 }
1304
1305 return 0x0;
1306}
1307
1308/**
1309 * ks8851_phy_read - MII interface PHY register read.
1310 * @dev: The network device the PHY is on.
1311 * @phy_addr: Address of PHY (ignored as we only have one)
1312 * @reg: The register to read.
1313 *
1314 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001315 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001316 * are always returned as zero.
1317 *
1318 * We return zero for unsupported registers as the MII code does not check
1319 * the value returned for any error status, and simply returns it to the
1320 * caller. The mii-tool that the driver was tested with takes any -ve error
1321 * as real PHY capabilities, thus displaying incorrect data to the user.
1322 */
1323static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1324{
1325 struct ks8851_net *ks = netdev_priv(dev);
1326 int ksreg;
1327 int result;
1328
1329 ksreg = ks8851_phy_reg(reg);
1330 if (!ksreg)
1331 return 0x0; /* no error return allowed, so use zero */
1332
1333 mutex_lock(&ks->lock);
1334 result = ks8851_rdreg16(ks, ksreg);
1335 mutex_unlock(&ks->lock);
1336
1337 return result;
1338}
1339
1340static void ks8851_phy_write(struct net_device *dev,
1341 int phy, int reg, int value)
1342{
1343 struct ks8851_net *ks = netdev_priv(dev);
1344 int ksreg;
1345
1346 ksreg = ks8851_phy_reg(reg);
1347 if (ksreg) {
1348 mutex_lock(&ks->lock);
1349 ks8851_wrreg16(ks, ksreg, value);
1350 mutex_unlock(&ks->lock);
1351 }
1352}
1353
1354/**
1355 * ks8851_read_selftest - read the selftest memory info.
1356 * @ks: The device state
1357 *
1358 * Read and check the TX/RX memory selftest information.
1359 */
1360static int ks8851_read_selftest(struct ks8851_net *ks)
1361{
1362 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1363 int ret = 0;
1364 unsigned rd;
1365
1366 rd = ks8851_rdreg16(ks, KS_MBIR);
1367
1368 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001369 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001370 return 0;
1371 }
1372
1373 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001374 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001375 ret |= 1;
1376 }
1377
1378 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001379 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001380 ret |= 2;
1381 }
1382
1383 return 0;
1384}
1385
1386/* driver bus management functions */
1387
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001388#ifdef CONFIG_PM
1389static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1390{
1391 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1392 struct net_device *dev = ks->netdev;
1393
1394 if (netif_running(dev)) {
1395 netif_device_detach(dev);
1396 ks8851_net_stop(dev);
1397 }
1398
1399 return 0;
1400}
1401
1402static int ks8851_resume(struct spi_device *spi)
1403{
1404 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1405 struct net_device *dev = ks->netdev;
1406
1407 if (netif_running(dev)) {
1408 ks8851_net_open(dev);
1409 netif_device_attach(dev);
1410 }
1411
1412 return 0;
1413}
1414#else
1415#define ks8851_suspend NULL
1416#define ks8851_resume NULL
1417#endif
1418
Ben Dooks3ba81f32009-07-16 05:24:08 +00001419static int __devinit ks8851_probe(struct spi_device *spi)
1420{
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001421 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001422 struct net_device *ndev;
1423 struct ks8851_net *ks;
1424 int ret;
1425
1426 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1427 if (!ndev) {
1428 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1429 return -ENOMEM;
1430 }
1431
1432 spi->bits_per_word = 8;
1433
1434 ks = netdev_priv(ndev);
1435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436 ks->vdd_io = regulator_get(&spi->dev, "vdd_io");
1437 ks->vdd_phy = regulator_get(&spi->dev, "vdd_phy");
1438
1439 if (!IS_ERR(ks->vdd_io))
1440 regulator_enable(ks->vdd_io);
1441
1442 if (!IS_ERR(ks->vdd_phy))
1443 regulator_enable(ks->vdd_phy);
1444
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001445 if (pdata && gpio_is_valid(pdata->irq_gpio)) {
1446 ret = gpio_request(pdata->irq_gpio, "ks8851_irq");
1447 if (ret) {
1448 pr_err("ks8851 gpio_request failed: %d\n", ret);
1449 goto err_irq_gpio;
1450 }
1451 }
1452
1453 if (pdata && gpio_is_valid(pdata->rst_gpio)) {
1454 ret = gpio_request(pdata->rst_gpio, "ks8851_rst");
1455 if (ret) {
1456 pr_err("ks8851 gpio_request failed: %d\n", ret);
1457 goto err_rst_gpio;
1458 }
1459 gpio_direction_output(pdata->rst_gpio, 1);
1460 }
1461
Ben Dooks3ba81f32009-07-16 05:24:08 +00001462 ks->netdev = ndev;
1463 ks->spidev = spi;
1464 ks->tx_space = 6144;
1465
1466 mutex_init(&ks->lock);
1467 spin_lock_init(&ks->statelock);
1468
1469 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1470 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1471 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1472
1473 /* initialise pre-made spi transfer messages */
1474
1475 spi_message_init(&ks->spi_msg1);
1476 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1477
1478 spi_message_init(&ks->spi_msg2);
1479 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1480 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1481
Ben Dooks1741a392010-04-29 13:16:28 +00001482 /* setup EEPROM state */
1483
1484 ks->eeprom.data = ks;
1485 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1486 ks->eeprom.register_read = ks8851_eeprom_regread;
1487 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1488
Ben Dooks3ba81f32009-07-16 05:24:08 +00001489 /* setup mii state */
1490 ks->mii.dev = ndev;
1491 ks->mii.phy_id = 1,
1492 ks->mii.phy_id_mask = 1;
1493 ks->mii.reg_num_mask = 0xf;
1494 ks->mii.mdio_read = ks8851_phy_read;
1495 ks->mii.mdio_write = ks8851_phy_write;
1496
1497 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1498
1499 /* set the default message enable */
1500 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1501 NETIF_MSG_PROBE |
1502 NETIF_MSG_LINK));
1503
1504 skb_queue_head_init(&ks->txq);
1505
1506 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1507 SET_NETDEV_DEV(ndev, &spi->dev);
1508
1509 dev_set_drvdata(&spi->dev, ks);
1510
1511 ndev->if_port = IF_PORT_100BASET;
1512 ndev->netdev_ops = &ks8851_netdev_ops;
1513 ndev->irq = spi->irq;
1514
Ben Dooks57dada62009-10-19 23:49:03 +00001515 /* issue a global soft reset to reset the device. */
1516 ks8851_soft_reset(ks, GRR_GSR);
1517
Ben Dooks3ba81f32009-07-16 05:24:08 +00001518 /* simple check for a valid chip being connected to the bus */
1519
1520 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1521 dev_err(&spi->dev, "failed to read device ID\n");
1522 ret = -ENODEV;
1523 goto err_id;
1524 }
1525
Sebastien Jan7d997462010-05-05 08:45:52 +00001526 /* cache the contents of the CCR register for EEPROM, etc. */
1527 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1528
1529 if (ks->rc_ccr & CCR_EEPROM)
1530 ks->eeprom_size = 128;
1531 else
1532 ks->eeprom_size = 0;
1533
Ben Dooks3ba81f32009-07-16 05:24:08 +00001534 ks8851_read_selftest(ks);
1535 ks8851_init_mac(ks);
1536
1537 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1538 ndev->name, ks);
1539 if (ret < 0) {
1540 dev_err(&spi->dev, "failed to get irq\n");
1541 goto err_irq;
1542 }
1543
1544 ret = register_netdev(ndev);
1545 if (ret) {
1546 dev_err(&spi->dev, "failed to register network device\n");
1547 goto err_netdev;
1548 }
1549
Ben Dooksff47cb02010-04-29 13:16:26 +00001550 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001551 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
Ben Dooksff47cb02010-04-29 13:16:26 +00001552 ndev->dev_addr, ndev->irq,
1553 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001554
1555 return 0;
1556
1557
1558err_netdev:
1559 free_irq(ndev->irq, ndev);
1560
1561err_id:
1562err_irq:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563 if (!IS_ERR(ks->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001564 regulator_disable(ks->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001565 regulator_put(ks->vdd_io);
1566 }
1567
1568 if (!IS_ERR(ks->vdd_phy)) {
1569 regulator_disable(ks->vdd_phy);
1570 regulator_put(ks->vdd_phy);
1571 }
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001572
Stepan Moskovchenko79b444f2011-10-12 13:47:39 -07001573 free_netdev(ndev);
1574
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001575 if (pdata && gpio_is_valid(pdata->rst_gpio))
1576 gpio_free(pdata->rst_gpio);
1577
1578err_rst_gpio:
1579 if (pdata && gpio_is_valid(pdata->irq_gpio))
1580 gpio_free(pdata->irq_gpio);
1581
1582err_irq_gpio:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001583 return ret;
1584}
1585
1586static int __devexit ks8851_remove(struct spi_device *spi)
1587{
1588 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001589 struct ks8851_pdata *pdata = spi->dev.platform_data;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001590
1591 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001592 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001594 if (!IS_ERR(priv->vdd_io)) {
Stepan Moskovchenko9a10d472011-09-21 16:55:04 -07001595 regulator_disable(priv->vdd_io);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596 regulator_put(priv->vdd_io);
1597 }
1598
1599 if (!IS_ERR(priv->vdd_phy)) {
1600 regulator_disable(priv->vdd_phy);
1601 regulator_put(priv->vdd_phy);
1602 }
1603
Ben Dooks3ba81f32009-07-16 05:24:08 +00001604 unregister_netdev(priv->netdev);
1605 free_irq(spi->irq, priv);
Stepan Moskovchenko93d79ec2011-09-21 16:52:16 -07001606
1607 if (pdata && gpio_is_valid(pdata->irq_gpio))
1608 gpio_free(pdata->irq_gpio);
1609
1610 if (pdata && gpio_is_valid(pdata->rst_gpio))
1611 gpio_free(pdata->rst_gpio);
1612
Ben Dooks3ba81f32009-07-16 05:24:08 +00001613 free_netdev(priv->netdev);
1614
1615 return 0;
1616}
1617
1618static struct spi_driver ks8851_driver = {
1619 .driver = {
1620 .name = "ks8851",
1621 .owner = THIS_MODULE,
1622 },
1623 .probe = ks8851_probe,
1624 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001625 .suspend = ks8851_suspend,
1626 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001627};
1628
1629static int __init ks8851_init(void)
1630{
1631 return spi_register_driver(&ks8851_driver);
1632}
1633
1634static void __exit ks8851_exit(void)
1635{
1636 spi_unregister_driver(&ks8851_driver);
1637}
1638
1639module_init(ks8851_init);
1640module_exit(ks8851_exit);
1641
1642MODULE_DESCRIPTION("KS8851 Network driver");
1643MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1644MODULE_LICENSE("GPL");
1645
1646module_param_named(message, msg_enable, int, 0);
1647MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001648MODULE_ALIAS("spi:ks8851");