au1xmmc.c 27 KB

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