sunxi-mmc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * Driver for sunxi SD/MMC host controllers
  3. * (C) Copyright 2007-2011 Reuuimlla Technology Co., Ltd.
  4. * (C) Copyright 2007-2011 Aaron Maoye <leafy.myeh@reuuimllatech.com>
  5. * (C) Copyright 2013-2014 O2S GmbH <www.o2s.ch>
  6. * (C) Copyright 2013-2014 David Lanzend�rfer <david.lanzendoerfer@o2s.ch>
  7. * (C) Copyright 2013-2014 Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/io.h>
  17. #include <linux/device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/clk.h>
  22. #include <linux/gpio.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/slab.h>
  28. #include <linux/reset.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/mmc/host.h>
  34. #include <linux/mmc/sd.h>
  35. #include <linux/mmc/sdio.h>
  36. #include <linux/mmc/mmc.h>
  37. #include <linux/mmc/core.h>
  38. #include <linux/mmc/card.h>
  39. #include <linux/mmc/slot-gpio.h>
  40. /* register offset definitions */
  41. #define SDXC_REG_GCTRL (0x00) /* SMC Global Control Register */
  42. #define SDXC_REG_CLKCR (0x04) /* SMC Clock Control Register */
  43. #define SDXC_REG_TMOUT (0x08) /* SMC Time Out Register */
  44. #define SDXC_REG_WIDTH (0x0C) /* SMC Bus Width Register */
  45. #define SDXC_REG_BLKSZ (0x10) /* SMC Block Size Register */
  46. #define SDXC_REG_BCNTR (0x14) /* SMC Byte Count Register */
  47. #define SDXC_REG_CMDR (0x18) /* SMC Command Register */
  48. #define SDXC_REG_CARG (0x1C) /* SMC Argument Register */
  49. #define SDXC_REG_RESP0 (0x20) /* SMC Response Register 0 */
  50. #define SDXC_REG_RESP1 (0x24) /* SMC Response Register 1 */
  51. #define SDXC_REG_RESP2 (0x28) /* SMC Response Register 2 */
  52. #define SDXC_REG_RESP3 (0x2C) /* SMC Response Register 3 */
  53. #define SDXC_REG_IMASK (0x30) /* SMC Interrupt Mask Register */
  54. #define SDXC_REG_MISTA (0x34) /* SMC Masked Interrupt Status Register */
  55. #define SDXC_REG_RINTR (0x38) /* SMC Raw Interrupt Status Register */
  56. #define SDXC_REG_STAS (0x3C) /* SMC Status Register */
  57. #define SDXC_REG_FTRGL (0x40) /* SMC FIFO Threshold Watermark Registe */
  58. #define SDXC_REG_FUNS (0x44) /* SMC Function Select Register */
  59. #define SDXC_REG_CBCR (0x48) /* SMC CIU Byte Count Register */
  60. #define SDXC_REG_BBCR (0x4C) /* SMC BIU Byte Count Register */
  61. #define SDXC_REG_DBGC (0x50) /* SMC Debug Enable Register */
  62. #define SDXC_REG_HWRST (0x78) /* SMC Card Hardware Reset for Register */
  63. #define SDXC_REG_DMAC (0x80) /* SMC IDMAC Control Register */
  64. #define SDXC_REG_DLBA (0x84) /* SMC IDMAC Descriptor List Base Addre */
  65. #define SDXC_REG_IDST (0x88) /* SMC IDMAC Status Register */
  66. #define SDXC_REG_IDIE (0x8C) /* SMC IDMAC Interrupt Enable Register */
  67. #define SDXC_REG_CHDA (0x90)
  68. #define SDXC_REG_CBDA (0x94)
  69. /* New registers introduced in A64 */
  70. #define SDXC_REG_A12A 0x058 /* SMC Auto Command 12 Register */
  71. #define SDXC_REG_SD_NTSR 0x05C /* SMC New Timing Set Register */
  72. #define SDXC_REG_DRV_DL 0x140 /* Drive Delay Control Register */
  73. #define SDXC_REG_SAMP_DL_REG 0x144 /* SMC sample delay control */
  74. #define SDXC_REG_DS_DL_REG 0x148 /* SMC data strobe delay control */
  75. #define mmc_readl(host, reg) \
  76. readl((host)->reg_base + SDXC_##reg)
  77. #define mmc_writel(host, reg, value) \
  78. writel((value), (host)->reg_base + SDXC_##reg)
  79. /* global control register bits */
  80. #define SDXC_SOFT_RESET BIT(0)
  81. #define SDXC_FIFO_RESET BIT(1)
  82. #define SDXC_DMA_RESET BIT(2)
  83. #define SDXC_INTERRUPT_ENABLE_BIT BIT(4)
  84. #define SDXC_DMA_ENABLE_BIT BIT(5)
  85. #define SDXC_DEBOUNCE_ENABLE_BIT BIT(8)
  86. #define SDXC_POSEDGE_LATCH_DATA BIT(9)
  87. #define SDXC_DDR_MODE BIT(10)
  88. #define SDXC_MEMORY_ACCESS_DONE BIT(29)
  89. #define SDXC_ACCESS_DONE_DIRECT BIT(30)
  90. #define SDXC_ACCESS_BY_AHB BIT(31)
  91. #define SDXC_ACCESS_BY_DMA (0 << 31)
  92. #define SDXC_HARDWARE_RESET \
  93. (SDXC_SOFT_RESET | SDXC_FIFO_RESET | SDXC_DMA_RESET)
  94. /* clock control bits */
  95. #define SDXC_CARD_CLOCK_ON BIT(16)
  96. #define SDXC_LOW_POWER_ON BIT(17)
  97. /* bus width */
  98. #define SDXC_WIDTH1 0
  99. #define SDXC_WIDTH4 1
  100. #define SDXC_WIDTH8 2
  101. /* smc command bits */
  102. #define SDXC_RESP_EXPIRE BIT(6)
  103. #define SDXC_LONG_RESPONSE BIT(7)
  104. #define SDXC_CHECK_RESPONSE_CRC BIT(8)
  105. #define SDXC_DATA_EXPIRE BIT(9)
  106. #define SDXC_WRITE BIT(10)
  107. #define SDXC_SEQUENCE_MODE BIT(11)
  108. #define SDXC_SEND_AUTO_STOP BIT(12)
  109. #define SDXC_WAIT_PRE_OVER BIT(13)
  110. #define SDXC_STOP_ABORT_CMD BIT(14)
  111. #define SDXC_SEND_INIT_SEQUENCE BIT(15)
  112. #define SDXC_UPCLK_ONLY BIT(21)
  113. #define SDXC_READ_CEATA_DEV BIT(22)
  114. #define SDXC_CCS_EXPIRE BIT(23)
  115. #define SDXC_ENABLE_BIT_BOOT BIT(24)
  116. #define SDXC_ALT_BOOT_OPTIONS BIT(25)
  117. #define SDXC_BOOT_ACK_EXPIRE BIT(26)
  118. #define SDXC_BOOT_ABORT BIT(27)
  119. #define SDXC_VOLTAGE_SWITCH BIT(28)
  120. #define SDXC_USE_HOLD_REGISTER BIT(29)
  121. #define SDXC_START BIT(31)
  122. /* interrupt bits */
  123. #define SDXC_RESP_ERROR BIT(1)
  124. #define SDXC_COMMAND_DONE BIT(2)
  125. #define SDXC_DATA_OVER BIT(3)
  126. #define SDXC_TX_DATA_REQUEST BIT(4)
  127. #define SDXC_RX_DATA_REQUEST BIT(5)
  128. #define SDXC_RESP_CRC_ERROR BIT(6)
  129. #define SDXC_DATA_CRC_ERROR BIT(7)
  130. #define SDXC_RESP_TIMEOUT BIT(8)
  131. #define SDXC_DATA_TIMEOUT BIT(9)
  132. #define SDXC_VOLTAGE_CHANGE_DONE BIT(10)
  133. #define SDXC_FIFO_RUN_ERROR BIT(11)
  134. #define SDXC_HARD_WARE_LOCKED BIT(12)
  135. #define SDXC_START_BIT_ERROR BIT(13)
  136. #define SDXC_AUTO_COMMAND_DONE BIT(14)
  137. #define SDXC_END_BIT_ERROR BIT(15)
  138. #define SDXC_SDIO_INTERRUPT BIT(16)
  139. #define SDXC_CARD_INSERT BIT(30)
  140. #define SDXC_CARD_REMOVE BIT(31)
  141. #define SDXC_INTERRUPT_ERROR_BIT \
  142. (SDXC_RESP_ERROR | SDXC_RESP_CRC_ERROR | SDXC_DATA_CRC_ERROR | \
  143. SDXC_RESP_TIMEOUT | SDXC_DATA_TIMEOUT | SDXC_FIFO_RUN_ERROR | \
  144. SDXC_HARD_WARE_LOCKED | SDXC_START_BIT_ERROR | SDXC_END_BIT_ERROR)
  145. #define SDXC_INTERRUPT_DONE_BIT \
  146. (SDXC_AUTO_COMMAND_DONE | SDXC_DATA_OVER | \
  147. SDXC_COMMAND_DONE | SDXC_VOLTAGE_CHANGE_DONE)
  148. /* status */
  149. #define SDXC_RXWL_FLAG BIT(0)
  150. #define SDXC_TXWL_FLAG BIT(1)
  151. #define SDXC_FIFO_EMPTY BIT(2)
  152. #define SDXC_FIFO_FULL BIT(3)
  153. #define SDXC_CARD_PRESENT BIT(8)
  154. #define SDXC_CARD_DATA_BUSY BIT(9)
  155. #define SDXC_DATA_FSM_BUSY BIT(10)
  156. #define SDXC_DMA_REQUEST BIT(31)
  157. #define SDXC_FIFO_SIZE 16
  158. /* Function select */
  159. #define SDXC_CEATA_ON (0xceaa << 16)
  160. #define SDXC_SEND_IRQ_RESPONSE BIT(0)
  161. #define SDXC_SDIO_READ_WAIT BIT(1)
  162. #define SDXC_ABORT_READ_DATA BIT(2)
  163. #define SDXC_SEND_CCSD BIT(8)
  164. #define SDXC_SEND_AUTO_STOPCCSD BIT(9)
  165. #define SDXC_CEATA_DEV_IRQ_ENABLE BIT(10)
  166. /* IDMA controller bus mod bit field */
  167. #define SDXC_IDMAC_SOFT_RESET BIT(0)
  168. #define SDXC_IDMAC_FIX_BURST BIT(1)
  169. #define SDXC_IDMAC_IDMA_ON BIT(7)
  170. #define SDXC_IDMAC_REFETCH_DES BIT(31)
  171. /* IDMA status bit field */
  172. #define SDXC_IDMAC_TRANSMIT_INTERRUPT BIT(0)
  173. #define SDXC_IDMAC_RECEIVE_INTERRUPT BIT(1)
  174. #define SDXC_IDMAC_FATAL_BUS_ERROR BIT(2)
  175. #define SDXC_IDMAC_DESTINATION_INVALID BIT(4)
  176. #define SDXC_IDMAC_CARD_ERROR_SUM BIT(5)
  177. #define SDXC_IDMAC_NORMAL_INTERRUPT_SUM BIT(8)
  178. #define SDXC_IDMAC_ABNORMAL_INTERRUPT_SUM BIT(9)
  179. #define SDXC_IDMAC_HOST_ABORT_INTERRUPT BIT(10)
  180. #define SDXC_IDMAC_IDLE (0 << 13)
  181. #define SDXC_IDMAC_SUSPEND (1 << 13)
  182. #define SDXC_IDMAC_DESC_READ (2 << 13)
  183. #define SDXC_IDMAC_DESC_CHECK (3 << 13)
  184. #define SDXC_IDMAC_READ_REQUEST_WAIT (4 << 13)
  185. #define SDXC_IDMAC_WRITE_REQUEST_WAIT (5 << 13)
  186. #define SDXC_IDMAC_READ (6 << 13)
  187. #define SDXC_IDMAC_WRITE (7 << 13)
  188. #define SDXC_IDMAC_DESC_CLOSE (8 << 13)
  189. /*
  190. * If the idma-des-size-bits of property is ie 13, bufsize bits are:
  191. * Bits 0-12: buf1 size
  192. * Bits 13-25: buf2 size
  193. * Bits 26-31: not used
  194. * Since we only ever set buf1 size, we can simply store it directly.
  195. */
  196. #define SDXC_IDMAC_DES0_DIC BIT(1) /* disable interrupt on completion */
  197. #define SDXC_IDMAC_DES0_LD BIT(2) /* last descriptor */
  198. #define SDXC_IDMAC_DES0_FD BIT(3) /* first descriptor */
  199. #define SDXC_IDMAC_DES0_CH BIT(4) /* chain mode */
  200. #define SDXC_IDMAC_DES0_ER BIT(5) /* end of ring */
  201. #define SDXC_IDMAC_DES0_CES BIT(30) /* card error summary */
  202. #define SDXC_IDMAC_DES0_OWN BIT(31) /* 1-idma owns it, 0-host owns it */
  203. #define SDXC_CLK_400K 0
  204. #define SDXC_CLK_25M 1
  205. #define SDXC_CLK_50M 2
  206. #define SDXC_CLK_50M_DDR 3
  207. #define SDXC_CLK_50M_DDR_8BIT 4
  208. #define SDXC_2X_TIMING_MODE BIT(31)
  209. #define SDXC_CAL_START BIT(15)
  210. #define SDXC_CAL_DONE BIT(14)
  211. #define SDXC_CAL_DL_SHIFT 8
  212. #define SDXC_CAL_DL_SW_EN BIT(7)
  213. #define SDXC_CAL_DL_SW_SHIFT 0
  214. #define SDXC_CAL_DL_MASK 0x3f
  215. #define SDXC_CAL_TIMEOUT 3 /* in seconds, 3s is enough*/
  216. struct sunxi_mmc_clk_delay {
  217. u32 output;
  218. u32 sample;
  219. };
  220. struct sunxi_idma_des {
  221. __le32 config;
  222. __le32 buf_size;
  223. __le32 buf_addr_ptr1;
  224. __le32 buf_addr_ptr2;
  225. };
  226. struct sunxi_mmc_cfg {
  227. u32 idma_des_size_bits;
  228. const struct sunxi_mmc_clk_delay *clk_delays;
  229. /* does the IP block support autocalibration? */
  230. bool can_calibrate;
  231. };
  232. struct sunxi_mmc_host {
  233. struct mmc_host *mmc;
  234. struct reset_control *reset;
  235. const struct sunxi_mmc_cfg *cfg;
  236. /* IO mapping base */
  237. void __iomem *reg_base;
  238. /* clock management */
  239. struct clk *clk_ahb;
  240. struct clk *clk_mmc;
  241. struct clk *clk_sample;
  242. struct clk *clk_output;
  243. /* irq */
  244. spinlock_t lock;
  245. int irq;
  246. u32 int_sum;
  247. u32 sdio_imask;
  248. /* dma */
  249. dma_addr_t sg_dma;
  250. void *sg_cpu;
  251. bool wait_dma;
  252. struct mmc_request *mrq;
  253. struct mmc_request *manual_stop_mrq;
  254. int ferror;
  255. /* vqmmc */
  256. bool vqmmc_enabled;
  257. };
  258. static int sunxi_mmc_reset_host(struct sunxi_mmc_host *host)
  259. {
  260. unsigned long expire = jiffies + msecs_to_jiffies(250);
  261. u32 rval;
  262. mmc_writel(host, REG_GCTRL, SDXC_HARDWARE_RESET);
  263. do {
  264. rval = mmc_readl(host, REG_GCTRL);
  265. } while (time_before(jiffies, expire) && (rval & SDXC_HARDWARE_RESET));
  266. if (rval & SDXC_HARDWARE_RESET) {
  267. dev_err(mmc_dev(host->mmc), "fatal err reset timeout\n");
  268. return -EIO;
  269. }
  270. return 0;
  271. }
  272. static int sunxi_mmc_init_host(struct mmc_host *mmc)
  273. {
  274. u32 rval;
  275. struct sunxi_mmc_host *host = mmc_priv(mmc);
  276. if (sunxi_mmc_reset_host(host))
  277. return -EIO;
  278. /*
  279. * Burst 8 transfers, RX trigger level: 7, TX trigger level: 8
  280. *
  281. * TODO: sun9i has a larger FIFO and supports higher trigger values
  282. */
  283. mmc_writel(host, REG_FTRGL, 0x20070008);
  284. /* Maximum timeout value */
  285. mmc_writel(host, REG_TMOUT, 0xffffffff);
  286. /* Unmask SDIO interrupt if needed */
  287. mmc_writel(host, REG_IMASK, host->sdio_imask);
  288. /* Clear all pending interrupts */
  289. mmc_writel(host, REG_RINTR, 0xffffffff);
  290. /* Debug register? undocumented */
  291. mmc_writel(host, REG_DBGC, 0xdeb);
  292. /* Enable CEATA support */
  293. mmc_writel(host, REG_FUNS, SDXC_CEATA_ON);
  294. /* Set DMA descriptor list base address */
  295. mmc_writel(host, REG_DLBA, host->sg_dma);
  296. rval = mmc_readl(host, REG_GCTRL);
  297. rval |= SDXC_INTERRUPT_ENABLE_BIT;
  298. /* Undocumented, but found in Allwinner code */
  299. rval &= ~SDXC_ACCESS_DONE_DIRECT;
  300. mmc_writel(host, REG_GCTRL, rval);
  301. return 0;
  302. }
  303. static void sunxi_mmc_init_idma_des(struct sunxi_mmc_host *host,
  304. struct mmc_data *data)
  305. {
  306. struct sunxi_idma_des *pdes = (struct sunxi_idma_des *)host->sg_cpu;
  307. dma_addr_t next_desc = host->sg_dma;
  308. int i, max_len = (1 << host->cfg->idma_des_size_bits);
  309. for (i = 0; i < data->sg_len; i++) {
  310. pdes[i].config = cpu_to_le32(SDXC_IDMAC_DES0_CH |
  311. SDXC_IDMAC_DES0_OWN |
  312. SDXC_IDMAC_DES0_DIC);
  313. if (data->sg[i].length == max_len)
  314. pdes[i].buf_size = 0; /* 0 == max_len */
  315. else
  316. pdes[i].buf_size = cpu_to_le32(data->sg[i].length);
  317. next_desc += sizeof(struct sunxi_idma_des);
  318. pdes[i].buf_addr_ptr1 =
  319. cpu_to_le32(sg_dma_address(&data->sg[i]));
  320. pdes[i].buf_addr_ptr2 = cpu_to_le32((u32)next_desc);
  321. }
  322. pdes[0].config |= cpu_to_le32(SDXC_IDMAC_DES0_FD);
  323. pdes[i - 1].config |= cpu_to_le32(SDXC_IDMAC_DES0_LD |
  324. SDXC_IDMAC_DES0_ER);
  325. pdes[i - 1].config &= cpu_to_le32(~SDXC_IDMAC_DES0_DIC);
  326. pdes[i - 1].buf_addr_ptr2 = 0;
  327. /*
  328. * Avoid the io-store starting the idmac hitting io-mem before the
  329. * descriptors hit the main-mem.
  330. */
  331. wmb();
  332. }
  333. static enum dma_data_direction sunxi_mmc_get_dma_dir(struct mmc_data *data)
  334. {
  335. if (data->flags & MMC_DATA_WRITE)
  336. return DMA_TO_DEVICE;
  337. else
  338. return DMA_FROM_DEVICE;
  339. }
  340. static int sunxi_mmc_map_dma(struct sunxi_mmc_host *host,
  341. struct mmc_data *data)
  342. {
  343. u32 i, dma_len;
  344. struct scatterlist *sg;
  345. dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  346. sunxi_mmc_get_dma_dir(data));
  347. if (dma_len == 0) {
  348. dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
  349. return -ENOMEM;
  350. }
  351. for_each_sg(data->sg, sg, data->sg_len, i) {
  352. if (sg->offset & 3 || sg->length & 3) {
  353. dev_err(mmc_dev(host->mmc),
  354. "unaligned scatterlist: os %x length %d\n",
  355. sg->offset, sg->length);
  356. return -EINVAL;
  357. }
  358. }
  359. return 0;
  360. }
  361. static void sunxi_mmc_start_dma(struct sunxi_mmc_host *host,
  362. struct mmc_data *data)
  363. {
  364. u32 rval;
  365. sunxi_mmc_init_idma_des(host, data);
  366. rval = mmc_readl(host, REG_GCTRL);
  367. rval |= SDXC_DMA_ENABLE_BIT;
  368. mmc_writel(host, REG_GCTRL, rval);
  369. rval |= SDXC_DMA_RESET;
  370. mmc_writel(host, REG_GCTRL, rval);
  371. mmc_writel(host, REG_DMAC, SDXC_IDMAC_SOFT_RESET);
  372. if (!(data->flags & MMC_DATA_WRITE))
  373. mmc_writel(host, REG_IDIE, SDXC_IDMAC_RECEIVE_INTERRUPT);
  374. mmc_writel(host, REG_DMAC,
  375. SDXC_IDMAC_FIX_BURST | SDXC_IDMAC_IDMA_ON);
  376. }
  377. static void sunxi_mmc_send_manual_stop(struct sunxi_mmc_host *host,
  378. struct mmc_request *req)
  379. {
  380. u32 arg, cmd_val, ri;
  381. unsigned long expire = jiffies + msecs_to_jiffies(1000);
  382. cmd_val = SDXC_START | SDXC_RESP_EXPIRE |
  383. SDXC_STOP_ABORT_CMD | SDXC_CHECK_RESPONSE_CRC;
  384. if (req->cmd->opcode == SD_IO_RW_EXTENDED) {
  385. cmd_val |= SD_IO_RW_DIRECT;
  386. arg = (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  387. ((req->cmd->arg >> 28) & 0x7);
  388. } else {
  389. cmd_val |= MMC_STOP_TRANSMISSION;
  390. arg = 0;
  391. }
  392. mmc_writel(host, REG_CARG, arg);
  393. mmc_writel(host, REG_CMDR, cmd_val);
  394. do {
  395. ri = mmc_readl(host, REG_RINTR);
  396. } while (!(ri & (SDXC_COMMAND_DONE | SDXC_INTERRUPT_ERROR_BIT)) &&
  397. time_before(jiffies, expire));
  398. if (!(ri & SDXC_COMMAND_DONE) || (ri & SDXC_INTERRUPT_ERROR_BIT)) {
  399. dev_err(mmc_dev(host->mmc), "send stop command failed\n");
  400. if (req->stop)
  401. req->stop->resp[0] = -ETIMEDOUT;
  402. } else {
  403. if (req->stop)
  404. req->stop->resp[0] = mmc_readl(host, REG_RESP0);
  405. }
  406. mmc_writel(host, REG_RINTR, 0xffff);
  407. }
  408. static void sunxi_mmc_dump_errinfo(struct sunxi_mmc_host *host)
  409. {
  410. struct mmc_command *cmd = host->mrq->cmd;
  411. struct mmc_data *data = host->mrq->data;
  412. /* For some cmds timeout is normal with sd/mmc cards */
  413. if ((host->int_sum & SDXC_INTERRUPT_ERROR_BIT) ==
  414. SDXC_RESP_TIMEOUT && (cmd->opcode == SD_IO_SEND_OP_COND ||
  415. cmd->opcode == SD_IO_RW_DIRECT))
  416. return;
  417. dev_err(mmc_dev(host->mmc),
  418. "smc %d err, cmd %d,%s%s%s%s%s%s%s%s%s%s !!\n",
  419. host->mmc->index, cmd->opcode,
  420. data ? (data->flags & MMC_DATA_WRITE ? " WR" : " RD") : "",
  421. host->int_sum & SDXC_RESP_ERROR ? " RE" : "",
  422. host->int_sum & SDXC_RESP_CRC_ERROR ? " RCE" : "",
  423. host->int_sum & SDXC_DATA_CRC_ERROR ? " DCE" : "",
  424. host->int_sum & SDXC_RESP_TIMEOUT ? " RTO" : "",
  425. host->int_sum & SDXC_DATA_TIMEOUT ? " DTO" : "",
  426. host->int_sum & SDXC_FIFO_RUN_ERROR ? " FE" : "",
  427. host->int_sum & SDXC_HARD_WARE_LOCKED ? " HL" : "",
  428. host->int_sum & SDXC_START_BIT_ERROR ? " SBE" : "",
  429. host->int_sum & SDXC_END_BIT_ERROR ? " EBE" : ""
  430. );
  431. }
  432. /* Called in interrupt context! */
  433. static irqreturn_t sunxi_mmc_finalize_request(struct sunxi_mmc_host *host)
  434. {
  435. struct mmc_request *mrq = host->mrq;
  436. struct mmc_data *data = mrq->data;
  437. u32 rval;
  438. mmc_writel(host, REG_IMASK, host->sdio_imask);
  439. mmc_writel(host, REG_IDIE, 0);
  440. if (host->int_sum & SDXC_INTERRUPT_ERROR_BIT) {
  441. sunxi_mmc_dump_errinfo(host);
  442. mrq->cmd->error = -ETIMEDOUT;
  443. if (data) {
  444. data->error = -ETIMEDOUT;
  445. host->manual_stop_mrq = mrq;
  446. }
  447. if (mrq->stop)
  448. mrq->stop->error = -ETIMEDOUT;
  449. } else {
  450. if (mrq->cmd->flags & MMC_RSP_136) {
  451. mrq->cmd->resp[0] = mmc_readl(host, REG_RESP3);
  452. mrq->cmd->resp[1] = mmc_readl(host, REG_RESP2);
  453. mrq->cmd->resp[2] = mmc_readl(host, REG_RESP1);
  454. mrq->cmd->resp[3] = mmc_readl(host, REG_RESP0);
  455. } else {
  456. mrq->cmd->resp[0] = mmc_readl(host, REG_RESP0);
  457. }
  458. if (data)
  459. data->bytes_xfered = data->blocks * data->blksz;
  460. }
  461. if (data) {
  462. mmc_writel(host, REG_IDST, 0x337);
  463. mmc_writel(host, REG_DMAC, 0);
  464. rval = mmc_readl(host, REG_GCTRL);
  465. rval |= SDXC_DMA_RESET;
  466. mmc_writel(host, REG_GCTRL, rval);
  467. rval &= ~SDXC_DMA_ENABLE_BIT;
  468. mmc_writel(host, REG_GCTRL, rval);
  469. rval |= SDXC_FIFO_RESET;
  470. mmc_writel(host, REG_GCTRL, rval);
  471. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  472. sunxi_mmc_get_dma_dir(data));
  473. }
  474. mmc_writel(host, REG_RINTR, 0xffff);
  475. host->mrq = NULL;
  476. host->int_sum = 0;
  477. host->wait_dma = false;
  478. return host->manual_stop_mrq ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  479. }
  480. static irqreturn_t sunxi_mmc_irq(int irq, void *dev_id)
  481. {
  482. struct sunxi_mmc_host *host = dev_id;
  483. struct mmc_request *mrq;
  484. u32 msk_int, idma_int;
  485. bool finalize = false;
  486. bool sdio_int = false;
  487. irqreturn_t ret = IRQ_HANDLED;
  488. spin_lock(&host->lock);
  489. idma_int = mmc_readl(host, REG_IDST);
  490. msk_int = mmc_readl(host, REG_MISTA);
  491. dev_dbg(mmc_dev(host->mmc), "irq: rq %p mi %08x idi %08x\n",
  492. host->mrq, msk_int, idma_int);
  493. mrq = host->mrq;
  494. if (mrq) {
  495. if (idma_int & SDXC_IDMAC_RECEIVE_INTERRUPT)
  496. host->wait_dma = false;
  497. host->int_sum |= msk_int;
  498. /* Wait for COMMAND_DONE on RESPONSE_TIMEOUT before finalize */
  499. if ((host->int_sum & SDXC_RESP_TIMEOUT) &&
  500. !(host->int_sum & SDXC_COMMAND_DONE))
  501. mmc_writel(host, REG_IMASK,
  502. host->sdio_imask | SDXC_COMMAND_DONE);
  503. /* Don't wait for dma on error */
  504. else if (host->int_sum & SDXC_INTERRUPT_ERROR_BIT)
  505. finalize = true;
  506. else if ((host->int_sum & SDXC_INTERRUPT_DONE_BIT) &&
  507. !host->wait_dma)
  508. finalize = true;
  509. }
  510. if (msk_int & SDXC_SDIO_INTERRUPT)
  511. sdio_int = true;
  512. mmc_writel(host, REG_RINTR, msk_int);
  513. mmc_writel(host, REG_IDST, idma_int);
  514. if (finalize)
  515. ret = sunxi_mmc_finalize_request(host);
  516. spin_unlock(&host->lock);
  517. if (finalize && ret == IRQ_HANDLED)
  518. mmc_request_done(host->mmc, mrq);
  519. if (sdio_int)
  520. mmc_signal_sdio_irq(host->mmc);
  521. return ret;
  522. }
  523. static irqreturn_t sunxi_mmc_handle_manual_stop(int irq, void *dev_id)
  524. {
  525. struct sunxi_mmc_host *host = dev_id;
  526. struct mmc_request *mrq;
  527. unsigned long iflags;
  528. spin_lock_irqsave(&host->lock, iflags);
  529. mrq = host->manual_stop_mrq;
  530. spin_unlock_irqrestore(&host->lock, iflags);
  531. if (!mrq) {
  532. dev_err(mmc_dev(host->mmc), "no request for manual stop\n");
  533. return IRQ_HANDLED;
  534. }
  535. dev_err(mmc_dev(host->mmc), "data error, sending stop command\n");
  536. /*
  537. * We will never have more than one outstanding request,
  538. * and we do not complete the request until after
  539. * we've cleared host->manual_stop_mrq so we do not need to
  540. * spin lock this function.
  541. * Additionally we have wait states within this function
  542. * so having it in a lock is a very bad idea.
  543. */
  544. sunxi_mmc_send_manual_stop(host, mrq);
  545. spin_lock_irqsave(&host->lock, iflags);
  546. host->manual_stop_mrq = NULL;
  547. spin_unlock_irqrestore(&host->lock, iflags);
  548. mmc_request_done(host->mmc, mrq);
  549. return IRQ_HANDLED;
  550. }
  551. static int sunxi_mmc_oclk_onoff(struct sunxi_mmc_host *host, u32 oclk_en)
  552. {
  553. unsigned long expire = jiffies + msecs_to_jiffies(750);
  554. u32 rval;
  555. rval = mmc_readl(host, REG_CLKCR);
  556. rval &= ~(SDXC_CARD_CLOCK_ON | SDXC_LOW_POWER_ON);
  557. if (oclk_en)
  558. rval |= SDXC_CARD_CLOCK_ON;
  559. mmc_writel(host, REG_CLKCR, rval);
  560. rval = SDXC_START | SDXC_UPCLK_ONLY | SDXC_WAIT_PRE_OVER;
  561. mmc_writel(host, REG_CMDR, rval);
  562. do {
  563. rval = mmc_readl(host, REG_CMDR);
  564. } while (time_before(jiffies, expire) && (rval & SDXC_START));
  565. /* clear irq status bits set by the command */
  566. mmc_writel(host, REG_RINTR,
  567. mmc_readl(host, REG_RINTR) & ~SDXC_SDIO_INTERRUPT);
  568. if (rval & SDXC_START) {
  569. dev_err(mmc_dev(host->mmc), "fatal err update clk timeout\n");
  570. return -EIO;
  571. }
  572. return 0;
  573. }
  574. static int sunxi_mmc_calibrate(struct sunxi_mmc_host *host, int reg_off)
  575. {
  576. u32 reg = readl(host->reg_base + reg_off);
  577. u32 delay;
  578. unsigned long timeout;
  579. if (!host->cfg->can_calibrate)
  580. return 0;
  581. reg &= ~(SDXC_CAL_DL_MASK << SDXC_CAL_DL_SW_SHIFT);
  582. reg &= ~SDXC_CAL_DL_SW_EN;
  583. writel(reg | SDXC_CAL_START, host->reg_base + reg_off);
  584. dev_dbg(mmc_dev(host->mmc), "calibration started\n");
  585. timeout = jiffies + HZ * SDXC_CAL_TIMEOUT;
  586. while (!((reg = readl(host->reg_base + reg_off)) & SDXC_CAL_DONE)) {
  587. if (time_before(jiffies, timeout))
  588. cpu_relax();
  589. else {
  590. reg &= ~SDXC_CAL_START;
  591. writel(reg, host->reg_base + reg_off);
  592. return -ETIMEDOUT;
  593. }
  594. }
  595. delay = (reg >> SDXC_CAL_DL_SHIFT) & SDXC_CAL_DL_MASK;
  596. reg &= ~SDXC_CAL_START;
  597. reg |= (delay << SDXC_CAL_DL_SW_SHIFT) | SDXC_CAL_DL_SW_EN;
  598. writel(reg, host->reg_base + reg_off);
  599. dev_dbg(mmc_dev(host->mmc), "calibration ended, reg is 0x%x\n", reg);
  600. return 0;
  601. }
  602. static int sunxi_mmc_clk_set_phase(struct sunxi_mmc_host *host,
  603. struct mmc_ios *ios, u32 rate)
  604. {
  605. int index;
  606. if (!host->cfg->clk_delays)
  607. return 0;
  608. /* determine delays */
  609. if (rate <= 400000) {
  610. index = SDXC_CLK_400K;
  611. } else if (rate <= 25000000) {
  612. index = SDXC_CLK_25M;
  613. } else if (rate <= 52000000) {
  614. if (ios->timing != MMC_TIMING_UHS_DDR50 &&
  615. ios->timing != MMC_TIMING_MMC_DDR52) {
  616. index = SDXC_CLK_50M;
  617. } else if (ios->bus_width == MMC_BUS_WIDTH_8) {
  618. index = SDXC_CLK_50M_DDR_8BIT;
  619. } else {
  620. index = SDXC_CLK_50M_DDR;
  621. }
  622. } else {
  623. return -EINVAL;
  624. }
  625. clk_set_phase(host->clk_sample, host->cfg->clk_delays[index].sample);
  626. clk_set_phase(host->clk_output, host->cfg->clk_delays[index].output);
  627. return 0;
  628. }
  629. static int sunxi_mmc_clk_set_rate(struct sunxi_mmc_host *host,
  630. struct mmc_ios *ios)
  631. {
  632. long rate;
  633. u32 rval, clock = ios->clock;
  634. int ret;
  635. /* 8 bit DDR requires a higher module clock */
  636. if (ios->timing == MMC_TIMING_MMC_DDR52 &&
  637. ios->bus_width == MMC_BUS_WIDTH_8)
  638. clock <<= 1;
  639. rate = clk_round_rate(host->clk_mmc, clock);
  640. if (rate < 0) {
  641. dev_err(mmc_dev(host->mmc), "error rounding clk to %d: %ld\n",
  642. clock, rate);
  643. return rate;
  644. }
  645. dev_dbg(mmc_dev(host->mmc), "setting clk to %d, rounded %ld\n",
  646. clock, rate);
  647. /* setting clock rate */
  648. ret = clk_set_rate(host->clk_mmc, rate);
  649. if (ret) {
  650. dev_err(mmc_dev(host->mmc), "error setting clk to %ld: %d\n",
  651. rate, ret);
  652. return ret;
  653. }
  654. ret = sunxi_mmc_oclk_onoff(host, 0);
  655. if (ret)
  656. return ret;
  657. /* clear internal divider */
  658. rval = mmc_readl(host, REG_CLKCR);
  659. rval &= ~0xff;
  660. /* set internal divider for 8 bit eMMC DDR, so card clock is right */
  661. if (ios->timing == MMC_TIMING_MMC_DDR52 &&
  662. ios->bus_width == MMC_BUS_WIDTH_8) {
  663. rval |= 1;
  664. rate >>= 1;
  665. }
  666. mmc_writel(host, REG_CLKCR, rval);
  667. ret = sunxi_mmc_clk_set_phase(host, ios, rate);
  668. if (ret)
  669. return ret;
  670. ret = sunxi_mmc_calibrate(host, SDXC_REG_SAMP_DL_REG);
  671. if (ret)
  672. return ret;
  673. /* TODO: enable calibrate on sdc2 SDXC_REG_DS_DL_REG of A64 */
  674. return sunxi_mmc_oclk_onoff(host, 1);
  675. }
  676. static void sunxi_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  677. {
  678. struct sunxi_mmc_host *host = mmc_priv(mmc);
  679. u32 rval;
  680. /* Set the power state */
  681. switch (ios->power_mode) {
  682. case MMC_POWER_ON:
  683. break;
  684. case MMC_POWER_UP:
  685. host->ferror = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  686. ios->vdd);
  687. if (host->ferror)
  688. return;
  689. if (!IS_ERR(mmc->supply.vqmmc)) {
  690. host->ferror = regulator_enable(mmc->supply.vqmmc);
  691. if (host->ferror) {
  692. dev_err(mmc_dev(mmc),
  693. "failed to enable vqmmc\n");
  694. return;
  695. }
  696. host->vqmmc_enabled = true;
  697. }
  698. host->ferror = sunxi_mmc_init_host(mmc);
  699. if (host->ferror)
  700. return;
  701. dev_dbg(mmc_dev(mmc), "power on!\n");
  702. break;
  703. case MMC_POWER_OFF:
  704. dev_dbg(mmc_dev(mmc), "power off!\n");
  705. sunxi_mmc_reset_host(host);
  706. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  707. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled)
  708. regulator_disable(mmc->supply.vqmmc);
  709. host->vqmmc_enabled = false;
  710. break;
  711. }
  712. /* set bus width */
  713. switch (ios->bus_width) {
  714. case MMC_BUS_WIDTH_1:
  715. mmc_writel(host, REG_WIDTH, SDXC_WIDTH1);
  716. break;
  717. case MMC_BUS_WIDTH_4:
  718. mmc_writel(host, REG_WIDTH, SDXC_WIDTH4);
  719. break;
  720. case MMC_BUS_WIDTH_8:
  721. mmc_writel(host, REG_WIDTH, SDXC_WIDTH8);
  722. break;
  723. }
  724. /* set ddr mode */
  725. rval = mmc_readl(host, REG_GCTRL);
  726. if (ios->timing == MMC_TIMING_UHS_DDR50 ||
  727. ios->timing == MMC_TIMING_MMC_DDR52)
  728. rval |= SDXC_DDR_MODE;
  729. else
  730. rval &= ~SDXC_DDR_MODE;
  731. mmc_writel(host, REG_GCTRL, rval);
  732. /* set up clock */
  733. if (ios->clock && ios->power_mode) {
  734. host->ferror = sunxi_mmc_clk_set_rate(host, ios);
  735. /* Android code had a usleep_range(50000, 55000); here */
  736. }
  737. }
  738. static int sunxi_mmc_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  739. {
  740. /* vqmmc regulator is available */
  741. if (!IS_ERR(mmc->supply.vqmmc))
  742. return mmc_regulator_set_vqmmc(mmc, ios);
  743. /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
  744. if (mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  745. return 0;
  746. return -EINVAL;
  747. }
  748. static void sunxi_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  749. {
  750. struct sunxi_mmc_host *host = mmc_priv(mmc);
  751. unsigned long flags;
  752. u32 imask;
  753. spin_lock_irqsave(&host->lock, flags);
  754. imask = mmc_readl(host, REG_IMASK);
  755. if (enable) {
  756. host->sdio_imask = SDXC_SDIO_INTERRUPT;
  757. imask |= SDXC_SDIO_INTERRUPT;
  758. } else {
  759. host->sdio_imask = 0;
  760. imask &= ~SDXC_SDIO_INTERRUPT;
  761. }
  762. mmc_writel(host, REG_IMASK, imask);
  763. spin_unlock_irqrestore(&host->lock, flags);
  764. }
  765. static void sunxi_mmc_hw_reset(struct mmc_host *mmc)
  766. {
  767. struct sunxi_mmc_host *host = mmc_priv(mmc);
  768. mmc_writel(host, REG_HWRST, 0);
  769. udelay(10);
  770. mmc_writel(host, REG_HWRST, 1);
  771. udelay(300);
  772. }
  773. static void sunxi_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  774. {
  775. struct sunxi_mmc_host *host = mmc_priv(mmc);
  776. struct mmc_command *cmd = mrq->cmd;
  777. struct mmc_data *data = mrq->data;
  778. unsigned long iflags;
  779. u32 imask = SDXC_INTERRUPT_ERROR_BIT;
  780. u32 cmd_val = SDXC_START | (cmd->opcode & 0x3f);
  781. bool wait_dma = host->wait_dma;
  782. int ret;
  783. /* Check for set_ios errors (should never happen) */
  784. if (host->ferror) {
  785. mrq->cmd->error = host->ferror;
  786. mmc_request_done(mmc, mrq);
  787. return;
  788. }
  789. if (data) {
  790. ret = sunxi_mmc_map_dma(host, data);
  791. if (ret < 0) {
  792. dev_err(mmc_dev(mmc), "map DMA failed\n");
  793. cmd->error = ret;
  794. data->error = ret;
  795. mmc_request_done(mmc, mrq);
  796. return;
  797. }
  798. }
  799. if (cmd->opcode == MMC_GO_IDLE_STATE) {
  800. cmd_val |= SDXC_SEND_INIT_SEQUENCE;
  801. imask |= SDXC_COMMAND_DONE;
  802. }
  803. if (cmd->flags & MMC_RSP_PRESENT) {
  804. cmd_val |= SDXC_RESP_EXPIRE;
  805. if (cmd->flags & MMC_RSP_136)
  806. cmd_val |= SDXC_LONG_RESPONSE;
  807. if (cmd->flags & MMC_RSP_CRC)
  808. cmd_val |= SDXC_CHECK_RESPONSE_CRC;
  809. if ((cmd->flags & MMC_CMD_MASK) == MMC_CMD_ADTC) {
  810. cmd_val |= SDXC_DATA_EXPIRE | SDXC_WAIT_PRE_OVER;
  811. if (cmd->data->stop) {
  812. imask |= SDXC_AUTO_COMMAND_DONE;
  813. cmd_val |= SDXC_SEND_AUTO_STOP;
  814. } else {
  815. imask |= SDXC_DATA_OVER;
  816. }
  817. if (cmd->data->flags & MMC_DATA_WRITE)
  818. cmd_val |= SDXC_WRITE;
  819. else
  820. wait_dma = true;
  821. } else {
  822. imask |= SDXC_COMMAND_DONE;
  823. }
  824. } else {
  825. imask |= SDXC_COMMAND_DONE;
  826. }
  827. dev_dbg(mmc_dev(mmc), "cmd %d(%08x) arg %x ie 0x%08x len %d\n",
  828. cmd_val & 0x3f, cmd_val, cmd->arg, imask,
  829. mrq->data ? mrq->data->blksz * mrq->data->blocks : 0);
  830. spin_lock_irqsave(&host->lock, iflags);
  831. if (host->mrq || host->manual_stop_mrq) {
  832. spin_unlock_irqrestore(&host->lock, iflags);
  833. if (data)
  834. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
  835. sunxi_mmc_get_dma_dir(data));
  836. dev_err(mmc_dev(mmc), "request already pending\n");
  837. mrq->cmd->error = -EBUSY;
  838. mmc_request_done(mmc, mrq);
  839. return;
  840. }
  841. if (data) {
  842. mmc_writel(host, REG_BLKSZ, data->blksz);
  843. mmc_writel(host, REG_BCNTR, data->blksz * data->blocks);
  844. sunxi_mmc_start_dma(host, data);
  845. }
  846. host->mrq = mrq;
  847. host->wait_dma = wait_dma;
  848. mmc_writel(host, REG_IMASK, host->sdio_imask | imask);
  849. mmc_writel(host, REG_CARG, cmd->arg);
  850. mmc_writel(host, REG_CMDR, cmd_val);
  851. spin_unlock_irqrestore(&host->lock, iflags);
  852. }
  853. static int sunxi_mmc_card_busy(struct mmc_host *mmc)
  854. {
  855. struct sunxi_mmc_host *host = mmc_priv(mmc);
  856. return !!(mmc_readl(host, REG_STAS) & SDXC_CARD_DATA_BUSY);
  857. }
  858. static struct mmc_host_ops sunxi_mmc_ops = {
  859. .request = sunxi_mmc_request,
  860. .set_ios = sunxi_mmc_set_ios,
  861. .get_ro = mmc_gpio_get_ro,
  862. .get_cd = mmc_gpio_get_cd,
  863. .enable_sdio_irq = sunxi_mmc_enable_sdio_irq,
  864. .start_signal_voltage_switch = sunxi_mmc_volt_switch,
  865. .hw_reset = sunxi_mmc_hw_reset,
  866. .card_busy = sunxi_mmc_card_busy,
  867. };
  868. static const struct sunxi_mmc_clk_delay sunxi_mmc_clk_delays[] = {
  869. [SDXC_CLK_400K] = { .output = 180, .sample = 180 },
  870. [SDXC_CLK_25M] = { .output = 180, .sample = 75 },
  871. [SDXC_CLK_50M] = { .output = 90, .sample = 120 },
  872. [SDXC_CLK_50M_DDR] = { .output = 60, .sample = 120 },
  873. /* Value from A83T "new timing mode". Works but might not be right. */
  874. [SDXC_CLK_50M_DDR_8BIT] = { .output = 90, .sample = 180 },
  875. };
  876. static const struct sunxi_mmc_clk_delay sun9i_mmc_clk_delays[] = {
  877. [SDXC_CLK_400K] = { .output = 180, .sample = 180 },
  878. [SDXC_CLK_25M] = { .output = 180, .sample = 75 },
  879. [SDXC_CLK_50M] = { .output = 150, .sample = 120 },
  880. [SDXC_CLK_50M_DDR] = { .output = 54, .sample = 36 },
  881. [SDXC_CLK_50M_DDR_8BIT] = { .output = 72, .sample = 72 },
  882. };
  883. static const struct sunxi_mmc_cfg sun4i_a10_cfg = {
  884. .idma_des_size_bits = 13,
  885. .clk_delays = NULL,
  886. .can_calibrate = false,
  887. };
  888. static const struct sunxi_mmc_cfg sun5i_a13_cfg = {
  889. .idma_des_size_bits = 16,
  890. .clk_delays = NULL,
  891. .can_calibrate = false,
  892. };
  893. static const struct sunxi_mmc_cfg sun7i_a20_cfg = {
  894. .idma_des_size_bits = 16,
  895. .clk_delays = sunxi_mmc_clk_delays,
  896. .can_calibrate = false,
  897. };
  898. static const struct sunxi_mmc_cfg sun9i_a80_cfg = {
  899. .idma_des_size_bits = 16,
  900. .clk_delays = sun9i_mmc_clk_delays,
  901. .can_calibrate = false,
  902. };
  903. static const struct sunxi_mmc_cfg sun50i_a64_cfg = {
  904. .idma_des_size_bits = 16,
  905. .clk_delays = NULL,
  906. .can_calibrate = true,
  907. };
  908. static const struct of_device_id sunxi_mmc_of_match[] = {
  909. { .compatible = "allwinner,sun4i-a10-mmc", .data = &sun4i_a10_cfg },
  910. { .compatible = "allwinner,sun5i-a13-mmc", .data = &sun5i_a13_cfg },
  911. { .compatible = "allwinner,sun7i-a20-mmc", .data = &sun7i_a20_cfg },
  912. { .compatible = "allwinner,sun9i-a80-mmc", .data = &sun9i_a80_cfg },
  913. { .compatible = "allwinner,sun50i-a64-mmc", .data = &sun50i_a64_cfg },
  914. { /* sentinel */ }
  915. };
  916. MODULE_DEVICE_TABLE(of, sunxi_mmc_of_match);
  917. static int sunxi_mmc_resource_request(struct sunxi_mmc_host *host,
  918. struct platform_device *pdev)
  919. {
  920. int ret;
  921. host->cfg = of_device_get_match_data(&pdev->dev);
  922. if (!host->cfg)
  923. return -EINVAL;
  924. ret = mmc_regulator_get_supply(host->mmc);
  925. if (ret) {
  926. if (ret != -EPROBE_DEFER)
  927. dev_err(&pdev->dev, "Could not get vmmc supply\n");
  928. return ret;
  929. }
  930. host->reg_base = devm_ioremap_resource(&pdev->dev,
  931. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  932. if (IS_ERR(host->reg_base))
  933. return PTR_ERR(host->reg_base);
  934. host->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  935. if (IS_ERR(host->clk_ahb)) {
  936. dev_err(&pdev->dev, "Could not get ahb clock\n");
  937. return PTR_ERR(host->clk_ahb);
  938. }
  939. host->clk_mmc = devm_clk_get(&pdev->dev, "mmc");
  940. if (IS_ERR(host->clk_mmc)) {
  941. dev_err(&pdev->dev, "Could not get mmc clock\n");
  942. return PTR_ERR(host->clk_mmc);
  943. }
  944. if (host->cfg->clk_delays) {
  945. host->clk_output = devm_clk_get(&pdev->dev, "output");
  946. if (IS_ERR(host->clk_output)) {
  947. dev_err(&pdev->dev, "Could not get output clock\n");
  948. return PTR_ERR(host->clk_output);
  949. }
  950. host->clk_sample = devm_clk_get(&pdev->dev, "sample");
  951. if (IS_ERR(host->clk_sample)) {
  952. dev_err(&pdev->dev, "Could not get sample clock\n");
  953. return PTR_ERR(host->clk_sample);
  954. }
  955. }
  956. host->reset = devm_reset_control_get_optional(&pdev->dev, "ahb");
  957. if (PTR_ERR(host->reset) == -EPROBE_DEFER)
  958. return PTR_ERR(host->reset);
  959. ret = clk_prepare_enable(host->clk_ahb);
  960. if (ret) {
  961. dev_err(&pdev->dev, "Enable ahb clk err %d\n", ret);
  962. return ret;
  963. }
  964. ret = clk_prepare_enable(host->clk_mmc);
  965. if (ret) {
  966. dev_err(&pdev->dev, "Enable mmc clk err %d\n", ret);
  967. goto error_disable_clk_ahb;
  968. }
  969. ret = clk_prepare_enable(host->clk_output);
  970. if (ret) {
  971. dev_err(&pdev->dev, "Enable output clk err %d\n", ret);
  972. goto error_disable_clk_mmc;
  973. }
  974. ret = clk_prepare_enable(host->clk_sample);
  975. if (ret) {
  976. dev_err(&pdev->dev, "Enable sample clk err %d\n", ret);
  977. goto error_disable_clk_output;
  978. }
  979. if (!IS_ERR(host->reset)) {
  980. ret = reset_control_deassert(host->reset);
  981. if (ret) {
  982. dev_err(&pdev->dev, "reset err %d\n", ret);
  983. goto error_disable_clk_sample;
  984. }
  985. }
  986. /*
  987. * Sometimes the controller asserts the irq on boot for some reason,
  988. * make sure the controller is in a sane state before enabling irqs.
  989. */
  990. ret = sunxi_mmc_reset_host(host);
  991. if (ret)
  992. goto error_assert_reset;
  993. host->irq = platform_get_irq(pdev, 0);
  994. return devm_request_threaded_irq(&pdev->dev, host->irq, sunxi_mmc_irq,
  995. sunxi_mmc_handle_manual_stop, 0, "sunxi-mmc", host);
  996. error_assert_reset:
  997. if (!IS_ERR(host->reset))
  998. reset_control_assert(host->reset);
  999. error_disable_clk_sample:
  1000. clk_disable_unprepare(host->clk_sample);
  1001. error_disable_clk_output:
  1002. clk_disable_unprepare(host->clk_output);
  1003. error_disable_clk_mmc:
  1004. clk_disable_unprepare(host->clk_mmc);
  1005. error_disable_clk_ahb:
  1006. clk_disable_unprepare(host->clk_ahb);
  1007. return ret;
  1008. }
  1009. static int sunxi_mmc_probe(struct platform_device *pdev)
  1010. {
  1011. struct sunxi_mmc_host *host;
  1012. struct mmc_host *mmc;
  1013. int ret;
  1014. mmc = mmc_alloc_host(sizeof(struct sunxi_mmc_host), &pdev->dev);
  1015. if (!mmc) {
  1016. dev_err(&pdev->dev, "mmc alloc host failed\n");
  1017. return -ENOMEM;
  1018. }
  1019. host = mmc_priv(mmc);
  1020. host->mmc = mmc;
  1021. spin_lock_init(&host->lock);
  1022. ret = sunxi_mmc_resource_request(host, pdev);
  1023. if (ret)
  1024. goto error_free_host;
  1025. host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  1026. &host->sg_dma, GFP_KERNEL);
  1027. if (!host->sg_cpu) {
  1028. dev_err(&pdev->dev, "Failed to allocate DMA descriptor mem\n");
  1029. ret = -ENOMEM;
  1030. goto error_free_host;
  1031. }
  1032. mmc->ops = &sunxi_mmc_ops;
  1033. mmc->max_blk_count = 8192;
  1034. mmc->max_blk_size = 4096;
  1035. mmc->max_segs = PAGE_SIZE / sizeof(struct sunxi_idma_des);
  1036. mmc->max_seg_size = (1 << host->cfg->idma_des_size_bits);
  1037. mmc->max_req_size = mmc->max_seg_size * mmc->max_segs;
  1038. /* 400kHz ~ 52MHz */
  1039. mmc->f_min = 400000;
  1040. mmc->f_max = 52000000;
  1041. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1042. MMC_CAP_ERASE | MMC_CAP_SDIO_IRQ;
  1043. if (host->cfg->clk_delays)
  1044. mmc->caps |= MMC_CAP_1_8V_DDR;
  1045. ret = mmc_of_parse(mmc);
  1046. if (ret)
  1047. goto error_free_dma;
  1048. ret = mmc_add_host(mmc);
  1049. if (ret)
  1050. goto error_free_dma;
  1051. dev_info(&pdev->dev, "base:0x%p irq:%u\n", host->reg_base, host->irq);
  1052. platform_set_drvdata(pdev, mmc);
  1053. return 0;
  1054. error_free_dma:
  1055. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1056. error_free_host:
  1057. mmc_free_host(mmc);
  1058. return ret;
  1059. }
  1060. static int sunxi_mmc_remove(struct platform_device *pdev)
  1061. {
  1062. struct mmc_host *mmc = platform_get_drvdata(pdev);
  1063. struct sunxi_mmc_host *host = mmc_priv(mmc);
  1064. mmc_remove_host(mmc);
  1065. disable_irq(host->irq);
  1066. sunxi_mmc_reset_host(host);
  1067. if (!IS_ERR(host->reset))
  1068. reset_control_assert(host->reset);
  1069. clk_disable_unprepare(host->clk_sample);
  1070. clk_disable_unprepare(host->clk_output);
  1071. clk_disable_unprepare(host->clk_mmc);
  1072. clk_disable_unprepare(host->clk_ahb);
  1073. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1074. mmc_free_host(mmc);
  1075. return 0;
  1076. }
  1077. static struct platform_driver sunxi_mmc_driver = {
  1078. .driver = {
  1079. .name = "sunxi-mmc",
  1080. .of_match_table = of_match_ptr(sunxi_mmc_of_match),
  1081. },
  1082. .probe = sunxi_mmc_probe,
  1083. .remove = sunxi_mmc_remove,
  1084. };
  1085. module_platform_driver(sunxi_mmc_driver);
  1086. MODULE_DESCRIPTION("Allwinner's SD/MMC Card Controller Driver");
  1087. MODULE_LICENSE("GPL v2");
  1088. MODULE_AUTHOR("David Lanzend�rfer <david.lanzendoerfer@o2s.ch>");
  1089. MODULE_ALIAS("platform:sunxi-mmc");