s3cmci.c 37 KB

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