plx_pci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/pci.h>
  28. #include <linux/can/dev.h>
  29. #include <linux/io.h>
  30. #include "sja1000.h"
  31. #define DRV_NAME "sja1000_plx_pci"
  32. MODULE_AUTHOR("Pavel Cheblakov <P.B.Cheblakov@inp.nsk.su>");
  33. MODULE_DESCRIPTION("Socket-CAN driver for PLX90xx PCI-bridge cards with "
  34. "the SJA1000 chips");
  35. MODULE_SUPPORTED_DEVICE("Adlink PCI-7841/cPCI-7841, "
  36. "Adlink PCI-7841/cPCI-7841 SE, "
  37. "Marathon CAN-bus-PCI, "
  38. "TEWS TECHNOLOGIES TPMC810, "
  39. "esd CAN-PCI/CPCI/PCI104/200, "
  40. "esd CAN-PCI/PMC/266, "
  41. "esd CAN-PCIe/2000, "
  42. "Connect Tech Inc. CANpro/104-Plus Opto (CRG001), "
  43. "IXXAT PC-I 04/PCI, "
  44. "ELCUS CAN-200-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 CAN200PCI_DEVICE_ID 0x9030
  107. #define CAN200PCI_VENDOR_ID 0x10b5
  108. #define CAN200PCI_SUB_DEVICE_ID 0x0301
  109. #define CAN200PCI_SUB_VENDOR_ID 0xe1c5
  110. #define IXXAT_PCI_VENDOR_ID 0x10b5
  111. #define IXXAT_PCI_DEVICE_ID 0x9050
  112. #define IXXAT_PCI_SUB_SYS_ID 0x2540
  113. #define MARATHON_PCI_DEVICE_ID 0x2715
  114. #define TEWS_PCI_VENDOR_ID 0x1498
  115. #define TEWS_PCI_DEVICE_ID_TMPC810 0x032A
  116. #define CTI_PCI_VENDOR_ID 0x12c4
  117. #define CTI_PCI_DEVICE_ID_CRG001 0x0900
  118. static void plx_pci_reset_common(struct pci_dev *pdev);
  119. static void plx_pci_reset_marathon(struct pci_dev *pdev);
  120. static void plx9056_pci_reset_common(struct pci_dev *pdev);
  121. struct plx_pci_channel_map {
  122. u32 bar;
  123. u32 offset;
  124. u32 size; /* 0x00 - auto, e.g. length of entire bar */
  125. };
  126. struct plx_pci_card_info {
  127. const char *name;
  128. int channel_count;
  129. u32 can_clock;
  130. u8 ocr; /* output control register */
  131. u8 cdr; /* clock divider register */
  132. /* Parameters for mapping local configuration space */
  133. struct plx_pci_channel_map conf_map;
  134. /* Parameters for mapping the SJA1000 chips */
  135. struct plx_pci_channel_map chan_map_tbl[PLX_PCI_MAX_CHAN];
  136. /* Pointer to device-dependent reset function */
  137. void (*reset_func)(struct pci_dev *pdev);
  138. };
  139. static struct plx_pci_card_info plx_pci_card_info_adlink = {
  140. "Adlink PCI-7841/cPCI-7841", 2,
  141. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  142. {1, 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_adlink_se = {
  147. "Adlink PCI-7841/cPCI-7841 SE", 2,
  148. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  149. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  150. &plx_pci_reset_common
  151. /* based on PLX9052 */
  152. };
  153. static struct plx_pci_card_info plx_pci_card_info_esd200 = {
  154. "esd CAN-PCI/CPCI/PCI104/200", 2,
  155. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  156. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  157. &plx_pci_reset_common
  158. /* based on PLX9030/9050 */
  159. };
  160. static struct plx_pci_card_info plx_pci_card_info_esd266 = {
  161. "esd CAN-PCI/PMC/266", 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 PLX9056 */
  166. };
  167. static struct plx_pci_card_info plx_pci_card_info_esd2000 = {
  168. "esd CAN-PCIe/2000", 2,
  169. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  170. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  171. &plx9056_pci_reset_common
  172. /* based on PEX8311 */
  173. };
  174. static struct plx_pci_card_info plx_pci_card_info_ixxat = {
  175. "IXXAT PC-I 04/PCI", 2,
  176. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  177. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x200, 0x80} },
  178. &plx_pci_reset_common
  179. /* based on PLX9050 */
  180. };
  181. static struct plx_pci_card_info plx_pci_card_info_marathon = {
  182. "Marathon CAN-bus-PCI", 2,
  183. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  184. {0, 0x00, 0x00}, { {2, 0x00, 0x00}, {4, 0x00, 0x00} },
  185. &plx_pci_reset_marathon
  186. /* based on PLX9052 */
  187. };
  188. static struct plx_pci_card_info plx_pci_card_info_tews = {
  189. "TEWS TECHNOLOGIES TPMC810", 2,
  190. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  191. {0, 0x00, 0x00}, { {2, 0x000, 0x80}, {2, 0x100, 0x80} },
  192. &plx_pci_reset_common
  193. /* based on PLX9030 */
  194. };
  195. static struct plx_pci_card_info plx_pci_card_info_cti = {
  196. "Connect Tech Inc. CANpro/104-Plus Opto (CRG001)", 2,
  197. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  198. {0, 0x00, 0x00}, { {2, 0x000, 0x80}, {2, 0x100, 0x80} },
  199. &plx_pci_reset_common
  200. /* based on PLX9030 */
  201. };
  202. static struct plx_pci_card_info plx_pci_card_info_elcus = {
  203. "Eclus CAN-200-PCI", 2,
  204. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  205. {1, 0x00, 0x00}, { {2, 0x00, 0x80}, {3, 0x00, 0x80} },
  206. &plx_pci_reset_common
  207. /* based on PLX9030 */
  208. };
  209. static const struct pci_device_id plx_pci_tbl[] = {
  210. {
  211. /* Adlink PCI-7841/cPCI-7841 */
  212. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  213. PCI_ANY_ID, PCI_ANY_ID,
  214. PCI_CLASS_NETWORK_OTHER << 8, ~0,
  215. (kernel_ulong_t)&plx_pci_card_info_adlink
  216. },
  217. {
  218. /* Adlink PCI-7841/cPCI-7841 SE */
  219. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  220. PCI_ANY_ID, PCI_ANY_ID,
  221. PCI_CLASS_COMMUNICATION_OTHER << 8, ~0,
  222. (kernel_ulong_t)&plx_pci_card_info_adlink_se
  223. },
  224. {
  225. /* esd CAN-PCI/200 */
  226. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
  227. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI200,
  228. 0, 0,
  229. (kernel_ulong_t)&plx_pci_card_info_esd200
  230. },
  231. {
  232. /* esd CAN-CPCI/200 */
  233. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  234. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_CPCI200,
  235. 0, 0,
  236. (kernel_ulong_t)&plx_pci_card_info_esd200
  237. },
  238. {
  239. /* esd CAN-PCI104/200 */
  240. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  241. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI104200,
  242. 0, 0,
  243. (kernel_ulong_t)&plx_pci_card_info_esd200
  244. },
  245. {
  246. /* esd CAN-PCI/266 */
  247. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  248. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI266,
  249. 0, 0,
  250. (kernel_ulong_t)&plx_pci_card_info_esd266
  251. },
  252. {
  253. /* esd CAN-PMC/266 */
  254. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  255. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PMC266,
  256. 0, 0,
  257. (kernel_ulong_t)&plx_pci_card_info_esd266
  258. },
  259. {
  260. /* esd CAN-PCIE/2000 */
  261. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  262. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCIE2000,
  263. 0, 0,
  264. (kernel_ulong_t)&plx_pci_card_info_esd2000
  265. },
  266. {
  267. /* IXXAT PC-I 04/PCI card */
  268. IXXAT_PCI_VENDOR_ID, IXXAT_PCI_DEVICE_ID,
  269. PCI_ANY_ID, IXXAT_PCI_SUB_SYS_ID,
  270. 0, 0,
  271. (kernel_ulong_t)&plx_pci_card_info_ixxat
  272. },
  273. {
  274. /* Marathon CAN-bus-PCI card */
  275. PCI_VENDOR_ID_PLX, MARATHON_PCI_DEVICE_ID,
  276. PCI_ANY_ID, PCI_ANY_ID,
  277. 0, 0,
  278. (kernel_ulong_t)&plx_pci_card_info_marathon
  279. },
  280. {
  281. /* TEWS TECHNOLOGIES TPMC810 card */
  282. TEWS_PCI_VENDOR_ID, TEWS_PCI_DEVICE_ID_TMPC810,
  283. PCI_ANY_ID, PCI_ANY_ID,
  284. 0, 0,
  285. (kernel_ulong_t)&plx_pci_card_info_tews
  286. },
  287. {
  288. /* Connect Tech Inc. CANpro/104-Plus Opto (CRG001) card */
  289. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  290. CTI_PCI_VENDOR_ID, CTI_PCI_DEVICE_ID_CRG001,
  291. 0, 0,
  292. (kernel_ulong_t)&plx_pci_card_info_cti
  293. },
  294. {
  295. /* Elcus CAN-200-PCI */
  296. CAN200PCI_VENDOR_ID, CAN200PCI_DEVICE_ID,
  297. CAN200PCI_SUB_VENDOR_ID, CAN200PCI_SUB_DEVICE_ID,
  298. 0, 0,
  299. (kernel_ulong_t)&plx_pci_card_info_elcus
  300. },
  301. { 0,}
  302. };
  303. MODULE_DEVICE_TABLE(pci, plx_pci_tbl);
  304. static u8 plx_pci_read_reg(const struct sja1000_priv *priv, int port)
  305. {
  306. return ioread8(priv->reg_base + port);
  307. }
  308. static void plx_pci_write_reg(const struct sja1000_priv *priv, int port, u8 val)
  309. {
  310. iowrite8(val, priv->reg_base + port);
  311. }
  312. /*
  313. * Check if a CAN controller is present at the specified location
  314. * by trying to switch 'em from the Basic mode into the PeliCAN mode.
  315. * Also check states of some registers in reset mode.
  316. */
  317. static inline int plx_pci_check_sja1000(const struct sja1000_priv *priv)
  318. {
  319. int flag = 0;
  320. /*
  321. * Check registers after hardware reset (the Basic mode)
  322. * See states on p. 10 of the Datasheet.
  323. */
  324. if ((priv->read_reg(priv, REG_CR) & REG_CR_BASICCAN_INITIAL_MASK) ==
  325. REG_CR_BASICCAN_INITIAL &&
  326. (priv->read_reg(priv, SJA1000_SR) == REG_SR_BASICCAN_INITIAL) &&
  327. (priv->read_reg(priv, SJA1000_IR) == REG_IR_BASICCAN_INITIAL))
  328. flag = 1;
  329. /* Bring the SJA1000 into the PeliCAN mode*/
  330. priv->write_reg(priv, SJA1000_CDR, CDR_PELICAN);
  331. /*
  332. * Check registers after reset in the PeliCAN mode.
  333. * See states on p. 23 of the Datasheet.
  334. */
  335. if (priv->read_reg(priv, SJA1000_MOD) == REG_MOD_PELICAN_INITIAL &&
  336. priv->read_reg(priv, SJA1000_SR) == REG_SR_PELICAN_INITIAL &&
  337. priv->read_reg(priv, SJA1000_IR) == REG_IR_PELICAN_INITIAL)
  338. return flag;
  339. return 0;
  340. }
  341. /*
  342. * PLX9030/50/52 software reset
  343. * Also LRESET# asserts and brings to reset device on the Local Bus (if wired).
  344. * For most cards it's enough for reset the SJA1000 chips.
  345. */
  346. static void plx_pci_reset_common(struct pci_dev *pdev)
  347. {
  348. struct plx_pci_card *card = pci_get_drvdata(pdev);
  349. u32 cntrl;
  350. cntrl = ioread32(card->conf_addr + PLX_CNTRL);
  351. cntrl |= PLX_PCI_RESET;
  352. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  353. udelay(100);
  354. cntrl ^= PLX_PCI_RESET;
  355. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  356. };
  357. /*
  358. * PLX9056 software reset
  359. * Assert LRESET# and reset device(s) on the Local Bus (if wired).
  360. */
  361. static void plx9056_pci_reset_common(struct pci_dev *pdev)
  362. {
  363. struct plx_pci_card *card = pci_get_drvdata(pdev);
  364. u32 cntrl;
  365. /* issue a local bus reset */
  366. cntrl = ioread32(card->conf_addr + PLX9056_CNTRL);
  367. cntrl |= PLX_PCI_RESET;
  368. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  369. udelay(100);
  370. cntrl ^= PLX_PCI_RESET;
  371. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  372. /* reload local configuration from EEPROM */
  373. cntrl |= PLX9056_PCI_RCR;
  374. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  375. /*
  376. * There is no safe way to poll for the end
  377. * of reconfiguration process. Waiting for 10ms
  378. * is safe.
  379. */
  380. mdelay(10);
  381. cntrl ^= PLX9056_PCI_RCR;
  382. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  383. };
  384. /* Special reset function for Marathon card */
  385. static void plx_pci_reset_marathon(struct pci_dev *pdev)
  386. {
  387. void __iomem *reset_addr;
  388. int i;
  389. static const int reset_bar[2] = {3, 5};
  390. plx_pci_reset_common(pdev);
  391. for (i = 0; i < 2; i++) {
  392. reset_addr = pci_iomap(pdev, reset_bar[i], 0);
  393. if (!reset_addr) {
  394. dev_err(&pdev->dev, "Failed to remap reset "
  395. "space %d (BAR%d)\n", i, reset_bar[i]);
  396. } else {
  397. /* reset the SJA1000 chip */
  398. iowrite8(0x1, reset_addr);
  399. udelay(100);
  400. pci_iounmap(pdev, reset_addr);
  401. }
  402. }
  403. }
  404. static void plx_pci_del_card(struct pci_dev *pdev)
  405. {
  406. struct plx_pci_card *card = pci_get_drvdata(pdev);
  407. struct net_device *dev;
  408. struct sja1000_priv *priv;
  409. int i = 0;
  410. for (i = 0; i < PLX_PCI_MAX_CHAN; i++) {
  411. dev = card->net_dev[i];
  412. if (!dev)
  413. continue;
  414. dev_info(&pdev->dev, "Removing %s\n", dev->name);
  415. unregister_sja1000dev(dev);
  416. priv = netdev_priv(dev);
  417. if (priv->reg_base)
  418. pci_iounmap(pdev, priv->reg_base);
  419. free_sja1000dev(dev);
  420. }
  421. card->reset_func(pdev);
  422. /*
  423. * Disable interrupts from PCI-card and disable local
  424. * interrupts
  425. */
  426. if (pdev->device != PCI_DEVICE_ID_PLX_9056)
  427. iowrite32(0x0, card->conf_addr + PLX_INTCSR);
  428. else
  429. iowrite32(0x0, card->conf_addr + PLX9056_INTCSR);
  430. if (card->conf_addr)
  431. pci_iounmap(pdev, card->conf_addr);
  432. kfree(card);
  433. pci_disable_device(pdev);
  434. }
  435. /*
  436. * Probe PLX90xx based device for the SJA1000 chips and register each
  437. * available CAN channel to SJA1000 Socket-CAN subsystem.
  438. */
  439. static int plx_pci_add_card(struct pci_dev *pdev,
  440. const struct pci_device_id *ent)
  441. {
  442. struct sja1000_priv *priv;
  443. struct net_device *dev;
  444. struct plx_pci_card *card;
  445. struct plx_pci_card_info *ci;
  446. int err, i;
  447. u32 val;
  448. void __iomem *addr;
  449. ci = (struct plx_pci_card_info *)ent->driver_data;
  450. if (pci_enable_device(pdev) < 0) {
  451. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  452. return -ENODEV;
  453. }
  454. dev_info(&pdev->dev, "Detected \"%s\" card at slot #%i\n",
  455. ci->name, PCI_SLOT(pdev->devfn));
  456. /* Allocate card structures to hold addresses, ... */
  457. card = kzalloc(sizeof(*card), GFP_KERNEL);
  458. if (!card) {
  459. pci_disable_device(pdev);
  460. return -ENOMEM;
  461. }
  462. pci_set_drvdata(pdev, card);
  463. card->channels = 0;
  464. /* Remap PLX90xx configuration space */
  465. addr = pci_iomap(pdev, ci->conf_map.bar, ci->conf_map.size);
  466. if (!addr) {
  467. err = -ENOMEM;
  468. dev_err(&pdev->dev, "Failed to remap configuration space "
  469. "(BAR%d)\n", ci->conf_map.bar);
  470. goto failure_cleanup;
  471. }
  472. card->conf_addr = addr + ci->conf_map.offset;
  473. ci->reset_func(pdev);
  474. card->reset_func = ci->reset_func;
  475. /* Detect available channels */
  476. for (i = 0; i < ci->channel_count; i++) {
  477. struct plx_pci_channel_map *cm = &ci->chan_map_tbl[i];
  478. dev = alloc_sja1000dev(0);
  479. if (!dev) {
  480. err = -ENOMEM;
  481. goto failure_cleanup;
  482. }
  483. card->net_dev[i] = dev;
  484. priv = netdev_priv(dev);
  485. priv->priv = card;
  486. priv->irq_flags = IRQF_SHARED;
  487. dev->irq = pdev->irq;
  488. /*
  489. * Remap IO space of the SJA1000 chips
  490. * This is device-dependent mapping
  491. */
  492. addr = pci_iomap(pdev, cm->bar, cm->size);
  493. if (!addr) {
  494. err = -ENOMEM;
  495. dev_err(&pdev->dev, "Failed to remap BAR%d\n", cm->bar);
  496. goto failure_cleanup;
  497. }
  498. priv->reg_base = addr + cm->offset;
  499. priv->read_reg = plx_pci_read_reg;
  500. priv->write_reg = plx_pci_write_reg;
  501. /* Check if channel is present */
  502. if (plx_pci_check_sja1000(priv)) {
  503. priv->can.clock.freq = ci->can_clock;
  504. priv->ocr = ci->ocr;
  505. priv->cdr = ci->cdr;
  506. SET_NETDEV_DEV(dev, &pdev->dev);
  507. dev->dev_id = i;
  508. /* Register SJA1000 device */
  509. err = register_sja1000dev(dev);
  510. if (err) {
  511. dev_err(&pdev->dev, "Registering device failed "
  512. "(err=%d)\n", err);
  513. goto failure_cleanup;
  514. }
  515. card->channels++;
  516. dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d "
  517. "registered as %s\n", i + 1, priv->reg_base,
  518. dev->irq, dev->name);
  519. } else {
  520. dev_err(&pdev->dev, "Channel #%d not detected\n",
  521. i + 1);
  522. free_sja1000dev(dev);
  523. card->net_dev[i] = NULL;
  524. }
  525. }
  526. if (!card->channels) {
  527. err = -ENODEV;
  528. goto failure_cleanup;
  529. }
  530. /*
  531. * Enable interrupts from PCI-card (PLX90xx) and enable Local_1,
  532. * Local_2 interrupts from the SJA1000 chips
  533. */
  534. if (pdev->device != PCI_DEVICE_ID_PLX_9056) {
  535. val = ioread32(card->conf_addr + PLX_INTCSR);
  536. if (pdev->subsystem_vendor == PCI_VENDOR_ID_ESDGMBH)
  537. val |= PLX_LINT1_EN | PLX_PCI_INT_EN;
  538. else
  539. val |= PLX_LINT1_EN | PLX_LINT2_EN | PLX_PCI_INT_EN;
  540. iowrite32(val, card->conf_addr + PLX_INTCSR);
  541. } else {
  542. iowrite32(PLX9056_LINTI | PLX9056_PCI_INT_EN,
  543. card->conf_addr + PLX9056_INTCSR);
  544. }
  545. return 0;
  546. failure_cleanup:
  547. dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
  548. plx_pci_del_card(pdev);
  549. return err;
  550. }
  551. static struct pci_driver plx_pci_driver = {
  552. .name = DRV_NAME,
  553. .id_table = plx_pci_tbl,
  554. .probe = plx_pci_add_card,
  555. .remove = plx_pci_del_card,
  556. };
  557. module_pci_driver(plx_pci_driver);