au1xmmc.c 29 KB

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