plx_pci.c 19 KB

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