davinci_mmc.c 42 KB

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