davinci_mmc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * davinci_mmc.c - TI DaVinci MMC/SD/SDIO driver
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. * Original author: Purushotam Kumar
  6. * Copyright (C) 2009 David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/ioport.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/io.h>
  30. #include <linux/irq.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/mmc/mmc.h>
  34. #include <mach/mmc.h>
  35. #include <mach/edma.h>
  36. /*
  37. * Register Definitions
  38. */
  39. #define DAVINCI_MMCCTL 0x00 /* Control Register */
  40. #define DAVINCI_MMCCLK 0x04 /* Memory Clock Control Register */
  41. #define DAVINCI_MMCST0 0x08 /* Status Register 0 */
  42. #define DAVINCI_MMCST1 0x0C /* Status Register 1 */
  43. #define DAVINCI_MMCIM 0x10 /* Interrupt Mask Register */
  44. #define DAVINCI_MMCTOR 0x14 /* Response Time-Out Register */
  45. #define DAVINCI_MMCTOD 0x18 /* Data Read Time-Out Register */
  46. #define DAVINCI_MMCBLEN 0x1C /* Block Length Register */
  47. #define DAVINCI_MMCNBLK 0x20 /* Number of Blocks Register */
  48. #define DAVINCI_MMCNBLC 0x24 /* Number of Blocks Counter Register */
  49. #define DAVINCI_MMCDRR 0x28 /* Data Receive Register */
  50. #define DAVINCI_MMCDXR 0x2C /* Data Transmit Register */
  51. #define DAVINCI_MMCCMD 0x30 /* Command Register */
  52. #define DAVINCI_MMCARGHL 0x34 /* Argument Register */
  53. #define DAVINCI_MMCRSP01 0x38 /* Response Register 0 and 1 */
  54. #define DAVINCI_MMCRSP23 0x3C /* Response Register 0 and 1 */
  55. #define DAVINCI_MMCRSP45 0x40 /* Response Register 0 and 1 */
  56. #define DAVINCI_MMCRSP67 0x44 /* Response Register 0 and 1 */
  57. #define DAVINCI_MMCDRSP 0x48 /* Data Response Register */
  58. #define DAVINCI_MMCETOK 0x4C
  59. #define DAVINCI_MMCCIDX 0x50 /* Command Index Register */
  60. #define DAVINCI_MMCCKC 0x54
  61. #define DAVINCI_MMCTORC 0x58
  62. #define DAVINCI_MMCTODC 0x5C
  63. #define DAVINCI_MMCBLNC 0x60
  64. #define DAVINCI_SDIOCTL 0x64
  65. #define DAVINCI_SDIOST0 0x68
  66. #define DAVINCI_SDIOIEN 0x6C
  67. #define DAVINCI_SDIOIST 0x70
  68. #define DAVINCI_MMCFIFOCTL 0x74 /* FIFO Control Register */
  69. /* DAVINCI_MMCCTL definitions */
  70. #define MMCCTL_DATRST (1 << 0)
  71. #define MMCCTL_CMDRST (1 << 1)
  72. #define MMCCTL_WIDTH_8_BIT (1 << 8)
  73. #define MMCCTL_WIDTH_4_BIT (1 << 2)
  74. #define MMCCTL_DATEG_DISABLED (0 << 6)
  75. #define MMCCTL_DATEG_RISING (1 << 6)
  76. #define MMCCTL_DATEG_FALLING (2 << 6)
  77. #define MMCCTL_DATEG_BOTH (3 << 6)
  78. #define MMCCTL_PERMDR_LE (0 << 9)
  79. #define MMCCTL_PERMDR_BE (1 << 9)
  80. #define MMCCTL_PERMDX_LE (0 << 10)
  81. #define MMCCTL_PERMDX_BE (1 << 10)
  82. /* DAVINCI_MMCCLK definitions */
  83. #define MMCCLK_CLKEN (1 << 8)
  84. #define MMCCLK_CLKRT_MASK (0xFF << 0)
  85. /* IRQ bit definitions, for DAVINCI_MMCST0 and DAVINCI_MMCIM */
  86. #define MMCST0_DATDNE BIT(0) /* data done */
  87. #define MMCST0_BSYDNE BIT(1) /* busy done */
  88. #define MMCST0_RSPDNE BIT(2) /* command done */
  89. #define MMCST0_TOUTRD BIT(3) /* data read timeout */
  90. #define MMCST0_TOUTRS BIT(4) /* command response timeout */
  91. #define MMCST0_CRCWR BIT(5) /* data write CRC error */
  92. #define MMCST0_CRCRD BIT(6) /* data read CRC error */
  93. #define MMCST0_CRCRS BIT(7) /* command response CRC error */
  94. #define MMCST0_DXRDY BIT(9) /* data transmit ready (fifo empty) */
  95. #define MMCST0_DRRDY BIT(10) /* data receive ready (data in fifo)*/
  96. #define MMCST0_DATED BIT(11) /* DAT3 edge detect */
  97. #define MMCST0_TRNDNE BIT(12) /* transfer done */
  98. /* DAVINCI_MMCST1 definitions */
  99. #define MMCST1_BUSY (1 << 0)
  100. /* DAVINCI_MMCCMD definitions */
  101. #define MMCCMD_CMD_MASK (0x3F << 0)
  102. #define MMCCMD_PPLEN (1 << 7)
  103. #define MMCCMD_BSYEXP (1 << 8)
  104. #define MMCCMD_RSPFMT_MASK (3 << 9)
  105. #define MMCCMD_RSPFMT_NONE (0 << 9)
  106. #define MMCCMD_RSPFMT_R1456 (1 << 9)
  107. #define MMCCMD_RSPFMT_R2 (2 << 9)
  108. #define MMCCMD_RSPFMT_R3 (3 << 9)
  109. #define MMCCMD_DTRW (1 << 11)
  110. #define MMCCMD_STRMTP (1 << 12)
  111. #define MMCCMD_WDATX (1 << 13)
  112. #define MMCCMD_INITCK (1 << 14)
  113. #define MMCCMD_DCLR (1 << 15)
  114. #define MMCCMD_DMATRIG (1 << 16)
  115. /* DAVINCI_MMCFIFOCTL definitions */
  116. #define MMCFIFOCTL_FIFORST (1 << 0)
  117. #define MMCFIFOCTL_FIFODIR_WR (1 << 1)
  118. #define MMCFIFOCTL_FIFODIR_RD (0 << 1)
  119. #define MMCFIFOCTL_FIFOLEV (1 << 2) /* 0 = 128 bits, 1 = 256 bits */
  120. #define MMCFIFOCTL_ACCWD_4 (0 << 3) /* access width of 4 bytes */
  121. #define MMCFIFOCTL_ACCWD_3 (1 << 3) /* access width of 3 bytes */
  122. #define MMCFIFOCTL_ACCWD_2 (2 << 3) /* access width of 2 bytes */
  123. #define MMCFIFOCTL_ACCWD_1 (3 << 3) /* access width of 1 byte */
  124. /* DAVINCI_SDIOST0 definitions */
  125. #define SDIOST0_DAT1_HI BIT(0)
  126. /* DAVINCI_SDIOIEN definitions */
  127. #define SDIOIEN_IOINTEN BIT(0)
  128. /* DAVINCI_SDIOIST definitions */
  129. #define SDIOIST_IOINT BIT(0)
  130. /* MMCSD Init clock in Hz in opendrain mode */
  131. #define MMCSD_INIT_CLOCK 200000
  132. /*
  133. * One scatterlist dma "segment" is at most MAX_CCNT rw_threshold units,
  134. * and we handle up to MAX_NR_SG segments. MMC_BLOCK_BOUNCE kicks in only
  135. * for drivers with max_segs == 1, making the segments bigger (64KB)
  136. * than the page or two that's otherwise typical. nr_sg (passed from
  137. * platform data) == 16 gives at least the same throughput boost, using
  138. * EDMA transfer linkage instead of spending CPU time copying pages.
  139. */
  140. #define MAX_CCNT ((1 << 16) - 1)
  141. #define MAX_NR_SG 16
  142. static unsigned rw_threshold = 32;
  143. module_param(rw_threshold, uint, S_IRUGO);
  144. MODULE_PARM_DESC(rw_threshold,
  145. "Read/Write threshold. Default = 32");
  146. static unsigned poll_threshold = 128;
  147. module_param(poll_threshold, uint, S_IRUGO);
  148. MODULE_PARM_DESC(poll_threshold,
  149. "Polling transaction size threshold. Default = 128");
  150. static unsigned poll_loopcount = 32;
  151. module_param(poll_loopcount, uint, S_IRUGO);
  152. MODULE_PARM_DESC(poll_loopcount,
  153. "Maximum polling loop count. Default = 32");
  154. static unsigned __initdata use_dma = 1;
  155. module_param(use_dma, uint, 0);
  156. MODULE_PARM_DESC(use_dma, "Whether to use DMA or not. Default = 1");
  157. struct mmc_davinci_host {
  158. struct mmc_command *cmd;
  159. struct mmc_data *data;
  160. struct mmc_host *mmc;
  161. struct clk *clk;
  162. unsigned int mmc_input_clk;
  163. void __iomem *base;
  164. struct resource *mem_res;
  165. int mmc_irq, sdio_irq;
  166. unsigned char bus_mode;
  167. #define DAVINCI_MMC_DATADIR_NONE 0
  168. #define DAVINCI_MMC_DATADIR_READ 1
  169. #define DAVINCI_MMC_DATADIR_WRITE 2
  170. unsigned char data_dir;
  171. unsigned char suspended;
  172. /* buffer is used during PIO of one scatterlist segment, and
  173. * is updated along with buffer_bytes_left. bytes_left applies
  174. * to all N blocks of the PIO transfer.
  175. */
  176. u8 *buffer;
  177. u32 buffer_bytes_left;
  178. u32 bytes_left;
  179. u32 rxdma, txdma;
  180. bool use_dma;
  181. bool do_dma;
  182. bool sdio_int;
  183. bool active_request;
  184. /* Scatterlist DMA uses one or more parameter RAM entries:
  185. * the main one (associated with rxdma or txdma) plus zero or
  186. * more links. The entries for a given transfer differ only
  187. * by memory buffer (address, length) and link field.
  188. */
  189. struct edmacc_param tx_template;
  190. struct edmacc_param rx_template;
  191. unsigned n_link;
  192. u32 links[MAX_NR_SG - 1];
  193. /* For PIO we walk scatterlists one segment at a time. */
  194. unsigned int sg_len;
  195. struct scatterlist *sg;
  196. /* Version of the MMC/SD controller */
  197. u8 version;
  198. /* for ns in one cycle calculation */
  199. unsigned ns_in_one_cycle;
  200. /* Number of sg segments */
  201. u8 nr_sg;
  202. #ifdef CONFIG_CPU_FREQ
  203. struct notifier_block freq_transition;
  204. #endif
  205. };
  206. static irqreturn_t mmc_davinci_irq(int irq, void *dev_id);
  207. /* PIO only */
  208. static void mmc_davinci_sg_to_buf(struct mmc_davinci_host *host)
  209. {
  210. host->buffer_bytes_left = sg_dma_len(host->sg);
  211. host->buffer = sg_virt(host->sg);
  212. if (host->buffer_bytes_left > host->bytes_left)
  213. host->buffer_bytes_left = host->bytes_left;
  214. }
  215. static void davinci_fifo_data_trans(struct mmc_davinci_host *host,
  216. unsigned int n)
  217. {
  218. u8 *p;
  219. unsigned int i;
  220. if (host->buffer_bytes_left == 0) {
  221. host->sg = sg_next(host->data->sg);
  222. mmc_davinci_sg_to_buf(host);
  223. }
  224. p = host->buffer;
  225. if (n > host->buffer_bytes_left)
  226. n = host->buffer_bytes_left;
  227. host->buffer_bytes_left -= n;
  228. host->bytes_left -= n;
  229. /* NOTE: we never transfer more than rw_threshold bytes
  230. * to/from the fifo here; there's no I/O overlap.
  231. * This also assumes that access width( i.e. ACCWD) is 4 bytes
  232. */
  233. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
  234. for (i = 0; i < (n >> 2); i++) {
  235. writel(*((u32 *)p), host->base + DAVINCI_MMCDXR);
  236. p = p + 4;
  237. }
  238. if (n & 3) {
  239. iowrite8_rep(host->base + DAVINCI_MMCDXR, p, (n & 3));
  240. p = p + (n & 3);
  241. }
  242. } else {
  243. for (i = 0; i < (n >> 2); i++) {
  244. *((u32 *)p) = readl(host->base + DAVINCI_MMCDRR);
  245. p = p + 4;
  246. }
  247. if (n & 3) {
  248. ioread8_rep(host->base + DAVINCI_MMCDRR, p, (n & 3));
  249. p = p + (n & 3);
  250. }
  251. }
  252. host->buffer = p;
  253. }
  254. static void mmc_davinci_start_command(struct mmc_davinci_host *host,
  255. struct mmc_command *cmd)
  256. {
  257. u32 cmd_reg = 0;
  258. u32 im_val;
  259. dev_dbg(mmc_dev(host->mmc), "CMD%d, arg 0x%08x%s\n",
  260. cmd->opcode, cmd->arg,
  261. ({ char *s;
  262. switch (mmc_resp_type(cmd)) {
  263. case MMC_RSP_R1:
  264. s = ", R1/R5/R6/R7 response";
  265. break;
  266. case MMC_RSP_R1B:
  267. s = ", R1b response";
  268. break;
  269. case MMC_RSP_R2:
  270. s = ", R2 response";
  271. break;
  272. case MMC_RSP_R3:
  273. s = ", R3/R4 response";
  274. break;
  275. default:
  276. s = ", (R? response)";
  277. break;
  278. }; s; }));
  279. host->cmd = cmd;
  280. switch (mmc_resp_type(cmd)) {
  281. case MMC_RSP_R1B:
  282. /* There's some spec confusion about when R1B is
  283. * allowed, but if the card doesn't issue a BUSY
  284. * then it's harmless for us to allow it.
  285. */
  286. cmd_reg |= MMCCMD_BSYEXP;
  287. /* FALLTHROUGH */
  288. case MMC_RSP_R1: /* 48 bits, CRC */
  289. cmd_reg |= MMCCMD_RSPFMT_R1456;
  290. break;
  291. case MMC_RSP_R2: /* 136 bits, CRC */
  292. cmd_reg |= MMCCMD_RSPFMT_R2;
  293. break;
  294. case MMC_RSP_R3: /* 48 bits, no CRC */
  295. cmd_reg |= MMCCMD_RSPFMT_R3;
  296. break;
  297. default:
  298. cmd_reg |= MMCCMD_RSPFMT_NONE;
  299. dev_dbg(mmc_dev(host->mmc), "unknown resp_type %04x\n",
  300. mmc_resp_type(cmd));
  301. break;
  302. }
  303. /* Set command index */
  304. cmd_reg |= cmd->opcode;
  305. /* Enable EDMA transfer triggers */
  306. if (host->do_dma)
  307. cmd_reg |= MMCCMD_DMATRIG;
  308. if (host->version == MMC_CTLR_VERSION_2 && host->data != NULL &&
  309. host->data_dir == DAVINCI_MMC_DATADIR_READ)
  310. cmd_reg |= MMCCMD_DMATRIG;
  311. /* Setting whether command involves data transfer or not */
  312. if (cmd->data)
  313. cmd_reg |= MMCCMD_WDATX;
  314. /* Setting whether stream or block transfer */
  315. if (cmd->flags & MMC_DATA_STREAM)
  316. cmd_reg |= MMCCMD_STRMTP;
  317. /* Setting whether data read or write */
  318. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE)
  319. cmd_reg |= MMCCMD_DTRW;
  320. if (host->bus_mode == MMC_BUSMODE_PUSHPULL)
  321. cmd_reg |= MMCCMD_PPLEN;
  322. /* set Command timeout */
  323. writel(0x1FFF, host->base + DAVINCI_MMCTOR);
  324. /* Enable interrupt (calculate here, defer until FIFO is stuffed). */
  325. im_val = MMCST0_RSPDNE | MMCST0_CRCRS | MMCST0_TOUTRS;
  326. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
  327. im_val |= MMCST0_DATDNE | MMCST0_CRCWR;
  328. if (!host->do_dma)
  329. im_val |= MMCST0_DXRDY;
  330. } else if (host->data_dir == DAVINCI_MMC_DATADIR_READ) {
  331. im_val |= MMCST0_DATDNE | MMCST0_CRCRD | MMCST0_TOUTRD;
  332. if (!host->do_dma)
  333. im_val |= MMCST0_DRRDY;
  334. }
  335. /*
  336. * Before non-DMA WRITE commands the controller needs priming:
  337. * FIFO should be populated with 32 bytes i.e. whatever is the FIFO size
  338. */
  339. if (!host->do_dma && (host->data_dir == DAVINCI_MMC_DATADIR_WRITE))
  340. davinci_fifo_data_trans(host, rw_threshold);
  341. writel(cmd->arg, host->base + DAVINCI_MMCARGHL);
  342. writel(cmd_reg, host->base + DAVINCI_MMCCMD);
  343. host->active_request = true;
  344. if (!host->do_dma && host->bytes_left <= poll_threshold) {
  345. u32 count = poll_loopcount;
  346. while (host->active_request && count--) {
  347. mmc_davinci_irq(0, host);
  348. cpu_relax();
  349. }
  350. }
  351. if (host->active_request)
  352. writel(im_val, host->base + DAVINCI_MMCIM);
  353. }
  354. /*----------------------------------------------------------------------*/
  355. /* DMA infrastructure */
  356. static void davinci_abort_dma(struct mmc_davinci_host *host)
  357. {
  358. int sync_dev;
  359. if (host->data_dir == DAVINCI_MMC_DATADIR_READ)
  360. sync_dev = host->rxdma;
  361. else
  362. sync_dev = host->txdma;
  363. edma_stop(sync_dev);
  364. edma_clean_channel(sync_dev);
  365. }
  366. static void
  367. mmc_davinci_xfer_done(struct mmc_davinci_host *host, struct mmc_data *data);
  368. static void mmc_davinci_dma_cb(unsigned channel, u16 ch_status, void *data)
  369. {
  370. if (DMA_COMPLETE != ch_status) {
  371. struct mmc_davinci_host *host = data;
  372. /* Currently means: DMA Event Missed, or "null" transfer
  373. * request was seen. In the future, TC errors (like bad
  374. * addresses) might be presented too.
  375. */
  376. dev_warn(mmc_dev(host->mmc), "DMA %s error\n",
  377. (host->data->flags & MMC_DATA_WRITE)
  378. ? "write" : "read");
  379. host->data->error = -EIO;
  380. mmc_davinci_xfer_done(host, host->data);
  381. }
  382. }
  383. /* Set up tx or rx template, to be modified and updated later */
  384. static void __init mmc_davinci_dma_setup(struct mmc_davinci_host *host,
  385. bool tx, struct edmacc_param *template)
  386. {
  387. unsigned sync_dev;
  388. const u16 acnt = 4;
  389. const u16 bcnt = rw_threshold >> 2;
  390. const u16 ccnt = 0;
  391. u32 src_port = 0;
  392. u32 dst_port = 0;
  393. s16 src_bidx, dst_bidx;
  394. s16 src_cidx, dst_cidx;
  395. /*
  396. * A-B Sync transfer: each DMA request is for one "frame" of
  397. * rw_threshold bytes, broken into "acnt"-size chunks repeated
  398. * "bcnt" times. Each segment needs "ccnt" such frames; since
  399. * we tell the block layer our mmc->max_seg_size limit, we can
  400. * trust (later) that it's within bounds.
  401. *
  402. * The FIFOs are read/written in 4-byte chunks (acnt == 4) and
  403. * EDMA will optimize memory operations to use larger bursts.
  404. */
  405. if (tx) {
  406. sync_dev = host->txdma;
  407. /* src_prt, ccnt, and link to be set up later */
  408. src_bidx = acnt;
  409. src_cidx = acnt * bcnt;
  410. dst_port = host->mem_res->start + DAVINCI_MMCDXR;
  411. dst_bidx = 0;
  412. dst_cidx = 0;
  413. } else {
  414. sync_dev = host->rxdma;
  415. src_port = host->mem_res->start + DAVINCI_MMCDRR;
  416. src_bidx = 0;
  417. src_cidx = 0;
  418. /* dst_prt, ccnt, and link to be set up later */
  419. dst_bidx = acnt;
  420. dst_cidx = acnt * bcnt;
  421. }
  422. /*
  423. * We can't use FIFO mode for the FIFOs because MMC FIFO addresses
  424. * are not 256-bit (32-byte) aligned. So we use INCR, and the W8BIT
  425. * parameter is ignored.
  426. */
  427. edma_set_src(sync_dev, src_port, INCR, W8BIT);
  428. edma_set_dest(sync_dev, dst_port, INCR, W8BIT);
  429. edma_set_src_index(sync_dev, src_bidx, src_cidx);
  430. edma_set_dest_index(sync_dev, dst_bidx, dst_cidx);
  431. edma_set_transfer_params(sync_dev, acnt, bcnt, ccnt, 8, ABSYNC);
  432. edma_read_slot(sync_dev, template);
  433. /* don't bother with irqs or chaining */
  434. template->opt |= EDMA_CHAN_SLOT(sync_dev) << 12;
  435. }
  436. static void mmc_davinci_send_dma_request(struct mmc_davinci_host *host,
  437. struct mmc_data *data)
  438. {
  439. struct edmacc_param *template;
  440. int channel, slot;
  441. unsigned link;
  442. struct scatterlist *sg;
  443. unsigned sg_len;
  444. unsigned bytes_left = host->bytes_left;
  445. const unsigned shift = ffs(rw_threshold) - 1;
  446. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE) {
  447. template = &host->tx_template;
  448. channel = host->txdma;
  449. } else {
  450. template = &host->rx_template;
  451. channel = host->rxdma;
  452. }
  453. /* We know sg_len and ccnt will never be out of range because
  454. * we told the mmc layer which in turn tells the block layer
  455. * to ensure that it only hands us one scatterlist segment
  456. * per EDMA PARAM entry. Update the PARAM
  457. * entries needed for each segment of this scatterlist.
  458. */
  459. for (slot = channel, link = 0, sg = data->sg, sg_len = host->sg_len;
  460. sg_len-- != 0 && bytes_left;
  461. sg = sg_next(sg), slot = host->links[link++]) {
  462. u32 buf = sg_dma_address(sg);
  463. unsigned count = sg_dma_len(sg);
  464. template->link_bcntrld = sg_len
  465. ? (EDMA_CHAN_SLOT(host->links[link]) << 5)
  466. : 0xffff;
  467. if (count > bytes_left)
  468. count = bytes_left;
  469. bytes_left -= count;
  470. if (host->data_dir == DAVINCI_MMC_DATADIR_WRITE)
  471. template->src = buf;
  472. else
  473. template->dst = buf;
  474. template->ccnt = count >> shift;
  475. edma_write_slot(slot, template);
  476. }
  477. if (host->version == MMC_CTLR_VERSION_2)
  478. edma_clear_event(channel);
  479. edma_start(channel);
  480. }
  481. static int mmc_davinci_start_dma_transfer(struct mmc_davinci_host *host,
  482. struct mmc_data *data)
  483. {
  484. int i;
  485. int mask = rw_threshold - 1;
  486. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  487. ((data->flags & MMC_DATA_WRITE)
  488. ? DMA_TO_DEVICE
  489. : DMA_FROM_DEVICE));
  490. /* no individual DMA segment should need a partial FIFO */
  491. for (i = 0; i < host->sg_len; i++) {
  492. if (sg_dma_len(data->sg + i) & mask) {
  493. dma_unmap_sg(mmc_dev(host->mmc),
  494. data->sg, data->sg_len,
  495. (data->flags & MMC_DATA_WRITE)
  496. ? DMA_TO_DEVICE
  497. : DMA_FROM_DEVICE);
  498. return -1;
  499. }
  500. }
  501. host->do_dma = 1;
  502. mmc_davinci_send_dma_request(host, data);
  503. return 0;
  504. }
  505. static void __init_or_module
  506. davinci_release_dma_channels(struct mmc_davinci_host *host)
  507. {
  508. unsigned i;
  509. if (!host->use_dma)
  510. return;
  511. for (i = 0; i < host->n_link; i++)
  512. edma_free_slot(host->links[i]);
  513. edma_free_channel(host->txdma);
  514. edma_free_channel(host->rxdma);
  515. }
  516. static int __init davinci_acquire_dma_channels(struct mmc_davinci_host *host)
  517. {
  518. u32 link_size;
  519. int r, i;
  520. /* Acquire master DMA write channel */
  521. r = edma_alloc_channel(host->txdma, mmc_davinci_dma_cb, host,
  522. EVENTQ_DEFAULT);
  523. if (r < 0) {
  524. dev_warn(mmc_dev(host->mmc), "alloc %s channel err %d\n",
  525. "tx", r);
  526. return r;
  527. }
  528. mmc_davinci_dma_setup(host, true, &host->tx_template);
  529. /* Acquire master DMA read channel */
  530. r = edma_alloc_channel(host->rxdma, mmc_davinci_dma_cb, host,
  531. EVENTQ_DEFAULT);
  532. if (r < 0) {
  533. dev_warn(mmc_dev(host->mmc), "alloc %s channel err %d\n",
  534. "rx", r);
  535. goto free_master_write;
  536. }
  537. mmc_davinci_dma_setup(host, false, &host->rx_template);
  538. /* Allocate parameter RAM slots, which will later be bound to a
  539. * channel as needed to handle a scatterlist.
  540. */
  541. link_size = min_t(unsigned, host->nr_sg, ARRAY_SIZE(host->links));
  542. for (i = 0; i < link_size; i++) {
  543. r = edma_alloc_slot(EDMA_CTLR(host->txdma), EDMA_SLOT_ANY);
  544. if (r < 0) {
  545. dev_dbg(mmc_dev(host->mmc), "dma PaRAM alloc --> %d\n",
  546. r);
  547. break;
  548. }
  549. host->links[i] = r;
  550. }
  551. host->n_link = i;
  552. return 0;
  553. free_master_write:
  554. edma_free_channel(host->txdma);
  555. return r;
  556. }
  557. /*----------------------------------------------------------------------*/
  558. static void
  559. mmc_davinci_prepare_data(struct mmc_davinci_host *host, struct mmc_request *req)
  560. {
  561. int fifo_lev = (rw_threshold == 32) ? MMCFIFOCTL_FIFOLEV : 0;
  562. int timeout;
  563. struct mmc_data *data = req->data;
  564. if (host->version == MMC_CTLR_VERSION_2)
  565. fifo_lev = (rw_threshold == 64) ? MMCFIFOCTL_FIFOLEV : 0;
  566. host->data = data;
  567. if (data == NULL) {
  568. host->data_dir = DAVINCI_MMC_DATADIR_NONE;
  569. writel(0, host->base + DAVINCI_MMCBLEN);
  570. writel(0, host->base + DAVINCI_MMCNBLK);
  571. return;
  572. }
  573. dev_dbg(mmc_dev(host->mmc), "%s %s, %d blocks of %d bytes\n",
  574. (data->flags & MMC_DATA_STREAM) ? "stream" : "block",
  575. (data->flags & MMC_DATA_WRITE) ? "write" : "read",
  576. data->blocks, data->blksz);
  577. dev_dbg(mmc_dev(host->mmc), " DTO %d cycles + %d ns\n",
  578. data->timeout_clks, data->timeout_ns);
  579. timeout = data->timeout_clks +
  580. (data->timeout_ns / host->ns_in_one_cycle);
  581. if (timeout > 0xffff)
  582. timeout = 0xffff;
  583. writel(timeout, host->base + DAVINCI_MMCTOD);
  584. writel(data->blocks, host->base + DAVINCI_MMCNBLK);
  585. writel(data->blksz, host->base + DAVINCI_MMCBLEN);
  586. /* Configure the FIFO */
  587. switch (data->flags & MMC_DATA_WRITE) {
  588. case MMC_DATA_WRITE:
  589. host->data_dir = DAVINCI_MMC_DATADIR_WRITE;
  590. writel(fifo_lev | MMCFIFOCTL_FIFODIR_WR | MMCFIFOCTL_FIFORST,
  591. host->base + DAVINCI_MMCFIFOCTL);
  592. writel(fifo_lev | MMCFIFOCTL_FIFODIR_WR,
  593. host->base + DAVINCI_MMCFIFOCTL);
  594. break;
  595. default:
  596. host->data_dir = DAVINCI_MMC_DATADIR_READ;
  597. writel(fifo_lev | MMCFIFOCTL_FIFODIR_RD | MMCFIFOCTL_FIFORST,
  598. host->base + DAVINCI_MMCFIFOCTL);
  599. writel(fifo_lev | MMCFIFOCTL_FIFODIR_RD,
  600. host->base + DAVINCI_MMCFIFOCTL);
  601. break;
  602. }
  603. host->buffer = NULL;
  604. host->bytes_left = data->blocks * data->blksz;
  605. /* For now we try to use DMA whenever we won't need partial FIFO
  606. * reads or writes, either for the whole transfer (as tested here)
  607. * or for any individual scatterlist segment (tested when we call
  608. * start_dma_transfer).
  609. *
  610. * While we *could* change that, unusual block sizes are rarely
  611. * used. The occasional fallback to PIO should't hurt.
  612. */
  613. if (host->use_dma && (host->bytes_left & (rw_threshold - 1)) == 0
  614. && mmc_davinci_start_dma_transfer(host, data) == 0) {
  615. /* zero this to ensure we take no PIO paths */
  616. host->bytes_left = 0;
  617. } else {
  618. /* Revert to CPU Copy */
  619. host->sg_len = data->sg_len;
  620. host->sg = host->data->sg;
  621. mmc_davinci_sg_to_buf(host);
  622. }
  623. }
  624. static void mmc_davinci_request(struct mmc_host *mmc, struct mmc_request *req)
  625. {
  626. struct mmc_davinci_host *host = mmc_priv(mmc);
  627. unsigned long timeout = jiffies + msecs_to_jiffies(900);
  628. u32 mmcst1 = 0;
  629. /* Card may still be sending BUSY after a previous operation,
  630. * typically some kind of write. If so, we can't proceed yet.
  631. */
  632. while (time_before(jiffies, timeout)) {
  633. mmcst1 = readl(host->base + DAVINCI_MMCST1);
  634. if (!(mmcst1 & MMCST1_BUSY))
  635. break;
  636. cpu_relax();
  637. }
  638. if (mmcst1 & MMCST1_BUSY) {
  639. dev_err(mmc_dev(host->mmc), "still BUSY? bad ... \n");
  640. req->cmd->error = -ETIMEDOUT;
  641. mmc_request_done(mmc, req);
  642. return;
  643. }
  644. host->do_dma = 0;
  645. mmc_davinci_prepare_data(host, req);
  646. mmc_davinci_start_command(host, req->cmd);
  647. }
  648. static unsigned int calculate_freq_for_card(struct mmc_davinci_host *host,
  649. unsigned int mmc_req_freq)
  650. {
  651. unsigned int mmc_freq = 0, mmc_pclk = 0, mmc_push_pull_divisor = 0;
  652. mmc_pclk = host->mmc_input_clk;
  653. if (mmc_req_freq && mmc_pclk > (2 * mmc_req_freq))
  654. mmc_push_pull_divisor = ((unsigned int)mmc_pclk
  655. / (2 * mmc_req_freq)) - 1;
  656. else
  657. mmc_push_pull_divisor = 0;
  658. mmc_freq = (unsigned int)mmc_pclk
  659. / (2 * (mmc_push_pull_divisor + 1));
  660. if (mmc_freq > mmc_req_freq)
  661. mmc_push_pull_divisor = mmc_push_pull_divisor + 1;
  662. /* Convert ns to clock cycles */
  663. if (mmc_req_freq <= 400000)
  664. host->ns_in_one_cycle = (1000000) / (((mmc_pclk
  665. / (2 * (mmc_push_pull_divisor + 1)))/1000));
  666. else
  667. host->ns_in_one_cycle = (1000000) / (((mmc_pclk
  668. / (2 * (mmc_push_pull_divisor + 1)))/1000000));
  669. return mmc_push_pull_divisor;
  670. }
  671. static void calculate_clk_divider(struct mmc_host *mmc, struct mmc_ios *ios)
  672. {
  673. unsigned int open_drain_freq = 0, mmc_pclk = 0;
  674. unsigned int mmc_push_pull_freq = 0;
  675. struct mmc_davinci_host *host = mmc_priv(mmc);
  676. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  677. u32 temp;
  678. /* Ignoring the init clock value passed for fixing the inter
  679. * operability with different cards.
  680. */
  681. open_drain_freq = ((unsigned int)mmc_pclk
  682. / (2 * MMCSD_INIT_CLOCK)) - 1;
  683. if (open_drain_freq > 0xFF)
  684. open_drain_freq = 0xFF;
  685. temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKRT_MASK;
  686. temp |= open_drain_freq;
  687. writel(temp, host->base + DAVINCI_MMCCLK);
  688. /* Convert ns to clock cycles */
  689. host->ns_in_one_cycle = (1000000) / (MMCSD_INIT_CLOCK/1000);
  690. } else {
  691. u32 temp;
  692. mmc_push_pull_freq = calculate_freq_for_card(host, ios->clock);
  693. if (mmc_push_pull_freq > 0xFF)
  694. mmc_push_pull_freq = 0xFF;
  695. temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKEN;
  696. writel(temp, host->base + DAVINCI_MMCCLK);
  697. udelay(10);
  698. temp = readl(host->base + DAVINCI_MMCCLK) & ~MMCCLK_CLKRT_MASK;
  699. temp |= mmc_push_pull_freq;
  700. writel(temp, host->base + DAVINCI_MMCCLK);
  701. writel(temp | MMCCLK_CLKEN, host->base + DAVINCI_MMCCLK);
  702. udelay(10);
  703. }
  704. }
  705. static void mmc_davinci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  706. {
  707. struct mmc_davinci_host *host = mmc_priv(mmc);
  708. struct platform_device *pdev = to_platform_device(mmc->parent);
  709. struct davinci_mmc_config *config = pdev->dev.platform_data;
  710. dev_dbg(mmc_dev(host->mmc),
  711. "clock %dHz busmode %d powermode %d Vdd %04x\n",
  712. ios->clock, ios->bus_mode, ios->power_mode,
  713. ios->vdd);
  714. switch (ios->power_mode) {
  715. case MMC_POWER_OFF:
  716. if (config && config->set_power)
  717. config->set_power(pdev->id, false);
  718. break;
  719. case MMC_POWER_UP:
  720. if (config && config->set_power)
  721. config->set_power(pdev->id, true);
  722. break;
  723. }
  724. switch (ios->bus_width) {
  725. case MMC_BUS_WIDTH_8:
  726. dev_dbg(mmc_dev(host->mmc), "Enabling 8 bit mode\n");
  727. writel((readl(host->base + DAVINCI_MMCCTL) &
  728. ~MMCCTL_WIDTH_4_BIT) | MMCCTL_WIDTH_8_BIT,
  729. host->base + DAVINCI_MMCCTL);
  730. break;
  731. case MMC_BUS_WIDTH_4:
  732. dev_dbg(mmc_dev(host->mmc), "Enabling 4 bit mode\n");
  733. if (host->version == MMC_CTLR_VERSION_2)
  734. writel((readl(host->base + DAVINCI_MMCCTL) &
  735. ~MMCCTL_WIDTH_8_BIT) | MMCCTL_WIDTH_4_BIT,
  736. host->base + DAVINCI_MMCCTL);
  737. else
  738. writel(readl(host->base + DAVINCI_MMCCTL) |
  739. MMCCTL_WIDTH_4_BIT,
  740. host->base + DAVINCI_MMCCTL);
  741. break;
  742. case MMC_BUS_WIDTH_1:
  743. dev_dbg(mmc_dev(host->mmc), "Enabling 1 bit mode\n");
  744. if (host->version == MMC_CTLR_VERSION_2)
  745. writel(readl(host->base + DAVINCI_MMCCTL) &
  746. ~(MMCCTL_WIDTH_8_BIT | MMCCTL_WIDTH_4_BIT),
  747. host->base + DAVINCI_MMCCTL);
  748. else
  749. writel(readl(host->base + DAVINCI_MMCCTL) &
  750. ~MMCCTL_WIDTH_4_BIT,
  751. host->base + DAVINCI_MMCCTL);
  752. break;
  753. }
  754. calculate_clk_divider(mmc, ios);
  755. host->bus_mode = ios->bus_mode;
  756. if (ios->power_mode == MMC_POWER_UP) {
  757. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  758. bool lose = true;
  759. /* Send clock cycles, poll completion */
  760. writel(0, host->base + DAVINCI_MMCARGHL);
  761. writel(MMCCMD_INITCK, host->base + DAVINCI_MMCCMD);
  762. while (time_before(jiffies, timeout)) {
  763. u32 tmp = readl(host->base + DAVINCI_MMCST0);
  764. if (tmp & MMCST0_RSPDNE) {
  765. lose = false;
  766. break;
  767. }
  768. cpu_relax();
  769. }
  770. if (lose)
  771. dev_warn(mmc_dev(host->mmc), "powerup timeout\n");
  772. }
  773. /* FIXME on power OFF, reset things ... */
  774. }
  775. static void
  776. mmc_davinci_xfer_done(struct mmc_davinci_host *host, struct mmc_data *data)
  777. {
  778. host->data = NULL;
  779. if (host->mmc->caps & MMC_CAP_SDIO_IRQ) {
  780. /*
  781. * SDIO Interrupt Detection work-around as suggested by
  782. * Davinci Errata (TMS320DM355 Silicon Revision 1.1 Errata
  783. * 2.1.6): Signal SDIO interrupt only if it is enabled by core
  784. */
  785. if (host->sdio_int && !(readl(host->base + DAVINCI_SDIOST0) &
  786. SDIOST0_DAT1_HI)) {
  787. writel(SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
  788. mmc_signal_sdio_irq(host->mmc);
  789. }
  790. }
  791. if (host->do_dma) {
  792. davinci_abort_dma(host);
  793. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  794. (data->flags & MMC_DATA_WRITE)
  795. ? DMA_TO_DEVICE
  796. : DMA_FROM_DEVICE);
  797. host->do_dma = false;
  798. }
  799. host->data_dir = DAVINCI_MMC_DATADIR_NONE;
  800. if (!data->stop || (host->cmd && host->cmd->error)) {
  801. mmc_request_done(host->mmc, data->mrq);
  802. writel(0, host->base + DAVINCI_MMCIM);
  803. host->active_request = false;
  804. } else
  805. mmc_davinci_start_command(host, data->stop);
  806. }
  807. static void mmc_davinci_cmd_done(struct mmc_davinci_host *host,
  808. struct mmc_command *cmd)
  809. {
  810. host->cmd = NULL;
  811. if (cmd->flags & MMC_RSP_PRESENT) {
  812. if (cmd->flags & MMC_RSP_136) {
  813. /* response type 2 */
  814. cmd->resp[3] = readl(host->base + DAVINCI_MMCRSP01);
  815. cmd->resp[2] = readl(host->base + DAVINCI_MMCRSP23);
  816. cmd->resp[1] = readl(host->base + DAVINCI_MMCRSP45);
  817. cmd->resp[0] = readl(host->base + DAVINCI_MMCRSP67);
  818. } else {
  819. /* response types 1, 1b, 3, 4, 5, 6 */
  820. cmd->resp[0] = readl(host->base + DAVINCI_MMCRSP67);
  821. }
  822. }
  823. if (host->data == NULL || cmd->error) {
  824. if (cmd->error == -ETIMEDOUT)
  825. cmd->mrq->cmd->retries = 0;
  826. mmc_request_done(host->mmc, cmd->mrq);
  827. writel(0, host->base + DAVINCI_MMCIM);
  828. host->active_request = false;
  829. }
  830. }
  831. static inline void mmc_davinci_reset_ctrl(struct mmc_davinci_host *host,
  832. int val)
  833. {
  834. u32 temp;
  835. temp = readl(host->base + DAVINCI_MMCCTL);
  836. if (val) /* reset */
  837. temp |= MMCCTL_CMDRST | MMCCTL_DATRST;
  838. else /* enable */
  839. temp &= ~(MMCCTL_CMDRST | MMCCTL_DATRST);
  840. writel(temp, host->base + DAVINCI_MMCCTL);
  841. udelay(10);
  842. }
  843. static void
  844. davinci_abort_data(struct mmc_davinci_host *host, struct mmc_data *data)
  845. {
  846. mmc_davinci_reset_ctrl(host, 1);
  847. mmc_davinci_reset_ctrl(host, 0);
  848. }
  849. static irqreturn_t mmc_davinci_sdio_irq(int irq, void *dev_id)
  850. {
  851. struct mmc_davinci_host *host = dev_id;
  852. unsigned int status;
  853. status = readl(host->base + DAVINCI_SDIOIST);
  854. if (status & SDIOIST_IOINT) {
  855. dev_dbg(mmc_dev(host->mmc),
  856. "SDIO interrupt status %x\n", status);
  857. writel(status | SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
  858. mmc_signal_sdio_irq(host->mmc);
  859. }
  860. return IRQ_HANDLED;
  861. }
  862. static irqreturn_t mmc_davinci_irq(int irq, void *dev_id)
  863. {
  864. struct mmc_davinci_host *host = (struct mmc_davinci_host *)dev_id;
  865. unsigned int status, qstatus;
  866. int end_command = 0;
  867. int end_transfer = 0;
  868. struct mmc_data *data = host->data;
  869. if (host->cmd == NULL && host->data == NULL) {
  870. status = readl(host->base + DAVINCI_MMCST0);
  871. dev_dbg(mmc_dev(host->mmc),
  872. "Spurious interrupt 0x%04x\n", status);
  873. /* Disable the interrupt from mmcsd */
  874. writel(0, host->base + DAVINCI_MMCIM);
  875. return IRQ_NONE;
  876. }
  877. status = readl(host->base + DAVINCI_MMCST0);
  878. qstatus = status;
  879. /* handle FIFO first when using PIO for data.
  880. * bytes_left will decrease to zero as I/O progress and status will
  881. * read zero over iteration because this controller status
  882. * register(MMCST0) reports any status only once and it is cleared
  883. * by read. So, it is not unbouned loop even in the case of
  884. * non-dma.
  885. */
  886. if (host->bytes_left && (status & (MMCST0_DXRDY | MMCST0_DRRDY))) {
  887. unsigned long im_val;
  888. /*
  889. * If interrupts fire during the following loop, they will be
  890. * handled by the handler, but the PIC will still buffer these.
  891. * As a result, the handler will be called again to serve these
  892. * needlessly. In order to avoid these spurious interrupts,
  893. * keep interrupts masked during the loop.
  894. */
  895. im_val = readl(host->base + DAVINCI_MMCIM);
  896. writel(0, host->base + DAVINCI_MMCIM);
  897. do {
  898. davinci_fifo_data_trans(host, rw_threshold);
  899. status = readl(host->base + DAVINCI_MMCST0);
  900. qstatus |= status;
  901. } while (host->bytes_left &&
  902. (status & (MMCST0_DXRDY | MMCST0_DRRDY)));
  903. /*
  904. * If an interrupt is pending, it is assumed it will fire when
  905. * it is unmasked. This assumption is also taken when the MMCIM
  906. * is first set. Otherwise, writing to MMCIM after reading the
  907. * status is race-prone.
  908. */
  909. writel(im_val, host->base + DAVINCI_MMCIM);
  910. }
  911. if (qstatus & MMCST0_DATDNE) {
  912. /* All blocks sent/received, and CRC checks passed */
  913. if (data != NULL) {
  914. if ((host->do_dma == 0) && (host->bytes_left > 0)) {
  915. /* if datasize < rw_threshold
  916. * no RX ints are generated
  917. */
  918. davinci_fifo_data_trans(host, host->bytes_left);
  919. }
  920. end_transfer = 1;
  921. data->bytes_xfered = data->blocks * data->blksz;
  922. } else {
  923. dev_err(mmc_dev(host->mmc),
  924. "DATDNE with no host->data\n");
  925. }
  926. }
  927. if (qstatus & MMCST0_TOUTRD) {
  928. /* Read data timeout */
  929. data->error = -ETIMEDOUT;
  930. end_transfer = 1;
  931. dev_dbg(mmc_dev(host->mmc),
  932. "read data timeout, status %x\n",
  933. qstatus);
  934. davinci_abort_data(host, data);
  935. }
  936. if (qstatus & (MMCST0_CRCWR | MMCST0_CRCRD)) {
  937. /* Data CRC error */
  938. data->error = -EILSEQ;
  939. end_transfer = 1;
  940. /* NOTE: this controller uses CRCWR to report both CRC
  941. * errors and timeouts (on writes). MMCDRSP values are
  942. * only weakly documented, but 0x9f was clearly a timeout
  943. * case and the two three-bit patterns in various SD specs
  944. * (101, 010) aren't part of it ...
  945. */
  946. if (qstatus & MMCST0_CRCWR) {
  947. u32 temp = readb(host->base + DAVINCI_MMCDRSP);
  948. if (temp == 0x9f)
  949. data->error = -ETIMEDOUT;
  950. }
  951. dev_dbg(mmc_dev(host->mmc), "data %s %s error\n",
  952. (qstatus & MMCST0_CRCWR) ? "write" : "read",
  953. (data->error == -ETIMEDOUT) ? "timeout" : "CRC");
  954. davinci_abort_data(host, data);
  955. }
  956. if (qstatus & MMCST0_TOUTRS) {
  957. /* Command timeout */
  958. if (host->cmd) {
  959. dev_dbg(mmc_dev(host->mmc),
  960. "CMD%d timeout, status %x\n",
  961. host->cmd->opcode, qstatus);
  962. host->cmd->error = -ETIMEDOUT;
  963. if (data) {
  964. end_transfer = 1;
  965. davinci_abort_data(host, data);
  966. } else
  967. end_command = 1;
  968. }
  969. }
  970. if (qstatus & MMCST0_CRCRS) {
  971. /* Command CRC error */
  972. dev_dbg(mmc_dev(host->mmc), "Command CRC error\n");
  973. if (host->cmd) {
  974. host->cmd->error = -EILSEQ;
  975. end_command = 1;
  976. }
  977. }
  978. if (qstatus & MMCST0_RSPDNE) {
  979. /* End of command phase */
  980. end_command = (int) host->cmd;
  981. }
  982. if (end_command)
  983. mmc_davinci_cmd_done(host, host->cmd);
  984. if (end_transfer)
  985. mmc_davinci_xfer_done(host, data);
  986. return IRQ_HANDLED;
  987. }
  988. static int mmc_davinci_get_cd(struct mmc_host *mmc)
  989. {
  990. struct platform_device *pdev = to_platform_device(mmc->parent);
  991. struct davinci_mmc_config *config = pdev->dev.platform_data;
  992. if (!config || !config->get_cd)
  993. return -ENOSYS;
  994. return config->get_cd(pdev->id);
  995. }
  996. static int mmc_davinci_get_ro(struct mmc_host *mmc)
  997. {
  998. struct platform_device *pdev = to_platform_device(mmc->parent);
  999. struct davinci_mmc_config *config = pdev->dev.platform_data;
  1000. if (!config || !config->get_ro)
  1001. return -ENOSYS;
  1002. return config->get_ro(pdev->id);
  1003. }
  1004. static void mmc_davinci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1005. {
  1006. struct mmc_davinci_host *host = mmc_priv(mmc);
  1007. if (enable) {
  1008. if (!(readl(host->base + DAVINCI_SDIOST0) & SDIOST0_DAT1_HI)) {
  1009. writel(SDIOIST_IOINT, host->base + DAVINCI_SDIOIST);
  1010. mmc_signal_sdio_irq(host->mmc);
  1011. } else {
  1012. host->sdio_int = true;
  1013. writel(readl(host->base + DAVINCI_SDIOIEN) |
  1014. SDIOIEN_IOINTEN, host->base + DAVINCI_SDIOIEN);
  1015. }
  1016. } else {
  1017. host->sdio_int = false;
  1018. writel(readl(host->base + DAVINCI_SDIOIEN) & ~SDIOIEN_IOINTEN,
  1019. host->base + DAVINCI_SDIOIEN);
  1020. }
  1021. }
  1022. static struct mmc_host_ops mmc_davinci_ops = {
  1023. .request = mmc_davinci_request,
  1024. .set_ios = mmc_davinci_set_ios,
  1025. .get_cd = mmc_davinci_get_cd,
  1026. .get_ro = mmc_davinci_get_ro,
  1027. .enable_sdio_irq = mmc_davinci_enable_sdio_irq,
  1028. };
  1029. /*----------------------------------------------------------------------*/
  1030. #ifdef CONFIG_CPU_FREQ
  1031. static int mmc_davinci_cpufreq_transition(struct notifier_block *nb,
  1032. unsigned long val, void *data)
  1033. {
  1034. struct mmc_davinci_host *host;
  1035. unsigned int mmc_pclk;
  1036. struct mmc_host *mmc;
  1037. unsigned long flags;
  1038. host = container_of(nb, struct mmc_davinci_host, freq_transition);
  1039. mmc = host->mmc;
  1040. mmc_pclk = clk_get_rate(host->clk);
  1041. if (val == CPUFREQ_POSTCHANGE) {
  1042. spin_lock_irqsave(&mmc->lock, flags);
  1043. host->mmc_input_clk = mmc_pclk;
  1044. calculate_clk_divider(mmc, &mmc->ios);
  1045. spin_unlock_irqrestore(&mmc->lock, flags);
  1046. }
  1047. return 0;
  1048. }
  1049. static inline int mmc_davinci_cpufreq_register(struct mmc_davinci_host *host)
  1050. {
  1051. host->freq_transition.notifier_call = mmc_davinci_cpufreq_transition;
  1052. return cpufreq_register_notifier(&host->freq_transition,
  1053. CPUFREQ_TRANSITION_NOTIFIER);
  1054. }
  1055. static inline void mmc_davinci_cpufreq_deregister(struct mmc_davinci_host *host)
  1056. {
  1057. cpufreq_unregister_notifier(&host->freq_transition,
  1058. CPUFREQ_TRANSITION_NOTIFIER);
  1059. }
  1060. #else
  1061. static inline int mmc_davinci_cpufreq_register(struct mmc_davinci_host *host)
  1062. {
  1063. return 0;
  1064. }
  1065. static inline void mmc_davinci_cpufreq_deregister(struct mmc_davinci_host *host)
  1066. {
  1067. }
  1068. #endif
  1069. static void __init init_mmcsd_host(struct mmc_davinci_host *host)
  1070. {
  1071. mmc_davinci_reset_ctrl(host, 1);
  1072. writel(0, host->base + DAVINCI_MMCCLK);
  1073. writel(MMCCLK_CLKEN, host->base + DAVINCI_MMCCLK);
  1074. writel(0x1FFF, host->base + DAVINCI_MMCTOR);
  1075. writel(0xFFFF, host->base + DAVINCI_MMCTOD);
  1076. mmc_davinci_reset_ctrl(host, 0);
  1077. }
  1078. static int __init davinci_mmcsd_probe(struct platform_device *pdev)
  1079. {
  1080. struct davinci_mmc_config *pdata = pdev->dev.platform_data;
  1081. struct mmc_davinci_host *host = NULL;
  1082. struct mmc_host *mmc = NULL;
  1083. struct resource *r, *mem = NULL;
  1084. int ret = 0, irq = 0;
  1085. size_t mem_size;
  1086. /* REVISIT: when we're fully converted, fail if pdata is NULL */
  1087. ret = -ENODEV;
  1088. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1089. irq = platform_get_irq(pdev, 0);
  1090. if (!r || irq == NO_IRQ)
  1091. goto out;
  1092. ret = -EBUSY;
  1093. mem_size = resource_size(r);
  1094. mem = request_mem_region(r->start, mem_size, pdev->name);
  1095. if (!mem)
  1096. goto out;
  1097. ret = -ENOMEM;
  1098. mmc = mmc_alloc_host(sizeof(struct mmc_davinci_host), &pdev->dev);
  1099. if (!mmc)
  1100. goto out;
  1101. host = mmc_priv(mmc);
  1102. host->mmc = mmc; /* Important */
  1103. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1104. if (!r)
  1105. goto out;
  1106. host->rxdma = r->start;
  1107. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1108. if (!r)
  1109. goto out;
  1110. host->txdma = r->start;
  1111. host->mem_res = mem;
  1112. host->base = ioremap(mem->start, mem_size);
  1113. if (!host->base)
  1114. goto out;
  1115. ret = -ENXIO;
  1116. host->clk = clk_get(&pdev->dev, "MMCSDCLK");
  1117. if (IS_ERR(host->clk)) {
  1118. ret = PTR_ERR(host->clk);
  1119. goto out;
  1120. }
  1121. clk_enable(host->clk);
  1122. host->mmc_input_clk = clk_get_rate(host->clk);
  1123. init_mmcsd_host(host);
  1124. if (pdata->nr_sg)
  1125. host->nr_sg = pdata->nr_sg - 1;
  1126. if (host->nr_sg > MAX_NR_SG || !host->nr_sg)
  1127. host->nr_sg = MAX_NR_SG;
  1128. host->use_dma = use_dma;
  1129. host->mmc_irq = irq;
  1130. host->sdio_irq = platform_get_irq(pdev, 1);
  1131. if (host->use_dma && davinci_acquire_dma_channels(host) != 0)
  1132. host->use_dma = 0;
  1133. /* REVISIT: someday, support IRQ-driven card detection. */
  1134. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1135. mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  1136. if (pdata && (pdata->wires == 4 || pdata->wires == 0))
  1137. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1138. if (pdata && (pdata->wires == 8))
  1139. mmc->caps |= (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA);
  1140. host->version = pdata->version;
  1141. mmc->ops = &mmc_davinci_ops;
  1142. mmc->f_min = 312500;
  1143. mmc->f_max = 25000000;
  1144. if (pdata && pdata->max_freq)
  1145. mmc->f_max = pdata->max_freq;
  1146. if (pdata && pdata->caps)
  1147. mmc->caps |= pdata->caps;
  1148. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1149. /* With no iommu coalescing pages, each phys_seg is a hw_seg.
  1150. * Each hw_seg uses one EDMA parameter RAM slot, always one
  1151. * channel and then usually some linked slots.
  1152. */
  1153. mmc->max_segs = 1 + host->n_link;
  1154. /* EDMA limit per hw segment (one or two MBytes) */
  1155. mmc->max_seg_size = MAX_CCNT * rw_threshold;
  1156. /* MMC/SD controller limits for multiblock requests */
  1157. mmc->max_blk_size = 4095; /* BLEN is 12 bits */
  1158. mmc->max_blk_count = 65535; /* NBLK is 16 bits */
  1159. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1160. dev_dbg(mmc_dev(host->mmc), "max_segs=%d\n", mmc->max_segs);
  1161. dev_dbg(mmc_dev(host->mmc), "max_blk_size=%d\n", mmc->max_blk_size);
  1162. dev_dbg(mmc_dev(host->mmc), "max_req_size=%d\n", mmc->max_req_size);
  1163. dev_dbg(mmc_dev(host->mmc), "max_seg_size=%d\n", mmc->max_seg_size);
  1164. platform_set_drvdata(pdev, host);
  1165. ret = mmc_davinci_cpufreq_register(host);
  1166. if (ret) {
  1167. dev_err(&pdev->dev, "failed to register cpufreq\n");
  1168. goto cpu_freq_fail;
  1169. }
  1170. ret = mmc_add_host(mmc);
  1171. if (ret < 0)
  1172. goto out;
  1173. ret = request_irq(irq, mmc_davinci_irq, 0, mmc_hostname(mmc), host);
  1174. if (ret)
  1175. goto out;
  1176. if (host->sdio_irq >= 0) {
  1177. ret = request_irq(host->sdio_irq, mmc_davinci_sdio_irq, 0,
  1178. mmc_hostname(mmc), host);
  1179. if (!ret)
  1180. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1181. }
  1182. rename_region(mem, mmc_hostname(mmc));
  1183. dev_info(mmc_dev(host->mmc), "Using %s, %d-bit mode\n",
  1184. host->use_dma ? "DMA" : "PIO",
  1185. (mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1);
  1186. return 0;
  1187. out:
  1188. mmc_davinci_cpufreq_deregister(host);
  1189. cpu_freq_fail:
  1190. if (host) {
  1191. davinci_release_dma_channels(host);
  1192. if (host->clk) {
  1193. clk_disable(host->clk);
  1194. clk_put(host->clk);
  1195. }
  1196. if (host->base)
  1197. iounmap(host->base);
  1198. }
  1199. if (mmc)
  1200. mmc_free_host(mmc);
  1201. if (mem)
  1202. release_resource(mem);
  1203. dev_dbg(&pdev->dev, "probe err %d\n", ret);
  1204. return ret;
  1205. }
  1206. static int __exit davinci_mmcsd_remove(struct platform_device *pdev)
  1207. {
  1208. struct mmc_davinci_host *host = platform_get_drvdata(pdev);
  1209. platform_set_drvdata(pdev, NULL);
  1210. if (host) {
  1211. mmc_davinci_cpufreq_deregister(host);
  1212. mmc_remove_host(host->mmc);
  1213. free_irq(host->mmc_irq, host);
  1214. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  1215. free_irq(host->sdio_irq, host);
  1216. davinci_release_dma_channels(host);
  1217. clk_disable(host->clk);
  1218. clk_put(host->clk);
  1219. iounmap(host->base);
  1220. release_resource(host->mem_res);
  1221. mmc_free_host(host->mmc);
  1222. }
  1223. return 0;
  1224. }
  1225. #ifdef CONFIG_PM
  1226. static int davinci_mmcsd_suspend(struct device *dev)
  1227. {
  1228. struct platform_device *pdev = to_platform_device(dev);
  1229. struct mmc_davinci_host *host = platform_get_drvdata(pdev);
  1230. int ret;
  1231. ret = mmc_suspend_host(host->mmc);
  1232. if (!ret) {
  1233. writel(0, host->base + DAVINCI_MMCIM);
  1234. mmc_davinci_reset_ctrl(host, 1);
  1235. clk_disable(host->clk);
  1236. host->suspended = 1;
  1237. } else {
  1238. host->suspended = 0;
  1239. }
  1240. return ret;
  1241. }
  1242. static int davinci_mmcsd_resume(struct device *dev)
  1243. {
  1244. struct platform_device *pdev = to_platform_device(dev);
  1245. struct mmc_davinci_host *host = platform_get_drvdata(pdev);
  1246. int ret;
  1247. if (!host->suspended)
  1248. return 0;
  1249. clk_enable(host->clk);
  1250. mmc_davinci_reset_ctrl(host, 0);
  1251. ret = mmc_resume_host(host->mmc);
  1252. if (!ret)
  1253. host->suspended = 0;
  1254. return ret;
  1255. }
  1256. static const struct dev_pm_ops davinci_mmcsd_pm = {
  1257. .suspend = davinci_mmcsd_suspend,
  1258. .resume = davinci_mmcsd_resume,
  1259. };
  1260. #define davinci_mmcsd_pm_ops (&davinci_mmcsd_pm)
  1261. #else
  1262. #define davinci_mmcsd_pm_ops NULL
  1263. #endif
  1264. static struct platform_driver davinci_mmcsd_driver = {
  1265. .driver = {
  1266. .name = "davinci_mmc",
  1267. .owner = THIS_MODULE,
  1268. .pm = davinci_mmcsd_pm_ops,
  1269. },
  1270. .remove = __exit_p(davinci_mmcsd_remove),
  1271. };
  1272. static int __init davinci_mmcsd_init(void)
  1273. {
  1274. return platform_driver_probe(&davinci_mmcsd_driver,
  1275. davinci_mmcsd_probe);
  1276. }
  1277. module_init(davinci_mmcsd_init);
  1278. static void __exit davinci_mmcsd_exit(void)
  1279. {
  1280. platform_driver_unregister(&davinci_mmcsd_driver);
  1281. }
  1282. module_exit(davinci_mmcsd_exit);
  1283. MODULE_AUTHOR("Texas Instruments India");
  1284. MODULE_LICENSE("GPL");
  1285. MODULE_DESCRIPTION("MMC/SD driver for Davinci MMC controller");