plx_pci.c 20 KB

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