au1xmmc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 don't we use the SD controllers' carddetect feature?
  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. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/mm.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/scatterlist.h>
  38. #include <linux/leds.h>
  39. #include <linux/mmc/host.h>
  40. #include <linux/slab.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. #define DRIVER_NAME "au1xxx-mmc"
  46. /* Set this to enable special debugging macros */
  47. /* #define DEBUG */
  48. #ifdef DEBUG
  49. #define DBG(fmt, idx, args...) \
  50. pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
  51. #else
  52. #define DBG(fmt, idx, args...) do {} while (0)
  53. #endif
  54. /* Hardware definitions */
  55. #define AU1XMMC_DESCRIPTOR_COUNT 1
  56. /* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
  57. #define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
  58. #define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
  59. #define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
  60. MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
  61. MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
  62. /* This gives us a hard value for the stop command that we can write directly
  63. * to the command register.
  64. */
  65. #define STOP_CMD \
  66. (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
  67. /* This is the set of interrupts that we configure by default. */
  68. #define AU1XMMC_INTERRUPTS \
  69. (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
  70. SD_CONFIG_CR | SD_CONFIG_I)
  71. /* The poll event (looking for insert/remove events runs twice a second. */
  72. #define AU1XMMC_DETECT_TIMEOUT (HZ/2)
  73. struct au1xmmc_host {
  74. struct mmc_host *mmc;
  75. struct mmc_request *mrq;
  76. u32 flags;
  77. u32 iobase;
  78. u32 clock;
  79. u32 bus_width;
  80. u32 power_mode;
  81. int status;
  82. struct {
  83. int len;
  84. int dir;
  85. } dma;
  86. struct {
  87. int index;
  88. int offset;
  89. int len;
  90. } pio;
  91. u32 tx_chan;
  92. u32 rx_chan;
  93. int irq;
  94. struct tasklet_struct finish_task;
  95. struct tasklet_struct data_task;
  96. struct au1xmmc_platform_data *platdata;
  97. struct platform_device *pdev;
  98. struct resource *ioarea;
  99. };
  100. /* Status flags used by the host structure */
  101. #define HOST_F_XMIT 0x0001
  102. #define HOST_F_RECV 0x0002
  103. #define HOST_F_DMA 0x0010
  104. #define HOST_F_DBDMA 0x0020
  105. #define HOST_F_ACTIVE 0x0100
  106. #define HOST_F_STOP 0x1000
  107. #define HOST_S_IDLE 0x0001
  108. #define HOST_S_CMD 0x0002
  109. #define HOST_S_DATA 0x0003
  110. #define HOST_S_STOP 0x0004
  111. /* Easy access macros */
  112. #define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
  113. #define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
  114. #define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
  115. #define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
  116. #define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
  117. #define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
  118. #define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
  119. #define HOST_CMD(h) ((h)->iobase + SD_CMD)
  120. #define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
  121. #define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
  122. #define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
  123. #define DMA_CHANNEL(h) \
  124. (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
  125. static inline int has_dbdma(void)
  126. {
  127. switch (alchemy_get_cputype()) {
  128. case ALCHEMY_CPU_AU1200:
  129. case ALCHEMY_CPU_AU1300:
  130. return 1;
  131. default:
  132. return 0;
  133. }
  134. }
  135. static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
  136. {
  137. u32 val = au_readl(HOST_CONFIG(host));
  138. val |= mask;
  139. au_writel(val, HOST_CONFIG(host));
  140. au_sync();
  141. }
  142. static inline void FLUSH_FIFO(struct au1xmmc_host *host)
  143. {
  144. u32 val = au_readl(HOST_CONFIG2(host));
  145. au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
  146. au_sync_delay(1);
  147. /* SEND_STOP will turn off clock control - this re-enables it */
  148. val &= ~SD_CONFIG2_DF;
  149. au_writel(val, HOST_CONFIG2(host));
  150. au_sync();
  151. }
  152. static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
  153. {
  154. u32 val = au_readl(HOST_CONFIG(host));
  155. val &= ~mask;
  156. au_writel(val, HOST_CONFIG(host));
  157. au_sync();
  158. }
  159. static inline void SEND_STOP(struct au1xmmc_host *host)
  160. {
  161. u32 config2;
  162. WARN_ON(host->status != HOST_S_DATA);
  163. host->status = HOST_S_STOP;
  164. config2 = au_readl(HOST_CONFIG2(host));
  165. au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
  166. au_sync();
  167. /* Send the stop command */
  168. au_writel(STOP_CMD, HOST_CMD(host));
  169. }
  170. static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
  171. {
  172. if (host->platdata && host->platdata->set_power)
  173. host->platdata->set_power(host->mmc, state);
  174. }
  175. static int au1xmmc_card_inserted(struct mmc_host *mmc)
  176. {
  177. struct au1xmmc_host *host = mmc_priv(mmc);
  178. if (host->platdata && host->platdata->card_inserted)
  179. return !!host->platdata->card_inserted(host->mmc);
  180. return -ENOSYS;
  181. }
  182. static int au1xmmc_card_readonly(struct mmc_host *mmc)
  183. {
  184. struct au1xmmc_host *host = mmc_priv(mmc);
  185. if (host->platdata && host->platdata->card_readonly)
  186. return !!host->platdata->card_readonly(mmc);
  187. return -ENOSYS;
  188. }
  189. static void au1xmmc_finish_request(struct au1xmmc_host *host)
  190. {
  191. struct mmc_request *mrq = host->mrq;
  192. host->mrq = NULL;
  193. host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
  194. host->dma.len = 0;
  195. host->dma.dir = 0;
  196. host->pio.index = 0;
  197. host->pio.offset = 0;
  198. host->pio.len = 0;
  199. host->status = HOST_S_IDLE;
  200. mmc_request_done(host->mmc, mrq);
  201. }
  202. static void au1xmmc_tasklet_finish(unsigned long param)
  203. {
  204. struct au1xmmc_host *host = (struct au1xmmc_host *) param;
  205. au1xmmc_finish_request(host);
  206. }
  207. static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
  208. struct mmc_command *cmd, struct mmc_data *data)
  209. {
  210. u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
  211. switch (mmc_resp_type(cmd)) {
  212. case MMC_RSP_NONE:
  213. break;
  214. case MMC_RSP_R1:
  215. mmccmd |= SD_CMD_RT_1;
  216. break;
  217. case MMC_RSP_R1B:
  218. mmccmd |= SD_CMD_RT_1B;
  219. break;
  220. case MMC_RSP_R2:
  221. mmccmd |= SD_CMD_RT_2;
  222. break;
  223. case MMC_RSP_R3:
  224. mmccmd |= SD_CMD_RT_3;
  225. break;
  226. default:
  227. pr_info("au1xmmc: unhandled response type %02x\n",
  228. mmc_resp_type(cmd));
  229. return -EINVAL;
  230. }
  231. if (data) {
  232. if (data->flags & MMC_DATA_READ) {
  233. if (data->blocks > 1)
  234. mmccmd |= SD_CMD_CT_4;
  235. else
  236. mmccmd |= SD_CMD_CT_2;
  237. } else if (data->flags & MMC_DATA_WRITE) {
  238. if (data->blocks > 1)
  239. mmccmd |= SD_CMD_CT_3;
  240. else
  241. mmccmd |= SD_CMD_CT_1;
  242. }
  243. }
  244. au_writel(cmd->arg, HOST_CMDARG(host));
  245. au_sync();
  246. if (wait)
  247. IRQ_OFF(host, SD_CONFIG_CR);
  248. au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
  249. au_sync();
  250. /* Wait for the command to go on the line */
  251. while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
  252. /* nop */;
  253. /* Wait for the command to come back */
  254. if (wait) {
  255. u32 status = au_readl(HOST_STATUS(host));
  256. while (!(status & SD_STATUS_CR))
  257. status = au_readl(HOST_STATUS(host));
  258. /* Clear the CR status */
  259. au_writel(SD_STATUS_CR, HOST_STATUS(host));
  260. IRQ_ON(host, SD_CONFIG_CR);
  261. }
  262. return 0;
  263. }
  264. static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
  265. {
  266. struct mmc_request *mrq = host->mrq;
  267. struct mmc_data *data;
  268. u32 crc;
  269. WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
  270. if (host->mrq == NULL)
  271. return;
  272. data = mrq->cmd->data;
  273. if (status == 0)
  274. status = au_readl(HOST_STATUS(host));
  275. /* The transaction is really over when the SD_STATUS_DB bit is clear */
  276. while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
  277. status = au_readl(HOST_STATUS(host));
  278. data->error = 0;
  279. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
  280. /* Process any errors */
  281. crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
  282. if (host->flags & HOST_F_XMIT)
  283. crc |= ((status & 0x07) == 0x02) ? 0 : 1;
  284. if (crc)
  285. data->error = -EILSEQ;
  286. /* Clear the CRC bits */
  287. au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
  288. data->bytes_xfered = 0;
  289. if (!data->error) {
  290. if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
  291. u32 chan = DMA_CHANNEL(host);
  292. chan_tab_t *c = *((chan_tab_t **)chan);
  293. au1x_dma_chan_t *cp = c->chan_ptr;
  294. data->bytes_xfered = cp->ddma_bytecnt;
  295. } else
  296. data->bytes_xfered =
  297. (data->blocks * data->blksz) - host->pio.len;
  298. }
  299. au1xmmc_finish_request(host);
  300. }
  301. static void au1xmmc_tasklet_data(unsigned long param)
  302. {
  303. struct au1xmmc_host *host = (struct au1xmmc_host *)param;
  304. u32 status = au_readl(HOST_STATUS(host));
  305. au1xmmc_data_complete(host, status);
  306. }
  307. #define AU1XMMC_MAX_TRANSFER 8
  308. static void au1xmmc_send_pio(struct au1xmmc_host *host)
  309. {
  310. struct mmc_data *data;
  311. int sg_len, max, count;
  312. unsigned char *sg_ptr, val;
  313. u32 status;
  314. struct scatterlist *sg;
  315. data = host->mrq->data;
  316. if (!(host->flags & HOST_F_XMIT))
  317. return;
  318. /* This is the pointer to the data buffer */
  319. sg = &data->sg[host->pio.index];
  320. sg_ptr = sg_virt(sg) + host->pio.offset;
  321. /* This is the space left inside the buffer */
  322. sg_len = data->sg[host->pio.index].length - host->pio.offset;
  323. /* Check if we need less than the size of the sg_buffer */
  324. max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
  325. if (max > AU1XMMC_MAX_TRANSFER)
  326. max = AU1XMMC_MAX_TRANSFER;
  327. for (count = 0; count < max; count++) {
  328. status = au_readl(HOST_STATUS(host));
  329. if (!(status & SD_STATUS_TH))
  330. break;
  331. val = *sg_ptr++;
  332. au_writel((unsigned long)val, HOST_TXPORT(host));
  333. au_sync();
  334. }
  335. host->pio.len -= count;
  336. host->pio.offset += count;
  337. if (count == sg_len) {
  338. host->pio.index++;
  339. host->pio.offset = 0;
  340. }
  341. if (host->pio.len == 0) {
  342. IRQ_OFF(host, SD_CONFIG_TH);
  343. if (host->flags & HOST_F_STOP)
  344. SEND_STOP(host);
  345. tasklet_schedule(&host->data_task);
  346. }
  347. }
  348. static void au1xmmc_receive_pio(struct au1xmmc_host *host)
  349. {
  350. struct mmc_data *data;
  351. int max, count, sg_len = 0;
  352. unsigned char *sg_ptr = NULL;
  353. u32 status, val;
  354. struct scatterlist *sg;
  355. data = host->mrq->data;
  356. if (!(host->flags & HOST_F_RECV))
  357. return;
  358. max = host->pio.len;
  359. if (host->pio.index < host->dma.len) {
  360. sg = &data->sg[host->pio.index];
  361. sg_ptr = sg_virt(sg) + host->pio.offset;
  362. /* This is the space left inside the buffer */
  363. sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
  364. /* Check if we need less than the size of the sg_buffer */
  365. if (sg_len < max)
  366. max = sg_len;
  367. }
  368. if (max > AU1XMMC_MAX_TRANSFER)
  369. max = AU1XMMC_MAX_TRANSFER;
  370. for (count = 0; count < max; count++) {
  371. status = au_readl(HOST_STATUS(host));
  372. if (!(status & SD_STATUS_NE))
  373. break;
  374. if (status & SD_STATUS_RC) {
  375. DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
  376. host->pio.len, count);
  377. break;
  378. }
  379. if (status & SD_STATUS_RO) {
  380. DBG("RX Overrun [%d + %d]\n", host->pdev->id,
  381. host->pio.len, count);
  382. break;
  383. }
  384. else if (status & SD_STATUS_RU) {
  385. DBG("RX Underrun [%d + %d]\n", host->pdev->id,
  386. host->pio.len, count);
  387. break;
  388. }
  389. val = au_readl(HOST_RXPORT(host));
  390. if (sg_ptr)
  391. *sg_ptr++ = (unsigned char)(val & 0xFF);
  392. }
  393. host->pio.len -= count;
  394. host->pio.offset += count;
  395. if (sg_len && count == sg_len) {
  396. host->pio.index++;
  397. host->pio.offset = 0;
  398. }
  399. if (host->pio.len == 0) {
  400. /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
  401. IRQ_OFF(host, SD_CONFIG_NE);
  402. if (host->flags & HOST_F_STOP)
  403. SEND_STOP(host);
  404. tasklet_schedule(&host->data_task);
  405. }
  406. }
  407. /* This is called when a command has been completed - grab the response
  408. * and check for errors. Then start the data transfer if it is indicated.
  409. */
  410. static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
  411. {
  412. struct mmc_request *mrq = host->mrq;
  413. struct mmc_command *cmd;
  414. u32 r[4];
  415. int i, trans;
  416. if (!host->mrq)
  417. return;
  418. cmd = mrq->cmd;
  419. cmd->error = 0;
  420. if (cmd->flags & MMC_RSP_PRESENT) {
  421. if (cmd->flags & MMC_RSP_136) {
  422. r[0] = au_readl(host->iobase + SD_RESP3);
  423. r[1] = au_readl(host->iobase + SD_RESP2);
  424. r[2] = au_readl(host->iobase + SD_RESP1);
  425. r[3] = au_readl(host->iobase + SD_RESP0);
  426. /* The CRC is omitted from the response, so really
  427. * we only got 120 bytes, but the engine expects
  428. * 128 bits, so we have to shift things up.
  429. */
  430. for (i = 0; i < 4; i++) {
  431. cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
  432. if (i != 3)
  433. cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
  434. }
  435. } else {
  436. /* Techincally, we should be getting all 48 bits of
  437. * the response (SD_RESP1 + SD_RESP2), but because
  438. * our response omits the CRC, our data ends up
  439. * being shifted 8 bits to the right. In this case,
  440. * that means that the OSR data starts at bit 31,
  441. * so we can just read RESP0 and return that.
  442. */
  443. cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
  444. }
  445. }
  446. /* Figure out errors */
  447. if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
  448. cmd->error = -EILSEQ;
  449. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  450. if (!trans || cmd->error) {
  451. IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
  452. tasklet_schedule(&host->finish_task);
  453. return;
  454. }
  455. host->status = HOST_S_DATA;
  456. if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
  457. u32 channel = DMA_CHANNEL(host);
  458. /* Start the DBDMA as soon as the buffer gets something in it */
  459. if (host->flags & HOST_F_RECV) {
  460. u32 mask = SD_STATUS_DB | SD_STATUS_NE;
  461. while((status & mask) != mask)
  462. status = au_readl(HOST_STATUS(host));
  463. }
  464. au1xxx_dbdma_start(channel);
  465. }
  466. }
  467. static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
  468. {
  469. unsigned int pbus = get_au1x00_speed();
  470. unsigned int divisor;
  471. u32 config;
  472. /* From databook:
  473. * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
  474. */
  475. pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
  476. pbus /= 2;
  477. divisor = ((pbus / rate) / 2) - 1;
  478. config = au_readl(HOST_CONFIG(host));
  479. config &= ~(SD_CONFIG_DIV);
  480. config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
  481. au_writel(config, HOST_CONFIG(host));
  482. au_sync();
  483. }
  484. static int au1xmmc_prepare_data(struct au1xmmc_host *host,
  485. struct mmc_data *data)
  486. {
  487. int datalen = data->blocks * data->blksz;
  488. if (data->flags & MMC_DATA_READ)
  489. host->flags |= HOST_F_RECV;
  490. else
  491. host->flags |= HOST_F_XMIT;
  492. if (host->mrq->stop)
  493. host->flags |= HOST_F_STOP;
  494. host->dma.dir = DMA_BIDIRECTIONAL;
  495. host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  496. data->sg_len, host->dma.dir);
  497. if (host->dma.len == 0)
  498. return -ETIMEDOUT;
  499. au_writel(data->blksz - 1, HOST_BLKSIZE(host));
  500. if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
  501. int i;
  502. u32 channel = DMA_CHANNEL(host);
  503. au1xxx_dbdma_stop(channel);
  504. for (i = 0; i < host->dma.len; i++) {
  505. u32 ret = 0, flags = DDMA_FLAGS_NOIE;
  506. struct scatterlist *sg = &data->sg[i];
  507. int sg_len = sg->length;
  508. int len = (datalen > sg_len) ? sg_len : datalen;
  509. if (i == host->dma.len - 1)
  510. flags = DDMA_FLAGS_IE;
  511. if (host->flags & HOST_F_XMIT) {
  512. ret = au1xxx_dbdma_put_source(channel,
  513. sg_phys(sg), len, flags);
  514. } else {
  515. ret = au1xxx_dbdma_put_dest(channel,
  516. sg_phys(sg), len, flags);
  517. }
  518. if (!ret)
  519. goto dataerr;
  520. datalen -= len;
  521. }
  522. } else {
  523. host->pio.index = 0;
  524. host->pio.offset = 0;
  525. host->pio.len = datalen;
  526. if (host->flags & HOST_F_XMIT)
  527. IRQ_ON(host, SD_CONFIG_TH);
  528. else
  529. IRQ_ON(host, SD_CONFIG_NE);
  530. /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
  531. }
  532. return 0;
  533. dataerr:
  534. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  535. host->dma.dir);
  536. return -ETIMEDOUT;
  537. }
  538. /* This actually starts a command or data transaction */
  539. static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
  540. {
  541. struct au1xmmc_host *host = mmc_priv(mmc);
  542. int ret = 0;
  543. WARN_ON(irqs_disabled());
  544. WARN_ON(host->status != HOST_S_IDLE);
  545. host->mrq = mrq;
  546. host->status = HOST_S_CMD;
  547. /* fail request immediately if no card is present */
  548. if (0 == au1xmmc_card_inserted(mmc)) {
  549. mrq->cmd->error = -ENOMEDIUM;
  550. au1xmmc_finish_request(host);
  551. return;
  552. }
  553. if (mrq->data) {
  554. FLUSH_FIFO(host);
  555. ret = au1xmmc_prepare_data(host, mrq->data);
  556. }
  557. if (!ret)
  558. ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
  559. if (ret) {
  560. mrq->cmd->error = ret;
  561. au1xmmc_finish_request(host);
  562. }
  563. }
  564. static void au1xmmc_reset_controller(struct au1xmmc_host *host)
  565. {
  566. /* Apply the clock */
  567. au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
  568. au_sync_delay(1);
  569. au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
  570. au_sync_delay(5);
  571. au_writel(~0, HOST_STATUS(host));
  572. au_sync();
  573. au_writel(0, HOST_BLKSIZE(host));
  574. au_writel(0x001fffff, HOST_TIMEOUT(host));
  575. au_sync();
  576. au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  577. au_sync();
  578. au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
  579. au_sync_delay(1);
  580. au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  581. au_sync();
  582. /* Configure interrupts */
  583. au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
  584. au_sync();
  585. }
  586. static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  587. {
  588. struct au1xmmc_host *host = mmc_priv(mmc);
  589. u32 config2;
  590. if (ios->power_mode == MMC_POWER_OFF)
  591. au1xmmc_set_power(host, 0);
  592. else if (ios->power_mode == MMC_POWER_ON) {
  593. au1xmmc_set_power(host, 1);
  594. }
  595. if (ios->clock && ios->clock != host->clock) {
  596. au1xmmc_set_clock(host, ios->clock);
  597. host->clock = ios->clock;
  598. }
  599. config2 = au_readl(HOST_CONFIG2(host));
  600. switch (ios->bus_width) {
  601. case MMC_BUS_WIDTH_8:
  602. config2 |= SD_CONFIG2_BB;
  603. break;
  604. case MMC_BUS_WIDTH_4:
  605. config2 &= ~SD_CONFIG2_BB;
  606. config2 |= SD_CONFIG2_WB;
  607. break;
  608. case MMC_BUS_WIDTH_1:
  609. config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
  610. break;
  611. }
  612. au_writel(config2, HOST_CONFIG2(host));
  613. au_sync();
  614. }
  615. #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
  616. #define STATUS_DATA_IN (SD_STATUS_NE)
  617. #define STATUS_DATA_OUT (SD_STATUS_TH)
  618. static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
  619. {
  620. struct au1xmmc_host *host = dev_id;
  621. u32 status;
  622. status = au_readl(HOST_STATUS(host));
  623. if (!(status & SD_STATUS_I))
  624. return IRQ_NONE; /* not ours */
  625. if (status & SD_STATUS_SI) /* SDIO */
  626. mmc_signal_sdio_irq(host->mmc);
  627. if (host->mrq && (status & STATUS_TIMEOUT)) {
  628. if (status & SD_STATUS_RAT)
  629. host->mrq->cmd->error = -ETIMEDOUT;
  630. else if (status & SD_STATUS_DT)
  631. host->mrq->data->error = -ETIMEDOUT;
  632. /* In PIO mode, interrupts might still be enabled */
  633. IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
  634. /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
  635. tasklet_schedule(&host->finish_task);
  636. }
  637. #if 0
  638. else if (status & SD_STATUS_DD) {
  639. /* Sometimes we get a DD before a NE in PIO mode */
  640. if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
  641. au1xmmc_receive_pio(host);
  642. else {
  643. au1xmmc_data_complete(host, status);
  644. /* tasklet_schedule(&host->data_task); */
  645. }
  646. }
  647. #endif
  648. else if (status & SD_STATUS_CR) {
  649. if (host->status == HOST_S_CMD)
  650. au1xmmc_cmd_complete(host, status);
  651. } else if (!(host->flags & HOST_F_DMA)) {
  652. if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
  653. au1xmmc_send_pio(host);
  654. else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
  655. au1xmmc_receive_pio(host);
  656. } else if (status & 0x203F3C70) {
  657. DBG("Unhandled status %8.8x\n", host->pdev->id,
  658. status);
  659. }
  660. au_writel(status, HOST_STATUS(host));
  661. au_sync();
  662. return IRQ_HANDLED;
  663. }
  664. /* 8bit memory DMA device */
  665. static dbdev_tab_t au1xmmc_mem_dbdev = {
  666. .dev_id = DSCR_CMD0_ALWAYS,
  667. .dev_flags = DEV_FLAGS_ANYUSE,
  668. .dev_tsize = 0,
  669. .dev_devwidth = 8,
  670. .dev_physaddr = 0x00000000,
  671. .dev_intlevel = 0,
  672. .dev_intpolarity = 0,
  673. };
  674. static int memid;
  675. static void au1xmmc_dbdma_callback(int irq, void *dev_id)
  676. {
  677. struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
  678. /* Avoid spurious interrupts */
  679. if (!host->mrq)
  680. return;
  681. if (host->flags & HOST_F_STOP)
  682. SEND_STOP(host);
  683. tasklet_schedule(&host->data_task);
  684. }
  685. static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
  686. {
  687. struct resource *res;
  688. int txid, rxid;
  689. res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
  690. if (!res)
  691. return -ENODEV;
  692. txid = res->start;
  693. res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
  694. if (!res)
  695. return -ENODEV;
  696. rxid = res->start;
  697. if (!memid)
  698. return -ENODEV;
  699. host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
  700. au1xmmc_dbdma_callback, (void *)host);
  701. if (!host->tx_chan) {
  702. dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
  703. return -ENODEV;
  704. }
  705. host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
  706. au1xmmc_dbdma_callback, (void *)host);
  707. if (!host->rx_chan) {
  708. dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
  709. au1xxx_dbdma_chan_free(host->tx_chan);
  710. return -ENODEV;
  711. }
  712. au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
  713. au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
  714. au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
  715. au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
  716. /* DBDMA is good to go */
  717. host->flags |= HOST_F_DMA | HOST_F_DBDMA;
  718. return 0;
  719. }
  720. static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
  721. {
  722. if (host->flags & HOST_F_DMA) {
  723. host->flags &= ~HOST_F_DMA;
  724. au1xxx_dbdma_chan_free(host->tx_chan);
  725. au1xxx_dbdma_chan_free(host->rx_chan);
  726. }
  727. }
  728. static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
  729. {
  730. struct au1xmmc_host *host = mmc_priv(mmc);
  731. if (en)
  732. IRQ_ON(host, SD_CONFIG_SI);
  733. else
  734. IRQ_OFF(host, SD_CONFIG_SI);
  735. }
  736. static const struct mmc_host_ops au1xmmc_ops = {
  737. .request = au1xmmc_request,
  738. .set_ios = au1xmmc_set_ios,
  739. .get_ro = au1xmmc_card_readonly,
  740. .get_cd = au1xmmc_card_inserted,
  741. .enable_sdio_irq = au1xmmc_enable_sdio_irq,
  742. };
  743. static int __devinit au1xmmc_probe(struct platform_device *pdev)
  744. {
  745. struct mmc_host *mmc;
  746. struct au1xmmc_host *host;
  747. struct resource *r;
  748. int ret, iflag;
  749. mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
  750. if (!mmc) {
  751. dev_err(&pdev->dev, "no memory for mmc_host\n");
  752. ret = -ENOMEM;
  753. goto out0;
  754. }
  755. host = mmc_priv(mmc);
  756. host->mmc = mmc;
  757. host->platdata = pdev->dev.platform_data;
  758. host->pdev = pdev;
  759. ret = -ENODEV;
  760. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  761. if (!r) {
  762. dev_err(&pdev->dev, "no mmio defined\n");
  763. goto out1;
  764. }
  765. host->ioarea = request_mem_region(r->start, resource_size(r),
  766. pdev->name);
  767. if (!host->ioarea) {
  768. dev_err(&pdev->dev, "mmio already in use\n");
  769. goto out1;
  770. }
  771. host->iobase = (unsigned long)ioremap(r->start, 0x3c);
  772. if (!host->iobase) {
  773. dev_err(&pdev->dev, "cannot remap mmio\n");
  774. goto out2;
  775. }
  776. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  777. if (!r) {
  778. dev_err(&pdev->dev, "no IRQ defined\n");
  779. goto out3;
  780. }
  781. host->irq = r->start;
  782. mmc->ops = &au1xmmc_ops;
  783. mmc->f_min = 450000;
  784. mmc->f_max = 24000000;
  785. mmc->max_blk_size = 2048;
  786. mmc->max_blk_count = 512;
  787. mmc->ocr_avail = AU1XMMC_OCR;
  788. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  789. mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
  790. iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
  791. switch (alchemy_get_cputype()) {
  792. case ALCHEMY_CPU_AU1100:
  793. mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
  794. break;
  795. case ALCHEMY_CPU_AU1200:
  796. mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
  797. break;
  798. case ALCHEMY_CPU_AU1300:
  799. iflag = 0; /* nothing is shared */
  800. mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
  801. mmc->f_max = 52000000;
  802. if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
  803. mmc->caps |= MMC_CAP_8_BIT_DATA;
  804. break;
  805. }
  806. ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
  807. if (ret) {
  808. dev_err(&pdev->dev, "cannot grab IRQ\n");
  809. goto out3;
  810. }
  811. host->status = HOST_S_IDLE;
  812. /* board-specific carddetect setup, if any */
  813. if (host->platdata && host->platdata->cd_setup) {
  814. ret = host->platdata->cd_setup(mmc, 1);
  815. if (ret) {
  816. dev_warn(&pdev->dev, "board CD setup failed\n");
  817. mmc->caps |= MMC_CAP_NEEDS_POLL;
  818. }
  819. } else
  820. mmc->caps |= MMC_CAP_NEEDS_POLL;
  821. /* platform may not be able to use all advertised caps */
  822. if (host->platdata)
  823. mmc->caps &= ~(host->platdata->mask_host_caps);
  824. tasklet_init(&host->data_task, au1xmmc_tasklet_data,
  825. (unsigned long)host);
  826. tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
  827. (unsigned long)host);
  828. if (has_dbdma()) {
  829. ret = au1xmmc_dbdma_init(host);
  830. if (ret)
  831. pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
  832. }
  833. #ifdef CONFIG_LEDS_CLASS
  834. if (host->platdata && host->platdata->led) {
  835. struct led_classdev *led = host->platdata->led;
  836. led->name = mmc_hostname(mmc);
  837. led->brightness = LED_OFF;
  838. led->default_trigger = mmc_hostname(mmc);
  839. ret = led_classdev_register(mmc_dev(mmc), led);
  840. if (ret)
  841. goto out5;
  842. }
  843. #endif
  844. au1xmmc_reset_controller(host);
  845. ret = mmc_add_host(mmc);
  846. if (ret) {
  847. dev_err(&pdev->dev, "cannot add mmc host\n");
  848. goto out6;
  849. }
  850. platform_set_drvdata(pdev, host);
  851. pr_info(DRIVER_NAME ": MMC Controller %d set up at %8.8X"
  852. " (mode=%s)\n", pdev->id, host->iobase,
  853. host->flags & HOST_F_DMA ? "dma" : "pio");
  854. return 0; /* all ok */
  855. out6:
  856. #ifdef CONFIG_LEDS_CLASS
  857. if (host->platdata && host->platdata->led)
  858. led_classdev_unregister(host->platdata->led);
  859. out5:
  860. #endif
  861. au_writel(0, HOST_ENABLE(host));
  862. au_writel(0, HOST_CONFIG(host));
  863. au_writel(0, HOST_CONFIG2(host));
  864. au_sync();
  865. if (host->flags & HOST_F_DBDMA)
  866. au1xmmc_dbdma_shutdown(host);
  867. tasklet_kill(&host->data_task);
  868. tasklet_kill(&host->finish_task);
  869. if (host->platdata && host->platdata->cd_setup &&
  870. !(mmc->caps & MMC_CAP_NEEDS_POLL))
  871. host->platdata->cd_setup(mmc, 0);
  872. free_irq(host->irq, host);
  873. out3:
  874. iounmap((void *)host->iobase);
  875. out2:
  876. release_resource(host->ioarea);
  877. kfree(host->ioarea);
  878. out1:
  879. mmc_free_host(mmc);
  880. out0:
  881. return ret;
  882. }
  883. static int __devexit au1xmmc_remove(struct platform_device *pdev)
  884. {
  885. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  886. if (host) {
  887. mmc_remove_host(host->mmc);
  888. #ifdef CONFIG_LEDS_CLASS
  889. if (host->platdata && host->platdata->led)
  890. led_classdev_unregister(host->platdata->led);
  891. #endif
  892. if (host->platdata && host->platdata->cd_setup &&
  893. !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
  894. host->platdata->cd_setup(host->mmc, 0);
  895. au_writel(0, HOST_ENABLE(host));
  896. au_writel(0, HOST_CONFIG(host));
  897. au_writel(0, HOST_CONFIG2(host));
  898. au_sync();
  899. tasklet_kill(&host->data_task);
  900. tasklet_kill(&host->finish_task);
  901. if (host->flags & HOST_F_DBDMA)
  902. au1xmmc_dbdma_shutdown(host);
  903. au1xmmc_set_power(host, 0);
  904. free_irq(host->irq, host);
  905. iounmap((void *)host->iobase);
  906. release_resource(host->ioarea);
  907. kfree(host->ioarea);
  908. mmc_free_host(host->mmc);
  909. platform_set_drvdata(pdev, NULL);
  910. }
  911. return 0;
  912. }
  913. #ifdef CONFIG_PM
  914. static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
  915. {
  916. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  917. int ret;
  918. ret = mmc_suspend_host(host->mmc);
  919. if (ret)
  920. return ret;
  921. au_writel(0, HOST_CONFIG2(host));
  922. au_writel(0, HOST_CONFIG(host));
  923. au_writel(0xffffffff, HOST_STATUS(host));
  924. au_writel(0, HOST_ENABLE(host));
  925. au_sync();
  926. return 0;
  927. }
  928. static int au1xmmc_resume(struct platform_device *pdev)
  929. {
  930. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  931. au1xmmc_reset_controller(host);
  932. return mmc_resume_host(host->mmc);
  933. }
  934. #else
  935. #define au1xmmc_suspend NULL
  936. #define au1xmmc_resume NULL
  937. #endif
  938. static struct platform_driver au1xmmc_driver = {
  939. .probe = au1xmmc_probe,
  940. .remove = au1xmmc_remove,
  941. .suspend = au1xmmc_suspend,
  942. .resume = au1xmmc_resume,
  943. .driver = {
  944. .name = DRIVER_NAME,
  945. .owner = THIS_MODULE,
  946. },
  947. };
  948. static int __init au1xmmc_init(void)
  949. {
  950. if (has_dbdma()) {
  951. /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
  952. * of 8 bits. And since devices are shared, we need to create
  953. * our own to avoid freaking out other devices.
  954. */
  955. memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
  956. if (!memid)
  957. pr_err("au1xmmc: cannot add memory dbdma\n");
  958. }
  959. return platform_driver_register(&au1xmmc_driver);
  960. }
  961. static void __exit au1xmmc_exit(void)
  962. {
  963. if (has_dbdma() && memid)
  964. au1xxx_ddma_del_device(memid);
  965. platform_driver_unregister(&au1xmmc_driver);
  966. }
  967. module_init(au1xmmc_init);
  968. module_exit(au1xmmc_exit);
  969. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  970. MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
  971. MODULE_LICENSE("GPL");
  972. MODULE_ALIAS("platform:au1xxx-mmc");