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