blob: a80735490cbad4d335189ab2275c3b619f5835cb [file] [log] [blame]
Paul Gortmaker3396c782012-01-27 13:36:01 +00001/* drivers/net/ethernet/micrel/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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000016#include <linux/interrupt.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
21#include <linux/ethtool.h>
22#include <linux/cache.h>
23#include <linux/crc32.h>
24#include <linux/mii.h>
Ben Dooks51b7b1c2011-11-21 08:58:00 +000025#include <linux/eeprom_93cx6.h>
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -070026#include <linux/ks8851.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000027
28#include <linux/spi/spi.h>
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -070029#include <linux/regulator/consumer.h>
30#include <linux/gpio.h>
31#include <linux/of.h>
32#include <linux/of_gpio.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000033
34#include "ks8851.h"
35
36/**
37 * struct ks8851_rxctrl - KS8851 driver rx control
38 * @mchash: Multicast hash-table data.
39 * @rxcr1: KS_RXCR1 register setting
40 * @rxcr2: KS_RXCR2 register setting
41 *
42 * Representation of the settings needs to control the receive filtering
43 * such as the multicast hash-filter and the receive register settings. This
44 * is used to make the job of working out if the receive settings change and
45 * then issuing the new settings to the worker that will send the necessary
46 * commands.
47 */
48struct ks8851_rxctrl {
49 u16 mchash[4];
50 u16 rxcr1;
51 u16 rxcr2;
52};
53
54/**
55 * union ks8851_tx_hdr - tx header data
56 * @txb: The header as bytes
57 * @txw: The header as 16bit, little-endian words
58 *
59 * A dual representation of the tx header data to allow
60 * access to individual bytes, and to allow 16bit accesses
61 * with 16bit alignment.
62 */
63union ks8851_tx_hdr {
64 u8 txb[6];
65 __le16 txw[3];
66};
67
68/**
69 * struct ks8851_net - KS8851 driver private data
70 * @netdev: The network device we're bound to
71 * @spidev: The spi device we're bound to.
72 * @lock: Lock to ensure that the device is not accessed when busy.
73 * @statelock: Lock on this structure for tx list.
74 * @mii: The MII state information for the mii calls.
75 * @rxctrl: RX settings for @rxctrl_work.
76 * @tx_work: Work queue for tx packets
77 * @irq_work: Work queue for servicing interrupts
78 * @rxctrl_work: Work queue for updating RX mode and multicast lists
79 * @txq: Queue of packets for transmission.
80 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
81 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
82 * @txh: Space for generating packet TX header in DMA-able data
83 * @rxd: Space for receiving SPI data, in DMA-able space.
84 * @txd: Space for transmitting SPI data, in DMA-able space.
85 * @msg_enable: The message flags controlling driver output (see ethtool).
86 * @fid: Incrementing frame id tag.
87 * @rc_ier: Cached copy of KS_IER.
Sebastien Jan7d997462010-05-05 08:45:52 +000088 * @rc_ccr: Cached copy of KS_CCR.
Ben Dooks3ba81f32009-07-16 05:24:08 +000089 * @rc_rxqcr: Cached copy of KS_RXQCR.
Sebastien Jan7d997462010-05-05 08:45:52 +000090 * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
Ben Dooks51b7b1c2011-11-21 08:58:00 +000091 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
Ben Dooks3ba81f32009-07-16 05:24:08 +000092 *
93 * The @lock ensures that the chip is protected when certain operations are
94 * in progress. When the read or write packet transfer is in progress, most
95 * of the chip registers are not ccessible until the transfer is finished and
96 * the DMA has been de-asserted.
97 *
98 * The @statelock is used to protect information in the structure which may
99 * need to be accessed via several sources, such as the network driver layer
100 * or one of the work queues.
101 *
102 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
103 * wants to DMA map them, it will not have any problems with data the driver
104 * modifies.
105 */
106struct ks8851_net {
107 struct net_device *netdev;
108 struct spi_device *spidev;
109 struct mutex lock;
110 spinlock_t statelock;
111
112 union ks8851_tx_hdr txh ____cacheline_aligned;
113 u8 rxd[8];
114 u8 txd[8];
115
116 u32 msg_enable ____cacheline_aligned;
117 u16 tx_space;
118 u8 fid;
119
120 u16 rc_ier;
121 u16 rc_rxqcr;
Sebastien Jan7d997462010-05-05 08:45:52 +0000122 u16 rc_ccr;
123 u16 eeprom_size;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000124
125 struct mii_if_info mii;
126 struct ks8851_rxctrl rxctrl;
127
128 struct work_struct tx_work;
129 struct work_struct irq_work;
130 struct work_struct rxctrl_work;
131
132 struct sk_buff_head txq;
133
134 struct spi_message spi_msg1;
135 struct spi_message spi_msg2;
136 struct spi_transfer spi_xfer1;
137 struct spi_transfer spi_xfer2[2];
Ben Dooks51b7b1c2011-11-21 08:58:00 +0000138
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -0700139 struct regulator *vdd_io;
140 struct regulator *vdd_phy;
141 int rst_gpio;
142
Ben Dooks51b7b1c2011-11-21 08:58:00 +0000143 struct eeprom_93cx6 eeprom;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000144};
145
146static int msg_enable;
147
Ben Dooks3ba81f32009-07-16 05:24:08 +0000148/* shift for byte-enable data */
149#define BYTE_EN(_x) ((_x) << 2)
150
151/* turn register number and byte-enable mask into data for start of packet */
152#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
153
154/* SPI register read/write calls.
155 *
156 * All these calls issue SPI transactions to access the chip's registers. They
157 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * chip is busy transferring packet data (RX/TX FIFO accesses).
Ben Dooks3ba81f32009-07-16 05:24:08 +0000159 */
160
161/**
162 * ks8851_wrreg16 - write 16bit register value to chip
163 * @ks: The chip state
164 * @reg: The register address
165 * @val: The value to write
166 *
167 * Issue a write to put the value @val into the register specified in @reg.
168 */
169static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
170{
171 struct spi_transfer *xfer = &ks->spi_xfer1;
172 struct spi_message *msg = &ks->spi_msg1;
173 __le16 txb[2];
174 int ret;
175
176 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
177 txb[1] = cpu_to_le16(val);
178
179 xfer->tx_buf = txb;
180 xfer->rx_buf = NULL;
181 xfer->len = 4;
182
183 ret = spi_sync(ks->spidev, msg);
184 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000185 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000186}
187
188/**
Ben Dooks160d0fa2009-10-19 23:49:04 +0000189 * ks8851_wrreg8 - write 8bit register value to chip
190 * @ks: The chip state
191 * @reg: The register address
192 * @val: The value to write
193 *
194 * Issue a write to put the value @val into the register specified in @reg.
195 */
196static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
197{
198 struct spi_transfer *xfer = &ks->spi_xfer1;
199 struct spi_message *msg = &ks->spi_msg1;
200 __le16 txb[2];
201 int ret;
202 int bit;
203
204 bit = 1 << (reg & 3);
205
206 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
207 txb[1] = val;
208
209 xfer->tx_buf = txb;
210 xfer->rx_buf = NULL;
211 xfer->len = 3;
212
213 ret = spi_sync(ks->spidev, msg);
214 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000215 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks160d0fa2009-10-19 23:49:04 +0000216}
217
218/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000219 * ks8851_rx_1msg - select whether to use one or two messages for spi read
220 * @ks: The device structure
221 *
222 * Return whether to generate a single message with a tx and rx buffer
223 * supplied to spi_sync(), or alternatively send the tx and rx buffers
224 * as separate messages.
225 *
226 * Depending on the hardware in use, a single message may be more efficient
227 * on interrupts or work done by the driver.
228 *
229 * This currently always returns true until we add some per-device data passed
230 * from the platform code to specify which mode is better.
231 */
232static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
233{
234 return true;
235}
236
237/**
238 * ks8851_rdreg - issue read register command and return the data
239 * @ks: The device state
240 * @op: The register address and byte enables in message format.
241 * @rxb: The RX buffer to return the result into
242 * @rxl: The length of data expected.
243 *
244 * This is the low level read call that issues the necessary spi message(s)
245 * to read data from the register specified in @op.
246 */
247static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
248 u8 *rxb, unsigned rxl)
249{
250 struct spi_transfer *xfer;
251 struct spi_message *msg;
252 __le16 *txb = (__le16 *)ks->txd;
253 u8 *trx = ks->rxd;
254 int ret;
255
256 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
257
258 if (ks8851_rx_1msg(ks)) {
259 msg = &ks->spi_msg1;
260 xfer = &ks->spi_xfer1;
261
262 xfer->tx_buf = txb;
263 xfer->rx_buf = trx;
264 xfer->len = rxl + 2;
265 } else {
266 msg = &ks->spi_msg2;
267 xfer = ks->spi_xfer2;
268
269 xfer->tx_buf = txb;
270 xfer->rx_buf = NULL;
271 xfer->len = 2;
272
273 xfer++;
274 xfer->tx_buf = NULL;
275 xfer->rx_buf = trx;
276 xfer->len = rxl;
277 }
278
279 ret = spi_sync(ks->spidev, msg);
280 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000281 netdev_err(ks->netdev, "read: spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000282 else if (ks8851_rx_1msg(ks))
283 memcpy(rxb, trx + 2, rxl);
284 else
285 memcpy(rxb, trx, rxl);
286}
287
288/**
289 * ks8851_rdreg8 - read 8 bit register from device
290 * @ks: The chip information
291 * @reg: The register address
292 *
293 * Read a 8bit register from the chip, returning the result
294*/
295static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
296{
297 u8 rxb[1];
298
299 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
300 return rxb[0];
301}
302
303/**
304 * ks8851_rdreg16 - read 16 bit register from device
305 * @ks: The chip information
306 * @reg: The register address
307 *
308 * Read a 16bit register from the chip, returning the result
309*/
310static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
311{
312 __le16 rx = 0;
313
314 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
315 return le16_to_cpu(rx);
316}
317
318/**
319 * ks8851_rdreg32 - read 32 bit register from device
320 * @ks: The chip information
321 * @reg: The register address
322 *
323 * Read a 32bit register from the chip.
324 *
325 * Note, this read requires the address be aligned to 4 bytes.
326*/
327static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
328{
329 __le32 rx = 0;
330
331 WARN_ON(reg & 3);
332
333 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
334 return le32_to_cpu(rx);
335}
336
337/**
338 * ks8851_soft_reset - issue one of the soft reset to the device
339 * @ks: The device state.
340 * @op: The bit(s) to set in the GRR
341 *
342 * Issue the relevant soft-reset command to the device's GRR register
343 * specified by @op.
344 *
345 * Note, the delays are in there as a caution to ensure that the reset
346 * has time to take effect and then complete. Since the datasheet does
347 * not currently specify the exact sequence, we have chosen something
348 * that seems to work with our device.
349 */
350static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
351{
352 ks8851_wrreg16(ks, KS_GRR, op);
353 mdelay(1); /* wait a short time to effect reset */
354 ks8851_wrreg16(ks, KS_GRR, 0);
355 mdelay(1); /* wait for condition to clear */
356}
357
358/**
Tristram Ha32f160d2011-11-21 08:57:59 +0000359 * ks8851_set_powermode - set power mode of the device
360 * @ks: The device state
361 * @pwrmode: The power mode value to write to KS_PMECR.
362 *
363 * Change the power mode of the chip.
364 */
365static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
366{
367 unsigned pmecr;
368
369 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
370
371 pmecr = ks8851_rdreg16(ks, KS_PMECR);
372 pmecr &= ~PMECR_PM_MASK;
373 pmecr |= pwrmode;
374
375 ks8851_wrreg16(ks, KS_PMECR, pmecr);
376}
377
378/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000379 * ks8851_write_mac_addr - write mac address to device registers
380 * @dev: The network device
381 *
382 * Update the KS8851 MAC address registers from the address in @dev.
383 *
384 * This call assumes that the chip is not running, so there is no need to
385 * shutdown the RXQ process whilst setting this.
386*/
387static int ks8851_write_mac_addr(struct net_device *dev)
388{
389 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000390 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000391
392 mutex_lock(&ks->lock);
393
Tristram Ha32f160d2011-11-21 08:57:59 +0000394 /*
395 * Wake up chip in case it was powered off when stopped; otherwise,
396 * the first write to the MAC address does not take effect.
397 */
398 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000399 for (i = 0; i < ETH_ALEN; i++)
400 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
Tristram Ha32f160d2011-11-21 08:57:59 +0000401 if (!netif_running(dev))
402 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000403
404 mutex_unlock(&ks->lock);
405
406 return 0;
407}
408
409/**
Ben Dooksa9a8de22011-11-21 08:57:58 +0000410 * ks8851_read_mac_addr - read mac address from device registers
411 * @dev: The network device
412 *
413 * Update our copy of the KS8851 MAC address from the registers of @dev.
414*/
415static void ks8851_read_mac_addr(struct net_device *dev)
416{
417 struct ks8851_net *ks = netdev_priv(dev);
418 int i;
419
420 mutex_lock(&ks->lock);
421
422 for (i = 0; i < ETH_ALEN; i++)
423 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
424
425 mutex_unlock(&ks->lock);
426}
427
428/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000429 * ks8851_init_mac - initialise the mac address
430 * @ks: The device structure
431 *
432 * Get or create the initial mac address for the device and then set that
Ben Dooksa9a8de22011-11-21 08:57:58 +0000433 * into the station address register. If there is an EEPROM present, then
434 * we try that. If no valid mac address is found we use random_ether_addr()
Ben Dooks3ba81f32009-07-16 05:24:08 +0000435 * to create a new one.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000436 */
437static void ks8851_init_mac(struct ks8851_net *ks)
438{
439 struct net_device *dev = ks->netdev;
440
Ben Dooksa9a8de22011-11-21 08:57:58 +0000441 /* first, try reading what we've got already */
442 if (ks->rc_ccr & CCR_EEPROM) {
443 ks8851_read_mac_addr(dev);
444 if (is_valid_ether_addr(dev->dev_addr))
445 return;
446
447 netdev_err(ks->netdev, "invalid mac address read %pM\n",
448 dev->dev_addr);
449 }
450
Danny Kukawka7ce5d222012-02-15 06:45:40 +0000451 eth_hw_addr_random(dev);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000452 ks8851_write_mac_addr(dev);
453}
454
455/**
456 * ks8851_irq - device interrupt handler
Uwe Kleine-Königfafdbf82012-02-12 11:29:06 +0000457 * @irq: Interrupt number passed from the IRQ handler.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000458 * @pw: The private word passed to register_irq(), our struct ks8851_net.
459 *
460 * Disable the interrupt from happening again until we've processed the
461 * current status by scheduling ks8851_irq_work().
462 */
463static irqreturn_t ks8851_irq(int irq, void *pw)
464{
465 struct ks8851_net *ks = pw;
466
467 disable_irq_nosync(irq);
468 schedule_work(&ks->irq_work);
469 return IRQ_HANDLED;
470}
471
472/**
473 * ks8851_rdfifo - read data from the receive fifo
474 * @ks: The device state.
475 * @buff: The buffer address
476 * @len: The length of the data to read
477 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100478 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000479 * the FIFO into the buffer specified by @buff.
480 */
481static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
482{
483 struct spi_transfer *xfer = ks->spi_xfer2;
484 struct spi_message *msg = &ks->spi_msg2;
485 u8 txb[1];
486 int ret;
487
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000488 netif_dbg(ks, rx_status, ks->netdev,
489 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000490
491 /* set the operation we're issuing */
492 txb[0] = KS_SPIOP_RXFIFO;
493
494 xfer->tx_buf = txb;
495 xfer->rx_buf = NULL;
496 xfer->len = 1;
497
498 xfer++;
499 xfer->rx_buf = buff;
500 xfer->tx_buf = NULL;
501 xfer->len = len;
502
503 ret = spi_sync(ks->spidev, msg);
504 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000505 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000506}
507
508/**
509 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
510 * @ks: The device state
511 * @rxpkt: The data for the received packet
512 *
513 * Dump the initial data from the packet to dev_dbg().
514*/
515static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
516{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000517 netdev_dbg(ks->netdev,
518 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
519 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
520 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
521 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000522}
523
524/**
525 * ks8851_rx_pkts - receive packets from the host
526 * @ks: The device information.
527 *
528 * This is called from the IRQ work queue when the system detects that there
529 * are packets in the receive queue. Find out how many packets there are and
530 * read them from the FIFO.
531 */
532static void ks8851_rx_pkts(struct ks8851_net *ks)
533{
534 struct sk_buff *skb;
535 unsigned rxfc;
536 unsigned rxlen;
537 unsigned rxstat;
538 u32 rxh;
539 u8 *rxpkt;
540
541 rxfc = ks8851_rdreg8(ks, KS_RXFC);
542
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000543 netif_dbg(ks, rx_status, ks->netdev,
544 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000545
546 /* Currently we're issuing a read per packet, but we could possibly
547 * improve the code by issuing a single read, getting the receive
548 * header, allocating the packet and then reading the packet data
549 * out in one go.
550 *
551 * This form of operation would require us to hold the SPI bus'
552 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300553 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000554 */
555
556 for (; rxfc != 0; rxfc--) {
557 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
558 rxstat = rxh & 0xffff;
559 rxlen = rxh >> 16;
560
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000561 netif_dbg(ks, rx_status, ks->netdev,
562 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000563
564 /* the length of the packet includes the 32bit CRC */
565
566 /* set dma read address */
567 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
568
569 /* start the packet dma process, and set auto-dequeue rx */
570 ks8851_wrreg16(ks, KS_RXQCR,
571 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
572
Eric Dumazet972c40b2010-09-08 13:26:55 +0000573 if (rxlen > 4) {
574 unsigned int rxalign;
575
576 rxlen -= 4;
577 rxalign = ALIGN(rxlen, 4);
578 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
579 if (skb) {
580
581 /* 4 bytes of status header + 4 bytes of
582 * garbage: we put them before ethernet
583 * header, so that they are copied,
584 * but ignored.
585 */
586
587 rxpkt = skb_put(skb, rxlen) - 8;
588
589 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
590
591 if (netif_msg_pktdata(ks))
592 ks8851_dbg_dumpkkt(ks, rxpkt);
593
594 skb->protocol = eth_type_trans(skb, ks->netdev);
Cousson, Benoitfbcf88b2012-02-13 07:37:12 +0000595 netif_rx_ni(skb);
Eric Dumazet972c40b2010-09-08 13:26:55 +0000596
597 ks->netdev->stats.rx_packets++;
598 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000599 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000600 }
601
602 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
603 }
604}
605
606/**
607 * ks8851_irq_work - work queue handler for dealing with interrupt requests
608 * @work: The work structure that was scheduled by schedule_work()
609 *
610 * This is the handler invoked when the ks8851_irq() is called to find out
611 * what happened, as we cannot allow ourselves to sleep whilst waiting for
612 * anything other process has the chip's lock.
613 *
614 * Read the interrupt status, work out what needs to be done and then clear
615 * any of the interrupts that are not needed.
616 */
617static void ks8851_irq_work(struct work_struct *work)
618{
619 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
620 unsigned status;
621 unsigned handled = 0;
622
623 mutex_lock(&ks->lock);
624
625 status = ks8851_rdreg16(ks, KS_ISR);
626
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000627 netif_dbg(ks, intr, ks->netdev,
628 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000629
Stephen Boyd062e55e2012-05-10 12:51:30 +0000630 if (status & IRQ_LCI)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000631 handled |= IRQ_LCI;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000632
633 if (status & IRQ_LDI) {
634 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
635 pmecr &= ~PMECR_WKEVT_MASK;
636 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
637
638 handled |= IRQ_LDI;
639 }
640
641 if (status & IRQ_RXPSI)
642 handled |= IRQ_RXPSI;
643
644 if (status & IRQ_TXI) {
645 handled |= IRQ_TXI;
646
647 /* no lock here, tx queue should have been stopped */
648
649 /* update our idea of how much tx space is available to the
650 * system */
651 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
652
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000653 netif_dbg(ks, intr, ks->netdev,
654 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000655 }
656
657 if (status & IRQ_RXI)
658 handled |= IRQ_RXI;
659
660 if (status & IRQ_SPIBEI) {
661 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
662 handled |= IRQ_SPIBEI;
663 }
664
665 ks8851_wrreg16(ks, KS_ISR, handled);
666
667 if (status & IRQ_RXI) {
668 /* the datasheet says to disable the rx interrupt during
669 * packet read-out, however we're masking the interrupt
670 * from the device so do not bother masking just the RX
671 * from the device. */
672
673 ks8851_rx_pkts(ks);
674 }
675
676 /* if something stopped the rx process, probably due to wanting
677 * to change the rx settings, then do something about restarting
678 * it. */
679 if (status & IRQ_RXPSI) {
680 struct ks8851_rxctrl *rxc = &ks->rxctrl;
681
682 /* update the multicast hash table */
683 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
684 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
685 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
686 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
687
688 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
689 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
690 }
691
692 mutex_unlock(&ks->lock);
693
Stephen Boyd062e55e2012-05-10 12:51:30 +0000694 if (status & IRQ_LCI)
695 mii_check_link(&ks->mii);
696
Ben Dooks3ba81f32009-07-16 05:24:08 +0000697 if (status & IRQ_TXI)
698 netif_wake_queue(ks->netdev);
699
700 enable_irq(ks->netdev->irq);
701}
702
703/**
704 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300705 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000706 *
707 * Returns the size of the TXFIFO message needed to send
708 * this packet.
709 */
710static inline unsigned calc_txlen(unsigned len)
711{
712 return ALIGN(len + 4, 4);
713}
714
715/**
716 * ks8851_wrpkt - write packet to TX FIFO
717 * @ks: The device state.
718 * @txp: The sk_buff to transmit.
719 * @irq: IRQ on completion of the packet.
720 *
721 * Send the @txp to the chip. This means creating the relevant packet header
722 * specifying the length of the packet and the other information the chip
723 * needs, such as IRQ on completion. Send the header and the packet data to
724 * the device.
725 */
726static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
727{
728 struct spi_transfer *xfer = ks->spi_xfer2;
729 struct spi_message *msg = &ks->spi_msg2;
730 unsigned fid = 0;
731 int ret;
732
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000733 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
734 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000735
736 fid = ks->fid++;
737 fid &= TXFR_TXFID_MASK;
738
739 if (irq)
740 fid |= TXFR_TXIC; /* irq on completion */
741
742 /* start header at txb[1] to align txw entries */
743 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
744 ks->txh.txw[1] = cpu_to_le16(fid);
745 ks->txh.txw[2] = cpu_to_le16(txp->len);
746
747 xfer->tx_buf = &ks->txh.txb[1];
748 xfer->rx_buf = NULL;
749 xfer->len = 5;
750
751 xfer++;
752 xfer->tx_buf = txp->data;
753 xfer->rx_buf = NULL;
754 xfer->len = ALIGN(txp->len, 4);
755
756 ret = spi_sync(ks->spidev, msg);
757 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000758 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000759}
760
761/**
762 * ks8851_done_tx - update and then free skbuff after transmitting
763 * @ks: The device state
764 * @txb: The buffer transmitted
765 */
766static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
767{
768 struct net_device *dev = ks->netdev;
769
770 dev->stats.tx_bytes += txb->len;
771 dev->stats.tx_packets++;
772
773 dev_kfree_skb(txb);
774}
775
776/**
777 * ks8851_tx_work - process tx packet(s)
778 * @work: The work strucutre what was scheduled.
779 *
780 * This is called when a number of packets have been scheduled for
781 * transmission and need to be sent to the device.
782 */
783static void ks8851_tx_work(struct work_struct *work)
784{
785 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
786 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000787 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000788
789 mutex_lock(&ks->lock);
790
791 while (!last) {
792 txb = skb_dequeue(&ks->txq);
793 last = skb_queue_empty(&ks->txq);
794
Abraham Arce761172f2010-04-16 14:48:43 +0000795 if (txb != NULL) {
796 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
797 ks8851_wrpkt(ks, txb, last);
798 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
799 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000800
Abraham Arce761172f2010-04-16 14:48:43 +0000801 ks8851_done_tx(ks, txb);
802 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000803 }
804
805 mutex_unlock(&ks->lock);
806}
807
808/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000809 * ks8851_net_open - open network device
810 * @dev: The network device being opened.
811 *
812 * Called when the network device is marked active, such as a user executing
813 * 'ifconfig up' on the device.
814 */
815static int ks8851_net_open(struct net_device *dev)
816{
817 struct ks8851_net *ks = netdev_priv(dev);
818
819 /* lock the card, even if we may not actually be doing anything
820 * else at the moment */
821 mutex_lock(&ks->lock);
822
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000823 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000824
825 /* bring chip out of any power saving mode it was in */
826 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
827
828 /* issue a soft reset to the RX/TX QMU to put it into a known
829 * state. */
830 ks8851_soft_reset(ks, GRR_QMU);
831
832 /* setup transmission parameters */
833
834 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
835 TXCR_TXPE | /* pad to min length */
836 TXCR_TXCRC | /* add CRC */
837 TXCR_TXFCE)); /* enable flow control */
838
839 /* auto-increment tx data, reset tx pointer */
840 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
841
842 /* setup receiver control */
843
844 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
845 RXCR1_RXFCE | /* enable flow control */
846 RXCR1_RXBE | /* broadcast enable */
847 RXCR1_RXUE | /* unicast enable */
848 RXCR1_RXE)); /* enable rx block */
849
850 /* transfer entire frames out in one go */
851 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
852
853 /* set receive counter timeouts */
854 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
855 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
856 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
857
858 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
859 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
860 RXQCR_RXDTTE); /* IRQ on time exceeded */
861
862 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
863
864 /* clear then enable interrupts */
865
866#define STD_IRQ (IRQ_LCI | /* Link Change */ \
867 IRQ_TXI | /* TX done */ \
868 IRQ_RXI | /* RX done */ \
869 IRQ_SPIBEI | /* SPI bus error */ \
870 IRQ_TXPSI | /* TX process stop */ \
871 IRQ_RXPSI) /* RX process stop */
872
873 ks->rc_ier = STD_IRQ;
874 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
875 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
876
877 netif_start_queue(ks->netdev);
878
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000879 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000880
881 mutex_unlock(&ks->lock);
882 return 0;
883}
884
885/**
886 * ks8851_net_stop - close network device
887 * @dev: The device being closed.
888 *
889 * Called to close down a network device which has been active. Cancell any
890 * work, shutdown the RX and TX process and then place the chip into a low
891 * power state whilst it is not being used.
892 */
893static int ks8851_net_stop(struct net_device *dev)
894{
895 struct ks8851_net *ks = netdev_priv(dev);
896
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000897 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000898
899 netif_stop_queue(dev);
900
901 mutex_lock(&ks->lock);
Stephen Boydc5a99932012-04-18 17:25:58 +0000902 /* turn off the IRQs and ack any outstanding */
903 ks8851_wrreg16(ks, KS_IER, 0x0000);
904 ks8851_wrreg16(ks, KS_ISR, 0xffff);
905 mutex_unlock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000906
907 /* stop any outstanding work */
908 flush_work(&ks->irq_work);
909 flush_work(&ks->tx_work);
910 flush_work(&ks->rxctrl_work);
911
Stephen Boydc5a99932012-04-18 17:25:58 +0000912 mutex_lock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000913 /* shutdown RX process */
914 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
915
916 /* shutdown TX process */
917 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
918
919 /* set powermode to soft power down to save power */
920 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Stephen Boydc5a99932012-04-18 17:25:58 +0000921 mutex_unlock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000922
923 /* ensure any queued tx buffers are dumped */
924 while (!skb_queue_empty(&ks->txq)) {
925 struct sk_buff *txb = skb_dequeue(&ks->txq);
926
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000927 netif_dbg(ks, ifdown, ks->netdev,
928 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000929
930 dev_kfree_skb(txb);
931 }
932
Ben Dooks3ba81f32009-07-16 05:24:08 +0000933 return 0;
934}
935
936/**
937 * ks8851_start_xmit - transmit packet
938 * @skb: The buffer to transmit
939 * @dev: The device used to transmit the packet.
940 *
941 * Called by the network layer to transmit the @skb. Queue the packet for
942 * the device and schedule the necessary work to transmit the packet when
943 * it is free.
944 *
945 * We do this to firstly avoid sleeping with the network device locked,
946 * and secondly so we can round up more than one packet to transmit which
947 * means we can try and avoid generating too many transmit done interrupts.
948 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000949static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
950 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000951{
952 struct ks8851_net *ks = netdev_priv(dev);
953 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000954 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000955
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000956 netif_dbg(ks, tx_queued, ks->netdev,
957 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000958
959 spin_lock(&ks->statelock);
960
961 if (needed > ks->tx_space) {
962 netif_stop_queue(dev);
963 ret = NETDEV_TX_BUSY;
964 } else {
965 ks->tx_space -= needed;
966 skb_queue_tail(&ks->txq, skb);
967 }
968
969 spin_unlock(&ks->statelock);
970 schedule_work(&ks->tx_work);
971
972 return ret;
973}
974
975/**
976 * ks8851_rxctrl_work - work handler to change rx mode
977 * @work: The work structure this belongs to.
978 *
979 * Lock the device and issue the necessary changes to the receive mode from
980 * the network device layer. This is done so that we can do this without
981 * having to sleep whilst holding the network device lock.
982 *
983 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
984 * receive parameters are programmed, we issue a write to disable the RXQ and
985 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
986 * complete. The interrupt handler then writes the new values into the chip.
987 */
988static void ks8851_rxctrl_work(struct work_struct *work)
989{
990 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
991
992 mutex_lock(&ks->lock);
993
994 /* need to shutdown RXQ before modifying filter parameters */
995 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
996
997 mutex_unlock(&ks->lock);
998}
999
1000static void ks8851_set_rx_mode(struct net_device *dev)
1001{
1002 struct ks8851_net *ks = netdev_priv(dev);
1003 struct ks8851_rxctrl rxctrl;
1004
1005 memset(&rxctrl, 0, sizeof(rxctrl));
1006
1007 if (dev->flags & IFF_PROMISC) {
1008 /* interface to receive everything */
1009
1010 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
1011 } else if (dev->flags & IFF_ALLMULTI) {
1012 /* accept all multicast packets */
1013
1014 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1015 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001016 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001017 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001018 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001019
1020 /* accept some multicast */
1021
Jiri Pirko22bedad2010-04-01 21:22:57 +00001022 netdev_for_each_mc_addr(ha, dev) {
1023 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001024 crc >>= (32 - 6); /* get top six bits */
1025
1026 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001027 }
1028
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001029 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001030 } else {
1031 /* just accept broadcast / unicast */
1032 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1033 }
1034
1035 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1036 RXCR1_RXBE | /* broadcast enable */
1037 RXCR1_RXE | /* RX process enable */
1038 RXCR1_RXFCE); /* enable flow control */
1039
1040 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1041
1042 /* schedule work to do the actual set of the data if needed */
1043
1044 spin_lock(&ks->statelock);
1045
1046 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1047 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1048 schedule_work(&ks->rxctrl_work);
1049 }
1050
1051 spin_unlock(&ks->statelock);
1052}
1053
1054static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1055{
1056 struct sockaddr *sa = addr;
1057
1058 if (netif_running(dev))
1059 return -EBUSY;
1060
1061 if (!is_valid_ether_addr(sa->sa_data))
1062 return -EADDRNOTAVAIL;
1063
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001064 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001065 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1066 return ks8851_write_mac_addr(dev);
1067}
1068
1069static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1070{
1071 struct ks8851_net *ks = netdev_priv(dev);
1072
1073 if (!netif_running(dev))
1074 return -EINVAL;
1075
1076 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1077}
1078
1079static const struct net_device_ops ks8851_netdev_ops = {
1080 .ndo_open = ks8851_net_open,
1081 .ndo_stop = ks8851_net_stop,
1082 .ndo_do_ioctl = ks8851_net_ioctl,
1083 .ndo_start_xmit = ks8851_start_xmit,
1084 .ndo_set_mac_address = ks8851_set_mac_address,
1085 .ndo_set_rx_mode = ks8851_set_rx_mode,
1086 .ndo_change_mtu = eth_change_mtu,
1087 .ndo_validate_addr = eth_validate_addr,
1088};
1089
1090/* ethtool support */
1091
1092static void ks8851_get_drvinfo(struct net_device *dev,
1093 struct ethtool_drvinfo *di)
1094{
1095 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1096 strlcpy(di->version, "1.00", sizeof(di->version));
1097 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1098}
1099
1100static u32 ks8851_get_msglevel(struct net_device *dev)
1101{
1102 struct ks8851_net *ks = netdev_priv(dev);
1103 return ks->msg_enable;
1104}
1105
1106static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1107{
1108 struct ks8851_net *ks = netdev_priv(dev);
1109 ks->msg_enable = to;
1110}
1111
1112static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1113{
1114 struct ks8851_net *ks = netdev_priv(dev);
1115 return mii_ethtool_gset(&ks->mii, cmd);
1116}
1117
1118static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1119{
1120 struct ks8851_net *ks = netdev_priv(dev);
1121 return mii_ethtool_sset(&ks->mii, cmd);
1122}
1123
1124static u32 ks8851_get_link(struct net_device *dev)
1125{
1126 struct ks8851_net *ks = netdev_priv(dev);
1127 return mii_link_ok(&ks->mii);
1128}
1129
1130static int ks8851_nway_reset(struct net_device *dev)
1131{
1132 struct ks8851_net *ks = netdev_priv(dev);
1133 return mii_nway_restart(&ks->mii);
1134}
1135
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001136/* EEPROM support */
1137
1138static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1139{
1140 struct ks8851_net *ks = ee->data;
1141 unsigned val;
1142
1143 val = ks8851_rdreg16(ks, KS_EEPCR);
1144
1145 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1146 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1147 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1148}
1149
1150static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1151{
1152 struct ks8851_net *ks = ee->data;
1153 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1154
1155 if (ee->drive_data)
1156 val |= EEPCR_EESRWA;
1157 if (ee->reg_data_in)
1158 val |= EEPCR_EEDO;
1159 if (ee->reg_data_clock)
1160 val |= EEPCR_EESCK;
1161 if (ee->reg_chip_select)
1162 val |= EEPCR_EECS;
1163
1164 ks8851_wrreg16(ks, KS_EEPCR, val);
1165}
1166
1167/**
1168 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1169 * @ks: The network device state.
1170 *
1171 * Check for the presence of an EEPROM, and then activate software access
1172 * to the device.
1173 */
1174static int ks8851_eeprom_claim(struct ks8851_net *ks)
1175{
1176 if (!(ks->rc_ccr & CCR_EEPROM))
1177 return -ENOENT;
1178
1179 mutex_lock(&ks->lock);
1180
1181 /* start with clock low, cs high */
1182 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1183 return 0;
1184}
1185
1186/**
1187 * ks8851_eeprom_release - release the EEPROM interface
1188 * @ks: The device state
1189 *
1190 * Release the software access to the device EEPROM
1191 */
1192static void ks8851_eeprom_release(struct ks8851_net *ks)
1193{
1194 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1195
1196 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1197 mutex_unlock(&ks->lock);
1198}
1199
1200#define KS_EEPROM_MAGIC (0x00008851)
1201
1202static int ks8851_set_eeprom(struct net_device *dev,
1203 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001204{
1205 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001206 int offset = ee->offset;
1207 int len = ee->len;
1208 u16 tmp;
1209
1210 /* currently only support byte writing */
1211 if (len != 1)
1212 return -EINVAL;
1213
1214 if (ee->magic != KS_EEPROM_MAGIC)
1215 return -EINVAL;
1216
1217 if (ks8851_eeprom_claim(ks))
1218 return -ENOENT;
1219
1220 eeprom_93cx6_wren(&ks->eeprom, true);
1221
1222 /* ethtool currently only supports writing bytes, which means
1223 * we have to read/modify/write our 16bit EEPROMs */
1224
1225 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1226
1227 if (offset & 1) {
1228 tmp &= 0xff;
1229 tmp |= *data << 8;
1230 } else {
1231 tmp &= 0xff00;
1232 tmp |= *data;
1233 }
1234
1235 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1236 eeprom_93cx6_wren(&ks->eeprom, false);
1237
1238 ks8851_eeprom_release(ks);
1239
1240 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001241}
1242
1243static int ks8851_get_eeprom(struct net_device *dev,
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001244 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001245{
1246 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001247 int offset = ee->offset;
1248 int len = ee->len;
Sebastien Jana84afa42010-05-05 08:45:54 +00001249
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001250 /* must be 2 byte aligned */
1251 if (len & 1 || offset & 1)
Sebastien Jana84afa42010-05-05 08:45:54 +00001252 return -EINVAL;
1253
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001254 if (ks8851_eeprom_claim(ks))
1255 return -ENOENT;
Sebastien Jana84afa42010-05-05 08:45:54 +00001256
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001257 ee->magic = KS_EEPROM_MAGIC;
Sebastien Jana84afa42010-05-05 08:45:54 +00001258
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001259 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1260 ks8851_eeprom_release(ks);
Sebastien Jana84afa42010-05-05 08:45:54 +00001261
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001262 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001263}
1264
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001265static int ks8851_get_eeprom_len(struct net_device *dev)
Sebastien Jana84afa42010-05-05 08:45:54 +00001266{
1267 struct ks8851_net *ks = netdev_priv(dev);
Sebastien Jana84afa42010-05-05 08:45:54 +00001268
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001269 /* currently, we assume it is an 93C46 attached, so return 128 */
1270 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001271}
1272
Ben Dooks3ba81f32009-07-16 05:24:08 +00001273static const struct ethtool_ops ks8851_ethtool_ops = {
1274 .get_drvinfo = ks8851_get_drvinfo,
1275 .get_msglevel = ks8851_get_msglevel,
1276 .set_msglevel = ks8851_set_msglevel,
1277 .get_settings = ks8851_get_settings,
1278 .set_settings = ks8851_set_settings,
1279 .get_link = ks8851_get_link,
1280 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001281 .get_eeprom_len = ks8851_get_eeprom_len,
1282 .get_eeprom = ks8851_get_eeprom,
1283 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001284};
1285
1286/* MII interface controls */
1287
1288/**
1289 * ks8851_phy_reg - convert MII register into a KS8851 register
1290 * @reg: MII register number.
1291 *
1292 * Return the KS8851 register number for the corresponding MII PHY register
1293 * if possible. Return zero if the MII register has no direct mapping to the
1294 * KS8851 register set.
1295 */
1296static int ks8851_phy_reg(int reg)
1297{
1298 switch (reg) {
1299 case MII_BMCR:
1300 return KS_P1MBCR;
1301 case MII_BMSR:
1302 return KS_P1MBSR;
1303 case MII_PHYSID1:
1304 return KS_PHY1ILR;
1305 case MII_PHYSID2:
1306 return KS_PHY1IHR;
1307 case MII_ADVERTISE:
1308 return KS_P1ANAR;
1309 case MII_LPA:
1310 return KS_P1ANLPR;
1311 }
1312
1313 return 0x0;
1314}
1315
1316/**
1317 * ks8851_phy_read - MII interface PHY register read.
1318 * @dev: The network device the PHY is on.
1319 * @phy_addr: Address of PHY (ignored as we only have one)
1320 * @reg: The register to read.
1321 *
1322 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001323 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001324 * are always returned as zero.
1325 *
1326 * We return zero for unsupported registers as the MII code does not check
1327 * the value returned for any error status, and simply returns it to the
1328 * caller. The mii-tool that the driver was tested with takes any -ve error
1329 * as real PHY capabilities, thus displaying incorrect data to the user.
1330 */
1331static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1332{
1333 struct ks8851_net *ks = netdev_priv(dev);
1334 int ksreg;
1335 int result;
1336
1337 ksreg = ks8851_phy_reg(reg);
1338 if (!ksreg)
1339 return 0x0; /* no error return allowed, so use zero */
1340
1341 mutex_lock(&ks->lock);
1342 result = ks8851_rdreg16(ks, ksreg);
1343 mutex_unlock(&ks->lock);
1344
1345 return result;
1346}
1347
1348static void ks8851_phy_write(struct net_device *dev,
1349 int phy, int reg, int value)
1350{
1351 struct ks8851_net *ks = netdev_priv(dev);
1352 int ksreg;
1353
1354 ksreg = ks8851_phy_reg(reg);
1355 if (ksreg) {
1356 mutex_lock(&ks->lock);
1357 ks8851_wrreg16(ks, ksreg, value);
1358 mutex_unlock(&ks->lock);
1359 }
1360}
1361
1362/**
1363 * ks8851_read_selftest - read the selftest memory info.
1364 * @ks: The device state
1365 *
1366 * Read and check the TX/RX memory selftest information.
1367 */
1368static int ks8851_read_selftest(struct ks8851_net *ks)
1369{
1370 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1371 int ret = 0;
1372 unsigned rd;
1373
1374 rd = ks8851_rdreg16(ks, KS_MBIR);
1375
1376 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001377 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001378 return 0;
1379 }
1380
1381 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001382 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001383 ret |= 1;
1384 }
1385
1386 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001387 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001388 ret |= 2;
1389 }
1390
1391 return 0;
1392}
1393
1394/* driver bus management functions */
1395
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001396#ifdef CONFIG_PM
1397static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1398{
1399 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1400 struct net_device *dev = ks->netdev;
1401
1402 if (netif_running(dev)) {
1403 netif_device_detach(dev);
1404 ks8851_net_stop(dev);
1405 }
1406
1407 return 0;
1408}
1409
1410static int ks8851_resume(struct spi_device *spi)
1411{
1412 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1413 struct net_device *dev = ks->netdev;
1414
1415 if (netif_running(dev)) {
1416 ks8851_net_open(dev);
1417 netif_device_attach(dev);
1418 }
1419
1420 return 0;
1421}
1422#else
1423#define ks8851_suspend NULL
1424#define ks8851_resume NULL
1425#endif
1426
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -07001427static int __devinit ks8851_init_hw(struct spi_device *spi,
1428 struct ks8851_net *ks)
1429{
1430 struct ks8851_pdata *pdata = spi->dev.platform_data;
1431 struct device_node *dnode = spi->dev.of_node;
1432 enum of_gpio_flags flags;
1433 int ret;
1434
1435 ks->rst_gpio = -ENODEV;
1436
1437 if (dnode)
1438 ks->rst_gpio = of_get_named_gpio_flags(dnode, "rst-gpio",
1439 0, &flags);
1440 else if (pdata)
1441 ks->rst_gpio = pdata->rst_gpio;
1442
1443 if (gpio_is_valid(ks->rst_gpio)) {
1444 ret = gpio_request(ks->rst_gpio, "ks8851_rst");
1445 if (ret) {
1446 pr_err("ks8851 gpio_request failed: %d\n", ret);
1447 return ret;
1448 }
1449
1450 /* Make sure the chip is in reset state */
1451 gpio_direction_output(ks->rst_gpio, 0);
1452 }
1453
1454 ks->vdd_io = regulator_get(&spi->dev, "vdd-io");
1455
1456 if (IS_ERR(ks->vdd_io)) {
1457 ret = PTR_ERR(ks->vdd_io);
1458 goto fail_gpio;
1459 }
1460
1461 ks->vdd_phy = regulator_get(&spi->dev, "vdd-phy");
1462
1463 if (IS_ERR(ks->vdd_phy)) {
1464 regulator_put(ks->vdd_io);
1465 ret = PTR_ERR(ks->vdd_phy);
1466 goto fail_gpio;
1467 }
1468
1469 regulator_enable(ks->vdd_io);
1470 regulator_enable(ks->vdd_phy);
1471
1472 /* Wait for atleast 10ms after turning on regulator */
1473 usleep_range(10000, 11000);
1474
1475 if (gpio_is_valid(ks->rst_gpio))
1476 gpio_direction_output(ks->rst_gpio, 1);
1477
1478 return 0;
1479
1480fail_gpio:
1481 if (gpio_is_valid(ks->rst_gpio))
1482 gpio_free(ks->rst_gpio);
1483
1484 return ret;
1485}
1486
Ben Dooks3ba81f32009-07-16 05:24:08 +00001487static int __devinit ks8851_probe(struct spi_device *spi)
1488{
1489 struct net_device *ndev;
1490 struct ks8851_net *ks;
1491 int ret;
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001492 unsigned cider;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001493
1494 ndev = alloc_etherdev(sizeof(struct ks8851_net));
Joe Perches41de8d42012-01-29 13:47:52 +00001495 if (!ndev)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001496 return -ENOMEM;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001497
1498 spi->bits_per_word = 8;
1499
1500 ks = netdev_priv(ndev);
1501
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -07001502 ret = ks8851_init_hw(spi, ks);
1503 if (ret)
1504 goto err_init;
1505
Ben Dooks3ba81f32009-07-16 05:24:08 +00001506 ks->netdev = ndev;
1507 ks->spidev = spi;
1508 ks->tx_space = 6144;
1509
1510 mutex_init(&ks->lock);
1511 spin_lock_init(&ks->statelock);
1512
1513 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1514 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1515 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1516
1517 /* initialise pre-made spi transfer messages */
1518
1519 spi_message_init(&ks->spi_msg1);
1520 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1521
1522 spi_message_init(&ks->spi_msg2);
1523 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1524 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1525
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001526 /* setup EEPROM state */
1527
1528 ks->eeprom.data = ks;
1529 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1530 ks->eeprom.register_read = ks8851_eeprom_regread;
1531 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1532
Ben Dooks3ba81f32009-07-16 05:24:08 +00001533 /* setup mii state */
1534 ks->mii.dev = ndev;
1535 ks->mii.phy_id = 1,
1536 ks->mii.phy_id_mask = 1;
1537 ks->mii.reg_num_mask = 0xf;
1538 ks->mii.mdio_read = ks8851_phy_read;
1539 ks->mii.mdio_write = ks8851_phy_write;
1540
1541 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1542
1543 /* set the default message enable */
1544 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1545 NETIF_MSG_PROBE |
1546 NETIF_MSG_LINK));
1547
1548 skb_queue_head_init(&ks->txq);
1549
1550 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1551 SET_NETDEV_DEV(ndev, &spi->dev);
1552
1553 dev_set_drvdata(&spi->dev, ks);
1554
1555 ndev->if_port = IF_PORT_100BASET;
1556 ndev->netdev_ops = &ks8851_netdev_ops;
1557 ndev->irq = spi->irq;
1558
Ben Dooks57dada62009-10-19 23:49:03 +00001559 /* issue a global soft reset to reset the device. */
1560 ks8851_soft_reset(ks, GRR_GSR);
1561
Ben Dooks3ba81f32009-07-16 05:24:08 +00001562 /* simple check for a valid chip being connected to the bus */
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001563 cider = ks8851_rdreg16(ks, KS_CIDER);
1564 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
Ben Dooks3ba81f32009-07-16 05:24:08 +00001565 dev_err(&spi->dev, "failed to read device ID\n");
1566 ret = -ENODEV;
1567 goto err_id;
1568 }
1569
Sebastien Jan7d997462010-05-05 08:45:52 +00001570 /* cache the contents of the CCR register for EEPROM, etc. */
1571 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1572
1573 if (ks->rc_ccr & CCR_EEPROM)
1574 ks->eeprom_size = 128;
1575 else
1576 ks->eeprom_size = 0;
1577
Ben Dooks3ba81f32009-07-16 05:24:08 +00001578 ks8851_read_selftest(ks);
1579 ks8851_init_mac(ks);
1580
1581 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1582 ndev->name, ks);
1583 if (ret < 0) {
1584 dev_err(&spi->dev, "failed to get irq\n");
1585 goto err_irq;
1586 }
1587
1588 ret = register_netdev(ndev);
1589 if (ret) {
1590 dev_err(&spi->dev, "failed to register network device\n");
1591 goto err_netdev;
1592 }
1593
Ben Dooksa9a8de22011-11-21 08:57:58 +00001594 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001595 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
Ben Dooksa9a8de22011-11-21 08:57:58 +00001596 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001597
1598 return 0;
1599
1600
1601err_netdev:
Matt Renzelmanne8195b22012-04-19 07:17:17 +00001602 free_irq(ndev->irq, ks);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001603
1604err_id:
1605err_irq:
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -07001606 if (gpio_is_valid(ks->rst_gpio))
1607 gpio_free(ks->rst_gpio);
1608
1609 if (!IS_ERR(ks->vdd_io)) {
1610 regulator_disable(ks->vdd_io);
1611 regulator_put(ks->vdd_io);
1612 }
1613
1614 if (!IS_ERR(ks->vdd_phy)) {
1615 regulator_disable(ks->vdd_phy);
1616 regulator_put(ks->vdd_phy);
1617 }
1618
1619err_init:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001620 free_netdev(ndev);
1621 return ret;
1622}
1623
1624static int __devexit ks8851_remove(struct spi_device *spi)
1625{
1626 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
1627
1628 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001629 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001630
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -07001631 if (gpio_is_valid(priv->rst_gpio))
1632 gpio_free(priv->rst_gpio);
1633
1634 if (!IS_ERR(priv->vdd_io)) {
1635 regulator_disable(priv->vdd_io);
1636 regulator_put(priv->vdd_io);
1637 }
1638
1639 if (!IS_ERR(priv->vdd_phy)) {
1640 regulator_disable(priv->vdd_phy);
1641 regulator_put(priv->vdd_phy);
1642 }
1643
Ben Dooks3ba81f32009-07-16 05:24:08 +00001644 unregister_netdev(priv->netdev);
1645 free_irq(spi->irq, priv);
1646 free_netdev(priv->netdev);
1647
1648 return 0;
1649}
1650
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -07001651static struct of_device_id ks8851_match_table[] = {
1652 {
1653 .compatible = "micrel,ks8851",
1654 },
1655 {}
1656};
1657
Ben Dooks3ba81f32009-07-16 05:24:08 +00001658static struct spi_driver ks8851_driver = {
1659 .driver = {
1660 .name = "ks8851",
Stepan Moskovchenkoccae4c42012-08-22 18:45:44 -07001661 .of_match_table = ks8851_match_table,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001662 .owner = THIS_MODULE,
1663 },
1664 .probe = ks8851_probe,
1665 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001666 .suspend = ks8851_suspend,
1667 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001668};
1669
1670static int __init ks8851_init(void)
1671{
1672 return spi_register_driver(&ks8851_driver);
1673}
1674
1675static void __exit ks8851_exit(void)
1676{
1677 spi_unregister_driver(&ks8851_driver);
1678}
1679
1680module_init(ks8851_init);
1681module_exit(ks8851_exit);
1682
1683MODULE_DESCRIPTION("KS8851 Network driver");
1684MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1685MODULE_LICENSE("GPL");
1686
1687module_param_named(message, msg_enable, int, 0);
1688MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001689MODULE_ALIAS("spi:ks8851");