ems_pcmcia.c 7.7 KB

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