softing_cs.c 9.7 KB

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