davinci_mmc.c 38 KB

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