Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* drivers/net/ethernet/micrel/ks8851.c
0003  *
0004  * Copyright 2009 Simtec Electronics
0005  *  http://www.simtec.co.uk/
0006  *  Ben Dooks <ben@simtec.co.uk>
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/kernel.h>
0014 #include <linux/netdevice.h>
0015 #include <linux/etherdevice.h>
0016 #include <linux/ethtool.h>
0017 #include <linux/cache.h>
0018 #include <linux/crc32.h>
0019 #include <linux/mii.h>
0020 #include <linux/regulator/consumer.h>
0021 
0022 #include <linux/gpio.h>
0023 #include <linux/of_gpio.h>
0024 #include <linux/of_mdio.h>
0025 #include <linux/of_net.h>
0026 
0027 #include "ks8851.h"
0028 
0029 /**
0030  * ks8851_lock - register access lock
0031  * @ks: The chip state
0032  * @flags: Spinlock flags
0033  *
0034  * Claim chip register access lock
0035  */
0036 static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
0037 {
0038     ks->lock(ks, flags);
0039 }
0040 
0041 /**
0042  * ks8851_unlock - register access unlock
0043  * @ks: The chip state
0044  * @flags: Spinlock flags
0045  *
0046  * Release chip register access lock
0047  */
0048 static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
0049 {
0050     ks->unlock(ks, flags);
0051 }
0052 
0053 /**
0054  * ks8851_wrreg16 - write 16bit register value to chip
0055  * @ks: The chip state
0056  * @reg: The register address
0057  * @val: The value to write
0058  *
0059  * Issue a write to put the value @val into the register specified in @reg.
0060  */
0061 static void ks8851_wrreg16(struct ks8851_net *ks, unsigned int reg,
0062                unsigned int val)
0063 {
0064     ks->wrreg16(ks, reg, val);
0065 }
0066 
0067 /**
0068  * ks8851_rdreg16 - read 16 bit register from device
0069  * @ks: The chip information
0070  * @reg: The register address
0071  *
0072  * Read a 16bit register from the chip, returning the result
0073  */
0074 static unsigned int ks8851_rdreg16(struct ks8851_net *ks,
0075                    unsigned int reg)
0076 {
0077     return ks->rdreg16(ks, reg);
0078 }
0079 
0080 /**
0081  * ks8851_soft_reset - issue one of the soft reset to the device
0082  * @ks: The device state.
0083  * @op: The bit(s) to set in the GRR
0084  *
0085  * Issue the relevant soft-reset command to the device's GRR register
0086  * specified by @op.
0087  *
0088  * Note, the delays are in there as a caution to ensure that the reset
0089  * has time to take effect and then complete. Since the datasheet does
0090  * not currently specify the exact sequence, we have chosen something
0091  * that seems to work with our device.
0092  */
0093 static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
0094 {
0095     ks8851_wrreg16(ks, KS_GRR, op);
0096     mdelay(1);  /* wait a short time to effect reset */
0097     ks8851_wrreg16(ks, KS_GRR, 0);
0098     mdelay(1);  /* wait for condition to clear */
0099 }
0100 
0101 /**
0102  * ks8851_set_powermode - set power mode of the device
0103  * @ks: The device state
0104  * @pwrmode: The power mode value to write to KS_PMECR.
0105  *
0106  * Change the power mode of the chip.
0107  */
0108 static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
0109 {
0110     unsigned pmecr;
0111 
0112     netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
0113 
0114     pmecr = ks8851_rdreg16(ks, KS_PMECR);
0115     pmecr &= ~PMECR_PM_MASK;
0116     pmecr |= pwrmode;
0117 
0118     ks8851_wrreg16(ks, KS_PMECR, pmecr);
0119 }
0120 
0121 /**
0122  * ks8851_write_mac_addr - write mac address to device registers
0123  * @dev: The network device
0124  *
0125  * Update the KS8851 MAC address registers from the address in @dev.
0126  *
0127  * This call assumes that the chip is not running, so there is no need to
0128  * shutdown the RXQ process whilst setting this.
0129 */
0130 static int ks8851_write_mac_addr(struct net_device *dev)
0131 {
0132     struct ks8851_net *ks = netdev_priv(dev);
0133     unsigned long flags;
0134     u16 val;
0135     int i;
0136 
0137     ks8851_lock(ks, &flags);
0138 
0139     /*
0140      * Wake up chip in case it was powered off when stopped; otherwise,
0141      * the first write to the MAC address does not take effect.
0142      */
0143     ks8851_set_powermode(ks, PMECR_PM_NORMAL);
0144 
0145     for (i = 0; i < ETH_ALEN; i += 2) {
0146         val = (dev->dev_addr[i] << 8) | dev->dev_addr[i + 1];
0147         ks8851_wrreg16(ks, KS_MAR(i), val);
0148     }
0149 
0150     if (!netif_running(dev))
0151         ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
0152 
0153     ks8851_unlock(ks, &flags);
0154 
0155     return 0;
0156 }
0157 
0158 /**
0159  * ks8851_read_mac_addr - read mac address from device registers
0160  * @dev: The network device
0161  *
0162  * Update our copy of the KS8851 MAC address from the registers of @dev.
0163 */
0164 static void ks8851_read_mac_addr(struct net_device *dev)
0165 {
0166     struct ks8851_net *ks = netdev_priv(dev);
0167     unsigned long flags;
0168     u8 addr[ETH_ALEN];
0169     u16 reg;
0170     int i;
0171 
0172     ks8851_lock(ks, &flags);
0173 
0174     for (i = 0; i < ETH_ALEN; i += 2) {
0175         reg = ks8851_rdreg16(ks, KS_MAR(i));
0176         addr[i] = reg >> 8;
0177         addr[i + 1] = reg & 0xff;
0178     }
0179     eth_hw_addr_set(dev, addr);
0180 
0181     ks8851_unlock(ks, &flags);
0182 }
0183 
0184 /**
0185  * ks8851_init_mac - initialise the mac address
0186  * @ks: The device structure
0187  * @np: The device node pointer
0188  *
0189  * Get or create the initial mac address for the device and then set that
0190  * into the station address register. A mac address supplied in the device
0191  * tree takes precedence. Otherwise, if there is an EEPROM present, then
0192  * we try that. If no valid mac address is found we use eth_random_addr()
0193  * to create a new one.
0194  */
0195 static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np)
0196 {
0197     struct net_device *dev = ks->netdev;
0198     int ret;
0199 
0200     ret = of_get_ethdev_address(np, dev);
0201     if (!ret) {
0202         ks8851_write_mac_addr(dev);
0203         return;
0204     }
0205 
0206     if (ks->rc_ccr & CCR_EEPROM) {
0207         ks8851_read_mac_addr(dev);
0208         if (is_valid_ether_addr(dev->dev_addr))
0209             return;
0210 
0211         netdev_err(ks->netdev, "invalid mac address read %pM\n",
0212                 dev->dev_addr);
0213     }
0214 
0215     eth_hw_addr_random(dev);
0216     ks8851_write_mac_addr(dev);
0217 }
0218 
0219 /**
0220  * ks8851_dbg_dumpkkt - dump initial packet contents to debug
0221  * @ks: The device state
0222  * @rxpkt: The data for the received packet
0223  *
0224  * Dump the initial data from the packet to dev_dbg().
0225  */
0226 static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
0227 {
0228     netdev_dbg(ks->netdev,
0229            "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
0230            rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
0231            rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
0232            rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
0233 }
0234 
0235 /**
0236  * ks8851_rx_skb - receive skbuff
0237  * @ks: The device state.
0238  * @skb: The skbuff
0239  */
0240 static void ks8851_rx_skb(struct ks8851_net *ks, struct sk_buff *skb)
0241 {
0242     ks->rx_skb(ks, skb);
0243 }
0244 
0245 /**
0246  * ks8851_rx_pkts - receive packets from the host
0247  * @ks: The device information.
0248  *
0249  * This is called from the IRQ work queue when the system detects that there
0250  * are packets in the receive queue. Find out how many packets there are and
0251  * read them from the FIFO.
0252  */
0253 static void ks8851_rx_pkts(struct ks8851_net *ks)
0254 {
0255     struct sk_buff *skb;
0256     unsigned rxfc;
0257     unsigned rxlen;
0258     unsigned rxstat;
0259     u8 *rxpkt;
0260 
0261     rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff;
0262 
0263     netif_dbg(ks, rx_status, ks->netdev,
0264           "%s: %d packets\n", __func__, rxfc);
0265 
0266     /* Currently we're issuing a read per packet, but we could possibly
0267      * improve the code by issuing a single read, getting the receive
0268      * header, allocating the packet and then reading the packet data
0269      * out in one go.
0270      *
0271      * This form of operation would require us to hold the SPI bus'
0272      * chipselect low during the entie transaction to avoid any
0273      * reset to the data stream coming from the chip.
0274      */
0275 
0276     for (; rxfc != 0; rxfc--) {
0277         rxstat = ks8851_rdreg16(ks, KS_RXFHSR);
0278         rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK;
0279 
0280         netif_dbg(ks, rx_status, ks->netdev,
0281               "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
0282 
0283         /* the length of the packet includes the 32bit CRC */
0284 
0285         /* set dma read address */
0286         ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
0287 
0288         /* start DMA access */
0289         ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
0290 
0291         if (rxlen > 4) {
0292             unsigned int rxalign;
0293 
0294             rxlen -= 4;
0295             rxalign = ALIGN(rxlen, 4);
0296             skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
0297             if (skb) {
0298 
0299                 /* 4 bytes of status header + 4 bytes of
0300                  * garbage: we put them before ethernet
0301                  * header, so that they are copied,
0302                  * but ignored.
0303                  */
0304 
0305                 rxpkt = skb_put(skb, rxlen) - 8;
0306 
0307                 ks->rdfifo(ks, rxpkt, rxalign + 8);
0308 
0309                 if (netif_msg_pktdata(ks))
0310                     ks8851_dbg_dumpkkt(ks, rxpkt);
0311 
0312                 skb->protocol = eth_type_trans(skb, ks->netdev);
0313                 ks8851_rx_skb(ks, skb);
0314 
0315                 ks->netdev->stats.rx_packets++;
0316                 ks->netdev->stats.rx_bytes += rxlen;
0317             }
0318         }
0319 
0320         /* end DMA access and dequeue packet */
0321         ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF);
0322     }
0323 }
0324 
0325 /**
0326  * ks8851_irq - IRQ handler for dealing with interrupt requests
0327  * @irq: IRQ number
0328  * @_ks: cookie
0329  *
0330  * This handler is invoked when the IRQ line asserts to find out what happened.
0331  * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
0332  * in thread context.
0333  *
0334  * Read the interrupt status, work out what needs to be done and then clear
0335  * any of the interrupts that are not needed.
0336  */
0337 static irqreturn_t ks8851_irq(int irq, void *_ks)
0338 {
0339     struct ks8851_net *ks = _ks;
0340     unsigned handled = 0;
0341     unsigned long flags;
0342     unsigned int status;
0343 
0344     ks8851_lock(ks, &flags);
0345 
0346     status = ks8851_rdreg16(ks, KS_ISR);
0347 
0348     netif_dbg(ks, intr, ks->netdev,
0349           "%s: status 0x%04x\n", __func__, status);
0350 
0351     if (status & IRQ_LCI)
0352         handled |= IRQ_LCI;
0353 
0354     if (status & IRQ_LDI) {
0355         u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
0356         pmecr &= ~PMECR_WKEVT_MASK;
0357         ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
0358 
0359         handled |= IRQ_LDI;
0360     }
0361 
0362     if (status & IRQ_RXPSI)
0363         handled |= IRQ_RXPSI;
0364 
0365     if (status & IRQ_TXI) {
0366         handled |= IRQ_TXI;
0367 
0368         /* no lock here, tx queue should have been stopped */
0369 
0370         /* update our idea of how much tx space is available to the
0371          * system */
0372         ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
0373 
0374         netif_dbg(ks, intr, ks->netdev,
0375               "%s: txspace %d\n", __func__, ks->tx_space);
0376     }
0377 
0378     if (status & IRQ_RXI)
0379         handled |= IRQ_RXI;
0380 
0381     if (status & IRQ_SPIBEI) {
0382         netdev_err(ks->netdev, "%s: spi bus error\n", __func__);
0383         handled |= IRQ_SPIBEI;
0384     }
0385 
0386     ks8851_wrreg16(ks, KS_ISR, handled);
0387 
0388     if (status & IRQ_RXI) {
0389         /* the datasheet says to disable the rx interrupt during
0390          * packet read-out, however we're masking the interrupt
0391          * from the device so do not bother masking just the RX
0392          * from the device. */
0393 
0394         ks8851_rx_pkts(ks);
0395     }
0396 
0397     /* if something stopped the rx process, probably due to wanting
0398      * to change the rx settings, then do something about restarting
0399      * it. */
0400     if (status & IRQ_RXPSI) {
0401         struct ks8851_rxctrl *rxc = &ks->rxctrl;
0402 
0403         /* update the multicast hash table */
0404         ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
0405         ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
0406         ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
0407         ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
0408 
0409         ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
0410         ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
0411     }
0412 
0413     ks8851_unlock(ks, &flags);
0414 
0415     if (status & IRQ_LCI)
0416         mii_check_link(&ks->mii);
0417 
0418     if (status & IRQ_TXI)
0419         netif_wake_queue(ks->netdev);
0420 
0421     return IRQ_HANDLED;
0422 }
0423 
0424 /**
0425  * ks8851_flush_tx_work - flush outstanding TX work
0426  * @ks: The device state
0427  */
0428 static void ks8851_flush_tx_work(struct ks8851_net *ks)
0429 {
0430     if (ks->flush_tx_work)
0431         ks->flush_tx_work(ks);
0432 }
0433 
0434 /**
0435  * ks8851_net_open - open network device
0436  * @dev: The network device being opened.
0437  *
0438  * Called when the network device is marked active, such as a user executing
0439  * 'ifconfig up' on the device.
0440  */
0441 static int ks8851_net_open(struct net_device *dev)
0442 {
0443     struct ks8851_net *ks = netdev_priv(dev);
0444     unsigned long flags;
0445     int ret;
0446 
0447     ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
0448                    IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0449                    dev->name, ks);
0450     if (ret < 0) {
0451         netdev_err(dev, "failed to get irq\n");
0452         return ret;
0453     }
0454 
0455     /* lock the card, even if we may not actually be doing anything
0456      * else at the moment */
0457     ks8851_lock(ks, &flags);
0458 
0459     netif_dbg(ks, ifup, ks->netdev, "opening\n");
0460 
0461     /* bring chip out of any power saving mode it was in */
0462     ks8851_set_powermode(ks, PMECR_PM_NORMAL);
0463 
0464     /* issue a soft reset to the RX/TX QMU to put it into a known
0465      * state. */
0466     ks8851_soft_reset(ks, GRR_QMU);
0467 
0468     /* setup transmission parameters */
0469 
0470     ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
0471                      TXCR_TXPE | /* pad to min length */
0472                      TXCR_TXCRC | /* add CRC */
0473                      TXCR_TXFCE)); /* enable flow control */
0474 
0475     /* auto-increment tx data, reset tx pointer */
0476     ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
0477 
0478     /* setup receiver control */
0479 
0480     ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /*  from mac filter */
0481                       RXCR1_RXFCE | /* enable flow control */
0482                       RXCR1_RXBE | /* broadcast enable */
0483                       RXCR1_RXUE | /* unicast enable */
0484                       RXCR1_RXE)); /* enable rx block */
0485 
0486     /* transfer entire frames out in one go */
0487     ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
0488 
0489     /* set receive counter timeouts */
0490     ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
0491     ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
0492     ks8851_wrreg16(ks, KS_RXFCTR, 10);  /* 10 frames to IRQ */
0493 
0494     ks->rc_rxqcr = (RXQCR_RXFCTE |  /* IRQ on frame count exceeded */
0495             RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
0496             RXQCR_RXDTTE);  /* IRQ on time exceeded */
0497 
0498     ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
0499 
0500     /* clear then enable interrupts */
0501     ks8851_wrreg16(ks, KS_ISR, ks->rc_ier);
0502     ks8851_wrreg16(ks, KS_IER, ks->rc_ier);
0503 
0504     netif_start_queue(ks->netdev);
0505 
0506     netif_dbg(ks, ifup, ks->netdev, "network device up\n");
0507 
0508     ks8851_unlock(ks, &flags);
0509     mii_check_link(&ks->mii);
0510     return 0;
0511 }
0512 
0513 /**
0514  * ks8851_net_stop - close network device
0515  * @dev: The device being closed.
0516  *
0517  * Called to close down a network device which has been active. Cancell any
0518  * work, shutdown the RX and TX process and then place the chip into a low
0519  * power state whilst it is not being used.
0520  */
0521 static int ks8851_net_stop(struct net_device *dev)
0522 {
0523     struct ks8851_net *ks = netdev_priv(dev);
0524     unsigned long flags;
0525 
0526     netif_info(ks, ifdown, dev, "shutting down\n");
0527 
0528     netif_stop_queue(dev);
0529 
0530     ks8851_lock(ks, &flags);
0531     /* turn off the IRQs and ack any outstanding */
0532     ks8851_wrreg16(ks, KS_IER, 0x0000);
0533     ks8851_wrreg16(ks, KS_ISR, 0xffff);
0534     ks8851_unlock(ks, &flags);
0535 
0536     /* stop any outstanding work */
0537     ks8851_flush_tx_work(ks);
0538     flush_work(&ks->rxctrl_work);
0539 
0540     ks8851_lock(ks, &flags);
0541     /* shutdown RX process */
0542     ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
0543 
0544     /* shutdown TX process */
0545     ks8851_wrreg16(ks, KS_TXCR, 0x0000);
0546 
0547     /* set powermode to soft power down to save power */
0548     ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
0549     ks8851_unlock(ks, &flags);
0550 
0551     /* ensure any queued tx buffers are dumped */
0552     while (!skb_queue_empty(&ks->txq)) {
0553         struct sk_buff *txb = skb_dequeue(&ks->txq);
0554 
0555         netif_dbg(ks, ifdown, ks->netdev,
0556               "%s: freeing txb %p\n", __func__, txb);
0557 
0558         dev_kfree_skb(txb);
0559     }
0560 
0561     free_irq(dev->irq, ks);
0562 
0563     return 0;
0564 }
0565 
0566 /**
0567  * ks8851_start_xmit - transmit packet
0568  * @skb: The buffer to transmit
0569  * @dev: The device used to transmit the packet.
0570  *
0571  * Called by the network layer to transmit the @skb. Queue the packet for
0572  * the device and schedule the necessary work to transmit the packet when
0573  * it is free.
0574  *
0575  * We do this to firstly avoid sleeping with the network device locked,
0576  * and secondly so we can round up more than one packet to transmit which
0577  * means we can try and avoid generating too many transmit done interrupts.
0578  */
0579 static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
0580                      struct net_device *dev)
0581 {
0582     struct ks8851_net *ks = netdev_priv(dev);
0583 
0584     return ks->start_xmit(skb, dev);
0585 }
0586 
0587 /**
0588  * ks8851_rxctrl_work - work handler to change rx mode
0589  * @work: The work structure this belongs to.
0590  *
0591  * Lock the device and issue the necessary changes to the receive mode from
0592  * the network device layer. This is done so that we can do this without
0593  * having to sleep whilst holding the network device lock.
0594  *
0595  * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
0596  * receive parameters are programmed, we issue a write to disable the RXQ and
0597  * then wait for the interrupt handler to be triggered once the RXQ shutdown is
0598  * complete. The interrupt handler then writes the new values into the chip.
0599  */
0600 static void ks8851_rxctrl_work(struct work_struct *work)
0601 {
0602     struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
0603     unsigned long flags;
0604 
0605     ks8851_lock(ks, &flags);
0606 
0607     /* need to shutdown RXQ before modifying filter parameters */
0608     ks8851_wrreg16(ks, KS_RXCR1, 0x00);
0609 
0610     ks8851_unlock(ks, &flags);
0611 }
0612 
0613 static void ks8851_set_rx_mode(struct net_device *dev)
0614 {
0615     struct ks8851_net *ks = netdev_priv(dev);
0616     struct ks8851_rxctrl rxctrl;
0617 
0618     memset(&rxctrl, 0, sizeof(rxctrl));
0619 
0620     if (dev->flags & IFF_PROMISC) {
0621         /* interface to receive everything */
0622 
0623         rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
0624     } else if (dev->flags & IFF_ALLMULTI) {
0625         /* accept all multicast packets */
0626 
0627         rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
0628                 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
0629     } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
0630         struct netdev_hw_addr *ha;
0631         u32 crc;
0632 
0633         /* accept some multicast */
0634 
0635         netdev_for_each_mc_addr(ha, dev) {
0636             crc = ether_crc(ETH_ALEN, ha->addr);
0637             crc >>= (32 - 6);  /* get top six bits */
0638 
0639             rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
0640         }
0641 
0642         rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
0643     } else {
0644         /* just accept broadcast / unicast */
0645         rxctrl.rxcr1 = RXCR1_RXPAFMA;
0646     }
0647 
0648     rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
0649              RXCR1_RXBE | /* broadcast enable */
0650              RXCR1_RXE | /* RX process enable */
0651              RXCR1_RXFCE); /* enable flow control */
0652 
0653     rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
0654 
0655     /* schedule work to do the actual set of the data if needed */
0656 
0657     spin_lock(&ks->statelock);
0658 
0659     if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
0660         memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
0661         schedule_work(&ks->rxctrl_work);
0662     }
0663 
0664     spin_unlock(&ks->statelock);
0665 }
0666 
0667 static int ks8851_set_mac_address(struct net_device *dev, void *addr)
0668 {
0669     struct sockaddr *sa = addr;
0670 
0671     if (netif_running(dev))
0672         return -EBUSY;
0673 
0674     if (!is_valid_ether_addr(sa->sa_data))
0675         return -EADDRNOTAVAIL;
0676 
0677     eth_hw_addr_set(dev, sa->sa_data);
0678     return ks8851_write_mac_addr(dev);
0679 }
0680 
0681 static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
0682 {
0683     struct ks8851_net *ks = netdev_priv(dev);
0684 
0685     if (!netif_running(dev))
0686         return -EINVAL;
0687 
0688     return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
0689 }
0690 
0691 static const struct net_device_ops ks8851_netdev_ops = {
0692     .ndo_open       = ks8851_net_open,
0693     .ndo_stop       = ks8851_net_stop,
0694     .ndo_eth_ioctl      = ks8851_net_ioctl,
0695     .ndo_start_xmit     = ks8851_start_xmit,
0696     .ndo_set_mac_address    = ks8851_set_mac_address,
0697     .ndo_set_rx_mode    = ks8851_set_rx_mode,
0698     .ndo_validate_addr  = eth_validate_addr,
0699 };
0700 
0701 /* ethtool support */
0702 
0703 static void ks8851_get_drvinfo(struct net_device *dev,
0704                    struct ethtool_drvinfo *di)
0705 {
0706     strlcpy(di->driver, "KS8851", sizeof(di->driver));
0707     strlcpy(di->version, "1.00", sizeof(di->version));
0708     strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
0709 }
0710 
0711 static u32 ks8851_get_msglevel(struct net_device *dev)
0712 {
0713     struct ks8851_net *ks = netdev_priv(dev);
0714     return ks->msg_enable;
0715 }
0716 
0717 static void ks8851_set_msglevel(struct net_device *dev, u32 to)
0718 {
0719     struct ks8851_net *ks = netdev_priv(dev);
0720     ks->msg_enable = to;
0721 }
0722 
0723 static int ks8851_get_link_ksettings(struct net_device *dev,
0724                      struct ethtool_link_ksettings *cmd)
0725 {
0726     struct ks8851_net *ks = netdev_priv(dev);
0727 
0728     mii_ethtool_get_link_ksettings(&ks->mii, cmd);
0729 
0730     return 0;
0731 }
0732 
0733 static int ks8851_set_link_ksettings(struct net_device *dev,
0734                      const struct ethtool_link_ksettings *cmd)
0735 {
0736     struct ks8851_net *ks = netdev_priv(dev);
0737     return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
0738 }
0739 
0740 static u32 ks8851_get_link(struct net_device *dev)
0741 {
0742     struct ks8851_net *ks = netdev_priv(dev);
0743     return mii_link_ok(&ks->mii);
0744 }
0745 
0746 static int ks8851_nway_reset(struct net_device *dev)
0747 {
0748     struct ks8851_net *ks = netdev_priv(dev);
0749     return mii_nway_restart(&ks->mii);
0750 }
0751 
0752 /* EEPROM support */
0753 
0754 static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
0755 {
0756     struct ks8851_net *ks = ee->data;
0757     unsigned val;
0758 
0759     val = ks8851_rdreg16(ks, KS_EEPCR);
0760 
0761     ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
0762     ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
0763     ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
0764 }
0765 
0766 static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
0767 {
0768     struct ks8851_net *ks = ee->data;
0769     unsigned val = EEPCR_EESA;  /* default - eeprom access on */
0770 
0771     if (ee->drive_data)
0772         val |= EEPCR_EESRWA;
0773     if (ee->reg_data_in)
0774         val |= EEPCR_EEDO;
0775     if (ee->reg_data_clock)
0776         val |= EEPCR_EESCK;
0777     if (ee->reg_chip_select)
0778         val |= EEPCR_EECS;
0779 
0780     ks8851_wrreg16(ks, KS_EEPCR, val);
0781 }
0782 
0783 /**
0784  * ks8851_eeprom_claim - claim device EEPROM and activate the interface
0785  * @ks: The network device state.
0786  *
0787  * Check for the presence of an EEPROM, and then activate software access
0788  * to the device.
0789  */
0790 static int ks8851_eeprom_claim(struct ks8851_net *ks)
0791 {
0792     /* start with clock low, cs high */
0793     ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
0794     return 0;
0795 }
0796 
0797 /**
0798  * ks8851_eeprom_release - release the EEPROM interface
0799  * @ks: The device state
0800  *
0801  * Release the software access to the device EEPROM
0802  */
0803 static void ks8851_eeprom_release(struct ks8851_net *ks)
0804 {
0805     unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
0806 
0807     ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
0808 }
0809 
0810 #define KS_EEPROM_MAGIC (0x00008851)
0811 
0812 static int ks8851_set_eeprom(struct net_device *dev,
0813                  struct ethtool_eeprom *ee, u8 *data)
0814 {
0815     struct ks8851_net *ks = netdev_priv(dev);
0816     int offset = ee->offset;
0817     unsigned long flags;
0818     int len = ee->len;
0819     u16 tmp;
0820 
0821     /* currently only support byte writing */
0822     if (len != 1)
0823         return -EINVAL;
0824 
0825     if (ee->magic != KS_EEPROM_MAGIC)
0826         return -EINVAL;
0827 
0828     if (!(ks->rc_ccr & CCR_EEPROM))
0829         return -ENOENT;
0830 
0831     ks8851_lock(ks, &flags);
0832 
0833     ks8851_eeprom_claim(ks);
0834 
0835     eeprom_93cx6_wren(&ks->eeprom, true);
0836 
0837     /* ethtool currently only supports writing bytes, which means
0838      * we have to read/modify/write our 16bit EEPROMs */
0839 
0840     eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
0841 
0842     if (offset & 1) {
0843         tmp &= 0xff;
0844         tmp |= *data << 8;
0845     } else {
0846         tmp &= 0xff00;
0847         tmp |= *data;
0848     }
0849 
0850     eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
0851     eeprom_93cx6_wren(&ks->eeprom, false);
0852 
0853     ks8851_eeprom_release(ks);
0854     ks8851_unlock(ks, &flags);
0855 
0856     return 0;
0857 }
0858 
0859 static int ks8851_get_eeprom(struct net_device *dev,
0860                  struct ethtool_eeprom *ee, u8 *data)
0861 {
0862     struct ks8851_net *ks = netdev_priv(dev);
0863     int offset = ee->offset;
0864     unsigned long flags;
0865     int len = ee->len;
0866 
0867     /* must be 2 byte aligned */
0868     if (len & 1 || offset & 1)
0869         return -EINVAL;
0870 
0871     if (!(ks->rc_ccr & CCR_EEPROM))
0872         return -ENOENT;
0873 
0874     ks8851_lock(ks, &flags);
0875 
0876     ks8851_eeprom_claim(ks);
0877 
0878     ee->magic = KS_EEPROM_MAGIC;
0879 
0880     eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
0881     ks8851_eeprom_release(ks);
0882     ks8851_unlock(ks, &flags);
0883 
0884     return 0;
0885 }
0886 
0887 static int ks8851_get_eeprom_len(struct net_device *dev)
0888 {
0889     struct ks8851_net *ks = netdev_priv(dev);
0890 
0891     /* currently, we assume it is an 93C46 attached, so return 128 */
0892     return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
0893 }
0894 
0895 static const struct ethtool_ops ks8851_ethtool_ops = {
0896     .get_drvinfo    = ks8851_get_drvinfo,
0897     .get_msglevel   = ks8851_get_msglevel,
0898     .set_msglevel   = ks8851_set_msglevel,
0899     .get_link   = ks8851_get_link,
0900     .nway_reset = ks8851_nway_reset,
0901     .get_eeprom_len = ks8851_get_eeprom_len,
0902     .get_eeprom = ks8851_get_eeprom,
0903     .set_eeprom = ks8851_set_eeprom,
0904     .get_link_ksettings = ks8851_get_link_ksettings,
0905     .set_link_ksettings = ks8851_set_link_ksettings,
0906 };
0907 
0908 /* MII interface controls */
0909 
0910 /**
0911  * ks8851_phy_reg - convert MII register into a KS8851 register
0912  * @reg: MII register number.
0913  *
0914  * Return the KS8851 register number for the corresponding MII PHY register
0915  * if possible. Return zero if the MII register has no direct mapping to the
0916  * KS8851 register set.
0917  */
0918 static int ks8851_phy_reg(int reg)
0919 {
0920     switch (reg) {
0921     case MII_BMCR:
0922         return KS_P1MBCR;
0923     case MII_BMSR:
0924         return KS_P1MBSR;
0925     case MII_PHYSID1:
0926         return KS_PHY1ILR;
0927     case MII_PHYSID2:
0928         return KS_PHY1IHR;
0929     case MII_ADVERTISE:
0930         return KS_P1ANAR;
0931     case MII_LPA:
0932         return KS_P1ANLPR;
0933     }
0934 
0935     return -EOPNOTSUPP;
0936 }
0937 
0938 static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg)
0939 {
0940     struct ks8851_net *ks = netdev_priv(dev);
0941     unsigned long flags;
0942     int result;
0943     int ksreg;
0944 
0945     ksreg = ks8851_phy_reg(reg);
0946     if (ksreg < 0)
0947         return ksreg;
0948 
0949     ks8851_lock(ks, &flags);
0950     result = ks8851_rdreg16(ks, ksreg);
0951     ks8851_unlock(ks, &flags);
0952 
0953     return result;
0954 }
0955 
0956 /**
0957  * ks8851_phy_read - MII interface PHY register read.
0958  * @dev: The network device the PHY is on.
0959  * @phy_addr: Address of PHY (ignored as we only have one)
0960  * @reg: The register to read.
0961  *
0962  * This call reads data from the PHY register specified in @reg. Since the
0963  * device does not support all the MII registers, the non-existent values
0964  * are always returned as zero.
0965  *
0966  * We return zero for unsupported registers as the MII code does not check
0967  * the value returned for any error status, and simply returns it to the
0968  * caller. The mii-tool that the driver was tested with takes any -ve error
0969  * as real PHY capabilities, thus displaying incorrect data to the user.
0970  */
0971 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
0972 {
0973     int ret;
0974 
0975     ret = ks8851_phy_read_common(dev, phy_addr, reg);
0976     if (ret < 0)
0977         return 0x0; /* no error return allowed, so use zero */
0978 
0979     return ret;
0980 }
0981 
0982 static void ks8851_phy_write(struct net_device *dev,
0983                  int phy, int reg, int value)
0984 {
0985     struct ks8851_net *ks = netdev_priv(dev);
0986     unsigned long flags;
0987     int ksreg;
0988 
0989     ksreg = ks8851_phy_reg(reg);
0990     if (ksreg >= 0) {
0991         ks8851_lock(ks, &flags);
0992         ks8851_wrreg16(ks, ksreg, value);
0993         ks8851_unlock(ks, &flags);
0994     }
0995 }
0996 
0997 static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg)
0998 {
0999     struct ks8851_net *ks = bus->priv;
1000 
1001     if (phy_id != 0)
1002         return -EOPNOTSUPP;
1003 
1004     /* KS8851 PHY ID registers are swapped in HW, swap them back. */
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 
1013 static 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  * ks8851_read_selftest - read the selftest memory info.
1023  * @ks: The device state
1024  *
1025  * Read and check the TX/RX memory selftest information.
1026  */
1027 static 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 /* driver bus management functions */
1047 
1048 #ifdef CONFIG_PM_SLEEP
1049 
1050 int 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 }
1062 EXPORT_SYMBOL_GPL(ks8851_suspend);
1063 
1064 int 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 }
1076 EXPORT_SYMBOL_GPL(ks8851_resume);
1077 #endif
1078 
1079 static 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 
1104 err_mdiobus_register:
1105     mdiobus_free(mii_bus);
1106     return ret;
1107 }
1108 
1109 static void ks8851_unregister_mdiobus(struct ks8851_net *ks)
1110 {
1111     mdiobus_unregister(ks->mii_bus);
1112     mdiobus_free(ks->mii_bus);
1113 }
1114 
1115 int 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     /* setup EEPROM state */
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     /* setup mii state */
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     /* set the default message enable */
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     /* issue a global soft reset to reset the device. */
1211     ks8851_soft_reset(ks, GRR_GSR);
1212 
1213     /* simple check for a valid chip being connected to the bus */
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     /* cache the contents of the CCR register for EEPROM, etc. */
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 
1239 err_id:
1240     ks8851_unregister_mdiobus(ks);
1241 err_mdio:
1242     if (gpio_is_valid(gpio))
1243         gpio_set_value(gpio, 0);
1244     regulator_disable(ks->vdd_reg);
1245 err_reg:
1246     regulator_disable(ks->vdd_io);
1247 err_reg_io:
1248     return ret;
1249 }
1250 EXPORT_SYMBOL_GPL(ks8851_probe_common);
1251 
1252 void 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 }
1267 EXPORT_SYMBOL_GPL(ks8851_remove_common);
1268 
1269 MODULE_DESCRIPTION("KS8851 Network driver");
1270 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1271 MODULE_LICENSE("GPL");