aml_sd_mmc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*******************************************************************
  2. *
  3. * Copyright C 2005 by Amlogic, Inc. All Rights Reserved.
  4. *
  5. * This file is licensed under the terms of the GNU General Public License
  6. * version 2. This program is licensed "as is" without any warranty of any
  7. * kind, whether express or implied.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/timer.h>
  19. #include <linux/clk.h>
  20. #include <linux/mmc/host.h>
  21. #include <linux/mmc/core.h>
  22. #include <linux/io.h>
  23. #include <linux/semaphore.h>
  24. #include <linux/gpio.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/mmc/mmc.h>
  27. #include <linux/mmc/sdio.h>
  28. #include <linux/mmc/sd.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/genhd.h>
  31. #include <mach/hardware.h>
  32. #include <plat/regops.h>
  33. //#include <plat/mmc.h>
  34. //#include <plat/cpu.h>
  35. #include <linux/slab.h>
  36. #include <mach/mmc.h>
  37. #include <asm/cacheflush.h>
  38. #include <asm/dma-mapping.h>
  39. #include <asm/outercache.h>
  40. #include <mach/power_gate.h>
  41. //#define dbg_print(a...) printk(a);
  42. #define dbg_print(a...)
  43. //#define DBG_LINE_INFO() printk(KERN_INFO "[%s] : %s\n",__func__,__FILE__);
  44. #define DBG_LINE_INFO()
  45. #define dev_err(a,s) printk(KERN_INFO s);
  46. #define AML_MMC_DISABLED_TIMEOUT 100
  47. #define AML_MMC_SLEEP_TIMEOUT 1000
  48. #define AML_MMC_OFF_TIMEOUT 8000
  49. #define MMC_TIMEOUT_MS 20
  50. #define DRIVER_NAME "aml_sd_mmc"
  51. /**
  52. * enable IRQ, after having disabled it.
  53. * @host: The device state.
  54. * @more: True if more IRQs are expected from transfer.
  55. *
  56. * Enable the main IRQ if needed after it has been disabled.
  57. *
  58. * The IRQ can be one of the following states:
  59. * - disabled during IDLE
  60. * - disabled whilst processing data
  61. * - enabled during transfer
  62. * - enabled whilst awaiting SDIO interrupt detection
  63. */
  64. #if 0
  65. static void aml_sd_enable_irq(struct aml_sd_host *host, bool more)
  66. {
  67. unsigned long flags;
  68. bool enable = false;
  69. local_irq_save(flags);
  70. host->irq_enabled = more;
  71. host->irq_disabled = false;
  72. enable = more | host->sdio_irqen;
  73. if (host->irq_state != enable) {
  74. host->irq_state = enable;
  75. if (enable)
  76. enable_irq(host->irq);
  77. else
  78. disable_irq(host->irq);
  79. }
  80. local_irq_restore(flags);
  81. }
  82. static void aml_sd_disable_irq(struct aml_sd_host *host, bool transfer)
  83. {
  84. unsigned long flags;
  85. DBG_LINE_INFO();
  86. local_irq_save(flags);
  87. host->irq_disabled = transfer;
  88. if (transfer && host->irq_state) {
  89. host->irq_state = false;
  90. disable_irq(host->irq);
  91. }
  92. local_irq_restore(flags);
  93. }
  94. /**
  95. * test whether the SDIO IRQ is being signalled
  96. * @host: The host to check.
  97. *
  98. * Test to see if the SDIO interrupt is being signalled in case the
  99. * controller has failed to re-detect a card interrupt. Read GPE8 and
  100. * see if it is low and if so, signal a SDIO interrupt.
  101. *
  102. * This is currently called if a request is finished (we assume that the
  103. * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
  104. * already being indicated.
  105. */
  106. static void aml_sd_check_sdio_irq(struct aml_sd_host *host)
  107. {
  108. // DBG_LINE_INFO();
  109. if (host->sdio_irqen) {
  110. // if (gpio_get_value(S3C2410_GPE(8)) == 0) {
  111. // printk(KERN_DEBUG "%s: signalling irq\n", __func__);
  112. mmc_signal_sdio_irq(host->mmc);
  113. // }
  114. }
  115. }
  116. #endif
  117. #ifdef CONFIG_PM
  118. static int aml_mmc_suspend(struct platform_device *pdev)
  119. {
  120. struct mmc_host *mmc = platform_get_drvdata(pdev);
  121. printk(KERN_INFO "***Entered %s:%s\n", __FILE__,__func__);
  122. // CLK_GATE_OFF(SDIO);
  123. return mmc_suspend_host(mmc);
  124. }
  125. static int aml_mmc_resume(struct platform_device *pdev)
  126. {
  127. struct mmc_host *mmc = platform_get_drvdata(pdev);
  128. //clock gate on
  129. printk(KERN_INFO "***Entered %s:%s\n", __FILE__,__func__);
  130. // CLK_GATE_ON(SDIO);
  131. return mmc_resume_host(mmc);
  132. }
  133. #else
  134. #define aml_mmc_suspend NULL
  135. #define aml_mmc_resume NULL
  136. #endif
  137. #if 0
  138. #ifdef CONFIG_PM
  139. /*
  140. * Restore the MMC host context, if it was lost as result of a
  141. * power state change.
  142. */
  143. static int aml_sd_context_restore(struct aml_sd_host *host)
  144. {
  145. struct mmc_ios *ios = &host->mmc->ios;
  146. struct aml_mmc_platform_data *pdata = host->pdata;
  147. return 0;
  148. }
  149. /*
  150. * Save the MMC host context (store the number of power state changes so far).
  151. */
  152. static void aml_sd_context_save(struct aml_sd_host *host)
  153. {
  154. #if 0
  155. struct aml_mmc_platform_data *pdata = host->pdata;
  156. int context_loss;
  157. if (pdata->get_context_loss_count) {
  158. context_loss = pdata->get_context_loss_count(host->dev);
  159. if (context_loss < 0)
  160. return;
  161. host->context_loss = context_loss;
  162. }
  163. #endif
  164. }
  165. #else
  166. static int aml_sd_context_restore(struct aml_sd_host *host)
  167. {
  168. return 0;
  169. }
  170. static void aml_sd_context_save(struct aml_sd_host *host)
  171. {
  172. }
  173. #endif
  174. #endif
  175. /*
  176. * Configure the response type and send the cmd.
  177. */
  178. //
  179. static int aml_mmc_check_response(struct mmc_command *cmd)
  180. {
  181. // DBG_LINE_INFO();
  182. int ret = (int)SD_NO_ERROR;
  183. SD_Response_R1_t * r1 = (SD_Response_R1_t*)cmd->resp;
  184. if(cmd->opcode == SD_SEND_IF_COND ||
  185. cmd->opcode == SD_SEND_RELATIVE_ADDR)
  186. return ret;
  187. switch (mmc_resp_type(cmd)) {
  188. case MMC_RSP_R1:
  189. case MMC_RSP_R1B:
  190. if (r1->card_status.OUT_OF_RANGE)
  191. return SD_ERROR_OUT_OF_RANGE;
  192. else if (r1->card_status.ADDRESS_ERROR)
  193. return SD_ERROR_ADDRESS;
  194. else if (r1->card_status.BLOCK_LEN_ERROR)
  195. return SD_ERROR_BLOCK_LEN;
  196. else if (r1->card_status.ERASE_SEQ_ERROR)
  197. return SD_ERROR_ERASE_SEQ;
  198. else if (r1->card_status.ERASE_PARAM)
  199. return SD_ERROR_ERASE_PARAM;
  200. else if (r1->card_status.WP_VIOLATION)
  201. return SD_ERROR_WP_VIOLATION;
  202. else if (r1->card_status.CARD_IS_LOCKED)
  203. return SD_ERROR_CARD_IS_LOCKED;
  204. else if (r1->card_status.LOCK_UNLOCK_FAILED)
  205. return SD_ERROR_LOCK_UNLOCK_FAILED;
  206. else if (r1->card_status.COM_CRC_ERROR)
  207. return SD_ERROR_COM_CRC;
  208. else if (r1->card_status.ILLEGAL_COMMAND)
  209. return SD_ERROR_ILLEGAL_COMMAND;
  210. else if (r1->card_status.CARD_ECC_FAILED)
  211. return SD_ERROR_CARD_ECC_FAILED;
  212. else if (r1->card_status.CC_ERROR)
  213. return SD_ERROR_CC;
  214. else if (r1->card_status.ERROR)
  215. return SD_ERROR_GENERAL;
  216. else if (r1->card_status.CID_CSD_OVERWRITE)
  217. return SD_ERROR_CID_CSD_OVERWRITE;
  218. else if (r1->card_status.AKE_SEQ_ERROR)
  219. if(cmd->opcode != SD_SEND_IF_COND)
  220. return SD_ERROR_AKE_SEQ;
  221. break;
  222. default:
  223. break;
  224. }
  225. return ret;
  226. }
  227. static int mmc_wait_busy(unsigned timeout)
  228. {
  229. unsigned r;
  230. // unsigned timebase=gettimer(0);
  231. int timercount = timeout;
  232. while(timercount > 0)
  233. {
  234. r = aml_read_reg32(P_SDIO_STATUS_IRQ);
  235. if(((r >> sdio_cmd_busy_bit) & 0x1) == 0)
  236. {
  237. return 0;
  238. }
  239. timercount --;
  240. udelay(8);
  241. }
  242. return -1;
  243. }
  244. static void mmc_soft_reset(void)
  245. {
  246. aml_write_reg32(P_SDIO_IRQ_CONFIG,aml_read_reg32(P_SDIO_IRQ_CONFIG)|(1<<soft_reset_bit));
  247. aml_write_reg32(P_SDIO_STATUS_IRQ,((1<<8) | (1<<9)));
  248. udelay(10);
  249. }
  250. static int
  251. aml_sd_send_command(struct aml_sd_host *host, struct mmc_command *cmd)
  252. {
  253. // DBG_LINE_INFO();
  254. struct mmc_host* mmc = host->mmc;
  255. int ret = ERROR_NONE, num_res;
  256. unsigned int cmd_send = 0;
  257. SDHW_CMD_Send_Reg_t *cmd_send_reg = (void *)&cmd_send;
  258. unsigned int cmd_ext = 0;
  259. SDHW_Extension_Reg_t *cmd_ext_reg = (void *)&cmd_ext;
  260. unsigned int status_irq = 0;
  261. SDIO_Status_IRQ_Reg_t *status_irq_reg = (void *)&status_irq;
  262. struct mmc_data* data = cmd->data;
  263. bool bread_command = 0;
  264. unsigned int timeout;
  265. unsigned int data_temp;
  266. unsigned int loop_num;
  267. unsigned char respone[18];
  268. dbg_print("[cmd: %d ]\n",cmd->opcode);
  269. cmd_send_reg->cmd_data = 0x40 | cmd->opcode;
  270. cmd_send_reg->use_int_window = 1;
  271. /* check read/write address overflow */
  272. switch (cmd->opcode) {
  273. case MMC_READ_SINGLE_BLOCK:
  274. case MMC_READ_MULTIPLE_BLOCK:
  275. case MMC_WRITE_BLOCK:
  276. case MMC_WRITE_MULTIPLE_BLOCK:
  277. {
  278. unsigned capacity = 0;
  279. struct mmc_card* card = mmc->card;
  280. if(card){
  281. capacity = card->csd.capacity << (card->csd.read_blkbits - 9);
  282. ret = (capacity * data->blksz <= cmd->arg);
  283. dbg_print("mmc->ocr:%d capacity:%x data->blksz:%d cmd->arg:%x ret:%d\n",
  284. mmc->ocr,capacity,data->blksz,cmd->arg,ret);
  285. if(ret){
  286. printk(KERN_INFO "!!! please check capacity. capacity:%x cmd->arg:%x\n",capacity,cmd->arg);
  287. return -1;
  288. }
  289. }
  290. }
  291. break;
  292. }
  293. switch (mmc_resp_type(cmd)) {
  294. case MMC_RSP_R1:
  295. case MMC_RSP_R1B:
  296. case MMC_RSP_R3:
  297. cmd_send_reg->cmd_res_bits = 45; // RESPONSE have 7(cmd)+32(respnse)+7(crc)-1 data
  298. break;
  299. case MMC_RSP_R2:
  300. cmd_send_reg->cmd_res_bits = 133; // RESPONSE have 7(cmd)+120(respnse)+7(crc)-1 data
  301. cmd_send_reg->res_crc7_from_8 = 1;
  302. break;
  303. default:
  304. cmd_send_reg->cmd_res_bits = 0; // NO_RESPONSE
  305. break;
  306. }
  307. switch (cmd->opcode) {
  308. case MMC_READ_SINGLE_BLOCK:
  309. case MMC_READ_MULTIPLE_BLOCK:
  310. case MMC_SEND_EXT_CSD: //same as: SD_CMD_SEND_IF_COND
  311. case MMC_SWITCH: //same as: MMC_CMD_SWITCH
  312. if(!data)
  313. break;
  314. #if 0
  315. {
  316. int j;
  317. // char* pv = sg_virt(data->sg);
  318. // for(j = 0; j < data->sg->length; j++){
  319. // pv[j] = 3;
  320. // }
  321. }
  322. #endif
  323. cmd_send_reg->res_with_data = 1;
  324. cmd_send_reg->repeat_package_times = data->blocks - 1;
  325. if(host->bus_width == SD_BUS_WIDE)
  326. cmd_ext_reg->data_rw_number = data->blksz * 8 + (16 - 1) * 4;
  327. else
  328. cmd_ext_reg->data_rw_number = data->blksz * 8 + 16 - 1;
  329. bread_command = 1;
  330. dma_sync_sg_for_device(host->dev,data->sg,data->sg_len,DMA_FROM_DEVICE);
  331. break;
  332. case MMC_WRITE_BLOCK:
  333. case MMC_WRITE_MULTIPLE_BLOCK:
  334. if(!data)
  335. break;
  336. cmd_send_reg->cmd_send_data = 1;
  337. cmd_send_reg->repeat_package_times = data->blocks - 1;
  338. if(host->bus_width == SD_BUS_WIDE)
  339. cmd_ext_reg->data_rw_number = data->blksz * 8 + (16 - 1) * 4;
  340. else
  341. cmd_ext_reg->data_rw_number = data->blksz * 8 + 16 - 1;
  342. dma_sync_sg_for_device(host->dev,data->sg,data->sg_len,DMA_TO_DEVICE);
  343. // dcache_clean_range(buffer,data->blocks<<9);
  344. break;
  345. case SD_APP_SEND_SCR:
  346. if(!data)
  347. break;
  348. bread_command = 1;
  349. cmd_send_reg->res_with_data = 1;
  350. if(host->bus_width == SD_BUS_WIDE)
  351. cmd_ext_reg->data_rw_number = data->blksz * 8 + (16 - 1) * 4;
  352. else
  353. cmd_ext_reg->data_rw_number = data->blksz * 8 + 16 - 1;
  354. dma_sync_sg_for_device(host->dev,data->sg,data->sg_len,DMA_FROM_DEVICE);
  355. break;
  356. default:
  357. break;
  358. }
  359. //cmd with R1b
  360. switch (cmd->opcode) {
  361. case MMC_STOP_TRANSMISSION:
  362. cmd_send_reg->check_dat0_busy = 1;
  363. break;
  364. default:
  365. break;
  366. }
  367. //cmd with R3
  368. switch (cmd->opcode) {
  369. case MMC_SEND_OP_COND:
  370. case SD_APP_OP_COND:
  371. cmd_send_reg->res_without_crc7 = 1;
  372. // cmd_ext = 0x8300;
  373. break;
  374. default:
  375. break;
  376. }
  377. timeout = TIMEOUT_SHORT;
  378. if(data && data->timeout_ns > 10000)
  379. timeout = data->timeout_ns /1000;
  380. if(data && data->sg){
  381. int i;
  382. struct scatterlist *sg;
  383. int nsgs = 0;
  384. unsigned address = cmd->arg;
  385. unsigned status_bk = status_irq;
  386. unsigned cmd_ext_bk = cmd_ext;
  387. unsigned cmd_send_bk = cmd_send;
  388. unsigned buffer;
  389. for_each_sg(data->sg, sg, data->sg_len, i){
  390. cmd_send = cmd_send_bk;
  391. cmd_ext = cmd_ext_bk;
  392. status_irq = status_bk;
  393. nsgs++;
  394. cmd_send_reg->repeat_package_times = (sg->length-1)/data->blksz;
  395. buffer = sg_phys(sg);
  396. status_irq_reg->if_int = 1;
  397. status_irq_reg->cmd_int = 1;
  398. dbg_print("P_SDIO_STATUS_IRQ=%x P_SDIO_M_ADDR=%x P_CMD_SEND=%x\n",status_irq,buffer,cmd_send);
  399. dbg_print("P_CMD_ARGUMENT =%x P_SDIO_EXTENSION=%x\n",cmd->arg,cmd_ext);
  400. dbg_print("address:%x[%x] length:%x blocks:%d \n",address,address * data->blksz,sg->length, cmd_send_reg->repeat_package_times);
  401. aml_write_reg32(P_SDIO_STATUS_IRQ,status_irq|(0x1fff<<19));
  402. aml_write_reg32(P_SDIO_EXTENSION,cmd_ext);
  403. aml_write_reg32(P_SDIO_M_ADDR,(unsigned int)buffer);
  404. aml_write_reg32(P_CMD_ARGUMENT,address);
  405. aml_write_reg32(P_CMD_SEND,cmd_send);
  406. if(mmc_wait_busy(timeout) !=0){
  407. printk(KERN_INFO "[mmc cmd %d] time out!\n",cmd->opcode);
  408. return SD_ERROR_TIMEOUT;
  409. }
  410. address += cmd_send_reg->repeat_package_times+1;
  411. //send command stop
  412. if(data->sg_len > 1 && nsgs < data->sg_len){
  413. aml_write_reg32(P_SDIO_STATUS_IRQ,status_irq|(0x1fff<<19));
  414. aml_write_reg32(P_SDIO_EXTENSION,0);
  415. aml_write_reg32(P_SDIO_M_ADDR,0);
  416. aml_write_reg32(P_CMD_ARGUMENT,0);
  417. aml_write_reg32(P_CMD_SEND,0x282d4c);
  418. if(mmc_wait_busy(TIMEOUT_SHORT) !=0){
  419. printk(KERN_INFO"[mmc cmd %d] time out!\n",MMC_STOP_TRANSMISSION);
  420. return SD_ERROR_TIMEOUT;
  421. }//wait
  422. }//if
  423. }//for each sg
  424. }//if
  425. else{
  426. status_irq_reg->if_int = 1;
  427. status_irq_reg->cmd_int = 1;
  428. dbg_print("P_SDIO_STATUS_IRQ=%x P_SDIO_M_ADDR=%x P_CMD_SEND=%x\n",status_irq,0,cmd_send);
  429. dbg_print("P_CMD_ARGUMENT =%x P_SDIO_EXTENSION=%x\n",cmd->arg,cmd_ext);
  430. aml_write_reg32(P_SDIO_STATUS_IRQ,status_irq|(0x1fff<<19));
  431. aml_write_reg32(P_SDIO_EXTENSION,cmd_ext);
  432. aml_write_reg32(P_SDIO_M_ADDR,0);
  433. aml_write_reg32(P_CMD_ARGUMENT,cmd->arg);
  434. aml_write_reg32(P_CMD_SEND,cmd_send);
  435. if(mmc_wait_busy(timeout) !=0){
  436. printk(KERN_INFO "[mmc cmd %d] time out!\n",cmd->opcode);
  437. return SD_ERROR_TIMEOUT;
  438. }
  439. }
  440. status_irq = aml_read_reg32(P_SDIO_STATUS_IRQ);
  441. if(cmd_send_reg->cmd_res_bits && !cmd_send_reg->res_without_crc7 && !status_irq_reg->res_crc7_ok)
  442. return SD_ERROR_COM_CRC;
  443. switch (mmc_resp_type(cmd)) {
  444. case MMC_RSP_R1:
  445. case MMC_RSP_R1B:
  446. case MMC_RSP_R3:
  447. num_res = RESPONSE_R1_R3_R6_R7_LENGTH;
  448. break;
  449. case MMC_RSP_R2:
  450. num_res = RESPONSE_R2_CID_CSD_LENGTH;
  451. break;
  452. default:
  453. num_res = RESPONSE_R4_R5_NONE_LENGTH;
  454. break;
  455. }
  456. if (num_res > 0) {
  457. unsigned int multi_config = 0;
  458. SDIO_Multi_Config_Reg_t *multi_config_reg = (void *)&multi_config;
  459. multi_config_reg->write_read_out_index = 1;
  460. aml_write_reg32(P_SDIO_MULT_CONFIG,multi_config);
  461. num_res--; // Minus CRC byte
  462. }
  463. loop_num = (num_res + 3 - 1) /4;
  464. while (num_res > 0) {
  465. data_temp = aml_read_reg32(P_CMD_ARGUMENT);
  466. if(num_res <= 1)
  467. break;
  468. respone[--num_res - 1] = data_temp & 0xFF;
  469. if(num_res <= 1)
  470. break;
  471. respone[--num_res - 1] = (data_temp >> 8) & 0xFF;
  472. if(num_res <= 1)
  473. break;
  474. respone[--num_res - 1] = (data_temp >> 16) & 0xFF;
  475. if(num_res <= 1)
  476. break;
  477. respone[--num_res - 1] = (data_temp >> 24) & 0xFF;
  478. }
  479. while (loop_num--) {
  480. ((uint *)cmd->resp)[loop_num] = __be32_to_cpu(((uint *)respone)[loop_num]);
  481. dbg_print("[mmc cmd:%d] response[%d]:%x\n",cmd->opcode,loop_num,((uint *)cmd->resp)[loop_num]);
  482. }
  483. //check_response
  484. ret = aml_mmc_check_response(cmd);
  485. if(ret)
  486. {
  487. printk(KERN_INFO "[mmc cmd:%d] check response failed!\n",cmd->opcode);
  488. return ret;
  489. }
  490. switch (cmd->opcode) {
  491. case MMC_READ_SINGLE_BLOCK:
  492. case MMC_READ_MULTIPLE_BLOCK:
  493. case MMC_SEND_EXT_CSD: //same as SD_CMD_SEND_IF_COND
  494. case MMC_SWITCH: //same as: MMC_CMD_SWITCH
  495. if(!data)
  496. break;
  497. if(!status_irq_reg->data_read_crc16_ok)
  498. return SD_ERROR_DATA_CRC;
  499. break;
  500. case MMC_WRITE_BLOCK:
  501. case MMC_WRITE_MULTIPLE_BLOCK:
  502. if(!status_irq_reg->data_write_crc16_ok)
  503. return SD_ERROR_DATA_CRC;
  504. break;
  505. case SD_APP_SEND_SCR:
  506. if(!status_irq_reg->data_read_crc16_ok)
  507. return SD_ERROR_DATA_CRC;
  508. break;
  509. case SD_IO_SEND_OP_COND:
  510. break;
  511. case SD_APP_OP_COND:
  512. break;
  513. default:
  514. break;
  515. }
  516. #if 0
  517. if(mmc_resp_type(cmd) == MMC_RSP_R2){
  518. if(cmd->resp[0]&1)
  519. host->locked = 1;
  520. else
  521. host->locked = 0;
  522. dbg_print("sdcard:locked:%d\n",host->locked);
  523. }
  524. #endif
  525. if(data && data->sg && bread_command == 1){
  526. dma_sync_sg_for_cpu(host->dev,data->sg,data->sg_len,DMA_FROM_DEVICE);
  527. #if 0 //dump value
  528. {
  529. int i=0;
  530. struct scatterlist *sg;
  531. printk("[mmc cmd:%d] argument:%x\n",cmd->opcode,cmd->arg);
  532. for_each_sg(data->sg, sg, data->sg_len, i) {
  533. char* pv = sg_virt(sg);
  534. int j;
  535. printk("virt_addr:%X phys_addr:%X :%d length:%x\n",pv,sg_phys(sg),i, sg->length);
  536. for(j = 0; j < 32 && j < sg->length; j++){
  537. if((j % 16) == 0)
  538. printk("\n");
  539. printk("%.2x ",pv[j]);
  540. }
  541. printk("\n");
  542. if( i > 2)
  543. break;
  544. }//for each
  545. }
  546. #endif
  547. }//if
  548. if(MMC_SEND_STATUS == cmd->opcode)
  549. mdelay(100);
  550. // printk("[cmd : %d ] run ok!\n",cmd->opcode);
  551. return SD_NO_ERROR;
  552. }
  553. static inline bool aml_host_usedma(struct aml_sd_host *host)
  554. {
  555. // DBG_LINE_INFO();
  556. return host->dodma;
  557. }
  558. #if 0
  559. static irqreturn_t aml_sd_irq(int irq, void *dev_id)
  560. {
  561. DBG_LINE_INFO();
  562. return IRQ_HANDLED;
  563. }
  564. #endif
  565. #if 0
  566. /*
  567. * ISR for the CardDetect Pin
  568. */
  569. static irqreturn_t aml_sd_irq_cd(int irq, void *dev_id)
  570. {
  571. struct aml_sd_host *host = (struct aml_sd_host *)dev_id;
  572. DBG_LINE_INFO();
  573. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  574. return IRQ_HANDLED;
  575. }
  576. #endif
  577. #if 0
  578. /* Protect the card while the cover is open */
  579. static void aml_sd_protect_card(struct aml_sd_host *host)
  580. {
  581. DBG_LINE_INFO();
  582. }
  583. /*
  584. * ISR for handling card insertion and removal
  585. */
  586. static irqreturn_t aml_sd_cd_handler(int irq, void *dev_id)
  587. {
  588. DBG_LINE_INFO();
  589. /*
  590. struct aml_sd_host *host = (struct aml_sd_host *)dev_id;
  591. if (host->suspended)
  592. return IRQ_HANDLED;
  593. schedule_work(&host->mmc_carddetect_work);
  594. */
  595. return IRQ_HANDLED;
  596. }
  597. #endif
  598. int aml_sd_get_cd(struct mmc_host *mmc)
  599. {
  600. struct aml_sd_host *host = mmc_priv(mmc);
  601. struct aml_mmc_platform_data * platform_data = host->pdata;
  602. int ret = 1;
  603. DBG_LINE_INFO();
  604. if(platform_data->sdio_detect)
  605. ret = platform_data->sdio_detect(host);
  606. dbg_print("sdio detect: %s.\n",ret ? "no inserted":"inserted");
  607. //need return 0: inserted. 1: no inserted
  608. return (ret?0:1);
  609. }
  610. /*
  611. * Configure block length for MMC/SD cards and initiate the transfer.
  612. */
  613. static int
  614. aml_sd_setup_data(struct aml_sd_host *host, struct mmc_data *data)
  615. {
  616. unsigned int status_irq = 0;
  617. SDIO_Status_IRQ_Reg_t *status_irq_reg = (void *)&status_irq;
  618. unsigned int irq_config = 0;
  619. MSHW_IRQ_Config_Reg_t *irq_config_reg = (void *)&irq_config;
  620. DBG_LINE_INFO();
  621. //u32 dcon, imsk, stoptries = 3;
  622. /* write DCON register */
  623. if (!data) {
  624. aml_write_reg32(P_SDIO_CONFIG,0);
  625. return 0;
  626. }
  627. if ((data->blksz & 3) != 0) {
  628. /* We cannot deal with unaligned blocks with more than
  629. * one block being transfered. */
  630. if (data->blocks > 1) {
  631. pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
  632. return -EINVAL;
  633. }
  634. }
  635. status_irq = aml_read_reg32(P_SDIO_STATUS_IRQ);
  636. while(status_irq_reg->cmd_busy && status_irq_reg->cmd_int){
  637. printk(KERN_INFO "%s() transfer still in progress.\n",__func__);
  638. //reset
  639. irq_config_reg->soft_reset = 1;
  640. aml_write_reg32(P_SDIO_IRQ_CONFIG,irq_config);
  641. udelay(1000);
  642. status_irq = aml_read_reg32(P_SDIO_STATUS_IRQ);
  643. }
  644. return 0;
  645. }
  646. void aml_mmc_goto_idle(void)
  647. {
  648. unsigned cmd_send;
  649. mmc_soft_reset();
  650. aml_write_reg32(P_CMD_ARGUMENT,0x0);
  651. cmd_send = ((0 << cmd_response_bits_bit) | // NO_RESPONSE
  652. (0x40 << cmd_command_bit));
  653. aml_write_reg32(P_CMD_SEND,cmd_send);
  654. if (mmc_wait_busy(TIMEOUT_SHORT) == 0)
  655. printk(KERN_INFO "mmc goto idle ok!\n");
  656. else
  657. printk(KERN_INFO "mmc goto idle failed!\n");
  658. mdelay(10);
  659. }
  660. /*
  661. * Request function. for read/write operation
  662. */
  663. static void aml_sd_send_request(struct mmc_host *mmc)
  664. {
  665. struct aml_sd_host *host = mmc_priv(mmc);
  666. struct mmc_request *mrq = host->mrq;
  667. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  668. int res = 0;
  669. // DBG_LINE_INFO();
  670. host->ccnt++;
  671. #if 1
  672. //for SDIO detect.
  673. if(cmd->opcode == 5 || cmd->opcode == 52)
  674. {
  675. cmd->error = -1;
  676. host->complete_what = COMPLETION_FINALIZE;
  677. mmc_request_done(mmc, mrq);
  678. return;
  679. }
  680. #endif
  681. if (cmd->data) {
  682. res = aml_sd_setup_data(host, cmd->data);
  683. host->dcnt++;
  684. if (res) {
  685. cmd->error = res;
  686. cmd->data->error = res;
  687. mmc_request_done(mmc, mrq);
  688. return;
  689. }
  690. if (res) {
  691. cmd->error = res;
  692. cmd->data->error = res;
  693. mmc_request_done(mmc, mrq);
  694. return;
  695. }
  696. }
  697. /* Send command */
  698. res = aml_sd_send_command(host, cmd);
  699. cmd->error = res;
  700. if (cmd->data && cmd->error)
  701. cmd->data->error = cmd->error;
  702. if(mrq->data){
  703. if (mrq->data->error == 0)
  704. mrq->data->bytes_xfered =(mrq->data->blocks * mrq->data->blksz);
  705. else
  706. mrq->data->bytes_xfered = 0;
  707. }
  708. if(res != 0){
  709. //goto idle
  710. aml_mmc_goto_idle();
  711. }
  712. if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
  713. host->cmd_is_stop = 1;
  714. cmd = mrq->stop;
  715. if(res != 0){
  716. cmd->error = res;
  717. if (cmd->data && cmd->error)
  718. cmd->data->error = cmd->error;
  719. }
  720. else{
  721. res = aml_sd_send_command(host, cmd);
  722. cmd->error = res;
  723. if (cmd->data && cmd->error)
  724. cmd->data->error = cmd->error;
  725. if(res != 0){
  726. //goto idle
  727. aml_mmc_goto_idle();
  728. }
  729. }
  730. }
  731. host->complete_what = COMPLETION_FINALIZE;
  732. mmc_request_done(mmc, mrq);
  733. }
  734. static void aml_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  735. {
  736. struct aml_sd_host *host = mmc_priv(mmc);
  737. DBG_LINE_INFO();
  738. host->status = "mmc request";
  739. host->cmd_is_stop = 0;
  740. host->mrq = mrq;
  741. if (aml_sd_get_cd(mmc) == 0) {
  742. // dbg(host, dbg_err, "%s: no medium present\n", __func__);
  743. printk(KERN_INFO "[mmc] no medium inserted!\n");
  744. host->mrq->cmd->error = -ENOMEDIUM;
  745. mmc_request_done(mmc, mrq);
  746. }
  747. else
  748. aml_sd_send_request(mmc);
  749. }
  750. /* Routine to configure clock values. Exposed API to core */
  751. static void aml_sd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  752. {
  753. struct aml_sd_host *host = mmc_priv(mmc);
  754. struct aml_mmc_platform_data * platform_data = host->pdata;
  755. unsigned int sdio_config;
  756. unsigned bus_width= ios->bus_width==MMC_BUS_WIDTH_4?1:0;//MMC
  757. unsigned clk_div=31;
  758. DBG_LINE_INFO();
  759. switch (ios->power_mode) {
  760. case MMC_POWER_ON:
  761. if(platform_data->sdio_pwr_on)
  762. platform_data->sdio_pwr_on(host->sdio_port);
  763. mdelay(50);
  764. break;
  765. case MMC_POWER_UP:
  766. return;
  767. break;
  768. case MMC_POWER_OFF:
  769. default:
  770. if(platform_data->sdio_pwr_prepare)
  771. platform_data->sdio_pwr_prepare(host->sdio_port);
  772. if(platform_data->sdio_pwr_off)
  773. platform_data->sdio_pwr_off(host->sdio_port);
  774. return;
  775. break;
  776. }
  777. if(ios->clock == 0)
  778. ios->clock = 300000;
  779. if(ios->clock<mmc->f_min)
  780. ios->clock=mmc->f_min;
  781. if(ios->clock>mmc->f_max)
  782. ios->clock=mmc->f_max;
  783. if(ios->clock)
  784. clk_div=(host->clk_rate / (2*ios->clock));
  785. sdio_config = ((2 << sdio_write_CRC_ok_status_bit) |
  786. (2 << sdio_write_Nwr_bit) |
  787. (3 << m_endian_bit) |
  788. (bus_width<<bus_width_bit)|
  789. (39 << cmd_argument_bits_bit) |
  790. (0 << cmd_out_at_posedge_bit) |
  791. (0 << cmd_disable_CRC_bit) |
  792. (0 << response_latch_at_negedge_bit) |
  793. (clk_div << cmd_clk_divide_bit));
  794. if(aml_read_reg32(P_SDIO_CONFIG) != sdio_config){
  795. if(platform_data->sdio_init)
  796. platform_data->sdio_init(host);
  797. host->bus_width = bus_width?SD_BUS_WIDE:1;
  798. mmc_soft_reset();
  799. mdelay(10);
  800. dbg_print("sdio_config=%x",sdio_config);
  801. dbg_print("host->clk_rate:%d\n",(int)host->clk_rate);
  802. dbg_print("sdio clk_div:%d\n",clk_div);
  803. dbg_print("clock:%d\n",ios->clock);
  804. dbg_print("bus_width:%x\n",bus_width);
  805. dbg_print("sdio_port:%d\n",host->sdio_port);
  806. dbg_print("sdio addtress:%x : %x\n",P_SDIO_CONFIG,P_SDIO_MULT_CONFIG);
  807. aml_write_reg32(P_SDIO_CONFIG,sdio_config);
  808. aml_write_reg32(P_SDIO_MULT_CONFIG,(host->sdio_port & 0x3)); //Switch to SDIO_A/B/C
  809. }
  810. }
  811. static int aml_sd_get_ro(struct mmc_host *mmc)
  812. {
  813. struct aml_sd_host *host = mmc_priv(mmc);
  814. DBG_LINE_INFO();
  815. return host->locked;
  816. }
  817. static const struct mmc_host_ops aml_sd_ops = {
  818. // .enable = aml_sd_enable_fclk,
  819. // .disable = aml_sd_disable_fclk,
  820. .request = aml_sd_request,
  821. .set_ios = aml_sd_set_ios,
  822. .get_cd = aml_sd_get_cd,
  823. .get_ro = aml_sd_get_ro,
  824. /* NYET -- enable_sdio_irq */
  825. // .enable_sdio_irq = aml_sd_enable_sdio_irq,
  826. };
  827. static struct aml_mmc_platform_data aml_mmc_def_pdata = {
  828. /* This is currently here to avoid a number of if (host->pdata)
  829. * checks. Any zero fields to ensure reasonable defaults are picked. */
  830. .no_wprotect = 1,
  831. .no_detect = 1,
  832. };
  833. static int __init aml_sd_probe(struct platform_device *pdev)
  834. {
  835. struct mmc_host *mmc;
  836. struct aml_sd_host *host = NULL;
  837. struct aml_mmc_platform_data* platform_data = (struct aml_mmc_platform_data*)pdev->dev.platform_data;
  838. int ret = 0;
  839. DBG_LINE_INFO();
  840. mmc = mmc_alloc_host(sizeof(struct aml_sd_host), &pdev->dev);
  841. if (!mmc) {
  842. ret = -ENOMEM;
  843. goto probe_out;
  844. }
  845. host = mmc_priv(mmc);
  846. host->mmc = mmc;
  847. host->dev = (struct device *)pdev;
  848. host->sdio_port = platform_data->sdio_port;
  849. host->pdata = (struct aml_mmc_platform_data*)pdev->dev.platform_data;
  850. if (!host->pdata) {
  851. pdev->dev.platform_data = &aml_mmc_def_pdata;
  852. host->pdata = &aml_mmc_def_pdata;
  853. }
  854. spin_lock_init(&host->complete_lock);
  855. if(platform_data->sdio_init)
  856. platform_data->sdio_init(host);
  857. host->complete_what = COMPLETION_NONE;
  858. host->pio_active = XFER_NONE;
  859. #ifdef CONFIG_MMC_AML_PIODMA
  860. host->dodma = host->pdata->dma;
  861. #endif
  862. host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  863. if (!host->mem) {
  864. dev_err(&pdev->dev,
  865. "failed to get io memory region resouce.\n");
  866. ret = -ENOENT;
  867. goto probe_free_gpio;
  868. }
  869. host->mem = request_mem_region(host->mem->start,
  870. resource_size(host->mem), pdev->name);
  871. if (!host->mem) {
  872. dev_err(&pdev->dev, "failed to request io memory region.\n");
  873. ret = -ENOENT;
  874. goto probe_free_gpio;
  875. }
  876. host->base = ioremap(host->mem->start, resource_size(host->mem));
  877. if (!host->base) {
  878. dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
  879. ret = -EINVAL;
  880. goto probe_free_mem_region;
  881. }
  882. host->irq_cd = -1;
  883. if (host->clk_rate == 0) {
  884. dev_err(&pdev->dev, "failed to find clock source.\n");
  885. host->clk = NULL;
  886. goto probe_free_dma;
  887. }
  888. dbg_print("host clock rate: %d\n",host->clk_rate);
  889. mmc->ops = &aml_sd_ops;
  890. if(platform_data->ocr_avail)
  891. mmc->ocr_avail = platform_data->ocr_avail;
  892. else
  893. mmc->ocr_avail = MMC_VDD_33_34;
  894. if(platform_data->host_caps)
  895. mmc->caps = platform_data->host_caps;
  896. else
  897. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED | MMC_CAP_NEEDS_POLL;
  898. if(platform_data->f_min)
  899. mmc->f_min = platform_data->f_min;
  900. else
  901. mmc->f_min = 200000;
  902. if(platform_data->f_max)
  903. mmc->f_max = platform_data->f_max;
  904. else
  905. mmc->f_max = 50000000;
  906. if(platform_data->clock)
  907. mmc->ios.clock = platform_data->clock;
  908. else
  909. mmc->ios.clock = 300000;
  910. mmc->ios.bus_width = MMC_BUS_WIDTH_1;
  911. mmc->ocr = mmc->ocr_avail;
  912. host->locked = 0;
  913. if(platform_data->max_blk_count)
  914. mmc->max_blk_count = platform_data->max_blk_count;
  915. else
  916. mmc->max_blk_count = 4095;
  917. if(platform_data->max_blk_size)
  918. mmc->max_blk_size = platform_data->max_blk_size;
  919. else
  920. mmc->max_blk_size = 4095;
  921. if(platform_data->max_req_size)
  922. mmc->max_req_size = platform_data->max_req_size;
  923. else
  924. mmc->max_req_size = 4095 * 512;
  925. if(platform_data->max_req_size)
  926. mmc->max_seg_size = platform_data->max_req_size;
  927. else
  928. mmc->max_seg_size = mmc->max_req_size;
  929. ret = mmc_add_host(mmc);
  930. if (ret) {
  931. dev_err(&pdev->dev, "[mmc probe] failed to add mmc host.\n");
  932. goto free_cpufreq;
  933. }
  934. platform_set_drvdata(pdev, mmc);
  935. return 0;
  936. free_cpufreq:
  937. probe_free_dma:
  938. probe_free_gpio:
  939. //probe_free_gpio_cd:
  940. //probe_iounmap:
  941. iounmap(host->base);
  942. probe_free_mem_region:
  943. release_mem_region(host->mem->start, resource_size(host->mem));
  944. mmc_free_host(mmc);
  945. probe_out:
  946. return ret;
  947. }
  948. void aml_sd_shutdown(struct platform_device *pdev)
  949. {
  950. struct mmc_host *mmc = platform_get_drvdata(pdev);
  951. struct aml_sd_host *host = mmc_priv(mmc);
  952. DBG_LINE_INFO();
  953. if (host->irq_cd >= 0)
  954. free_irq(host->irq_cd, host);
  955. mmc_remove_host(mmc);
  956. clk_disable(host->clk);
  957. }
  958. int aml_sd_remove(struct platform_device *pdev)
  959. {
  960. struct mmc_host *mmc = platform_get_drvdata(pdev);
  961. struct aml_sd_host *host = mmc_priv(mmc);
  962. struct aml_mmc_platform_data *pd = host->pdata;
  963. DBG_LINE_INFO();
  964. aml_sd_shutdown(pdev);
  965. clk_put(host->clk);
  966. // tasklet_disable(&host->pio_tasklet);
  967. // free_irq(host->irq, host);
  968. if (!pd->no_wprotect)
  969. gpio_free(pd->gpio_wprotect);
  970. if (!pd->no_detect)
  971. gpio_free(pd->gpio_detect);
  972. iounmap(host->base);
  973. release_mem_region(host->mem->start, resource_size(host->mem));
  974. mmc_free_host(mmc);
  975. return 0;
  976. }
  977. static struct platform_driver aml_sd_driver = {
  978. .probe = aml_sd_probe,
  979. .remove = aml_sd_remove,
  980. .suspend = aml_mmc_suspend,
  981. .resume = aml_mmc_resume,
  982. .driver = {
  983. .name = DRIVER_NAME,
  984. .owner = THIS_MODULE,
  985. },
  986. };
  987. static int __init aml_mmc_init(void)
  988. {
  989. /* Register the MMC driver */
  990. return platform_driver_register(&aml_sd_driver);
  991. }
  992. static void __exit aml_mmc_cleanup(void)
  993. {
  994. /* Unregister MMC driver */
  995. platform_driver_unregister(&aml_sd_driver);
  996. }
  997. module_init(aml_mmc_init);
  998. module_exit(aml_mmc_cleanup);
  999. MODULE_DESCRIPTION("Amlogic Multimedia Card driver");
  1000. MODULE_LICENSE("GPL");
  1001. MODULE_ALIAS("platform:" DRIVER_NAME);
  1002. MODULE_AUTHOR("Amlogic Inc");