uniphier-sd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2017-2018 Socionext Inc.
  4. // Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5. #include <linux/bitfield.h>
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/mmc/host.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/pinctrl/consumer.h>
  15. #include <linux/platform_data/tmio.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/reset.h>
  19. #include "tmio_mmc.h"
  20. #define UNIPHIER_SD_CLK_CTL_DIV1024 BIT(16)
  21. #define UNIPHIER_SD_CLK_CTL_DIV1 BIT(10)
  22. #define UNIPHIER_SD_CLKCTL_OFFEN BIT(9) // auto SDCLK stop
  23. #define UNIPHIER_SD_CC_EXT_MODE 0x1b0
  24. #define UNIPHIER_SD_CC_EXT_MODE_DMA BIT(1)
  25. #define UNIPHIER_SD_HOST_MODE 0x1c8
  26. #define UNIPHIER_SD_VOLT 0x1e4
  27. #define UNIPHIER_SD_VOLT_MASK GENMASK(1, 0)
  28. #define UNIPHIER_SD_VOLT_OFF 0
  29. #define UNIPHIER_SD_VOLT_330 1 // 3.3V signal
  30. #define UNIPHIER_SD_VOLT_180 2 // 1.8V signal
  31. #define UNIPHIER_SD_DMA_MODE 0x410
  32. #define UNIPHIER_SD_DMA_MODE_DIR_MASK GENMASK(17, 16)
  33. #define UNIPHIER_SD_DMA_MODE_DIR_TO_DEV 0
  34. #define UNIPHIER_SD_DMA_MODE_DIR_FROM_DEV 1
  35. #define UNIPHIER_SD_DMA_MODE_WIDTH_MASK GENMASK(5, 4)
  36. #define UNIPHIER_SD_DMA_MODE_WIDTH_8 0
  37. #define UNIPHIER_SD_DMA_MODE_WIDTH_16 1
  38. #define UNIPHIER_SD_DMA_MODE_WIDTH_32 2
  39. #define UNIPHIER_SD_DMA_MODE_WIDTH_64 3
  40. #define UNIPHIER_SD_DMA_MODE_ADDR_INC BIT(0) // 1: inc, 0: fixed
  41. #define UNIPHIER_SD_DMA_CTL 0x414
  42. #define UNIPHIER_SD_DMA_CTL_START BIT(0) // start DMA (auto cleared)
  43. #define UNIPHIER_SD_DMA_RST 0x418
  44. #define UNIPHIER_SD_DMA_RST_CH1 BIT(9)
  45. #define UNIPHIER_SD_DMA_RST_CH0 BIT(8)
  46. #define UNIPHIER_SD_DMA_ADDR_L 0x440
  47. #define UNIPHIER_SD_DMA_ADDR_H 0x444
  48. /* SD control */
  49. #define UNIPHIER_SDCTRL_CHOFFSET 0x200
  50. #define UNIPHIER_SDCTRL_MODE 0x30
  51. #define UNIPHIER_SDCTRL_MODE_UHS1MOD BIT(15)
  52. #define UNIPHIER_SDCTRL_MODE_SDRSEL BIT(14)
  53. /*
  54. * IP is extended to support various features: built-in DMA engine,
  55. * 1/1024 divisor, etc.
  56. */
  57. #define UNIPHIER_SD_CAP_EXTENDED_IP BIT(0)
  58. /* RX channel of the built-in DMA controller is broken (Pro5) */
  59. #define UNIPHIER_SD_CAP_BROKEN_DMA_RX BIT(1)
  60. struct uniphier_sd_priv {
  61. struct tmio_mmc_data tmio_data;
  62. struct pinctrl *pinctrl;
  63. struct pinctrl_state *pinstate_uhs;
  64. struct clk *clk;
  65. struct reset_control *rst;
  66. struct reset_control *rst_br;
  67. struct reset_control *rst_hw;
  68. struct dma_chan *chan;
  69. enum dma_data_direction dma_dir;
  70. struct regmap *sdctrl_regmap;
  71. u32 sdctrl_ch;
  72. unsigned long clk_rate;
  73. unsigned long caps;
  74. };
  75. static void *uniphier_sd_priv(struct tmio_mmc_host *host)
  76. {
  77. return container_of(host->pdata, struct uniphier_sd_priv, tmio_data);
  78. }
  79. static void uniphier_sd_dma_endisable(struct tmio_mmc_host *host, int enable)
  80. {
  81. sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? DMA_ENABLE_DMASDRW : 0);
  82. }
  83. /* external DMA engine */
  84. static void uniphier_sd_external_dma_issue(struct work_struct *t)
  85. {
  86. struct tmio_mmc_host *host = from_work(host, t, dma_issue);
  87. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  88. uniphier_sd_dma_endisable(host, 1);
  89. dma_async_issue_pending(priv->chan);
  90. }
  91. static void uniphier_sd_external_dma_callback(void *param,
  92. const struct dmaengine_result *result)
  93. {
  94. struct tmio_mmc_host *host = param;
  95. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  96. unsigned long flags;
  97. dma_unmap_sg(mmc_dev(host->mmc), host->sg_ptr, host->sg_len,
  98. priv->dma_dir);
  99. spin_lock_irqsave(&host->lock, flags);
  100. if (result->result == DMA_TRANS_NOERROR) {
  101. /*
  102. * When the external DMA engine is enabled, strangely enough,
  103. * the DATAEND flag can be asserted even if the DMA engine has
  104. * not been kicked yet. Enable the TMIO_STAT_DATAEND irq only
  105. * after we make sure the DMA engine finishes the transfer,
  106. * hence, in this callback.
  107. */
  108. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  109. } else {
  110. host->data->error = -ETIMEDOUT;
  111. tmio_mmc_do_data_irq(host);
  112. }
  113. spin_unlock_irqrestore(&host->lock, flags);
  114. }
  115. static void uniphier_sd_external_dma_start(struct tmio_mmc_host *host,
  116. struct mmc_data *data)
  117. {
  118. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  119. enum dma_transfer_direction dma_tx_dir;
  120. struct dma_async_tx_descriptor *desc;
  121. dma_cookie_t cookie;
  122. int sg_len;
  123. if (!priv->chan)
  124. goto force_pio;
  125. if (data->flags & MMC_DATA_READ) {
  126. priv->dma_dir = DMA_FROM_DEVICE;
  127. dma_tx_dir = DMA_DEV_TO_MEM;
  128. } else {
  129. priv->dma_dir = DMA_TO_DEVICE;
  130. dma_tx_dir = DMA_MEM_TO_DEV;
  131. }
  132. sg_len = dma_map_sg(mmc_dev(host->mmc), host->sg_ptr, host->sg_len,
  133. priv->dma_dir);
  134. if (sg_len == 0)
  135. goto force_pio;
  136. desc = dmaengine_prep_slave_sg(priv->chan, host->sg_ptr, sg_len,
  137. dma_tx_dir, DMA_CTRL_ACK);
  138. if (!desc)
  139. goto unmap_sg;
  140. desc->callback_result = uniphier_sd_external_dma_callback;
  141. desc->callback_param = host;
  142. cookie = dmaengine_submit(desc);
  143. if (cookie < 0)
  144. goto unmap_sg;
  145. host->dma_on = true;
  146. return;
  147. unmap_sg:
  148. dma_unmap_sg(mmc_dev(host->mmc), host->sg_ptr, host->sg_len,
  149. priv->dma_dir);
  150. force_pio:
  151. uniphier_sd_dma_endisable(host, 0);
  152. }
  153. static void uniphier_sd_external_dma_enable(struct tmio_mmc_host *host,
  154. bool enable)
  155. {
  156. }
  157. static void uniphier_sd_external_dma_request(struct tmio_mmc_host *host,
  158. struct tmio_mmc_data *pdata)
  159. {
  160. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  161. struct dma_chan *chan;
  162. chan = dma_request_chan(mmc_dev(host->mmc), "rx-tx");
  163. if (IS_ERR(chan)) {
  164. dev_warn(mmc_dev(host->mmc),
  165. "failed to request DMA channel. falling back to PIO\n");
  166. return; /* just use PIO even for -EPROBE_DEFER */
  167. }
  168. /* this driver uses a single channel for both RX an TX */
  169. priv->chan = chan;
  170. host->chan_rx = chan;
  171. host->chan_tx = chan;
  172. INIT_WORK(&host->dma_issue, uniphier_sd_external_dma_issue);
  173. }
  174. static void uniphier_sd_external_dma_release(struct tmio_mmc_host *host)
  175. {
  176. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  177. if (priv->chan)
  178. dma_release_channel(priv->chan);
  179. }
  180. static void uniphier_sd_external_dma_abort(struct tmio_mmc_host *host)
  181. {
  182. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  183. uniphier_sd_dma_endisable(host, 0);
  184. if (priv->chan)
  185. dmaengine_terminate_sync(priv->chan);
  186. }
  187. static void uniphier_sd_external_dma_dataend(struct tmio_mmc_host *host)
  188. {
  189. uniphier_sd_dma_endisable(host, 0);
  190. tmio_mmc_do_data_irq(host);
  191. }
  192. static const struct tmio_mmc_dma_ops uniphier_sd_external_dma_ops = {
  193. .start = uniphier_sd_external_dma_start,
  194. .enable = uniphier_sd_external_dma_enable,
  195. .request = uniphier_sd_external_dma_request,
  196. .release = uniphier_sd_external_dma_release,
  197. .abort = uniphier_sd_external_dma_abort,
  198. .dataend = uniphier_sd_external_dma_dataend,
  199. };
  200. static void uniphier_sd_internal_dma_issue(struct work_struct *t)
  201. {
  202. struct tmio_mmc_host *host = from_work(host, t, dma_issue);
  203. unsigned long flags;
  204. spin_lock_irqsave(&host->lock, flags);
  205. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  206. spin_unlock_irqrestore(&host->lock, flags);
  207. uniphier_sd_dma_endisable(host, 1);
  208. writel(UNIPHIER_SD_DMA_CTL_START, host->ctl + UNIPHIER_SD_DMA_CTL);
  209. }
  210. static void uniphier_sd_internal_dma_start(struct tmio_mmc_host *host,
  211. struct mmc_data *data)
  212. {
  213. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  214. struct scatterlist *sg = host->sg_ptr;
  215. dma_addr_t dma_addr;
  216. unsigned int dma_mode_dir;
  217. u32 dma_mode;
  218. int sg_len;
  219. if ((data->flags & MMC_DATA_READ) && !host->chan_rx)
  220. goto force_pio;
  221. if (WARN_ON(host->sg_len != 1))
  222. goto force_pio;
  223. if (!IS_ALIGNED(sg->offset, 8))
  224. goto force_pio;
  225. if (data->flags & MMC_DATA_READ) {
  226. priv->dma_dir = DMA_FROM_DEVICE;
  227. dma_mode_dir = UNIPHIER_SD_DMA_MODE_DIR_FROM_DEV;
  228. } else {
  229. priv->dma_dir = DMA_TO_DEVICE;
  230. dma_mode_dir = UNIPHIER_SD_DMA_MODE_DIR_TO_DEV;
  231. }
  232. sg_len = dma_map_sg(mmc_dev(host->mmc), sg, 1, priv->dma_dir);
  233. if (sg_len == 0)
  234. goto force_pio;
  235. dma_mode = FIELD_PREP(UNIPHIER_SD_DMA_MODE_DIR_MASK, dma_mode_dir);
  236. dma_mode |= FIELD_PREP(UNIPHIER_SD_DMA_MODE_WIDTH_MASK,
  237. UNIPHIER_SD_DMA_MODE_WIDTH_64);
  238. dma_mode |= UNIPHIER_SD_DMA_MODE_ADDR_INC;
  239. writel(dma_mode, host->ctl + UNIPHIER_SD_DMA_MODE);
  240. dma_addr = sg_dma_address(data->sg);
  241. writel(lower_32_bits(dma_addr), host->ctl + UNIPHIER_SD_DMA_ADDR_L);
  242. writel(upper_32_bits(dma_addr), host->ctl + UNIPHIER_SD_DMA_ADDR_H);
  243. host->dma_on = true;
  244. return;
  245. force_pio:
  246. uniphier_sd_dma_endisable(host, 0);
  247. }
  248. static void uniphier_sd_internal_dma_enable(struct tmio_mmc_host *host,
  249. bool enable)
  250. {
  251. }
  252. static void uniphier_sd_internal_dma_request(struct tmio_mmc_host *host,
  253. struct tmio_mmc_data *pdata)
  254. {
  255. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  256. /*
  257. * Due to a hardware bug, Pro5 cannot use DMA for RX.
  258. * We can still use DMA for TX, but PIO for RX.
  259. */
  260. if (!(priv->caps & UNIPHIER_SD_CAP_BROKEN_DMA_RX))
  261. host->chan_rx = (void *)0xdeadbeaf;
  262. host->chan_tx = (void *)0xdeadbeaf;
  263. INIT_WORK(&host->dma_issue, uniphier_sd_internal_dma_issue);
  264. }
  265. static void uniphier_sd_internal_dma_release(struct tmio_mmc_host *host)
  266. {
  267. /* Each value is set to zero to assume "disabling" each DMA */
  268. host->chan_rx = NULL;
  269. host->chan_tx = NULL;
  270. }
  271. static void uniphier_sd_internal_dma_abort(struct tmio_mmc_host *host)
  272. {
  273. u32 tmp;
  274. uniphier_sd_dma_endisable(host, 0);
  275. tmp = readl(host->ctl + UNIPHIER_SD_DMA_RST);
  276. tmp &= ~(UNIPHIER_SD_DMA_RST_CH1 | UNIPHIER_SD_DMA_RST_CH0);
  277. writel(tmp, host->ctl + UNIPHIER_SD_DMA_RST);
  278. tmp |= UNIPHIER_SD_DMA_RST_CH1 | UNIPHIER_SD_DMA_RST_CH0;
  279. writel(tmp, host->ctl + UNIPHIER_SD_DMA_RST);
  280. }
  281. static void uniphier_sd_internal_dma_dataend(struct tmio_mmc_host *host)
  282. {
  283. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  284. uniphier_sd_dma_endisable(host, 0);
  285. dma_unmap_sg(mmc_dev(host->mmc), host->sg_ptr, 1, priv->dma_dir);
  286. tmio_mmc_do_data_irq(host);
  287. }
  288. static const struct tmio_mmc_dma_ops uniphier_sd_internal_dma_ops = {
  289. .start = uniphier_sd_internal_dma_start,
  290. .enable = uniphier_sd_internal_dma_enable,
  291. .request = uniphier_sd_internal_dma_request,
  292. .release = uniphier_sd_internal_dma_release,
  293. .abort = uniphier_sd_internal_dma_abort,
  294. .dataend = uniphier_sd_internal_dma_dataend,
  295. };
  296. static int uniphier_sd_clk_enable(struct tmio_mmc_host *host)
  297. {
  298. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  299. struct mmc_host *mmc = host->mmc;
  300. int ret;
  301. ret = clk_prepare_enable(priv->clk);
  302. if (ret)
  303. return ret;
  304. ret = clk_set_rate(priv->clk, ULONG_MAX);
  305. if (ret)
  306. goto disable_clk;
  307. priv->clk_rate = clk_get_rate(priv->clk);
  308. /* If max-frequency property is set, use it. */
  309. if (!mmc->f_max)
  310. mmc->f_max = priv->clk_rate;
  311. /*
  312. * 1/512 is the finest divisor in the original IP. Newer versions
  313. * also supports 1/1024 divisor. (UniPhier-specific extension)
  314. */
  315. if (priv->caps & UNIPHIER_SD_CAP_EXTENDED_IP)
  316. mmc->f_min = priv->clk_rate / 1024;
  317. else
  318. mmc->f_min = priv->clk_rate / 512;
  319. ret = reset_control_deassert(priv->rst);
  320. if (ret)
  321. goto disable_clk;
  322. ret = reset_control_deassert(priv->rst_br);
  323. if (ret)
  324. goto assert_rst;
  325. return 0;
  326. assert_rst:
  327. reset_control_assert(priv->rst);
  328. disable_clk:
  329. clk_disable_unprepare(priv->clk);
  330. return ret;
  331. }
  332. static void uniphier_sd_clk_disable(struct tmio_mmc_host *host)
  333. {
  334. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  335. reset_control_assert(priv->rst_br);
  336. reset_control_assert(priv->rst);
  337. clk_disable_unprepare(priv->clk);
  338. }
  339. static void uniphier_sd_hw_reset(struct mmc_host *mmc)
  340. {
  341. struct tmio_mmc_host *host = mmc_priv(mmc);
  342. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  343. reset_control_assert(priv->rst_hw);
  344. /* For eMMC, minimum is 1us but give it 9us for good measure */
  345. udelay(9);
  346. reset_control_deassert(priv->rst_hw);
  347. /* For eMMC, minimum is 200us but give it 300us for good measure */
  348. usleep_range(300, 1000);
  349. }
  350. static void uniphier_sd_speed_switch(struct tmio_mmc_host *host)
  351. {
  352. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  353. unsigned int offset;
  354. u32 val = 0;
  355. if (!(host->mmc->caps & MMC_CAP_UHS))
  356. return;
  357. if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR50 ||
  358. host->mmc->ios.timing == MMC_TIMING_UHS_SDR104)
  359. val = UNIPHIER_SDCTRL_MODE_SDRSEL;
  360. offset = UNIPHIER_SDCTRL_CHOFFSET * priv->sdctrl_ch
  361. + UNIPHIER_SDCTRL_MODE;
  362. regmap_write_bits(priv->sdctrl_regmap, offset,
  363. UNIPHIER_SDCTRL_MODE_SDRSEL, val);
  364. }
  365. static void uniphier_sd_uhs_enable(struct tmio_mmc_host *host, bool uhs_en)
  366. {
  367. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  368. unsigned int offset;
  369. u32 val;
  370. if (!(host->mmc->caps & MMC_CAP_UHS))
  371. return;
  372. val = (uhs_en) ? UNIPHIER_SDCTRL_MODE_UHS1MOD : 0;
  373. offset = UNIPHIER_SDCTRL_CHOFFSET * priv->sdctrl_ch
  374. + UNIPHIER_SDCTRL_MODE;
  375. regmap_write_bits(priv->sdctrl_regmap, offset,
  376. UNIPHIER_SDCTRL_MODE_UHS1MOD, val);
  377. }
  378. static void uniphier_sd_set_clock(struct tmio_mmc_host *host,
  379. unsigned int clock)
  380. {
  381. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  382. unsigned long divisor;
  383. u32 tmp;
  384. tmp = readl(host->ctl + (CTL_SD_CARD_CLK_CTL << 1));
  385. /* stop the clock before changing its rate to avoid a glitch signal */
  386. tmp &= ~CLK_CTL_SCLKEN;
  387. writel(tmp, host->ctl + (CTL_SD_CARD_CLK_CTL << 1));
  388. uniphier_sd_speed_switch(host);
  389. if (clock == 0)
  390. return;
  391. tmp &= ~UNIPHIER_SD_CLK_CTL_DIV1024;
  392. tmp &= ~UNIPHIER_SD_CLK_CTL_DIV1;
  393. tmp &= ~CLK_CTL_DIV_MASK;
  394. divisor = priv->clk_rate / clock;
  395. /*
  396. * In the original IP, bit[7:0] represents the divisor.
  397. * bit7 set: 1/512, ... bit0 set:1/4, all bits clear: 1/2
  398. *
  399. * The IP does not define a way to achieve 1/1. For UniPhier variants,
  400. * bit10 is used for 1/1. Newer versions of UniPhier variants use
  401. * bit16 for 1/1024.
  402. */
  403. if (divisor <= 1)
  404. tmp |= UNIPHIER_SD_CLK_CTL_DIV1;
  405. else if (priv->caps & UNIPHIER_SD_CAP_EXTENDED_IP && divisor > 512)
  406. tmp |= UNIPHIER_SD_CLK_CTL_DIV1024;
  407. else
  408. tmp |= roundup_pow_of_two(divisor) >> 2;
  409. writel(tmp, host->ctl + (CTL_SD_CARD_CLK_CTL << 1));
  410. tmp |= CLK_CTL_SCLKEN;
  411. writel(tmp, host->ctl + (CTL_SD_CARD_CLK_CTL << 1));
  412. }
  413. static void uniphier_sd_host_init(struct tmio_mmc_host *host)
  414. {
  415. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  416. u32 val;
  417. /*
  418. * Connected to 32bit AXI.
  419. * This register holds settings for SoC-specific internal bus
  420. * connection. What is worse, the register spec was changed,
  421. * breaking the backward compatibility. Write an appropriate
  422. * value depending on a flag associated with a compatible string.
  423. */
  424. if (priv->caps & UNIPHIER_SD_CAP_EXTENDED_IP)
  425. val = 0x00000101;
  426. else
  427. val = 0x00000000;
  428. writel(val, host->ctl + UNIPHIER_SD_HOST_MODE);
  429. val = 0;
  430. /*
  431. * If supported, the controller can automatically
  432. * enable/disable the clock line to the card.
  433. */
  434. if (priv->caps & UNIPHIER_SD_CAP_EXTENDED_IP)
  435. val |= UNIPHIER_SD_CLKCTL_OFFEN;
  436. writel(val, host->ctl + (CTL_SD_CARD_CLK_CTL << 1));
  437. }
  438. static int uniphier_sd_start_signal_voltage_switch(struct mmc_host *mmc,
  439. struct mmc_ios *ios)
  440. {
  441. struct tmio_mmc_host *host = mmc_priv(mmc);
  442. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  443. struct pinctrl_state *pinstate = NULL;
  444. u32 val, tmp;
  445. bool uhs_en;
  446. switch (ios->signal_voltage) {
  447. case MMC_SIGNAL_VOLTAGE_330:
  448. val = UNIPHIER_SD_VOLT_330;
  449. uhs_en = false;
  450. break;
  451. case MMC_SIGNAL_VOLTAGE_180:
  452. val = UNIPHIER_SD_VOLT_180;
  453. pinstate = priv->pinstate_uhs;
  454. uhs_en = true;
  455. break;
  456. default:
  457. return -ENOTSUPP;
  458. }
  459. tmp = readl(host->ctl + UNIPHIER_SD_VOLT);
  460. tmp &= ~UNIPHIER_SD_VOLT_MASK;
  461. tmp |= FIELD_PREP(UNIPHIER_SD_VOLT_MASK, val);
  462. writel(tmp, host->ctl + UNIPHIER_SD_VOLT);
  463. if (pinstate)
  464. pinctrl_select_state(priv->pinctrl, pinstate);
  465. else
  466. pinctrl_select_default_state(mmc_dev(mmc));
  467. uniphier_sd_uhs_enable(host, uhs_en);
  468. return 0;
  469. }
  470. static int uniphier_sd_uhs_init(struct tmio_mmc_host *host)
  471. {
  472. struct uniphier_sd_priv *priv = uniphier_sd_priv(host);
  473. struct device *dev = &host->pdev->dev;
  474. struct device_node *np = dev->of_node;
  475. struct of_phandle_args args;
  476. int ret;
  477. priv->pinctrl = devm_pinctrl_get(mmc_dev(host->mmc));
  478. if (IS_ERR(priv->pinctrl))
  479. return PTR_ERR(priv->pinctrl);
  480. priv->pinstate_uhs = pinctrl_lookup_state(priv->pinctrl, "uhs");
  481. if (IS_ERR(priv->pinstate_uhs))
  482. return PTR_ERR(priv->pinstate_uhs);
  483. ret = of_parse_phandle_with_fixed_args(np,
  484. "socionext,syscon-uhs-mode",
  485. 1, 0, &args);
  486. if (ret) {
  487. dev_err(dev, "Can't get syscon-uhs-mode property\n");
  488. return ret;
  489. }
  490. priv->sdctrl_regmap = syscon_node_to_regmap(args.np);
  491. of_node_put(args.np);
  492. if (IS_ERR(priv->sdctrl_regmap)) {
  493. dev_err(dev, "Can't map syscon-uhs-mode\n");
  494. return PTR_ERR(priv->sdctrl_regmap);
  495. }
  496. priv->sdctrl_ch = args.args[0];
  497. return 0;
  498. }
  499. static int uniphier_sd_probe(struct platform_device *pdev)
  500. {
  501. struct device *dev = &pdev->dev;
  502. struct uniphier_sd_priv *priv;
  503. struct tmio_mmc_data *tmio_data;
  504. struct tmio_mmc_host *host;
  505. int irq, ret;
  506. irq = platform_get_irq(pdev, 0);
  507. if (irq < 0)
  508. return irq;
  509. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  510. if (!priv)
  511. return -ENOMEM;
  512. priv->caps = (unsigned long)of_device_get_match_data(dev);
  513. priv->clk = devm_clk_get(dev, NULL);
  514. if (IS_ERR(priv->clk)) {
  515. dev_err(dev, "failed to get clock\n");
  516. return PTR_ERR(priv->clk);
  517. }
  518. priv->rst = devm_reset_control_get_shared(dev, "host");
  519. if (IS_ERR(priv->rst)) {
  520. dev_err(dev, "failed to get host reset\n");
  521. return PTR_ERR(priv->rst);
  522. }
  523. /* old version has one more reset */
  524. if (!(priv->caps & UNIPHIER_SD_CAP_EXTENDED_IP)) {
  525. priv->rst_br = devm_reset_control_get_shared(dev, "bridge");
  526. if (IS_ERR(priv->rst_br)) {
  527. dev_err(dev, "failed to get bridge reset\n");
  528. return PTR_ERR(priv->rst_br);
  529. }
  530. }
  531. tmio_data = &priv->tmio_data;
  532. tmio_data->flags |= TMIO_MMC_32BIT_DATA_PORT;
  533. tmio_data->flags |= TMIO_MMC_USE_BUSY_TIMEOUT;
  534. host = tmio_mmc_host_alloc(pdev, tmio_data);
  535. if (IS_ERR(host))
  536. return PTR_ERR(host);
  537. if (host->mmc->caps & MMC_CAP_HW_RESET) {
  538. priv->rst_hw = devm_reset_control_get_exclusive(dev, "hw");
  539. if (IS_ERR(priv->rst_hw)) {
  540. dev_err(dev, "failed to get hw reset\n");
  541. return PTR_ERR(priv->rst_hw);
  542. }
  543. host->ops.card_hw_reset = uniphier_sd_hw_reset;
  544. }
  545. if (host->mmc->caps & MMC_CAP_UHS) {
  546. ret = uniphier_sd_uhs_init(host);
  547. if (ret) {
  548. dev_warn(dev,
  549. "failed to setup UHS (error %d). Disabling UHS.",
  550. ret);
  551. host->mmc->caps &= ~MMC_CAP_UHS;
  552. } else {
  553. host->ops.start_signal_voltage_switch =
  554. uniphier_sd_start_signal_voltage_switch;
  555. }
  556. }
  557. if (priv->caps & UNIPHIER_SD_CAP_EXTENDED_IP)
  558. host->dma_ops = &uniphier_sd_internal_dma_ops;
  559. else
  560. host->dma_ops = &uniphier_sd_external_dma_ops;
  561. host->bus_shift = 1;
  562. host->clk_enable = uniphier_sd_clk_enable;
  563. host->clk_disable = uniphier_sd_clk_disable;
  564. host->set_clock = uniphier_sd_set_clock;
  565. ret = uniphier_sd_clk_enable(host);
  566. if (ret)
  567. return ret;
  568. uniphier_sd_host_init(host);
  569. tmio_data->ocr_mask = MMC_VDD_32_33 | MMC_VDD_33_34;
  570. if (host->mmc->caps & MMC_CAP_UHS)
  571. tmio_data->ocr_mask |= MMC_VDD_165_195;
  572. tmio_data->max_segs = 1;
  573. tmio_data->max_blk_count = U16_MAX;
  574. sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, TMIO_MASK_ALL);
  575. ret = devm_request_irq(dev, irq, tmio_mmc_irq, IRQF_SHARED,
  576. dev_name(dev), host);
  577. if (ret)
  578. goto disable_clk;
  579. ret = tmio_mmc_host_probe(host);
  580. if (ret)
  581. goto disable_clk;
  582. return 0;
  583. disable_clk:
  584. uniphier_sd_clk_disable(host);
  585. return ret;
  586. }
  587. static void uniphier_sd_remove(struct platform_device *pdev)
  588. {
  589. struct tmio_mmc_host *host = platform_get_drvdata(pdev);
  590. tmio_mmc_host_remove(host);
  591. uniphier_sd_clk_disable(host);
  592. }
  593. static const struct of_device_id uniphier_sd_match[] = {
  594. {
  595. .compatible = "socionext,uniphier-sd-v2.91",
  596. },
  597. {
  598. .compatible = "socionext,uniphier-sd-v3.1",
  599. .data = (void *)(UNIPHIER_SD_CAP_EXTENDED_IP |
  600. UNIPHIER_SD_CAP_BROKEN_DMA_RX),
  601. },
  602. {
  603. .compatible = "socionext,uniphier-sd-v3.1.1",
  604. .data = (void *)UNIPHIER_SD_CAP_EXTENDED_IP,
  605. },
  606. { /* sentinel */ }
  607. };
  608. MODULE_DEVICE_TABLE(of, uniphier_sd_match);
  609. static struct platform_driver uniphier_sd_driver = {
  610. .probe = uniphier_sd_probe,
  611. .remove = uniphier_sd_remove,
  612. .driver = {
  613. .name = "uniphier-sd",
  614. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  615. .of_match_table = uniphier_sd_match,
  616. },
  617. };
  618. module_platform_driver(uniphier_sd_driver);
  619. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  620. MODULE_DESCRIPTION("UniPhier SD/eMMC host controller driver");
  621. MODULE_LICENSE("GPL v2");