plx_pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008-2010 Pavel Cheblakov <P.B.Cheblakov@inp.nsk.su>
  4. *
  5. * Derived from the ems_pci.c driver:
  6. * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  7. * Copyright (C) 2008 Markus Plessing <plessing@ems-wuensche.com>
  8. * Copyright (C) 2008 Sebastian Haas <haas@ems-wuensche.com>
  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 <P.B.Cheblakov@inp.nsk.su>");
  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_DEVICE_ID_CRG001 0x0900
  101. #define MOXA_PCI_VENDOR_ID 0x1393
  102. #define MOXA_PCI_DEVICE_ID 0x0100
  103. #define ASEM_RAW_CAN_VENDOR_ID 0x10b5
  104. #define ASEM_RAW_CAN_DEVICE_ID 0x9030
  105. #define ASEM_RAW_CAN_SUB_VENDOR_ID 0x3000
  106. #define ASEM_RAW_CAN_SUB_DEVICE_ID 0x1001
  107. #define ASEM_RAW_CAN_SUB_DEVICE_ID_BIS 0x1002
  108. #define ASEM_RAW_CAN_RST_REGISTER 0x54
  109. #define ASEM_RAW_CAN_RST_MASK_CAN1 0x20
  110. #define ASEM_RAW_CAN_RST_MASK_CAN2 0x04
  111. static void plx_pci_reset_common(struct pci_dev *pdev);
  112. static void plx9056_pci_reset_common(struct pci_dev *pdev);
  113. static void plx_pci_reset_marathon_pci(struct pci_dev *pdev);
  114. static void plx_pci_reset_marathon_pcie(struct pci_dev *pdev);
  115. static void plx_pci_reset_asem_dual_can_raw(struct pci_dev *pdev);
  116. struct plx_pci_channel_map {
  117. u32 bar;
  118. u32 offset;
  119. u32 size; /* 0x00 - auto, e.g. length of entire bar */
  120. };
  121. struct plx_pci_card_info {
  122. const char *name;
  123. int channel_count;
  124. u32 can_clock;
  125. u8 ocr; /* output control register */
  126. u8 cdr; /* clock divider register */
  127. /* Parameters for mapping local configuration space */
  128. struct plx_pci_channel_map conf_map;
  129. /* Parameters for mapping the SJA1000 chips */
  130. struct plx_pci_channel_map chan_map_tbl[PLX_PCI_MAX_CHAN];
  131. /* Pointer to device-dependent reset function */
  132. void (*reset_func)(struct pci_dev *pdev);
  133. };
  134. static struct plx_pci_card_info plx_pci_card_info_adlink = {
  135. "Adlink PCI-7841/cPCI-7841", 2,
  136. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  137. {1, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  138. &plx_pci_reset_common
  139. /* based on PLX9052 */
  140. };
  141. static struct plx_pci_card_info plx_pci_card_info_adlink_se = {
  142. "Adlink PCI-7841/cPCI-7841 SE", 2,
  143. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  144. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  145. &plx_pci_reset_common
  146. /* based on PLX9052 */
  147. };
  148. static struct plx_pci_card_info plx_pci_card_info_esd200 = {
  149. "esd CAN-PCI/CPCI/PCI104/200", 2,
  150. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  151. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  152. &plx_pci_reset_common
  153. /* based on PLX9030/9050 */
  154. };
  155. static struct plx_pci_card_info plx_pci_card_info_esd266 = {
  156. "esd CAN-PCI/PMC/266", 2,
  157. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  158. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  159. &plx9056_pci_reset_common
  160. /* based on PLX9056 */
  161. };
  162. static struct plx_pci_card_info plx_pci_card_info_esd2000 = {
  163. "esd CAN-PCIe/2000", 2,
  164. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  165. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x100, 0x80} },
  166. &plx9056_pci_reset_common
  167. /* based on PEX8311 */
  168. };
  169. static struct plx_pci_card_info plx_pci_card_info_ixxat = {
  170. "IXXAT PC-I 04/PCI", 2,
  171. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  172. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x200, 0x80} },
  173. &plx_pci_reset_common
  174. /* based on PLX9050 */
  175. };
  176. static struct plx_pci_card_info plx_pci_card_info_marathon_pci = {
  177. "Marathon CAN-bus-PCI", 2,
  178. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  179. {0, 0x00, 0x00}, { {2, 0x00, 0x00}, {4, 0x00, 0x00} },
  180. &plx_pci_reset_marathon_pci
  181. /* based on PLX9052 */
  182. };
  183. static struct plx_pci_card_info plx_pci_card_info_marathon_pcie = {
  184. "Marathon CAN-bus-PCIe", 2,
  185. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  186. {0, 0x00, 0x00}, { {2, 0x00, 0x00}, {3, 0x80, 0x00} },
  187. &plx_pci_reset_marathon_pcie
  188. /* based on PEX8311 */
  189. };
  190. static struct plx_pci_card_info plx_pci_card_info_tews = {
  191. "TEWS TECHNOLOGIES TPMC810", 2,
  192. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  193. {0, 0x00, 0x00}, { {2, 0x000, 0x80}, {2, 0x100, 0x80} },
  194. &plx_pci_reset_common
  195. /* based on PLX9030 */
  196. };
  197. static struct plx_pci_card_info plx_pci_card_info_cti = {
  198. "Connect Tech Inc. CANpro/104-Plus Opto (CRG001)", 2,
  199. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  200. {0, 0x00, 0x00}, { {2, 0x000, 0x80}, {2, 0x100, 0x80} },
  201. &plx_pci_reset_common
  202. /* based on PLX9030 */
  203. };
  204. static struct plx_pci_card_info plx_pci_card_info_elcus = {
  205. "Eclus CAN-200-PCI", 2,
  206. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  207. {1, 0x00, 0x00}, { {2, 0x00, 0x80}, {3, 0x00, 0x80} },
  208. &plx_pci_reset_common
  209. /* based on PLX9030 */
  210. };
  211. static struct plx_pci_card_info plx_pci_card_info_moxa = {
  212. "MOXA", 2,
  213. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  214. {0, 0x00, 0x00}, { {0, 0x00, 0x80}, {1, 0x00, 0x80} },
  215. &plx_pci_reset_common
  216. /* based on PLX9052 */
  217. };
  218. static struct plx_pci_card_info plx_pci_card_info_asem_dual_can = {
  219. "ASEM Dual CAN raw PCI", 2,
  220. PLX_PCI_CAN_CLOCK, ASEM_PCI_OCR, PLX_PCI_CDR,
  221. {0, 0x00, 0x00}, { {2, 0x00, 0x00}, {4, 0x00, 0x00} },
  222. &plx_pci_reset_asem_dual_can_raw
  223. /* based on PLX9030 */
  224. };
  225. static const struct pci_device_id plx_pci_tbl[] = {
  226. {
  227. /* Adlink PCI-7841/cPCI-7841 */
  228. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  229. PCI_ANY_ID, PCI_ANY_ID,
  230. PCI_CLASS_NETWORK_OTHER << 8, ~0,
  231. (kernel_ulong_t)&plx_pci_card_info_adlink
  232. },
  233. {
  234. /* Adlink PCI-7841/cPCI-7841 SE */
  235. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  236. PCI_ANY_ID, PCI_ANY_ID,
  237. PCI_CLASS_COMMUNICATION_OTHER << 8, ~0,
  238. (kernel_ulong_t)&plx_pci_card_info_adlink_se
  239. },
  240. {
  241. /* esd CAN-PCI/200 */
  242. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
  243. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI200,
  244. 0, 0,
  245. (kernel_ulong_t)&plx_pci_card_info_esd200
  246. },
  247. {
  248. /* esd CAN-CPCI/200 */
  249. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  250. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_CPCI200,
  251. 0, 0,
  252. (kernel_ulong_t)&plx_pci_card_info_esd200
  253. },
  254. {
  255. /* esd CAN-PCI104/200 */
  256. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  257. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI104200,
  258. 0, 0,
  259. (kernel_ulong_t)&plx_pci_card_info_esd200
  260. },
  261. {
  262. /* esd CAN-PCI/266 */
  263. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  264. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCI266,
  265. 0, 0,
  266. (kernel_ulong_t)&plx_pci_card_info_esd266
  267. },
  268. {
  269. /* esd CAN-PMC/266 */
  270. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  271. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PMC266,
  272. 0, 0,
  273. (kernel_ulong_t)&plx_pci_card_info_esd266
  274. },
  275. {
  276. /* esd CAN-PCIE/2000 */
  277. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9056,
  278. PCI_VENDOR_ID_ESDGMBH, ESD_PCI_SUB_SYS_ID_PCIE2000,
  279. 0, 0,
  280. (kernel_ulong_t)&plx_pci_card_info_esd2000
  281. },
  282. {
  283. /* IXXAT PC-I 04/PCI card */
  284. IXXAT_PCI_VENDOR_ID, IXXAT_PCI_DEVICE_ID,
  285. PCI_ANY_ID, IXXAT_PCI_SUB_SYS_ID,
  286. 0, 0,
  287. (kernel_ulong_t)&plx_pci_card_info_ixxat
  288. },
  289. {
  290. /* Marathon CAN-bus-PCI card */
  291. PCI_VENDOR_ID_PLX, MARATHON_PCI_DEVICE_ID,
  292. PCI_ANY_ID, PCI_ANY_ID,
  293. 0, 0,
  294. (kernel_ulong_t)&plx_pci_card_info_marathon_pci
  295. },
  296. {
  297. /* Marathon CAN-bus-PCIe card */
  298. PCI_VENDOR_ID_PLX, MARATHON_PCIE_DEVICE_ID,
  299. PCI_ANY_ID, PCI_ANY_ID,
  300. 0, 0,
  301. (kernel_ulong_t)&plx_pci_card_info_marathon_pcie
  302. },
  303. {
  304. /* TEWS TECHNOLOGIES TPMC810 card */
  305. TEWS_PCI_VENDOR_ID, TEWS_PCI_DEVICE_ID_TMPC810,
  306. PCI_ANY_ID, PCI_ANY_ID,
  307. 0, 0,
  308. (kernel_ulong_t)&plx_pci_card_info_tews
  309. },
  310. {
  311. /* Connect Tech Inc. CANpro/104-Plus Opto (CRG001) card */
  312. PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
  313. PCI_SUBVENDOR_ID_CONNECT_TECH, CTI_PCI_DEVICE_ID_CRG001,
  314. 0, 0,
  315. (kernel_ulong_t)&plx_pci_card_info_cti
  316. },
  317. {
  318. /* Elcus CAN-200-PCI */
  319. CAN200PCI_VENDOR_ID, CAN200PCI_DEVICE_ID,
  320. CAN200PCI_SUB_VENDOR_ID, CAN200PCI_SUB_DEVICE_ID,
  321. 0, 0,
  322. (kernel_ulong_t)&plx_pci_card_info_elcus
  323. },
  324. {
  325. /* moxa */
  326. MOXA_PCI_VENDOR_ID, MOXA_PCI_DEVICE_ID,
  327. PCI_ANY_ID, PCI_ANY_ID,
  328. 0, 0,
  329. (kernel_ulong_t)&plx_pci_card_info_moxa
  330. },
  331. {
  332. /* ASEM Dual CAN raw */
  333. ASEM_RAW_CAN_VENDOR_ID, ASEM_RAW_CAN_DEVICE_ID,
  334. ASEM_RAW_CAN_SUB_VENDOR_ID, ASEM_RAW_CAN_SUB_DEVICE_ID,
  335. 0, 0,
  336. (kernel_ulong_t)&plx_pci_card_info_asem_dual_can
  337. },
  338. {
  339. /* ASEM Dual CAN raw -new model */
  340. ASEM_RAW_CAN_VENDOR_ID, ASEM_RAW_CAN_DEVICE_ID,
  341. ASEM_RAW_CAN_SUB_VENDOR_ID, ASEM_RAW_CAN_SUB_DEVICE_ID_BIS,
  342. 0, 0,
  343. (kernel_ulong_t)&plx_pci_card_info_asem_dual_can
  344. },
  345. { 0,}
  346. };
  347. MODULE_DEVICE_TABLE(pci, plx_pci_tbl);
  348. static u8 plx_pci_read_reg(const struct sja1000_priv *priv, int port)
  349. {
  350. return ioread8(priv->reg_base + port);
  351. }
  352. static void plx_pci_write_reg(const struct sja1000_priv *priv, int port, u8 val)
  353. {
  354. iowrite8(val, priv->reg_base + port);
  355. }
  356. /*
  357. * Check if a CAN controller is present at the specified location
  358. * by trying to switch 'em from the Basic mode into the PeliCAN mode.
  359. * Also check states of some registers in reset mode.
  360. */
  361. static inline int plx_pci_check_sja1000(const struct sja1000_priv *priv)
  362. {
  363. int flag = 0;
  364. /*
  365. * Check registers after hardware reset (the Basic mode)
  366. * See states on p. 10 of the Datasheet.
  367. */
  368. if ((priv->read_reg(priv, REG_CR) & REG_CR_BASICCAN_INITIAL_MASK) ==
  369. REG_CR_BASICCAN_INITIAL &&
  370. (priv->read_reg(priv, SJA1000_SR) == REG_SR_BASICCAN_INITIAL) &&
  371. (priv->read_reg(priv, SJA1000_IR) == REG_IR_BASICCAN_INITIAL))
  372. flag = 1;
  373. /* Bring the SJA1000 into the PeliCAN mode*/
  374. priv->write_reg(priv, SJA1000_CDR, CDR_PELICAN);
  375. /*
  376. * Check registers after reset in the PeliCAN mode.
  377. * See states on p. 23 of the Datasheet.
  378. */
  379. if (priv->read_reg(priv, SJA1000_MOD) == REG_MOD_PELICAN_INITIAL &&
  380. priv->read_reg(priv, SJA1000_SR) == REG_SR_PELICAN_INITIAL &&
  381. priv->read_reg(priv, SJA1000_IR) == REG_IR_PELICAN_INITIAL)
  382. return flag;
  383. return 0;
  384. }
  385. /*
  386. * PLX9030/50/52 software reset
  387. * Also LRESET# asserts and brings to reset device on the Local Bus (if wired).
  388. * For most cards it's enough for reset the SJA1000 chips.
  389. */
  390. static void plx_pci_reset_common(struct pci_dev *pdev)
  391. {
  392. struct plx_pci_card *card = pci_get_drvdata(pdev);
  393. u32 cntrl;
  394. cntrl = ioread32(card->conf_addr + PLX_CNTRL);
  395. cntrl |= PLX_PCI_RESET;
  396. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  397. udelay(100);
  398. cntrl ^= PLX_PCI_RESET;
  399. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  400. };
  401. /*
  402. * PLX9056 software reset
  403. * Assert LRESET# and reset device(s) on the Local Bus (if wired).
  404. */
  405. static void plx9056_pci_reset_common(struct pci_dev *pdev)
  406. {
  407. struct plx_pci_card *card = pci_get_drvdata(pdev);
  408. u32 cntrl;
  409. /* issue a local bus reset */
  410. cntrl = ioread32(card->conf_addr + PLX9056_CNTRL);
  411. cntrl |= PLX_PCI_RESET;
  412. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  413. udelay(100);
  414. cntrl ^= PLX_PCI_RESET;
  415. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  416. /* reload local configuration from EEPROM */
  417. cntrl |= PLX9056_PCI_RCR;
  418. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  419. /*
  420. * There is no safe way to poll for the end
  421. * of reconfiguration process. Waiting for 10ms
  422. * is safe.
  423. */
  424. mdelay(10);
  425. cntrl ^= PLX9056_PCI_RCR;
  426. iowrite32(cntrl, card->conf_addr + PLX9056_CNTRL);
  427. };
  428. /* Special reset function for Marathon CAN-bus-PCI card */
  429. static void plx_pci_reset_marathon_pci(struct pci_dev *pdev)
  430. {
  431. void __iomem *reset_addr;
  432. int i;
  433. static const int reset_bar[2] = {3, 5};
  434. plx_pci_reset_common(pdev);
  435. for (i = 0; i < 2; i++) {
  436. reset_addr = pci_iomap(pdev, reset_bar[i], 0);
  437. if (!reset_addr) {
  438. dev_err(&pdev->dev, "Failed to remap reset "
  439. "space %d (BAR%d)\n", i, reset_bar[i]);
  440. } else {
  441. /* reset the SJA1000 chip */
  442. iowrite8(0x1, reset_addr);
  443. udelay(100);
  444. pci_iounmap(pdev, reset_addr);
  445. }
  446. }
  447. }
  448. /* Special reset function for Marathon CAN-bus-PCIe card */
  449. static void plx_pci_reset_marathon_pcie(struct pci_dev *pdev)
  450. {
  451. void __iomem *addr;
  452. void __iomem *reset_addr;
  453. int i;
  454. plx9056_pci_reset_common(pdev);
  455. for (i = 0; i < 2; i++) {
  456. struct plx_pci_channel_map *chan_map =
  457. &plx_pci_card_info_marathon_pcie.chan_map_tbl[i];
  458. addr = pci_iomap(pdev, chan_map->bar, chan_map->size);
  459. if (!addr) {
  460. dev_err(&pdev->dev, "Failed to remap reset "
  461. "space %d (BAR%d)\n", i, chan_map->bar);
  462. } else {
  463. /* reset the SJA1000 chip */
  464. #define MARATHON_PCIE_RESET_OFFSET 32
  465. reset_addr = addr + chan_map->offset +
  466. MARATHON_PCIE_RESET_OFFSET;
  467. iowrite8(0x1, reset_addr);
  468. udelay(100);
  469. pci_iounmap(pdev, addr);
  470. }
  471. }
  472. }
  473. /* Special reset function for ASEM Dual CAN raw card */
  474. static void plx_pci_reset_asem_dual_can_raw(struct pci_dev *pdev)
  475. {
  476. void __iomem *bar0_addr;
  477. u8 tmpval;
  478. plx_pci_reset_common(pdev);
  479. bar0_addr = pci_iomap(pdev, 0, 0);
  480. if (!bar0_addr) {
  481. dev_err(&pdev->dev, "Failed to remap reset space 0 (BAR0)\n");
  482. return;
  483. }
  484. /* reset the two SJA1000 chips */
  485. tmpval = ioread8(bar0_addr + ASEM_RAW_CAN_RST_REGISTER);
  486. tmpval &= ~(ASEM_RAW_CAN_RST_MASK_CAN1 | ASEM_RAW_CAN_RST_MASK_CAN2);
  487. iowrite8(tmpval, bar0_addr + ASEM_RAW_CAN_RST_REGISTER);
  488. usleep_range(300, 400);
  489. tmpval |= ASEM_RAW_CAN_RST_MASK_CAN1 | ASEM_RAW_CAN_RST_MASK_CAN2;
  490. iowrite8(tmpval, bar0_addr + ASEM_RAW_CAN_RST_REGISTER);
  491. usleep_range(300, 400);
  492. pci_iounmap(pdev, bar0_addr);
  493. }
  494. static void plx_pci_del_card(struct pci_dev *pdev)
  495. {
  496. struct plx_pci_card *card = pci_get_drvdata(pdev);
  497. struct net_device *dev;
  498. struct sja1000_priv *priv;
  499. int i = 0;
  500. for (i = 0; i < PLX_PCI_MAX_CHAN; i++) {
  501. dev = card->net_dev[i];
  502. if (!dev)
  503. continue;
  504. dev_info(&pdev->dev, "Removing %s\n", dev->name);
  505. unregister_sja1000dev(dev);
  506. priv = netdev_priv(dev);
  507. if (priv->reg_base)
  508. pci_iounmap(pdev, priv->reg_base);
  509. free_sja1000dev(dev);
  510. }
  511. card->reset_func(pdev);
  512. /*
  513. * Disable interrupts from PCI-card and disable local
  514. * interrupts
  515. */
  516. if (pdev->device != PCI_DEVICE_ID_PLX_9056 &&
  517. pdev->device != MARATHON_PCIE_DEVICE_ID)
  518. iowrite32(0x0, card->conf_addr + PLX_INTCSR);
  519. else
  520. iowrite32(0x0, card->conf_addr + PLX9056_INTCSR);
  521. if (card->conf_addr)
  522. pci_iounmap(pdev, card->conf_addr);
  523. kfree(card);
  524. pci_disable_device(pdev);
  525. }
  526. /*
  527. * Probe PLX90xx based device for the SJA1000 chips and register each
  528. * available CAN channel to SJA1000 Socket-CAN subsystem.
  529. */
  530. static int plx_pci_add_card(struct pci_dev *pdev,
  531. const struct pci_device_id *ent)
  532. {
  533. struct sja1000_priv *priv;
  534. struct net_device *dev;
  535. struct plx_pci_card *card;
  536. struct plx_pci_card_info *ci;
  537. int err, i;
  538. u32 val;
  539. void __iomem *addr;
  540. ci = (struct plx_pci_card_info *)ent->driver_data;
  541. if (pci_enable_device(pdev) < 0) {
  542. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  543. return -ENODEV;
  544. }
  545. dev_info(&pdev->dev, "Detected \"%s\" card at slot #%i\n",
  546. ci->name, PCI_SLOT(pdev->devfn));
  547. /* Allocate card structures to hold addresses, ... */
  548. card = kzalloc(sizeof(*card), GFP_KERNEL);
  549. if (!card) {
  550. pci_disable_device(pdev);
  551. return -ENOMEM;
  552. }
  553. pci_set_drvdata(pdev, card);
  554. card->channels = 0;
  555. /* Remap PLX90xx configuration space */
  556. addr = pci_iomap(pdev, ci->conf_map.bar, ci->conf_map.size);
  557. if (!addr) {
  558. err = -ENOMEM;
  559. dev_err(&pdev->dev, "Failed to remap configuration space "
  560. "(BAR%d)\n", ci->conf_map.bar);
  561. goto failure_cleanup;
  562. }
  563. card->conf_addr = addr + ci->conf_map.offset;
  564. ci->reset_func(pdev);
  565. card->reset_func = ci->reset_func;
  566. /* Detect available channels */
  567. for (i = 0; i < ci->channel_count; i++) {
  568. struct plx_pci_channel_map *cm = &ci->chan_map_tbl[i];
  569. dev = alloc_sja1000dev(0);
  570. if (!dev) {
  571. err = -ENOMEM;
  572. goto failure_cleanup;
  573. }
  574. card->net_dev[i] = dev;
  575. priv = netdev_priv(dev);
  576. priv->priv = card;
  577. priv->irq_flags = IRQF_SHARED;
  578. dev->irq = pdev->irq;
  579. /*
  580. * Remap IO space of the SJA1000 chips
  581. * This is device-dependent mapping
  582. */
  583. addr = pci_iomap(pdev, cm->bar, cm->size);
  584. if (!addr) {
  585. err = -ENOMEM;
  586. dev_err(&pdev->dev, "Failed to remap BAR%d\n", cm->bar);
  587. goto failure_cleanup;
  588. }
  589. priv->reg_base = addr + cm->offset;
  590. priv->read_reg = plx_pci_read_reg;
  591. priv->write_reg = plx_pci_write_reg;
  592. /* Check if channel is present */
  593. if (plx_pci_check_sja1000(priv)) {
  594. priv->can.clock.freq = ci->can_clock;
  595. priv->ocr = ci->ocr;
  596. priv->cdr = ci->cdr;
  597. SET_NETDEV_DEV(dev, &pdev->dev);
  598. dev->dev_id = i;
  599. /* Register SJA1000 device */
  600. err = register_sja1000dev(dev);
  601. if (err) {
  602. dev_err(&pdev->dev, "Registering device failed "
  603. "(err=%d)\n", err);
  604. goto failure_cleanup;
  605. }
  606. card->channels++;
  607. dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d "
  608. "registered as %s\n", i + 1, priv->reg_base,
  609. dev->irq, dev->name);
  610. } else {
  611. dev_err(&pdev->dev, "Channel #%d not detected\n",
  612. i + 1);
  613. free_sja1000dev(dev);
  614. card->net_dev[i] = NULL;
  615. }
  616. }
  617. if (!card->channels) {
  618. err = -ENODEV;
  619. goto failure_cleanup;
  620. }
  621. /*
  622. * Enable interrupts from PCI-card (PLX90xx) and enable Local_1,
  623. * Local_2 interrupts from the SJA1000 chips
  624. */
  625. if (pdev->device != PCI_DEVICE_ID_PLX_9056 &&
  626. pdev->device != MARATHON_PCIE_DEVICE_ID) {
  627. val = ioread32(card->conf_addr + PLX_INTCSR);
  628. if (pdev->subsystem_vendor == PCI_VENDOR_ID_ESDGMBH)
  629. val |= PLX_LINT1_EN | PLX_PCI_INT_EN;
  630. else
  631. val |= PLX_LINT1_EN | PLX_LINT2_EN | PLX_PCI_INT_EN;
  632. iowrite32(val, card->conf_addr + PLX_INTCSR);
  633. } else {
  634. iowrite32(PLX9056_LINTI | PLX9056_PCI_INT_EN,
  635. card->conf_addr + PLX9056_INTCSR);
  636. }
  637. return 0;
  638. failure_cleanup:
  639. dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
  640. plx_pci_del_card(pdev);
  641. return err;
  642. }
  643. static struct pci_driver plx_pci_driver = {
  644. .name = DRV_NAME,
  645. .id_table = plx_pci_tbl,
  646. .probe = plx_pci_add_card,
  647. .remove = plx_pci_del_card,
  648. };
  649. module_pci_driver(plx_pci_driver);