s3cmci.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. /*
  2. * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
  3. *
  4. * Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
  5. *
  6. * Current driver maintained by Ben Dooks and Simtec Electronics
  7. * Copyright (C) 2008 Simtec Electronics <ben-linux@fluff.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/clk.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/gpio.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <mach/dma.h>
  25. #include <mach/regs-sdi.h>
  26. #include <mach/regs-gpio.h>
  27. #include <plat/mci.h>
  28. #include "s3cmci.h"
  29. #define DRIVER_NAME "s3c-mci"
  30. enum dbg_channels {
  31. dbg_err = (1 << 0),
  32. dbg_debug = (1 << 1),
  33. dbg_info = (1 << 2),
  34. dbg_irq = (1 << 3),
  35. dbg_sg = (1 << 4),
  36. dbg_dma = (1 << 5),
  37. dbg_pio = (1 << 6),
  38. dbg_fail = (1 << 7),
  39. dbg_conf = (1 << 8),
  40. };
  41. static const int dbgmap_err = dbg_fail;
  42. static const int dbgmap_info = dbg_info | dbg_conf;
  43. static const int dbgmap_debug = dbg_err | dbg_debug;
  44. #define dbg(host, channels, args...) \
  45. do { \
  46. if (dbgmap_err & channels) \
  47. dev_err(&host->pdev->dev, args); \
  48. else if (dbgmap_info & channels) \
  49. dev_info(&host->pdev->dev, args); \
  50. else if (dbgmap_debug & channels) \
  51. dev_dbg(&host->pdev->dev, args); \
  52. } while (0)
  53. static struct s3c2410_dma_client s3cmci_dma_client = {
  54. .name = "s3c-mci",
  55. };
  56. static void finalize_request(struct s3cmci_host *host);
  57. static void s3cmci_send_request(struct mmc_host *mmc);
  58. static void s3cmci_reset(struct s3cmci_host *host);
  59. #ifdef CONFIG_MMC_DEBUG
  60. static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
  61. {
  62. u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
  63. u32 datcon, datcnt, datsta, fsta, imask;
  64. con = readl(host->base + S3C2410_SDICON);
  65. pre = readl(host->base + S3C2410_SDIPRE);
  66. cmdarg = readl(host->base + S3C2410_SDICMDARG);
  67. cmdcon = readl(host->base + S3C2410_SDICMDCON);
  68. cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
  69. r0 = readl(host->base + S3C2410_SDIRSP0);
  70. r1 = readl(host->base + S3C2410_SDIRSP1);
  71. r2 = readl(host->base + S3C2410_SDIRSP2);
  72. r3 = readl(host->base + S3C2410_SDIRSP3);
  73. timer = readl(host->base + S3C2410_SDITIMER);
  74. bsize = readl(host->base + S3C2410_SDIBSIZE);
  75. datcon = readl(host->base + S3C2410_SDIDCON);
  76. datcnt = readl(host->base + S3C2410_SDIDCNT);
  77. datsta = readl(host->base + S3C2410_SDIDSTA);
  78. fsta = readl(host->base + S3C2410_SDIFSTA);
  79. imask = readl(host->base + host->sdiimsk);
  80. dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
  81. prefix, con, pre, timer);
  82. dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
  83. prefix, cmdcon, cmdarg, cmdsta);
  84. dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
  85. " DSTA:[%08x] DCNT:[%08x]\n",
  86. prefix, datcon, fsta, datsta, datcnt);
  87. dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
  88. " R2:[%08x] R3:[%08x]\n",
  89. prefix, r0, r1, r2, r3);
  90. }
  91. static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
  92. int stop)
  93. {
  94. snprintf(host->dbgmsg_cmd, 300,
  95. "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
  96. host->ccnt, (stop ? " (STOP)" : ""),
  97. cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
  98. if (cmd->data) {
  99. snprintf(host->dbgmsg_dat, 300,
  100. "#%u bsize:%u blocks:%u bytes:%u",
  101. host->dcnt, cmd->data->blksz,
  102. cmd->data->blocks,
  103. cmd->data->blocks * cmd->data->blksz);
  104. } else {
  105. host->dbgmsg_dat[0] = '\0';
  106. }
  107. }
  108. static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
  109. int fail)
  110. {
  111. unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
  112. if (!cmd)
  113. return;
  114. if (cmd->error == 0) {
  115. dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
  116. host->dbgmsg_cmd, cmd->resp[0]);
  117. } else {
  118. dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
  119. cmd->error, host->dbgmsg_cmd, host->status);
  120. }
  121. if (!cmd->data)
  122. return;
  123. if (cmd->data->error == 0) {
  124. dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
  125. } else {
  126. dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
  127. cmd->data->error, host->dbgmsg_dat,
  128. readl(host->base + S3C2410_SDIDCNT));
  129. }
  130. }
  131. #else
  132. static void dbg_dumpcmd(struct s3cmci_host *host,
  133. struct mmc_command *cmd, int fail) { }
  134. static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
  135. int stop) { }
  136. static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
  137. #endif /* CONFIG_MMC_DEBUG */
  138. static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
  139. {
  140. u32 newmask;
  141. newmask = readl(host->base + host->sdiimsk);
  142. newmask |= imask;
  143. writel(newmask, host->base + host->sdiimsk);
  144. return newmask;
  145. }
  146. static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
  147. {
  148. u32 newmask;
  149. newmask = readl(host->base + host->sdiimsk);
  150. newmask &= ~imask;
  151. writel(newmask, host->base + host->sdiimsk);
  152. return newmask;
  153. }
  154. static inline void clear_imask(struct s3cmci_host *host)
  155. {
  156. u32 mask = readl(host->base + host->sdiimsk);
  157. /* preserve the SDIO IRQ mask state */
  158. mask &= S3C2410_SDIIMSK_SDIOIRQ;
  159. writel(mask, host->base + host->sdiimsk);
  160. }
  161. /**
  162. * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
  163. * @host: The host to check.
  164. *
  165. * Test to see if the SDIO interrupt is being signalled in case the
  166. * controller has failed to re-detect a card interrupt. Read GPE8 and
  167. * see if it is low and if so, signal a SDIO interrupt.
  168. *
  169. * This is currently called if a request is finished (we assume that the
  170. * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
  171. * already being indicated.
  172. */
  173. static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
  174. {
  175. if (host->sdio_irqen) {
  176. if (gpio_get_value(S3C2410_GPE(8)) == 0) {
  177. printk(KERN_DEBUG "%s: signalling irq\n", __func__);
  178. mmc_signal_sdio_irq(host->mmc);
  179. }
  180. }
  181. }
  182. static inline int get_data_buffer(struct s3cmci_host *host,
  183. u32 *bytes, u32 **pointer)
  184. {
  185. struct scatterlist *sg;
  186. if (host->pio_active == XFER_NONE)
  187. return -EINVAL;
  188. if ((!host->mrq) || (!host->mrq->data))
  189. return -EINVAL;
  190. if (host->pio_sgptr >= host->mrq->data->sg_len) {
  191. dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
  192. host->pio_sgptr, host->mrq->data->sg_len);
  193. return -EBUSY;
  194. }
  195. sg = &host->mrq->data->sg[host->pio_sgptr];
  196. *bytes = sg->length;
  197. *pointer = sg_virt(sg);
  198. host->pio_sgptr++;
  199. dbg(host, dbg_sg, "new buffer (%i/%i)\n",
  200. host->pio_sgptr, host->mrq->data->sg_len);
  201. return 0;
  202. }
  203. static inline u32 fifo_count(struct s3cmci_host *host)
  204. {
  205. u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
  206. fifostat &= S3C2410_SDIFSTA_COUNTMASK;
  207. return fifostat;
  208. }
  209. static inline u32 fifo_free(struct s3cmci_host *host)
  210. {
  211. u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
  212. fifostat &= S3C2410_SDIFSTA_COUNTMASK;
  213. return 63 - fifostat;
  214. }
  215. /**
  216. * s3cmci_enable_irq - enable IRQ, after having disabled it.
  217. * @host: The device state.
  218. * @more: True if more IRQs are expected from transfer.
  219. *
  220. * Enable the main IRQ if needed after it has been disabled.
  221. *
  222. * The IRQ can be one of the following states:
  223. * - disabled during IDLE
  224. * - disabled whilst processing data
  225. * - enabled during transfer
  226. * - enabled whilst awaiting SDIO interrupt detection
  227. */
  228. static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
  229. {
  230. unsigned long flags;
  231. bool enable = false;
  232. local_irq_save(flags);
  233. host->irq_enabled = more;
  234. host->irq_disabled = false;
  235. enable = more | host->sdio_irqen;
  236. if (host->irq_state != enable) {
  237. host->irq_state = enable;
  238. if (enable)
  239. enable_irq(host->irq);
  240. else
  241. disable_irq(host->irq);
  242. }
  243. local_irq_restore(flags);
  244. }
  245. /**
  246. *
  247. */
  248. static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
  249. {
  250. unsigned long flags;
  251. local_irq_save(flags);
  252. //printk(KERN_DEBUG "%s: transfer %d\n", __func__, transfer);
  253. host->irq_disabled = transfer;
  254. if (transfer && host->irq_state) {
  255. host->irq_state = false;
  256. disable_irq(host->irq);
  257. }
  258. local_irq_restore(flags);
  259. }
  260. static void do_pio_read(struct s3cmci_host *host)
  261. {
  262. int res;
  263. u32 fifo;
  264. u32 *ptr;
  265. u32 fifo_words;
  266. void __iomem *from_ptr;
  267. /* write real prescaler to host, it might be set slow to fix */
  268. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  269. from_ptr = host->base + host->sdidata;
  270. while ((fifo = fifo_count(host))) {
  271. if (!host->pio_bytes) {
  272. res = get_data_buffer(host, &host->pio_bytes,
  273. &host->pio_ptr);
  274. if (res) {
  275. host->pio_active = XFER_NONE;
  276. host->complete_what = COMPLETION_FINALIZE;
  277. dbg(host, dbg_pio, "pio_read(): "
  278. "complete (no more data).\n");
  279. return;
  280. }
  281. dbg(host, dbg_pio,
  282. "pio_read(): new target: [%i]@[%p]\n",
  283. host->pio_bytes, host->pio_ptr);
  284. }
  285. dbg(host, dbg_pio,
  286. "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
  287. fifo, host->pio_bytes,
  288. readl(host->base + S3C2410_SDIDCNT));
  289. /* If we have reached the end of the block, we can
  290. * read a word and get 1 to 3 bytes. If we in the
  291. * middle of the block, we have to read full words,
  292. * otherwise we will write garbage, so round down to
  293. * an even multiple of 4. */
  294. if (fifo >= host->pio_bytes)
  295. fifo = host->pio_bytes;
  296. else
  297. fifo -= fifo & 3;
  298. host->pio_bytes -= fifo;
  299. host->pio_count += fifo;
  300. fifo_words = fifo >> 2;
  301. ptr = host->pio_ptr;
  302. while (fifo_words--)
  303. *ptr++ = readl(from_ptr);
  304. host->pio_ptr = ptr;
  305. if (fifo & 3) {
  306. u32 n = fifo & 3;
  307. u32 data = readl(from_ptr);
  308. u8 *p = (u8 *)host->pio_ptr;
  309. while (n--) {
  310. *p++ = data;
  311. data >>= 8;
  312. }
  313. }
  314. }
  315. if (!host->pio_bytes) {
  316. res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
  317. if (res) {
  318. dbg(host, dbg_pio,
  319. "pio_read(): complete (no more buffers).\n");
  320. host->pio_active = XFER_NONE;
  321. host->complete_what = COMPLETION_FINALIZE;
  322. return;
  323. }
  324. }
  325. enable_imask(host,
  326. S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
  327. }
  328. static void do_pio_write(struct s3cmci_host *host)
  329. {
  330. void __iomem *to_ptr;
  331. int res;
  332. u32 fifo;
  333. u32 *ptr;
  334. to_ptr = host->base + host->sdidata;
  335. while ((fifo = fifo_free(host)) > 3) {
  336. if (!host->pio_bytes) {
  337. res = get_data_buffer(host, &host->pio_bytes,
  338. &host->pio_ptr);
  339. if (res) {
  340. dbg(host, dbg_pio,
  341. "pio_write(): complete (no more data).\n");
  342. host->pio_active = XFER_NONE;
  343. return;
  344. }
  345. dbg(host, dbg_pio,
  346. "pio_write(): new source: [%i]@[%p]\n",
  347. host->pio_bytes, host->pio_ptr);
  348. }
  349. /* If we have reached the end of the block, we have to
  350. * write exactly the remaining number of bytes. If we
  351. * in the middle of the block, we have to write full
  352. * words, so round down to an even multiple of 4. */
  353. if (fifo >= host->pio_bytes)
  354. fifo = host->pio_bytes;
  355. else
  356. fifo -= fifo & 3;
  357. host->pio_bytes -= fifo;
  358. host->pio_count += fifo;
  359. fifo = (fifo + 3) >> 2;
  360. ptr = host->pio_ptr;
  361. while (fifo--)
  362. writel(*ptr++, to_ptr);
  363. host->pio_ptr = ptr;
  364. }
  365. enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  366. }
  367. static void pio_tasklet(unsigned long data)
  368. {
  369. struct s3cmci_host *host = (struct s3cmci_host *) data;
  370. s3cmci_disable_irq(host, true);
  371. if (host->pio_active == XFER_WRITE)
  372. do_pio_write(host);
  373. if (host->pio_active == XFER_READ)
  374. do_pio_read(host);
  375. if (host->complete_what == COMPLETION_FINALIZE) {
  376. clear_imask(host);
  377. if (host->pio_active != XFER_NONE) {
  378. dbg(host, dbg_err, "unfinished %s "
  379. "- pio_count:[%u] pio_bytes:[%u]\n",
  380. (host->pio_active == XFER_READ) ? "read" : "write",
  381. host->pio_count, host->pio_bytes);
  382. if (host->mrq->data)
  383. host->mrq->data->error = -EINVAL;
  384. }
  385. s3cmci_enable_irq(host, false);
  386. finalize_request(host);
  387. } else
  388. s3cmci_enable_irq(host, true);
  389. }
  390. /*
  391. * ISR for SDI Interface IRQ
  392. * Communication between driver and ISR works as follows:
  393. * host->mrq points to current request
  394. * host->complete_what Indicates when the request is considered done
  395. * COMPLETION_CMDSENT when the command was sent
  396. * COMPLETION_RSPFIN when a response was received
  397. * COMPLETION_XFERFINISH when the data transfer is finished
  398. * COMPLETION_XFERFINISH_RSPFIN both of the above.
  399. * host->complete_request is the completion-object the driver waits for
  400. *
  401. * 1) Driver sets up host->mrq and host->complete_what
  402. * 2) Driver prepares the transfer
  403. * 3) Driver enables interrupts
  404. * 4) Driver starts transfer
  405. * 5) Driver waits for host->complete_rquest
  406. * 6) ISR checks for request status (errors and success)
  407. * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
  408. * 7) ISR completes host->complete_request
  409. * 8) ISR disables interrupts
  410. * 9) Driver wakes up and takes care of the request
  411. *
  412. * Note: "->error"-fields are expected to be set to 0 before the request
  413. * was issued by mmc.c - therefore they are only set, when an error
  414. * contition comes up
  415. */
  416. static irqreturn_t s3cmci_irq(int irq, void *dev_id)
  417. {
  418. struct s3cmci_host *host = dev_id;
  419. struct mmc_command *cmd;
  420. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
  421. u32 mci_cclear = 0, mci_dclear;
  422. unsigned long iflags;
  423. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  424. mci_imsk = readl(host->base + host->sdiimsk);
  425. if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
  426. if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
  427. mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
  428. writel(mci_dclear, host->base + S3C2410_SDIDSTA);
  429. mmc_signal_sdio_irq(host->mmc);
  430. return IRQ_HANDLED;
  431. }
  432. }
  433. spin_lock_irqsave(&host->complete_lock, iflags);
  434. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  435. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  436. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  437. mci_dclear = 0;
  438. if ((host->complete_what == COMPLETION_NONE) ||
  439. (host->complete_what == COMPLETION_FINALIZE)) {
  440. host->status = "nothing to complete";
  441. clear_imask(host);
  442. goto irq_out;
  443. }
  444. if (!host->mrq) {
  445. host->status = "no active mrq";
  446. clear_imask(host);
  447. goto irq_out;
  448. }
  449. cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
  450. if (!cmd) {
  451. host->status = "no active cmd";
  452. clear_imask(host);
  453. goto irq_out;
  454. }
  455. if (!host->dodma) {
  456. if ((host->pio_active == XFER_WRITE) &&
  457. (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
  458. disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  459. tasklet_schedule(&host->pio_tasklet);
  460. host->status = "pio tx";
  461. }
  462. if ((host->pio_active == XFER_READ) &&
  463. (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
  464. disable_imask(host,
  465. S3C2410_SDIIMSK_RXFIFOHALF |
  466. S3C2410_SDIIMSK_RXFIFOLAST);
  467. tasklet_schedule(&host->pio_tasklet);
  468. host->status = "pio rx";
  469. }
  470. }
  471. if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
  472. dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
  473. cmd->error = -ETIMEDOUT;
  474. host->status = "error: command timeout";
  475. goto fail_transfer;
  476. }
  477. if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
  478. if (host->complete_what == COMPLETION_CMDSENT) {
  479. host->status = "ok: command sent";
  480. goto close_transfer;
  481. }
  482. mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
  483. }
  484. if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
  485. if (cmd->flags & MMC_RSP_CRC) {
  486. if (host->mrq->cmd->flags & MMC_RSP_136) {
  487. dbg(host, dbg_irq,
  488. "fixup: ignore CRC fail with long rsp\n");
  489. } else {
  490. /* note, we used to fail the transfer
  491. * here, but it seems that this is just
  492. * the hardware getting it wrong.
  493. *
  494. * cmd->error = -EILSEQ;
  495. * host->status = "error: bad command crc";
  496. * goto fail_transfer;
  497. */
  498. }
  499. }
  500. mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
  501. }
  502. if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
  503. if (host->complete_what == COMPLETION_RSPFIN) {
  504. host->status = "ok: command response received";
  505. goto close_transfer;
  506. }
  507. if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
  508. host->complete_what = COMPLETION_XFERFINISH;
  509. mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
  510. }
  511. /* errors handled after this point are only relevant
  512. when a data transfer is in progress */
  513. if (!cmd->data)
  514. goto clear_status_bits;
  515. /* Check for FIFO failure */
  516. if (host->is2440) {
  517. if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
  518. dbg(host, dbg_err, "FIFO failure\n");
  519. host->mrq->data->error = -EILSEQ;
  520. host->status = "error: 2440 fifo failure";
  521. goto fail_transfer;
  522. }
  523. } else {
  524. if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
  525. dbg(host, dbg_err, "FIFO failure\n");
  526. cmd->data->error = -EILSEQ;
  527. host->status = "error: fifo failure";
  528. goto fail_transfer;
  529. }
  530. }
  531. if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
  532. dbg(host, dbg_err, "bad data crc (outgoing)\n");
  533. cmd->data->error = -EILSEQ;
  534. host->status = "error: bad data crc (outgoing)";
  535. goto fail_transfer;
  536. }
  537. if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
  538. dbg(host, dbg_err, "bad data crc (incoming)\n");
  539. cmd->data->error = -EILSEQ;
  540. host->status = "error: bad data crc (incoming)";
  541. goto fail_transfer;
  542. }
  543. if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
  544. dbg(host, dbg_err, "data timeout\n");
  545. cmd->data->error = -ETIMEDOUT;
  546. host->status = "error: data timeout";
  547. goto fail_transfer;
  548. }
  549. if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
  550. if (host->complete_what == COMPLETION_XFERFINISH) {
  551. host->status = "ok: data transfer completed";
  552. goto close_transfer;
  553. }
  554. if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
  555. host->complete_what = COMPLETION_RSPFIN;
  556. mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
  557. }
  558. clear_status_bits:
  559. writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
  560. writel(mci_dclear, host->base + S3C2410_SDIDSTA);
  561. goto irq_out;
  562. fail_transfer:
  563. host->pio_active = XFER_NONE;
  564. close_transfer:
  565. host->complete_what = COMPLETION_FINALIZE;
  566. clear_imask(host);
  567. tasklet_schedule(&host->pio_tasklet);
  568. goto irq_out;
  569. irq_out:
  570. dbg(host, dbg_irq,
  571. "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
  572. mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
  573. spin_unlock_irqrestore(&host->complete_lock, iflags);
  574. return IRQ_HANDLED;
  575. }
  576. /*
  577. * ISR for the CardDetect Pin
  578. */
  579. static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
  580. {
  581. struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
  582. dbg(host, dbg_irq, "card detect\n");
  583. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  584. return IRQ_HANDLED;
  585. }
  586. static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
  587. void *buf_id, int size,
  588. enum s3c2410_dma_buffresult result)
  589. {
  590. struct s3cmci_host *host = buf_id;
  591. unsigned long iflags;
  592. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
  593. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  594. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  595. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  596. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  597. BUG_ON(!host->mrq);
  598. BUG_ON(!host->mrq->data);
  599. BUG_ON(!host->dmatogo);
  600. spin_lock_irqsave(&host->complete_lock, iflags);
  601. if (result != S3C2410_RES_OK) {
  602. dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
  603. "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
  604. mci_csta, mci_dsta, mci_fsta,
  605. mci_dcnt, result, host->dmatogo);
  606. goto fail_request;
  607. }
  608. host->dmatogo--;
  609. if (host->dmatogo) {
  610. dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
  611. "DCNT:[%08x] toGo:%u\n",
  612. size, mci_dsta, mci_dcnt, host->dmatogo);
  613. goto out;
  614. }
  615. dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
  616. size, mci_dsta, mci_dcnt);
  617. host->complete_what = COMPLETION_FINALIZE;
  618. out:
  619. tasklet_schedule(&host->pio_tasklet);
  620. spin_unlock_irqrestore(&host->complete_lock, iflags);
  621. return;
  622. fail_request:
  623. host->mrq->data->error = -EINVAL;
  624. host->complete_what = COMPLETION_FINALIZE;
  625. clear_imask(host);
  626. goto out;
  627. }
  628. static void finalize_request(struct s3cmci_host *host)
  629. {
  630. struct mmc_request *mrq = host->mrq;
  631. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  632. int debug_as_failure = 0;
  633. if (host->complete_what != COMPLETION_FINALIZE)
  634. return;
  635. if (!mrq)
  636. return;
  637. if (cmd->data && (cmd->error == 0) &&
  638. (cmd->data->error == 0)) {
  639. if (host->dodma && (!host->dma_complete)) {
  640. dbg(host, dbg_dma, "DMA Missing!\n");
  641. return;
  642. }
  643. }
  644. /* Read response from controller. */
  645. cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
  646. cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
  647. cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
  648. cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
  649. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  650. if (cmd->error)
  651. debug_as_failure = 1;
  652. if (cmd->data && cmd->data->error)
  653. debug_as_failure = 1;
  654. dbg_dumpcmd(host, cmd, debug_as_failure);
  655. /* Cleanup controller */
  656. writel(0, host->base + S3C2410_SDICMDARG);
  657. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  658. writel(0, host->base + S3C2410_SDICMDCON);
  659. clear_imask(host);
  660. if (cmd->data && cmd->error)
  661. cmd->data->error = cmd->error;
  662. if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
  663. host->cmd_is_stop = 1;
  664. s3cmci_send_request(host->mmc);
  665. return;
  666. }
  667. /* If we have no data transfer we are finished here */
  668. if (!mrq->data)
  669. goto request_done;
  670. /* Calulate the amout of bytes transfer if there was no error */
  671. if (mrq->data->error == 0) {
  672. mrq->data->bytes_xfered =
  673. (mrq->data->blocks * mrq->data->blksz);
  674. } else {
  675. mrq->data->bytes_xfered = 0;
  676. }
  677. /* If we had an error while transfering data we flush the
  678. * DMA channel and the fifo to clear out any garbage. */
  679. if (mrq->data->error != 0) {
  680. if (host->dodma)
  681. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  682. if (host->is2440) {
  683. /* Clear failure register and reset fifo. */
  684. writel(S3C2440_SDIFSTA_FIFORESET |
  685. S3C2440_SDIFSTA_FIFOFAIL,
  686. host->base + S3C2410_SDIFSTA);
  687. } else {
  688. u32 mci_con;
  689. /* reset fifo */
  690. mci_con = readl(host->base + S3C2410_SDICON);
  691. mci_con |= S3C2410_SDICON_FIFORESET;
  692. writel(mci_con, host->base + S3C2410_SDICON);
  693. }
  694. }
  695. request_done:
  696. host->complete_what = COMPLETION_NONE;
  697. host->mrq = NULL;
  698. s3cmci_check_sdio_irq(host);
  699. mmc_request_done(host->mmc, mrq);
  700. }
  701. static void s3cmci_dma_setup(struct s3cmci_host *host,
  702. enum s3c2410_dmasrc source)
  703. {
  704. static enum s3c2410_dmasrc last_source = -1;
  705. static int setup_ok;
  706. if (last_source == source)
  707. return;
  708. last_source = source;
  709. s3c2410_dma_devconfig(host->dma, source,
  710. host->mem->start + host->sdidata);
  711. if (!setup_ok) {
  712. s3c2410_dma_config(host->dma, 4);
  713. s3c2410_dma_set_buffdone_fn(host->dma,
  714. s3cmci_dma_done_callback);
  715. s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
  716. setup_ok = 1;
  717. }
  718. }
  719. static void s3cmci_send_command(struct s3cmci_host *host,
  720. struct mmc_command *cmd)
  721. {
  722. u32 ccon, imsk;
  723. imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
  724. S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
  725. S3C2410_SDIIMSK_RESPONSECRC;
  726. enable_imask(host, imsk);
  727. if (cmd->data)
  728. host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
  729. else if (cmd->flags & MMC_RSP_PRESENT)
  730. host->complete_what = COMPLETION_RSPFIN;
  731. else
  732. host->complete_what = COMPLETION_CMDSENT;
  733. writel(cmd->arg, host->base + S3C2410_SDICMDARG);
  734. ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
  735. ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
  736. if (cmd->flags & MMC_RSP_PRESENT)
  737. ccon |= S3C2410_SDICMDCON_WAITRSP;
  738. if (cmd->flags & MMC_RSP_136)
  739. ccon |= S3C2410_SDICMDCON_LONGRSP;
  740. writel(ccon, host->base + S3C2410_SDICMDCON);
  741. }
  742. static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
  743. {
  744. u32 dcon, imsk, stoptries = 3;
  745. /* write DCON register */
  746. if (!data) {
  747. writel(0, host->base + S3C2410_SDIDCON);
  748. return 0;
  749. }
  750. if ((data->blksz & 3) != 0) {
  751. /* We cannot deal with unaligned blocks with more than
  752. * one block being transfered. */
  753. if (data->blocks > 1) {
  754. pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
  755. return -EINVAL;
  756. }
  757. }
  758. while (readl(host->base + S3C2410_SDIDSTA) &
  759. (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
  760. dbg(host, dbg_err,
  761. "mci_setup_data() transfer stillin progress.\n");
  762. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  763. s3cmci_reset(host);
  764. if ((stoptries--) == 0) {
  765. dbg_dumpregs(host, "DRF");
  766. return -EINVAL;
  767. }
  768. }
  769. dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
  770. if (host->dodma)
  771. dcon |= S3C2410_SDIDCON_DMAEN;
  772. if (host->bus_width == MMC_BUS_WIDTH_4)
  773. dcon |= S3C2410_SDIDCON_WIDEBUS;
  774. if (!(data->flags & MMC_DATA_STREAM))
  775. dcon |= S3C2410_SDIDCON_BLOCKMODE;
  776. if (data->flags & MMC_DATA_WRITE) {
  777. dcon |= S3C2410_SDIDCON_TXAFTERRESP;
  778. dcon |= S3C2410_SDIDCON_XFER_TXSTART;
  779. }
  780. if (data->flags & MMC_DATA_READ) {
  781. dcon |= S3C2410_SDIDCON_RXAFTERCMD;
  782. dcon |= S3C2410_SDIDCON_XFER_RXSTART;
  783. }
  784. if (host->is2440) {
  785. dcon |= S3C2440_SDIDCON_DS_WORD;
  786. dcon |= S3C2440_SDIDCON_DATSTART;
  787. }
  788. writel(dcon, host->base + S3C2410_SDIDCON);
  789. /* write BSIZE register */
  790. writel(data->blksz, host->base + S3C2410_SDIBSIZE);
  791. /* add to IMASK register */
  792. imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
  793. S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
  794. enable_imask(host, imsk);
  795. /* write TIMER register */
  796. if (host->is2440) {
  797. writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
  798. } else {
  799. writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
  800. /* FIX: set slow clock to prevent timeouts on read */
  801. if (data->flags & MMC_DATA_READ)
  802. writel(0xFF, host->base + S3C2410_SDIPRE);
  803. }
  804. return 0;
  805. }
  806. #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
  807. static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
  808. {
  809. int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  810. BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
  811. host->pio_sgptr = 0;
  812. host->pio_bytes = 0;
  813. host->pio_count = 0;
  814. host->pio_active = rw ? XFER_WRITE : XFER_READ;
  815. if (rw) {
  816. do_pio_write(host);
  817. enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  818. } else {
  819. enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
  820. | S3C2410_SDIIMSK_RXFIFOLAST);
  821. }
  822. return 0;
  823. }
  824. static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
  825. {
  826. int dma_len, i;
  827. int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  828. BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
  829. s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
  830. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  831. dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  832. (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  833. if (dma_len == 0)
  834. return -ENOMEM;
  835. host->dma_complete = 0;
  836. host->dmatogo = dma_len;
  837. for (i = 0; i < dma_len; i++) {
  838. int res;
  839. dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
  840. sg_dma_address(&data->sg[i]),
  841. sg_dma_len(&data->sg[i]));
  842. res = s3c2410_dma_enqueue(host->dma, (void *) host,
  843. sg_dma_address(&data->sg[i]),
  844. sg_dma_len(&data->sg[i]));
  845. if (res) {
  846. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  847. return -EBUSY;
  848. }
  849. }
  850. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
  851. return 0;
  852. }
  853. static void s3cmci_send_request(struct mmc_host *mmc)
  854. {
  855. struct s3cmci_host *host = mmc_priv(mmc);
  856. struct mmc_request *mrq = host->mrq;
  857. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  858. host->ccnt++;
  859. prepare_dbgmsg(host, cmd, host->cmd_is_stop);
  860. /* Clear command, data and fifo status registers
  861. Fifo clear only necessary on 2440, but doesn't hurt on 2410
  862. */
  863. writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
  864. writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
  865. writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
  866. if (cmd->data) {
  867. int res = s3cmci_setup_data(host, cmd->data);
  868. host->dcnt++;
  869. if (res) {
  870. dbg(host, dbg_err, "setup data error %d\n", res);
  871. cmd->error = res;
  872. cmd->data->error = res;
  873. mmc_request_done(mmc, mrq);
  874. return;
  875. }
  876. if (host->dodma)
  877. res = s3cmci_prepare_dma(host, cmd->data);
  878. else
  879. res = s3cmci_prepare_pio(host, cmd->data);
  880. if (res) {
  881. dbg(host, dbg_err, "data prepare error %d\n", res);
  882. cmd->error = res;
  883. cmd->data->error = res;
  884. mmc_request_done(mmc, mrq);
  885. return;
  886. }
  887. }
  888. /* Send command */
  889. s3cmci_send_command(host, cmd);
  890. /* Enable Interrupt */
  891. s3cmci_enable_irq(host, true);
  892. }
  893. static int s3cmci_card_present(struct mmc_host *mmc)
  894. {
  895. struct s3cmci_host *host = mmc_priv(mmc);
  896. struct s3c24xx_mci_pdata *pdata = host->pdata;
  897. int ret;
  898. if (pdata->gpio_detect == 0)
  899. return -ENOSYS;
  900. ret = gpio_get_value(pdata->gpio_detect) ? 0 : 1;
  901. return ret ^ pdata->detect_invert;
  902. }
  903. static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  904. {
  905. struct s3cmci_host *host = mmc_priv(mmc);
  906. host->status = "mmc request";
  907. host->cmd_is_stop = 0;
  908. host->mrq = mrq;
  909. if (s3cmci_card_present(mmc) == 0) {
  910. dbg(host, dbg_err, "%s: no medium present\n", __func__);
  911. host->mrq->cmd->error = -ENOMEDIUM;
  912. mmc_request_done(mmc, mrq);
  913. } else
  914. s3cmci_send_request(mmc);
  915. }
  916. static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
  917. {
  918. u32 mci_psc;
  919. /* Set clock */
  920. for (mci_psc = 0; mci_psc < 255; mci_psc++) {
  921. host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
  922. if (host->real_rate <= ios->clock)
  923. break;
  924. }
  925. if (mci_psc > 255)
  926. mci_psc = 255;
  927. host->prescaler = mci_psc;
  928. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  929. /* If requested clock is 0, real_rate will be 0, too */
  930. if (ios->clock == 0)
  931. host->real_rate = 0;
  932. }
  933. static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  934. {
  935. struct s3cmci_host *host = mmc_priv(mmc);
  936. u32 mci_con;
  937. /* Set the power state */
  938. mci_con = readl(host->base + S3C2410_SDICON);
  939. switch (ios->power_mode) {
  940. case MMC_POWER_ON:
  941. case MMC_POWER_UP:
  942. s3c2410_gpio_cfgpin(S3C2410_GPE(5), S3C2410_GPE5_SDCLK);
  943. s3c2410_gpio_cfgpin(S3C2410_GPE(6), S3C2410_GPE6_SDCMD);
  944. s3c2410_gpio_cfgpin(S3C2410_GPE(7), S3C2410_GPE7_SDDAT0);
  945. s3c2410_gpio_cfgpin(S3C2410_GPE(8), S3C2410_GPE8_SDDAT1);
  946. s3c2410_gpio_cfgpin(S3C2410_GPE(9), S3C2410_GPE9_SDDAT2);
  947. s3c2410_gpio_cfgpin(S3C2410_GPE(10), S3C2410_GPE10_SDDAT3);
  948. if (host->pdata->set_power)
  949. host->pdata->set_power(ios->power_mode, ios->vdd);
  950. if (!host->is2440)
  951. mci_con |= S3C2410_SDICON_FIFORESET;
  952. break;
  953. case MMC_POWER_OFF:
  954. default:
  955. gpio_direction_output(S3C2410_GPE(5), 0);
  956. if (host->is2440)
  957. mci_con |= S3C2440_SDICON_SDRESET;
  958. if (host->pdata->set_power)
  959. host->pdata->set_power(ios->power_mode, ios->vdd);
  960. break;
  961. }
  962. s3cmci_set_clk(host, ios);
  963. /* Set CLOCK_ENABLE */
  964. if (ios->clock)
  965. mci_con |= S3C2410_SDICON_CLOCKTYPE;
  966. else
  967. mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
  968. writel(mci_con, host->base + S3C2410_SDICON);
  969. if ((ios->power_mode == MMC_POWER_ON) ||
  970. (ios->power_mode == MMC_POWER_UP)) {
  971. dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
  972. host->real_rate/1000, ios->clock/1000);
  973. } else {
  974. dbg(host, dbg_conf, "powered down.\n");
  975. }
  976. host->bus_width = ios->bus_width;
  977. }
  978. static void s3cmci_reset(struct s3cmci_host *host)
  979. {
  980. u32 con = readl(host->base + S3C2410_SDICON);
  981. con |= S3C2440_SDICON_SDRESET;
  982. writel(con, host->base + S3C2410_SDICON);
  983. }
  984. static int s3cmci_get_ro(struct mmc_host *mmc)
  985. {
  986. struct s3cmci_host *host = mmc_priv(mmc);
  987. struct s3c24xx_mci_pdata *pdata = host->pdata;
  988. int ret;
  989. if (pdata->gpio_wprotect == 0)
  990. return 0;
  991. ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
  992. if (pdata->wprotect_invert)
  993. ret = !ret;
  994. return ret;
  995. }
  996. static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  997. {
  998. struct s3cmci_host *host = mmc_priv(mmc);
  999. unsigned long flags;
  1000. u32 con;
  1001. local_irq_save(flags);
  1002. con = readl(host->base + S3C2410_SDICON);
  1003. host->sdio_irqen = enable;
  1004. if (enable == host->sdio_irqen)
  1005. goto same_state;
  1006. if (enable) {
  1007. con |= S3C2410_SDICON_SDIOIRQ;
  1008. enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
  1009. if (!host->irq_state && !host->irq_disabled) {
  1010. host->irq_state = true;
  1011. enable_irq(host->irq);
  1012. }
  1013. } else {
  1014. disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
  1015. con &= ~S3C2410_SDICON_SDIOIRQ;
  1016. if (!host->irq_enabled && host->irq_state) {
  1017. disable_irq_nosync(host->irq);
  1018. host->irq_state = false;
  1019. }
  1020. }
  1021. writel(con, host->base + S3C2410_SDICON);
  1022. same_state:
  1023. local_irq_restore(flags);
  1024. s3cmci_check_sdio_irq(host);
  1025. }
  1026. static struct mmc_host_ops s3cmci_ops = {
  1027. .request = s3cmci_request,
  1028. .set_ios = s3cmci_set_ios,
  1029. .get_ro = s3cmci_get_ro,
  1030. .get_cd = s3cmci_card_present,
  1031. .enable_sdio_irq = s3cmci_enable_sdio_irq,
  1032. };
  1033. static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
  1034. /* This is currently here to avoid a number of if (host->pdata)
  1035. * checks. Any zero fields to ensure reaonable defaults are picked. */
  1036. };
  1037. #ifdef CONFIG_CPU_FREQ
  1038. static int s3cmci_cpufreq_transition(struct notifier_block *nb,
  1039. unsigned long val, void *data)
  1040. {
  1041. struct s3cmci_host *host;
  1042. struct mmc_host *mmc;
  1043. unsigned long newclk;
  1044. unsigned long flags;
  1045. host = container_of(nb, struct s3cmci_host, freq_transition);
  1046. newclk = clk_get_rate(host->clk);
  1047. mmc = host->mmc;
  1048. if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
  1049. (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
  1050. spin_lock_irqsave(&mmc->lock, flags);
  1051. host->clk_rate = newclk;
  1052. if (mmc->ios.power_mode != MMC_POWER_OFF &&
  1053. mmc->ios.clock != 0)
  1054. s3cmci_set_clk(host, &mmc->ios);
  1055. spin_unlock_irqrestore(&mmc->lock, flags);
  1056. }
  1057. return 0;
  1058. }
  1059. static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
  1060. {
  1061. host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
  1062. return cpufreq_register_notifier(&host->freq_transition,
  1063. CPUFREQ_TRANSITION_NOTIFIER);
  1064. }
  1065. static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
  1066. {
  1067. cpufreq_unregister_notifier(&host->freq_transition,
  1068. CPUFREQ_TRANSITION_NOTIFIER);
  1069. }
  1070. #else
  1071. static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
  1072. {
  1073. return 0;
  1074. }
  1075. static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
  1076. {
  1077. }
  1078. #endif
  1079. #ifdef CONFIG_DEBUG_FS
  1080. static int s3cmci_state_show(struct seq_file *seq, void *v)
  1081. {
  1082. struct s3cmci_host *host = seq->private;
  1083. seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
  1084. seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
  1085. seq_printf(seq, "Prescale = %d\n", host->prescaler);
  1086. seq_printf(seq, "is2440 = %d\n", host->is2440);
  1087. seq_printf(seq, "IRQ = %d\n", host->irq);
  1088. seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
  1089. seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
  1090. seq_printf(seq, "IRQ state = %d\n", host->irq_state);
  1091. seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
  1092. seq_printf(seq, "Do DMA = %d\n", host->dodma);
  1093. seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
  1094. seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
  1095. return 0;
  1096. }
  1097. static int s3cmci_state_open(struct inode *inode, struct file *file)
  1098. {
  1099. return single_open(file, s3cmci_state_show, inode->i_private);
  1100. }
  1101. static const struct file_operations s3cmci_fops_state = {
  1102. .owner = THIS_MODULE,
  1103. .open = s3cmci_state_open,
  1104. .read = seq_read,
  1105. .llseek = seq_lseek,
  1106. .release = single_release,
  1107. };
  1108. #define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
  1109. struct s3cmci_reg {
  1110. unsigned short addr;
  1111. unsigned char *name;
  1112. } debug_regs[] = {
  1113. DBG_REG(CON),
  1114. DBG_REG(PRE),
  1115. DBG_REG(CMDARG),
  1116. DBG_REG(CMDCON),
  1117. DBG_REG(CMDSTAT),
  1118. DBG_REG(RSP0),
  1119. DBG_REG(RSP1),
  1120. DBG_REG(RSP2),
  1121. DBG_REG(RSP3),
  1122. DBG_REG(TIMER),
  1123. DBG_REG(BSIZE),
  1124. DBG_REG(DCON),
  1125. DBG_REG(DCNT),
  1126. DBG_REG(DSTA),
  1127. DBG_REG(FSTA),
  1128. {}
  1129. };
  1130. static int s3cmci_regs_show(struct seq_file *seq, void *v)
  1131. {
  1132. struct s3cmci_host *host = seq->private;
  1133. struct s3cmci_reg *rptr = debug_regs;
  1134. for (; rptr->name; rptr++)
  1135. seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
  1136. readl(host->base + rptr->addr));
  1137. seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
  1138. return 0;
  1139. }
  1140. static int s3cmci_regs_open(struct inode *inode, struct file *file)
  1141. {
  1142. return single_open(file, s3cmci_regs_show, inode->i_private);
  1143. }
  1144. static const struct file_operations s3cmci_fops_regs = {
  1145. .owner = THIS_MODULE,
  1146. .open = s3cmci_regs_open,
  1147. .read = seq_read,
  1148. .llseek = seq_lseek,
  1149. .release = single_release,
  1150. };
  1151. static void s3cmci_debugfs_attach(struct s3cmci_host *host)
  1152. {
  1153. struct device *dev = &host->pdev->dev;
  1154. host->debug_root = debugfs_create_dir(dev_name(dev), NULL);
  1155. if (IS_ERR(host->debug_root)) {
  1156. dev_err(dev, "failed to create debugfs root\n");
  1157. return;
  1158. }
  1159. host->debug_state = debugfs_create_file("state", 0444,
  1160. host->debug_root, host,
  1161. &s3cmci_fops_state);
  1162. if (IS_ERR(host->debug_state))
  1163. dev_err(dev, "failed to create debug state file\n");
  1164. host->debug_regs = debugfs_create_file("regs", 0444,
  1165. host->debug_root, host,
  1166. &s3cmci_fops_regs);
  1167. if (IS_ERR(host->debug_regs))
  1168. dev_err(dev, "failed to create debug regs file\n");
  1169. }
  1170. static void s3cmci_debugfs_remove(struct s3cmci_host *host)
  1171. {
  1172. debugfs_remove(host->debug_regs);
  1173. debugfs_remove(host->debug_state);
  1174. debugfs_remove(host->debug_root);
  1175. }
  1176. #else
  1177. static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
  1178. static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
  1179. #endif /* CONFIG_DEBUG_FS */
  1180. static int __devinit s3cmci_probe(struct platform_device *pdev)
  1181. {
  1182. struct s3cmci_host *host;
  1183. struct mmc_host *mmc;
  1184. int ret;
  1185. int is2440;
  1186. int i;
  1187. is2440 = platform_get_device_id(pdev)->driver_data;
  1188. mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
  1189. if (!mmc) {
  1190. ret = -ENOMEM;
  1191. goto probe_out;
  1192. }
  1193. for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
  1194. ret = gpio_request(i, dev_name(&pdev->dev));
  1195. if (ret) {
  1196. dev_err(&pdev->dev, "failed to get gpio %d\n", i);
  1197. for (i--; i >= S3C2410_GPE(5); i--)
  1198. gpio_free(i);
  1199. goto probe_free_host;
  1200. }
  1201. }
  1202. host = mmc_priv(mmc);
  1203. host->mmc = mmc;
  1204. host->pdev = pdev;
  1205. host->is2440 = is2440;
  1206. host->pdata = pdev->dev.platform_data;
  1207. if (!host->pdata) {
  1208. pdev->dev.platform_data = &s3cmci_def_pdata;
  1209. host->pdata = &s3cmci_def_pdata;
  1210. }
  1211. spin_lock_init(&host->complete_lock);
  1212. tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
  1213. if (is2440) {
  1214. host->sdiimsk = S3C2440_SDIIMSK;
  1215. host->sdidata = S3C2440_SDIDATA;
  1216. host->clk_div = 1;
  1217. } else {
  1218. host->sdiimsk = S3C2410_SDIIMSK;
  1219. host->sdidata = S3C2410_SDIDATA;
  1220. host->clk_div = 2;
  1221. }
  1222. host->dodma = 0;
  1223. host->complete_what = COMPLETION_NONE;
  1224. host->pio_active = XFER_NONE;
  1225. host->dma = S3CMCI_DMA;
  1226. host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1227. if (!host->mem) {
  1228. dev_err(&pdev->dev,
  1229. "failed to get io memory region resouce.\n");
  1230. ret = -ENOENT;
  1231. goto probe_free_gpio;
  1232. }
  1233. host->mem = request_mem_region(host->mem->start,
  1234. resource_size(host->mem), pdev->name);
  1235. if (!host->mem) {
  1236. dev_err(&pdev->dev, "failed to request io memory region.\n");
  1237. ret = -ENOENT;
  1238. goto probe_free_gpio;
  1239. }
  1240. host->base = ioremap(host->mem->start, resource_size(host->mem));
  1241. if (!host->base) {
  1242. dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
  1243. ret = -EINVAL;
  1244. goto probe_free_mem_region;
  1245. }
  1246. host->irq = platform_get_irq(pdev, 0);
  1247. if (host->irq == 0) {
  1248. dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
  1249. ret = -EINVAL;
  1250. goto probe_iounmap;
  1251. }
  1252. if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
  1253. dev_err(&pdev->dev, "failed to request mci interrupt.\n");
  1254. ret = -ENOENT;
  1255. goto probe_iounmap;
  1256. }
  1257. /* We get spurious interrupts even when we have set the IMSK
  1258. * register to ignore everything, so use disable_irq() to make
  1259. * ensure we don't lock the system with un-serviceable requests. */
  1260. disable_irq(host->irq);
  1261. host->irq_state = false;
  1262. if (host->pdata->gpio_detect) {
  1263. ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");
  1264. if (ret) {
  1265. dev_err(&pdev->dev, "failed to get detect gpio\n");
  1266. goto probe_free_irq;
  1267. }
  1268. }
  1269. host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
  1270. if (host->irq_cd >= 0) {
  1271. if (request_irq(host->irq_cd, s3cmci_irq_cd,
  1272. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1273. DRIVER_NAME, host)) {
  1274. dev_err(&pdev->dev, "can't get card detect irq.\n");
  1275. ret = -ENOENT;
  1276. goto probe_free_gpio_cd;
  1277. }
  1278. } else {
  1279. dev_warn(&pdev->dev, "host detect has no irq available\n");
  1280. gpio_direction_input(host->pdata->gpio_detect);
  1281. }
  1282. if (host->pdata->gpio_wprotect) {
  1283. ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");
  1284. if (ret) {
  1285. dev_err(&pdev->dev, "failed to get writeprotect\n");
  1286. goto probe_free_irq_cd;
  1287. }
  1288. gpio_direction_input(host->pdata->gpio_wprotect);
  1289. }
  1290. if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
  1291. dev_err(&pdev->dev, "unable to get DMA channel.\n");
  1292. ret = -EBUSY;
  1293. goto probe_free_gpio_wp;
  1294. }
  1295. host->clk = clk_get(&pdev->dev, "sdi");
  1296. if (IS_ERR(host->clk)) {
  1297. dev_err(&pdev->dev, "failed to find clock source.\n");
  1298. ret = PTR_ERR(host->clk);
  1299. host->clk = NULL;
  1300. goto probe_free_host;
  1301. }
  1302. ret = clk_enable(host->clk);
  1303. if (ret) {
  1304. dev_err(&pdev->dev, "failed to enable clock source.\n");
  1305. goto clk_free;
  1306. }
  1307. host->clk_rate = clk_get_rate(host->clk);
  1308. mmc->ops = &s3cmci_ops;
  1309. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1310. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  1311. mmc->f_min = host->clk_rate / (host->clk_div * 256);
  1312. mmc->f_max = host->clk_rate / host->clk_div;
  1313. if (host->pdata->ocr_avail)
  1314. mmc->ocr_avail = host->pdata->ocr_avail;
  1315. mmc->max_blk_count = 4095;
  1316. mmc->max_blk_size = 4095;
  1317. mmc->max_req_size = 4095 * 512;
  1318. mmc->max_seg_size = mmc->max_req_size;
  1319. mmc->max_phys_segs = 128;
  1320. mmc->max_hw_segs = 128;
  1321. dbg(host, dbg_debug,
  1322. "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
  1323. (host->is2440?"2440":""),
  1324. host->base, host->irq, host->irq_cd, host->dma);
  1325. ret = s3cmci_cpufreq_register(host);
  1326. if (ret) {
  1327. dev_err(&pdev->dev, "failed to register cpufreq\n");
  1328. goto free_dmabuf;
  1329. }
  1330. ret = mmc_add_host(mmc);
  1331. if (ret) {
  1332. dev_err(&pdev->dev, "failed to add mmc host.\n");
  1333. goto free_cpufreq;
  1334. }
  1335. s3cmci_debugfs_attach(host);
  1336. platform_set_drvdata(pdev, mmc);
  1337. dev_info(&pdev->dev, "initialisation done.\n");
  1338. return 0;
  1339. free_cpufreq:
  1340. s3cmci_cpufreq_deregister(host);
  1341. free_dmabuf:
  1342. clk_disable(host->clk);
  1343. clk_free:
  1344. clk_put(host->clk);
  1345. probe_free_gpio_wp:
  1346. if (host->pdata->gpio_wprotect)
  1347. gpio_free(host->pdata->gpio_wprotect);
  1348. probe_free_gpio_cd:
  1349. if (host->pdata->gpio_detect)
  1350. gpio_free(host->pdata->gpio_detect);
  1351. probe_free_irq_cd:
  1352. if (host->irq_cd >= 0)
  1353. free_irq(host->irq_cd, host);
  1354. probe_free_irq:
  1355. free_irq(host->irq, host);
  1356. probe_iounmap:
  1357. iounmap(host->base);
  1358. probe_free_mem_region:
  1359. release_mem_region(host->mem->start, resource_size(host->mem));
  1360. probe_free_gpio:
  1361. for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
  1362. gpio_free(i);
  1363. probe_free_host:
  1364. mmc_free_host(mmc);
  1365. probe_out:
  1366. return ret;
  1367. }
  1368. static void s3cmci_shutdown(struct platform_device *pdev)
  1369. {
  1370. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1371. struct s3cmci_host *host = mmc_priv(mmc);
  1372. if (host->irq_cd >= 0)
  1373. free_irq(host->irq_cd, host);
  1374. s3cmci_debugfs_remove(host);
  1375. s3cmci_cpufreq_deregister(host);
  1376. mmc_remove_host(mmc);
  1377. clk_disable(host->clk);
  1378. }
  1379. static int __devexit s3cmci_remove(struct platform_device *pdev)
  1380. {
  1381. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1382. struct s3cmci_host *host = mmc_priv(mmc);
  1383. struct s3c24xx_mci_pdata *pd = host->pdata;
  1384. int i;
  1385. s3cmci_shutdown(pdev);
  1386. clk_put(host->clk);
  1387. tasklet_disable(&host->pio_tasklet);
  1388. s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
  1389. free_irq(host->irq, host);
  1390. if (pd->gpio_wprotect)
  1391. gpio_free(pd->gpio_wprotect);
  1392. if (pd->gpio_detect)
  1393. gpio_free(pd->gpio_detect);
  1394. for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
  1395. gpio_free(i);
  1396. iounmap(host->base);
  1397. release_mem_region(host->mem->start, resource_size(host->mem));
  1398. mmc_free_host(mmc);
  1399. return 0;
  1400. }
  1401. static struct platform_device_id s3cmci_driver_ids[] = {
  1402. {
  1403. .name = "s3c2410-sdi",
  1404. .driver_data = 0,
  1405. }, {
  1406. .name = "s3c2412-sdi",
  1407. .driver_data = 1,
  1408. }, {
  1409. .name = "s3c2440-sdi",
  1410. .driver_data = 1,
  1411. },
  1412. { }
  1413. };
  1414. MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
  1415. #ifdef CONFIG_PM
  1416. static int s3cmci_suspend(struct device *dev)
  1417. {
  1418. struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
  1419. struct pm_message event = { PM_EVENT_SUSPEND };
  1420. return mmc_suspend_host(mmc, event);
  1421. }
  1422. static int s3cmci_resume(struct device *dev)
  1423. {
  1424. struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
  1425. return mmc_resume_host(mmc);
  1426. }
  1427. static struct dev_pm_ops s3cmci_pm = {
  1428. .suspend = s3cmci_suspend,
  1429. .resume = s3cmci_resume,
  1430. };
  1431. #define s3cmci_pm_ops &s3cmci_pm
  1432. #else /* CONFIG_PM */
  1433. #define s3cmci_pm_ops NULL
  1434. #endif /* CONFIG_PM */
  1435. static struct platform_driver s3cmci_driver = {
  1436. .driver = {
  1437. .name = "s3c-sdi",
  1438. .owner = THIS_MODULE,
  1439. .pm = s3cmci_pm_ops,
  1440. },
  1441. .id_table = s3cmci_driver_ids,
  1442. .probe = s3cmci_probe,
  1443. .remove = __devexit_p(s3cmci_remove),
  1444. .shutdown = s3cmci_shutdown,
  1445. };
  1446. static int __init s3cmci_init(void)
  1447. {
  1448. return platform_driver_register(&s3cmci_driver);
  1449. }
  1450. static void __exit s3cmci_exit(void)
  1451. {
  1452. platform_driver_unregister(&s3cmci_driver);
  1453. }
  1454. module_init(s3cmci_init);
  1455. module_exit(s3cmci_exit);
  1456. MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
  1457. MODULE_LICENSE("GPL v2");
  1458. MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");