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