au1xmmc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
  3. *
  4. * Copyright (c) 2005, Advanced Micro Devices, Inc.
  5. *
  6. * Developed with help from the 2.4.30 MMC AU1XXX controller including
  7. * the following copyright notices:
  8. * Copyright (c) 2003-2004 Embedded Edge, LLC.
  9. * Portions Copyright (C) 2002 Embedix, Inc
  10. * Copyright 2002 Hewlett-Packard Company
  11. * 2.6 version of this driver inspired by:
  12. * (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
  13. * All Rights Reserved.
  14. * (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
  15. * All Rights Reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. /* Why don't we use the SD controllers' carddetect feature?
  22. *
  23. * From the AU1100 MMC application guide:
  24. * If the Au1100-based design is intended to support both MultiMediaCards
  25. * and 1- or 4-data bit SecureDigital cards, then the solution is to
  26. * connect a weak (560KOhm) pull-up resistor to connector pin 1.
  27. * In doing so, a MMC card never enters SPI-mode communications,
  28. * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
  29. * (the low to high transition will not occur).
  30. */
  31. #include <linux/clk.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/mm.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/scatterlist.h>
  39. #include <linux/leds.h>
  40. #include <linux/mmc/host.h>
  41. #include <linux/slab.h>
  42. #include <asm/io.h>
  43. #include <asm/mach-au1x00/au1000.h>
  44. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  45. #include <asm/mach-au1x00/au1100_mmc.h>
  46. #define DRIVER_NAME "au1xxx-mmc"
  47. /* Set this to enable special debugging macros */
  48. /* #define DEBUG */
  49. #ifdef DEBUG
  50. #define DBG(fmt, idx, args...) \
  51. pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
  52. #else
  53. #define DBG(fmt, idx, args...) do {} while (0)
  54. #endif
  55. /* Hardware definitions */
  56. #define AU1XMMC_DESCRIPTOR_COUNT 1
  57. /* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
  58. #define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
  59. #define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
  60. #define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
  61. MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
  62. MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
  63. /* This gives us a hard value for the stop command that we can write directly
  64. * to the command register.
  65. */
  66. #define STOP_CMD \
  67. (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
  68. /* This is the set of interrupts that we configure by default. */
  69. #define AU1XMMC_INTERRUPTS \
  70. (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
  71. SD_CONFIG_CR | SD_CONFIG_I)
  72. /* The poll event (looking for insert/remove events runs twice a second. */
  73. #define AU1XMMC_DETECT_TIMEOUT (HZ/2)
  74. struct au1xmmc_host {
  75. struct mmc_host *mmc;
  76. struct mmc_request *mrq;
  77. u32 flags;
  78. void __iomem *iobase;
  79. u32 clock;
  80. u32 bus_width;
  81. u32 power_mode;
  82. int status;
  83. struct {
  84. int len;
  85. int dir;
  86. } dma;
  87. struct {
  88. int index;
  89. int offset;
  90. int len;
  91. } pio;
  92. u32 tx_chan;
  93. u32 rx_chan;
  94. int irq;
  95. struct tasklet_struct finish_task;
  96. struct tasklet_struct data_task;
  97. struct au1xmmc_platform_data *platdata;
  98. struct platform_device *pdev;
  99. struct resource *ioarea;
  100. struct clk *clk;
  101. };
  102. /* Status flags used by the host structure */
  103. #define HOST_F_XMIT 0x0001
  104. #define HOST_F_RECV 0x0002
  105. #define HOST_F_DMA 0x0010
  106. #define HOST_F_DBDMA 0x0020
  107. #define HOST_F_ACTIVE 0x0100
  108. #define HOST_F_STOP 0x1000
  109. #define HOST_S_IDLE 0x0001
  110. #define HOST_S_CMD 0x0002
  111. #define HOST_S_DATA 0x0003
  112. #define HOST_S_STOP 0x0004
  113. /* Easy access macros */
  114. #define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
  115. #define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
  116. #define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
  117. #define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
  118. #define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
  119. #define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
  120. #define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
  121. #define HOST_CMD(h) ((h)->iobase + SD_CMD)
  122. #define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
  123. #define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
  124. #define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
  125. #define DMA_CHANNEL(h) \
  126. (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
  127. static inline int has_dbdma(void)
  128. {
  129. switch (alchemy_get_cputype()) {
  130. case ALCHEMY_CPU_AU1200:
  131. case ALCHEMY_CPU_AU1300:
  132. return 1;
  133. default:
  134. return 0;
  135. }
  136. }
  137. static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
  138. {
  139. u32 val = __raw_readl(HOST_CONFIG(host));
  140. val |= mask;
  141. __raw_writel(val, HOST_CONFIG(host));
  142. wmb(); /* drain writebuffer */
  143. }
  144. static inline void FLUSH_FIFO(struct au1xmmc_host *host)
  145. {
  146. u32 val = __raw_readl(HOST_CONFIG2(host));
  147. __raw_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
  148. wmb(); /* drain writebuffer */
  149. mdelay(1);
  150. /* SEND_STOP will turn off clock control - this re-enables it */
  151. val &= ~SD_CONFIG2_DF;
  152. __raw_writel(val, HOST_CONFIG2(host));
  153. wmb(); /* drain writebuffer */
  154. }
  155. static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
  156. {
  157. u32 val = __raw_readl(HOST_CONFIG(host));
  158. val &= ~mask;
  159. __raw_writel(val, HOST_CONFIG(host));
  160. wmb(); /* drain writebuffer */
  161. }
  162. static inline void SEND_STOP(struct au1xmmc_host *host)
  163. {
  164. u32 config2;
  165. WARN_ON(host->status != HOST_S_DATA);
  166. host->status = HOST_S_STOP;
  167. config2 = __raw_readl(HOST_CONFIG2(host));
  168. __raw_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
  169. wmb(); /* drain writebuffer */
  170. /* Send the stop command */
  171. __raw_writel(STOP_CMD, HOST_CMD(host));
  172. wmb(); /* drain writebuffer */
  173. }
  174. static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
  175. {
  176. if (host->platdata && host->platdata->set_power)
  177. host->platdata->set_power(host->mmc, state);
  178. }
  179. static int au1xmmc_card_inserted(struct mmc_host *mmc)
  180. {
  181. struct au1xmmc_host *host = mmc_priv(mmc);
  182. if (host->platdata && host->platdata->card_inserted)
  183. return !!host->platdata->card_inserted(host->mmc);
  184. return -ENOSYS;
  185. }
  186. static int au1xmmc_card_readonly(struct mmc_host *mmc)
  187. {
  188. struct au1xmmc_host *host = mmc_priv(mmc);
  189. if (host->platdata && host->platdata->card_readonly)
  190. return !!host->platdata->card_readonly(mmc);
  191. return -ENOSYS;
  192. }
  193. static void au1xmmc_finish_request(struct au1xmmc_host *host)
  194. {
  195. struct mmc_request *mrq = host->mrq;
  196. host->mrq = NULL;
  197. host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
  198. host->dma.len = 0;
  199. host->dma.dir = 0;
  200. host->pio.index = 0;
  201. host->pio.offset = 0;
  202. host->pio.len = 0;
  203. host->status = HOST_S_IDLE;
  204. mmc_request_done(host->mmc, mrq);
  205. }
  206. static void au1xmmc_tasklet_finish(unsigned long param)
  207. {
  208. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  209. au1xmmc_finish_request(host);
  210. }
  211. static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
  212. struct mmc_command *cmd, struct mmc_data *data)
  213. {
  214. u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
  215. switch (mmc_resp_type(cmd)) {
  216. case MMC_RSP_NONE:
  217. break;
  218. case MMC_RSP_R1:
  219. mmccmd |= SD_CMD_RT_1;
  220. break;
  221. case MMC_RSP_R1B:
  222. mmccmd |= SD_CMD_RT_1B;
  223. break;
  224. case MMC_RSP_R2:
  225. mmccmd |= SD_CMD_RT_2;
  226. break;
  227. case MMC_RSP_R3:
  228. mmccmd |= SD_CMD_RT_3;
  229. break;
  230. default:
  231. pr_info("au1xmmc: unhandled response type %02x\n",
  232. mmc_resp_type(cmd));
  233. return -EINVAL;
  234. }
  235. if (data) {
  236. if (data->flags & MMC_DATA_READ) {
  237. if (data->blocks > 1)
  238. mmccmd |= SD_CMD_CT_4;
  239. else
  240. mmccmd |= SD_CMD_CT_2;
  241. } else if (data->flags & MMC_DATA_WRITE) {
  242. if (data->blocks > 1)
  243. mmccmd |= SD_CMD_CT_3;
  244. else
  245. mmccmd |= SD_CMD_CT_1;
  246. }
  247. }
  248. __raw_writel(cmd->arg, HOST_CMDARG(host));
  249. wmb(); /* drain writebuffer */
  250. if (wait)
  251. IRQ_OFF(host, SD_CONFIG_CR);
  252. __raw_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
  253. wmb(); /* drain writebuffer */
  254. /* Wait for the command to go on the line */
  255. while (__raw_readl(HOST_CMD(host)) & SD_CMD_GO)
  256. /* nop */;
  257. /* Wait for the command to come back */
  258. if (wait) {
  259. u32 status = __raw_readl(HOST_STATUS(host));
  260. while (!(status & SD_STATUS_CR))
  261. status = __raw_readl(HOST_STATUS(host));
  262. /* Clear the CR status */
  263. __raw_writel(SD_STATUS_CR, HOST_STATUS(host));
  264. IRQ_ON(host, SD_CONFIG_CR);
  265. }
  266. return 0;
  267. }
  268. static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
  269. {
  270. struct mmc_request *mrq = host->mrq;
  271. struct mmc_data *data;
  272. u32 crc;
  273. WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
  274. if (host->mrq == NULL)
  275. return;
  276. data = mrq->cmd->data;
  277. if (status == 0)
  278. status = __raw_readl(HOST_STATUS(host));
  279. /* The transaction is really over when the SD_STATUS_DB bit is clear */
  280. while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
  281. status = __raw_readl(HOST_STATUS(host));
  282. data->error = 0;
  283. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
  284. /* Process any errors */
  285. crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
  286. if (host->flags & HOST_F_XMIT)
  287. crc |= ((status & 0x07) == 0x02) ? 0 : 1;
  288. if (crc)
  289. data->error = -EILSEQ;
  290. /* Clear the CRC bits */
  291. __raw_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
  292. data->bytes_xfered = 0;
  293. if (!data->error) {
  294. if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
  295. u32 chan = DMA_CHANNEL(host);
  296. chan_tab_t *c = *((chan_tab_t **)chan);
  297. au1x_dma_chan_t *cp = c->chan_ptr;
  298. data->bytes_xfered = cp->ddma_bytecnt;
  299. } else
  300. data->bytes_xfered =
  301. (data->blocks * data->blksz) - host->pio.len;
  302. }
  303. au1xmmc_finish_request(host);
  304. }
  305. static void au1xmmc_tasklet_data(unsigned long param)
  306. {
  307. struct au1xmmc_host *host = (struct au1xmmc_host *)param;
  308. u32 status = __raw_readl(HOST_STATUS(host));
  309. au1xmmc_data_complete(host, status);
  310. }
  311. #define AU1XMMC_MAX_TRANSFER 8
  312. static void au1xmmc_send_pio(struct au1xmmc_host *host)
  313. {
  314. struct mmc_data *data;
  315. int sg_len, max, count;
  316. unsigned char *sg_ptr, val;
  317. u32 status;
  318. struct scatterlist *sg;
  319. data = host->mrq->data;
  320. if (!(host->flags & HOST_F_XMIT))
  321. return;
  322. /* This is the pointer to the data buffer */
  323. sg = &data->sg[host->pio.index];
  324. sg_ptr = sg_virt(sg) + host->pio.offset;
  325. /* This is the space left inside the buffer */
  326. sg_len = data->sg[host->pio.index].length - host->pio.offset;
  327. /* Check if we need less than the size of the sg_buffer */
  328. max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
  329. if (max > AU1XMMC_MAX_TRANSFER)
  330. max = AU1XMMC_MAX_TRANSFER;
  331. for (count = 0; count < max; count++) {
  332. status = __raw_readl(HOST_STATUS(host));
  333. if (!(status & SD_STATUS_TH))
  334. break;
  335. val = *sg_ptr++;
  336. __raw_writel((unsigned long)val, HOST_TXPORT(host));
  337. wmb(); /* drain writebuffer */
  338. }
  339. host->pio.len -= count;
  340. host->pio.offset += count;
  341. if (count == sg_len) {
  342. host->pio.index++;
  343. host->pio.offset = 0;
  344. }
  345. if (host->pio.len == 0) {
  346. IRQ_OFF(host, SD_CONFIG_TH);
  347. if (host->flags & HOST_F_STOP)
  348. SEND_STOP(host);
  349. tasklet_schedule(&host->data_task);
  350. }
  351. }
  352. static void au1xmmc_receive_pio(struct au1xmmc_host *host)
  353. {
  354. struct mmc_data *data;
  355. int max, count, sg_len = 0;
  356. unsigned char *sg_ptr = NULL;
  357. u32 status, val;
  358. struct scatterlist *sg;
  359. data = host->mrq->data;
  360. if (!(host->flags & HOST_F_RECV))
  361. return;
  362. max = host->pio.len;
  363. if (host->pio.index < host->dma.len) {
  364. sg = &data->sg[host->pio.index];
  365. sg_ptr = sg_virt(sg) + host->pio.offset;
  366. /* This is the space left inside the buffer */
  367. sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
  368. /* Check if we need less than the size of the sg_buffer */
  369. if (sg_len < max)
  370. max = sg_len;
  371. }
  372. if (max > AU1XMMC_MAX_TRANSFER)
  373. max = AU1XMMC_MAX_TRANSFER;
  374. for (count = 0; count < max; count++) {
  375. status = __raw_readl(HOST_STATUS(host));
  376. if (!(status & SD_STATUS_NE))
  377. break;
  378. if (status & SD_STATUS_RC) {
  379. DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
  380. host->pio.len, count);
  381. break;
  382. }
  383. if (status & SD_STATUS_RO) {
  384. DBG("RX Overrun [%d + %d]\n", host->pdev->id,
  385. host->pio.len, count);
  386. break;
  387. }
  388. else if (status & SD_STATUS_RU) {
  389. DBG("RX Underrun [%d + %d]\n", host->pdev->id,
  390. host->pio.len, count);
  391. break;
  392. }
  393. val = __raw_readl(HOST_RXPORT(host));
  394. if (sg_ptr)
  395. *sg_ptr++ = (unsigned char)(val & 0xFF);
  396. }
  397. host->pio.len -= count;
  398. host->pio.offset += count;
  399. if (sg_len && count == sg_len) {
  400. host->pio.index++;
  401. host->pio.offset = 0;
  402. }
  403. if (host->pio.len == 0) {
  404. /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
  405. IRQ_OFF(host, SD_CONFIG_NE);
  406. if (host->flags & HOST_F_STOP)
  407. SEND_STOP(host);
  408. tasklet_schedule(&host->data_task);
  409. }
  410. }
  411. /* This is called when a command has been completed - grab the response
  412. * and check for errors. Then start the data transfer if it is indicated.
  413. */
  414. static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
  415. {
  416. struct mmc_request *mrq = host->mrq;
  417. struct mmc_command *cmd;
  418. u32 r[4];
  419. int i, trans;
  420. if (!host->mrq)
  421. return;
  422. cmd = mrq->cmd;
  423. cmd->error = 0;
  424. if (cmd->flags & MMC_RSP_PRESENT) {
  425. if (cmd->flags & MMC_RSP_136) {
  426. r[0] = __raw_readl(host->iobase + SD_RESP3);
  427. r[1] = __raw_readl(host->iobase + SD_RESP2);
  428. r[2] = __raw_readl(host->iobase + SD_RESP1);
  429. r[3] = __raw_readl(host->iobase + SD_RESP0);
  430. /* The CRC is omitted from the response, so really
  431. * we only got 120 bytes, but the engine expects
  432. * 128 bits, so we have to shift things up.
  433. */
  434. for (i = 0; i < 4; i++) {
  435. cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
  436. if (i != 3)
  437. cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
  438. }
  439. } else {
  440. /* Techincally, we should be getting all 48 bits of
  441. * the response (SD_RESP1 + SD_RESP2), but because
  442. * our response omits the CRC, our data ends up
  443. * being shifted 8 bits to the right. In this case,
  444. * that means that the OSR data starts at bit 31,
  445. * so we can just read RESP0 and return that.
  446. */
  447. cmd->resp[0] = __raw_readl(host->iobase + SD_RESP0);
  448. }
  449. }
  450. /* Figure out errors */
  451. if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
  452. cmd->error = -EILSEQ;
  453. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  454. if (!trans || cmd->error) {
  455. IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
  456. tasklet_schedule(&host->finish_task);
  457. return;
  458. }
  459. host->status = HOST_S_DATA;
  460. if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
  461. u32 channel = DMA_CHANNEL(host);
  462. /* Start the DBDMA as soon as the buffer gets something in it */
  463. if (host->flags & HOST_F_RECV) {
  464. u32 mask = SD_STATUS_DB | SD_STATUS_NE;
  465. while((status & mask) != mask)
  466. status = __raw_readl(HOST_STATUS(host));
  467. }
  468. au1xxx_dbdma_start(channel);
  469. }
  470. }
  471. static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
  472. {
  473. unsigned int pbus = clk_get_rate(host->clk);
  474. unsigned int divisor = ((pbus / rate) / 2) - 1;
  475. u32 config;
  476. config = __raw_readl(HOST_CONFIG(host));
  477. config &= ~(SD_CONFIG_DIV);
  478. config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
  479. __raw_writel(config, HOST_CONFIG(host));
  480. wmb(); /* drain writebuffer */
  481. }
  482. static int au1xmmc_prepare_data(struct au1xmmc_host *host,
  483. struct mmc_data *data)
  484. {
  485. int datalen = data->blocks * data->blksz;
  486. if (data->flags & MMC_DATA_READ)
  487. host->flags |= HOST_F_RECV;
  488. else
  489. host->flags |= HOST_F_XMIT;
  490. if (host->mrq->stop)
  491. host->flags |= HOST_F_STOP;
  492. host->dma.dir = DMA_BIDIRECTIONAL;
  493. host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  494. data->sg_len, host->dma.dir);
  495. if (host->dma.len == 0)
  496. return -ETIMEDOUT;
  497. __raw_writel(data->blksz - 1, HOST_BLKSIZE(host));
  498. if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
  499. int i;
  500. u32 channel = DMA_CHANNEL(host);
  501. au1xxx_dbdma_stop(channel);
  502. for (i = 0; i < host->dma.len; i++) {
  503. u32 ret = 0, flags = DDMA_FLAGS_NOIE;
  504. struct scatterlist *sg = &data->sg[i];
  505. int sg_len = sg->length;
  506. int len = (datalen > sg_len) ? sg_len : datalen;
  507. if (i == host->dma.len - 1)
  508. flags = DDMA_FLAGS_IE;
  509. if (host->flags & HOST_F_XMIT) {
  510. ret = au1xxx_dbdma_put_source(channel,
  511. sg_phys(sg), len, flags);
  512. } else {
  513. ret = au1xxx_dbdma_put_dest(channel,
  514. sg_phys(sg), len, flags);
  515. }
  516. if (!ret)
  517. goto dataerr;
  518. datalen -= len;
  519. }
  520. } else {
  521. host->pio.index = 0;
  522. host->pio.offset = 0;
  523. host->pio.len = datalen;
  524. if (host->flags & HOST_F_XMIT)
  525. IRQ_ON(host, SD_CONFIG_TH);
  526. else
  527. IRQ_ON(host, SD_CONFIG_NE);
  528. /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
  529. }
  530. return 0;
  531. dataerr:
  532. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  533. host->dma.dir);
  534. return -ETIMEDOUT;
  535. }
  536. /* This actually starts a command or data transaction */
  537. static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
  538. {
  539. struct au1xmmc_host *host = mmc_priv(mmc);
  540. int ret = 0;
  541. WARN_ON(irqs_disabled());
  542. WARN_ON(host->status != HOST_S_IDLE);
  543. host->mrq = mrq;
  544. host->status = HOST_S_CMD;
  545. /* fail request immediately if no card is present */
  546. if (0 == au1xmmc_card_inserted(mmc)) {
  547. mrq->cmd->error = -ENOMEDIUM;
  548. au1xmmc_finish_request(host);
  549. return;
  550. }
  551. if (mrq->data) {
  552. FLUSH_FIFO(host);
  553. ret = au1xmmc_prepare_data(host, mrq->data);
  554. }
  555. if (!ret)
  556. ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
  557. if (ret) {
  558. mrq->cmd->error = ret;
  559. au1xmmc_finish_request(host);
  560. }
  561. }
  562. static void au1xmmc_reset_controller(struct au1xmmc_host *host)
  563. {
  564. /* Apply the clock */
  565. __raw_writel(SD_ENABLE_CE, HOST_ENABLE(host));
  566. wmb(); /* drain writebuffer */
  567. mdelay(1);
  568. __raw_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
  569. wmb(); /* drain writebuffer */
  570. mdelay(5);
  571. __raw_writel(~0, HOST_STATUS(host));
  572. wmb(); /* drain writebuffer */
  573. __raw_writel(0, HOST_BLKSIZE(host));
  574. __raw_writel(0x001fffff, HOST_TIMEOUT(host));
  575. wmb(); /* drain writebuffer */
  576. __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  577. wmb(); /* drain writebuffer */
  578. __raw_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
  579. wmb(); /* drain writebuffer */
  580. mdelay(1);
  581. __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  582. wmb(); /* drain writebuffer */
  583. /* Configure interrupts */
  584. __raw_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
  585. wmb(); /* drain writebuffer */
  586. }
  587. static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  588. {
  589. struct au1xmmc_host *host = mmc_priv(mmc);
  590. u32 config2;
  591. if (ios->power_mode == MMC_POWER_OFF)
  592. au1xmmc_set_power(host, 0);
  593. else if (ios->power_mode == MMC_POWER_ON) {
  594. au1xmmc_set_power(host, 1);
  595. }
  596. if (ios->clock && ios->clock != host->clock) {
  597. au1xmmc_set_clock(host, ios->clock);
  598. host->clock = ios->clock;
  599. }
  600. config2 = __raw_readl(HOST_CONFIG2(host));
  601. switch (ios->bus_width) {
  602. case MMC_BUS_WIDTH_8:
  603. config2 |= SD_CONFIG2_BB;
  604. break;
  605. case MMC_BUS_WIDTH_4:
  606. config2 &= ~SD_CONFIG2_BB;
  607. config2 |= SD_CONFIG2_WB;
  608. break;
  609. case MMC_BUS_WIDTH_1:
  610. config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
  611. break;
  612. }
  613. __raw_writel(config2, HOST_CONFIG2(host));
  614. wmb(); /* drain writebuffer */
  615. }
  616. #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
  617. #define STATUS_DATA_IN (SD_STATUS_NE)
  618. #define STATUS_DATA_OUT (SD_STATUS_TH)
  619. static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
  620. {
  621. struct au1xmmc_host *host = dev_id;
  622. u32 status;
  623. status = __raw_readl(HOST_STATUS(host));
  624. if (!(status & SD_STATUS_I))
  625. return IRQ_NONE; /* not ours */
  626. if (status & SD_STATUS_SI) /* SDIO */
  627. mmc_signal_sdio_irq(host->mmc);
  628. if (host->mrq && (status & STATUS_TIMEOUT)) {
  629. if (status & SD_STATUS_RAT)
  630. host->mrq->cmd->error = -ETIMEDOUT;
  631. else if (status & SD_STATUS_DT)
  632. host->mrq->data->error = -ETIMEDOUT;
  633. /* In PIO mode, interrupts might still be enabled */
  634. IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
  635. /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
  636. tasklet_schedule(&host->finish_task);
  637. }
  638. #if 0
  639. else if (status & SD_STATUS_DD) {
  640. /* Sometimes we get a DD before a NE in PIO mode */
  641. if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
  642. au1xmmc_receive_pio(host);
  643. else {
  644. au1xmmc_data_complete(host, status);
  645. /* tasklet_schedule(&host->data_task); */
  646. }
  647. }
  648. #endif
  649. else if (status & SD_STATUS_CR) {
  650. if (host->status == HOST_S_CMD)
  651. au1xmmc_cmd_complete(host, status);
  652. } else if (!(host->flags & HOST_F_DMA)) {
  653. if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
  654. au1xmmc_send_pio(host);
  655. else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
  656. au1xmmc_receive_pio(host);
  657. } else if (status & 0x203F3C70) {
  658. DBG("Unhandled status %8.8x\n", host->pdev->id,
  659. status);
  660. }
  661. __raw_writel(status, HOST_STATUS(host));
  662. wmb(); /* drain writebuffer */
  663. return IRQ_HANDLED;
  664. }
  665. /* 8bit memory DMA device */
  666. static dbdev_tab_t au1xmmc_mem_dbdev = {
  667. .dev_id = DSCR_CMD0_ALWAYS,
  668. .dev_flags = DEV_FLAGS_ANYUSE,
  669. .dev_tsize = 0,
  670. .dev_devwidth = 8,
  671. .dev_physaddr = 0x00000000,
  672. .dev_intlevel = 0,
  673. .dev_intpolarity = 0,
  674. };
  675. static int memid;
  676. static void au1xmmc_dbdma_callback(int irq, void *dev_id)
  677. {
  678. struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
  679. /* Avoid spurious interrupts */
  680. if (!host->mrq)
  681. return;
  682. if (host->flags & HOST_F_STOP)
  683. SEND_STOP(host);
  684. tasklet_schedule(&host->data_task);
  685. }
  686. static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
  687. {
  688. struct resource *res;
  689. int txid, rxid;
  690. res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
  691. if (!res)
  692. return -ENODEV;
  693. txid = res->start;
  694. res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
  695. if (!res)
  696. return -ENODEV;
  697. rxid = res->start;
  698. if (!memid)
  699. return -ENODEV;
  700. host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
  701. au1xmmc_dbdma_callback, (void *)host);
  702. if (!host->tx_chan) {
  703. dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
  704. return -ENODEV;
  705. }
  706. host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
  707. au1xmmc_dbdma_callback, (void *)host);
  708. if (!host->rx_chan) {
  709. dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
  710. au1xxx_dbdma_chan_free(host->tx_chan);
  711. return -ENODEV;
  712. }
  713. au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
  714. au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
  715. au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
  716. au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
  717. /* DBDMA is good to go */
  718. host->flags |= HOST_F_DMA | HOST_F_DBDMA;
  719. return 0;
  720. }
  721. static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
  722. {
  723. if (host->flags & HOST_F_DMA) {
  724. host->flags &= ~HOST_F_DMA;
  725. au1xxx_dbdma_chan_free(host->tx_chan);
  726. au1xxx_dbdma_chan_free(host->rx_chan);
  727. }
  728. }
  729. static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
  730. {
  731. struct au1xmmc_host *host = mmc_priv(mmc);
  732. if (en)
  733. IRQ_ON(host, SD_CONFIG_SI);
  734. else
  735. IRQ_OFF(host, SD_CONFIG_SI);
  736. }
  737. static const struct mmc_host_ops au1xmmc_ops = {
  738. .request = au1xmmc_request,
  739. .set_ios = au1xmmc_set_ios,
  740. .get_ro = au1xmmc_card_readonly,
  741. .get_cd = au1xmmc_card_inserted,
  742. .enable_sdio_irq = au1xmmc_enable_sdio_irq,
  743. };
  744. static int au1xmmc_probe(struct platform_device *pdev)
  745. {
  746. struct mmc_host *mmc;
  747. struct au1xmmc_host *host;
  748. struct resource *r;
  749. int ret, iflag;
  750. mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
  751. if (!mmc) {
  752. dev_err(&pdev->dev, "no memory for mmc_host\n");
  753. ret = -ENOMEM;
  754. goto out0;
  755. }
  756. host = mmc_priv(mmc);
  757. host->mmc = mmc;
  758. host->platdata = pdev->dev.platform_data;
  759. host->pdev = pdev;
  760. ret = -ENODEV;
  761. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  762. if (!r) {
  763. dev_err(&pdev->dev, "no mmio defined\n");
  764. goto out1;
  765. }
  766. host->ioarea = request_mem_region(r->start, resource_size(r),
  767. pdev->name);
  768. if (!host->ioarea) {
  769. dev_err(&pdev->dev, "mmio already in use\n");
  770. goto out1;
  771. }
  772. host->iobase = ioremap(r->start, 0x3c);
  773. if (!host->iobase) {
  774. dev_err(&pdev->dev, "cannot remap mmio\n");
  775. goto out2;
  776. }
  777. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  778. if (!r) {
  779. dev_err(&pdev->dev, "no IRQ defined\n");
  780. goto out3;
  781. }
  782. host->irq = r->start;
  783. mmc->ops = &au1xmmc_ops;
  784. mmc->f_min = 450000;
  785. mmc->f_max = 24000000;
  786. mmc->max_blk_size = 2048;
  787. mmc->max_blk_count = 512;
  788. mmc->ocr_avail = AU1XMMC_OCR;
  789. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  790. mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
  791. iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
  792. switch (alchemy_get_cputype()) {
  793. case ALCHEMY_CPU_AU1100:
  794. mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
  795. break;
  796. case ALCHEMY_CPU_AU1200:
  797. mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
  798. break;
  799. case ALCHEMY_CPU_AU1300:
  800. iflag = 0; /* nothing is shared */
  801. mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
  802. mmc->f_max = 52000000;
  803. if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
  804. mmc->caps |= MMC_CAP_8_BIT_DATA;
  805. break;
  806. }
  807. ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
  808. if (ret) {
  809. dev_err(&pdev->dev, "cannot grab IRQ\n");
  810. goto out3;
  811. }
  812. host->clk = clk_get(&pdev->dev, ALCHEMY_PERIPH_CLK);
  813. if (IS_ERR(host->clk)) {
  814. dev_err(&pdev->dev, "cannot find clock\n");
  815. ret = PTR_ERR(host->clk);
  816. goto out_irq;
  817. }
  818. ret = clk_prepare_enable(host->clk);
  819. if (ret) {
  820. dev_err(&pdev->dev, "cannot enable clock\n");
  821. goto out_clk;
  822. }
  823. host->status = HOST_S_IDLE;
  824. /* board-specific carddetect setup, if any */
  825. if (host->platdata && host->platdata->cd_setup) {
  826. ret = host->platdata->cd_setup(mmc, 1);
  827. if (ret) {
  828. dev_warn(&pdev->dev, "board CD setup failed\n");
  829. mmc->caps |= MMC_CAP_NEEDS_POLL;
  830. }
  831. } else
  832. mmc->caps |= MMC_CAP_NEEDS_POLL;
  833. /* platform may not be able to use all advertised caps */
  834. if (host->platdata)
  835. mmc->caps &= ~(host->platdata->mask_host_caps);
  836. tasklet_init(&host->data_task, au1xmmc_tasklet_data,
  837. (unsigned long)host);
  838. tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
  839. (unsigned long)host);
  840. if (has_dbdma()) {
  841. ret = au1xmmc_dbdma_init(host);
  842. if (ret)
  843. pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
  844. }
  845. #ifdef CONFIG_LEDS_CLASS
  846. if (host->platdata && host->platdata->led) {
  847. struct led_classdev *led = host->platdata->led;
  848. led->name = mmc_hostname(mmc);
  849. led->brightness = LED_OFF;
  850. led->default_trigger = mmc_hostname(mmc);
  851. ret = led_classdev_register(mmc_dev(mmc), led);
  852. if (ret)
  853. goto out5;
  854. }
  855. #endif
  856. au1xmmc_reset_controller(host);
  857. ret = mmc_add_host(mmc);
  858. if (ret) {
  859. dev_err(&pdev->dev, "cannot add mmc host\n");
  860. goto out6;
  861. }
  862. platform_set_drvdata(pdev, host);
  863. pr_info(DRIVER_NAME ": MMC Controller %d set up at %p"
  864. " (mode=%s)\n", pdev->id, host->iobase,
  865. host->flags & HOST_F_DMA ? "dma" : "pio");
  866. return 0; /* all ok */
  867. out6:
  868. #ifdef CONFIG_LEDS_CLASS
  869. if (host->platdata && host->platdata->led)
  870. led_classdev_unregister(host->platdata->led);
  871. out5:
  872. #endif
  873. __raw_writel(0, HOST_ENABLE(host));
  874. __raw_writel(0, HOST_CONFIG(host));
  875. __raw_writel(0, HOST_CONFIG2(host));
  876. wmb(); /* drain writebuffer */
  877. if (host->flags & HOST_F_DBDMA)
  878. au1xmmc_dbdma_shutdown(host);
  879. tasklet_kill(&host->data_task);
  880. tasklet_kill(&host->finish_task);
  881. if (host->platdata && host->platdata->cd_setup &&
  882. !(mmc->caps & MMC_CAP_NEEDS_POLL))
  883. host->platdata->cd_setup(mmc, 0);
  884. out_clk:
  885. clk_disable_unprepare(host->clk);
  886. clk_put(host->clk);
  887. out_irq:
  888. free_irq(host->irq, host);
  889. out3:
  890. iounmap((void *)host->iobase);
  891. out2:
  892. release_resource(host->ioarea);
  893. kfree(host->ioarea);
  894. out1:
  895. mmc_free_host(mmc);
  896. out0:
  897. return ret;
  898. }
  899. static int au1xmmc_remove(struct platform_device *pdev)
  900. {
  901. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  902. if (host) {
  903. mmc_remove_host(host->mmc);
  904. #ifdef CONFIG_LEDS_CLASS
  905. if (host->platdata && host->platdata->led)
  906. led_classdev_unregister(host->platdata->led);
  907. #endif
  908. if (host->platdata && host->platdata->cd_setup &&
  909. !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
  910. host->platdata->cd_setup(host->mmc, 0);
  911. __raw_writel(0, HOST_ENABLE(host));
  912. __raw_writel(0, HOST_CONFIG(host));
  913. __raw_writel(0, HOST_CONFIG2(host));
  914. wmb(); /* drain writebuffer */
  915. tasklet_kill(&host->data_task);
  916. tasklet_kill(&host->finish_task);
  917. if (host->flags & HOST_F_DBDMA)
  918. au1xmmc_dbdma_shutdown(host);
  919. au1xmmc_set_power(host, 0);
  920. clk_disable_unprepare(host->clk);
  921. clk_put(host->clk);
  922. free_irq(host->irq, host);
  923. iounmap((void *)host->iobase);
  924. release_resource(host->ioarea);
  925. kfree(host->ioarea);
  926. mmc_free_host(host->mmc);
  927. }
  928. return 0;
  929. }
  930. #ifdef CONFIG_PM
  931. static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
  932. {
  933. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  934. __raw_writel(0, HOST_CONFIG2(host));
  935. __raw_writel(0, HOST_CONFIG(host));
  936. __raw_writel(0xffffffff, HOST_STATUS(host));
  937. __raw_writel(0, HOST_ENABLE(host));
  938. wmb(); /* drain writebuffer */
  939. return 0;
  940. }
  941. static int au1xmmc_resume(struct platform_device *pdev)
  942. {
  943. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  944. au1xmmc_reset_controller(host);
  945. return 0;
  946. }
  947. #else
  948. #define au1xmmc_suspend NULL
  949. #define au1xmmc_resume NULL
  950. #endif
  951. static struct platform_driver au1xmmc_driver = {
  952. .probe = au1xmmc_probe,
  953. .remove = au1xmmc_remove,
  954. .suspend = au1xmmc_suspend,
  955. .resume = au1xmmc_resume,
  956. .driver = {
  957. .name = DRIVER_NAME,
  958. },
  959. };
  960. static int __init au1xmmc_init(void)
  961. {
  962. if (has_dbdma()) {
  963. /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
  964. * of 8 bits. And since devices are shared, we need to create
  965. * our own to avoid freaking out other devices.
  966. */
  967. memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
  968. if (!memid)
  969. pr_err("au1xmmc: cannot add memory dbdma\n");
  970. }
  971. return platform_driver_register(&au1xmmc_driver);
  972. }
  973. static void __exit au1xmmc_exit(void)
  974. {
  975. if (has_dbdma() && memid)
  976. au1xxx_ddma_del_device(memid);
  977. platform_driver_unregister(&au1xmmc_driver);
  978. }
  979. module_init(au1xmmc_init);
  980. module_exit(au1xmmc_exit);
  981. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  982. MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
  983. MODULE_LICENSE("GPL");
  984. MODULE_ALIAS("platform:au1xxx-mmc");