s3cmci.c 34 KB

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