meson-gx-mmc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Amlogic SD/eMMC driver for the GX/S905 family SoCs
  4. *
  5. * Copyright (c) 2016 BayLibre, SAS.
  6. * Author: Kevin Hilman <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/ioport.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/mmc/mmc.h>
  20. #include <linux/mmc/sdio.h>
  21. #include <linux/mmc/slot-gpio.h>
  22. #include <linux/io.h>
  23. #include <linux/clk.h>
  24. #include <linux/clk-provider.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/reset.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/bitfield.h>
  29. #include <linux/pinctrl/consumer.h>
  30. #define DRIVER_NAME "meson-gx-mmc"
  31. #define SD_EMMC_CLOCK 0x0
  32. #define CLK_DIV_MASK GENMASK(5, 0)
  33. #define CLK_SRC_MASK GENMASK(7, 6)
  34. #define CLK_CORE_PHASE_MASK GENMASK(9, 8)
  35. #define CLK_TX_PHASE_MASK GENMASK(11, 10)
  36. #define CLK_RX_PHASE_MASK GENMASK(13, 12)
  37. #define CLK_PHASE_0 0
  38. #define CLK_PHASE_180 2
  39. #define CLK_V2_TX_DELAY_MASK GENMASK(19, 16)
  40. #define CLK_V2_RX_DELAY_MASK GENMASK(23, 20)
  41. #define CLK_V2_ALWAYS_ON BIT(24)
  42. #define CLK_V2_IRQ_SDIO_SLEEP BIT(25)
  43. #define CLK_V3_TX_DELAY_MASK GENMASK(21, 16)
  44. #define CLK_V3_RX_DELAY_MASK GENMASK(27, 22)
  45. #define CLK_V3_ALWAYS_ON BIT(28)
  46. #define CLK_V3_IRQ_SDIO_SLEEP BIT(29)
  47. #define CLK_TX_DELAY_MASK(h) (h->data->tx_delay_mask)
  48. #define CLK_RX_DELAY_MASK(h) (h->data->rx_delay_mask)
  49. #define CLK_ALWAYS_ON(h) (h->data->always_on)
  50. #define CLK_IRQ_SDIO_SLEEP(h) (h->data->irq_sdio_sleep)
  51. #define SD_EMMC_DELAY 0x4
  52. #define SD_EMMC_ADJUST 0x8
  53. #define ADJUST_ADJ_DELAY_MASK GENMASK(21, 16)
  54. #define ADJUST_DS_EN BIT(15)
  55. #define ADJUST_ADJ_EN BIT(13)
  56. #define SD_EMMC_DELAY1 0x4
  57. #define SD_EMMC_DELAY2 0x8
  58. #define SD_EMMC_V3_ADJUST 0xc
  59. #define SD_EMMC_CALOUT 0x10
  60. #define SD_EMMC_START 0x40
  61. #define START_DESC_INIT BIT(0)
  62. #define START_DESC_BUSY BIT(1)
  63. #define START_DESC_ADDR_MASK GENMASK(31, 2)
  64. #define SD_EMMC_CFG 0x44
  65. #define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
  66. #define CFG_BUS_WIDTH_1 0x0
  67. #define CFG_BUS_WIDTH_4 0x1
  68. #define CFG_BUS_WIDTH_8 0x2
  69. #define CFG_DDR BIT(2)
  70. #define CFG_BLK_LEN_MASK GENMASK(7, 4)
  71. #define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
  72. #define CFG_RC_CC_MASK GENMASK(15, 12)
  73. #define CFG_STOP_CLOCK BIT(22)
  74. #define CFG_CLK_ALWAYS_ON BIT(18)
  75. #define CFG_CHK_DS BIT(20)
  76. #define CFG_AUTO_CLK BIT(23)
  77. #define CFG_ERR_ABORT BIT(27)
  78. #define SD_EMMC_STATUS 0x48
  79. #define STATUS_BUSY BIT(31)
  80. #define STATUS_DESC_BUSY BIT(30)
  81. #define STATUS_DATI GENMASK(23, 16)
  82. #define SD_EMMC_IRQ_EN 0x4c
  83. #define IRQ_RXD_ERR_MASK GENMASK(7, 0)
  84. #define IRQ_TXD_ERR BIT(8)
  85. #define IRQ_DESC_ERR BIT(9)
  86. #define IRQ_RESP_ERR BIT(10)
  87. #define IRQ_CRC_ERR \
  88. (IRQ_RXD_ERR_MASK | IRQ_TXD_ERR | IRQ_DESC_ERR | IRQ_RESP_ERR)
  89. #define IRQ_RESP_TIMEOUT BIT(11)
  90. #define IRQ_DESC_TIMEOUT BIT(12)
  91. #define IRQ_TIMEOUTS \
  92. (IRQ_RESP_TIMEOUT | IRQ_DESC_TIMEOUT)
  93. #define IRQ_END_OF_CHAIN BIT(13)
  94. #define IRQ_RESP_STATUS BIT(14)
  95. #define IRQ_SDIO BIT(15)
  96. #define IRQ_EN_MASK \
  97. (IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN)
  98. #define SD_EMMC_CMD_CFG 0x50
  99. #define SD_EMMC_CMD_ARG 0x54
  100. #define SD_EMMC_CMD_DAT 0x58
  101. #define SD_EMMC_CMD_RSP 0x5c
  102. #define SD_EMMC_CMD_RSP1 0x60
  103. #define SD_EMMC_CMD_RSP2 0x64
  104. #define SD_EMMC_CMD_RSP3 0x68
  105. #define SD_EMMC_RXD 0x94
  106. #define SD_EMMC_TXD 0x94
  107. #define SD_EMMC_LAST_REG SD_EMMC_TXD
  108. #define SD_EMMC_SRAM_DATA_BUF_LEN 1536
  109. #define SD_EMMC_SRAM_DATA_BUF_OFF 0x200
  110. #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
  111. #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
  112. #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
  113. #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
  114. #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
  115. #define SD_EMMC_DESC_BUF_LEN PAGE_SIZE
  116. #define SD_EMMC_PRE_REQ_DONE BIT(0)
  117. #define SD_EMMC_DESC_CHAIN_MODE BIT(1)
  118. #define MUX_CLK_NUM_PARENTS 2
  119. struct meson_mmc_data {
  120. unsigned int tx_delay_mask;
  121. unsigned int rx_delay_mask;
  122. unsigned int always_on;
  123. unsigned int adjust;
  124. unsigned int irq_sdio_sleep;
  125. };
  126. struct sd_emmc_desc {
  127. u32 cmd_cfg;
  128. u32 cmd_arg;
  129. u32 cmd_data;
  130. u32 cmd_resp;
  131. };
  132. struct meson_host {
  133. struct device *dev;
  134. struct meson_mmc_data *data;
  135. struct mmc_host *mmc;
  136. struct mmc_command *cmd;
  137. void __iomem *regs;
  138. struct clk *core_clk;
  139. struct clk *mux_clk;
  140. struct clk *mmc_clk;
  141. unsigned long req_rate;
  142. bool ddr;
  143. bool dram_access_quirk;
  144. struct pinctrl *pinctrl;
  145. struct pinctrl_state *pins_clk_gate;
  146. unsigned int bounce_buf_size;
  147. void *bounce_buf;
  148. void __iomem *bounce_iomem_buf;
  149. dma_addr_t bounce_dma_addr;
  150. struct sd_emmc_desc *descs;
  151. dma_addr_t descs_dma_addr;
  152. int irq;
  153. bool vqmmc_enabled;
  154. bool needs_pre_post_req;
  155. spinlock_t lock;
  156. };
  157. #define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
  158. #define CMD_CFG_BLOCK_MODE BIT(9)
  159. #define CMD_CFG_R1B BIT(10)
  160. #define CMD_CFG_END_OF_CHAIN BIT(11)
  161. #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
  162. #define CMD_CFG_NO_RESP BIT(16)
  163. #define CMD_CFG_NO_CMD BIT(17)
  164. #define CMD_CFG_DATA_IO BIT(18)
  165. #define CMD_CFG_DATA_WR BIT(19)
  166. #define CMD_CFG_RESP_NOCRC BIT(20)
  167. #define CMD_CFG_RESP_128 BIT(21)
  168. #define CMD_CFG_RESP_NUM BIT(22)
  169. #define CMD_CFG_DATA_NUM BIT(23)
  170. #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
  171. #define CMD_CFG_ERROR BIT(30)
  172. #define CMD_CFG_OWNER BIT(31)
  173. #define CMD_DATA_MASK GENMASK(31, 2)
  174. #define CMD_DATA_BIG_ENDIAN BIT(1)
  175. #define CMD_DATA_SRAM BIT(0)
  176. #define CMD_RESP_MASK GENMASK(31, 1)
  177. #define CMD_RESP_SRAM BIT(0)
  178. static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data)
  179. {
  180. unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC;
  181. if (!timeout)
  182. return SD_EMMC_CMD_TIMEOUT_DATA;
  183. timeout = roundup_pow_of_two(timeout);
  184. return min(timeout, 32768U); /* max. 2^15 ms */
  185. }
  186. static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd)
  187. {
  188. if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error)
  189. return cmd->mrq->cmd;
  190. else if (mmc_op_multi(cmd->opcode) &&
  191. (!cmd->mrq->sbc || cmd->error || cmd->data->error))
  192. return cmd->mrq->stop;
  193. else
  194. return NULL;
  195. }
  196. static void meson_mmc_get_transfer_mode(struct mmc_host *mmc,
  197. struct mmc_request *mrq)
  198. {
  199. struct meson_host *host = mmc_priv(mmc);
  200. struct mmc_data *data = mrq->data;
  201. struct scatterlist *sg;
  202. int i;
  203. /*
  204. * When Controller DMA cannot directly access DDR memory, disable
  205. * support for Chain Mode to directly use the internal SRAM using
  206. * the bounce buffer mode.
  207. */
  208. if (host->dram_access_quirk)
  209. return;
  210. /* SD_IO_RW_EXTENDED (CMD53) can also use block mode under the hood */
  211. if (data->blocks > 1 || mrq->cmd->opcode == SD_IO_RW_EXTENDED) {
  212. /*
  213. * In block mode DMA descriptor format, "length" field indicates
  214. * number of blocks and there is no way to pass DMA size that
  215. * is not multiple of SDIO block size, making it impossible to
  216. * tie more than one memory buffer with single SDIO block.
  217. * Block mode sg buffer size should be aligned with SDIO block
  218. * size, otherwise chain mode could not be used.
  219. */
  220. for_each_sg(data->sg, sg, data->sg_len, i) {
  221. if (sg->length % data->blksz) {
  222. dev_warn_once(mmc_dev(mmc),
  223. "unaligned sg len %u blksize %u, disabling descriptor DMA for transfer\n",
  224. sg->length, data->blksz);
  225. return;
  226. }
  227. }
  228. }
  229. for_each_sg(data->sg, sg, data->sg_len, i) {
  230. /* check for 8 byte alignment */
  231. if (sg->offset % 8) {
  232. dev_warn_once(mmc_dev(mmc),
  233. "unaligned sg offset %u, disabling descriptor DMA for transfer\n",
  234. sg->offset);
  235. return;
  236. }
  237. }
  238. data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE;
  239. }
  240. static inline bool meson_mmc_desc_chain_mode(const struct mmc_data *data)
  241. {
  242. return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE;
  243. }
  244. static inline bool meson_mmc_bounce_buf_read(const struct mmc_data *data)
  245. {
  246. return data && data->flags & MMC_DATA_READ &&
  247. !meson_mmc_desc_chain_mode(data);
  248. }
  249. static void meson_mmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  250. {
  251. struct mmc_data *data = mrq->data;
  252. if (!data)
  253. return;
  254. meson_mmc_get_transfer_mode(mmc, mrq);
  255. data->host_cookie |= SD_EMMC_PRE_REQ_DONE;
  256. if (!meson_mmc_desc_chain_mode(data))
  257. return;
  258. data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
  259. mmc_get_dma_dir(data));
  260. if (!data->sg_count)
  261. dev_err(mmc_dev(mmc), "dma_map_sg failed");
  262. }
  263. static void meson_mmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  264. int err)
  265. {
  266. struct mmc_data *data = mrq->data;
  267. if (data && meson_mmc_desc_chain_mode(data) && data->sg_count)
  268. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
  269. mmc_get_dma_dir(data));
  270. }
  271. /*
  272. * Gating the clock on this controller is tricky. It seems the mmc clock
  273. * is also used by the controller. It may crash during some operation if the
  274. * clock is stopped. The safest thing to do, whenever possible, is to keep
  275. * clock running at stop it at the pad using the pinmux.
  276. */
  277. static void meson_mmc_clk_gate(struct meson_host *host)
  278. {
  279. u32 cfg;
  280. if (host->pins_clk_gate) {
  281. pinctrl_select_state(host->pinctrl, host->pins_clk_gate);
  282. } else {
  283. /*
  284. * If the pinmux is not provided - default to the classic and
  285. * unsafe method
  286. */
  287. cfg = readl(host->regs + SD_EMMC_CFG);
  288. cfg |= CFG_STOP_CLOCK;
  289. writel(cfg, host->regs + SD_EMMC_CFG);
  290. }
  291. }
  292. static void meson_mmc_clk_ungate(struct meson_host *host)
  293. {
  294. u32 cfg;
  295. if (host->pins_clk_gate)
  296. pinctrl_select_default_state(host->dev);
  297. /* Make sure the clock is not stopped in the controller */
  298. cfg = readl(host->regs + SD_EMMC_CFG);
  299. cfg &= ~CFG_STOP_CLOCK;
  300. writel(cfg, host->regs + SD_EMMC_CFG);
  301. }
  302. static int meson_mmc_clk_set(struct meson_host *host, unsigned long rate,
  303. bool ddr)
  304. {
  305. struct mmc_host *mmc = host->mmc;
  306. int ret;
  307. u32 cfg;
  308. /* Same request - bail-out */
  309. if (host->ddr == ddr && host->req_rate == rate)
  310. return 0;
  311. /* stop clock */
  312. meson_mmc_clk_gate(host);
  313. host->req_rate = 0;
  314. mmc->actual_clock = 0;
  315. /* return with clock being stopped */
  316. if (!rate)
  317. return 0;
  318. /* Stop the clock during rate change to avoid glitches */
  319. cfg = readl(host->regs + SD_EMMC_CFG);
  320. cfg |= CFG_STOP_CLOCK;
  321. writel(cfg, host->regs + SD_EMMC_CFG);
  322. if (ddr) {
  323. /* DDR modes require higher module clock */
  324. rate <<= 1;
  325. cfg |= CFG_DDR;
  326. } else {
  327. cfg &= ~CFG_DDR;
  328. }
  329. writel(cfg, host->regs + SD_EMMC_CFG);
  330. host->ddr = ddr;
  331. ret = clk_set_rate(host->mmc_clk, rate);
  332. if (ret) {
  333. dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
  334. rate, ret);
  335. return ret;
  336. }
  337. host->req_rate = rate;
  338. mmc->actual_clock = clk_get_rate(host->mmc_clk);
  339. /* We should report the real output frequency of the controller */
  340. if (ddr) {
  341. host->req_rate >>= 1;
  342. mmc->actual_clock >>= 1;
  343. }
  344. dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock);
  345. if (rate != mmc->actual_clock)
  346. dev_dbg(host->dev, "requested rate was %lu\n", rate);
  347. /* (re)start clock */
  348. meson_mmc_clk_ungate(host);
  349. return 0;
  350. }
  351. /*
  352. * The SD/eMMC IP block has an internal mux and divider used for
  353. * generating the MMC clock. Use the clock framework to create and
  354. * manage these clocks.
  355. */
  356. static int meson_mmc_clk_init(struct meson_host *host)
  357. {
  358. struct clk_init_data init;
  359. struct clk_mux *mux;
  360. struct clk_divider *div;
  361. char clk_name[32];
  362. int i, ret = 0;
  363. const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
  364. const char *clk_parent[1];
  365. u32 clk_reg;
  366. /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
  367. clk_reg = CLK_ALWAYS_ON(host);
  368. clk_reg |= CLK_DIV_MASK;
  369. clk_reg |= FIELD_PREP(CLK_CORE_PHASE_MASK, CLK_PHASE_180);
  370. clk_reg |= FIELD_PREP(CLK_TX_PHASE_MASK, CLK_PHASE_0);
  371. clk_reg |= FIELD_PREP(CLK_RX_PHASE_MASK, CLK_PHASE_0);
  372. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  373. clk_reg |= CLK_IRQ_SDIO_SLEEP(host);
  374. writel(clk_reg, host->regs + SD_EMMC_CLOCK);
  375. /* get the mux parents */
  376. for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
  377. struct clk *clk;
  378. char name[16];
  379. snprintf(name, sizeof(name), "clkin%d", i);
  380. clk = devm_clk_get(host->dev, name);
  381. if (IS_ERR(clk))
  382. return dev_err_probe(host->dev, PTR_ERR(clk),
  383. "Missing clock %s\n", name);
  384. mux_parent_names[i] = __clk_get_name(clk);
  385. }
  386. /* create the mux */
  387. mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL);
  388. if (!mux)
  389. return -ENOMEM;
  390. snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
  391. init.name = clk_name;
  392. init.ops = &clk_mux_ops;
  393. init.flags = 0;
  394. init.parent_names = mux_parent_names;
  395. init.num_parents = MUX_CLK_NUM_PARENTS;
  396. mux->reg = host->regs + SD_EMMC_CLOCK;
  397. mux->shift = __ffs(CLK_SRC_MASK);
  398. mux->mask = CLK_SRC_MASK >> mux->shift;
  399. mux->hw.init = &init;
  400. host->mux_clk = devm_clk_register(host->dev, &mux->hw);
  401. if (WARN_ON(IS_ERR(host->mux_clk)))
  402. return PTR_ERR(host->mux_clk);
  403. /* create the divider */
  404. div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL);
  405. if (!div)
  406. return -ENOMEM;
  407. snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
  408. init.name = clk_name;
  409. init.ops = &clk_divider_ops;
  410. init.flags = CLK_SET_RATE_PARENT;
  411. clk_parent[0] = __clk_get_name(host->mux_clk);
  412. init.parent_names = clk_parent;
  413. init.num_parents = 1;
  414. div->reg = host->regs + SD_EMMC_CLOCK;
  415. div->shift = __ffs(CLK_DIV_MASK);
  416. div->width = __builtin_popcountl(CLK_DIV_MASK);
  417. div->hw.init = &init;
  418. div->flags = CLK_DIVIDER_ONE_BASED;
  419. host->mmc_clk = devm_clk_register(host->dev, &div->hw);
  420. if (WARN_ON(IS_ERR(host->mmc_clk)))
  421. return PTR_ERR(host->mmc_clk);
  422. /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
  423. host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000);
  424. ret = clk_set_rate(host->mmc_clk, host->mmc->f_min);
  425. if (ret)
  426. return ret;
  427. return clk_prepare_enable(host->mmc_clk);
  428. }
  429. static void meson_mmc_disable_resampling(struct meson_host *host)
  430. {
  431. unsigned int val = readl(host->regs + host->data->adjust);
  432. val &= ~ADJUST_ADJ_EN;
  433. writel(val, host->regs + host->data->adjust);
  434. }
  435. static void meson_mmc_reset_resampling(struct meson_host *host)
  436. {
  437. unsigned int val;
  438. meson_mmc_disable_resampling(host);
  439. val = readl(host->regs + host->data->adjust);
  440. val &= ~ADJUST_ADJ_DELAY_MASK;
  441. writel(val, host->regs + host->data->adjust);
  442. }
  443. static int meson_mmc_resampling_tuning(struct mmc_host *mmc, u32 opcode)
  444. {
  445. struct meson_host *host = mmc_priv(mmc);
  446. unsigned int val, dly, max_dly, i;
  447. int ret;
  448. /* Resampling is done using the source clock */
  449. max_dly = DIV_ROUND_UP(clk_get_rate(host->mux_clk),
  450. clk_get_rate(host->mmc_clk));
  451. val = readl(host->regs + host->data->adjust);
  452. val |= ADJUST_ADJ_EN;
  453. writel(val, host->regs + host->data->adjust);
  454. if (mmc_doing_retune(mmc))
  455. dly = FIELD_GET(ADJUST_ADJ_DELAY_MASK, val) + 1;
  456. else
  457. dly = 0;
  458. for (i = 0; i < max_dly; i++) {
  459. val &= ~ADJUST_ADJ_DELAY_MASK;
  460. val |= FIELD_PREP(ADJUST_ADJ_DELAY_MASK, (dly + i) % max_dly);
  461. writel(val, host->regs + host->data->adjust);
  462. ret = mmc_send_tuning(mmc, opcode, NULL);
  463. if (!ret) {
  464. dev_dbg(mmc_dev(mmc), "resampling delay: %u\n",
  465. (dly + i) % max_dly);
  466. return 0;
  467. }
  468. }
  469. meson_mmc_reset_resampling(host);
  470. return -EIO;
  471. }
  472. static int meson_mmc_prepare_ios_clock(struct meson_host *host,
  473. struct mmc_ios *ios)
  474. {
  475. bool ddr;
  476. switch (ios->timing) {
  477. case MMC_TIMING_MMC_DDR52:
  478. case MMC_TIMING_UHS_DDR50:
  479. ddr = true;
  480. break;
  481. default:
  482. ddr = false;
  483. break;
  484. }
  485. return meson_mmc_clk_set(host, ios->clock, ddr);
  486. }
  487. static void meson_mmc_check_resampling(struct meson_host *host,
  488. struct mmc_ios *ios)
  489. {
  490. switch (ios->timing) {
  491. case MMC_TIMING_LEGACY:
  492. case MMC_TIMING_MMC_HS:
  493. case MMC_TIMING_SD_HS:
  494. case MMC_TIMING_MMC_DDR52:
  495. meson_mmc_disable_resampling(host);
  496. break;
  497. }
  498. }
  499. static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  500. {
  501. struct meson_host *host = mmc_priv(mmc);
  502. u32 bus_width, val;
  503. int err;
  504. /*
  505. * GPIO regulator, only controls switching between 1v8 and
  506. * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
  507. */
  508. switch (ios->power_mode) {
  509. case MMC_POWER_OFF:
  510. if (!IS_ERR(mmc->supply.vmmc))
  511. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  512. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  513. regulator_disable(mmc->supply.vqmmc);
  514. host->vqmmc_enabled = false;
  515. }
  516. break;
  517. case MMC_POWER_UP:
  518. if (!IS_ERR(mmc->supply.vmmc))
  519. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  520. break;
  521. case MMC_POWER_ON:
  522. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  523. int ret = regulator_enable(mmc->supply.vqmmc);
  524. if (ret < 0)
  525. dev_err(host->dev,
  526. "failed to enable vqmmc regulator\n");
  527. else
  528. host->vqmmc_enabled = true;
  529. }
  530. break;
  531. }
  532. /* Bus width */
  533. switch (ios->bus_width) {
  534. case MMC_BUS_WIDTH_1:
  535. bus_width = CFG_BUS_WIDTH_1;
  536. break;
  537. case MMC_BUS_WIDTH_4:
  538. bus_width = CFG_BUS_WIDTH_4;
  539. break;
  540. case MMC_BUS_WIDTH_8:
  541. bus_width = CFG_BUS_WIDTH_8;
  542. break;
  543. default:
  544. dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
  545. ios->bus_width);
  546. bus_width = CFG_BUS_WIDTH_4;
  547. }
  548. val = readl(host->regs + SD_EMMC_CFG);
  549. val &= ~CFG_BUS_WIDTH_MASK;
  550. val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
  551. writel(val, host->regs + SD_EMMC_CFG);
  552. meson_mmc_check_resampling(host, ios);
  553. err = meson_mmc_prepare_ios_clock(host, ios);
  554. if (err)
  555. dev_err(host->dev, "Failed to set clock: %d\n,", err);
  556. dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val);
  557. }
  558. static void meson_mmc_request_done(struct mmc_host *mmc,
  559. struct mmc_request *mrq)
  560. {
  561. struct meson_host *host = mmc_priv(mmc);
  562. host->cmd = NULL;
  563. if (host->needs_pre_post_req)
  564. meson_mmc_post_req(mmc, mrq, 0);
  565. mmc_request_done(host->mmc, mrq);
  566. }
  567. static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz)
  568. {
  569. struct meson_host *host = mmc_priv(mmc);
  570. u32 cfg, blksz_old;
  571. cfg = readl(host->regs + SD_EMMC_CFG);
  572. blksz_old = FIELD_GET(CFG_BLK_LEN_MASK, cfg);
  573. if (!is_power_of_2(blksz))
  574. dev_err(host->dev, "blksz %u is not a power of 2\n", blksz);
  575. blksz = ilog2(blksz);
  576. /* check if block-size matches, if not update */
  577. if (blksz == blksz_old)
  578. return;
  579. dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__,
  580. blksz_old, blksz);
  581. cfg &= ~CFG_BLK_LEN_MASK;
  582. cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blksz);
  583. writel(cfg, host->regs + SD_EMMC_CFG);
  584. }
  585. static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg)
  586. {
  587. if (cmd->flags & MMC_RSP_PRESENT) {
  588. if (cmd->flags & MMC_RSP_136)
  589. *cmd_cfg |= CMD_CFG_RESP_128;
  590. *cmd_cfg |= CMD_CFG_RESP_NUM;
  591. if (!(cmd->flags & MMC_RSP_CRC))
  592. *cmd_cfg |= CMD_CFG_RESP_NOCRC;
  593. if (cmd->flags & MMC_RSP_BUSY)
  594. *cmd_cfg |= CMD_CFG_R1B;
  595. } else {
  596. *cmd_cfg |= CMD_CFG_NO_RESP;
  597. }
  598. }
  599. static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg)
  600. {
  601. struct meson_host *host = mmc_priv(mmc);
  602. struct sd_emmc_desc *desc = host->descs;
  603. struct mmc_data *data = host->cmd->data;
  604. struct scatterlist *sg;
  605. u32 start;
  606. int i;
  607. if (data->flags & MMC_DATA_WRITE)
  608. cmd_cfg |= CMD_CFG_DATA_WR;
  609. if (data->blocks > 1) {
  610. cmd_cfg |= CMD_CFG_BLOCK_MODE;
  611. meson_mmc_set_blksz(mmc, data->blksz);
  612. }
  613. for_each_sg(data->sg, sg, data->sg_count, i) {
  614. unsigned int len = sg_dma_len(sg);
  615. if (data->blocks > 1)
  616. len /= data->blksz;
  617. desc[i].cmd_cfg = cmd_cfg;
  618. desc[i].cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, len);
  619. if (i > 0)
  620. desc[i].cmd_cfg |= CMD_CFG_NO_CMD;
  621. desc[i].cmd_arg = host->cmd->arg;
  622. desc[i].cmd_resp = 0;
  623. desc[i].cmd_data = sg_dma_address(sg);
  624. }
  625. desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN;
  626. dma_wmb(); /* ensure descriptor is written before kicked */
  627. start = host->descs_dma_addr | START_DESC_BUSY;
  628. writel(start, host->regs + SD_EMMC_START);
  629. }
  630. /* local sg copy for dram_access_quirk */
  631. static void meson_mmc_copy_buffer(struct meson_host *host, struct mmc_data *data,
  632. size_t buflen, bool to_buffer)
  633. {
  634. unsigned int sg_flags = SG_MITER_ATOMIC;
  635. struct scatterlist *sgl = data->sg;
  636. unsigned int nents = data->sg_len;
  637. struct sg_mapping_iter miter;
  638. unsigned int offset = 0;
  639. if (to_buffer)
  640. sg_flags |= SG_MITER_FROM_SG;
  641. else
  642. sg_flags |= SG_MITER_TO_SG;
  643. sg_miter_start(&miter, sgl, nents, sg_flags);
  644. while ((offset < buflen) && sg_miter_next(&miter)) {
  645. unsigned int buf_offset = 0;
  646. unsigned int len, left;
  647. u32 *buf = miter.addr;
  648. len = min(miter.length, buflen - offset);
  649. left = len;
  650. if (to_buffer) {
  651. do {
  652. writel(*buf++, host->bounce_iomem_buf + offset + buf_offset);
  653. buf_offset += 4;
  654. left -= 4;
  655. } while (left);
  656. } else {
  657. do {
  658. *buf++ = readl(host->bounce_iomem_buf + offset + buf_offset);
  659. buf_offset += 4;
  660. left -= 4;
  661. } while (left);
  662. }
  663. offset += len;
  664. }
  665. sg_miter_stop(&miter);
  666. }
  667. static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
  668. {
  669. struct meson_host *host = mmc_priv(mmc);
  670. struct mmc_data *data = cmd->data;
  671. u32 cmd_cfg = 0, cmd_data = 0;
  672. unsigned int xfer_bytes = 0;
  673. /* Setup descriptors */
  674. dma_rmb();
  675. host->cmd = cmd;
  676. cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
  677. cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
  678. meson_mmc_set_response_bits(cmd, &cmd_cfg);
  679. /* data? */
  680. if (data) {
  681. data->bytes_xfered = 0;
  682. cmd_cfg |= CMD_CFG_DATA_IO;
  683. cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
  684. ilog2(meson_mmc_get_timeout_msecs(data)));
  685. if (meson_mmc_desc_chain_mode(data)) {
  686. meson_mmc_desc_chain_transfer(mmc, cmd_cfg);
  687. return;
  688. }
  689. if (data->blocks > 1) {
  690. cmd_cfg |= CMD_CFG_BLOCK_MODE;
  691. cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
  692. data->blocks);
  693. meson_mmc_set_blksz(mmc, data->blksz);
  694. } else {
  695. cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
  696. }
  697. xfer_bytes = data->blksz * data->blocks;
  698. if (data->flags & MMC_DATA_WRITE) {
  699. cmd_cfg |= CMD_CFG_DATA_WR;
  700. WARN_ON(xfer_bytes > host->bounce_buf_size);
  701. if (host->dram_access_quirk)
  702. meson_mmc_copy_buffer(host, data, xfer_bytes, true);
  703. else
  704. sg_copy_to_buffer(data->sg, data->sg_len,
  705. host->bounce_buf, xfer_bytes);
  706. dma_wmb();
  707. }
  708. cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
  709. } else {
  710. cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
  711. ilog2(SD_EMMC_CMD_TIMEOUT));
  712. }
  713. /* Last descriptor */
  714. cmd_cfg |= CMD_CFG_END_OF_CHAIN;
  715. writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
  716. writel(cmd_data, host->regs + SD_EMMC_CMD_DAT);
  717. writel(0, host->regs + SD_EMMC_CMD_RSP);
  718. wmb(); /* ensure descriptor is written before kicked */
  719. writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
  720. }
  721. static int meson_mmc_validate_dram_access(struct mmc_host *mmc, struct mmc_data *data)
  722. {
  723. struct scatterlist *sg;
  724. int i;
  725. /* Reject request if any element offset or size is not 32bit aligned */
  726. for_each_sg(data->sg, sg, data->sg_len, i) {
  727. if (!IS_ALIGNED(sg->offset, sizeof(u32)) ||
  728. !IS_ALIGNED(sg->length, sizeof(u32))) {
  729. dev_err(mmc_dev(mmc), "unaligned sg offset %u len %u\n",
  730. data->sg->offset, data->sg->length);
  731. return -EINVAL;
  732. }
  733. }
  734. return 0;
  735. }
  736. static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  737. {
  738. struct meson_host *host = mmc_priv(mmc);
  739. host->needs_pre_post_req = mrq->data &&
  740. !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE);
  741. /*
  742. * The memory at the end of the controller used as bounce buffer for
  743. * the dram_access_quirk only accepts 32bit read/write access,
  744. * check the aligment and length of the data before starting the request.
  745. */
  746. if (host->dram_access_quirk && mrq->data) {
  747. mrq->cmd->error = meson_mmc_validate_dram_access(mmc, mrq->data);
  748. if (mrq->cmd->error) {
  749. mmc_request_done(mmc, mrq);
  750. return;
  751. }
  752. }
  753. if (host->needs_pre_post_req) {
  754. meson_mmc_get_transfer_mode(mmc, mrq);
  755. if (!meson_mmc_desc_chain_mode(mrq->data))
  756. host->needs_pre_post_req = false;
  757. }
  758. if (host->needs_pre_post_req)
  759. meson_mmc_pre_req(mmc, mrq);
  760. /* Stop execution */
  761. writel(0, host->regs + SD_EMMC_START);
  762. meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd);
  763. }
  764. static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
  765. {
  766. struct meson_host *host = mmc_priv(mmc);
  767. if (cmd->flags & MMC_RSP_136) {
  768. cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
  769. cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
  770. cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
  771. cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
  772. } else if (cmd->flags & MMC_RSP_PRESENT) {
  773. cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
  774. }
  775. }
  776. static void __meson_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  777. {
  778. struct meson_host *host = mmc_priv(mmc);
  779. u32 reg_irqen = IRQ_EN_MASK;
  780. if (enable)
  781. reg_irqen |= IRQ_SDIO;
  782. writel(reg_irqen, host->regs + SD_EMMC_IRQ_EN);
  783. }
  784. static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
  785. {
  786. struct meson_host *host = dev_id;
  787. struct mmc_command *cmd;
  788. u32 status, raw_status, irq_mask = IRQ_EN_MASK;
  789. irqreturn_t ret = IRQ_NONE;
  790. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  791. irq_mask |= IRQ_SDIO;
  792. raw_status = readl(host->regs + SD_EMMC_STATUS);
  793. status = raw_status & irq_mask;
  794. if (!status) {
  795. dev_dbg(host->dev,
  796. "Unexpected IRQ! irq_en 0x%08x - status 0x%08x\n",
  797. irq_mask, raw_status);
  798. return IRQ_NONE;
  799. }
  800. if (WARN_ON(!host))
  801. return IRQ_NONE;
  802. /* ack all raised interrupts */
  803. writel(status, host->regs + SD_EMMC_STATUS);
  804. cmd = host->cmd;
  805. if (status & IRQ_SDIO) {
  806. spin_lock(&host->lock);
  807. __meson_mmc_enable_sdio_irq(host->mmc, 0);
  808. sdio_signal_irq(host->mmc);
  809. spin_unlock(&host->lock);
  810. status &= ~IRQ_SDIO;
  811. if (!status)
  812. return IRQ_HANDLED;
  813. }
  814. if (WARN_ON(!cmd))
  815. return IRQ_NONE;
  816. cmd->error = 0;
  817. if (status & IRQ_CRC_ERR) {
  818. dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status);
  819. cmd->error = -EILSEQ;
  820. ret = IRQ_WAKE_THREAD;
  821. goto out;
  822. }
  823. if (status & IRQ_TIMEOUTS) {
  824. dev_dbg(host->dev, "Timeout - status 0x%08x\n", status);
  825. cmd->error = -ETIMEDOUT;
  826. ret = IRQ_WAKE_THREAD;
  827. goto out;
  828. }
  829. meson_mmc_read_resp(host->mmc, cmd);
  830. if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
  831. struct mmc_data *data = cmd->data;
  832. if (data && !cmd->error)
  833. data->bytes_xfered = data->blksz * data->blocks;
  834. return IRQ_WAKE_THREAD;
  835. }
  836. out:
  837. if (cmd->error) {
  838. /* Stop desc in case of errors */
  839. u32 start = readl(host->regs + SD_EMMC_START);
  840. start &= ~START_DESC_BUSY;
  841. writel(start, host->regs + SD_EMMC_START);
  842. }
  843. return ret;
  844. }
  845. static int meson_mmc_wait_desc_stop(struct meson_host *host)
  846. {
  847. u32 status;
  848. /*
  849. * It may sometimes take a while for it to actually halt. Here, we
  850. * are giving it 5ms to comply
  851. *
  852. * If we don't confirm the descriptor is stopped, it might raise new
  853. * IRQs after we have called mmc_request_done() which is bad.
  854. */
  855. return readl_poll_timeout(host->regs + SD_EMMC_STATUS, status,
  856. !(status & (STATUS_BUSY | STATUS_DESC_BUSY)),
  857. 100, 5000);
  858. }
  859. static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
  860. {
  861. struct meson_host *host = dev_id;
  862. struct mmc_command *next_cmd, *cmd = host->cmd;
  863. struct mmc_data *data;
  864. unsigned int xfer_bytes;
  865. if (WARN_ON(!cmd))
  866. return IRQ_NONE;
  867. if (cmd->error) {
  868. meson_mmc_wait_desc_stop(host);
  869. meson_mmc_request_done(host->mmc, cmd->mrq);
  870. return IRQ_HANDLED;
  871. }
  872. data = cmd->data;
  873. if (meson_mmc_bounce_buf_read(data)) {
  874. xfer_bytes = data->blksz * data->blocks;
  875. WARN_ON(xfer_bytes > host->bounce_buf_size);
  876. if (host->dram_access_quirk)
  877. meson_mmc_copy_buffer(host, data, xfer_bytes, false);
  878. else
  879. sg_copy_from_buffer(data->sg, data->sg_len,
  880. host->bounce_buf, xfer_bytes);
  881. }
  882. next_cmd = meson_mmc_get_next_command(cmd);
  883. if (next_cmd)
  884. meson_mmc_start_cmd(host->mmc, next_cmd);
  885. else
  886. meson_mmc_request_done(host->mmc, cmd->mrq);
  887. return IRQ_HANDLED;
  888. }
  889. /*
  890. * NOTE: we only need this until the GPIO/pinctrl driver can handle
  891. * interrupts. For now, the MMC core will use this for polling.
  892. */
  893. static int meson_mmc_get_cd(struct mmc_host *mmc)
  894. {
  895. int status = mmc_gpio_get_cd(mmc);
  896. if (status == -ENOSYS)
  897. return 1; /* assume present */
  898. return status;
  899. }
  900. static void meson_mmc_cfg_init(struct meson_host *host)
  901. {
  902. u32 cfg = 0;
  903. cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK,
  904. ilog2(SD_EMMC_CFG_RESP_TIMEOUT));
  905. cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP));
  906. cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE));
  907. /* abort chain on R/W errors */
  908. cfg |= CFG_ERR_ABORT;
  909. writel(cfg, host->regs + SD_EMMC_CFG);
  910. }
  911. static int meson_mmc_card_busy(struct mmc_host *mmc)
  912. {
  913. struct meson_host *host = mmc_priv(mmc);
  914. u32 regval;
  915. regval = readl(host->regs + SD_EMMC_STATUS);
  916. /* We are only interrested in lines 0 to 3, so mask the other ones */
  917. return !(FIELD_GET(STATUS_DATI, regval) & 0xf);
  918. }
  919. static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  920. {
  921. int ret;
  922. /* vqmmc regulator is available */
  923. if (!IS_ERR(mmc->supply.vqmmc)) {
  924. /*
  925. * The usual amlogic setup uses a GPIO to switch from one
  926. * regulator to the other. While the voltage ramp up is
  927. * pretty fast, care must be taken when switching from 3.3v
  928. * to 1.8v. Please make sure the regulator framework is aware
  929. * of your own regulator constraints
  930. */
  931. ret = mmc_regulator_set_vqmmc(mmc, ios);
  932. return ret < 0 ? ret : 0;
  933. }
  934. /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
  935. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  936. return 0;
  937. return -EINVAL;
  938. }
  939. static void meson_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  940. {
  941. struct meson_host *host = mmc_priv(mmc);
  942. unsigned long flags;
  943. spin_lock_irqsave(&host->lock, flags);
  944. __meson_mmc_enable_sdio_irq(mmc, enable);
  945. spin_unlock_irqrestore(&host->lock, flags);
  946. }
  947. static void meson_mmc_ack_sdio_irq(struct mmc_host *mmc)
  948. {
  949. meson_mmc_enable_sdio_irq(mmc, 1);
  950. }
  951. static const struct mmc_host_ops meson_mmc_ops = {
  952. .request = meson_mmc_request,
  953. .set_ios = meson_mmc_set_ios,
  954. .get_cd = meson_mmc_get_cd,
  955. .pre_req = meson_mmc_pre_req,
  956. .post_req = meson_mmc_post_req,
  957. .execute_tuning = meson_mmc_resampling_tuning,
  958. .card_busy = meson_mmc_card_busy,
  959. .start_signal_voltage_switch = meson_mmc_voltage_switch,
  960. .enable_sdio_irq = meson_mmc_enable_sdio_irq,
  961. .ack_sdio_irq = meson_mmc_ack_sdio_irq,
  962. };
  963. static int meson_mmc_probe(struct platform_device *pdev)
  964. {
  965. struct resource *res;
  966. struct meson_host *host;
  967. struct mmc_host *mmc;
  968. int ret;
  969. mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
  970. if (!mmc)
  971. return -ENOMEM;
  972. host = mmc_priv(mmc);
  973. host->mmc = mmc;
  974. host->dev = &pdev->dev;
  975. dev_set_drvdata(&pdev->dev, host);
  976. /* The G12A SDIO Controller needs an SRAM bounce buffer */
  977. host->dram_access_quirk = device_property_read_bool(&pdev->dev,
  978. "amlogic,dram-access-quirk");
  979. /* Get regulators and the supported OCR mask */
  980. host->vqmmc_enabled = false;
  981. ret = mmc_regulator_get_supply(mmc);
  982. if (ret)
  983. goto free_host;
  984. ret = mmc_of_parse(mmc);
  985. if (ret) {
  986. if (ret != -EPROBE_DEFER)
  987. dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
  988. goto free_host;
  989. }
  990. mmc->caps |= MMC_CAP_CMD23;
  991. if (mmc->caps & MMC_CAP_SDIO_IRQ)
  992. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  993. host->data = (struct meson_mmc_data *)
  994. of_device_get_match_data(&pdev->dev);
  995. if (!host->data) {
  996. ret = -EINVAL;
  997. goto free_host;
  998. }
  999. ret = device_reset_optional(&pdev->dev);
  1000. if (ret) {
  1001. dev_err_probe(&pdev->dev, ret, "device reset failed\n");
  1002. goto free_host;
  1003. }
  1004. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1005. host->regs = devm_ioremap_resource(&pdev->dev, res);
  1006. if (IS_ERR(host->regs)) {
  1007. ret = PTR_ERR(host->regs);
  1008. goto free_host;
  1009. }
  1010. host->irq = platform_get_irq(pdev, 0);
  1011. if (host->irq < 0) {
  1012. ret = host->irq;
  1013. goto free_host;
  1014. }
  1015. host->pinctrl = devm_pinctrl_get(&pdev->dev);
  1016. if (IS_ERR(host->pinctrl)) {
  1017. ret = PTR_ERR(host->pinctrl);
  1018. goto free_host;
  1019. }
  1020. host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl,
  1021. "clk-gate");
  1022. if (IS_ERR(host->pins_clk_gate)) {
  1023. dev_warn(&pdev->dev,
  1024. "can't get clk-gate pinctrl, using clk_stop bit\n");
  1025. host->pins_clk_gate = NULL;
  1026. }
  1027. host->core_clk = devm_clk_get(&pdev->dev, "core");
  1028. if (IS_ERR(host->core_clk)) {
  1029. ret = PTR_ERR(host->core_clk);
  1030. goto free_host;
  1031. }
  1032. ret = clk_prepare_enable(host->core_clk);
  1033. if (ret)
  1034. goto free_host;
  1035. ret = meson_mmc_clk_init(host);
  1036. if (ret)
  1037. goto err_core_clk;
  1038. /* set config to sane default */
  1039. meson_mmc_cfg_init(host);
  1040. /* Stop execution */
  1041. writel(0, host->regs + SD_EMMC_START);
  1042. /* clear, ack and enable interrupts */
  1043. writel(0, host->regs + SD_EMMC_IRQ_EN);
  1044. writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
  1045. writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
  1046. ret = request_threaded_irq(host->irq, meson_mmc_irq,
  1047. meson_mmc_irq_thread, IRQF_ONESHOT,
  1048. dev_name(&pdev->dev), host);
  1049. if (ret)
  1050. goto err_init_clk;
  1051. spin_lock_init(&host->lock);
  1052. if (host->dram_access_quirk) {
  1053. /* Limit segments to 1 due to low available sram memory */
  1054. mmc->max_segs = 1;
  1055. /* Limit to the available sram memory */
  1056. mmc->max_blk_count = SD_EMMC_SRAM_DATA_BUF_LEN /
  1057. mmc->max_blk_size;
  1058. } else {
  1059. mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
  1060. mmc->max_segs = SD_EMMC_DESC_BUF_LEN /
  1061. sizeof(struct sd_emmc_desc);
  1062. }
  1063. mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
  1064. mmc->max_seg_size = mmc->max_req_size;
  1065. /*
  1066. * At the moment, we don't know how to reliably enable HS400.
  1067. * From the different datasheets, it is not even clear if this mode
  1068. * is officially supported by any of the SoCs
  1069. */
  1070. mmc->caps2 &= ~MMC_CAP2_HS400;
  1071. if (host->dram_access_quirk) {
  1072. /*
  1073. * The MMC Controller embeds 1,5KiB of internal SRAM
  1074. * that can be used to be used as bounce buffer.
  1075. * In the case of the G12A SDIO controller, use these
  1076. * instead of the DDR memory
  1077. */
  1078. host->bounce_buf_size = SD_EMMC_SRAM_DATA_BUF_LEN;
  1079. host->bounce_iomem_buf = host->regs + SD_EMMC_SRAM_DATA_BUF_OFF;
  1080. host->bounce_dma_addr = res->start + SD_EMMC_SRAM_DATA_BUF_OFF;
  1081. } else {
  1082. /* data bounce buffer */
  1083. host->bounce_buf_size = mmc->max_req_size;
  1084. host->bounce_buf =
  1085. dmam_alloc_coherent(host->dev, host->bounce_buf_size,
  1086. &host->bounce_dma_addr, GFP_KERNEL);
  1087. if (host->bounce_buf == NULL) {
  1088. dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
  1089. ret = -ENOMEM;
  1090. goto err_free_irq;
  1091. }
  1092. }
  1093. host->descs = dmam_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
  1094. &host->descs_dma_addr, GFP_KERNEL);
  1095. if (!host->descs) {
  1096. dev_err(host->dev, "Allocating descriptor DMA buffer failed\n");
  1097. ret = -ENOMEM;
  1098. goto err_free_irq;
  1099. }
  1100. mmc->ops = &meson_mmc_ops;
  1101. ret = mmc_add_host(mmc);
  1102. if (ret)
  1103. goto err_free_irq;
  1104. return 0;
  1105. err_free_irq:
  1106. free_irq(host->irq, host);
  1107. err_init_clk:
  1108. clk_disable_unprepare(host->mmc_clk);
  1109. err_core_clk:
  1110. clk_disable_unprepare(host->core_clk);
  1111. free_host:
  1112. mmc_free_host(mmc);
  1113. return ret;
  1114. }
  1115. static int meson_mmc_remove(struct platform_device *pdev)
  1116. {
  1117. struct meson_host *host = dev_get_drvdata(&pdev->dev);
  1118. mmc_remove_host(host->mmc);
  1119. /* disable interrupts */
  1120. writel(0, host->regs + SD_EMMC_IRQ_EN);
  1121. free_irq(host->irq, host);
  1122. clk_disable_unprepare(host->mmc_clk);
  1123. clk_disable_unprepare(host->core_clk);
  1124. mmc_free_host(host->mmc);
  1125. return 0;
  1126. }
  1127. static const struct meson_mmc_data meson_gx_data = {
  1128. .tx_delay_mask = CLK_V2_TX_DELAY_MASK,
  1129. .rx_delay_mask = CLK_V2_RX_DELAY_MASK,
  1130. .always_on = CLK_V2_ALWAYS_ON,
  1131. .adjust = SD_EMMC_ADJUST,
  1132. .irq_sdio_sleep = CLK_V2_IRQ_SDIO_SLEEP,
  1133. };
  1134. static const struct meson_mmc_data meson_axg_data = {
  1135. .tx_delay_mask = CLK_V3_TX_DELAY_MASK,
  1136. .rx_delay_mask = CLK_V3_RX_DELAY_MASK,
  1137. .always_on = CLK_V3_ALWAYS_ON,
  1138. .adjust = SD_EMMC_V3_ADJUST,
  1139. .irq_sdio_sleep = CLK_V3_IRQ_SDIO_SLEEP,
  1140. };
  1141. static const struct of_device_id meson_mmc_of_match[] = {
  1142. { .compatible = "amlogic,meson-gx-mmc", .data = &meson_gx_data },
  1143. { .compatible = "amlogic,meson-gxbb-mmc", .data = &meson_gx_data },
  1144. { .compatible = "amlogic,meson-gxl-mmc", .data = &meson_gx_data },
  1145. { .compatible = "amlogic,meson-gxm-mmc", .data = &meson_gx_data },
  1146. { .compatible = "amlogic,meson-axg-mmc", .data = &meson_axg_data },
  1147. {}
  1148. };
  1149. MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
  1150. static struct platform_driver meson_mmc_driver = {
  1151. .probe = meson_mmc_probe,
  1152. .remove = meson_mmc_remove,
  1153. .driver = {
  1154. .name = DRIVER_NAME,
  1155. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1156. .of_match_table = meson_mmc_of_match,
  1157. },
  1158. };
  1159. module_platform_driver(meson_mmc_driver);
  1160. MODULE_DESCRIPTION("Amlogic S905*/GX*/AXG SD/eMMC driver");
  1161. MODULE_AUTHOR("Kevin Hilman <[email protected]>");
  1162. MODULE_LICENSE("GPL v2");