s3cmci.c 38 KB

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