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/gpio.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <mach/dma.h>
  23. #include <mach/regs-sdi.h>
  24. #include <mach/regs-gpio.h>
  25. #include <plat/mci.h>
  26. #include "s3cmci.h"
  27. #define DRIVER_NAME "s3c-mci"
  28. enum dbg_channels {
  29. dbg_err = (1 << 0),
  30. dbg_debug = (1 << 1),
  31. dbg_info = (1 << 2),
  32. dbg_irq = (1 << 3),
  33. dbg_sg = (1 << 4),
  34. dbg_dma = (1 << 5),
  35. dbg_pio = (1 << 6),
  36. dbg_fail = (1 << 7),
  37. dbg_conf = (1 << 8),
  38. };
  39. static const int dbgmap_err = dbg_fail;
  40. static const int dbgmap_info = dbg_info | dbg_conf;
  41. static const int dbgmap_debug = dbg_err | dbg_debug;
  42. #define dbg(host, channels, args...) \
  43. do { \
  44. if (dbgmap_err & channels) \
  45. dev_err(&host->pdev->dev, args); \
  46. else if (dbgmap_info & channels) \
  47. dev_info(&host->pdev->dev, args); \
  48. else if (dbgmap_debug & channels) \
  49. dev_dbg(&host->pdev->dev, args); \
  50. } while (0)
  51. #define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)
  52. static struct s3c2410_dma_client s3cmci_dma_client = {
  53. .name = "s3c-mci",
  54. };
  55. static void finalize_request(struct s3cmci_host *host);
  56. static void s3cmci_send_request(struct mmc_host *mmc);
  57. static void s3cmci_reset(struct s3cmci_host *host);
  58. #ifdef CONFIG_MMC_DEBUG
  59. static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
  60. {
  61. u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
  62. u32 datcon, datcnt, datsta, fsta, imask;
  63. con = readl(host->base + S3C2410_SDICON);
  64. pre = readl(host->base + S3C2410_SDIPRE);
  65. cmdarg = readl(host->base + S3C2410_SDICMDARG);
  66. cmdcon = readl(host->base + S3C2410_SDICMDCON);
  67. cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
  68. r0 = readl(host->base + S3C2410_SDIRSP0);
  69. r1 = readl(host->base + S3C2410_SDIRSP1);
  70. r2 = readl(host->base + S3C2410_SDIRSP2);
  71. r3 = readl(host->base + S3C2410_SDIRSP3);
  72. timer = readl(host->base + S3C2410_SDITIMER);
  73. bsize = readl(host->base + S3C2410_SDIBSIZE);
  74. datcon = readl(host->base + S3C2410_SDIDCON);
  75. datcnt = readl(host->base + S3C2410_SDIDCNT);
  76. datsta = readl(host->base + S3C2410_SDIDSTA);
  77. fsta = readl(host->base + S3C2410_SDIFSTA);
  78. imask = readl(host->base + host->sdiimsk);
  79. dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
  80. prefix, con, pre, timer);
  81. dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
  82. prefix, cmdcon, cmdarg, cmdsta);
  83. dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
  84. " DSTA:[%08x] DCNT:[%08x]\n",
  85. prefix, datcon, fsta, datsta, datcnt);
  86. dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
  87. " R2:[%08x] R3:[%08x]\n",
  88. prefix, r0, r1, r2, r3);
  89. }
  90. static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
  91. int stop)
  92. {
  93. snprintf(host->dbgmsg_cmd, 300,
  94. "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
  95. host->ccnt, (stop ? " (STOP)" : ""),
  96. cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
  97. if (cmd->data) {
  98. snprintf(host->dbgmsg_dat, 300,
  99. "#%u bsize:%u blocks:%u bytes:%u",
  100. host->dcnt, cmd->data->blksz,
  101. cmd->data->blocks,
  102. cmd->data->blocks * cmd->data->blksz);
  103. } else {
  104. host->dbgmsg_dat[0] = '\0';
  105. }
  106. }
  107. static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
  108. int fail)
  109. {
  110. unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
  111. if (!cmd)
  112. return;
  113. if (cmd->error == 0) {
  114. dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
  115. host->dbgmsg_cmd, cmd->resp[0]);
  116. } else {
  117. dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
  118. cmd->error, host->dbgmsg_cmd, host->status);
  119. }
  120. if (!cmd->data)
  121. return;
  122. if (cmd->data->error == 0) {
  123. dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
  124. } else {
  125. dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
  126. cmd->data->error, host->dbgmsg_dat,
  127. readl(host->base + S3C2410_SDIDCNT));
  128. }
  129. }
  130. #else
  131. static void dbg_dumpcmd(struct s3cmci_host *host,
  132. struct mmc_command *cmd, int fail) { }
  133. static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
  134. int stop) { }
  135. static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
  136. #endif /* CONFIG_MMC_DEBUG */
  137. static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
  138. {
  139. u32 newmask;
  140. newmask = readl(host->base + host->sdiimsk);
  141. newmask |= imask;
  142. writel(newmask, host->base + host->sdiimsk);
  143. return newmask;
  144. }
  145. static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
  146. {
  147. u32 newmask;
  148. newmask = readl(host->base + host->sdiimsk);
  149. newmask &= ~imask;
  150. writel(newmask, host->base + host->sdiimsk);
  151. return newmask;
  152. }
  153. static inline void clear_imask(struct s3cmci_host *host)
  154. {
  155. writel(0, host->base + host->sdiimsk);
  156. }
  157. static inline int get_data_buffer(struct s3cmci_host *host,
  158. u32 *bytes, u32 **pointer)
  159. {
  160. struct scatterlist *sg;
  161. if (host->pio_active == XFER_NONE)
  162. return -EINVAL;
  163. if ((!host->mrq) || (!host->mrq->data))
  164. return -EINVAL;
  165. if (host->pio_sgptr >= host->mrq->data->sg_len) {
  166. dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
  167. host->pio_sgptr, host->mrq->data->sg_len);
  168. return -EBUSY;
  169. }
  170. sg = &host->mrq->data->sg[host->pio_sgptr];
  171. *bytes = sg->length;
  172. *pointer = sg_virt(sg);
  173. host->pio_sgptr++;
  174. dbg(host, dbg_sg, "new buffer (%i/%i)\n",
  175. host->pio_sgptr, host->mrq->data->sg_len);
  176. return 0;
  177. }
  178. static inline u32 fifo_count(struct s3cmci_host *host)
  179. {
  180. u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
  181. fifostat &= S3C2410_SDIFSTA_COUNTMASK;
  182. return fifostat;
  183. }
  184. static inline u32 fifo_free(struct s3cmci_host *host)
  185. {
  186. u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
  187. fifostat &= S3C2410_SDIFSTA_COUNTMASK;
  188. return 63 - fifostat;
  189. }
  190. static void do_pio_read(struct s3cmci_host *host)
  191. {
  192. int res;
  193. u32 fifo;
  194. u32 *ptr;
  195. u32 fifo_words;
  196. void __iomem *from_ptr;
  197. /* write real prescaler to host, it might be set slow to fix */
  198. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  199. from_ptr = host->base + host->sdidata;
  200. while ((fifo = fifo_count(host))) {
  201. if (!host->pio_bytes) {
  202. res = get_data_buffer(host, &host->pio_bytes,
  203. &host->pio_ptr);
  204. if (res) {
  205. host->pio_active = XFER_NONE;
  206. host->complete_what = COMPLETION_FINALIZE;
  207. dbg(host, dbg_pio, "pio_read(): "
  208. "complete (no more data).\n");
  209. return;
  210. }
  211. dbg(host, dbg_pio,
  212. "pio_read(): new target: [%i]@[%p]\n",
  213. host->pio_bytes, host->pio_ptr);
  214. }
  215. dbg(host, dbg_pio,
  216. "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
  217. fifo, host->pio_bytes,
  218. readl(host->base + S3C2410_SDIDCNT));
  219. /* If we have reached the end of the block, we can
  220. * read a word and get 1 to 3 bytes. If we in the
  221. * middle of the block, we have to read full words,
  222. * otherwise we will write garbage, so round down to
  223. * an even multiple of 4. */
  224. if (fifo >= host->pio_bytes)
  225. fifo = host->pio_bytes;
  226. else
  227. fifo -= fifo & 3;
  228. host->pio_bytes -= fifo;
  229. host->pio_count += fifo;
  230. fifo_words = fifo >> 2;
  231. ptr = host->pio_ptr;
  232. while (fifo_words--)
  233. *ptr++ = readl(from_ptr);
  234. host->pio_ptr = ptr;
  235. if (fifo & 3) {
  236. u32 n = fifo & 3;
  237. u32 data = readl(from_ptr);
  238. u8 *p = (u8 *)host->pio_ptr;
  239. while (n--) {
  240. *p++ = data;
  241. data >>= 8;
  242. }
  243. }
  244. }
  245. if (!host->pio_bytes) {
  246. res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
  247. if (res) {
  248. dbg(host, dbg_pio,
  249. "pio_read(): complete (no more buffers).\n");
  250. host->pio_active = XFER_NONE;
  251. host->complete_what = COMPLETION_FINALIZE;
  252. return;
  253. }
  254. }
  255. enable_imask(host,
  256. S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
  257. }
  258. static void do_pio_write(struct s3cmci_host *host)
  259. {
  260. void __iomem *to_ptr;
  261. int res;
  262. u32 fifo;
  263. u32 *ptr;
  264. to_ptr = host->base + host->sdidata;
  265. while ((fifo = fifo_free(host)) > 3) {
  266. if (!host->pio_bytes) {
  267. res = get_data_buffer(host, &host->pio_bytes,
  268. &host->pio_ptr);
  269. if (res) {
  270. dbg(host, dbg_pio,
  271. "pio_write(): complete (no more data).\n");
  272. host->pio_active = XFER_NONE;
  273. return;
  274. }
  275. dbg(host, dbg_pio,
  276. "pio_write(): new source: [%i]@[%p]\n",
  277. host->pio_bytes, host->pio_ptr);
  278. }
  279. /* If we have reached the end of the block, we have to
  280. * write exactly the remaining number of bytes. If we
  281. * in the middle of the block, we have to write full
  282. * words, so round down to an even multiple of 4. */
  283. if (fifo >= host->pio_bytes)
  284. fifo = host->pio_bytes;
  285. else
  286. fifo -= fifo & 3;
  287. host->pio_bytes -= fifo;
  288. host->pio_count += fifo;
  289. fifo = (fifo + 3) >> 2;
  290. ptr = host->pio_ptr;
  291. while (fifo--)
  292. writel(*ptr++, to_ptr);
  293. host->pio_ptr = ptr;
  294. }
  295. enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  296. }
  297. static void pio_tasklet(unsigned long data)
  298. {
  299. struct s3cmci_host *host = (struct s3cmci_host *) data;
  300. disable_irq(host->irq);
  301. if (host->pio_active == XFER_WRITE)
  302. do_pio_write(host);
  303. if (host->pio_active == XFER_READ)
  304. do_pio_read(host);
  305. if (host->complete_what == COMPLETION_FINALIZE) {
  306. clear_imask(host);
  307. if (host->pio_active != XFER_NONE) {
  308. dbg(host, dbg_err, "unfinished %s "
  309. "- pio_count:[%u] pio_bytes:[%u]\n",
  310. (host->pio_active == XFER_READ) ? "read" : "write",
  311. host->pio_count, host->pio_bytes);
  312. if (host->mrq->data)
  313. host->mrq->data->error = -EINVAL;
  314. }
  315. finalize_request(host);
  316. } else
  317. enable_irq(host->irq);
  318. }
  319. /*
  320. * ISR for SDI Interface IRQ
  321. * Communication between driver and ISR works as follows:
  322. * host->mrq points to current request
  323. * host->complete_what Indicates when the request is considered done
  324. * COMPLETION_CMDSENT when the command was sent
  325. * COMPLETION_RSPFIN when a response was received
  326. * COMPLETION_XFERFINISH when the data transfer is finished
  327. * COMPLETION_XFERFINISH_RSPFIN both of the above.
  328. * host->complete_request is the completion-object the driver waits for
  329. *
  330. * 1) Driver sets up host->mrq and host->complete_what
  331. * 2) Driver prepares the transfer
  332. * 3) Driver enables interrupts
  333. * 4) Driver starts transfer
  334. * 5) Driver waits for host->complete_rquest
  335. * 6) ISR checks for request status (errors and success)
  336. * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
  337. * 7) ISR completes host->complete_request
  338. * 8) ISR disables interrupts
  339. * 9) Driver wakes up and takes care of the request
  340. *
  341. * Note: "->error"-fields are expected to be set to 0 before the request
  342. * was issued by mmc.c - therefore they are only set, when an error
  343. * contition comes up
  344. */
  345. static irqreturn_t s3cmci_irq(int irq, void *dev_id)
  346. {
  347. struct s3cmci_host *host = dev_id;
  348. struct mmc_command *cmd;
  349. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
  350. u32 mci_cclear, mci_dclear;
  351. unsigned long iflags;
  352. spin_lock_irqsave(&host->complete_lock, iflags);
  353. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  354. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  355. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  356. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  357. mci_imsk = readl(host->base + host->sdiimsk);
  358. mci_cclear = 0;
  359. mci_dclear = 0;
  360. if ((host->complete_what == COMPLETION_NONE) ||
  361. (host->complete_what == COMPLETION_FINALIZE)) {
  362. host->status = "nothing to complete";
  363. clear_imask(host);
  364. goto irq_out;
  365. }
  366. if (!host->mrq) {
  367. host->status = "no active mrq";
  368. clear_imask(host);
  369. goto irq_out;
  370. }
  371. cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
  372. if (!cmd) {
  373. host->status = "no active cmd";
  374. clear_imask(host);
  375. goto irq_out;
  376. }
  377. if (!host->dodma) {
  378. if ((host->pio_active == XFER_WRITE) &&
  379. (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
  380. disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
  381. tasklet_schedule(&host->pio_tasklet);
  382. host->status = "pio tx";
  383. }
  384. if ((host->pio_active == XFER_READ) &&
  385. (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
  386. disable_imask(host,
  387. S3C2410_SDIIMSK_RXFIFOHALF |
  388. S3C2410_SDIIMSK_RXFIFOLAST);
  389. tasklet_schedule(&host->pio_tasklet);
  390. host->status = "pio rx";
  391. }
  392. }
  393. if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
  394. dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
  395. cmd->error = -ETIMEDOUT;
  396. host->status = "error: command timeout";
  397. goto fail_transfer;
  398. }
  399. if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
  400. if (host->complete_what == COMPLETION_CMDSENT) {
  401. host->status = "ok: command sent";
  402. goto close_transfer;
  403. }
  404. mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
  405. }
  406. if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
  407. if (cmd->flags & MMC_RSP_CRC) {
  408. if (host->mrq->cmd->flags & MMC_RSP_136) {
  409. dbg(host, dbg_irq,
  410. "fixup: ignore CRC fail with long rsp\n");
  411. } else {
  412. /* note, we used to fail the transfer
  413. * here, but it seems that this is just
  414. * the hardware getting it wrong.
  415. *
  416. * cmd->error = -EILSEQ;
  417. * host->status = "error: bad command crc";
  418. * goto fail_transfer;
  419. */
  420. }
  421. }
  422. mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
  423. }
  424. if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
  425. if (host->complete_what == COMPLETION_RSPFIN) {
  426. host->status = "ok: command response received";
  427. goto close_transfer;
  428. }
  429. if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
  430. host->complete_what = COMPLETION_XFERFINISH;
  431. mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
  432. }
  433. /* errors handled after this point are only relevant
  434. when a data transfer is in progress */
  435. if (!cmd->data)
  436. goto clear_status_bits;
  437. /* Check for FIFO failure */
  438. if (host->is2440) {
  439. if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
  440. dbg(host, dbg_err, "FIFO failure\n");
  441. host->mrq->data->error = -EILSEQ;
  442. host->status = "error: 2440 fifo failure";
  443. goto fail_transfer;
  444. }
  445. } else {
  446. if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
  447. dbg(host, dbg_err, "FIFO failure\n");
  448. cmd->data->error = -EILSEQ;
  449. host->status = "error: fifo failure";
  450. goto fail_transfer;
  451. }
  452. }
  453. if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
  454. dbg(host, dbg_err, "bad data crc (outgoing)\n");
  455. cmd->data->error = -EILSEQ;
  456. host->status = "error: bad data crc (outgoing)";
  457. goto fail_transfer;
  458. }
  459. if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
  460. dbg(host, dbg_err, "bad data crc (incoming)\n");
  461. cmd->data->error = -EILSEQ;
  462. host->status = "error: bad data crc (incoming)";
  463. goto fail_transfer;
  464. }
  465. if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
  466. dbg(host, dbg_err, "data timeout\n");
  467. cmd->data->error = -ETIMEDOUT;
  468. host->status = "error: data timeout";
  469. goto fail_transfer;
  470. }
  471. if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
  472. if (host->complete_what == COMPLETION_XFERFINISH) {
  473. host->status = "ok: data transfer completed";
  474. goto close_transfer;
  475. }
  476. if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
  477. host->complete_what = COMPLETION_RSPFIN;
  478. mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
  479. }
  480. clear_status_bits:
  481. writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
  482. writel(mci_dclear, host->base + S3C2410_SDIDSTA);
  483. goto irq_out;
  484. fail_transfer:
  485. host->pio_active = XFER_NONE;
  486. close_transfer:
  487. host->complete_what = COMPLETION_FINALIZE;
  488. clear_imask(host);
  489. tasklet_schedule(&host->pio_tasklet);
  490. goto irq_out;
  491. irq_out:
  492. dbg(host, dbg_irq,
  493. "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
  494. mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
  495. spin_unlock_irqrestore(&host->complete_lock, iflags);
  496. return IRQ_HANDLED;
  497. }
  498. /*
  499. * ISR for the CardDetect Pin
  500. */
  501. static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
  502. {
  503. struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
  504. dbg(host, dbg_irq, "card detect\n");
  505. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  506. return IRQ_HANDLED;
  507. }
  508. static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
  509. void *buf_id, int size,
  510. enum s3c2410_dma_buffresult result)
  511. {
  512. struct s3cmci_host *host = buf_id;
  513. unsigned long iflags;
  514. u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
  515. mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
  516. mci_dsta = readl(host->base + S3C2410_SDIDSTA);
  517. mci_fsta = readl(host->base + S3C2410_SDIFSTA);
  518. mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
  519. BUG_ON(!host->mrq);
  520. BUG_ON(!host->mrq->data);
  521. BUG_ON(!host->dmatogo);
  522. spin_lock_irqsave(&host->complete_lock, iflags);
  523. if (result != S3C2410_RES_OK) {
  524. dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
  525. "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
  526. mci_csta, mci_dsta, mci_fsta,
  527. mci_dcnt, result, host->dmatogo);
  528. goto fail_request;
  529. }
  530. host->dmatogo--;
  531. if (host->dmatogo) {
  532. dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
  533. "DCNT:[%08x] toGo:%u\n",
  534. size, mci_dsta, mci_dcnt, host->dmatogo);
  535. goto out;
  536. }
  537. dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
  538. size, mci_dsta, mci_dcnt);
  539. host->complete_what = COMPLETION_FINALIZE;
  540. out:
  541. tasklet_schedule(&host->pio_tasklet);
  542. spin_unlock_irqrestore(&host->complete_lock, iflags);
  543. return;
  544. fail_request:
  545. host->mrq->data->error = -EINVAL;
  546. host->complete_what = COMPLETION_FINALIZE;
  547. writel(0, host->base + host->sdiimsk);
  548. goto out;
  549. }
  550. static void finalize_request(struct s3cmci_host *host)
  551. {
  552. struct mmc_request *mrq = host->mrq;
  553. struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
  554. int debug_as_failure = 0;
  555. if (host->complete_what != COMPLETION_FINALIZE)
  556. return;
  557. if (!mrq)
  558. return;
  559. if (cmd->data && (cmd->error == 0) &&
  560. (cmd->data->error == 0)) {
  561. if (host->dodma && (!host->dma_complete)) {
  562. dbg(host, dbg_dma, "DMA Missing!\n");
  563. return;
  564. }
  565. }
  566. /* Read response from controller. */
  567. cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
  568. cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
  569. cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
  570. cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
  571. writel(host->prescaler, host->base + S3C2410_SDIPRE);
  572. if (cmd->error)
  573. debug_as_failure = 1;
  574. if (cmd->data && cmd->data->error)
  575. debug_as_failure = 1;
  576. dbg_dumpcmd(host, cmd, debug_as_failure);
  577. /* Cleanup controller */
  578. writel(0, host->base + S3C2410_SDICMDARG);
  579. writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  580. writel(0, host->base + S3C2410_SDICMDCON);
  581. writel(0, host->base + host->sdiimsk);
  582. if (cmd->data && cmd->error)
  583. cmd->data->error = cmd->error;
  584. if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
  585. host->cmd_is_stop = 1;
  586. s3cmci_send_request(host->mmc);
  587. return;
  588. }
  589. /* If we have no data transfer we are finished here */
  590. if (!mrq->data)
  591. goto request_done;
  592. /* Calulate the amout of bytes transfer if there was no error */
  593. if (mrq->data->error == 0) {
  594. mrq->data->bytes_xfered =
  595. (mrq->data->blocks * mrq->data->blksz);
  596. } else {
  597. mrq->data->bytes_xfered = 0;
  598. }
  599. /* If we had an error while transfering data we flush the
  600. * DMA channel and the fifo to clear out any garbage. */
  601. if (mrq->data->error != 0) {
  602. if (host->dodma)
  603. s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
  604. if (host->is2440) {
  605. /* Clear failure register and reset fifo. */
  606. writel(S3C2440_SDIFSTA_FIFORESET |
  607. S3C2440_SDIFSTA_FIFOFAIL,
  608. host->base + S3C2410_SDIFSTA);
  609. } else {
  610. u32 mci_con;
  611. /* reset fifo */
  612. mci_con = readl(host->base + S3C2410_SDICON);
  613. mci_con |= S3C2410_SDICON_FIFORESET;
  614. writel(mci_con, host->base + S3C2410_SDICON);
  615. }
  616. }
  617. request_done:
  618. host->complete_what = COMPLETION_NONE;
  619. host->mrq = NULL;
  620. mmc_request_done(host->mmc, mrq);
  621. }
  622. static void s3cmci_dma_setup(struct s3cmci_host *host,
  623. enum s3c2410_dmasrc source)
  624. {
  625. static enum s3c2410_dmasrc last_source = -1;
  626. static int setup_ok;
  627. if (last_source == source)
  628. return;
  629. last_source = source;
  630. s3c2410_dma_devconfig(host->dma, source,
  631. host->mem->start + host->sdidata);
  632. if (!setup_ok) {
  633. s3c2410_dma_config(host->dma, 4);
  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_GPIO_OUTPUT);
  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");