smssdio.c 8.5 KB

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