davinci_mmc.c 39 KB

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