s3cmci.c 35 KB

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