s3cmci.c 46 KB

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