au1xmmc.c 23 KB

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