at91_mci.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-07-25 weety first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <drivers/mmcsd_core.h>
  13. #include <at91sam926x.h>
  14. #include "at91_mci.h"
  15. #define USE_SLOT_B
  16. //#define RT_MCI_DBG
  17. #ifdef RT_MCI_DBG
  18. #define mci_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  19. #else
  20. #define mci_dbg(fmt, ...)
  21. #endif
  22. #define MMU_NOCACHE_ADDR(a) ((rt_uint32_t)a | (1UL<<31))
  23. extern void mmu_clean_dcache(rt_uint32_t buffer, rt_uint32_t size);
  24. extern void mmu_invalidate_dcache(rt_uint32_t buffer, rt_uint32_t size);
  25. #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
  26. | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
  27. | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
  28. #define at91_mci_read(reg) readl(AT91SAM9260_BASE_MCI + (reg))
  29. #define at91_mci_write(reg, val) writel((val), AT91SAM9260_BASE_MCI + (reg))
  30. #define REQ_ST_INIT (1U << 0)
  31. #define REQ_ST_CMD (1U << 1)
  32. #define REQ_ST_STOP (1U << 2)
  33. struct at91_mci {
  34. struct rt_mmcsd_host *host;
  35. struct rt_mmcsd_req *req;
  36. struct rt_mmcsd_cmd *cmd;
  37. struct rt_timer timer;
  38. //struct rt_semaphore sem_ack;
  39. rt_uint32_t *buf;
  40. rt_uint32_t current_status;
  41. };
  42. /*
  43. * Reset the controller and restore most of the state
  44. */
  45. static void at91_reset_host()
  46. {
  47. rt_uint32_t mr;
  48. rt_uint32_t sdcr;
  49. rt_uint32_t dtor;
  50. rt_uint32_t imr;
  51. rt_uint32_t level;
  52. level = rt_hw_interrupt_disable();
  53. imr = at91_mci_read(AT91_MCI_IMR);
  54. at91_mci_write(AT91_MCI_IDR, 0xffffffff);
  55. /* save current state */
  56. mr = at91_mci_read(AT91_MCI_MR) & 0x7fff;
  57. sdcr = at91_mci_read(AT91_MCI_SDCR);
  58. dtor = at91_mci_read(AT91_MCI_DTOR);
  59. /* reset the controller */
  60. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  61. /* restore state */
  62. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  63. at91_mci_write(AT91_MCI_MR, mr);
  64. at91_mci_write(AT91_MCI_SDCR, sdcr);
  65. at91_mci_write(AT91_MCI_DTOR, dtor);
  66. at91_mci_write(AT91_MCI_IER, imr);
  67. /* make sure sdio interrupts will fire */
  68. at91_mci_read(AT91_MCI_SR);
  69. rt_hw_interrupt_enable(level);
  70. }
  71. /*
  72. * Enable the controller
  73. */
  74. static void at91_mci_enable()
  75. {
  76. rt_uint32_t mr;
  77. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  78. at91_mci_write(AT91_MCI_IDR, 0xffffffff);
  79. at91_mci_write(AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
  80. mr = AT91_MCI_PDCMODE | 0x34a;
  81. mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
  82. at91_mci_write(AT91_MCI_MR, mr);
  83. /* use Slot A or B (only one at same time) */
  84. at91_mci_write(AT91_MCI_SDCR, 1); /* use slot b */
  85. }
  86. /*
  87. * Disable the controller
  88. */
  89. static void at91_mci_disable()
  90. {
  91. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  92. }
  93. static void at91_timeout_timer(void *data)
  94. {
  95. struct at91_mci *mci;
  96. mci = (struct at91_mci *)data;
  97. if (mci->req)
  98. {
  99. rt_kprintf("Timeout waiting end of packet\n");
  100. if (mci->current_status == REQ_ST_CMD)
  101. {
  102. if (mci->req->cmd && mci->req->data)
  103. {
  104. mci->req->data->err = -RT_ETIMEOUT;
  105. }
  106. else
  107. {
  108. if (mci->req->cmd)
  109. mci->req->cmd->err = -RT_ETIMEOUT;
  110. }
  111. }
  112. else if (mci->current_status == REQ_ST_STOP)
  113. {
  114. mci->req->stop->err = -RT_ETIMEOUT;
  115. }
  116. at91_reset_host();
  117. mmcsd_req_complete(mci->host);
  118. }
  119. }
  120. /*
  121. * Prepare a dma read
  122. */
  123. static void at91_mci_init_dma_read(struct at91_mci *mci)
  124. {
  125. rt_uint8_t i;
  126. struct rt_mmcsd_cmd *cmd;
  127. struct rt_mmcsd_data *data;
  128. rt_uint32_t length;
  129. mci_dbg("pre dma read\n");
  130. cmd = mci->cmd;
  131. if (!cmd)
  132. {
  133. mci_dbg("no command\n");
  134. return;
  135. }
  136. data = cmd->data;
  137. if (!data)
  138. {
  139. mci_dbg("no data\n");
  140. return;
  141. }
  142. for (i = 0; i < 1; i++)
  143. {
  144. /* Check to see if this needs filling */
  145. if (i == 0)
  146. {
  147. if (at91_mci_read(AT91_PDC_RCR) != 0)
  148. {
  149. mci_dbg("Transfer active in current\n");
  150. continue;
  151. }
  152. }
  153. else {
  154. if (at91_mci_read(AT91_PDC_RNCR) != 0)
  155. {
  156. mci_dbg("Transfer active in next\n");
  157. continue;
  158. }
  159. }
  160. length = data->blksize * data->blks;
  161. mci_dbg("dma address = %08X, length = %d\n", data->buf, length);
  162. if (i == 0)
  163. {
  164. at91_mci_write(AT91_PDC_RPR, (rt_uint32_t)(data->buf));
  165. at91_mci_write(AT91_PDC_RCR, (data->blksize & 0x3) ? length : length / 4);
  166. }
  167. else
  168. {
  169. at91_mci_write(AT91_PDC_RNPR, (rt_uint32_t)(data->buf));
  170. at91_mci_write(AT91_PDC_RNCR, (data->blksize & 0x3) ? length : length / 4);
  171. }
  172. }
  173. mci_dbg("pre dma read done\n");
  174. }
  175. /*
  176. * Send a command
  177. */
  178. static void at91_mci_send_command(struct at91_mci *mci, struct rt_mmcsd_cmd *cmd)
  179. {
  180. rt_uint32_t cmdr, mr;
  181. rt_uint32_t block_length;
  182. struct rt_mmcsd_data *data = cmd->data;
  183. struct rt_mmcsd_host *host = mci->host;
  184. rt_uint32_t blocks;
  185. rt_uint32_t ier = 0;
  186. rt_uint32_t length;
  187. mci->cmd = cmd;
  188. /* Needed for leaving busy state before CMD1 */
  189. if ((at91_mci_read(AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->cmd_code == 1))
  190. {
  191. mci_dbg("Clearing timeout\n");
  192. at91_mci_write(AT91_MCI_ARGR, 0);
  193. at91_mci_write(AT91_MCI_CMDR, AT91_MCI_OPDCMD);
  194. while (!(at91_mci_read(AT91_MCI_SR) & AT91_MCI_CMDRDY))
  195. {
  196. /* spin */
  197. mci_dbg("Clearing: SR = %08X\n", at91_mci_read(AT91_MCI_SR));
  198. }
  199. }
  200. cmdr = cmd->cmd_code;
  201. if (resp_type(cmd) == RESP_NONE)
  202. cmdr |= AT91_MCI_RSPTYP_NONE;
  203. else
  204. {
  205. /* if a response is expected then allow maximum response latancy */
  206. cmdr |= AT91_MCI_MAXLAT;
  207. /* set 136 bit response for R2, 48 bit response otherwise */
  208. if (resp_type(cmd) == RESP_R2)
  209. cmdr |= AT91_MCI_RSPTYP_136;
  210. else
  211. cmdr |= AT91_MCI_RSPTYP_48;
  212. }
  213. if (data)
  214. {
  215. block_length = data->blksize;
  216. blocks = data->blks;
  217. /* always set data start - also set direction flag for read */
  218. if (data->flags & DATA_DIR_READ)
  219. cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
  220. else if (data->flags & DATA_DIR_WRITE)
  221. cmdr |= AT91_MCI_TRCMD_START;
  222. if (data->flags & DATA_STREAM)
  223. cmdr |= AT91_MCI_TRTYP_STREAM;
  224. if (data->blks > 1)
  225. cmdr |= AT91_MCI_TRTYP_MULTIPLE;
  226. }
  227. else
  228. {
  229. block_length = 0;
  230. blocks = 0;
  231. }
  232. /*if (cmd->cmd_code == GO_IDLE_STATE)
  233. {
  234. cmdr |= AT91_MCI_SPCMD_INIT;
  235. }*/
  236. if (cmd->cmd_code == STOP_TRANSMISSION)
  237. cmdr |= AT91_MCI_TRCMD_STOP;
  238. if (host->io_cfg.bus_mode == MMCSD_BUSMODE_OPENDRAIN)
  239. cmdr |= AT91_MCI_OPDCMD;
  240. /*
  241. * Set the arguments and send the command
  242. */
  243. mci_dbg("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
  244. cmd->cmd_code, cmdr, cmd->arg, blocks, block_length, at91_mci_read(AT91_MCI_MR));
  245. if (!data)
  246. {
  247. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);
  248. at91_mci_write(AT91_PDC_RPR, 0);
  249. at91_mci_write(AT91_PDC_RCR, 0);
  250. at91_mci_write(AT91_PDC_RNPR, 0);
  251. at91_mci_write(AT91_PDC_RNCR, 0);
  252. at91_mci_write(AT91_PDC_TPR, 0);
  253. at91_mci_write(AT91_PDC_TCR, 0);
  254. at91_mci_write(AT91_PDC_TNPR, 0);
  255. at91_mci_write(AT91_PDC_TNCR, 0);
  256. ier = AT91_MCI_CMDRDY;
  257. }
  258. else
  259. {
  260. /* zero block length and PDC mode */
  261. mr = at91_mci_read(AT91_MCI_MR) & 0x5fff;
  262. mr |= (data->blksize & 0x3) ? AT91_MCI_PDCFBYTE : 0;
  263. mr |= (block_length << 16);
  264. mr |= AT91_MCI_PDCMODE;
  265. at91_mci_write(AT91_MCI_MR, mr);
  266. at91_mci_write(AT91_MCI_BLKR,
  267. AT91_MCI_BLKR_BCNT(blocks) |
  268. AT91_MCI_BLKR_BLKLEN(block_length));
  269. /*
  270. * Disable the PDC controller
  271. */
  272. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  273. if (cmdr & AT91_MCI_TRCMD_START)
  274. {
  275. if (cmdr & AT91_MCI_TRDIR)
  276. {
  277. /*
  278. * Handle a read
  279. */
  280. mmu_invalidate_dcache((rt_uint32_t)data->buf, data->blksize*data->blks);
  281. at91_mci_init_dma_read(mci);
  282. ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
  283. }
  284. else
  285. {
  286. /*
  287. * Handle a write
  288. */
  289. length = block_length * blocks;
  290. /*
  291. * at91mci MCI1 rev2xx Data Write Operation and
  292. * number of bytes erratum
  293. */
  294. if (length < 12)
  295. {
  296. length = 12;
  297. mci->buf = rt_malloc(length);
  298. if (!mci->buf)
  299. {
  300. rt_kprintf("rt alloc tx buffer failed\n");
  301. cmd->err = -RT_ENOMEM;
  302. mmcsd_req_complete(mci->host);
  303. return;
  304. }
  305. rt_memset(mci->buf, 0, 12);
  306. rt_memcpy(mci->buf, data->buf, length);
  307. mmu_clean_dcache((rt_uint32_t)mci->buf, length);
  308. at91_mci_write(AT91_PDC_TPR, (rt_uint32_t)(mci->buf));
  309. at91_mci_write(AT91_PDC_TCR, (data->blksize & 0x3) ?
  310. length : length / 4);
  311. }
  312. else
  313. {
  314. mmu_clean_dcache((rt_uint32_t)data->buf, data->blksize*data->blks);
  315. at91_mci_write(AT91_PDC_TPR, (rt_uint32_t)(data->buf));
  316. at91_mci_write(AT91_PDC_TCR, (data->blksize & 0x3) ?
  317. length : length / 4);
  318. }
  319. mci_dbg("Transmitting %d bytes\n", length);
  320. ier = AT91_MCI_CMDRDY;
  321. }
  322. }
  323. }
  324. /*
  325. * Send the command and then enable the PDC - not the other way round as
  326. * the data sheet says
  327. */
  328. at91_mci_write(AT91_MCI_ARGR, cmd->arg);
  329. at91_mci_write(AT91_MCI_CMDR, cmdr);
  330. if (cmdr & AT91_MCI_TRCMD_START)
  331. {
  332. if (cmdr & AT91_MCI_TRDIR)
  333. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTEN);
  334. }
  335. /* Enable selected interrupts */
  336. at91_mci_write(AT91_MCI_IER, AT91_MCI_ERRORS | ier);
  337. }
  338. /*
  339. * Process the next step in the request
  340. */
  341. static void at91_mci_process_next(struct at91_mci *mci)
  342. {
  343. if (mci->current_status == REQ_ST_INIT)
  344. {
  345. mci->current_status = REQ_ST_CMD;
  346. at91_mci_send_command(mci, mci->req->cmd);
  347. }
  348. else if ((mci->current_status == REQ_ST_CMD) && mci->req->stop)
  349. {
  350. mci->current_status = REQ_ST_STOP;
  351. at91_mci_send_command(mci, mci->req->stop);
  352. }
  353. else
  354. {
  355. rt_timer_stop(&mci->timer);
  356. /* the mci controller hangs after some transfers,
  357. * and the workaround is to reset it after each transfer.
  358. */
  359. at91_reset_host();
  360. mmcsd_req_complete(mci->host);
  361. }
  362. }
  363. /*
  364. * Handle an MMC request
  365. */
  366. static void at91_mci_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  367. {
  368. rt_uint32_t timeout = RT_TICK_PER_SECOND;
  369. struct at91_mci *mci = host->private_data;
  370. mci->req = req;
  371. mci->current_status = REQ_ST_INIT;
  372. rt_timer_control(&mci->timer, RT_TIMER_CTRL_SET_TIME, (void*)&timeout);
  373. rt_timer_start(&mci->timer);
  374. at91_mci_process_next(mci);
  375. }
  376. /*
  377. * Handle transmitted data
  378. */
  379. static void at91_mci_handle_transmitted(struct at91_mci *mci)
  380. {
  381. struct rt_mmcsd_cmd *cmd;
  382. struct rt_mmcsd_data *data;
  383. mci_dbg("Handling the transmit\n");
  384. /* Disable the transfer */
  385. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  386. /* Now wait for cmd ready */
  387. at91_mci_write(AT91_MCI_IDR, AT91_MCI_TXBUFE);
  388. cmd = mci->cmd;
  389. if (!cmd) return;
  390. data = cmd->data;
  391. if (!data) return;
  392. if (data->blks > 1)
  393. {
  394. mci_dbg("multiple write : wait for BLKE...\n");
  395. at91_mci_write(AT91_MCI_IER, AT91_MCI_BLKE);
  396. } else
  397. at91_mci_write(AT91_MCI_IER, AT91_MCI_NOTBUSY);
  398. }
  399. /*
  400. * Handle after a dma read
  401. */
  402. static void at91_mci_post_dma_read(struct at91_mci *mci)
  403. {
  404. struct rt_mmcsd_cmd *cmd;
  405. struct rt_mmcsd_data *data;
  406. mci_dbg("post dma read\n");
  407. cmd = mci->cmd;
  408. if (!cmd)
  409. {
  410. mci_dbg("no command\n");
  411. return;
  412. }
  413. data = cmd->data;
  414. if (!data)
  415. {
  416. mci_dbg("no data\n");
  417. return;
  418. }
  419. at91_mci_write(AT91_MCI_IDR, AT91_MCI_ENDRX);
  420. at91_mci_write(AT91_MCI_IER, AT91_MCI_RXBUFF);
  421. mci_dbg("post dma read done\n");
  422. }
  423. /*Handle after command sent ready*/
  424. static int at91_mci_handle_cmdrdy(struct at91_mci *mci)
  425. {
  426. if (!mci->cmd)
  427. return 1;
  428. else if (!mci->cmd->data)
  429. {
  430. if (mci->current_status == REQ_ST_STOP)
  431. {
  432. /*After multi block write, we must wait for NOTBUSY*/
  433. at91_mci_write(AT91_MCI_IER, AT91_MCI_NOTBUSY);
  434. }
  435. else return 1;
  436. }
  437. else if (mci->cmd->data->flags & DATA_DIR_WRITE)
  438. {
  439. /*After sendding multi-block-write command, start DMA transfer*/
  440. at91_mci_write(AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
  441. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_TXTEN);
  442. }
  443. /* command not completed, have to wait */
  444. return 0;
  445. }
  446. /*
  447. * Handle a command that has been completed
  448. */
  449. static void at91_mci_completed_command(struct at91_mci *mci, rt_uint32_t status)
  450. {
  451. struct rt_mmcsd_cmd *cmd = mci->cmd;
  452. struct rt_mmcsd_data *data = cmd->data;
  453. at91_mci_write(AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  454. cmd->resp[0] = at91_mci_read(AT91_MCI_RSPR(0));
  455. cmd->resp[1] = at91_mci_read(AT91_MCI_RSPR(1));
  456. cmd->resp[2] = at91_mci_read(AT91_MCI_RSPR(2));
  457. cmd->resp[3] = at91_mci_read(AT91_MCI_RSPR(3));
  458. if (mci->buf)
  459. {
  460. //rt_memcpy(data->buf, mci->buf, data->blksize*data->blks);
  461. rt_free(mci->buf);
  462. mci->buf = RT_NULL;
  463. }
  464. mci_dbg("Status = %08X/%08x [%08X %08X %08X %08X]\n",
  465. status, at91_mci_read(AT91_MCI_SR),
  466. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  467. if (status & AT91_MCI_ERRORS)
  468. {
  469. if ((status & AT91_MCI_RCRCE) && (resp_type(cmd) & (RESP_R3|RESP_R4)))
  470. {
  471. cmd->err = 0;
  472. }
  473. else
  474. {
  475. if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE))
  476. {
  477. if (data)
  478. {
  479. if (status & AT91_MCI_DTOE)
  480. data->err = -RT_ETIMEOUT;
  481. else if (status & AT91_MCI_DCRCE)
  482. data->err = -RT_ERROR;
  483. }
  484. }
  485. else
  486. {
  487. if (status & AT91_MCI_RTOE)
  488. cmd->err = -RT_ETIMEOUT;
  489. else if (status & AT91_MCI_RCRCE)
  490. cmd->err = -RT_ERROR;
  491. else
  492. cmd->err = -RT_ERROR;
  493. }
  494. rt_kprintf("error detected and set to %d/%d (cmd = %d)\n",
  495. cmd->err, data ? data->err : 0,
  496. cmd->cmd_code);
  497. }
  498. }
  499. else
  500. cmd->err = 0;
  501. at91_mci_process_next(mci);
  502. }
  503. /*
  504. * Handle an interrupt
  505. */
  506. static void at91_mci_irq(int irq, void *param)
  507. {
  508. struct at91_mci *mci = (struct at91_mci *)param;
  509. rt_int32_t completed = 0;
  510. rt_uint32_t int_status, int_mask;
  511. int_status = at91_mci_read(AT91_MCI_SR);
  512. int_mask = at91_mci_read(AT91_MCI_IMR);
  513. mci_dbg("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
  514. int_status & int_mask);
  515. int_status = int_status & int_mask;
  516. if (int_status & AT91_MCI_ERRORS)
  517. {
  518. completed = 1;
  519. if (int_status & AT91_MCI_UNRE)
  520. mci_dbg("MMC: Underrun error\n");
  521. if (int_status & AT91_MCI_OVRE)
  522. mci_dbg("MMC: Overrun error\n");
  523. if (int_status & AT91_MCI_DTOE)
  524. mci_dbg("MMC: Data timeout\n");
  525. if (int_status & AT91_MCI_DCRCE)
  526. mci_dbg("MMC: CRC error in data\n");
  527. if (int_status & AT91_MCI_RTOE)
  528. mci_dbg("MMC: Response timeout\n");
  529. if (int_status & AT91_MCI_RENDE)
  530. mci_dbg("MMC: Response end bit error\n");
  531. if (int_status & AT91_MCI_RCRCE)
  532. mci_dbg("MMC: Response CRC error\n");
  533. if (int_status & AT91_MCI_RDIRE)
  534. mci_dbg("MMC: Response direction error\n");
  535. if (int_status & AT91_MCI_RINDE)
  536. mci_dbg("MMC: Response index error\n");
  537. }
  538. else
  539. {
  540. /* Only continue processing if no errors */
  541. if (int_status & AT91_MCI_TXBUFE)
  542. {
  543. mci_dbg("TX buffer empty\n");
  544. at91_mci_handle_transmitted(mci);
  545. }
  546. if (int_status & AT91_MCI_ENDRX)
  547. {
  548. mci_dbg("ENDRX\n");
  549. at91_mci_post_dma_read(mci);
  550. }
  551. if (int_status & AT91_MCI_RXBUFF)
  552. {
  553. mci_dbg("RX buffer full\n");
  554. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  555. at91_mci_write(AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
  556. completed = 1;
  557. }
  558. if (int_status & AT91_MCI_ENDTX)
  559. mci_dbg("Transmit has ended\n");
  560. if (int_status & AT91_MCI_NOTBUSY)
  561. {
  562. mci_dbg("Card is ready\n");
  563. //at91_mci_update_bytes_xfered(host);
  564. completed = 1;
  565. }
  566. if (int_status & AT91_MCI_DTIP)
  567. mci_dbg("Data transfer in progress\n");
  568. if (int_status & AT91_MCI_BLKE)
  569. {
  570. mci_dbg("Block transfer has ended\n");
  571. if (mci->req->data && mci->req->data->blks > 1)
  572. {
  573. /* multi block write : complete multi write
  574. * command and send stop */
  575. completed = 1;
  576. }
  577. else
  578. {
  579. at91_mci_write(AT91_MCI_IER, AT91_MCI_NOTBUSY);
  580. }
  581. }
  582. /*if (int_status & AT91_MCI_SDIOIRQA)
  583. rt_mmcsd_signal_sdio_irq(host->mmc);*/
  584. if (int_status & AT91_MCI_SDIOIRQB)
  585. sdio_irq_wakeup(mci->host);
  586. if (int_status & AT91_MCI_TXRDY)
  587. mci_dbg("Ready to transmit\n");
  588. if (int_status & AT91_MCI_RXRDY)
  589. mci_dbg("Ready to receive\n");
  590. if (int_status & AT91_MCI_CMDRDY)
  591. {
  592. mci_dbg("Command ready\n");
  593. completed = at91_mci_handle_cmdrdy(mci);
  594. }
  595. }
  596. if (completed)
  597. {
  598. mci_dbg("Completed command\n");
  599. at91_mci_write(AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  600. at91_mci_completed_command(mci, int_status);
  601. }
  602. else
  603. at91_mci_write(AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  604. }
  605. /*
  606. * Set the IOCFG
  607. */
  608. static void at91_mci_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
  609. {
  610. rt_uint32_t clkdiv;
  611. //struct at91_mci *mci = host->private_data;
  612. rt_uint32_t at91_master_clock = clk_get_rate(clk_get("mck"));
  613. if (io_cfg->clock == 0)
  614. {
  615. /* Disable the MCI controller */
  616. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS);
  617. clkdiv = 0;
  618. }
  619. else
  620. {
  621. /* Enable the MCI controller */
  622. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  623. if ((at91_master_clock % (io_cfg->clock * 2)) == 0)
  624. clkdiv = ((at91_master_clock / io_cfg->clock) / 2) - 1;
  625. else
  626. clkdiv = (at91_master_clock / io_cfg->clock) / 2;
  627. mci_dbg("clkdiv = %d. mcck = %ld\n", clkdiv,
  628. at91_master_clock / (2 * (clkdiv + 1)));
  629. }
  630. if (io_cfg->bus_width == MMCSD_BUS_WIDTH_4)
  631. {
  632. mci_dbg("MMC: Setting controller bus width to 4\n");
  633. at91_mci_write(AT91_MCI_SDCR, at91_mci_read(AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
  634. }
  635. else
  636. {
  637. mci_dbg("MMC: Setting controller bus width to 1\n");
  638. at91_mci_write(AT91_MCI_SDCR, at91_mci_read(AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
  639. }
  640. /* Set the clock divider */
  641. at91_mci_write(AT91_MCI_MR, (at91_mci_read(AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
  642. /* maybe switch power to the card */
  643. switch (io_cfg->power_mode)
  644. {
  645. case MMCSD_POWER_OFF:
  646. break;
  647. case MMCSD_POWER_UP:
  648. break;
  649. case MMCSD_POWER_ON:
  650. /*at91_mci_write(AT91_MCI_ARGR, 0);
  651. at91_mci_write(AT91_MCI_CMDR, 0|AT91_MCI_SPCMD_INIT|AT91_MCI_OPDCMD);
  652. mci_dbg("MCI_SR=0x%08x\n", at91_mci_read(AT91_MCI_SR));
  653. while (!(at91_mci_read(AT91_MCI_SR) & AT91_MCI_CMDRDY))
  654. {
  655. }
  656. mci_dbg("at91 mci power on\n");*/
  657. break;
  658. default:
  659. rt_kprintf("unknown power_mode %d\n", io_cfg->power_mode);
  660. break;
  661. }
  662. }
  663. static void at91_mci_enable_sdio_irq(struct rt_mmcsd_host *host, rt_int32_t enable)
  664. {
  665. at91_mci_write(enable ? AT91_MCI_IER : AT91_MCI_IDR, AT91_MCI_SDIOIRQB);
  666. }
  667. static const struct rt_mmcsd_host_ops ops = {
  668. at91_mci_request,
  669. at91_mci_set_iocfg,
  670. RT_NULL,
  671. at91_mci_enable_sdio_irq,
  672. };
  673. void at91_mci_detect(int irq, void *param)
  674. {
  675. rt_kprintf("mmcsd gpio detected\n");
  676. }
  677. static void mci_gpio_init()
  678. {
  679. #ifdef USE_SLOT_B
  680. at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 0)|(1 << 1)|(1 << 3)|(1 << 4)|(1 << 5));
  681. at91_sys_write(AT91_PIOA + PIO_PUDR, (1 << 8));
  682. at91_sys_write(AT91_PIOA + PIO_BSR, (1 << 0)|(1 << 1)|(1 << 3)|(1 << 4)|(1 << 5));
  683. at91_sys_write(AT91_PIOA + PIO_ASR, (1 << 8));
  684. at91_sys_write(AT91_PIOA + PIO_PDR, (1 << 0)|(1 << 1)|(1 << 3)|(1 << 4)|(1 << 5)|(1 << 8));
  685. at91_sys_write(AT91_PIOA + PIO_IDR, (1 << 6)|(1 << 7));
  686. at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 6)|(1 << 7));
  687. at91_sys_write(AT91_PIOA + PIO_ODR, (1 << 6)|(1 << 7));
  688. at91_sys_write(AT91_PIOA + PIO_PER, (1 << 6)|(1 << 7));
  689. #else
  690. at91_sys_write(AT91_PIOA + PIO_PUER, (1 << 6)|(1 << 7)|(1 << 9)|(1 << 10)|(1 << 11));
  691. at91_sys_write(AT91_PIOA + PIO_ASR, (1 << 6)|(1 << 7)|(1 << 9)|(1 << 10)|(1 << 11)|(1 << 8));
  692. at91_sys_write(AT91_PIOA + PIO_PDR, (1 << 6)|(1 << 7)|(1 << 9)|(1 << 10)|(1 << 11)|(1 << 8));
  693. #endif
  694. }
  695. int at91_mci_init(void)
  696. {
  697. struct rt_mmcsd_host *host;
  698. struct at91_mci *mci;
  699. host = mmcsd_alloc_host();
  700. if (!host)
  701. {
  702. return -RT_ERROR;
  703. }
  704. mci = rt_malloc(sizeof(struct at91_mci));
  705. if (!mci)
  706. {
  707. rt_kprintf("alloc mci failed\n");
  708. goto err;
  709. }
  710. rt_memset(mci, 0, sizeof(struct at91_mci));
  711. host->ops = &ops;
  712. host->freq_min = 375000;
  713. host->freq_max = 25000000;
  714. host->valid_ocr = VDD_32_33 | VDD_33_34;
  715. host->flags = MMCSD_BUSWIDTH_4 | MMCSD_MUTBLKWRITE | \
  716. MMCSD_SUP_HIGHSPEED | MMCSD_SUP_SDIO_IRQ;
  717. host->max_seg_size = 65535;
  718. host->max_dma_segs = 2;
  719. host->max_blk_size = 512;
  720. host->max_blk_count = 4096;
  721. mci->host = host;
  722. mci_gpio_init();
  723. at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_MCI); //enable MCI clock
  724. at91_mci_disable();
  725. at91_mci_enable();
  726. /* instal interrupt */
  727. rt_hw_interrupt_install(AT91SAM9260_ID_MCI, at91_mci_irq,
  728. (void *)mci, "MMC");
  729. rt_hw_interrupt_umask(AT91SAM9260_ID_MCI);
  730. rt_hw_interrupt_install(gpio_to_irq(AT91_PIN_PA7),
  731. at91_mci_detect, RT_NULL, "MMC_DETECT");
  732. rt_hw_interrupt_umask(gpio_to_irq(AT91_PIN_PA7));
  733. rt_timer_init(&mci->timer, "mci_timer",
  734. at91_timeout_timer,
  735. mci,
  736. RT_TICK_PER_SECOND,
  737. RT_TIMER_FLAG_PERIODIC);
  738. //rt_timer_start(&mci->timer);
  739. //rt_sem_init(&mci->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
  740. host->private_data = mci;
  741. mmcsd_change(host);
  742. return 0;
  743. err:
  744. mmcsd_free_host(host);
  745. return -RT_ENOMEM;
  746. }
  747. INIT_DEVICE_EXPORT(at91_mci_init);
  748. #include "finsh.h"
  749. FINSH_FUNCTION_EXPORT(at91_mci_init, at91sam9260 sd init);
  750. void mci_dump(void)
  751. {
  752. rt_uint32_t i;
  753. rt_kprintf("PIOA_PSR=0x%08x\n", at91_sys_read(AT91_PIOA+PIO_PSR));
  754. rt_kprintf("PIOA_ABSR=0x%08x\n", at91_sys_read(AT91_PIOA+PIO_ABSR));
  755. rt_kprintf("PIOA_PUSR=0x%08x\n", at91_sys_read(AT91_PIOA+PIO_PUSR));
  756. for (i = 0; i <= 0x4c; i += 4) {
  757. rt_kprintf("0x%08x:0x%08x\n", AT91SAM9260_BASE_MCI+i, at91_mci_read(i));
  758. }
  759. }
  760. FINSH_FUNCTION_EXPORT(mci_dump, dump register for mci);