1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/net/ethernet/micrel/ks8851.c
3  *
4  * Copyright 2009 Simtec Electronics
5  *	http://www.simtec.co.uk/
6  *	Ben Dooks <ben@simtec.co.uk>
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/ethtool.h>
17 #include <linux/cache.h>
18 #include <linux/crc32.h>
19 #include <linux/mii.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/regulator/consumer.h>
22 
23 #include <linux/of_mdio.h>
24 #include <linux/of_net.h>
25 
26 #include "ks8851.h"
27 
28 /**
29  * ks8851_lock - register access lock
30  * @ks: The chip state
31  * @flags: Spinlock flags
32  *
33  * Claim chip register access lock
34  */
35 static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
36 {
37 	ks->lock(ks, flags);
38 }
39 
40 /**
41  * ks8851_unlock - register access unlock
42  * @ks: The chip state
43  * @flags: Spinlock flags
44  *
45  * Release chip register access lock
46  */
47 static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
48 {
49 	ks->unlock(ks, flags);
50 }
51 
52 /**
53  * ks8851_wrreg16 - write 16bit register value to chip
54  * @ks: The chip state
55  * @reg: The register address
56  * @val: The value to write
57  *
58  * Issue a write to put the value @val into the register specified in @reg.
59  */
60 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned int reg,
61 			   unsigned int val)
62 {
63 	ks->wrreg16(ks, reg, val);
64 }
65 
66 /**
67  * ks8851_rdreg16 - read 16 bit register from device
68  * @ks: The chip information
69  * @reg: The register address
70  *
71  * Read a 16bit register from the chip, returning the result
72  */
73 static unsigned int ks8851_rdreg16(struct ks8851_net *ks,
74 				   unsigned int reg)
75 {
76 	return ks->rdreg16(ks, reg);
77 }
78 
79 /**
80  * ks8851_soft_reset - issue one of the soft reset to the device
81  * @ks: The device state.
82  * @op: The bit(s) to set in the GRR
83  *
84  * Issue the relevant soft-reset command to the device's GRR register
85  * specified by @op.
86  *
87  * Note, the delays are in there as a caution to ensure that the reset
88  * has time to take effect and then complete. Since the datasheet does
89  * not currently specify the exact sequence, we have chosen something
90  * that seems to work with our device.
91  */
92 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
93 {
94 	ks8851_wrreg16(ks, KS_GRR, op);
95 	mdelay(1);	/* wait a short time to effect reset */
96 	ks8851_wrreg16(ks, KS_GRR, 0);
97 	mdelay(1);	/* wait for condition to clear */
98 }
99 
100 /**
101  * ks8851_set_powermode - set power mode of the device
102  * @ks: The device state
103  * @pwrmode: The power mode value to write to KS_PMECR.
104  *
105  * Change the power mode of the chip.
106  */
107 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
108 {
109 	unsigned pmecr;
110 
111 	netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
112 
113 	pmecr = ks8851_rdreg16(ks, KS_PMECR);
114 	pmecr &= ~PMECR_PM_MASK;
115 	pmecr |= pwrmode;
116 
117 	ks8851_wrreg16(ks, KS_PMECR, pmecr);
118 }
119 
120 /**
121  * ks8851_write_mac_addr - write mac address to device registers
122  * @dev: The network device
123  *
124  * Update the KS8851 MAC address registers from the address in @dev.
125  *
126  * This call assumes that the chip is not running, so there is no need to
127  * shutdown the RXQ process whilst setting this.
128 */
129 static int ks8851_write_mac_addr(struct net_device *dev)
130 {
131 	struct ks8851_net *ks = netdev_priv(dev);
132 	unsigned long flags;
133 	u16 val;
134 	int i;
135 
136 	ks8851_lock(ks, &flags);
137 
138 	/*
139 	 * Wake up chip in case it was powered off when stopped; otherwise,
140 	 * the first write to the MAC address does not take effect.
141 	 */
142 	ks8851_set_powermode(ks, PMECR_PM_NORMAL);
143 
144 	for (i = 0; i < ETH_ALEN; i += 2) {
145 		val = (dev->dev_addr[i] << 8) | dev->dev_addr[i + 1];
146 		ks8851_wrreg16(ks, KS_MAR(i), val);
147 	}
148 
149 	if (!netif_running(dev))
150 		ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
151 
152 	ks8851_unlock(ks, &flags);
153 
154 	return 0;
155 }
156 
157 /**
158  * ks8851_read_mac_addr - read mac address from device registers
159  * @dev: The network device
160  *
161  * Update our copy of the KS8851 MAC address from the registers of @dev.
162 */
163 static void ks8851_read_mac_addr(struct net_device *dev)
164 {
165 	struct ks8851_net *ks = netdev_priv(dev);
166 	unsigned long flags;
167 	u8 addr[ETH_ALEN];
168 	u16 reg;
169 	int i;
170 
171 	ks8851_lock(ks, &flags);
172 
173 	for (i = 0; i < ETH_ALEN; i += 2) {
174 		reg = ks8851_rdreg16(ks, KS_MAR(i));
175 		addr[i] = reg >> 8;
176 		addr[i + 1] = reg & 0xff;
177 	}
178 	eth_hw_addr_set(dev, addr);
179 
180 	ks8851_unlock(ks, &flags);
181 }
182 
183 /**
184  * ks8851_init_mac - initialise the mac address
185  * @ks: The device structure
186  * @np: The device node pointer
187  *
188  * Get or create the initial mac address for the device and then set that
189  * into the station address register. A mac address supplied in the device
190  * tree takes precedence. Otherwise, if there is an EEPROM present, then
191  * we try that. If no valid mac address is found we use eth_random_addr()
192  * to create a new one.
193  */
194 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
195 {
196 	struct net_device *dev = ks->netdev;
197 	int ret;
198 
199 	ret = of_get_ethdev_address(np, dev);
200 	if (!ret) {
201 		ks8851_write_mac_addr(dev);
202 		return;
203 	}
204 
205 	if (ks->rc_ccr & CCR_EEPROM) {
206 		ks8851_read_mac_addr(dev);
207 		if (is_valid_ether_addr(dev->dev_addr))
208 			return;
209 
210 		netdev_err(ks->netdev, "invalid mac address read %pM\n",
211 				dev->dev_addr);
212 	}
213 
214 	eth_hw_addr_random(dev);
215 	ks8851_write_mac_addr(dev);
216 }
217 
218 /**
219  * ks8851_dbg_dumpkkt - dump initial packet contents to debug
220  * @ks: The device state
221  * @rxpkt: The data for the received packet
222  *
223  * Dump the initial data from the packet to dev_dbg().
224  */
225 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
226 {
227 	netdev_dbg(ks->netdev,
228 		   "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
229 		   rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
230 		   rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
231 		   rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
232 }
233 
234 /**
235  * ks8851_rx_pkts - receive packets from the host
236  * @ks: The device information.
237  *
238  * This is called from the IRQ work queue when the system detects that there
239  * are packets in the receive queue. Find out how many packets there are and
240  * read them from the FIFO.
241  */
242 static void ks8851_rx_pkts(struct ks8851_net *ks)
243 {
244 	struct sk_buff *skb;
245 	unsigned rxfc;
246 	unsigned rxlen;
247 	unsigned rxstat;
248 	u8 *rxpkt;
249 
250 	rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
251 
252 	netif_dbg(ks, rx_status, ks->netdev,
253 		  "%s: %d packets\n", __func__, rxfc);
254 
255 	/* Currently we're issuing a read per packet, but we could possibly
256 	 * improve the code by issuing a single read, getting the receive
257 	 * header, allocating the packet and then reading the packet data
258 	 * out in one go.
259 	 *
260 	 * This form of operation would require us to hold the SPI bus'
261 	 * chipselect low during the entie transaction to avoid any
262 	 * reset to the data stream coming from the chip.
263 	 */
264 
265 	for (; rxfc != 0; rxfc--) {
266 		rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
267 		rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
268 
269 		netif_dbg(ks, rx_status, ks->netdev,
270 			  "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
271 
272 		/* the length of the packet includes the 32bit CRC */
273 
274 		/* set dma read address */
275 		ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
276 
277 		/* start DMA access */
278 		ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
279 
280 		if (rxlen > 4) {
281 			unsigned int rxalign;
282 
283 			rxlen -= 4;
284 			rxalign = ALIGN(rxlen, 4);
285 			skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
286 			if (skb) {
287 
288 				/* 4 bytes of status header + 4 bytes of
289 				 * garbage: we put them before ethernet
290 				 * header, so that they are copied,
291 				 * but ignored.
292 				 */
293 
294 				rxpkt = skb_put(skb, rxlen) - 8;
295 
296 				ks->rdfifo(ks, rxpkt, rxalign + 8);
297 
298 				if (netif_msg_pktdata(ks))
299 					ks8851_dbg_dumpkkt(ks, rxpkt);
300 
301 				skb->protocol = eth_type_trans(skb, ks->netdev);
302 				__netif_rx(skb);
303 
304 				ks->netdev->stats.rx_packets++;
305 				ks->netdev->stats.rx_bytes += rxlen;
306 			}
307 		}
308 
309 		/* end DMA access and dequeue packet */
310 		ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
311 	}
312 }
313 
314 /**
315  * ks8851_irq - IRQ handler for dealing with interrupt requests
316  * @irq: IRQ number
317  * @_ks: cookie
318  *
319  * This handler is invoked when the IRQ line asserts to find out what happened.
320  * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
321  * in thread context.
322  *
323  * Read the interrupt status, work out what needs to be done and then clear
324  * any of the interrupts that are not needed.
325  */
326 static irqreturn_t ks8851_irq(int irq, void *_ks)
327 {
328 	struct ks8851_net *ks = _ks;
329 	unsigned handled = 0;
330 	unsigned long flags;
331 	unsigned int status;
332 
333 	local_bh_disable();
334 
335 	ks8851_lock(ks, &flags);
336 
337 	status = ks8851_rdreg16(ks, KS_ISR);
338 
339 	netif_dbg(ks, intr, ks->netdev,
340 		  "%s: status 0x%04x\n", __func__, status);
341 
342 	if (status & IRQ_LCI)
343 		handled |= IRQ_LCI;
344 
345 	if (status & IRQ_LDI) {
346 		u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
347 		pmecr &= ~PMECR_WKEVT_MASK;
348 		ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
349 
350 		handled |= IRQ_LDI;
351 	}
352 
353 	if (status & IRQ_RXPSI)
354 		handled |= IRQ_RXPSI;
355 
356 	if (status & IRQ_TXI) {
357 		unsigned short tx_space = ks8851_rdreg16(ks, KS_TXMIR);
358 
359 		netif_dbg(ks, intr, ks->netdev,
360 			  "%s: txspace %d\n", __func__, tx_space);
361 
362 		spin_lock(&ks->statelock);
363 		ks->tx_space = tx_space;
364 		if (netif_queue_stopped(ks->netdev))
365 			netif_wake_queue(ks->netdev);
366 		spin_unlock(&ks->statelock);
367 
368 		handled |= IRQ_TXI;
369 	}
370 
371 	if (status & IRQ_RXI)
372 		handled |= IRQ_RXI;
373 
374 	if (status & IRQ_SPIBEI) {
375 		netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
376 		handled |= IRQ_SPIBEI;
377 	}
378 
379 	ks8851_wrreg16(ks, KS_ISR, handled);
380 
381 	if (status & IRQ_RXI) {
382 		/* the datasheet says to disable the rx interrupt during
383 		 * packet read-out, however we're masking the interrupt
384 		 * from the device so do not bother masking just the RX
385 		 * from the device. */
386 
387 		ks8851_rx_pkts(ks);
388 	}
389 
390 	/* if something stopped the rx process, probably due to wanting
391 	 * to change the rx settings, then do something about restarting
392 	 * it. */
393 	if (status & IRQ_RXPSI) {
394 		struct ks8851_rxctrl *rxc = &ks->rxctrl;
395 
396 		/* update the multicast hash table */
397 		ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
398 		ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
399 		ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
400 		ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
401 
402 		ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
403 		ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
404 	}
405 
406 	ks8851_unlock(ks, &flags);
407 
408 	if (status & IRQ_LCI)
409 		mii_check_link(&ks->mii);
410 
411 	local_bh_enable();
412 
413 	return IRQ_HANDLED;
414 }
415 
416 /**
417  * ks8851_flush_tx_work - flush outstanding TX work
418  * @ks: The device state
419  */
420 static void ks8851_flush_tx_work(struct ks8851_net *ks)
421 {
422 	if (ks->flush_tx_work)
423 		ks->flush_tx_work(ks);
424 }
425 
426 /**
427  * ks8851_net_open - open network device
428  * @dev: The network device being opened.
429  *
430  * Called when the network device is marked active, such as a user executing
431  * 'ifconfig up' on the device.
432  */
433 static int ks8851_net_open(struct net_device *dev)
434 {
435 	struct ks8851_net *ks = netdev_priv(dev);
436 	unsigned long flags;
437 	int ret;
438 
439 	ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
440 				   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
441 				   dev->name, ks);
442 	if (ret < 0) {
443 		netdev_err(dev, "failed to get irq\n");
444 		return ret;
445 	}
446 
447 	/* lock the card, even if we may not actually be doing anything
448 	 * else at the moment */
449 	ks8851_lock(ks, &flags);
450 
451 	netif_dbg(ks, ifup, ks->netdev, "opening\n");
452 
453 	/* bring chip out of any power saving mode it was in */
454 	ks8851_set_powermode(ks, PMECR_PM_NORMAL);
455 
456 	/* issue a soft reset to the RX/TX QMU to put it into a known
457 	 * state. */
458 	ks8851_soft_reset(ks, GRR_QMU);
459 
460 	/* setup transmission parameters */
461 
462 	ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
463 				     TXCR_TXPE | /* pad to min length */
464 				     TXCR_TXCRC | /* add CRC */
465 				     TXCR_TXFCE)); /* enable flow control */
466 
467 	/* auto-increment tx data, reset tx pointer */
468 	ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
469 
470 	/* setup receiver control */
471 
472 	ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /*  from mac filter */
473 				      RXCR1_RXFCE | /* enable flow control */
474 				      RXCR1_RXBE | /* broadcast enable */
475 				      RXCR1_RXUE | /* unicast enable */
476 				      RXCR1_RXE)); /* enable rx block */
477 
478 	/* transfer entire frames out in one go */
479 	ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
480 
481 	/* set receive counter timeouts */
482 	ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
483 	ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
484 	ks8851_wrreg16(ks, KS_RXFCTR, 10);  /* 10 frames to IRQ */
485 
486 	ks->rc_rxqcr = (RXQCR_RXFCTE |  /* IRQ on frame count exceeded */
487 			RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
488 			RXQCR_RXDTTE);  /* IRQ on time exceeded */
489 
490 	ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
491 
492 	/* clear then enable interrupts */
493 	ks8851_wrreg16(ks, KS_ISR, ks->rc_ier);
494 	ks8851_wrreg16(ks, KS_IER, ks->rc_ier);
495 
496 	ks->queued_len = 0;
497 	netif_start_queue(ks->netdev);
498 
499 	netif_dbg(ks, ifup, ks->netdev, "network device up\n");
500 
501 	ks8851_unlock(ks, &flags);
502 	mii_check_link(&ks->mii);
503 	return 0;
504 }
505 
506 /**
507  * ks8851_net_stop - close network device
508  * @dev: The device being closed.
509  *
510  * Called to close down a network device which has been active. Cancell any
511  * work, shutdown the RX and TX process and then place the chip into a low
512  * power state whilst it is not being used.
513  */
514 static int ks8851_net_stop(struct net_device *dev)
515 {
516 	struct ks8851_net *ks = netdev_priv(dev);
517 	unsigned long flags;
518 
519 	netif_info(ks, ifdown, dev, "shutting down\n");
520 
521 	netif_stop_queue(dev);
522 
523 	ks8851_lock(ks, &flags);
524 	/* turn off the IRQs and ack any outstanding */
525 	ks8851_wrreg16(ks, KS_IER, 0x0000);
526 	ks8851_wrreg16(ks, KS_ISR, 0xffff);
527 	ks8851_unlock(ks, &flags);
528 
529 	/* stop any outstanding work */
530 	ks8851_flush_tx_work(ks);
531 	flush_work(&ks->rxctrl_work);
532 
533 	ks8851_lock(ks, &flags);
534 	/* shutdown RX process */
535 	ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
536 
537 	/* shutdown TX process */
538 	ks8851_wrreg16(ks, KS_TXCR, 0x0000);
539 
540 	/* set powermode to soft power down to save power */
541 	ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
542 	ks8851_unlock(ks, &flags);
543 
544 	/* ensure any queued tx buffers are dumped */
545 	while (!skb_queue_empty(&ks->txq)) {
546 		struct sk_buff *txb = skb_dequeue(&ks->txq);
547 
548 		netif_dbg(ks, ifdown, ks->netdev,
549 			  "%s: freeing txb %p\n", __func__, txb);
550 
551 		dev_kfree_skb(txb);
552 	}
553 
554 	free_irq(dev->irq, ks);
555 
556 	return 0;
557 }
558 
559 /**
560  * ks8851_start_xmit - transmit packet
561  * @skb: The buffer to transmit
562  * @dev: The device used to transmit the packet.
563  *
564  * Called by the network layer to transmit the @skb. Queue the packet for
565  * the device and schedule the necessary work to transmit the packet when
566  * it is free.
567  *
568  * We do this to firstly avoid sleeping with the network device locked,
569  * and secondly so we can round up more than one packet to transmit which
570  * means we can try and avoid generating too many transmit done interrupts.
571  */
572 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
573 				     struct net_device *dev)
574 {
575 	struct ks8851_net *ks = netdev_priv(dev);
576 
577 	return ks->start_xmit(skb, dev);
578 }
579 
580 /**
581  * ks8851_rxctrl_work - work handler to change rx mode
582  * @work: The work structure this belongs to.
583  *
584  * Lock the device and issue the necessary changes to the receive mode from
585  * the network device layer. This is done so that we can do this without
586  * having to sleep whilst holding the network device lock.
587  *
588  * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
589  * receive parameters are programmed, we issue a write to disable the RXQ and
590  * then wait for the interrupt handler to be triggered once the RXQ shutdown is
591  * complete. The interrupt handler then writes the new values into the chip.
592  */
593 static void ks8851_rxctrl_work(struct work_struct *work)
594 {
595 	struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
596 	unsigned long flags;
597 
598 	ks8851_lock(ks, &flags);
599 
600 	/* need to shutdown RXQ before modifying filter parameters */
601 	ks8851_wrreg16(ks, KS_RXCR1, 0x00);
602 
603 	ks8851_unlock(ks, &flags);
604 }
605 
606 static void ks8851_set_rx_mode(struct net_device *dev)
607 {
608 	struct ks8851_net *ks = netdev_priv(dev);
609 	struct ks8851_rxctrl rxctrl;
610 
611 	memset(&rxctrl, 0, sizeof(rxctrl));
612 
613 	if (dev->flags & IFF_PROMISC) {
614 		/* interface to receive everything */
615 
616 		rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
617 	} else if (dev->flags & IFF_ALLMULTI) {
618 		/* accept all multicast packets */
619 
620 		rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
621 				RXCR1_RXPAFMA | RXCR1_RXMAFMA);
622 	} else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
623 		struct netdev_hw_addr *ha;
624 		u32 crc;
625 
626 		/* accept some multicast */
627 
628 		netdev_for_each_mc_addr(ha, dev) {
629 			crc = ether_crc(ETH_ALEN, ha->addr);
630 			crc >>= (32 - 6);  /* get top six bits */
631 
632 			rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
633 		}
634 
635 		rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
636 	} else {
637 		/* just accept broadcast / unicast */
638 		rxctrl.rxcr1 = RXCR1_RXPAFMA;
639 	}
640 
641 	rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
642 			 RXCR1_RXBE | /* broadcast enable */
643 			 RXCR1_RXE | /* RX process enable */
644 			 RXCR1_RXFCE); /* enable flow control */
645 
646 	rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
647 
648 	/* schedule work to do the actual set of the data if needed */
649 
650 	spin_lock(&ks->statelock);
651 
652 	if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
653 		memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
654 		schedule_work(&ks->rxctrl_work);
655 	}
656 
657 	spin_unlock(&ks->statelock);
658 }
659 
660 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
661 {
662 	struct sockaddr *sa = addr;
663 
664 	if (netif_running(dev))
665 		return -EBUSY;
666 
667 	if (!is_valid_ether_addr(sa->sa_data))
668 		return -EADDRNOTAVAIL;
669 
670 	eth_hw_addr_set(dev, sa->sa_data);
671 	return ks8851_write_mac_addr(dev);
672 }
673 
674 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
675 {
676 	struct ks8851_net *ks = netdev_priv(dev);
677 
678 	if (!netif_running(dev))
679 		return -EINVAL;
680 
681 	return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
682 }
683 
684 static const struct net_device_ops ks8851_netdev_ops = {
685 	.ndo_open		= ks8851_net_open,
686 	.ndo_stop		= ks8851_net_stop,
687 	.ndo_eth_ioctl		= ks8851_net_ioctl,
688 	.ndo_start_xmit		= ks8851_start_xmit,
689 	.ndo_set_mac_address	= ks8851_set_mac_address,
690 	.ndo_set_rx_mode	= ks8851_set_rx_mode,
691 	.ndo_validate_addr	= eth_validate_addr,
692 };
693 
694 /* ethtool support */
695 
696 static void ks8851_get_drvinfo(struct net_device *dev,
697 			       struct ethtool_drvinfo *di)
698 {
699 	strscpy(di->driver, "KS8851", sizeof(di->driver));
700 	strscpy(di->version, "1.00", sizeof(di->version));
701 	strscpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
702 }
703 
704 static u32 ks8851_get_msglevel(struct net_device *dev)
705 {
706 	struct ks8851_net *ks = netdev_priv(dev);
707 	return ks->msg_enable;
708 }
709 
710 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
711 {
712 	struct ks8851_net *ks = netdev_priv(dev);
713 	ks->msg_enable = to;
714 }
715 
716 static int ks8851_get_link_ksettings(struct net_device *dev,
717 				     struct ethtool_link_ksettings *cmd)
718 {
719 	struct ks8851_net *ks = netdev_priv(dev);
720 
721 	mii_ethtool_get_link_ksettings(&ks->mii, cmd);
722 
723 	return 0;
724 }
725 
726 static int ks8851_set_link_ksettings(struct net_device *dev,
727 				     const struct ethtool_link_ksettings *cmd)
728 {
729 	struct ks8851_net *ks = netdev_priv(dev);
730 	return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
731 }
732 
733 static u32 ks8851_get_link(struct net_device *dev)
734 {
735 	struct ks8851_net *ks = netdev_priv(dev);
736 	return mii_link_ok(&ks->mii);
737 }
738 
739 static int ks8851_nway_reset(struct net_device *dev)
740 {
741 	struct ks8851_net *ks = netdev_priv(dev);
742 	return mii_nway_restart(&ks->mii);
743 }
744 
745 /* EEPROM support */
746 
747 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
748 {
749 	struct ks8851_net *ks = ee->data;
750 	unsigned val;
751 
752 	val = ks8851_rdreg16(ks, KS_EEPCR);
753 
754 	ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
755 	ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
756 	ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
757 }
758 
759 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
760 {
761 	struct ks8851_net *ks = ee->data;
762 	unsigned val = EEPCR_EESA;	/* default - eeprom access on */
763 
764 	if (ee->drive_data)
765 		val |= EEPCR_EESRWA;
766 	if (ee->reg_data_in)
767 		val |= EEPCR_EEDO;
768 	if (ee->reg_data_clock)
769 		val |= EEPCR_EESCK;
770 	if (ee->reg_chip_select)
771 		val |= EEPCR_EECS;
772 
773 	ks8851_wrreg16(ks, KS_EEPCR, val);
774 }
775 
776 /**
777  * ks8851_eeprom_claim - claim device EEPROM and activate the interface
778  * @ks: The network device state.
779  *
780  * Check for the presence of an EEPROM, and then activate software access
781  * to the device.
782  */
783 static int ks8851_eeprom_claim(struct ks8851_net *ks)
784 {
785 	/* start with clock low, cs high */
786 	ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
787 	return 0;
788 }
789 
790 /**
791  * ks8851_eeprom_release - release the EEPROM interface
792  * @ks: The device state
793  *
794  * Release the software access to the device EEPROM
795  */
796 static void ks8851_eeprom_release(struct ks8851_net *ks)
797 {
798 	unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
799 
800 	ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
801 }
802 
803 #define KS_EEPROM_MAGIC (0x00008851)
804 
805 static int ks8851_set_eeprom(struct net_device *dev,
806 			     struct ethtool_eeprom *ee, u8 *data)
807 {
808 	struct ks8851_net *ks = netdev_priv(dev);
809 	int offset = ee->offset;
810 	unsigned long flags;
811 	int len = ee->len;
812 	u16 tmp;
813 
814 	/* currently only support byte writing */
815 	if (len != 1)
816 		return -EINVAL;
817 
818 	if (ee->magic != KS_EEPROM_MAGIC)
819 		return -EINVAL;
820 
821 	if (!(ks->rc_ccr & CCR_EEPROM))
822 		return -ENOENT;
823 
824 	ks8851_lock(ks, &flags);
825 
826 	ks8851_eeprom_claim(ks);
827 
828 	eeprom_93cx6_wren(&ks->eeprom, true);
829 
830 	/* ethtool currently only supports writing bytes, which means
831 	 * we have to read/modify/write our 16bit EEPROMs */
832 
833 	eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
834 
835 	if (offset & 1) {
836 		tmp &= 0xff;
837 		tmp |= *data << 8;
838 	} else {
839 		tmp &= 0xff00;
840 		tmp |= *data;
841 	}
842 
843 	eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
844 	eeprom_93cx6_wren(&ks->eeprom, false);
845 
846 	ks8851_eeprom_release(ks);
847 	ks8851_unlock(ks, &flags);
848 
849 	return 0;
850 }
851 
852 static int ks8851_get_eeprom(struct net_device *dev,
853 			     struct ethtool_eeprom *ee, u8 *data)
854 {
855 	struct ks8851_net *ks = netdev_priv(dev);
856 	int offset = ee->offset;
857 	unsigned long flags;
858 	int len = ee->len;
859 
860 	/* must be 2 byte aligned */
861 	if (len & 1 || offset & 1)
862 		return -EINVAL;
863 
864 	if (!(ks->rc_ccr & CCR_EEPROM))
865 		return -ENOENT;
866 
867 	ks8851_lock(ks, &flags);
868 
869 	ks8851_eeprom_claim(ks);
870 
871 	ee->magic = KS_EEPROM_MAGIC;
872 
873 	eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
874 	ks8851_eeprom_release(ks);
875 	ks8851_unlock(ks, &flags);
876 
877 	return 0;
878 }
879 
880 static int ks8851_get_eeprom_len(struct net_device *dev)
881 {
882 	struct ks8851_net *ks = netdev_priv(dev);
883 
884 	/* currently, we assume it is an 93C46 attached, so return 128 */
885 	return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
886 }
887 
888 static const struct ethtool_ops ks8851_ethtool_ops = {
889 	.get_drvinfo	= ks8851_get_drvinfo,
890 	.get_msglevel	= ks8851_get_msglevel,
891 	.set_msglevel	= ks8851_set_msglevel,
892 	.get_link	= ks8851_get_link,
893 	.nway_reset	= ks8851_nway_reset,
894 	.get_eeprom_len	= ks8851_get_eeprom_len,
895 	.get_eeprom	= ks8851_get_eeprom,
896 	.set_eeprom	= ks8851_set_eeprom,
897 	.get_link_ksettings = ks8851_get_link_ksettings,
898 	.set_link_ksettings = ks8851_set_link_ksettings,
899 };
900 
901 /* MII interface controls */
902 
903 /**
904  * ks8851_phy_reg - convert MII register into a KS8851 register
905  * @reg: MII register number.
906  *
907  * Return the KS8851 register number for the corresponding MII PHY register
908  * if possible. Return zero if the MII register has no direct mapping to the
909  * KS8851 register set.
910  */
911 static int ks8851_phy_reg(int reg)
912 {
913 	switch (reg) {
914 	case MII_BMCR:
915 		return KS_P1MBCR;
916 	case MII_BMSR:
917 		return KS_P1MBSR;
918 	case MII_PHYSID1:
919 		return KS_PHY1ILR;
920 	case MII_PHYSID2:
921 		return KS_PHY1IHR;
922 	case MII_ADVERTISE:
923 		return KS_P1ANAR;
924 	case MII_LPA:
925 		return KS_P1ANLPR;
926 	}
927 
928 	return -EOPNOTSUPP;
929 }
930 
931 static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg)
932 {
933 	struct ks8851_net *ks = netdev_priv(dev);
934 	unsigned long flags;
935 	int result;
936 	int ksreg;
937 
938 	ksreg = ks8851_phy_reg(reg);
939 	if (ksreg < 0)
940 		return ksreg;
941 
942 	ks8851_lock(ks, &flags);
943 	result = ks8851_rdreg16(ks, ksreg);
944 	ks8851_unlock(ks, &flags);
945 
946 	return result;
947 }
948 
949 /**
950  * ks8851_phy_read - MII interface PHY register read.
951  * @dev: The network device the PHY is on.
952  * @phy_addr: Address of PHY (ignored as we only have one)
953  * @reg: The register to read.
954  *
955  * This call reads data from the PHY register specified in @reg. Since the
956  * device does not support all the MII registers, the non-existent values
957  * are always returned as zero.
958  *
959  * We return zero for unsupported registers as the MII code does not check
960  * the value returned for any error status, and simply returns it to the
961  * caller. The mii-tool that the driver was tested with takes any -ve error
962  * as real PHY capabilities, thus displaying incorrect data to the user.
963  */
964 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
965 {
966 	int ret;
967 
968 	ret = ks8851_phy_read_common(dev, phy_addr, reg);
969 	if (ret < 0)
970 		return 0x0;	/* no error return allowed, so use zero */
971 
972 	return ret;
973 }
974 
975 static void ks8851_phy_write(struct net_device *dev,
976 			     int phy, int reg, int value)
977 {
978 	struct ks8851_net *ks = netdev_priv(dev);
979 	unsigned long flags;
980 	int ksreg;
981 
982 	ksreg = ks8851_phy_reg(reg);
983 	if (ksreg >= 0) {
984 		ks8851_lock(ks, &flags);
985 		ks8851_wrreg16(ks, ksreg, value);
986 		ks8851_unlock(ks, &flags);
987 	}
988 }
989 
990 static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg)
991 {
992 	struct ks8851_net *ks = bus->priv;
993 
994 	if (phy_id != 0)
995 		return -EOPNOTSUPP;
996 
997 	/* KS8851 PHY ID registers are swapped in HW, swap them back. */
998 	if (reg == MII_PHYSID1)
999 		reg = MII_PHYSID2;
1000 	else if (reg == MII_PHYSID2)
1001 		reg = MII_PHYSID1;
1002 
1003 	return ks8851_phy_read_common(ks->netdev, phy_id, reg);
1004 }
1005 
1006 static int ks8851_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
1007 {
1008 	struct ks8851_net *ks = bus->priv;
1009 
1010 	ks8851_phy_write(ks->netdev, phy_id, reg, val);
1011 	return 0;
1012 }
1013 
1014 /**
1015  * ks8851_read_selftest - read the selftest memory info.
1016  * @ks: The device state
1017  *
1018  * Read and check the TX/RX memory selftest information.
1019  */
1020 static void ks8851_read_selftest(struct ks8851_net *ks)
1021 {
1022 	unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1023 	unsigned rd;
1024 
1025 	rd = ks8851_rdreg16(ks, KS_MBIR);
1026 
1027 	if ((rd & both_done) != both_done) {
1028 		netdev_warn(ks->netdev, "Memory selftest not finished\n");
1029 		return;
1030 	}
1031 
1032 	if (rd & MBIR_TXMBFA)
1033 		netdev_err(ks->netdev, "TX memory selftest fail\n");
1034 
1035 	if (rd & MBIR_RXMBFA)
1036 		netdev_err(ks->netdev, "RX memory selftest fail\n");
1037 }
1038 
1039 /* driver bus management functions */
1040 
1041 #ifdef CONFIG_PM_SLEEP
1042 
1043 int ks8851_suspend(struct device *dev)
1044 {
1045 	struct ks8851_net *ks = dev_get_drvdata(dev);
1046 	struct net_device *netdev = ks->netdev;
1047 
1048 	if (netif_running(netdev)) {
1049 		netif_device_detach(netdev);
1050 		ks8851_net_stop(netdev);
1051 	}
1052 
1053 	return 0;
1054 }
1055 EXPORT_SYMBOL_GPL(ks8851_suspend);
1056 
1057 int ks8851_resume(struct device *dev)
1058 {
1059 	struct ks8851_net *ks = dev_get_drvdata(dev);
1060 	struct net_device *netdev = ks->netdev;
1061 
1062 	if (netif_running(netdev)) {
1063 		ks8851_net_open(netdev);
1064 		netif_device_attach(netdev);
1065 	}
1066 
1067 	return 0;
1068 }
1069 EXPORT_SYMBOL_GPL(ks8851_resume);
1070 #endif
1071 
1072 static int ks8851_register_mdiobus(struct ks8851_net *ks, struct device *dev)
1073 {
1074 	struct mii_bus *mii_bus;
1075 	int ret;
1076 
1077 	mii_bus = mdiobus_alloc();
1078 	if (!mii_bus)
1079 		return -ENOMEM;
1080 
1081 	mii_bus->name = "ks8851_eth_mii";
1082 	mii_bus->read = ks8851_mdio_read;
1083 	mii_bus->write = ks8851_mdio_write;
1084 	mii_bus->priv = ks;
1085 	mii_bus->parent = dev;
1086 	mii_bus->phy_mask = ~((u32)BIT(0));
1087 	snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
1088 
1089 	ret = mdiobus_register(mii_bus);
1090 	if (ret)
1091 		goto err_mdiobus_register;
1092 
1093 	ks->mii_bus = mii_bus;
1094 
1095 	return 0;
1096 
1097 err_mdiobus_register:
1098 	mdiobus_free(mii_bus);
1099 	return ret;
1100 }
1101 
1102 static void ks8851_unregister_mdiobus(struct ks8851_net *ks)
1103 {
1104 	mdiobus_unregister(ks->mii_bus);
1105 	mdiobus_free(ks->mii_bus);
1106 }
1107 
1108 int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1109 			int msg_en)
1110 {
1111 	struct ks8851_net *ks = netdev_priv(netdev);
1112 	unsigned cider;
1113 	int ret;
1114 
1115 	ks->netdev = netdev;
1116 	ks->tx_space = 6144;
1117 
1118 	ks->gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1119 	ret = PTR_ERR_OR_ZERO(ks->gpio);
1120 	if (ret) {
1121 		if (ret != -EPROBE_DEFER)
1122 			dev_err(dev, "reset gpio request failed: %d\n", ret);
1123 		return ret;
1124 	}
1125 
1126 	ret = gpiod_set_consumer_name(ks->gpio, "ks8851_rst_n");
1127 	if (ret) {
1128 		dev_err(dev, "failed to set reset gpio name: %d\n", ret);
1129 		return ret;
1130 	}
1131 
1132 	ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1133 	if (IS_ERR(ks->vdd_io)) {
1134 		ret = PTR_ERR(ks->vdd_io);
1135 		goto err_reg_io;
1136 	}
1137 
1138 	ret = regulator_enable(ks->vdd_io);
1139 	if (ret) {
1140 		dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1141 		goto err_reg_io;
1142 	}
1143 
1144 	ks->vdd_reg = devm_regulator_get(dev, "vdd");
1145 	if (IS_ERR(ks->vdd_reg)) {
1146 		ret = PTR_ERR(ks->vdd_reg);
1147 		goto err_reg;
1148 	}
1149 
1150 	ret = regulator_enable(ks->vdd_reg);
1151 	if (ret) {
1152 		dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1153 		goto err_reg;
1154 	}
1155 
1156 	if (ks->gpio) {
1157 		usleep_range(10000, 11000);
1158 		gpiod_set_value_cansleep(ks->gpio, 0);
1159 	}
1160 
1161 	spin_lock_init(&ks->statelock);
1162 
1163 	INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1164 
1165 	SET_NETDEV_DEV(netdev, dev);
1166 
1167 	/* setup EEPROM state */
1168 	ks->eeprom.data = ks;
1169 	ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1170 	ks->eeprom.register_read = ks8851_eeprom_regread;
1171 	ks->eeprom.register_write = ks8851_eeprom_regwrite;
1172 
1173 	/* setup mii state */
1174 	ks->mii.dev		= netdev;
1175 	ks->mii.phy_id		= 1;
1176 	ks->mii.phy_id_mask	= 1;
1177 	ks->mii.reg_num_mask	= 0xf;
1178 	ks->mii.mdio_read	= ks8851_phy_read;
1179 	ks->mii.mdio_write	= ks8851_phy_write;
1180 
1181 	dev_info(dev, "message enable is %d\n", msg_en);
1182 
1183 	ret = ks8851_register_mdiobus(ks, dev);
1184 	if (ret)
1185 		goto err_mdio;
1186 
1187 	/* set the default message enable */
1188 	ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1189 						NETIF_MSG_PROBE |
1190 						NETIF_MSG_LINK);
1191 
1192 	skb_queue_head_init(&ks->txq);
1193 
1194 	netdev->ethtool_ops = &ks8851_ethtool_ops;
1195 
1196 	dev_set_drvdata(dev, ks);
1197 
1198 	netif_carrier_off(ks->netdev);
1199 	netdev->if_port = IF_PORT_100BASET;
1200 	netdev->netdev_ops = &ks8851_netdev_ops;
1201 
1202 	/* issue a global soft reset to reset the device. */
1203 	ks8851_soft_reset(ks, GRR_GSR);
1204 
1205 	/* simple check for a valid chip being connected to the bus */
1206 	cider = ks8851_rdreg16(ks, KS_CIDER);
1207 	if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1208 		dev_err(dev, "failed to read device ID\n");
1209 		ret = -ENODEV;
1210 		goto err_id;
1211 	}
1212 
1213 	/* cache the contents of the CCR register for EEPROM, etc. */
1214 	ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1215 
1216 	ks8851_read_selftest(ks);
1217 	ks8851_init_mac(ks, dev->of_node);
1218 
1219 	ret = register_netdev(netdev);
1220 	if (ret) {
1221 		dev_err(dev, "failed to register network device\n");
1222 		goto err_id;
1223 	}
1224 
1225 	netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1226 		    CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1227 		    ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1228 
1229 	return 0;
1230 
1231 err_id:
1232 	ks8851_unregister_mdiobus(ks);
1233 err_mdio:
1234 	if (ks->gpio)
1235 		gpiod_set_value_cansleep(ks->gpio, 1);
1236 	regulator_disable(ks->vdd_reg);
1237 err_reg:
1238 	regulator_disable(ks->vdd_io);
1239 err_reg_io:
1240 	return ret;
1241 }
1242 EXPORT_SYMBOL_GPL(ks8851_probe_common);
1243 
1244 void ks8851_remove_common(struct device *dev)
1245 {
1246 	struct ks8851_net *priv = dev_get_drvdata(dev);
1247 
1248 	ks8851_unregister_mdiobus(priv);
1249 
1250 	if (netif_msg_drv(priv))
1251 		dev_info(dev, "remove\n");
1252 
1253 	unregister_netdev(priv->netdev);
1254 	if (priv->gpio)
1255 		gpiod_set_value_cansleep(priv->gpio, 1);
1256 	regulator_disable(priv->vdd_reg);
1257 	regulator_disable(priv->vdd_io);
1258 }
1259 EXPORT_SYMBOL_GPL(ks8851_remove_common);
1260 
1261 MODULE_DESCRIPTION("KS8851 Network driver");
1262 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1263 MODULE_LICENSE("GPL");
1264