davinci_mmc.c 39 KB

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