softing_cs.c 9.3 KB

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