s3cmci.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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/irq.h>
  20. #include <linux/io.h>
  21. #include <mach/dma.h>
  22. #include <mach/regs-sdi.h>
  23. #include <mach/regs-gpio.h>
  24. #include <plat/mci.h>
  25. #include "s3cmci.h"
  26. #define DRIVER_NAME "s3c-mci"
  27. enum dbg_channels {
  28. dbg_err = (1 << 0),
  29. dbg_debug = (1 << 1),
  30. dbg_info = (1 << 2),
  31. dbg_irq = (1 << 3),
  32. dbg_sg = (1 << 4),
  33. dbg_dma = (1 << 5),
  34. dbg_pio = (1 << 6),
  35. dbg_fail = (1 << 7),
  36. dbg_conf = (1 << 8),
  37. };
  38. static const int dbgmap_err = dbg_fail;
  39. static const int dbgmap_info = dbg_info | dbg_conf;
  40. static const int dbgmap_debug = dbg_err | dbg_debug;
  41. #define dbg(host, channels, args...) \
  42. do { \
  43. if (dbgmap_err & channels) \
  44. dev_err(&host->pdev->dev, args); \
  45. else if (dbgmap_info & channels) \
  46. dev_info(&host->pdev->dev, args); \
  47. else if (dbgmap_debug & channels) \
  48. dev_dbg(&host->pdev->dev, args); \
  49. } while (0)
  50. #define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)
  51. static struct s3c2410_dma_client s3cmci_dma_client = {
  52. .name = "s3c-mci",
  53. };
  54. static void finalize_request(struct s3cmci_host *host);
  55. static void s3cmci_send_request(struct mmc_host *mmc);
  56. static void s3cmci_reset(struct s3cmci_host *host);
  57. #ifdef CONFIG_MMC_DEBUG
  58. static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
  59. {
  60. u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
  61. u32 datcon, datcnt, datsta, fsta, imask;
  62. con = readl(host->base + S3C2410_SDICON);
  63. pre = readl(host->base + S3C2410_SDIPRE);
  64. cmdarg = readl(host->base + S3C2410_SDICMDARG);
  65. cmdcon = readl(host->base + S3C2410_SDICMDCON);
  66. cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
  67. r0 = readl(host->base + S3C2410_SDIRSP0);
  68. r1 = readl(host->base + S3C2410_SDIRSP1);
  69. r2 = readl(host->base + S3C2410_SDIRSP2);
  70. r3 = readl(host->base + S3C2410_SDIRSP3);
  71. timer = readl(host->base + S3C2410_SDITIMER);
  72. bsize = readl(host->base + S3C2410_SDIBSIZE);
  73. datcon = readl(host->base + S3C2410_SDIDCON);
  74. datcnt = readl(host->base + S3C2410_SDIDCNT);
  75. datsta = readl(host->base + S3C2410_SDIDSTA);
  76. fsta = readl(host->base + S3C2410_SDIFSTA);
  77. imask = readl(host->base + host->sdiimsk);
  78. dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
  79. prefix, con, pre, timer);
  80. dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
  81. prefix, cmdcon, cmdarg, cmdsta);
  82. dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
  83. " DSTA:[%08x] DCNT:[%08x]\n",
  84. prefix, datcon, fsta, datsta, datcnt);
  85. dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
  86. " R2:[%08x] R3:[%08x]\n",
  87. prefix, r0, r1, r2, r3);
  88. }
  89. static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
  90. int stop)
  91. {
  92. snprintf(host->dbgmsg_cmd, 300,
  93. "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
  94. host->ccnt, (stop ? " (STOP)" : ""),
  95. cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
  96. if (cmd->data) {
  97. snprintf(host->dbgmsg_dat, 300,
  98. "#%u bsize:%u blocks:%u bytes:%u",
  99. host->dcnt, cmd->data->blksz,
  100. cmd->data->blocks,
  101. cmd->data->blocks * cmd->data->blksz);
  102. } else {
  103. host->dbgmsg_dat[0] = '\0';
  104. }
  105. }
  106. static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
  107. int fail)
  108. {
  109. unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
  110. if (!cmd)
  111. return;
  112. if (cmd->error == 0) {
  113. dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
  114. host->dbgmsg_cmd, cmd->resp[0]);
  115. } else {
  116. dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
  117. cmd->error, host->dbgmsg_cmd, host->status);
  118. }
  119. if (!cmd->data)
  120. return;
  121. if (cmd->data->error == 0) {
  122. dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
  123. } else {
  124. dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
  125. cmd->data->error, host->dbgmsg_dat,
  126. readl(host->base + S3C2410_SDIDCNT));
  127. }
  128. }
  129. #else
  130. static void dbg_dumpcmd(struct s3cmci_host *host,
  131. struct mmc_command *cmd, int fail) { }
  132. static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
  133. int stop) { }
  134. static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
  135. #endif /* CONFIG_MMC_DEBUG */
  136. static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
  137. {
  138. u32 newmask;
  139. newmask = readl(host->base + host->sdiimsk);
  140. newmask |= imask;
  141. writel(newmask, host->base + host->sdiimsk);
  142. return newmask;
  143. }
  144. static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
  145. {
  146. u32 newmask;
  147. newmask = readl(host->base + host->sdiimsk);
  148. newmask &= ~imask;
  149. writel(newmask, host->base + host->sdiimsk);
  150. return newmask;
  151. }
  152. static inline void clear_imask(struct s3cmci_host *host)
  153. {
  154. writel(0, host->base + host->sdiimsk);
  155. }
  156. static inline int get_data_buffer(struct s3cmci_host *host,
  157. u32 *bytes, u32 **pointer)
  158. {
  159. struct scatterlist *sg;
  160. if (host->pio_active == XFER_NONE)
  161. return -EINVAL;
  162. if ((!host->mrq) || (!host->mrq->data))
  163. return -EINVAL;
  164. if (host->pio_sgptr >= host->mrq->data->sg_len) {
  165. dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
  166. host->pio_sgptr, host->mrq->data->sg_len);
  167. return -EBUSY;
  168. }
  169. sg = &host->mrq->data->sg[host->pio_sgptr];
  170. *bytes = sg->length;
  171. *pointer = sg_virt(sg);
  172. host->pio_sgptr++;
  173. dbg(host, dbg_sg, "new buffer (%i/%i)\n",
  174. host->pio_sgptr, host->mrq->data->sg_len);
  175. return 0;
  176. }
  177. static inline u32 fifo_count(struct s3cmci_host *host)
  178. {
  179. u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
  180. fifostat &= S3C2410_SDIFSTA_COUNTMASK;
  181. return fifostat;
  182. }
  183. static inline u32 fifo_free(struct s3cmci_host *host)
  184. {
  185. u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
  186. fifostat &= S3C2410_SDIFSTA_COUNTMASK;
  187. return 63 - fifostat;
  188. }
  189. static void do_pio_read(struct s3cmci_host *host)
  190. {
  191. int res;
  192. u32 fifo;
  193. u32 *ptr;
  194. u32 fifo_words;
  195. void __iomem *from_ptr;
  196. /* write real prescaler to host, it might be set slow to fix */
  197. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  198. from_ptr = host->base + host->sdidata;
  199. while ((fifo = fifo_count(host))) {
  200. if (!host->pio_bytes) {
  201. res = get_data_buffer(host, &host->pio_bytes,
  202. &host->pio_ptr);
  203. if (res) {
  204. host->pio_active = XFER_NONE;
  205. host->complete_what = COMPLETION_FINALIZE;
  206. dbg(host, dbg_pio, "pio_read(): "
  207. "complete (no more data).\n");
  208. return;
  209. }
  210. dbg(host, dbg_pio,
  211. "pio_read(): new target: [%i]@[%p]\n",
  212. host->pio_bytes, host->pio_ptr);
  213. }
  214. dbg(host, dbg_pio,
  215. "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
  216. fifo, host->pio_bytes,
  217. readl(host->base + S3C2410_SDIDCNT));
  218. /* If we have reached the end of the block, we can
  219. * read a word and get 1 to 3 bytes. If we in the
  220. * middle of the block, we have to read full words,
  221. * otherwise we will write garbage, so round down to
  222. * an even multiple of 4. */
  223. if (fifo >= host->pio_bytes)
  224. fifo = host->pio_bytes;
  225. else
  226. fifo -= fifo & 3;
  227. host->pio_bytes -= fifo;
  228. host->pio_count += fifo;
  229. fifo_words = fifo >> 2;
  230. ptr = host->pio_ptr;
  231. while (fifo_words--)
  232. *ptr++ = readl(from_ptr);
  233. host->pio_ptr = ptr;
  234. if (fifo & 3) {
  235. u32 n = fifo & 3;
  236. u32 data = readl(from_ptr);
  237. u8 *p = (u8 *)host->pio_ptr;
  238. while (n--) {
  239. *p++ = data;
  240. data >>= 8;
  241. }
  242. }
  243. }
  244. if (!host->pio_bytes) {
  245. res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
  246. if (res) {
  247. dbg(host, dbg_pio,
  248. "pio_read(): complete (no more buffers).\n");
  249. host->pio_active = XFER_NONE;
  250. host->complete_what = COMPLETION_FINALIZE;
  251. return;
  252. }
  253. }
  254. enable_imask(host,
  255. S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
  256. }
  257. static void do_pio_write(struct s3cmci_host *host)
  258. {
  259. void __iomem *to_ptr;
  260. int res;
  261. u32 fifo;
  262. u32 *ptr;
  263. to_ptr = host->base + host->sdidata;
  264. while ((fifo = fifo_free(host)) > 3) {
  265. if (!host->pio_bytes) {
  266. res = get_data_buffer(host, &host->pio_bytes,
  267. &host->pio_ptr);
  268. if (res) {
  269. dbg(host, dbg_pio,
  270. "pio_write(): complete (no more data).\n");
  271. host->pio_active = XFER_NONE;
  272. return;
  273. }
  274. dbg(host, dbg_pio,
  275. "pio_write(): new source: [%i]@[%p]\n",
  276. host->pio_bytes, host->pio_ptr);
  277. }
  278. /* If we have reached the end of the block, we have to
  279. * write exactly the remaining number of bytes. If we
  280. * in the middle of the block, we have to write full
  281. * words, so round down to an even multiple of 4. */
  282. if (fifo >= host->pio_bytes)
  283. fifo = host->pio_bytes;
  284. else
  285. fifo -= fifo & 3;
  286. host->pio_bytes -= fifo;
  287. host->pio_count += fifo;
  288. fifo = (fifo + 3) >> 2;
  289. ptr = host->pio_ptr;
  290. while (fifo--)
  291. writel(*ptr++, to_ptr);
  292. host->pio_ptr = ptr;
  293. }
  294. enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  295. }
  296. static void pio_tasklet(unsigned long data)
  297. {
  298. struct s3cmci_host *host = (struct s3cmci_host *) data;
  299. disable_irq(host->irq);
  300. if (host->pio_active == XFER_WRITE)
  301. do_pio_write(host);
  302. if (host->pio_active == XFER_READ)
  303. do_pio_read(host);
  304. if (host->complete_what == COMPLETION_FINALIZE) {
  305. clear_imask(host);
  306. if (host->pio_active != XFER_NONE) {
  307. dbg(host, dbg_err, "unfinished %s "
  308. "- pio_count:[%u] pio_bytes:[%u]\n",
  309. (host->pio_active == XFER_READ) ? "read" : "write",
  310. host->pio_count, host->pio_bytes);
  311. if (host->mrq->data)
  312. host->mrq->data->error = -EINVAL;
  313. }
  314. finalize_request(host);
  315. } else
  316. enable_irq(host->irq);
  317. }
  318. /*
  319. * ISR for SDI Interface IRQ
  320. * Communication between driver and ISR works as follows:
  321. * host->mrq points to current request
  322. * host->complete_what Indicates when the request is considered done
  323. * COMPLETION_CMDSENT when the command was sent
  324. * COMPLETION_RSPFIN when a response was received
  325. * COMPLETION_XFERFINISH when the data transfer is finished
  326. * COMPLETION_XFERFINISH_RSPFIN both of the above.
  327. * host->complete_request is the completion-object the driver waits for
  328. *
  329. * 1) Driver sets up host->mrq and host->complete_what
  330. * 2) Driver prepares the transfer
  331. * 3) Driver enables interrupts
  332. * 4) Driver starts transfer
  333. * 5) Driver waits for host->complete_rquest
  334. * 6) ISR checks for request status (errors and success)
  335. * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
  336. * 7) ISR completes host->complete_request
  337. * 8) ISR disables interrupts
  338. * 9) Driver wakes up and takes care of the request
  339. *
  340. * Note: "->error"-fields are expected to be set to 0 before the request
  341. * was issued by mmc.c - therefore they are only set, when an error
  342. * contition comes up
  343. */
  344. static irqreturn_t s3cmci_irq(int irq, void *dev_id)
  345. {
  346. struct s3cmci_host *host = dev_id;
  347. struct mmc_command *cmd;
  348. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
  349. u32 mci_cclear, mci_dclear;
  350. unsigned long iflags;
  351. spin_lock_irqsave(&host->complete_lock, iflags);
  352. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  353. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  354. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  355. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  356. mci_imsk = readl(host->base + host->sdiimsk);
  357. mci_cclear = 0;
  358. mci_dclear = 0;
  359. if ((host->complete_what == COMPLETION_NONE) ||
  360. (host->complete_what == COMPLETION_FINALIZE)) {
  361. host->status = "nothing to complete";
  362. clear_imask(host);
  363. goto irq_out;
  364. }
  365. if (!host->mrq) {
  366. host->status = "no active mrq";
  367. clear_imask(host);
  368. goto irq_out;
  369. }
  370. cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
  371. if (!cmd) {
  372. host->status = "no active cmd";
  373. clear_imask(host);
  374. goto irq_out;
  375. }
  376. if (!host->dodma) {
  377. if ((host->pio_active == XFER_WRITE) &&
  378. (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
  379. disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  380. tasklet_schedule(&host->pio_tasklet);
  381. host->status = "pio tx";
  382. }
  383. if ((host->pio_active == XFER_READ) &&
  384. (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
  385. disable_imask(host,
  386. S3C2410_SDIIMSK_RXFIFOHALF |
  387. S3C2410_SDIIMSK_RXFIFOLAST);
  388. tasklet_schedule(&host->pio_tasklet);
  389. host->status = "pio rx";
  390. }
  391. }
  392. if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
  393. dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
  394. cmd->error = -ETIMEDOUT;
  395. host->status = "error: command timeout";
  396. goto fail_transfer;
  397. }
  398. if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
  399. if (host->complete_what == COMPLETION_CMDSENT) {
  400. host->status = "ok: command sent";
  401. goto close_transfer;
  402. }
  403. mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
  404. }
  405. if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
  406. if (cmd->flags & MMC_RSP_CRC) {
  407. if (host->mrq->cmd->flags & MMC_RSP_136) {
  408. dbg(host, dbg_irq,
  409. "fixup: ignore CRC fail with long rsp\n");
  410. } else {
  411. /* note, we used to fail the transfer
  412. * here, but it seems that this is just
  413. * the hardware getting it wrong.
  414. *
  415. * cmd->error = -EILSEQ;
  416. * host->status = "error: bad command crc";
  417. * goto fail_transfer;
  418. */
  419. }
  420. }
  421. mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
  422. }
  423. if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
  424. if (host->complete_what == COMPLETION_RSPFIN) {
  425. host->status = "ok: command response received";
  426. goto close_transfer;
  427. }
  428. if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
  429. host->complete_what = COMPLETION_XFERFINISH;
  430. mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
  431. }
  432. /* errors handled after this point are only relevant
  433. when a data transfer is in progress */
  434. if (!cmd->data)
  435. goto clear_status_bits;
  436. /* Check for FIFO failure */
  437. if (host->is2440) {
  438. if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
  439. dbg(host, dbg_err, "FIFO failure\n");
  440. host->mrq->data->error = -EILSEQ;
  441. host->status = "error: 2440 fifo failure";
  442. goto fail_transfer;
  443. }
  444. } else {
  445. if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
  446. dbg(host, dbg_err, "FIFO failure\n");
  447. cmd->data->error = -EILSEQ;
  448. host->status = "error: fifo failure";
  449. goto fail_transfer;
  450. }
  451. }
  452. if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
  453. dbg(host, dbg_err, "bad data crc (outgoing)\n");
  454. cmd->data->error = -EILSEQ;
  455. host->status = "error: bad data crc (outgoing)";
  456. goto fail_transfer;
  457. }
  458. if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
  459. dbg(host, dbg_err, "bad data crc (incoming)\n");
  460. cmd->data->error = -EILSEQ;
  461. host->status = "error: bad data crc (incoming)";
  462. goto fail_transfer;
  463. }
  464. if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
  465. dbg(host, dbg_err, "data timeout\n");
  466. cmd->data->error = -ETIMEDOUT;
  467. host->status = "error: data timeout";
  468. goto fail_transfer;
  469. }
  470. if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
  471. if (host->complete_what == COMPLETION_XFERFINISH) {
  472. host->status = "ok: data transfer completed";
  473. goto close_transfer;
  474. }
  475. if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
  476. host->complete_what = COMPLETION_RSPFIN;
  477. mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
  478. }
  479. clear_status_bits:
  480. writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
  481. writel(mci_dclear, host->base + S3C2410_SDIDSTA);
  482. goto irq_out;
  483. fail_transfer:
  484. host->pio_active = XFER_NONE;
  485. close_transfer:
  486. host->complete_what = COMPLETION_FINALIZE;
  487. clear_imask(host);
  488. tasklet_schedule(&host->pio_tasklet);
  489. goto irq_out;
  490. irq_out:
  491. dbg(host, dbg_irq,
  492. "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
  493. mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
  494. spin_unlock_irqrestore(&host->complete_lock, iflags);
  495. return IRQ_HANDLED;
  496. }
  497. /*
  498. * ISR for the CardDetect Pin
  499. */
  500. static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
  501. {
  502. struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
  503. dbg(host, dbg_irq, "card detect\n");
  504. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  505. return IRQ_HANDLED;
  506. }
  507. static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
  508. void *buf_id, int size,
  509. enum s3c2410_dma_buffresult result)
  510. {
  511. struct s3cmci_host *host = buf_id;
  512. unsigned long iflags;
  513. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
  514. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  515. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  516. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  517. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  518. BUG_ON(!host->mrq);
  519. BUG_ON(!host->mrq->data);
  520. BUG_ON(!host->dmatogo);
  521. spin_lock_irqsave(&host->complete_lock, iflags);
  522. if (result != S3C2410_RES_OK) {
  523. dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
  524. "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
  525. mci_csta, mci_dsta, mci_fsta,
  526. mci_dcnt, result, host->dmatogo);
  527. goto fail_request;
  528. }
  529. host->dmatogo--;
  530. if (host->dmatogo) {
  531. dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
  532. "DCNT:[%08x] toGo:%u\n",
  533. size, mci_dsta, mci_dcnt, host->dmatogo);
  534. goto out;
  535. }
  536. dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
  537. size, mci_dsta, mci_dcnt);
  538. host->complete_what = COMPLETION_FINALIZE;
  539. out:
  540. tasklet_schedule(&host->pio_tasklet);
  541. spin_unlock_irqrestore(&host->complete_lock, iflags);
  542. return;
  543. fail_request:
  544. host->mrq->data->error = -EINVAL;
  545. host->complete_what = COMPLETION_FINALIZE;
  546. writel(0, host->base + host->sdiimsk);
  547. goto out;
  548. }
  549. static void finalize_request(struct s3cmci_host *host)
  550. {
  551. struct mmc_request *mrq = host->mrq;
  552. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  553. int debug_as_failure = 0;
  554. if (host->complete_what != COMPLETION_FINALIZE)
  555. return;
  556. if (!mrq)
  557. return;
  558. if (cmd->data && (cmd->error == 0) &&
  559. (cmd->data->error == 0)) {
  560. if (host->dodma && (!host->dma_complete)) {
  561. dbg(host, dbg_dma, "DMA Missing!\n");
  562. return;
  563. }
  564. }
  565. /* Read response from controller. */
  566. cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
  567. cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
  568. cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
  569. cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
  570. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  571. if (cmd->error)
  572. debug_as_failure = 1;
  573. if (cmd->data && cmd->data->error)
  574. debug_as_failure = 1;
  575. dbg_dumpcmd(host, cmd, debug_as_failure);
  576. /* Cleanup controller */
  577. writel(0, host->base + S3C2410_SDICMDARG);
  578. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  579. writel(0, host->base + S3C2410_SDICMDCON);
  580. writel(0, host->base + host->sdiimsk);
  581. if (cmd->data && cmd->error)
  582. cmd->data->error = cmd->error;
  583. if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
  584. host->cmd_is_stop = 1;
  585. s3cmci_send_request(host->mmc);
  586. return;
  587. }
  588. /* If we have no data transfer we are finished here */
  589. if (!mrq->data)
  590. goto request_done;
  591. /* Calulate the amout of bytes transfer if there was no error */
  592. if (mrq->data->error == 0) {
  593. mrq->data->bytes_xfered =
  594. (mrq->data->blocks * mrq->data->blksz);
  595. } else {
  596. mrq->data->bytes_xfered = 0;
  597. }
  598. /* If we had an error while transfering data we flush the
  599. * DMA channel and the fifo to clear out any garbage. */
  600. if (mrq->data->error != 0) {
  601. if (host->dodma)
  602. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  603. if (host->is2440) {
  604. /* Clear failure register and reset fifo. */
  605. writel(S3C2440_SDIFSTA_FIFORESET |
  606. S3C2440_SDIFSTA_FIFOFAIL,
  607. host->base + S3C2410_SDIFSTA);
  608. } else {
  609. u32 mci_con;
  610. /* reset fifo */
  611. mci_con = readl(host->base + S3C2410_SDICON);
  612. mci_con |= S3C2410_SDICON_FIFORESET;
  613. writel(mci_con, host->base + S3C2410_SDICON);
  614. }
  615. }
  616. request_done:
  617. host->complete_what = COMPLETION_NONE;
  618. host->mrq = NULL;
  619. mmc_request_done(host->mmc, mrq);
  620. }
  621. static void s3cmci_dma_setup(struct s3cmci_host *host,
  622. enum s3c2410_dmasrc source)
  623. {
  624. static enum s3c2410_dmasrc last_source = -1;
  625. static int setup_ok;
  626. if (last_source == source)
  627. return;
  628. last_source = source;
  629. s3c2410_dma_devconfig(host->dma, source, 3,
  630. host->mem->start + host->sdidata);
  631. if (!setup_ok) {
  632. s3c2410_dma_config(host->dma, 4, 0);
  633. s3c2410_dma_set_buffdone_fn(host->dma,
  634. s3cmci_dma_done_callback);
  635. s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
  636. setup_ok = 1;
  637. }
  638. }
  639. static void s3cmci_send_command(struct s3cmci_host *host,
  640. struct mmc_command *cmd)
  641. {
  642. u32 ccon, imsk;
  643. imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
  644. S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
  645. S3C2410_SDIIMSK_RESPONSECRC;
  646. enable_imask(host, imsk);
  647. if (cmd->data)
  648. host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
  649. else if (cmd->flags & MMC_RSP_PRESENT)
  650. host->complete_what = COMPLETION_RSPFIN;
  651. else
  652. host->complete_what = COMPLETION_CMDSENT;
  653. writel(cmd->arg, host->base + S3C2410_SDICMDARG);
  654. ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
  655. ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
  656. if (cmd->flags & MMC_RSP_PRESENT)
  657. ccon |= S3C2410_SDICMDCON_WAITRSP;
  658. if (cmd->flags & MMC_RSP_136)
  659. ccon |= S3C2410_SDICMDCON_LONGRSP;
  660. writel(ccon, host->base + S3C2410_SDICMDCON);
  661. }
  662. static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
  663. {
  664. u32 dcon, imsk, stoptries = 3;
  665. /* write DCON register */
  666. if (!data) {
  667. writel(0, host->base + S3C2410_SDIDCON);
  668. return 0;
  669. }
  670. if ((data->blksz & 3) != 0) {
  671. /* We cannot deal with unaligned blocks with more than
  672. * one block being transfered. */
  673. if (data->blocks > 1) {
  674. pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
  675. return -EINVAL;
  676. }
  677. }
  678. while (readl(host->base + S3C2410_SDIDSTA) &
  679. (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
  680. dbg(host, dbg_err,
  681. "mci_setup_data() transfer stillin progress.\n");
  682. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  683. s3cmci_reset(host);
  684. if ((stoptries--) == 0) {
  685. dbg_dumpregs(host, "DRF");
  686. return -EINVAL;
  687. }
  688. }
  689. dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
  690. if (host->dodma)
  691. dcon |= S3C2410_SDIDCON_DMAEN;
  692. if (host->bus_width == MMC_BUS_WIDTH_4)
  693. dcon |= S3C2410_SDIDCON_WIDEBUS;
  694. if (!(data->flags & MMC_DATA_STREAM))
  695. dcon |= S3C2410_SDIDCON_BLOCKMODE;
  696. if (data->flags & MMC_DATA_WRITE) {
  697. dcon |= S3C2410_SDIDCON_TXAFTERRESP;
  698. dcon |= S3C2410_SDIDCON_XFER_TXSTART;
  699. }
  700. if (data->flags & MMC_DATA_READ) {
  701. dcon |= S3C2410_SDIDCON_RXAFTERCMD;
  702. dcon |= S3C2410_SDIDCON_XFER_RXSTART;
  703. }
  704. if (host->is2440) {
  705. dcon |= S3C2440_SDIDCON_DS_WORD;
  706. dcon |= S3C2440_SDIDCON_DATSTART;
  707. }
  708. writel(dcon, host->base + S3C2410_SDIDCON);
  709. /* write BSIZE register */
  710. writel(data->blksz, host->base + S3C2410_SDIBSIZE);
  711. /* add to IMASK register */
  712. imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
  713. S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
  714. enable_imask(host, imsk);
  715. /* write TIMER register */
  716. if (host->is2440) {
  717. writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
  718. } else {
  719. writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
  720. /* FIX: set slow clock to prevent timeouts on read */
  721. if (data->flags & MMC_DATA_READ)
  722. writel(0xFF, host->base + S3C2410_SDIPRE);
  723. }
  724. return 0;
  725. }
  726. #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
  727. static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
  728. {
  729. int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  730. BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
  731. host->pio_sgptr = 0;
  732. host->pio_bytes = 0;
  733. host->pio_count = 0;
  734. host->pio_active = rw ? XFER_WRITE : XFER_READ;
  735. if (rw) {
  736. do_pio_write(host);
  737. enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  738. } else {
  739. enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
  740. | S3C2410_SDIIMSK_RXFIFOLAST);
  741. }
  742. return 0;
  743. }
  744. static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
  745. {
  746. int dma_len, i;
  747. int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  748. BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
  749. s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
  750. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  751. dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  752. (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  753. if (dma_len == 0)
  754. return -ENOMEM;
  755. host->dma_complete = 0;
  756. host->dmatogo = dma_len;
  757. for (i = 0; i < dma_len; i++) {
  758. int res;
  759. dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
  760. sg_dma_address(&data->sg[i]),
  761. sg_dma_len(&data->sg[i]));
  762. res = s3c2410_dma_enqueue(host->dma, (void *) host,
  763. sg_dma_address(&data->sg[i]),
  764. sg_dma_len(&data->sg[i]));
  765. if (res) {
  766. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  767. return -EBUSY;
  768. }
  769. }
  770. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
  771. return 0;
  772. }
  773. static void s3cmci_send_request(struct mmc_host *mmc)
  774. {
  775. struct s3cmci_host *host = mmc_priv(mmc);
  776. struct mmc_request *mrq = host->mrq;
  777. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  778. host->ccnt++;
  779. prepare_dbgmsg(host, cmd, host->cmd_is_stop);
  780. /* Clear command, data and fifo status registers
  781. Fifo clear only necessary on 2440, but doesn't hurt on 2410
  782. */
  783. writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
  784. writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
  785. writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
  786. if (cmd->data) {
  787. int res = s3cmci_setup_data(host, cmd->data);
  788. host->dcnt++;
  789. if (res) {
  790. dbg(host, dbg_err, "setup data error %d\n", res);
  791. cmd->error = res;
  792. cmd->data->error = res;
  793. mmc_request_done(mmc, mrq);
  794. return;
  795. }
  796. if (host->dodma)
  797. res = s3cmci_prepare_dma(host, cmd->data);
  798. else
  799. res = s3cmci_prepare_pio(host, cmd->data);
  800. if (res) {
  801. dbg(host, dbg_err, "data prepare error %d\n", res);
  802. cmd->error = res;
  803. cmd->data->error = res;
  804. mmc_request_done(mmc, mrq);
  805. return;
  806. }
  807. }
  808. /* Send command */
  809. s3cmci_send_command(host, cmd);
  810. /* Enable Interrupt */
  811. enable_irq(host->irq);
  812. }
  813. static int s3cmci_card_present(struct mmc_host *mmc)
  814. {
  815. struct s3cmci_host *host = mmc_priv(mmc);
  816. struct s3c24xx_mci_pdata *pdata = host->pdata;
  817. int ret;
  818. if (pdata->gpio_detect == 0)
  819. return -ENOSYS;
  820. ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
  821. return ret ^ pdata->detect_invert;
  822. }
  823. static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  824. {
  825. struct s3cmci_host *host = mmc_priv(mmc);
  826. host->status = "mmc request";
  827. host->cmd_is_stop = 0;
  828. host->mrq = mrq;
  829. if (s3cmci_card_present(mmc) == 0) {
  830. dbg(host, dbg_err, "%s: no medium present\n", __func__);
  831. host->mrq->cmd->error = -ENOMEDIUM;
  832. mmc_request_done(mmc, mrq);
  833. } else
  834. s3cmci_send_request(mmc);
  835. }
  836. static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
  837. {
  838. u32 mci_psc;
  839. /* Set clock */
  840. for (mci_psc = 0; mci_psc < 255; mci_psc++) {
  841. host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
  842. if (host->real_rate <= ios->clock)
  843. break;
  844. }
  845. if (mci_psc > 255)
  846. mci_psc = 255;
  847. host->prescaler = mci_psc;
  848. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  849. /* If requested clock is 0, real_rate will be 0, too */
  850. if (ios->clock == 0)
  851. host->real_rate = 0;
  852. }
  853. static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  854. {
  855. struct s3cmci_host *host = mmc_priv(mmc);
  856. u32 mci_con;
  857. /* Set the power state */
  858. mci_con = readl(host->base + S3C2410_SDICON);
  859. switch (ios->power_mode) {
  860. case MMC_POWER_ON:
  861. case MMC_POWER_UP:
  862. s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
  863. s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
  864. s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
  865. s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
  866. s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
  867. s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
  868. if (host->pdata->set_power)
  869. host->pdata->set_power(ios->power_mode, ios->vdd);
  870. if (!host->is2440)
  871. mci_con |= S3C2410_SDICON_FIFORESET;
  872. break;
  873. case MMC_POWER_OFF:
  874. default:
  875. s3c2410_gpio_setpin(S3C2410_GPE5, 0);
  876. s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);
  877. if (host->is2440)
  878. mci_con |= S3C2440_SDICON_SDRESET;
  879. if (host->pdata->set_power)
  880. host->pdata->set_power(ios->power_mode, ios->vdd);
  881. break;
  882. }
  883. s3cmci_set_clk(host, ios);
  884. /* Set CLOCK_ENABLE */
  885. if (ios->clock)
  886. mci_con |= S3C2410_SDICON_CLOCKTYPE;
  887. else
  888. mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
  889. writel(mci_con, host->base + S3C2410_SDICON);
  890. if ((ios->power_mode == MMC_POWER_ON) ||
  891. (ios->power_mode == MMC_POWER_UP)) {
  892. dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
  893. host->real_rate/1000, ios->clock/1000);
  894. } else {
  895. dbg(host, dbg_conf, "powered down.\n");
  896. }
  897. host->bus_width = ios->bus_width;
  898. }
  899. static void s3cmci_reset(struct s3cmci_host *host)
  900. {
  901. u32 con = readl(host->base + S3C2410_SDICON);
  902. con |= S3C2440_SDICON_SDRESET;
  903. writel(con, host->base + S3C2410_SDICON);
  904. }
  905. static int s3cmci_get_ro(struct mmc_host *mmc)
  906. {
  907. struct s3cmci_host *host = mmc_priv(mmc);
  908. struct s3c24xx_mci_pdata *pdata = host->pdata;
  909. int ret;
  910. if (pdata->gpio_wprotect == 0)
  911. return 0;
  912. ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
  913. if (pdata->wprotect_invert)
  914. ret = !ret;
  915. return ret;
  916. }
  917. static struct mmc_host_ops s3cmci_ops = {
  918. .request = s3cmci_request,
  919. .set_ios = s3cmci_set_ios,
  920. .get_ro = s3cmci_get_ro,
  921. .get_cd = s3cmci_card_present,
  922. };
  923. static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
  924. /* This is currently here to avoid a number of if (host->pdata)
  925. * checks. Any zero fields to ensure reaonable defaults are picked. */
  926. };
  927. #ifdef CONFIG_CPU_FREQ
  928. static int s3cmci_cpufreq_transition(struct notifier_block *nb,
  929. unsigned long val, void *data)
  930. {
  931. struct s3cmci_host *host;
  932. struct mmc_host *mmc;
  933. unsigned long newclk;
  934. unsigned long flags;
  935. host = container_of(nb, struct s3cmci_host, freq_transition);
  936. newclk = clk_get_rate(host->clk);
  937. mmc = host->mmc;
  938. if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
  939. (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
  940. spin_lock_irqsave(&mmc->lock, flags);
  941. host->clk_rate = newclk;
  942. if (mmc->ios.power_mode != MMC_POWER_OFF &&
  943. mmc->ios.clock != 0)
  944. s3cmci_set_clk(host, &mmc->ios);
  945. spin_unlock_irqrestore(&mmc->lock, flags);
  946. }
  947. return 0;
  948. }
  949. static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
  950. {
  951. host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
  952. return cpufreq_register_notifier(&host->freq_transition,
  953. CPUFREQ_TRANSITION_NOTIFIER);
  954. }
  955. static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
  956. {
  957. cpufreq_unregister_notifier(&host->freq_transition,
  958. CPUFREQ_TRANSITION_NOTIFIER);
  959. }
  960. #else
  961. static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
  962. {
  963. return 0;
  964. }
  965. static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
  966. {
  967. }
  968. #endif
  969. static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
  970. {
  971. struct s3cmci_host *host;
  972. struct mmc_host *mmc;
  973. int ret;
  974. mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
  975. if (!mmc) {
  976. ret = -ENOMEM;
  977. goto probe_out;
  978. }
  979. host = mmc_priv(mmc);
  980. host->mmc = mmc;
  981. host->pdev = pdev;
  982. host->is2440 = is2440;
  983. host->pdata = pdev->dev.platform_data;
  984. if (!host->pdata) {
  985. pdev->dev.platform_data = &s3cmci_def_pdata;
  986. host->pdata = &s3cmci_def_pdata;
  987. }
  988. spin_lock_init(&host->complete_lock);
  989. tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
  990. if (is2440) {
  991. host->sdiimsk = S3C2440_SDIIMSK;
  992. host->sdidata = S3C2440_SDIDATA;
  993. host->clk_div = 1;
  994. } else {
  995. host->sdiimsk = S3C2410_SDIIMSK;
  996. host->sdidata = S3C2410_SDIDATA;
  997. host->clk_div = 2;
  998. }
  999. host->dodma = 0;
  1000. host->complete_what = COMPLETION_NONE;
  1001. host->pio_active = XFER_NONE;
  1002. host->dma = S3CMCI_DMA;
  1003. host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1004. if (!host->mem) {
  1005. dev_err(&pdev->dev,
  1006. "failed to get io memory region resouce.\n");
  1007. ret = -ENOENT;
  1008. goto probe_free_host;
  1009. }
  1010. host->mem = request_mem_region(host->mem->start,
  1011. RESSIZE(host->mem), pdev->name);
  1012. if (!host->mem) {
  1013. dev_err(&pdev->dev, "failed to request io memory region.\n");
  1014. ret = -ENOENT;
  1015. goto probe_free_host;
  1016. }
  1017. host->base = ioremap(host->mem->start, RESSIZE(host->mem));
  1018. if (!host->base) {
  1019. dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
  1020. ret = -EINVAL;
  1021. goto probe_free_mem_region;
  1022. }
  1023. host->irq = platform_get_irq(pdev, 0);
  1024. if (host->irq == 0) {
  1025. dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
  1026. ret = -EINVAL;
  1027. goto probe_iounmap;
  1028. }
  1029. if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
  1030. dev_err(&pdev->dev, "failed to request mci interrupt.\n");
  1031. ret = -ENOENT;
  1032. goto probe_iounmap;
  1033. }
  1034. /* We get spurious interrupts even when we have set the IMSK
  1035. * register to ignore everything, so use disable_irq() to make
  1036. * ensure we don't lock the system with un-serviceable requests. */
  1037. disable_irq(host->irq);
  1038. host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
  1039. if (host->irq_cd >= 0) {
  1040. if (request_irq(host->irq_cd, s3cmci_irq_cd,
  1041. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1042. DRIVER_NAME, host)) {
  1043. dev_err(&pdev->dev, "can't get card detect irq.\n");
  1044. ret = -ENOENT;
  1045. goto probe_free_irq;
  1046. }
  1047. } else {
  1048. dev_warn(&pdev->dev, "host detect has no irq available\n");
  1049. s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
  1050. S3C2410_GPIO_INPUT);
  1051. }
  1052. if (host->pdata->gpio_wprotect)
  1053. s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
  1054. S3C2410_GPIO_INPUT);
  1055. if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
  1056. dev_err(&pdev->dev, "unable to get DMA channel.\n");
  1057. ret = -EBUSY;
  1058. goto probe_free_irq_cd;
  1059. }
  1060. host->clk = clk_get(&pdev->dev, "sdi");
  1061. if (IS_ERR(host->clk)) {
  1062. dev_err(&pdev->dev, "failed to find clock source.\n");
  1063. ret = PTR_ERR(host->clk);
  1064. host->clk = NULL;
  1065. goto probe_free_host;
  1066. }
  1067. ret = clk_enable(host->clk);
  1068. if (ret) {
  1069. dev_err(&pdev->dev, "failed to enable clock source.\n");
  1070. goto clk_free;
  1071. }
  1072. host->clk_rate = clk_get_rate(host->clk);
  1073. mmc->ops = &s3cmci_ops;
  1074. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1075. mmc->caps = MMC_CAP_4_BIT_DATA;
  1076. mmc->f_min = host->clk_rate / (host->clk_div * 256);
  1077. mmc->f_max = host->clk_rate / host->clk_div;
  1078. if (host->pdata->ocr_avail)
  1079. mmc->ocr_avail = host->pdata->ocr_avail;
  1080. mmc->max_blk_count = 4095;
  1081. mmc->max_blk_size = 4095;
  1082. mmc->max_req_size = 4095 * 512;
  1083. mmc->max_seg_size = mmc->max_req_size;
  1084. mmc->max_phys_segs = 128;
  1085. mmc->max_hw_segs = 128;
  1086. dbg(host, dbg_debug,
  1087. "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
  1088. (host->is2440?"2440":""),
  1089. host->base, host->irq, host->irq_cd, host->dma);
  1090. ret = s3cmci_cpufreq_register(host);
  1091. if (ret) {
  1092. dev_err(&pdev->dev, "failed to register cpufreq\n");
  1093. goto free_dmabuf;
  1094. }
  1095. ret = mmc_add_host(mmc);
  1096. if (ret) {
  1097. dev_err(&pdev->dev, "failed to add mmc host.\n");
  1098. goto free_cpufreq;
  1099. }
  1100. platform_set_drvdata(pdev, mmc);
  1101. dev_info(&pdev->dev, "initialisation done.\n");
  1102. return 0;
  1103. free_cpufreq:
  1104. s3cmci_cpufreq_deregister(host);
  1105. free_dmabuf:
  1106. clk_disable(host->clk);
  1107. clk_free:
  1108. clk_put(host->clk);
  1109. probe_free_irq_cd:
  1110. if (host->irq_cd >= 0)
  1111. free_irq(host->irq_cd, host);
  1112. probe_free_irq:
  1113. free_irq(host->irq, host);
  1114. probe_iounmap:
  1115. iounmap(host->base);
  1116. probe_free_mem_region:
  1117. release_mem_region(host->mem->start, RESSIZE(host->mem));
  1118. probe_free_host:
  1119. mmc_free_host(mmc);
  1120. probe_out:
  1121. return ret;
  1122. }
  1123. static void s3cmci_shutdown(struct platform_device *pdev)
  1124. {
  1125. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1126. struct s3cmci_host *host = mmc_priv(mmc);
  1127. if (host->irq_cd >= 0)
  1128. free_irq(host->irq_cd, host);
  1129. s3cmci_cpufreq_deregister(host);
  1130. mmc_remove_host(mmc);
  1131. clk_disable(host->clk);
  1132. }
  1133. static int __devexit s3cmci_remove(struct platform_device *pdev)
  1134. {
  1135. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1136. struct s3cmci_host *host = mmc_priv(mmc);
  1137. s3cmci_shutdown(pdev);
  1138. clk_put(host->clk);
  1139. tasklet_disable(&host->pio_tasklet);
  1140. s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
  1141. free_irq(host->irq, host);
  1142. iounmap(host->base);
  1143. release_mem_region(host->mem->start, RESSIZE(host->mem));
  1144. mmc_free_host(mmc);
  1145. return 0;
  1146. }
  1147. static int __devinit s3cmci_2410_probe(struct platform_device *dev)
  1148. {
  1149. return s3cmci_probe(dev, 0);
  1150. }
  1151. static int __devinit s3cmci_2412_probe(struct platform_device *dev)
  1152. {
  1153. return s3cmci_probe(dev, 1);
  1154. }
  1155. static int __devinit s3cmci_2440_probe(struct platform_device *dev)
  1156. {
  1157. return s3cmci_probe(dev, 1);
  1158. }
  1159. #ifdef CONFIG_PM
  1160. static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
  1161. {
  1162. struct mmc_host *mmc = platform_get_drvdata(dev);
  1163. return mmc_suspend_host(mmc, state);
  1164. }
  1165. static int s3cmci_resume(struct platform_device *dev)
  1166. {
  1167. struct mmc_host *mmc = platform_get_drvdata(dev);
  1168. return mmc_resume_host(mmc);
  1169. }
  1170. #else /* CONFIG_PM */
  1171. #define s3cmci_suspend NULL
  1172. #define s3cmci_resume NULL
  1173. #endif /* CONFIG_PM */
  1174. static struct platform_driver s3cmci_2410_driver = {
  1175. .driver.name = "s3c2410-sdi",
  1176. .driver.owner = THIS_MODULE,
  1177. .probe = s3cmci_2410_probe,
  1178. .remove = __devexit_p(s3cmci_remove),
  1179. .shutdown = s3cmci_shutdown,
  1180. .suspend = s3cmci_suspend,
  1181. .resume = s3cmci_resume,
  1182. };
  1183. static struct platform_driver s3cmci_2412_driver = {
  1184. .driver.name = "s3c2412-sdi",
  1185. .driver.owner = THIS_MODULE,
  1186. .probe = s3cmci_2412_probe,
  1187. .remove = __devexit_p(s3cmci_remove),
  1188. .shutdown = s3cmci_shutdown,
  1189. .suspend = s3cmci_suspend,
  1190. .resume = s3cmci_resume,
  1191. };
  1192. static struct platform_driver s3cmci_2440_driver = {
  1193. .driver.name = "s3c2440-sdi",
  1194. .driver.owner = THIS_MODULE,
  1195. .probe = s3cmci_2440_probe,
  1196. .remove = __devexit_p(s3cmci_remove),
  1197. .shutdown = s3cmci_shutdown,
  1198. .suspend = s3cmci_suspend,
  1199. .resume = s3cmci_resume,
  1200. };
  1201. static int __init s3cmci_init(void)
  1202. {
  1203. platform_driver_register(&s3cmci_2410_driver);
  1204. platform_driver_register(&s3cmci_2412_driver);
  1205. platform_driver_register(&s3cmci_2440_driver);
  1206. return 0;
  1207. }
  1208. static void __exit s3cmci_exit(void)
  1209. {
  1210. platform_driver_unregister(&s3cmci_2410_driver);
  1211. platform_driver_unregister(&s3cmci_2412_driver);
  1212. platform_driver_unregister(&s3cmci_2440_driver);
  1213. }
  1214. module_init(s3cmci_init);
  1215. module_exit(s3cmci_exit);
  1216. MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
  1217. MODULE_LICENSE("GPL v2");
  1218. MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");
  1219. MODULE_ALIAS("platform:s3c2410-sdi");
  1220. MODULE_ALIAS("platform:s3c2412-sdi");
  1221. MODULE_ALIAS("platform:s3c2440-sdi");