at91_mci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. /*
  44. * Reset the controller and restore most of the state
  45. */
  46. static void at91_reset_host()
  47. {
  48. rt_uint32_t mr;
  49. rt_uint32_t sdcr;
  50. rt_uint32_t dtor;
  51. rt_uint32_t imr;
  52. rt_uint32_t level;
  53. level = rt_hw_interrupt_disable();
  54. imr = at91_mci_read(AT91_MCI_IMR);
  55. at91_mci_write(AT91_MCI_IDR, 0xffffffff);
  56. /* save current state */
  57. mr = at91_mci_read(AT91_MCI_MR) & 0x7fff;
  58. sdcr = at91_mci_read(AT91_MCI_SDCR);
  59. dtor = at91_mci_read(AT91_MCI_DTOR);
  60. /* reset the controller */
  61. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  62. /* restore state */
  63. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  64. at91_mci_write(AT91_MCI_MR, mr);
  65. at91_mci_write(AT91_MCI_SDCR, sdcr);
  66. at91_mci_write(AT91_MCI_DTOR, dtor);
  67. at91_mci_write(AT91_MCI_IER, imr);
  68. /* make sure sdio interrupts will fire */
  69. at91_mci_read(AT91_MCI_SR);
  70. rt_hw_interrupt_enable(level);
  71. }
  72. /*
  73. * Enable the controller
  74. */
  75. static void at91_mci_enable()
  76. {
  77. rt_uint32_t mr;
  78. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  79. at91_mci_write(AT91_MCI_IDR, 0xffffffff);
  80. at91_mci_write(AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
  81. mr = AT91_MCI_PDCMODE | 0x34a;
  82. mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
  83. at91_mci_write(AT91_MCI_MR, mr);
  84. /* use Slot A or B (only one at same time) */
  85. at91_mci_write(AT91_MCI_SDCR, 1); /* use slot b */
  86. }
  87. /*
  88. * Disable the controller
  89. */
  90. static void at91_mci_disable()
  91. {
  92. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  93. }
  94. static void at91_timeout_timer(void *data)
  95. {
  96. struct at91_mci *mci;
  97. mci = (struct at91_mci *)data;
  98. if (mci->req)
  99. {
  100. rt_kprintf("Timeout waiting end of packet\n");
  101. if (mci->current_status == REQ_ST_CMD)
  102. {
  103. if (mci->req->cmd && mci->req->data)
  104. {
  105. mci->req->data->err = -RT_ETIMEOUT;
  106. }
  107. else
  108. {
  109. if (mci->req->cmd)
  110. mci->req->cmd->err = -RT_ETIMEOUT;
  111. }
  112. }
  113. else if (mci->current_status == REQ_ST_STOP)
  114. {
  115. mci->req->stop->err = -RT_ETIMEOUT;
  116. }
  117. at91_reset_host();
  118. mmcsd_req_complete(mci->host);
  119. }
  120. }
  121. /*
  122. * Prepare a dma read
  123. */
  124. static void at91_mci_init_dma_read(struct at91_mci *mci)
  125. {
  126. rt_uint8_t i;
  127. struct rt_mmcsd_cmd *cmd;
  128. struct rt_mmcsd_data *data;
  129. rt_uint32_t length;
  130. mci_dbg("pre dma read\n");
  131. cmd = mci->cmd;
  132. if (!cmd)
  133. {
  134. mci_dbg("no command\n");
  135. return;
  136. }
  137. data = cmd->data;
  138. if (!data)
  139. {
  140. mci_dbg("no data\n");
  141. return;
  142. }
  143. for (i = 0; i < 1; i++)
  144. {
  145. /* Check to see if this needs filling */
  146. if (i == 0)
  147. {
  148. if (at91_mci_read(AT91_PDC_RCR) != 0)
  149. {
  150. mci_dbg("Transfer active in current\n");
  151. continue;
  152. }
  153. }
  154. else {
  155. if (at91_mci_read(AT91_PDC_RNCR) != 0)
  156. {
  157. mci_dbg("Transfer active in next\n");
  158. continue;
  159. }
  160. }
  161. length = data->blksize * data->blks;
  162. mci_dbg("dma address = %08X, length = %d\n", data->buf, length);
  163. if (i == 0)
  164. {
  165. at91_mci_write(AT91_PDC_RPR, (rt_uint32_t)(data->buf));
  166. at91_mci_write(AT91_PDC_RCR, (data->blksize & 0x3) ? length : length / 4);
  167. }
  168. else
  169. {
  170. at91_mci_write(AT91_PDC_RNPR, (rt_uint32_t)(data->buf));
  171. at91_mci_write(AT91_PDC_RNCR, (data->blksize & 0x3) ? length : length / 4);
  172. }
  173. }
  174. mci_dbg("pre dma read done\n");
  175. }
  176. /*
  177. * Send a command
  178. */
  179. static void at91_mci_send_command(struct at91_mci *mci, struct rt_mmcsd_cmd *cmd)
  180. {
  181. rt_uint32_t cmdr, mr;
  182. rt_uint32_t block_length;
  183. struct rt_mmcsd_data *data = cmd->data;
  184. struct rt_mmcsd_host *host = mci->host;
  185. rt_uint32_t blocks;
  186. rt_uint32_t ier = 0;
  187. rt_uint32_t length;
  188. mci->cmd = cmd;
  189. /* Needed for leaving busy state before CMD1 */
  190. if ((at91_mci_read(AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->cmd_code == 1))
  191. {
  192. mci_dbg("Clearing timeout\n");
  193. at91_mci_write(AT91_MCI_ARGR, 0);
  194. at91_mci_write(AT91_MCI_CMDR, AT91_MCI_OPDCMD);
  195. while (!(at91_mci_read(AT91_MCI_SR) & AT91_MCI_CMDRDY))
  196. {
  197. /* spin */
  198. mci_dbg("Clearing: SR = %08X\n", at91_mci_read(AT91_MCI_SR));
  199. }
  200. }
  201. cmdr = cmd->cmd_code;
  202. if (resp_type(cmd) == RESP_NONE)
  203. cmdr |= AT91_MCI_RSPTYP_NONE;
  204. else
  205. {
  206. /* if a response is expected then allow maximum response latancy */
  207. cmdr |= AT91_MCI_MAXLAT;
  208. /* set 136 bit response for R2, 48 bit response otherwise */
  209. if (resp_type(cmd) == RESP_R2)
  210. cmdr |= AT91_MCI_RSPTYP_136;
  211. else
  212. cmdr |= AT91_MCI_RSPTYP_48;
  213. }
  214. if (data)
  215. {
  216. block_length = data->blksize;
  217. blocks = data->blks;
  218. /* always set data start - also set direction flag for read */
  219. if (data->flags & DATA_DIR_READ)
  220. cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
  221. else if (data->flags & DATA_DIR_WRITE)
  222. cmdr |= AT91_MCI_TRCMD_START;
  223. if (data->flags & DATA_STREAM)
  224. cmdr |= AT91_MCI_TRTYP_STREAM;
  225. if (data->blks > 1)
  226. cmdr |= AT91_MCI_TRTYP_MULTIPLE;
  227. }
  228. else
  229. {
  230. block_length = 0;
  231. blocks = 0;
  232. }
  233. /*if (cmd->cmd_code == GO_IDLE_STATE)
  234. {
  235. cmdr |= AT91_MCI_SPCMD_INIT;
  236. }*/
  237. if (cmd->cmd_code == STOP_TRANSMISSION)
  238. cmdr |= AT91_MCI_TRCMD_STOP;
  239. if (host->io_cfg.bus_mode == MMCSD_BUSMODE_OPENDRAIN)
  240. cmdr |= AT91_MCI_OPDCMD;
  241. /*
  242. * Set the arguments and send the command
  243. */
  244. mci_dbg("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
  245. cmd->cmd_code, cmdr, cmd->arg, blocks, block_length, at91_mci_read(AT91_MCI_MR));
  246. if (!data)
  247. {
  248. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);
  249. at91_mci_write(AT91_PDC_RPR, 0);
  250. at91_mci_write(AT91_PDC_RCR, 0);
  251. at91_mci_write(AT91_PDC_RNPR, 0);
  252. at91_mci_write(AT91_PDC_RNCR, 0);
  253. at91_mci_write(AT91_PDC_TPR, 0);
  254. at91_mci_write(AT91_PDC_TCR, 0);
  255. at91_mci_write(AT91_PDC_TNPR, 0);
  256. at91_mci_write(AT91_PDC_TNCR, 0);
  257. ier = AT91_MCI_CMDRDY;
  258. }
  259. else
  260. {
  261. /* zero block length and PDC mode */
  262. mr = at91_mci_read(AT91_MCI_MR) & 0x5fff;
  263. mr |= (data->blksize & 0x3) ? AT91_MCI_PDCFBYTE : 0;
  264. mr |= (block_length << 16);
  265. mr |= AT91_MCI_PDCMODE;
  266. at91_mci_write(AT91_MCI_MR, mr);
  267. at91_mci_write(AT91_MCI_BLKR,
  268. AT91_MCI_BLKR_BCNT(blocks) |
  269. AT91_MCI_BLKR_BLKLEN(block_length));
  270. /*
  271. * Disable the PDC controller
  272. */
  273. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  274. if (cmdr & AT91_MCI_TRCMD_START)
  275. {
  276. if (cmdr & AT91_MCI_TRDIR)
  277. {
  278. /*
  279. * Handle a read
  280. */
  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, block_length * blocks);
  307. at91_mci_write(AT91_PDC_TPR, (rt_uint32_t)(mci->buf));
  308. at91_mci_write(AT91_PDC_TCR, (data->blksize & 0x3) ?
  309. length : length / 4);
  310. }
  311. else
  312. {
  313. at91_mci_write(AT91_PDC_TPR, (rt_uint32_t)(data->buf));
  314. at91_mci_write(AT91_PDC_TCR, (data->blksize & 0x3) ?
  315. length : length / 4);
  316. }
  317. mci_dbg("Transmitting %d bytes\n", length);
  318. ier = AT91_MCI_CMDRDY;
  319. }
  320. }
  321. }
  322. /*
  323. * Send the command and then enable the PDC - not the other way round as
  324. * the data sheet says
  325. */
  326. at91_mci_write(AT91_MCI_ARGR, cmd->arg);
  327. at91_mci_write(AT91_MCI_CMDR, cmdr);
  328. if (cmdr & AT91_MCI_TRCMD_START)
  329. {
  330. if (cmdr & AT91_MCI_TRDIR)
  331. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTEN);
  332. }
  333. /* Enable selected interrupts */
  334. at91_mci_write(AT91_MCI_IER, AT91_MCI_ERRORS | ier);
  335. }
  336. /*
  337. * Process the next step in the request
  338. */
  339. static void at91_mci_process_next(struct at91_mci *mci)
  340. {
  341. if (mci->current_status == REQ_ST_INIT)
  342. {
  343. mci->current_status = REQ_ST_CMD;
  344. at91_mci_send_command(mci, mci->req->cmd);
  345. }
  346. else if ((mci->current_status == REQ_ST_CMD) && mci->req->stop)
  347. {
  348. mci->current_status = REQ_ST_STOP;
  349. at91_mci_send_command(mci, mci->req->stop);
  350. }
  351. else
  352. {
  353. rt_timer_stop(&mci->timer);
  354. /* the mci controller hangs after some transfers,
  355. * and the workaround is to reset it after each transfer.
  356. */
  357. at91_reset_host();
  358. mmcsd_req_complete(mci->host);
  359. }
  360. }
  361. /*
  362. * Handle an MMC request
  363. */
  364. static void at91_mci_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  365. {
  366. rt_uint32_t timeout = RT_TICK_PER_SECOND;
  367. struct at91_mci *mci = host->private_data;
  368. mci->req = req;
  369. mci->current_status = REQ_ST_INIT;
  370. rt_timer_control(&mci->timer, RT_TIMER_CTRL_SET_TIME, (void*)&timeout);
  371. rt_timer_start(&mci->timer);
  372. at91_mci_process_next(mci);
  373. }
  374. /*
  375. * Handle transmitted data
  376. */
  377. static void at91_mci_handle_transmitted(struct at91_mci *mci)
  378. {
  379. struct rt_mmcsd_cmd *cmd;
  380. struct rt_mmcsd_data *data;
  381. mci_dbg("Handling the transmit\n");
  382. /* Disable the transfer */
  383. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  384. /* Now wait for cmd ready */
  385. at91_mci_write(AT91_MCI_IDR, AT91_MCI_TXBUFE);
  386. cmd = mci->cmd;
  387. if (!cmd) return;
  388. data = cmd->data;
  389. if (!data) return;
  390. if (data->blks > 1)
  391. {
  392. mci_dbg("multiple write : wait for BLKE...\n");
  393. at91_mci_write(AT91_MCI_IER, AT91_MCI_BLKE);
  394. } else
  395. at91_mci_write(AT91_MCI_IER, AT91_MCI_NOTBUSY);
  396. }
  397. /*
  398. * Handle after a dma read
  399. */
  400. static void at91_mci_post_dma_read(struct at91_mci *mci)
  401. {
  402. struct rt_mmcsd_cmd *cmd;
  403. struct rt_mmcsd_data *data;
  404. mci_dbg("post dma read\n");
  405. cmd = mci->cmd;
  406. if (!cmd)
  407. {
  408. mci_dbg("no command\n");
  409. return;
  410. }
  411. data = cmd->data;
  412. if (!data)
  413. {
  414. mci_dbg("no data\n");
  415. return;
  416. }
  417. at91_mci_write(AT91_MCI_IDR, AT91_MCI_ENDRX);
  418. at91_mci_write(AT91_MCI_IER, AT91_MCI_RXBUFF);
  419. mci_dbg("post dma read done\n");
  420. }
  421. /*Handle after command sent ready*/
  422. static int at91_mci_handle_cmdrdy(struct at91_mci *mci)
  423. {
  424. if (!mci->cmd)
  425. return 1;
  426. else if (!mci->cmd->data)
  427. {
  428. if (mci->current_status == REQ_ST_STOP)
  429. {
  430. /*After multi block write, we must wait for NOTBUSY*/
  431. at91_mci_write(AT91_MCI_IER, AT91_MCI_NOTBUSY);
  432. }
  433. else return 1;
  434. }
  435. else if (mci->cmd->data->flags & DATA_DIR_WRITE)
  436. {
  437. /*After sendding multi-block-write command, start DMA transfer*/
  438. at91_mci_write(AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
  439. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_TXTEN);
  440. }
  441. /* command not completed, have to wait */
  442. return 0;
  443. }
  444. /*
  445. * Handle a command that has been completed
  446. */
  447. static void at91_mci_completed_command(struct at91_mci *mci, rt_uint32_t status)
  448. {
  449. struct rt_mmcsd_cmd *cmd = mci->cmd;
  450. struct rt_mmcsd_data *data = cmd->data;
  451. at91_mci_write(AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  452. cmd->resp[0] = at91_mci_read(AT91_MCI_RSPR(0));
  453. cmd->resp[1] = at91_mci_read(AT91_MCI_RSPR(1));
  454. cmd->resp[2] = at91_mci_read(AT91_MCI_RSPR(2));
  455. cmd->resp[3] = at91_mci_read(AT91_MCI_RSPR(3));
  456. if (mci->buf)
  457. {
  458. //rt_memcpy(data->buf, mci->buf, data->blksize*data->blks);
  459. rt_free(mci->buf);
  460. mci->buf = RT_NULL;
  461. }
  462. mci_dbg("Status = %08X/%08x [%08X %08X %08X %08X]\n",
  463. status, at91_mci_read(AT91_MCI_SR),
  464. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  465. if (status & AT91_MCI_ERRORS)
  466. {
  467. if ((status & AT91_MCI_RCRCE) && (resp_type(cmd) & (RESP_R3|RESP_R4)))
  468. {
  469. cmd->err = 0;
  470. }
  471. else
  472. {
  473. if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE))
  474. {
  475. if (data)
  476. {
  477. if (status & AT91_MCI_DTOE)
  478. data->err = -RT_ETIMEOUT;
  479. else if (status & AT91_MCI_DCRCE)
  480. data->err = -RT_ERROR;
  481. }
  482. }
  483. else
  484. {
  485. if (status & AT91_MCI_RTOE)
  486. cmd->err = -RT_ETIMEOUT;
  487. else if (status & AT91_MCI_RCRCE)
  488. cmd->err = -RT_ERROR;
  489. else
  490. cmd->err = -RT_ERROR;
  491. }
  492. rt_kprintf("error detected and set to %d/%d (cmd = %d)\n",
  493. cmd->err, data ? data->err : 0,
  494. cmd->cmd_code);
  495. }
  496. }
  497. else
  498. cmd->err = 0;
  499. at91_mci_process_next(mci);
  500. }
  501. /*
  502. * Handle an interrupt
  503. */
  504. static void at91_mci_irq(int irq, void *param)
  505. {
  506. struct at91_mci *mci = (struct at91_mci *)param;
  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(mci);
  543. }
  544. if (int_status & AT91_MCI_ENDRX)
  545. {
  546. mci_dbg("ENDRX\n");
  547. at91_mci_post_dma_read(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 (mci->req->data && 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(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(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(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, void *param)
  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. mci = rt_malloc(sizeof(struct at91_mci));
  703. if (!mci)
  704. {
  705. rt_kprintf("alloc mci failed\n");
  706. goto err;
  707. }
  708. rt_memset(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. 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,
  725. (void *)mci, "MMC");
  726. rt_hw_interrupt_umask(AT91SAM9260_ID_MCI);
  727. rt_hw_interrupt_install(gpio_to_irq(AT91_PIN_PA7),
  728. at91_mci_detect, RT_NULL, "MMC_DETECT");
  729. rt_hw_interrupt_umask(gpio_to_irq(AT91_PIN_PA7));
  730. rt_timer_init(&mci->timer, "mci_timer",
  731. at91_timeout_timer,
  732. mci,
  733. RT_TICK_PER_SECOND,
  734. RT_TIMER_FLAG_PERIODIC);
  735. //rt_timer_start(&mci->timer);
  736. //rt_sem_init(&mci->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
  737. host->private_data = mci;
  738. mmcsd_change(host);
  739. return 0;
  740. err:
  741. mmcsd_free_host(host);
  742. return -RT_ENOMEM;
  743. }
  744. #include "finsh.h"
  745. FINSH_FUNCTION_EXPORT(at91_mci_init, at91sam9260 sd init);
  746. void mci_dump(void)
  747. {
  748. rt_uint32_t i;
  749. rt_kprintf("PIOA_PSR=0x%08x\n", at91_sys_read(AT91_PIOA+PIO_PSR));
  750. rt_kprintf("PIOA_ABSR=0x%08x\n", at91_sys_read(AT91_PIOA+PIO_ABSR));
  751. rt_kprintf("PIOA_PUSR=0x%08x\n", at91_sys_read(AT91_PIOA+PIO_PUSR));
  752. for (i = 0; i <= 0x4c; i += 4) {
  753. rt_kprintf("0x%08x:0x%08x\n", AT91SAM9260_BASE_MCI+i, at91_mci_read(i));
  754. }
  755. }
  756. FINSH_FUNCTION_EXPORT(mci_dump, dump register for mci);