plx_pci.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright (C) 2008-2010 Pavel Cheblakov <P.B.Cheblakov@inp.nsk.su>
  3. *
  4. * Derived from the ems_pci.c driver:
  5. * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  6. * Copyright (C) 2008 Markus Plessing <plessing@ems-wuensche.com>
  7. * Copyright (C) 2008 Sebastian Haas <haas@ems-wuensche.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the version 2 of the GNU General Public License
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/pci.h>
  29. #include <linux/can/dev.h>
  30. #include <linux/io.h>
  31. #include "sja1000.h"
  32. #define DRV_NAME "sja1000_plx_pci"
  33. MODULE_AUTHOR("Pavel Cheblakov <P.B.Cheblakov@inp.nsk.su>");
  34. MODULE_DESCRIPTION("Socket-CAN driver for PLX90xx PCI-bridge cards with "
  35. "the SJA1000 chips");
  36. MODULE_SUPPORTED_DEVICE("Adlink PCI-7841/cPCI-7841, "
  37. "Adlink PCI-7841/cPCI-7841 SE, "
  38. "Marathon CAN-bus-PCI, "
  39. "TEWS TECHNOLOGIES TPMC810, "
  40. "esd CAN-PCI/CPCI/PCI104/200, "
  41. "esd CAN-PCI/PMC/266, "
  42. "esd CAN-PCIe/2000, "
  43. "IXXAT PC-I 04/PCI")
  44. MODULE_LICENSE("GPL v2");
  45. #define PLX_PCI_MAX_CHAN 2
  46. struct plx_pci_card {
  47. int channels; /* detected channels count */
  48. struct net_device *net_dev[PLX_PCI_MAX_CHAN];
  49. void __iomem *conf_addr;
  50. /* Pointer to device-dependent reset function */
  51. void (*reset_func)(struct pci_dev *pdev);
  52. };
  53. #define PLX_PCI_CAN_CLOCK (16000000 / 2)
  54. /* PLX9030/9050/9052 registers */
  55. #define PLX_INTCSR 0x4c /* Interrupt Control/Status */
  56. #define PLX_CNTRL 0x50 /* User I/O, Direct Slave Response,
  57. * Serial EEPROM, and Initialization
  58. * Control register
  59. */
  60. #define PLX_LINT1_EN 0x1 /* Local interrupt 1 enable */
  61. #define PLX_LINT2_EN (1 << 3) /* Local interrupt 2 enable */
  62. #define PLX_PCI_INT_EN (1 << 6) /* PCI Interrupt Enable */
  63. #define PLX_PCI_RESET (1 << 30) /* PCI Adapter Software Reset */
  64. /* PLX9056 registers */
  65. #define PLX9056_INTCSR 0x68 /* Interrupt Control/Status */
  66. #define PLX9056_CNTRL 0x6c /* Control / Software Reset */
  67. #define PLX9056_LINTI (1 << 11)
  68. #define PLX9056_PCI_INT_EN (1 << 8)
  69. #define PLX9056_PCI_RCR (1 << 29) /* Read Configuration Registers */
  70. /*
  71. * The board configuration is probably following:
  72. * RX1 is connected to ground.
  73. * TX1 is not connected.
  74. * CLKO is not connected.
  75. * Setting the OCR register to 0xDA is a good idea.
  76. * This means normal output mode, push-pull and the correct polarity.
  77. */
  78. #define PLX_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
  79. /*
  80. * In the CDR register, you should set CBP to 1.
  81. * You will probably also want to set the clock divider value to 7
  82. * (meaning direct oscillator output) because the second SJA1000 chip
  83. * is driven by the first one CLKOUT output.
  84. */
  85. #define PLX_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  86. /* SJA1000 Control Register in the BasicCAN Mode */
  87. #define REG_CR 0x00
  88. /* States of some SJA1000 registers after hardware reset in the BasicCAN mode*/
  89. #define REG_CR_BASICCAN_INITIAL 0x21
  90. #define REG_CR_BASICCAN_INITIAL_MASK 0xa1
  91. #define REG_SR_BASICCAN_INITIAL 0x0c
  92. #define REG_IR_BASICCAN_INITIAL 0xe0
  93. /* States of some SJA1000 registers after hardware reset in the PeliCAN mode*/
  94. #define REG_MOD_PELICAN_INITIAL 0x01
  95. #define REG_SR_PELICAN_INITIAL 0x3c
  96. #define REG_IR_PELICAN_INITIAL 0x00
  97. #define ADLINK_PCI_VENDOR_ID 0x144A
  98. #define ADLINK_PCI_DEVICE_ID 0x7841
  99. #define ESD_PCI_SUB_SYS_ID_PCI200 0x0004
  100. #define ESD_PCI_SUB_SYS_ID_PCI266 0x0009
  101. #define ESD_PCI_SUB_SYS_ID_PMC266 0x000e
  102. #define ESD_PCI_SUB_SYS_ID_CPCI200 0x010b
  103. #define ESD_PCI_SUB_SYS_ID_PCIE2000 0x0200
  104. #define ESD_PCI_SUB_SYS_ID_PCI104200 0x0501
  105. #define IXXAT_PCI_VENDOR_ID 0x10b5
  106. #define IXXAT_PCI_DEVICE_ID 0x9050
  107. #define IXXAT_PCI_SUB_SYS_ID 0x2540
  108. #define MARATHON_PCI_DEVICE_ID 0x2715
  109. #define TEWS_PCI_VENDOR_ID 0x1498
  110. #define TEWS_PCI_DEVICE_ID_TMPC810 0x032A
  111. static void plx_pci_reset_common(struct pci_dev *pdev);
  112. static void plx_pci_reset_marathon(struct pci_dev *pdev);
  113. static void plx9056_pci_reset_common(struct pci_dev *pdev);
  114. struct plx_pci_channel_map {
  115. u32 bar;
  116. u32 offset;
  117. u32 size; /* 0x00 - auto, e.g. length of entire bar */
  118. };
  119. struct plx_pci_card_info {
  120. const char *name;
  121. int channel_count;
  122. u32 can_clock;
  123. u8 ocr; /* output control register */
  124. u8 cdr; /* clock divider register */
  125. /* Parameters for mapping local configuration space */
  126. struct plx_pci_channel_map conf_map;
  127. /* Parameters for mapping the SJA1000 chips */
  128. struct plx_pci_channel_map chan_map_tbl[PLX_PCI_MAX_CHAN];
  129. /* Pointer to device-dependent reset function */
  130. void (*reset_func)(struct pci_dev *pdev);
  131. };
  132. static struct plx_pci_card_info plx_pci_card_info_adlink __devinitdata = {
  133. "Adlink PCI-7841/cPCI-7841", 2,
  134. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  135. {1, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  136. &plx_pci_reset_common
  137. /* based on PLX9052 */
  138. };
  139. static struct plx_pci_card_info plx_pci_card_info_adlink_se __devinitdata = {
  140. "Adlink PCI-7841/cPCI-7841 SE", 2,
  141. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  142. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  143. &plx_pci_reset_common
  144. /* based on PLX9052 */
  145. };
  146. static struct plx_pci_card_info plx_pci_card_info_esd200 __devinitdata = {
  147. "esd CAN-PCI/CPCI/PCI104/200", 2,
  148. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  149. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  150. &plx_pci_reset_common
  151. /* based on PLX9030/9050 */
  152. };
  153. static struct plx_pci_card_info plx_pci_card_info_esd266 __devinitdata = {
  154. "esd CAN-PCI/PMC/266", 2,
  155. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  156. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  157. &plx9056_pci_reset_common
  158. /* based on PLX9056 */
  159. };
  160. static struct plx_pci_card_info plx_pci_card_info_esd2000 __devinitdata = {
  161. "esd CAN-PCIe/2000", 2,
  162. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  163. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  164. &plx9056_pci_reset_common
  165. /* based on PEX8311 */
  166. };
  167. static struct plx_pci_card_info plx_pci_card_info_ixxat __devinitdata = {
  168. "IXXAT PC-I 04/PCI", 2,
  169. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  170. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x200, 0x80} },
  171. &plx_pci_reset_common
  172. /* based on PLX9050 */
  173. };
  174. static struct plx_pci_card_info plx_pci_card_info_marathon __devinitdata = {
  175. "Marathon CAN-bus-PCI", 2,
  176. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  177. {0, 0x00, 0x00}, { {2, 0x00, 0x00}, {4, 0x00, 0x00} },
  178. &plx_pci_reset_marathon
  179. /* based on PLX9052 */
  180. };
  181. static struct plx_pci_card_info plx_pci_card_info_tews __devinitdata = {
  182. "TEWS TECHNOLOGIES TPMC810", 2,
  183. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  184. {0, 0x00, 0x00}, { {2, 0x000, 0x80}, {2, 0x100, 0x80} },
  185. &plx_pci_reset_common
  186. /* based on PLX9030 */
  187. };
  188. static DEFINE_PCI_DEVICE_TABLE(plx_pci_tbl) = {
  189. {
  190. /* Adlink PCI-7841/cPCI-7841 */
  191. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  192. PCI_ANY_ID, PCI_ANY_ID,
  193. PCI_CLASS_NETWORK_OTHER << 8, ~0,
  194. (kernel_ulong_t)&plx_pci_card_info_adlink
  195. },
  196. {
  197. /* Adlink PCI-7841/cPCI-7841 SE */
  198. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  199. PCI_ANY_ID, PCI_ANY_ID,
  200. PCI_CLASS_COMMUNICATION_OTHER << 8, ~0,
  201. (kernel_ulong_t)&plx_pci_card_info_adlink_se
  202. },
  203. {
  204. /* esd CAN-PCI/200 */
  205. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
  206. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI200,
  207. 0, 0,
  208. (kernel_ulong_t)&plx_pci_card_info_esd200
  209. },
  210. {
  211. /* esd CAN-CPCI/200 */
  212. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  213. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_CPCI200,
  214. 0, 0,
  215. (kernel_ulong_t)&plx_pci_card_info_esd200
  216. },
  217. {
  218. /* esd CAN-PCI104/200 */
  219. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  220. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI104200,
  221. 0, 0,
  222. (kernel_ulong_t)&plx_pci_card_info_esd200
  223. },
  224. {
  225. /* esd CAN-PCI/266 */
  226. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  227. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI266,
  228. 0, 0,
  229. (kernel_ulong_t)&plx_pci_card_info_esd266
  230. },
  231. {
  232. /* esd CAN-PMC/266 */
  233. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  234. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PMC266,
  235. 0, 0,
  236. (kernel_ulong_t)&plx_pci_card_info_esd266
  237. },
  238. {
  239. /* esd CAN-PCIE/2000 */
  240. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  241. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCIE2000,
  242. 0, 0,
  243. (kernel_ulong_t)&plx_pci_card_info_esd2000
  244. },
  245. {
  246. /* IXXAT PC-I 04/PCI card */
  247. IXXAT_PCI_VENDOR_ID, IXXAT_PCI_DEVICE_ID,
  248. PCI_ANY_ID, IXXAT_PCI_SUB_SYS_ID,
  249. 0, 0,
  250. (kernel_ulong_t)&plx_pci_card_info_ixxat
  251. },
  252. {
  253. /* Marathon CAN-bus-PCI card */
  254. PCI_VENDOR_ID_PLX, MARATHON_PCI_DEVICE_ID,
  255. PCI_ANY_ID, PCI_ANY_ID,
  256. 0, 0,
  257. (kernel_ulong_t)&plx_pci_card_info_marathon
  258. },
  259. {
  260. /* TEWS TECHNOLOGIES TPMC810 card */
  261. TEWS_PCI_VENDOR_ID, TEWS_PCI_DEVICE_ID_TMPC810,
  262. PCI_ANY_ID, PCI_ANY_ID,
  263. 0, 0,
  264. (kernel_ulong_t)&plx_pci_card_info_tews
  265. },
  266. { 0,}
  267. };
  268. MODULE_DEVICE_TABLE(pci, plx_pci_tbl);
  269. static u8 plx_pci_read_reg(const struct sja1000_priv *priv, int port)
  270. {
  271. return ioread8(priv->reg_base + port);
  272. }
  273. static void plx_pci_write_reg(const struct sja1000_priv *priv, int port, u8 val)
  274. {
  275. iowrite8(val, priv->reg_base + port);
  276. }
  277. /*
  278. * Check if a CAN controller is present at the specified location
  279. * by trying to switch 'em from the Basic mode into the PeliCAN mode.
  280. * Also check states of some registers in reset mode.
  281. */
  282. static inline int plx_pci_check_sja1000(const struct sja1000_priv *priv)
  283. {
  284. int flag = 0;
  285. /*
  286. * Check registers after hardware reset (the Basic mode)
  287. * See states on p. 10 of the Datasheet.
  288. */
  289. if ((priv->read_reg(priv, REG_CR) & REG_CR_BASICCAN_INITIAL_MASK) ==
  290. REG_CR_BASICCAN_INITIAL &&
  291. (priv->read_reg(priv, SJA1000_REG_SR) == REG_SR_BASICCAN_INITIAL) &&
  292. (priv->read_reg(priv, REG_IR) == REG_IR_BASICCAN_INITIAL))
  293. flag = 1;
  294. /* Bring the SJA1000 into the PeliCAN mode*/
  295. priv->write_reg(priv, REG_CDR, CDR_PELICAN);
  296. /*
  297. * Check registers after reset in the PeliCAN mode.
  298. * See states on p. 23 of the Datasheet.
  299. */
  300. if (priv->read_reg(priv, REG_MOD) == REG_MOD_PELICAN_INITIAL &&
  301. priv->read_reg(priv, SJA1000_REG_SR) == REG_SR_PELICAN_INITIAL &&
  302. priv->read_reg(priv, REG_IR) == REG_IR_PELICAN_INITIAL)
  303. return flag;
  304. return 0;
  305. }
  306. /*
  307. * PLX9030/50/52 software reset
  308. * Also LRESET# asserts and brings to reset device on the Local Bus (if wired).
  309. * For most cards it's enough for reset the SJA1000 chips.
  310. */
  311. static void plx_pci_reset_common(struct pci_dev *pdev)
  312. {
  313. struct plx_pci_card *card = pci_get_drvdata(pdev);
  314. u32 cntrl;
  315. cntrl = ioread32(card->conf_addr + PLX_CNTRL);
  316. cntrl |= PLX_PCI_RESET;
  317. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  318. udelay(100);
  319. cntrl ^= PLX_PCI_RESET;
  320. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  321. };
  322. /*
  323. * PLX9056 software reset
  324. * Assert LRESET# and reset device(s) on the Local Bus (if wired).
  325. */
  326. static void plx9056_pci_reset_common(struct pci_dev *pdev)
  327. {
  328. struct plx_pci_card *card = pci_get_drvdata(pdev);
  329. u32 cntrl;
  330. /* issue a local bus reset */
  331. cntrl = ioread32(card->conf_addr + PLX9056_CNTRL);
  332. cntrl |= PLX_PCI_RESET;
  333. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  334. udelay(100);
  335. cntrl ^= PLX_PCI_RESET;
  336. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  337. /* reload local configuration from EEPROM */
  338. cntrl |= PLX9056_PCI_RCR;
  339. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  340. /*
  341. * There is no safe way to poll for the end
  342. * of reconfiguration process. Waiting for 10ms
  343. * is safe.
  344. */
  345. mdelay(10);
  346. cntrl ^= PLX9056_PCI_RCR;
  347. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  348. };
  349. /* Special reset function for Marathon card */
  350. static void plx_pci_reset_marathon(struct pci_dev *pdev)
  351. {
  352. void __iomem *reset_addr;
  353. int i;
  354. static const int reset_bar[2] = {3, 5};
  355. plx_pci_reset_common(pdev);
  356. for (i = 0; i < 2; i++) {
  357. reset_addr = pci_iomap(pdev, reset_bar[i], 0);
  358. if (!reset_addr) {
  359. dev_err(&pdev->dev, "Failed to remap reset "
  360. "space %d (BAR%d)\n", i, reset_bar[i]);
  361. } else {
  362. /* reset the SJA1000 chip */
  363. iowrite8(0x1, reset_addr);
  364. udelay(100);
  365. pci_iounmap(pdev, reset_addr);
  366. }
  367. }
  368. }
  369. static void plx_pci_del_card(struct pci_dev *pdev)
  370. {
  371. struct plx_pci_card *card = pci_get_drvdata(pdev);
  372. struct net_device *dev;
  373. struct sja1000_priv *priv;
  374. int i = 0;
  375. for (i = 0; i < PLX_PCI_MAX_CHAN; i++) {
  376. dev = card->net_dev[i];
  377. if (!dev)
  378. continue;
  379. dev_info(&pdev->dev, "Removing %s\n", dev->name);
  380. unregister_sja1000dev(dev);
  381. priv = netdev_priv(dev);
  382. if (priv->reg_base)
  383. pci_iounmap(pdev, priv->reg_base);
  384. free_sja1000dev(dev);
  385. }
  386. card->reset_func(pdev);
  387. /*
  388. * Disable interrupts from PCI-card and disable local
  389. * interrupts
  390. */
  391. if (pdev->device != PCI_DEVICE_ID_PLX_9056)
  392. iowrite32(0x0, card->conf_addr + PLX_INTCSR);
  393. else
  394. iowrite32(0x0, card->conf_addr + PLX9056_INTCSR);
  395. if (card->conf_addr)
  396. pci_iounmap(pdev, card->conf_addr);
  397. kfree(card);
  398. pci_disable_device(pdev);
  399. pci_set_drvdata(pdev, NULL);
  400. }
  401. /*
  402. * Probe PLX90xx based device for the SJA1000 chips and register each
  403. * available CAN channel to SJA1000 Socket-CAN subsystem.
  404. */
  405. static int __devinit plx_pci_add_card(struct pci_dev *pdev,
  406. const struct pci_device_id *ent)
  407. {
  408. struct sja1000_priv *priv;
  409. struct net_device *dev;
  410. struct plx_pci_card *card;
  411. struct plx_pci_card_info *ci;
  412. int err, i;
  413. u32 val;
  414. void __iomem *addr;
  415. ci = (struct plx_pci_card_info *)ent->driver_data;
  416. if (pci_enable_device(pdev) < 0) {
  417. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  418. return -ENODEV;
  419. }
  420. dev_info(&pdev->dev, "Detected \"%s\" card at slot #%i\n",
  421. ci->name, PCI_SLOT(pdev->devfn));
  422. /* Allocate card structures to hold addresses, ... */
  423. card = kzalloc(sizeof(*card), GFP_KERNEL);
  424. if (!card) {
  425. dev_err(&pdev->dev, "Unable to allocate memory\n");
  426. pci_disable_device(pdev);
  427. return -ENOMEM;
  428. }
  429. pci_set_drvdata(pdev, card);
  430. card->channels = 0;
  431. /* Remap PLX90xx configuration space */
  432. addr = pci_iomap(pdev, ci->conf_map.bar, ci->conf_map.size);
  433. if (!addr) {
  434. err = -ENOMEM;
  435. dev_err(&pdev->dev, "Failed to remap configuration space "
  436. "(BAR%d)\n", ci->conf_map.bar);
  437. goto failure_cleanup;
  438. }
  439. card->conf_addr = addr + ci->conf_map.offset;
  440. ci->reset_func(pdev);
  441. card->reset_func = ci->reset_func;
  442. /* Detect available channels */
  443. for (i = 0; i < ci->channel_count; i++) {
  444. struct plx_pci_channel_map *cm = &ci->chan_map_tbl[i];
  445. dev = alloc_sja1000dev(0);
  446. if (!dev) {
  447. err = -ENOMEM;
  448. goto failure_cleanup;
  449. }
  450. card->net_dev[i] = dev;
  451. priv = netdev_priv(dev);
  452. priv->priv = card;
  453. priv->irq_flags = IRQF_SHARED;
  454. dev->irq = pdev->irq;
  455. /*
  456. * Remap IO space of the SJA1000 chips
  457. * This is device-dependent mapping
  458. */
  459. addr = pci_iomap(pdev, cm->bar, cm->size);
  460. if (!addr) {
  461. err = -ENOMEM;
  462. dev_err(&pdev->dev, "Failed to remap BAR%d\n", cm->bar);
  463. goto failure_cleanup;
  464. }
  465. priv->reg_base = addr + cm->offset;
  466. priv->read_reg = plx_pci_read_reg;
  467. priv->write_reg = plx_pci_write_reg;
  468. /* Check if channel is present */
  469. if (plx_pci_check_sja1000(priv)) {
  470. priv->can.clock.freq = ci->can_clock;
  471. priv->ocr = ci->ocr;
  472. priv->cdr = ci->cdr;
  473. SET_NETDEV_DEV(dev, &pdev->dev);
  474. /* Register SJA1000 device */
  475. err = register_sja1000dev(dev);
  476. if (err) {
  477. dev_err(&pdev->dev, "Registering device failed "
  478. "(err=%d)\n", err);
  479. goto failure_cleanup;
  480. }
  481. card->channels++;
  482. dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d "
  483. "registered as %s\n", i + 1, priv->reg_base,
  484. dev->irq, dev->name);
  485. } else {
  486. dev_err(&pdev->dev, "Channel #%d not detected\n",
  487. i + 1);
  488. free_sja1000dev(dev);
  489. card->net_dev[i] = NULL;
  490. }
  491. }
  492. if (!card->channels) {
  493. err = -ENODEV;
  494. goto failure_cleanup;
  495. }
  496. /*
  497. * Enable interrupts from PCI-card (PLX90xx) and enable Local_1,
  498. * Local_2 interrupts from the SJA1000 chips
  499. */
  500. if (pdev->device != PCI_DEVICE_ID_PLX_9056) {
  501. val = ioread32(card->conf_addr + PLX_INTCSR);
  502. if (pdev->subsystem_vendor == PCI_VENDOR_ID_ESDGMBH)
  503. val |= PLX_LINT1_EN | PLX_PCI_INT_EN;
  504. else
  505. val |= PLX_LINT1_EN | PLX_LINT2_EN | PLX_PCI_INT_EN;
  506. iowrite32(val, card->conf_addr + PLX_INTCSR);
  507. } else {
  508. iowrite32(PLX9056_LINTI | PLX9056_PCI_INT_EN,
  509. card->conf_addr + PLX9056_INTCSR);
  510. }
  511. return 0;
  512. failure_cleanup:
  513. dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
  514. plx_pci_del_card(pdev);
  515. return err;
  516. }
  517. static struct pci_driver plx_pci_driver = {
  518. .name = DRV_NAME,
  519. .id_table = plx_pci_tbl,
  520. .probe = plx_pci_add_card,
  521. .remove = plx_pci_del_card,
  522. };
  523. static int __init plx_pci_init(void)
  524. {
  525. return pci_register_driver(&plx_pci_driver);
  526. }
  527. static void __exit plx_pci_exit(void)
  528. {
  529. pci_unregister_driver(&plx_pci_driver);
  530. }
  531. module_init(plx_pci_init);
  532. module_exit(plx_pci_exit);