at91_mci.c 20 KB

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