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