davinci_mmc.c 41 KB

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