peak_pci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
  3. * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
  4. *
  5. * Derived from the PCAN project file driver/src/pcan_pci.c:
  6. *
  7. * Copyright (C) 2001-2006 PEAK System-Technik GmbH
  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. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/delay.h>
  23. #include <linux/pci.h>
  24. #include <linux/io.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-algo-bit.h>
  27. #include <linux/can.h>
  28. #include <linux/can/dev.h>
  29. #include "sja1000.h"
  30. MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
  31. MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI family cards");
  32. MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe/PCIeC miniPCI CAN cards");
  33. MODULE_SUPPORTED_DEVICE("PEAK PCAN miniPCIe/cPCI PC/104+ PCI/104e CAN Cards");
  34. MODULE_LICENSE("GPL v2");
  35. #define DRV_NAME "peak_pci"
  36. struct peak_pciec_card;
  37. struct peak_pci_chan {
  38. void __iomem *cfg_base; /* Common for all channels */
  39. struct net_device *prev_dev; /* Chain of network devices */
  40. u16 icr_mask; /* Interrupt mask for fast ack */
  41. struct peak_pciec_card *pciec_card; /* only for PCIeC LEDs */
  42. };
  43. #define PEAK_PCI_CAN_CLOCK (16000000 / 2)
  44. #define PEAK_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  45. #define PEAK_PCI_OCR OCR_TX0_PUSHPULL
  46. /*
  47. * Important PITA registers
  48. */
  49. #define PITA_ICR 0x00 /* Interrupt control register */
  50. #define PITA_GPIOICR 0x18 /* GPIO interface control register */
  51. #define PITA_MISC 0x1C /* Miscellaneous register */
  52. #define PEAK_PCI_CFG_SIZE 0x1000 /* Size of the config PCI bar */
  53. #define PEAK_PCI_CHAN_SIZE 0x0400 /* Size used by the channel */
  54. #define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */
  55. #define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */
  56. #define PEAK_PCIEC_DEVICE_ID 0x0002 /* for ExpressCard slot cards */
  57. #define PEAK_PCIE_DEVICE_ID 0x0003 /* for nextgen PCIe slot cards */
  58. #define PEAK_CPCI_DEVICE_ID 0x0004 /* for nextgen cPCI slot cards */
  59. #define PEAK_MPCI_DEVICE_ID 0x0005 /* for nextgen miniPCI slot cards */
  60. #define PEAK_PC_104P_DEVICE_ID 0x0006 /* PCAN-PC/104+ cards */
  61. #define PEAK_PCI_104E_DEVICE_ID 0x0007 /* PCAN-PCI/104 Express cards */
  62. #define PEAK_MPCIE_DEVICE_ID 0x0008 /* The miniPCIe slot cards */
  63. #define PEAK_PCI_CHAN_MAX 4
  64. static const u16 peak_pci_icr_masks[PEAK_PCI_CHAN_MAX] = {
  65. 0x02, 0x01, 0x40, 0x80
  66. };
  67. static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl) = {
  68. {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  69. {PEAK_PCI_VENDOR_ID, PEAK_PCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  70. {PEAK_PCI_VENDOR_ID, PEAK_MPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  71. {PEAK_PCI_VENDOR_ID, PEAK_MPCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  72. {PEAK_PCI_VENDOR_ID, PEAK_PC_104P_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  73. {PEAK_PCI_VENDOR_ID, PEAK_PCI_104E_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  74. {PEAK_PCI_VENDOR_ID, PEAK_CPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  75. #ifdef CONFIG_CAN_PEAK_PCIEC
  76. {PEAK_PCI_VENDOR_ID, PEAK_PCIEC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  77. #endif
  78. {0,}
  79. };
  80. MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
  81. #ifdef CONFIG_CAN_PEAK_PCIEC
  82. /*
  83. * PCAN-ExpressCard needs I2C bit-banging configuration option.
  84. */
  85. /* GPIOICR byte access offsets */
  86. #define PITA_GPOUT 0x18 /* GPx output value */
  87. #define PITA_GPIN 0x19 /* GPx input value */
  88. #define PITA_GPOEN 0x1A /* configure GPx as ouput pin */
  89. /* I2C GP bits */
  90. #define PITA_GPIN_SCL 0x01 /* Serial Clock Line */
  91. #define PITA_GPIN_SDA 0x04 /* Serial DAta line */
  92. #define PCA9553_1_SLAVEADDR (0xC4 >> 1)
  93. /* PCA9553 LS0 fields values */
  94. enum {
  95. PCA9553_LOW,
  96. PCA9553_HIGHZ,
  97. PCA9553_PWM0,
  98. PCA9553_PWM1
  99. };
  100. /* LEDs control */
  101. #define PCA9553_ON PCA9553_LOW
  102. #define PCA9553_OFF PCA9553_HIGHZ
  103. #define PCA9553_SLOW PCA9553_PWM0
  104. #define PCA9553_FAST PCA9553_PWM1
  105. #define PCA9553_LED(c) (1 << (c))
  106. #define PCA9553_LED_STATE(s, c) ((s) << ((c) << 1))
  107. #define PCA9553_LED_ON(c) PCA9553_LED_STATE(PCA9553_ON, c)
  108. #define PCA9553_LED_OFF(c) PCA9553_LED_STATE(PCA9553_OFF, c)
  109. #define PCA9553_LED_SLOW(c) PCA9553_LED_STATE(PCA9553_SLOW, c)
  110. #define PCA9553_LED_FAST(c) PCA9553_LED_STATE(PCA9553_FAST, c)
  111. #define PCA9553_LED_MASK(c) PCA9553_LED_STATE(0x03, c)
  112. #define PCA9553_LED_OFF_ALL (PCA9553_LED_OFF(0) | PCA9553_LED_OFF(1))
  113. #define PCA9553_LS0_INIT 0x40 /* initial value (!= from 0x00) */
  114. struct peak_pciec_chan {
  115. struct net_device *netdev;
  116. unsigned long prev_rx_bytes;
  117. unsigned long prev_tx_bytes;
  118. };
  119. struct peak_pciec_card {
  120. void __iomem *cfg_base; /* Common for all channels */
  121. void __iomem *reg_base; /* first channel base address */
  122. u8 led_cache; /* leds state cache */
  123. /* PCIExpressCard i2c data */
  124. struct i2c_algo_bit_data i2c_bit;
  125. struct i2c_adapter led_chip;
  126. struct delayed_work led_work; /* led delayed work */
  127. int chan_count;
  128. struct peak_pciec_chan channel[PEAK_PCI_CHAN_MAX];
  129. };
  130. /* "normal" pci register write callback is overloaded for leds control */
  131. static void peak_pci_write_reg(const struct sja1000_priv *priv,
  132. int port, u8 val);
  133. static inline void pita_set_scl_highz(struct peak_pciec_card *card)
  134. {
  135. u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SCL;
  136. writeb(gp_outen, card->cfg_base + PITA_GPOEN);
  137. }
  138. static inline void pita_set_sda_highz(struct peak_pciec_card *card)
  139. {
  140. u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SDA;
  141. writeb(gp_outen, card->cfg_base + PITA_GPOEN);
  142. }
  143. static void peak_pciec_init_pita_gpio(struct peak_pciec_card *card)
  144. {
  145. /* raise SCL & SDA GPIOs to high-Z */
  146. pita_set_scl_highz(card);
  147. pita_set_sda_highz(card);
  148. }
  149. static void pita_setsda(void *data, int state)
  150. {
  151. struct peak_pciec_card *card = (struct peak_pciec_card *)data;
  152. u8 gp_out, gp_outen;
  153. /* set output sda always to 0 */
  154. gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SDA;
  155. writeb(gp_out, card->cfg_base + PITA_GPOUT);
  156. /* control output sda with GPOEN */
  157. gp_outen = readb(card->cfg_base + PITA_GPOEN);
  158. if (state)
  159. gp_outen &= ~PITA_GPIN_SDA;
  160. else
  161. gp_outen |= PITA_GPIN_SDA;
  162. writeb(gp_outen, card->cfg_base + PITA_GPOEN);
  163. }
  164. static void pita_setscl(void *data, int state)
  165. {
  166. struct peak_pciec_card *card = (struct peak_pciec_card *)data;
  167. u8 gp_out, gp_outen;
  168. /* set output scl always to 0 */
  169. gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SCL;
  170. writeb(gp_out, card->cfg_base + PITA_GPOUT);
  171. /* control output scl with GPOEN */
  172. gp_outen = readb(card->cfg_base + PITA_GPOEN);
  173. if (state)
  174. gp_outen &= ~PITA_GPIN_SCL;
  175. else
  176. gp_outen |= PITA_GPIN_SCL;
  177. writeb(gp_outen, card->cfg_base + PITA_GPOEN);
  178. }
  179. static int pita_getsda(void *data)
  180. {
  181. struct peak_pciec_card *card = (struct peak_pciec_card *)data;
  182. /* set tristate */
  183. pita_set_sda_highz(card);
  184. return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SDA) ? 1 : 0;
  185. }
  186. static int pita_getscl(void *data)
  187. {
  188. struct peak_pciec_card *card = (struct peak_pciec_card *)data;
  189. /* set tristate */
  190. pita_set_scl_highz(card);
  191. return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SCL) ? 1 : 0;
  192. }
  193. /*
  194. * write commands to the LED chip though the I2C-bus of the PCAN-PCIeC
  195. */
  196. static int peak_pciec_write_pca9553(struct peak_pciec_card *card,
  197. u8 offset, u8 data)
  198. {
  199. u8 buffer[2] = {
  200. offset,
  201. data
  202. };
  203. struct i2c_msg msg = {
  204. .addr = PCA9553_1_SLAVEADDR,
  205. .len = 2,
  206. .buf = buffer,
  207. };
  208. int ret;
  209. /* cache led mask */
  210. if ((offset == 5) && (data == card->led_cache))
  211. return 0;
  212. ret = i2c_transfer(&card->led_chip, &msg, 1);
  213. if (ret < 0)
  214. return ret;
  215. if (offset == 5)
  216. card->led_cache = data;
  217. return 0;
  218. }
  219. /*
  220. * delayed work callback used to control the LEDs
  221. */
  222. static void peak_pciec_led_work(struct work_struct *work)
  223. {
  224. struct peak_pciec_card *card =
  225. container_of(work, struct peak_pciec_card, led_work.work);
  226. struct net_device *netdev;
  227. u8 new_led = card->led_cache;
  228. int i, up_count = 0;
  229. /* first check what is to do */
  230. for (i = 0; i < card->chan_count; i++) {
  231. /* default is: not configured */
  232. new_led &= ~PCA9553_LED_MASK(i);
  233. new_led |= PCA9553_LED_ON(i);
  234. netdev = card->channel[i].netdev;
  235. if (!netdev || !(netdev->flags & IFF_UP))
  236. continue;
  237. up_count++;
  238. /* no activity (but configured) */
  239. new_led &= ~PCA9553_LED_MASK(i);
  240. new_led |= PCA9553_LED_SLOW(i);
  241. /* if bytes counters changed, set fast blinking led */
  242. if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) {
  243. card->channel[i].prev_rx_bytes = netdev->stats.rx_bytes;
  244. new_led &= ~PCA9553_LED_MASK(i);
  245. new_led |= PCA9553_LED_FAST(i);
  246. }
  247. if (netdev->stats.tx_bytes != card->channel[i].prev_tx_bytes) {
  248. card->channel[i].prev_tx_bytes = netdev->stats.tx_bytes;
  249. new_led &= ~PCA9553_LED_MASK(i);
  250. new_led |= PCA9553_LED_FAST(i);
  251. }
  252. }
  253. /* check if LS0 settings changed, only update i2c if so */
  254. peak_pciec_write_pca9553(card, 5, new_led);
  255. /* restart timer (except if no more configured channels) */
  256. if (up_count)
  257. schedule_delayed_work(&card->led_work, HZ);
  258. }
  259. /*
  260. * set LEDs blinking state
  261. */
  262. static void peak_pciec_set_leds(struct peak_pciec_card *card, u8 led_mask, u8 s)
  263. {
  264. u8 new_led = card->led_cache;
  265. int i;
  266. /* first check what is to do */
  267. for (i = 0; i < card->chan_count; i++)
  268. if (led_mask & PCA9553_LED(i)) {
  269. new_led &= ~PCA9553_LED_MASK(i);
  270. new_led |= PCA9553_LED_STATE(s, i);
  271. }
  272. /* check if LS0 settings changed, only update i2c if so */
  273. peak_pciec_write_pca9553(card, 5, new_led);
  274. }
  275. /*
  276. * start one second delayed work to control LEDs
  277. */
  278. static void peak_pciec_start_led_work(struct peak_pciec_card *card)
  279. {
  280. if (!delayed_work_pending(&card->led_work))
  281. schedule_delayed_work(&card->led_work, HZ);
  282. }
  283. /*
  284. * stop LEDs delayed work
  285. */
  286. static void peak_pciec_stop_led_work(struct peak_pciec_card *card)
  287. {
  288. cancel_delayed_work_sync(&card->led_work);
  289. }
  290. /*
  291. * initialize the PCA9553 4-bit I2C-bus LED chip
  292. */
  293. static int peak_pciec_init_leds(struct peak_pciec_card *card)
  294. {
  295. int err;
  296. /* prescaler for frequency 0: "SLOW" = 1 Hz = "44" */
  297. err = peak_pciec_write_pca9553(card, 1, 44 / 1);
  298. if (err)
  299. return err;
  300. /* duty cycle 0: 50% */
  301. err = peak_pciec_write_pca9553(card, 2, 0x80);
  302. if (err)
  303. return err;
  304. /* prescaler for frequency 1: "FAST" = 5 Hz */
  305. err = peak_pciec_write_pca9553(card, 3, 44 / 5);
  306. if (err)
  307. return err;
  308. /* duty cycle 1: 50% */
  309. err = peak_pciec_write_pca9553(card, 4, 0x80);
  310. if (err)
  311. return err;
  312. /* switch LEDs to initial state */
  313. return peak_pciec_write_pca9553(card, 5, PCA9553_LS0_INIT);
  314. }
  315. /*
  316. * restore LEDs state to off peak_pciec_leds_exit
  317. */
  318. static void peak_pciec_leds_exit(struct peak_pciec_card *card)
  319. {
  320. /* switch LEDs to off */
  321. peak_pciec_write_pca9553(card, 5, PCA9553_LED_OFF_ALL);
  322. }
  323. /*
  324. * normal write sja1000 register method overloaded to catch when controller
  325. * is started or stopped, to control leds
  326. */
  327. static void peak_pciec_write_reg(const struct sja1000_priv *priv,
  328. int port, u8 val)
  329. {
  330. struct peak_pci_chan *chan = priv->priv;
  331. struct peak_pciec_card *card = chan->pciec_card;
  332. int c = (priv->reg_base - card->reg_base) / PEAK_PCI_CHAN_SIZE;
  333. /* sja1000 register changes control the leds state */
  334. if (port == REG_MOD)
  335. switch (val) {
  336. case MOD_RM:
  337. /* Reset Mode: set led on */
  338. peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_ON);
  339. break;
  340. case 0x00:
  341. /* Normal Mode: led slow blinking and start led timer */
  342. peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_SLOW);
  343. peak_pciec_start_led_work(card);
  344. break;
  345. default:
  346. break;
  347. }
  348. /* call base function */
  349. peak_pci_write_reg(priv, port, val);
  350. }
  351. static struct i2c_algo_bit_data peak_pciec_i2c_bit_ops = {
  352. .setsda = pita_setsda,
  353. .setscl = pita_setscl,
  354. .getsda = pita_getsda,
  355. .getscl = pita_getscl,
  356. .udelay = 10,
  357. .timeout = HZ,
  358. };
  359. static int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
  360. {
  361. struct sja1000_priv *priv = netdev_priv(dev);
  362. struct peak_pci_chan *chan = priv->priv;
  363. struct peak_pciec_card *card;
  364. int err;
  365. /* copy i2c object address from 1st channel */
  366. if (chan->prev_dev) {
  367. struct sja1000_priv *prev_priv = netdev_priv(chan->prev_dev);
  368. struct peak_pci_chan *prev_chan = prev_priv->priv;
  369. card = prev_chan->pciec_card;
  370. if (!card)
  371. return -ENODEV;
  372. /* channel is the first one: do the init part */
  373. } else {
  374. /* create the bit banging I2C adapter structure */
  375. card = kzalloc(sizeof(struct peak_pciec_card), GFP_KERNEL);
  376. if (!card) {
  377. dev_err(&pdev->dev,
  378. "failed allocating memory for i2c chip\n");
  379. return -ENOMEM;
  380. }
  381. card->cfg_base = chan->cfg_base;
  382. card->reg_base = priv->reg_base;
  383. card->led_chip.owner = THIS_MODULE;
  384. card->led_chip.dev.parent = &pdev->dev;
  385. card->led_chip.algo_data = &card->i2c_bit;
  386. strncpy(card->led_chip.name, "peak_i2c",
  387. sizeof(card->led_chip.name));
  388. card->i2c_bit = peak_pciec_i2c_bit_ops;
  389. card->i2c_bit.udelay = 10;
  390. card->i2c_bit.timeout = HZ;
  391. card->i2c_bit.data = card;
  392. peak_pciec_init_pita_gpio(card);
  393. err = i2c_bit_add_bus(&card->led_chip);
  394. if (err) {
  395. dev_err(&pdev->dev, "i2c init failed\n");
  396. goto pciec_init_err_1;
  397. }
  398. err = peak_pciec_init_leds(card);
  399. if (err) {
  400. dev_err(&pdev->dev, "leds hardware init failed\n");
  401. goto pciec_init_err_2;
  402. }
  403. INIT_DELAYED_WORK(&card->led_work, peak_pciec_led_work);
  404. /* PCAN-ExpressCard needs its own callback for leds */
  405. priv->write_reg = peak_pciec_write_reg;
  406. }
  407. chan->pciec_card = card;
  408. card->channel[card->chan_count++].netdev = dev;
  409. return 0;
  410. pciec_init_err_2:
  411. i2c_del_adapter(&card->led_chip);
  412. pciec_init_err_1:
  413. peak_pciec_init_pita_gpio(card);
  414. kfree(card);
  415. return err;
  416. }
  417. static void peak_pciec_remove(struct peak_pciec_card *card)
  418. {
  419. peak_pciec_stop_led_work(card);
  420. peak_pciec_leds_exit(card);
  421. i2c_del_adapter(&card->led_chip);
  422. peak_pciec_init_pita_gpio(card);
  423. kfree(card);
  424. }
  425. #else /* CONFIG_CAN_PEAK_PCIEC */
  426. /*
  427. * Placebo functions when PCAN-ExpressCard support is not selected
  428. */
  429. static inline int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
  430. {
  431. return -ENODEV;
  432. }
  433. static inline void peak_pciec_remove(struct peak_pciec_card *card)
  434. {
  435. }
  436. #endif /* CONFIG_CAN_PEAK_PCIEC */
  437. static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
  438. {
  439. return readb(priv->reg_base + (port << 2));
  440. }
  441. static void peak_pci_write_reg(const struct sja1000_priv *priv,
  442. int port, u8 val)
  443. {
  444. writeb(val, priv->reg_base + (port << 2));
  445. }
  446. static void peak_pci_post_irq(const struct sja1000_priv *priv)
  447. {
  448. struct peak_pci_chan *chan = priv->priv;
  449. u16 icr;
  450. /* Select and clear in PITA stored interrupt */
  451. icr = readw(chan->cfg_base + PITA_ICR);
  452. if (icr & chan->icr_mask)
  453. writew(chan->icr_mask, chan->cfg_base + PITA_ICR);
  454. }
  455. static int peak_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  456. {
  457. struct sja1000_priv *priv;
  458. struct peak_pci_chan *chan;
  459. struct net_device *dev;
  460. void __iomem *cfg_base, *reg_base;
  461. u16 sub_sys_id, icr;
  462. int i, err, channels;
  463. err = pci_enable_device(pdev);
  464. if (err)
  465. return err;
  466. err = pci_request_regions(pdev, DRV_NAME);
  467. if (err)
  468. goto failure_disable_pci;
  469. err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
  470. if (err)
  471. goto failure_release_regions;
  472. dev_dbg(&pdev->dev, "probing device %04x:%04x:%04x\n",
  473. pdev->vendor, pdev->device, sub_sys_id);
  474. err = pci_write_config_word(pdev, 0x44, 0);
  475. if (err)
  476. goto failure_release_regions;
  477. if (sub_sys_id >= 12)
  478. channels = 4;
  479. else if (sub_sys_id >= 10)
  480. channels = 3;
  481. else if (sub_sys_id >= 4)
  482. channels = 2;
  483. else
  484. channels = 1;
  485. cfg_base = pci_iomap(pdev, 0, PEAK_PCI_CFG_SIZE);
  486. if (!cfg_base) {
  487. dev_err(&pdev->dev, "failed to map PCI resource #0\n");
  488. err = -ENOMEM;
  489. goto failure_release_regions;
  490. }
  491. reg_base = pci_iomap(pdev, 1, PEAK_PCI_CHAN_SIZE * channels);
  492. if (!reg_base) {
  493. dev_err(&pdev->dev, "failed to map PCI resource #1\n");
  494. err = -ENOMEM;
  495. goto failure_unmap_cfg_base;
  496. }
  497. /* Set GPIO control register */
  498. writew(0x0005, cfg_base + PITA_GPIOICR + 2);
  499. /* Enable all channels of this card */
  500. writeb(0x00, cfg_base + PITA_GPIOICR);
  501. /* Toggle reset */
  502. writeb(0x05, cfg_base + PITA_MISC + 3);
  503. mdelay(5);
  504. /* Leave parport mux mode */
  505. writeb(0x04, cfg_base + PITA_MISC + 3);
  506. icr = readw(cfg_base + PITA_ICR + 2);
  507. for (i = 0; i < channels; i++) {
  508. dev = alloc_sja1000dev(sizeof(struct peak_pci_chan));
  509. if (!dev) {
  510. err = -ENOMEM;
  511. goto failure_remove_channels;
  512. }
  513. priv = netdev_priv(dev);
  514. chan = priv->priv;
  515. chan->cfg_base = cfg_base;
  516. priv->reg_base = reg_base + i * PEAK_PCI_CHAN_SIZE;
  517. priv->read_reg = peak_pci_read_reg;
  518. priv->write_reg = peak_pci_write_reg;
  519. priv->post_irq = peak_pci_post_irq;
  520. priv->can.clock.freq = PEAK_PCI_CAN_CLOCK;
  521. priv->ocr = PEAK_PCI_OCR;
  522. priv->cdr = PEAK_PCI_CDR;
  523. /* Neither a slave nor a single device distributes the clock */
  524. if (channels == 1 || i > 0)
  525. priv->cdr |= CDR_CLK_OFF;
  526. /* Setup interrupt handling */
  527. priv->irq_flags = IRQF_SHARED;
  528. dev->irq = pdev->irq;
  529. chan->icr_mask = peak_pci_icr_masks[i];
  530. icr |= chan->icr_mask;
  531. SET_NETDEV_DEV(dev, &pdev->dev);
  532. /* Create chain of SJA1000 devices */
  533. chan->prev_dev = pci_get_drvdata(pdev);
  534. pci_set_drvdata(pdev, dev);
  535. /*
  536. * PCAN-ExpressCard needs some additional i2c init.
  537. * This must be done *before* register_sja1000dev() but
  538. * *after* devices linkage
  539. */
  540. if (pdev->device == PEAK_PCIEC_DEVICE_ID) {
  541. err = peak_pciec_probe(pdev, dev);
  542. if (err) {
  543. dev_err(&pdev->dev,
  544. "failed to probe device (err %d)\n",
  545. err);
  546. goto failure_free_dev;
  547. }
  548. }
  549. err = register_sja1000dev(dev);
  550. if (err) {
  551. dev_err(&pdev->dev, "failed to register device\n");
  552. goto failure_free_dev;
  553. }
  554. dev_info(&pdev->dev,
  555. "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n",
  556. dev->name, priv->reg_base, chan->cfg_base, dev->irq);
  557. }
  558. /* Enable interrupts */
  559. writew(icr, cfg_base + PITA_ICR + 2);
  560. return 0;
  561. failure_free_dev:
  562. pci_set_drvdata(pdev, chan->prev_dev);
  563. free_sja1000dev(dev);
  564. failure_remove_channels:
  565. /* Disable interrupts */
  566. writew(0x0, cfg_base + PITA_ICR + 2);
  567. chan = NULL;
  568. for (dev = pci_get_drvdata(pdev); dev; dev = chan->prev_dev) {
  569. unregister_sja1000dev(dev);
  570. free_sja1000dev(dev);
  571. priv = netdev_priv(dev);
  572. chan = priv->priv;
  573. }
  574. /* free any PCIeC resources too */
  575. if (chan && chan->pciec_card)
  576. peak_pciec_remove(chan->pciec_card);
  577. pci_iounmap(pdev, reg_base);
  578. failure_unmap_cfg_base:
  579. pci_iounmap(pdev, cfg_base);
  580. failure_release_regions:
  581. pci_release_regions(pdev);
  582. failure_disable_pci:
  583. pci_disable_device(pdev);
  584. return err;
  585. }
  586. static void peak_pci_remove(struct pci_dev *pdev)
  587. {
  588. struct net_device *dev = pci_get_drvdata(pdev); /* Last device */
  589. struct sja1000_priv *priv = netdev_priv(dev);
  590. struct peak_pci_chan *chan = priv->priv;
  591. void __iomem *cfg_base = chan->cfg_base;
  592. void __iomem *reg_base = priv->reg_base;
  593. /* Disable interrupts */
  594. writew(0x0, cfg_base + PITA_ICR + 2);
  595. /* Loop over all registered devices */
  596. while (1) {
  597. dev_info(&pdev->dev, "removing device %s\n", dev->name);
  598. unregister_sja1000dev(dev);
  599. free_sja1000dev(dev);
  600. dev = chan->prev_dev;
  601. if (!dev) {
  602. /* do that only for first channel */
  603. if (chan->pciec_card)
  604. peak_pciec_remove(chan->pciec_card);
  605. break;
  606. }
  607. priv = netdev_priv(dev);
  608. chan = priv->priv;
  609. }
  610. pci_iounmap(pdev, reg_base);
  611. pci_iounmap(pdev, cfg_base);
  612. pci_release_regions(pdev);
  613. pci_disable_device(pdev);
  614. pci_set_drvdata(pdev, NULL);
  615. }
  616. static struct pci_driver peak_pci_driver = {
  617. .name = DRV_NAME,
  618. .id_table = peak_pci_tbl,
  619. .probe = peak_pci_probe,
  620. .remove = peak_pci_remove,
  621. };
  622. module_pci_driver(peak_pci_driver);