au1xmmc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * linux/drivers/mmc/host/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 (ARRAY_SIZE(au1xmmc_card_table))
  69. /* This array stores pointers for the hosts (used by the IRQ handler) */
  70. struct au1xmmc_host *au1xmmc_hosts[AU1XMMC_CONTROLLER_COUNT];
  71. static int dma = 1;
  72. #ifdef MODULE
  73. module_param(dma, bool, 0);
  74. MODULE_PARM_DESC(dma, "Use DMA engine for data transfers (0 = disabled)");
  75. #endif
  76. static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
  77. {
  78. u32 val = au_readl(HOST_CONFIG(host));
  79. val |= mask;
  80. au_writel(val, HOST_CONFIG(host));
  81. au_sync();
  82. }
  83. static inline void FLUSH_FIFO(struct au1xmmc_host *host)
  84. {
  85. u32 val = au_readl(HOST_CONFIG2(host));
  86. au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
  87. au_sync_delay(1);
  88. /* SEND_STOP will turn off clock control - this re-enables it */
  89. val &= ~SD_CONFIG2_DF;
  90. au_writel(val, HOST_CONFIG2(host));
  91. au_sync();
  92. }
  93. static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
  94. {
  95. u32 val = au_readl(HOST_CONFIG(host));
  96. val &= ~mask;
  97. au_writel(val, HOST_CONFIG(host));
  98. au_sync();
  99. }
  100. static inline void SEND_STOP(struct au1xmmc_host *host)
  101. {
  102. /* We know the value of CONFIG2, so avoid a read we don't need */
  103. u32 mask = SD_CONFIG2_EN;
  104. WARN_ON(host->status != HOST_S_DATA);
  105. host->status = HOST_S_STOP;
  106. au_writel(mask | SD_CONFIG2_DF, HOST_CONFIG2(host));
  107. au_sync();
  108. /* Send the stop commmand */
  109. au_writel(STOP_CMD, HOST_CMD(host));
  110. }
  111. static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
  112. {
  113. u32 val = au1xmmc_card_table[host->id].bcsrpwr;
  114. bcsr->board &= ~val;
  115. if (state) bcsr->board |= val;
  116. au_sync_delay(1);
  117. }
  118. static inline int au1xmmc_card_inserted(struct au1xmmc_host *host)
  119. {
  120. return (bcsr->sig_status & au1xmmc_card_table[host->id].bcsrstatus)
  121. ? 1 : 0;
  122. }
  123. static int au1xmmc_card_readonly(struct mmc_host *mmc)
  124. {
  125. struct au1xmmc_host *host = mmc_priv(mmc);
  126. return (bcsr->status & au1xmmc_card_table[host->id].wpstatus)
  127. ? 1 : 0;
  128. }
  129. static void au1xmmc_finish_request(struct au1xmmc_host *host)
  130. {
  131. struct mmc_request *mrq = host->mrq;
  132. host->mrq = NULL;
  133. host->flags &= HOST_F_ACTIVE;
  134. host->dma.len = 0;
  135. host->dma.dir = 0;
  136. host->pio.index = 0;
  137. host->pio.offset = 0;
  138. host->pio.len = 0;
  139. host->status = HOST_S_IDLE;
  140. bcsr->disk_leds |= (1 << 8);
  141. mmc_request_done(host->mmc, mrq);
  142. }
  143. static void au1xmmc_tasklet_finish(unsigned long param)
  144. {
  145. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  146. au1xmmc_finish_request(host);
  147. }
  148. static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
  149. struct mmc_command *cmd, struct mmc_data *data)
  150. {
  151. u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
  152. switch (mmc_resp_type(cmd)) {
  153. case MMC_RSP_NONE:
  154. break;
  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. default:
  168. printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
  169. mmc_resp_type(cmd));
  170. return -EINVAL;
  171. }
  172. if (data) {
  173. if (flags & MMC_DATA_READ) {
  174. if (data->blocks > 1)
  175. mmccmd |= SD_CMD_CT_4;
  176. else
  177. mmccmd |= SD_CMD_CT_2;
  178. } else if (flags & MMC_DATA_WRITE) {
  179. if (data->blocks > 1)
  180. mmccmd |= SD_CMD_CT_3;
  181. else
  182. mmccmd |= SD_CMD_CT_1;
  183. }
  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 0;
  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 = 0;
  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 = -EILSEQ;
  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) {
  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 = 0;
  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 = -EILSEQ;
  396. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  397. if (!trans || cmd->error) {
  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 -ETIMEDOUT;
  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 0;
  488. dataerr:
  489. dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
  490. return -ETIMEDOUT;
  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. unsigned int flags = 0;
  499. int ret = 0;
  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. flags = mrq->data->flags;
  508. ret = au1xmmc_prepare_data(host, mrq->data);
  509. }
  510. if (!ret)
  511. ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
  512. if (ret) {
  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. if (ios->power_mode == MMC_POWER_OFF)
  543. au1xmmc_set_power(host, 0);
  544. else if (ios->power_mode == MMC_POWER_ON) {
  545. au1xmmc_set_power(host, 1);
  546. }
  547. if (ios->clock && ios->clock != host->clock) {
  548. au1xmmc_set_clock(host, ios->clock);
  549. host->clock = ios->clock;
  550. }
  551. }
  552. static void au1xmmc_dma_callback(int irq, void *dev_id)
  553. {
  554. struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
  555. /* Avoid spurious interrupts */
  556. if (!host->mrq)
  557. return;
  558. if (host->flags & HOST_F_STOP)
  559. SEND_STOP(host);
  560. tasklet_schedule(&host->data_task);
  561. }
  562. #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
  563. #define STATUS_DATA_IN (SD_STATUS_NE)
  564. #define STATUS_DATA_OUT (SD_STATUS_TH)
  565. static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
  566. {
  567. u32 status;
  568. int i, ret = 0;
  569. disable_irq(AU1100_SD_IRQ);
  570. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  571. struct au1xmmc_host * host = au1xmmc_hosts[i];
  572. u32 handled = 1;
  573. status = au_readl(HOST_STATUS(host));
  574. if (host->mrq && (status & STATUS_TIMEOUT)) {
  575. if (status & SD_STATUS_RAT)
  576. host->mrq->cmd->error = -ETIMEDOUT;
  577. else if (status & SD_STATUS_DT)
  578. host->mrq->data->error = -ETIMEDOUT;
  579. /* In PIO mode, interrupts might still be enabled */
  580. IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
  581. //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF);
  582. tasklet_schedule(&host->finish_task);
  583. }
  584. #if 0
  585. else if (status & SD_STATUS_DD) {
  586. /* Sometimes we get a DD before a NE in PIO mode */
  587. if (!(host->flags & HOST_F_DMA) &&
  588. (status & SD_STATUS_NE))
  589. au1xmmc_receive_pio(host);
  590. else {
  591. au1xmmc_data_complete(host, status);
  592. //tasklet_schedule(&host->data_task);
  593. }
  594. }
  595. #endif
  596. else if (status & (SD_STATUS_CR)) {
  597. if (host->status == HOST_S_CMD)
  598. au1xmmc_cmd_complete(host,status);
  599. }
  600. else if (!(host->flags & HOST_F_DMA)) {
  601. if ((host->flags & HOST_F_XMIT) &&
  602. (status & STATUS_DATA_OUT))
  603. au1xmmc_send_pio(host);
  604. else if ((host->flags & HOST_F_RECV) &&
  605. (status & STATUS_DATA_IN))
  606. au1xmmc_receive_pio(host);
  607. }
  608. else if (status & 0x203FBC70) {
  609. DBG("Unhandled status %8.8x\n", host->id, status);
  610. handled = 0;
  611. }
  612. au_writel(status, HOST_STATUS(host));
  613. au_sync();
  614. ret |= handled;
  615. }
  616. enable_irq(AU1100_SD_IRQ);
  617. return ret;
  618. }
  619. static void au1xmmc_poll_event(unsigned long arg)
  620. {
  621. struct au1xmmc_host *host = (struct au1xmmc_host *) arg;
  622. int card = au1xmmc_card_inserted(host);
  623. int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;
  624. if (card != controller) {
  625. host->flags &= ~HOST_F_ACTIVE;
  626. if (card) host->flags |= HOST_F_ACTIVE;
  627. mmc_detect_change(host->mmc, 0);
  628. }
  629. if (host->mrq != NULL) {
  630. u32 status = au_readl(HOST_STATUS(host));
  631. DBG("PENDING - %8.8x\n", host->id, status);
  632. }
  633. mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
  634. }
  635. static dbdev_tab_t au1xmmc_mem_dbdev =
  636. {
  637. DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
  638. };
  639. static void au1xmmc_init_dma(struct au1xmmc_host *host)
  640. {
  641. u32 rxchan, txchan;
  642. int txid = au1xmmc_card_table[host->id].tx_devid;
  643. int rxid = au1xmmc_card_table[host->id].rx_devid;
  644. /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
  645. of 8 bits. And since devices are shared, we need to create
  646. our own to avoid freaking out other devices
  647. */
  648. int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
  649. txchan = au1xxx_dbdma_chan_alloc(memid, txid,
  650. au1xmmc_dma_callback, (void *) host);
  651. rxchan = au1xxx_dbdma_chan_alloc(rxid, memid,
  652. au1xmmc_dma_callback, (void *) host);
  653. au1xxx_dbdma_set_devwidth(txchan, 8);
  654. au1xxx_dbdma_set_devwidth(rxchan, 8);
  655. au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT);
  656. au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT);
  657. host->tx_chan = txchan;
  658. host->rx_chan = rxchan;
  659. }
  660. static const struct mmc_host_ops au1xmmc_ops = {
  661. .request = au1xmmc_request,
  662. .set_ios = au1xmmc_set_ios,
  663. .get_ro = au1xmmc_card_readonly,
  664. };
  665. static int __devinit au1xmmc_probe(struct platform_device *pdev)
  666. {
  667. int i, ret = 0;
  668. /* THe interrupt is shared among all controllers */
  669. ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, IRQF_DISABLED, "MMC", 0);
  670. if (ret) {
  671. printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
  672. AU1100_SD_IRQ, ret);
  673. return -ENXIO;
  674. }
  675. disable_irq(AU1100_SD_IRQ);
  676. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  677. struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
  678. struct au1xmmc_host *host = 0;
  679. if (!mmc) {
  680. printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
  681. au1xmmc_hosts[i] = 0;
  682. continue;
  683. }
  684. mmc->ops = &au1xmmc_ops;
  685. mmc->f_min = 450000;
  686. mmc->f_max = 24000000;
  687. mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
  688. mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
  689. mmc->max_blk_size = 2048;
  690. mmc->max_blk_count = 512;
  691. mmc->ocr_avail = AU1XMMC_OCR;
  692. host = mmc_priv(mmc);
  693. host->mmc = mmc;
  694. host->id = i;
  695. host->iobase = au1xmmc_card_table[host->id].iobase;
  696. host->clock = 0;
  697. host->power_mode = MMC_POWER_OFF;
  698. host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
  699. host->status = HOST_S_IDLE;
  700. init_timer(&host->timer);
  701. host->timer.function = au1xmmc_poll_event;
  702. host->timer.data = (unsigned long) host;
  703. host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
  704. tasklet_init(&host->data_task, au1xmmc_tasklet_data,
  705. (unsigned long) host);
  706. tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
  707. (unsigned long) host);
  708. spin_lock_init(&host->lock);
  709. if (dma != 0)
  710. au1xmmc_init_dma(host);
  711. au1xmmc_reset_controller(host);
  712. mmc_add_host(mmc);
  713. au1xmmc_hosts[i] = host;
  714. add_timer(&host->timer);
  715. printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
  716. host->id, host->iobase, dma ? "dma" : "pio");
  717. }
  718. enable_irq(AU1100_SD_IRQ);
  719. return 0;
  720. }
  721. static int __devexit au1xmmc_remove(struct platform_device *pdev)
  722. {
  723. int i;
  724. disable_irq(AU1100_SD_IRQ);
  725. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  726. struct au1xmmc_host *host = au1xmmc_hosts[i];
  727. if (!host) continue;
  728. tasklet_kill(&host->data_task);
  729. tasklet_kill(&host->finish_task);
  730. del_timer_sync(&host->timer);
  731. au1xmmc_set_power(host, 0);
  732. mmc_remove_host(host->mmc);
  733. au1xxx_dbdma_chan_free(host->tx_chan);
  734. au1xxx_dbdma_chan_free(host->rx_chan);
  735. au_writel(0x0, HOST_ENABLE(host));
  736. au_sync();
  737. }
  738. free_irq(AU1100_SD_IRQ, 0);
  739. return 0;
  740. }
  741. static struct platform_driver au1xmmc_driver = {
  742. .probe = au1xmmc_probe,
  743. .remove = au1xmmc_remove,
  744. .suspend = NULL,
  745. .resume = NULL,
  746. .driver = {
  747. .name = DRIVER_NAME,
  748. },
  749. };
  750. static int __init au1xmmc_init(void)
  751. {
  752. return platform_driver_register(&au1xmmc_driver);
  753. }
  754. static void __exit au1xmmc_exit(void)
  755. {
  756. platform_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