au1xmmc.c 23 KB

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