1
2
3
4
5
6
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
31
32
33
34
35
36static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
37{
38 ks->lock(ks, flags);
39}
40
41
42
43
44
45
46
47
48static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
49{
50 ks->unlock(ks, flags);
51}
52
53
54
55
56
57
58
59
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
69
70
71
72
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
82
83
84
85
86
87
88
89
90
91
92
93static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
94{
95 ks8851_wrreg16(ks, KS_GRR, op);
96 mdelay(1);
97 ks8851_wrreg16(ks, KS_GRR, 0);
98 mdelay(1);
99}
100
101
102
103
104
105
106
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
123
124
125
126
127
128
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
141
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
160
161
162
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 u8 addr[ETH_ALEN];
169 u16 reg;
170 int i;
171
172 ks8851_lock(ks, &flags);
173
174 for (i = 0; i < ETH_ALEN; i += 2) {
175 reg = ks8851_rdreg16(ks, KS_MAR(i));
176 addr[i] = reg >> 8;
177 addr[i + 1] = reg & 0xff;
178 }
179 eth_hw_addr_set(dev, addr);
180
181 ks8851_unlock(ks, &flags);
182}
183
184
185
186
187
188
189
190
191
192
193
194
195static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
196{
197 struct net_device *dev = ks->netdev;
198 int ret;
199
200 ret = of_get_ethdev_address(np, dev);
201 if (!ret) {
202 ks8851_write_mac_addr(dev);
203 return;
204 }
205
206 if (ks->rc_ccr & CCR_EEPROM) {
207 ks8851_read_mac_addr(dev);
208 if (is_valid_ether_addr(dev->dev_addr))
209 return;
210
211 netdev_err(ks->netdev, "invalid mac address read %pM\n",
212 dev->dev_addr);
213 }
214
215 eth_hw_addr_random(dev);
216 ks8851_write_mac_addr(dev);
217}
218
219
220
221
222
223
224
225
226static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
227{
228 netdev_dbg(ks->netdev,
229 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
230 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
231 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
232 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
233}
234
235
236
237
238
239
240static void ks8851_rx_skb(struct ks8851_net *ks, struct sk_buff *skb)
241{
242 ks->rx_skb(ks, skb);
243}
244
245
246
247
248
249
250
251
252
253static void ks8851_rx_pkts(struct ks8851_net *ks)
254{
255 struct sk_buff *skb;
256 unsigned rxfc;
257 unsigned rxlen;
258 unsigned rxstat;
259 u8 *rxpkt;
260
261 rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
262
263 netif_dbg(ks, rx_status, ks->netdev,
264 "%s: %d packets\n", __func__, rxfc);
265
266
267
268
269
270
271
272
273
274
275
276 for (; rxfc != 0; rxfc--) {
277 rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
278 rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
279
280 netif_dbg(ks, rx_status, ks->netdev,
281 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
282
283
284
285
286 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
287
288
289 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
290
291 if (rxlen > 4) {
292 unsigned int rxalign;
293
294 rxlen -= 4;
295 rxalign = ALIGN(rxlen, 4);
296 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
297 if (skb) {
298
299
300
301
302
303
304
305 rxpkt = skb_put(skb, rxlen) - 8;
306
307 ks->rdfifo(ks, rxpkt, rxalign + 8);
308
309 if (netif_msg_pktdata(ks))
310 ks8851_dbg_dumpkkt(ks, rxpkt);
311
312 skb->protocol = eth_type_trans(skb, ks->netdev);
313 ks8851_rx_skb(ks, skb);
314
315 ks->netdev->stats.rx_packets++;
316 ks->netdev->stats.rx_bytes += rxlen;
317 }
318 }
319
320
321 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
322 }
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337static irqreturn_t ks8851_irq(int irq, void *_ks)
338{
339 struct ks8851_net *ks = _ks;
340 unsigned handled = 0;
341 unsigned long flags;
342 unsigned int status;
343
344 ks8851_lock(ks, &flags);
345
346 status = ks8851_rdreg16(ks, KS_ISR);
347
348 netif_dbg(ks, intr, ks->netdev,
349 "%s: status 0x%04x\n", __func__, status);
350
351 if (status & IRQ_LCI)
352 handled |= IRQ_LCI;
353
354 if (status & IRQ_LDI) {
355 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
356 pmecr &= ~PMECR_WKEVT_MASK;
357 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
358
359 handled |= IRQ_LDI;
360 }
361
362 if (status & IRQ_RXPSI)
363 handled |= IRQ_RXPSI;
364
365 if (status & IRQ_TXI) {
366 handled |= IRQ_TXI;
367
368
369
370
371
372 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
373
374 netif_dbg(ks, intr, ks->netdev,
375 "%s: txspace %d\n", __func__, ks->tx_space);
376 }
377
378 if (status & IRQ_RXI)
379 handled |= IRQ_RXI;
380
381 if (status & IRQ_SPIBEI) {
382 netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
383 handled |= IRQ_SPIBEI;
384 }
385
386 ks8851_wrreg16(ks, KS_ISR, handled);
387
388 if (status & IRQ_RXI) {
389
390
391
392
393
394 ks8851_rx_pkts(ks);
395 }
396
397
398
399
400 if (status & IRQ_RXPSI) {
401 struct ks8851_rxctrl *rxc = &ks->rxctrl;
402
403
404 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
405 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
406 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
407 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
408
409 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
410 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
411 }
412
413 ks8851_unlock(ks, &flags);
414
415 if (status & IRQ_LCI)
416 mii_check_link(&ks->mii);
417
418 if (status & IRQ_TXI)
419 netif_wake_queue(ks->netdev);
420
421 return IRQ_HANDLED;
422}
423
424
425
426
427
428static void ks8851_flush_tx_work(struct ks8851_net *ks)
429{
430 if (ks->flush_tx_work)
431 ks->flush_tx_work(ks);
432}
433
434
435
436
437
438
439
440
441static int ks8851_net_open(struct net_device *dev)
442{
443 struct ks8851_net *ks = netdev_priv(dev);
444 unsigned long flags;
445 int ret;
446
447 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
448 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
449 dev->name, ks);
450 if (ret < 0) {
451 netdev_err(dev, "failed to get irq\n");
452 return ret;
453 }
454
455
456
457 ks8851_lock(ks, &flags);
458
459 netif_dbg(ks, ifup, ks->netdev, "opening\n");
460
461
462 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
463
464
465
466 ks8851_soft_reset(ks, GRR_QMU);
467
468
469
470 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE |
471 TXCR_TXPE |
472 TXCR_TXCRC |
473 TXCR_TXFCE));
474
475
476 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
477
478
479
480 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA |
481 RXCR1_RXFCE |
482 RXCR1_RXBE |
483 RXCR1_RXUE |
484 RXCR1_RXE));
485
486
487 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
488
489
490 ks8851_wrreg16(ks, KS_RXDTTR, 1000);
491 ks8851_wrreg16(ks, KS_RXDBCTR, 4096);
492 ks8851_wrreg16(ks, KS_RXFCTR, 10);
493
494 ks->rc_rxqcr = (RXQCR_RXFCTE |
495 RXQCR_RXDBCTE |
496 RXQCR_RXDTTE);
497
498 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
499
500
501 ks8851_wrreg16(ks, KS_ISR, ks->rc_ier);
502 ks8851_wrreg16(ks, KS_IER, ks->rc_ier);
503
504 netif_start_queue(ks->netdev);
505
506 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
507
508 ks8851_unlock(ks, &flags);
509 mii_check_link(&ks->mii);
510 return 0;
511}
512
513
514
515
516
517
518
519
520
521static int ks8851_net_stop(struct net_device *dev)
522{
523 struct ks8851_net *ks = netdev_priv(dev);
524 unsigned long flags;
525
526 netif_info(ks, ifdown, dev, "shutting down\n");
527
528 netif_stop_queue(dev);
529
530 ks8851_lock(ks, &flags);
531
532 ks8851_wrreg16(ks, KS_IER, 0x0000);
533 ks8851_wrreg16(ks, KS_ISR, 0xffff);
534 ks8851_unlock(ks, &flags);
535
536
537 ks8851_flush_tx_work(ks);
538 flush_work(&ks->rxctrl_work);
539
540 ks8851_lock(ks, &flags);
541
542 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
543
544
545 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
546
547
548 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
549 ks8851_unlock(ks, &flags);
550
551
552 while (!skb_queue_empty(&ks->txq)) {
553 struct sk_buff *txb = skb_dequeue(&ks->txq);
554
555 netif_dbg(ks, ifdown, ks->netdev,
556 "%s: freeing txb %p\n", __func__, txb);
557
558 dev_kfree_skb(txb);
559 }
560
561 free_irq(dev->irq, ks);
562
563 return 0;
564}
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
580 struct net_device *dev)
581{
582 struct ks8851_net *ks = netdev_priv(dev);
583
584 return ks->start_xmit(skb, dev);
585}
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600static void ks8851_rxctrl_work(struct work_struct *work)
601{
602 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
603 unsigned long flags;
604
605 ks8851_lock(ks, &flags);
606
607
608 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
609
610 ks8851_unlock(ks, &flags);
611}
612
613static void ks8851_set_rx_mode(struct net_device *dev)
614{
615 struct ks8851_net *ks = netdev_priv(dev);
616 struct ks8851_rxctrl rxctrl;
617
618 memset(&rxctrl, 0, sizeof(rxctrl));
619
620 if (dev->flags & IFF_PROMISC) {
621
622
623 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
624 } else if (dev->flags & IFF_ALLMULTI) {
625
626
627 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
628 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
629 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
630 struct netdev_hw_addr *ha;
631 u32 crc;
632
633
634
635 netdev_for_each_mc_addr(ha, dev) {
636 crc = ether_crc(ETH_ALEN, ha->addr);
637 crc >>= (32 - 6);
638
639 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
640 }
641
642 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
643 } else {
644
645 rxctrl.rxcr1 = RXCR1_RXPAFMA;
646 }
647
648 rxctrl.rxcr1 |= (RXCR1_RXUE |
649 RXCR1_RXBE |
650 RXCR1_RXE |
651 RXCR1_RXFCE);
652
653 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
654
655
656
657 spin_lock(&ks->statelock);
658
659 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
660 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
661 schedule_work(&ks->rxctrl_work);
662 }
663
664 spin_unlock(&ks->statelock);
665}
666
667static int ks8851_set_mac_address(struct net_device *dev, void *addr)
668{
669 struct sockaddr *sa = addr;
670
671 if (netif_running(dev))
672 return -EBUSY;
673
674 if (!is_valid_ether_addr(sa->sa_data))
675 return -EADDRNOTAVAIL;
676
677 eth_hw_addr_set(dev, sa->sa_data);
678 return ks8851_write_mac_addr(dev);
679}
680
681static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
682{
683 struct ks8851_net *ks = netdev_priv(dev);
684
685 if (!netif_running(dev))
686 return -EINVAL;
687
688 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
689}
690
691static const struct net_device_ops ks8851_netdev_ops = {
692 .ndo_open = ks8851_net_open,
693 .ndo_stop = ks8851_net_stop,
694 .ndo_eth_ioctl = ks8851_net_ioctl,
695 .ndo_start_xmit = ks8851_start_xmit,
696 .ndo_set_mac_address = ks8851_set_mac_address,
697 .ndo_set_rx_mode = ks8851_set_rx_mode,
698 .ndo_validate_addr = eth_validate_addr,
699};
700
701
702
703static void ks8851_get_drvinfo(struct net_device *dev,
704 struct ethtool_drvinfo *di)
705{
706 strlcpy(di->driver, "KS8851", sizeof(di->driver));
707 strlcpy(di->version, "1.00", sizeof(di->version));
708 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
709}
710
711static u32 ks8851_get_msglevel(struct net_device *dev)
712{
713 struct ks8851_net *ks = netdev_priv(dev);
714 return ks->msg_enable;
715}
716
717static void ks8851_set_msglevel(struct net_device *dev, u32 to)
718{
719 struct ks8851_net *ks = netdev_priv(dev);
720 ks->msg_enable = to;
721}
722
723static int ks8851_get_link_ksettings(struct net_device *dev,
724 struct ethtool_link_ksettings *cmd)
725{
726 struct ks8851_net *ks = netdev_priv(dev);
727
728 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
729
730 return 0;
731}
732
733static int ks8851_set_link_ksettings(struct net_device *dev,
734 const struct ethtool_link_ksettings *cmd)
735{
736 struct ks8851_net *ks = netdev_priv(dev);
737 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
738}
739
740static u32 ks8851_get_link(struct net_device *dev)
741{
742 struct ks8851_net *ks = netdev_priv(dev);
743 return mii_link_ok(&ks->mii);
744}
745
746static int ks8851_nway_reset(struct net_device *dev)
747{
748 struct ks8851_net *ks = netdev_priv(dev);
749 return mii_nway_restart(&ks->mii);
750}
751
752
753
754static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
755{
756 struct ks8851_net *ks = ee->data;
757 unsigned val;
758
759 val = ks8851_rdreg16(ks, KS_EEPCR);
760
761 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
762 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
763 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
764}
765
766static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
767{
768 struct ks8851_net *ks = ee->data;
769 unsigned val = EEPCR_EESA;
770
771 if (ee->drive_data)
772 val |= EEPCR_EESRWA;
773 if (ee->reg_data_in)
774 val |= EEPCR_EEDO;
775 if (ee->reg_data_clock)
776 val |= EEPCR_EESCK;
777 if (ee->reg_chip_select)
778 val |= EEPCR_EECS;
779
780 ks8851_wrreg16(ks, KS_EEPCR, val);
781}
782
783
784
785
786
787
788
789
790static int ks8851_eeprom_claim(struct ks8851_net *ks)
791{
792
793 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
794 return 0;
795}
796
797
798
799
800
801
802
803static void ks8851_eeprom_release(struct ks8851_net *ks)
804{
805 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
806
807 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
808}
809
810#define KS_EEPROM_MAGIC (0x00008851)
811
812static int ks8851_set_eeprom(struct net_device *dev,
813 struct ethtool_eeprom *ee, u8 *data)
814{
815 struct ks8851_net *ks = netdev_priv(dev);
816 int offset = ee->offset;
817 unsigned long flags;
818 int len = ee->len;
819 u16 tmp;
820
821
822 if (len != 1)
823 return -EINVAL;
824
825 if (ee->magic != KS_EEPROM_MAGIC)
826 return -EINVAL;
827
828 if (!(ks->rc_ccr & CCR_EEPROM))
829 return -ENOENT;
830
831 ks8851_lock(ks, &flags);
832
833 ks8851_eeprom_claim(ks);
834
835 eeprom_93cx6_wren(&ks->eeprom, true);
836
837
838
839
840 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
841
842 if (offset & 1) {
843 tmp &= 0xff;
844 tmp |= *data << 8;
845 } else {
846 tmp &= 0xff00;
847 tmp |= *data;
848 }
849
850 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
851 eeprom_93cx6_wren(&ks->eeprom, false);
852
853 ks8851_eeprom_release(ks);
854 ks8851_unlock(ks, &flags);
855
856 return 0;
857}
858
859static int ks8851_get_eeprom(struct net_device *dev,
860 struct ethtool_eeprom *ee, u8 *data)
861{
862 struct ks8851_net *ks = netdev_priv(dev);
863 int offset = ee->offset;
864 unsigned long flags;
865 int len = ee->len;
866
867
868 if (len & 1 || offset & 1)
869 return -EINVAL;
870
871 if (!(ks->rc_ccr & CCR_EEPROM))
872 return -ENOENT;
873
874 ks8851_lock(ks, &flags);
875
876 ks8851_eeprom_claim(ks);
877
878 ee->magic = KS_EEPROM_MAGIC;
879
880 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
881 ks8851_eeprom_release(ks);
882 ks8851_unlock(ks, &flags);
883
884 return 0;
885}
886
887static int ks8851_get_eeprom_len(struct net_device *dev)
888{
889 struct ks8851_net *ks = netdev_priv(dev);
890
891
892 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
893}
894
895static const struct ethtool_ops ks8851_ethtool_ops = {
896 .get_drvinfo = ks8851_get_drvinfo,
897 .get_msglevel = ks8851_get_msglevel,
898 .set_msglevel = ks8851_set_msglevel,
899 .get_link = ks8851_get_link,
900 .nway_reset = ks8851_nway_reset,
901 .get_eeprom_len = ks8851_get_eeprom_len,
902 .get_eeprom = ks8851_get_eeprom,
903 .set_eeprom = ks8851_set_eeprom,
904 .get_link_ksettings = ks8851_get_link_ksettings,
905 .set_link_ksettings = ks8851_set_link_ksettings,
906};
907
908
909
910
911
912
913
914
915
916
917
918static int ks8851_phy_reg(int reg)
919{
920 switch (reg) {
921 case MII_BMCR:
922 return KS_P1MBCR;
923 case MII_BMSR:
924 return KS_P1MBSR;
925 case MII_PHYSID1:
926 return KS_PHY1ILR;
927 case MII_PHYSID2:
928 return KS_PHY1IHR;
929 case MII_ADVERTISE:
930 return KS_P1ANAR;
931 case MII_LPA:
932 return KS_P1ANLPR;
933 }
934
935 return -EOPNOTSUPP;
936}
937
938static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg)
939{
940 struct ks8851_net *ks = netdev_priv(dev);
941 unsigned long flags;
942 int result;
943 int ksreg;
944
945 ksreg = ks8851_phy_reg(reg);
946 if (ksreg < 0)
947 return ksreg;
948
949 ks8851_lock(ks, &flags);
950 result = ks8851_rdreg16(ks, ksreg);
951 ks8851_unlock(ks, &flags);
952
953 return result;
954}
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
972{
973 int ret;
974
975 ret = ks8851_phy_read_common(dev, phy_addr, reg);
976 if (ret < 0)
977 return 0x0;
978
979 return ret;
980}
981
982static void ks8851_phy_write(struct net_device *dev,
983 int phy, int reg, int value)
984{
985 struct ks8851_net *ks = netdev_priv(dev);
986 unsigned long flags;
987 int ksreg;
988
989 ksreg = ks8851_phy_reg(reg);
990 if (ksreg >= 0) {
991 ks8851_lock(ks, &flags);
992 ks8851_wrreg16(ks, ksreg, value);
993 ks8851_unlock(ks, &flags);
994 }
995}
996
997static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg)
998{
999 struct ks8851_net *ks = bus->priv;
1000
1001 if (phy_id != 0)
1002 return -EOPNOTSUPP;
1003
1004
1005 if (reg == MII_PHYSID1)
1006 reg = MII_PHYSID2;
1007 else if (reg == MII_PHYSID2)
1008 reg = MII_PHYSID1;
1009
1010 return ks8851_phy_read_common(ks->netdev, phy_id, reg);
1011}
1012
1013static int ks8851_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
1014{
1015 struct ks8851_net *ks = bus->priv;
1016
1017 ks8851_phy_write(ks->netdev, phy_id, reg, val);
1018 return 0;
1019}
1020
1021
1022
1023
1024
1025
1026
1027static void ks8851_read_selftest(struct ks8851_net *ks)
1028{
1029 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1030 unsigned rd;
1031
1032 rd = ks8851_rdreg16(ks, KS_MBIR);
1033
1034 if ((rd & both_done) != both_done) {
1035 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1036 return;
1037 }
1038
1039 if (rd & MBIR_TXMBFA)
1040 netdev_err(ks->netdev, "TX memory selftest fail\n");
1041
1042 if (rd & MBIR_RXMBFA)
1043 netdev_err(ks->netdev, "RX memory selftest fail\n");
1044}
1045
1046
1047
1048#ifdef CONFIG_PM_SLEEP
1049
1050int ks8851_suspend(struct device *dev)
1051{
1052 struct ks8851_net *ks = dev_get_drvdata(dev);
1053 struct net_device *netdev = ks->netdev;
1054
1055 if (netif_running(netdev)) {
1056 netif_device_detach(netdev);
1057 ks8851_net_stop(netdev);
1058 }
1059
1060 return 0;
1061}
1062EXPORT_SYMBOL_GPL(ks8851_suspend);
1063
1064int ks8851_resume(struct device *dev)
1065{
1066 struct ks8851_net *ks = dev_get_drvdata(dev);
1067 struct net_device *netdev = ks->netdev;
1068
1069 if (netif_running(netdev)) {
1070 ks8851_net_open(netdev);
1071 netif_device_attach(netdev);
1072 }
1073
1074 return 0;
1075}
1076EXPORT_SYMBOL_GPL(ks8851_resume);
1077#endif
1078
1079static int ks8851_register_mdiobus(struct ks8851_net *ks, struct device *dev)
1080{
1081 struct mii_bus *mii_bus;
1082 int ret;
1083
1084 mii_bus = mdiobus_alloc();
1085 if (!mii_bus)
1086 return -ENOMEM;
1087
1088 mii_bus->name = "ks8851_eth_mii";
1089 mii_bus->read = ks8851_mdio_read;
1090 mii_bus->write = ks8851_mdio_write;
1091 mii_bus->priv = ks;
1092 mii_bus->parent = dev;
1093 mii_bus->phy_mask = ~((u32)BIT(0));
1094 snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
1095
1096 ret = mdiobus_register(mii_bus);
1097 if (ret)
1098 goto err_mdiobus_register;
1099
1100 ks->mii_bus = mii_bus;
1101
1102 return 0;
1103
1104err_mdiobus_register:
1105 mdiobus_free(mii_bus);
1106 return ret;
1107}
1108
1109static void ks8851_unregister_mdiobus(struct ks8851_net *ks)
1110{
1111 mdiobus_unregister(ks->mii_bus);
1112 mdiobus_free(ks->mii_bus);
1113}
1114
1115int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1116 int msg_en)
1117{
1118 struct ks8851_net *ks = netdev_priv(netdev);
1119 unsigned cider;
1120 int gpio;
1121 int ret;
1122
1123 ks->netdev = netdev;
1124 ks->tx_space = 6144;
1125
1126 gpio = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0, NULL);
1127 if (gpio == -EPROBE_DEFER)
1128 return gpio;
1129
1130 ks->gpio = gpio;
1131 if (gpio_is_valid(gpio)) {
1132 ret = devm_gpio_request_one(dev, gpio,
1133 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1134 if (ret) {
1135 dev_err(dev, "reset gpio request failed\n");
1136 return ret;
1137 }
1138 }
1139
1140 ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1141 if (IS_ERR(ks->vdd_io)) {
1142 ret = PTR_ERR(ks->vdd_io);
1143 goto err_reg_io;
1144 }
1145
1146 ret = regulator_enable(ks->vdd_io);
1147 if (ret) {
1148 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1149 goto err_reg_io;
1150 }
1151
1152 ks->vdd_reg = devm_regulator_get(dev, "vdd");
1153 if (IS_ERR(ks->vdd_reg)) {
1154 ret = PTR_ERR(ks->vdd_reg);
1155 goto err_reg;
1156 }
1157
1158 ret = regulator_enable(ks->vdd_reg);
1159 if (ret) {
1160 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1161 goto err_reg;
1162 }
1163
1164 if (gpio_is_valid(gpio)) {
1165 usleep_range(10000, 11000);
1166 gpio_set_value(gpio, 1);
1167 }
1168
1169 spin_lock_init(&ks->statelock);
1170
1171 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1172
1173 SET_NETDEV_DEV(netdev, dev);
1174
1175
1176 ks->eeprom.data = ks;
1177 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1178 ks->eeprom.register_read = ks8851_eeprom_regread;
1179 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1180
1181
1182 ks->mii.dev = netdev;
1183 ks->mii.phy_id = 1;
1184 ks->mii.phy_id_mask = 1;
1185 ks->mii.reg_num_mask = 0xf;
1186 ks->mii.mdio_read = ks8851_phy_read;
1187 ks->mii.mdio_write = ks8851_phy_write;
1188
1189 dev_info(dev, "message enable is %d\n", msg_en);
1190
1191 ret = ks8851_register_mdiobus(ks, dev);
1192 if (ret)
1193 goto err_mdio;
1194
1195
1196 ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1197 NETIF_MSG_PROBE |
1198 NETIF_MSG_LINK);
1199
1200 skb_queue_head_init(&ks->txq);
1201
1202 netdev->ethtool_ops = &ks8851_ethtool_ops;
1203
1204 dev_set_drvdata(dev, ks);
1205
1206 netif_carrier_off(ks->netdev);
1207 netdev->if_port = IF_PORT_100BASET;
1208 netdev->netdev_ops = &ks8851_netdev_ops;
1209
1210
1211 ks8851_soft_reset(ks, GRR_GSR);
1212
1213
1214 cider = ks8851_rdreg16(ks, KS_CIDER);
1215 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1216 dev_err(dev, "failed to read device ID\n");
1217 ret = -ENODEV;
1218 goto err_id;
1219 }
1220
1221
1222 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1223
1224 ks8851_read_selftest(ks);
1225 ks8851_init_mac(ks, dev->of_node);
1226
1227 ret = register_netdev(netdev);
1228 if (ret) {
1229 dev_err(dev, "failed to register network device\n");
1230 goto err_id;
1231 }
1232
1233 netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1234 CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1235 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1236
1237 return 0;
1238
1239err_id:
1240 ks8851_unregister_mdiobus(ks);
1241err_mdio:
1242 if (gpio_is_valid(gpio))
1243 gpio_set_value(gpio, 0);
1244 regulator_disable(ks->vdd_reg);
1245err_reg:
1246 regulator_disable(ks->vdd_io);
1247err_reg_io:
1248 return ret;
1249}
1250EXPORT_SYMBOL_GPL(ks8851_probe_common);
1251
1252void ks8851_remove_common(struct device *dev)
1253{
1254 struct ks8851_net *priv = dev_get_drvdata(dev);
1255
1256 ks8851_unregister_mdiobus(priv);
1257
1258 if (netif_msg_drv(priv))
1259 dev_info(dev, "remove\n");
1260
1261 unregister_netdev(priv->netdev);
1262 if (gpio_is_valid(priv->gpio))
1263 gpio_set_value(priv->gpio, 0);
1264 regulator_disable(priv->vdd_reg);
1265 regulator_disable(priv->vdd_io);
1266}
1267EXPORT_SYMBOL_GPL(ks8851_remove_common);
1268
1269MODULE_DESCRIPTION("KS8851 Network driver");
1270MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1271MODULE_LICENSE("GPL");
1272