s3cmci.c 46 KB

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