blob: 208e25a9e08cc05ead0f8ae6adc8bc7375542bdd [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
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>
25
26#include <linux/spi/spi.h>
27
28#include "ks8851.h"
29
30/**
31 * struct ks8851_rxctrl - KS8851 driver rx control
32 * @mchash: Multicast hash-table data.
33 * @rxcr1: KS_RXCR1 register setting
34 * @rxcr2: KS_RXCR2 register setting
35 *
36 * Representation of the settings needs to control the receive filtering
37 * such as the multicast hash-filter and the receive register settings. This
38 * is used to make the job of working out if the receive settings change and
39 * then issuing the new settings to the worker that will send the necessary
40 * commands.
41 */
42struct ks8851_rxctrl {
43 u16 mchash[4];
44 u16 rxcr1;
45 u16 rxcr2;
46};
47
48/**
49 * union ks8851_tx_hdr - tx header data
50 * @txb: The header as bytes
51 * @txw: The header as 16bit, little-endian words
52 *
53 * A dual representation of the tx header data to allow
54 * access to individual bytes, and to allow 16bit accesses
55 * with 16bit alignment.
56 */
57union ks8851_tx_hdr {
58 u8 txb[6];
59 __le16 txw[3];
60};
61
62/**
63 * struct ks8851_net - KS8851 driver private data
64 * @netdev: The network device we're bound to
65 * @spidev: The spi device we're bound to.
66 * @lock: Lock to ensure that the device is not accessed when busy.
67 * @statelock: Lock on this structure for tx list.
68 * @mii: The MII state information for the mii calls.
69 * @rxctrl: RX settings for @rxctrl_work.
70 * @tx_work: Work queue for tx packets
71 * @irq_work: Work queue for servicing interrupts
72 * @rxctrl_work: Work queue for updating RX mode and multicast lists
73 * @txq: Queue of packets for transmission.
74 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
75 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
76 * @txh: Space for generating packet TX header in DMA-able data
77 * @rxd: Space for receiving SPI data, in DMA-able space.
78 * @txd: Space for transmitting SPI data, in DMA-able space.
79 * @msg_enable: The message flags controlling driver output (see ethtool).
80 * @fid: Incrementing frame id tag.
81 * @rc_ier: Cached copy of KS_IER.
Sebastien Jan7d997462010-05-05 08:45:52 +000082 * @rc_ccr: Cached copy of KS_CCR.
Ben Dooks3ba81f32009-07-16 05:24:08 +000083 * @rc_rxqcr: Cached copy of KS_RXQCR.
Sebastien Jan7d997462010-05-05 08:45:52 +000084 * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
Ben Dooks3ba81f32009-07-16 05:24:08 +000085 *
86 * The @lock ensures that the chip is protected when certain operations are
87 * in progress. When the read or write packet transfer is in progress, most
88 * of the chip registers are not ccessible until the transfer is finished and
89 * the DMA has been de-asserted.
90 *
91 * The @statelock is used to protect information in the structure which may
92 * need to be accessed via several sources, such as the network driver layer
93 * or one of the work queues.
94 *
95 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
96 * wants to DMA map them, it will not have any problems with data the driver
97 * modifies.
98 */
99struct ks8851_net {
100 struct net_device *netdev;
101 struct spi_device *spidev;
102 struct mutex lock;
103 spinlock_t statelock;
104
105 union ks8851_tx_hdr txh ____cacheline_aligned;
106 u8 rxd[8];
107 u8 txd[8];
108
109 u32 msg_enable ____cacheline_aligned;
110 u16 tx_space;
111 u8 fid;
112
113 u16 rc_ier;
114 u16 rc_rxqcr;
Sebastien Jan7d997462010-05-05 08:45:52 +0000115 u16 rc_ccr;
116 u16 eeprom_size;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000117
118 struct mii_if_info mii;
119 struct ks8851_rxctrl rxctrl;
120
121 struct work_struct tx_work;
122 struct work_struct irq_work;
123 struct work_struct rxctrl_work;
124
125 struct sk_buff_head txq;
126
127 struct spi_message spi_msg1;
128 struct spi_message spi_msg2;
129 struct spi_transfer spi_xfer1;
130 struct spi_transfer spi_xfer2[2];
131};
132
133static int msg_enable;
134
Ben Dooks3ba81f32009-07-16 05:24:08 +0000135/* shift for byte-enable data */
136#define BYTE_EN(_x) ((_x) << 2)
137
138/* turn register number and byte-enable mask into data for start of packet */
139#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
140
141/* SPI register read/write calls.
142 *
143 * All these calls issue SPI transactions to access the chip's registers. They
144 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300145 * chip is busy transferring packet data (RX/TX FIFO accesses).
Ben Dooks3ba81f32009-07-16 05:24:08 +0000146 */
147
148/**
149 * ks8851_wrreg16 - write 16bit register value to chip
150 * @ks: The chip state
151 * @reg: The register address
152 * @val: The value to write
153 *
154 * Issue a write to put the value @val into the register specified in @reg.
155 */
156static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
157{
158 struct spi_transfer *xfer = &ks->spi_xfer1;
159 struct spi_message *msg = &ks->spi_msg1;
160 __le16 txb[2];
161 int ret;
162
163 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
164 txb[1] = cpu_to_le16(val);
165
166 xfer->tx_buf = txb;
167 xfer->rx_buf = NULL;
168 xfer->len = 4;
169
170 ret = spi_sync(ks->spidev, msg);
171 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000172 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000173}
174
175/**
Ben Dooks160d0fa2009-10-19 23:49:04 +0000176 * ks8851_wrreg8 - write 8bit register value to chip
177 * @ks: The chip state
178 * @reg: The register address
179 * @val: The value to write
180 *
181 * Issue a write to put the value @val into the register specified in @reg.
182 */
183static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
184{
185 struct spi_transfer *xfer = &ks->spi_xfer1;
186 struct spi_message *msg = &ks->spi_msg1;
187 __le16 txb[2];
188 int ret;
189 int bit;
190
191 bit = 1 << (reg & 3);
192
193 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
194 txb[1] = val;
195
196 xfer->tx_buf = txb;
197 xfer->rx_buf = NULL;
198 xfer->len = 3;
199
200 ret = spi_sync(ks->spidev, msg);
201 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000202 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks160d0fa2009-10-19 23:49:04 +0000203}
204
205/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000206 * ks8851_rx_1msg - select whether to use one or two messages for spi read
207 * @ks: The device structure
208 *
209 * Return whether to generate a single message with a tx and rx buffer
210 * supplied to spi_sync(), or alternatively send the tx and rx buffers
211 * as separate messages.
212 *
213 * Depending on the hardware in use, a single message may be more efficient
214 * on interrupts or work done by the driver.
215 *
216 * This currently always returns true until we add some per-device data passed
217 * from the platform code to specify which mode is better.
218 */
219static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
220{
221 return true;
222}
223
224/**
225 * ks8851_rdreg - issue read register command and return the data
226 * @ks: The device state
227 * @op: The register address and byte enables in message format.
228 * @rxb: The RX buffer to return the result into
229 * @rxl: The length of data expected.
230 *
231 * This is the low level read call that issues the necessary spi message(s)
232 * to read data from the register specified in @op.
233 */
234static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
235 u8 *rxb, unsigned rxl)
236{
237 struct spi_transfer *xfer;
238 struct spi_message *msg;
239 __le16 *txb = (__le16 *)ks->txd;
240 u8 *trx = ks->rxd;
241 int ret;
242
243 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
244
245 if (ks8851_rx_1msg(ks)) {
246 msg = &ks->spi_msg1;
247 xfer = &ks->spi_xfer1;
248
249 xfer->tx_buf = txb;
250 xfer->rx_buf = trx;
251 xfer->len = rxl + 2;
252 } else {
253 msg = &ks->spi_msg2;
254 xfer = ks->spi_xfer2;
255
256 xfer->tx_buf = txb;
257 xfer->rx_buf = NULL;
258 xfer->len = 2;
259
260 xfer++;
261 xfer->tx_buf = NULL;
262 xfer->rx_buf = trx;
263 xfer->len = rxl;
264 }
265
266 ret = spi_sync(ks->spidev, msg);
267 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000268 netdev_err(ks->netdev, "read: spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000269 else if (ks8851_rx_1msg(ks))
270 memcpy(rxb, trx + 2, rxl);
271 else
272 memcpy(rxb, trx, rxl);
273}
274
275/**
276 * ks8851_rdreg8 - read 8 bit register from device
277 * @ks: The chip information
278 * @reg: The register address
279 *
280 * Read a 8bit register from the chip, returning the result
281*/
282static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
283{
284 u8 rxb[1];
285
286 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
287 return rxb[0];
288}
289
290/**
291 * ks8851_rdreg16 - read 16 bit register from device
292 * @ks: The chip information
293 * @reg: The register address
294 *
295 * Read a 16bit register from the chip, returning the result
296*/
297static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
298{
299 __le16 rx = 0;
300
301 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
302 return le16_to_cpu(rx);
303}
304
305/**
306 * ks8851_rdreg32 - read 32 bit register from device
307 * @ks: The chip information
308 * @reg: The register address
309 *
310 * Read a 32bit register from the chip.
311 *
312 * Note, this read requires the address be aligned to 4 bytes.
313*/
314static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
315{
316 __le32 rx = 0;
317
318 WARN_ON(reg & 3);
319
320 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
321 return le32_to_cpu(rx);
322}
323
324/**
325 * ks8851_soft_reset - issue one of the soft reset to the device
326 * @ks: The device state.
327 * @op: The bit(s) to set in the GRR
328 *
329 * Issue the relevant soft-reset command to the device's GRR register
330 * specified by @op.
331 *
332 * Note, the delays are in there as a caution to ensure that the reset
333 * has time to take effect and then complete. Since the datasheet does
334 * not currently specify the exact sequence, we have chosen something
335 * that seems to work with our device.
336 */
337static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
338{
339 ks8851_wrreg16(ks, KS_GRR, op);
340 mdelay(1); /* wait a short time to effect reset */
341 ks8851_wrreg16(ks, KS_GRR, 0);
342 mdelay(1); /* wait for condition to clear */
343}
344
345/**
Tristram Ha32f160d2011-11-21 08:57:59 +0000346 * ks8851_set_powermode - set power mode of the device
347 * @ks: The device state
348 * @pwrmode: The power mode value to write to KS_PMECR.
349 *
350 * Change the power mode of the chip.
351 */
352static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
353{
354 unsigned pmecr;
355
356 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
357
358 pmecr = ks8851_rdreg16(ks, KS_PMECR);
359 pmecr &= ~PMECR_PM_MASK;
360 pmecr |= pwrmode;
361
362 ks8851_wrreg16(ks, KS_PMECR, pmecr);
363}
364
365/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000366 * ks8851_write_mac_addr - write mac address to device registers
367 * @dev: The network device
368 *
369 * Update the KS8851 MAC address registers from the address in @dev.
370 *
371 * This call assumes that the chip is not running, so there is no need to
372 * shutdown the RXQ process whilst setting this.
373*/
374static int ks8851_write_mac_addr(struct net_device *dev)
375{
376 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000377 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000378
379 mutex_lock(&ks->lock);
380
Tristram Ha32f160d2011-11-21 08:57:59 +0000381 /*
382 * Wake up chip in case it was powered off when stopped; otherwise,
383 * the first write to the MAC address does not take effect.
384 */
385 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000386 for (i = 0; i < ETH_ALEN; i++)
387 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
Tristram Ha32f160d2011-11-21 08:57:59 +0000388 if (!netif_running(dev))
389 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000390
391 mutex_unlock(&ks->lock);
392
393 return 0;
394}
395
396/**
Ben Dooksa9a8de22011-11-21 08:57:58 +0000397 * ks8851_read_mac_addr - read mac address from device registers
398 * @dev: The network device
399 *
400 * Update our copy of the KS8851 MAC address from the registers of @dev.
401*/
402static void ks8851_read_mac_addr(struct net_device *dev)
403{
404 struct ks8851_net *ks = netdev_priv(dev);
405 int i;
406
407 mutex_lock(&ks->lock);
408
409 for (i = 0; i < ETH_ALEN; i++)
410 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
411
412 mutex_unlock(&ks->lock);
413}
414
415/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000416 * ks8851_init_mac - initialise the mac address
417 * @ks: The device structure
418 *
419 * Get or create the initial mac address for the device and then set that
Ben Dooksa9a8de22011-11-21 08:57:58 +0000420 * into the station address register. If there is an EEPROM present, then
421 * we try that. If no valid mac address is found we use random_ether_addr()
Ben Dooks3ba81f32009-07-16 05:24:08 +0000422 * to create a new one.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000423 */
424static void ks8851_init_mac(struct ks8851_net *ks)
425{
426 struct net_device *dev = ks->netdev;
427
Ben Dooksa9a8de22011-11-21 08:57:58 +0000428 /* first, try reading what we've got already */
429 if (ks->rc_ccr & CCR_EEPROM) {
430 ks8851_read_mac_addr(dev);
431 if (is_valid_ether_addr(dev->dev_addr))
432 return;
433
434 netdev_err(ks->netdev, "invalid mac address read %pM\n",
435 dev->dev_addr);
436 }
437
Ben Dooks3ba81f32009-07-16 05:24:08 +0000438 random_ether_addr(dev->dev_addr);
439 ks8851_write_mac_addr(dev);
440}
441
442/**
443 * ks8851_irq - device interrupt handler
444 * @irq: Interrupt number passed from the IRQ hnalder.
445 * @pw: The private word passed to register_irq(), our struct ks8851_net.
446 *
447 * Disable the interrupt from happening again until we've processed the
448 * current status by scheduling ks8851_irq_work().
449 */
450static irqreturn_t ks8851_irq(int irq, void *pw)
451{
452 struct ks8851_net *ks = pw;
453
454 disable_irq_nosync(irq);
455 schedule_work(&ks->irq_work);
456 return IRQ_HANDLED;
457}
458
459/**
460 * ks8851_rdfifo - read data from the receive fifo
461 * @ks: The device state.
462 * @buff: The buffer address
463 * @len: The length of the data to read
464 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100465 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000466 * the FIFO into the buffer specified by @buff.
467 */
468static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
469{
470 struct spi_transfer *xfer = ks->spi_xfer2;
471 struct spi_message *msg = &ks->spi_msg2;
472 u8 txb[1];
473 int ret;
474
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000475 netif_dbg(ks, rx_status, ks->netdev,
476 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000477
478 /* set the operation we're issuing */
479 txb[0] = KS_SPIOP_RXFIFO;
480
481 xfer->tx_buf = txb;
482 xfer->rx_buf = NULL;
483 xfer->len = 1;
484
485 xfer++;
486 xfer->rx_buf = buff;
487 xfer->tx_buf = NULL;
488 xfer->len = len;
489
490 ret = spi_sync(ks->spidev, msg);
491 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000492 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000493}
494
495/**
496 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
497 * @ks: The device state
498 * @rxpkt: The data for the received packet
499 *
500 * Dump the initial data from the packet to dev_dbg().
501*/
502static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
503{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000504 netdev_dbg(ks->netdev,
505 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
506 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
507 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
508 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000509}
510
511/**
512 * ks8851_rx_pkts - receive packets from the host
513 * @ks: The device information.
514 *
515 * This is called from the IRQ work queue when the system detects that there
516 * are packets in the receive queue. Find out how many packets there are and
517 * read them from the FIFO.
518 */
519static void ks8851_rx_pkts(struct ks8851_net *ks)
520{
521 struct sk_buff *skb;
522 unsigned rxfc;
523 unsigned rxlen;
524 unsigned rxstat;
525 u32 rxh;
526 u8 *rxpkt;
527
528 rxfc = ks8851_rdreg8(ks, KS_RXFC);
529
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000530 netif_dbg(ks, rx_status, ks->netdev,
531 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000532
533 /* Currently we're issuing a read per packet, but we could possibly
534 * improve the code by issuing a single read, getting the receive
535 * header, allocating the packet and then reading the packet data
536 * out in one go.
537 *
538 * This form of operation would require us to hold the SPI bus'
539 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300540 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000541 */
542
543 for (; rxfc != 0; rxfc--) {
544 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
545 rxstat = rxh & 0xffff;
546 rxlen = rxh >> 16;
547
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000548 netif_dbg(ks, rx_status, ks->netdev,
549 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000550
551 /* the length of the packet includes the 32bit CRC */
552
553 /* set dma read address */
554 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
555
556 /* start the packet dma process, and set auto-dequeue rx */
557 ks8851_wrreg16(ks, KS_RXQCR,
558 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
559
Eric Dumazet972c40b2010-09-08 13:26:55 +0000560 if (rxlen > 4) {
561 unsigned int rxalign;
562
563 rxlen -= 4;
564 rxalign = ALIGN(rxlen, 4);
565 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
566 if (skb) {
567
568 /* 4 bytes of status header + 4 bytes of
569 * garbage: we put them before ethernet
570 * header, so that they are copied,
571 * but ignored.
572 */
573
574 rxpkt = skb_put(skb, rxlen) - 8;
575
576 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
577
578 if (netif_msg_pktdata(ks))
579 ks8851_dbg_dumpkkt(ks, rxpkt);
580
581 skb->protocol = eth_type_trans(skb, ks->netdev);
582 netif_rx(skb);
583
584 ks->netdev->stats.rx_packets++;
585 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000586 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000587 }
588
589 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
590 }
591}
592
593/**
594 * ks8851_irq_work - work queue handler for dealing with interrupt requests
595 * @work: The work structure that was scheduled by schedule_work()
596 *
597 * This is the handler invoked when the ks8851_irq() is called to find out
598 * what happened, as we cannot allow ourselves to sleep whilst waiting for
599 * anything other process has the chip's lock.
600 *
601 * Read the interrupt status, work out what needs to be done and then clear
602 * any of the interrupts that are not needed.
603 */
604static void ks8851_irq_work(struct work_struct *work)
605{
606 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
607 unsigned status;
608 unsigned handled = 0;
609
610 mutex_lock(&ks->lock);
611
612 status = ks8851_rdreg16(ks, KS_ISR);
613
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000614 netif_dbg(ks, intr, ks->netdev,
615 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000616
617 if (status & IRQ_LCI) {
618 /* should do something about checking link status */
619 handled |= IRQ_LCI;
620 }
621
622 if (status & IRQ_LDI) {
623 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
624 pmecr &= ~PMECR_WKEVT_MASK;
625 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
626
627 handled |= IRQ_LDI;
628 }
629
630 if (status & IRQ_RXPSI)
631 handled |= IRQ_RXPSI;
632
633 if (status & IRQ_TXI) {
634 handled |= IRQ_TXI;
635
636 /* no lock here, tx queue should have been stopped */
637
638 /* update our idea of how much tx space is available to the
639 * system */
640 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
641
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000642 netif_dbg(ks, intr, ks->netdev,
643 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000644 }
645
646 if (status & IRQ_RXI)
647 handled |= IRQ_RXI;
648
649 if (status & IRQ_SPIBEI) {
650 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
651 handled |= IRQ_SPIBEI;
652 }
653
654 ks8851_wrreg16(ks, KS_ISR, handled);
655
656 if (status & IRQ_RXI) {
657 /* the datasheet says to disable the rx interrupt during
658 * packet read-out, however we're masking the interrupt
659 * from the device so do not bother masking just the RX
660 * from the device. */
661
662 ks8851_rx_pkts(ks);
663 }
664
665 /* if something stopped the rx process, probably due to wanting
666 * to change the rx settings, then do something about restarting
667 * it. */
668 if (status & IRQ_RXPSI) {
669 struct ks8851_rxctrl *rxc = &ks->rxctrl;
670
671 /* update the multicast hash table */
672 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
673 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
674 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
675 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
676
677 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
678 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
679 }
680
681 mutex_unlock(&ks->lock);
682
683 if (status & IRQ_TXI)
684 netif_wake_queue(ks->netdev);
685
686 enable_irq(ks->netdev->irq);
687}
688
689/**
690 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300691 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000692 *
693 * Returns the size of the TXFIFO message needed to send
694 * this packet.
695 */
696static inline unsigned calc_txlen(unsigned len)
697{
698 return ALIGN(len + 4, 4);
699}
700
701/**
702 * ks8851_wrpkt - write packet to TX FIFO
703 * @ks: The device state.
704 * @txp: The sk_buff to transmit.
705 * @irq: IRQ on completion of the packet.
706 *
707 * Send the @txp to the chip. This means creating the relevant packet header
708 * specifying the length of the packet and the other information the chip
709 * needs, such as IRQ on completion. Send the header and the packet data to
710 * the device.
711 */
712static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
713{
714 struct spi_transfer *xfer = ks->spi_xfer2;
715 struct spi_message *msg = &ks->spi_msg2;
716 unsigned fid = 0;
717 int ret;
718
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000719 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
720 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000721
722 fid = ks->fid++;
723 fid &= TXFR_TXFID_MASK;
724
725 if (irq)
726 fid |= TXFR_TXIC; /* irq on completion */
727
728 /* start header at txb[1] to align txw entries */
729 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
730 ks->txh.txw[1] = cpu_to_le16(fid);
731 ks->txh.txw[2] = cpu_to_le16(txp->len);
732
733 xfer->tx_buf = &ks->txh.txb[1];
734 xfer->rx_buf = NULL;
735 xfer->len = 5;
736
737 xfer++;
738 xfer->tx_buf = txp->data;
739 xfer->rx_buf = NULL;
740 xfer->len = ALIGN(txp->len, 4);
741
742 ret = spi_sync(ks->spidev, msg);
743 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000744 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000745}
746
747/**
748 * ks8851_done_tx - update and then free skbuff after transmitting
749 * @ks: The device state
750 * @txb: The buffer transmitted
751 */
752static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
753{
754 struct net_device *dev = ks->netdev;
755
756 dev->stats.tx_bytes += txb->len;
757 dev->stats.tx_packets++;
758
759 dev_kfree_skb(txb);
760}
761
762/**
763 * ks8851_tx_work - process tx packet(s)
764 * @work: The work strucutre what was scheduled.
765 *
766 * This is called when a number of packets have been scheduled for
767 * transmission and need to be sent to the device.
768 */
769static void ks8851_tx_work(struct work_struct *work)
770{
771 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
772 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000773 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000774
775 mutex_lock(&ks->lock);
776
777 while (!last) {
778 txb = skb_dequeue(&ks->txq);
779 last = skb_queue_empty(&ks->txq);
780
Abraham Arce761172f2010-04-16 14:48:43 +0000781 if (txb != NULL) {
782 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
783 ks8851_wrpkt(ks, txb, last);
784 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
785 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000786
Abraham Arce761172f2010-04-16 14:48:43 +0000787 ks8851_done_tx(ks, txb);
788 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000789 }
790
791 mutex_unlock(&ks->lock);
792}
793
794/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000795 * ks8851_net_open - open network device
796 * @dev: The network device being opened.
797 *
798 * Called when the network device is marked active, such as a user executing
799 * 'ifconfig up' on the device.
800 */
801static int ks8851_net_open(struct net_device *dev)
802{
803 struct ks8851_net *ks = netdev_priv(dev);
804
805 /* lock the card, even if we may not actually be doing anything
806 * else at the moment */
807 mutex_lock(&ks->lock);
808
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000809 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000810
811 /* bring chip out of any power saving mode it was in */
812 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
813
814 /* issue a soft reset to the RX/TX QMU to put it into a known
815 * state. */
816 ks8851_soft_reset(ks, GRR_QMU);
817
818 /* setup transmission parameters */
819
820 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
821 TXCR_TXPE | /* pad to min length */
822 TXCR_TXCRC | /* add CRC */
823 TXCR_TXFCE)); /* enable flow control */
824
825 /* auto-increment tx data, reset tx pointer */
826 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
827
828 /* setup receiver control */
829
830 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
831 RXCR1_RXFCE | /* enable flow control */
832 RXCR1_RXBE | /* broadcast enable */
833 RXCR1_RXUE | /* unicast enable */
834 RXCR1_RXE)); /* enable rx block */
835
836 /* transfer entire frames out in one go */
837 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
838
839 /* set receive counter timeouts */
840 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
841 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
842 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
843
844 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
845 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
846 RXQCR_RXDTTE); /* IRQ on time exceeded */
847
848 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
849
850 /* clear then enable interrupts */
851
852#define STD_IRQ (IRQ_LCI | /* Link Change */ \
853 IRQ_TXI | /* TX done */ \
854 IRQ_RXI | /* RX done */ \
855 IRQ_SPIBEI | /* SPI bus error */ \
856 IRQ_TXPSI | /* TX process stop */ \
857 IRQ_RXPSI) /* RX process stop */
858
859 ks->rc_ier = STD_IRQ;
860 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
861 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
862
863 netif_start_queue(ks->netdev);
864
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000865 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000866
867 mutex_unlock(&ks->lock);
868 return 0;
869}
870
871/**
872 * ks8851_net_stop - close network device
873 * @dev: The device being closed.
874 *
875 * Called to close down a network device which has been active. Cancell any
876 * work, shutdown the RX and TX process and then place the chip into a low
877 * power state whilst it is not being used.
878 */
879static int ks8851_net_stop(struct net_device *dev)
880{
881 struct ks8851_net *ks = netdev_priv(dev);
882
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000883 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000884
885 netif_stop_queue(dev);
886
887 mutex_lock(&ks->lock);
888
889 /* stop any outstanding work */
890 flush_work(&ks->irq_work);
891 flush_work(&ks->tx_work);
892 flush_work(&ks->rxctrl_work);
893
894 /* turn off the IRQs and ack any outstanding */
895 ks8851_wrreg16(ks, KS_IER, 0x0000);
896 ks8851_wrreg16(ks, KS_ISR, 0xffff);
897
898 /* shutdown RX process */
899 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
900
901 /* shutdown TX process */
902 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
903
904 /* set powermode to soft power down to save power */
905 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
906
907 /* ensure any queued tx buffers are dumped */
908 while (!skb_queue_empty(&ks->txq)) {
909 struct sk_buff *txb = skb_dequeue(&ks->txq);
910
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000911 netif_dbg(ks, ifdown, ks->netdev,
912 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000913
914 dev_kfree_skb(txb);
915 }
916
917 mutex_unlock(&ks->lock);
918 return 0;
919}
920
921/**
922 * ks8851_start_xmit - transmit packet
923 * @skb: The buffer to transmit
924 * @dev: The device used to transmit the packet.
925 *
926 * Called by the network layer to transmit the @skb. Queue the packet for
927 * the device and schedule the necessary work to transmit the packet when
928 * it is free.
929 *
930 * We do this to firstly avoid sleeping with the network device locked,
931 * and secondly so we can round up more than one packet to transmit which
932 * means we can try and avoid generating too many transmit done interrupts.
933 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000934static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
935 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000936{
937 struct ks8851_net *ks = netdev_priv(dev);
938 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000939 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000940
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000941 netif_dbg(ks, tx_queued, ks->netdev,
942 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000943
944 spin_lock(&ks->statelock);
945
946 if (needed > ks->tx_space) {
947 netif_stop_queue(dev);
948 ret = NETDEV_TX_BUSY;
949 } else {
950 ks->tx_space -= needed;
951 skb_queue_tail(&ks->txq, skb);
952 }
953
954 spin_unlock(&ks->statelock);
955 schedule_work(&ks->tx_work);
956
957 return ret;
958}
959
960/**
961 * ks8851_rxctrl_work - work handler to change rx mode
962 * @work: The work structure this belongs to.
963 *
964 * Lock the device and issue the necessary changes to the receive mode from
965 * the network device layer. This is done so that we can do this without
966 * having to sleep whilst holding the network device lock.
967 *
968 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
969 * receive parameters are programmed, we issue a write to disable the RXQ and
970 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
971 * complete. The interrupt handler then writes the new values into the chip.
972 */
973static void ks8851_rxctrl_work(struct work_struct *work)
974{
975 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
976
977 mutex_lock(&ks->lock);
978
979 /* need to shutdown RXQ before modifying filter parameters */
980 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
981
982 mutex_unlock(&ks->lock);
983}
984
985static void ks8851_set_rx_mode(struct net_device *dev)
986{
987 struct ks8851_net *ks = netdev_priv(dev);
988 struct ks8851_rxctrl rxctrl;
989
990 memset(&rxctrl, 0, sizeof(rxctrl));
991
992 if (dev->flags & IFF_PROMISC) {
993 /* interface to receive everything */
994
995 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
996 } else if (dev->flags & IFF_ALLMULTI) {
997 /* accept all multicast packets */
998
999 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1000 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001001 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001002 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001003 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001004
1005 /* accept some multicast */
1006
Jiri Pirko22bedad32010-04-01 21:22:57 +00001007 netdev_for_each_mc_addr(ha, dev) {
1008 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001009 crc >>= (32 - 6); /* get top six bits */
1010
1011 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001012 }
1013
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001014 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001015 } else {
1016 /* just accept broadcast / unicast */
1017 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1018 }
1019
1020 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1021 RXCR1_RXBE | /* broadcast enable */
1022 RXCR1_RXE | /* RX process enable */
1023 RXCR1_RXFCE); /* enable flow control */
1024
1025 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1026
1027 /* schedule work to do the actual set of the data if needed */
1028
1029 spin_lock(&ks->statelock);
1030
1031 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1032 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1033 schedule_work(&ks->rxctrl_work);
1034 }
1035
1036 spin_unlock(&ks->statelock);
1037}
1038
1039static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1040{
1041 struct sockaddr *sa = addr;
1042
1043 if (netif_running(dev))
1044 return -EBUSY;
1045
1046 if (!is_valid_ether_addr(sa->sa_data))
1047 return -EADDRNOTAVAIL;
1048
1049 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1050 return ks8851_write_mac_addr(dev);
1051}
1052
1053static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1054{
1055 struct ks8851_net *ks = netdev_priv(dev);
1056
1057 if (!netif_running(dev))
1058 return -EINVAL;
1059
1060 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1061}
1062
1063static const struct net_device_ops ks8851_netdev_ops = {
1064 .ndo_open = ks8851_net_open,
1065 .ndo_stop = ks8851_net_stop,
1066 .ndo_do_ioctl = ks8851_net_ioctl,
1067 .ndo_start_xmit = ks8851_start_xmit,
1068 .ndo_set_mac_address = ks8851_set_mac_address,
1069 .ndo_set_rx_mode = ks8851_set_rx_mode,
1070 .ndo_change_mtu = eth_change_mtu,
1071 .ndo_validate_addr = eth_validate_addr,
1072};
1073
Sebastien Jana4bdfff2010-05-05 08:45:53 +00001074/* Companion eeprom access */
1075
1076enum { /* EEPROM programming states */
1077 EEPROM_CONTROL,
1078 EEPROM_ADDRESS,
1079 EEPROM_DATA,
1080 EEPROM_COMPLETE
1081};
1082
1083/**
1084 * ks8851_eeprom_read - read a 16bits word in ks8851 companion EEPROM
1085 * @dev: The network device the PHY is on.
1086 * @addr: EEPROM address to read
1087 *
1088 * eeprom_size: used to define the data coding length. Can be changed
1089 * through debug-fs.
1090 *
1091 * Programs a read on the EEPROM using ks8851 EEPROM SW access feature.
1092 * Warning: The READ feature is not supported on ks8851 revision 0.
1093 *
1094 * Rough programming model:
1095 * - on period start: set clock high and read value on bus
1096 * - on period / 2: set clock low and program value on bus
1097 * - start on period / 2
1098 */
1099unsigned int ks8851_eeprom_read(struct net_device *dev, unsigned int addr)
1100{
1101 struct ks8851_net *ks = netdev_priv(dev);
1102 int eepcr;
1103 int ctrl = EEPROM_OP_READ;
1104 int state = EEPROM_CONTROL;
1105 int bit_count = EEPROM_OP_LEN - 1;
1106 unsigned int data = 0;
1107 int dummy;
1108 unsigned int addr_len;
1109
1110 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1111
1112 /* start transaction: chip select high, authorize write */
1113 mutex_lock(&ks->lock);
1114 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1115 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1116 eepcr |= EEPCR_EECS;
1117 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1118 mutex_unlock(&ks->lock);
1119
1120 while (state != EEPROM_COMPLETE) {
1121 /* falling clock period starts... */
1122 /* set EED_IO pin for control and address */
1123 eepcr &= ~EEPCR_EEDO;
1124 switch (state) {
1125 case EEPROM_CONTROL:
1126 eepcr |= ((ctrl >> bit_count) & 1) << 2;
1127 if (bit_count-- <= 0) {
1128 bit_count = addr_len - 1;
1129 state = EEPROM_ADDRESS;
1130 }
1131 break;
1132 case EEPROM_ADDRESS:
1133 eepcr |= ((addr >> bit_count) & 1) << 2;
1134 bit_count--;
1135 break;
1136 case EEPROM_DATA:
1137 /* Change to receive mode */
1138 eepcr &= ~EEPCR_EESRWA;
1139 break;
1140 }
1141
1142 /* lower clock */
1143 eepcr &= ~EEPCR_EESCK;
1144
1145 mutex_lock(&ks->lock);
1146 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1147 mutex_unlock(&ks->lock);
1148
1149 /* waitread period / 2 */
1150 udelay(EEPROM_SK_PERIOD / 2);
1151
1152 /* rising clock period starts... */
1153
1154 /* raise clock */
1155 mutex_lock(&ks->lock);
1156 eepcr |= EEPCR_EESCK;
1157 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1158 mutex_unlock(&ks->lock);
1159
1160 /* Manage read */
1161 switch (state) {
1162 case EEPROM_ADDRESS:
1163 if (bit_count < 0) {
1164 bit_count = EEPROM_DATA_LEN - 1;
1165 state = EEPROM_DATA;
1166 }
1167 break;
1168 case EEPROM_DATA:
1169 mutex_lock(&ks->lock);
1170 dummy = ks8851_rdreg16(ks, KS_EEPCR);
1171 mutex_unlock(&ks->lock);
1172 data |= ((dummy >> EEPCR_EESB_OFFSET) & 1) << bit_count;
1173 if (bit_count-- <= 0)
1174 state = EEPROM_COMPLETE;
1175 break;
1176 }
1177
1178 /* wait period / 2 */
1179 udelay(EEPROM_SK_PERIOD / 2);
1180 }
1181
1182 /* close transaction */
1183 mutex_lock(&ks->lock);
1184 eepcr &= ~EEPCR_EECS;
1185 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1186 eepcr = 0;
1187 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1188 mutex_unlock(&ks->lock);
1189
1190 return data;
1191}
1192
1193/**
1194 * ks8851_eeprom_write - write a 16bits word in ks8851 companion EEPROM
1195 * @dev: The network device the PHY is on.
1196 * @op: operand (can be WRITE, EWEN, EWDS)
1197 * @addr: EEPROM address to write
1198 * @data: data to write
1199 *
1200 * eeprom_size: used to define the data coding length. Can be changed
1201 * through debug-fs.
1202 *
1203 * Programs a write on the EEPROM using ks8851 EEPROM SW access feature.
1204 *
1205 * Note that a write enable is required before writing data.
1206 *
1207 * Rough programming model:
1208 * - on period start: set clock high
1209 * - on period / 2: set clock low and program value on bus
1210 * - start on period / 2
1211 */
1212void ks8851_eeprom_write(struct net_device *dev, unsigned int op,
1213 unsigned int addr, unsigned int data)
1214{
1215 struct ks8851_net *ks = netdev_priv(dev);
1216 int eepcr;
1217 int state = EEPROM_CONTROL;
1218 int bit_count = EEPROM_OP_LEN - 1;
1219 unsigned int addr_len;
1220
1221 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1222
1223 switch (op) {
1224 case EEPROM_OP_EWEN:
1225 addr = 0x30;
1226 break;
1227 case EEPROM_OP_EWDS:
1228 addr = 0;
1229 break;
1230 }
1231
1232 /* start transaction: chip select high, authorize write */
1233 mutex_lock(&ks->lock);
1234 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1235 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1236 eepcr |= EEPCR_EECS;
1237 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1238 mutex_unlock(&ks->lock);
1239
1240 while (state != EEPROM_COMPLETE) {
1241 /* falling clock period starts... */
1242 /* set EED_IO pin for control and address */
1243 eepcr &= ~EEPCR_EEDO;
1244 switch (state) {
1245 case EEPROM_CONTROL:
1246 eepcr |= ((op >> bit_count) & 1) << 2;
1247 if (bit_count-- <= 0) {
1248 bit_count = addr_len - 1;
1249 state = EEPROM_ADDRESS;
1250 }
1251 break;
1252 case EEPROM_ADDRESS:
1253 eepcr |= ((addr >> bit_count) & 1) << 2;
1254 if (bit_count-- <= 0) {
1255 if (op == EEPROM_OP_WRITE) {
1256 bit_count = EEPROM_DATA_LEN - 1;
1257 state = EEPROM_DATA;
1258 } else {
1259 state = EEPROM_COMPLETE;
1260 }
1261 }
1262 break;
1263 case EEPROM_DATA:
1264 eepcr |= ((data >> bit_count) & 1) << 2;
1265 if (bit_count-- <= 0)
1266 state = EEPROM_COMPLETE;
1267 break;
1268 }
1269
1270 /* lower clock */
1271 eepcr &= ~EEPCR_EESCK;
1272
1273 mutex_lock(&ks->lock);
1274 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1275 mutex_unlock(&ks->lock);
1276
1277 /* wait period / 2 */
1278 udelay(EEPROM_SK_PERIOD / 2);
1279
1280 /* rising clock period starts... */
1281
1282 /* raise clock */
1283 eepcr |= EEPCR_EESCK;
1284 mutex_lock(&ks->lock);
1285 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1286 mutex_unlock(&ks->lock);
1287
1288 /* wait period / 2 */
1289 udelay(EEPROM_SK_PERIOD / 2);
1290 }
1291
1292 /* close transaction */
1293 mutex_lock(&ks->lock);
1294 eepcr &= ~EEPCR_EECS;
1295 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1296 eepcr = 0;
1297 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1298 mutex_unlock(&ks->lock);
1299
1300}
1301
Ben Dooks3ba81f32009-07-16 05:24:08 +00001302/* ethtool support */
1303
1304static void ks8851_get_drvinfo(struct net_device *dev,
1305 struct ethtool_drvinfo *di)
1306{
1307 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1308 strlcpy(di->version, "1.00", sizeof(di->version));
1309 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1310}
1311
1312static u32 ks8851_get_msglevel(struct net_device *dev)
1313{
1314 struct ks8851_net *ks = netdev_priv(dev);
1315 return ks->msg_enable;
1316}
1317
1318static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1319{
1320 struct ks8851_net *ks = netdev_priv(dev);
1321 ks->msg_enable = to;
1322}
1323
1324static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1325{
1326 struct ks8851_net *ks = netdev_priv(dev);
1327 return mii_ethtool_gset(&ks->mii, cmd);
1328}
1329
1330static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1331{
1332 struct ks8851_net *ks = netdev_priv(dev);
1333 return mii_ethtool_sset(&ks->mii, cmd);
1334}
1335
1336static u32 ks8851_get_link(struct net_device *dev)
1337{
1338 struct ks8851_net *ks = netdev_priv(dev);
1339 return mii_link_ok(&ks->mii);
1340}
1341
1342static int ks8851_nway_reset(struct net_device *dev)
1343{
1344 struct ks8851_net *ks = netdev_priv(dev);
1345 return mii_nway_restart(&ks->mii);
1346}
1347
Sebastien Jana84afa42010-05-05 08:45:54 +00001348static int ks8851_get_eeprom_len(struct net_device *dev)
1349{
1350 struct ks8851_net *ks = netdev_priv(dev);
1351 return ks->eeprom_size;
1352}
1353
1354static int ks8851_get_eeprom(struct net_device *dev,
1355 struct ethtool_eeprom *eeprom, u8 *bytes)
1356{
1357 struct ks8851_net *ks = netdev_priv(dev);
1358 u16 *eeprom_buff;
1359 int first_word;
1360 int last_word;
1361 int ret_val = 0;
1362 u16 i;
1363
1364 if (eeprom->len == 0)
1365 return -EINVAL;
1366
1367 if (eeprom->len > ks->eeprom_size)
1368 return -EINVAL;
1369
1370 eeprom->magic = ks8851_rdreg16(ks, KS_CIDER);
1371
1372 first_word = eeprom->offset >> 1;
1373 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1374
1375 eeprom_buff = kmalloc(sizeof(u16) *
1376 (last_word - first_word + 1), GFP_KERNEL);
1377 if (!eeprom_buff)
1378 return -ENOMEM;
1379
1380 for (i = 0; i < last_word - first_word + 1; i++)
1381 eeprom_buff[i] = ks8851_eeprom_read(dev, first_word + 1);
1382
1383 /* Device's eeprom is little-endian, word addressable */
1384 for (i = 0; i < last_word - first_word + 1; i++)
1385 le16_to_cpus(&eeprom_buff[i]);
1386
1387 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
1388 kfree(eeprom_buff);
1389
1390 return ret_val;
1391}
1392
1393static int ks8851_set_eeprom(struct net_device *dev,
1394 struct ethtool_eeprom *eeprom, u8 *bytes)
1395{
1396 struct ks8851_net *ks = netdev_priv(dev);
1397 u16 *eeprom_buff;
1398 void *ptr;
1399 int max_len;
1400 int first_word;
1401 int last_word;
1402 int ret_val = 0;
1403 u16 i;
1404
1405 if (eeprom->len == 0)
1406 return -EOPNOTSUPP;
1407
1408 if (eeprom->len > ks->eeprom_size)
1409 return -EINVAL;
1410
1411 if (eeprom->magic != ks8851_rdreg16(ks, KS_CIDER))
1412 return -EFAULT;
1413
1414 first_word = eeprom->offset >> 1;
1415 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1416 max_len = (last_word - first_word + 1) * 2;
1417 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
1418 if (!eeprom_buff)
1419 return -ENOMEM;
1420
1421 ptr = (void *)eeprom_buff;
1422
1423 if (eeprom->offset & 1) {
1424 /* need read/modify/write of first changed EEPROM word */
1425 /* only the second byte of the word is being modified */
1426 eeprom_buff[0] = ks8851_eeprom_read(dev, first_word);
1427 ptr++;
1428 }
1429 if ((eeprom->offset + eeprom->len) & 1)
1430 /* need read/modify/write of last changed EEPROM word */
1431 /* only the first byte of the word is being modified */
1432 eeprom_buff[last_word - first_word] =
1433 ks8851_eeprom_read(dev, last_word);
1434
1435
1436 /* Device's eeprom is little-endian, word addressable */
1437 le16_to_cpus(&eeprom_buff[0]);
1438 le16_to_cpus(&eeprom_buff[last_word - first_word]);
1439
1440 memcpy(ptr, bytes, eeprom->len);
1441
1442 for (i = 0; i < last_word - first_word + 1; i++)
1443 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
1444
1445 ks8851_eeprom_write(dev, EEPROM_OP_EWEN, 0, 0);
1446
1447 for (i = 0; i < last_word - first_word + 1; i++) {
1448 ks8851_eeprom_write(dev, EEPROM_OP_WRITE, first_word + i,
1449 eeprom_buff[i]);
1450 mdelay(EEPROM_WRITE_TIME);
1451 }
1452
1453 ks8851_eeprom_write(dev, EEPROM_OP_EWDS, 0, 0);
1454
1455 kfree(eeprom_buff);
1456 return ret_val;
1457}
1458
Ben Dooks3ba81f32009-07-16 05:24:08 +00001459static const struct ethtool_ops ks8851_ethtool_ops = {
1460 .get_drvinfo = ks8851_get_drvinfo,
1461 .get_msglevel = ks8851_get_msglevel,
1462 .set_msglevel = ks8851_set_msglevel,
1463 .get_settings = ks8851_get_settings,
1464 .set_settings = ks8851_set_settings,
1465 .get_link = ks8851_get_link,
1466 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001467 .get_eeprom_len = ks8851_get_eeprom_len,
1468 .get_eeprom = ks8851_get_eeprom,
1469 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001470};
1471
1472/* MII interface controls */
1473
1474/**
1475 * ks8851_phy_reg - convert MII register into a KS8851 register
1476 * @reg: MII register number.
1477 *
1478 * Return the KS8851 register number for the corresponding MII PHY register
1479 * if possible. Return zero if the MII register has no direct mapping to the
1480 * KS8851 register set.
1481 */
1482static int ks8851_phy_reg(int reg)
1483{
1484 switch (reg) {
1485 case MII_BMCR:
1486 return KS_P1MBCR;
1487 case MII_BMSR:
1488 return KS_P1MBSR;
1489 case MII_PHYSID1:
1490 return KS_PHY1ILR;
1491 case MII_PHYSID2:
1492 return KS_PHY1IHR;
1493 case MII_ADVERTISE:
1494 return KS_P1ANAR;
1495 case MII_LPA:
1496 return KS_P1ANLPR;
1497 }
1498
1499 return 0x0;
1500}
1501
1502/**
1503 * ks8851_phy_read - MII interface PHY register read.
1504 * @dev: The network device the PHY is on.
1505 * @phy_addr: Address of PHY (ignored as we only have one)
1506 * @reg: The register to read.
1507 *
1508 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001509 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001510 * are always returned as zero.
1511 *
1512 * We return zero for unsupported registers as the MII code does not check
1513 * the value returned for any error status, and simply returns it to the
1514 * caller. The mii-tool that the driver was tested with takes any -ve error
1515 * as real PHY capabilities, thus displaying incorrect data to the user.
1516 */
1517static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1518{
1519 struct ks8851_net *ks = netdev_priv(dev);
1520 int ksreg;
1521 int result;
1522
1523 ksreg = ks8851_phy_reg(reg);
1524 if (!ksreg)
1525 return 0x0; /* no error return allowed, so use zero */
1526
1527 mutex_lock(&ks->lock);
1528 result = ks8851_rdreg16(ks, ksreg);
1529 mutex_unlock(&ks->lock);
1530
1531 return result;
1532}
1533
1534static void ks8851_phy_write(struct net_device *dev,
1535 int phy, int reg, int value)
1536{
1537 struct ks8851_net *ks = netdev_priv(dev);
1538 int ksreg;
1539
1540 ksreg = ks8851_phy_reg(reg);
1541 if (ksreg) {
1542 mutex_lock(&ks->lock);
1543 ks8851_wrreg16(ks, ksreg, value);
1544 mutex_unlock(&ks->lock);
1545 }
1546}
1547
1548/**
1549 * ks8851_read_selftest - read the selftest memory info.
1550 * @ks: The device state
1551 *
1552 * Read and check the TX/RX memory selftest information.
1553 */
1554static int ks8851_read_selftest(struct ks8851_net *ks)
1555{
1556 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1557 int ret = 0;
1558 unsigned rd;
1559
1560 rd = ks8851_rdreg16(ks, KS_MBIR);
1561
1562 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001563 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001564 return 0;
1565 }
1566
1567 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001568 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001569 ret |= 1;
1570 }
1571
1572 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001573 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001574 ret |= 2;
1575 }
1576
1577 return 0;
1578}
1579
1580/* driver bus management functions */
1581
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001582#ifdef CONFIG_PM
1583static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1584{
1585 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1586 struct net_device *dev = ks->netdev;
1587
1588 if (netif_running(dev)) {
1589 netif_device_detach(dev);
1590 ks8851_net_stop(dev);
1591 }
1592
1593 return 0;
1594}
1595
1596static int ks8851_resume(struct spi_device *spi)
1597{
1598 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1599 struct net_device *dev = ks->netdev;
1600
1601 if (netif_running(dev)) {
1602 ks8851_net_open(dev);
1603 netif_device_attach(dev);
1604 }
1605
1606 return 0;
1607}
1608#else
1609#define ks8851_suspend NULL
1610#define ks8851_resume NULL
1611#endif
1612
Ben Dooks3ba81f32009-07-16 05:24:08 +00001613static int __devinit ks8851_probe(struct spi_device *spi)
1614{
1615 struct net_device *ndev;
1616 struct ks8851_net *ks;
1617 int ret;
1618
1619 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1620 if (!ndev) {
1621 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1622 return -ENOMEM;
1623 }
1624
1625 spi->bits_per_word = 8;
1626
1627 ks = netdev_priv(ndev);
1628
1629 ks->netdev = ndev;
1630 ks->spidev = spi;
1631 ks->tx_space = 6144;
1632
1633 mutex_init(&ks->lock);
1634 spin_lock_init(&ks->statelock);
1635
1636 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1637 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1638 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1639
1640 /* initialise pre-made spi transfer messages */
1641
1642 spi_message_init(&ks->spi_msg1);
1643 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1644
1645 spi_message_init(&ks->spi_msg2);
1646 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1647 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1648
1649 /* setup mii state */
1650 ks->mii.dev = ndev;
1651 ks->mii.phy_id = 1,
1652 ks->mii.phy_id_mask = 1;
1653 ks->mii.reg_num_mask = 0xf;
1654 ks->mii.mdio_read = ks8851_phy_read;
1655 ks->mii.mdio_write = ks8851_phy_write;
1656
1657 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1658
1659 /* set the default message enable */
1660 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1661 NETIF_MSG_PROBE |
1662 NETIF_MSG_LINK));
1663
1664 skb_queue_head_init(&ks->txq);
1665
1666 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1667 SET_NETDEV_DEV(ndev, &spi->dev);
1668
1669 dev_set_drvdata(&spi->dev, ks);
1670
1671 ndev->if_port = IF_PORT_100BASET;
1672 ndev->netdev_ops = &ks8851_netdev_ops;
1673 ndev->irq = spi->irq;
1674
Ben Dooks57dada62009-10-19 23:49:03 +00001675 /* issue a global soft reset to reset the device. */
1676 ks8851_soft_reset(ks, GRR_GSR);
1677
Ben Dooks3ba81f32009-07-16 05:24:08 +00001678 /* simple check for a valid chip being connected to the bus */
1679
1680 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1681 dev_err(&spi->dev, "failed to read device ID\n");
1682 ret = -ENODEV;
1683 goto err_id;
1684 }
1685
Sebastien Jan7d997462010-05-05 08:45:52 +00001686 /* cache the contents of the CCR register for EEPROM, etc. */
1687 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1688
1689 if (ks->rc_ccr & CCR_EEPROM)
1690 ks->eeprom_size = 128;
1691 else
1692 ks->eeprom_size = 0;
1693
Ben Dooks3ba81f32009-07-16 05:24:08 +00001694 ks8851_read_selftest(ks);
1695 ks8851_init_mac(ks);
1696
1697 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1698 ndev->name, ks);
1699 if (ret < 0) {
1700 dev_err(&spi->dev, "failed to get irq\n");
1701 goto err_irq;
1702 }
1703
1704 ret = register_netdev(ndev);
1705 if (ret) {
1706 dev_err(&spi->dev, "failed to register network device\n");
1707 goto err_netdev;
1708 }
1709
Ben Dooksa9a8de22011-11-21 08:57:58 +00001710 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001711 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
Ben Dooksa9a8de22011-11-21 08:57:58 +00001712 ndev->dev_addr, ndev->irq,
1713 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001714
1715 return 0;
1716
1717
1718err_netdev:
1719 free_irq(ndev->irq, ndev);
1720
1721err_id:
1722err_irq:
1723 free_netdev(ndev);
1724 return ret;
1725}
1726
1727static int __devexit ks8851_remove(struct spi_device *spi)
1728{
1729 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
1730
1731 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001732 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001733
1734 unregister_netdev(priv->netdev);
1735 free_irq(spi->irq, priv);
1736 free_netdev(priv->netdev);
1737
1738 return 0;
1739}
1740
1741static struct spi_driver ks8851_driver = {
1742 .driver = {
1743 .name = "ks8851",
1744 .owner = THIS_MODULE,
1745 },
1746 .probe = ks8851_probe,
1747 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001748 .suspend = ks8851_suspend,
1749 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001750};
1751
1752static int __init ks8851_init(void)
1753{
1754 return spi_register_driver(&ks8851_driver);
1755}
1756
1757static void __exit ks8851_exit(void)
1758{
1759 spi_unregister_driver(&ks8851_driver);
1760}
1761
1762module_init(ks8851_init);
1763module_exit(ks8851_exit);
1764
1765MODULE_DESCRIPTION("KS8851 Network driver");
1766MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1767MODULE_LICENSE("GPL");
1768
1769module_param_named(message, msg_enable, int, 0);
1770MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001771MODULE_ALIAS("spi:ks8851");