smssdio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "smscoreapi.h"
  34. #include <linux/moduleparam.h>
  35. #include <linux/slab.h>
  36. #include <linux/firmware.h>
  37. #include <linux/delay.h>
  38. #include <linux/mmc/card.h>
  39. #include <linux/mmc/sdio_func.h>
  40. #include <linux/mmc/sdio_ids.h>
  41. #include <linux/module.h>
  42. #include "sms-cards.h"
  43. #include "smsendian.h"
  44. /* Registers */
  45. #define SMSSDIO_DATA 0x00
  46. #define SMSSDIO_INT 0x04
  47. #define SMSSDIO_BLOCK_SIZE 128
  48. static const struct sdio_device_id smssdio_ids[] = {
  49. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_STELLAR),
  50. .driver_data = SMS1XXX_BOARD_SIANO_STELLAR},
  51. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_A0),
  52. .driver_data = SMS1XXX_BOARD_SIANO_NOVA_A},
  53. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_B0),
  54. .driver_data = SMS1XXX_BOARD_SIANO_NOVA_B},
  55. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VEGA_A0),
  56. .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
  57. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VENICE),
  58. .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
  59. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x302),
  60. .driver_data = SMS1XXX_BOARD_SIANO_MING},
  61. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x500),
  62. .driver_data = SMS1XXX_BOARD_SIANO_PELE},
  63. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x600),
  64. .driver_data = SMS1XXX_BOARD_SIANO_RIO},
  65. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x700),
  66. .driver_data = SMS1XXX_BOARD_SIANO_DENVER_2160},
  67. {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x800),
  68. .driver_data = SMS1XXX_BOARD_SIANO_DENVER_1530},
  69. { /* end: all zeroes */ },
  70. };
  71. MODULE_DEVICE_TABLE(sdio, smssdio_ids);
  72. struct smssdio_device {
  73. struct sdio_func *func;
  74. struct smscore_device_t *coredev;
  75. struct smscore_buffer_t *split_cb;
  76. };
  77. /*******************************************************************/
  78. /* Siano core callbacks */
  79. /*******************************************************************/
  80. static int smssdio_sendrequest(void *context, void *buffer, size_t size)
  81. {
  82. int ret = 0;
  83. struct smssdio_device *smsdev;
  84. smsdev = context;
  85. sdio_claim_host(smsdev->func);
  86. smsendian_handle_tx_message((struct sms_msg_data *) buffer);
  87. while (size >= smsdev->func->cur_blksize) {
  88. ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
  89. buffer, smsdev->func->cur_blksize);
  90. if (ret)
  91. goto out;
  92. buffer += smsdev->func->cur_blksize;
  93. size -= smsdev->func->cur_blksize;
  94. }
  95. if (size) {
  96. ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
  97. buffer, size);
  98. }
  99. out:
  100. sdio_release_host(smsdev->func);
  101. return ret;
  102. }
  103. /*******************************************************************/
  104. /* SDIO callbacks */
  105. /*******************************************************************/
  106. static void smssdio_interrupt(struct sdio_func *func)
  107. {
  108. int ret;
  109. struct smssdio_device *smsdev;
  110. struct smscore_buffer_t *cb;
  111. struct sms_msg_hdr *hdr;
  112. size_t size;
  113. smsdev = sdio_get_drvdata(func);
  114. /*
  115. * The interrupt register has no defined meaning. It is just
  116. * a way of turning of the level triggered interrupt.
  117. */
  118. (void)sdio_readb(func, SMSSDIO_INT, &ret);
  119. if (ret) {
  120. pr_err("Unable to read interrupt register!\n");
  121. return;
  122. }
  123. if (smsdev->split_cb == NULL) {
  124. cb = smscore_getbuffer(smsdev->coredev);
  125. if (!cb) {
  126. pr_err("Unable to allocate data buffer!\n");
  127. return;
  128. }
  129. ret = sdio_memcpy_fromio(smsdev->func,
  130. cb->p,
  131. SMSSDIO_DATA,
  132. SMSSDIO_BLOCK_SIZE);
  133. if (ret) {
  134. pr_err("Error %d reading initial block!\n", ret);
  135. return;
  136. }
  137. hdr = cb->p;
  138. if (hdr->msg_flags & MSG_HDR_FLAG_SPLIT_MSG) {
  139. smsdev->split_cb = cb;
  140. return;
  141. }
  142. if (hdr->msg_length > smsdev->func->cur_blksize)
  143. size = hdr->msg_length - smsdev->func->cur_blksize;
  144. else
  145. size = 0;
  146. } else {
  147. cb = smsdev->split_cb;
  148. hdr = cb->p;
  149. size = hdr->msg_length - sizeof(struct sms_msg_hdr);
  150. smsdev->split_cb = NULL;
  151. }
  152. if (size) {
  153. void *buffer;
  154. buffer = cb->p + (hdr->msg_length - size);
  155. size = ALIGN(size, SMSSDIO_BLOCK_SIZE);
  156. BUG_ON(smsdev->func->cur_blksize != SMSSDIO_BLOCK_SIZE);
  157. /*
  158. * First attempt to transfer all of it in one go...
  159. */
  160. ret = sdio_memcpy_fromio(smsdev->func,
  161. buffer,
  162. SMSSDIO_DATA,
  163. size);
  164. if (ret && ret != -EINVAL) {
  165. smscore_putbuffer(smsdev->coredev, cb);
  166. pr_err("Error %d reading data from card!\n", ret);
  167. return;
  168. }
  169. /*
  170. * ..then fall back to one block at a time if that is
  171. * not possible...
  172. *
  173. * (we have to do this manually because of the
  174. * problem with the "increase address" bit)
  175. */
  176. if (ret == -EINVAL) {
  177. while (size) {
  178. ret = sdio_memcpy_fromio(smsdev->func,
  179. buffer, SMSSDIO_DATA,
  180. smsdev->func->cur_blksize);
  181. if (ret) {
  182. smscore_putbuffer(smsdev->coredev, cb);
  183. pr_err("Error %d reading data from card!\n",
  184. ret);
  185. return;
  186. }
  187. buffer += smsdev->func->cur_blksize;
  188. if (size > smsdev->func->cur_blksize)
  189. size -= smsdev->func->cur_blksize;
  190. else
  191. size = 0;
  192. }
  193. }
  194. }
  195. cb->size = hdr->msg_length;
  196. cb->offset = 0;
  197. smsendian_handle_rx_message((struct sms_msg_data *) cb->p);
  198. smscore_onresponse(smsdev->coredev, cb);
  199. }
  200. static int smssdio_probe(struct sdio_func *func,
  201. const struct sdio_device_id *id)
  202. {
  203. int ret;
  204. int board_id;
  205. struct smssdio_device *smsdev;
  206. struct smsdevice_params_t params;
  207. board_id = id->driver_data;
  208. smsdev = kzalloc(sizeof(struct smssdio_device), GFP_KERNEL);
  209. if (!smsdev)
  210. return -ENOMEM;
  211. smsdev->func = func;
  212. memset(&params, 0, sizeof(struct smsdevice_params_t));
  213. params.device = &func->dev;
  214. params.buffer_size = 0x5000; /* ?? */
  215. params.num_buffers = 22; /* ?? */
  216. params.context = smsdev;
  217. snprintf(params.devpath, sizeof(params.devpath),
  218. "sdio\\%s", sdio_func_id(func));
  219. params.sendrequest_handler = smssdio_sendrequest;
  220. params.device_type = sms_get_board(board_id)->type;
  221. if (params.device_type != SMS_STELLAR)
  222. params.flags |= SMS_DEVICE_FAMILY2;
  223. else {
  224. /*
  225. * FIXME: Stellar needs special handling...
  226. */
  227. ret = -ENODEV;
  228. goto free;
  229. }
  230. ret = smscore_register_device(&params, &smsdev->coredev, NULL);
  231. if (ret < 0)
  232. goto free;
  233. smscore_set_board_id(smsdev->coredev, board_id);
  234. sdio_claim_host(func);
  235. ret = sdio_enable_func(func);
  236. if (ret)
  237. goto release;
  238. ret = sdio_set_block_size(func, SMSSDIO_BLOCK_SIZE);
  239. if (ret)
  240. goto disable;
  241. ret = sdio_claim_irq(func, smssdio_interrupt);
  242. if (ret)
  243. goto disable;
  244. sdio_set_drvdata(func, smsdev);
  245. sdio_release_host(func);
  246. ret = smscore_start_device(smsdev->coredev);
  247. if (ret < 0)
  248. goto reclaim;
  249. return 0;
  250. reclaim:
  251. sdio_claim_host(func);
  252. sdio_release_irq(func);
  253. disable:
  254. sdio_disable_func(func);
  255. release:
  256. sdio_release_host(func);
  257. smscore_unregister_device(smsdev->coredev);
  258. free:
  259. kfree(smsdev);
  260. return ret;
  261. }
  262. static void smssdio_remove(struct sdio_func *func)
  263. {
  264. struct smssdio_device *smsdev;
  265. smsdev = sdio_get_drvdata(func);
  266. /* FIXME: racy! */
  267. if (smsdev->split_cb)
  268. smscore_putbuffer(smsdev->coredev, smsdev->split_cb);
  269. smscore_unregister_device(smsdev->coredev);
  270. sdio_claim_host(func);
  271. sdio_release_irq(func);
  272. sdio_disable_func(func);
  273. sdio_release_host(func);
  274. kfree(smsdev);
  275. }
  276. static struct sdio_driver smssdio_driver = {
  277. .name = "smssdio",
  278. .id_table = smssdio_ids,
  279. .probe = smssdio_probe,
  280. .remove = smssdio_remove,
  281. };
  282. /*******************************************************************/
  283. /* Module functions */
  284. /*******************************************************************/
  285. static int __init smssdio_module_init(void)
  286. {
  287. int ret = 0;
  288. printk(KERN_INFO "smssdio: Siano SMS1xxx SDIO driver\n");
  289. printk(KERN_INFO "smssdio: Copyright Pierre Ossman\n");
  290. ret = sdio_register_driver(&smssdio_driver);
  291. return ret;
  292. }
  293. static void __exit smssdio_module_exit(void)
  294. {
  295. sdio_unregister_driver(&smssdio_driver);
  296. }
  297. module_init(smssdio_module_init);
  298. module_exit(smssdio_module_exit);
  299. MODULE_DESCRIPTION("Siano SMS1xxx SDIO driver");
  300. MODULE_AUTHOR("Pierre Ossman");
  301. MODULE_LICENSE("GPL");