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