sd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*****************************************************************
  2. ** **
  3. ** Copyright (C) 2004 Amlogic,Inc. **
  4. ** All rights reserved **
  5. ** Filename : sd.c /Project:AVOS driver **
  6. ** Revision : 1.0 **
  7. ** **
  8. *****************************************************************/
  9. #include <linux/err.h>
  10. #include <linux/device.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/slab.h>
  13. #include <linux/interrupt.h>
  14. #include <mach/am_regs.h>
  15. #include <mach/irqs.h>
  16. #include <mach/card_io.h>
  17. #include <mach/mod_gate.h>
  18. #include <linux/cardreader/card_block.h>
  19. #include <linux/cardreader/cardreader.h>
  20. #include <linux/cardreader/sdio.h>
  21. #include "sd_misc.h"
  22. #include "sd_protocol.h"
  23. struct memory_card *card_find_card(struct card_host *host, u8 card_type);
  24. void sd_insert_detector(struct memory_card *card)
  25. {
  26. SD_MMC_Card_Info_t *sd_mmc_info = (SD_MMC_Card_Info_t *)card->card_info;
  27. int ret = sd_mmc_check_insert(sd_mmc_info);
  28. if(ret)
  29. card->card_status = CARD_INSERTED;
  30. else
  31. card->card_status = CARD_REMOVED;
  32. return;
  33. }
  34. void sd_open(struct memory_card *card)
  35. {
  36. int ret;
  37. struct aml_card_info *aml_card_info = card->card_plat_info;
  38. SD_MMC_Card_Info_t *sd_mmc_info = (SD_MMC_Card_Info_t *)card->card_info;
  39. #ifdef CONFIG_ARCH_MESON6
  40. switch_mod_gate_by_type(MOD_SDIO,1);
  41. #endif
  42. if (aml_card_info->card_extern_init)
  43. aml_card_info->card_extern_init();
  44. ret = sd_mmc_init(sd_mmc_info);
  45. if(ret)
  46. ret = sd_mmc_init(sd_mmc_info);
  47. if(ret)
  48. ret = sd_mmc_init(sd_mmc_info);
  49. card->capacity = sd_mmc_info->blk_nums;
  50. card->sdio_funcs = sd_mmc_info->sdio_function_nums;
  51. memcpy(card->raw_cid, &(sd_mmc_info->raw_cid), sizeof(card->raw_cid));
  52. if(sd_mmc_info->write_protected_flag)
  53. card->state |= CARD_STATE_READONLY;
  54. #ifdef CONFIG_ARCH_MESON6
  55. switch_mod_gate_by_type(MOD_SDIO,0);
  56. #endif
  57. if(ret)
  58. card->unit_state = CARD_UNIT_READY;
  59. else
  60. card->unit_state = CARD_UNIT_PROCESSED;
  61. return;
  62. }
  63. void sd_close(struct memory_card *card)
  64. {
  65. SD_MMC_Card_Info_t *sd_mmc_info = (SD_MMC_Card_Info_t *)card->card_info;
  66. if(!sd_mmc_info)
  67. {
  68. printk("error: no card to exit\n");
  69. return;
  70. }
  71. sd_mmc_exit(sd_mmc_info);
  72. sd_mmc_free(sd_mmc_info);
  73. sd_mmc_info = NULL;
  74. card->card_info = NULL;
  75. card->unit_state = CARD_UNIT_PROCESSED;
  76. return;
  77. }
  78. void sd_suspend(struct memory_card *card)
  79. {
  80. struct aml_card_info *aml_card_info = card->card_plat_info;
  81. struct card_host *host = card->host;
  82. SD_MMC_Card_Info_t *sd_mmc_info = (SD_MMC_Card_Info_t *)card->card_info;
  83. printk("***Entered %s:%s\n", __FILE__,__func__);
  84. __card_claim_host(host, card);
  85. if(card->card_type == CARD_SDIO)
  86. {
  87. card_release_host(host);
  88. return;
  89. }
  90. memset(sd_mmc_info, 0, sizeof(SD_MMC_Card_Info_t));
  91. if (card->host->dma_buf != NULL) {
  92. sd_mmc_info->sd_mmc_buf = card->host->dma_buf;
  93. sd_mmc_info->sd_mmc_phy_buf = card->host->dma_phy_buf;
  94. }
  95. card->card_io_init(card);
  96. sd_mmc_info->io_pad_type = aml_card_info->io_pad_type;
  97. sd_mmc_info->bus_width = SD_BUS_SINGLE;
  98. sd_mmc_info->sdio_clk_unit = 3000;
  99. sd_mmc_info->clks_nac = SD_MMC_TIME_NAC_DEFAULT;
  100. sd_mmc_info->max_blk_count = card->host->max_blk_count;
  101. card_release_host(host);
  102. }
  103. void sd_resume(struct memory_card *card)
  104. {
  105. printk("***Entered %s:%s\n", __FILE__,__func__);
  106. }
  107. static int sd_request(struct memory_card *card, struct card_blk_request *brq)
  108. {
  109. SD_MMC_Card_Info_t *sd_mmc_info = (SD_MMC_Card_Info_t *)card->card_info;
  110. unsigned int lba, byte_cnt,ret;
  111. unsigned char *data_buf;
  112. struct card_host *host = card->host;
  113. struct memory_card *sdio_card;
  114. SD_MMC_Card_Info_t *sdio_info;
  115. lba = brq->card_data.lba;
  116. byte_cnt = brq->card_data.blk_size * brq->card_data.blk_nums;
  117. data_buf = brq->crq.buf;
  118. if(sd_mmc_info == NULL){
  119. brq->card_data.error = SD_MMC_ERROR_NO_CARD_INS;
  120. printk("[sd_request] sd_mmc_info == NULL, return SD_MMC_ERROR_NO_CARD_INS\n");
  121. return 0;
  122. }
  123. if(!sd_mmc_info->blk_len){
  124. card->card_io_init(card);
  125. card->card_detector(card);
  126. if(card->card_status == CARD_REMOVED){
  127. brq->card_data.error = SD_MMC_ERROR_NO_CARD_INS;
  128. return 0;
  129. }
  130. #ifdef CONFIG_ARCH_MESON6
  131. switch_mod_gate_by_type(MOD_SDIO,1);
  132. #endif
  133. ret = sd_mmc_init(sd_mmc_info);
  134. #ifdef CONFIG_ARCH_MESON6
  135. switch_mod_gate_by_type(MOD_SDIO,0);
  136. #endif
  137. if(ret){
  138. brq->card_data.error = SD_MMC_ERROR_NO_CARD_INS;
  139. return 0;
  140. }
  141. }
  142. #ifdef CONFIG_ARCH_MESON6
  143. switch_mod_gate_by_type(MOD_SDIO,1);
  144. #endif
  145. sdio_card = card_find_card(host, CARD_SDIO);
  146. if (sdio_card) {
  147. sdio_close_host_interrupt(SDIO_IF_INT);
  148. sdio_info = (SD_MMC_Card_Info_t *)sdio_card->card_info;
  149. sd_gpio_enable(sdio_info->io_pad_type);
  150. }
  151. sd_sdio_enable(sd_mmc_info->io_pad_type);
  152. if(brq->crq.cmd == READ) {
  153. brq->card_data.error = sd_mmc_read_data(sd_mmc_info, lba, byte_cnt, data_buf);
  154. }
  155. else if(brq->crq.cmd == WRITE) {
  156. brq->card_data.error = sd_mmc_write_data(sd_mmc_info, lba, byte_cnt, data_buf);
  157. }
  158. sd_gpio_enable(sd_mmc_info->io_pad_type);
  159. sdio_card = card_find_card(host, CARD_SDIO);
  160. if(sdio_card) {
  161. sdio_info = (SD_MMC_Card_Info_t *)sdio_card->card_info;
  162. sd_sdio_enable(sdio_info->io_pad_type);
  163. if (sdio_info->sd_save_hw_io_flag) {
  164. WRITE_CBUS_REG(SDIO_CONFIG, sdio_info->sd_save_hw_io_config);
  165. WRITE_CBUS_REG(SDIO_MULT_CONFIG, sdio_info->sd_save_hw_io_mult_config);
  166. }
  167. sdio_open_host_interrupt(SDIO_IF_INT);
  168. }
  169. #ifdef CONFIG_ARCH_MESON6
  170. switch_mod_gate_by_type(MOD_SDIO,0);
  171. #endif
  172. return 0;
  173. }
  174. static int sdio_request(struct memory_card *card, struct card_blk_request *brq)
  175. {
  176. SD_MMC_Card_Info_t *sdio_info = (SD_MMC_Card_Info_t *)card->card_info;
  177. int incr_addr, err;
  178. unsigned addr, blocks, blksz, fn, read_after_write;
  179. u8 *in, *out, *buf;
  180. #ifdef CONFIG_ARCH_MESON6
  181. switch_mod_gate_by_type(MOD_SDIO,1);
  182. #endif
  183. //set_cpus_allowed_ptr(current, cpumask_of(0));
  184. sd_sdio_enable(sdio_info->io_pad_type);
  185. if (brq->crq.cmd & SDIO_OPS_REG) {
  186. WARN_ON(brq->card_data.blk_size != 1);
  187. WARN_ON(brq->card_data.blk_nums != 1);
  188. in = brq->crq.buf;
  189. addr = brq->card_data.lba;
  190. fn = brq->card_data.flags;
  191. out = brq->crq.back_buf;
  192. if (brq->crq.cmd & READ_AFTER_WRITE)
  193. read_after_write = 1;
  194. else
  195. read_after_write = 0;
  196. if ((brq->crq.cmd & 0x1 )== WRITE) {
  197. err = sdio_write_reg(sdio_info, fn, addr, in, read_after_write);
  198. if (err) {
  199. printk("sdio card write_reg failed %d at addr: %x \n", err, addr);
  200. brq->card_data.error = err;
  201. goto err;
  202. }
  203. }
  204. else {
  205. err = sdio_read_reg(sdio_info, fn, addr, out);
  206. if (err) {
  207. printk("sdio card read_reg failed %d at addr: %x \n", err, addr);
  208. brq->card_data.error = err;
  209. goto err;
  210. }
  211. }
  212. }
  213. else {
  214. if (brq->crq.cmd & SDIO_FIFO_ADDR)
  215. incr_addr = 1;
  216. else
  217. incr_addr = 0;
  218. buf = brq->crq.buf;
  219. addr = brq->card_data.lba;
  220. blksz = brq->card_data.blk_size;
  221. blocks = brq->card_data.blk_nums;
  222. fn = brq->card_data.flags;
  223. sdio_info->sdio_blk_len[fn] = card->sdio_func[fn-1]->cur_blksize;
  224. if ((brq->crq.cmd & 0x1)== WRITE) {
  225. err = sdio_write_data(sdio_info, fn, incr_addr, addr, blocks*blksz, buf);
  226. if (err) {
  227. printk("sdio card write_data failed %d at addr: %x, function: %d \n", err, addr, fn);
  228. brq->card_data.error = err;
  229. goto err;
  230. }
  231. }
  232. else {
  233. err = sdio_read_data(sdio_info, fn, incr_addr, addr, blocks*blksz, buf);
  234. if (err) {
  235. printk("sdio card read_data failed %d at addr: %x, function: %d\n", err, addr, fn);
  236. brq->card_data.error = err;
  237. goto err;
  238. }
  239. }
  240. }
  241. //sd_gpio_enable(sdio_info->io_pad_type);
  242. brq->card_data.error = 0;
  243. #ifdef CONFIG_ARCH_MESON6
  244. switch_mod_gate_by_type(MOD_SDIO,0);
  245. #endif
  246. return 0;
  247. err:
  248. #ifdef CONFIG_ARCH_MESON6
  249. switch_mod_gate_by_type(MOD_SDIO,0);
  250. #endif
  251. //sd_gpio_enable(sdio_info->io_pad_type);
  252. return err;
  253. }
  254. int sd_mmc_probe(struct memory_card *card)
  255. {
  256. struct aml_card_info *aml_card_info = card->card_plat_info;
  257. SD_MMC_Card_Info_t *sd_mmc_info = sd_mmc_malloc(sizeof(SD_MMC_Card_Info_t), GFP_KERNEL);
  258. if (sd_mmc_info == NULL)
  259. return -ENOMEM;
  260. if (card->host->dma_buf != NULL) {
  261. sd_mmc_info->sd_mmc_buf = card->host->dma_buf;
  262. sd_mmc_info->sd_mmc_phy_buf = card->host->dma_phy_buf;
  263. }
  264. card->card_info = sd_mmc_info;
  265. card->card_io_init = sd_io_init;
  266. card->card_detector = sd_insert_detector;
  267. card->card_insert_process = sd_open;
  268. card->card_remove_process = sd_close;
  269. card->card_request_process = sd_request;
  270. card->card_suspend = sd_suspend;
  271. card->card_resume = sd_resume;
  272. if (aml_card_info->card_extern_init)
  273. aml_card_info->card_extern_init();
  274. card->card_io_init(card);
  275. sd_mmc_prepare_init(sd_mmc_info);
  276. sd_mmc_info->io_pad_type = aml_card_info->io_pad_type;
  277. sd_mmc_info->bus_width = SD_BUS_SINGLE;
  278. sd_mmc_info->sdio_clk_unit = 3000;
  279. sd_mmc_info->clks_nac = SD_MMC_TIME_NAC_DEFAULT;
  280. sd_mmc_info->max_blk_count = card->host->max_blk_count;
  281. return 0;
  282. }
  283. int sdio_probe(struct memory_card *card)
  284. {
  285. struct aml_card_info *aml_card_info = card->card_plat_info;
  286. SD_MMC_Card_Info_t *sdio_info = sd_mmc_malloc(sizeof(SD_MMC_Card_Info_t), GFP_KERNEL);
  287. if (sdio_info == NULL)
  288. return -ENOMEM;
  289. if (card->host->dma_buf != NULL) {
  290. sdio_info->sd_mmc_buf = card->host->dma_buf;
  291. sdio_info->sd_mmc_phy_buf = card->host->dma_phy_buf;
  292. }
  293. card->card_info = sdio_info;
  294. card->card_io_init = sd_io_init;
  295. card->card_detector = sd_insert_detector;
  296. card->card_insert_process = sd_open;
  297. card->card_remove_process = sd_close;
  298. card->card_request_process = sdio_request;
  299. card->card_suspend = sd_suspend;
  300. card->card_resume = sd_resume;
  301. if (aml_card_info->card_extern_init)
  302. aml_card_info->card_extern_init();
  303. card->card_io_init(card);
  304. sd_mmc_prepare_init(sdio_info);
  305. sdio_info->io_pad_type = aml_card_info->io_pad_type;
  306. sdio_info->bus_width = SD_BUS_SINGLE;
  307. sdio_info->sdio_clk_unit = 3000;
  308. sdio_info->clks_nac = SD_MMC_TIME_NAC_DEFAULT;
  309. sdio_info->max_blk_count = card->host->max_blk_count;
  310. return 0;
  311. }
  312. #ifdef CONFIG_INAND
  313. int inand_probe(struct memory_card *card)
  314. {
  315. struct aml_card_info *aml_card_info = card->card_plat_info;
  316. SD_MMC_Card_Info_t *sdio_info = sd_mmc_malloc(sizeof(SD_MMC_Card_Info_t), GFP_KERNEL);
  317. if (sdio_info == NULL)
  318. return -ENOMEM;
  319. if (card->host->dma_buf != NULL) {
  320. sdio_info->sd_mmc_buf = card->host->dma_buf;
  321. sdio_info->sd_mmc_phy_buf = card->host->dma_phy_buf;
  322. }
  323. card->card_info = sdio_info;
  324. card->card_io_init = sd_io_init;
  325. card->card_detector = sd_insert_detector;
  326. card->card_insert_process = sd_open;
  327. card->card_remove_process = sd_close;
  328. card->card_request_process = sd_request;
  329. card->card_suspend = sd_suspend;
  330. card->card_resume = sd_resume;
  331. if (aml_card_info->card_extern_init)
  332. aml_card_info->card_extern_init();
  333. card->card_io_init(card);
  334. sd_mmc_prepare_init(sdio_info);
  335. sdio_info->io_pad_type = aml_card_info->io_pad_type;
  336. sdio_info->bus_width = SD_BUS_SINGLE;
  337. sdio_info->sdio_clk_unit = 3000;
  338. sdio_info->clks_nac = SD_MMC_TIME_NAC_DEFAULT;
  339. sdio_info->max_blk_count = card->host->max_blk_count;
  340. return 0;
  341. }
  342. #ifdef CONFIG_INAND_LP
  343. static int sd_part_request(struct memory_card *card,
  344. struct card_blk_request *brq)
  345. {
  346. int err;
  347. brq->card_data.lba += card->part_offset;
  348. WARN_ON(brq->card_data.lba+brq->card_data.blk_nums > card->capacity);
  349. err = sd_request(card, brq);
  350. return err;
  351. }
  352. int inand_lp_probe(struct memory_card *card)
  353. {
  354. int err;
  355. err = inand_probe(card);
  356. card->card_request_process = sd_part_request;
  357. return err;
  358. }
  359. #endif
  360. #endif
  361. MODULE_DESCRIPTION("Amlogic sd card Interface driver");
  362. MODULE_LICENSE("GPL");