au1xmmc.c 23 KB

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