blob: f79dce2ccd7d210f5d23c421c149f095e1d7fb26 [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
375 * into the station address register. Currently we assume that the device
376 * does not have a valid mac address in it, and so we use random_ether_addr()
377 * to create a new one.
378 *
379 * In future, the driver should check to see if the device has an EEPROM
380 * attached and whether that has a valid ethernet address in it.
381 */
382static void ks8851_init_mac(struct ks8851_net *ks)
383{
384 struct net_device *dev = ks->netdev;
385
386 random_ether_addr(dev->dev_addr);
387 ks8851_write_mac_addr(dev);
388}
389
390/**
391 * ks8851_irq - device interrupt handler
392 * @irq: Interrupt number passed from the IRQ hnalder.
393 * @pw: The private word passed to register_irq(), our struct ks8851_net.
394 *
395 * Disable the interrupt from happening again until we've processed the
396 * current status by scheduling ks8851_irq_work().
397 */
398static irqreturn_t ks8851_irq(int irq, void *pw)
399{
400 struct ks8851_net *ks = pw;
401
402 disable_irq_nosync(irq);
403 schedule_work(&ks->irq_work);
404 return IRQ_HANDLED;
405}
406
407/**
408 * ks8851_rdfifo - read data from the receive fifo
409 * @ks: The device state.
410 * @buff: The buffer address
411 * @len: The length of the data to read
412 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100413 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000414 * the FIFO into the buffer specified by @buff.
415 */
416static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
417{
418 struct spi_transfer *xfer = ks->spi_xfer2;
419 struct spi_message *msg = &ks->spi_msg2;
420 u8 txb[1];
421 int ret;
422
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000423 netif_dbg(ks, rx_status, ks->netdev,
424 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000425
426 /* set the operation we're issuing */
427 txb[0] = KS_SPIOP_RXFIFO;
428
429 xfer->tx_buf = txb;
430 xfer->rx_buf = NULL;
431 xfer->len = 1;
432
433 xfer++;
434 xfer->rx_buf = buff;
435 xfer->tx_buf = NULL;
436 xfer->len = len;
437
438 ret = spi_sync(ks->spidev, msg);
439 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000440 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000441}
442
443/**
444 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
445 * @ks: The device state
446 * @rxpkt: The data for the received packet
447 *
448 * Dump the initial data from the packet to dev_dbg().
449*/
450static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
451{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000452 netdev_dbg(ks->netdev,
453 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
454 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
455 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
456 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000457}
458
459/**
460 * ks8851_rx_pkts - receive packets from the host
461 * @ks: The device information.
462 *
463 * This is called from the IRQ work queue when the system detects that there
464 * are packets in the receive queue. Find out how many packets there are and
465 * read them from the FIFO.
466 */
467static void ks8851_rx_pkts(struct ks8851_net *ks)
468{
469 struct sk_buff *skb;
470 unsigned rxfc;
471 unsigned rxlen;
472 unsigned rxstat;
473 u32 rxh;
474 u8 *rxpkt;
475
476 rxfc = ks8851_rdreg8(ks, KS_RXFC);
477
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000478 netif_dbg(ks, rx_status, ks->netdev,
479 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000480
481 /* Currently we're issuing a read per packet, but we could possibly
482 * improve the code by issuing a single read, getting the receive
483 * header, allocating the packet and then reading the packet data
484 * out in one go.
485 *
486 * This form of operation would require us to hold the SPI bus'
487 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300488 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000489 */
490
491 for (; rxfc != 0; rxfc--) {
492 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
493 rxstat = rxh & 0xffff;
494 rxlen = rxh >> 16;
495
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000496 netif_dbg(ks, rx_status, ks->netdev,
497 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000498
499 /* the length of the packet includes the 32bit CRC */
500
501 /* set dma read address */
502 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
503
504 /* start the packet dma process, and set auto-dequeue rx */
505 ks8851_wrreg16(ks, KS_RXQCR,
506 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
507
Eric Dumazet972c40b2010-09-08 13:26:55 +0000508 if (rxlen > 4) {
509 unsigned int rxalign;
510
511 rxlen -= 4;
512 rxalign = ALIGN(rxlen, 4);
513 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
514 if (skb) {
515
516 /* 4 bytes of status header + 4 bytes of
517 * garbage: we put them before ethernet
518 * header, so that they are copied,
519 * but ignored.
520 */
521
522 rxpkt = skb_put(skb, rxlen) - 8;
523
524 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
525
526 if (netif_msg_pktdata(ks))
527 ks8851_dbg_dumpkkt(ks, rxpkt);
528
529 skb->protocol = eth_type_trans(skb, ks->netdev);
530 netif_rx(skb);
531
532 ks->netdev->stats.rx_packets++;
533 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000534 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000535 }
536
537 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
538 }
539}
540
541/**
542 * ks8851_irq_work - work queue handler for dealing with interrupt requests
543 * @work: The work structure that was scheduled by schedule_work()
544 *
545 * This is the handler invoked when the ks8851_irq() is called to find out
546 * what happened, as we cannot allow ourselves to sleep whilst waiting for
547 * anything other process has the chip's lock.
548 *
549 * Read the interrupt status, work out what needs to be done and then clear
550 * any of the interrupts that are not needed.
551 */
552static void ks8851_irq_work(struct work_struct *work)
553{
554 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
555 unsigned status;
556 unsigned handled = 0;
557
558 mutex_lock(&ks->lock);
559
560 status = ks8851_rdreg16(ks, KS_ISR);
561
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000562 netif_dbg(ks, intr, ks->netdev,
563 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000564
565 if (status & IRQ_LCI) {
566 /* should do something about checking link status */
567 handled |= IRQ_LCI;
568 }
569
570 if (status & IRQ_LDI) {
571 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
572 pmecr &= ~PMECR_WKEVT_MASK;
573 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
574
575 handled |= IRQ_LDI;
576 }
577
578 if (status & IRQ_RXPSI)
579 handled |= IRQ_RXPSI;
580
581 if (status & IRQ_TXI) {
582 handled |= IRQ_TXI;
583
584 /* no lock here, tx queue should have been stopped */
585
586 /* update our idea of how much tx space is available to the
587 * system */
588 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
589
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000590 netif_dbg(ks, intr, ks->netdev,
591 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000592 }
593
594 if (status & IRQ_RXI)
595 handled |= IRQ_RXI;
596
597 if (status & IRQ_SPIBEI) {
598 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
599 handled |= IRQ_SPIBEI;
600 }
601
602 ks8851_wrreg16(ks, KS_ISR, handled);
603
604 if (status & IRQ_RXI) {
605 /* the datasheet says to disable the rx interrupt during
606 * packet read-out, however we're masking the interrupt
607 * from the device so do not bother masking just the RX
608 * from the device. */
609
610 ks8851_rx_pkts(ks);
611 }
612
613 /* if something stopped the rx process, probably due to wanting
614 * to change the rx settings, then do something about restarting
615 * it. */
616 if (status & IRQ_RXPSI) {
617 struct ks8851_rxctrl *rxc = &ks->rxctrl;
618
619 /* update the multicast hash table */
620 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
621 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
622 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
623 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
624
625 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
626 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
627 }
628
629 mutex_unlock(&ks->lock);
630
631 if (status & IRQ_TXI)
632 netif_wake_queue(ks->netdev);
633
634 enable_irq(ks->netdev->irq);
635}
636
637/**
638 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300639 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000640 *
641 * Returns the size of the TXFIFO message needed to send
642 * this packet.
643 */
644static inline unsigned calc_txlen(unsigned len)
645{
646 return ALIGN(len + 4, 4);
647}
648
649/**
650 * ks8851_wrpkt - write packet to TX FIFO
651 * @ks: The device state.
652 * @txp: The sk_buff to transmit.
653 * @irq: IRQ on completion of the packet.
654 *
655 * Send the @txp to the chip. This means creating the relevant packet header
656 * specifying the length of the packet and the other information the chip
657 * needs, such as IRQ on completion. Send the header and the packet data to
658 * the device.
659 */
660static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
661{
662 struct spi_transfer *xfer = ks->spi_xfer2;
663 struct spi_message *msg = &ks->spi_msg2;
664 unsigned fid = 0;
665 int ret;
666
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000667 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
668 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000669
670 fid = ks->fid++;
671 fid &= TXFR_TXFID_MASK;
672
673 if (irq)
674 fid |= TXFR_TXIC; /* irq on completion */
675
676 /* start header at txb[1] to align txw entries */
677 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
678 ks->txh.txw[1] = cpu_to_le16(fid);
679 ks->txh.txw[2] = cpu_to_le16(txp->len);
680
681 xfer->tx_buf = &ks->txh.txb[1];
682 xfer->rx_buf = NULL;
683 xfer->len = 5;
684
685 xfer++;
686 xfer->tx_buf = txp->data;
687 xfer->rx_buf = NULL;
688 xfer->len = ALIGN(txp->len, 4);
689
690 ret = spi_sync(ks->spidev, msg);
691 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000692 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000693}
694
695/**
696 * ks8851_done_tx - update and then free skbuff after transmitting
697 * @ks: The device state
698 * @txb: The buffer transmitted
699 */
700static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
701{
702 struct net_device *dev = ks->netdev;
703
704 dev->stats.tx_bytes += txb->len;
705 dev->stats.tx_packets++;
706
707 dev_kfree_skb(txb);
708}
709
710/**
711 * ks8851_tx_work - process tx packet(s)
712 * @work: The work strucutre what was scheduled.
713 *
714 * This is called when a number of packets have been scheduled for
715 * transmission and need to be sent to the device.
716 */
717static void ks8851_tx_work(struct work_struct *work)
718{
719 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
720 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000721 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000722
723 mutex_lock(&ks->lock);
724
725 while (!last) {
726 txb = skb_dequeue(&ks->txq);
727 last = skb_queue_empty(&ks->txq);
728
Abraham Arce761172f2010-04-16 14:48:43 +0000729 if (txb != NULL) {
730 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
731 ks8851_wrpkt(ks, txb, last);
732 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
733 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000734
Abraham Arce761172f2010-04-16 14:48:43 +0000735 ks8851_done_tx(ks, txb);
736 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000737 }
738
739 mutex_unlock(&ks->lock);
740}
741
742/**
743 * ks8851_set_powermode - set power mode of the device
744 * @ks: The device state
745 * @pwrmode: The power mode value to write to KS_PMECR.
746 *
747 * Change the power mode of the chip.
748 */
749static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
750{
751 unsigned pmecr;
752
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000753 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000754
755 pmecr = ks8851_rdreg16(ks, KS_PMECR);
756 pmecr &= ~PMECR_PM_MASK;
757 pmecr |= pwrmode;
758
759 ks8851_wrreg16(ks, KS_PMECR, pmecr);
760}
761
762/**
763 * ks8851_net_open - open network device
764 * @dev: The network device being opened.
765 *
766 * Called when the network device is marked active, such as a user executing
767 * 'ifconfig up' on the device.
768 */
769static int ks8851_net_open(struct net_device *dev)
770{
771 struct ks8851_net *ks = netdev_priv(dev);
772
773 /* lock the card, even if we may not actually be doing anything
774 * else at the moment */
775 mutex_lock(&ks->lock);
776
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000777 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000778
779 /* bring chip out of any power saving mode it was in */
780 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
781
782 /* issue a soft reset to the RX/TX QMU to put it into a known
783 * state. */
784 ks8851_soft_reset(ks, GRR_QMU);
785
786 /* setup transmission parameters */
787
788 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
789 TXCR_TXPE | /* pad to min length */
790 TXCR_TXCRC | /* add CRC */
791 TXCR_TXFCE)); /* enable flow control */
792
793 /* auto-increment tx data, reset tx pointer */
794 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
795
796 /* setup receiver control */
797
798 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
799 RXCR1_RXFCE | /* enable flow control */
800 RXCR1_RXBE | /* broadcast enable */
801 RXCR1_RXUE | /* unicast enable */
802 RXCR1_RXE)); /* enable rx block */
803
804 /* transfer entire frames out in one go */
805 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
806
807 /* set receive counter timeouts */
808 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
809 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
810 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
811
812 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
813 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
814 RXQCR_RXDTTE); /* IRQ on time exceeded */
815
816 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
817
818 /* clear then enable interrupts */
819
820#define STD_IRQ (IRQ_LCI | /* Link Change */ \
821 IRQ_TXI | /* TX done */ \
822 IRQ_RXI | /* RX done */ \
823 IRQ_SPIBEI | /* SPI bus error */ \
824 IRQ_TXPSI | /* TX process stop */ \
825 IRQ_RXPSI) /* RX process stop */
826
827 ks->rc_ier = STD_IRQ;
828 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
829 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
830
831 netif_start_queue(ks->netdev);
832
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000833 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000834
835 mutex_unlock(&ks->lock);
836 return 0;
837}
838
839/**
840 * ks8851_net_stop - close network device
841 * @dev: The device being closed.
842 *
843 * Called to close down a network device which has been active. Cancell any
844 * work, shutdown the RX and TX process and then place the chip into a low
845 * power state whilst it is not being used.
846 */
847static int ks8851_net_stop(struct net_device *dev)
848{
849 struct ks8851_net *ks = netdev_priv(dev);
850
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000851 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000852
853 netif_stop_queue(dev);
854
855 mutex_lock(&ks->lock);
856
857 /* stop any outstanding work */
858 flush_work(&ks->irq_work);
859 flush_work(&ks->tx_work);
860 flush_work(&ks->rxctrl_work);
861
862 /* turn off the IRQs and ack any outstanding */
863 ks8851_wrreg16(ks, KS_IER, 0x0000);
864 ks8851_wrreg16(ks, KS_ISR, 0xffff);
865
866 /* shutdown RX process */
867 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
868
869 /* shutdown TX process */
870 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
871
872 /* set powermode to soft power down to save power */
873 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
874
875 /* ensure any queued tx buffers are dumped */
876 while (!skb_queue_empty(&ks->txq)) {
877 struct sk_buff *txb = skb_dequeue(&ks->txq);
878
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000879 netif_dbg(ks, ifdown, ks->netdev,
880 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000881
882 dev_kfree_skb(txb);
883 }
884
885 mutex_unlock(&ks->lock);
886 return 0;
887}
888
889/**
890 * ks8851_start_xmit - transmit packet
891 * @skb: The buffer to transmit
892 * @dev: The device used to transmit the packet.
893 *
894 * Called by the network layer to transmit the @skb. Queue the packet for
895 * the device and schedule the necessary work to transmit the packet when
896 * it is free.
897 *
898 * We do this to firstly avoid sleeping with the network device locked,
899 * and secondly so we can round up more than one packet to transmit which
900 * means we can try and avoid generating too many transmit done interrupts.
901 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000902static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
903 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000904{
905 struct ks8851_net *ks = netdev_priv(dev);
906 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000907 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000908
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000909 netif_dbg(ks, tx_queued, ks->netdev,
910 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000911
912 spin_lock(&ks->statelock);
913
914 if (needed > ks->tx_space) {
915 netif_stop_queue(dev);
916 ret = NETDEV_TX_BUSY;
917 } else {
918 ks->tx_space -= needed;
919 skb_queue_tail(&ks->txq, skb);
920 }
921
922 spin_unlock(&ks->statelock);
923 schedule_work(&ks->tx_work);
924
925 return ret;
926}
927
928/**
929 * ks8851_rxctrl_work - work handler to change rx mode
930 * @work: The work structure this belongs to.
931 *
932 * Lock the device and issue the necessary changes to the receive mode from
933 * the network device layer. This is done so that we can do this without
934 * having to sleep whilst holding the network device lock.
935 *
936 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
937 * receive parameters are programmed, we issue a write to disable the RXQ and
938 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
939 * complete. The interrupt handler then writes the new values into the chip.
940 */
941static void ks8851_rxctrl_work(struct work_struct *work)
942{
943 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
944
945 mutex_lock(&ks->lock);
946
947 /* need to shutdown RXQ before modifying filter parameters */
948 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
949
950 mutex_unlock(&ks->lock);
951}
952
953static void ks8851_set_rx_mode(struct net_device *dev)
954{
955 struct ks8851_net *ks = netdev_priv(dev);
956 struct ks8851_rxctrl rxctrl;
957
958 memset(&rxctrl, 0, sizeof(rxctrl));
959
960 if (dev->flags & IFF_PROMISC) {
961 /* interface to receive everything */
962
963 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
964 } else if (dev->flags & IFF_ALLMULTI) {
965 /* accept all multicast packets */
966
967 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
968 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000969 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000970 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000971 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000972
973 /* accept some multicast */
974
Jiri Pirko22bedad2010-04-01 21:22:57 +0000975 netdev_for_each_mc_addr(ha, dev) {
976 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000977 crc >>= (32 - 6); /* get top six bits */
978
979 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +0000980 }
981
Ben Dooksb6a71bf2009-10-19 23:49:05 +0000982 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000983 } else {
984 /* just accept broadcast / unicast */
985 rxctrl.rxcr1 = RXCR1_RXPAFMA;
986 }
987
988 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
989 RXCR1_RXBE | /* broadcast enable */
990 RXCR1_RXE | /* RX process enable */
991 RXCR1_RXFCE); /* enable flow control */
992
993 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
994
995 /* schedule work to do the actual set of the data if needed */
996
997 spin_lock(&ks->statelock);
998
999 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1000 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1001 schedule_work(&ks->rxctrl_work);
1002 }
1003
1004 spin_unlock(&ks->statelock);
1005}
1006
1007static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1008{
1009 struct sockaddr *sa = addr;
1010
1011 if (netif_running(dev))
1012 return -EBUSY;
1013
1014 if (!is_valid_ether_addr(sa->sa_data))
1015 return -EADDRNOTAVAIL;
1016
1017 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1018 return ks8851_write_mac_addr(dev);
1019}
1020
1021static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1022{
1023 struct ks8851_net *ks = netdev_priv(dev);
1024
1025 if (!netif_running(dev))
1026 return -EINVAL;
1027
1028 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1029}
1030
1031static const struct net_device_ops ks8851_netdev_ops = {
1032 .ndo_open = ks8851_net_open,
1033 .ndo_stop = ks8851_net_stop,
1034 .ndo_do_ioctl = ks8851_net_ioctl,
1035 .ndo_start_xmit = ks8851_start_xmit,
1036 .ndo_set_mac_address = ks8851_set_mac_address,
1037 .ndo_set_rx_mode = ks8851_set_rx_mode,
1038 .ndo_change_mtu = eth_change_mtu,
1039 .ndo_validate_addr = eth_validate_addr,
1040};
1041
Sebastien Jana4bdfff2010-05-05 08:45:53 +00001042/* Companion eeprom access */
1043
1044enum { /* EEPROM programming states */
1045 EEPROM_CONTROL,
1046 EEPROM_ADDRESS,
1047 EEPROM_DATA,
1048 EEPROM_COMPLETE
1049};
1050
1051/**
1052 * ks8851_eeprom_read - read a 16bits word in ks8851 companion EEPROM
1053 * @dev: The network device the PHY is on.
1054 * @addr: EEPROM address to read
1055 *
1056 * eeprom_size: used to define the data coding length. Can be changed
1057 * through debug-fs.
1058 *
1059 * Programs a read on the EEPROM using ks8851 EEPROM SW access feature.
1060 * Warning: The READ feature is not supported on ks8851 revision 0.
1061 *
1062 * Rough programming model:
1063 * - on period start: set clock high and read value on bus
1064 * - on period / 2: set clock low and program value on bus
1065 * - start on period / 2
1066 */
1067unsigned int ks8851_eeprom_read(struct net_device *dev, unsigned int addr)
1068{
1069 struct ks8851_net *ks = netdev_priv(dev);
1070 int eepcr;
1071 int ctrl = EEPROM_OP_READ;
1072 int state = EEPROM_CONTROL;
1073 int bit_count = EEPROM_OP_LEN - 1;
1074 unsigned int data = 0;
1075 int dummy;
1076 unsigned int addr_len;
1077
1078 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1079
1080 /* start transaction: chip select high, authorize write */
1081 mutex_lock(&ks->lock);
1082 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1083 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1084 eepcr |= EEPCR_EECS;
1085 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1086 mutex_unlock(&ks->lock);
1087
1088 while (state != EEPROM_COMPLETE) {
1089 /* falling clock period starts... */
1090 /* set EED_IO pin for control and address */
1091 eepcr &= ~EEPCR_EEDO;
1092 switch (state) {
1093 case EEPROM_CONTROL:
1094 eepcr |= ((ctrl >> bit_count) & 1) << 2;
1095 if (bit_count-- <= 0) {
1096 bit_count = addr_len - 1;
1097 state = EEPROM_ADDRESS;
1098 }
1099 break;
1100 case EEPROM_ADDRESS:
1101 eepcr |= ((addr >> bit_count) & 1) << 2;
1102 bit_count--;
1103 break;
1104 case EEPROM_DATA:
1105 /* Change to receive mode */
1106 eepcr &= ~EEPCR_EESRWA;
1107 break;
1108 }
1109
1110 /* lower clock */
1111 eepcr &= ~EEPCR_EESCK;
1112
1113 mutex_lock(&ks->lock);
1114 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1115 mutex_unlock(&ks->lock);
1116
1117 /* waitread period / 2 */
1118 udelay(EEPROM_SK_PERIOD / 2);
1119
1120 /* rising clock period starts... */
1121
1122 /* raise clock */
1123 mutex_lock(&ks->lock);
1124 eepcr |= EEPCR_EESCK;
1125 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1126 mutex_unlock(&ks->lock);
1127
1128 /* Manage read */
1129 switch (state) {
1130 case EEPROM_ADDRESS:
1131 if (bit_count < 0) {
1132 bit_count = EEPROM_DATA_LEN - 1;
1133 state = EEPROM_DATA;
1134 }
1135 break;
1136 case EEPROM_DATA:
1137 mutex_lock(&ks->lock);
1138 dummy = ks8851_rdreg16(ks, KS_EEPCR);
1139 mutex_unlock(&ks->lock);
1140 data |= ((dummy >> EEPCR_EESB_OFFSET) & 1) << bit_count;
1141 if (bit_count-- <= 0)
1142 state = EEPROM_COMPLETE;
1143 break;
1144 }
1145
1146 /* wait period / 2 */
1147 udelay(EEPROM_SK_PERIOD / 2);
1148 }
1149
1150 /* close transaction */
1151 mutex_lock(&ks->lock);
1152 eepcr &= ~EEPCR_EECS;
1153 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1154 eepcr = 0;
1155 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1156 mutex_unlock(&ks->lock);
1157
1158 return data;
1159}
1160
1161/**
1162 * ks8851_eeprom_write - write a 16bits word in ks8851 companion EEPROM
1163 * @dev: The network device the PHY is on.
1164 * @op: operand (can be WRITE, EWEN, EWDS)
1165 * @addr: EEPROM address to write
1166 * @data: data to write
1167 *
1168 * eeprom_size: used to define the data coding length. Can be changed
1169 * through debug-fs.
1170 *
1171 * Programs a write on the EEPROM using ks8851 EEPROM SW access feature.
1172 *
1173 * Note that a write enable is required before writing data.
1174 *
1175 * Rough programming model:
1176 * - on period start: set clock high
1177 * - on period / 2: set clock low and program value on bus
1178 * - start on period / 2
1179 */
1180void ks8851_eeprom_write(struct net_device *dev, unsigned int op,
1181 unsigned int addr, unsigned int data)
1182{
1183 struct ks8851_net *ks = netdev_priv(dev);
1184 int eepcr;
1185 int state = EEPROM_CONTROL;
1186 int bit_count = EEPROM_OP_LEN - 1;
1187 unsigned int addr_len;
1188
1189 addr_len = (ks->eeprom_size == 128) ? 6 : 8;
1190
1191 switch (op) {
1192 case EEPROM_OP_EWEN:
1193 addr = 0x30;
1194 break;
1195 case EEPROM_OP_EWDS:
1196 addr = 0;
1197 break;
1198 }
1199
1200 /* start transaction: chip select high, authorize write */
1201 mutex_lock(&ks->lock);
1202 eepcr = EEPCR_EESA | EEPCR_EESRWA;
1203 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1204 eepcr |= EEPCR_EECS;
1205 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1206 mutex_unlock(&ks->lock);
1207
1208 while (state != EEPROM_COMPLETE) {
1209 /* falling clock period starts... */
1210 /* set EED_IO pin for control and address */
1211 eepcr &= ~EEPCR_EEDO;
1212 switch (state) {
1213 case EEPROM_CONTROL:
1214 eepcr |= ((op >> bit_count) & 1) << 2;
1215 if (bit_count-- <= 0) {
1216 bit_count = addr_len - 1;
1217 state = EEPROM_ADDRESS;
1218 }
1219 break;
1220 case EEPROM_ADDRESS:
1221 eepcr |= ((addr >> bit_count) & 1) << 2;
1222 if (bit_count-- <= 0) {
1223 if (op == EEPROM_OP_WRITE) {
1224 bit_count = EEPROM_DATA_LEN - 1;
1225 state = EEPROM_DATA;
1226 } else {
1227 state = EEPROM_COMPLETE;
1228 }
1229 }
1230 break;
1231 case EEPROM_DATA:
1232 eepcr |= ((data >> bit_count) & 1) << 2;
1233 if (bit_count-- <= 0)
1234 state = EEPROM_COMPLETE;
1235 break;
1236 }
1237
1238 /* lower clock */
1239 eepcr &= ~EEPCR_EESCK;
1240
1241 mutex_lock(&ks->lock);
1242 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1243 mutex_unlock(&ks->lock);
1244
1245 /* wait period / 2 */
1246 udelay(EEPROM_SK_PERIOD / 2);
1247
1248 /* rising clock period starts... */
1249
1250 /* raise clock */
1251 eepcr |= EEPCR_EESCK;
1252 mutex_lock(&ks->lock);
1253 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1254 mutex_unlock(&ks->lock);
1255
1256 /* wait period / 2 */
1257 udelay(EEPROM_SK_PERIOD / 2);
1258 }
1259
1260 /* close transaction */
1261 mutex_lock(&ks->lock);
1262 eepcr &= ~EEPCR_EECS;
1263 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1264 eepcr = 0;
1265 ks8851_wrreg16(ks, KS_EEPCR, eepcr);
1266 mutex_unlock(&ks->lock);
1267
1268}
1269
Ben Dooks3ba81f32009-07-16 05:24:08 +00001270/* ethtool support */
1271
1272static void ks8851_get_drvinfo(struct net_device *dev,
1273 struct ethtool_drvinfo *di)
1274{
1275 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1276 strlcpy(di->version, "1.00", sizeof(di->version));
1277 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1278}
1279
1280static u32 ks8851_get_msglevel(struct net_device *dev)
1281{
1282 struct ks8851_net *ks = netdev_priv(dev);
1283 return ks->msg_enable;
1284}
1285
1286static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1287{
1288 struct ks8851_net *ks = netdev_priv(dev);
1289 ks->msg_enable = to;
1290}
1291
1292static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1293{
1294 struct ks8851_net *ks = netdev_priv(dev);
1295 return mii_ethtool_gset(&ks->mii, cmd);
1296}
1297
1298static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1299{
1300 struct ks8851_net *ks = netdev_priv(dev);
1301 return mii_ethtool_sset(&ks->mii, cmd);
1302}
1303
1304static u32 ks8851_get_link(struct net_device *dev)
1305{
1306 struct ks8851_net *ks = netdev_priv(dev);
1307 return mii_link_ok(&ks->mii);
1308}
1309
1310static int ks8851_nway_reset(struct net_device *dev)
1311{
1312 struct ks8851_net *ks = netdev_priv(dev);
1313 return mii_nway_restart(&ks->mii);
1314}
1315
Sebastien Jana84afa42010-05-05 08:45:54 +00001316static int ks8851_get_eeprom_len(struct net_device *dev)
1317{
1318 struct ks8851_net *ks = netdev_priv(dev);
1319 return ks->eeprom_size;
1320}
1321
1322static int ks8851_get_eeprom(struct net_device *dev,
1323 struct ethtool_eeprom *eeprom, u8 *bytes)
1324{
1325 struct ks8851_net *ks = netdev_priv(dev);
1326 u16 *eeprom_buff;
1327 int first_word;
1328 int last_word;
1329 int ret_val = 0;
1330 u16 i;
1331
1332 if (eeprom->len == 0)
1333 return -EINVAL;
1334
1335 if (eeprom->len > ks->eeprom_size)
1336 return -EINVAL;
1337
1338 eeprom->magic = ks8851_rdreg16(ks, KS_CIDER);
1339
1340 first_word = eeprom->offset >> 1;
1341 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1342
1343 eeprom_buff = kmalloc(sizeof(u16) *
1344 (last_word - first_word + 1), GFP_KERNEL);
1345 if (!eeprom_buff)
1346 return -ENOMEM;
1347
1348 for (i = 0; i < last_word - first_word + 1; i++)
1349 eeprom_buff[i] = ks8851_eeprom_read(dev, first_word + 1);
1350
1351 /* Device's eeprom is little-endian, word addressable */
1352 for (i = 0; i < last_word - first_word + 1; i++)
1353 le16_to_cpus(&eeprom_buff[i]);
1354
1355 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
1356 kfree(eeprom_buff);
1357
1358 return ret_val;
1359}
1360
1361static int ks8851_set_eeprom(struct net_device *dev,
1362 struct ethtool_eeprom *eeprom, u8 *bytes)
1363{
1364 struct ks8851_net *ks = netdev_priv(dev);
1365 u16 *eeprom_buff;
1366 void *ptr;
1367 int max_len;
1368 int first_word;
1369 int last_word;
1370 int ret_val = 0;
1371 u16 i;
1372
1373 if (eeprom->len == 0)
1374 return -EOPNOTSUPP;
1375
1376 if (eeprom->len > ks->eeprom_size)
1377 return -EINVAL;
1378
1379 if (eeprom->magic != ks8851_rdreg16(ks, KS_CIDER))
1380 return -EFAULT;
1381
1382 first_word = eeprom->offset >> 1;
1383 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
1384 max_len = (last_word - first_word + 1) * 2;
1385 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
1386 if (!eeprom_buff)
1387 return -ENOMEM;
1388
1389 ptr = (void *)eeprom_buff;
1390
1391 if (eeprom->offset & 1) {
1392 /* need read/modify/write of first changed EEPROM word */
1393 /* only the second byte of the word is being modified */
1394 eeprom_buff[0] = ks8851_eeprom_read(dev, first_word);
1395 ptr++;
1396 }
1397 if ((eeprom->offset + eeprom->len) & 1)
1398 /* need read/modify/write of last changed EEPROM word */
1399 /* only the first byte of the word is being modified */
1400 eeprom_buff[last_word - first_word] =
1401 ks8851_eeprom_read(dev, last_word);
1402
1403
1404 /* Device's eeprom is little-endian, word addressable */
1405 le16_to_cpus(&eeprom_buff[0]);
1406 le16_to_cpus(&eeprom_buff[last_word - first_word]);
1407
1408 memcpy(ptr, bytes, eeprom->len);
1409
1410 for (i = 0; i < last_word - first_word + 1; i++)
1411 eeprom_buff[i] = cpu_to_le16(eeprom_buff[i]);
1412
1413 ks8851_eeprom_write(dev, EEPROM_OP_EWEN, 0, 0);
1414
1415 for (i = 0; i < last_word - first_word + 1; i++) {
1416 ks8851_eeprom_write(dev, EEPROM_OP_WRITE, first_word + i,
1417 eeprom_buff[i]);
1418 mdelay(EEPROM_WRITE_TIME);
1419 }
1420
1421 ks8851_eeprom_write(dev, EEPROM_OP_EWDS, 0, 0);
1422
1423 kfree(eeprom_buff);
1424 return ret_val;
1425}
1426
Ben Dooks3ba81f32009-07-16 05:24:08 +00001427static const struct ethtool_ops ks8851_ethtool_ops = {
1428 .get_drvinfo = ks8851_get_drvinfo,
1429 .get_msglevel = ks8851_get_msglevel,
1430 .set_msglevel = ks8851_set_msglevel,
1431 .get_settings = ks8851_get_settings,
1432 .set_settings = ks8851_set_settings,
1433 .get_link = ks8851_get_link,
1434 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001435 .get_eeprom_len = ks8851_get_eeprom_len,
1436 .get_eeprom = ks8851_get_eeprom,
1437 .set_eeprom = ks8851_set_eeprom,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001438};
1439
1440/* MII interface controls */
1441
1442/**
1443 * ks8851_phy_reg - convert MII register into a KS8851 register
1444 * @reg: MII register number.
1445 *
1446 * Return the KS8851 register number for the corresponding MII PHY register
1447 * if possible. Return zero if the MII register has no direct mapping to the
1448 * KS8851 register set.
1449 */
1450static int ks8851_phy_reg(int reg)
1451{
1452 switch (reg) {
1453 case MII_BMCR:
1454 return KS_P1MBCR;
1455 case MII_BMSR:
1456 return KS_P1MBSR;
1457 case MII_PHYSID1:
1458 return KS_PHY1ILR;
1459 case MII_PHYSID2:
1460 return KS_PHY1IHR;
1461 case MII_ADVERTISE:
1462 return KS_P1ANAR;
1463 case MII_LPA:
1464 return KS_P1ANLPR;
1465 }
1466
1467 return 0x0;
1468}
1469
1470/**
1471 * ks8851_phy_read - MII interface PHY register read.
1472 * @dev: The network device the PHY is on.
1473 * @phy_addr: Address of PHY (ignored as we only have one)
1474 * @reg: The register to read.
1475 *
1476 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001477 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001478 * are always returned as zero.
1479 *
1480 * We return zero for unsupported registers as the MII code does not check
1481 * the value returned for any error status, and simply returns it to the
1482 * caller. The mii-tool that the driver was tested with takes any -ve error
1483 * as real PHY capabilities, thus displaying incorrect data to the user.
1484 */
1485static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1486{
1487 struct ks8851_net *ks = netdev_priv(dev);
1488 int ksreg;
1489 int result;
1490
1491 ksreg = ks8851_phy_reg(reg);
1492 if (!ksreg)
1493 return 0x0; /* no error return allowed, so use zero */
1494
1495 mutex_lock(&ks->lock);
1496 result = ks8851_rdreg16(ks, ksreg);
1497 mutex_unlock(&ks->lock);
1498
1499 return result;
1500}
1501
1502static void ks8851_phy_write(struct net_device *dev,
1503 int phy, int reg, int value)
1504{
1505 struct ks8851_net *ks = netdev_priv(dev);
1506 int ksreg;
1507
1508 ksreg = ks8851_phy_reg(reg);
1509 if (ksreg) {
1510 mutex_lock(&ks->lock);
1511 ks8851_wrreg16(ks, ksreg, value);
1512 mutex_unlock(&ks->lock);
1513 }
1514}
1515
1516/**
1517 * ks8851_read_selftest - read the selftest memory info.
1518 * @ks: The device state
1519 *
1520 * Read and check the TX/RX memory selftest information.
1521 */
1522static int ks8851_read_selftest(struct ks8851_net *ks)
1523{
1524 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1525 int ret = 0;
1526 unsigned rd;
1527
1528 rd = ks8851_rdreg16(ks, KS_MBIR);
1529
1530 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001531 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001532 return 0;
1533 }
1534
1535 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001536 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001537 ret |= 1;
1538 }
1539
1540 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001541 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001542 ret |= 2;
1543 }
1544
1545 return 0;
1546}
1547
1548/* driver bus management functions */
1549
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001550#ifdef CONFIG_PM
1551static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
1552{
1553 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1554 struct net_device *dev = ks->netdev;
1555
1556 if (netif_running(dev)) {
1557 netif_device_detach(dev);
1558 ks8851_net_stop(dev);
1559 }
1560
1561 return 0;
1562}
1563
1564static int ks8851_resume(struct spi_device *spi)
1565{
1566 struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
1567 struct net_device *dev = ks->netdev;
1568
1569 if (netif_running(dev)) {
1570 ks8851_net_open(dev);
1571 netif_device_attach(dev);
1572 }
1573
1574 return 0;
1575}
1576#else
1577#define ks8851_suspend NULL
1578#define ks8851_resume NULL
1579#endif
1580
Ben Dooks3ba81f32009-07-16 05:24:08 +00001581static int __devinit ks8851_probe(struct spi_device *spi)
1582{
1583 struct net_device *ndev;
1584 struct ks8851_net *ks;
1585 int ret;
1586
1587 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1588 if (!ndev) {
1589 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1590 return -ENOMEM;
1591 }
1592
1593 spi->bits_per_word = 8;
1594
1595 ks = netdev_priv(ndev);
1596
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001597 ks->vdd_io = regulator_get(&spi->dev, "vdd_io");
1598 ks->vdd_phy = regulator_get(&spi->dev, "vdd_phy");
1599
1600 if (!IS_ERR(ks->vdd_io))
1601 regulator_enable(ks->vdd_io);
1602
1603 if (!IS_ERR(ks->vdd_phy))
1604 regulator_enable(ks->vdd_phy);
1605
Ben Dooks3ba81f32009-07-16 05:24:08 +00001606 ks->netdev = ndev;
1607 ks->spidev = spi;
1608 ks->tx_space = 6144;
1609
1610 mutex_init(&ks->lock);
1611 spin_lock_init(&ks->statelock);
1612
1613 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1614 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1615 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1616
1617 /* initialise pre-made spi transfer messages */
1618
1619 spi_message_init(&ks->spi_msg1);
1620 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1621
1622 spi_message_init(&ks->spi_msg2);
1623 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1624 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1625
1626 /* setup mii state */
1627 ks->mii.dev = ndev;
1628 ks->mii.phy_id = 1,
1629 ks->mii.phy_id_mask = 1;
1630 ks->mii.reg_num_mask = 0xf;
1631 ks->mii.mdio_read = ks8851_phy_read;
1632 ks->mii.mdio_write = ks8851_phy_write;
1633
1634 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1635
1636 /* set the default message enable */
1637 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1638 NETIF_MSG_PROBE |
1639 NETIF_MSG_LINK));
1640
1641 skb_queue_head_init(&ks->txq);
1642
1643 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1644 SET_NETDEV_DEV(ndev, &spi->dev);
1645
1646 dev_set_drvdata(&spi->dev, ks);
1647
1648 ndev->if_port = IF_PORT_100BASET;
1649 ndev->netdev_ops = &ks8851_netdev_ops;
1650 ndev->irq = spi->irq;
1651
Ben Dooks57dada62009-10-19 23:49:03 +00001652 /* issue a global soft reset to reset the device. */
1653 ks8851_soft_reset(ks, GRR_GSR);
1654
Ben Dooks3ba81f32009-07-16 05:24:08 +00001655 /* simple check for a valid chip being connected to the bus */
1656
1657 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1658 dev_err(&spi->dev, "failed to read device ID\n");
1659 ret = -ENODEV;
1660 goto err_id;
1661 }
1662
Sebastien Jan7d997462010-05-05 08:45:52 +00001663 /* cache the contents of the CCR register for EEPROM, etc. */
1664 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1665
1666 if (ks->rc_ccr & CCR_EEPROM)
1667 ks->eeprom_size = 128;
1668 else
1669 ks->eeprom_size = 0;
1670
Ben Dooks3ba81f32009-07-16 05:24:08 +00001671 ks8851_read_selftest(ks);
1672 ks8851_init_mac(ks);
1673
1674 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1675 ndev->name, ks);
1676 if (ret < 0) {
1677 dev_err(&spi->dev, "failed to get irq\n");
1678 goto err_irq;
1679 }
1680
1681 ret = register_netdev(ndev);
1682 if (ret) {
1683 dev_err(&spi->dev, "failed to register network device\n");
1684 goto err_netdev;
1685 }
1686
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001687 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d\n",
1688 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
1689 ndev->dev_addr, ndev->irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001690
1691 return 0;
1692
1693
1694err_netdev:
1695 free_irq(ndev->irq, ndev);
1696
1697err_id:
1698err_irq:
1699 free_netdev(ndev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700
1701 if (!IS_ERR(ks->vdd_io)) {
1702 regulator_disable(ks->vdd_phy);
1703 regulator_put(ks->vdd_io);
1704 }
1705
1706 if (!IS_ERR(ks->vdd_phy)) {
1707 regulator_disable(ks->vdd_phy);
1708 regulator_put(ks->vdd_phy);
1709 }
Ben Dooks3ba81f32009-07-16 05:24:08 +00001710 return ret;
1711}
1712
1713static int __devexit ks8851_remove(struct spi_device *spi)
1714{
1715 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
1716
1717 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001718 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001720 if (!IS_ERR(priv->vdd_io)) {
1721 regulator_disable(priv->vdd_phy);
1722 regulator_put(priv->vdd_io);
1723 }
1724
1725 if (!IS_ERR(priv->vdd_phy)) {
1726 regulator_disable(priv->vdd_phy);
1727 regulator_put(priv->vdd_phy);
1728 }
1729
Ben Dooks3ba81f32009-07-16 05:24:08 +00001730 unregister_netdev(priv->netdev);
1731 free_irq(spi->irq, priv);
1732 free_netdev(priv->netdev);
1733
1734 return 0;
1735}
1736
1737static struct spi_driver ks8851_driver = {
1738 .driver = {
1739 .name = "ks8851",
1740 .owner = THIS_MODULE,
1741 },
1742 .probe = ks8851_probe,
1743 .remove = __devexit_p(ks8851_remove),
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001744 .suspend = ks8851_suspend,
1745 .resume = ks8851_resume,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001746};
1747
1748static int __init ks8851_init(void)
1749{
1750 return spi_register_driver(&ks8851_driver);
1751}
1752
1753static void __exit ks8851_exit(void)
1754{
1755 spi_unregister_driver(&ks8851_driver);
1756}
1757
1758module_init(ks8851_init);
1759module_exit(ks8851_exit);
1760
1761MODULE_DESCRIPTION("KS8851 Network driver");
1762MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1763MODULE_LICENSE("GPL");
1764
1765module_param_named(message, msg_enable, int, 0);
1766MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001767MODULE_ALIAS("spi:ks8851");