au1xmmc.c 28 KB

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