au1xmmc.c 23 KB

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