sun8i-a33-mbus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright (C) 2020-2021 Samuel Holland <[email protected]>
  4. //
  5. #include <linux/clk.h>
  6. #include <linux/devfreq.h>
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/property.h>
  14. #define MBUS_CR 0x0000
  15. #define MBUS_CR_GET_DRAM_TYPE(x) (((x) >> 16) & 0x7)
  16. #define MBUS_CR_DRAM_TYPE_DDR2 2
  17. #define MBUS_CR_DRAM_TYPE_DDR3 3
  18. #define MBUS_CR_DRAM_TYPE_DDR4 4
  19. #define MBUS_CR_DRAM_TYPE_LPDDR2 6
  20. #define MBUS_CR_DRAM_TYPE_LPDDR3 7
  21. #define MBUS_TMR 0x000c
  22. #define MBUS_TMR_PERIOD(x) ((x) - 1)
  23. #define MBUS_PMU_CFG 0x009c
  24. #define MBUS_PMU_CFG_PERIOD(x) (((x) - 1) << 16)
  25. #define MBUS_PMU_CFG_UNIT (0x3 << 1)
  26. #define MBUS_PMU_CFG_UNIT_B (0x0 << 1)
  27. #define MBUS_PMU_CFG_UNIT_KB (0x1 << 1)
  28. #define MBUS_PMU_CFG_UNIT_MB (0x2 << 1)
  29. #define MBUS_PMU_CFG_ENABLE (0x1 << 0)
  30. #define MBUS_PMU_BWCR(n) (0x00a0 + (0x04 * (n)))
  31. #define MBUS_TOTAL_BWCR MBUS_PMU_BWCR(5)
  32. #define MBUS_TOTAL_BWCR_H616 MBUS_PMU_BWCR(13)
  33. #define MBUS_MDFSCR 0x0100
  34. #define MBUS_MDFSCR_BUFFER_TIMING (0x1 << 15)
  35. #define MBUS_MDFSCR_PAD_HOLD (0x1 << 13)
  36. #define MBUS_MDFSCR_BYPASS (0x1 << 4)
  37. #define MBUS_MDFSCR_MODE (0x1 << 1)
  38. #define MBUS_MDFSCR_MODE_DFS (0x0 << 1)
  39. #define MBUS_MDFSCR_MODE_CFS (0x1 << 1)
  40. #define MBUS_MDFSCR_START (0x1 << 0)
  41. #define MBUS_MDFSMRMR 0x0108
  42. #define DRAM_PWRCTL 0x0004
  43. #define DRAM_PWRCTL_SELFREF_EN (0x1 << 0)
  44. #define DRAM_RFSHTMG 0x0090
  45. #define DRAM_RFSHTMG_TREFI(x) ((x) << 16)
  46. #define DRAM_RFSHTMG_TRFC(x) ((x) << 0)
  47. #define DRAM_VTFCR 0x00b8
  48. #define DRAM_VTFCR_VTF_ENABLE (0x3 << 8)
  49. #define DRAM_ODTMAP 0x0120
  50. #define DRAM_DX_MAX 4
  51. #define DRAM_DXnGCR0(n) (0x0344 + 0x80 * (n))
  52. #define DRAM_DXnGCR0_DXODT (0x3 << 4)
  53. #define DRAM_DXnGCR0_DXODT_DYNAMIC (0x0 << 4)
  54. #define DRAM_DXnGCR0_DXODT_ENABLED (0x1 << 4)
  55. #define DRAM_DXnGCR0_DXODT_DISABLED (0x2 << 4)
  56. #define DRAM_DXnGCR0_DXEN (0x1 << 0)
  57. struct sun8i_a33_mbus_variant {
  58. u32 min_dram_divider;
  59. u32 max_dram_divider;
  60. u32 odt_freq_mhz;
  61. };
  62. struct sun8i_a33_mbus {
  63. const struct sun8i_a33_mbus_variant *variant;
  64. void __iomem *reg_dram;
  65. void __iomem *reg_mbus;
  66. struct clk *clk_bus;
  67. struct clk *clk_dram;
  68. struct clk *clk_mbus;
  69. struct devfreq *devfreq_dram;
  70. struct devfreq_simple_ondemand_data gov_data;
  71. struct devfreq_dev_profile profile;
  72. u32 data_width;
  73. u32 nominal_bw;
  74. u32 odtmap;
  75. u32 tREFI_ns;
  76. u32 tRFC_ns;
  77. unsigned long freq_table[];
  78. };
  79. /*
  80. * The unit for this value is (MBUS clock cycles / MBUS_TMR_PERIOD). When
  81. * MBUS_TMR_PERIOD is programmed to match the MBUS clock frequency in MHz, as
  82. * it is during DRAM init and during probe, the resulting unit is microseconds.
  83. */
  84. static int pmu_period = 50000;
  85. module_param(pmu_period, int, 0644);
  86. MODULE_PARM_DESC(pmu_period, "Bandwidth measurement period (microseconds)");
  87. static u32 sun8i_a33_mbus_get_peak_bw(struct sun8i_a33_mbus *priv)
  88. {
  89. /* Returns the peak transfer (in KiB) during any single PMU period. */
  90. return readl_relaxed(priv->reg_mbus + MBUS_TOTAL_BWCR);
  91. }
  92. static void sun8i_a33_mbus_restart_pmu_counters(struct sun8i_a33_mbus *priv)
  93. {
  94. u32 pmu_cfg = MBUS_PMU_CFG_PERIOD(pmu_period) | MBUS_PMU_CFG_UNIT_KB;
  95. /* All PMU counters are cleared on a disable->enable transition. */
  96. writel_relaxed(pmu_cfg,
  97. priv->reg_mbus + MBUS_PMU_CFG);
  98. writel_relaxed(pmu_cfg | MBUS_PMU_CFG_ENABLE,
  99. priv->reg_mbus + MBUS_PMU_CFG);
  100. }
  101. static void sun8i_a33_mbus_update_nominal_bw(struct sun8i_a33_mbus *priv,
  102. u32 ddr_freq_mhz)
  103. {
  104. /*
  105. * Nominal bandwidth (KiB per PMU period):
  106. *
  107. * DDR transfers microseconds KiB
  108. * ------------- * ------------ * --------
  109. * microsecond PMU period transfer
  110. */
  111. priv->nominal_bw = ddr_freq_mhz * pmu_period * priv->data_width / 1024;
  112. }
  113. static int sun8i_a33_mbus_set_dram_freq(struct sun8i_a33_mbus *priv,
  114. unsigned long freq)
  115. {
  116. u32 ddr_freq_mhz = freq / USEC_PER_SEC; /* DDR */
  117. u32 dram_freq_mhz = ddr_freq_mhz / 2; /* SDR */
  118. u32 mctl_freq_mhz = dram_freq_mhz / 2; /* HDR */
  119. u32 dxodt, mdfscr, pwrctl, vtfcr;
  120. u32 i, tREFI_32ck, tRFC_ck;
  121. int ret;
  122. /* The rate change is not effective until the MDFS process runs. */
  123. ret = clk_set_rate(priv->clk_dram, freq);
  124. if (ret)
  125. return ret;
  126. /* Disable automatic self-refesh and VTF before starting MDFS. */
  127. pwrctl = readl_relaxed(priv->reg_dram + DRAM_PWRCTL) &
  128. ~DRAM_PWRCTL_SELFREF_EN;
  129. writel_relaxed(pwrctl, priv->reg_dram + DRAM_PWRCTL);
  130. vtfcr = readl_relaxed(priv->reg_dram + DRAM_VTFCR);
  131. writel_relaxed(vtfcr & ~DRAM_VTFCR_VTF_ENABLE,
  132. priv->reg_dram + DRAM_VTFCR);
  133. /* Set up MDFS and enable double buffering for timing registers. */
  134. mdfscr = MBUS_MDFSCR_MODE_DFS |
  135. MBUS_MDFSCR_BYPASS |
  136. MBUS_MDFSCR_PAD_HOLD |
  137. MBUS_MDFSCR_BUFFER_TIMING;
  138. writel(mdfscr, priv->reg_mbus + MBUS_MDFSCR);
  139. /* Update the buffered copy of RFSHTMG. */
  140. tREFI_32ck = priv->tREFI_ns * mctl_freq_mhz / 1000 / 32;
  141. tRFC_ck = DIV_ROUND_UP(priv->tRFC_ns * mctl_freq_mhz, 1000);
  142. writel(DRAM_RFSHTMG_TREFI(tREFI_32ck) | DRAM_RFSHTMG_TRFC(tRFC_ck),
  143. priv->reg_dram + DRAM_RFSHTMG);
  144. /* Enable ODT if needed, or disable it to save power. */
  145. if (priv->odtmap && dram_freq_mhz > priv->variant->odt_freq_mhz) {
  146. dxodt = DRAM_DXnGCR0_DXODT_DYNAMIC;
  147. writel(priv->odtmap, priv->reg_dram + DRAM_ODTMAP);
  148. } else {
  149. dxodt = DRAM_DXnGCR0_DXODT_DISABLED;
  150. writel(0, priv->reg_dram + DRAM_ODTMAP);
  151. }
  152. for (i = 0; i < DRAM_DX_MAX; ++i) {
  153. void __iomem *reg = priv->reg_dram + DRAM_DXnGCR0(i);
  154. writel((readl(reg) & ~DRAM_DXnGCR0_DXODT) | dxodt, reg);
  155. }
  156. dev_dbg(priv->devfreq_dram->dev.parent,
  157. "Setting DRAM to %u MHz, tREFI=%u, tRFC=%u, ODT=%s\n",
  158. dram_freq_mhz, tREFI_32ck, tRFC_ck,
  159. dxodt == DRAM_DXnGCR0_DXODT_DYNAMIC ? "dynamic" : "disabled");
  160. /* Trigger hardware MDFS. */
  161. writel(mdfscr | MBUS_MDFSCR_START, priv->reg_mbus + MBUS_MDFSCR);
  162. ret = readl_poll_timeout_atomic(priv->reg_mbus + MBUS_MDFSCR, mdfscr,
  163. !(mdfscr & MBUS_MDFSCR_START), 10, 1000);
  164. if (ret)
  165. return ret;
  166. /* Disable double buffering. */
  167. writel(0, priv->reg_mbus + MBUS_MDFSCR);
  168. /* Restore VTF configuration. */
  169. writel_relaxed(vtfcr, priv->reg_dram + DRAM_VTFCR);
  170. /* Enable automatic self-refresh at the lowest frequency only. */
  171. if (freq == priv->freq_table[0])
  172. pwrctl |= DRAM_PWRCTL_SELFREF_EN;
  173. writel_relaxed(pwrctl, priv->reg_dram + DRAM_PWRCTL);
  174. sun8i_a33_mbus_restart_pmu_counters(priv);
  175. sun8i_a33_mbus_update_nominal_bw(priv, ddr_freq_mhz);
  176. return 0;
  177. }
  178. static int sun8i_a33_mbus_set_dram_target(struct device *dev,
  179. unsigned long *freq, u32 flags)
  180. {
  181. struct sun8i_a33_mbus *priv = dev_get_drvdata(dev);
  182. struct devfreq *devfreq = priv->devfreq_dram;
  183. struct dev_pm_opp *opp;
  184. int ret;
  185. opp = devfreq_recommended_opp(dev, freq, flags);
  186. if (IS_ERR(opp))
  187. return PTR_ERR(opp);
  188. dev_pm_opp_put(opp);
  189. if (*freq == devfreq->previous_freq)
  190. return 0;
  191. ret = sun8i_a33_mbus_set_dram_freq(priv, *freq);
  192. if (ret) {
  193. dev_warn(dev, "failed to set DRAM frequency: %d\n", ret);
  194. *freq = devfreq->previous_freq;
  195. }
  196. return ret;
  197. }
  198. static int sun8i_a33_mbus_get_dram_status(struct device *dev,
  199. struct devfreq_dev_status *stat)
  200. {
  201. struct sun8i_a33_mbus *priv = dev_get_drvdata(dev);
  202. stat->busy_time = sun8i_a33_mbus_get_peak_bw(priv);
  203. stat->total_time = priv->nominal_bw;
  204. stat->current_frequency = priv->devfreq_dram->previous_freq;
  205. sun8i_a33_mbus_restart_pmu_counters(priv);
  206. dev_dbg(dev, "Using %lu/%lu (%lu%%) at %lu MHz\n",
  207. stat->busy_time, stat->total_time,
  208. DIV_ROUND_CLOSEST(stat->busy_time * 100, stat->total_time),
  209. stat->current_frequency / USEC_PER_SEC);
  210. return 0;
  211. }
  212. static int sun8i_a33_mbus_hw_init(struct device *dev,
  213. struct sun8i_a33_mbus *priv,
  214. unsigned long ddr_freq)
  215. {
  216. u32 i, mbus_cr, mbus_freq_mhz;
  217. /* Choose tREFI and tRFC to match the configured DRAM type. */
  218. mbus_cr = readl_relaxed(priv->reg_mbus + MBUS_CR);
  219. switch (MBUS_CR_GET_DRAM_TYPE(mbus_cr)) {
  220. case MBUS_CR_DRAM_TYPE_DDR2:
  221. case MBUS_CR_DRAM_TYPE_DDR3:
  222. case MBUS_CR_DRAM_TYPE_DDR4:
  223. priv->tREFI_ns = 7800;
  224. priv->tRFC_ns = 350;
  225. break;
  226. case MBUS_CR_DRAM_TYPE_LPDDR2:
  227. case MBUS_CR_DRAM_TYPE_LPDDR3:
  228. priv->tREFI_ns = 3900;
  229. priv->tRFC_ns = 210;
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. /* Save ODTMAP so it can be restored when raising the frequency. */
  235. priv->odtmap = readl_relaxed(priv->reg_dram + DRAM_ODTMAP);
  236. /* Compute the DRAM data bus width by counting enabled DATx8 blocks. */
  237. for (i = 0; i < DRAM_DX_MAX; ++i) {
  238. void __iomem *reg = priv->reg_dram + DRAM_DXnGCR0(i);
  239. if (!(readl_relaxed(reg) & DRAM_DXnGCR0_DXEN))
  240. break;
  241. }
  242. priv->data_width = i;
  243. dev_dbg(dev, "Detected %u-bit %sDDRx with%s ODT\n",
  244. priv->data_width * 8,
  245. MBUS_CR_GET_DRAM_TYPE(mbus_cr) > 4 ? "LP" : "",
  246. priv->odtmap ? "" : "out");
  247. /* Program MBUS_TMR such that the PMU period unit is microseconds. */
  248. mbus_freq_mhz = clk_get_rate(priv->clk_mbus) / USEC_PER_SEC;
  249. writel_relaxed(MBUS_TMR_PERIOD(mbus_freq_mhz),
  250. priv->reg_mbus + MBUS_TMR);
  251. /* "Master Ready Mask Register" bits must be set or MDFS will block. */
  252. writel_relaxed(0xffffffff, priv->reg_mbus + MBUS_MDFSMRMR);
  253. sun8i_a33_mbus_restart_pmu_counters(priv);
  254. sun8i_a33_mbus_update_nominal_bw(priv, ddr_freq / USEC_PER_SEC);
  255. return 0;
  256. }
  257. static int __maybe_unused sun8i_a33_mbus_suspend(struct device *dev)
  258. {
  259. struct sun8i_a33_mbus *priv = dev_get_drvdata(dev);
  260. clk_disable_unprepare(priv->clk_bus);
  261. return 0;
  262. }
  263. static int __maybe_unused sun8i_a33_mbus_resume(struct device *dev)
  264. {
  265. struct sun8i_a33_mbus *priv = dev_get_drvdata(dev);
  266. return clk_prepare_enable(priv->clk_bus);
  267. }
  268. static int sun8i_a33_mbus_probe(struct platform_device *pdev)
  269. {
  270. const struct sun8i_a33_mbus_variant *variant;
  271. struct device *dev = &pdev->dev;
  272. struct sun8i_a33_mbus *priv;
  273. unsigned long base_freq;
  274. unsigned int max_state;
  275. const char *err;
  276. int i, ret;
  277. variant = device_get_match_data(dev);
  278. if (!variant)
  279. return -EINVAL;
  280. max_state = variant->max_dram_divider - variant->min_dram_divider + 1;
  281. priv = devm_kzalloc(dev, struct_size(priv, freq_table, max_state), GFP_KERNEL);
  282. if (!priv)
  283. return -ENOMEM;
  284. platform_set_drvdata(pdev, priv);
  285. priv->variant = variant;
  286. priv->reg_dram = devm_platform_ioremap_resource_byname(pdev, "dram");
  287. if (IS_ERR(priv->reg_dram))
  288. return PTR_ERR(priv->reg_dram);
  289. priv->reg_mbus = devm_platform_ioremap_resource_byname(pdev, "mbus");
  290. if (IS_ERR(priv->reg_mbus))
  291. return PTR_ERR(priv->reg_mbus);
  292. priv->clk_bus = devm_clk_get(dev, "bus");
  293. if (IS_ERR(priv->clk_bus))
  294. return dev_err_probe(dev, PTR_ERR(priv->clk_bus),
  295. "failed to get bus clock\n");
  296. priv->clk_dram = devm_clk_get(dev, "dram");
  297. if (IS_ERR(priv->clk_dram))
  298. return dev_err_probe(dev, PTR_ERR(priv->clk_dram),
  299. "failed to get dram clock\n");
  300. priv->clk_mbus = devm_clk_get(dev, "mbus");
  301. if (IS_ERR(priv->clk_mbus))
  302. return dev_err_probe(dev, PTR_ERR(priv->clk_mbus),
  303. "failed to get mbus clock\n");
  304. ret = clk_prepare_enable(priv->clk_bus);
  305. if (ret)
  306. return dev_err_probe(dev, ret,
  307. "failed to enable bus clock\n");
  308. /* Lock the DRAM clock rate to keep priv->nominal_bw in sync. */
  309. ret = clk_rate_exclusive_get(priv->clk_dram);
  310. if (ret) {
  311. err = "failed to lock dram clock rate\n";
  312. goto err_disable_bus;
  313. }
  314. /* Lock the MBUS clock rate to keep MBUS_TMR_PERIOD in sync. */
  315. ret = clk_rate_exclusive_get(priv->clk_mbus);
  316. if (ret) {
  317. err = "failed to lock mbus clock rate\n";
  318. goto err_unlock_dram;
  319. }
  320. priv->gov_data.upthreshold = 10;
  321. priv->gov_data.downdifferential = 5;
  322. priv->profile.initial_freq = clk_get_rate(priv->clk_dram);
  323. priv->profile.polling_ms = 1000;
  324. priv->profile.target = sun8i_a33_mbus_set_dram_target;
  325. priv->profile.get_dev_status = sun8i_a33_mbus_get_dram_status;
  326. priv->profile.freq_table = priv->freq_table;
  327. priv->profile.max_state = max_state;
  328. ret = devm_pm_opp_set_clkname(dev, "dram");
  329. if (ret) {
  330. err = "failed to add OPP table\n";
  331. goto err_unlock_mbus;
  332. }
  333. base_freq = clk_get_rate(clk_get_parent(priv->clk_dram));
  334. for (i = 0; i < max_state; ++i) {
  335. unsigned int div = variant->max_dram_divider - i;
  336. priv->freq_table[i] = base_freq / div;
  337. ret = dev_pm_opp_add(dev, priv->freq_table[i], 0);
  338. if (ret) {
  339. err = "failed to add OPPs\n";
  340. goto err_remove_opps;
  341. }
  342. }
  343. ret = sun8i_a33_mbus_hw_init(dev, priv, priv->profile.initial_freq);
  344. if (ret) {
  345. err = "failed to init hardware\n";
  346. goto err_remove_opps;
  347. }
  348. priv->devfreq_dram = devfreq_add_device(dev, &priv->profile,
  349. DEVFREQ_GOV_SIMPLE_ONDEMAND,
  350. &priv->gov_data);
  351. if (IS_ERR(priv->devfreq_dram)) {
  352. ret = PTR_ERR(priv->devfreq_dram);
  353. err = "failed to add devfreq device\n";
  354. goto err_remove_opps;
  355. }
  356. /*
  357. * This must be set manually after registering the devfreq device,
  358. * because there is no way to select a dynamic OPP as the suspend OPP.
  359. */
  360. priv->devfreq_dram->suspend_freq = priv->freq_table[0];
  361. return 0;
  362. err_remove_opps:
  363. dev_pm_opp_remove_all_dynamic(dev);
  364. err_unlock_mbus:
  365. clk_rate_exclusive_put(priv->clk_mbus);
  366. err_unlock_dram:
  367. clk_rate_exclusive_put(priv->clk_dram);
  368. err_disable_bus:
  369. clk_disable_unprepare(priv->clk_bus);
  370. return dev_err_probe(dev, ret, err);
  371. }
  372. static int sun8i_a33_mbus_remove(struct platform_device *pdev)
  373. {
  374. struct sun8i_a33_mbus *priv = platform_get_drvdata(pdev);
  375. unsigned long initial_freq = priv->profile.initial_freq;
  376. struct device *dev = &pdev->dev;
  377. int ret;
  378. devfreq_remove_device(priv->devfreq_dram);
  379. ret = sun8i_a33_mbus_set_dram_freq(priv, initial_freq);
  380. if (ret)
  381. dev_warn(dev, "failed to restore DRAM frequency: %d\n", ret);
  382. dev_pm_opp_remove_all_dynamic(dev);
  383. clk_rate_exclusive_put(priv->clk_mbus);
  384. clk_rate_exclusive_put(priv->clk_dram);
  385. clk_disable_unprepare(priv->clk_bus);
  386. return 0;
  387. }
  388. static const struct sun8i_a33_mbus_variant sun50i_a64_mbus = {
  389. .min_dram_divider = 1,
  390. .max_dram_divider = 4,
  391. .odt_freq_mhz = 400,
  392. };
  393. static const struct of_device_id sun8i_a33_mbus_of_match[] = {
  394. { .compatible = "allwinner,sun50i-a64-mbus", .data = &sun50i_a64_mbus },
  395. { .compatible = "allwinner,sun50i-h5-mbus", .data = &sun50i_a64_mbus },
  396. { },
  397. };
  398. MODULE_DEVICE_TABLE(of, sun8i_a33_mbus_of_match);
  399. static SIMPLE_DEV_PM_OPS(sun8i_a33_mbus_pm_ops,
  400. sun8i_a33_mbus_suspend, sun8i_a33_mbus_resume);
  401. static struct platform_driver sun8i_a33_mbus_driver = {
  402. .probe = sun8i_a33_mbus_probe,
  403. .remove = sun8i_a33_mbus_remove,
  404. .driver = {
  405. .name = "sun8i-a33-mbus",
  406. .of_match_table = sun8i_a33_mbus_of_match,
  407. .pm = pm_ptr(&sun8i_a33_mbus_pm_ops),
  408. },
  409. };
  410. module_platform_driver(sun8i_a33_mbus_driver);
  411. MODULE_AUTHOR("Samuel Holland <[email protected]>");
  412. MODULE_DESCRIPTION("Allwinner sun8i/sun50i MBUS DEVFREQ Driver");
  413. MODULE_LICENSE("GPL v2");