ems_pcmcia.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2008 Sebastian Haas (initial chardev implementation)
  3. * Copyright (C) 2010 Markus Plessing <plessing@ems-wuensche.com>
  4. * Rework for mainline by Oliver Hartkopp <socketcan@hartkopp.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/delay.h>
  20. #include <linux/io.h>
  21. #include <pcmcia/cistpl.h>
  22. #include <pcmcia/ds.h>
  23. #include <linux/can.h>
  24. #include <linux/can/dev.h>
  25. #include "sja1000.h"
  26. #define DRV_NAME "ems_pcmcia"
  27. MODULE_AUTHOR("Markus Plessing <plessing@ems-wuensche.com>");
  28. MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-CARD cards");
  29. MODULE_SUPPORTED_DEVICE("EMS CPC-CARD CAN card");
  30. MODULE_LICENSE("GPL v2");
  31. #define EMS_PCMCIA_MAX_CHAN 2
  32. struct ems_pcmcia_card {
  33. int channels;
  34. struct pcmcia_device *pcmcia_dev;
  35. struct net_device *net_dev[EMS_PCMCIA_MAX_CHAN];
  36. void __iomem *base_addr;
  37. };
  38. #define EMS_PCMCIA_CAN_CLOCK (16000000 / 2)
  39. /*
  40. * The board configuration is probably following:
  41. * RX1 is connected to ground.
  42. * TX1 is not connected.
  43. * CLKO is not connected.
  44. * Setting the OCR register to 0xDA is a good idea.
  45. * This means normal output mode , push-pull and the correct polarity.
  46. */
  47. #define EMS_PCMCIA_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
  48. /*
  49. * In the CDR register, you should set CBP to 1.
  50. * You will probably also want to set the clock divider value to 7
  51. * (meaning direct oscillator output) because the second SJA1000 chip
  52. * is driven by the first one CLKOUT output.
  53. */
  54. #define EMS_PCMCIA_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  55. #define EMS_PCMCIA_MEM_SIZE 4096 /* Size of the remapped io-memory */
  56. #define EMS_PCMCIA_CAN_BASE_OFFSET 0x100 /* Offset where controllers starts */
  57. #define EMS_PCMCIA_CAN_CTRL_SIZE 0x80 /* Memory size for each controller */
  58. #define EMS_CMD_RESET 0x00 /* Perform a reset of the card */
  59. #define EMS_CMD_MAP 0x03 /* Map CAN controllers into card' memory */
  60. #define EMS_CMD_UMAP 0x02 /* Unmap CAN controllers from card' memory */
  61. static struct pcmcia_device_id ems_pcmcia_tbl[] = {
  62. PCMCIA_DEVICE_PROD_ID123("EMS_T_W", "CPC-Card", "V2.0", 0xeab1ea23,
  63. 0xa338573f, 0xe4575800),
  64. PCMCIA_DEVICE_NULL,
  65. };
  66. MODULE_DEVICE_TABLE(pcmcia, ems_pcmcia_tbl);
  67. static u8 ems_pcmcia_read_reg(const struct sja1000_priv *priv, int port)
  68. {
  69. return readb(priv->reg_base + port);
  70. }
  71. static void ems_pcmcia_write_reg(const struct sja1000_priv *priv, int port,
  72. u8 val)
  73. {
  74. writeb(val, priv->reg_base + port);
  75. }
  76. static irqreturn_t ems_pcmcia_interrupt(int irq, void *dev_id)
  77. {
  78. struct ems_pcmcia_card *card = dev_id;
  79. struct net_device *dev;
  80. irqreturn_t retval = IRQ_NONE;
  81. int i, again;
  82. /* Card not present */
  83. if (readw(card->base_addr) != 0xAA55)
  84. return IRQ_HANDLED;
  85. do {
  86. again = 0;
  87. /* Check interrupt for each channel */
  88. for (i = 0; i < card->channels; i++) {
  89. dev = card->net_dev[i];
  90. if (!dev)
  91. continue;
  92. if (sja1000_interrupt(irq, dev) == IRQ_HANDLED)
  93. again = 1;
  94. }
  95. /* At least one channel handled the interrupt */
  96. if (again)
  97. retval = IRQ_HANDLED;
  98. } while (again);
  99. return retval;
  100. }
  101. /*
  102. * Check if a CAN controller is present at the specified location
  103. * by trying to set 'em into the PeliCAN mode
  104. */
  105. static inline int ems_pcmcia_check_chan(struct sja1000_priv *priv)
  106. {
  107. /* Make sure SJA1000 is in reset mode */
  108. ems_pcmcia_write_reg(priv, SJA1000_MOD, 1);
  109. ems_pcmcia_write_reg(priv, SJA1000_CDR, CDR_PELICAN);
  110. /* read reset-values */
  111. if (ems_pcmcia_read_reg(priv, SJA1000_CDR) == CDR_PELICAN)
  112. return 1;
  113. return 0;
  114. }
  115. static void ems_pcmcia_del_card(struct pcmcia_device *pdev)
  116. {
  117. struct ems_pcmcia_card *card = pdev->priv;
  118. struct net_device *dev;
  119. int i;
  120. free_irq(pdev->irq, card);
  121. for (i = 0; i < card->channels; i++) {
  122. dev = card->net_dev[i];
  123. if (!dev)
  124. continue;
  125. printk(KERN_INFO "%s: removing %s on channel #%d\n",
  126. DRV_NAME, dev->name, i);
  127. unregister_sja1000dev(dev);
  128. free_sja1000dev(dev);
  129. }
  130. writeb(EMS_CMD_UMAP, card->base_addr);
  131. iounmap(card->base_addr);
  132. kfree(card);
  133. pdev->priv = NULL;
  134. }
  135. /*
  136. * Probe PCI device for EMS CAN signature and register each available
  137. * CAN channel to SJA1000 Socket-CAN subsystem.
  138. */
  139. static int ems_pcmcia_add_card(struct pcmcia_device *pdev, unsigned long base)
  140. {
  141. struct sja1000_priv *priv;
  142. struct net_device *dev;
  143. struct ems_pcmcia_card *card;
  144. int err, i;
  145. /* Allocating card structures to hold addresses, ... */
  146. card = kzalloc(sizeof(struct ems_pcmcia_card), GFP_KERNEL);
  147. if (!card)
  148. return -ENOMEM;
  149. pdev->priv = card;
  150. card->channels = 0;
  151. card->base_addr = ioremap(base, EMS_PCMCIA_MEM_SIZE);
  152. if (!card->base_addr) {
  153. err = -ENOMEM;
  154. goto failure_cleanup;
  155. }
  156. /* Check for unique EMS CAN signature */
  157. if (readw(card->base_addr) != 0xAA55) {
  158. err = -ENODEV;
  159. goto failure_cleanup;
  160. }
  161. /* Request board reset */
  162. writeb(EMS_CMD_RESET, card->base_addr);
  163. /* Make sure CAN controllers are mapped into card's memory space */
  164. writeb(EMS_CMD_MAP, card->base_addr);
  165. /* Detect available channels */
  166. for (i = 0; i < EMS_PCMCIA_MAX_CHAN; i++) {
  167. dev = alloc_sja1000dev(0);
  168. if (!dev) {
  169. err = -ENOMEM;
  170. goto failure_cleanup;
  171. }
  172. card->net_dev[i] = dev;
  173. priv = netdev_priv(dev);
  174. priv->priv = card;
  175. SET_NETDEV_DEV(dev, &pdev->dev);
  176. dev->dev_id = i;
  177. priv->irq_flags = IRQF_SHARED;
  178. dev->irq = pdev->irq;
  179. priv->reg_base = card->base_addr + EMS_PCMCIA_CAN_BASE_OFFSET +
  180. (i * EMS_PCMCIA_CAN_CTRL_SIZE);
  181. /* Check if channel is present */
  182. if (ems_pcmcia_check_chan(priv)) {
  183. priv->read_reg = ems_pcmcia_read_reg;
  184. priv->write_reg = ems_pcmcia_write_reg;
  185. priv->can.clock.freq = EMS_PCMCIA_CAN_CLOCK;
  186. priv->ocr = EMS_PCMCIA_OCR;
  187. priv->cdr = EMS_PCMCIA_CDR;
  188. priv->flags |= SJA1000_CUSTOM_IRQ_HANDLER;
  189. /* Register SJA1000 device */
  190. err = register_sja1000dev(dev);
  191. if (err) {
  192. free_sja1000dev(dev);
  193. goto failure_cleanup;
  194. }
  195. card->channels++;
  196. printk(KERN_INFO "%s: registered %s on channel "
  197. "#%d at 0x%p, irq %d\n", DRV_NAME, dev->name,
  198. i, priv->reg_base, dev->irq);
  199. } else
  200. free_sja1000dev(dev);
  201. }
  202. err = request_irq(dev->irq, &ems_pcmcia_interrupt, IRQF_SHARED,
  203. DRV_NAME, card);
  204. if (!err)
  205. return 0;
  206. failure_cleanup:
  207. ems_pcmcia_del_card(pdev);
  208. return err;
  209. }
  210. /*
  211. * Setup PCMCIA socket and probe for EMS CPC-CARD
  212. */
  213. static int ems_pcmcia_probe(struct pcmcia_device *dev)
  214. {
  215. int csval;
  216. /* General socket configuration */
  217. dev->config_flags |= CONF_ENABLE_IRQ;
  218. dev->config_index = 1;
  219. dev->config_regs = PRESENT_OPTION;
  220. /* The io structure describes IO port mapping */
  221. dev->resource[0]->end = 16;
  222. dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  223. dev->resource[1]->end = 16;
  224. dev->resource[1]->flags |= IO_DATA_PATH_WIDTH_16;
  225. dev->io_lines = 5;
  226. /* Allocate a memory window */
  227. dev->resource[2]->flags =
  228. (WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_CM | WIN_ENABLE);
  229. dev->resource[2]->start = dev->resource[2]->end = 0;
  230. csval = pcmcia_request_window(dev, dev->resource[2], 0);
  231. if (csval) {
  232. dev_err(&dev->dev, "pcmcia_request_window failed (err=%d)\n",
  233. csval);
  234. return 0;
  235. }
  236. csval = pcmcia_map_mem_page(dev, dev->resource[2], dev->config_base);
  237. if (csval) {
  238. dev_err(&dev->dev, "pcmcia_map_mem_page failed (err=%d)\n",
  239. csval);
  240. return 0;
  241. }
  242. csval = pcmcia_enable_device(dev);
  243. if (csval) {
  244. dev_err(&dev->dev, "pcmcia_enable_device failed (err=%d)\n",
  245. csval);
  246. return 0;
  247. }
  248. ems_pcmcia_add_card(dev, dev->resource[2]->start);
  249. return 0;
  250. }
  251. /*
  252. * Release claimed resources
  253. */
  254. static void ems_pcmcia_remove(struct pcmcia_device *dev)
  255. {
  256. ems_pcmcia_del_card(dev);
  257. pcmcia_disable_device(dev);
  258. }
  259. static struct pcmcia_driver ems_pcmcia_driver = {
  260. .name = DRV_NAME,
  261. .probe = ems_pcmcia_probe,
  262. .remove = ems_pcmcia_remove,
  263. .id_table = ems_pcmcia_tbl,
  264. };
  265. module_pcmcia_driver(ems_pcmcia_driver);