blob: 279ee4612981b0af8d482674c6f1f5525988a6f7 [file] [log] [blame]
Paul Gortmaker3396c782012-01-27 13:36:01 +00001/* drivers/net/ethernet/micrel/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>
Ben Dooks51b7b1c2011-11-21 08:58:00 +000025#include <linux/eeprom_93cx6.h>
Nishanth Menonebf4ad92014-03-21 01:52:48 -050026#include <linux/regulator/consumer.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000027
28#include <linux/spi/spi.h>
Stephen Boyd73fdeb82014-05-23 12:57:19 -070029#include <linux/gpio.h>
30#include <linux/of_gpio.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000031
32#include "ks8851.h"
33
34/**
35 * struct ks8851_rxctrl - KS8851 driver rx control
36 * @mchash: Multicast hash-table data.
37 * @rxcr1: KS_RXCR1 register setting
38 * @rxcr2: KS_RXCR2 register setting
39 *
40 * Representation of the settings needs to control the receive filtering
41 * such as the multicast hash-filter and the receive register settings. This
42 * is used to make the job of working out if the receive settings change and
43 * then issuing the new settings to the worker that will send the necessary
44 * commands.
45 */
46struct ks8851_rxctrl {
47 u16 mchash[4];
48 u16 rxcr1;
49 u16 rxcr2;
50};
51
52/**
53 * union ks8851_tx_hdr - tx header data
54 * @txb: The header as bytes
55 * @txw: The header as 16bit, little-endian words
56 *
57 * A dual representation of the tx header data to allow
58 * access to individual bytes, and to allow 16bit accesses
59 * with 16bit alignment.
60 */
61union ks8851_tx_hdr {
62 u8 txb[6];
63 __le16 txw[3];
64};
65
66/**
67 * struct ks8851_net - KS8851 driver private data
68 * @netdev: The network device we're bound to
69 * @spidev: The spi device we're bound to.
70 * @lock: Lock to ensure that the device is not accessed when busy.
71 * @statelock: Lock on this structure for tx list.
72 * @mii: The MII state information for the mii calls.
73 * @rxctrl: RX settings for @rxctrl_work.
74 * @tx_work: Work queue for tx packets
Ben Dooks3ba81f32009-07-16 05:24:08 +000075 * @rxctrl_work: Work queue for updating RX mode and multicast lists
76 * @txq: Queue of packets for transmission.
77 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
78 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
79 * @txh: Space for generating packet TX header in DMA-able data
80 * @rxd: Space for receiving SPI data, in DMA-able space.
81 * @txd: Space for transmitting SPI data, in DMA-able space.
82 * @msg_enable: The message flags controlling driver output (see ethtool).
83 * @fid: Incrementing frame id tag.
84 * @rc_ier: Cached copy of KS_IER.
Sebastien Jan7d997462010-05-05 08:45:52 +000085 * @rc_ccr: Cached copy of KS_CCR.
Ben Dooks3ba81f32009-07-16 05:24:08 +000086 * @rc_rxqcr: Cached copy of KS_RXQCR.
Ben Dooks51b7b1c2011-11-21 08:58:00 +000087 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
Nishanth Menonebf4ad92014-03-21 01:52:48 -050088 * @vdd_reg: Optional regulator supplying the chip
Stephen Boyd73fdeb82014-05-23 12:57:19 -070089 * @vdd_io: Optional digital power supply for IO
90 * @gpio: Optional reset_n gpio
Ben Dooks3ba81f32009-07-16 05:24:08 +000091 *
92 * The @lock ensures that the chip is protected when certain operations are
93 * in progress. When the read or write packet transfer is in progress, most
94 * of the chip registers are not ccessible until the transfer is finished and
95 * the DMA has been de-asserted.
96 *
97 * The @statelock is used to protect information in the structure which may
98 * need to be accessed via several sources, such as the network driver layer
99 * or one of the work queues.
100 *
101 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
102 * wants to DMA map them, it will not have any problems with data the driver
103 * modifies.
104 */
105struct ks8851_net {
106 struct net_device *netdev;
107 struct spi_device *spidev;
108 struct mutex lock;
109 spinlock_t statelock;
110
111 union ks8851_tx_hdr txh ____cacheline_aligned;
112 u8 rxd[8];
113 u8 txd[8];
114
115 u32 msg_enable ____cacheline_aligned;
116 u16 tx_space;
117 u8 fid;
118
119 u16 rc_ier;
120 u16 rc_rxqcr;
Sebastien Jan7d997462010-05-05 08:45:52 +0000121 u16 rc_ccr;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000122
123 struct mii_if_info mii;
124 struct ks8851_rxctrl rxctrl;
125
126 struct work_struct tx_work;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000127 struct work_struct rxctrl_work;
128
129 struct sk_buff_head txq;
130
131 struct spi_message spi_msg1;
132 struct spi_message spi_msg2;
133 struct spi_transfer spi_xfer1;
134 struct spi_transfer spi_xfer2[2];
Ben Dooks51b7b1c2011-11-21 08:58:00 +0000135
136 struct eeprom_93cx6 eeprom;
Nishanth Menonebf4ad92014-03-21 01:52:48 -0500137 struct regulator *vdd_reg;
Stephen Boyd73fdeb82014-05-23 12:57:19 -0700138 struct regulator *vdd_io;
139 int gpio;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000140};
141
142static int msg_enable;
143
Ben Dooks3ba81f32009-07-16 05:24:08 +0000144/* shift for byte-enable data */
145#define BYTE_EN(_x) ((_x) << 2)
146
147/* turn register number and byte-enable mask into data for start of packet */
148#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
149
150/* SPI register read/write calls.
151 *
152 * All these calls issue SPI transactions to access the chip's registers. They
153 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300154 * chip is busy transferring packet data (RX/TX FIFO accesses).
Ben Dooks3ba81f32009-07-16 05:24:08 +0000155 */
156
157/**
158 * ks8851_wrreg16 - write 16bit register value to chip
159 * @ks: The chip state
160 * @reg: The register address
161 * @val: The value to write
162 *
163 * Issue a write to put the value @val into the register specified in @reg.
164 */
165static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
166{
167 struct spi_transfer *xfer = &ks->spi_xfer1;
168 struct spi_message *msg = &ks->spi_msg1;
169 __le16 txb[2];
170 int ret;
171
172 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
173 txb[1] = cpu_to_le16(val);
174
175 xfer->tx_buf = txb;
176 xfer->rx_buf = NULL;
177 xfer->len = 4;
178
179 ret = spi_sync(ks->spidev, msg);
180 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000181 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000182}
183
184/**
Ben Dooks160d0fa2009-10-19 23:49:04 +0000185 * ks8851_wrreg8 - write 8bit register value to chip
186 * @ks: The chip state
187 * @reg: The register address
188 * @val: The value to write
189 *
190 * Issue a write to put the value @val into the register specified in @reg.
191 */
192static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
193{
194 struct spi_transfer *xfer = &ks->spi_xfer1;
195 struct spi_message *msg = &ks->spi_msg1;
196 __le16 txb[2];
197 int ret;
198 int bit;
199
200 bit = 1 << (reg & 3);
201
202 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
203 txb[1] = val;
204
205 xfer->tx_buf = txb;
206 xfer->rx_buf = NULL;
207 xfer->len = 3;
208
209 ret = spi_sync(ks->spidev, msg);
210 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000211 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks160d0fa2009-10-19 23:49:04 +0000212}
213
214/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000215 * ks8851_rx_1msg - select whether to use one or two messages for spi read
216 * @ks: The device structure
217 *
218 * Return whether to generate a single message with a tx and rx buffer
219 * supplied to spi_sync(), or alternatively send the tx and rx buffers
220 * as separate messages.
221 *
222 * Depending on the hardware in use, a single message may be more efficient
223 * on interrupts or work done by the driver.
224 *
225 * This currently always returns true until we add some per-device data passed
226 * from the platform code to specify which mode is better.
227 */
228static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
229{
230 return true;
231}
232
233/**
234 * ks8851_rdreg - issue read register command and return the data
235 * @ks: The device state
236 * @op: The register address and byte enables in message format.
237 * @rxb: The RX buffer to return the result into
238 * @rxl: The length of data expected.
239 *
240 * This is the low level read call that issues the necessary spi message(s)
241 * to read data from the register specified in @op.
242 */
243static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
244 u8 *rxb, unsigned rxl)
245{
246 struct spi_transfer *xfer;
247 struct spi_message *msg;
248 __le16 *txb = (__le16 *)ks->txd;
249 u8 *trx = ks->rxd;
250 int ret;
251
252 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
253
254 if (ks8851_rx_1msg(ks)) {
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 } else {
262 msg = &ks->spi_msg2;
263 xfer = ks->spi_xfer2;
264
265 xfer->tx_buf = txb;
266 xfer->rx_buf = NULL;
267 xfer->len = 2;
268
269 xfer++;
270 xfer->tx_buf = NULL;
271 xfer->rx_buf = trx;
272 xfer->len = rxl;
273 }
274
275 ret = spi_sync(ks->spidev, msg);
276 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000277 netdev_err(ks->netdev, "read: spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000278 else if (ks8851_rx_1msg(ks))
279 memcpy(rxb, trx + 2, rxl);
280 else
281 memcpy(rxb, trx, rxl);
282}
283
284/**
285 * ks8851_rdreg8 - read 8 bit register from device
286 * @ks: The chip information
287 * @reg: The register address
288 *
289 * Read a 8bit register from the chip, returning the result
290*/
291static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
292{
293 u8 rxb[1];
294
295 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
296 return rxb[0];
297}
298
299/**
300 * ks8851_rdreg16 - read 16 bit register from device
301 * @ks: The chip information
302 * @reg: The register address
303 *
304 * Read a 16bit register from the chip, returning the result
305*/
306static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
307{
308 __le16 rx = 0;
309
310 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
311 return le16_to_cpu(rx);
312}
313
314/**
315 * ks8851_rdreg32 - read 32 bit register from device
316 * @ks: The chip information
317 * @reg: The register address
318 *
319 * Read a 32bit register from the chip.
320 *
321 * Note, this read requires the address be aligned to 4 bytes.
322*/
323static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
324{
325 __le32 rx = 0;
326
327 WARN_ON(reg & 3);
328
329 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
330 return le32_to_cpu(rx);
331}
332
333/**
334 * ks8851_soft_reset - issue one of the soft reset to the device
335 * @ks: The device state.
336 * @op: The bit(s) to set in the GRR
337 *
338 * Issue the relevant soft-reset command to the device's GRR register
339 * specified by @op.
340 *
341 * Note, the delays are in there as a caution to ensure that the reset
342 * has time to take effect and then complete. Since the datasheet does
343 * not currently specify the exact sequence, we have chosen something
344 * that seems to work with our device.
345 */
346static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
347{
348 ks8851_wrreg16(ks, KS_GRR, op);
349 mdelay(1); /* wait a short time to effect reset */
350 ks8851_wrreg16(ks, KS_GRR, 0);
351 mdelay(1); /* wait for condition to clear */
352}
353
354/**
Tristram Ha32f160d2011-11-21 08:57:59 +0000355 * ks8851_set_powermode - set power mode of the device
356 * @ks: The device state
357 * @pwrmode: The power mode value to write to KS_PMECR.
358 *
359 * Change the power mode of the chip.
360 */
361static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
362{
363 unsigned pmecr;
364
365 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
366
367 pmecr = ks8851_rdreg16(ks, KS_PMECR);
368 pmecr &= ~PMECR_PM_MASK;
369 pmecr |= pwrmode;
370
371 ks8851_wrreg16(ks, KS_PMECR, pmecr);
372}
373
374/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000375 * ks8851_write_mac_addr - write mac address to device registers
376 * @dev: The network device
377 *
378 * Update the KS8851 MAC address registers from the address in @dev.
379 *
380 * This call assumes that the chip is not running, so there is no need to
381 * shutdown the RXQ process whilst setting this.
382*/
383static int ks8851_write_mac_addr(struct net_device *dev)
384{
385 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000386 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000387
388 mutex_lock(&ks->lock);
389
Tristram Ha32f160d2011-11-21 08:57:59 +0000390 /*
391 * Wake up chip in case it was powered off when stopped; otherwise,
392 * the first write to the MAC address does not take effect.
393 */
394 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000395 for (i = 0; i < ETH_ALEN; i++)
396 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
Tristram Ha32f160d2011-11-21 08:57:59 +0000397 if (!netif_running(dev))
398 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000399
400 mutex_unlock(&ks->lock);
401
402 return 0;
403}
404
405/**
Ben Dooksa9a8de22011-11-21 08:57:58 +0000406 * ks8851_read_mac_addr - read mac address from device registers
407 * @dev: The network device
408 *
409 * Update our copy of the KS8851 MAC address from the registers of @dev.
410*/
411static void ks8851_read_mac_addr(struct net_device *dev)
412{
413 struct ks8851_net *ks = netdev_priv(dev);
414 int i;
415
416 mutex_lock(&ks->lock);
417
418 for (i = 0; i < ETH_ALEN; i++)
419 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
420
421 mutex_unlock(&ks->lock);
422}
423
424/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000425 * ks8851_init_mac - initialise the mac address
426 * @ks: The device structure
427 *
428 * Get or create the initial mac address for the device and then set that
Ben Dooksa9a8de22011-11-21 08:57:58 +0000429 * into the station address register. If there is an EEPROM present, then
Joe Perches7efd26d2012-07-12 19:33:06 +0000430 * we try that. If no valid mac address is found we use eth_random_addr()
Ben Dooks3ba81f32009-07-16 05:24:08 +0000431 * to create a new one.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000432 */
433static void ks8851_init_mac(struct ks8851_net *ks)
434{
435 struct net_device *dev = ks->netdev;
436
Ben Dooksa9a8de22011-11-21 08:57:58 +0000437 /* first, try reading what we've got already */
438 if (ks->rc_ccr & CCR_EEPROM) {
439 ks8851_read_mac_addr(dev);
440 if (is_valid_ether_addr(dev->dev_addr))
441 return;
442
443 netdev_err(ks->netdev, "invalid mac address read %pM\n",
444 dev->dev_addr);
445 }
446
Danny Kukawka7ce5d222012-02-15 06:45:40 +0000447 eth_hw_addr_random(dev);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000448 ks8851_write_mac_addr(dev);
449}
450
451/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000452 * ks8851_rdfifo - read data from the receive fifo
453 * @ks: The device state.
454 * @buff: The buffer address
455 * @len: The length of the data to read
456 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100457 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000458 * the FIFO into the buffer specified by @buff.
459 */
460static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
461{
462 struct spi_transfer *xfer = ks->spi_xfer2;
463 struct spi_message *msg = &ks->spi_msg2;
464 u8 txb[1];
465 int ret;
466
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000467 netif_dbg(ks, rx_status, ks->netdev,
468 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000469
470 /* set the operation we're issuing */
471 txb[0] = KS_SPIOP_RXFIFO;
472
473 xfer->tx_buf = txb;
474 xfer->rx_buf = NULL;
475 xfer->len = 1;
476
477 xfer++;
478 xfer->rx_buf = buff;
479 xfer->tx_buf = NULL;
480 xfer->len = len;
481
482 ret = spi_sync(ks->spidev, msg);
483 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000484 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000485}
486
487/**
488 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
489 * @ks: The device state
490 * @rxpkt: The data for the received packet
491 *
492 * Dump the initial data from the packet to dev_dbg().
493*/
494static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
495{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000496 netdev_dbg(ks->netdev,
497 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
498 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
499 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
500 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000501}
502
503/**
504 * ks8851_rx_pkts - receive packets from the host
505 * @ks: The device information.
506 *
507 * This is called from the IRQ work queue when the system detects that there
508 * are packets in the receive queue. Find out how many packets there are and
509 * read them from the FIFO.
510 */
511static void ks8851_rx_pkts(struct ks8851_net *ks)
512{
513 struct sk_buff *skb;
514 unsigned rxfc;
515 unsigned rxlen;
516 unsigned rxstat;
517 u32 rxh;
518 u8 *rxpkt;
519
520 rxfc = ks8851_rdreg8(ks, KS_RXFC);
521
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000522 netif_dbg(ks, rx_status, ks->netdev,
523 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000524
525 /* Currently we're issuing a read per packet, but we could possibly
526 * improve the code by issuing a single read, getting the receive
527 * header, allocating the packet and then reading the packet data
528 * out in one go.
529 *
530 * This form of operation would require us to hold the SPI bus'
531 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300532 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000533 */
534
535 for (; rxfc != 0; rxfc--) {
536 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
537 rxstat = rxh & 0xffff;
Max.Nekludov@us.elster.com14bc4352013-03-29 05:27:36 +0000538 rxlen = (rxh >> 16) & 0xfff;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000539
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000540 netif_dbg(ks, rx_status, ks->netdev,
541 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000542
543 /* the length of the packet includes the 32bit CRC */
544
545 /* set dma read address */
546 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
547
548 /* start the packet dma process, and set auto-dequeue rx */
549 ks8851_wrreg16(ks, KS_RXQCR,
550 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
551
Eric Dumazet972c40b2010-09-08 13:26:55 +0000552 if (rxlen > 4) {
553 unsigned int rxalign;
554
555 rxlen -= 4;
556 rxalign = ALIGN(rxlen, 4);
557 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
558 if (skb) {
559
560 /* 4 bytes of status header + 4 bytes of
561 * garbage: we put them before ethernet
562 * header, so that they are copied,
563 * but ignored.
564 */
565
566 rxpkt = skb_put(skb, rxlen) - 8;
567
568 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
569
570 if (netif_msg_pktdata(ks))
571 ks8851_dbg_dumpkkt(ks, rxpkt);
572
573 skb->protocol = eth_type_trans(skb, ks->netdev);
Cousson, Benoitfbcf88b2012-02-13 07:37:12 +0000574 netif_rx_ni(skb);
Eric Dumazet972c40b2010-09-08 13:26:55 +0000575
576 ks->netdev->stats.rx_packets++;
577 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000578 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000579 }
580
581 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
582 }
583}
584
585/**
Felipe Balbi656a05c2013-01-29 09:16:30 +0200586 * ks8851_irq - IRQ handler for dealing with interrupt requests
587 * @irq: IRQ number
588 * @_ks: cookie
Ben Dooks3ba81f32009-07-16 05:24:08 +0000589 *
Felipe Balbi656a05c2013-01-29 09:16:30 +0200590 * This handler is invoked when the IRQ line asserts to find out what happened.
591 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
592 * in thread context.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000593 *
594 * Read the interrupt status, work out what needs to be done and then clear
595 * any of the interrupts that are not needed.
596 */
Felipe Balbi656a05c2013-01-29 09:16:30 +0200597static irqreturn_t ks8851_irq(int irq, void *_ks)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000598{
Felipe Balbi656a05c2013-01-29 09:16:30 +0200599 struct ks8851_net *ks = _ks;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000600 unsigned status;
601 unsigned handled = 0;
602
603 mutex_lock(&ks->lock);
604
605 status = ks8851_rdreg16(ks, KS_ISR);
606
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000607 netif_dbg(ks, intr, ks->netdev,
608 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000609
Stephen Boyd062e55e2012-05-10 12:51:30 +0000610 if (status & IRQ_LCI)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000611 handled |= IRQ_LCI;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000612
613 if (status & IRQ_LDI) {
614 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
615 pmecr &= ~PMECR_WKEVT_MASK;
616 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
617
618 handled |= IRQ_LDI;
619 }
620
621 if (status & IRQ_RXPSI)
622 handled |= IRQ_RXPSI;
623
624 if (status & IRQ_TXI) {
625 handled |= IRQ_TXI;
626
627 /* no lock here, tx queue should have been stopped */
628
629 /* update our idea of how much tx space is available to the
630 * system */
631 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
632
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000633 netif_dbg(ks, intr, ks->netdev,
634 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000635 }
636
637 if (status & IRQ_RXI)
638 handled |= IRQ_RXI;
639
640 if (status & IRQ_SPIBEI) {
641 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
642 handled |= IRQ_SPIBEI;
643 }
644
645 ks8851_wrreg16(ks, KS_ISR, handled);
646
647 if (status & IRQ_RXI) {
648 /* the datasheet says to disable the rx interrupt during
649 * packet read-out, however we're masking the interrupt
650 * from the device so do not bother masking just the RX
651 * from the device. */
652
653 ks8851_rx_pkts(ks);
654 }
655
656 /* if something stopped the rx process, probably due to wanting
657 * to change the rx settings, then do something about restarting
658 * it. */
659 if (status & IRQ_RXPSI) {
660 struct ks8851_rxctrl *rxc = &ks->rxctrl;
661
662 /* update the multicast hash table */
663 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
664 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
665 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
666 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
667
668 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
669 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
670 }
671
672 mutex_unlock(&ks->lock);
673
Stephen Boyd062e55e2012-05-10 12:51:30 +0000674 if (status & IRQ_LCI)
675 mii_check_link(&ks->mii);
676
Ben Dooks3ba81f32009-07-16 05:24:08 +0000677 if (status & IRQ_TXI)
678 netif_wake_queue(ks->netdev);
679
Felipe Balbi656a05c2013-01-29 09:16:30 +0200680 return IRQ_HANDLED;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000681}
682
683/**
684 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300685 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000686 *
687 * Returns the size of the TXFIFO message needed to send
688 * this packet.
689 */
690static inline unsigned calc_txlen(unsigned len)
691{
692 return ALIGN(len + 4, 4);
693}
694
695/**
696 * ks8851_wrpkt - write packet to TX FIFO
697 * @ks: The device state.
698 * @txp: The sk_buff to transmit.
699 * @irq: IRQ on completion of the packet.
700 *
701 * Send the @txp to the chip. This means creating the relevant packet header
702 * specifying the length of the packet and the other information the chip
703 * needs, such as IRQ on completion. Send the header and the packet data to
704 * the device.
705 */
706static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
707{
708 struct spi_transfer *xfer = ks->spi_xfer2;
709 struct spi_message *msg = &ks->spi_msg2;
710 unsigned fid = 0;
711 int ret;
712
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000713 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
714 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000715
716 fid = ks->fid++;
717 fid &= TXFR_TXFID_MASK;
718
719 if (irq)
720 fid |= TXFR_TXIC; /* irq on completion */
721
722 /* start header at txb[1] to align txw entries */
723 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
724 ks->txh.txw[1] = cpu_to_le16(fid);
725 ks->txh.txw[2] = cpu_to_le16(txp->len);
726
727 xfer->tx_buf = &ks->txh.txb[1];
728 xfer->rx_buf = NULL;
729 xfer->len = 5;
730
731 xfer++;
732 xfer->tx_buf = txp->data;
733 xfer->rx_buf = NULL;
734 xfer->len = ALIGN(txp->len, 4);
735
736 ret = spi_sync(ks->spidev, msg);
737 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000738 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000739}
740
741/**
742 * ks8851_done_tx - update and then free skbuff after transmitting
743 * @ks: The device state
744 * @txb: The buffer transmitted
745 */
746static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
747{
748 struct net_device *dev = ks->netdev;
749
750 dev->stats.tx_bytes += txb->len;
751 dev->stats.tx_packets++;
752
753 dev_kfree_skb(txb);
754}
755
756/**
757 * ks8851_tx_work - process tx packet(s)
758 * @work: The work strucutre what was scheduled.
759 *
760 * This is called when a number of packets have been scheduled for
761 * transmission and need to be sent to the device.
762 */
763static void ks8851_tx_work(struct work_struct *work)
764{
765 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
766 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000767 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000768
769 mutex_lock(&ks->lock);
770
771 while (!last) {
772 txb = skb_dequeue(&ks->txq);
773 last = skb_queue_empty(&ks->txq);
774
Abraham Arce761172f2010-04-16 14:48:43 +0000775 if (txb != NULL) {
776 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
777 ks8851_wrpkt(ks, txb, last);
778 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
779 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000780
Abraham Arce761172f2010-04-16 14:48:43 +0000781 ks8851_done_tx(ks, txb);
782 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000783 }
784
785 mutex_unlock(&ks->lock);
786}
787
788/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000789 * ks8851_net_open - open network device
790 * @dev: The network device being opened.
791 *
792 * Called when the network device is marked active, such as a user executing
793 * 'ifconfig up' on the device.
794 */
795static int ks8851_net_open(struct net_device *dev)
796{
797 struct ks8851_net *ks = netdev_priv(dev);
798
799 /* lock the card, even if we may not actually be doing anything
800 * else at the moment */
801 mutex_lock(&ks->lock);
802
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000803 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000804
805 /* bring chip out of any power saving mode it was in */
806 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
807
808 /* issue a soft reset to the RX/TX QMU to put it into a known
809 * state. */
810 ks8851_soft_reset(ks, GRR_QMU);
811
812 /* setup transmission parameters */
813
814 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
815 TXCR_TXPE | /* pad to min length */
816 TXCR_TXCRC | /* add CRC */
817 TXCR_TXFCE)); /* enable flow control */
818
819 /* auto-increment tx data, reset tx pointer */
820 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
821
822 /* setup receiver control */
823
824 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
825 RXCR1_RXFCE | /* enable flow control */
826 RXCR1_RXBE | /* broadcast enable */
827 RXCR1_RXUE | /* unicast enable */
828 RXCR1_RXE)); /* enable rx block */
829
830 /* transfer entire frames out in one go */
831 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
832
833 /* set receive counter timeouts */
834 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
835 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
836 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
837
838 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
839 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
840 RXQCR_RXDTTE); /* IRQ on time exceeded */
841
842 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
843
844 /* clear then enable interrupts */
845
846#define STD_IRQ (IRQ_LCI | /* Link Change */ \
847 IRQ_TXI | /* TX done */ \
848 IRQ_RXI | /* RX done */ \
849 IRQ_SPIBEI | /* SPI bus error */ \
850 IRQ_TXPSI | /* TX process stop */ \
851 IRQ_RXPSI) /* RX process stop */
852
853 ks->rc_ier = STD_IRQ;
854 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
855 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
856
857 netif_start_queue(ks->netdev);
858
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000859 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000860
861 mutex_unlock(&ks->lock);
862 return 0;
863}
864
865/**
866 * ks8851_net_stop - close network device
867 * @dev: The device being closed.
868 *
869 * Called to close down a network device which has been active. Cancell any
870 * work, shutdown the RX and TX process and then place the chip into a low
871 * power state whilst it is not being used.
872 */
873static int ks8851_net_stop(struct net_device *dev)
874{
875 struct ks8851_net *ks = netdev_priv(dev);
876
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000877 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000878
879 netif_stop_queue(dev);
880
881 mutex_lock(&ks->lock);
Stephen Boydc5a99932012-04-18 17:25:58 +0000882 /* turn off the IRQs and ack any outstanding */
883 ks8851_wrreg16(ks, KS_IER, 0x0000);
884 ks8851_wrreg16(ks, KS_ISR, 0xffff);
885 mutex_unlock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000886
887 /* stop any outstanding work */
Ben Dooks3ba81f32009-07-16 05:24:08 +0000888 flush_work(&ks->tx_work);
889 flush_work(&ks->rxctrl_work);
890
Stephen Boydc5a99932012-04-18 17:25:58 +0000891 mutex_lock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000892 /* shutdown RX process */
893 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
894
895 /* shutdown TX process */
896 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
897
898 /* set powermode to soft power down to save power */
899 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Stephen Boydc5a99932012-04-18 17:25:58 +0000900 mutex_unlock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000901
902 /* ensure any queued tx buffers are dumped */
903 while (!skb_queue_empty(&ks->txq)) {
904 struct sk_buff *txb = skb_dequeue(&ks->txq);
905
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000906 netif_dbg(ks, ifdown, ks->netdev,
907 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000908
909 dev_kfree_skb(txb);
910 }
911
Ben Dooks3ba81f32009-07-16 05:24:08 +0000912 return 0;
913}
914
915/**
916 * ks8851_start_xmit - transmit packet
917 * @skb: The buffer to transmit
918 * @dev: The device used to transmit the packet.
919 *
920 * Called by the network layer to transmit the @skb. Queue the packet for
921 * the device and schedule the necessary work to transmit the packet when
922 * it is free.
923 *
924 * We do this to firstly avoid sleeping with the network device locked,
925 * and secondly so we can round up more than one packet to transmit which
926 * means we can try and avoid generating too many transmit done interrupts.
927 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000928static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
929 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000930{
931 struct ks8851_net *ks = netdev_priv(dev);
932 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000933 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000934
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000935 netif_dbg(ks, tx_queued, ks->netdev,
936 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000937
938 spin_lock(&ks->statelock);
939
940 if (needed > ks->tx_space) {
941 netif_stop_queue(dev);
942 ret = NETDEV_TX_BUSY;
943 } else {
944 ks->tx_space -= needed;
945 skb_queue_tail(&ks->txq, skb);
946 }
947
948 spin_unlock(&ks->statelock);
949 schedule_work(&ks->tx_work);
950
951 return ret;
952}
953
954/**
955 * ks8851_rxctrl_work - work handler to change rx mode
956 * @work: The work structure this belongs to.
957 *
958 * Lock the device and issue the necessary changes to the receive mode from
959 * the network device layer. This is done so that we can do this without
960 * having to sleep whilst holding the network device lock.
961 *
962 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
963 * receive parameters are programmed, we issue a write to disable the RXQ and
964 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
965 * complete. The interrupt handler then writes the new values into the chip.
966 */
967static void ks8851_rxctrl_work(struct work_struct *work)
968{
969 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
970
971 mutex_lock(&ks->lock);
972
973 /* need to shutdown RXQ before modifying filter parameters */
974 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
975
976 mutex_unlock(&ks->lock);
977}
978
979static void ks8851_set_rx_mode(struct net_device *dev)
980{
981 struct ks8851_net *ks = netdev_priv(dev);
982 struct ks8851_rxctrl rxctrl;
983
984 memset(&rxctrl, 0, sizeof(rxctrl));
985
986 if (dev->flags & IFF_PROMISC) {
987 /* interface to receive everything */
988
989 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
990 } else if (dev->flags & IFF_ALLMULTI) {
991 /* accept all multicast packets */
992
993 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
994 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000995 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000996 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000997 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000998
999 /* accept some multicast */
1000
Jiri Pirko22bedad32010-04-01 21:22:57 +00001001 netdev_for_each_mc_addr(ha, dev) {
1002 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001003 crc >>= (32 - 6); /* get top six bits */
1004
1005 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001006 }
1007
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001008 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001009 } else {
1010 /* just accept broadcast / unicast */
1011 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1012 }
1013
1014 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1015 RXCR1_RXBE | /* broadcast enable */
1016 RXCR1_RXE | /* RX process enable */
1017 RXCR1_RXFCE); /* enable flow control */
1018
1019 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1020
1021 /* schedule work to do the actual set of the data if needed */
1022
1023 spin_lock(&ks->statelock);
1024
1025 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1026 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1027 schedule_work(&ks->rxctrl_work);
1028 }
1029
1030 spin_unlock(&ks->statelock);
1031}
1032
1033static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1034{
1035 struct sockaddr *sa = addr;
1036
1037 if (netif_running(dev))
1038 return -EBUSY;
1039
1040 if (!is_valid_ether_addr(sa->sa_data))
1041 return -EADDRNOTAVAIL;
1042
1043 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1044 return ks8851_write_mac_addr(dev);
1045}
1046
1047static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1048{
1049 struct ks8851_net *ks = netdev_priv(dev);
1050
1051 if (!netif_running(dev))
1052 return -EINVAL;
1053
1054 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1055}
1056
1057static const struct net_device_ops ks8851_netdev_ops = {
1058 .ndo_open = ks8851_net_open,
1059 .ndo_stop = ks8851_net_stop,
1060 .ndo_do_ioctl = ks8851_net_ioctl,
1061 .ndo_start_xmit = ks8851_start_xmit,
1062 .ndo_set_mac_address = ks8851_set_mac_address,
1063 .ndo_set_rx_mode = ks8851_set_rx_mode,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001064 .ndo_validate_addr = eth_validate_addr,
1065};
1066
1067/* ethtool support */
1068
1069static void ks8851_get_drvinfo(struct net_device *dev,
1070 struct ethtool_drvinfo *di)
1071{
1072 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1073 strlcpy(di->version, "1.00", sizeof(di->version));
1074 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1075}
1076
1077static u32 ks8851_get_msglevel(struct net_device *dev)
1078{
1079 struct ks8851_net *ks = netdev_priv(dev);
1080 return ks->msg_enable;
1081}
1082
1083static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1084{
1085 struct ks8851_net *ks = netdev_priv(dev);
1086 ks->msg_enable = to;
1087}
1088
Philippe Reynes98f2b092017-02-09 09:57:47 +01001089static int ks8851_get_link_ksettings(struct net_device *dev,
1090 struct ethtool_link_ksettings *cmd)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001091{
1092 struct ks8851_net *ks = netdev_priv(dev);
Philippe Reynes98f2b092017-02-09 09:57:47 +01001093 return mii_ethtool_get_link_ksettings(&ks->mii, cmd);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001094}
1095
Philippe Reynes98f2b092017-02-09 09:57:47 +01001096static int ks8851_set_link_ksettings(struct net_device *dev,
1097 const struct ethtool_link_ksettings *cmd)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001098{
1099 struct ks8851_net *ks = netdev_priv(dev);
Philippe Reynes98f2b092017-02-09 09:57:47 +01001100 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001101}
1102
1103static u32 ks8851_get_link(struct net_device *dev)
1104{
1105 struct ks8851_net *ks = netdev_priv(dev);
1106 return mii_link_ok(&ks->mii);
1107}
1108
1109static int ks8851_nway_reset(struct net_device *dev)
1110{
1111 struct ks8851_net *ks = netdev_priv(dev);
1112 return mii_nway_restart(&ks->mii);
1113}
1114
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001115/* EEPROM support */
1116
1117static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1118{
1119 struct ks8851_net *ks = ee->data;
1120 unsigned val;
1121
1122 val = ks8851_rdreg16(ks, KS_EEPCR);
1123
1124 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1125 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1126 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1127}
1128
1129static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1130{
1131 struct ks8851_net *ks = ee->data;
1132 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1133
1134 if (ee->drive_data)
1135 val |= EEPCR_EESRWA;
1136 if (ee->reg_data_in)
1137 val |= EEPCR_EEDO;
1138 if (ee->reg_data_clock)
1139 val |= EEPCR_EESCK;
1140 if (ee->reg_chip_select)
1141 val |= EEPCR_EECS;
1142
1143 ks8851_wrreg16(ks, KS_EEPCR, val);
1144}
1145
1146/**
1147 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1148 * @ks: The network device state.
1149 *
1150 * Check for the presence of an EEPROM, and then activate software access
1151 * to the device.
1152 */
1153static int ks8851_eeprom_claim(struct ks8851_net *ks)
1154{
1155 if (!(ks->rc_ccr & CCR_EEPROM))
1156 return -ENOENT;
1157
1158 mutex_lock(&ks->lock);
1159
1160 /* start with clock low, cs high */
1161 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1162 return 0;
1163}
1164
1165/**
1166 * ks8851_eeprom_release - release the EEPROM interface
1167 * @ks: The device state
1168 *
1169 * Release the software access to the device EEPROM
1170 */
1171static void ks8851_eeprom_release(struct ks8851_net *ks)
1172{
1173 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1174
1175 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1176 mutex_unlock(&ks->lock);
1177}
1178
1179#define KS_EEPROM_MAGIC (0x00008851)
1180
1181static int ks8851_set_eeprom(struct net_device *dev,
1182 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001183{
1184 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001185 int offset = ee->offset;
1186 int len = ee->len;
1187 u16 tmp;
1188
1189 /* currently only support byte writing */
1190 if (len != 1)
1191 return -EINVAL;
1192
1193 if (ee->magic != KS_EEPROM_MAGIC)
1194 return -EINVAL;
1195
1196 if (ks8851_eeprom_claim(ks))
1197 return -ENOENT;
1198
1199 eeprom_93cx6_wren(&ks->eeprom, true);
1200
1201 /* ethtool currently only supports writing bytes, which means
1202 * we have to read/modify/write our 16bit EEPROMs */
1203
1204 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1205
1206 if (offset & 1) {
1207 tmp &= 0xff;
1208 tmp |= *data << 8;
1209 } else {
1210 tmp &= 0xff00;
1211 tmp |= *data;
1212 }
1213
1214 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1215 eeprom_93cx6_wren(&ks->eeprom, false);
1216
1217 ks8851_eeprom_release(ks);
1218
1219 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001220}
1221
1222static int ks8851_get_eeprom(struct net_device *dev,
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001223 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001224{
1225 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001226 int offset = ee->offset;
1227 int len = ee->len;
Sebastien Jana84afa42010-05-05 08:45:54 +00001228
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001229 /* must be 2 byte aligned */
1230 if (len & 1 || offset & 1)
Sebastien Jana84afa42010-05-05 08:45:54 +00001231 return -EINVAL;
1232
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001233 if (ks8851_eeprom_claim(ks))
1234 return -ENOENT;
Sebastien Jana84afa42010-05-05 08:45:54 +00001235
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001236 ee->magic = KS_EEPROM_MAGIC;
Sebastien Jana84afa42010-05-05 08:45:54 +00001237
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001238 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1239 ks8851_eeprom_release(ks);
Sebastien Jana84afa42010-05-05 08:45:54 +00001240
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001241 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001242}
1243
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001244static int ks8851_get_eeprom_len(struct net_device *dev)
Sebastien Jana84afa42010-05-05 08:45:54 +00001245{
1246 struct ks8851_net *ks = netdev_priv(dev);
Sebastien Jana84afa42010-05-05 08:45:54 +00001247
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001248 /* currently, we assume it is an 93C46 attached, so return 128 */
1249 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001250}
1251
Ben Dooks3ba81f32009-07-16 05:24:08 +00001252static const struct ethtool_ops ks8851_ethtool_ops = {
1253 .get_drvinfo = ks8851_get_drvinfo,
1254 .get_msglevel = ks8851_get_msglevel,
1255 .set_msglevel = ks8851_set_msglevel,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001256 .get_link = ks8851_get_link,
1257 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001258 .get_eeprom_len = ks8851_get_eeprom_len,
1259 .get_eeprom = ks8851_get_eeprom,
1260 .set_eeprom = ks8851_set_eeprom,
Philippe Reynes98f2b092017-02-09 09:57:47 +01001261 .get_link_ksettings = ks8851_get_link_ksettings,
1262 .set_link_ksettings = ks8851_set_link_ksettings,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001263};
1264
1265/* MII interface controls */
1266
1267/**
1268 * ks8851_phy_reg - convert MII register into a KS8851 register
1269 * @reg: MII register number.
1270 *
1271 * Return the KS8851 register number for the corresponding MII PHY register
1272 * if possible. Return zero if the MII register has no direct mapping to the
1273 * KS8851 register set.
1274 */
1275static int ks8851_phy_reg(int reg)
1276{
1277 switch (reg) {
1278 case MII_BMCR:
1279 return KS_P1MBCR;
1280 case MII_BMSR:
1281 return KS_P1MBSR;
1282 case MII_PHYSID1:
1283 return KS_PHY1ILR;
1284 case MII_PHYSID2:
1285 return KS_PHY1IHR;
1286 case MII_ADVERTISE:
1287 return KS_P1ANAR;
1288 case MII_LPA:
1289 return KS_P1ANLPR;
1290 }
1291
1292 return 0x0;
1293}
1294
1295/**
1296 * ks8851_phy_read - MII interface PHY register read.
1297 * @dev: The network device the PHY is on.
1298 * @phy_addr: Address of PHY (ignored as we only have one)
1299 * @reg: The register to read.
1300 *
1301 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001302 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001303 * are always returned as zero.
1304 *
1305 * We return zero for unsupported registers as the MII code does not check
1306 * the value returned for any error status, and simply returns it to the
1307 * caller. The mii-tool that the driver was tested with takes any -ve error
1308 * as real PHY capabilities, thus displaying incorrect data to the user.
1309 */
1310static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1311{
1312 struct ks8851_net *ks = netdev_priv(dev);
1313 int ksreg;
1314 int result;
1315
1316 ksreg = ks8851_phy_reg(reg);
1317 if (!ksreg)
1318 return 0x0; /* no error return allowed, so use zero */
1319
1320 mutex_lock(&ks->lock);
1321 result = ks8851_rdreg16(ks, ksreg);
1322 mutex_unlock(&ks->lock);
1323
1324 return result;
1325}
1326
1327static void ks8851_phy_write(struct net_device *dev,
1328 int phy, int reg, int value)
1329{
1330 struct ks8851_net *ks = netdev_priv(dev);
1331 int ksreg;
1332
1333 ksreg = ks8851_phy_reg(reg);
1334 if (ksreg) {
1335 mutex_lock(&ks->lock);
1336 ks8851_wrreg16(ks, ksreg, value);
1337 mutex_unlock(&ks->lock);
1338 }
1339}
1340
1341/**
1342 * ks8851_read_selftest - read the selftest memory info.
1343 * @ks: The device state
1344 *
1345 * Read and check the TX/RX memory selftest information.
1346 */
1347static int ks8851_read_selftest(struct ks8851_net *ks)
1348{
1349 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1350 int ret = 0;
1351 unsigned rd;
1352
1353 rd = ks8851_rdreg16(ks, KS_MBIR);
1354
1355 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001356 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001357 return 0;
1358 }
1359
1360 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001361 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001362 ret |= 1;
1363 }
1364
1365 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001366 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001367 ret |= 2;
1368 }
1369
1370 return 0;
1371}
1372
1373/* driver bus management functions */
1374
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001375#ifdef CONFIG_PM_SLEEP
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001376
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001377static int ks8851_suspend(struct device *dev)
1378{
1379 struct ks8851_net *ks = dev_get_drvdata(dev);
1380 struct net_device *netdev = ks->netdev;
1381
1382 if (netif_running(netdev)) {
1383 netif_device_detach(netdev);
1384 ks8851_net_stop(netdev);
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001385 }
1386
1387 return 0;
1388}
1389
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001390static int ks8851_resume(struct device *dev)
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001391{
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001392 struct ks8851_net *ks = dev_get_drvdata(dev);
1393 struct net_device *netdev = ks->netdev;
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001394
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001395 if (netif_running(netdev)) {
1396 ks8851_net_open(netdev);
1397 netif_device_attach(netdev);
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001398 }
1399
1400 return 0;
1401}
Fabio Estevam8ac2b3c2013-04-16 09:28:31 +00001402#endif
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001403
1404static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001405
Bill Pemberton654b8c52012-12-03 09:23:59 -05001406static int ks8851_probe(struct spi_device *spi)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001407{
1408 struct net_device *ndev;
1409 struct ks8851_net *ks;
1410 int ret;
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001411 unsigned cider;
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001412 int gpio;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001413
1414 ndev = alloc_etherdev(sizeof(struct ks8851_net));
Joe Perches41de8d42012-01-29 13:47:52 +00001415 if (!ndev)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001416 return -ENOMEM;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001417
1418 spi->bits_per_word = 8;
1419
1420 ks = netdev_priv(ndev);
1421
1422 ks->netdev = ndev;
1423 ks->spidev = spi;
1424 ks->tx_space = 6144;
1425
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001426 gpio = of_get_named_gpio_flags(spi->dev.of_node, "reset-gpios",
1427 0, NULL);
1428 if (gpio == -EPROBE_DEFER) {
1429 ret = gpio;
1430 goto err_gpio;
1431 }
1432
1433 ks->gpio = gpio;
1434 if (gpio_is_valid(gpio)) {
1435 ret = devm_gpio_request_one(&spi->dev, gpio,
1436 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1437 if (ret) {
1438 dev_err(&spi->dev, "reset gpio request failed\n");
1439 goto err_gpio;
1440 }
1441 }
1442
Stephen Boydd64eed12014-05-28 13:11:12 -07001443 ks->vdd_io = devm_regulator_get(&spi->dev, "vdd-io");
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001444 if (IS_ERR(ks->vdd_io)) {
1445 ret = PTR_ERR(ks->vdd_io);
Stephen Boydd64eed12014-05-28 13:11:12 -07001446 goto err_reg_io;
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001447 }
1448
Stephen Boydd64eed12014-05-28 13:11:12 -07001449 ret = regulator_enable(ks->vdd_io);
1450 if (ret) {
1451 dev_err(&spi->dev, "regulator vdd_io enable fail: %d\n",
1452 ret);
1453 goto err_reg_io;
1454 }
1455
1456 ks->vdd_reg = devm_regulator_get(&spi->dev, "vdd");
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001457 if (IS_ERR(ks->vdd_reg)) {
1458 ret = PTR_ERR(ks->vdd_reg);
Stephen Boydd64eed12014-05-28 13:11:12 -07001459 goto err_reg;
1460 }
1461
1462 ret = regulator_enable(ks->vdd_reg);
1463 if (ret) {
1464 dev_err(&spi->dev, "regulator vdd enable fail: %d\n",
1465 ret);
1466 goto err_reg;
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001467 }
1468
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001469 if (gpio_is_valid(gpio)) {
1470 usleep_range(10000, 11000);
1471 gpio_set_value(gpio, 1);
1472 }
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001473
Ben Dooks3ba81f32009-07-16 05:24:08 +00001474 mutex_init(&ks->lock);
1475 spin_lock_init(&ks->statelock);
1476
1477 INIT_WORK(&ks->tx_work, ks8851_tx_work);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001478 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1479
1480 /* initialise pre-made spi transfer messages */
1481
1482 spi_message_init(&ks->spi_msg1);
1483 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1484
1485 spi_message_init(&ks->spi_msg2);
1486 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1487 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1488
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001489 /* setup EEPROM state */
1490
1491 ks->eeprom.data = ks;
1492 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1493 ks->eeprom.register_read = ks8851_eeprom_regread;
1494 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1495
Ben Dooks3ba81f32009-07-16 05:24:08 +00001496 /* setup mii state */
1497 ks->mii.dev = ndev;
1498 ks->mii.phy_id = 1,
1499 ks->mii.phy_id_mask = 1;
1500 ks->mii.reg_num_mask = 0xf;
1501 ks->mii.mdio_read = ks8851_phy_read;
1502 ks->mii.mdio_write = ks8851_phy_write;
1503
1504 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1505
1506 /* set the default message enable */
1507 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1508 NETIF_MSG_PROBE |
1509 NETIF_MSG_LINK));
1510
1511 skb_queue_head_init(&ks->txq);
1512
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001513 ndev->ethtool_ops = &ks8851_ethtool_ops;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001514 SET_NETDEV_DEV(ndev, &spi->dev);
1515
Jingoo Han8f996602013-04-05 20:35:43 +00001516 spi_set_drvdata(spi, ks);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001517
1518 ndev->if_port = IF_PORT_100BASET;
1519 ndev->netdev_ops = &ks8851_netdev_ops;
1520 ndev->irq = spi->irq;
1521
Ben Dooks57dada62009-10-19 23:49:03 +00001522 /* issue a global soft reset to reset the device. */
1523 ks8851_soft_reset(ks, GRR_GSR);
1524
Ben Dooks3ba81f32009-07-16 05:24:08 +00001525 /* simple check for a valid chip being connected to the bus */
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001526 cider = ks8851_rdreg16(ks, KS_CIDER);
1527 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
Ben Dooks3ba81f32009-07-16 05:24:08 +00001528 dev_err(&spi->dev, "failed to read device ID\n");
1529 ret = -ENODEV;
1530 goto err_id;
1531 }
1532
Sebastien Jan7d997462010-05-05 08:45:52 +00001533 /* cache the contents of the CCR register for EEPROM, etc. */
1534 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1535
Ben Dooks3ba81f32009-07-16 05:24:08 +00001536 ks8851_read_selftest(ks);
1537 ks8851_init_mac(ks);
1538
Felipe Balbi656a05c2013-01-29 09:16:30 +02001539 ret = request_threaded_irq(spi->irq, NULL, ks8851_irq,
1540 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1541 ndev->name, ks);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001542 if (ret < 0) {
1543 dev_err(&spi->dev, "failed to get irq\n");
1544 goto err_irq;
1545 }
1546
1547 ret = register_netdev(ndev);
1548 if (ret) {
1549 dev_err(&spi->dev, "failed to register network device\n");
1550 goto err_netdev;
1551 }
1552
Ben Dooksa9a8de22011-11-21 08:57:58 +00001553 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001554 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
Ben Dooksa9a8de22011-11-21 08:57:58 +00001555 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001556
1557 return 0;
1558
1559
1560err_netdev:
Matt Renzelmanne8195b22012-04-19 07:17:17 +00001561 free_irq(ndev->irq, ks);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001562
Ben Dooks3ba81f32009-07-16 05:24:08 +00001563err_irq:
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001564 if (gpio_is_valid(gpio))
1565 gpio_set_value(gpio, 0);
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001566err_id:
Stephen Boydd64eed12014-05-28 13:11:12 -07001567 regulator_disable(ks->vdd_reg);
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001568err_reg:
Stephen Boydd64eed12014-05-28 13:11:12 -07001569 regulator_disable(ks->vdd_io);
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001570err_reg_io:
1571err_gpio:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001572 free_netdev(ndev);
1573 return ret;
1574}
1575
Bill Pemberton654b8c52012-12-03 09:23:59 -05001576static int ks8851_remove(struct spi_device *spi)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001577{
Jingoo Han8f996602013-04-05 20:35:43 +00001578 struct ks8851_net *priv = spi_get_drvdata(spi);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001579
1580 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001581 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001582
1583 unregister_netdev(priv->netdev);
1584 free_irq(spi->irq, priv);
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001585 if (gpio_is_valid(priv->gpio))
1586 gpio_set_value(priv->gpio, 0);
Stephen Boydd64eed12014-05-28 13:11:12 -07001587 regulator_disable(priv->vdd_reg);
1588 regulator_disable(priv->vdd_io);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001589 free_netdev(priv->netdev);
1590
1591 return 0;
1592}
1593
Stephen Boydf4c6e062014-05-23 12:57:20 -07001594static const struct of_device_id ks8851_match_table[] = {
1595 { .compatible = "micrel,ks8851" },
1596 { }
1597};
Javier Martinez Canillas88c79662015-09-16 11:11:22 +02001598MODULE_DEVICE_TABLE(of, ks8851_match_table);
Stephen Boydf4c6e062014-05-23 12:57:20 -07001599
Ben Dooks3ba81f32009-07-16 05:24:08 +00001600static struct spi_driver ks8851_driver = {
1601 .driver = {
1602 .name = "ks8851",
Stephen Boydf4c6e062014-05-23 12:57:20 -07001603 .of_match_table = ks8851_match_table,
Fabio Estevam8ac2b3c2013-04-16 09:28:31 +00001604 .pm = &ks8851_pm_ops,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001605 },
1606 .probe = ks8851_probe,
Bill Pemberton654b8c52012-12-03 09:23:59 -05001607 .remove = ks8851_remove,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001608};
Lars-Peter Clausen0582ce92013-04-06 23:33:03 +00001609module_spi_driver(ks8851_driver);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001610
1611MODULE_DESCRIPTION("KS8851 Network driver");
1612MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1613MODULE_LICENSE("GPL");
1614
1615module_param_named(message, msg_enable, int, 0);
1616MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001617MODULE_ALIAS("spi:ks8851");