blob: 33305c9c5a62df45a5c582c6baf320cc81b4bb0f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Paul Gortmaker3396c782012-01-27 13:36:01 +00002/* drivers/net/ethernet/micrel/ks8851.c
Ben Dooks3ba81f32009-07-16 05:24:08 +00003 *
4 * Copyright 2009 Simtec Electronics
5 * http://www.simtec.co.uk/
6 * Ben Dooks <ben@simtec.co.uk>
Ben Dooks3ba81f32009-07-16 05:24:08 +00007 */
8
Joe Perches0dc7d2b2010-02-27 14:43:51 +00009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Ben Dooks3ba81f32009-07-16 05:24:08 +000011#define DEBUG
12
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000013#include <linux/interrupt.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/ethtool.h>
19#include <linux/cache.h>
20#include <linux/crc32.h>
21#include <linux/mii.h>
Ben Dooks51b7b1c2011-11-21 08:58:00 +000022#include <linux/eeprom_93cx6.h>
Nishanth Menonebf4ad92014-03-21 01:52:48 -050023#include <linux/regulator/consumer.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000024
25#include <linux/spi/spi.h>
Stephen Boyd73fdeb82014-05-23 12:57:19 -070026#include <linux/gpio.h>
27#include <linux/of_gpio.h>
Lukas Wunner566bd542017-12-18 11:17:07 +010028#include <linux/of_net.h>
Ben Dooks3ba81f32009-07-16 05:24:08 +000029
30#include "ks8851.h"
31
32/**
33 * struct ks8851_rxctrl - KS8851 driver rx control
34 * @mchash: Multicast hash-table data.
35 * @rxcr1: KS_RXCR1 register setting
36 * @rxcr2: KS_RXCR2 register setting
37 *
38 * Representation of the settings needs to control the receive filtering
39 * such as the multicast hash-filter and the receive register settings. This
40 * is used to make the job of working out if the receive settings change and
41 * then issuing the new settings to the worker that will send the necessary
42 * commands.
43 */
44struct ks8851_rxctrl {
45 u16 mchash[4];
46 u16 rxcr1;
47 u16 rxcr2;
48};
49
50/**
51 * union ks8851_tx_hdr - tx header data
52 * @txb: The header as bytes
53 * @txw: The header as 16bit, little-endian words
54 *
55 * A dual representation of the tx header data to allow
56 * access to individual bytes, and to allow 16bit accesses
57 * with 16bit alignment.
58 */
59union ks8851_tx_hdr {
60 u8 txb[6];
61 __le16 txw[3];
62};
63
64/**
65 * struct ks8851_net - KS8851 driver private data
66 * @netdev: The network device we're bound to
67 * @spidev: The spi device we're bound to.
68 * @lock: Lock to ensure that the device is not accessed when busy.
69 * @statelock: Lock on this structure for tx list.
70 * @mii: The MII state information for the mii calls.
71 * @rxctrl: RX settings for @rxctrl_work.
72 * @tx_work: Work queue for tx packets
Ben Dooks3ba81f32009-07-16 05:24:08 +000073 * @rxctrl_work: Work queue for updating RX mode and multicast lists
74 * @txq: Queue of packets for transmission.
75 * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
76 * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
77 * @txh: Space for generating packet TX header in DMA-able data
78 * @rxd: Space for receiving SPI data, in DMA-able space.
79 * @txd: Space for transmitting SPI data, in DMA-able space.
80 * @msg_enable: The message flags controlling driver output (see ethtool).
81 * @fid: Incrementing frame id tag.
82 * @rc_ier: Cached copy of KS_IER.
Sebastien Jan7d997462010-05-05 08:45:52 +000083 * @rc_ccr: Cached copy of KS_CCR.
Ben Dooks3ba81f32009-07-16 05:24:08 +000084 * @rc_rxqcr: Cached copy of KS_RXQCR.
Ben Dooks51b7b1c2011-11-21 08:58:00 +000085 * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
Nishanth Menonebf4ad92014-03-21 01:52:48 -050086 * @vdd_reg: Optional regulator supplying the chip
Stephen Boyd73fdeb82014-05-23 12:57:19 -070087 * @vdd_io: Optional digital power supply for IO
88 * @gpio: Optional reset_n gpio
Ben Dooks3ba81f32009-07-16 05:24:08 +000089 *
90 * The @lock ensures that the chip is protected when certain operations are
91 * in progress. When the read or write packet transfer is in progress, most
92 * of the chip registers are not ccessible until the transfer is finished and
93 * the DMA has been de-asserted.
94 *
95 * The @statelock is used to protect information in the structure which may
96 * need to be accessed via several sources, such as the network driver layer
97 * or one of the work queues.
98 *
99 * We align the buffers we may use for rx/tx to ensure that if the SPI driver
100 * wants to DMA map them, it will not have any problems with data the driver
101 * modifies.
102 */
103struct ks8851_net {
104 struct net_device *netdev;
105 struct spi_device *spidev;
106 struct mutex lock;
107 spinlock_t statelock;
108
109 union ks8851_tx_hdr txh ____cacheline_aligned;
110 u8 rxd[8];
111 u8 txd[8];
112
113 u32 msg_enable ____cacheline_aligned;
114 u16 tx_space;
115 u8 fid;
116
117 u16 rc_ier;
118 u16 rc_rxqcr;
Sebastien Jan7d997462010-05-05 08:45:52 +0000119 u16 rc_ccr;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000120
121 struct mii_if_info mii;
122 struct ks8851_rxctrl rxctrl;
123
124 struct work_struct tx_work;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000125 struct work_struct rxctrl_work;
126
127 struct sk_buff_head txq;
128
129 struct spi_message spi_msg1;
130 struct spi_message spi_msg2;
131 struct spi_transfer spi_xfer1;
132 struct spi_transfer spi_xfer2[2];
Ben Dooks51b7b1c2011-11-21 08:58:00 +0000133
134 struct eeprom_93cx6 eeprom;
Nishanth Menonebf4ad92014-03-21 01:52:48 -0500135 struct regulator *vdd_reg;
Stephen Boyd73fdeb82014-05-23 12:57:19 -0700136 struct regulator *vdd_io;
137 int gpio;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000138};
139
140static int msg_enable;
141
Lukas Wunneraae079a2019-03-20 15:02:00 +0100142/* SPI frame opcodes */
143#define KS_SPIOP_RD (0x00)
144#define KS_SPIOP_WR (0x40)
145#define KS_SPIOP_RXFIFO (0x80)
146#define KS_SPIOP_TXFIFO (0xC0)
147
Ben Dooks3ba81f32009-07-16 05:24:08 +0000148/* shift for byte-enable data */
149#define BYTE_EN(_x) ((_x) << 2)
150
151/* turn register number and byte-enable mask into data for start of packet */
152#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
153
154/* SPI register read/write calls.
155 *
156 * All these calls issue SPI transactions to access the chip's registers. They
157 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * chip is busy transferring packet data (RX/TX FIFO accesses).
Ben Dooks3ba81f32009-07-16 05:24:08 +0000159 */
160
161/**
162 * ks8851_wrreg16 - write 16bit register value to chip
163 * @ks: The chip state
164 * @reg: The register address
165 * @val: The value to write
166 *
167 * Issue a write to put the value @val into the register specified in @reg.
168 */
169static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
170{
171 struct spi_transfer *xfer = &ks->spi_xfer1;
172 struct spi_message *msg = &ks->spi_msg1;
173 __le16 txb[2];
174 int ret;
175
176 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
177 txb[1] = cpu_to_le16(val);
178
179 xfer->tx_buf = txb;
180 xfer->rx_buf = NULL;
181 xfer->len = 4;
182
183 ret = spi_sync(ks->spidev, msg);
184 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000185 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000186}
187
188/**
Ben Dooks160d0fa2009-10-19 23:49:04 +0000189 * ks8851_wrreg8 - write 8bit register value to chip
190 * @ks: The chip state
191 * @reg: The register address
192 * @val: The value to write
193 *
194 * Issue a write to put the value @val into the register specified in @reg.
195 */
196static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
197{
198 struct spi_transfer *xfer = &ks->spi_xfer1;
199 struct spi_message *msg = &ks->spi_msg1;
200 __le16 txb[2];
201 int ret;
202 int bit;
203
204 bit = 1 << (reg & 3);
205
206 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
207 txb[1] = val;
208
209 xfer->tx_buf = txb;
210 xfer->rx_buf = NULL;
211 xfer->len = 3;
212
213 ret = spi_sync(ks->spidev, msg);
214 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000215 netdev_err(ks->netdev, "spi_sync() failed\n");
Ben Dooks160d0fa2009-10-19 23:49:04 +0000216}
217
218/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000219 * ks8851_rdreg - issue read register command and return the data
220 * @ks: The device state
221 * @op: The register address and byte enables in message format.
222 * @rxb: The RX buffer to return the result into
223 * @rxl: The length of data expected.
224 *
225 * This is the low level read call that issues the necessary spi message(s)
226 * to read data from the register specified in @op.
227 */
228static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
229 u8 *rxb, unsigned rxl)
230{
231 struct spi_transfer *xfer;
232 struct spi_message *msg;
233 __le16 *txb = (__le16 *)ks->txd;
234 u8 *trx = ks->rxd;
235 int ret;
236
237 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
238
Sergey Shcherbakov9efd3832017-03-09 02:58:14 +0200239 if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
Ben Dooks3ba81f32009-07-16 05:24:08 +0000240 msg = &ks->spi_msg2;
241 xfer = ks->spi_xfer2;
242
243 xfer->tx_buf = txb;
244 xfer->rx_buf = NULL;
245 xfer->len = 2;
246
247 xfer++;
248 xfer->tx_buf = NULL;
249 xfer->rx_buf = trx;
250 xfer->len = rxl;
Sergey Shcherbakov9efd3832017-03-09 02:58:14 +0200251 } else {
252 msg = &ks->spi_msg1;
253 xfer = &ks->spi_xfer1;
254
255 xfer->tx_buf = txb;
256 xfer->rx_buf = trx;
257 xfer->len = rxl + 2;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000258 }
259
260 ret = spi_sync(ks->spidev, msg);
261 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000262 netdev_err(ks->netdev, "read: spi_sync() failed\n");
Sergey Shcherbakov9efd3832017-03-09 02:58:14 +0200263 else if (ks->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000264 memcpy(rxb, trx, rxl);
Sergey Shcherbakov9efd3832017-03-09 02:58:14 +0200265 else
266 memcpy(rxb, trx + 2, rxl);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000267}
268
269/**
270 * ks8851_rdreg8 - read 8 bit register from device
271 * @ks: The chip information
272 * @reg: The register address
273 *
274 * Read a 8bit register from the chip, returning the result
275*/
276static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
277{
278 u8 rxb[1];
279
280 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
281 return rxb[0];
282}
283
284/**
285 * ks8851_rdreg16 - read 16 bit register from device
286 * @ks: The chip information
287 * @reg: The register address
288 *
289 * Read a 16bit register from the chip, returning the result
290*/
291static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
292{
293 __le16 rx = 0;
294
295 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
296 return le16_to_cpu(rx);
297}
298
299/**
300 * ks8851_rdreg32 - read 32 bit register from device
301 * @ks: The chip information
302 * @reg: The register address
303 *
304 * Read a 32bit register from the chip.
305 *
306 * Note, this read requires the address be aligned to 4 bytes.
307*/
308static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
309{
310 __le32 rx = 0;
311
312 WARN_ON(reg & 3);
313
314 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
315 return le32_to_cpu(rx);
316}
317
318/**
319 * ks8851_soft_reset - issue one of the soft reset to the device
320 * @ks: The device state.
321 * @op: The bit(s) to set in the GRR
322 *
323 * Issue the relevant soft-reset command to the device's GRR register
324 * specified by @op.
325 *
326 * Note, the delays are in there as a caution to ensure that the reset
327 * has time to take effect and then complete. Since the datasheet does
328 * not currently specify the exact sequence, we have chosen something
329 * that seems to work with our device.
330 */
331static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
332{
333 ks8851_wrreg16(ks, KS_GRR, op);
334 mdelay(1); /* wait a short time to effect reset */
335 ks8851_wrreg16(ks, KS_GRR, 0);
336 mdelay(1); /* wait for condition to clear */
337}
338
339/**
Tristram Ha32f160d2011-11-21 08:57:59 +0000340 * ks8851_set_powermode - set power mode of the device
341 * @ks: The device state
342 * @pwrmode: The power mode value to write to KS_PMECR.
343 *
344 * Change the power mode of the chip.
345 */
346static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
347{
348 unsigned pmecr;
349
350 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
351
352 pmecr = ks8851_rdreg16(ks, KS_PMECR);
353 pmecr &= ~PMECR_PM_MASK;
354 pmecr |= pwrmode;
355
356 ks8851_wrreg16(ks, KS_PMECR, pmecr);
357}
358
359/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000360 * ks8851_write_mac_addr - write mac address to device registers
361 * @dev: The network device
362 *
363 * Update the KS8851 MAC address registers from the address in @dev.
364 *
365 * This call assumes that the chip is not running, so there is no need to
366 * shutdown the RXQ process whilst setting this.
367*/
368static int ks8851_write_mac_addr(struct net_device *dev)
369{
370 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000371 int i;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000372
373 mutex_lock(&ks->lock);
374
Tristram Ha32f160d2011-11-21 08:57:59 +0000375 /*
376 * Wake up chip in case it was powered off when stopped; otherwise,
377 * the first write to the MAC address does not take effect.
378 */
379 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
Ben Dooks160d0fa2009-10-19 23:49:04 +0000380 for (i = 0; i < ETH_ALEN; i++)
381 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
Tristram Ha32f160d2011-11-21 08:57:59 +0000382 if (!netif_running(dev))
383 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000384
385 mutex_unlock(&ks->lock);
386
387 return 0;
388}
389
390/**
Ben Dooksa9a8de22011-11-21 08:57:58 +0000391 * ks8851_read_mac_addr - read mac address from device registers
392 * @dev: The network device
393 *
394 * Update our copy of the KS8851 MAC address from the registers of @dev.
395*/
396static void ks8851_read_mac_addr(struct net_device *dev)
397{
398 struct ks8851_net *ks = netdev_priv(dev);
399 int i;
400
401 mutex_lock(&ks->lock);
402
403 for (i = 0; i < ETH_ALEN; i++)
404 dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
405
406 mutex_unlock(&ks->lock);
407}
408
409/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000410 * ks8851_init_mac - initialise the mac address
411 * @ks: The device structure
412 *
413 * Get or create the initial mac address for the device and then set that
Lukas Wunner566bd542017-12-18 11:17:07 +0100414 * into the station address register. A mac address supplied in the device
415 * tree takes precedence. Otherwise, if there is an EEPROM present, then
Joe Perches7efd26d2012-07-12 19:33:06 +0000416 * we try that. If no valid mac address is found we use eth_random_addr()
Ben Dooks3ba81f32009-07-16 05:24:08 +0000417 * to create a new one.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000418 */
419static void ks8851_init_mac(struct ks8851_net *ks)
420{
421 struct net_device *dev = ks->netdev;
Lukas Wunner566bd542017-12-18 11:17:07 +0100422 const u8 *mac_addr;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000423
Lukas Wunner566bd542017-12-18 11:17:07 +0100424 mac_addr = of_get_mac_address(ks->spidev->dev.of_node);
Petr Å tetiara51645f2019-05-06 23:27:04 +0200425 if (!IS_ERR(mac_addr)) {
Petr Å tetiar2d2924a2019-05-10 11:35:17 +0200426 ether_addr_copy(dev->dev_addr, mac_addr);
Lukas Wunner566bd542017-12-18 11:17:07 +0100427 ks8851_write_mac_addr(dev);
428 return;
429 }
430
Ben Dooksa9a8de22011-11-21 08:57:58 +0000431 if (ks->rc_ccr & CCR_EEPROM) {
432 ks8851_read_mac_addr(dev);
433 if (is_valid_ether_addr(dev->dev_addr))
434 return;
435
436 netdev_err(ks->netdev, "invalid mac address read %pM\n",
437 dev->dev_addr);
438 }
439
Danny Kukawka7ce5d222012-02-15 06:45:40 +0000440 eth_hw_addr_random(dev);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000441 ks8851_write_mac_addr(dev);
442}
443
444/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000445 * ks8851_rdfifo - read data from the receive fifo
446 * @ks: The device state.
447 * @buff: The buffer address
448 * @len: The length of the data to read
449 *
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100450 * Issue an RXQ FIFO read command and read the @len amount of data from
Ben Dooks3ba81f32009-07-16 05:24:08 +0000451 * the FIFO into the buffer specified by @buff.
452 */
453static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
454{
455 struct spi_transfer *xfer = ks->spi_xfer2;
456 struct spi_message *msg = &ks->spi_msg2;
457 u8 txb[1];
458 int ret;
459
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000460 netif_dbg(ks, rx_status, ks->netdev,
461 "%s: %d@%p\n", __func__, len, buff);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000462
463 /* set the operation we're issuing */
464 txb[0] = KS_SPIOP_RXFIFO;
465
466 xfer->tx_buf = txb;
467 xfer->rx_buf = NULL;
468 xfer->len = 1;
469
470 xfer++;
471 xfer->rx_buf = buff;
472 xfer->tx_buf = NULL;
473 xfer->len = len;
474
475 ret = spi_sync(ks->spidev, msg);
476 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000477 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000478}
479
480/**
481 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
482 * @ks: The device state
483 * @rxpkt: The data for the received packet
484 *
485 * Dump the initial data from the packet to dev_dbg().
486*/
487static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
488{
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000489 netdev_dbg(ks->netdev,
490 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
491 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
492 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
493 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000494}
495
496/**
497 * ks8851_rx_pkts - receive packets from the host
498 * @ks: The device information.
499 *
500 * This is called from the IRQ work queue when the system detects that there
501 * are packets in the receive queue. Find out how many packets there are and
502 * read them from the FIFO.
503 */
504static void ks8851_rx_pkts(struct ks8851_net *ks)
505{
506 struct sk_buff *skb;
507 unsigned rxfc;
508 unsigned rxlen;
509 unsigned rxstat;
510 u32 rxh;
511 u8 *rxpkt;
512
513 rxfc = ks8851_rdreg8(ks, KS_RXFC);
514
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000515 netif_dbg(ks, rx_status, ks->netdev,
516 "%s: %d packets\n", __func__, rxfc);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000517
518 /* Currently we're issuing a read per packet, but we could possibly
519 * improve the code by issuing a single read, getting the receive
520 * header, allocating the packet and then reading the packet data
521 * out in one go.
522 *
523 * This form of operation would require us to hold the SPI bus'
524 * chipselect low during the entie transaction to avoid any
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300525 * reset to the data stream coming from the chip.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000526 */
527
528 for (; rxfc != 0; rxfc--) {
529 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
530 rxstat = rxh & 0xffff;
Max.Nekludov@us.elster.com14bc4352013-03-29 05:27:36 +0000531 rxlen = (rxh >> 16) & 0xfff;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000532
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000533 netif_dbg(ks, rx_status, ks->netdev,
534 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000535
536 /* the length of the packet includes the 32bit CRC */
537
538 /* set dma read address */
539 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
540
Lukas Wunner536d3682019-03-20 15:02:00 +0100541 /* start DMA access */
542 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000543
Eric Dumazet972c40b2010-09-08 13:26:55 +0000544 if (rxlen > 4) {
545 unsigned int rxalign;
546
547 rxlen -= 4;
548 rxalign = ALIGN(rxlen, 4);
549 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
550 if (skb) {
551
552 /* 4 bytes of status header + 4 bytes of
553 * garbage: we put them before ethernet
554 * header, so that they are copied,
555 * but ignored.
556 */
557
558 rxpkt = skb_put(skb, rxlen) - 8;
559
560 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
561
562 if (netif_msg_pktdata(ks))
563 ks8851_dbg_dumpkkt(ks, rxpkt);
564
565 skb->protocol = eth_type_trans(skb, ks->netdev);
Cousson, Benoitfbcf88b2012-02-13 07:37:12 +0000566 netif_rx_ni(skb);
Eric Dumazet972c40b2010-09-08 13:26:55 +0000567
568 ks->netdev->stats.rx_packets++;
569 ks->netdev->stats.rx_bytes += rxlen;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000570 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000571 }
572
Lukas Wunner536d3682019-03-20 15:02:00 +0100573 /* end DMA access and dequeue packet */
574 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000575 }
576}
577
578/**
Felipe Balbi656a05c2013-01-29 09:16:30 +0200579 * ks8851_irq - IRQ handler for dealing with interrupt requests
580 * @irq: IRQ number
581 * @_ks: cookie
Ben Dooks3ba81f32009-07-16 05:24:08 +0000582 *
Felipe Balbi656a05c2013-01-29 09:16:30 +0200583 * This handler is invoked when the IRQ line asserts to find out what happened.
584 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
585 * in thread context.
Ben Dooks3ba81f32009-07-16 05:24:08 +0000586 *
587 * Read the interrupt status, work out what needs to be done and then clear
588 * any of the interrupts that are not needed.
589 */
Felipe Balbi656a05c2013-01-29 09:16:30 +0200590static irqreturn_t ks8851_irq(int irq, void *_ks)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000591{
Felipe Balbi656a05c2013-01-29 09:16:30 +0200592 struct ks8851_net *ks = _ks;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000593 unsigned status;
594 unsigned handled = 0;
595
596 mutex_lock(&ks->lock);
597
598 status = ks8851_rdreg16(ks, KS_ISR);
599
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000600 netif_dbg(ks, intr, ks->netdev,
601 "%s: status 0x%04x\n", __func__, status);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000602
Stephen Boyd062e55e2012-05-10 12:51:30 +0000603 if (status & IRQ_LCI)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000604 handled |= IRQ_LCI;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000605
606 if (status & IRQ_LDI) {
607 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
608 pmecr &= ~PMECR_WKEVT_MASK;
609 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
610
611 handled |= IRQ_LDI;
612 }
613
614 if (status & IRQ_RXPSI)
615 handled |= IRQ_RXPSI;
616
617 if (status & IRQ_TXI) {
618 handled |= IRQ_TXI;
619
620 /* no lock here, tx queue should have been stopped */
621
622 /* update our idea of how much tx space is available to the
623 * system */
624 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
625
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000626 netif_dbg(ks, intr, ks->netdev,
627 "%s: txspace %d\n", __func__, ks->tx_space);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000628 }
629
630 if (status & IRQ_RXI)
631 handled |= IRQ_RXI;
632
633 if (status & IRQ_SPIBEI) {
634 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
635 handled |= IRQ_SPIBEI;
636 }
637
638 ks8851_wrreg16(ks, KS_ISR, handled);
639
640 if (status & IRQ_RXI) {
641 /* the datasheet says to disable the rx interrupt during
642 * packet read-out, however we're masking the interrupt
643 * from the device so do not bother masking just the RX
644 * from the device. */
645
646 ks8851_rx_pkts(ks);
647 }
648
649 /* if something stopped the rx process, probably due to wanting
650 * to change the rx settings, then do something about restarting
651 * it. */
652 if (status & IRQ_RXPSI) {
653 struct ks8851_rxctrl *rxc = &ks->rxctrl;
654
655 /* update the multicast hash table */
656 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
657 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
658 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
659 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
660
661 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
662 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
663 }
664
665 mutex_unlock(&ks->lock);
666
Stephen Boyd062e55e2012-05-10 12:51:30 +0000667 if (status & IRQ_LCI)
668 mii_check_link(&ks->mii);
669
Ben Dooks3ba81f32009-07-16 05:24:08 +0000670 if (status & IRQ_TXI)
671 netif_wake_queue(ks->netdev);
672
Felipe Balbi656a05c2013-01-29 09:16:30 +0200673 return IRQ_HANDLED;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000674}
675
676/**
677 * calc_txlen - calculate size of message to send packet
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300678 * @len: Length of data
Ben Dooks3ba81f32009-07-16 05:24:08 +0000679 *
680 * Returns the size of the TXFIFO message needed to send
681 * this packet.
682 */
683static inline unsigned calc_txlen(unsigned len)
684{
685 return ALIGN(len + 4, 4);
686}
687
688/**
689 * ks8851_wrpkt - write packet to TX FIFO
690 * @ks: The device state.
691 * @txp: The sk_buff to transmit.
692 * @irq: IRQ on completion of the packet.
693 *
694 * Send the @txp to the chip. This means creating the relevant packet header
695 * specifying the length of the packet and the other information the chip
696 * needs, such as IRQ on completion. Send the header and the packet data to
697 * the device.
698 */
699static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
700{
701 struct spi_transfer *xfer = ks->spi_xfer2;
702 struct spi_message *msg = &ks->spi_msg2;
703 unsigned fid = 0;
704 int ret;
705
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000706 netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
707 __func__, txp, txp->len, txp->data, irq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000708
709 fid = ks->fid++;
710 fid &= TXFR_TXFID_MASK;
711
712 if (irq)
713 fid |= TXFR_TXIC; /* irq on completion */
714
715 /* start header at txb[1] to align txw entries */
716 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
717 ks->txh.txw[1] = cpu_to_le16(fid);
718 ks->txh.txw[2] = cpu_to_le16(txp->len);
719
720 xfer->tx_buf = &ks->txh.txb[1];
721 xfer->rx_buf = NULL;
722 xfer->len = 5;
723
724 xfer++;
725 xfer->tx_buf = txp->data;
726 xfer->rx_buf = NULL;
727 xfer->len = ALIGN(txp->len, 4);
728
729 ret = spi_sync(ks->spidev, msg);
730 if (ret < 0)
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000731 netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000732}
733
734/**
735 * ks8851_done_tx - update and then free skbuff after transmitting
736 * @ks: The device state
737 * @txb: The buffer transmitted
738 */
739static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
740{
741 struct net_device *dev = ks->netdev;
742
743 dev->stats.tx_bytes += txb->len;
744 dev->stats.tx_packets++;
745
746 dev_kfree_skb(txb);
747}
748
749/**
750 * ks8851_tx_work - process tx packet(s)
751 * @work: The work strucutre what was scheduled.
752 *
753 * This is called when a number of packets have been scheduled for
754 * transmission and need to be sent to the device.
755 */
756static void ks8851_tx_work(struct work_struct *work)
757{
758 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
759 struct sk_buff *txb;
Tristram Ha3320eae2009-12-03 11:06:42 +0000760 bool last = skb_queue_empty(&ks->txq);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000761
762 mutex_lock(&ks->lock);
763
764 while (!last) {
765 txb = skb_dequeue(&ks->txq);
766 last = skb_queue_empty(&ks->txq);
767
Abraham Arce761172f2010-04-16 14:48:43 +0000768 if (txb != NULL) {
769 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
770 ks8851_wrpkt(ks, txb, last);
771 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
772 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000773
Abraham Arce761172f2010-04-16 14:48:43 +0000774 ks8851_done_tx(ks, txb);
775 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000776 }
777
778 mutex_unlock(&ks->lock);
779}
780
781/**
Ben Dooks3ba81f32009-07-16 05:24:08 +0000782 * ks8851_net_open - open network device
783 * @dev: The network device being opened.
784 *
785 * Called when the network device is marked active, such as a user executing
786 * 'ifconfig up' on the device.
787 */
788static int ks8851_net_open(struct net_device *dev)
789{
790 struct ks8851_net *ks = netdev_priv(dev);
Lukas Wunnerd268f312019-03-20 15:02:00 +0100791 int ret;
792
793 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
794 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
795 dev->name, ks);
796 if (ret < 0) {
797 netdev_err(dev, "failed to get irq\n");
798 return ret;
799 }
Ben Dooks3ba81f32009-07-16 05:24:08 +0000800
801 /* lock the card, even if we may not actually be doing anything
802 * else at the moment */
803 mutex_lock(&ks->lock);
804
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000805 netif_dbg(ks, ifup, ks->netdev, "opening\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000806
807 /* bring chip out of any power saving mode it was in */
808 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
809
810 /* issue a soft reset to the RX/TX QMU to put it into a known
811 * state. */
812 ks8851_soft_reset(ks, GRR_QMU);
813
814 /* setup transmission parameters */
815
816 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
817 TXCR_TXPE | /* pad to min length */
818 TXCR_TXCRC | /* add CRC */
819 TXCR_TXFCE)); /* enable flow control */
820
821 /* auto-increment tx data, reset tx pointer */
822 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
823
824 /* setup receiver control */
825
826 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
827 RXCR1_RXFCE | /* enable flow control */
828 RXCR1_RXBE | /* broadcast enable */
829 RXCR1_RXUE | /* unicast enable */
830 RXCR1_RXE)); /* enable rx block */
831
832 /* transfer entire frames out in one go */
833 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
834
835 /* set receive counter timeouts */
836 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
837 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
838 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
839
840 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
841 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
842 RXQCR_RXDTTE); /* IRQ on time exceeded */
843
844 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
845
846 /* clear then enable interrupts */
847
848#define STD_IRQ (IRQ_LCI | /* Link Change */ \
849 IRQ_TXI | /* TX done */ \
850 IRQ_RXI | /* RX done */ \
851 IRQ_SPIBEI | /* SPI bus error */ \
852 IRQ_TXPSI | /* TX process stop */ \
853 IRQ_RXPSI) /* RX process stop */
854
855 ks->rc_ier = STD_IRQ;
856 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
857 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
858
859 netif_start_queue(ks->netdev);
860
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000861 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000862
863 mutex_unlock(&ks->lock);
Lukas Wunner9624baf2019-03-20 15:02:00 +0100864 mii_check_link(&ks->mii);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000865 return 0;
866}
867
868/**
869 * ks8851_net_stop - close network device
870 * @dev: The device being closed.
871 *
872 * Called to close down a network device which has been active. Cancell any
873 * work, shutdown the RX and TX process and then place the chip into a low
874 * power state whilst it is not being used.
875 */
876static int ks8851_net_stop(struct net_device *dev)
877{
878 struct ks8851_net *ks = netdev_priv(dev);
879
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000880 netif_info(ks, ifdown, dev, "shutting down\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +0000881
882 netif_stop_queue(dev);
883
884 mutex_lock(&ks->lock);
Stephen Boydc5a99932012-04-18 17:25:58 +0000885 /* turn off the IRQs and ack any outstanding */
886 ks8851_wrreg16(ks, KS_IER, 0x0000);
887 ks8851_wrreg16(ks, KS_ISR, 0xffff);
888 mutex_unlock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000889
890 /* stop any outstanding work */
Ben Dooks3ba81f32009-07-16 05:24:08 +0000891 flush_work(&ks->tx_work);
892 flush_work(&ks->rxctrl_work);
893
Stephen Boydc5a99932012-04-18 17:25:58 +0000894 mutex_lock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000895 /* shutdown RX process */
896 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
897
898 /* shutdown TX process */
899 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
900
901 /* set powermode to soft power down to save power */
902 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
Stephen Boydc5a99932012-04-18 17:25:58 +0000903 mutex_unlock(&ks->lock);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000904
905 /* ensure any queued tx buffers are dumped */
906 while (!skb_queue_empty(&ks->txq)) {
907 struct sk_buff *txb = skb_dequeue(&ks->txq);
908
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000909 netif_dbg(ks, ifdown, ks->netdev,
910 "%s: freeing txb %p\n", __func__, txb);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000911
912 dev_kfree_skb(txb);
913 }
914
Lukas Wunnerd268f312019-03-20 15:02:00 +0100915 free_irq(dev->irq, ks);
916
Ben Dooks3ba81f32009-07-16 05:24:08 +0000917 return 0;
918}
919
920/**
921 * ks8851_start_xmit - transmit packet
922 * @skb: The buffer to transmit
923 * @dev: The device used to transmit the packet.
924 *
925 * Called by the network layer to transmit the @skb. Queue the packet for
926 * the device and schedule the necessary work to transmit the packet when
927 * it is free.
928 *
929 * We do this to firstly avoid sleeping with the network device locked,
930 * and secondly so we can round up more than one packet to transmit which
931 * means we can try and avoid generating too many transmit done interrupts.
932 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000933static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
934 struct net_device *dev)
Ben Dooks3ba81f32009-07-16 05:24:08 +0000935{
936 struct ks8851_net *ks = netdev_priv(dev);
937 unsigned needed = calc_txlen(skb->len);
Stephen Hemminger613573252009-08-31 19:50:58 +0000938 netdev_tx_t ret = NETDEV_TX_OK;
Ben Dooks3ba81f32009-07-16 05:24:08 +0000939
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000940 netif_dbg(ks, tx_queued, ks->netdev,
941 "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
Ben Dooks3ba81f32009-07-16 05:24:08 +0000942
943 spin_lock(&ks->statelock);
944
945 if (needed > ks->tx_space) {
946 netif_stop_queue(dev);
947 ret = NETDEV_TX_BUSY;
948 } else {
949 ks->tx_space -= needed;
950 skb_queue_tail(&ks->txq, skb);
951 }
952
953 spin_unlock(&ks->statelock);
954 schedule_work(&ks->tx_work);
955
956 return ret;
957}
958
959/**
960 * ks8851_rxctrl_work - work handler to change rx mode
961 * @work: The work structure this belongs to.
962 *
963 * Lock the device and issue the necessary changes to the receive mode from
964 * the network device layer. This is done so that we can do this without
965 * having to sleep whilst holding the network device lock.
966 *
967 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
968 * receive parameters are programmed, we issue a write to disable the RXQ and
969 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
970 * complete. The interrupt handler then writes the new values into the chip.
971 */
972static void ks8851_rxctrl_work(struct work_struct *work)
973{
974 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
975
976 mutex_lock(&ks->lock);
977
978 /* need to shutdown RXQ before modifying filter parameters */
979 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
980
981 mutex_unlock(&ks->lock);
982}
983
984static void ks8851_set_rx_mode(struct net_device *dev)
985{
986 struct ks8851_net *ks = netdev_priv(dev);
987 struct ks8851_rxctrl rxctrl;
988
989 memset(&rxctrl, 0, sizeof(rxctrl));
990
991 if (dev->flags & IFF_PROMISC) {
992 /* interface to receive everything */
993
994 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
995 } else if (dev->flags & IFF_ALLMULTI) {
996 /* accept all multicast packets */
997
998 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
999 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001000 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001001 struct netdev_hw_addr *ha;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001002 u32 crc;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001003
1004 /* accept some multicast */
1005
Jiri Pirko22bedad32010-04-01 21:22:57 +00001006 netdev_for_each_mc_addr(ha, dev) {
1007 crc = ether_crc(ETH_ALEN, ha->addr);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001008 crc >>= (32 - 6); /* get top six bits */
1009
1010 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
Ben Dooks3ba81f32009-07-16 05:24:08 +00001011 }
1012
Ben Dooksb6a71bf2009-10-19 23:49:05 +00001013 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001014 } else {
1015 /* just accept broadcast / unicast */
1016 rxctrl.rxcr1 = RXCR1_RXPAFMA;
1017 }
1018
1019 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
1020 RXCR1_RXBE | /* broadcast enable */
1021 RXCR1_RXE | /* RX process enable */
1022 RXCR1_RXFCE); /* enable flow control */
1023
1024 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
1025
1026 /* schedule work to do the actual set of the data if needed */
1027
1028 spin_lock(&ks->statelock);
1029
1030 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1031 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1032 schedule_work(&ks->rxctrl_work);
1033 }
1034
1035 spin_unlock(&ks->statelock);
1036}
1037
1038static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1039{
1040 struct sockaddr *sa = addr;
1041
1042 if (netif_running(dev))
1043 return -EBUSY;
1044
1045 if (!is_valid_ether_addr(sa->sa_data))
1046 return -EADDRNOTAVAIL;
1047
1048 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1049 return ks8851_write_mac_addr(dev);
1050}
1051
1052static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1053{
1054 struct ks8851_net *ks = netdev_priv(dev);
1055
1056 if (!netif_running(dev))
1057 return -EINVAL;
1058
1059 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1060}
1061
1062static const struct net_device_ops ks8851_netdev_ops = {
1063 .ndo_open = ks8851_net_open,
1064 .ndo_stop = ks8851_net_stop,
1065 .ndo_do_ioctl = ks8851_net_ioctl,
1066 .ndo_start_xmit = ks8851_start_xmit,
1067 .ndo_set_mac_address = ks8851_set_mac_address,
1068 .ndo_set_rx_mode = ks8851_set_rx_mode,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001069 .ndo_validate_addr = eth_validate_addr,
1070};
1071
1072/* ethtool support */
1073
1074static void ks8851_get_drvinfo(struct net_device *dev,
1075 struct ethtool_drvinfo *di)
1076{
1077 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1078 strlcpy(di->version, "1.00", sizeof(di->version));
1079 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1080}
1081
1082static u32 ks8851_get_msglevel(struct net_device *dev)
1083{
1084 struct ks8851_net *ks = netdev_priv(dev);
1085 return ks->msg_enable;
1086}
1087
1088static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1089{
1090 struct ks8851_net *ks = netdev_priv(dev);
1091 ks->msg_enable = to;
1092}
1093
Philippe Reynes98f2b092017-02-09 09:57:47 +01001094static int ks8851_get_link_ksettings(struct net_device *dev,
1095 struct ethtool_link_ksettings *cmd)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001096{
1097 struct ks8851_net *ks = netdev_priv(dev);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001098
1099 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1100
1101 return 0;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001102}
1103
Philippe Reynes98f2b092017-02-09 09:57:47 +01001104static int ks8851_set_link_ksettings(struct net_device *dev,
1105 const struct ethtool_link_ksettings *cmd)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001106{
1107 struct ks8851_net *ks = netdev_priv(dev);
Philippe Reynes98f2b092017-02-09 09:57:47 +01001108 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001109}
1110
1111static u32 ks8851_get_link(struct net_device *dev)
1112{
1113 struct ks8851_net *ks = netdev_priv(dev);
1114 return mii_link_ok(&ks->mii);
1115}
1116
1117static int ks8851_nway_reset(struct net_device *dev)
1118{
1119 struct ks8851_net *ks = netdev_priv(dev);
1120 return mii_nway_restart(&ks->mii);
1121}
1122
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001123/* EEPROM support */
1124
1125static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
1126{
1127 struct ks8851_net *ks = ee->data;
1128 unsigned val;
1129
1130 val = ks8851_rdreg16(ks, KS_EEPCR);
1131
1132 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
1133 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
1134 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
1135}
1136
1137static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
1138{
1139 struct ks8851_net *ks = ee->data;
1140 unsigned val = EEPCR_EESA; /* default - eeprom access on */
1141
1142 if (ee->drive_data)
1143 val |= EEPCR_EESRWA;
1144 if (ee->reg_data_in)
1145 val |= EEPCR_EEDO;
1146 if (ee->reg_data_clock)
1147 val |= EEPCR_EESCK;
1148 if (ee->reg_chip_select)
1149 val |= EEPCR_EECS;
1150
1151 ks8851_wrreg16(ks, KS_EEPCR, val);
1152}
1153
1154/**
1155 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
1156 * @ks: The network device state.
1157 *
1158 * Check for the presence of an EEPROM, and then activate software access
1159 * to the device.
1160 */
1161static int ks8851_eeprom_claim(struct ks8851_net *ks)
1162{
1163 if (!(ks->rc_ccr & CCR_EEPROM))
1164 return -ENOENT;
1165
1166 mutex_lock(&ks->lock);
1167
1168 /* start with clock low, cs high */
1169 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
1170 return 0;
1171}
1172
1173/**
1174 * ks8851_eeprom_release - release the EEPROM interface
1175 * @ks: The device state
1176 *
1177 * Release the software access to the device EEPROM
1178 */
1179static void ks8851_eeprom_release(struct ks8851_net *ks)
1180{
1181 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
1182
1183 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
1184 mutex_unlock(&ks->lock);
1185}
1186
1187#define KS_EEPROM_MAGIC (0x00008851)
1188
1189static int ks8851_set_eeprom(struct net_device *dev,
1190 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001191{
1192 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001193 int offset = ee->offset;
1194 int len = ee->len;
1195 u16 tmp;
1196
1197 /* currently only support byte writing */
1198 if (len != 1)
1199 return -EINVAL;
1200
1201 if (ee->magic != KS_EEPROM_MAGIC)
1202 return -EINVAL;
1203
1204 if (ks8851_eeprom_claim(ks))
1205 return -ENOENT;
1206
1207 eeprom_93cx6_wren(&ks->eeprom, true);
1208
1209 /* ethtool currently only supports writing bytes, which means
1210 * we have to read/modify/write our 16bit EEPROMs */
1211
1212 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
1213
1214 if (offset & 1) {
1215 tmp &= 0xff;
1216 tmp |= *data << 8;
1217 } else {
1218 tmp &= 0xff00;
1219 tmp |= *data;
1220 }
1221
1222 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
1223 eeprom_93cx6_wren(&ks->eeprom, false);
1224
1225 ks8851_eeprom_release(ks);
1226
1227 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001228}
1229
1230static int ks8851_get_eeprom(struct net_device *dev,
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001231 struct ethtool_eeprom *ee, u8 *data)
Sebastien Jana84afa42010-05-05 08:45:54 +00001232{
1233 struct ks8851_net *ks = netdev_priv(dev);
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001234 int offset = ee->offset;
1235 int len = ee->len;
Sebastien Jana84afa42010-05-05 08:45:54 +00001236
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001237 /* must be 2 byte aligned */
1238 if (len & 1 || offset & 1)
Sebastien Jana84afa42010-05-05 08:45:54 +00001239 return -EINVAL;
1240
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001241 if (ks8851_eeprom_claim(ks))
1242 return -ENOENT;
Sebastien Jana84afa42010-05-05 08:45:54 +00001243
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001244 ee->magic = KS_EEPROM_MAGIC;
Sebastien Jana84afa42010-05-05 08:45:54 +00001245
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001246 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
1247 ks8851_eeprom_release(ks);
Sebastien Jana84afa42010-05-05 08:45:54 +00001248
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001249 return 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001250}
1251
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001252static int ks8851_get_eeprom_len(struct net_device *dev)
Sebastien Jana84afa42010-05-05 08:45:54 +00001253{
1254 struct ks8851_net *ks = netdev_priv(dev);
Sebastien Jana84afa42010-05-05 08:45:54 +00001255
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001256 /* currently, we assume it is an 93C46 attached, so return 128 */
1257 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
Sebastien Jana84afa42010-05-05 08:45:54 +00001258}
1259
Ben Dooks3ba81f32009-07-16 05:24:08 +00001260static const struct ethtool_ops ks8851_ethtool_ops = {
1261 .get_drvinfo = ks8851_get_drvinfo,
1262 .get_msglevel = ks8851_get_msglevel,
1263 .set_msglevel = ks8851_set_msglevel,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001264 .get_link = ks8851_get_link,
1265 .nway_reset = ks8851_nway_reset,
Sebastien Jana84afa42010-05-05 08:45:54 +00001266 .get_eeprom_len = ks8851_get_eeprom_len,
1267 .get_eeprom = ks8851_get_eeprom,
1268 .set_eeprom = ks8851_set_eeprom,
Philippe Reynes98f2b092017-02-09 09:57:47 +01001269 .get_link_ksettings = ks8851_get_link_ksettings,
1270 .set_link_ksettings = ks8851_set_link_ksettings,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001271};
1272
1273/* MII interface controls */
1274
1275/**
1276 * ks8851_phy_reg - convert MII register into a KS8851 register
1277 * @reg: MII register number.
1278 *
1279 * Return the KS8851 register number for the corresponding MII PHY register
1280 * if possible. Return zero if the MII register has no direct mapping to the
1281 * KS8851 register set.
1282 */
1283static int ks8851_phy_reg(int reg)
1284{
1285 switch (reg) {
1286 case MII_BMCR:
1287 return KS_P1MBCR;
1288 case MII_BMSR:
1289 return KS_P1MBSR;
1290 case MII_PHYSID1:
1291 return KS_PHY1ILR;
1292 case MII_PHYSID2:
1293 return KS_PHY1IHR;
1294 case MII_ADVERTISE:
1295 return KS_P1ANAR;
1296 case MII_LPA:
1297 return KS_P1ANLPR;
1298 }
1299
1300 return 0x0;
1301}
1302
1303/**
1304 * ks8851_phy_read - MII interface PHY register read.
1305 * @dev: The network device the PHY is on.
1306 * @phy_addr: Address of PHY (ignored as we only have one)
1307 * @reg: The register to read.
1308 *
1309 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001310 * device does not support all the MII registers, the non-existent values
Ben Dooks3ba81f32009-07-16 05:24:08 +00001311 * are always returned as zero.
1312 *
1313 * We return zero for unsupported registers as the MII code does not check
1314 * the value returned for any error status, and simply returns it to the
1315 * caller. The mii-tool that the driver was tested with takes any -ve error
1316 * as real PHY capabilities, thus displaying incorrect data to the user.
1317 */
1318static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1319{
1320 struct ks8851_net *ks = netdev_priv(dev);
1321 int ksreg;
1322 int result;
1323
1324 ksreg = ks8851_phy_reg(reg);
1325 if (!ksreg)
1326 return 0x0; /* no error return allowed, so use zero */
1327
1328 mutex_lock(&ks->lock);
1329 result = ks8851_rdreg16(ks, ksreg);
1330 mutex_unlock(&ks->lock);
1331
1332 return result;
1333}
1334
1335static void ks8851_phy_write(struct net_device *dev,
1336 int phy, int reg, int value)
1337{
1338 struct ks8851_net *ks = netdev_priv(dev);
1339 int ksreg;
1340
1341 ksreg = ks8851_phy_reg(reg);
1342 if (ksreg) {
1343 mutex_lock(&ks->lock);
1344 ks8851_wrreg16(ks, ksreg, value);
1345 mutex_unlock(&ks->lock);
1346 }
1347}
1348
1349/**
1350 * ks8851_read_selftest - read the selftest memory info.
1351 * @ks: The device state
1352 *
1353 * Read and check the TX/RX memory selftest information.
1354 */
1355static int ks8851_read_selftest(struct ks8851_net *ks)
1356{
1357 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1358 int ret = 0;
1359 unsigned rd;
1360
1361 rd = ks8851_rdreg16(ks, KS_MBIR);
1362
1363 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001364 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001365 return 0;
1366 }
1367
1368 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001369 netdev_err(ks->netdev, "TX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001370 ret |= 1;
1371 }
1372
1373 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001374 netdev_err(ks->netdev, "RX memory selftest fail\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001375 ret |= 2;
1376 }
1377
1378 return 0;
1379}
1380
1381/* driver bus management functions */
1382
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001383#ifdef CONFIG_PM_SLEEP
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001384
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001385static int ks8851_suspend(struct device *dev)
1386{
1387 struct ks8851_net *ks = dev_get_drvdata(dev);
1388 struct net_device *netdev = ks->netdev;
1389
1390 if (netif_running(netdev)) {
1391 netif_device_detach(netdev);
1392 ks8851_net_stop(netdev);
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001393 }
1394
1395 return 0;
1396}
1397
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001398static int ks8851_resume(struct device *dev)
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001399{
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001400 struct ks8851_net *ks = dev_get_drvdata(dev);
1401 struct net_device *netdev = ks->netdev;
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001402
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001403 if (netif_running(netdev)) {
1404 ks8851_net_open(netdev);
1405 netif_device_attach(netdev);
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001406 }
1407
1408 return 0;
1409}
Fabio Estevam8ac2b3c02013-04-16 09:28:31 +00001410#endif
Lars-Peter Clausend5b40922013-04-06 23:33:04 +00001411
1412static SIMPLE_DEV_PM_OPS(ks8851_pm_ops, ks8851_suspend, ks8851_resume);
Arce, Abraham1d5439b2010-10-28 18:57:20 +00001413
Bill Pemberton654b8c52012-12-03 09:23:59 -05001414static int ks8851_probe(struct spi_device *spi)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001415{
1416 struct net_device *ndev;
1417 struct ks8851_net *ks;
1418 int ret;
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001419 unsigned cider;
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001420 int gpio;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001421
1422 ndev = alloc_etherdev(sizeof(struct ks8851_net));
Joe Perches41de8d42012-01-29 13:47:52 +00001423 if (!ndev)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001424 return -ENOMEM;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001425
1426 spi->bits_per_word = 8;
1427
1428 ks = netdev_priv(ndev);
1429
1430 ks->netdev = ndev;
1431 ks->spidev = spi;
1432 ks->tx_space = 6144;
1433
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001434 gpio = of_get_named_gpio_flags(spi->dev.of_node, "reset-gpios",
1435 0, NULL);
1436 if (gpio == -EPROBE_DEFER) {
1437 ret = gpio;
1438 goto err_gpio;
1439 }
1440
1441 ks->gpio = gpio;
1442 if (gpio_is_valid(gpio)) {
1443 ret = devm_gpio_request_one(&spi->dev, gpio,
1444 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1445 if (ret) {
1446 dev_err(&spi->dev, "reset gpio request failed\n");
1447 goto err_gpio;
1448 }
1449 }
1450
Stephen Boydd64eed12014-05-28 13:11:12 -07001451 ks->vdd_io = devm_regulator_get(&spi->dev, "vdd-io");
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001452 if (IS_ERR(ks->vdd_io)) {
1453 ret = PTR_ERR(ks->vdd_io);
Stephen Boydd64eed12014-05-28 13:11:12 -07001454 goto err_reg_io;
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001455 }
1456
Stephen Boydd64eed12014-05-28 13:11:12 -07001457 ret = regulator_enable(ks->vdd_io);
1458 if (ret) {
1459 dev_err(&spi->dev, "regulator vdd_io enable fail: %d\n",
1460 ret);
1461 goto err_reg_io;
1462 }
1463
1464 ks->vdd_reg = devm_regulator_get(&spi->dev, "vdd");
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001465 if (IS_ERR(ks->vdd_reg)) {
1466 ret = PTR_ERR(ks->vdd_reg);
Stephen Boydd64eed12014-05-28 13:11:12 -07001467 goto err_reg;
1468 }
1469
1470 ret = regulator_enable(ks->vdd_reg);
1471 if (ret) {
1472 dev_err(&spi->dev, "regulator vdd enable fail: %d\n",
1473 ret);
1474 goto err_reg;
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001475 }
1476
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001477 if (gpio_is_valid(gpio)) {
1478 usleep_range(10000, 11000);
1479 gpio_set_value(gpio, 1);
1480 }
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001481
Ben Dooks3ba81f32009-07-16 05:24:08 +00001482 mutex_init(&ks->lock);
1483 spin_lock_init(&ks->statelock);
1484
1485 INIT_WORK(&ks->tx_work, ks8851_tx_work);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001486 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1487
1488 /* initialise pre-made spi transfer messages */
1489
1490 spi_message_init(&ks->spi_msg1);
1491 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1492
1493 spi_message_init(&ks->spi_msg2);
1494 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1495 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1496
Ben Dooks51b7b1c2011-11-21 08:58:00 +00001497 /* setup EEPROM state */
1498
1499 ks->eeprom.data = ks;
1500 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1501 ks->eeprom.register_read = ks8851_eeprom_regread;
1502 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1503
Ben Dooks3ba81f32009-07-16 05:24:08 +00001504 /* setup mii state */
1505 ks->mii.dev = ndev;
1506 ks->mii.phy_id = 1,
1507 ks->mii.phy_id_mask = 1;
1508 ks->mii.reg_num_mask = 0xf;
1509 ks->mii.mdio_read = ks8851_phy_read;
1510 ks->mii.mdio_write = ks8851_phy_write;
1511
1512 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1513
1514 /* set the default message enable */
1515 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1516 NETIF_MSG_PROBE |
1517 NETIF_MSG_LINK));
1518
1519 skb_queue_head_init(&ks->txq);
1520
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001521 ndev->ethtool_ops = &ks8851_ethtool_ops;
Ben Dooks3ba81f32009-07-16 05:24:08 +00001522 SET_NETDEV_DEV(ndev, &spi->dev);
1523
Jingoo Han8f996602013-04-05 20:35:43 +00001524 spi_set_drvdata(spi, ks);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001525
Lukas Wunner9624baf2019-03-20 15:02:00 +01001526 netif_carrier_off(ks->netdev);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001527 ndev->if_port = IF_PORT_100BASET;
1528 ndev->netdev_ops = &ks8851_netdev_ops;
1529 ndev->irq = spi->irq;
1530
Ben Dooks57dada62009-10-19 23:49:03 +00001531 /* issue a global soft reset to reset the device. */
1532 ks8851_soft_reset(ks, GRR_GSR);
1533
Ben Dooks3ba81f32009-07-16 05:24:08 +00001534 /* simple check for a valid chip being connected to the bus */
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001535 cider = ks8851_rdreg16(ks, KS_CIDER);
1536 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
Ben Dooks3ba81f32009-07-16 05:24:08 +00001537 dev_err(&spi->dev, "failed to read device ID\n");
1538 ret = -ENODEV;
1539 goto err_id;
1540 }
1541
Sebastien Jan7d997462010-05-05 08:45:52 +00001542 /* cache the contents of the CCR register for EEPROM, etc. */
1543 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1544
Ben Dooks3ba81f32009-07-16 05:24:08 +00001545 ks8851_read_selftest(ks);
1546 ks8851_init_mac(ks);
1547
Ben Dooks3ba81f32009-07-16 05:24:08 +00001548 ret = register_netdev(ndev);
1549 if (ret) {
1550 dev_err(&spi->dev, "failed to register network device\n");
1551 goto err_netdev;
1552 }
1553
Ben Dooksa9a8de22011-11-21 08:57:58 +00001554 netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
Matt Renzelmann51c61a22012-04-13 07:59:40 +00001555 CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
Ben Dooksa9a8de22011-11-21 08:57:58 +00001556 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001557
1558 return 0;
1559
Ben Dooks3ba81f32009-07-16 05:24:08 +00001560err_netdev:
Lukas Wunner761cfa92019-03-20 15:02:00 +01001561err_id:
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001562 if (gpio_is_valid(gpio))
1563 gpio_set_value(gpio, 0);
Stephen Boydd64eed12014-05-28 13:11:12 -07001564 regulator_disable(ks->vdd_reg);
Nishanth Menonebf4ad92014-03-21 01:52:48 -05001565err_reg:
Stephen Boydd64eed12014-05-28 13:11:12 -07001566 regulator_disable(ks->vdd_io);
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001567err_reg_io:
1568err_gpio:
Ben Dooks3ba81f32009-07-16 05:24:08 +00001569 free_netdev(ndev);
1570 return ret;
1571}
1572
Bill Pemberton654b8c52012-12-03 09:23:59 -05001573static int ks8851_remove(struct spi_device *spi)
Ben Dooks3ba81f32009-07-16 05:24:08 +00001574{
Jingoo Han8f996602013-04-05 20:35:43 +00001575 struct ks8851_net *priv = spi_get_drvdata(spi);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001576
1577 if (netif_msg_drv(priv))
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001578 dev_info(&spi->dev, "remove\n");
Ben Dooks3ba81f32009-07-16 05:24:08 +00001579
1580 unregister_netdev(priv->netdev);
Stephen Boyd73fdeb82014-05-23 12:57:19 -07001581 if (gpio_is_valid(priv->gpio))
1582 gpio_set_value(priv->gpio, 0);
Stephen Boydd64eed12014-05-28 13:11:12 -07001583 regulator_disable(priv->vdd_reg);
1584 regulator_disable(priv->vdd_io);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001585 free_netdev(priv->netdev);
1586
1587 return 0;
1588}
1589
Stephen Boydf4c6e062014-05-23 12:57:20 -07001590static const struct of_device_id ks8851_match_table[] = {
1591 { .compatible = "micrel,ks8851" },
1592 { }
1593};
Javier Martinez Canillas88c79662015-09-16 11:11:22 +02001594MODULE_DEVICE_TABLE(of, ks8851_match_table);
Stephen Boydf4c6e062014-05-23 12:57:20 -07001595
Ben Dooks3ba81f32009-07-16 05:24:08 +00001596static struct spi_driver ks8851_driver = {
1597 .driver = {
1598 .name = "ks8851",
Stephen Boydf4c6e062014-05-23 12:57:20 -07001599 .of_match_table = ks8851_match_table,
Fabio Estevam8ac2b3c02013-04-16 09:28:31 +00001600 .pm = &ks8851_pm_ops,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001601 },
1602 .probe = ks8851_probe,
Bill Pemberton654b8c52012-12-03 09:23:59 -05001603 .remove = ks8851_remove,
Ben Dooks3ba81f32009-07-16 05:24:08 +00001604};
Lars-Peter Clausen0582ce92013-04-06 23:33:03 +00001605module_spi_driver(ks8851_driver);
Ben Dooks3ba81f32009-07-16 05:24:08 +00001606
1607MODULE_DESCRIPTION("KS8851 Network driver");
1608MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1609MODULE_LICENSE("GPL");
1610
1611module_param_named(message, msg_enable, int, 0);
1612MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001613MODULE_ALIAS("spi:ks8851");