au1xmmc.c 28 KB

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