at91_mci.c 25 KB

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