au1xmmc.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 (mmc_rsp_type(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_PRESENT) {
  368. if (cmd->flags & MMC_RSP_136) {
  369. u32 r[4];
  370. int i;
  371. r[0] = au_readl(host->iobase + SD_RESP3);
  372. r[1] = au_readl(host->iobase + SD_RESP2);
  373. r[2] = au_readl(host->iobase + SD_RESP1);
  374. r[3] = au_readl(host->iobase + SD_RESP0);
  375. /* The CRC is omitted from the response, so really
  376. * we only got 120 bytes, but the engine expects
  377. * 128 bits, so we have to shift things up
  378. */
  379. for(i = 0; i < 4; i++) {
  380. cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
  381. if (i != 3)
  382. cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
  383. }
  384. } else {
  385. /* Techincally, we should be getting all 48 bits of
  386. * the response (SD_RESP1 + SD_RESP2), but because
  387. * our response omits the CRC, our data ends up
  388. * being shifted 8 bits to the right. In this case,
  389. * that means that the OSR data starts at bit 31,
  390. * so we can just read RESP0 and return that
  391. */
  392. cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
  393. }
  394. }
  395. /* Figure out errors */
  396. if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
  397. cmd->error = MMC_ERR_BADCRC;
  398. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  399. if (!trans || cmd->error != MMC_ERR_NONE) {
  400. IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
  401. tasklet_schedule(&host->finish_task);
  402. return;
  403. }
  404. host->status = HOST_S_DATA;
  405. if (host->flags & HOST_F_DMA) {
  406. u32 channel = DMA_CHANNEL(host);
  407. /* Start the DMA as soon as the buffer gets something in it */
  408. if (host->flags & HOST_F_RECV) {
  409. u32 mask = SD_STATUS_DB | SD_STATUS_NE;
  410. while((status & mask) != mask)
  411. status = au_readl(HOST_STATUS(host));
  412. }
  413. au1xxx_dbdma_start(channel);
  414. }
  415. }
  416. static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
  417. {
  418. unsigned int pbus = get_au1x00_speed();
  419. unsigned int divisor;
  420. u32 config;
  421. /* From databook:
  422. divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
  423. */
  424. pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
  425. pbus /= 2;
  426. divisor = ((pbus / rate) / 2) - 1;
  427. config = au_readl(HOST_CONFIG(host));
  428. config &= ~(SD_CONFIG_DIV);
  429. config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
  430. au_writel(config, HOST_CONFIG(host));
  431. au_sync();
  432. }
  433. static int
  434. au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
  435. {
  436. int datalen = data->blocks * (1 << data->blksz_bits);
  437. if (dma != 0)
  438. host->flags |= HOST_F_DMA;
  439. if (data->flags & MMC_DATA_READ)
  440. host->flags |= HOST_F_RECV;
  441. else
  442. host->flags |= HOST_F_XMIT;
  443. if (host->mrq->stop)
  444. host->flags |= HOST_F_STOP;
  445. host->dma.dir = DMA_BIDIRECTIONAL;
  446. host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  447. data->sg_len, host->dma.dir);
  448. if (host->dma.len == 0)
  449. return MMC_ERR_TIMEOUT;
  450. au_writel((1 << data->blksz_bits) - 1, HOST_BLKSIZE(host));
  451. if (host->flags & HOST_F_DMA) {
  452. int i;
  453. u32 channel = DMA_CHANNEL(host);
  454. au1xxx_dbdma_stop(channel);
  455. for(i = 0; i < host->dma.len; i++) {
  456. u32 ret = 0, flags = DDMA_FLAGS_NOIE;
  457. struct scatterlist *sg = &data->sg[i];
  458. int sg_len = sg->length;
  459. int len = (datalen > sg_len) ? sg_len : datalen;
  460. if (i == host->dma.len - 1)
  461. flags = DDMA_FLAGS_IE;
  462. if (host->flags & HOST_F_XMIT){
  463. ret = au1xxx_dbdma_put_source_flags(channel,
  464. (void *) (page_address(sg->page) +
  465. sg->offset),
  466. len, flags);
  467. }
  468. else {
  469. ret = au1xxx_dbdma_put_dest_flags(channel,
  470. (void *) (page_address(sg->page) +
  471. sg->offset),
  472. len, flags);
  473. }
  474. if (!ret)
  475. goto dataerr;
  476. datalen -= len;
  477. }
  478. }
  479. else {
  480. host->pio.index = 0;
  481. host->pio.offset = 0;
  482. host->pio.len = datalen;
  483. if (host->flags & HOST_F_XMIT)
  484. IRQ_ON(host, SD_CONFIG_TH);
  485. else
  486. IRQ_ON(host, SD_CONFIG_NE);
  487. //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
  488. }
  489. return MMC_ERR_NONE;
  490. dataerr:
  491. dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
  492. return MMC_ERR_TIMEOUT;
  493. }
  494. /* static void au1xmmc_request
  495. This actually starts a command or data transaction
  496. */
  497. static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
  498. {
  499. struct au1xmmc_host *host = mmc_priv(mmc);
  500. int ret = MMC_ERR_NONE;
  501. WARN_ON(irqs_disabled());
  502. WARN_ON(host->status != HOST_S_IDLE);
  503. host->mrq = mrq;
  504. host->status = HOST_S_CMD;
  505. bcsr->disk_leds &= ~(1 << 8);
  506. if (mrq->data) {
  507. FLUSH_FIFO(host);
  508. ret = au1xmmc_prepare_data(host, mrq->data);
  509. }
  510. if (ret == MMC_ERR_NONE)
  511. ret = au1xmmc_send_command(host, 0, mrq->cmd);
  512. if (ret != MMC_ERR_NONE) {
  513. mrq->cmd->error = ret;
  514. au1xmmc_finish_request(host);
  515. }
  516. }
  517. static void au1xmmc_reset_controller(struct au1xmmc_host *host)
  518. {
  519. /* Apply the clock */
  520. au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
  521. au_sync_delay(1);
  522. au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
  523. au_sync_delay(5);
  524. au_writel(~0, HOST_STATUS(host));
  525. au_sync();
  526. au_writel(0, HOST_BLKSIZE(host));
  527. au_writel(0x001fffff, HOST_TIMEOUT(host));
  528. au_sync();
  529. au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  530. au_sync();
  531. au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
  532. au_sync_delay(1);
  533. au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  534. au_sync();
  535. /* Configure interrupts */
  536. au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
  537. au_sync();
  538. }
  539. static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
  540. {
  541. struct au1xmmc_host *host = mmc_priv(mmc);
  542. DEBUG("set_ios (power=%u, clock=%uHz, vdd=%u, mode=%u)\n",
  543. host->id, ios->power_mode, ios->clock, ios->vdd,
  544. ios->bus_mode);
  545. if (ios->power_mode == MMC_POWER_OFF)
  546. au1xmmc_set_power(host, 0);
  547. else if (ios->power_mode == MMC_POWER_ON) {
  548. au1xmmc_set_power(host, 1);
  549. }
  550. if (ios->clock && ios->clock != host->clock) {
  551. au1xmmc_set_clock(host, ios->clock);
  552. host->clock = ios->clock;
  553. }
  554. }
  555. static void au1xmmc_dma_callback(int irq, void *dev_id, struct pt_regs *regs)
  556. {
  557. struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
  558. u32 status;
  559. /* Avoid spurious interrupts */
  560. if (!host->mrq)
  561. return;
  562. if (host->flags & HOST_F_STOP)
  563. SEND_STOP(host);
  564. tasklet_schedule(&host->data_task);
  565. }
  566. #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
  567. #define STATUS_DATA_IN (SD_STATUS_NE)
  568. #define STATUS_DATA_OUT (SD_STATUS_TH)
  569. static irqreturn_t au1xmmc_irq(int irq, void *dev_id, struct pt_regs *regs)
  570. {
  571. u32 status;
  572. int i, ret = 0;
  573. disable_irq(AU1100_SD_IRQ);
  574. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  575. struct au1xmmc_host * host = au1xmmc_hosts[i];
  576. u32 handled = 1;
  577. status = au_readl(HOST_STATUS(host));
  578. if (host->mrq && (status & STATUS_TIMEOUT)) {
  579. if (status & SD_STATUS_RAT)
  580. host->mrq->cmd->error = MMC_ERR_TIMEOUT;
  581. else if (status & SD_STATUS_DT)
  582. host->mrq->data->error = MMC_ERR_TIMEOUT;
  583. /* In PIO mode, interrupts might still be enabled */
  584. IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
  585. //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF);
  586. tasklet_schedule(&host->finish_task);
  587. }
  588. #if 0
  589. else if (status & SD_STATUS_DD) {
  590. /* Sometimes we get a DD before a NE in PIO mode */
  591. if (!(host->flags & HOST_F_DMA) &&
  592. (status & SD_STATUS_NE))
  593. au1xmmc_receive_pio(host);
  594. else {
  595. au1xmmc_data_complete(host, status);
  596. //tasklet_schedule(&host->data_task);
  597. }
  598. }
  599. #endif
  600. else if (status & (SD_STATUS_CR)) {
  601. if (host->status == HOST_S_CMD)
  602. au1xmmc_cmd_complete(host,status);
  603. }
  604. else if (!(host->flags & HOST_F_DMA)) {
  605. if ((host->flags & HOST_F_XMIT) &&
  606. (status & STATUS_DATA_OUT))
  607. au1xmmc_send_pio(host);
  608. else if ((host->flags & HOST_F_RECV) &&
  609. (status & STATUS_DATA_IN))
  610. au1xmmc_receive_pio(host);
  611. }
  612. else if (status & 0x203FBC70) {
  613. DEBUG("Unhandled status %8.8x\n", host->id, status);
  614. handled = 0;
  615. }
  616. au_writel(status, HOST_STATUS(host));
  617. au_sync();
  618. ret |= handled;
  619. }
  620. enable_irq(AU1100_SD_IRQ);
  621. return ret;
  622. }
  623. static void au1xmmc_poll_event(unsigned long arg)
  624. {
  625. struct au1xmmc_host *host = (struct au1xmmc_host *) arg;
  626. int card = au1xmmc_card_inserted(host);
  627. int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
  628. if (card != controller) {
  629. host->flags &= ~HOST_F_ACTIVE;
  630. if (card) host->flags |= HOST_F_ACTIVE;
  631. mmc_detect_change(host->mmc, 0);
  632. }
  633. if (host->mrq != NULL) {
  634. u32 status = au_readl(HOST_STATUS(host));
  635. DEBUG("PENDING - %8.8x\n", host->id, status);
  636. }
  637. mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
  638. }
  639. static dbdev_tab_t au1xmmc_mem_dbdev =
  640. {
  641. DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
  642. };
  643. static void au1xmmc_init_dma(struct au1xmmc_host *host)
  644. {
  645. u32 rxchan, txchan;
  646. int txid = au1xmmc_card_table[host->id].tx_devid;
  647. int rxid = au1xmmc_card_table[host->id].rx_devid;
  648. /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
  649. of 8 bits. And since devices are shared, we need to create
  650. our own to avoid freaking out other devices
  651. */
  652. int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
  653. txchan = au1xxx_dbdma_chan_alloc(memid, txid,
  654. au1xmmc_dma_callback, (void *) host);
  655. rxchan = au1xxx_dbdma_chan_alloc(rxid, memid,
  656. au1xmmc_dma_callback, (void *) host);
  657. au1xxx_dbdma_set_devwidth(txchan, 8);
  658. au1xxx_dbdma_set_devwidth(rxchan, 8);
  659. au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT);
  660. au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT);
  661. host->tx_chan = txchan;
  662. host->rx_chan = rxchan;
  663. }
  664. struct mmc_host_ops au1xmmc_ops = {
  665. .request = au1xmmc_request,
  666. .set_ios = au1xmmc_set_ios,
  667. };
  668. static int au1xmmc_probe(struct device *dev)
  669. {
  670. int i, ret = 0;
  671. /* THe interrupt is shared among all controllers */
  672. ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, SA_INTERRUPT, "MMC", 0);
  673. if (ret) {
  674. printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
  675. AU1100_SD_IRQ, ret);
  676. return -ENXIO;
  677. }
  678. disable_irq(AU1100_SD_IRQ);
  679. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  680. struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), dev);
  681. struct au1xmmc_host *host = 0;
  682. if (!mmc) {
  683. printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
  684. au1xmmc_hosts[i] = 0;
  685. continue;
  686. }
  687. mmc->ops = &au1xmmc_ops;
  688. mmc->f_min = 450000;
  689. mmc->f_max = 24000000;
  690. mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
  691. mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
  692. mmc->ocr_avail = AU1XMMC_OCR;
  693. host = mmc_priv(mmc);
  694. host->mmc = mmc;
  695. host->id = i;
  696. host->iobase = au1xmmc_card_table[host->id].iobase;
  697. host->clock = 0;
  698. host->power_mode = MMC_POWER_OFF;
  699. host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
  700. host->status = HOST_S_IDLE;
  701. init_timer(&host->timer);
  702. host->timer.function = au1xmmc_poll_event;
  703. host->timer.data = (unsigned long) host;
  704. host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
  705. tasklet_init(&host->data_task, au1xmmc_tasklet_data,
  706. (unsigned long) host);
  707. tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
  708. (unsigned long) host);
  709. spin_lock_init(&host->lock);
  710. if (dma != 0)
  711. au1xmmc_init_dma(host);
  712. au1xmmc_reset_controller(host);
  713. mmc_add_host(mmc);
  714. au1xmmc_hosts[i] = host;
  715. add_timer(&host->timer);
  716. printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
  717. host->id, host->iobase, dma ? "dma" : "pio");
  718. }
  719. enable_irq(AU1100_SD_IRQ);
  720. return 0;
  721. }
  722. static int au1xmmc_remove(struct device *dev)
  723. {
  724. int i;
  725. disable_irq(AU1100_SD_IRQ);
  726. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  727. struct au1xmmc_host *host = au1xmmc_hosts[i];
  728. if (!host) continue;
  729. tasklet_kill(&host->data_task);
  730. tasklet_kill(&host->finish_task);
  731. del_timer_sync(&host->timer);
  732. au1xmmc_set_power(host, 0);
  733. mmc_remove_host(host->mmc);
  734. au1xxx_dbdma_chan_free(host->tx_chan);
  735. au1xxx_dbdma_chan_free(host->rx_chan);
  736. au_writel(0x0, HOST_ENABLE(host));
  737. au_sync();
  738. }
  739. free_irq(AU1100_SD_IRQ, 0);
  740. return 0;
  741. }
  742. static struct device_driver au1xmmc_driver = {
  743. .name = DRIVER_NAME,
  744. .bus = &platform_bus_type,
  745. .probe = au1xmmc_probe,
  746. .remove = au1xmmc_remove,
  747. .suspend = NULL,
  748. .resume = NULL
  749. };
  750. static int __init au1xmmc_init(void)
  751. {
  752. return driver_register(&au1xmmc_driver);
  753. }
  754. static void __exit au1xmmc_exit(void)
  755. {
  756. driver_unregister(&au1xmmc_driver);
  757. }
  758. module_init(au1xmmc_init);
  759. module_exit(au1xmmc_exit);
  760. #ifdef MODULE
  761. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  762. MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
  763. MODULE_LICENSE("GPL");
  764. #endif