au1xmmc.c 23 KB

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