1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/net/ethernet/micrel/ks8851.c
4 * Copyright 2009 Simtec Electronics
5 * http://www.simtec.co.uk/
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
23 #include <linux/of_mdio.h>
24 #include <linux/of_net.h>
29 * ks8851_lock - register access lock
31 * @flags: Spinlock flags
33 * Claim chip register access lock
35 static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
41 * ks8851_unlock - register access unlock
43 * @flags: Spinlock flags
45 * Release chip register access lock
47 static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
49 ks->unlock(ks, flags);
53 * ks8851_wrreg16 - write 16bit register value to chip
55 * @reg: The register address
56 * @val: The value to write
58 * Issue a write to put the value @val into the register specified in @reg.
60 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned int reg,
63 ks->wrreg16(ks, reg, val);
67 * ks8851_rdreg16 - read 16 bit register from device
68 * @ks: The chip information
69 * @reg: The register address
71 * Read a 16bit register from the chip, returning the result
73 static unsigned int ks8851_rdreg16(struct ks8851_net *ks,
76 return ks->rdreg16(ks, reg);
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
84 * Issue the relevant soft-reset command to the device's GRR register
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.
92 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
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 */
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.
105 * Change the power mode of the chip.
107 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
111 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
113 pmecr = ks8851_rdreg16(ks, KS_PMECR);
114 pmecr &= ~PMECR_PM_MASK;
117 ks8851_wrreg16(ks, KS_PMECR, pmecr);
121 * ks8851_write_mac_addr - write mac address to device registers
122 * @dev: The network device
124 * Update the KS8851 MAC address registers from the address in @dev.
126 * This call assumes that the chip is not running, so there is no need to
127 * shutdown the RXQ process whilst setting this.
129 static int ks8851_write_mac_addr(struct net_device *dev)
131 struct ks8851_net *ks = netdev_priv(dev);
136 ks8851_lock(ks, &flags);
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.
142 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
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);
149 if (!netif_running(dev))
150 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
152 ks8851_unlock(ks, &flags);
158 * ks8851_read_mac_addr - read mac address from device registers
159 * @dev: The network device
161 * Update our copy of the KS8851 MAC address from the registers of @dev.
163 static void ks8851_read_mac_addr(struct net_device *dev)
165 struct ks8851_net *ks = netdev_priv(dev);
171 ks8851_lock(ks, &flags);
173 for (i = 0; i < ETH_ALEN; i += 2) {
174 reg = ks8851_rdreg16(ks, KS_MAR(i));
176 addr[i + 1] = reg & 0xff;
178 eth_hw_addr_set(dev, addr);
180 ks8851_unlock(ks, &flags);
184 * ks8851_init_mac - initialise the mac address
185 * @ks: The device structure
186 * @np: The device node pointer
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.
194 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
196 struct net_device *dev = ks->netdev;
199 ret = of_get_ethdev_address(np, dev);
201 ks8851_write_mac_addr(dev);
205 if (ks->rc_ccr & CCR_EEPROM) {
206 ks8851_read_mac_addr(dev);
207 if (is_valid_ether_addr(dev->dev_addr))
210 netdev_err(ks->netdev, "invalid mac address read %pM\n",
214 eth_hw_addr_random(dev);
215 ks8851_write_mac_addr(dev);
219 * ks8851_rx_pkts - receive packets from the host
220 * @ks: The device information.
221 * @rxq: Queue of packets received in this function.
223 * This is called from the IRQ work queue when the system detects that there
224 * are packets in the receive queue. Find out how many packets there are and
225 * read them from the FIFO.
227 static void ks8851_rx_pkts(struct ks8851_net *ks, struct sk_buff_head *rxq)
235 rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
237 netif_dbg(ks, rx_status, ks->netdev,
238 "%s: %d packets\n", __func__, rxfc);
240 /* Currently we're issuing a read per packet, but we could possibly
241 * improve the code by issuing a single read, getting the receive
242 * header, allocating the packet and then reading the packet data
245 * This form of operation would require us to hold the SPI bus'
246 * chipselect low during the entie transaction to avoid any
247 * reset to the data stream coming from the chip.
250 for (; rxfc != 0; rxfc--) {
251 rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
252 rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
254 netif_dbg(ks, rx_status, ks->netdev,
255 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
257 /* the length of the packet includes the 32bit CRC */
259 /* set dma read address */
260 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
262 /* start DMA access */
263 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
266 unsigned int rxalign;
269 rxalign = ALIGN(rxlen, 4);
270 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
273 /* 4 bytes of status header + 4 bytes of
274 * garbage: we put them before ethernet
275 * header, so that they are copied,
279 rxpkt = skb_put(skb, rxlen) - 8;
281 ks->rdfifo(ks, rxpkt, rxalign + 8);
283 netif_dbg(ks, pktdata, ks->netdev,
284 "pkt %12ph\n", &rxpkt[4]);
286 skb->protocol = eth_type_trans(skb, ks->netdev);
287 __skb_queue_tail(rxq, skb);
289 ks->netdev->stats.rx_packets++;
290 ks->netdev->stats.rx_bytes += rxlen;
294 /* end DMA access and dequeue packet */
295 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
300 * ks8851_irq - IRQ handler for dealing with interrupt requests
304 * This handler is invoked when the IRQ line asserts to find out what happened.
305 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
308 * Read the interrupt status, work out what needs to be done and then clear
309 * any of the interrupts that are not needed.
311 static irqreturn_t ks8851_irq(int irq, void *_ks)
313 struct ks8851_net *ks = _ks;
314 struct sk_buff_head rxq;
319 ks8851_lock(ks, &flags);
321 status = ks8851_rdreg16(ks, KS_ISR);
322 ks8851_wrreg16(ks, KS_ISR, status);
324 netif_dbg(ks, intr, ks->netdev,
325 "%s: status 0x%04x\n", __func__, status);
327 if (status & IRQ_LDI) {
328 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
329 pmecr &= ~PMECR_WKEVT_MASK;
330 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
333 if (status & IRQ_TXI) {
334 unsigned short tx_space = ks8851_rdreg16(ks, KS_TXMIR);
336 netif_dbg(ks, intr, ks->netdev,
337 "%s: txspace %d\n", __func__, tx_space);
339 spin_lock_bh(&ks->statelock);
340 ks->tx_space = tx_space;
341 if (netif_queue_stopped(ks->netdev))
342 netif_wake_queue(ks->netdev);
343 spin_unlock_bh(&ks->statelock);
346 if (status & IRQ_SPIBEI) {
347 netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
350 if (status & IRQ_RXI) {
351 /* the datasheet says to disable the rx interrupt during
352 * packet read-out, however we're masking the interrupt
353 * from the device so do not bother masking just the RX
354 * from the device. */
356 __skb_queue_head_init(&rxq);
357 ks8851_rx_pkts(ks, &rxq);
360 /* if something stopped the rx process, probably due to wanting
361 * to change the rx settings, then do something about restarting
363 if (status & IRQ_RXPSI) {
364 struct ks8851_rxctrl *rxc = &ks->rxctrl;
366 /* update the multicast hash table */
367 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
368 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
369 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
370 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
372 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
373 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
376 ks8851_unlock(ks, &flags);
378 if (status & IRQ_LCI)
379 mii_check_link(&ks->mii);
381 if (status & IRQ_RXI)
382 while ((skb = __skb_dequeue(&rxq)))
389 * ks8851_flush_tx_work - flush outstanding TX work
390 * @ks: The device state
392 static void ks8851_flush_tx_work(struct ks8851_net *ks)
394 if (ks->flush_tx_work)
395 ks->flush_tx_work(ks);
399 * ks8851_net_open - open network device
400 * @dev: The network device being opened.
402 * Called when the network device is marked active, such as a user executing
403 * 'ifconfig up' on the device.
405 static int ks8851_net_open(struct net_device *dev)
407 struct ks8851_net *ks = netdev_priv(dev);
411 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
412 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
415 netdev_err(dev, "failed to get irq\n");
419 /* lock the card, even if we may not actually be doing anything
420 * else at the moment */
421 ks8851_lock(ks, &flags);
423 netif_dbg(ks, ifup, ks->netdev, "opening\n");
425 /* bring chip out of any power saving mode it was in */
426 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
428 /* issue a soft reset to the RX/TX QMU to put it into a known
430 ks8851_soft_reset(ks, GRR_QMU);
432 /* setup transmission parameters */
434 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
435 TXCR_TXPE | /* pad to min length */
436 TXCR_TXCRC | /* add CRC */
437 TXCR_TXFCE)); /* enable flow control */
439 /* auto-increment tx data, reset tx pointer */
440 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
442 /* setup receiver control */
444 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
445 RXCR1_RXFCE | /* enable flow control */
446 RXCR1_RXBE | /* broadcast enable */
447 RXCR1_RXUE | /* unicast enable */
448 RXCR1_RXE)); /* enable rx block */
450 /* transfer entire frames out in one go */
451 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
453 /* set receive counter timeouts */
454 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
455 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
456 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
458 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
459 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
460 RXQCR_RXDTTE); /* IRQ on time exceeded */
462 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
464 /* clear then enable interrupts */
465 ks8851_wrreg16(ks, KS_ISR, ks->rc_ier);
466 ks8851_wrreg16(ks, KS_IER, ks->rc_ier);
469 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
470 netif_start_queue(ks->netdev);
472 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
474 ks8851_unlock(ks, &flags);
475 mii_check_link(&ks->mii);
480 * ks8851_net_stop - close network device
481 * @dev: The device being closed.
483 * Called to close down a network device which has been active. Cancell any
484 * work, shutdown the RX and TX process and then place the chip into a low
485 * power state whilst it is not being used.
487 static int ks8851_net_stop(struct net_device *dev)
489 struct ks8851_net *ks = netdev_priv(dev);
492 netif_info(ks, ifdown, dev, "shutting down\n");
494 netif_stop_queue(dev);
496 ks8851_lock(ks, &flags);
497 /* turn off the IRQs and ack any outstanding */
498 ks8851_wrreg16(ks, KS_IER, 0x0000);
499 ks8851_wrreg16(ks, KS_ISR, 0xffff);
500 ks8851_unlock(ks, &flags);
502 /* stop any outstanding work */
503 ks8851_flush_tx_work(ks);
504 flush_work(&ks->rxctrl_work);
506 ks8851_lock(ks, &flags);
507 /* shutdown RX process */
508 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
510 /* shutdown TX process */
511 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
513 /* set powermode to soft power down to save power */
514 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
515 ks8851_unlock(ks, &flags);
517 /* ensure any queued tx buffers are dumped */
518 while (!skb_queue_empty(&ks->txq)) {
519 struct sk_buff *txb = skb_dequeue(&ks->txq);
521 netif_dbg(ks, ifdown, ks->netdev,
522 "%s: freeing txb %p\n", __func__, txb);
527 free_irq(dev->irq, ks);
533 * ks8851_start_xmit - transmit packet
534 * @skb: The buffer to transmit
535 * @dev: The device used to transmit the packet.
537 * Called by the network layer to transmit the @skb. Queue the packet for
538 * the device and schedule the necessary work to transmit the packet when
541 * We do this to firstly avoid sleeping with the network device locked,
542 * and secondly so we can round up more than one packet to transmit which
543 * means we can try and avoid generating too many transmit done interrupts.
545 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
546 struct net_device *dev)
548 struct ks8851_net *ks = netdev_priv(dev);
550 return ks->start_xmit(skb, dev);
554 * ks8851_rxctrl_work - work handler to change rx mode
555 * @work: The work structure this belongs to.
557 * Lock the device and issue the necessary changes to the receive mode from
558 * the network device layer. This is done so that we can do this without
559 * having to sleep whilst holding the network device lock.
561 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
562 * receive parameters are programmed, we issue a write to disable the RXQ and
563 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
564 * complete. The interrupt handler then writes the new values into the chip.
566 static void ks8851_rxctrl_work(struct work_struct *work)
568 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
571 ks8851_lock(ks, &flags);
573 /* need to shutdown RXQ before modifying filter parameters */
574 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
576 ks8851_unlock(ks, &flags);
579 static void ks8851_set_rx_mode(struct net_device *dev)
581 struct ks8851_net *ks = netdev_priv(dev);
582 struct ks8851_rxctrl rxctrl;
584 memset(&rxctrl, 0, sizeof(rxctrl));
586 if (dev->flags & IFF_PROMISC) {
587 /* interface to receive everything */
589 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
590 } else if (dev->flags & IFF_ALLMULTI) {
591 /* accept all multicast packets */
593 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
594 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
595 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
596 struct netdev_hw_addr *ha;
599 /* accept some multicast */
601 netdev_for_each_mc_addr(ha, dev) {
602 crc = ether_crc(ETH_ALEN, ha->addr);
603 crc >>= (32 - 6); /* get top six bits */
605 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
608 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
610 /* just accept broadcast / unicast */
611 rxctrl.rxcr1 = RXCR1_RXPAFMA;
614 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
615 RXCR1_RXBE | /* broadcast enable */
616 RXCR1_RXE | /* RX process enable */
617 RXCR1_RXFCE); /* enable flow control */
619 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
621 /* schedule work to do the actual set of the data if needed */
623 spin_lock_bh(&ks->statelock);
625 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
626 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
627 schedule_work(&ks->rxctrl_work);
630 spin_unlock_bh(&ks->statelock);
633 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
635 struct sockaddr *sa = addr;
637 if (netif_running(dev))
640 if (!is_valid_ether_addr(sa->sa_data))
641 return -EADDRNOTAVAIL;
643 eth_hw_addr_set(dev, sa->sa_data);
644 return ks8851_write_mac_addr(dev);
647 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
649 struct ks8851_net *ks = netdev_priv(dev);
651 if (!netif_running(dev))
654 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
657 static const struct net_device_ops ks8851_netdev_ops = {
658 .ndo_open = ks8851_net_open,
659 .ndo_stop = ks8851_net_stop,
660 .ndo_eth_ioctl = ks8851_net_ioctl,
661 .ndo_start_xmit = ks8851_start_xmit,
662 .ndo_set_mac_address = ks8851_set_mac_address,
663 .ndo_set_rx_mode = ks8851_set_rx_mode,
664 .ndo_validate_addr = eth_validate_addr,
667 /* ethtool support */
669 static void ks8851_get_drvinfo(struct net_device *dev,
670 struct ethtool_drvinfo *di)
672 strscpy(di->driver, "KS8851", sizeof(di->driver));
673 strscpy(di->version, "1.00", sizeof(di->version));
674 strscpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
677 static u32 ks8851_get_msglevel(struct net_device *dev)
679 struct ks8851_net *ks = netdev_priv(dev);
680 return ks->msg_enable;
683 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
685 struct ks8851_net *ks = netdev_priv(dev);
689 static int ks8851_get_link_ksettings(struct net_device *dev,
690 struct ethtool_link_ksettings *cmd)
692 struct ks8851_net *ks = netdev_priv(dev);
694 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
699 static int ks8851_set_link_ksettings(struct net_device *dev,
700 const struct ethtool_link_ksettings *cmd)
702 struct ks8851_net *ks = netdev_priv(dev);
703 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
706 static u32 ks8851_get_link(struct net_device *dev)
708 struct ks8851_net *ks = netdev_priv(dev);
709 return mii_link_ok(&ks->mii);
712 static int ks8851_nway_reset(struct net_device *dev)
714 struct ks8851_net *ks = netdev_priv(dev);
715 return mii_nway_restart(&ks->mii);
720 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
722 struct ks8851_net *ks = ee->data;
725 val = ks8851_rdreg16(ks, KS_EEPCR);
727 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
728 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
729 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
732 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
734 struct ks8851_net *ks = ee->data;
735 unsigned val = EEPCR_EESA; /* default - eeprom access on */
741 if (ee->reg_data_clock)
743 if (ee->reg_chip_select)
746 ks8851_wrreg16(ks, KS_EEPCR, val);
750 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
751 * @ks: The network device state.
753 * Check for the presence of an EEPROM, and then activate software access
756 static int ks8851_eeprom_claim(struct ks8851_net *ks)
758 /* start with clock low, cs high */
759 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
764 * ks8851_eeprom_release - release the EEPROM interface
765 * @ks: The device state
767 * Release the software access to the device EEPROM
769 static void ks8851_eeprom_release(struct ks8851_net *ks)
771 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
773 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
776 #define KS_EEPROM_MAGIC (0x00008851)
778 static int ks8851_set_eeprom(struct net_device *dev,
779 struct ethtool_eeprom *ee, u8 *data)
781 struct ks8851_net *ks = netdev_priv(dev);
782 int offset = ee->offset;
787 /* currently only support byte writing */
791 if (ee->magic != KS_EEPROM_MAGIC)
794 if (!(ks->rc_ccr & CCR_EEPROM))
797 ks8851_lock(ks, &flags);
799 ks8851_eeprom_claim(ks);
801 eeprom_93cx6_wren(&ks->eeprom, true);
803 /* ethtool currently only supports writing bytes, which means
804 * we have to read/modify/write our 16bit EEPROMs */
806 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
816 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
817 eeprom_93cx6_wren(&ks->eeprom, false);
819 ks8851_eeprom_release(ks);
820 ks8851_unlock(ks, &flags);
825 static int ks8851_get_eeprom(struct net_device *dev,
826 struct ethtool_eeprom *ee, u8 *data)
828 struct ks8851_net *ks = netdev_priv(dev);
829 int offset = ee->offset;
833 /* must be 2 byte aligned */
834 if (len & 1 || offset & 1)
837 if (!(ks->rc_ccr & CCR_EEPROM))
840 ks8851_lock(ks, &flags);
842 ks8851_eeprom_claim(ks);
844 ee->magic = KS_EEPROM_MAGIC;
846 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
847 ks8851_eeprom_release(ks);
848 ks8851_unlock(ks, &flags);
853 static int ks8851_get_eeprom_len(struct net_device *dev)
855 struct ks8851_net *ks = netdev_priv(dev);
857 /* currently, we assume it is an 93C46 attached, so return 128 */
858 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
861 static const struct ethtool_ops ks8851_ethtool_ops = {
862 .get_drvinfo = ks8851_get_drvinfo,
863 .get_msglevel = ks8851_get_msglevel,
864 .set_msglevel = ks8851_set_msglevel,
865 .get_link = ks8851_get_link,
866 .nway_reset = ks8851_nway_reset,
867 .get_eeprom_len = ks8851_get_eeprom_len,
868 .get_eeprom = ks8851_get_eeprom,
869 .set_eeprom = ks8851_set_eeprom,
870 .get_link_ksettings = ks8851_get_link_ksettings,
871 .set_link_ksettings = ks8851_set_link_ksettings,
874 /* MII interface controls */
877 * ks8851_phy_reg - convert MII register into a KS8851 register
878 * @reg: MII register number.
880 * Return the KS8851 register number for the corresponding MII PHY register
881 * if possible. Return zero if the MII register has no direct mapping to the
882 * KS8851 register set.
884 static int ks8851_phy_reg(int reg)
904 static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg)
906 struct ks8851_net *ks = netdev_priv(dev);
911 ksreg = ks8851_phy_reg(reg);
915 ks8851_lock(ks, &flags);
916 result = ks8851_rdreg16(ks, ksreg);
917 ks8851_unlock(ks, &flags);
923 * ks8851_phy_read - MII interface PHY register read.
924 * @dev: The network device the PHY is on.
925 * @phy_addr: Address of PHY (ignored as we only have one)
926 * @reg: The register to read.
928 * This call reads data from the PHY register specified in @reg. Since the
929 * device does not support all the MII registers, the non-existent values
930 * are always returned as zero.
932 * We return zero for unsupported registers as the MII code does not check
933 * the value returned for any error status, and simply returns it to the
934 * caller. The mii-tool that the driver was tested with takes any -ve error
935 * as real PHY capabilities, thus displaying incorrect data to the user.
937 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
941 ret = ks8851_phy_read_common(dev, phy_addr, reg);
943 return 0x0; /* no error return allowed, so use zero */
948 static void ks8851_phy_write(struct net_device *dev,
949 int phy, int reg, int value)
951 struct ks8851_net *ks = netdev_priv(dev);
955 ksreg = ks8851_phy_reg(reg);
957 ks8851_lock(ks, &flags);
958 ks8851_wrreg16(ks, ksreg, value);
959 ks8851_unlock(ks, &flags);
963 static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg)
965 struct ks8851_net *ks = bus->priv;
970 /* KS8851 PHY ID registers are swapped in HW, swap them back. */
971 if (reg == MII_PHYSID1)
973 else if (reg == MII_PHYSID2)
976 return ks8851_phy_read_common(ks->netdev, phy_id, reg);
979 static int ks8851_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
981 struct ks8851_net *ks = bus->priv;
983 ks8851_phy_write(ks->netdev, phy_id, reg, val);
988 * ks8851_read_selftest - read the selftest memory info.
989 * @ks: The device state
991 * Read and check the TX/RX memory selftest information.
993 static void ks8851_read_selftest(struct ks8851_net *ks)
995 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
998 rd = ks8851_rdreg16(ks, KS_MBIR);
1000 if ((rd & both_done) != both_done) {
1001 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1005 if (rd & MBIR_TXMBFA)
1006 netdev_err(ks->netdev, "TX memory selftest fail\n");
1008 if (rd & MBIR_RXMBFA)
1009 netdev_err(ks->netdev, "RX memory selftest fail\n");
1012 /* driver bus management functions */
1014 #ifdef CONFIG_PM_SLEEP
1016 int ks8851_suspend(struct device *dev)
1018 struct ks8851_net *ks = dev_get_drvdata(dev);
1019 struct net_device *netdev = ks->netdev;
1021 if (netif_running(netdev)) {
1022 netif_device_detach(netdev);
1023 ks8851_net_stop(netdev);
1028 EXPORT_SYMBOL_GPL(ks8851_suspend);
1030 int ks8851_resume(struct device *dev)
1032 struct ks8851_net *ks = dev_get_drvdata(dev);
1033 struct net_device *netdev = ks->netdev;
1035 if (netif_running(netdev)) {
1036 ks8851_net_open(netdev);
1037 netif_device_attach(netdev);
1042 EXPORT_SYMBOL_GPL(ks8851_resume);
1045 static int ks8851_register_mdiobus(struct ks8851_net *ks, struct device *dev)
1047 struct mii_bus *mii_bus;
1050 mii_bus = mdiobus_alloc();
1054 mii_bus->name = "ks8851_eth_mii";
1055 mii_bus->read = ks8851_mdio_read;
1056 mii_bus->write = ks8851_mdio_write;
1058 mii_bus->parent = dev;
1059 mii_bus->phy_mask = ~((u32)BIT(0));
1060 snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
1062 ret = mdiobus_register(mii_bus);
1064 goto err_mdiobus_register;
1066 ks->mii_bus = mii_bus;
1070 err_mdiobus_register:
1071 mdiobus_free(mii_bus);
1075 static void ks8851_unregister_mdiobus(struct ks8851_net *ks)
1077 mdiobus_unregister(ks->mii_bus);
1078 mdiobus_free(ks->mii_bus);
1081 int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1084 struct ks8851_net *ks = netdev_priv(netdev);
1088 ks->netdev = netdev;
1090 ks->gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1091 ret = PTR_ERR_OR_ZERO(ks->gpio);
1093 if (ret != -EPROBE_DEFER)
1094 dev_err(dev, "reset gpio request failed: %d\n", ret);
1098 ret = gpiod_set_consumer_name(ks->gpio, "ks8851_rst_n");
1100 dev_err(dev, "failed to set reset gpio name: %d\n", ret);
1104 ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1105 if (IS_ERR(ks->vdd_io)) {
1106 ret = PTR_ERR(ks->vdd_io);
1110 ret = regulator_enable(ks->vdd_io);
1112 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1116 ks->vdd_reg = devm_regulator_get(dev, "vdd");
1117 if (IS_ERR(ks->vdd_reg)) {
1118 ret = PTR_ERR(ks->vdd_reg);
1122 ret = regulator_enable(ks->vdd_reg);
1124 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1129 usleep_range(10000, 11000);
1130 gpiod_set_value_cansleep(ks->gpio, 0);
1133 spin_lock_init(&ks->statelock);
1135 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1137 SET_NETDEV_DEV(netdev, dev);
1139 /* setup EEPROM state */
1140 ks->eeprom.data = ks;
1141 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1142 ks->eeprom.register_read = ks8851_eeprom_regread;
1143 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1145 /* setup mii state */
1146 ks->mii.dev = netdev;
1148 ks->mii.phy_id_mask = 1;
1149 ks->mii.reg_num_mask = 0xf;
1150 ks->mii.mdio_read = ks8851_phy_read;
1151 ks->mii.mdio_write = ks8851_phy_write;
1153 dev_info(dev, "message enable is %d\n", msg_en);
1155 ret = ks8851_register_mdiobus(ks, dev);
1159 /* set the default message enable */
1160 ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1164 skb_queue_head_init(&ks->txq);
1166 netdev->ethtool_ops = &ks8851_ethtool_ops;
1168 dev_set_drvdata(dev, ks);
1170 netif_carrier_off(ks->netdev);
1171 netdev->if_port = IF_PORT_100BASET;
1172 netdev->netdev_ops = &ks8851_netdev_ops;
1174 /* issue a global soft reset to reset the device. */
1175 ks8851_soft_reset(ks, GRR_GSR);
1177 /* simple check for a valid chip being connected to the bus */
1178 cider = ks8851_rdreg16(ks, KS_CIDER);
1179 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1180 dev_err(dev, "failed to read device ID\n");
1185 /* cache the contents of the CCR register for EEPROM, etc. */
1186 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1188 ks8851_read_selftest(ks);
1189 ks8851_init_mac(ks, dev->of_node);
1191 ret = register_netdev(netdev);
1193 dev_err(dev, "failed to register network device\n");
1197 netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1198 CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1199 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1204 ks8851_unregister_mdiobus(ks);
1207 gpiod_set_value_cansleep(ks->gpio, 1);
1208 regulator_disable(ks->vdd_reg);
1210 regulator_disable(ks->vdd_io);
1214 EXPORT_SYMBOL_GPL(ks8851_probe_common);
1216 void ks8851_remove_common(struct device *dev)
1218 struct ks8851_net *priv = dev_get_drvdata(dev);
1220 ks8851_unregister_mdiobus(priv);
1222 if (netif_msg_drv(priv))
1223 dev_info(dev, "remove\n");
1225 unregister_netdev(priv->netdev);
1227 gpiod_set_value_cansleep(priv->gpio, 1);
1228 regulator_disable(priv->vdd_reg);
1229 regulator_disable(priv->vdd_io);
1231 EXPORT_SYMBOL_GPL(ks8851_remove_common);
1233 MODULE_DESCRIPTION("KS8851 Network driver");
1235 MODULE_LICENSE("GPL");