s3cmci.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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 <mach/regs-sdi.h>
  19. #include <mach/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. static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
  473. void *buf_id, int size,
  474. enum s3c2410_dma_buffresult result)
  475. {
  476. struct s3cmci_host *host = buf_id;
  477. unsigned long iflags;
  478. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
  479. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  480. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  481. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  482. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  483. BUG_ON(!host->mrq);
  484. BUG_ON(!host->mrq->data);
  485. BUG_ON(!host->dmatogo);
  486. spin_lock_irqsave(&host->complete_lock, iflags);
  487. if (result != S3C2410_RES_OK) {
  488. dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
  489. "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
  490. mci_csta, mci_dsta, mci_fsta,
  491. mci_dcnt, result, host->dmatogo);
  492. goto fail_request;
  493. }
  494. host->dmatogo--;
  495. if (host->dmatogo) {
  496. dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
  497. "DCNT:[%08x] toGo:%u\n",
  498. size, mci_dsta, mci_dcnt, host->dmatogo);
  499. goto out;
  500. }
  501. dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
  502. size, mci_dsta, mci_dcnt);
  503. host->complete_what = COMPLETION_FINALIZE;
  504. out:
  505. tasklet_schedule(&host->pio_tasklet);
  506. spin_unlock_irqrestore(&host->complete_lock, iflags);
  507. return;
  508. fail_request:
  509. host->mrq->data->error = -EINVAL;
  510. host->complete_what = COMPLETION_FINALIZE;
  511. writel(0, host->base + host->sdiimsk);
  512. goto out;
  513. }
  514. static void finalize_request(struct s3cmci_host *host)
  515. {
  516. struct mmc_request *mrq = host->mrq;
  517. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  518. int debug_as_failure = 0;
  519. if (host->complete_what != COMPLETION_FINALIZE)
  520. return;
  521. if (!mrq)
  522. return;
  523. if (cmd->data && (cmd->error == 0) &&
  524. (cmd->data->error == 0)) {
  525. if (host->dodma && (!host->dma_complete)) {
  526. dbg(host, dbg_dma, "DMA Missing!\n");
  527. return;
  528. }
  529. }
  530. /* Read response from controller. */
  531. cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
  532. cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
  533. cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
  534. cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
  535. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  536. if (cmd->error)
  537. debug_as_failure = 1;
  538. if (cmd->data && cmd->data->error)
  539. debug_as_failure = 1;
  540. dbg_dumpcmd(host, cmd, debug_as_failure);
  541. /* Cleanup controller */
  542. writel(0, host->base + S3C2410_SDICMDARG);
  543. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  544. writel(0, host->base + S3C2410_SDICMDCON);
  545. writel(0, host->base + host->sdiimsk);
  546. if (cmd->data && cmd->error)
  547. cmd->data->error = cmd->error;
  548. if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
  549. host->cmd_is_stop = 1;
  550. s3cmci_send_request(host->mmc);
  551. return;
  552. }
  553. /* If we have no data transfer we are finished here */
  554. if (!mrq->data)
  555. goto request_done;
  556. /* Calulate the amout of bytes transfer if there was no error */
  557. if (mrq->data->error == 0) {
  558. mrq->data->bytes_xfered =
  559. (mrq->data->blocks * mrq->data->blksz);
  560. } else {
  561. mrq->data->bytes_xfered = 0;
  562. }
  563. /* If we had an error while transfering data we flush the
  564. * DMA channel and the fifo to clear out any garbage. */
  565. if (mrq->data->error != 0) {
  566. if (host->dodma)
  567. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  568. if (host->is2440) {
  569. /* Clear failure register and reset fifo. */
  570. writel(S3C2440_SDIFSTA_FIFORESET |
  571. S3C2440_SDIFSTA_FIFOFAIL,
  572. host->base + S3C2410_SDIFSTA);
  573. } else {
  574. u32 mci_con;
  575. /* reset fifo */
  576. mci_con = readl(host->base + S3C2410_SDICON);
  577. mci_con |= S3C2410_SDICON_FIFORESET;
  578. writel(mci_con, host->base + S3C2410_SDICON);
  579. }
  580. }
  581. request_done:
  582. host->complete_what = COMPLETION_NONE;
  583. host->mrq = NULL;
  584. mmc_request_done(host->mmc, mrq);
  585. }
  586. static void s3cmci_dma_setup(struct s3cmci_host *host,
  587. enum s3c2410_dmasrc source)
  588. {
  589. static enum s3c2410_dmasrc last_source = -1;
  590. static int setup_ok;
  591. if (last_source == source)
  592. return;
  593. last_source = source;
  594. s3c2410_dma_devconfig(host->dma, source, 3,
  595. host->mem->start + host->sdidata);
  596. if (!setup_ok) {
  597. s3c2410_dma_config(host->dma, 4,
  598. (S3C2410_DCON_HWTRIG | S3C2410_DCON_CH0_SDI));
  599. s3c2410_dma_set_buffdone_fn(host->dma,
  600. s3cmci_dma_done_callback);
  601. s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
  602. setup_ok = 1;
  603. }
  604. }
  605. static void s3cmci_send_command(struct s3cmci_host *host,
  606. struct mmc_command *cmd)
  607. {
  608. u32 ccon, imsk;
  609. imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
  610. S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
  611. S3C2410_SDIIMSK_RESPONSECRC;
  612. enable_imask(host, imsk);
  613. if (cmd->data)
  614. host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
  615. else if (cmd->flags & MMC_RSP_PRESENT)
  616. host->complete_what = COMPLETION_RSPFIN;
  617. else
  618. host->complete_what = COMPLETION_CMDSENT;
  619. writel(cmd->arg, host->base + S3C2410_SDICMDARG);
  620. ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
  621. ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
  622. if (cmd->flags & MMC_RSP_PRESENT)
  623. ccon |= S3C2410_SDICMDCON_WAITRSP;
  624. if (cmd->flags & MMC_RSP_136)
  625. ccon |= S3C2410_SDICMDCON_LONGRSP;
  626. writel(ccon, host->base + S3C2410_SDICMDCON);
  627. }
  628. static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
  629. {
  630. u32 dcon, imsk, stoptries = 3;
  631. /* write DCON register */
  632. if (!data) {
  633. writel(0, host->base + S3C2410_SDIDCON);
  634. return 0;
  635. }
  636. if ((data->blksz & 3) != 0) {
  637. /* We cannot deal with unaligned blocks with more than
  638. * one block being transfered. */
  639. if (data->blocks > 1)
  640. return -EINVAL;
  641. /* No support yet for non-word block transfers. */
  642. return -EINVAL;
  643. }
  644. while (readl(host->base + S3C2410_SDIDSTA) &
  645. (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
  646. dbg(host, dbg_err,
  647. "mci_setup_data() transfer stillin progress.\n");
  648. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  649. s3cmci_reset(host);
  650. if ((stoptries--) == 0) {
  651. dbg_dumpregs(host, "DRF");
  652. return -EINVAL;
  653. }
  654. }
  655. dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
  656. if (host->dodma)
  657. dcon |= S3C2410_SDIDCON_DMAEN;
  658. if (host->bus_width == MMC_BUS_WIDTH_4)
  659. dcon |= S3C2410_SDIDCON_WIDEBUS;
  660. if (!(data->flags & MMC_DATA_STREAM))
  661. dcon |= S3C2410_SDIDCON_BLOCKMODE;
  662. if (data->flags & MMC_DATA_WRITE) {
  663. dcon |= S3C2410_SDIDCON_TXAFTERRESP;
  664. dcon |= S3C2410_SDIDCON_XFER_TXSTART;
  665. }
  666. if (data->flags & MMC_DATA_READ) {
  667. dcon |= S3C2410_SDIDCON_RXAFTERCMD;
  668. dcon |= S3C2410_SDIDCON_XFER_RXSTART;
  669. }
  670. if (host->is2440) {
  671. dcon |= S3C2440_SDIDCON_DS_WORD;
  672. dcon |= S3C2440_SDIDCON_DATSTART;
  673. }
  674. writel(dcon, host->base + S3C2410_SDIDCON);
  675. /* write BSIZE register */
  676. writel(data->blksz, host->base + S3C2410_SDIBSIZE);
  677. /* add to IMASK register */
  678. imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
  679. S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
  680. enable_imask(host, imsk);
  681. /* write TIMER register */
  682. if (host->is2440) {
  683. writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
  684. } else {
  685. writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
  686. /* FIX: set slow clock to prevent timeouts on read */
  687. if (data->flags & MMC_DATA_READ)
  688. writel(0xFF, host->base + S3C2410_SDIPRE);
  689. }
  690. return 0;
  691. }
  692. #define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
  693. static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
  694. {
  695. int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  696. BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
  697. host->pio_sgptr = 0;
  698. host->pio_words = 0;
  699. host->pio_count = 0;
  700. host->pio_active = rw ? XFER_WRITE : XFER_READ;
  701. if (rw) {
  702. do_pio_write(host);
  703. enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  704. } else {
  705. enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
  706. | S3C2410_SDIIMSK_RXFIFOLAST);
  707. }
  708. return 0;
  709. }
  710. static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
  711. {
  712. int dma_len, i;
  713. int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  714. BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
  715. s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
  716. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  717. dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  718. (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  719. if (dma_len == 0)
  720. return -ENOMEM;
  721. host->dma_complete = 0;
  722. host->dmatogo = dma_len;
  723. for (i = 0; i < dma_len; i++) {
  724. int res;
  725. dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
  726. sg_dma_address(&data->sg[i]),
  727. sg_dma_len(&data->sg[i]));
  728. res = s3c2410_dma_enqueue(host->dma, (void *) host,
  729. sg_dma_address(&data->sg[i]),
  730. sg_dma_len(&data->sg[i]));
  731. if (res) {
  732. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  733. return -EBUSY;
  734. }
  735. }
  736. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
  737. return 0;
  738. }
  739. static void s3cmci_send_request(struct mmc_host *mmc)
  740. {
  741. struct s3cmci_host *host = mmc_priv(mmc);
  742. struct mmc_request *mrq = host->mrq;
  743. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  744. host->ccnt++;
  745. prepare_dbgmsg(host, cmd, host->cmd_is_stop);
  746. /* Clear command, data and fifo status registers
  747. Fifo clear only necessary on 2440, but doesn't hurt on 2410
  748. */
  749. writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
  750. writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
  751. writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
  752. if (cmd->data) {
  753. int res = s3cmci_setup_data(host, cmd->data);
  754. host->dcnt++;
  755. if (res) {
  756. dbg(host, dbg_err, "setup data error %d\n", res);
  757. cmd->error = res;
  758. cmd->data->error = res;
  759. mmc_request_done(mmc, mrq);
  760. return;
  761. }
  762. if (host->dodma)
  763. res = s3cmci_prepare_dma(host, cmd->data);
  764. else
  765. res = s3cmci_prepare_pio(host, cmd->data);
  766. if (res) {
  767. dbg(host, dbg_err, "data prepare error %d\n", res);
  768. cmd->error = res;
  769. cmd->data->error = res;
  770. mmc_request_done(mmc, mrq);
  771. return;
  772. }
  773. }
  774. /* Send command */
  775. s3cmci_send_command(host, cmd);
  776. /* Enable Interrupt */
  777. enable_irq(host->irq);
  778. }
  779. static int s3cmci_card_present(struct mmc_host *mmc)
  780. {
  781. struct s3cmci_host *host = mmc_priv(mmc);
  782. struct s3c24xx_mci_pdata *pdata = host->pdata;
  783. int ret;
  784. if (pdata->gpio_detect == 0)
  785. return -ENOSYS;
  786. ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
  787. return ret ^ pdata->detect_invert;
  788. }
  789. static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  790. {
  791. struct s3cmci_host *host = mmc_priv(mmc);
  792. host->status = "mmc request";
  793. host->cmd_is_stop = 0;
  794. host->mrq = mrq;
  795. if (s3cmci_card_present(mmc) == 0) {
  796. dbg(host, dbg_err, "%s: no medium present\n", __func__);
  797. host->mrq->cmd->error = -ENOMEDIUM;
  798. mmc_request_done(mmc, mrq);
  799. } else
  800. s3cmci_send_request(mmc);
  801. }
  802. static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  803. {
  804. struct s3cmci_host *host = mmc_priv(mmc);
  805. u32 mci_psc, mci_con;
  806. /* Set the power state */
  807. mci_con = readl(host->base + S3C2410_SDICON);
  808. switch (ios->power_mode) {
  809. case MMC_POWER_ON:
  810. case MMC_POWER_UP:
  811. s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
  812. s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
  813. s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
  814. s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
  815. s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
  816. s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
  817. if (host->pdata->set_power)
  818. host->pdata->set_power(ios->power_mode, ios->vdd);
  819. if (!host->is2440)
  820. mci_con |= S3C2410_SDICON_FIFORESET;
  821. break;
  822. case MMC_POWER_OFF:
  823. default:
  824. s3c2410_gpio_setpin(S3C2410_GPE5, 0);
  825. s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);
  826. if (host->is2440)
  827. mci_con |= S3C2440_SDICON_SDRESET;
  828. if (host->pdata->set_power)
  829. host->pdata->set_power(ios->power_mode, ios->vdd);
  830. break;
  831. }
  832. /* Set clock */
  833. for (mci_psc = 0; mci_psc < 255; mci_psc++) {
  834. host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
  835. if (host->real_rate <= ios->clock)
  836. break;
  837. }
  838. if (mci_psc > 255)
  839. mci_psc = 255;
  840. host->prescaler = mci_psc;
  841. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  842. /* If requested clock is 0, real_rate will be 0, too */
  843. if (ios->clock == 0)
  844. host->real_rate = 0;
  845. /* Set CLOCK_ENABLE */
  846. if (ios->clock)
  847. mci_con |= S3C2410_SDICON_CLOCKTYPE;
  848. else
  849. mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
  850. writel(mci_con, host->base + S3C2410_SDICON);
  851. if ((ios->power_mode == MMC_POWER_ON) ||
  852. (ios->power_mode == MMC_POWER_UP)) {
  853. dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
  854. host->real_rate/1000, ios->clock/1000);
  855. } else {
  856. dbg(host, dbg_conf, "powered down.\n");
  857. }
  858. host->bus_width = ios->bus_width;
  859. }
  860. static void s3cmci_reset(struct s3cmci_host *host)
  861. {
  862. u32 con = readl(host->base + S3C2410_SDICON);
  863. con |= S3C2440_SDICON_SDRESET;
  864. writel(con, host->base + S3C2410_SDICON);
  865. }
  866. static int s3cmci_get_ro(struct mmc_host *mmc)
  867. {
  868. struct s3cmci_host *host = mmc_priv(mmc);
  869. struct s3c24xx_mci_pdata *pdata = host->pdata;
  870. int ret;
  871. if (pdata->gpio_wprotect == 0)
  872. return 0;
  873. ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
  874. if (pdata->wprotect_invert)
  875. ret = !ret;
  876. return ret;
  877. }
  878. static struct mmc_host_ops s3cmci_ops = {
  879. .request = s3cmci_request,
  880. .set_ios = s3cmci_set_ios,
  881. .get_ro = s3cmci_get_ro,
  882. .get_cd = s3cmci_card_present,
  883. };
  884. static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
  885. /* This is currently here to avoid a number of if (host->pdata)
  886. * checks. Any zero fields to ensure reaonable defaults are picked. */
  887. };
  888. static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
  889. {
  890. struct s3cmci_host *host;
  891. struct mmc_host *mmc;
  892. int ret;
  893. mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
  894. if (!mmc) {
  895. ret = -ENOMEM;
  896. goto probe_out;
  897. }
  898. host = mmc_priv(mmc);
  899. host->mmc = mmc;
  900. host->pdev = pdev;
  901. host->is2440 = is2440;
  902. host->pdata = pdev->dev.platform_data;
  903. if (!host->pdata) {
  904. pdev->dev.platform_data = &s3cmci_def_pdata;
  905. host->pdata = &s3cmci_def_pdata;
  906. }
  907. spin_lock_init(&host->complete_lock);
  908. tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
  909. if (is2440) {
  910. host->sdiimsk = S3C2440_SDIIMSK;
  911. host->sdidata = S3C2440_SDIDATA;
  912. host->clk_div = 1;
  913. } else {
  914. host->sdiimsk = S3C2410_SDIIMSK;
  915. host->sdidata = S3C2410_SDIDATA;
  916. host->clk_div = 2;
  917. }
  918. host->dodma = 0;
  919. host->complete_what = COMPLETION_NONE;
  920. host->pio_active = XFER_NONE;
  921. host->dma = S3CMCI_DMA;
  922. host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  923. if (!host->mem) {
  924. dev_err(&pdev->dev,
  925. "failed to get io memory region resouce.\n");
  926. ret = -ENOENT;
  927. goto probe_free_host;
  928. }
  929. host->mem = request_mem_region(host->mem->start,
  930. RESSIZE(host->mem), pdev->name);
  931. if (!host->mem) {
  932. dev_err(&pdev->dev, "failed to request io memory region.\n");
  933. ret = -ENOENT;
  934. goto probe_free_host;
  935. }
  936. host->base = ioremap(host->mem->start, RESSIZE(host->mem));
  937. if (!host->base) {
  938. dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
  939. ret = -EINVAL;
  940. goto probe_free_mem_region;
  941. }
  942. host->irq = platform_get_irq(pdev, 0);
  943. if (host->irq == 0) {
  944. dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
  945. ret = -EINVAL;
  946. goto probe_iounmap;
  947. }
  948. if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
  949. dev_err(&pdev->dev, "failed to request mci interrupt.\n");
  950. ret = -ENOENT;
  951. goto probe_iounmap;
  952. }
  953. /* We get spurious interrupts even when we have set the IMSK
  954. * register to ignore everything, so use disable_irq() to make
  955. * ensure we don't lock the system with un-serviceable requests. */
  956. disable_irq(host->irq);
  957. host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
  958. if (host->irq_cd >= 0) {
  959. if (request_irq(host->irq_cd, s3cmci_irq_cd,
  960. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  961. DRIVER_NAME, host)) {
  962. dev_err(&pdev->dev, "can't get card detect irq.\n");
  963. ret = -ENOENT;
  964. goto probe_free_irq;
  965. }
  966. } else {
  967. dev_warn(&pdev->dev, "host detect has no irq available\n");
  968. s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
  969. S3C2410_GPIO_INPUT);
  970. }
  971. if (host->pdata->gpio_wprotect)
  972. s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
  973. S3C2410_GPIO_INPUT);
  974. if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
  975. dev_err(&pdev->dev, "unable to get DMA channel.\n");
  976. ret = -EBUSY;
  977. goto probe_free_irq_cd;
  978. }
  979. host->clk = clk_get(&pdev->dev, "sdi");
  980. if (IS_ERR(host->clk)) {
  981. dev_err(&pdev->dev, "failed to find clock source.\n");
  982. ret = PTR_ERR(host->clk);
  983. host->clk = NULL;
  984. goto probe_free_host;
  985. }
  986. ret = clk_enable(host->clk);
  987. if (ret) {
  988. dev_err(&pdev->dev, "failed to enable clock source.\n");
  989. goto clk_free;
  990. }
  991. host->clk_rate = clk_get_rate(host->clk);
  992. mmc->ops = &s3cmci_ops;
  993. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  994. mmc->caps = MMC_CAP_4_BIT_DATA;
  995. mmc->f_min = host->clk_rate / (host->clk_div * 256);
  996. mmc->f_max = host->clk_rate / host->clk_div;
  997. if (host->pdata->ocr_avail)
  998. mmc->ocr_avail = host->pdata->ocr_avail;
  999. mmc->max_blk_count = 4095;
  1000. mmc->max_blk_size = 4095;
  1001. mmc->max_req_size = 4095 * 512;
  1002. mmc->max_seg_size = mmc->max_req_size;
  1003. mmc->max_phys_segs = 128;
  1004. mmc->max_hw_segs = 128;
  1005. dbg(host, dbg_debug,
  1006. "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
  1007. (host->is2440?"2440":""),
  1008. host->base, host->irq, host->irq_cd, host->dma);
  1009. ret = mmc_add_host(mmc);
  1010. if (ret) {
  1011. dev_err(&pdev->dev, "failed to add mmc host.\n");
  1012. goto free_dmabuf;
  1013. }
  1014. platform_set_drvdata(pdev, mmc);
  1015. dev_info(&pdev->dev, "initialisation done.\n");
  1016. return 0;
  1017. free_dmabuf:
  1018. clk_disable(host->clk);
  1019. clk_free:
  1020. clk_put(host->clk);
  1021. probe_free_irq_cd:
  1022. if (host->irq_cd >= 0)
  1023. free_irq(host->irq_cd, host);
  1024. probe_free_irq:
  1025. free_irq(host->irq, host);
  1026. probe_iounmap:
  1027. iounmap(host->base);
  1028. probe_free_mem_region:
  1029. release_mem_region(host->mem->start, RESSIZE(host->mem));
  1030. probe_free_host:
  1031. mmc_free_host(mmc);
  1032. probe_out:
  1033. return ret;
  1034. }
  1035. static void s3cmci_shutdown(struct platform_device *pdev)
  1036. {
  1037. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1038. struct s3cmci_host *host = mmc_priv(mmc);
  1039. if (host->irq_cd >= 0)
  1040. free_irq(host->irq_cd, host);
  1041. mmc_remove_host(mmc);
  1042. clk_disable(host->clk);
  1043. }
  1044. static int __devexit s3cmci_remove(struct platform_device *pdev)
  1045. {
  1046. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1047. struct s3cmci_host *host = mmc_priv(mmc);
  1048. s3cmci_shutdown(pdev);
  1049. clk_put(host->clk);
  1050. tasklet_disable(&host->pio_tasklet);
  1051. s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
  1052. free_irq(host->irq, host);
  1053. iounmap(host->base);
  1054. release_mem_region(host->mem->start, RESSIZE(host->mem));
  1055. mmc_free_host(mmc);
  1056. return 0;
  1057. }
  1058. static int __devinit s3cmci_2410_probe(struct platform_device *dev)
  1059. {
  1060. return s3cmci_probe(dev, 0);
  1061. }
  1062. static int __devinit s3cmci_2412_probe(struct platform_device *dev)
  1063. {
  1064. return s3cmci_probe(dev, 1);
  1065. }
  1066. static int __devinit s3cmci_2440_probe(struct platform_device *dev)
  1067. {
  1068. return s3cmci_probe(dev, 1);
  1069. }
  1070. #ifdef CONFIG_PM
  1071. static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
  1072. {
  1073. struct mmc_host *mmc = platform_get_drvdata(dev);
  1074. return mmc_suspend_host(mmc, state);
  1075. }
  1076. static int s3cmci_resume(struct platform_device *dev)
  1077. {
  1078. struct mmc_host *mmc = platform_get_drvdata(dev);
  1079. return mmc_resume_host(mmc);
  1080. }
  1081. #else /* CONFIG_PM */
  1082. #define s3cmci_suspend NULL
  1083. #define s3cmci_resume NULL
  1084. #endif /* CONFIG_PM */
  1085. static struct platform_driver s3cmci_2410_driver = {
  1086. .driver.name = "s3c2410-sdi",
  1087. .driver.owner = THIS_MODULE,
  1088. .probe = s3cmci_2410_probe,
  1089. .remove = __devexit_p(s3cmci_remove),
  1090. .shutdown = s3cmci_shutdown,
  1091. .suspend = s3cmci_suspend,
  1092. .resume = s3cmci_resume,
  1093. };
  1094. static struct platform_driver s3cmci_2412_driver = {
  1095. .driver.name = "s3c2412-sdi",
  1096. .driver.owner = THIS_MODULE,
  1097. .probe = s3cmci_2412_probe,
  1098. .remove = __devexit_p(s3cmci_remove),
  1099. .shutdown = s3cmci_shutdown,
  1100. .suspend = s3cmci_suspend,
  1101. .resume = s3cmci_resume,
  1102. };
  1103. static struct platform_driver s3cmci_2440_driver = {
  1104. .driver.name = "s3c2440-sdi",
  1105. .driver.owner = THIS_MODULE,
  1106. .probe = s3cmci_2440_probe,
  1107. .remove = __devexit_p(s3cmci_remove),
  1108. .shutdown = s3cmci_shutdown,
  1109. .suspend = s3cmci_suspend,
  1110. .resume = s3cmci_resume,
  1111. };
  1112. static int __init s3cmci_init(void)
  1113. {
  1114. platform_driver_register(&s3cmci_2410_driver);
  1115. platform_driver_register(&s3cmci_2412_driver);
  1116. platform_driver_register(&s3cmci_2440_driver);
  1117. return 0;
  1118. }
  1119. static void __exit s3cmci_exit(void)
  1120. {
  1121. platform_driver_unregister(&s3cmci_2410_driver);
  1122. platform_driver_unregister(&s3cmci_2412_driver);
  1123. platform_driver_unregister(&s3cmci_2440_driver);
  1124. }
  1125. module_init(s3cmci_init);
  1126. module_exit(s3cmci_exit);
  1127. MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
  1128. MODULE_LICENSE("GPL v2");
  1129. MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>");
  1130. MODULE_ALIAS("platform:s3c2410-sdi");
  1131. MODULE_ALIAS("platform:s3c2412-sdi");
  1132. MODULE_ALIAS("platform:s3c2440-sdi");