at91_mci.c 23 KB

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