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/spi/spi.h>
0023 #include <linux/gpio.h>
0024 #include <linux/of_gpio.h>
0025 #include <linux/of_net.h>
0026 
0027 #include "ks8851.h"
0028 
0029 static int msg_enable;
0030 
0031 /**
0032  * struct ks8851_net_spi - KS8851 SPI driver private data
0033  * @lock: Lock to ensure that the device is not accessed when busy.
0034  * @tx_work: Work queue for tx packets
0035  * @ks8851: KS8851 driver common private data
0036  * @spidev: The spi device we're bound to.
0037  * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
0038  * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
0039  * @spi_xfer1: @spi_msg1 SPI transfer structure
0040  * @spi_xfer2: @spi_msg2 SPI transfer structure
0041  *
0042  * The @lock ensures that the chip is protected when certain operations are
0043  * in progress. When the read or write packet transfer is in progress, most
0044  * of the chip registers are not ccessible until the transfer is finished and
0045  * the DMA has been de-asserted.
0046  */
0047 struct ks8851_net_spi {
0048     struct ks8851_net   ks8851;
0049     struct mutex        lock;
0050     struct work_struct  tx_work;
0051     struct spi_device   *spidev;
0052     struct spi_message  spi_msg1;
0053     struct spi_message  spi_msg2;
0054     struct spi_transfer spi_xfer1;
0055     struct spi_transfer spi_xfer2[2];
0056 };
0057 
0058 #define to_ks8851_spi(ks) container_of((ks), struct ks8851_net_spi, ks8851)
0059 
0060 /* SPI frame opcodes */
0061 #define KS_SPIOP_RD 0x00
0062 #define KS_SPIOP_WR 0x40
0063 #define KS_SPIOP_RXFIFO 0x80
0064 #define KS_SPIOP_TXFIFO 0xC0
0065 
0066 /* shift for byte-enable data */
0067 #define BYTE_EN(_x) ((_x) << 2)
0068 
0069 /* turn register number and byte-enable mask into data for start of packet */
0070 #define MK_OP(_byteen, _reg)    \
0071     (BYTE_EN(_byteen) | (_reg) << (8 + 2) | (_reg) >> 6)
0072 
0073 /**
0074  * ks8851_lock_spi - register access lock
0075  * @ks: The chip state
0076  * @flags: Spinlock flags
0077  *
0078  * Claim chip register access lock
0079  */
0080 static void ks8851_lock_spi(struct ks8851_net *ks, unsigned long *flags)
0081 {
0082     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0083 
0084     mutex_lock(&kss->lock);
0085 }
0086 
0087 /**
0088  * ks8851_unlock_spi - register access unlock
0089  * @ks: The chip state
0090  * @flags: Spinlock flags
0091  *
0092  * Release chip register access lock
0093  */
0094 static void ks8851_unlock_spi(struct ks8851_net *ks, unsigned long *flags)
0095 {
0096     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0097 
0098     mutex_unlock(&kss->lock);
0099 }
0100 
0101 /* SPI register read/write calls.
0102  *
0103  * All these calls issue SPI transactions to access the chip's registers. They
0104  * all require that the necessary lock is held to prevent accesses when the
0105  * chip is busy transferring packet data (RX/TX FIFO accesses).
0106  */
0107 
0108 /**
0109  * ks8851_wrreg16_spi - write 16bit register value to chip via SPI
0110  * @ks: The chip state
0111  * @reg: The register address
0112  * @val: The value to write
0113  *
0114  * Issue a write to put the value @val into the register specified in @reg.
0115  */
0116 static void ks8851_wrreg16_spi(struct ks8851_net *ks, unsigned int reg,
0117                    unsigned int val)
0118 {
0119     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0120     struct spi_transfer *xfer = &kss->spi_xfer1;
0121     struct spi_message *msg = &kss->spi_msg1;
0122     __le16 txb[2];
0123     int ret;
0124 
0125     txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
0126     txb[1] = cpu_to_le16(val);
0127 
0128     xfer->tx_buf = txb;
0129     xfer->rx_buf = NULL;
0130     xfer->len = 4;
0131 
0132     ret = spi_sync(kss->spidev, msg);
0133     if (ret < 0)
0134         netdev_err(ks->netdev, "spi_sync() failed\n");
0135 }
0136 
0137 /**
0138  * ks8851_rdreg - issue read register command and return the data
0139  * @ks: The device state
0140  * @op: The register address and byte enables in message format.
0141  * @rxb: The RX buffer to return the result into
0142  * @rxl: The length of data expected.
0143  *
0144  * This is the low level read call that issues the necessary spi message(s)
0145  * to read data from the register specified in @op.
0146  */
0147 static void ks8851_rdreg(struct ks8851_net *ks, unsigned int op,
0148              u8 *rxb, unsigned int rxl)
0149 {
0150     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0151     struct spi_transfer *xfer;
0152     struct spi_message *msg;
0153     __le16 *txb = (__le16 *)ks->txd;
0154     u8 *trx = ks->rxd;
0155     int ret;
0156 
0157     txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
0158 
0159     if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX) {
0160         msg = &kss->spi_msg2;
0161         xfer = kss->spi_xfer2;
0162 
0163         xfer->tx_buf = txb;
0164         xfer->rx_buf = NULL;
0165         xfer->len = 2;
0166 
0167         xfer++;
0168         xfer->tx_buf = NULL;
0169         xfer->rx_buf = trx;
0170         xfer->len = rxl;
0171     } else {
0172         msg = &kss->spi_msg1;
0173         xfer = &kss->spi_xfer1;
0174 
0175         xfer->tx_buf = txb;
0176         xfer->rx_buf = trx;
0177         xfer->len = rxl + 2;
0178     }
0179 
0180     ret = spi_sync(kss->spidev, msg);
0181     if (ret < 0)
0182         netdev_err(ks->netdev, "read: spi_sync() failed\n");
0183     else if (kss->spidev->master->flags & SPI_MASTER_HALF_DUPLEX)
0184         memcpy(rxb, trx, rxl);
0185     else
0186         memcpy(rxb, trx + 2, rxl);
0187 }
0188 
0189 /**
0190  * ks8851_rdreg16_spi - read 16 bit register from device via SPI
0191  * @ks: The chip information
0192  * @reg: The register address
0193  *
0194  * Read a 16bit register from the chip, returning the result
0195  */
0196 static unsigned int ks8851_rdreg16_spi(struct ks8851_net *ks, unsigned int reg)
0197 {
0198     __le16 rx = 0;
0199 
0200     ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
0201     return le16_to_cpu(rx);
0202 }
0203 
0204 /**
0205  * ks8851_rdfifo_spi - read data from the receive fifo via SPI
0206  * @ks: The device state.
0207  * @buff: The buffer address
0208  * @len: The length of the data to read
0209  *
0210  * Issue an RXQ FIFO read command and read the @len amount of data from
0211  * the FIFO into the buffer specified by @buff.
0212  */
0213 static void ks8851_rdfifo_spi(struct ks8851_net *ks, u8 *buff, unsigned int len)
0214 {
0215     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0216     struct spi_transfer *xfer = kss->spi_xfer2;
0217     struct spi_message *msg = &kss->spi_msg2;
0218     u8 txb[1];
0219     int ret;
0220 
0221     netif_dbg(ks, rx_status, ks->netdev,
0222           "%s: %d@%p\n", __func__, len, buff);
0223 
0224     /* set the operation we're issuing */
0225     txb[0] = KS_SPIOP_RXFIFO;
0226 
0227     xfer->tx_buf = txb;
0228     xfer->rx_buf = NULL;
0229     xfer->len = 1;
0230 
0231     xfer++;
0232     xfer->rx_buf = buff;
0233     xfer->tx_buf = NULL;
0234     xfer->len = len;
0235 
0236     ret = spi_sync(kss->spidev, msg);
0237     if (ret < 0)
0238         netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
0239 }
0240 
0241 /**
0242  * ks8851_wrfifo_spi - write packet to TX FIFO via SPI
0243  * @ks: The device state.
0244  * @txp: The sk_buff to transmit.
0245  * @irq: IRQ on completion of the packet.
0246  *
0247  * Send the @txp to the chip. This means creating the relevant packet header
0248  * specifying the length of the packet and the other information the chip
0249  * needs, such as IRQ on completion. Send the header and the packet data to
0250  * the device.
0251  */
0252 static void ks8851_wrfifo_spi(struct ks8851_net *ks, struct sk_buff *txp,
0253                   bool irq)
0254 {
0255     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0256     struct spi_transfer *xfer = kss->spi_xfer2;
0257     struct spi_message *msg = &kss->spi_msg2;
0258     unsigned int fid = 0;
0259     int ret;
0260 
0261     netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
0262           __func__, txp, txp->len, txp->data, irq);
0263 
0264     fid = ks->fid++;
0265     fid &= TXFR_TXFID_MASK;
0266 
0267     if (irq)
0268         fid |= TXFR_TXIC;   /* irq on completion */
0269 
0270     /* start header at txb[1] to align txw entries */
0271     ks->txh.txb[1] = KS_SPIOP_TXFIFO;
0272     ks->txh.txw[1] = cpu_to_le16(fid);
0273     ks->txh.txw[2] = cpu_to_le16(txp->len);
0274 
0275     xfer->tx_buf = &ks->txh.txb[1];
0276     xfer->rx_buf = NULL;
0277     xfer->len = 5;
0278 
0279     xfer++;
0280     xfer->tx_buf = txp->data;
0281     xfer->rx_buf = NULL;
0282     xfer->len = ALIGN(txp->len, 4);
0283 
0284     ret = spi_sync(kss->spidev, msg);
0285     if (ret < 0)
0286         netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
0287 }
0288 
0289 /**
0290  * ks8851_rx_skb_spi - receive skbuff
0291  * @ks: The device state
0292  * @skb: The skbuff
0293  */
0294 static void ks8851_rx_skb_spi(struct ks8851_net *ks, struct sk_buff *skb)
0295 {
0296     netif_rx(skb);
0297 }
0298 
0299 /**
0300  * ks8851_tx_work - process tx packet(s)
0301  * @work: The work strucutre what was scheduled.
0302  *
0303  * This is called when a number of packets have been scheduled for
0304  * transmission and need to be sent to the device.
0305  */
0306 static void ks8851_tx_work(struct work_struct *work)
0307 {
0308     struct ks8851_net_spi *kss;
0309     struct ks8851_net *ks;
0310     unsigned long flags;
0311     struct sk_buff *txb;
0312     bool last;
0313 
0314     kss = container_of(work, struct ks8851_net_spi, tx_work);
0315     ks = &kss->ks8851;
0316     last = skb_queue_empty(&ks->txq);
0317 
0318     ks8851_lock_spi(ks, &flags);
0319 
0320     while (!last) {
0321         txb = skb_dequeue(&ks->txq);
0322         last = skb_queue_empty(&ks->txq);
0323 
0324         if (txb) {
0325             ks8851_wrreg16_spi(ks, KS_RXQCR,
0326                        ks->rc_rxqcr | RXQCR_SDA);
0327             ks8851_wrfifo_spi(ks, txb, last);
0328             ks8851_wrreg16_spi(ks, KS_RXQCR, ks->rc_rxqcr);
0329             ks8851_wrreg16_spi(ks, KS_TXQCR, TXQCR_METFE);
0330 
0331             ks8851_done_tx(ks, txb);
0332         }
0333     }
0334 
0335     ks8851_unlock_spi(ks, &flags);
0336 }
0337 
0338 /**
0339  * ks8851_flush_tx_work_spi - flush outstanding TX work
0340  * @ks: The device state
0341  */
0342 static void ks8851_flush_tx_work_spi(struct ks8851_net *ks)
0343 {
0344     struct ks8851_net_spi *kss = to_ks8851_spi(ks);
0345 
0346     flush_work(&kss->tx_work);
0347 }
0348 
0349 /**
0350  * calc_txlen - calculate size of message to send packet
0351  * @len: Length of data
0352  *
0353  * Returns the size of the TXFIFO message needed to send
0354  * this packet.
0355  */
0356 static unsigned int calc_txlen(unsigned int len)
0357 {
0358     return ALIGN(len + 4, 4);
0359 }
0360 
0361 /**
0362  * ks8851_start_xmit_spi - transmit packet using SPI
0363  * @skb: The buffer to transmit
0364  * @dev: The device used to transmit the packet.
0365  *
0366  * Called by the network layer to transmit the @skb. Queue the packet for
0367  * the device and schedule the necessary work to transmit the packet when
0368  * it is free.
0369  *
0370  * We do this to firstly avoid sleeping with the network device locked,
0371  * and secondly so we can round up more than one packet to transmit which
0372  * means we can try and avoid generating too many transmit done interrupts.
0373  */
0374 static netdev_tx_t ks8851_start_xmit_spi(struct sk_buff *skb,
0375                      struct net_device *dev)
0376 {
0377     unsigned int needed = calc_txlen(skb->len);
0378     struct ks8851_net *ks = netdev_priv(dev);
0379     netdev_tx_t ret = NETDEV_TX_OK;
0380     struct ks8851_net_spi *kss;
0381 
0382     kss = to_ks8851_spi(ks);
0383 
0384     netif_dbg(ks, tx_queued, ks->netdev,
0385           "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
0386 
0387     spin_lock(&ks->statelock);
0388 
0389     if (needed > ks->tx_space) {
0390         netif_stop_queue(dev);
0391         ret = NETDEV_TX_BUSY;
0392     } else {
0393         ks->tx_space -= needed;
0394         skb_queue_tail(&ks->txq, skb);
0395     }
0396 
0397     spin_unlock(&ks->statelock);
0398     schedule_work(&kss->tx_work);
0399 
0400     return ret;
0401 }
0402 
0403 static int ks8851_probe_spi(struct spi_device *spi)
0404 {
0405     struct device *dev = &spi->dev;
0406     struct ks8851_net_spi *kss;
0407     struct net_device *netdev;
0408     struct ks8851_net *ks;
0409 
0410     netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_spi));
0411     if (!netdev)
0412         return -ENOMEM;
0413 
0414     spi->bits_per_word = 8;
0415 
0416     ks = netdev_priv(netdev);
0417 
0418     ks->lock = ks8851_lock_spi;
0419     ks->unlock = ks8851_unlock_spi;
0420     ks->rdreg16 = ks8851_rdreg16_spi;
0421     ks->wrreg16 = ks8851_wrreg16_spi;
0422     ks->rdfifo = ks8851_rdfifo_spi;
0423     ks->wrfifo = ks8851_wrfifo_spi;
0424     ks->start_xmit = ks8851_start_xmit_spi;
0425     ks->rx_skb = ks8851_rx_skb_spi;
0426     ks->flush_tx_work = ks8851_flush_tx_work_spi;
0427 
0428 #define STD_IRQ (IRQ_LCI |  /* Link Change */   \
0429          IRQ_TXI |  /* TX done */       \
0430          IRQ_RXI |  /* RX done */       \
0431          IRQ_SPIBEI |   /* SPI bus error */ \
0432          IRQ_TXPSI |    /* TX process stop */   \
0433          IRQ_RXPSI) /* RX process stop */
0434     ks->rc_ier = STD_IRQ;
0435 
0436     kss = to_ks8851_spi(ks);
0437 
0438     kss->spidev = spi;
0439     mutex_init(&kss->lock);
0440     INIT_WORK(&kss->tx_work, ks8851_tx_work);
0441 
0442     /* initialise pre-made spi transfer messages */
0443     spi_message_init(&kss->spi_msg1);
0444     spi_message_add_tail(&kss->spi_xfer1, &kss->spi_msg1);
0445 
0446     spi_message_init(&kss->spi_msg2);
0447     spi_message_add_tail(&kss->spi_xfer2[0], &kss->spi_msg2);
0448     spi_message_add_tail(&kss->spi_xfer2[1], &kss->spi_msg2);
0449 
0450     netdev->irq = spi->irq;
0451 
0452     return ks8851_probe_common(netdev, dev, msg_enable);
0453 }
0454 
0455 static void ks8851_remove_spi(struct spi_device *spi)
0456 {
0457     ks8851_remove_common(&spi->dev);
0458 }
0459 
0460 static const struct of_device_id ks8851_match_table[] = {
0461     { .compatible = "micrel,ks8851" },
0462     { }
0463 };
0464 MODULE_DEVICE_TABLE(of, ks8851_match_table);
0465 
0466 static struct spi_driver ks8851_driver = {
0467     .driver = {
0468         .name = "ks8851",
0469         .of_match_table = ks8851_match_table,
0470         .pm = &ks8851_pm_ops,
0471     },
0472     .probe = ks8851_probe_spi,
0473     .remove = ks8851_remove_spi,
0474 };
0475 module_spi_driver(ks8851_driver);
0476 
0477 MODULE_DESCRIPTION("KS8851 Network driver");
0478 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
0479 MODULE_LICENSE("GPL");
0480 
0481 module_param_named(message, msg_enable, int, 0);
0482 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
0483 MODULE_ALIAS("spi:ks8851");