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