s3cmci.c 48 KB

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