au1xmmc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * linux/drivers/mmc/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 is a timer used to detect insert events?
  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. * So we use the timer to check the status manually.
  32. */
  33. #include <linux/config.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/device.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/mmc/host.h>
  41. #include <linux/mmc/protocol.h>
  42. #include <asm/io.h>
  43. #include <asm/mach-au1x00/au1000.h>
  44. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  45. #include <asm/mach-au1x00/au1100_mmc.h>
  46. #include <asm/scatterlist.h>
  47. #include <au1xxx.h>
  48. #include "au1xmmc.h"
  49. #define DRIVER_NAME "au1xxx-mmc"
  50. /* Set this to enable special debugging macros */
  51. /* #define MMC_DEBUG */
  52. #ifdef MMC_DEBUG
  53. #define DEBUG(fmt, idx, args...) printk("au1xx(%d): DEBUG: " fmt, idx, ##args)
  54. #else
  55. #define DEBUG(fmt, idx, args...)
  56. #endif
  57. const struct {
  58. u32 iobase;
  59. u32 tx_devid, rx_devid;
  60. u16 bcsrpwr;
  61. u16 bcsrstatus;
  62. u16 wpstatus;
  63. } au1xmmc_card_table[] = {
  64. { SD0_BASE, DSCR_CMD0_SDMS_TX0, DSCR_CMD0_SDMS_RX0,
  65. BCSR_BOARD_SD0PWR, BCSR_INT_SD0INSERT, BCSR_STATUS_SD0WP },
  66. #ifndef CONFIG_MIPS_DB1200
  67. { SD1_BASE, DSCR_CMD0_SDMS_TX1, DSCR_CMD0_SDMS_RX1,
  68. BCSR_BOARD_DS1PWR, BCSR_INT_SD1INSERT, BCSR_STATUS_SD1WP }
  69. #endif
  70. };
  71. #define AU1XMMC_CONTROLLER_COUNT \
  72. (sizeof(au1xmmc_card_table) / sizeof(au1xmmc_card_table[0]))
  73. /* This array stores pointers for the hosts (used by the IRQ handler) */
  74. struct au1xmmc_host *au1xmmc_hosts[AU1XMMC_CONTROLLER_COUNT];
  75. static int dma = 1;
  76. #ifdef MODULE
  77. MODULE_PARM(dma, "i");
  78. MODULE_PARM_DESC(dma, "Use DMA engine for data transfers (0 = disabled)");
  79. #endif
  80. static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
  81. {
  82. u32 val = au_readl(HOST_CONFIG(host));
  83. val |= mask;
  84. au_writel(val, HOST_CONFIG(host));
  85. au_sync();
  86. }
  87. static inline void FLUSH_FIFO(struct au1xmmc_host *host)
  88. {
  89. u32 val = au_readl(HOST_CONFIG2(host));
  90. au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
  91. au_sync_delay(1);
  92. /* SEND_STOP will turn off clock control - this re-enables it */
  93. val &= ~SD_CONFIG2_DF;
  94. au_writel(val, HOST_CONFIG2(host));
  95. au_sync();
  96. }
  97. static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
  98. {
  99. u32 val = au_readl(HOST_CONFIG(host));
  100. val &= ~mask;
  101. au_writel(val, HOST_CONFIG(host));
  102. au_sync();
  103. }
  104. static inline void SEND_STOP(struct au1xmmc_host *host)
  105. {
  106. /* We know the value of CONFIG2, so avoid a read we don't need */
  107. u32 mask = SD_CONFIG2_EN;
  108. WARN_ON(host->status != HOST_S_DATA);
  109. host->status = HOST_S_STOP;
  110. au_writel(mask | SD_CONFIG2_DF, HOST_CONFIG2(host));
  111. au_sync();
  112. /* Send the stop commmand */
  113. au_writel(STOP_CMD, HOST_CMD(host));
  114. }
  115. static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
  116. {
  117. u32 val = au1xmmc_card_table[host->id].bcsrpwr;
  118. bcsr->board &= ~val;
  119. if (state) bcsr->board |= val;
  120. au_sync_delay(1);
  121. }
  122. static inline int au1xmmc_card_inserted(struct au1xmmc_host *host)
  123. {
  124. return (bcsr->sig_status & au1xmmc_card_table[host->id].bcsrstatus)
  125. ? 1 : 0;
  126. }
  127. static inline int au1xmmc_card_readonly(struct au1xmmc_host *host)
  128. {
  129. return (bcsr->status & au1xmmc_card_table[host->id].wpstatus)
  130. ? 1 : 0;
  131. }
  132. static void au1xmmc_finish_request(struct au1xmmc_host *host)
  133. {
  134. struct mmc_request *mrq = host->mrq;
  135. host->mrq = NULL;
  136. host->flags &= HOST_F_ACTIVE;
  137. host->dma.len = 0;
  138. host->dma.dir = 0;
  139. host->pio.index = 0;
  140. host->pio.offset = 0;
  141. host->pio.len = 0;
  142. host->status = HOST_S_IDLE;
  143. bcsr->disk_leds |= (1 << 8);
  144. mmc_request_done(host->mmc, mrq);
  145. }
  146. static void au1xmmc_tasklet_finish(unsigned long param)
  147. {
  148. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  149. au1xmmc_finish_request(host);
  150. }
  151. static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
  152. struct mmc_command *cmd)
  153. {
  154. u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
  155. switch(cmd->flags) {
  156. case MMC_RSP_R1:
  157. mmccmd |= SD_CMD_RT_1;
  158. break;
  159. case MMC_RSP_R1B:
  160. mmccmd |= SD_CMD_RT_1B;
  161. break;
  162. case MMC_RSP_R2:
  163. mmccmd |= SD_CMD_RT_2;
  164. break;
  165. case MMC_RSP_R3:
  166. mmccmd |= SD_CMD_RT_3;
  167. break;
  168. }
  169. switch(cmd->opcode) {
  170. case MMC_READ_SINGLE_BLOCK:
  171. case SD_APP_SEND_SCR:
  172. mmccmd |= SD_CMD_CT_2;
  173. break;
  174. case MMC_READ_MULTIPLE_BLOCK:
  175. mmccmd |= SD_CMD_CT_4;
  176. break;
  177. case MMC_WRITE_BLOCK:
  178. mmccmd |= SD_CMD_CT_1;
  179. break;
  180. case MMC_WRITE_MULTIPLE_BLOCK:
  181. mmccmd |= SD_CMD_CT_3;
  182. break;
  183. case MMC_STOP_TRANSMISSION:
  184. mmccmd |= SD_CMD_CT_7;
  185. break;
  186. }
  187. au_writel(cmd->arg, HOST_CMDARG(host));
  188. au_sync();
  189. if (wait)
  190. IRQ_OFF(host, SD_CONFIG_CR);
  191. au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
  192. au_sync();
  193. /* Wait for the command to go on the line */
  194. while(1) {
  195. if (!(au_readl(HOST_CMD(host)) & SD_CMD_GO))
  196. break;
  197. }
  198. /* Wait for the command to come back */
  199. if (wait) {
  200. u32 status = au_readl(HOST_STATUS(host));
  201. while(!(status & SD_STATUS_CR))
  202. status = au_readl(HOST_STATUS(host));
  203. /* Clear the CR status */
  204. au_writel(SD_STATUS_CR, HOST_STATUS(host));
  205. IRQ_ON(host, SD_CONFIG_CR);
  206. }
  207. return MMC_ERR_NONE;
  208. }
  209. static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
  210. {
  211. struct mmc_request *mrq = host->mrq;
  212. struct mmc_data *data;
  213. u32 crc;
  214. WARN_ON(host->status != HOST_S_DATA && host->status != HOST_S_STOP);
  215. if (host->mrq == NULL)
  216. return;
  217. data = mrq->cmd->data;
  218. if (status == 0)
  219. status = au_readl(HOST_STATUS(host));
  220. /* The transaction is really over when the SD_STATUS_DB bit is clear */
  221. while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
  222. status = au_readl(HOST_STATUS(host));
  223. data->error = MMC_ERR_NONE;
  224. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
  225. /* Process any errors */
  226. crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
  227. if (host->flags & HOST_F_XMIT)
  228. crc |= ((status & 0x07) == 0x02) ? 0 : 1;
  229. if (crc)
  230. data->error = MMC_ERR_BADCRC;
  231. /* Clear the CRC bits */
  232. au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
  233. data->bytes_xfered = 0;
  234. if (data->error == MMC_ERR_NONE) {
  235. if (host->flags & HOST_F_DMA) {
  236. u32 chan = DMA_CHANNEL(host);
  237. chan_tab_t *c = *((chan_tab_t **) chan);
  238. au1x_dma_chan_t *cp = c->chan_ptr;
  239. data->bytes_xfered = cp->ddma_bytecnt;
  240. }
  241. else
  242. data->bytes_xfered =
  243. (data->blocks * (1 << data->blksz_bits)) -
  244. host->pio.len;
  245. }
  246. au1xmmc_finish_request(host);
  247. }
  248. static void au1xmmc_tasklet_data(unsigned long param)
  249. {
  250. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  251. u32 status = au_readl(HOST_STATUS(host));
  252. au1xmmc_data_complete(host, status);
  253. }
  254. #define AU1XMMC_MAX_TRANSFER 8
  255. static void au1xmmc_send_pio(struct au1xmmc_host *host)
  256. {
  257. struct mmc_data *data = 0;
  258. int sg_len, max, count = 0;
  259. unsigned char *sg_ptr;
  260. u32 status = 0;
  261. struct scatterlist *sg;
  262. data = host->mrq->data;
  263. if (!(host->flags & HOST_F_XMIT))
  264. return;
  265. /* This is the pointer to the data buffer */
  266. sg = &data->sg[host->pio.index];
  267. sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
  268. /* This is the space left inside the buffer */
  269. sg_len = data->sg[host->pio.index].length - host->pio.offset;
  270. /* Check to if we need less then the size of the sg_buffer */
  271. max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
  272. if (max > AU1XMMC_MAX_TRANSFER) max = AU1XMMC_MAX_TRANSFER;
  273. for(count = 0; count < max; count++ ) {
  274. unsigned char val;
  275. status = au_readl(HOST_STATUS(host));
  276. if (!(status & SD_STATUS_TH))
  277. break;
  278. val = *sg_ptr++;
  279. au_writel((unsigned long) val, HOST_TXPORT(host));
  280. au_sync();
  281. }
  282. host->pio.len -= count;
  283. host->pio.offset += count;
  284. if (count == sg_len) {
  285. host->pio.index++;
  286. host->pio.offset = 0;
  287. }
  288. if (host->pio.len == 0) {
  289. IRQ_OFF(host, SD_CONFIG_TH);
  290. if (host->flags & HOST_F_STOP)
  291. SEND_STOP(host);
  292. tasklet_schedule(&host->data_task);
  293. }
  294. }
  295. static void au1xmmc_receive_pio(struct au1xmmc_host *host)
  296. {
  297. struct mmc_data *data = 0;
  298. int sg_len = 0, max = 0, count = 0;
  299. unsigned char *sg_ptr = 0;
  300. u32 status = 0;
  301. struct scatterlist *sg;
  302. data = host->mrq->data;
  303. if (!(host->flags & HOST_F_RECV))
  304. return;
  305. max = host->pio.len;
  306. if (host->pio.index < host->dma.len) {
  307. sg = &data->sg[host->pio.index];
  308. sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
  309. /* This is the space left inside the buffer */
  310. sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
  311. /* Check to if we need less then the size of the sg_buffer */
  312. if (sg_len < max) max = sg_len;
  313. }
  314. if (max > AU1XMMC_MAX_TRANSFER)
  315. max = AU1XMMC_MAX_TRANSFER;
  316. for(count = 0; count < max; count++ ) {
  317. u32 val;
  318. status = au_readl(HOST_STATUS(host));
  319. if (!(status & SD_STATUS_NE))
  320. break;
  321. if (status & SD_STATUS_RC) {
  322. DEBUG("RX CRC Error [%d + %d].\n", host->id,
  323. host->pio.len, count);
  324. break;
  325. }
  326. if (status & SD_STATUS_RO) {
  327. DEBUG("RX Overrun [%d + %d]\n", host->id,
  328. host->pio.len, count);
  329. break;
  330. }
  331. else if (status & SD_STATUS_RU) {
  332. DEBUG("RX Underrun [%d + %d]\n", host->id,
  333. host->pio.len, count);
  334. break;
  335. }
  336. val = au_readl(HOST_RXPORT(host));
  337. if (sg_ptr)
  338. *sg_ptr++ = (unsigned char) (val & 0xFF);
  339. }
  340. host->pio.len -= count;
  341. host->pio.offset += count;
  342. if (sg_len && count == sg_len) {
  343. host->pio.index++;
  344. host->pio.offset = 0;
  345. }
  346. if (host->pio.len == 0) {
  347. //IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF);
  348. IRQ_OFF(host, SD_CONFIG_NE);
  349. if (host->flags & HOST_F_STOP)
  350. SEND_STOP(host);
  351. tasklet_schedule(&host->data_task);
  352. }
  353. }
  354. /* static void au1xmmc_cmd_complete
  355. This is called when a command has been completed - grab the response
  356. and check for errors. Then start the data transfer if it is indicated.
  357. */
  358. static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
  359. {
  360. struct mmc_request *mrq = host->mrq;
  361. struct mmc_command *cmd;
  362. int trans;
  363. if (!host->mrq)
  364. return;
  365. cmd = mrq->cmd;
  366. cmd->error = MMC_ERR_NONE;
  367. if ((cmd->flags & MMC_RSP_MASK) == MMC_RSP_SHORT) {
  368. /* Techincally, we should be getting all 48 bits of the response
  369. * (SD_RESP1 + SD_RESP2), but because our response omits the CRC,
  370. * our data ends up being shifted 8 bits to the right. In this case,
  371. * that means that the OSR data starts at bit 31, so we can just
  372. * read RESP0 and return that
  373. */
  374. cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
  375. }
  376. else if ((cmd->flags & MMC_RSP_MASK) == MMC_RSP_LONG) {
  377. u32 r[4];
  378. int i;
  379. r[0] = au_readl(host->iobase + SD_RESP3);
  380. r[1] = au_readl(host->iobase + SD_RESP2);
  381. r[2] = au_readl(host->iobase + SD_RESP1);
  382. r[3] = au_readl(host->iobase + SD_RESP0);
  383. /* The CRC is omitted from the response, so really we only got
  384. * 120 bytes, but the engine expects 128 bits, so we have to shift
  385. * things up
  386. */
  387. for(i = 0; i < 4; i++) {
  388. cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
  389. if (i != 3) cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
  390. }
  391. }
  392. /* Figure out errors */
  393. if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
  394. cmd->error = MMC_ERR_BADCRC;
  395. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  396. if (!trans || cmd->error != MMC_ERR_NONE) {
  397. IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
  398. tasklet_schedule(&host->finish_task);
  399. return;
  400. }
  401. host->status = HOST_S_DATA;
  402. if (host->flags & HOST_F_DMA) {
  403. u32 channel = DMA_CHANNEL(host);
  404. /* Start the DMA as soon as the buffer gets something in it */
  405. if (host->flags & HOST_F_RECV) {
  406. u32 mask = SD_STATUS_DB | SD_STATUS_NE;
  407. while((status & mask) != mask)
  408. status = au_readl(HOST_STATUS(host));
  409. }
  410. au1xxx_dbdma_start(channel);
  411. }
  412. }
  413. static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
  414. {
  415. unsigned int pbus = get_au1x00_speed();
  416. unsigned int divisor;
  417. u32 config;
  418. /* From databook:
  419. divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
  420. */
  421. pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
  422. pbus /= 2;
  423. divisor = ((pbus / rate) / 2) - 1;
  424. config = au_readl(HOST_CONFIG(host));
  425. config &= ~(SD_CONFIG_DIV);
  426. config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
  427. au_writel(config, HOST_CONFIG(host));
  428. au_sync();
  429. }
  430. static int
  431. au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
  432. {
  433. int datalen = data->blocks * (1 << data->blksz_bits);
  434. if (dma != 0)
  435. host->flags |= HOST_F_DMA;
  436. if (data->flags & MMC_DATA_READ)
  437. host->flags |= HOST_F_RECV;
  438. else
  439. host->flags |= HOST_F_XMIT;
  440. if (host->mrq->stop)
  441. host->flags |= HOST_F_STOP;
  442. host->dma.dir = DMA_BIDIRECTIONAL;
  443. host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  444. data->sg_len, host->dma.dir);
  445. if (host->dma.len == 0)
  446. return MMC_ERR_TIMEOUT;
  447. au_writel((1 << data->blksz_bits) - 1, HOST_BLKSIZE(host));
  448. if (host->flags & HOST_F_DMA) {
  449. int i;
  450. u32 channel = DMA_CHANNEL(host);
  451. au1xxx_dbdma_stop(channel);
  452. for(i = 0; i < host->dma.len; i++) {
  453. u32 ret = 0, flags = DDMA_FLAGS_NOIE;
  454. struct scatterlist *sg = &data->sg[i];
  455. int sg_len = sg->length;
  456. int len = (datalen > sg_len) ? sg_len : datalen;
  457. if (i == host->dma.len - 1)
  458. flags = DDMA_FLAGS_IE;
  459. if (host->flags & HOST_F_XMIT){
  460. ret = au1xxx_dbdma_put_source_flags(channel,
  461. (void *) (page_address(sg->page) +
  462. sg->offset),
  463. len, flags);
  464. }
  465. else {
  466. ret = au1xxx_dbdma_put_dest_flags(channel,
  467. (void *) (page_address(sg->page) +
  468. sg->offset),
  469. len, flags);
  470. }
  471. if (!ret)
  472. goto dataerr;
  473. datalen -= len;
  474. }
  475. }
  476. else {
  477. host->pio.index = 0;
  478. host->pio.offset = 0;
  479. host->pio.len = datalen;
  480. if (host->flags & HOST_F_XMIT)
  481. IRQ_ON(host, SD_CONFIG_TH);
  482. else
  483. IRQ_ON(host, SD_CONFIG_NE);
  484. //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
  485. }
  486. return MMC_ERR_NONE;
  487. dataerr:
  488. dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
  489. return MMC_ERR_TIMEOUT;
  490. }
  491. /* static void au1xmmc_request
  492. This actually starts a command or data transaction
  493. */
  494. static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
  495. {
  496. struct au1xmmc_host *host = mmc_priv(mmc);
  497. int ret = MMC_ERR_NONE;
  498. WARN_ON(irqs_disabled());
  499. WARN_ON(host->status != HOST_S_IDLE);
  500. host->mrq = mrq;
  501. host->status = HOST_S_CMD;
  502. bcsr->disk_leds &= ~(1 << 8);
  503. if (mrq->data) {
  504. FLUSH_FIFO(host);
  505. ret = au1xmmc_prepare_data(host, mrq->data);
  506. }
  507. if (ret == MMC_ERR_NONE)
  508. ret = au1xmmc_send_command(host, 0, mrq->cmd);
  509. if (ret != MMC_ERR_NONE) {
  510. mrq->cmd->error = ret;
  511. au1xmmc_finish_request(host);
  512. }
  513. }
  514. static void au1xmmc_reset_controller(struct au1xmmc_host *host)
  515. {
  516. /* Apply the clock */
  517. au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
  518. au_sync_delay(1);
  519. au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
  520. au_sync_delay(5);
  521. au_writel(~0, HOST_STATUS(host));
  522. au_sync();
  523. au_writel(0, HOST_BLKSIZE(host));
  524. au_writel(0x001fffff, HOST_TIMEOUT(host));
  525. au_sync();
  526. au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  527. au_sync();
  528. au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
  529. au_sync_delay(1);
  530. au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  531. au_sync();
  532. /* Configure interrupts */
  533. au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
  534. au_sync();
  535. }
  536. static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
  537. {
  538. struct au1xmmc_host *host = mmc_priv(mmc);
  539. DEBUG("set_ios (power=%u, clock=%uHz, vdd=%u, mode=%u)\n",
  540. host->id, ios->power_mode, ios->clock, ios->vdd,
  541. ios->bus_mode);
  542. if (ios->power_mode == MMC_POWER_OFF)
  543. au1xmmc_set_power(host, 0);
  544. else if (ios->power_mode == MMC_POWER_ON) {
  545. au1xmmc_set_power(host, 1);
  546. }
  547. if (ios->clock && ios->clock != host->clock) {
  548. au1xmmc_set_clock(host, ios->clock);
  549. host->clock = ios->clock;
  550. }
  551. }
  552. static void au1xmmc_dma_callback(int irq, void *dev_id, struct pt_regs *regs)
  553. {
  554. struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
  555. u32 status;
  556. /* Avoid spurious interrupts */
  557. if (!host->mrq)
  558. return;
  559. if (host->flags & HOST_F_STOP)
  560. SEND_STOP(host);
  561. tasklet_schedule(&host->data_task);
  562. }
  563. #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
  564. #define STATUS_DATA_IN (SD_STATUS_NE)
  565. #define STATUS_DATA_OUT (SD_STATUS_TH)
  566. static irqreturn_t au1xmmc_irq(int irq, void *dev_id, struct pt_regs *regs)
  567. {
  568. u32 status;
  569. int i, ret = 0;
  570. disable_irq(AU1100_SD_IRQ);
  571. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  572. struct au1xmmc_host * host = au1xmmc_hosts[i];
  573. u32 handled = 1;
  574. status = au_readl(HOST_STATUS(host));
  575. if (host->mrq && (status & STATUS_TIMEOUT)) {
  576. if (status & SD_STATUS_RAT)
  577. host->mrq->cmd->error = MMC_ERR_TIMEOUT;
  578. else if (status & SD_STATUS_DT)
  579. host->mrq->data->error = MMC_ERR_TIMEOUT;
  580. /* In PIO mode, interrupts might still be enabled */
  581. IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
  582. //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF);
  583. tasklet_schedule(&host->finish_task);
  584. }
  585. #if 0
  586. else if (status & SD_STATUS_DD) {
  587. /* Sometimes we get a DD before a NE in PIO mode */
  588. if (!(host->flags & HOST_F_DMA) &&
  589. (status & SD_STATUS_NE))
  590. au1xmmc_receive_pio(host);
  591. else {
  592. au1xmmc_data_complete(host, status);
  593. //tasklet_schedule(&host->data_task);
  594. }
  595. }
  596. #endif
  597. else if (status & (SD_STATUS_CR)) {
  598. if (host->status == HOST_S_CMD)
  599. au1xmmc_cmd_complete(host,status);
  600. }
  601. else if (!(host->flags & HOST_F_DMA)) {
  602. if ((host->flags & HOST_F_XMIT) &&
  603. (status & STATUS_DATA_OUT))
  604. au1xmmc_send_pio(host);
  605. else if ((host->flags & HOST_F_RECV) &&
  606. (status & STATUS_DATA_IN))
  607. au1xmmc_receive_pio(host);
  608. }
  609. else if (status & 0x203FBC70) {
  610. DEBUG("Unhandled status %8.8x\n", host->id, status);
  611. handled = 0;
  612. }
  613. au_writel(status, HOST_STATUS(host));
  614. au_sync();
  615. ret |= handled;
  616. }
  617. enable_irq(AU1100_SD_IRQ);
  618. return ret;
  619. }
  620. static void au1xmmc_poll_event(unsigned long arg)
  621. {
  622. struct au1xmmc_host *host = (struct au1xmmc_host *) arg;
  623. int card = au1xmmc_card_inserted(host);
  624. int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
  625. if (card != controller) {
  626. host->flags &= ~HOST_F_ACTIVE;
  627. if (card) host->flags |= HOST_F_ACTIVE;
  628. mmc_detect_change(host->mmc, 0);
  629. }
  630. if (host->mrq != NULL) {
  631. u32 status = au_readl(HOST_STATUS(host));
  632. DEBUG("PENDING - %8.8x\n", host->id, status);
  633. }
  634. mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
  635. }
  636. static dbdev_tab_t au1xmmc_mem_dbdev =
  637. {
  638. DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
  639. };
  640. static void au1xmmc_init_dma(struct au1xmmc_host *host)
  641. {
  642. u32 rxchan, txchan;
  643. int txid = au1xmmc_card_table[host->id].tx_devid;
  644. int rxid = au1xmmc_card_table[host->id].rx_devid;
  645. /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
  646. of 8 bits. And since devices are shared, we need to create
  647. our own to avoid freaking out other devices
  648. */
  649. int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
  650. txchan = au1xxx_dbdma_chan_alloc(memid, txid,
  651. au1xmmc_dma_callback, (void *) host);
  652. rxchan = au1xxx_dbdma_chan_alloc(rxid, memid,
  653. au1xmmc_dma_callback, (void *) host);
  654. au1xxx_dbdma_set_devwidth(txchan, 8);
  655. au1xxx_dbdma_set_devwidth(rxchan, 8);
  656. au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT);
  657. au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT);
  658. host->tx_chan = txchan;
  659. host->rx_chan = rxchan;
  660. }
  661. struct mmc_host_ops au1xmmc_ops = {
  662. .request = au1xmmc_request,
  663. .set_ios = au1xmmc_set_ios,
  664. };
  665. static int au1xmmc_probe(struct device *dev)
  666. {
  667. int i, ret = 0;
  668. /* THe interrupt is shared among all controllers */
  669. ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, SA_INTERRUPT, "MMC", 0);
  670. if (ret) {
  671. printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
  672. AU1100_SD_IRQ, ret);
  673. return -ENXIO;
  674. }
  675. disable_irq(AU1100_SD_IRQ);
  676. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  677. struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), dev);
  678. struct au1xmmc_host *host = 0;
  679. if (!mmc) {
  680. printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
  681. au1xmmc_hosts[i] = 0;
  682. continue;
  683. }
  684. mmc->ops = &au1xmmc_ops;
  685. mmc->f_min = 450000;
  686. mmc->f_max = 24000000;
  687. mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
  688. mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
  689. mmc->ocr_avail = AU1XMMC_OCR;
  690. host = mmc_priv(mmc);
  691. host->mmc = mmc;
  692. host->id = i;
  693. host->iobase = au1xmmc_card_table[host->id].iobase;
  694. host->clock = 0;
  695. host->power_mode = MMC_POWER_OFF;
  696. host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
  697. host->status = HOST_S_IDLE;
  698. init_timer(&host->timer);
  699. host->timer.function = au1xmmc_poll_event;
  700. host->timer.data = (unsigned long) host;
  701. host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
  702. tasklet_init(&host->data_task, au1xmmc_tasklet_data,
  703. (unsigned long) host);
  704. tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
  705. (unsigned long) host);
  706. spin_lock_init(&host->lock);
  707. if (dma != 0)
  708. au1xmmc_init_dma(host);
  709. au1xmmc_reset_controller(host);
  710. mmc_add_host(mmc);
  711. au1xmmc_hosts[i] = host;
  712. add_timer(&host->timer);
  713. printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
  714. host->id, host->iobase, dma ? "dma" : "pio");
  715. }
  716. enable_irq(AU1100_SD_IRQ);
  717. return 0;
  718. }
  719. static int au1xmmc_remove(struct device *dev)
  720. {
  721. int i;
  722. disable_irq(AU1100_SD_IRQ);
  723. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  724. struct au1xmmc_host *host = au1xmmc_hosts[i];
  725. if (!host) continue;
  726. tasklet_kill(&host->data_task);
  727. tasklet_kill(&host->finish_task);
  728. del_timer_sync(&host->timer);
  729. au1xmmc_set_power(host, 0);
  730. mmc_remove_host(host->mmc);
  731. au1xxx_dbdma_chan_free(host->tx_chan);
  732. au1xxx_dbdma_chan_free(host->rx_chan);
  733. au_writel(0x0, HOST_ENABLE(host));
  734. au_sync();
  735. }
  736. free_irq(AU1100_SD_IRQ, 0);
  737. return 0;
  738. }
  739. static struct device_driver au1xmmc_driver = {
  740. .name = DRIVER_NAME,
  741. .bus = &platform_bus_type,
  742. .probe = au1xmmc_probe,
  743. .remove = au1xmmc_remove,
  744. .suspend = NULL,
  745. .resume = NULL
  746. };
  747. static int __init au1xmmc_init(void)
  748. {
  749. return driver_register(&au1xmmc_driver);
  750. }
  751. static void __exit au1xmmc_exit(void)
  752. {
  753. driver_unregister(&au1xmmc_driver);
  754. }
  755. module_init(au1xmmc_init);
  756. module_exit(au1xmmc_exit);
  757. #ifdef MODULE
  758. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  759. MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
  760. MODULE_LICENSE("GPL");
  761. #endif