cardreader.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*****************************************************************
  2. ** **
  3. ** Copyright (C) 2004 Amlogic,Inc. **
  4. ** All rights reserved **
  5. ** Filename : cardreader.c /Project: driver **
  6. ** Revision : 1.0 **
  7. ** **
  8. *****************************************************************/
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/err.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/mii.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/mutex.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irqreturn.h>
  23. #include <linux/device.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/cardreader/sdio.h>
  27. #include <linux/cardreader/cardreader.h>
  28. #include <linux/cardreader/card_block.h>
  29. #include <linux/kthread.h>
  30. #include <asm/cacheflush.h>
  31. #include <mach/am_regs.h>
  32. #include <mach/irqs.h>
  33. #include <mach/card_io.h>
  34. #define card_list_to_card(l) container_of(l, struct memory_card, node)
  35. struct completion card_devadd_comp;
  36. struct amlogic_card_host
  37. {
  38. struct card_host *host;
  39. struct aml_card_platform *board_data;
  40. int present;
  41. /*
  42. * Flag indicating when the command has been sent. This is used to
  43. * work out whether or not to send the stop
  44. */
  45. unsigned int flags;
  46. /* flag for current bus settings */
  47. unsigned bus_mode;
  48. /* Latest in the scatterlist that has been enabled for transfer, but not freed */
  49. int in_use_index;
  50. /* Latest in the scatterlist that has been enabled for transfer */
  51. int transfer_index;
  52. };
  53. //wait_queue_head_t sdio_wait_event;
  54. extern void sdio_if_int_handler(struct card_host *host);
  55. extern void sdio_cmd_int_handle(struct memory_card *card);
  56. extern void sdio_timeout_int_handle(struct memory_card *card);
  57. static int card_reader_monitor(void *arg);
  58. void card_detect_change(struct card_host *host, unsigned long delay);
  59. struct card_host *card_alloc_host(int extra, struct device *dev);
  60. static void card_setup(struct card_host *host);
  61. static void amlogic_card_request(struct card_host *host, struct card_blk_request *brq);
  62. struct memory_card *card_find_card(struct card_host *host, u8 card_type);
  63. static struct memory_card *card_alloc_card(struct card_host *host)
  64. {
  65. struct memory_card *card;
  66. card = kzalloc(sizeof(struct memory_card), GFP_KERNEL);
  67. if (!card)
  68. return ERR_PTR(-ENOMEM);
  69. card_init_card(card, host);
  70. list_add(&card->node, &host->cards);
  71. return card;
  72. }
  73. #include <linux/init.h>
  74. static int use_sdxc = 0;
  75. static int __init using_sdxc(char *str)
  76. {
  77. if (*str)
  78. return 0;
  79. use_sdxc = 1;
  80. printk("using sdxc\n");
  81. return 1;
  82. }
  83. __setup("sdxc", using_sdxc);
  84. static void card_reader_initialize(struct card_host *host)
  85. {
  86. struct amlogic_card_host *aml_host = card_priv(host);
  87. struct aml_card_platform *card_platform = aml_host->board_data;
  88. struct aml_card_info *card_plat_info;
  89. struct memory_card *card;
  90. int i, err = 0;
  91. for (i=0; i<card_platform->card_num; i++) {
  92. card_plat_info = &card_platform->card_info[i];
  93. if (!strcmp("xd_card", card_plat_info->name)) {
  94. #ifdef CONFIG_XD
  95. card = card_find_card(host, CARD_XD_PICTURE);
  96. if (card)
  97. continue;
  98. card = card_alloc_card(host);
  99. if (!card)
  100. continue;
  101. card->unit_state = CARD_UNIT_NOT_READY;
  102. strcpy(card->name, CARD_XD_NAME_STR);
  103. card->card_type = CARD_XD_PICTURE;
  104. card->card_plat_info = card_plat_info;
  105. err = xd_probe(card);
  106. if (err)
  107. continue;
  108. #endif
  109. }
  110. else if (!strcmp("ms_card", card_plat_info->name)) {
  111. #ifdef CONFIG_MS_MSPRO
  112. card = card_find_card(host, CARD_MEMORY_STICK);
  113. if (card)
  114. continue;
  115. card = card_alloc_card(host);
  116. if (!card)
  117. continue;
  118. card->unit_state = CARD_UNIT_NOT_READY;
  119. strcpy(card->name, CARD_MS_NAME_STR);
  120. card->card_type = CARD_MEMORY_STICK;
  121. card->card_plat_info = card_plat_info;
  122. err = ms_probe(card);
  123. if (err)
  124. continue;
  125. #endif
  126. }
  127. else if (!strcmp("sd_card", card_plat_info->name)) {
  128. #ifdef CONFIG_SD_MMC
  129. card = card_find_card(host, CARD_SECURE_DIGITAL);
  130. if (card)
  131. continue;
  132. card = card_alloc_card(host);
  133. if (!card)
  134. continue;
  135. card->unit_state = CARD_UNIT_NOT_READY;
  136. strcpy(card->name, CARD_SD_NAME_STR);
  137. card->card_type = CARD_SECURE_DIGITAL;
  138. card->card_plat_info = card_plat_info;
  139. err = sd_mmc_probe(card);
  140. if (err)
  141. continue;
  142. #endif
  143. }
  144. else if (!strcmp("inand_card", card_plat_info->name)) {
  145. #ifdef CONFIG_INAND
  146. card = card_find_card(host, CARD_INAND);
  147. if (card)
  148. continue;
  149. card = card_alloc_card(host);
  150. if (!card)
  151. continue;
  152. card->unit_state = CARD_UNIT_NOT_READY;
  153. strcpy(card->name, CARD_INAND_NAME_STR);
  154. card->card_type = CARD_INAND;
  155. card->card_plat_info = card_plat_info;
  156. err = inand_probe(card);
  157. if (err)
  158. continue;
  159. #endif
  160. }
  161. else if (!strcmp("inand_card_lp", card_plat_info->name)) {
  162. #ifdef CONFIG_INAND_LP
  163. card = card_find_card(host, CARD_INAND_LP);
  164. if (card)
  165. continue;
  166. card = card_alloc_card(host);
  167. if (!card)
  168. continue;
  169. card->unit_state = CARD_UNIT_NOT_READY;
  170. strcpy(card->name, CARD_INAND_LASTP_NAME_STR);
  171. card->card_type = CARD_INAND_LP;
  172. card->card_plat_info = card_plat_info;
  173. err = inand_lp_probe(card);
  174. if (err)
  175. continue;
  176. #endif
  177. }
  178. else if (!strcmp("sdio_card", card_plat_info->name)) {
  179. #ifdef CONFIG_SDIO
  180. card = card_find_card(host, CARD_SDIO);
  181. if (card)
  182. continue;
  183. card = card_alloc_card(host);
  184. if (!card)
  185. continue;
  186. card->unit_state = CARD_UNIT_NOT_READY;
  187. strcpy(card->name, CARD_SDIO_NAME_STR);
  188. card->card_type = CARD_SDIO;
  189. card->card_plat_info = card_plat_info;
  190. err = sdio_probe(card);
  191. if (err)
  192. continue;
  193. #endif
  194. }
  195. else if (!strcmp("cf_card", card_plat_info->name)) {
  196. #ifdef CONFIG_CF
  197. card = card_find_card(host, CARD_COMPACT_FLASH);
  198. if (card)
  199. continue;
  200. card = card_alloc_card(host);
  201. if (!card)
  202. continue;
  203. card->unit_state = CARD_UNIT_NOT_READY;
  204. strcpy(card->name, CARD_CF_NAME_STR);
  205. card->card_type = CARD_COMPACT_FLASH;
  206. card->card_plat_info = card_plat_info;
  207. err = cf_probe(card);
  208. if (err)
  209. continue;
  210. #endif
  211. }
  212. }
  213. }
  214. static irqreturn_t sdio_interrupt_monitor(int irq, void *dev_id, struct pt_regs *regs)
  215. {
  216. struct card_host *host = (struct card_host *)dev_id;
  217. struct memory_card *card = host->card_busy;
  218. unsigned sdio_interrupt_resource;
  219. sdio_interrupt_resource = sdio_check_interrupt();
  220. switch (sdio_interrupt_resource) {
  221. case SDIO_IF_INT:
  222. sdio_if_int_handler(host);
  223. break;
  224. case SDIO_CMD_INT:
  225. sdio_cmd_int_handle(card);
  226. break;
  227. case SDIO_TIMEOUT_INT:
  228. sdio_timeout_int_handle(card);
  229. break;
  230. case SDIO_SOFT_INT:
  231. //AVDetachIrq(sdio_int_handler);
  232. //sdio_int_handler = -1;
  233. break;
  234. case SDIO_NO_INT:
  235. break;
  236. default:
  237. break;
  238. }
  239. return IRQ_HANDLED;
  240. }
  241. static irqreturn_t sdxc_interrupt_monitor(int irq, void *dev_id, struct pt_regs *regs)
  242. {
  243. struct card_host *host = (struct card_host *)dev_id;
  244. struct memory_card *card = host->card_busy;
  245. unsigned sdio_interrupt_resource;
  246. sdio_interrupt_resource = sdio_check_interrupt();
  247. switch (sdio_interrupt_resource) {
  248. case SDIO_IF_INT:
  249. sdio_if_int_handler(host);
  250. break;
  251. case SDIO_CMD_INT:
  252. sdio_cmd_int_handle(card);
  253. break;
  254. case SDIO_TIMEOUT_INT:
  255. sdio_timeout_int_handle(card);
  256. break;
  257. case SDIO_SOFT_INT:
  258. //AVDetachIrq(sdio_int_handler);
  259. //sdio_int_handler = -1;
  260. break;
  261. case SDIO_NO_INT:
  262. break;
  263. default:
  264. break;
  265. }
  266. return IRQ_HANDLED;
  267. }
  268. struct card_host *sdio_host;
  269. static int card_reader_init(struct card_host *host)
  270. {
  271. sdio_host = host;
  272. host->dma_buf = dma_alloc_coherent(NULL, host->max_req_size, (dma_addr_t *)&host->dma_phy_buf, GFP_KERNEL);
  273. if(host->dma_buf == NULL)
  274. return -ENOMEM;
  275. card_reader_initialize(host);
  276. host->card_task = kthread_run(card_reader_monitor, host, "card");
  277. if (!host->card_task)
  278. printk("card creat process failed\n");
  279. else
  280. printk("card creat process sucessful\n");
  281. if (request_irq(INT_SDIO, (irq_handler_t) sdio_interrupt_monitor, 0, "sd_mmc", host)) {
  282. printk("request SDIO irq error!!!\n");
  283. return -1;
  284. }
  285. if (request_irq(INT_WIFI_WATCHDOG, (irq_handler_t) sdxc_interrupt_monitor, 0, "sdxc_mmc", host)) {
  286. free_irq(INT_SDIO, host);
  287. printk("request SDXC irq error!!!\n");
  288. return -1;
  289. }
  290. #ifdef CONFIG_SDIO_HARD_IRQ
  291. host->caps |= CARD_CAP_SDIO_IRQ;
  292. #endif
  293. return 0;
  294. }
  295. static int card_reader_monitor(void *data)
  296. {
  297. unsigned card_type, card_4in1_init_type;
  298. struct card_host *card_host = (struct card_host *)data;
  299. struct memory_card *card = NULL;
  300. card_4in1_init_type = 0;
  301. daemonize("card_read_monitor");
  302. while(1) {
  303. msleep(200);
  304. if(card_host->card_task_state)
  305. {
  306. set_current_state(TASK_INTERRUPTIBLE);
  307. schedule();
  308. set_current_state(TASK_RUNNING);
  309. }
  310. for(card_type=CARD_XD_PICTURE; card_type<CARD_MAX_UNIT; card_type++) {
  311. card_reader_initialize(card_host);
  312. card = card_find_card(card_host, card_type);
  313. if (card == NULL)
  314. continue;
  315. __card_claim_host(card_host, card);
  316. card->card_io_init(card);
  317. card->card_detector(card);
  318. card_release_host(card_host);
  319. if((card->card_status == CARD_INSERTED) && (((card->unit_state != CARD_UNIT_READY)
  320. && ((card_type == CARD_SDIO) ||(card_type == CARD_INAND) ||(card_type == CARD_INAND_LP)
  321. || (card_host->slot_detector == CARD_REMOVED)||(card->card_slot_mode == CARD_SLOT_DISJUNCT)))
  322. ||(card->unit_state == CARD_UNIT_RESUMED))) {
  323. if(card->unit_state == CARD_UNIT_RESUMED){
  324. __card_claim_host(card_host, card);
  325. card->card_insert_process(card);
  326. card_release_host(card_host);
  327. card->unit_state = CARD_UNIT_READY;
  328. printk("monitor : resume\n");
  329. break;
  330. }
  331. __card_claim_host(card_host, card);
  332. card->card_insert_process(card);
  333. card_release_host(card_host);
  334. if(card->unit_state == CARD_UNIT_PROCESSED) {
  335. if(card->card_slot_mode == CARD_SLOT_4_1) {
  336. if (card_type != CARD_SDIO && card_type != CARD_INAND) {
  337. card_host->slot_detector = CARD_INSERTED;
  338. card_4in1_init_type = card_type;
  339. }
  340. }
  341. card->unit_state = CARD_UNIT_READY;
  342. card_host->card_type = card_type;
  343. if(!(card->state & CARD_STATE_PRESENT))
  344. card->state |= CARD_STATE_INITED;
  345. if (card_type == CARD_SDIO)
  346. card_host->card = card;
  347. card_detect_change(card_host, 0);
  348. printk("monitor : INSERT\n");
  349. }
  350. }
  351. else if((card->card_status == CARD_REMOVED) && ((card->unit_state != CARD_UNIT_NOT_READY)
  352. ||(card->unit_state == CARD_UNIT_RESUMED))){
  353. if(card->card_slot_mode == CARD_SLOT_4_1) {
  354. if (card_type == card_4in1_init_type)
  355. card_host->slot_detector = CARD_REMOVED;
  356. }
  357. card->card_remove_process(card);
  358. if(card->unit_state == CARD_UNIT_PROCESSED) {
  359. card->unit_state = CARD_UNIT_NOT_READY;
  360. printk("monitor : REMOVED\n");
  361. if(card) {
  362. list_del(&card->node);
  363. card_remove_card(card);
  364. }
  365. }
  366. }
  367. }
  368. }
  369. return 0;
  370. }
  371. static void card_deselect_cards(struct card_host *host)
  372. {
  373. if (host->card_selected)
  374. host->card_selected = NULL;
  375. }
  376. /*
  377. * Check whether cards we already know about are still present.
  378. * We do this by requesting status, and checking whether a card
  379. * responds.
  380. *
  381. * A request for status does not cause a state change in data
  382. * transfer mode.
  383. */
  384. static void card_check_cards(struct card_host *host)
  385. {
  386. //struct list_head *l, *n;
  387. card_deselect_cards(host);
  388. /*list_for_each_safe(l, n, &host->cards)
  389. {
  390. struct memory_card *card = card_list_to_card(l);
  391. if(card->card_type == host->card_type)
  392. continue;
  393. card->state = CARD_STATE_DEAD;
  394. } */
  395. }
  396. int __card_claim_host(struct card_host *host, struct memory_card *card)
  397. {
  398. DECLARE_WAITQUEUE(wait, current);
  399. unsigned long flags;
  400. int err = 0;
  401. add_wait_queue(&host->wq, &wait);
  402. spin_lock_irqsave(&host->lock, flags);
  403. while (1) {
  404. set_current_state(TASK_UNINTERRUPTIBLE);
  405. if (host->card_busy == NULL)
  406. break;
  407. spin_unlock_irqrestore(&host->lock, flags);
  408. schedule();
  409. spin_lock_irqsave(&host->lock, flags);
  410. }
  411. set_current_state(TASK_RUNNING);
  412. host->card_busy = card;
  413. if (!host->claimed)
  414. host->claimed = 1;
  415. spin_unlock_irqrestore(&host->lock, flags);
  416. remove_wait_queue(&host->wq, &wait);
  417. if (card != (void *)-1) {
  418. host->card_selected = card;
  419. }
  420. return err;
  421. }
  422. EXPORT_SYMBOL(__card_claim_host);
  423. void sdio_reinit(void)
  424. {
  425. struct memory_card* sdio_card = card_find_card(sdio_host, CARD_SDIO);
  426. __card_claim_host(sdio_host, sdio_card);
  427. sdio_card->card_io_init(sdio_card);
  428. sdio_card->card_detector(sdio_card);
  429. sdio_card->card_insert_process(sdio_card);
  430. sdio_card->unit_state = CARD_UNIT_READY;
  431. card_release_host(sdio_host);
  432. }
  433. EXPORT_SYMBOL(sdio_reinit);
  434. #ifdef CONFIG_SDIO
  435. int sdio_read_common_cis(struct memory_card *card);
  436. int sdio_read_func_cis(struct sdio_func *func);
  437. static int card_sdio_init_func(struct memory_card *card, unsigned int fn)
  438. {
  439. int ret = 0;
  440. struct sdio_func *func;
  441. BUG_ON(fn > SDIO_MAX_FUNCS);
  442. card_claim_host(card->host);
  443. func = sdio_alloc_func(card);
  444. if (IS_ERR(func))
  445. return PTR_ERR(func);
  446. func->num = fn;
  447. ret = sdio_read_func_cis(func);
  448. if (ret)
  449. goto fail;
  450. card_release_host(card->host);
  451. card->sdio_func[fn - 1] = func;
  452. return 0;
  453. fail:
  454. card_release_host(card->host);
  455. sdio_remove_func(func);
  456. return ret;
  457. }
  458. #if defined(CONFIG_MACH_MESON_8726M_REFB09)
  459. extern int amlogic_wifi_power(int on);
  460. #endif
  461. static int card_sdio_init_card(struct memory_card *card)
  462. {
  463. int err, i;
  464. /*read common cis for samsung nrx600 wifi*/
  465. card_claim_host(card->host);
  466. sdio_read_common_cis(card);
  467. card_release_host(card->host);
  468. for (i = 0; i < card->sdio_funcs; i++) {
  469. err = card_sdio_init_func(card, i + 1);
  470. if (err)
  471. return err;
  472. err = sdio_add_func(card->sdio_func[i]);
  473. if (err)
  474. return err;
  475. }
  476. #if defined(CONFIG_MACH_MESON_8726M_REFB09)
  477. amlogic_wifi_power(0);
  478. #endif
  479. return 0;
  480. }
  481. /*
  482. * Host is being removed. Free up the current card.
  483. */
  484. static void card_sdio_remove(struct card_host *host)
  485. {
  486. int i;
  487. BUG_ON(!host);
  488. BUG_ON(!host->card);
  489. for (i = 0;i < host->card->sdio_funcs;i++) {
  490. if (host->card->sdio_func[i]) {
  491. sdio_remove_func(host->card->sdio_func[i]);
  492. host->card->sdio_func[i] = NULL;
  493. }
  494. }
  495. card_remove_card(host->card);
  496. host->card = NULL;
  497. }
  498. #else
  499. static int card_sdio_init_func(struct memory_card *card, unsigned int fn)
  500. {
  501. return 0;
  502. }
  503. static int card_sdio_init_card(struct memory_card *card)
  504. {
  505. return 0;
  506. }
  507. /*
  508. * Host is being removed. Free up the current card.
  509. */
  510. static void card_sdio_remove(struct card_host *host)
  511. {
  512. }
  513. #endif
  514. /*
  515. * Locate a Memory card on this Memory host given a raw CID.
  516. */
  517. struct memory_card *card_find_card(struct card_host *host, u8 card_type)
  518. {
  519. struct memory_card *card;
  520. list_for_each_entry(card, &host->cards, node) {
  521. if (card->card_type == card_type)
  522. return card;
  523. }
  524. return NULL;
  525. }
  526. /**
  527. * card_add_host - initialise host hardware
  528. * @host: card host
  529. */
  530. int card_add_host(struct card_host *host)
  531. {
  532. int ret;
  533. ret = card_add_host_sysfs(host);
  534. /*if (ret == 0)
  535. {
  536. card_detect_change(host, 0);
  537. } */
  538. return ret;
  539. }
  540. /**
  541. * card_remove_host - remove host hardware
  542. * @host: card host
  543. *
  544. * Unregister and remove all cards associated with this host,
  545. * and power down the CARD bus.
  546. */
  547. void card_remove_host(struct card_host *host)
  548. {
  549. struct list_head *l, *n;
  550. list_for_each_safe(l, n, &host->cards) {
  551. struct memory_card *card = card_list_to_card(l);
  552. card_remove_card(card);
  553. }
  554. if (host->dma_buf != NULL)
  555. {
  556. dma_free_coherent(NULL, host->max_req_size, host->dma_buf, (dma_addr_t )host->dma_phy_buf);
  557. host->dma_buf = NULL;
  558. host->dma_phy_buf = NULL;
  559. }
  560. free_irq(INT_SDIO, host);
  561. free_irq(INT_WIFI_WATCHDOG, host);
  562. card_remove_host_sysfs(host);
  563. }
  564. /**
  565. * card_free_host - free the host structure
  566. * @host: card host
  567. *
  568. * Free the host once all references to it have been dropped.
  569. */
  570. void card_free_host(struct card_host *host)
  571. {
  572. card_flush_scheduled_work();
  573. card_free_host_sysfs(host);
  574. }
  575. static void card_discover_cards(struct card_host *host)
  576. {
  577. int err;
  578. struct memory_card *card;
  579. BUG_ON(host->card_busy == NULL);
  580. card = card_find_card(host, host->card_type);
  581. if (!card) {
  582. card = card_alloc_card(host);
  583. if (IS_ERR(card)) {
  584. err = PTR_ERR(card);
  585. }
  586. list_add(&card->node, &host->cards);
  587. }
  588. if (card->card_type == CARD_SDIO)
  589. host->card = card;
  590. card->state &= (~CARD_STATE_DEAD);
  591. }
  592. static void card_setup(struct card_host *host)
  593. {
  594. card_discover_cards(host);
  595. }
  596. /**
  597. * card_release_host - release a host
  598. * @host: card host to release
  599. *
  600. * Release a CARD host, allowing others to claim the host
  601. * for their operations.
  602. */
  603. void card_release_host(struct card_host *host)
  604. {
  605. unsigned long flags;
  606. BUG_ON(host->card_busy == NULL);
  607. spin_lock_irqsave(&host->lock, flags);
  608. host->card_busy = NULL;
  609. host->claimed = 0;
  610. spin_unlock_irqrestore(&host->lock, flags);
  611. wake_up(&host->wq);
  612. }
  613. static void card_reader_rescan(struct work_struct *work)
  614. {
  615. int err = 0;
  616. struct list_head *l, *n;
  617. struct card_host *host = container_of(work, struct card_host, detect);
  618. card_claim_host(host);
  619. card_setup(host);
  620. card_check_cards(host);
  621. card_release_host(host);
  622. list_for_each_safe(l, n, &host->cards) {
  623. struct memory_card *card = card_list_to_card(l);
  624. /*
  625. * If this is a new and good card, register it.
  626. */
  627. if ((!(card->state & CARD_STATE_PRESENT)) && (!(card->state & CARD_STATE_DEAD) && (card->state & CARD_STATE_INITED))) {
  628. if (card_register_card(card))
  629. card->state = CARD_STATE_DEAD;
  630. else
  631. card->state = CARD_STATE_PRESENT;
  632. if ((card->card_type == CARD_SDIO)) {
  633. err = card_sdio_init_card(card);
  634. if (err)
  635. card_sdio_remove(host);
  636. }
  637. if(card->card_type == CARD_INAND_LP)
  638. {
  639. err = card_init_inand_lp(card);
  640. if (err)
  641. card_remove_inand_lp(host);
  642. }
  643. }
  644. /*
  645. * If this card is dead, destroy it.
  646. */
  647. if (card->state == CARD_STATE_DEAD) {
  648. list_del(&card->node);
  649. card_remove_card(card);
  650. }
  651. }
  652. }
  653. struct card_host *card_alloc_host(int extra, struct device *dev)
  654. {
  655. struct card_host *host;
  656. host = card_alloc_host_sysfs(extra, dev);
  657. if (host) {
  658. spin_lock_init(&host->lock);
  659. init_waitqueue_head(&host->wq);
  660. INIT_LIST_HEAD(&host->cards);
  661. INIT_WORK(&host->detect, card_reader_rescan);
  662. /*
  663. * By default, hosts do not support SGIO or large requests.
  664. * They have to set these according to their abilities.
  665. */
  666. host->max_hw_segs = 1;
  667. host->max_phys_segs = 1;
  668. host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 5);
  669. host->max_seg_size = PAGE_CACHE_SIZE;
  670. host->max_blk_size = 512;
  671. ////host->max_blk_count = 256; //lin : for sdxc wr endian bug
  672. if (use_sdxc)
  673. host->max_blk_count = 256;
  674. else
  675. host->max_blk_count = 256;
  676. host->max_req_size = 512*256; /*for CONFIG_CARD_BLOCK_BOUNCE fix me*/
  677. printk("card max_req_size is %dK \n", host->max_req_size/1024);
  678. }
  679. return host;
  680. }
  681. int card_wait_for_req(struct card_host *host, struct card_blk_request *brq)
  682. {
  683. WARN_ON(host->card_busy == NULL);
  684. host->ops->request(host, brq);
  685. return 0;
  686. }
  687. EXPORT_SYMBOL(card_wait_for_req);
  688. /**
  689. * card_detect_change - process change of state on a memory card socket
  690. * @host: host which changed state.
  691. * @delay: optional delay to wait before detection (jiffies)
  692. *
  693. * All we know is that card(s) have been inserted or removed
  694. * from the socket(s). We don't know which socket or cards.
  695. */
  696. void card_detect_change(struct card_host *host, unsigned long delay)
  697. {
  698. /*if (delay)
  699. card_schedule_delayed_work(&host->detect, delay);
  700. else */
  701. init_completion(&card_devadd_comp);
  702. card_schedule_work(&host->detect);
  703. wait_for_completion(&card_devadd_comp);
  704. }
  705. EXPORT_SYMBOL(card_detect_change);
  706. static void amlogic_enable_sdio_irq(struct card_host *host, int enable)
  707. {
  708. if (enable)
  709. sdio_open_host_interrupt(SDIO_IF_INT);
  710. else
  711. sdio_close_host_interrupt(SDIO_IF_INT);
  712. return;
  713. }
  714. static struct card_host_ops amlogic_card_ops = {
  715. .request = amlogic_card_request,
  716. .enable_sdio_irq = amlogic_enable_sdio_irq,
  717. };
  718. struct card_host * the_card_host;
  719. static struct aml_card_info my_card_info[] = {
  720. [0] = {
  721. .name = "sd_card",
  722. .work_mode = CARD_HW_MODE,
  723. .io_pad_type = SDXC_CARD_0_5,
  724. .card_ins_en_reg = CARD_GPIO_ENABLE,
  725. .card_ins_en_mask = PREG_IO_29_MASK,
  726. .card_ins_input_reg = CARD_GPIO_INPUT,
  727. .card_ins_input_mask = PREG_IO_29_MASK,
  728. .card_power_en_reg = CARD_GPIO_ENABLE,
  729. .card_power_en_mask = PREG_IO_31_MASK,
  730. .card_power_output_reg = CARD_GPIO_OUTPUT,
  731. .card_power_output_mask = PREG_IO_31_MASK,
  732. .card_power_en_lev = 0,
  733. .card_wp_en_reg = 0,
  734. .card_wp_en_mask = 0,
  735. .card_wp_input_reg = 0,
  736. .card_wp_input_mask = 0,
  737. .card_extern_init = 0,
  738. },
  739. [1] = {
  740. .name = "inand_card",
  741. .work_mode = CARD_HW_MODE,
  742. .io_pad_type = SDXC_BOOT_0_11,
  743. .card_ins_en_reg = 0,
  744. .card_ins_en_mask = 0,
  745. .card_ins_input_reg = 0,
  746. .card_ins_input_mask = 0,
  747. .card_power_en_reg = BOOT_GPIO_ENABLE,
  748. .card_power_en_mask = PREG_IO_9_MASK,
  749. .card_power_output_reg = BOOT_GPIO_OUTPUT,
  750. .card_power_output_mask = PREG_IO_9_MASK,
  751. .card_power_en_lev = 1,
  752. .card_wp_en_reg = 0,
  753. .card_wp_en_mask = 0,
  754. .card_wp_input_reg = 0,
  755. .card_wp_input_mask = 0,
  756. .card_extern_init = 0,
  757. },
  758. /*
  759. [1] = {
  760. .name = "sdio_card",
  761. .work_mode = CARD_HW_MODE,
  762. .io_pad_type = SDIO_A_GPIOX_0_3,
  763. .card_ins_en_reg = 0,
  764. .card_ins_en_mask = 0,
  765. .card_ins_input_reg = 0,
  766. .card_ins_input_mask = 0,
  767. .card_power_en_reg = EGPIO_GPIOC_ENABLE,
  768. .card_power_en_mask = PREG_IO_7_MASK,
  769. .card_power_output_reg = EGPIO_GPIOC_OUTPUT,
  770. .card_power_output_mask = PREG_IO_7_MASK,
  771. .card_power_en_lev = 1,
  772. .card_wp_en_reg = 0,
  773. .card_wp_en_mask = 0,
  774. .card_wp_input_reg = 0,
  775. .card_wp_input_mask = 0,
  776. .card_extern_init = sdio_extern_init,
  777. },
  778. */
  779. };
  780. static int amlogic_card_probe(struct platform_device *pdev)
  781. {
  782. struct card_host *host;
  783. struct amlogic_card_host *aml_host;
  784. int ret;
  785. host = card_alloc_host(sizeof(struct amlogic_card_host), &pdev->dev);
  786. if (!host) {
  787. printk("Failed to allocate card host\n");
  788. return -ENOMEM;
  789. }
  790. the_card_host = host;
  791. host->ops = &amlogic_card_ops;
  792. aml_host = card_priv(host);
  793. aml_host->host = host;
  794. aml_host->bus_mode = 0;
  795. aml_host->board_data = pdev->dev.platform_data;
  796. if (use_sdxc) {
  797. aml_host->board_data->card_info = my_card_info;
  798. aml_host->board_data->card_num = ARRAY_SIZE(my_card_info);
  799. }
  800. platform_set_drvdata(pdev, host);
  801. /*
  802. * Add host to CARD layer
  803. */
  804. ret = card_add_host(host);
  805. /*
  806. * monitor card insertion/removal if we can
  807. */
  808. ret = card_reader_init(host);
  809. if (ret) {
  810. card_free_host(host);
  811. return ret;
  812. }
  813. return 0;
  814. }
  815. /*
  816. * Remove a device
  817. */
  818. static int amlogic_card_remove(struct platform_device *pdev)
  819. {
  820. struct card_host *card = platform_get_drvdata(pdev);
  821. struct amlogic_card_host *host;
  822. if (!card)
  823. return -1;
  824. host = card_priv(card);
  825. card_remove_host(card);
  826. card_free_host(card);
  827. platform_set_drvdata(pdev, NULL);
  828. return 0;
  829. }
  830. static void amlogic_card_request(struct card_host *host, struct card_blk_request *brq)
  831. {
  832. struct memory_card *card = host->card_busy;
  833. BUG_ON(card == NULL);
  834. card->card_request_process(card, brq);
  835. return;
  836. }
  837. static struct platform_driver amlogic_card_driver = {
  838. .probe = amlogic_card_probe,
  839. .remove = amlogic_card_remove,
  840. .driver =
  841. {
  842. .name = "AMLOGIC_CARD",
  843. .owner = THIS_MODULE,
  844. },
  845. };
  846. static int __init amlogic_card_init(void)
  847. {
  848. return platform_driver_register(&amlogic_card_driver);
  849. }
  850. static void __exit amlogic_card_exit(void)
  851. {
  852. platform_driver_unregister(&amlogic_card_driver);
  853. }
  854. module_init(amlogic_card_init);
  855. module_exit(amlogic_card_exit);
  856. MODULE_DESCRIPTION("Amlogic Memory Card Interface driver");
  857. MODULE_LICENSE("GPL");