s3cmci.c 46 KB

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