at91_mci.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. * linux/drivers/mmc/host/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/atmel_pdc.h>
  55. #include <linux/gfp.h>
  56. #include <linux/highmem.h>
  57. #include <linux/mmc/host.h>
  58. #include <linux/mmc/sdio.h>
  59. #include <asm/io.h>
  60. #include <asm/irq.h>
  61. #include <asm/gpio.h>
  62. #include <mach/board.h>
  63. #include <mach/cpu.h>
  64. #include <mach/at91_mci.h>
  65. #define DRIVER_NAME "at91_mci"
  66. static inline int at91mci_is_mci1rev2xx(void)
  67. {
  68. return ( cpu_is_at91sam9260()
  69. || cpu_is_at91sam9263()
  70. || cpu_is_at91cap9()
  71. || cpu_is_at91sam9rl()
  72. || cpu_is_at91sam9g10()
  73. || cpu_is_at91sam9g20()
  74. );
  75. }
  76. #define FL_SENT_COMMAND (1 << 0)
  77. #define FL_SENT_STOP (1 << 1)
  78. #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
  79. | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
  80. | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
  81. #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
  82. #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
  83. #define MCI_BLKSIZE 512
  84. #define MCI_MAXBLKSIZE 4095
  85. #define MCI_BLKATONCE 256
  86. #define MCI_BUFSIZE (MCI_BLKSIZE * MCI_BLKATONCE)
  87. /*
  88. * Low level type for this driver
  89. */
  90. struct at91mci_host
  91. {
  92. struct mmc_host *mmc;
  93. struct mmc_command *cmd;
  94. struct mmc_request *request;
  95. void __iomem *baseaddr;
  96. int irq;
  97. struct at91_mmc_data *board;
  98. int present;
  99. struct clk *mci_clk;
  100. /*
  101. * Flag indicating when the command has been sent. This is used to
  102. * work out whether or not to send the stop
  103. */
  104. unsigned int flags;
  105. /* flag for current bus settings */
  106. u32 bus_mode;
  107. /* DMA buffer used for transmitting */
  108. unsigned int* buffer;
  109. dma_addr_t physical_address;
  110. unsigned int total_length;
  111. /* Latest in the scatterlist that has been enabled for transfer, but not freed */
  112. int in_use_index;
  113. /* Latest in the scatterlist that has been enabled for transfer */
  114. int transfer_index;
  115. /* Timer for timeouts */
  116. struct timer_list timer;
  117. };
  118. /*
  119. * Reset the controller and restore most of the state
  120. */
  121. static void at91_reset_host(struct at91mci_host *host)
  122. {
  123. unsigned long flags;
  124. u32 mr;
  125. u32 sdcr;
  126. u32 dtor;
  127. u32 imr;
  128. local_irq_save(flags);
  129. imr = at91_mci_read(host, AT91_MCI_IMR);
  130. at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
  131. /* save current state */
  132. mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
  133. sdcr = at91_mci_read(host, AT91_MCI_SDCR);
  134. dtor = at91_mci_read(host, AT91_MCI_DTOR);
  135. /* reset the controller */
  136. at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  137. /* restore state */
  138. at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
  139. at91_mci_write(host, AT91_MCI_MR, mr);
  140. at91_mci_write(host, AT91_MCI_SDCR, sdcr);
  141. at91_mci_write(host, AT91_MCI_DTOR, dtor);
  142. at91_mci_write(host, AT91_MCI_IER, imr);
  143. /* make sure sdio interrupts will fire */
  144. at91_mci_read(host, AT91_MCI_SR);
  145. local_irq_restore(flags);
  146. }
  147. static void at91_timeout_timer(unsigned long data)
  148. {
  149. struct at91mci_host *host;
  150. host = (struct at91mci_host *)data;
  151. if (host->request) {
  152. dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
  153. if (host->cmd && host->cmd->data) {
  154. host->cmd->data->error = -ETIMEDOUT;
  155. } else {
  156. if (host->cmd)
  157. host->cmd->error = -ETIMEDOUT;
  158. else
  159. host->request->cmd->error = -ETIMEDOUT;
  160. }
  161. at91_reset_host(host);
  162. mmc_request_done(host->mmc, host->request);
  163. }
  164. }
  165. /*
  166. * Copy from sg to a dma block - used for transfers
  167. */
  168. static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
  169. {
  170. unsigned int len, i, size;
  171. unsigned *dmabuf = host->buffer;
  172. size = data->blksz * data->blocks;
  173. len = data->sg_len;
  174. /* MCI1 rev2xx Data Write Operation and number of bytes erratum */
  175. if (at91mci_is_mci1rev2xx())
  176. if (host->total_length == 12)
  177. memset(dmabuf, 0, 12);
  178. /*
  179. * Just loop through all entries. Size might not
  180. * be the entire list though so make sure that
  181. * we do not transfer too much.
  182. */
  183. for (i = 0; i < len; i++) {
  184. struct scatterlist *sg;
  185. int amount;
  186. unsigned int *sgbuffer;
  187. sg = &data->sg[i];
  188. sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
  189. amount = min(size, sg->length);
  190. size -= amount;
  191. if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
  192. int index;
  193. for (index = 0; index < (amount / 4); index++)
  194. *dmabuf++ = swab32(sgbuffer[index]);
  195. } else {
  196. char *tmpv = (char *)dmabuf;
  197. memcpy(tmpv, sgbuffer, amount);
  198. tmpv += amount;
  199. dmabuf = (unsigned *)tmpv;
  200. }
  201. kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
  202. if (size == 0)
  203. break;
  204. }
  205. /*
  206. * Check that we didn't get a request to transfer
  207. * more data than can fit into the SG list.
  208. */
  209. BUG_ON(size != 0);
  210. }
  211. /*
  212. * Handle after a dma read
  213. */
  214. static void at91_mci_post_dma_read(struct at91mci_host *host)
  215. {
  216. struct mmc_command *cmd;
  217. struct mmc_data *data;
  218. unsigned int len, i, size;
  219. unsigned *dmabuf = host->buffer;
  220. pr_debug("post dma read\n");
  221. cmd = host->cmd;
  222. if (!cmd) {
  223. pr_debug("no command\n");
  224. return;
  225. }
  226. data = cmd->data;
  227. if (!data) {
  228. pr_debug("no data\n");
  229. return;
  230. }
  231. size = data->blksz * data->blocks;
  232. len = data->sg_len;
  233. at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
  234. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
  235. for (i = 0; i < len; i++) {
  236. struct scatterlist *sg;
  237. int amount;
  238. unsigned int *sgbuffer;
  239. sg = &data->sg[i];
  240. sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
  241. amount = min(size, sg->length);
  242. size -= amount;
  243. if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
  244. int index;
  245. for (index = 0; index < (amount / 4); index++)
  246. sgbuffer[index] = swab32(*dmabuf++);
  247. } else {
  248. char *tmpv = (char *)dmabuf;
  249. memcpy(sgbuffer, tmpv, amount);
  250. tmpv += amount;
  251. dmabuf = (unsigned *)tmpv;
  252. }
  253. flush_kernel_dcache_page(sg_page(sg));
  254. kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
  255. data->bytes_xfered += amount;
  256. if (size == 0)
  257. break;
  258. }
  259. pr_debug("post dma read done\n");
  260. }
  261. /*
  262. * Handle transmitted data
  263. */
  264. static void at91_mci_handle_transmitted(struct at91mci_host *host)
  265. {
  266. struct mmc_command *cmd;
  267. struct mmc_data *data;
  268. pr_debug("Handling the transmit\n");
  269. /* Disable the transfer */
  270. at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
  271. /* Now wait for cmd ready */
  272. at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
  273. cmd = host->cmd;
  274. if (!cmd) return;
  275. data = cmd->data;
  276. if (!data) return;
  277. if (cmd->data->blocks > 1) {
  278. pr_debug("multiple write : wait for BLKE...\n");
  279. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
  280. } else
  281. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
  282. }
  283. /*
  284. * Update bytes tranfered count during a write operation
  285. */
  286. static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
  287. {
  288. struct mmc_data *data;
  289. /* always deal with the effective request (and not the current cmd) */
  290. if (host->request->cmd && host->request->cmd->error != 0)
  291. return;
  292. if (host->request->data) {
  293. data = host->request->data;
  294. if (data->flags & MMC_DATA_WRITE) {
  295. /* card is in IDLE mode now */
  296. pr_debug("-> bytes_xfered %d, total_length = %d\n",
  297. data->bytes_xfered, host->total_length);
  298. data->bytes_xfered = data->blksz * data->blocks;
  299. }
  300. }
  301. }
  302. /*Handle after command sent ready*/
  303. static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
  304. {
  305. if (!host->cmd)
  306. return 1;
  307. else if (!host->cmd->data) {
  308. if (host->flags & FL_SENT_STOP) {
  309. /*After multi block write, we must wait for NOTBUSY*/
  310. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
  311. } else return 1;
  312. } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
  313. /*After sendding multi-block-write command, start DMA transfer*/
  314. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
  315. at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
  316. }
  317. /* command not completed, have to wait */
  318. return 0;
  319. }
  320. /*
  321. * Enable the controller
  322. */
  323. static void at91_mci_enable(struct at91mci_host *host)
  324. {
  325. unsigned int mr;
  326. at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
  327. at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
  328. at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
  329. mr = AT91_MCI_PDCMODE | 0x34a;
  330. if (at91mci_is_mci1rev2xx())
  331. mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
  332. at91_mci_write(host, AT91_MCI_MR, mr);
  333. /* use Slot A or B (only one at same time) */
  334. at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
  335. }
  336. /*
  337. * Disable the controller
  338. */
  339. static void at91_mci_disable(struct at91mci_host *host)
  340. {
  341. at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
  342. }
  343. /*
  344. * Send a command
  345. */
  346. static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
  347. {
  348. unsigned int cmdr, mr;
  349. unsigned int block_length;
  350. struct mmc_data *data = cmd->data;
  351. unsigned int blocks;
  352. unsigned int ier = 0;
  353. host->cmd = cmd;
  354. /* Needed for leaving busy state before CMD1 */
  355. if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
  356. pr_debug("Clearing timeout\n");
  357. at91_mci_write(host, AT91_MCI_ARGR, 0);
  358. at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
  359. while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
  360. /* spin */
  361. pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
  362. }
  363. }
  364. cmdr = cmd->opcode;
  365. if (mmc_resp_type(cmd) == MMC_RSP_NONE)
  366. cmdr |= AT91_MCI_RSPTYP_NONE;
  367. else {
  368. /* if a response is expected then allow maximum response latancy */
  369. cmdr |= AT91_MCI_MAXLAT;
  370. /* set 136 bit response for R2, 48 bit response otherwise */
  371. if (mmc_resp_type(cmd) == MMC_RSP_R2)
  372. cmdr |= AT91_MCI_RSPTYP_136;
  373. else
  374. cmdr |= AT91_MCI_RSPTYP_48;
  375. }
  376. if (data) {
  377. if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
  378. if (data->blksz & 0x3) {
  379. pr_debug("Unsupported block size\n");
  380. cmd->error = -EINVAL;
  381. mmc_request_done(host->mmc, host->request);
  382. return;
  383. }
  384. if (data->flags & MMC_DATA_STREAM) {
  385. pr_debug("Stream commands not supported\n");
  386. cmd->error = -EINVAL;
  387. mmc_request_done(host->mmc, host->request);
  388. return;
  389. }
  390. }
  391. block_length = data->blksz;
  392. blocks = data->blocks;
  393. /* always set data start - also set direction flag for read */
  394. if (data->flags & MMC_DATA_READ)
  395. cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
  396. else if (data->flags & MMC_DATA_WRITE)
  397. cmdr |= AT91_MCI_TRCMD_START;
  398. if (cmd->opcode == SD_IO_RW_EXTENDED) {
  399. cmdr |= AT91_MCI_TRTYP_SDIO_BLOCK;
  400. } else {
  401. if (data->flags & MMC_DATA_STREAM)
  402. cmdr |= AT91_MCI_TRTYP_STREAM;
  403. if (data->blocks > 1)
  404. cmdr |= AT91_MCI_TRTYP_MULTIPLE;
  405. }
  406. }
  407. else {
  408. block_length = 0;
  409. blocks = 0;
  410. }
  411. if (host->flags & FL_SENT_STOP)
  412. cmdr |= AT91_MCI_TRCMD_STOP;
  413. if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
  414. cmdr |= AT91_MCI_OPDCMD;
  415. /*
  416. * Set the arguments and send the command
  417. */
  418. pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
  419. cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
  420. if (!data) {
  421. at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
  422. at91_mci_write(host, ATMEL_PDC_RPR, 0);
  423. at91_mci_write(host, ATMEL_PDC_RCR, 0);
  424. at91_mci_write(host, ATMEL_PDC_RNPR, 0);
  425. at91_mci_write(host, ATMEL_PDC_RNCR, 0);
  426. at91_mci_write(host, ATMEL_PDC_TPR, 0);
  427. at91_mci_write(host, ATMEL_PDC_TCR, 0);
  428. at91_mci_write(host, ATMEL_PDC_TNPR, 0);
  429. at91_mci_write(host, ATMEL_PDC_TNCR, 0);
  430. ier = AT91_MCI_CMDRDY;
  431. } else {
  432. /* zero block length and PDC mode */
  433. mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
  434. mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
  435. mr |= (block_length << 16);
  436. mr |= AT91_MCI_PDCMODE;
  437. at91_mci_write(host, AT91_MCI_MR, mr);
  438. if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
  439. at91_mci_write(host, AT91_MCI_BLKR,
  440. AT91_MCI_BLKR_BCNT(blocks) |
  441. AT91_MCI_BLKR_BLKLEN(block_length));
  442. /*
  443. * Disable the PDC controller
  444. */
  445. at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
  446. if (cmdr & AT91_MCI_TRCMD_START) {
  447. data->bytes_xfered = 0;
  448. host->transfer_index = 0;
  449. host->in_use_index = 0;
  450. if (cmdr & AT91_MCI_TRDIR) {
  451. /*
  452. * Handle a read
  453. */
  454. host->total_length = 0;
  455. at91_mci_write(host, ATMEL_PDC_RPR, host->physical_address);
  456. at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ?
  457. (blocks * block_length) : (blocks * block_length) / 4);
  458. at91_mci_write(host, ATMEL_PDC_RNPR, 0);
  459. at91_mci_write(host, ATMEL_PDC_RNCR, 0);
  460. ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
  461. }
  462. else {
  463. /*
  464. * Handle a write
  465. */
  466. host->total_length = block_length * blocks;
  467. /*
  468. * MCI1 rev2xx Data Write Operation and
  469. * number of bytes erratum
  470. */
  471. if (at91mci_is_mci1rev2xx())
  472. if (host->total_length < 12)
  473. host->total_length = 12;
  474. at91_mci_sg_to_dma(host, data);
  475. pr_debug("Transmitting %d bytes\n", host->total_length);
  476. at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
  477. at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
  478. host->total_length : host->total_length / 4);
  479. ier = AT91_MCI_CMDRDY;
  480. }
  481. }
  482. }
  483. /*
  484. * Send the command and then enable the PDC - not the other way round as
  485. * the data sheet says
  486. */
  487. at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
  488. at91_mci_write(host, AT91_MCI_CMDR, cmdr);
  489. if (cmdr & AT91_MCI_TRCMD_START) {
  490. if (cmdr & AT91_MCI_TRDIR)
  491. at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
  492. }
  493. /* Enable selected interrupts */
  494. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
  495. }
  496. /*
  497. * Process the next step in the request
  498. */
  499. static void at91_mci_process_next(struct at91mci_host *host)
  500. {
  501. if (!(host->flags & FL_SENT_COMMAND)) {
  502. host->flags |= FL_SENT_COMMAND;
  503. at91_mci_send_command(host, host->request->cmd);
  504. }
  505. else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
  506. host->flags |= FL_SENT_STOP;
  507. at91_mci_send_command(host, host->request->stop);
  508. } else {
  509. del_timer(&host->timer);
  510. /* the at91rm9200 mci controller hangs after some transfers,
  511. * and the workaround is to reset it after each transfer.
  512. */
  513. if (cpu_is_at91rm9200())
  514. at91_reset_host(host);
  515. mmc_request_done(host->mmc, host->request);
  516. }
  517. }
  518. /*
  519. * Handle a command that has been completed
  520. */
  521. static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
  522. {
  523. struct mmc_command *cmd = host->cmd;
  524. struct mmc_data *data = cmd->data;
  525. at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  526. cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
  527. cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
  528. cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
  529. cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
  530. pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
  531. status, at91_mci_read(host, AT91_MCI_SR),
  532. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  533. if (status & AT91_MCI_ERRORS) {
  534. if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
  535. cmd->error = 0;
  536. }
  537. else {
  538. if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
  539. if (data) {
  540. if (status & AT91_MCI_DTOE)
  541. data->error = -ETIMEDOUT;
  542. else if (status & AT91_MCI_DCRCE)
  543. data->error = -EILSEQ;
  544. }
  545. } else {
  546. if (status & AT91_MCI_RTOE)
  547. cmd->error = -ETIMEDOUT;
  548. else if (status & AT91_MCI_RCRCE)
  549. cmd->error = -EILSEQ;
  550. else
  551. cmd->error = -EIO;
  552. }
  553. pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
  554. cmd->error, data ? data->error : 0,
  555. cmd->opcode, cmd->retries);
  556. }
  557. }
  558. else
  559. cmd->error = 0;
  560. at91_mci_process_next(host);
  561. }
  562. /*
  563. * Handle an MMC request
  564. */
  565. static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  566. {
  567. struct at91mci_host *host = mmc_priv(mmc);
  568. host->request = mrq;
  569. host->flags = 0;
  570. /* more than 1s timeout needed with slow SD cards */
  571. mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
  572. at91_mci_process_next(host);
  573. }
  574. /*
  575. * Set the IOS
  576. */
  577. static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  578. {
  579. int clkdiv;
  580. struct at91mci_host *host = mmc_priv(mmc);
  581. unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
  582. host->bus_mode = ios->bus_mode;
  583. if (ios->clock == 0) {
  584. /* Disable the MCI controller */
  585. at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
  586. clkdiv = 0;
  587. }
  588. else {
  589. /* Enable the MCI controller */
  590. at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
  591. if ((at91_master_clock % (ios->clock * 2)) == 0)
  592. clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
  593. else
  594. clkdiv = (at91_master_clock / ios->clock) / 2;
  595. pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
  596. at91_master_clock / (2 * (clkdiv + 1)));
  597. }
  598. if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
  599. pr_debug("MMC: Setting controller bus width to 4\n");
  600. at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
  601. }
  602. else {
  603. pr_debug("MMC: Setting controller bus width to 1\n");
  604. at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
  605. }
  606. /* Set the clock divider */
  607. at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
  608. /* maybe switch power to the card */
  609. if (host->board->vcc_pin) {
  610. switch (ios->power_mode) {
  611. case MMC_POWER_OFF:
  612. gpio_set_value(host->board->vcc_pin, 0);
  613. break;
  614. case MMC_POWER_UP:
  615. gpio_set_value(host->board->vcc_pin, 1);
  616. break;
  617. case MMC_POWER_ON:
  618. break;
  619. default:
  620. WARN_ON(1);
  621. }
  622. }
  623. }
  624. /*
  625. * Handle an interrupt
  626. */
  627. static irqreturn_t at91_mci_irq(int irq, void *devid)
  628. {
  629. struct at91mci_host *host = devid;
  630. int completed = 0;
  631. unsigned int int_status, int_mask;
  632. int_status = at91_mci_read(host, AT91_MCI_SR);
  633. int_mask = at91_mci_read(host, AT91_MCI_IMR);
  634. pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
  635. int_status & int_mask);
  636. int_status = int_status & int_mask;
  637. if (int_status & AT91_MCI_ERRORS) {
  638. completed = 1;
  639. if (int_status & AT91_MCI_UNRE)
  640. pr_debug("MMC: Underrun error\n");
  641. if (int_status & AT91_MCI_OVRE)
  642. pr_debug("MMC: Overrun error\n");
  643. if (int_status & AT91_MCI_DTOE)
  644. pr_debug("MMC: Data timeout\n");
  645. if (int_status & AT91_MCI_DCRCE)
  646. pr_debug("MMC: CRC error in data\n");
  647. if (int_status & AT91_MCI_RTOE)
  648. pr_debug("MMC: Response timeout\n");
  649. if (int_status & AT91_MCI_RENDE)
  650. pr_debug("MMC: Response end bit error\n");
  651. if (int_status & AT91_MCI_RCRCE)
  652. pr_debug("MMC: Response CRC error\n");
  653. if (int_status & AT91_MCI_RDIRE)
  654. pr_debug("MMC: Response direction error\n");
  655. if (int_status & AT91_MCI_RINDE)
  656. pr_debug("MMC: Response index error\n");
  657. } else {
  658. /* Only continue processing if no errors */
  659. if (int_status & AT91_MCI_TXBUFE) {
  660. pr_debug("TX buffer empty\n");
  661. at91_mci_handle_transmitted(host);
  662. }
  663. if (int_status & AT91_MCI_ENDRX) {
  664. pr_debug("ENDRX\n");
  665. at91_mci_post_dma_read(host);
  666. }
  667. if (int_status & AT91_MCI_RXBUFF) {
  668. pr_debug("RX buffer full\n");
  669. at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
  670. at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
  671. completed = 1;
  672. }
  673. if (int_status & AT91_MCI_ENDTX)
  674. pr_debug("Transmit has ended\n");
  675. if (int_status & AT91_MCI_NOTBUSY) {
  676. pr_debug("Card is ready\n");
  677. at91_mci_update_bytes_xfered(host);
  678. completed = 1;
  679. }
  680. if (int_status & AT91_MCI_DTIP)
  681. pr_debug("Data transfer in progress\n");
  682. if (int_status & AT91_MCI_BLKE) {
  683. pr_debug("Block transfer has ended\n");
  684. if (host->request->data && host->request->data->blocks > 1) {
  685. /* multi block write : complete multi write
  686. * command and send stop */
  687. completed = 1;
  688. } else {
  689. at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
  690. }
  691. }
  692. if (int_status & AT91_MCI_SDIOIRQA)
  693. mmc_signal_sdio_irq(host->mmc);
  694. if (int_status & AT91_MCI_SDIOIRQB)
  695. mmc_signal_sdio_irq(host->mmc);
  696. if (int_status & AT91_MCI_TXRDY)
  697. pr_debug("Ready to transmit\n");
  698. if (int_status & AT91_MCI_RXRDY)
  699. pr_debug("Ready to receive\n");
  700. if (int_status & AT91_MCI_CMDRDY) {
  701. pr_debug("Command ready\n");
  702. completed = at91_mci_handle_cmdrdy(host);
  703. }
  704. }
  705. if (completed) {
  706. pr_debug("Completed command\n");
  707. at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  708. at91_mci_completed_command(host, int_status);
  709. } else
  710. at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
  711. return IRQ_HANDLED;
  712. }
  713. static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
  714. {
  715. struct at91mci_host *host = _host;
  716. int present = !gpio_get_value(irq_to_gpio(irq));
  717. /*
  718. * we expect this irq on both insert and remove,
  719. * and use a short delay to debounce.
  720. */
  721. if (present != host->present) {
  722. host->present = present;
  723. pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
  724. present ? "insert" : "remove");
  725. if (!present) {
  726. pr_debug("****** Resetting SD-card bus width ******\n");
  727. at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
  728. }
  729. /* 0.5s needed because of early card detect switch firing */
  730. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  731. }
  732. return IRQ_HANDLED;
  733. }
  734. static int at91_mci_get_ro(struct mmc_host *mmc)
  735. {
  736. struct at91mci_host *host = mmc_priv(mmc);
  737. if (host->board->wp_pin)
  738. return !!gpio_get_value(host->board->wp_pin);
  739. /*
  740. * Board doesn't support read only detection; let the mmc core
  741. * decide what to do.
  742. */
  743. return -ENOSYS;
  744. }
  745. static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  746. {
  747. struct at91mci_host *host = mmc_priv(mmc);
  748. pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
  749. host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
  750. at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
  751. host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
  752. }
  753. static const struct mmc_host_ops at91_mci_ops = {
  754. .request = at91_mci_request,
  755. .set_ios = at91_mci_set_ios,
  756. .get_ro = at91_mci_get_ro,
  757. .enable_sdio_irq = at91_mci_enable_sdio_irq,
  758. };
  759. /*
  760. * Probe for the device
  761. */
  762. static int __init at91_mci_probe(struct platform_device *pdev)
  763. {
  764. struct mmc_host *mmc;
  765. struct at91mci_host *host;
  766. struct resource *res;
  767. int ret;
  768. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  769. if (!res)
  770. return -ENXIO;
  771. if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME))
  772. return -EBUSY;
  773. mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
  774. if (!mmc) {
  775. ret = -ENOMEM;
  776. dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
  777. goto fail6;
  778. }
  779. mmc->ops = &at91_mci_ops;
  780. mmc->f_min = 375000;
  781. mmc->f_max = 25000000;
  782. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  783. mmc->caps = 0;
  784. mmc->max_blk_size = MCI_MAXBLKSIZE;
  785. mmc->max_blk_count = MCI_BLKATONCE;
  786. mmc->max_req_size = MCI_BUFSIZE;
  787. mmc->max_segs = MCI_BLKATONCE;
  788. mmc->max_seg_size = MCI_BUFSIZE;
  789. host = mmc_priv(mmc);
  790. host->mmc = mmc;
  791. host->bus_mode = 0;
  792. host->board = pdev->dev.platform_data;
  793. if (host->board->wire4) {
  794. if (at91mci_is_mci1rev2xx())
  795. mmc->caps |= MMC_CAP_4_BIT_DATA;
  796. else
  797. dev_warn(&pdev->dev, "4 wire bus mode not supported"
  798. " - using 1 wire\n");
  799. }
  800. host->buffer = dma_alloc_coherent(&pdev->dev, MCI_BUFSIZE,
  801. &host->physical_address, GFP_KERNEL);
  802. if (!host->buffer) {
  803. ret = -ENOMEM;
  804. dev_err(&pdev->dev, "Can't allocate transmit buffer\n");
  805. goto fail5;
  806. }
  807. /* Add SDIO capability when available */
  808. if (at91mci_is_mci1rev2xx()) {
  809. /* at91mci MCI1 rev2xx sdio interrupt erratum */
  810. if (host->board->wire4 || !host->board->slot_b)
  811. mmc->caps |= MMC_CAP_SDIO_IRQ;
  812. }
  813. /*
  814. * Reserve GPIOs ... board init code makes sure these pins are set
  815. * up as GPIOs with the right direction (input, except for vcc)
  816. */
  817. if (host->board->det_pin) {
  818. ret = gpio_request(host->board->det_pin, "mmc_detect");
  819. if (ret < 0) {
  820. dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
  821. goto fail4b;
  822. }
  823. }
  824. if (host->board->wp_pin) {
  825. ret = gpio_request(host->board->wp_pin, "mmc_wp");
  826. if (ret < 0) {
  827. dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
  828. goto fail4;
  829. }
  830. }
  831. if (host->board->vcc_pin) {
  832. ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
  833. if (ret < 0) {
  834. dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
  835. goto fail3;
  836. }
  837. }
  838. /*
  839. * Get Clock
  840. */
  841. host->mci_clk = clk_get(&pdev->dev, "mci_clk");
  842. if (IS_ERR(host->mci_clk)) {
  843. ret = -ENODEV;
  844. dev_dbg(&pdev->dev, "no mci_clk?\n");
  845. goto fail2;
  846. }
  847. /*
  848. * Map I/O region
  849. */
  850. host->baseaddr = ioremap(res->start, resource_size(res));
  851. if (!host->baseaddr) {
  852. ret = -ENOMEM;
  853. goto fail1;
  854. }
  855. /*
  856. * Reset hardware
  857. */
  858. clk_enable(host->mci_clk); /* Enable the peripheral clock */
  859. at91_mci_disable(host);
  860. at91_mci_enable(host);
  861. /*
  862. * Allocate the MCI interrupt
  863. */
  864. host->irq = platform_get_irq(pdev, 0);
  865. ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
  866. mmc_hostname(mmc), host);
  867. if (ret) {
  868. dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
  869. goto fail0;
  870. }
  871. setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
  872. platform_set_drvdata(pdev, mmc);
  873. /*
  874. * Add host to MMC layer
  875. */
  876. if (host->board->det_pin) {
  877. host->present = !gpio_get_value(host->board->det_pin);
  878. }
  879. else
  880. host->present = -1;
  881. mmc_add_host(mmc);
  882. /*
  883. * monitor card insertion/removal if we can
  884. */
  885. if (host->board->det_pin) {
  886. ret = request_irq(gpio_to_irq(host->board->det_pin),
  887. at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
  888. if (ret)
  889. dev_warn(&pdev->dev, "request MMC detect irq failed\n");
  890. else
  891. device_init_wakeup(&pdev->dev, 1);
  892. }
  893. pr_debug("Added MCI driver\n");
  894. return 0;
  895. fail0:
  896. clk_disable(host->mci_clk);
  897. iounmap(host->baseaddr);
  898. fail1:
  899. clk_put(host->mci_clk);
  900. fail2:
  901. if (host->board->vcc_pin)
  902. gpio_free(host->board->vcc_pin);
  903. fail3:
  904. if (host->board->wp_pin)
  905. gpio_free(host->board->wp_pin);
  906. fail4:
  907. if (host->board->det_pin)
  908. gpio_free(host->board->det_pin);
  909. fail4b:
  910. if (host->buffer)
  911. dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
  912. host->buffer, host->physical_address);
  913. fail5:
  914. mmc_free_host(mmc);
  915. fail6:
  916. release_mem_region(res->start, resource_size(res));
  917. dev_err(&pdev->dev, "probe failed, err %d\n", ret);
  918. return ret;
  919. }
  920. /*
  921. * Remove a device
  922. */
  923. static int __exit at91_mci_remove(struct platform_device *pdev)
  924. {
  925. struct mmc_host *mmc = platform_get_drvdata(pdev);
  926. struct at91mci_host *host;
  927. struct resource *res;
  928. if (!mmc)
  929. return -1;
  930. host = mmc_priv(mmc);
  931. if (host->buffer)
  932. dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
  933. host->buffer, host->physical_address);
  934. if (host->board->det_pin) {
  935. if (device_can_wakeup(&pdev->dev))
  936. free_irq(gpio_to_irq(host->board->det_pin), host);
  937. device_init_wakeup(&pdev->dev, 0);
  938. gpio_free(host->board->det_pin);
  939. }
  940. at91_mci_disable(host);
  941. del_timer_sync(&host->timer);
  942. mmc_remove_host(mmc);
  943. free_irq(host->irq, host);
  944. clk_disable(host->mci_clk); /* Disable the peripheral clock */
  945. clk_put(host->mci_clk);
  946. if (host->board->vcc_pin)
  947. gpio_free(host->board->vcc_pin);
  948. if (host->board->wp_pin)
  949. gpio_free(host->board->wp_pin);
  950. iounmap(host->baseaddr);
  951. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  952. release_mem_region(res->start, resource_size(res));
  953. mmc_free_host(mmc);
  954. platform_set_drvdata(pdev, NULL);
  955. pr_debug("MCI Removed\n");
  956. return 0;
  957. }
  958. #ifdef CONFIG_PM
  959. static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
  960. {
  961. struct mmc_host *mmc = platform_get_drvdata(pdev);
  962. struct at91mci_host *host = mmc_priv(mmc);
  963. int ret = 0;
  964. if (host->board->det_pin && device_may_wakeup(&pdev->dev))
  965. enable_irq_wake(host->board->det_pin);
  966. if (mmc)
  967. ret = mmc_suspend_host(mmc);
  968. return ret;
  969. }
  970. static int at91_mci_resume(struct platform_device *pdev)
  971. {
  972. struct mmc_host *mmc = platform_get_drvdata(pdev);
  973. struct at91mci_host *host = mmc_priv(mmc);
  974. int ret = 0;
  975. if (host->board->det_pin && device_may_wakeup(&pdev->dev))
  976. disable_irq_wake(host->board->det_pin);
  977. if (mmc)
  978. ret = mmc_resume_host(mmc);
  979. return ret;
  980. }
  981. #else
  982. #define at91_mci_suspend NULL
  983. #define at91_mci_resume NULL
  984. #endif
  985. static struct platform_driver at91_mci_driver = {
  986. .remove = __exit_p(at91_mci_remove),
  987. .suspend = at91_mci_suspend,
  988. .resume = at91_mci_resume,
  989. .driver = {
  990. .name = DRIVER_NAME,
  991. .owner = THIS_MODULE,
  992. },
  993. };
  994. static int __init at91_mci_init(void)
  995. {
  996. return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
  997. }
  998. static void __exit at91_mci_exit(void)
  999. {
  1000. platform_driver_unregister(&at91_mci_driver);
  1001. }
  1002. module_init(at91_mci_init);
  1003. module_exit(at91_mci_exit);
  1004. MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
  1005. MODULE_AUTHOR("Nick Randell");
  1006. MODULE_LICENSE("GPL");
  1007. MODULE_ALIAS("platform:at91_mci");