s3cmci.c 38 KB

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