smssdio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * smssdio.c - Siano 1xxx SDIO interface driver
  3. *
  4. * Copyright 2008 Pierre Ossman
  5. *
  6. * Based on code by Siano Mobile Silicon, Inc.,
  7. * Copyright (C) 2006-2008, Uri Shkolnik
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. *
  15. * This hardware is a bit odd in that all transfers should be done
  16. * to/from the SMSSDIO_DATA register, yet the "increase address" bit
  17. * always needs to be set.
  18. *
  19. * Also, buffers from the card are always aligned to 128 byte
  20. * boundaries.
  21. */
  22. /*
  23. * General cleanup notes:
  24. *
  25. * - only typedefs should be name *_t
  26. *
  27. * - use ERR_PTR and friends for smscore_register_device()
  28. *
  29. * - smscore_getbuffer should zero fields
  30. *
  31. * Fix stop command
  32. */
  33. #include <linux/moduleparam.h>
  34. #include <linux/slab.h>
  35. #include <linux/firmware.h>
  36. #include <linux/delay.h>
  37. #include <linux/mmc/card.h>
  38. #include <linux/mmc/sdio_func.h>
  39. #include <linux/mmc/sdio_ids.h>
  40. #include "smscoreapi.h"
  41. #include "sms-cards.h"
  42. /* Registers */
  43. #define SMSSDIO_DATA 0x00
  44. #define SMSSDIO_INT 0x04
  45. #define SMSSDIO_BLOCK_SIZE 128
  46. static const struct sdio_device_id smssdio_ids[] __devinitconst = {
  47. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_STELLAR),
  48. .driver_data = SMS1XXX_BOARD_SIANO_STELLAR},
  49. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_A0),
  50. .driver_data = SMS1XXX_BOARD_SIANO_NOVA_A},
  51. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_B0),
  52. .driver_data = SMS1XXX_BOARD_SIANO_NOVA_B},
  53. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VEGA_A0),
  54. .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
  55. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VENICE),
  56. .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
  57. { /* end: all zeroes */ },
  58. };
  59. MODULE_DEVICE_TABLE(sdio, smssdio_ids);
  60. struct smssdio_device {
  61. struct sdio_func *func;
  62. struct smscore_device_t *coredev;
  63. struct smscore_buffer_t *split_cb;
  64. };
  65. /*******************************************************************/
  66. /* Siano core callbacks */
  67. /*******************************************************************/
  68. static int smssdio_sendrequest(void *context, void *buffer, size_t size)
  69. {
  70. int ret = 0;
  71. struct smssdio_device *smsdev;
  72. smsdev = context;
  73. sdio_claim_host(smsdev->func);
  74. while (size >= smsdev->func->cur_blksize) {
  75. ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
  76. buffer, smsdev->func->cur_blksize);
  77. if (ret)
  78. goto out;
  79. buffer += smsdev->func->cur_blksize;
  80. size -= smsdev->func->cur_blksize;
  81. }
  82. if (size) {
  83. ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
  84. buffer, size);
  85. }
  86. out:
  87. sdio_release_host(smsdev->func);
  88. return ret;
  89. }
  90. /*******************************************************************/
  91. /* SDIO callbacks */
  92. /*******************************************************************/
  93. static void smssdio_interrupt(struct sdio_func *func)
  94. {
  95. int ret, isr;
  96. struct smssdio_device *smsdev;
  97. struct smscore_buffer_t *cb;
  98. struct SmsMsgHdr_ST *hdr;
  99. size_t size;
  100. smsdev = sdio_get_drvdata(func);
  101. /*
  102. * The interrupt register has no defined meaning. It is just
  103. * a way of turning of the level triggered interrupt.
  104. */
  105. isr = sdio_readb(func, SMSSDIO_INT, &ret);
  106. if (ret) {
  107. sms_err("Unable to read interrupt register!\n");
  108. return;
  109. }
  110. if (smsdev->split_cb == NULL) {
  111. cb = smscore_getbuffer(smsdev->coredev);
  112. if (!cb) {
  113. sms_err("Unable to allocate data buffer!\n");
  114. return;
  115. }
  116. ret = sdio_memcpy_fromio(smsdev->func,
  117. cb->p,
  118. SMSSDIO_DATA,
  119. SMSSDIO_BLOCK_SIZE);
  120. if (ret) {
  121. sms_err("Error %d reading initial block!\n", ret);
  122. return;
  123. }
  124. hdr = cb->p;
  125. if (hdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG) {
  126. smsdev->split_cb = cb;
  127. return;
  128. }
  129. if (hdr->msgLength > smsdev->func->cur_blksize)
  130. size = hdr->msgLength - smsdev->func->cur_blksize;
  131. else
  132. size = 0;
  133. } else {
  134. cb = smsdev->split_cb;
  135. hdr = cb->p;
  136. size = hdr->msgLength - sizeof(struct SmsMsgHdr_ST);
  137. smsdev->split_cb = NULL;
  138. }
  139. if (size) {
  140. void *buffer;
  141. buffer = cb->p + (hdr->msgLength - size);
  142. size = ALIGN(size, SMSSDIO_BLOCK_SIZE);
  143. BUG_ON(smsdev->func->cur_blksize != SMSSDIO_BLOCK_SIZE);
  144. /*
  145. * First attempt to transfer all of it in one go...
  146. */
  147. ret = sdio_memcpy_fromio(smsdev->func,
  148. buffer,
  149. SMSSDIO_DATA,
  150. size);
  151. if (ret && ret != -EINVAL) {
  152. smscore_putbuffer(smsdev->coredev, cb);
  153. sms_err("Error %d reading data from card!\n", ret);
  154. return;
  155. }
  156. /*
  157. * ..then fall back to one block at a time if that is
  158. * not possible...
  159. *
  160. * (we have to do this manually because of the
  161. * problem with the "increase address" bit)
  162. */
  163. if (ret == -EINVAL) {
  164. while (size) {
  165. ret = sdio_memcpy_fromio(smsdev->func,
  166. buffer, SMSSDIO_DATA,
  167. smsdev->func->cur_blksize);
  168. if (ret) {
  169. smscore_putbuffer(smsdev->coredev, cb);
  170. sms_err("Error %d reading "
  171. "data from card!\n", ret);
  172. return;
  173. }
  174. buffer += smsdev->func->cur_blksize;
  175. if (size > smsdev->func->cur_blksize)
  176. size -= smsdev->func->cur_blksize;
  177. else
  178. size = 0;
  179. }
  180. }
  181. }
  182. cb->size = hdr->msgLength;
  183. cb->offset = 0;
  184. smscore_onresponse(smsdev->coredev, cb);
  185. }
  186. static int __devinit smssdio_probe(struct sdio_func *func,
  187. const struct sdio_device_id *id)
  188. {
  189. int ret;
  190. int board_id;
  191. struct smssdio_device *smsdev;
  192. struct smsdevice_params_t params;
  193. board_id = id->driver_data;
  194. smsdev = kzalloc(sizeof(struct smssdio_device), GFP_KERNEL);
  195. if (!smsdev)
  196. return -ENOMEM;
  197. smsdev->func = func;
  198. memset(&params, 0, sizeof(struct smsdevice_params_t));
  199. params.device = &func->dev;
  200. params.buffer_size = 0x5000; /* ?? */
  201. params.num_buffers = 22; /* ?? */
  202. params.context = smsdev;
  203. snprintf(params.devpath, sizeof(params.devpath),
  204. "sdio\\%s", sdio_func_id(func));
  205. params.sendrequest_handler = smssdio_sendrequest;
  206. params.device_type = sms_get_board(board_id)->type;
  207. if (params.device_type != SMS_STELLAR)
  208. params.flags |= SMS_DEVICE_FAMILY2;
  209. else {
  210. /*
  211. * FIXME: Stellar needs special handling...
  212. */
  213. ret = -ENODEV;
  214. goto free;
  215. }
  216. ret = smscore_register_device(&params, &smsdev->coredev);
  217. if (ret < 0)
  218. goto free;
  219. smscore_set_board_id(smsdev->coredev, board_id);
  220. sdio_claim_host(func);
  221. ret = sdio_enable_func(func);
  222. if (ret)
  223. goto release;
  224. ret = sdio_set_block_size(func, SMSSDIO_BLOCK_SIZE);
  225. if (ret)
  226. goto disable;
  227. ret = sdio_claim_irq(func, smssdio_interrupt);
  228. if (ret)
  229. goto disable;
  230. sdio_set_drvdata(func, smsdev);
  231. sdio_release_host(func);
  232. ret = smscore_start_device(smsdev->coredev);
  233. if (ret < 0)
  234. goto reclaim;
  235. return 0;
  236. reclaim:
  237. sdio_claim_host(func);
  238. sdio_release_irq(func);
  239. disable:
  240. sdio_disable_func(func);
  241. release:
  242. sdio_release_host(func);
  243. smscore_unregister_device(smsdev->coredev);
  244. free:
  245. kfree(smsdev);
  246. return ret;
  247. }
  248. static void smssdio_remove(struct sdio_func *func)
  249. {
  250. struct smssdio_device *smsdev;
  251. smsdev = sdio_get_drvdata(func);
  252. /* FIXME: racy! */
  253. if (smsdev->split_cb)
  254. smscore_putbuffer(smsdev->coredev, smsdev->split_cb);
  255. smscore_unregister_device(smsdev->coredev);
  256. sdio_claim_host(func);
  257. sdio_release_irq(func);
  258. sdio_disable_func(func);
  259. sdio_release_host(func);
  260. kfree(smsdev);
  261. }
  262. static struct sdio_driver smssdio_driver = {
  263. .name = "smssdio",
  264. .id_table = smssdio_ids,
  265. .probe = smssdio_probe,
  266. .remove = smssdio_remove,
  267. };
  268. /*******************************************************************/
  269. /* Module functions */
  270. /*******************************************************************/
  271. static int __init smssdio_module_init(void)
  272. {
  273. int ret = 0;
  274. printk(KERN_INFO "smssdio: Siano SMS1xxx SDIO driver\n");
  275. printk(KERN_INFO "smssdio: Copyright Pierre Ossman\n");
  276. ret = sdio_register_driver(&smssdio_driver);
  277. return ret;
  278. }
  279. static void __exit smssdio_module_exit(void)
  280. {
  281. sdio_unregister_driver(&smssdio_driver);
  282. }
  283. module_init(smssdio_module_init);
  284. module_exit(smssdio_module_exit);
  285. MODULE_DESCRIPTION("Siano SMS1xxx SDIO driver");
  286. MODULE_AUTHOR("Pierre Ossman");
  287. MODULE_LICENSE("GPL");