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