s3cmci.c 37 KB

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