]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/net/ethernet/micrel/ks8851.c
Merge tag 'kconfig-v5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[mirror_ubuntu-hirsute-kernel.git] / drivers / net / ethernet / micrel / ks8851.c
1 /* drivers/net/ethernet/micrel/ks8851.c
2 *
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
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #define DEBUG
15
16 #include <linux/interrupt.h>
17 #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 #include <linux/eeprom_93cx6.h>
26 #include <linux/regulator/consumer.h>
27
28 #include <linux/spi/spi.h>
29 #include <linux/gpio.h>
30 #include <linux/of_gpio.h>
31 #include <linux/of_net.h>
32
33 #include "ks8851.h"
34
35 /**
36 * struct ks8851_rxctrl - KS8851 driver rx control
37 * @mchash: Multicast hash-table data.
38 * @rxcr1: KS_RXCR1 register setting
39 * @rxcr2: KS_RXCR2 register setting
40 *
41 * Representation of the settings needs to control the receive filtering
42 * such as the multicast hash-filter and the receive register settings. This
43 * is used to make the job of working out if the receive settings change and
44 * then issuing the new settings to the worker that will send the necessary
45 * commands.
46 */
47 struct ks8851_rxctrl {
48 u16 mchash[4];
49 u16 rxcr1;
50 u16 rxcr2;
51 };
52
53 /**
54 * union ks8851_tx_hdr - tx header data
55 * @txb: The header as bytes
56 * @txw: The header as 16bit, little-endian words
57 *
58 * A dual representation of the tx header data to allow
59 * access to individual bytes, and to allow 16bit accesses
60 * with 16bit alignment.
61 */
62 union ks8851_tx_hdr {
63 u8 txb[6];
64 __le16 txw[3];
65 };
66
67 /**
68 * struct ks8851_net - KS8851 driver private data
69 * @netdev: The network device we're bound to
70 * @spidev: The spi device we're bound to.
71 * @lock: Lock to ensure that the device is not accessed when busy.
72 * @statelock: Lock on this structure for tx list.
73 * @mii: The MII state information for the mii calls.
74 * @rxctrl: RX settings for @rxctrl_work.
75 * @tx_work: Work queue for tx packets
76 * @rxctrl_work: Work queue for updating RX mode and multicast lists
77 * @txq: Queue of packets for transmission.
78 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
79 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
80 * @txh: Space for generating packet TX header in DMA-able data
81 * @rxd: Space for receiving SPI data, in DMA-able space.
82 * @txd: Space for transmitting SPI data, in DMA-able space.
83 * @msg_enable: The message flags controlling driver output (see ethtool).
84 * @fid: Incrementing frame id tag.
85 * @rc_ier: Cached copy of KS_IER.
86 * @rc_ccr: Cached copy of KS_CCR.
87 * @rc_rxqcr: Cached copy of KS_RXQCR.
88 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
89 * @vdd_reg: Optional regulator supplying the chip
90 * @vdd_io: Optional digital power supply for IO
91 * @gpio: Optional reset_n gpio
92 *
93 * The @lock ensures that the chip is protected when certain operations are
94 * in progress. When the read or write packet transfer is in progress, most
95 * of the chip registers are not ccessible until the transfer is finished and
96 * the DMA has been de-asserted.
97 *
98 * The @statelock is used to protect information in the structure which may
99 * need to be accessed via several sources, such as the network driver layer
100 * or one of the work queues.
101 *
102 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
103 * wants to DMA map them, it will not have any problems with data the driver
104 * modifies.
105 */
106 struct ks8851_net {
107 struct net_device *netdev;
108 struct spi_device *spidev;
109 struct mutex lock;
110 spinlock_t statelock;
111
112 union ks8851_tx_hdr txh ____cacheline_aligned;
113 u8 rxd[8];
114 u8 txd[8];
115
116 u32 msg_enable ____cacheline_aligned;
117 u16 tx_space;
118 u8 fid;
119
120 u16 rc_ier;
121 u16 rc_rxqcr;
122 u16 rc_ccr;
123
124 struct mii_if_info mii;
125 struct ks8851_rxctrl rxctrl;
126
127 struct work_struct tx_work;
128 struct work_struct rxctrl_work;
129
130 struct sk_buff_head txq;
131
132 struct spi_message spi_msg1;
133 struct spi_message spi_msg2;
134 struct spi_transfer spi_xfer1;
135 struct spi_transfer spi_xfer2[2];
136
137 struct eeprom_93cx6 eeprom;
138 struct regulator *vdd_reg;
139 struct regulator *vdd_io;
140 int gpio;
141 };
142
143 static int msg_enable;
144
145 /* SPI frame opcodes */
146 #define KS_SPIOP_RD (0x00)
147 #define KS_SPIOP_WR (0x40)
148 #define KS_SPIOP_RXFIFO (0x80)
149 #define KS_SPIOP_TXFIFO (0xC0)
150
151 /* shift for byte-enable data */
152 #define BYTE_EN(_x) ((_x) << 2)
153
154 /* turn register number and byte-enable mask into data for start of packet */
155 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
156
157 /* SPI register read/write calls.
158 *
159 * All these calls issue SPI transactions to access the chip's registers. They
160 * all require that the necessary lock is held to prevent accesses when the
161 * chip is busy transferring packet data (RX/TX FIFO accesses).
162 */
163
164 /**
165 * ks8851_wrreg16 - write 16bit register value to chip
166 * @ks: The chip state
167 * @reg: The register address
168 * @val: The value to write
169 *
170 * Issue a write to put the value @val into the register specified in @reg.
171 */
172 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
173 {
174 struct spi_transfer *xfer = &ks->spi_xfer1;
175 struct spi_message *msg = &ks->spi_msg1;
176 __le16 txb[2];
177 int ret;
178
179 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
180 txb[1] = cpu_to_le16(val);
181
182 xfer->tx_buf = txb;
183 xfer->rx_buf = NULL;
184 xfer->len = 4;
185
186 ret = spi_sync(ks->spidev, msg);
187 if (ret < 0)
188 netdev_err(ks->netdev, "spi_sync() failed\n");
189 }
190
191 /**
192 * ks8851_wrreg8 - write 8bit register value to chip
193 * @ks: The chip state
194 * @reg: The register address
195 * @val: The value to write
196 *
197 * Issue a write to put the value @val into the register specified in @reg.
198 */
199 static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
200 {
201 struct spi_transfer *xfer = &ks->spi_xfer1;
202 struct spi_message *msg = &ks->spi_msg1;
203 __le16 txb[2];
204 int ret;
205 int bit;
206
207 bit = 1 << (reg & 3);
208
209 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
210 txb[1] = val;
211
212 xfer->tx_buf = txb;
213 xfer->rx_buf = NULL;
214 xfer->len = 3;
215
216 ret = spi_sync(ks->spidev, msg);
217 if (ret < 0)
218 netdev_err(ks->netdev, "spi_sync() failed\n");
219 }
220
221 /**
222 * ks8851_rdreg - issue read register command and return the data
223 * @ks: The device state
224 * @op: The register address and byte enables in message format.
225 * @rxb: The RX buffer to return the result into
226 * @rxl: The length of data expected.
227 *
228 * This is the low level read call that issues the necessary spi message(s)
229 * to read data from the register specified in @op.
230 */
231 static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
232 u8 *rxb, unsigned rxl)
233 {
234 struct spi_transfer *xfer;
235 struct spi_message *msg;
236 __le16 *txb = (__le16 *)ks->txd;
237 u8 *trx = ks->rxd;
238 int ret;
239
240 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
241
242 if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
243 msg = &ks->spi_msg2;
244 xfer = ks->spi_xfer2;
245
246 xfer->tx_buf = txb;
247 xfer->rx_buf = NULL;
248 xfer->len = 2;
249
250 xfer++;
251 xfer->tx_buf = NULL;
252 xfer->rx_buf = trx;
253 xfer->len = rxl;
254 } else {
255 msg = &ks->spi_msg1;
256 xfer = &ks->spi_xfer1;
257
258 xfer->tx_buf = txb;
259 xfer->rx_buf = trx;
260 xfer->len = rxl + 2;
261 }
262
263 ret = spi_sync(ks->spidev, msg);
264 if (ret < 0)
265 netdev_err(ks->netdev, "read: spi_sync() failed\n");
266 else if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
267 memcpy(rxb, trx, rxl);
268 else
269 memcpy(rxb, trx + 2, rxl);
270 }
271
272 /**
273 * ks8851_rdreg8 - read 8 bit register from device
274 * @ks: The chip information
275 * @reg: The register address
276 *
277 * Read a 8bit register from the chip, returning the result
278 */
279 static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
280 {
281 u8 rxb[1];
282
283 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
284 return rxb[0];
285 }
286
287 /**
288 * ks8851_rdreg16 - read 16 bit register from device
289 * @ks: The chip information
290 * @reg: The register address
291 *
292 * Read a 16bit register from the chip, returning the result
293 */
294 static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
295 {
296 __le16 rx = 0;
297
298 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
299 return le16_to_cpu(rx);
300 }
301
302 /**
303 * ks8851_rdreg32 - read 32 bit register from device
304 * @ks: The chip information
305 * @reg: The register address
306 *
307 * Read a 32bit register from the chip.
308 *
309 * Note, this read requires the address be aligned to 4 bytes.
310 */
311 static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
312 {
313 __le32 rx = 0;
314
315 WARN_ON(reg & 3);
316
317 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
318 return le32_to_cpu(rx);
319 }
320
321 /**
322 * ks8851_soft_reset - issue one of the soft reset to the device
323 * @ks: The device state.
324 * @op: The bit(s) to set in the GRR
325 *
326 * Issue the relevant soft-reset command to the device's GRR register
327 * specified by @op.
328 *
329 * Note, the delays are in there as a caution to ensure that the reset
330 * has time to take effect and then complete. Since the datasheet does
331 * not currently specify the exact sequence, we have chosen something
332 * that seems to work with our device.
333 */
334 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
335 {
336 ks8851_wrreg16(ks, KS_GRR, op);
337 mdelay(1); /* wait a short time to effect reset */
338 ks8851_wrreg16(ks, KS_GRR, 0);
339 mdelay(1); /* wait for condition to clear */
340 }
341
342 /**
343 * ks8851_set_powermode - set power mode of the device
344 * @ks: The device state
345 * @pwrmode: The power mode value to write to KS_PMECR.
346 *
347 * Change the power mode of the chip.
348 */
349 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
350 {
351 unsigned pmecr;
352
353 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
354
355 pmecr = ks8851_rdreg16(ks, KS_PMECR);
356 pmecr &= ~PMECR_PM_MASK;
357 pmecr |= pwrmode;
358
359 ks8851_wrreg16(ks, KS_PMECR, pmecr);
360 }
361
362 /**
363 * ks8851_write_mac_addr - write mac address to device registers
364 * @dev: The network device
365 *
366 * Update the KS8851 MAC address registers from the address in @dev.
367 *
368 * This call assumes that the chip is not running, so there is no need to
369 * shutdown the RXQ process whilst setting this.
370 */
371 static int ks8851_write_mac_addr(struct net_device *dev)
372 {
373 struct ks8851_net *ks = netdev_priv(dev);
374 int i;
375
376 mutex_lock(&ks->lock);
377
378 /*
379 * Wake up chip in case it was powered off when stopped; otherwise,
380 * the first write to the MAC address does not take effect.
381 */
382 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
383 for (i = 0; i < ETH_ALEN; i++)
384 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
385 if (!netif_running(dev))
386 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
387
388 mutex_unlock(&ks->lock);
389
390 return 0;
391 }
392
393 /**
394 * ks8851_read_mac_addr - read mac address from device registers
395 * @dev: The network device
396 *
397 * Update our copy of the KS8851 MAC address from the registers of @dev.
398 */
399 static void ks8851_read_mac_addr(struct net_device *dev)
400 {
401 struct ks8851_net *ks = netdev_priv(dev);
402 int i;
403
404 mutex_lock(&ks->lock);
405
406 for (i = 0; i < ETH_ALEN; i++)
407 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
408
409 mutex_unlock(&ks->lock);
410 }
411
412 /**
413 * ks8851_init_mac - initialise the mac address
414 * @ks: The device structure
415 *
416 * Get or create the initial mac address for the device and then set that
417 * into the station address register. A mac address supplied in the device
418 * tree takes precedence. Otherwise, if there is an EEPROM present, then
419 * we try that. If no valid mac address is found we use eth_random_addr()
420 * to create a new one.
421 */
422 static void ks8851_init_mac(struct ks8851_net *ks)
423 {
424 struct net_device *dev = ks->netdev;
425 const u8 *mac_addr;
426
427 mac_addr = of_get_mac_address(ks->spidev->dev.of_node);
428 if (!IS_ERR(mac_addr)) {
429 ether_addr_copy(dev->dev_addr, mac_addr);
430 ks8851_write_mac_addr(dev);
431 return;
432 }
433
434 if (ks->rc_ccr & CCR_EEPROM) {
435 ks8851_read_mac_addr(dev);
436 if (is_valid_ether_addr(dev->dev_addr))
437 return;
438
439 netdev_err(ks->netdev, "invalid mac address read %pM\n",
440 dev->dev_addr);
441 }
442
443 eth_hw_addr_random(dev);
444 ks8851_write_mac_addr(dev);
445 }
446
447 /**
448 * ks8851_rdfifo - read data from the receive fifo
449 * @ks: The device state.
450 * @buff: The buffer address
451 * @len: The length of the data to read
452 *
453 * Issue an RXQ FIFO read command and read the @len amount of data from
454 * the FIFO into the buffer specified by @buff.
455 */
456 static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
457 {
458 struct spi_transfer *xfer = ks->spi_xfer2;
459 struct spi_message *msg = &ks->spi_msg2;
460 u8 txb[1];
461 int ret;
462
463 netif_dbg(ks, rx_status, ks->netdev,
464 "%s: %d@%p\n", __func__, len, buff);
465
466 /* set the operation we're issuing */
467 txb[0] = KS_SPIOP_RXFIFO;
468
469 xfer->tx_buf = txb;
470 xfer->rx_buf = NULL;
471 xfer->len = 1;
472
473 xfer++;
474 xfer->rx_buf = buff;
475 xfer->tx_buf = NULL;
476 xfer->len = len;
477
478 ret = spi_sync(ks->spidev, msg);
479 if (ret < 0)
480 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
481 }
482
483 /**
484 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
485 * @ks: The device state
486 * @rxpkt: The data for the received packet
487 *
488 * Dump the initial data from the packet to dev_dbg().
489 */
490 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
491 {
492 netdev_dbg(ks->netdev,
493 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
494 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
495 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
496 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
497 }
498
499 /**
500 * ks8851_rx_pkts - receive packets from the host
501 * @ks: The device information.
502 *
503 * This is called from the IRQ work queue when the system detects that there
504 * are packets in the receive queue. Find out how many packets there are and
505 * read them from the FIFO.
506 */
507 static void ks8851_rx_pkts(struct ks8851_net *ks)
508 {
509 struct sk_buff *skb;
510 unsigned rxfc;
511 unsigned rxlen;
512 unsigned rxstat;
513 u32 rxh;
514 u8 *rxpkt;
515
516 rxfc = ks8851_rdreg8(ks, KS_RXFC);
517
518 netif_dbg(ks, rx_status, ks->netdev,
519 "%s: %d packets\n", __func__, rxfc);
520
521 /* Currently we're issuing a read per packet, but we could possibly
522 * improve the code by issuing a single read, getting the receive
523 * header, allocating the packet and then reading the packet data
524 * out in one go.
525 *
526 * This form of operation would require us to hold the SPI bus'
527 * chipselect low during the entie transaction to avoid any
528 * reset to the data stream coming from the chip.
529 */
530
531 for (; rxfc != 0; rxfc--) {
532 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
533 rxstat = rxh & 0xffff;
534 rxlen = (rxh >> 16) & 0xfff;
535
536 netif_dbg(ks, rx_status, ks->netdev,
537 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
538
539 /* the length of the packet includes the 32bit CRC */
540
541 /* set dma read address */
542 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
543
544 /* start DMA access */
545 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
546
547 if (rxlen > 4) {
548 unsigned int rxalign;
549
550 rxlen -= 4;
551 rxalign = ALIGN(rxlen, 4);
552 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
553 if (skb) {
554
555 /* 4 bytes of status header + 4 bytes of
556 * garbage: we put them before ethernet
557 * header, so that they are copied,
558 * but ignored.
559 */
560
561 rxpkt = skb_put(skb, rxlen) - 8;
562
563 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
564
565 if (netif_msg_pktdata(ks))
566 ks8851_dbg_dumpkkt(ks, rxpkt);
567
568 skb->protocol = eth_type_trans(skb, ks->netdev);
569 netif_rx_ni(skb);
570
571 ks->netdev->stats.rx_packets++;
572 ks->netdev->stats.rx_bytes += rxlen;
573 }
574 }
575
576 /* end DMA access and dequeue packet */
577 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
578 }
579 }
580
581 /**
582 * ks8851_irq - IRQ handler for dealing with interrupt requests
583 * @irq: IRQ number
584 * @_ks: cookie
585 *
586 * This handler is invoked when the IRQ line asserts to find out what happened.
587 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
588 * in thread context.
589 *
590 * Read the interrupt status, work out what needs to be done and then clear
591 * any of the interrupts that are not needed.
592 */
593 static irqreturn_t ks8851_irq(int irq, void *_ks)
594 {
595 struct ks8851_net *ks = _ks;
596 unsigned status;
597 unsigned handled = 0;
598
599 mutex_lock(&ks->lock);
600
601 status = ks8851_rdreg16(ks, KS_ISR);
602
603 netif_dbg(ks, intr, ks->netdev,
604 "%s: status 0x%04x\n", __func__, status);
605
606 if (status & IRQ_LCI)
607 handled |= IRQ_LCI;
608
609 if (status & IRQ_LDI) {
610 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
611 pmecr &= ~PMECR_WKEVT_MASK;
612 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
613
614 handled |= IRQ_LDI;
615 }
616
617 if (status & IRQ_RXPSI)
618 handled |= IRQ_RXPSI;
619
620 if (status & IRQ_TXI) {
621 handled |= IRQ_TXI;
622
623 /* no lock here, tx queue should have been stopped */
624
625 /* update our idea of how much tx space is available to the
626 * system */
627 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
628
629 netif_dbg(ks, intr, ks->netdev,
630 "%s: txspace %d\n", __func__, ks->tx_space);
631 }
632
633 if (status & IRQ_RXI)
634 handled |= IRQ_RXI;
635
636 if (status & IRQ_SPIBEI) {
637 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
638 handled |= IRQ_SPIBEI;
639 }
640
641 ks8851_wrreg16(ks, KS_ISR, handled);
642
643 if (status & IRQ_RXI) {
644 /* the datasheet says to disable the rx interrupt during
645 * packet read-out, however we're masking the interrupt
646 * from the device so do not bother masking just the RX
647 * from the device. */
648
649 ks8851_rx_pkts(ks);
650 }
651
652 /* if something stopped the rx process, probably due to wanting
653 * to change the rx settings, then do something about restarting
654 * it. */
655 if (status & IRQ_RXPSI) {
656 struct ks8851_rxctrl *rxc = &ks->rxctrl;
657
658 /* update the multicast hash table */
659 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
660 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
661 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
662 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
663
664 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
665 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
666 }
667
668 mutex_unlock(&ks->lock);
669
670 if (status & IRQ_LCI)
671 mii_check_link(&ks->mii);
672
673 if (status & IRQ_TXI)
674 netif_wake_queue(ks->netdev);
675
676 return IRQ_HANDLED;
677 }
678
679 /**
680 * calc_txlen - calculate size of message to send packet
681 * @len: Length of data
682 *
683 * Returns the size of the TXFIFO message needed to send
684 * this packet.
685 */
686 static inline unsigned calc_txlen(unsigned len)
687 {
688 return ALIGN(len + 4, 4);
689 }
690
691 /**
692 * ks8851_wrpkt - write packet to TX FIFO
693 * @ks: The device state.
694 * @txp: The sk_buff to transmit.
695 * @irq: IRQ on completion of the packet.
696 *
697 * Send the @txp to the chip. This means creating the relevant packet header
698 * specifying the length of the packet and the other information the chip
699 * needs, such as IRQ on completion. Send the header and the packet data to
700 * the device.
701 */
702 static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
703 {
704 struct spi_transfer *xfer = ks->spi_xfer2;
705 struct spi_message *msg = &ks->spi_msg2;
706 unsigned fid = 0;
707 int ret;
708
709 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
710 __func__, txp, txp->len, txp->data, irq);
711
712 fid = ks->fid++;
713 fid &= TXFR_TXFID_MASK;
714
715 if (irq)
716 fid |= TXFR_TXIC; /* irq on completion */
717
718 /* start header at txb[1] to align txw entries */
719 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
720 ks->txh.txw[1] = cpu_to_le16(fid);
721 ks->txh.txw[2] = cpu_to_le16(txp->len);
722
723 xfer->tx_buf = &ks->txh.txb[1];
724 xfer->rx_buf = NULL;
725 xfer->len = 5;
726
727 xfer++;
728 xfer->tx_buf = txp->data;
729 xfer->rx_buf = NULL;
730 xfer->len = ALIGN(txp->len, 4);
731
732 ret = spi_sync(ks->spidev, msg);
733 if (ret < 0)
734 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
735 }
736
737 /**
738 * ks8851_done_tx - update and then free skbuff after transmitting
739 * @ks: The device state
740 * @txb: The buffer transmitted
741 */
742 static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
743 {
744 struct net_device *dev = ks->netdev;
745
746 dev->stats.tx_bytes += txb->len;
747 dev->stats.tx_packets++;
748
749 dev_kfree_skb(txb);
750 }
751
752 /**
753 * ks8851_tx_work - process tx packet(s)
754 * @work: The work strucutre what was scheduled.
755 *
756 * This is called when a number of packets have been scheduled for
757 * transmission and need to be sent to the device.
758 */
759 static void ks8851_tx_work(struct work_struct *work)
760 {
761 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
762 struct sk_buff *txb;
763 bool last = skb_queue_empty(&ks->txq);
764
765 mutex_lock(&ks->lock);
766
767 while (!last) {
768 txb = skb_dequeue(&ks->txq);
769 last = skb_queue_empty(&ks->txq);
770
771 if (txb != NULL) {
772 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
773 ks8851_wrpkt(ks, txb, last);
774 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
775 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
776
777 ks8851_done_tx(ks, txb);
778 }
779 }
780
781 mutex_unlock(&ks->lock);
782 }
783
784 /**
785 * ks8851_net_open - open network device
786 * @dev: The network device being opened.
787 *
788 * Called when the network device is marked active, such as a user executing
789 * 'ifconfig up' on the device.
790 */
791 static int ks8851_net_open(struct net_device *dev)
792 {
793 struct ks8851_net *ks = netdev_priv(dev);
794 int ret;
795
796 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
797 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
798 dev->name, ks);
799 if (ret < 0) {
800 netdev_err(dev, "failed to get irq\n");
801 return ret;
802 }
803
804 /* lock the card, even if we may not actually be doing anything
805 * else at the moment */
806 mutex_lock(&ks->lock);
807
808 netif_dbg(ks, ifup, ks->netdev, "opening\n");
809
810 /* bring chip out of any power saving mode it was in */
811 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
812
813 /* issue a soft reset to the RX/TX QMU to put it into a known
814 * state. */
815 ks8851_soft_reset(ks, GRR_QMU);
816
817 /* setup transmission parameters */
818
819 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
820 TXCR_TXPE | /* pad to min length */
821 TXCR_TXCRC | /* add CRC */
822 TXCR_TXFCE)); /* enable flow control */
823
824 /* auto-increment tx data, reset tx pointer */
825 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
826
827 /* setup receiver control */
828
829 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
830 RXCR1_RXFCE | /* enable flow control */
831 RXCR1_RXBE | /* broadcast enable */
832 RXCR1_RXUE | /* unicast enable */
833 RXCR1_RXE)); /* enable rx block */
834
835 /* transfer entire frames out in one go */
836 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
837
838 /* set receive counter timeouts */
839 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
840 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
841 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
842
843 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
844 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
845 RXQCR_RXDTTE); /* IRQ on time exceeded */
846
847 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
848
849 /* clear then enable interrupts */
850
851 #define STD_IRQ (IRQ_LCI | /* Link Change */ \
852 IRQ_TXI | /* TX done */ \
853 IRQ_RXI | /* RX done */ \
854 IRQ_SPIBEI | /* SPI bus error */ \
855 IRQ_TXPSI | /* TX process stop */ \
856 IRQ_RXPSI) /* RX process stop */
857
858 ks->rc_ier = STD_IRQ;
859 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
860 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
861
862 netif_start_queue(ks->netdev);
863
864 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
865
866 mutex_unlock(&ks->lock);
867 mii_check_link(&ks->mii);
868 return 0;
869 }
870
871 /**
872 * ks8851_net_stop - close network device
873 * @dev: The device being closed.
874 *
875 * Called to close down a network device which has been active. Cancell any
876 * work, shutdown the RX and TX process and then place the chip into a low
877 * power state whilst it is not being used.
878 */
879 static int ks8851_net_stop(struct net_device *dev)
880 {
881 struct ks8851_net *ks = netdev_priv(dev);
882
883 netif_info(ks, ifdown, dev, "shutting down\n");
884
885 netif_stop_queue(dev);
886
887 mutex_lock(&ks->lock);
888 /* turn off the IRQs and ack any outstanding */
889 ks8851_wrreg16(ks, KS_IER, 0x0000);
890 ks8851_wrreg16(ks, KS_ISR, 0xffff);
891 mutex_unlock(&ks->lock);
892
893 /* stop any outstanding work */
894 flush_work(&ks->tx_work);
895 flush_work(&ks->rxctrl_work);
896
897 mutex_lock(&ks->lock);
898 /* shutdown RX process */
899 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
900
901 /* shutdown TX process */
902 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
903
904 /* set powermode to soft power down to save power */
905 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
906 mutex_unlock(&ks->lock);
907
908 /* ensure any queued tx buffers are dumped */
909 while (!skb_queue_empty(&ks->txq)) {
910 struct sk_buff *txb = skb_dequeue(&ks->txq);
911
912 netif_dbg(ks, ifdown, ks->netdev,
913 "%s: freeing txb %p\n", __func__, txb);
914
915 dev_kfree_skb(txb);
916 }
917
918 free_irq(dev->irq, ks);
919
920 return 0;
921 }
922
923 /**
924 * ks8851_start_xmit - transmit packet
925 * @skb: The buffer to transmit
926 * @dev: The device used to transmit the packet.
927 *
928 * Called by the network layer to transmit the @skb. Queue the packet for
929 * the device and schedule the necessary work to transmit the packet when
930 * it is free.
931 *
932 * We do this to firstly avoid sleeping with the network device locked,
933 * and secondly so we can round up more than one packet to transmit which
934 * means we can try and avoid generating too many transmit done interrupts.
935 */
936 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
937 struct net_device *dev)
938 {
939 struct ks8851_net *ks = netdev_priv(dev);
940 unsigned needed = calc_txlen(skb->len);
941 netdev_tx_t ret = NETDEV_TX_OK;
942
943 netif_dbg(ks, tx_queued, ks->netdev,
944 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
945
946 spin_lock(&ks->statelock);
947
948 if (needed > ks->tx_space) {
949 netif_stop_queue(dev);
950 ret = NETDEV_TX_BUSY;
951 } else {
952 ks->tx_space -= needed;
953 skb_queue_tail(&ks->txq, skb);
954 }
955
956 spin_unlock(&ks->statelock);
957 schedule_work(&ks->tx_work);
958
959 return ret;
960 }
961
962 /**
963 * ks8851_rxctrl_work - work handler to change rx mode
964 * @work: The work structure this belongs to.
965 *
966 * Lock the device and issue the necessary changes to the receive mode from
967 * the network device layer. This is done so that we can do this without
968 * having to sleep whilst holding the network device lock.
969 *
970 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
971 * receive parameters are programmed, we issue a write to disable the RXQ and
972 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
973 * complete. The interrupt handler then writes the new values into the chip.
974 */
975 static void ks8851_rxctrl_work(struct work_struct *work)
976 {
977 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
978
979 mutex_lock(&ks->lock);
980
981 /* need to shutdown RXQ before modifying filter parameters */
982 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
983
984 mutex_unlock(&ks->lock);
985 }
986
987 static void ks8851_set_rx_mode(struct net_device *dev)
988 {
989 struct ks8851_net *ks = netdev_priv(dev);
990 struct ks8851_rxctrl rxctrl;
991
992 memset(&rxctrl, 0, sizeof(rxctrl));
993
994 if (dev->flags & IFF_PROMISC) {
995 /* interface to receive everything */
996
997 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
998 } else if (dev->flags & IFF_ALLMULTI) {
999 /* accept all multicast packets */
1000
1001 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
1002 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
1003 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
1004 struct netdev_hw_addr *ha;
1005 u32 crc;
1006
1007 /* accept some multicast */
1008
1009 netdev_for_each_mc_addr(ha, dev) {
1010 crc = ether_crc(ETH_ALEN, ha->addr);
1011 crc >>= (32 - 6); /* get top six bits */
1012
1013 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1014 }
1015
1016 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
1017 } else {
1018 /* just accept broadcast / unicast */
1019 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1020 }
1021
1022 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1023 RXCR1_RXBE | /* broadcast enable */
1024 RXCR1_RXE | /* RX process enable */
1025 RXCR1_RXFCE); /* enable flow control */
1026
1027 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1028
1029 /* schedule work to do the actual set of the data if needed */
1030
1031 spin_lock(&ks->statelock);
1032
1033 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1034 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1035 schedule_work(&ks->rxctrl_work);
1036 }
1037
1038 spin_unlock(&ks->statelock);
1039 }
1040
1041 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1042 {
1043 struct sockaddr *sa = addr;
1044
1045 if (netif_running(dev))
1046 return -EBUSY;
1047
1048 if (!is_valid_ether_addr(sa->sa_data))
1049 return -EADDRNOTAVAIL;
1050
1051 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1052 return ks8851_write_mac_addr(dev);
1053 }
1054
1055 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1056 {
1057 struct ks8851_net *ks = netdev_priv(dev);
1058
1059 if (!netif_running(dev))
1060 return -EINVAL;
1061
1062 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1063 }
1064
1065 static const struct net_device_ops ks8851_netdev_ops = {
1066 .ndo_open = ks8851_net_open,
1067 .ndo_stop = ks8851_net_stop,
1068 .ndo_do_ioctl = ks8851_net_ioctl,
1069 .ndo_start_xmit = ks8851_start_xmit,
1070 .ndo_set_mac_address = ks8851_set_mac_address,
1071 .ndo_set_rx_mode = ks8851_set_rx_mode,
1072 .ndo_validate_addr = eth_validate_addr,
1073 };
1074
1075 /* ethtool support */
1076
1077 static void ks8851_get_drvinfo(struct net_device *dev,
1078 struct ethtool_drvinfo *di)
1079 {
1080 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1081 strlcpy(di->version, "1.00", sizeof(di->version));
1082 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1083 }
1084
1085 static u32 ks8851_get_msglevel(struct net_device *dev)
1086 {
1087 struct ks8851_net *ks = netdev_priv(dev);
1088 return ks->msg_enable;
1089 }
1090
1091 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1092 {
1093 struct ks8851_net *ks = netdev_priv(dev);
1094 ks->msg_enable = to;
1095 }
1096
1097 static int ks8851_get_link_ksettings(struct net_device *dev,
1098 struct ethtool_link_ksettings *cmd)
1099 {
1100 struct ks8851_net *ks = netdev_priv(dev);
1101
1102 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1103
1104 return 0;
1105 }
1106
1107 static int ks8851_set_link_ksettings(struct net_device *dev,
1108 const struct ethtool_link_ksettings *cmd)
1109 {
1110 struct ks8851_net *ks = netdev_priv(dev);
1111 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1112 }
1113
1114 static u32 ks8851_get_link(struct net_device *dev)
1115 {
1116 struct ks8851_net *ks = netdev_priv(dev);
1117 return mii_link_ok(&ks->mii);
1118 }
1119
1120 static int ks8851_nway_reset(struct net_device *dev)
1121 {
1122 struct ks8851_net *ks = netdev_priv(dev);
1123 return mii_nway_restart(&ks->mii);
1124 }
1125
1126 /* EEPROM support */
1127
1128 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1129 {
1130 struct ks8851_net *ks = ee->data;
1131 unsigned val;
1132
1133 val = ks8851_rdreg16(ks, KS_EEPCR);
1134
1135 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1136 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1137 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1138 }
1139
1140 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1141 {
1142 struct ks8851_net *ks = ee->data;
1143 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1144
1145 if (ee->drive_data)
1146 val |= EEPCR_EESRWA;
1147 if (ee->reg_data_in)
1148 val |= EEPCR_EEDO;
1149 if (ee->reg_data_clock)
1150 val |= EEPCR_EESCK;
1151 if (ee->reg_chip_select)
1152 val |= EEPCR_EECS;
1153
1154 ks8851_wrreg16(ks, KS_EEPCR, val);
1155 }
1156
1157 /**
1158 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1159 * @ks: The network device state.
1160 *
1161 * Check for the presence of an EEPROM, and then activate software access
1162 * to the device.
1163 */
1164 static int ks8851_eeprom_claim(struct ks8851_net *ks)
1165 {
1166 if (!(ks->rc_ccr & CCR_EEPROM))
1167 return -ENOENT;
1168
1169 mutex_lock(&ks->lock);
1170
1171 /* start with clock low, cs high */
1172 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1173 return 0;
1174 }
1175
1176 /**
1177 * ks8851_eeprom_release - release the EEPROM interface
1178 * @ks: The device state
1179 *
1180 * Release the software access to the device EEPROM
1181 */
1182 static void ks8851_eeprom_release(struct ks8851_net *ks)
1183 {
1184 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1185
1186 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1187 mutex_unlock(&ks->lock);
1188 }
1189
1190 #define KS_EEPROM_MAGIC (0x00008851)
1191
1192 static int ks8851_set_eeprom(struct net_device *dev,
1193 struct ethtool_eeprom *ee, u8 *data)
1194 {
1195 struct ks8851_net *ks = netdev_priv(dev);
1196 int offset = ee->offset;
1197 int len = ee->len;
1198 u16 tmp;
1199
1200 /* currently only support byte writing */
1201 if (len != 1)
1202 return -EINVAL;
1203
1204 if (ee->magic != KS_EEPROM_MAGIC)
1205 return -EINVAL;
1206
1207 if (ks8851_eeprom_claim(ks))
1208 return -ENOENT;
1209
1210 eeprom_93cx6_wren(&ks->eeprom, true);
1211
1212 /* ethtool currently only supports writing bytes, which means
1213 * we have to read/modify/write our 16bit EEPROMs */
1214
1215 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1216
1217 if (offset & 1) {
1218 tmp &= 0xff;
1219 tmp |= *data << 8;
1220 } else {
1221 tmp &= 0xff00;
1222 tmp |= *data;
1223 }
1224
1225 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1226 eeprom_93cx6_wren(&ks->eeprom, false);
1227
1228 ks8851_eeprom_release(ks);
1229
1230 return 0;
1231 }
1232
1233 static int ks8851_get_eeprom(struct net_device *dev,
1234 struct ethtool_eeprom *ee, u8 *data)
1235 {
1236 struct ks8851_net *ks = netdev_priv(dev);
1237 int offset = ee->offset;
1238 int len = ee->len;
1239
1240 /* must be 2 byte aligned */
1241 if (len & 1 || offset & 1)
1242 return -EINVAL;
1243
1244 if (ks8851_eeprom_claim(ks))
1245 return -ENOENT;
1246
1247 ee->magic = KS_EEPROM_MAGIC;
1248
1249 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1250 ks8851_eeprom_release(ks);
1251
1252 return 0;
1253 }
1254
1255 static int ks8851_get_eeprom_len(struct net_device *dev)
1256 {
1257 struct ks8851_net *ks = netdev_priv(dev);
1258
1259 /* currently, we assume it is an 93C46 attached, so return 128 */
1260 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
1261 }
1262
1263 static const struct ethtool_ops ks8851_ethtool_ops = {
1264 .get_drvinfo = ks8851_get_drvinfo,
1265 .get_msglevel = ks8851_get_msglevel,
1266 .set_msglevel = ks8851_set_msglevel,
1267 .get_link = ks8851_get_link,
1268 .nway_reset = ks8851_nway_reset,
1269 .get_eeprom_len = ks8851_get_eeprom_len,
1270 .get_eeprom = ks8851_get_eeprom,
1271 .set_eeprom = ks8851_set_eeprom,
1272 .get_link_ksettings = ks8851_get_link_ksettings,
1273 .set_link_ksettings = ks8851_set_link_ksettings,
1274 };
1275
1276 /* MII interface controls */
1277
1278 /**
1279 * ks8851_phy_reg - convert MII register into a KS8851 register
1280 * @reg: MII register number.
1281 *
1282 * Return the KS8851 register number for the corresponding MII PHY register
1283 * if possible. Return zero if the MII register has no direct mapping to the
1284 * KS8851 register set.
1285 */
1286 static int ks8851_phy_reg(int reg)
1287 {
1288 switch (reg) {
1289 case MII_BMCR:
1290 return KS_P1MBCR;
1291 case MII_BMSR:
1292 return KS_P1MBSR;
1293 case MII_PHYSID1:
1294 return KS_PHY1ILR;
1295 case MII_PHYSID2:
1296 return KS_PHY1IHR;
1297 case MII_ADVERTISE:
1298 return KS_P1ANAR;
1299 case MII_LPA:
1300 return KS_P1ANLPR;
1301 }
1302
1303 return 0x0;
1304 }
1305
1306 /**
1307 * ks8851_phy_read - MII interface PHY register read.
1308 * @dev: The network device the PHY is on.
1309 * @phy_addr: Address of PHY (ignored as we only have one)
1310 * @reg: The register to read.
1311 *
1312 * This call reads data from the PHY register specified in @reg. Since the
1313 * device does not support all the MII registers, the non-existent values
1314 * are always returned as zero.
1315 *
1316 * We return zero for unsupported registers as the MII code does not check
1317 * the value returned for any error status, and simply returns it to the
1318 * caller. The mii-tool that the driver was tested with takes any -ve error
1319 * as real PHY capabilities, thus displaying incorrect data to the user.
1320 */
1321 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1322 {
1323 struct ks8851_net *ks = netdev_priv(dev);
1324 int ksreg;
1325 int result;
1326
1327 ksreg = ks8851_phy_reg(reg);
1328 if (!ksreg)
1329 return 0x0; /* no error return allowed, so use zero */
1330
1331 mutex_lock(&ks->lock);
1332 result = ks8851_rdreg16(ks, ksreg);
1333 mutex_unlock(&ks->lock);
1334
1335 return result;
1336 }
1337
1338 static void ks8851_phy_write(struct net_device *dev,
1339 int phy, int reg, int value)
1340 {
1341 struct ks8851_net *ks = netdev_priv(dev);
1342 int ksreg;
1343
1344 ksreg = ks8851_phy_reg(reg);
1345 if (ksreg) {
1346 mutex_lock(&ks->lock);
1347 ks8851_wrreg16(ks, ksreg, value);
1348 mutex_unlock(&ks->lock);
1349 }
1350 }
1351
1352 /**
1353 * ks8851_read_selftest - read the selftest memory info.
1354 * @ks: The device state
1355 *
1356 * Read and check the TX/RX memory selftest information.
1357 */
1358 static int ks8851_read_selftest(struct ks8851_net *ks)
1359 {
1360 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1361 int ret = 0;
1362 unsigned rd;
1363
1364 rd = ks8851_rdreg16(ks, KS_MBIR);
1365
1366 if ((rd & both_done) != both_done) {
1367 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1368 return 0;
1369 }
1370
1371 if (rd & MBIR_TXMBFA) {
1372 netdev_err(ks->netdev, "TX memory selftest fail\n");
1373 ret |= 1;
1374 }
1375
1376 if (rd & MBIR_RXMBFA) {
1377 netdev_err(ks->netdev, "RX memory selftest fail\n");
1378 ret |= 2;
1379 }
1380
1381 return 0;
1382 }
1383
1384 /* driver bus management functions */
1385
1386 #ifdef CONFIG_PM_SLEEP
1387
1388 static int ks8851_suspend(struct device *dev)
1389 {
1390 struct ks8851_net *ks = dev_get_drvdata(dev);
1391 struct net_device *netdev = ks->netdev;
1392
1393 if (netif_running(netdev)) {
1394 netif_device_detach(netdev);
1395 ks8851_net_stop(netdev);
1396 }
1397
1398 return 0;
1399 }
1400
1401 static int ks8851_resume(struct device *dev)
1402 {
1403 struct ks8851_net *ks = dev_get_drvdata(dev);
1404 struct net_device *netdev = ks->netdev;
1405
1406 if (netif_running(netdev)) {
1407 ks8851_net_open(netdev);
1408 netif_device_attach(netdev);
1409 }
1410
1411 return 0;
1412 }
1413 #endif
1414
1415 static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
1416
1417 static int ks8851_probe(struct spi_device *spi)
1418 {
1419 struct net_device *ndev;
1420 struct ks8851_net *ks;
1421 int ret;
1422 unsigned cider;
1423 int gpio;
1424
1425 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1426 if (!ndev)
1427 return -ENOMEM;
1428
1429 spi->bits_per_word = 8;
1430
1431 ks = netdev_priv(ndev);
1432
1433 ks->netdev = ndev;
1434 ks->spidev = spi;
1435 ks->tx_space = 6144;
1436
1437 gpio = of_get_named_gpio_flags(spi->dev.of_node, "reset-gpios",
1438 0, NULL);
1439 if (gpio == -EPROBE_DEFER) {
1440 ret = gpio;
1441 goto err_gpio;
1442 }
1443
1444 ks->gpio = gpio;
1445 if (gpio_is_valid(gpio)) {
1446 ret = devm_gpio_request_one(&spi->dev, gpio,
1447 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1448 if (ret) {
1449 dev_err(&spi->dev, "reset gpio request failed\n");
1450 goto err_gpio;
1451 }
1452 }
1453
1454 ks->vdd_io = devm_regulator_get(&spi->dev, "vdd-io");
1455 if (IS_ERR(ks->vdd_io)) {
1456 ret = PTR_ERR(ks->vdd_io);
1457 goto err_reg_io;
1458 }
1459
1460 ret = regulator_enable(ks->vdd_io);
1461 if (ret) {
1462 dev_err(&spi->dev, "regulator vdd_io enable fail: %d\n",
1463 ret);
1464 goto err_reg_io;
1465 }
1466
1467 ks->vdd_reg = devm_regulator_get(&spi->dev, "vdd");
1468 if (IS_ERR(ks->vdd_reg)) {
1469 ret = PTR_ERR(ks->vdd_reg);
1470 goto err_reg;
1471 }
1472
1473 ret = regulator_enable(ks->vdd_reg);
1474 if (ret) {
1475 dev_err(&spi->dev, "regulator vdd enable fail: %d\n",
1476 ret);
1477 goto err_reg;
1478 }
1479
1480 if (gpio_is_valid(gpio)) {
1481 usleep_range(10000, 11000);
1482 gpio_set_value(gpio, 1);
1483 }
1484
1485 mutex_init(&ks->lock);
1486 spin_lock_init(&ks->statelock);
1487
1488 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1489 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1490
1491 /* initialise pre-made spi transfer messages */
1492
1493 spi_message_init(&ks->spi_msg1);
1494 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1495
1496 spi_message_init(&ks->spi_msg2);
1497 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1498 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1499
1500 /* setup EEPROM state */
1501
1502 ks->eeprom.data = ks;
1503 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1504 ks->eeprom.register_read = ks8851_eeprom_regread;
1505 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1506
1507 /* setup mii state */
1508 ks->mii.dev = ndev;
1509 ks->mii.phy_id = 1,
1510 ks->mii.phy_id_mask = 1;
1511 ks->mii.reg_num_mask = 0xf;
1512 ks->mii.mdio_read = ks8851_phy_read;
1513 ks->mii.mdio_write = ks8851_phy_write;
1514
1515 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1516
1517 /* set the default message enable */
1518 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1519 NETIF_MSG_PROBE |
1520 NETIF_MSG_LINK));
1521
1522 skb_queue_head_init(&ks->txq);
1523
1524 ndev->ethtool_ops = &ks8851_ethtool_ops;
1525 SET_NETDEV_DEV(ndev, &spi->dev);
1526
1527 spi_set_drvdata(spi, ks);
1528
1529 netif_carrier_off(ks->netdev);
1530 ndev->if_port = IF_PORT_100BASET;
1531 ndev->netdev_ops = &ks8851_netdev_ops;
1532 ndev->irq = spi->irq;
1533
1534 /* issue a global soft reset to reset the device. */
1535 ks8851_soft_reset(ks, GRR_GSR);
1536
1537 /* simple check for a valid chip being connected to the bus */
1538 cider = ks8851_rdreg16(ks, KS_CIDER);
1539 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1540 dev_err(&spi->dev, "failed to read device ID\n");
1541 ret = -ENODEV;
1542 goto err_id;
1543 }
1544
1545 /* cache the contents of the CCR register for EEPROM, etc. */
1546 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1547
1548 ks8851_read_selftest(ks);
1549 ks8851_init_mac(ks);
1550
1551 ret = register_netdev(ndev);
1552 if (ret) {
1553 dev_err(&spi->dev, "failed to register network device\n");
1554 goto err_netdev;
1555 }
1556
1557 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1558 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
1559 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1560
1561 return 0;
1562
1563 err_netdev:
1564 err_id:
1565 if (gpio_is_valid(gpio))
1566 gpio_set_value(gpio, 0);
1567 regulator_disable(ks->vdd_reg);
1568 err_reg:
1569 regulator_disable(ks->vdd_io);
1570 err_reg_io:
1571 err_gpio:
1572 free_netdev(ndev);
1573 return ret;
1574 }
1575
1576 static int ks8851_remove(struct spi_device *spi)
1577 {
1578 struct ks8851_net *priv = spi_get_drvdata(spi);
1579
1580 if (netif_msg_drv(priv))
1581 dev_info(&spi->dev, "remove\n");
1582
1583 unregister_netdev(priv->netdev);
1584 if (gpio_is_valid(priv->gpio))
1585 gpio_set_value(priv->gpio, 0);
1586 regulator_disable(priv->vdd_reg);
1587 regulator_disable(priv->vdd_io);
1588 free_netdev(priv->netdev);
1589
1590 return 0;
1591 }
1592
1593 static const struct of_device_id ks8851_match_table[] = {
1594 { .compatible = "micrel,ks8851" },
1595 { }
1596 };
1597 MODULE_DEVICE_TABLE(of, ks8851_match_table);
1598
1599 static struct spi_driver ks8851_driver = {
1600 .driver = {
1601 .name = "ks8851",
1602 .of_match_table = ks8851_match_table,
1603 .pm = &ks8851_pm_ops,
1604 },
1605 .probe = ks8851_probe,
1606 .remove = ks8851_remove,
1607 };
1608 module_spi_driver(ks8851_driver);
1609
1610 MODULE_DESCRIPTION("KS8851 Network driver");
1611 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1612 MODULE_LICENSE("GPL");
1613
1614 module_param_named(message, msg_enable, int, 0);
1615 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1616 MODULE_ALIAS("spi:ks8851");