davinci_mmc.c 38 KB

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