at91_mci.c 22 KB

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