softing_cs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008-2010
  4. *
  5. * - Kurt Van Dijck, EIA Electronics
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <pcmcia/cistpl.h>
  11. #include <pcmcia/ds.h>
  12. #include "softing_platform.h"
  13. static int softingcs_index;
  14. static DEFINE_SPINLOCK(softingcs_index_lock);
  15. static int softingcs_reset(struct platform_device *pdev, int v);
  16. static int softingcs_enable_irq(struct platform_device *pdev, int v);
  17. /*
  18. * platform_data descriptions
  19. */
  20. #define MHZ (1000*1000)
  21. static const struct softing_platform_data softingcs_platform_data[] = {
  22. {
  23. .name = "CANcard",
  24. .manf = 0x0168, .prod = 0x001,
  25. .generation = 1,
  26. .nbus = 2,
  27. .freq = 16 * MHZ, .max_brp = 32, .max_sjw = 4,
  28. .dpram_size = 0x0800,
  29. .boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
  30. .load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
  31. .app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
  32. .reset = softingcs_reset,
  33. .enable_irq = softingcs_enable_irq,
  34. }, {
  35. .name = "CANcard-NEC",
  36. .manf = 0x0168, .prod = 0x002,
  37. .generation = 1,
  38. .nbus = 2,
  39. .freq = 16 * MHZ, .max_brp = 32, .max_sjw = 4,
  40. .dpram_size = 0x0800,
  41. .boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
  42. .load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
  43. .app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
  44. .reset = softingcs_reset,
  45. .enable_irq = softingcs_enable_irq,
  46. }, {
  47. .name = "CANcard-SJA",
  48. .manf = 0x0168, .prod = 0x004,
  49. .generation = 1,
  50. .nbus = 2,
  51. .freq = 20 * MHZ, .max_brp = 32, .max_sjw = 4,
  52. .dpram_size = 0x0800,
  53. .boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
  54. .load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
  55. .app = {0x0010, 0x0d0000, fw_dir "cansja.bin",},
  56. .reset = softingcs_reset,
  57. .enable_irq = softingcs_enable_irq,
  58. }, {
  59. .name = "CANcard-2",
  60. .manf = 0x0168, .prod = 0x005,
  61. .generation = 2,
  62. .nbus = 2,
  63. .freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
  64. .dpram_size = 0x1000,
  65. .boot = {0x0000, 0x000000, fw_dir "bcard2.bin",},
  66. .load = {0x0120, 0x00f600, fw_dir "ldcard2.bin",},
  67. .app = {0x0010, 0x0d0000, fw_dir "cancrd2.bin",},
  68. .reset = softingcs_reset,
  69. .enable_irq = NULL,
  70. }, {
  71. .name = "Vector-CANcard",
  72. .manf = 0x0168, .prod = 0x081,
  73. .generation = 1,
  74. .nbus = 2,
  75. .freq = 16 * MHZ, .max_brp = 64, .max_sjw = 4,
  76. .dpram_size = 0x0800,
  77. .boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
  78. .load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
  79. .app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
  80. .reset = softingcs_reset,
  81. .enable_irq = softingcs_enable_irq,
  82. }, {
  83. .name = "Vector-CANcard-SJA",
  84. .manf = 0x0168, .prod = 0x084,
  85. .generation = 1,
  86. .nbus = 2,
  87. .freq = 20 * MHZ, .max_brp = 32, .max_sjw = 4,
  88. .dpram_size = 0x0800,
  89. .boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
  90. .load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
  91. .app = {0x0010, 0x0d0000, fw_dir "cansja.bin",},
  92. .reset = softingcs_reset,
  93. .enable_irq = softingcs_enable_irq,
  94. }, {
  95. .name = "Vector-CANcard-2",
  96. .manf = 0x0168, .prod = 0x085,
  97. .generation = 2,
  98. .nbus = 2,
  99. .freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
  100. .dpram_size = 0x1000,
  101. .boot = {0x0000, 0x000000, fw_dir "bcard2.bin",},
  102. .load = {0x0120, 0x00f600, fw_dir "ldcard2.bin",},
  103. .app = {0x0010, 0x0d0000, fw_dir "cancrd2.bin",},
  104. .reset = softingcs_reset,
  105. .enable_irq = NULL,
  106. }, {
  107. .name = "EDICcard-NEC",
  108. .manf = 0x0168, .prod = 0x102,
  109. .generation = 1,
  110. .nbus = 2,
  111. .freq = 16 * MHZ, .max_brp = 64, .max_sjw = 4,
  112. .dpram_size = 0x0800,
  113. .boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
  114. .load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
  115. .app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
  116. .reset = softingcs_reset,
  117. .enable_irq = softingcs_enable_irq,
  118. }, {
  119. .name = "EDICcard-2",
  120. .manf = 0x0168, .prod = 0x105,
  121. .generation = 2,
  122. .nbus = 2,
  123. .freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
  124. .dpram_size = 0x1000,
  125. .boot = {0x0000, 0x000000, fw_dir "bcard2.bin",},
  126. .load = {0x0120, 0x00f600, fw_dir "ldcard2.bin",},
  127. .app = {0x0010, 0x0d0000, fw_dir "cancrd2.bin",},
  128. .reset = softingcs_reset,
  129. .enable_irq = NULL,
  130. }, {
  131. 0, 0,
  132. },
  133. };
  134. MODULE_FIRMWARE(fw_dir "bcard.bin");
  135. MODULE_FIRMWARE(fw_dir "ldcard.bin");
  136. MODULE_FIRMWARE(fw_dir "cancard.bin");
  137. MODULE_FIRMWARE(fw_dir "cansja.bin");
  138. MODULE_FIRMWARE(fw_dir "bcard2.bin");
  139. MODULE_FIRMWARE(fw_dir "ldcard2.bin");
  140. MODULE_FIRMWARE(fw_dir "cancrd2.bin");
  141. static const struct softing_platform_data
  142. *softingcs_find_platform_data(unsigned int manf, unsigned int prod)
  143. {
  144. const struct softing_platform_data *lp;
  145. for (lp = softingcs_platform_data; lp->manf; ++lp) {
  146. if ((lp->manf == manf) && (lp->prod == prod))
  147. return lp;
  148. }
  149. return NULL;
  150. }
  151. /*
  152. * platformdata callbacks
  153. */
  154. static int softingcs_reset(struct platform_device *pdev, int v)
  155. {
  156. struct pcmcia_device *pcmcia = to_pcmcia_dev(pdev->dev.parent);
  157. dev_dbg(&pdev->dev, "pcmcia config [2] %02x\n", v ? 0 : 0x20);
  158. return pcmcia_write_config_byte(pcmcia, 2, v ? 0 : 0x20);
  159. }
  160. static int softingcs_enable_irq(struct platform_device *pdev, int v)
  161. {
  162. struct pcmcia_device *pcmcia = to_pcmcia_dev(pdev->dev.parent);
  163. dev_dbg(&pdev->dev, "pcmcia config [0] %02x\n", v ? 0x60 : 0);
  164. return pcmcia_write_config_byte(pcmcia, 0, v ? 0x60 : 0);
  165. }
  166. /*
  167. * pcmcia check
  168. */
  169. static int softingcs_probe_config(struct pcmcia_device *pcmcia, void *priv_data)
  170. {
  171. struct softing_platform_data *pdat = priv_data;
  172. struct resource *pres;
  173. int memspeed = 0;
  174. WARN_ON(!pdat);
  175. pres = pcmcia->resource[PCMCIA_IOMEM_0];
  176. if (resource_size(pres) < 0x1000)
  177. return -ERANGE;
  178. pres->flags |= WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  179. if (pdat->generation < 2) {
  180. pres->flags |= WIN_USE_WAIT | WIN_DATA_WIDTH_8;
  181. memspeed = 3;
  182. } else {
  183. pres->flags |= WIN_DATA_WIDTH_16;
  184. }
  185. return pcmcia_request_window(pcmcia, pres, memspeed);
  186. }
  187. static void softingcs_remove(struct pcmcia_device *pcmcia)
  188. {
  189. struct platform_device *pdev = pcmcia->priv;
  190. /* free bits */
  191. platform_device_unregister(pdev);
  192. /* release pcmcia stuff */
  193. pcmcia_disable_device(pcmcia);
  194. }
  195. /*
  196. * platform_device wrapper
  197. * pdev->resource has 2 entries: io & irq
  198. */
  199. static void softingcs_pdev_release(struct device *dev)
  200. {
  201. struct platform_device *pdev = to_platform_device(dev);
  202. kfree(pdev);
  203. }
  204. static int softingcs_probe(struct pcmcia_device *pcmcia)
  205. {
  206. int ret;
  207. struct platform_device *pdev;
  208. const struct softing_platform_data *pdat;
  209. struct resource *pres;
  210. struct dev {
  211. struct platform_device pdev;
  212. struct resource res[2];
  213. } *dev;
  214. /* find matching platform_data */
  215. pdat = softingcs_find_platform_data(pcmcia->manf_id, pcmcia->card_id);
  216. if (!pdat)
  217. return -ENOTTY;
  218. /* setup pcmcia device */
  219. pcmcia->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IOMEM |
  220. CONF_AUTO_SET_VPP | CONF_AUTO_CHECK_VCC;
  221. ret = pcmcia_loop_config(pcmcia, softingcs_probe_config, (void *)pdat);
  222. if (ret)
  223. goto pcmcia_failed;
  224. ret = pcmcia_enable_device(pcmcia);
  225. if (ret < 0)
  226. goto pcmcia_failed;
  227. pres = pcmcia->resource[PCMCIA_IOMEM_0];
  228. if (!pres) {
  229. ret = -EBADF;
  230. goto pcmcia_bad;
  231. }
  232. /* create softing platform device */
  233. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  234. if (!dev) {
  235. ret = -ENOMEM;
  236. goto mem_failed;
  237. }
  238. dev->pdev.resource = dev->res;
  239. dev->pdev.num_resources = ARRAY_SIZE(dev->res);
  240. dev->pdev.dev.release = softingcs_pdev_release;
  241. pdev = &dev->pdev;
  242. pdev->dev.platform_data = (void *)pdat;
  243. pdev->dev.parent = &pcmcia->dev;
  244. pcmcia->priv = pdev;
  245. /* platform device resources */
  246. pdev->resource[0].flags = IORESOURCE_MEM;
  247. pdev->resource[0].start = pres->start;
  248. pdev->resource[0].end = pres->end;
  249. pdev->resource[1].flags = IORESOURCE_IRQ;
  250. pdev->resource[1].start = pcmcia->irq;
  251. pdev->resource[1].end = pdev->resource[1].start;
  252. /* platform device setup */
  253. spin_lock(&softingcs_index_lock);
  254. pdev->id = softingcs_index++;
  255. spin_unlock(&softingcs_index_lock);
  256. pdev->name = "softing";
  257. dev_set_name(&pdev->dev, "softingcs.%i", pdev->id);
  258. ret = platform_device_register(pdev);
  259. if (ret < 0)
  260. goto platform_failed;
  261. dev_info(&pcmcia->dev, "created %s\n", dev_name(&pdev->dev));
  262. return 0;
  263. platform_failed:
  264. platform_device_put(pdev);
  265. mem_failed:
  266. pcmcia_bad:
  267. pcmcia_failed:
  268. pcmcia_disable_device(pcmcia);
  269. pcmcia->priv = NULL;
  270. return ret;
  271. }
  272. static const struct pcmcia_device_id softingcs_ids[] = {
  273. /* softing */
  274. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0001),
  275. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0002),
  276. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0004),
  277. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0005),
  278. /* vector, manufacturer? */
  279. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0081),
  280. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0084),
  281. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0085),
  282. /* EDIC */
  283. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0102),
  284. PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0105),
  285. PCMCIA_DEVICE_NULL,
  286. };
  287. MODULE_DEVICE_TABLE(pcmcia, softingcs_ids);
  288. static struct pcmcia_driver softingcs_driver = {
  289. .owner = THIS_MODULE,
  290. .name = "softingcs",
  291. .id_table = softingcs_ids,
  292. .probe = softingcs_probe,
  293. .remove = softingcs_remove,
  294. };
  295. module_pcmcia_driver(softingcs_driver);
  296. MODULE_DESCRIPTION("softing CANcard driver"
  297. ", links PCMCIA card to softing driver");
  298. MODULE_LICENSE("GPL v2");