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/
6 * Ben Dooks <ben@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/regulator/consumer.h>
22 #include <linux/gpio.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
30 * ks8851_lock - register access lock
32 * @flags: Spinlock flags
34 * Claim chip register access lock
36 static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
42 * ks8851_unlock - register access unlock
44 * @flags: Spinlock flags
46 * Release chip register access lock
48 static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
50 ks->unlock(ks, flags);
54 * ks8851_wrreg16 - write 16bit register value to chip
56 * @reg: The register address
57 * @val: The value to write
59 * Issue a write to put the value @val into the register specified in @reg.
61 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned int reg,
64 ks->wrreg16(ks, reg, val);
68 * ks8851_rdreg16 - read 16 bit register from device
69 * @ks: The chip information
70 * @reg: The register address
72 * Read a 16bit register from the chip, returning the result
74 static unsigned int ks8851_rdreg16(struct ks8851_net *ks,
77 return ks->rdreg16(ks, reg);
81 * ks8851_soft_reset - issue one of the soft reset to the device
82 * @ks: The device state.
83 * @op: The bit(s) to set in the GRR
85 * Issue the relevant soft-reset command to the device's GRR register
88 * Note, the delays are in there as a caution to ensure that the reset
89 * has time to take effect and then complete. Since the datasheet does
90 * not currently specify the exact sequence, we have chosen something
91 * that seems to work with our device.
93 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
95 ks8851_wrreg16(ks, KS_GRR, op);
96 mdelay(1); /* wait a short time to effect reset */
97 ks8851_wrreg16(ks, KS_GRR, 0);
98 mdelay(1); /* wait for condition to clear */
102 * ks8851_set_powermode - set power mode of the device
103 * @ks: The device state
104 * @pwrmode: The power mode value to write to KS_PMECR.
106 * Change the power mode of the chip.
108 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
112 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
114 pmecr = ks8851_rdreg16(ks, KS_PMECR);
115 pmecr &= ~PMECR_PM_MASK;
118 ks8851_wrreg16(ks, KS_PMECR, pmecr);
122 * ks8851_write_mac_addr - write mac address to device registers
123 * @dev: The network device
125 * Update the KS8851 MAC address registers from the address in @dev.
127 * This call assumes that the chip is not running, so there is no need to
128 * shutdown the RXQ process whilst setting this.
130 static int ks8851_write_mac_addr(struct net_device *dev)
132 struct ks8851_net *ks = netdev_priv(dev);
137 ks8851_lock(ks, &flags);
140 * Wake up chip in case it was powered off when stopped; otherwise,
141 * the first write to the MAC address does not take effect.
143 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
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);
150 if (!netif_running(dev))
151 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
153 ks8851_unlock(ks, &flags);
159 * ks8851_read_mac_addr - read mac address from device registers
160 * @dev: The network device
162 * Update our copy of the KS8851 MAC address from the registers of @dev.
164 static void ks8851_read_mac_addr(struct net_device *dev)
166 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));
175 dev->dev_addr[i] = reg >> 8;
176 dev->dev_addr[i + 1] = reg & 0xff;
179 ks8851_unlock(ks, &flags);
183 * ks8851_init_mac - initialise the mac address
184 * @ks: The device structure
185 * @np: The device node pointer
187 * Get or create the initial mac address for the device and then set that
188 * into the station address register. A mac address supplied in the device
189 * tree takes precedence. Otherwise, if there is an EEPROM present, then
190 * we try that. If no valid mac address is found we use eth_random_addr()
191 * to create a new one.
193 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
195 struct net_device *dev = ks->netdev;
198 ret = of_get_mac_address(np, dev->dev_addr);
200 ks8851_write_mac_addr(dev);
204 if (ks->rc_ccr & CCR_EEPROM) {
205 ks8851_read_mac_addr(dev);
206 if (is_valid_ether_addr(dev->dev_addr))
209 netdev_err(ks->netdev, "invalid mac address read %pM\n",
213 eth_hw_addr_random(dev);
214 ks8851_write_mac_addr(dev);
218 * ks8851_dbg_dumpkkt - dump initial packet contents to debug
219 * @ks: The device state
220 * @rxpkt: The data for the received packet
222 * Dump the initial data from the packet to dev_dbg().
224 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
226 netdev_dbg(ks->netdev,
227 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
228 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
229 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
230 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
234 * ks8851_rx_skb - receive skbuff
235 * @ks: The device state.
238 static void ks8851_rx_skb(struct ks8851_net *ks, struct sk_buff *skb)
244 * ks8851_rx_pkts - receive packets from the host
245 * @ks: The device information.
247 * This is called from the IRQ work queue when the system detects that there
248 * are packets in the receive queue. Find out how many packets there are and
249 * read them from the FIFO.
251 static void ks8851_rx_pkts(struct ks8851_net *ks)
259 rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
261 netif_dbg(ks, rx_status, ks->netdev,
262 "%s: %d packets\n", __func__, rxfc);
264 /* Currently we're issuing a read per packet, but we could possibly
265 * improve the code by issuing a single read, getting the receive
266 * header, allocating the packet and then reading the packet data
269 * This form of operation would require us to hold the SPI bus'
270 * chipselect low during the entie transaction to avoid any
271 * reset to the data stream coming from the chip.
274 for (; rxfc != 0; rxfc--) {
275 rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
276 rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
278 netif_dbg(ks, rx_status, ks->netdev,
279 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
281 /* the length of the packet includes the 32bit CRC */
283 /* set dma read address */
284 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
286 /* start DMA access */
287 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
290 unsigned int rxalign;
293 rxalign = ALIGN(rxlen, 4);
294 skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
297 /* 4 bytes of status header + 4 bytes of
298 * garbage: we put them before ethernet
299 * header, so that they are copied,
303 rxpkt = skb_put(skb, rxlen) - 8;
305 ks->rdfifo(ks, rxpkt, rxalign + 8);
307 if (netif_msg_pktdata(ks))
308 ks8851_dbg_dumpkkt(ks, rxpkt);
310 skb->protocol = eth_type_trans(skb, ks->netdev);
311 ks8851_rx_skb(ks, skb);
313 ks->netdev->stats.rx_packets++;
314 ks->netdev->stats.rx_bytes += rxlen;
318 /* end DMA access and dequeue packet */
319 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
324 * ks8851_irq - IRQ handler for dealing with interrupt requests
328 * This handler is invoked when the IRQ line asserts to find out what happened.
329 * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
332 * Read the interrupt status, work out what needs to be done and then clear
333 * any of the interrupts that are not needed.
335 static irqreturn_t ks8851_irq(int irq, void *_ks)
337 struct ks8851_net *ks = _ks;
338 unsigned handled = 0;
342 ks8851_lock(ks, &flags);
344 status = ks8851_rdreg16(ks, KS_ISR);
346 netif_dbg(ks, intr, ks->netdev,
347 "%s: status 0x%04x\n", __func__, status);
349 if (status & IRQ_LCI)
352 if (status & IRQ_LDI) {
353 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
354 pmecr &= ~PMECR_WKEVT_MASK;
355 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
360 if (status & IRQ_RXPSI)
361 handled |= IRQ_RXPSI;
363 if (status & IRQ_TXI) {
366 /* no lock here, tx queue should have been stopped */
368 /* update our idea of how much tx space is available to the
370 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
372 netif_dbg(ks, intr, ks->netdev,
373 "%s: txspace %d\n", __func__, ks->tx_space);
376 if (status & IRQ_RXI)
379 if (status & IRQ_SPIBEI) {
380 netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
381 handled |= IRQ_SPIBEI;
384 ks8851_wrreg16(ks, KS_ISR, handled);
386 if (status & IRQ_RXI) {
387 /* the datasheet says to disable the rx interrupt during
388 * packet read-out, however we're masking the interrupt
389 * from the device so do not bother masking just the RX
390 * from the device. */
395 /* if something stopped the rx process, probably due to wanting
396 * to change the rx settings, then do something about restarting
398 if (status & IRQ_RXPSI) {
399 struct ks8851_rxctrl *rxc = &ks->rxctrl;
401 /* update the multicast hash table */
402 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
403 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
404 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
405 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
407 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
408 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
411 ks8851_unlock(ks, &flags);
413 if (status & IRQ_LCI)
414 mii_check_link(&ks->mii);
416 if (status & IRQ_TXI)
417 netif_wake_queue(ks->netdev);
423 * ks8851_flush_tx_work - flush outstanding TX work
424 * @ks: The device state
426 static void ks8851_flush_tx_work(struct ks8851_net *ks)
428 if (ks->flush_tx_work)
429 ks->flush_tx_work(ks);
433 * ks8851_net_open - open network device
434 * @dev: The network device being opened.
436 * Called when the network device is marked active, such as a user executing
437 * 'ifconfig up' on the device.
439 static int ks8851_net_open(struct net_device *dev)
441 struct ks8851_net *ks = netdev_priv(dev);
445 ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
446 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
449 netdev_err(dev, "failed to get irq\n");
453 /* lock the card, even if we may not actually be doing anything
454 * else at the moment */
455 ks8851_lock(ks, &flags);
457 netif_dbg(ks, ifup, ks->netdev, "opening\n");
459 /* bring chip out of any power saving mode it was in */
460 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
462 /* issue a soft reset to the RX/TX QMU to put it into a known
464 ks8851_soft_reset(ks, GRR_QMU);
466 /* setup transmission parameters */
468 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
469 TXCR_TXPE | /* pad to min length */
470 TXCR_TXCRC | /* add CRC */
471 TXCR_TXFCE)); /* enable flow control */
473 /* auto-increment tx data, reset tx pointer */
474 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
476 /* setup receiver control */
478 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
479 RXCR1_RXFCE | /* enable flow control */
480 RXCR1_RXBE | /* broadcast enable */
481 RXCR1_RXUE | /* unicast enable */
482 RXCR1_RXE)); /* enable rx block */
484 /* transfer entire frames out in one go */
485 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
487 /* set receive counter timeouts */
488 ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
489 ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
490 ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
492 ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
493 RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
494 RXQCR_RXDTTE); /* IRQ on time exceeded */
496 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
498 /* clear then enable interrupts */
499 ks8851_wrreg16(ks, KS_ISR, ks->rc_ier);
500 ks8851_wrreg16(ks, KS_IER, ks->rc_ier);
502 netif_start_queue(ks->netdev);
504 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
506 ks8851_unlock(ks, &flags);
507 mii_check_link(&ks->mii);
512 * ks8851_net_stop - close network device
513 * @dev: The device being closed.
515 * Called to close down a network device which has been active. Cancell any
516 * work, shutdown the RX and TX process and then place the chip into a low
517 * power state whilst it is not being used.
519 static int ks8851_net_stop(struct net_device *dev)
521 struct ks8851_net *ks = netdev_priv(dev);
524 netif_info(ks, ifdown, dev, "shutting down\n");
526 netif_stop_queue(dev);
528 ks8851_lock(ks, &flags);
529 /* turn off the IRQs and ack any outstanding */
530 ks8851_wrreg16(ks, KS_IER, 0x0000);
531 ks8851_wrreg16(ks, KS_ISR, 0xffff);
532 ks8851_unlock(ks, &flags);
534 /* stop any outstanding work */
535 ks8851_flush_tx_work(ks);
536 flush_work(&ks->rxctrl_work);
538 ks8851_lock(ks, &flags);
539 /* shutdown RX process */
540 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
542 /* shutdown TX process */
543 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
545 /* set powermode to soft power down to save power */
546 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
547 ks8851_unlock(ks, &flags);
549 /* ensure any queued tx buffers are dumped */
550 while (!skb_queue_empty(&ks->txq)) {
551 struct sk_buff *txb = skb_dequeue(&ks->txq);
553 netif_dbg(ks, ifdown, ks->netdev,
554 "%s: freeing txb %p\n", __func__, txb);
559 free_irq(dev->irq, ks);
565 * ks8851_start_xmit - transmit packet
566 * @skb: The buffer to transmit
567 * @dev: The device used to transmit the packet.
569 * Called by the network layer to transmit the @skb. Queue the packet for
570 * the device and schedule the necessary work to transmit the packet when
573 * We do this to firstly avoid sleeping with the network device locked,
574 * and secondly so we can round up more than one packet to transmit which
575 * means we can try and avoid generating too many transmit done interrupts.
577 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
578 struct net_device *dev)
580 struct ks8851_net *ks = netdev_priv(dev);
582 return ks->start_xmit(skb, dev);
586 * ks8851_rxctrl_work - work handler to change rx mode
587 * @work: The work structure this belongs to.
589 * Lock the device and issue the necessary changes to the receive mode from
590 * the network device layer. This is done so that we can do this without
591 * having to sleep whilst holding the network device lock.
593 * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
594 * receive parameters are programmed, we issue a write to disable the RXQ and
595 * then wait for the interrupt handler to be triggered once the RXQ shutdown is
596 * complete. The interrupt handler then writes the new values into the chip.
598 static void ks8851_rxctrl_work(struct work_struct *work)
600 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
603 ks8851_lock(ks, &flags);
605 /* need to shutdown RXQ before modifying filter parameters */
606 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
608 ks8851_unlock(ks, &flags);
611 static void ks8851_set_rx_mode(struct net_device *dev)
613 struct ks8851_net *ks = netdev_priv(dev);
614 struct ks8851_rxctrl rxctrl;
616 memset(&rxctrl, 0, sizeof(rxctrl));
618 if (dev->flags & IFF_PROMISC) {
619 /* interface to receive everything */
621 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
622 } else if (dev->flags & IFF_ALLMULTI) {
623 /* accept all multicast packets */
625 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
626 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
627 } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
628 struct netdev_hw_addr *ha;
631 /* accept some multicast */
633 netdev_for_each_mc_addr(ha, dev) {
634 crc = ether_crc(ETH_ALEN, ha->addr);
635 crc >>= (32 - 6); /* get top six bits */
637 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
640 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
642 /* just accept broadcast / unicast */
643 rxctrl.rxcr1 = RXCR1_RXPAFMA;
646 rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
647 RXCR1_RXBE | /* broadcast enable */
648 RXCR1_RXE | /* RX process enable */
649 RXCR1_RXFCE); /* enable flow control */
651 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
653 /* schedule work to do the actual set of the data if needed */
655 spin_lock(&ks->statelock);
657 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
658 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
659 schedule_work(&ks->rxctrl_work);
662 spin_unlock(&ks->statelock);
665 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
667 struct sockaddr *sa = addr;
669 if (netif_running(dev))
672 if (!is_valid_ether_addr(sa->sa_data))
673 return -EADDRNOTAVAIL;
675 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
676 return ks8851_write_mac_addr(dev);
679 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
681 struct ks8851_net *ks = netdev_priv(dev);
683 if (!netif_running(dev))
686 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
689 static const struct net_device_ops ks8851_netdev_ops = {
690 .ndo_open = ks8851_net_open,
691 .ndo_stop = ks8851_net_stop,
692 .ndo_do_ioctl = ks8851_net_ioctl,
693 .ndo_start_xmit = ks8851_start_xmit,
694 .ndo_set_mac_address = ks8851_set_mac_address,
695 .ndo_set_rx_mode = ks8851_set_rx_mode,
696 .ndo_validate_addr = eth_validate_addr,
699 /* ethtool support */
701 static void ks8851_get_drvinfo(struct net_device *dev,
702 struct ethtool_drvinfo *di)
704 strlcpy(di->driver, "KS8851", sizeof(di->driver));
705 strlcpy(di->version, "1.00", sizeof(di->version));
706 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
709 static u32 ks8851_get_msglevel(struct net_device *dev)
711 struct ks8851_net *ks = netdev_priv(dev);
712 return ks->msg_enable;
715 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
717 struct ks8851_net *ks = netdev_priv(dev);
721 static int ks8851_get_link_ksettings(struct net_device *dev,
722 struct ethtool_link_ksettings *cmd)
724 struct ks8851_net *ks = netdev_priv(dev);
726 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
731 static int ks8851_set_link_ksettings(struct net_device *dev,
732 const struct ethtool_link_ksettings *cmd)
734 struct ks8851_net *ks = netdev_priv(dev);
735 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
738 static u32 ks8851_get_link(struct net_device *dev)
740 struct ks8851_net *ks = netdev_priv(dev);
741 return mii_link_ok(&ks->mii);
744 static int ks8851_nway_reset(struct net_device *dev)
746 struct ks8851_net *ks = netdev_priv(dev);
747 return mii_nway_restart(&ks->mii);
752 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
754 struct ks8851_net *ks = ee->data;
757 val = ks8851_rdreg16(ks, KS_EEPCR);
759 ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
760 ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
761 ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
764 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
766 struct ks8851_net *ks = ee->data;
767 unsigned val = EEPCR_EESA; /* default - eeprom access on */
773 if (ee->reg_data_clock)
775 if (ee->reg_chip_select)
778 ks8851_wrreg16(ks, KS_EEPCR, val);
782 * ks8851_eeprom_claim - claim device EEPROM and activate the interface
783 * @ks: The network device state.
785 * Check for the presence of an EEPROM, and then activate software access
788 static int ks8851_eeprom_claim(struct ks8851_net *ks)
790 /* start with clock low, cs high */
791 ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
796 * ks8851_eeprom_release - release the EEPROM interface
797 * @ks: The device state
799 * Release the software access to the device EEPROM
801 static void ks8851_eeprom_release(struct ks8851_net *ks)
803 unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
805 ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
808 #define KS_EEPROM_MAGIC (0x00008851)
810 static int ks8851_set_eeprom(struct net_device *dev,
811 struct ethtool_eeprom *ee, u8 *data)
813 struct ks8851_net *ks = netdev_priv(dev);
814 int offset = ee->offset;
819 /* currently only support byte writing */
823 if (ee->magic != KS_EEPROM_MAGIC)
826 if (!(ks->rc_ccr & CCR_EEPROM))
829 ks8851_lock(ks, &flags);
831 ks8851_eeprom_claim(ks);
833 eeprom_93cx6_wren(&ks->eeprom, true);
835 /* ethtool currently only supports writing bytes, which means
836 * we have to read/modify/write our 16bit EEPROMs */
838 eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
848 eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
849 eeprom_93cx6_wren(&ks->eeprom, false);
851 ks8851_eeprom_release(ks);
852 ks8851_unlock(ks, &flags);
857 static int ks8851_get_eeprom(struct net_device *dev,
858 struct ethtool_eeprom *ee, u8 *data)
860 struct ks8851_net *ks = netdev_priv(dev);
861 int offset = ee->offset;
865 /* must be 2 byte aligned */
866 if (len & 1 || offset & 1)
869 if (!(ks->rc_ccr & CCR_EEPROM))
872 ks8851_lock(ks, &flags);
874 ks8851_eeprom_claim(ks);
876 ee->magic = KS_EEPROM_MAGIC;
878 eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
879 ks8851_eeprom_release(ks);
880 ks8851_unlock(ks, &flags);
885 static int ks8851_get_eeprom_len(struct net_device *dev)
887 struct ks8851_net *ks = netdev_priv(dev);
889 /* currently, we assume it is an 93C46 attached, so return 128 */
890 return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
893 static const struct ethtool_ops ks8851_ethtool_ops = {
894 .get_drvinfo = ks8851_get_drvinfo,
895 .get_msglevel = ks8851_get_msglevel,
896 .set_msglevel = ks8851_set_msglevel,
897 .get_link = ks8851_get_link,
898 .nway_reset = ks8851_nway_reset,
899 .get_eeprom_len = ks8851_get_eeprom_len,
900 .get_eeprom = ks8851_get_eeprom,
901 .set_eeprom = ks8851_set_eeprom,
902 .get_link_ksettings = ks8851_get_link_ksettings,
903 .set_link_ksettings = ks8851_set_link_ksettings,
906 /* MII interface controls */
909 * ks8851_phy_reg - convert MII register into a KS8851 register
910 * @reg: MII register number.
912 * Return the KS8851 register number for the corresponding MII PHY register
913 * if possible. Return zero if the MII register has no direct mapping to the
914 * KS8851 register set.
916 static int ks8851_phy_reg(int reg)
936 static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg)
938 struct ks8851_net *ks = netdev_priv(dev);
943 ksreg = ks8851_phy_reg(reg);
947 ks8851_lock(ks, &flags);
948 result = ks8851_rdreg16(ks, ksreg);
949 ks8851_unlock(ks, &flags);
955 * ks8851_phy_read - MII interface PHY register read.
956 * @dev: The network device the PHY is on.
957 * @phy_addr: Address of PHY (ignored as we only have one)
958 * @reg: The register to read.
960 * This call reads data from the PHY register specified in @reg. Since the
961 * device does not support all the MII registers, the non-existent values
962 * are always returned as zero.
964 * We return zero for unsupported registers as the MII code does not check
965 * the value returned for any error status, and simply returns it to the
966 * caller. The mii-tool that the driver was tested with takes any -ve error
967 * as real PHY capabilities, thus displaying incorrect data to the user.
969 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
973 ret = ks8851_phy_read_common(dev, phy_addr, reg);
975 return 0x0; /* no error return allowed, so use zero */
980 static void ks8851_phy_write(struct net_device *dev,
981 int phy, int reg, int value)
983 struct ks8851_net *ks = netdev_priv(dev);
987 ksreg = ks8851_phy_reg(reg);
989 ks8851_lock(ks, &flags);
990 ks8851_wrreg16(ks, ksreg, value);
991 ks8851_unlock(ks, &flags);
995 static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg)
997 struct ks8851_net *ks = bus->priv;
1002 /* KS8851 PHY ID registers are swapped in HW, swap them back. */
1003 if (reg == MII_PHYSID1)
1005 else if (reg == MII_PHYSID2)
1008 return ks8851_phy_read_common(ks->netdev, phy_id, reg);
1011 static int ks8851_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
1013 struct ks8851_net *ks = bus->priv;
1015 ks8851_phy_write(ks->netdev, phy_id, reg, val);
1020 * ks8851_read_selftest - read the selftest memory info.
1021 * @ks: The device state
1023 * Read and check the TX/RX memory selftest information.
1025 static void ks8851_read_selftest(struct ks8851_net *ks)
1027 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1030 rd = ks8851_rdreg16(ks, KS_MBIR);
1032 if ((rd & both_done) != both_done) {
1033 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1037 if (rd & MBIR_TXMBFA)
1038 netdev_err(ks->netdev, "TX memory selftest fail\n");
1040 if (rd & MBIR_RXMBFA)
1041 netdev_err(ks->netdev, "RX memory selftest fail\n");
1044 /* driver bus management functions */
1046 #ifdef CONFIG_PM_SLEEP
1048 int ks8851_suspend(struct device *dev)
1050 struct ks8851_net *ks = dev_get_drvdata(dev);
1051 struct net_device *netdev = ks->netdev;
1053 if (netif_running(netdev)) {
1054 netif_device_detach(netdev);
1055 ks8851_net_stop(netdev);
1061 int ks8851_resume(struct device *dev)
1063 struct ks8851_net *ks = dev_get_drvdata(dev);
1064 struct net_device *netdev = ks->netdev;
1066 if (netif_running(netdev)) {
1067 ks8851_net_open(netdev);
1068 netif_device_attach(netdev);
1075 static int ks8851_register_mdiobus(struct ks8851_net *ks, struct device *dev)
1077 struct mii_bus *mii_bus;
1080 mii_bus = mdiobus_alloc();
1084 mii_bus->name = "ks8851_eth_mii";
1085 mii_bus->read = ks8851_mdio_read;
1086 mii_bus->write = ks8851_mdio_write;
1088 mii_bus->parent = dev;
1089 mii_bus->phy_mask = ~((u32)BIT(0));
1090 snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
1092 ret = mdiobus_register(mii_bus);
1094 goto err_mdiobus_register;
1096 ks->mii_bus = mii_bus;
1100 err_mdiobus_register:
1101 mdiobus_free(mii_bus);
1105 static void ks8851_unregister_mdiobus(struct ks8851_net *ks)
1107 mdiobus_unregister(ks->mii_bus);
1108 mdiobus_free(ks->mii_bus);
1111 int ks8851_probe_common(struct net_device *netdev, struct device *dev,
1114 struct ks8851_net *ks = netdev_priv(netdev);
1119 ks->netdev = netdev;
1120 ks->tx_space = 6144;
1122 gpio = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0, NULL);
1123 if (gpio == -EPROBE_DEFER)
1127 if (gpio_is_valid(gpio)) {
1128 ret = devm_gpio_request_one(dev, gpio,
1129 GPIOF_OUT_INIT_LOW, "ks8851_rst_n");
1131 dev_err(dev, "reset gpio request failed\n");
1136 ks->vdd_io = devm_regulator_get(dev, "vdd-io");
1137 if (IS_ERR(ks->vdd_io)) {
1138 ret = PTR_ERR(ks->vdd_io);
1142 ret = regulator_enable(ks->vdd_io);
1144 dev_err(dev, "regulator vdd_io enable fail: %d\n", ret);
1148 ks->vdd_reg = devm_regulator_get(dev, "vdd");
1149 if (IS_ERR(ks->vdd_reg)) {
1150 ret = PTR_ERR(ks->vdd_reg);
1154 ret = regulator_enable(ks->vdd_reg);
1156 dev_err(dev, "regulator vdd enable fail: %d\n", ret);
1160 if (gpio_is_valid(gpio)) {
1161 usleep_range(10000, 11000);
1162 gpio_set_value(gpio, 1);
1165 spin_lock_init(&ks->statelock);
1167 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1169 SET_NETDEV_DEV(netdev, dev);
1171 /* setup EEPROM state */
1172 ks->eeprom.data = ks;
1173 ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
1174 ks->eeprom.register_read = ks8851_eeprom_regread;
1175 ks->eeprom.register_write = ks8851_eeprom_regwrite;
1177 /* setup mii state */
1178 ks->mii.dev = netdev;
1180 ks->mii.phy_id_mask = 1;
1181 ks->mii.reg_num_mask = 0xf;
1182 ks->mii.mdio_read = ks8851_phy_read;
1183 ks->mii.mdio_write = ks8851_phy_write;
1185 dev_info(dev, "message enable is %d\n", msg_en);
1187 ret = ks8851_register_mdiobus(ks, dev);
1191 /* set the default message enable */
1192 ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV |
1196 skb_queue_head_init(&ks->txq);
1198 netdev->ethtool_ops = &ks8851_ethtool_ops;
1200 dev_set_drvdata(dev, ks);
1202 netif_carrier_off(ks->netdev);
1203 netdev->if_port = IF_PORT_100BASET;
1204 netdev->netdev_ops = &ks8851_netdev_ops;
1206 /* issue a global soft reset to reset the device. */
1207 ks8851_soft_reset(ks, GRR_GSR);
1209 /* simple check for a valid chip being connected to the bus */
1210 cider = ks8851_rdreg16(ks, KS_CIDER);
1211 if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
1212 dev_err(dev, "failed to read device ID\n");
1217 /* cache the contents of the CCR register for EEPROM, etc. */
1218 ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
1220 ks8851_read_selftest(ks);
1221 ks8851_init_mac(ks, dev->of_node);
1223 ret = register_netdev(netdev);
1225 dev_err(dev, "failed to register network device\n");
1229 netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1230 CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq,
1231 ks->rc_ccr & CCR_EEPROM ? "has" : "no");
1236 ks8851_unregister_mdiobus(ks);
1238 if (gpio_is_valid(gpio))
1239 gpio_set_value(gpio, 0);
1240 regulator_disable(ks->vdd_reg);
1242 regulator_disable(ks->vdd_io);
1247 int ks8851_remove_common(struct device *dev)
1249 struct ks8851_net *priv = dev_get_drvdata(dev);
1251 ks8851_unregister_mdiobus(priv);
1253 if (netif_msg_drv(priv))
1254 dev_info(dev, "remove\n");
1256 unregister_netdev(priv->netdev);
1257 if (gpio_is_valid(priv->gpio))
1258 gpio_set_value(priv->gpio, 0);
1259 regulator_disable(priv->vdd_reg);
1260 regulator_disable(priv->vdd_io);