at91_mci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * linux/drivers/mmc/at91_mci.c - ATMEL AT91RM9200 MCI Driver
  3. *
  4. * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
  5. *
  6. * Copyright (C) 2006 Malcolm Noyes
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. This is the AT91RM9200 MCI driver that has been tested with both MMC cards
  14. and SD-cards. Boards that support write protect are now supported.
  15. The CCAT91SBC001 board does not support SD cards.
  16. The three entry points are at91_mci_request, at91_mci_set_ios
  17. and at91_mci_get_ro.
  18. SET IOS
  19. This configures the device to put it into the correct mode and clock speed
  20. required.
  21. MCI REQUEST
  22. MCI request processes the commands sent in the mmc_request structure. This
  23. can consist of a processing command and a stop command in the case of
  24. multiple block transfers.
  25. There are three main types of request, commands, reads and writes.
  26. Commands are straight forward. The command is submitted to the controller and
  27. the request function returns. When the controller generates an interrupt to indicate
  28. the command is finished, the response to the command are read and the mmc_request_done
  29. function called to end the request.
  30. Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
  31. controller to manage the transfers.
  32. A read is done from the controller directly to the scatterlist passed in from the request.
  33. Due to a bug in the controller, when a read is completed, all the words are byte
  34. swapped in the scatterlist buffers.
  35. The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
  36. A write is slightly different in that the bytes to write are read from the scatterlist
  37. into a dma memory buffer (this is in case the source buffer should be read only). The
  38. entire write buffer is then done from this single dma memory buffer.
  39. The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
  40. GET RO
  41. Gets the status of the write protect pin, if available.
  42. */
  43. #include <linux/config.h>
  44. #include <linux/module.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/init.h>
  47. #include <linux/ioport.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/blkdev.h>
  51. #include <linux/delay.h>
  52. #include <linux/err.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/clk.h>
  55. #include <linux/mmc/host.h>
  56. #include <linux/mmc/protocol.h>
  57. #include <asm/io.h>
  58. #include <asm/irq.h>
  59. #include <asm/mach/mmc.h>
  60. #include <asm/arch/board.h>
  61. #include <asm/arch/gpio.h>
  62. #include <asm/arch/at91rm9200_mci.h>
  63. #include <asm/arch/at91rm9200_pdc.h>
  64. #define DRIVER_NAME "at91_mci"
  65. #undef SUPPORT_4WIRE
  66. #ifdef CONFIG_MMC_DEBUG
  67. #define DBG(fmt...) \
  68. printk(fmt)
  69. #else
  70. #define DBG(fmt...) do { } while (0)
  71. #endif
  72. static struct clk *mci_clk;
  73. #define FL_SENT_COMMAND (1 << 0)
  74. #define FL_SENT_STOP (1 << 1)
  75. /*
  76. * Read from a MCI register.
  77. */
  78. static inline unsigned long at91_mci_read(unsigned int reg)
  79. {
  80. void __iomem *mci_base = (void __iomem *)AT91_VA_BASE_MCI;
  81. return __raw_readl(mci_base + reg);
  82. }
  83. /*
  84. * Write to a MCI register.
  85. */
  86. static inline void at91_mci_write(unsigned int reg, unsigned long value)
  87. {
  88. void __iomem *mci_base = (void __iomem *)AT91_VA_BASE_MCI;
  89. __raw_writel(value, mci_base + reg);
  90. }
  91. /*
  92. * Low level type for this driver
  93. */
  94. struct at91mci_host
  95. {
  96. struct mmc_host *mmc;
  97. struct mmc_command *cmd;
  98. struct mmc_request *request;
  99. struct at91_mmc_data *board;
  100. int present;
  101. /*
  102. * Flag indicating when the command has been sent. This is used to
  103. * work out whether or not to send the stop
  104. */
  105. unsigned int flags;
  106. /* flag for current bus settings */
  107. u32 bus_mode;
  108. /* DMA buffer used for transmitting */
  109. unsigned int* buffer;
  110. dma_addr_t physical_address;
  111. unsigned int total_length;
  112. /* Latest in the scatterlist that has been enabled for transfer, but not freed */
  113. int in_use_index;
  114. /* Latest in the scatterlist that has been enabled for transfer */
  115. int transfer_index;
  116. };
  117. /*
  118. * Copy from sg to a dma block - used for transfers
  119. */
  120. static inline void at91mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
  121. {
  122. unsigned int len, i, size;
  123. unsigned *dmabuf = host->buffer;
  124. size = host->total_length;
  125. len = data->sg_len;
  126. /*
  127. * Just loop through all entries. Size might not
  128. * be the entire list though so make sure that
  129. * we do not transfer too much.
  130. */
  131. for (i = 0; i < len; i++) {
  132. struct scatterlist *sg;
  133. int amount;
  134. int index;
  135. unsigned int *sgbuffer;
  136. sg = &data->sg[i];
  137. sgbuffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
  138. amount = min(size, sg->length);
  139. size -= amount;
  140. amount /= 4;
  141. for (index = 0; index < amount; index++)
  142. *dmabuf++ = swab32(sgbuffer[index]);
  143. kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
  144. if (size == 0)
  145. break;
  146. }
  147. /*
  148. * Check that we didn't get a request to transfer
  149. * more data than can fit into the SG list.
  150. */
  151. BUG_ON(size != 0);
  152. }
  153. /*
  154. * Prepare a dma read
  155. */
  156. static void at91mci_pre_dma_read(struct at91mci_host *host)
  157. {
  158. int i;
  159. struct scatterlist *sg;
  160. struct mmc_command *cmd;
  161. struct mmc_data *data;
  162. DBG("pre dma read\n");
  163. cmd = host->cmd;
  164. if (!cmd) {
  165. DBG("no command\n");
  166. return;
  167. }
  168. data = cmd->data;
  169. if (!data) {
  170. DBG("no data\n");
  171. return;
  172. }
  173. for (i = 0; i < 2; i++) {
  174. /* nothing left to transfer */
  175. if (host->transfer_index >= data->sg_len) {
  176. DBG("Nothing left to transfer (index = %d)\n", host->transfer_index);
  177. break;
  178. }
  179. /* Check to see if this needs filling */
  180. if (i == 0) {
  181. if (at91_mci_read(AT91_PDC_RCR) != 0) {
  182. DBG("Transfer active in current\n");
  183. continue;
  184. }
  185. }
  186. else {
  187. if (at91_mci_read(AT91_PDC_RNCR) != 0) {
  188. DBG("Transfer active in next\n");
  189. continue;
  190. }
  191. }
  192. /* Setup the next transfer */
  193. DBG("Using transfer index %d\n", host->transfer_index);
  194. sg = &data->sg[host->transfer_index++];
  195. DBG("sg = %p\n", sg);
  196. sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);
  197. DBG("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
  198. if (i == 0) {
  199. at91_mci_write(AT91_PDC_RPR, sg->dma_address);
  200. at91_mci_write(AT91_PDC_RCR, sg->length / 4);
  201. }
  202. else {
  203. at91_mci_write(AT91_PDC_RNPR, sg->dma_address);
  204. at91_mci_write(AT91_PDC_RNCR, sg->length / 4);
  205. }
  206. }
  207. DBG("pre dma read done\n");
  208. }
  209. /*
  210. * Handle after a dma read
  211. */
  212. static void at91mci_post_dma_read(struct at91mci_host *host)
  213. {
  214. struct mmc_command *cmd;
  215. struct mmc_data *data;
  216. DBG("post dma read\n");
  217. cmd = host->cmd;
  218. if (!cmd) {
  219. DBG("no command\n");
  220. return;
  221. }
  222. data = cmd->data;
  223. if (!data) {
  224. DBG("no data\n");
  225. return;
  226. }
  227. while (host->in_use_index < host->transfer_index) {
  228. unsigned int *buffer;
  229. int index;
  230. int len;
  231. struct scatterlist *sg;
  232. DBG("finishing index %d\n", host->in_use_index);
  233. sg = &data->sg[host->in_use_index++];
  234. DBG("Unmapping page %08X\n", sg->dma_address);
  235. dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
  236. /* Swap the contents of the buffer */
  237. buffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
  238. DBG("buffer = %p, length = %d\n", buffer, sg->length);
  239. data->bytes_xfered += sg->length;
  240. len = sg->length / 4;
  241. for (index = 0; index < len; index++) {
  242. buffer[index] = swab32(buffer[index]);
  243. }
  244. kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
  245. flush_dcache_page(sg->page);
  246. }
  247. /* Is there another transfer to trigger? */
  248. if (host->transfer_index < data->sg_len)
  249. at91mci_pre_dma_read(host);
  250. else {
  251. at91_mci_write(AT91_MCI_IER, AT91_MCI_RXBUFF);
  252. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  253. }
  254. DBG("post dma read done\n");
  255. }
  256. /*
  257. * Handle transmitted data
  258. */
  259. static void at91_mci_handle_transmitted(struct at91mci_host *host)
  260. {
  261. struct mmc_command *cmd;
  262. struct mmc_data *data;
  263. DBG("Handling the transmit\n");
  264. /* Disable the transfer */
  265. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  266. /* Now wait for cmd ready */
  267. at91_mci_write(AT91_MCI_IDR, AT91_MCI_TXBUFE);
  268. at91_mci_write(AT91_MCI_IER, AT91_MCI_NOTBUSY);
  269. cmd = host->cmd;
  270. if (!cmd) return;
  271. data = cmd->data;
  272. if (!data) return;
  273. data->bytes_xfered = host->total_length;
  274. }
  275. /*
  276. * Enable the controller
  277. */
  278. static void at91_mci_enable(void)
  279. {
  280. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  281. at91_mci_write(AT91_MCI_IDR, 0xFFFFFFFF);
  282. at91_mci_write(AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
  283. at91_mci_write(AT91_MCI_MR, 0x834A);
  284. at91_mci_write(AT91_MCI_SDCR, 0x0);
  285. }
  286. /*
  287. * Disable the controller
  288. */
  289. static void at91_mci_disable(void)
  290. {
  291. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  292. }
  293. /*
  294. * Send a command
  295. * return the interrupts to enable
  296. */
  297. static unsigned int at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
  298. {
  299. unsigned int cmdr, mr;
  300. unsigned int block_length;
  301. struct mmc_data *data = cmd->data;
  302. unsigned int blocks;
  303. unsigned int ier = 0;
  304. host->cmd = cmd;
  305. /* Not sure if this is needed */
  306. #if 0
  307. if ((at91_mci_read(AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
  308. DBG("Clearing timeout\n");
  309. at91_mci_write(AT91_MCI_ARGR, 0);
  310. at91_mci_write(AT91_MCI_CMDR, AT91_MCI_OPDCMD);
  311. while (!(at91_mci_read(AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
  312. /* spin */
  313. DBG("Clearing: SR = %08X\n", at91_mci_read(AT91_MCI_SR));
  314. }
  315. }
  316. #endif
  317. cmdr = cmd->opcode;
  318. if (mmc_resp_type(cmd) == MMC_RSP_NONE)
  319. cmdr |= AT91_MCI_RSPTYP_NONE;
  320. else {
  321. /* if a response is expected then allow maximum response latancy */
  322. cmdr |= AT91_MCI_MAXLAT;
  323. /* set 136 bit response for R2, 48 bit response otherwise */
  324. if (mmc_resp_type(cmd) == MMC_RSP_R2)
  325. cmdr |= AT91_MCI_RSPTYP_136;
  326. else
  327. cmdr |= AT91_MCI_RSPTYP_48;
  328. }
  329. if (data) {
  330. block_length = 1 << data->blksz_bits;
  331. blocks = data->blocks;
  332. /* always set data start - also set direction flag for read */
  333. if (data->flags & MMC_DATA_READ)
  334. cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
  335. else if (data->flags & MMC_DATA_WRITE)
  336. cmdr |= AT91_MCI_TRCMD_START;
  337. if (data->flags & MMC_DATA_STREAM)
  338. cmdr |= AT91_MCI_TRTYP_STREAM;
  339. if (data->flags & MMC_DATA_MULTI)
  340. cmdr |= AT91_MCI_TRTYP_MULTIPLE;
  341. }
  342. else {
  343. block_length = 0;
  344. blocks = 0;
  345. }
  346. if (cmd->opcode == MMC_STOP_TRANSMISSION)
  347. cmdr |= AT91_MCI_TRCMD_STOP;
  348. if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
  349. cmdr |= AT91_MCI_OPDCMD;
  350. /*
  351. * Set the arguments and send the command
  352. */
  353. DBG("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08lX)\n",
  354. cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(AT91_MCI_MR));
  355. if (!data) {
  356. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);
  357. at91_mci_write(AT91_PDC_RPR, 0);
  358. at91_mci_write(AT91_PDC_RCR, 0);
  359. at91_mci_write(AT91_PDC_RNPR, 0);
  360. at91_mci_write(AT91_PDC_RNCR, 0);
  361. at91_mci_write(AT91_PDC_TPR, 0);
  362. at91_mci_write(AT91_PDC_TCR, 0);
  363. at91_mci_write(AT91_PDC_TNPR, 0);
  364. at91_mci_write(AT91_PDC_TNCR, 0);
  365. at91_mci_write(AT91_MCI_ARGR, cmd->arg);
  366. at91_mci_write(AT91_MCI_CMDR, cmdr);
  367. return AT91_MCI_CMDRDY;
  368. }
  369. mr = at91_mci_read(AT91_MCI_MR) & 0x7fff; /* zero block length and PDC mode */
  370. at91_mci_write(AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
  371. /*
  372. * Disable the PDC controller
  373. */
  374. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
  375. if (cmdr & AT91_MCI_TRCMD_START) {
  376. data->bytes_xfered = 0;
  377. host->transfer_index = 0;
  378. host->in_use_index = 0;
  379. if (cmdr & AT91_MCI_TRDIR) {
  380. /*
  381. * Handle a read
  382. */
  383. host->buffer = NULL;
  384. host->total_length = 0;
  385. at91mci_pre_dma_read(host);
  386. ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
  387. }
  388. else {
  389. /*
  390. * Handle a write
  391. */
  392. host->total_length = block_length * blocks;
  393. host->buffer = dma_alloc_coherent(NULL,
  394. host->total_length,
  395. &host->physical_address, GFP_KERNEL);
  396. at91mci_sg_to_dma(host, data);
  397. DBG("Transmitting %d bytes\n", host->total_length);
  398. at91_mci_write(AT91_PDC_TPR, host->physical_address);
  399. at91_mci_write(AT91_PDC_TCR, host->total_length / 4);
  400. ier = AT91_MCI_TXBUFE;
  401. }
  402. }
  403. /*
  404. * Send the command and then enable the PDC - not the other way round as
  405. * the data sheet says
  406. */
  407. at91_mci_write(AT91_MCI_ARGR, cmd->arg);
  408. at91_mci_write(AT91_MCI_CMDR, cmdr);
  409. if (cmdr & AT91_MCI_TRCMD_START) {
  410. if (cmdr & AT91_MCI_TRDIR)
  411. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTEN);
  412. else
  413. at91_mci_write(AT91_PDC_PTCR, AT91_PDC_TXTEN);
  414. }
  415. return ier;
  416. }
  417. /*
  418. * Wait for a command to complete
  419. */
  420. static void at91mci_process_command(struct at91mci_host *host, struct mmc_command *cmd)
  421. {
  422. unsigned int ier;
  423. ier = at91_mci_send_command(host, cmd);
  424. DBG("setting ier to %08X\n", ier);
  425. /* Stop on errors or the required value */
  426. at91_mci_write(AT91_MCI_IER, 0xffff0000 | ier);
  427. }
  428. /*
  429. * Process the next step in the request
  430. */
  431. static void at91mci_process_next(struct at91mci_host *host)
  432. {
  433. if (!(host->flags & FL_SENT_COMMAND)) {
  434. host->flags |= FL_SENT_COMMAND;
  435. at91mci_process_command(host, host->request->cmd);
  436. }
  437. else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
  438. host->flags |= FL_SENT_STOP;
  439. at91mci_process_command(host, host->request->stop);
  440. }
  441. else
  442. mmc_request_done(host->mmc, host->request);
  443. }
  444. /*
  445. * Handle a command that has been completed
  446. */
  447. static void at91mci_completed_command(struct at91mci_host *host)
  448. {
  449. struct mmc_command *cmd = host->cmd;
  450. unsigned int status;
  451. at91_mci_write(AT91_MCI_IDR, 0xffffffff);
  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 (host->buffer) {
  457. dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
  458. host->buffer = NULL;
  459. }
  460. status = at91_mci_read(AT91_MCI_SR);
  461. DBG("Status = %08X [%08X %08X %08X %08X]\n",
  462. status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  463. if (status & (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE |
  464. AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE |
  465. AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) {
  466. if ((status & AT91_MCI_RCRCE) &&
  467. ((cmd->opcode == MMC_SEND_OP_COND) || (cmd->opcode == SD_APP_OP_COND))) {
  468. cmd->error = MMC_ERR_NONE;
  469. }
  470. else {
  471. if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
  472. cmd->error = MMC_ERR_TIMEOUT;
  473. else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
  474. cmd->error = MMC_ERR_BADCRC;
  475. else if (status & (AT91_MCI_OVRE | AT91_MCI_UNRE))
  476. cmd->error = MMC_ERR_FIFO;
  477. else
  478. cmd->error = MMC_ERR_FAILED;
  479. DBG("Error detected and set to %d (cmd = %d, retries = %d)\n",
  480. cmd->error, cmd->opcode, cmd->retries);
  481. }
  482. }
  483. else
  484. cmd->error = MMC_ERR_NONE;
  485. at91mci_process_next(host);
  486. }
  487. /*
  488. * Handle an MMC request
  489. */
  490. static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  491. {
  492. struct at91mci_host *host = mmc_priv(mmc);
  493. host->request = mrq;
  494. host->flags = 0;
  495. at91mci_process_next(host);
  496. }
  497. /*
  498. * Set the IOS
  499. */
  500. static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  501. {
  502. int clkdiv;
  503. struct at91mci_host *host = mmc_priv(mmc);
  504. unsigned long at91_master_clock = clk_get_rate(mci_clk);
  505. DBG("Clock %uHz, busmode %u, powermode %u, Vdd %u\n",
  506. ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
  507. if (host)
  508. host->bus_mode = ios->bus_mode;
  509. else
  510. printk("MMC: No host for bus_mode\n");
  511. if (ios->clock == 0) {
  512. /* Disable the MCI controller */
  513. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIDIS);
  514. clkdiv = 0;
  515. }
  516. else {
  517. /* Enable the MCI controller */
  518. at91_mci_write(AT91_MCI_CR, AT91_MCI_MCIEN);
  519. if ((at91_master_clock % (ios->clock * 2)) == 0)
  520. clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
  521. else
  522. clkdiv = (at91_master_clock / ios->clock) / 2;
  523. DBG("clkdiv = %d. mcck = %ld\n", clkdiv,
  524. at91_master_clock / (2 * (clkdiv + 1)));
  525. }
  526. if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
  527. DBG("MMC: Setting controller bus width to 4\n");
  528. at91_mci_write(AT91_MCI_SDCR, at91_mci_read(AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
  529. }
  530. else {
  531. DBG("MMC: Setting controller bus width to 1\n");
  532. at91_mci_write(AT91_MCI_SDCR, at91_mci_read(AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
  533. }
  534. /* Set the clock divider */
  535. at91_mci_write(AT91_MCI_MR, (at91_mci_read(AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
  536. /* maybe switch power to the card */
  537. if (host && host->board->vcc_pin) {
  538. switch (ios->power_mode) {
  539. case MMC_POWER_OFF:
  540. at91_set_gpio_output(host->board->vcc_pin, 0);
  541. break;
  542. case MMC_POWER_UP:
  543. case MMC_POWER_ON:
  544. at91_set_gpio_output(host->board->vcc_pin, 1);
  545. break;
  546. }
  547. }
  548. }
  549. /*
  550. * Handle an interrupt
  551. */
  552. static irqreturn_t at91_mci_irq(int irq, void *devid, struct pt_regs *regs)
  553. {
  554. struct at91mci_host *host = devid;
  555. int completed = 0;
  556. unsigned int int_status;
  557. if (host == NULL)
  558. return IRQ_HANDLED;
  559. int_status = at91_mci_read(AT91_MCI_SR);
  560. DBG("MCI irq: status = %08X, %08lX, %08lX\n", int_status, at91_mci_read(AT91_MCI_IMR),
  561. int_status & at91_mci_read(AT91_MCI_IMR));
  562. if ((int_status & at91_mci_read(AT91_MCI_IMR)) & 0xffff0000)
  563. completed = 1;
  564. int_status &= at91_mci_read(AT91_MCI_IMR);
  565. if (int_status & AT91_MCI_UNRE)
  566. DBG("MMC: Underrun error\n");
  567. if (int_status & AT91_MCI_OVRE)
  568. DBG("MMC: Overrun error\n");
  569. if (int_status & AT91_MCI_DTOE)
  570. DBG("MMC: Data timeout\n");
  571. if (int_status & AT91_MCI_DCRCE)
  572. DBG("MMC: CRC error in data\n");
  573. if (int_status & AT91_MCI_RTOE)
  574. DBG("MMC: Response timeout\n");
  575. if (int_status & AT91_MCI_RENDE)
  576. DBG("MMC: Response end bit error\n");
  577. if (int_status & AT91_MCI_RCRCE)
  578. DBG("MMC: Response CRC error\n");
  579. if (int_status & AT91_MCI_RDIRE)
  580. DBG("MMC: Response direction error\n");
  581. if (int_status & AT91_MCI_RINDE)
  582. DBG("MMC: Response index error\n");
  583. /* Only continue processing if no errors */
  584. if (!completed) {
  585. if (int_status & AT91_MCI_TXBUFE) {
  586. DBG("TX buffer empty\n");
  587. at91_mci_handle_transmitted(host);
  588. }
  589. if (int_status & AT91_MCI_RXBUFF) {
  590. DBG("RX buffer full\n");
  591. at91_mci_write(AT91_MCI_IER, AT91_MCI_CMDRDY);
  592. }
  593. if (int_status & AT91_MCI_ENDTX) {
  594. DBG("Transmit has ended\n");
  595. }
  596. if (int_status & AT91_MCI_ENDRX) {
  597. DBG("Receive has ended\n");
  598. at91mci_post_dma_read(host);
  599. }
  600. if (int_status & AT91_MCI_NOTBUSY) {
  601. DBG("Card is ready\n");
  602. at91_mci_write(AT91_MCI_IER, AT91_MCI_CMDRDY);
  603. }
  604. if (int_status & AT91_MCI_DTIP) {
  605. DBG("Data transfer in progress\n");
  606. }
  607. if (int_status & AT91_MCI_BLKE) {
  608. DBG("Block transfer has ended\n");
  609. }
  610. if (int_status & AT91_MCI_TXRDY) {
  611. DBG("Ready to transmit\n");
  612. }
  613. if (int_status & AT91_MCI_RXRDY) {
  614. DBG("Ready to receive\n");
  615. }
  616. if (int_status & AT91_MCI_CMDRDY) {
  617. DBG("Command ready\n");
  618. completed = 1;
  619. }
  620. }
  621. at91_mci_write(AT91_MCI_IDR, int_status);
  622. if (completed) {
  623. DBG("Completed command\n");
  624. at91_mci_write(AT91_MCI_IDR, 0xffffffff);
  625. at91mci_completed_command(host);
  626. }
  627. return IRQ_HANDLED;
  628. }
  629. static irqreturn_t at91_mmc_det_irq(int irq, void *_host, struct pt_regs *regs)
  630. {
  631. struct at91mci_host *host = _host;
  632. int present = !at91_get_gpio_value(irq);
  633. /*
  634. * we expect this irq on both insert and remove,
  635. * and use a short delay to debounce.
  636. */
  637. if (present != host->present) {
  638. host->present = present;
  639. DBG("%s: card %s\n", mmc_hostname(host->mmc),
  640. present ? "insert" : "remove");
  641. if (!present) {
  642. DBG("****** Resetting SD-card bus width ******\n");
  643. at91_mci_write(AT91_MCI_SDCR, 0);
  644. }
  645. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  646. }
  647. return IRQ_HANDLED;
  648. }
  649. int at91_mci_get_ro(struct mmc_host *mmc)
  650. {
  651. int read_only = 0;
  652. struct at91mci_host *host = mmc_priv(mmc);
  653. if (host->board->wp_pin) {
  654. read_only = at91_get_gpio_value(host->board->wp_pin);
  655. printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
  656. (read_only ? "read-only" : "read-write") );
  657. }
  658. else {
  659. printk(KERN_WARNING "%s: host does not support reading read-only "
  660. "switch. Assuming write-enable.\n", mmc_hostname(mmc));
  661. }
  662. return read_only;
  663. }
  664. static struct mmc_host_ops at91_mci_ops = {
  665. .request = at91_mci_request,
  666. .set_ios = at91_mci_set_ios,
  667. .get_ro = at91_mci_get_ro,
  668. };
  669. /*
  670. * Probe for the device
  671. */
  672. static int at91_mci_probe(struct platform_device *pdev)
  673. {
  674. struct mmc_host *mmc;
  675. struct at91mci_host *host;
  676. int ret;
  677. DBG("Probe MCI devices\n");
  678. at91_mci_disable();
  679. at91_mci_enable();
  680. mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
  681. if (!mmc) {
  682. DBG("Failed to allocate mmc host\n");
  683. return -ENOMEM;
  684. }
  685. mmc->ops = &at91_mci_ops;
  686. mmc->f_min = 375000;
  687. mmc->f_max = 25000000;
  688. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  689. host = mmc_priv(mmc);
  690. host->mmc = mmc;
  691. host->buffer = NULL;
  692. host->bus_mode = 0;
  693. host->board = pdev->dev.platform_data;
  694. if (host->board->wire4) {
  695. #ifdef SUPPORT_4WIRE
  696. mmc->caps |= MMC_CAP_4_BIT_DATA;
  697. #else
  698. printk("MMC: 4 wire bus mode not supported by this driver - using 1 wire\n");
  699. #endif
  700. }
  701. /*
  702. * Get Clock
  703. */
  704. mci_clk = clk_get(&pdev->dev, "mci_clk");
  705. if (!mci_clk) {
  706. printk(KERN_ERR "AT91 MMC: no clock defined.\n");
  707. return -ENODEV;
  708. }
  709. clk_enable(mci_clk); /* Enable the peripheral clock */
  710. /*
  711. * Allocate the MCI interrupt
  712. */
  713. ret = request_irq(AT91_ID_MCI, at91_mci_irq, SA_SHIRQ, DRIVER_NAME, host);
  714. if (ret) {
  715. DBG("Failed to request MCI interrupt\n");
  716. return ret;
  717. }
  718. platform_set_drvdata(pdev, mmc);
  719. /*
  720. * Add host to MMC layer
  721. */
  722. if (host->board->det_pin)
  723. host->present = !at91_get_gpio_value(host->board->det_pin);
  724. else
  725. host->present = -1;
  726. mmc_add_host(mmc);
  727. /*
  728. * monitor card insertion/removal if we can
  729. */
  730. if (host->board->det_pin) {
  731. ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
  732. SA_SAMPLE_RANDOM, DRIVER_NAME, host);
  733. if (ret)
  734. DBG("couldn't allocate MMC detect irq\n");
  735. }
  736. DBG(KERN_INFO "Added MCI driver\n");
  737. return 0;
  738. }
  739. /*
  740. * Remove a device
  741. */
  742. static int at91_mci_remove(struct platform_device *pdev)
  743. {
  744. struct mmc_host *mmc = platform_get_drvdata(pdev);
  745. struct at91mci_host *host;
  746. if (!mmc)
  747. return -1;
  748. host = mmc_priv(mmc);
  749. if (host->present != -1) {
  750. free_irq(host->board->det_pin, host);
  751. cancel_delayed_work(&host->mmc->detect);
  752. }
  753. mmc_remove_host(mmc);
  754. at91_mci_disable();
  755. free_irq(AT91_ID_MCI, host);
  756. mmc_free_host(mmc);
  757. clk_disable(mci_clk); /* Disable the peripheral clock */
  758. clk_put(mci_clk);
  759. platform_set_drvdata(pdev, NULL);
  760. DBG("Removed\n");
  761. return 0;
  762. }
  763. #ifdef CONFIG_PM
  764. static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
  765. {
  766. struct mmc_host *mmc = platform_get_drvdata(pdev);
  767. int ret = 0;
  768. if (mmc)
  769. ret = mmc_suspend_host(mmc, state);
  770. return ret;
  771. }
  772. static int at91_mci_resume(struct platform_device *pdev)
  773. {
  774. struct mmc_host *mmc = platform_get_drvdata(pdev);
  775. int ret = 0;
  776. if (mmc)
  777. ret = mmc_resume_host(mmc);
  778. return ret;
  779. }
  780. #else
  781. #define at91_mci_suspend NULL
  782. #define at91_mci_resume NULL
  783. #endif
  784. static struct platform_driver at91_mci_driver = {
  785. .probe = at91_mci_probe,
  786. .remove = at91_mci_remove,
  787. .suspend = at91_mci_suspend,
  788. .resume = at91_mci_resume,
  789. .driver = {
  790. .name = DRIVER_NAME,
  791. .owner = THIS_MODULE,
  792. },
  793. };
  794. static int __init at91_mci_init(void)
  795. {
  796. return platform_driver_register(&at91_mci_driver);
  797. }
  798. static void __exit at91_mci_exit(void)
  799. {
  800. platform_driver_unregister(&at91_mci_driver);
  801. }
  802. module_init(at91_mci_init);
  803. module_exit(at91_mci_exit);
  804. MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
  805. MODULE_AUTHOR("Nick Randell");
  806. MODULE_LICENSE("GPL");