au1xmmc.c 28 KB

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