s3cmci.c 48 KB

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