peak_pci.c 20 KB

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