sunxi-mmc.c 42 KB

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