davinci_mmc.c 40 KB

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