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