clk-mt7621.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Mediatek MT7621 Clock Driver
  4. * Author: Sergio Paracuellos <[email protected]>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bitops.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/clk.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/reset-controller.h>
  14. #include <linux/slab.h>
  15. #include <dt-bindings/clock/mt7621-clk.h>
  16. #include <dt-bindings/reset/mt7621-reset.h>
  17. /* Configuration registers */
  18. #define SYSC_REG_SYSTEM_CONFIG0 0x10
  19. #define SYSC_REG_SYSTEM_CONFIG1 0x14
  20. #define SYSC_REG_CLKCFG0 0x2c
  21. #define SYSC_REG_CLKCFG1 0x30
  22. #define SYSC_REG_RESET_CTRL 0x34
  23. #define SYSC_REG_CUR_CLK_STS 0x44
  24. #define MEMC_REG_CPU_PLL 0x648
  25. #define XTAL_MODE_SEL_MASK GENMASK(8, 6)
  26. #define CPU_CLK_SEL_MASK GENMASK(31, 30)
  27. #define CUR_CPU_FDIV_MASK GENMASK(12, 8)
  28. #define CUR_CPU_FFRAC_MASK GENMASK(4, 0)
  29. #define CPU_PLL_PREDIV_MASK GENMASK(13, 12)
  30. #define CPU_PLL_FBDIV_MASK GENMASK(10, 4)
  31. struct mt7621_clk_priv {
  32. struct regmap *sysc;
  33. struct regmap *memc;
  34. };
  35. struct mt7621_clk {
  36. struct clk_hw hw;
  37. struct mt7621_clk_priv *priv;
  38. };
  39. struct mt7621_fixed_clk {
  40. u8 idx;
  41. const char *name;
  42. const char *parent_name;
  43. unsigned long rate;
  44. struct clk_hw *hw;
  45. };
  46. struct mt7621_gate {
  47. u8 idx;
  48. const char *name;
  49. const char *parent_name;
  50. struct mt7621_clk_priv *priv;
  51. u32 bit_idx;
  52. struct clk_hw hw;
  53. };
  54. #define GATE(_id, _name, _pname, _shift) \
  55. { \
  56. .idx = _id, \
  57. .name = _name, \
  58. .parent_name = _pname, \
  59. .bit_idx = _shift \
  60. }
  61. static struct mt7621_gate mt7621_gates[] = {
  62. GATE(MT7621_CLK_HSDMA, "hsdma", "150m", BIT(5)),
  63. GATE(MT7621_CLK_FE, "fe", "250m", BIT(6)),
  64. GATE(MT7621_CLK_SP_DIVTX, "sp_divtx", "270m", BIT(7)),
  65. GATE(MT7621_CLK_TIMER, "timer", "50m", BIT(8)),
  66. GATE(MT7621_CLK_PCM, "pcm", "270m", BIT(11)),
  67. GATE(MT7621_CLK_PIO, "pio", "50m", BIT(13)),
  68. GATE(MT7621_CLK_GDMA, "gdma", "bus", BIT(14)),
  69. GATE(MT7621_CLK_NAND, "nand", "125m", BIT(15)),
  70. GATE(MT7621_CLK_I2C, "i2c", "50m", BIT(16)),
  71. GATE(MT7621_CLK_I2S, "i2s", "270m", BIT(17)),
  72. GATE(MT7621_CLK_SPI, "spi", "bus", BIT(18)),
  73. GATE(MT7621_CLK_UART1, "uart1", "50m", BIT(19)),
  74. GATE(MT7621_CLK_UART2, "uart2", "50m", BIT(20)),
  75. GATE(MT7621_CLK_UART3, "uart3", "50m", BIT(21)),
  76. GATE(MT7621_CLK_ETH, "eth", "50m", BIT(23)),
  77. GATE(MT7621_CLK_PCIE0, "pcie0", "125m", BIT(24)),
  78. GATE(MT7621_CLK_PCIE1, "pcie1", "125m", BIT(25)),
  79. GATE(MT7621_CLK_PCIE2, "pcie2", "125m", BIT(26)),
  80. GATE(MT7621_CLK_CRYPTO, "crypto", "250m", BIT(29)),
  81. GATE(MT7621_CLK_SHXC, "shxc", "50m", BIT(30))
  82. };
  83. static inline struct mt7621_gate *to_mt7621_gate(struct clk_hw *hw)
  84. {
  85. return container_of(hw, struct mt7621_gate, hw);
  86. }
  87. static int mt7621_gate_enable(struct clk_hw *hw)
  88. {
  89. struct mt7621_gate *clk_gate = to_mt7621_gate(hw);
  90. struct regmap *sysc = clk_gate->priv->sysc;
  91. return regmap_update_bits(sysc, SYSC_REG_CLKCFG1,
  92. clk_gate->bit_idx, clk_gate->bit_idx);
  93. }
  94. static void mt7621_gate_disable(struct clk_hw *hw)
  95. {
  96. struct mt7621_gate *clk_gate = to_mt7621_gate(hw);
  97. struct regmap *sysc = clk_gate->priv->sysc;
  98. regmap_update_bits(sysc, SYSC_REG_CLKCFG1, clk_gate->bit_idx, 0);
  99. }
  100. static int mt7621_gate_is_enabled(struct clk_hw *hw)
  101. {
  102. struct mt7621_gate *clk_gate = to_mt7621_gate(hw);
  103. struct regmap *sysc = clk_gate->priv->sysc;
  104. u32 val;
  105. if (regmap_read(sysc, SYSC_REG_CLKCFG1, &val))
  106. return 0;
  107. return val & BIT(clk_gate->bit_idx);
  108. }
  109. static const struct clk_ops mt7621_gate_ops = {
  110. .enable = mt7621_gate_enable,
  111. .disable = mt7621_gate_disable,
  112. .is_enabled = mt7621_gate_is_enabled,
  113. };
  114. static int mt7621_gate_ops_init(struct device *dev,
  115. struct mt7621_gate *sclk)
  116. {
  117. struct clk_init_data init = {
  118. .flags = CLK_SET_RATE_PARENT,
  119. .num_parents = 1,
  120. .parent_names = &sclk->parent_name,
  121. .ops = &mt7621_gate_ops,
  122. .name = sclk->name,
  123. };
  124. sclk->hw.init = &init;
  125. return devm_clk_hw_register(dev, &sclk->hw);
  126. }
  127. static int mt7621_register_gates(struct device *dev,
  128. struct clk_hw_onecell_data *clk_data,
  129. struct mt7621_clk_priv *priv)
  130. {
  131. struct clk_hw **hws = clk_data->hws;
  132. struct mt7621_gate *sclk;
  133. int ret, i;
  134. for (i = 0; i < ARRAY_SIZE(mt7621_gates); i++) {
  135. sclk = &mt7621_gates[i];
  136. sclk->priv = priv;
  137. ret = mt7621_gate_ops_init(dev, sclk);
  138. if (ret) {
  139. dev_err(dev, "Couldn't register clock %s\n", sclk->name);
  140. goto err_clk_unreg;
  141. }
  142. hws[sclk->idx] = &sclk->hw;
  143. }
  144. return 0;
  145. err_clk_unreg:
  146. while (--i >= 0) {
  147. sclk = &mt7621_gates[i];
  148. clk_hw_unregister(&sclk->hw);
  149. }
  150. return ret;
  151. }
  152. #define FIXED(_id, _name, _rate) \
  153. { \
  154. .idx = _id, \
  155. .name = _name, \
  156. .parent_name = "xtal", \
  157. .rate = _rate \
  158. }
  159. static struct mt7621_fixed_clk mt7621_fixed_clks[] = {
  160. FIXED(MT7621_CLK_50M, "50m", 50000000),
  161. FIXED(MT7621_CLK_125M, "125m", 125000000),
  162. FIXED(MT7621_CLK_150M, "150m", 150000000),
  163. FIXED(MT7621_CLK_250M, "250m", 250000000),
  164. FIXED(MT7621_CLK_270M, "270m", 270000000),
  165. };
  166. static int mt7621_register_fixed_clocks(struct device *dev,
  167. struct clk_hw_onecell_data *clk_data)
  168. {
  169. struct clk_hw **hws = clk_data->hws;
  170. struct mt7621_fixed_clk *sclk;
  171. int ret, i;
  172. for (i = 0; i < ARRAY_SIZE(mt7621_fixed_clks); i++) {
  173. sclk = &mt7621_fixed_clks[i];
  174. sclk->hw = clk_hw_register_fixed_rate(dev, sclk->name,
  175. sclk->parent_name, 0,
  176. sclk->rate);
  177. if (IS_ERR(sclk->hw)) {
  178. dev_err(dev, "Couldn't register clock %s\n", sclk->name);
  179. ret = PTR_ERR(sclk->hw);
  180. goto err_clk_unreg;
  181. }
  182. hws[sclk->idx] = sclk->hw;
  183. }
  184. return 0;
  185. err_clk_unreg:
  186. while (--i >= 0) {
  187. sclk = &mt7621_fixed_clks[i];
  188. clk_hw_unregister_fixed_rate(sclk->hw);
  189. }
  190. return ret;
  191. }
  192. static inline struct mt7621_clk *to_mt7621_clk(struct clk_hw *hw)
  193. {
  194. return container_of(hw, struct mt7621_clk, hw);
  195. }
  196. static unsigned long mt7621_xtal_recalc_rate(struct clk_hw *hw,
  197. unsigned long parent_rate)
  198. {
  199. struct mt7621_clk *clk = to_mt7621_clk(hw);
  200. struct regmap *sysc = clk->priv->sysc;
  201. u32 val;
  202. regmap_read(sysc, SYSC_REG_SYSTEM_CONFIG0, &val);
  203. val = FIELD_GET(XTAL_MODE_SEL_MASK, val);
  204. if (val <= 2)
  205. return 20000000;
  206. if (val <= 5)
  207. return 40000000;
  208. return 25000000;
  209. }
  210. static unsigned long mt7621_cpu_recalc_rate(struct clk_hw *hw,
  211. unsigned long xtal_clk)
  212. {
  213. static const u32 prediv_tbl[] = { 0, 1, 2, 2 };
  214. struct mt7621_clk *clk = to_mt7621_clk(hw);
  215. struct regmap *sysc = clk->priv->sysc;
  216. struct regmap *memc = clk->priv->memc;
  217. u32 clkcfg, clk_sel, curclk, ffiv, ffrac;
  218. u32 pll, prediv, fbdiv;
  219. unsigned long cpu_clk;
  220. regmap_read(sysc, SYSC_REG_CLKCFG0, &clkcfg);
  221. clk_sel = FIELD_GET(CPU_CLK_SEL_MASK, clkcfg);
  222. regmap_read(sysc, SYSC_REG_CUR_CLK_STS, &curclk);
  223. ffiv = FIELD_GET(CUR_CPU_FDIV_MASK, curclk);
  224. ffrac = FIELD_GET(CUR_CPU_FFRAC_MASK, curclk);
  225. switch (clk_sel) {
  226. case 0:
  227. cpu_clk = 500000000;
  228. break;
  229. case 1:
  230. regmap_read(memc, MEMC_REG_CPU_PLL, &pll);
  231. fbdiv = FIELD_GET(CPU_PLL_FBDIV_MASK, pll);
  232. prediv = FIELD_GET(CPU_PLL_PREDIV_MASK, pll);
  233. cpu_clk = ((fbdiv + 1) * xtal_clk) >> prediv_tbl[prediv];
  234. break;
  235. default:
  236. cpu_clk = xtal_clk;
  237. }
  238. return cpu_clk / ffiv * ffrac;
  239. }
  240. static unsigned long mt7621_bus_recalc_rate(struct clk_hw *hw,
  241. unsigned long parent_rate)
  242. {
  243. return parent_rate / 4;
  244. }
  245. #define CLK_BASE(_name, _parent, _recalc) { \
  246. .init = &(struct clk_init_data) { \
  247. .name = _name, \
  248. .ops = &(const struct clk_ops) { \
  249. .recalc_rate = _recalc, \
  250. }, \
  251. .parent_data = &(const struct clk_parent_data) { \
  252. .name = _parent, \
  253. .fw_name = _parent \
  254. }, \
  255. .num_parents = _parent ? 1 : 0 \
  256. }, \
  257. }
  258. static struct mt7621_clk mt7621_clks_base[] = {
  259. { CLK_BASE("xtal", NULL, mt7621_xtal_recalc_rate) },
  260. { CLK_BASE("cpu", "xtal", mt7621_cpu_recalc_rate) },
  261. { CLK_BASE("bus", "cpu", mt7621_bus_recalc_rate) },
  262. };
  263. static struct clk_hw *mt7621_clk_early[MT7621_CLK_MAX];
  264. static int mt7621_register_early_clocks(struct device_node *np,
  265. struct clk_hw_onecell_data *clk_data,
  266. struct mt7621_clk_priv *priv)
  267. {
  268. struct clk_hw **hws = clk_data->hws;
  269. struct mt7621_clk *sclk;
  270. int ret, i, j;
  271. for (i = 0; i < ARRAY_SIZE(mt7621_clks_base); i++) {
  272. sclk = &mt7621_clks_base[i];
  273. sclk->priv = priv;
  274. ret = of_clk_hw_register(np, &sclk->hw);
  275. if (ret) {
  276. pr_err("Couldn't register top clock %i\n", i);
  277. goto err_clk_unreg;
  278. }
  279. hws[i] = &sclk->hw;
  280. mt7621_clk_early[i] = &sclk->hw;
  281. }
  282. for (j = i; j < MT7621_CLK_MAX; j++)
  283. mt7621_clk_early[j] = ERR_PTR(-EPROBE_DEFER);
  284. return 0;
  285. err_clk_unreg:
  286. while (--i >= 0) {
  287. sclk = &mt7621_clks_base[i];
  288. clk_hw_unregister(&sclk->hw);
  289. }
  290. return ret;
  291. }
  292. static void __init mt7621_clk_init(struct device_node *node)
  293. {
  294. struct mt7621_clk_priv *priv;
  295. struct clk_hw_onecell_data *clk_data;
  296. int ret, i, count;
  297. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  298. if (!priv)
  299. return;
  300. priv->sysc = syscon_node_to_regmap(node);
  301. if (IS_ERR(priv->sysc)) {
  302. pr_err("Could not get sysc syscon regmap\n");
  303. goto free_clk_priv;
  304. }
  305. priv->memc = syscon_regmap_lookup_by_phandle(node, "ralink,memctl");
  306. if (IS_ERR(priv->memc)) {
  307. pr_err("Could not get memc syscon regmap\n");
  308. goto free_clk_priv;
  309. }
  310. count = ARRAY_SIZE(mt7621_clks_base) +
  311. ARRAY_SIZE(mt7621_fixed_clks) + ARRAY_SIZE(mt7621_gates);
  312. clk_data = kzalloc(struct_size(clk_data, hws, count), GFP_KERNEL);
  313. if (!clk_data)
  314. goto free_clk_priv;
  315. ret = mt7621_register_early_clocks(node, clk_data, priv);
  316. if (ret) {
  317. pr_err("Couldn't register top clocks\n");
  318. goto free_clk_data;
  319. }
  320. clk_data->num = count;
  321. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  322. if (ret) {
  323. pr_err("Couldn't add clk hw provider\n");
  324. goto unreg_clk_top;
  325. }
  326. return;
  327. unreg_clk_top:
  328. for (i = 0; i < ARRAY_SIZE(mt7621_clks_base); i++) {
  329. struct mt7621_clk *sclk = &mt7621_clks_base[i];
  330. clk_hw_unregister(&sclk->hw);
  331. }
  332. free_clk_data:
  333. kfree(clk_data);
  334. free_clk_priv:
  335. kfree(priv);
  336. }
  337. CLK_OF_DECLARE_DRIVER(mt7621_clk, "mediatek,mt7621-sysc", mt7621_clk_init);
  338. struct mt7621_rst {
  339. struct reset_controller_dev rcdev;
  340. struct regmap *sysc;
  341. };
  342. static struct mt7621_rst *to_mt7621_rst(struct reset_controller_dev *dev)
  343. {
  344. return container_of(dev, struct mt7621_rst, rcdev);
  345. }
  346. static int mt7621_assert_device(struct reset_controller_dev *rcdev,
  347. unsigned long id)
  348. {
  349. struct mt7621_rst *data = to_mt7621_rst(rcdev);
  350. struct regmap *sysc = data->sysc;
  351. return regmap_update_bits(sysc, SYSC_REG_RESET_CTRL, BIT(id), BIT(id));
  352. }
  353. static int mt7621_deassert_device(struct reset_controller_dev *rcdev,
  354. unsigned long id)
  355. {
  356. struct mt7621_rst *data = to_mt7621_rst(rcdev);
  357. struct regmap *sysc = data->sysc;
  358. return regmap_update_bits(sysc, SYSC_REG_RESET_CTRL, BIT(id), 0);
  359. }
  360. static int mt7621_reset_device(struct reset_controller_dev *rcdev,
  361. unsigned long id)
  362. {
  363. int ret;
  364. ret = mt7621_assert_device(rcdev, id);
  365. if (ret < 0)
  366. return ret;
  367. return mt7621_deassert_device(rcdev, id);
  368. }
  369. static int mt7621_rst_xlate(struct reset_controller_dev *rcdev,
  370. const struct of_phandle_args *reset_spec)
  371. {
  372. unsigned long id = reset_spec->args[0];
  373. if (id == MT7621_RST_SYS || id >= rcdev->nr_resets)
  374. return -EINVAL;
  375. return id;
  376. }
  377. static const struct reset_control_ops reset_ops = {
  378. .reset = mt7621_reset_device,
  379. .assert = mt7621_assert_device,
  380. .deassert = mt7621_deassert_device
  381. };
  382. static int mt7621_reset_init(struct device *dev, struct regmap *sysc)
  383. {
  384. struct mt7621_rst *rst_data;
  385. rst_data = devm_kzalloc(dev, sizeof(*rst_data), GFP_KERNEL);
  386. if (!rst_data)
  387. return -ENOMEM;
  388. rst_data->sysc = sysc;
  389. rst_data->rcdev.ops = &reset_ops;
  390. rst_data->rcdev.owner = THIS_MODULE;
  391. rst_data->rcdev.nr_resets = 32;
  392. rst_data->rcdev.of_reset_n_cells = 1;
  393. rst_data->rcdev.of_xlate = mt7621_rst_xlate;
  394. rst_data->rcdev.of_node = dev_of_node(dev);
  395. return devm_reset_controller_register(dev, &rst_data->rcdev);
  396. }
  397. static int mt7621_clk_probe(struct platform_device *pdev)
  398. {
  399. struct device_node *np = pdev->dev.of_node;
  400. struct clk_hw_onecell_data *clk_data;
  401. struct device *dev = &pdev->dev;
  402. struct mt7621_clk_priv *priv;
  403. int ret, i, count;
  404. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  405. if (!priv)
  406. return -ENOMEM;
  407. priv->sysc = syscon_node_to_regmap(np);
  408. if (IS_ERR(priv->sysc)) {
  409. ret = PTR_ERR(priv->sysc);
  410. dev_err(dev, "Could not get sysc syscon regmap\n");
  411. return ret;
  412. }
  413. priv->memc = syscon_regmap_lookup_by_phandle(np, "ralink,memctl");
  414. if (IS_ERR(priv->memc)) {
  415. ret = PTR_ERR(priv->memc);
  416. dev_err(dev, "Could not get memc syscon regmap\n");
  417. return ret;
  418. }
  419. ret = mt7621_reset_init(dev, priv->sysc);
  420. if (ret) {
  421. dev_err(dev, "Could not init reset controller\n");
  422. return ret;
  423. }
  424. count = ARRAY_SIZE(mt7621_clks_base) +
  425. ARRAY_SIZE(mt7621_fixed_clks) + ARRAY_SIZE(mt7621_gates);
  426. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
  427. GFP_KERNEL);
  428. if (!clk_data)
  429. return -ENOMEM;
  430. for (i = 0; i < ARRAY_SIZE(mt7621_clks_base); i++)
  431. clk_data->hws[i] = mt7621_clk_early[i];
  432. ret = mt7621_register_fixed_clocks(dev, clk_data);
  433. if (ret) {
  434. dev_err(dev, "Couldn't register fixed clocks\n");
  435. return ret;
  436. }
  437. ret = mt7621_register_gates(dev, clk_data, priv);
  438. if (ret) {
  439. dev_err(dev, "Couldn't register fixed clock gates\n");
  440. goto unreg_clk_fixed;
  441. }
  442. clk_data->num = count;
  443. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
  444. if (ret) {
  445. dev_err(dev, "Couldn't add clk hw provider\n");
  446. goto unreg_clk_gates;
  447. }
  448. return 0;
  449. unreg_clk_gates:
  450. for (i = 0; i < ARRAY_SIZE(mt7621_gates); i++) {
  451. struct mt7621_gate *sclk = &mt7621_gates[i];
  452. clk_hw_unregister(&sclk->hw);
  453. }
  454. unreg_clk_fixed:
  455. for (i = 0; i < ARRAY_SIZE(mt7621_fixed_clks); i++) {
  456. struct mt7621_fixed_clk *sclk = &mt7621_fixed_clks[i];
  457. clk_hw_unregister_fixed_rate(sclk->hw);
  458. }
  459. return ret;
  460. }
  461. static const struct of_device_id mt7621_clk_of_match[] = {
  462. { .compatible = "mediatek,mt7621-sysc" },
  463. {}
  464. };
  465. static struct platform_driver mt7621_clk_driver = {
  466. .probe = mt7621_clk_probe,
  467. .driver = {
  468. .name = "mt7621-clk",
  469. .of_match_table = mt7621_clk_of_match,
  470. },
  471. };
  472. static int __init mt7621_clk_reset_init(void)
  473. {
  474. return platform_driver_register(&mt7621_clk_driver);
  475. }
  476. arch_initcall(mt7621_clk_reset_init);