plx_pci.c 17 KB

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