Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2008 Per Dalen <per.dalen@cnw.se>
0004  *
0005  * Parts of this software are based on (derived) the following:
0006  *
0007  * - Kvaser linux driver, version 4.72 BETA
0008  *   Copyright (C) 2002-2007 KVASER AB
0009  *
0010  * - Lincan driver, version 0.3.3, OCERA project
0011  *   Copyright (C) 2004 Pavel Pisa
0012  *   Copyright (C) 2001 Arnaud Westenberg
0013  *
0014  * - Socketcan SJA1000 drivers
0015  *   Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
0016  *   Copyright (c) 2002-2007 Volkswagen Group Electronic Research
0017  *   Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
0018  *   38106 Braunschweig, GERMANY
0019  */
0020 
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/netdevice.h>
0025 #include <linux/delay.h>
0026 #include <linux/pci.h>
0027 #include <linux/can/dev.h>
0028 #include <linux/io.h>
0029 
0030 #include "sja1000.h"
0031 
0032 #define DRV_NAME  "kvaser_pci"
0033 
0034 MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
0035 MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
0036 MODULE_LICENSE("GPL v2");
0037 
0038 #define MAX_NO_OF_CHANNELS        4 /* max no of channels on a single card */
0039 
0040 struct kvaser_pci {
0041     int channel;
0042     struct pci_dev *pci_dev;
0043     struct net_device *slave_dev[MAX_NO_OF_CHANNELS-1];
0044     void __iomem *conf_addr;
0045     void __iomem *res_addr;
0046     int no_channels;
0047     u8 xilinx_ver;
0048 };
0049 
0050 #define KVASER_PCI_CAN_CLOCK      (16000000 / 2)
0051 
0052 /*
0053  * The board configuration is probably following:
0054  * RX1 is connected to ground.
0055  * TX1 is not connected.
0056  * CLKO is not connected.
0057  * Setting the OCR register to 0xDA is a good idea.
0058  * This means  normal output mode , push-pull and the correct polarity.
0059  */
0060 #define KVASER_PCI_OCR            (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
0061 
0062 /*
0063  * In the CDR register, you should set CBP to 1.
0064  * You will probably also want to set the clock divider value to 0
0065  * (meaning divide-by-2), the Pelican bit, and the clock-off bit
0066  * (you will have no need for CLKOUT anyway).
0067  */
0068 #define KVASER_PCI_CDR            (CDR_CBP | CDR_CLKOUT_MASK)
0069 
0070 /*
0071  * These register values are valid for revision 14 of the Xilinx logic.
0072  */
0073 #define XILINX_VERINT             7   /* Lower nibble simulate interrupts,
0074                      high nibble version number. */
0075 
0076 #define XILINX_PRESUMED_VERSION   14
0077 
0078 /*
0079  * Important S5920 registers
0080  */
0081 #define S5920_INTCSR              0x38
0082 #define S5920_PTCR                0x60
0083 #define INTCSR_ADDON_INTENABLE_M  0x2000
0084 
0085 
0086 #define KVASER_PCI_PORT_BYTES     0x20
0087 
0088 #define PCI_CONFIG_PORT_SIZE      0x80      /* size of the config io-memory */
0089 #define PCI_PORT_SIZE             0x80      /* size of a channel io-memory */
0090 #define PCI_PORT_XILINX_SIZE      0x08      /* size of a xilinx io-memory */
0091 
0092 #define KVASER_PCI_VENDOR_ID1     0x10e8    /* the PCI device and vendor IDs */
0093 #define KVASER_PCI_DEVICE_ID1     0x8406
0094 
0095 #define KVASER_PCI_VENDOR_ID2     0x1a07    /* the PCI device and vendor IDs */
0096 #define KVASER_PCI_DEVICE_ID2     0x0008
0097 
0098 static const struct pci_device_id kvaser_pci_tbl[] = {
0099     {KVASER_PCI_VENDOR_ID1, KVASER_PCI_DEVICE_ID1, PCI_ANY_ID, PCI_ANY_ID,},
0100     {KVASER_PCI_VENDOR_ID2, KVASER_PCI_DEVICE_ID2, PCI_ANY_ID, PCI_ANY_ID,},
0101     { 0,}
0102 };
0103 
0104 MODULE_DEVICE_TABLE(pci, kvaser_pci_tbl);
0105 
0106 static u8 kvaser_pci_read_reg(const struct sja1000_priv *priv, int port)
0107 {
0108     return ioread8(priv->reg_base + port);
0109 }
0110 
0111 static void kvaser_pci_write_reg(const struct sja1000_priv *priv,
0112                  int port, u8 val)
0113 {
0114     iowrite8(val, priv->reg_base + port);
0115 }
0116 
0117 static void kvaser_pci_disable_irq(struct net_device *dev)
0118 {
0119     struct sja1000_priv *priv = netdev_priv(dev);
0120     struct kvaser_pci *board = priv->priv;
0121     u32 intcsr;
0122 
0123     /* Disable interrupts from card */
0124     intcsr = ioread32(board->conf_addr + S5920_INTCSR);
0125     intcsr &= ~INTCSR_ADDON_INTENABLE_M;
0126     iowrite32(intcsr, board->conf_addr + S5920_INTCSR);
0127 }
0128 
0129 static void kvaser_pci_enable_irq(struct net_device *dev)
0130 {
0131     struct sja1000_priv *priv = netdev_priv(dev);
0132     struct kvaser_pci *board = priv->priv;
0133     u32 tmp_en_io;
0134 
0135     /* Enable interrupts from card */
0136     tmp_en_io = ioread32(board->conf_addr + S5920_INTCSR);
0137     tmp_en_io |= INTCSR_ADDON_INTENABLE_M;
0138     iowrite32(tmp_en_io, board->conf_addr + S5920_INTCSR);
0139 }
0140 
0141 static int number_of_sja1000_chip(void __iomem *base_addr)
0142 {
0143     u8 status;
0144     int i;
0145 
0146     for (i = 0; i < MAX_NO_OF_CHANNELS; i++) {
0147         /* reset chip */
0148         iowrite8(MOD_RM, base_addr +
0149              (i * KVASER_PCI_PORT_BYTES) + SJA1000_MOD);
0150         status = ioread8(base_addr +
0151                  (i * KVASER_PCI_PORT_BYTES) + SJA1000_MOD);
0152         /* check reset bit */
0153         if (!(status & MOD_RM))
0154             break;
0155     }
0156 
0157     return i;
0158 }
0159 
0160 static void kvaser_pci_del_chan(struct net_device *dev)
0161 {
0162     struct sja1000_priv *priv;
0163     struct kvaser_pci *board;
0164     int i;
0165 
0166     if (!dev)
0167         return;
0168     priv = netdev_priv(dev);
0169     board = priv->priv;
0170     if (!board)
0171         return;
0172 
0173     dev_info(&board->pci_dev->dev, "Removing device %s\n",
0174          dev->name);
0175 
0176     /* Disable PCI interrupts */
0177     kvaser_pci_disable_irq(dev);
0178 
0179     for (i = 0; i < board->no_channels - 1; i++) {
0180         if (board->slave_dev[i]) {
0181             dev_info(&board->pci_dev->dev, "Removing device %s\n",
0182                  board->slave_dev[i]->name);
0183             unregister_sja1000dev(board->slave_dev[i]);
0184             free_sja1000dev(board->slave_dev[i]);
0185         }
0186     }
0187     unregister_sja1000dev(dev);
0188 
0189     pci_iounmap(board->pci_dev, priv->reg_base);
0190     pci_iounmap(board->pci_dev, board->conf_addr);
0191     pci_iounmap(board->pci_dev, board->res_addr);
0192 
0193     free_sja1000dev(dev);
0194 }
0195 
0196 static int kvaser_pci_add_chan(struct pci_dev *pdev, int channel,
0197                    struct net_device **master_dev,
0198                    void __iomem *conf_addr,
0199                    void __iomem *res_addr,
0200                    void __iomem *base_addr)
0201 {
0202     struct net_device *dev;
0203     struct sja1000_priv *priv;
0204     struct kvaser_pci *board;
0205     int err;
0206 
0207     dev = alloc_sja1000dev(sizeof(struct kvaser_pci));
0208     if (dev == NULL)
0209         return -ENOMEM;
0210 
0211     priv = netdev_priv(dev);
0212     board = priv->priv;
0213 
0214     board->pci_dev = pdev;
0215     board->channel = channel;
0216 
0217     /* S5920 */
0218     board->conf_addr = conf_addr;
0219 
0220     /* XILINX board wide address */
0221     board->res_addr = res_addr;
0222 
0223     if (channel == 0) {
0224         board->xilinx_ver =
0225             ioread8(board->res_addr + XILINX_VERINT) >> 4;
0226 
0227         /* Assert PTADR# - we're in passive mode so the other bits are
0228            not important */
0229         iowrite32(0x80808080UL, board->conf_addr + S5920_PTCR);
0230 
0231         /* Enable interrupts from card */
0232         kvaser_pci_enable_irq(dev);
0233     } else {
0234         struct sja1000_priv *master_priv = netdev_priv(*master_dev);
0235         struct kvaser_pci *master_board = master_priv->priv;
0236         master_board->slave_dev[channel - 1] = dev;
0237         master_board->no_channels = channel + 1;
0238         board->xilinx_ver = master_board->xilinx_ver;
0239     }
0240 
0241     priv->reg_base = base_addr + channel * KVASER_PCI_PORT_BYTES;
0242 
0243     priv->read_reg = kvaser_pci_read_reg;
0244     priv->write_reg = kvaser_pci_write_reg;
0245 
0246     priv->can.clock.freq = KVASER_PCI_CAN_CLOCK;
0247 
0248     priv->ocr = KVASER_PCI_OCR;
0249     priv->cdr = KVASER_PCI_CDR;
0250 
0251     priv->irq_flags = IRQF_SHARED;
0252     dev->irq = pdev->irq;
0253 
0254     dev_info(&pdev->dev, "reg_base=%p conf_addr=%p irq=%d\n",
0255          priv->reg_base, board->conf_addr, dev->irq);
0256 
0257     SET_NETDEV_DEV(dev, &pdev->dev);
0258     dev->dev_id = channel;
0259 
0260     /* Register SJA1000 device */
0261     err = register_sja1000dev(dev);
0262     if (err) {
0263         dev_err(&pdev->dev, "Registering device failed (err=%d)\n",
0264             err);
0265         goto failure;
0266     }
0267 
0268     if (channel == 0)
0269         *master_dev = dev;
0270 
0271     return 0;
0272 
0273 failure:
0274     kvaser_pci_del_chan(dev);
0275     return err;
0276 }
0277 
0278 static int kvaser_pci_init_one(struct pci_dev *pdev,
0279                    const struct pci_device_id *ent)
0280 {
0281     int err;
0282     struct net_device *master_dev = NULL;
0283     struct sja1000_priv *priv;
0284     struct kvaser_pci *board;
0285     int no_channels;
0286     void __iomem *base_addr = NULL;
0287     void __iomem *conf_addr = NULL;
0288     void __iomem *res_addr = NULL;
0289     int i;
0290 
0291     dev_info(&pdev->dev, "initializing device %04x:%04x\n",
0292          pdev->vendor, pdev->device);
0293 
0294     err = pci_enable_device(pdev);
0295     if (err)
0296         goto failure;
0297 
0298     err = pci_request_regions(pdev, DRV_NAME);
0299     if (err)
0300         goto failure_release_pci;
0301 
0302     /* S5920 */
0303     conf_addr = pci_iomap(pdev, 0, PCI_CONFIG_PORT_SIZE);
0304     if (conf_addr == NULL) {
0305         err = -ENODEV;
0306         goto failure_release_regions;
0307     }
0308 
0309     /* XILINX board wide address */
0310     res_addr = pci_iomap(pdev, 2, PCI_PORT_XILINX_SIZE);
0311     if (res_addr == NULL) {
0312         err = -ENOMEM;
0313         goto failure_iounmap;
0314     }
0315 
0316     base_addr = pci_iomap(pdev, 1, PCI_PORT_SIZE);
0317     if (base_addr == NULL) {
0318         err = -ENOMEM;
0319         goto failure_iounmap;
0320     }
0321 
0322     no_channels = number_of_sja1000_chip(base_addr);
0323     if (no_channels == 0) {
0324         err = -ENOMEM;
0325         goto failure_iounmap;
0326     }
0327 
0328     for (i = 0; i < no_channels; i++) {
0329         err = kvaser_pci_add_chan(pdev, i, &master_dev,
0330                       conf_addr, res_addr,
0331                       base_addr);
0332         if (err)
0333             goto failure_cleanup;
0334     }
0335 
0336     priv = netdev_priv(master_dev);
0337     board = priv->priv;
0338 
0339     dev_info(&pdev->dev, "xilinx version=%d number of channels=%d\n",
0340          board->xilinx_ver, board->no_channels);
0341 
0342     pci_set_drvdata(pdev, master_dev);
0343     return 0;
0344 
0345 failure_cleanup:
0346     kvaser_pci_del_chan(master_dev);
0347 
0348 failure_iounmap:
0349     if (conf_addr != NULL)
0350         pci_iounmap(pdev, conf_addr);
0351     if (res_addr != NULL)
0352         pci_iounmap(pdev, res_addr);
0353     if (base_addr != NULL)
0354         pci_iounmap(pdev, base_addr);
0355 
0356 failure_release_regions:
0357     pci_release_regions(pdev);
0358 
0359 failure_release_pci:
0360     pci_disable_device(pdev);
0361 
0362 failure:
0363     return err;
0364 
0365 }
0366 
0367 static void kvaser_pci_remove_one(struct pci_dev *pdev)
0368 {
0369     struct net_device *dev = pci_get_drvdata(pdev);
0370 
0371     kvaser_pci_del_chan(dev);
0372 
0373     pci_release_regions(pdev);
0374     pci_disable_device(pdev);
0375 }
0376 
0377 static struct pci_driver kvaser_pci_driver = {
0378     .name = DRV_NAME,
0379     .id_table = kvaser_pci_tbl,
0380     .probe = kvaser_pci_init_one,
0381     .remove = kvaser_pci_remove_one,
0382 };
0383 
0384 module_pci_driver(kvaser_pci_driver);