plx_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (C) 2008-2010 Pavel Cheblakov <P.B.Cheblakov@inp.nsk.su>
  3. *
  4. * Derived from the ems_pci.c driver:
  5. * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  6. * Copyright (C) 2008 Markus Plessing <plessing@ems-wuensche.com>
  7. * Copyright (C) 2008 Sebastian Haas <haas@ems-wuensche.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the version 2 of the GNU General Public License
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/pci.h>
  29. #include <linux/can.h>
  30. #include <linux/can/dev.h>
  31. #include <linux/io.h>
  32. #include "sja1000.h"
  33. #define DRV_NAME "sja1000_plx_pci"
  34. MODULE_AUTHOR("Pavel Cheblakov <P.B.Cheblakov@inp.nsk.su>");
  35. MODULE_DESCRIPTION("Socket-CAN driver for PLX90xx PCI-bridge cards with "
  36. "the SJA1000 chips");
  37. MODULE_SUPPORTED_DEVICE("Adlink PCI-7841/cPCI-7841, "
  38. "Adlink PCI-7841/cPCI-7841 SE, "
  39. "Marathon CAN-bus-PCI, "
  40. "TEWS TECHNOLOGIES TPMC810");
  41. MODULE_LICENSE("GPL v2");
  42. #define PLX_PCI_MAX_CHAN 2
  43. struct plx_pci_card {
  44. int channels; /* detected channels count */
  45. struct net_device *net_dev[PLX_PCI_MAX_CHAN];
  46. void __iomem *conf_addr;
  47. };
  48. #define PLX_PCI_CAN_CLOCK (16000000 / 2)
  49. /* PLX90xx registers */
  50. #define PLX_INTCSR 0x4c /* Interrupt Control/Status */
  51. #define PLX_CNTRL 0x50 /* User I/O, Direct Slave Response,
  52. * Serial EEPROM, and Initialization
  53. * Control register
  54. */
  55. #define PLX_LINT1_EN 0x1 /* Local interrupt 1 enable */
  56. #define PLX_LINT2_EN (1 << 3) /* Local interrupt 2 enable */
  57. #define PLX_PCI_INT_EN (1 << 6) /* PCI Interrupt Enable */
  58. #define PLX_PCI_RESET (1 << 30) /* PCI Adapter Software Reset */
  59. /*
  60. * The board configuration is probably following:
  61. * RX1 is connected to ground.
  62. * TX1 is not connected.
  63. * CLKO is not connected.
  64. * Setting the OCR register to 0xDA is a good idea.
  65. * This means normal output mode, push-pull and the correct polarity.
  66. */
  67. #define PLX_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
  68. /*
  69. * In the CDR register, you should set CBP to 1.
  70. * You will probably also want to set the clock divider value to 7
  71. * (meaning direct oscillator output) because the second SJA1000 chip
  72. * is driven by the first one CLKOUT output.
  73. */
  74. #define PLX_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  75. /* SJA1000 Control Register in the BasicCAN Mode */
  76. #define REG_CR 0x00
  77. /* States of some SJA1000 registers after hardware reset in the BasicCAN mode*/
  78. #define REG_CR_BASICCAN_INITIAL 0x21
  79. #define REG_CR_BASICCAN_INITIAL_MASK 0xa1
  80. #define REG_SR_BASICCAN_INITIAL 0x0c
  81. #define REG_IR_BASICCAN_INITIAL 0xe0
  82. /* States of some SJA1000 registers after hardware reset in the PeliCAN mode*/
  83. #define REG_MOD_PELICAN_INITIAL 0x01
  84. #define REG_SR_PELICAN_INITIAL 0x3c
  85. #define REG_IR_PELICAN_INITIAL 0x00
  86. #define ADLINK_PCI_VENDOR_ID 0x144A
  87. #define ADLINK_PCI_DEVICE_ID 0x7841
  88. #define MARATHON_PCI_DEVICE_ID 0x2715
  89. #define TEWS_PCI_VENDOR_ID 0x1498
  90. #define TEWS_PCI_DEVICE_ID_TMPC810 0x032A
  91. static void plx_pci_reset_common(struct pci_dev *pdev);
  92. static void plx_pci_reset_marathon(struct pci_dev *pdev);
  93. struct plx_pci_channel_map {
  94. u32 bar;
  95. u32 offset;
  96. u32 size; /* 0x00 - auto, e.g. length of entire bar */
  97. };
  98. struct plx_pci_card_info {
  99. const char *name;
  100. int channel_count;
  101. u32 can_clock;
  102. u8 ocr; /* output control register */
  103. u8 cdr; /* clock divider register */
  104. /* Parameters for mapping local configuration space */
  105. struct plx_pci_channel_map conf_map;
  106. /* Parameters for mapping the SJA1000 chips */
  107. struct plx_pci_channel_map chan_map_tbl[PLX_PCI_MAX_CHAN];
  108. /* Pointer to device-dependent reset function */
  109. void (*reset_func)(struct pci_dev *pdev);
  110. };
  111. static struct plx_pci_card_info plx_pci_card_info_adlink __devinitdata = {
  112. "Adlink PCI-7841/cPCI-7841", 2,
  113. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  114. {1, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  115. &plx_pci_reset_common
  116. /* based on PLX9052 */
  117. };
  118. static struct plx_pci_card_info plx_pci_card_info_adlink_se __devinitdata = {
  119. "Adlink PCI-7841/cPCI-7841 SE", 2,
  120. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  121. {0, 0x00, 0x00}, { {2, 0x00, 0x80}, {2, 0x80, 0x80} },
  122. &plx_pci_reset_common
  123. /* based on PLX9052 */
  124. };
  125. static struct plx_pci_card_info plx_pci_card_info_marathon __devinitdata = {
  126. "Marathon CAN-bus-PCI", 2,
  127. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  128. {0, 0x00, 0x00}, { {2, 0x00, 0x00}, {4, 0x00, 0x00} },
  129. &plx_pci_reset_marathon
  130. /* based on PLX9052 */
  131. };
  132. static struct plx_pci_card_info plx_pci_card_info_tews __devinitdata = {
  133. "TEWS TECHNOLOGIES TPMC810", 2,
  134. PLX_PCI_CAN_CLOCK, PLX_PCI_OCR, PLX_PCI_CDR,
  135. {0, 0x00, 0x00}, { {2, 0x000, 0x80}, {2, 0x100, 0x80} },
  136. &plx_pci_reset_common
  137. /* based on PLX9030 */
  138. };
  139. static DEFINE_PCI_DEVICE_TABLE(plx_pci_tbl) = {
  140. {
  141. /* Adlink PCI-7841/cPCI-7841 */
  142. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  143. PCI_ANY_ID, PCI_ANY_ID,
  144. PCI_CLASS_NETWORK_OTHER << 8, ~0,
  145. (kernel_ulong_t)&plx_pci_card_info_adlink
  146. },
  147. {
  148. /* Adlink PCI-7841/cPCI-7841 SE */
  149. ADLINK_PCI_VENDOR_ID, ADLINK_PCI_DEVICE_ID,
  150. PCI_ANY_ID, PCI_ANY_ID,
  151. PCI_CLASS_COMMUNICATION_OTHER << 8, ~0,
  152. (kernel_ulong_t)&plx_pci_card_info_adlink_se
  153. },
  154. {
  155. /* Marathon CAN-bus-PCI card */
  156. PCI_VENDOR_ID_PLX, MARATHON_PCI_DEVICE_ID,
  157. PCI_ANY_ID, PCI_ANY_ID,
  158. 0, 0,
  159. (kernel_ulong_t)&plx_pci_card_info_marathon
  160. },
  161. {
  162. /* TEWS TECHNOLOGIES TPMC810 card */
  163. TEWS_PCI_VENDOR_ID, TEWS_PCI_DEVICE_ID_TMPC810,
  164. PCI_ANY_ID, PCI_ANY_ID,
  165. 0, 0,
  166. (kernel_ulong_t)&plx_pci_card_info_tews
  167. },
  168. { 0,}
  169. };
  170. MODULE_DEVICE_TABLE(pci, plx_pci_tbl);
  171. static u8 plx_pci_read_reg(const struct sja1000_priv *priv, int port)
  172. {
  173. return ioread8(priv->reg_base + port);
  174. }
  175. static void plx_pci_write_reg(const struct sja1000_priv *priv, int port, u8 val)
  176. {
  177. iowrite8(val, priv->reg_base + port);
  178. }
  179. /*
  180. * Check if a CAN controller is present at the specified location
  181. * by trying to switch 'em from the Basic mode into the PeliCAN mode.
  182. * Also check states of some registers in reset mode.
  183. */
  184. static inline int plx_pci_check_sja1000(const struct sja1000_priv *priv)
  185. {
  186. int flag = 0;
  187. /*
  188. * Check registers after hardware reset (the Basic mode)
  189. * See states on p. 10 of the Datasheet.
  190. */
  191. if ((priv->read_reg(priv, REG_CR) & REG_CR_BASICCAN_INITIAL_MASK) ==
  192. REG_CR_BASICCAN_INITIAL &&
  193. (priv->read_reg(priv, REG_SR) == REG_SR_BASICCAN_INITIAL) &&
  194. (priv->read_reg(priv, REG_IR) == REG_IR_BASICCAN_INITIAL))
  195. flag = 1;
  196. /* Bring the SJA1000 into the PeliCAN mode*/
  197. priv->write_reg(priv, REG_CDR, CDR_PELICAN);
  198. /*
  199. * Check registers after reset in the PeliCAN mode.
  200. * See states on p. 23 of the Datasheet.
  201. */
  202. if (priv->read_reg(priv, REG_MOD) == REG_MOD_PELICAN_INITIAL &&
  203. priv->read_reg(priv, REG_SR) == REG_SR_PELICAN_INITIAL &&
  204. priv->read_reg(priv, REG_IR) == REG_IR_PELICAN_INITIAL)
  205. return flag;
  206. return 0;
  207. }
  208. /*
  209. * PLX90xx software reset
  210. * Also LRESET# asserts and brings to reset device on the Local Bus (if wired).
  211. * For most cards it's enough for reset the SJA1000 chips.
  212. */
  213. static void plx_pci_reset_common(struct pci_dev *pdev)
  214. {
  215. struct plx_pci_card *card = pci_get_drvdata(pdev);
  216. u32 cntrl;
  217. cntrl = ioread32(card->conf_addr + PLX_CNTRL);
  218. cntrl |= PLX_PCI_RESET;
  219. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  220. udelay(100);
  221. cntrl ^= PLX_PCI_RESET;
  222. iowrite32(cntrl, card->conf_addr + PLX_CNTRL);
  223. };
  224. /* Special reset function for Marathon card */
  225. static void plx_pci_reset_marathon(struct pci_dev *pdev)
  226. {
  227. void __iomem *reset_addr;
  228. int i;
  229. int reset_bar[2] = {3, 5};
  230. plx_pci_reset_common(pdev);
  231. for (i = 0; i < 2; i++) {
  232. reset_addr = pci_iomap(pdev, reset_bar[i], 0);
  233. if (!reset_addr) {
  234. dev_err(&pdev->dev, "Failed to remap reset "
  235. "space %d (BAR%d)\n", i, reset_bar[i]);
  236. } else {
  237. /* reset the SJA1000 chip */
  238. iowrite8(0x1, reset_addr);
  239. udelay(100);
  240. pci_iounmap(pdev, reset_addr);
  241. }
  242. }
  243. }
  244. static void plx_pci_del_card(struct pci_dev *pdev)
  245. {
  246. struct plx_pci_card *card = pci_get_drvdata(pdev);
  247. struct net_device *dev;
  248. struct sja1000_priv *priv;
  249. int i = 0;
  250. for (i = 0; i < card->channels; i++) {
  251. dev = card->net_dev[i];
  252. if (!dev)
  253. continue;
  254. dev_info(&pdev->dev, "Removing %s\n", dev->name);
  255. unregister_sja1000dev(dev);
  256. priv = netdev_priv(dev);
  257. if (priv->reg_base)
  258. pci_iounmap(pdev, priv->reg_base);
  259. free_sja1000dev(dev);
  260. }
  261. plx_pci_reset_common(pdev);
  262. /*
  263. * Disable interrupts from PCI-card (PLX90xx) and disable Local_1,
  264. * Local_2 interrupts
  265. */
  266. iowrite32(0x0, card->conf_addr + PLX_INTCSR);
  267. if (card->conf_addr)
  268. pci_iounmap(pdev, card->conf_addr);
  269. kfree(card);
  270. pci_disable_device(pdev);
  271. pci_set_drvdata(pdev, NULL);
  272. }
  273. /*
  274. * Probe PLX90xx based device for the SJA1000 chips and register each
  275. * available CAN channel to SJA1000 Socket-CAN subsystem.
  276. */
  277. static int __devinit plx_pci_add_card(struct pci_dev *pdev,
  278. const struct pci_device_id *ent)
  279. {
  280. struct sja1000_priv *priv;
  281. struct net_device *dev;
  282. struct plx_pci_card *card;
  283. struct plx_pci_card_info *ci;
  284. int err, i;
  285. u32 val;
  286. void __iomem *addr;
  287. ci = (struct plx_pci_card_info *)ent->driver_data;
  288. if (pci_enable_device(pdev) < 0) {
  289. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  290. return -ENODEV;
  291. }
  292. dev_info(&pdev->dev, "Detected \"%s\" card at slot #%i\n",
  293. ci->name, PCI_SLOT(pdev->devfn));
  294. /* Allocate card structures to hold addresses, ... */
  295. card = kzalloc(sizeof(*card), GFP_KERNEL);
  296. if (!card) {
  297. dev_err(&pdev->dev, "Unable to allocate memory\n");
  298. pci_disable_device(pdev);
  299. return -ENOMEM;
  300. }
  301. pci_set_drvdata(pdev, card);
  302. card->channels = 0;
  303. /* Remap PLX90xx configuration space */
  304. addr = pci_iomap(pdev, ci->conf_map.bar, ci->conf_map.size);
  305. if (!addr) {
  306. err = -ENOMEM;
  307. dev_err(&pdev->dev, "Failed to remap configuration space "
  308. "(BAR%d)\n", ci->conf_map.bar);
  309. goto failure_cleanup;
  310. }
  311. card->conf_addr = addr + ci->conf_map.offset;
  312. ci->reset_func(pdev);
  313. /* Detect available channels */
  314. for (i = 0; i < ci->channel_count; i++) {
  315. struct plx_pci_channel_map *cm = &ci->chan_map_tbl[i];
  316. dev = alloc_sja1000dev(0);
  317. if (!dev) {
  318. err = -ENOMEM;
  319. goto failure_cleanup;
  320. }
  321. card->net_dev[i] = dev;
  322. priv = netdev_priv(dev);
  323. priv->priv = card;
  324. priv->irq_flags = IRQF_SHARED;
  325. dev->irq = pdev->irq;
  326. /*
  327. * Remap IO space of the SJA1000 chips
  328. * This is device-dependent mapping
  329. */
  330. addr = pci_iomap(pdev, cm->bar, cm->size);
  331. if (!addr) {
  332. err = -ENOMEM;
  333. dev_err(&pdev->dev, "Failed to remap BAR%d\n", cm->bar);
  334. goto failure_cleanup;
  335. }
  336. priv->reg_base = addr + cm->offset;
  337. priv->read_reg = plx_pci_read_reg;
  338. priv->write_reg = plx_pci_write_reg;
  339. /* Check if channel is present */
  340. if (plx_pci_check_sja1000(priv)) {
  341. priv->can.clock.freq = ci->can_clock;
  342. priv->ocr = ci->ocr;
  343. priv->cdr = ci->cdr;
  344. SET_NETDEV_DEV(dev, &pdev->dev);
  345. /* Register SJA1000 device */
  346. err = register_sja1000dev(dev);
  347. if (err) {
  348. dev_err(&pdev->dev, "Registering device failed "
  349. "(err=%d)\n", err);
  350. free_sja1000dev(dev);
  351. goto failure_cleanup;
  352. }
  353. card->channels++;
  354. dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d "
  355. "registered as %s\n", i + 1, priv->reg_base,
  356. dev->irq, dev->name);
  357. } else {
  358. dev_err(&pdev->dev, "Channel #%d not detected\n",
  359. i + 1);
  360. free_sja1000dev(dev);
  361. }
  362. }
  363. if (!card->channels) {
  364. err = -ENODEV;
  365. goto failure_cleanup;
  366. }
  367. /*
  368. * Enable interrupts from PCI-card (PLX90xx) and enable Local_1,
  369. * Local_2 interrupts from the SJA1000 chips
  370. */
  371. val = ioread32(card->conf_addr + PLX_INTCSR);
  372. val |= PLX_LINT1_EN | PLX_LINT2_EN | PLX_PCI_INT_EN;
  373. iowrite32(val, card->conf_addr + PLX_INTCSR);
  374. return 0;
  375. failure_cleanup:
  376. dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
  377. plx_pci_del_card(pdev);
  378. return err;
  379. }
  380. static struct pci_driver plx_pci_driver = {
  381. .name = DRV_NAME,
  382. .id_table = plx_pci_tbl,
  383. .probe = plx_pci_add_card,
  384. .remove = plx_pci_del_card,
  385. };
  386. static int __init plx_pci_init(void)
  387. {
  388. return pci_register_driver(&plx_pci_driver);
  389. }
  390. static void __exit plx_pci_exit(void)
  391. {
  392. pci_unregister_driver(&plx_pci_driver);
  393. }
  394. module_init(plx_pci_init);
  395. module_exit(plx_pci_exit);