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