au1xmmc.c 28 KB

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