uniphier-sd.c 18 KB

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