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