1 /* 2 * PCI bus driver for Bosch C_CAN/D_CAN controller 3 * 4 * Copyright (C) 2012 Federico Vaga <federico.vaga@gmail.com> 5 * 6 * Borrowed from c_can_platform.c 7 * 8 * This file is licensed under the terms of the GNU General Public 9 * License version 2. This program is licensed "as is" without any 10 * warranty of any kind, whether express or implied. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/netdevice.h> 16 #include <linux/pci.h> 17 18 #include <linux/can/dev.h> 19 20 #include "c_can.h" 21 22 #define PCI_DEVICE_ID_PCH_CAN 0x8818 23 #define PCH_PCI_SOFT_RESET 0x01fc 24 25 enum c_can_pci_reg_align { 26 C_CAN_REG_ALIGN_16, 27 C_CAN_REG_ALIGN_32, 28 C_CAN_REG_32, 29 }; 30 31 struct c_can_pci_data { 32 /* Specify if is C_CAN or D_CAN */ 33 enum c_can_dev_id type; 34 /* Set the register alignment in the memory */ 35 enum c_can_pci_reg_align reg_align; 36 /* Set the frequency */ 37 unsigned int freq; 38 /* PCI bar number */ 39 int bar; 40 /* Callback for reset */ 41 void (*init)(const struct c_can_priv *priv, bool enable); 42 }; 43 44 /* 16-bit c_can registers can be arranged differently in the memory 45 * architecture of different implementations. For example: 16-bit 46 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc. 47 * Handle the same by providing a common read/write interface. 48 */ 49 static u16 c_can_pci_read_reg_aligned_to_16bit(const struct c_can_priv *priv, 50 enum reg index) 51 { 52 return readw(priv->base + priv->regs[index]); 53 } 54 55 static void c_can_pci_write_reg_aligned_to_16bit(const struct c_can_priv *priv, 56 enum reg index, u16 val) 57 { 58 writew(val, priv->base + priv->regs[index]); 59 } 60 61 static u16 c_can_pci_read_reg_aligned_to_32bit(const struct c_can_priv *priv, 62 enum reg index) 63 { 64 return readw(priv->base + 2 * priv->regs[index]); 65 } 66 67 static void c_can_pci_write_reg_aligned_to_32bit(const struct c_can_priv *priv, 68 enum reg index, u16 val) 69 { 70 writew(val, priv->base + 2 * priv->regs[index]); 71 } 72 73 static u16 c_can_pci_read_reg_32bit(const struct c_can_priv *priv, 74 enum reg index) 75 { 76 return (u16)ioread32(priv->base + 2 * priv->regs[index]); 77 } 78 79 static void c_can_pci_write_reg_32bit(const struct c_can_priv *priv, 80 enum reg index, u16 val) 81 { 82 iowrite32((u32)val, priv->base + 2 * priv->regs[index]); 83 } 84 85 static u32 c_can_pci_read_reg32(const struct c_can_priv *priv, enum reg index) 86 { 87 u32 val; 88 89 val = priv->read_reg(priv, index); 90 val |= ((u32)priv->read_reg(priv, index + 1)) << 16; 91 92 return val; 93 } 94 95 static void c_can_pci_write_reg32(const struct c_can_priv *priv, enum reg index, 96 u32 val) 97 { 98 priv->write_reg(priv, index + 1, val >> 16); 99 priv->write_reg(priv, index, val); 100 } 101 102 static void c_can_pci_reset_pch(const struct c_can_priv *priv, bool enable) 103 { 104 if (enable) { 105 u32 __iomem *addr = priv->base + PCH_PCI_SOFT_RESET; 106 107 /* write to sw reset register */ 108 iowrite32(1, addr); 109 iowrite32(0, addr); 110 } 111 } 112 113 static int c_can_pci_probe(struct pci_dev *pdev, 114 const struct pci_device_id *ent) 115 { 116 struct c_can_pci_data *c_can_pci_data = (void *)ent->driver_data; 117 struct c_can_priv *priv; 118 struct net_device *dev; 119 void __iomem *addr; 120 int ret; 121 122 ret = pci_enable_device(pdev); 123 if (ret) { 124 dev_err(&pdev->dev, "pci_enable_device FAILED\n"); 125 goto out; 126 } 127 128 ret = pci_request_regions(pdev, KBUILD_MODNAME); 129 if (ret) { 130 dev_err(&pdev->dev, "pci_request_regions FAILED\n"); 131 goto out_disable_device; 132 } 133 134 ret = pci_enable_msi(pdev); 135 if (!ret) { 136 dev_info(&pdev->dev, "MSI enabled\n"); 137 pci_set_master(pdev); 138 } 139 140 addr = pci_iomap(pdev, c_can_pci_data->bar, 141 pci_resource_len(pdev, c_can_pci_data->bar)); 142 if (!addr) { 143 dev_err(&pdev->dev, 144 "device has no PCI memory resources, failing adapter\n"); 145 ret = -ENOMEM; 146 goto out_release_regions; 147 } 148 149 /* allocate the c_can device */ 150 dev = alloc_c_can_dev(); 151 if (!dev) { 152 ret = -ENOMEM; 153 goto out_iounmap; 154 } 155 156 priv = netdev_priv(dev); 157 pci_set_drvdata(pdev, dev); 158 SET_NETDEV_DEV(dev, &pdev->dev); 159 160 dev->irq = pdev->irq; 161 priv->base = addr; 162 priv->device = &pdev->dev; 163 164 if (!c_can_pci_data->freq) { 165 dev_err(&pdev->dev, "no clock frequency defined\n"); 166 ret = -ENODEV; 167 goto out_free_c_can; 168 } else { 169 priv->can.clock.freq = c_can_pci_data->freq; 170 } 171 172 /* Configure CAN type */ 173 switch (c_can_pci_data->type) { 174 case BOSCH_C_CAN: 175 priv->regs = reg_map_c_can; 176 break; 177 case BOSCH_D_CAN: 178 priv->regs = reg_map_d_can; 179 break; 180 default: 181 ret = -EINVAL; 182 goto out_free_c_can; 183 } 184 185 priv->type = c_can_pci_data->type; 186 187 /* Configure access to registers */ 188 switch (c_can_pci_data->reg_align) { 189 case C_CAN_REG_ALIGN_32: 190 priv->read_reg = c_can_pci_read_reg_aligned_to_32bit; 191 priv->write_reg = c_can_pci_write_reg_aligned_to_32bit; 192 break; 193 case C_CAN_REG_ALIGN_16: 194 priv->read_reg = c_can_pci_read_reg_aligned_to_16bit; 195 priv->write_reg = c_can_pci_write_reg_aligned_to_16bit; 196 break; 197 case C_CAN_REG_32: 198 priv->read_reg = c_can_pci_read_reg_32bit; 199 priv->write_reg = c_can_pci_write_reg_32bit; 200 break; 201 default: 202 ret = -EINVAL; 203 goto out_free_c_can; 204 } 205 priv->read_reg32 = c_can_pci_read_reg32; 206 priv->write_reg32 = c_can_pci_write_reg32; 207 208 priv->raminit = c_can_pci_data->init; 209 210 ret = register_c_can_dev(dev); 211 if (ret) { 212 dev_err(&pdev->dev, "registering %s failed (err=%d)\n", 213 KBUILD_MODNAME, ret); 214 goto out_free_c_can; 215 } 216 217 dev_dbg(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n", 218 KBUILD_MODNAME, priv->regs, dev->irq); 219 220 return 0; 221 222 out_free_c_can: 223 free_c_can_dev(dev); 224 out_iounmap: 225 pci_iounmap(pdev, addr); 226 out_release_regions: 227 pci_disable_msi(pdev); 228 pci_clear_master(pdev); 229 pci_release_regions(pdev); 230 out_disable_device: 231 pci_disable_device(pdev); 232 out: 233 return ret; 234 } 235 236 static void c_can_pci_remove(struct pci_dev *pdev) 237 { 238 struct net_device *dev = pci_get_drvdata(pdev); 239 struct c_can_priv *priv = netdev_priv(dev); 240 void __iomem *addr = priv->base; 241 242 unregister_c_can_dev(dev); 243 244 free_c_can_dev(dev); 245 246 pci_iounmap(pdev, addr); 247 pci_disable_msi(pdev); 248 pci_clear_master(pdev); 249 pci_release_regions(pdev); 250 pci_disable_device(pdev); 251 } 252 253 static const struct c_can_pci_data c_can_sta2x11 = { 254 .type = BOSCH_C_CAN, 255 .reg_align = C_CAN_REG_ALIGN_32, 256 .freq = 52000000, /* 52 Mhz */ 257 .bar = 0, 258 }; 259 260 static const struct c_can_pci_data c_can_pch = { 261 .type = BOSCH_C_CAN, 262 .reg_align = C_CAN_REG_32, 263 .freq = 50000000, /* 50 MHz */ 264 .init = c_can_pci_reset_pch, 265 .bar = 1, 266 }; 267 268 #define C_CAN_ID(_vend, _dev, _driverdata) { \ 269 PCI_DEVICE(_vend, _dev), \ 270 .driver_data = (unsigned long)&_driverdata, \ 271 } 272 273 static const struct pci_device_id c_can_pci_tbl[] = { 274 C_CAN_ID(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_CAN, 275 c_can_sta2x11), 276 C_CAN_ID(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PCH_CAN, 277 c_can_pch), 278 {}, 279 }; 280 281 static struct pci_driver c_can_pci_driver = { 282 .name = KBUILD_MODNAME, 283 .id_table = c_can_pci_tbl, 284 .probe = c_can_pci_probe, 285 .remove = c_can_pci_remove, 286 }; 287 288 module_pci_driver(c_can_pci_driver); 289 290 MODULE_AUTHOR("Federico Vaga <federico.vaga@gmail.com>"); 291 MODULE_LICENSE("GPL v2"); 292 MODULE_DESCRIPTION("PCI CAN bus driver for Bosch C_CAN/D_CAN controller"); 293 MODULE_DEVICE_TABLE(pci, c_can_pci_tbl); 294