s3cmci.c 48 KB

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