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/iopoll.h>
0018 #include <linux/mii.h>
0019 
0020 #include <linux/platform_device.h>
0021 #include <linux/of_net.h>
0022 
0023 #include "ks8851.h"
0024 
0025 static int msg_enable;
0026 
0027 #define BE3             0x8000      /* Byte Enable 3 */
0028 #define BE2             0x4000      /* Byte Enable 2 */
0029 #define BE1             0x2000      /* Byte Enable 1 */
0030 #define BE0             0x1000      /* Byte Enable 0 */
0031 
0032 /**
0033  * struct ks8851_net_par - KS8851 Parallel driver private data
0034  * @ks8851: KS8851 driver common private data
0035  * @lock: Lock to ensure that the device is not accessed when busy.
0036  * @hw_addr : start address of data register.
0037  * @hw_addr_cmd : start address of command register.
0038  * @cmd_reg_cache   : command register cached.
0039  *
0040  * The @lock ensures that the chip is protected when certain operations are
0041  * in progress. When the read or write packet transfer is in progress, most
0042  * of the chip registers are not accessible until the transfer is finished
0043  * and the DMA has been de-asserted.
0044  */
0045 struct ks8851_net_par {
0046     struct ks8851_net   ks8851;
0047     spinlock_t      lock;
0048     void __iomem        *hw_addr;
0049     void __iomem        *hw_addr_cmd;
0050     u16         cmd_reg_cache;
0051 };
0052 
0053 #define to_ks8851_par(ks) container_of((ks), struct ks8851_net_par, ks8851)
0054 
0055 /**
0056  * ks8851_lock_par - register access lock
0057  * @ks: The chip state
0058  * @flags: Spinlock flags
0059  *
0060  * Claim chip register access lock
0061  */
0062 static void ks8851_lock_par(struct ks8851_net *ks, unsigned long *flags)
0063 {
0064     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0065 
0066     spin_lock_irqsave(&ksp->lock, *flags);
0067 }
0068 
0069 /**
0070  * ks8851_unlock_par - register access unlock
0071  * @ks: The chip state
0072  * @flags: Spinlock flags
0073  *
0074  * Release chip register access lock
0075  */
0076 static void ks8851_unlock_par(struct ks8851_net *ks, unsigned long *flags)
0077 {
0078     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0079 
0080     spin_unlock_irqrestore(&ksp->lock, *flags);
0081 }
0082 
0083 /**
0084  * ks_check_endian - Check whether endianness of the bus is correct
0085  * @ks    : The chip information
0086  *
0087  * The KS8851-16MLL EESK pin allows selecting the endianness of the 16bit
0088  * bus. To maintain optimum performance, the bus endianness should be set
0089  * such that it matches the endianness of the CPU.
0090  */
0091 static int ks_check_endian(struct ks8851_net *ks)
0092 {
0093     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0094     u16 cider;
0095 
0096     /*
0097      * Read CIDER register first, however read it the "wrong" way around.
0098      * If the endian strap on the KS8851-16MLL in incorrect and the chip
0099      * is operating in different endianness than the CPU, then the meaning
0100      * of BE[3:0] byte-enable bits is also swapped such that:
0101      *    BE[3,2,1,0] becomes BE[1,0,3,2]
0102      *
0103      * Luckily for us, the byte-enable bits are the top four MSbits of
0104      * the address register and the CIDER register is at offset 0xc0.
0105      * Hence, by reading address 0xc0c0, which is not impacted by endian
0106      * swapping, we assert either BE[3:2] or BE[1:0] while reading the
0107      * CIDER register.
0108      *
0109      * If the bus configuration is correct, reading 0xc0c0 asserts
0110      * BE[3:2] and this read returns 0x0000, because to read register
0111      * with bottom two LSbits of address set to 0, BE[1:0] must be
0112      * asserted.
0113      *
0114      * If the bus configuration is NOT correct, reading 0xc0c0 asserts
0115      * BE[1:0] and this read returns non-zero 0x8872 value.
0116      */
0117     iowrite16(BE3 | BE2 | KS_CIDER, ksp->hw_addr_cmd);
0118     cider = ioread16(ksp->hw_addr);
0119     if (!cider)
0120         return 0;
0121 
0122     netdev_err(ks->netdev, "incorrect EESK endian strap setting\n");
0123 
0124     return -EINVAL;
0125 }
0126 
0127 /**
0128  * ks8851_wrreg16_par - write 16bit register value to chip
0129  * @ks: The chip state
0130  * @reg: The register address
0131  * @val: The value to write
0132  *
0133  * Issue a write to put the value @val into the register specified in @reg.
0134  */
0135 static void ks8851_wrreg16_par(struct ks8851_net *ks, unsigned int reg,
0136                    unsigned int val)
0137 {
0138     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0139 
0140     ksp->cmd_reg_cache = (u16)reg | ((BE1 | BE0) << (reg & 0x02));
0141     iowrite16(ksp->cmd_reg_cache, ksp->hw_addr_cmd);
0142     iowrite16(val, ksp->hw_addr);
0143 }
0144 
0145 /**
0146  * ks8851_rdreg16_par - read 16 bit register from chip
0147  * @ks: The chip information
0148  * @reg: The register address
0149  *
0150  * Read a 16bit register from the chip, returning the result
0151  */
0152 static unsigned int ks8851_rdreg16_par(struct ks8851_net *ks, unsigned int reg)
0153 {
0154     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0155 
0156     ksp->cmd_reg_cache = (u16)reg | ((BE1 | BE0) << (reg & 0x02));
0157     iowrite16(ksp->cmd_reg_cache, ksp->hw_addr_cmd);
0158     return ioread16(ksp->hw_addr);
0159 }
0160 
0161 /**
0162  * ks8851_rdfifo_par - read data from the receive fifo
0163  * @ks: The device state.
0164  * @buff: The buffer address
0165  * @len: The length of the data to read
0166  *
0167  * Issue an RXQ FIFO read command and read the @len amount of data from
0168  * the FIFO into the buffer specified by @buff.
0169  */
0170 static void ks8851_rdfifo_par(struct ks8851_net *ks, u8 *buff, unsigned int len)
0171 {
0172     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0173 
0174     netif_dbg(ks, rx_status, ks->netdev,
0175           "%s: %d@%p\n", __func__, len, buff);
0176 
0177     ioread16_rep(ksp->hw_addr, (u16 *)buff + 1, len / 2);
0178 }
0179 
0180 /**
0181  * ks8851_wrfifo_par - write packet to TX FIFO
0182  * @ks: The device state.
0183  * @txp: The sk_buff to transmit.
0184  * @irq: IRQ on completion of the packet.
0185  *
0186  * Send the @txp to the chip. This means creating the relevant packet header
0187  * specifying the length of the packet and the other information the chip
0188  * needs, such as IRQ on completion. Send the header and the packet data to
0189  * the device.
0190  */
0191 static void ks8851_wrfifo_par(struct ks8851_net *ks, struct sk_buff *txp,
0192                   bool irq)
0193 {
0194     struct ks8851_net_par *ksp = to_ks8851_par(ks);
0195     unsigned int len = ALIGN(txp->len, 4);
0196     unsigned int fid = 0;
0197 
0198     netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
0199           __func__, txp, txp->len, txp->data, irq);
0200 
0201     fid = ks->fid++;
0202     fid &= TXFR_TXFID_MASK;
0203 
0204     if (irq)
0205         fid |= TXFR_TXIC;   /* irq on completion */
0206 
0207     iowrite16(fid, ksp->hw_addr);
0208     iowrite16(txp->len, ksp->hw_addr);
0209 
0210     iowrite16_rep(ksp->hw_addr, txp->data, len / 2);
0211 }
0212 
0213 /**
0214  * ks8851_rx_skb_par - receive skbuff
0215  * @ks: The device state.
0216  * @skb: The skbuff
0217  */
0218 static void ks8851_rx_skb_par(struct ks8851_net *ks, struct sk_buff *skb)
0219 {
0220     netif_rx(skb);
0221 }
0222 
0223 static unsigned int ks8851_rdreg16_par_txqcr(struct ks8851_net *ks)
0224 {
0225     return ks8851_rdreg16_par(ks, KS_TXQCR);
0226 }
0227 
0228 /**
0229  * ks8851_start_xmit_par - transmit packet
0230  * @skb: The buffer to transmit
0231  * @dev: The device used to transmit the packet.
0232  *
0233  * Called by the network layer to transmit the @skb. Queue the packet for
0234  * the device and schedule the necessary work to transmit the packet when
0235  * it is free.
0236  *
0237  * We do this to firstly avoid sleeping with the network device locked,
0238  * and secondly so we can round up more than one packet to transmit which
0239  * means we can try and avoid generating too many transmit done interrupts.
0240  */
0241 static netdev_tx_t ks8851_start_xmit_par(struct sk_buff *skb,
0242                      struct net_device *dev)
0243 {
0244     struct ks8851_net *ks = netdev_priv(dev);
0245     netdev_tx_t ret = NETDEV_TX_OK;
0246     unsigned long flags;
0247     unsigned int txqcr;
0248     u16 txmir;
0249     int err;
0250 
0251     netif_dbg(ks, tx_queued, ks->netdev,
0252           "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
0253 
0254     ks8851_lock_par(ks, &flags);
0255 
0256     txmir = ks8851_rdreg16_par(ks, KS_TXMIR) & 0x1fff;
0257 
0258     if (likely(txmir >= skb->len + 12)) {
0259         ks8851_wrreg16_par(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
0260         ks8851_wrfifo_par(ks, skb, false);
0261         ks8851_wrreg16_par(ks, KS_RXQCR, ks->rc_rxqcr);
0262         ks8851_wrreg16_par(ks, KS_TXQCR, TXQCR_METFE);
0263 
0264         err = readx_poll_timeout_atomic(ks8851_rdreg16_par_txqcr, ks,
0265                         txqcr, !(txqcr & TXQCR_METFE),
0266                         5, 1000000);
0267         if (err)
0268             ret = NETDEV_TX_BUSY;
0269 
0270         ks8851_done_tx(ks, skb);
0271     } else {
0272         ret = NETDEV_TX_BUSY;
0273     }
0274 
0275     ks8851_unlock_par(ks, &flags);
0276 
0277     return ret;
0278 }
0279 
0280 static int ks8851_probe_par(struct platform_device *pdev)
0281 {
0282     struct device *dev = &pdev->dev;
0283     struct ks8851_net_par *ksp;
0284     struct net_device *netdev;
0285     struct ks8851_net *ks;
0286     int ret;
0287 
0288     netdev = devm_alloc_etherdev(dev, sizeof(struct ks8851_net_par));
0289     if (!netdev)
0290         return -ENOMEM;
0291 
0292     ks = netdev_priv(netdev);
0293 
0294     ks->lock = ks8851_lock_par;
0295     ks->unlock = ks8851_unlock_par;
0296     ks->rdreg16 = ks8851_rdreg16_par;
0297     ks->wrreg16 = ks8851_wrreg16_par;
0298     ks->rdfifo = ks8851_rdfifo_par;
0299     ks->wrfifo = ks8851_wrfifo_par;
0300     ks->start_xmit = ks8851_start_xmit_par;
0301     ks->rx_skb = ks8851_rx_skb_par;
0302 
0303 #define STD_IRQ (IRQ_LCI |  /* Link Change */   \
0304          IRQ_RXI |  /* RX done */       \
0305          IRQ_RXPSI) /* RX process stop */
0306     ks->rc_ier = STD_IRQ;
0307 
0308     ksp = to_ks8851_par(ks);
0309     spin_lock_init(&ksp->lock);
0310 
0311     ksp->hw_addr = devm_platform_ioremap_resource(pdev, 0);
0312     if (IS_ERR(ksp->hw_addr))
0313         return PTR_ERR(ksp->hw_addr);
0314 
0315     ksp->hw_addr_cmd = devm_platform_ioremap_resource(pdev, 1);
0316     if (IS_ERR(ksp->hw_addr_cmd))
0317         return PTR_ERR(ksp->hw_addr_cmd);
0318 
0319     ret = ks_check_endian(ks);
0320     if (ret)
0321         return ret;
0322 
0323     netdev->irq = platform_get_irq(pdev, 0);
0324     if (netdev->irq < 0)
0325         return netdev->irq;
0326 
0327     return ks8851_probe_common(netdev, dev, msg_enable);
0328 }
0329 
0330 static int ks8851_remove_par(struct platform_device *pdev)
0331 {
0332     ks8851_remove_common(&pdev->dev);
0333 
0334     return 0;
0335 }
0336 
0337 static const struct of_device_id ks8851_match_table[] = {
0338     { .compatible = "micrel,ks8851-mll" },
0339     { }
0340 };
0341 MODULE_DEVICE_TABLE(of, ks8851_match_table);
0342 
0343 static struct platform_driver ks8851_driver = {
0344     .driver = {
0345         .name = "ks8851",
0346         .of_match_table = ks8851_match_table,
0347         .pm = &ks8851_pm_ops,
0348     },
0349     .probe = ks8851_probe_par,
0350     .remove = ks8851_remove_par,
0351 };
0352 module_platform_driver(ks8851_driver);
0353 
0354 MODULE_DESCRIPTION("KS8851 Network driver");
0355 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
0356 MODULE_LICENSE("GPL");
0357 
0358 module_param_named(message, msg_enable, int, 0);
0359 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");