au1xmmc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * linux/drivers/mmc/au1xmmc.c - AU1XX0 MMC driver
  3. *
  4. * Copyright (c) 2005, Advanced Micro Devices, Inc.
  5. *
  6. * Developed with help from the 2.4.30 MMC AU1XXX controller including
  7. * the following copyright notices:
  8. * Copyright (c) 2003-2004 Embedded Edge, LLC.
  9. * Portions Copyright (C) 2002 Embedix, Inc
  10. * Copyright 2002 Hewlett-Packard Company
  11. * 2.6 version of this driver inspired by:
  12. * (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
  13. * All Rights Reserved.
  14. * (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
  15. * All Rights Reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. /* Why is a timer used to detect insert events?
  22. *
  23. * From the AU1100 MMC application guide:
  24. * If the Au1100-based design is intended to support both MultiMediaCards
  25. * and 1- or 4-data bit SecureDigital cards, then the solution is to
  26. * connect a weak (560KOhm) pull-up resistor to connector pin 1.
  27. * In doing so, a MMC card never enters SPI-mode communications,
  28. * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
  29. * (the low to high transition will not occur).
  30. *
  31. * So we use the timer to check the status manually.
  32. */
  33. #include <linux/config.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/mmc/host.h>
  41. #include <linux/mmc/protocol.h>
  42. #include <asm/io.h>
  43. #include <asm/mach-au1x00/au1000.h>
  44. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  45. #include <asm/mach-au1x00/au1100_mmc.h>
  46. #include <asm/scatterlist.h>
  47. #include <au1xxx.h>
  48. #include "au1xmmc.h"
  49. #define DRIVER_NAME "au1xxx-mmc"
  50. /* Set this to enable special debugging macros */
  51. #ifdef DEBUG
  52. #define DBG(fmt, idx, args...) printk("au1xx(%d): DEBUG: " fmt, idx, ##args)
  53. #else
  54. #define DBG(fmt, idx, args...)
  55. #endif
  56. const struct {
  57. u32 iobase;
  58. u32 tx_devid, rx_devid;
  59. u16 bcsrpwr;
  60. u16 bcsrstatus;
  61. u16 wpstatus;
  62. } au1xmmc_card_table[] = {
  63. { SD0_BASE, DSCR_CMD0_SDMS_TX0, DSCR_CMD0_SDMS_RX0,
  64. BCSR_BOARD_SD0PWR, BCSR_INT_SD0INSERT, BCSR_STATUS_SD0WP },
  65. #ifndef CONFIG_MIPS_DB1200
  66. { SD1_BASE, DSCR_CMD0_SDMS_TX1, DSCR_CMD0_SDMS_RX1,
  67. BCSR_BOARD_DS1PWR, BCSR_INT_SD1INSERT, BCSR_STATUS_SD1WP }
  68. #endif
  69. };
  70. #define AU1XMMC_CONTROLLER_COUNT \
  71. (sizeof(au1xmmc_card_table) / sizeof(au1xmmc_card_table[0]))
  72. /* This array stores pointers for the hosts (used by the IRQ handler) */
  73. struct au1xmmc_host *au1xmmc_hosts[AU1XMMC_CONTROLLER_COUNT];
  74. static int dma = 1;
  75. #ifdef MODULE
  76. module_param(dma, bool, 0);
  77. MODULE_PARM_DESC(dma, "Use DMA engine for data transfers (0 = disabled)");
  78. #endif
  79. static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
  80. {
  81. u32 val = au_readl(HOST_CONFIG(host));
  82. val |= mask;
  83. au_writel(val, HOST_CONFIG(host));
  84. au_sync();
  85. }
  86. static inline void FLUSH_FIFO(struct au1xmmc_host *host)
  87. {
  88. u32 val = au_readl(HOST_CONFIG2(host));
  89. au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
  90. au_sync_delay(1);
  91. /* SEND_STOP will turn off clock control - this re-enables it */
  92. val &= ~SD_CONFIG2_DF;
  93. au_writel(val, HOST_CONFIG2(host));
  94. au_sync();
  95. }
  96. static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
  97. {
  98. u32 val = au_readl(HOST_CONFIG(host));
  99. val &= ~mask;
  100. au_writel(val, HOST_CONFIG(host));
  101. au_sync();
  102. }
  103. static inline void SEND_STOP(struct au1xmmc_host *host)
  104. {
  105. /* We know the value of CONFIG2, so avoid a read we don't need */
  106. u32 mask = SD_CONFIG2_EN;
  107. WARN_ON(host->status != HOST_S_DATA);
  108. host->status = HOST_S_STOP;
  109. au_writel(mask | SD_CONFIG2_DF, HOST_CONFIG2(host));
  110. au_sync();
  111. /* Send the stop commmand */
  112. au_writel(STOP_CMD, HOST_CMD(host));
  113. }
  114. static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
  115. {
  116. u32 val = au1xmmc_card_table[host->id].bcsrpwr;
  117. bcsr->board &= ~val;
  118. if (state) bcsr->board |= val;
  119. au_sync_delay(1);
  120. }
  121. static inline int au1xmmc_card_inserted(struct au1xmmc_host *host)
  122. {
  123. return (bcsr->sig_status & au1xmmc_card_table[host->id].bcsrstatus)
  124. ? 1 : 0;
  125. }
  126. static inline int au1xmmc_card_readonly(struct au1xmmc_host *host)
  127. {
  128. return (bcsr->status & au1xmmc_card_table[host->id].wpstatus)
  129. ? 1 : 0;
  130. }
  131. static void au1xmmc_finish_request(struct au1xmmc_host *host)
  132. {
  133. struct mmc_request *mrq = host->mrq;
  134. host->mrq = NULL;
  135. host->flags &= HOST_F_ACTIVE;
  136. host->dma.len = 0;
  137. host->dma.dir = 0;
  138. host->pio.index = 0;
  139. host->pio.offset = 0;
  140. host->pio.len = 0;
  141. host->status = HOST_S_IDLE;
  142. bcsr->disk_leds |= (1 << 8);
  143. mmc_request_done(host->mmc, mrq);
  144. }
  145. static void au1xmmc_tasklet_finish(unsigned long param)
  146. {
  147. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  148. au1xmmc_finish_request(host);
  149. }
  150. static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
  151. struct mmc_command *cmd)
  152. {
  153. u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
  154. switch (mmc_resp_type(cmd)) {
  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. }
  168. switch(cmd->opcode) {
  169. case MMC_READ_SINGLE_BLOCK:
  170. case SD_APP_SEND_SCR:
  171. mmccmd |= SD_CMD_CT_2;
  172. break;
  173. case MMC_READ_MULTIPLE_BLOCK:
  174. mmccmd |= SD_CMD_CT_4;
  175. break;
  176. case MMC_WRITE_BLOCK:
  177. mmccmd |= SD_CMD_CT_1;
  178. break;
  179. case MMC_WRITE_MULTIPLE_BLOCK:
  180. mmccmd |= SD_CMD_CT_3;
  181. break;
  182. case MMC_STOP_TRANSMISSION:
  183. mmccmd |= SD_CMD_CT_7;
  184. break;
  185. }
  186. au_writel(cmd->arg, HOST_CMDARG(host));
  187. au_sync();
  188. if (wait)
  189. IRQ_OFF(host, SD_CONFIG_CR);
  190. au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
  191. au_sync();
  192. /* Wait for the command to go on the line */
  193. while(1) {
  194. if (!(au_readl(HOST_CMD(host)) & SD_CMD_GO))
  195. break;
  196. }
  197. /* Wait for the command to come back */
  198. if (wait) {
  199. u32 status = au_readl(HOST_STATUS(host));
  200. while(!(status & SD_STATUS_CR))
  201. status = au_readl(HOST_STATUS(host));
  202. /* Clear the CR status */
  203. au_writel(SD_STATUS_CR, HOST_STATUS(host));
  204. IRQ_ON(host, SD_CONFIG_CR);
  205. }
  206. return MMC_ERR_NONE;
  207. }
  208. static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
  209. {
  210. struct mmc_request *mrq = host->mrq;
  211. struct mmc_data *data;
  212. u32 crc;
  213. WARN_ON(host->status != HOST_S_DATA && host->status != HOST_S_STOP);
  214. if (host->mrq == NULL)
  215. return;
  216. data = mrq->cmd->data;
  217. if (status == 0)
  218. status = au_readl(HOST_STATUS(host));
  219. /* The transaction is really over when the SD_STATUS_DB bit is clear */
  220. while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
  221. status = au_readl(HOST_STATUS(host));
  222. data->error = MMC_ERR_NONE;
  223. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
  224. /* Process any errors */
  225. crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
  226. if (host->flags & HOST_F_XMIT)
  227. crc |= ((status & 0x07) == 0x02) ? 0 : 1;
  228. if (crc)
  229. data->error = MMC_ERR_BADCRC;
  230. /* Clear the CRC bits */
  231. au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
  232. data->bytes_xfered = 0;
  233. if (data->error == MMC_ERR_NONE) {
  234. if (host->flags & HOST_F_DMA) {
  235. u32 chan = DMA_CHANNEL(host);
  236. chan_tab_t *c = *((chan_tab_t **) chan);
  237. au1x_dma_chan_t *cp = c->chan_ptr;
  238. data->bytes_xfered = cp->ddma_bytecnt;
  239. }
  240. else
  241. data->bytes_xfered =
  242. (data->blocks * data->blksz) -
  243. host->pio.len;
  244. }
  245. au1xmmc_finish_request(host);
  246. }
  247. static void au1xmmc_tasklet_data(unsigned long param)
  248. {
  249. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  250. u32 status = au_readl(HOST_STATUS(host));
  251. au1xmmc_data_complete(host, status);
  252. }
  253. #define AU1XMMC_MAX_TRANSFER 8
  254. static void au1xmmc_send_pio(struct au1xmmc_host *host)
  255. {
  256. struct mmc_data *data = 0;
  257. int sg_len, max, count = 0;
  258. unsigned char *sg_ptr;
  259. u32 status = 0;
  260. struct scatterlist *sg;
  261. data = host->mrq->data;
  262. if (!(host->flags & HOST_F_XMIT))
  263. return;
  264. /* This is the pointer to the data buffer */
  265. sg = &data->sg[host->pio.index];
  266. sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
  267. /* This is the space left inside the buffer */
  268. sg_len = data->sg[host->pio.index].length - host->pio.offset;
  269. /* Check to if we need less then the size of the sg_buffer */
  270. max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
  271. if (max > AU1XMMC_MAX_TRANSFER) max = AU1XMMC_MAX_TRANSFER;
  272. for(count = 0; count < max; count++ ) {
  273. unsigned char val;
  274. status = au_readl(HOST_STATUS(host));
  275. if (!(status & SD_STATUS_TH))
  276. break;
  277. val = *sg_ptr++;
  278. au_writel((unsigned long) val, HOST_TXPORT(host));
  279. au_sync();
  280. }
  281. host->pio.len -= count;
  282. host->pio.offset += count;
  283. if (count == sg_len) {
  284. host->pio.index++;
  285. host->pio.offset = 0;
  286. }
  287. if (host->pio.len == 0) {
  288. IRQ_OFF(host, SD_CONFIG_TH);
  289. if (host->flags & HOST_F_STOP)
  290. SEND_STOP(host);
  291. tasklet_schedule(&host->data_task);
  292. }
  293. }
  294. static void au1xmmc_receive_pio(struct au1xmmc_host *host)
  295. {
  296. struct mmc_data *data = 0;
  297. int sg_len = 0, max = 0, count = 0;
  298. unsigned char *sg_ptr = 0;
  299. u32 status = 0;
  300. struct scatterlist *sg;
  301. data = host->mrq->data;
  302. if (!(host->flags & HOST_F_RECV))
  303. return;
  304. max = host->pio.len;
  305. if (host->pio.index < host->dma.len) {
  306. sg = &data->sg[host->pio.index];
  307. sg_ptr = page_address(sg->page) + sg->offset + host->pio.offset;
  308. /* This is the space left inside the buffer */
  309. sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
  310. /* Check to if we need less then the size of the sg_buffer */
  311. if (sg_len < max) max = sg_len;
  312. }
  313. if (max > AU1XMMC_MAX_TRANSFER)
  314. max = AU1XMMC_MAX_TRANSFER;
  315. for(count = 0; count < max; count++ ) {
  316. u32 val;
  317. status = au_readl(HOST_STATUS(host));
  318. if (!(status & SD_STATUS_NE))
  319. break;
  320. if (status & SD_STATUS_RC) {
  321. DBG("RX CRC Error [%d + %d].\n", host->id,
  322. host->pio.len, count);
  323. break;
  324. }
  325. if (status & SD_STATUS_RO) {
  326. DBG("RX Overrun [%d + %d]\n", host->id,
  327. host->pio.len, count);
  328. break;
  329. }
  330. else if (status & SD_STATUS_RU) {
  331. DBG("RX Underrun [%d + %d]\n", host->id,
  332. host->pio.len, count);
  333. break;
  334. }
  335. val = au_readl(HOST_RXPORT(host));
  336. if (sg_ptr)
  337. *sg_ptr++ = (unsigned char) (val & 0xFF);
  338. }
  339. host->pio.len -= count;
  340. host->pio.offset += count;
  341. if (sg_len && count == sg_len) {
  342. host->pio.index++;
  343. host->pio.offset = 0;
  344. }
  345. if (host->pio.len == 0) {
  346. //IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF);
  347. IRQ_OFF(host, SD_CONFIG_NE);
  348. if (host->flags & HOST_F_STOP)
  349. SEND_STOP(host);
  350. tasklet_schedule(&host->data_task);
  351. }
  352. }
  353. /* static void au1xmmc_cmd_complete
  354. This is called when a command has been completed - grab the response
  355. and check for errors. Then start the data transfer if it is indicated.
  356. */
  357. static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
  358. {
  359. struct mmc_request *mrq = host->mrq;
  360. struct mmc_command *cmd;
  361. int trans;
  362. if (!host->mrq)
  363. return;
  364. cmd = mrq->cmd;
  365. cmd->error = MMC_ERR_NONE;
  366. if (cmd->flags & MMC_RSP_PRESENT) {
  367. if (cmd->flags & MMC_RSP_136) {
  368. u32 r[4];
  369. int i;
  370. r[0] = au_readl(host->iobase + SD_RESP3);
  371. r[1] = au_readl(host->iobase + SD_RESP2);
  372. r[2] = au_readl(host->iobase + SD_RESP1);
  373. r[3] = au_readl(host->iobase + SD_RESP0);
  374. /* The CRC is omitted from the response, so really
  375. * we only got 120 bytes, but the engine expects
  376. * 128 bits, so we have to shift things up
  377. */
  378. for(i = 0; i < 4; i++) {
  379. cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
  380. if (i != 3)
  381. cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
  382. }
  383. } else {
  384. /* Techincally, we should be getting all 48 bits of
  385. * the response (SD_RESP1 + SD_RESP2), but because
  386. * our response omits the CRC, our data ends up
  387. * being shifted 8 bits to the right. In this case,
  388. * that means that the OSR data starts at bit 31,
  389. * so we can just read RESP0 and return that
  390. */
  391. cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
  392. }
  393. }
  394. /* Figure out errors */
  395. if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
  396. cmd->error = MMC_ERR_BADCRC;
  397. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  398. if (!trans || cmd->error != MMC_ERR_NONE) {
  399. IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
  400. tasklet_schedule(&host->finish_task);
  401. return;
  402. }
  403. host->status = HOST_S_DATA;
  404. if (host->flags & HOST_F_DMA) {
  405. u32 channel = DMA_CHANNEL(host);
  406. /* Start the DMA as soon as the buffer gets something in it */
  407. if (host->flags & HOST_F_RECV) {
  408. u32 mask = SD_STATUS_DB | SD_STATUS_NE;
  409. while((status & mask) != mask)
  410. status = au_readl(HOST_STATUS(host));
  411. }
  412. au1xxx_dbdma_start(channel);
  413. }
  414. }
  415. static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
  416. {
  417. unsigned int pbus = get_au1x00_speed();
  418. unsigned int divisor;
  419. u32 config;
  420. /* From databook:
  421. divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
  422. */
  423. pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
  424. pbus /= 2;
  425. divisor = ((pbus / rate) / 2) - 1;
  426. config = au_readl(HOST_CONFIG(host));
  427. config &= ~(SD_CONFIG_DIV);
  428. config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
  429. au_writel(config, HOST_CONFIG(host));
  430. au_sync();
  431. }
  432. static int
  433. au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
  434. {
  435. int datalen = data->blocks * data->blksz;
  436. if (dma != 0)
  437. host->flags |= HOST_F_DMA;
  438. if (data->flags & MMC_DATA_READ)
  439. host->flags |= HOST_F_RECV;
  440. else
  441. host->flags |= HOST_F_XMIT;
  442. if (host->mrq->stop)
  443. host->flags |= HOST_F_STOP;
  444. host->dma.dir = DMA_BIDIRECTIONAL;
  445. host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  446. data->sg_len, host->dma.dir);
  447. if (host->dma.len == 0)
  448. return MMC_ERR_TIMEOUT;
  449. au_writel(data->blksz - 1, HOST_BLKSIZE(host));
  450. if (host->flags & HOST_F_DMA) {
  451. int i;
  452. u32 channel = DMA_CHANNEL(host);
  453. au1xxx_dbdma_stop(channel);
  454. for(i = 0; i < host->dma.len; i++) {
  455. u32 ret = 0, flags = DDMA_FLAGS_NOIE;
  456. struct scatterlist *sg = &data->sg[i];
  457. int sg_len = sg->length;
  458. int len = (datalen > sg_len) ? sg_len : datalen;
  459. if (i == host->dma.len - 1)
  460. flags = DDMA_FLAGS_IE;
  461. if (host->flags & HOST_F_XMIT){
  462. ret = au1xxx_dbdma_put_source_flags(channel,
  463. (void *) (page_address(sg->page) +
  464. sg->offset),
  465. len, flags);
  466. }
  467. else {
  468. ret = au1xxx_dbdma_put_dest_flags(channel,
  469. (void *) (page_address(sg->page) +
  470. sg->offset),
  471. len, flags);
  472. }
  473. if (!ret)
  474. goto dataerr;
  475. datalen -= len;
  476. }
  477. }
  478. else {
  479. host->pio.index = 0;
  480. host->pio.offset = 0;
  481. host->pio.len = datalen;
  482. if (host->flags & HOST_F_XMIT)
  483. IRQ_ON(host, SD_CONFIG_TH);
  484. else
  485. IRQ_ON(host, SD_CONFIG_NE);
  486. //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
  487. }
  488. return MMC_ERR_NONE;
  489. dataerr:
  490. dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
  491. return MMC_ERR_TIMEOUT;
  492. }
  493. /* static void au1xmmc_request
  494. This actually starts a command or data transaction
  495. */
  496. static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
  497. {
  498. struct au1xmmc_host *host = mmc_priv(mmc);
  499. int ret = MMC_ERR_NONE;
  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. ret = au1xmmc_prepare_data(host, mrq->data);
  508. }
  509. if (ret == MMC_ERR_NONE)
  510. ret = au1xmmc_send_command(host, 0, mrq->cmd);
  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, struct pt_regs *regs)
  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, struct pt_regs *regs)
  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. struct mmc_host_ops au1xmmc_ops = {
  660. .request = au1xmmc_request,
  661. .set_ios = au1xmmc_set_ios,
  662. };
  663. static int __devinit au1xmmc_probe(struct platform_device *pdev)
  664. {
  665. int i, ret = 0;
  666. /* THe interrupt is shared among all controllers */
  667. ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, SA_INTERRUPT, "MMC", 0);
  668. if (ret) {
  669. printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n",
  670. AU1100_SD_IRQ, ret);
  671. return -ENXIO;
  672. }
  673. disable_irq(AU1100_SD_IRQ);
  674. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  675. struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
  676. struct au1xmmc_host *host = 0;
  677. if (!mmc) {
  678. printk(DRIVER_NAME "ERROR: no mem for host %d\n", i);
  679. au1xmmc_hosts[i] = 0;
  680. continue;
  681. }
  682. mmc->ops = &au1xmmc_ops;
  683. mmc->f_min = 450000;
  684. mmc->f_max = 24000000;
  685. mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
  686. mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
  687. mmc->ocr_avail = AU1XMMC_OCR;
  688. host = mmc_priv(mmc);
  689. host->mmc = mmc;
  690. host->id = i;
  691. host->iobase = au1xmmc_card_table[host->id].iobase;
  692. host->clock = 0;
  693. host->power_mode = MMC_POWER_OFF;
  694. host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0;
  695. host->status = HOST_S_IDLE;
  696. init_timer(&host->timer);
  697. host->timer.function = au1xmmc_poll_event;
  698. host->timer.data = (unsigned long) host;
  699. host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
  700. tasklet_init(&host->data_task, au1xmmc_tasklet_data,
  701. (unsigned long) host);
  702. tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
  703. (unsigned long) host);
  704. spin_lock_init(&host->lock);
  705. if (dma != 0)
  706. au1xmmc_init_dma(host);
  707. au1xmmc_reset_controller(host);
  708. mmc_add_host(mmc);
  709. au1xmmc_hosts[i] = host;
  710. add_timer(&host->timer);
  711. printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n",
  712. host->id, host->iobase, dma ? "dma" : "pio");
  713. }
  714. enable_irq(AU1100_SD_IRQ);
  715. return 0;
  716. }
  717. static int __devexit au1xmmc_remove(struct platform_device *pdev)
  718. {
  719. int i;
  720. disable_irq(AU1100_SD_IRQ);
  721. for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) {
  722. struct au1xmmc_host *host = au1xmmc_hosts[i];
  723. if (!host) continue;
  724. tasklet_kill(&host->data_task);
  725. tasklet_kill(&host->finish_task);
  726. del_timer_sync(&host->timer);
  727. au1xmmc_set_power(host, 0);
  728. mmc_remove_host(host->mmc);
  729. au1xxx_dbdma_chan_free(host->tx_chan);
  730. au1xxx_dbdma_chan_free(host->rx_chan);
  731. au_writel(0x0, HOST_ENABLE(host));
  732. au_sync();
  733. }
  734. free_irq(AU1100_SD_IRQ, 0);
  735. return 0;
  736. }
  737. static struct platform_driver au1xmmc_driver = {
  738. .probe = au1xmmc_probe,
  739. .remove = au1xmmc_remove,
  740. .suspend = NULL,
  741. .resume = NULL,
  742. .driver = {
  743. .name = DRIVER_NAME,
  744. },
  745. };
  746. static int __init au1xmmc_init(void)
  747. {
  748. return platform_driver_register(&au1xmmc_driver);
  749. }
  750. static void __exit au1xmmc_exit(void)
  751. {
  752. platform_driver_unregister(&au1xmmc_driver);
  753. }
  754. module_init(au1xmmc_init);
  755. module_exit(au1xmmc_exit);
  756. #ifdef MODULE
  757. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  758. MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
  759. MODULE_LICENSE("GPL");
  760. #endif