clk-aspeed.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #define pr_fmt(fmt) "clk-aspeed: " fmt
  3. #include <linux/clk-provider.h>
  4. #include <linux/mfd/syscon.h>
  5. #include <linux/of_address.h>
  6. #include <linux/of_device.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/regmap.h>
  9. #include <linux/reset-controller.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <dt-bindings/clock/aspeed-clock.h>
  13. #define ASPEED_NUM_CLKS 35
  14. #define ASPEED_RESET_CTRL 0x04
  15. #define ASPEED_CLK_SELECTION 0x08
  16. #define ASPEED_CLK_STOP_CTRL 0x0c
  17. #define ASPEED_MPLL_PARAM 0x20
  18. #define ASPEED_HPLL_PARAM 0x24
  19. #define AST2500_HPLL_BYPASS_EN BIT(20)
  20. #define AST2400_HPLL_STRAPPED BIT(18)
  21. #define AST2400_HPLL_BYPASS_EN BIT(17)
  22. #define ASPEED_MISC_CTRL 0x2c
  23. #define UART_DIV13_EN BIT(12)
  24. #define ASPEED_STRAP 0x70
  25. #define CLKIN_25MHZ_EN BIT(23)
  26. #define AST2400_CLK_SOURCE_SEL BIT(18)
  27. #define ASPEED_CLK_SELECTION_2 0xd8
  28. /* Globally visible clocks */
  29. static DEFINE_SPINLOCK(aspeed_clk_lock);
  30. /* Keeps track of all clocks */
  31. static struct clk_hw_onecell_data *aspeed_clk_data;
  32. static void __iomem *scu_base;
  33. /**
  34. * struct aspeed_gate_data - Aspeed gated clocks
  35. * @clock_idx: bit used to gate this clock in the clock register
  36. * @reset_idx: bit used to reset this IP in the reset register. -1 if no
  37. * reset is required when enabling the clock
  38. * @name: the clock name
  39. * @parent_name: the name of the parent clock
  40. * @flags: standard clock framework flags
  41. */
  42. struct aspeed_gate_data {
  43. u8 clock_idx;
  44. s8 reset_idx;
  45. const char *name;
  46. const char *parent_name;
  47. unsigned long flags;
  48. };
  49. /**
  50. * struct aspeed_clk_gate - Aspeed specific clk_gate structure
  51. * @hw: handle between common and hardware-specific interfaces
  52. * @reg: register controlling gate
  53. * @clock_idx: bit used to gate this clock in the clock register
  54. * @reset_idx: bit used to reset this IP in the reset register. -1 if no
  55. * reset is required when enabling the clock
  56. * @flags: hardware-specific flags
  57. * @lock: register lock
  58. *
  59. * Some of the clocks in the Aspeed SoC must be put in reset before enabling.
  60. * This modified version of clk_gate allows an optional reset bit to be
  61. * specified.
  62. */
  63. struct aspeed_clk_gate {
  64. struct clk_hw hw;
  65. struct regmap *map;
  66. u8 clock_idx;
  67. s8 reset_idx;
  68. u8 flags;
  69. spinlock_t *lock;
  70. };
  71. #define to_aspeed_clk_gate(_hw) container_of(_hw, struct aspeed_clk_gate, hw)
  72. /* TODO: ask Aspeed about the actual parent data */
  73. static const struct aspeed_gate_data aspeed_gates[] = {
  74. /* clk rst name parent flags */
  75. [ASPEED_CLK_GATE_ECLK] = { 0, -1, "eclk-gate", "eclk", 0 }, /* Video Engine */
  76. [ASPEED_CLK_GATE_GCLK] = { 1, 7, "gclk-gate", NULL, 0 }, /* 2D engine */
  77. [ASPEED_CLK_GATE_MCLK] = { 2, -1, "mclk-gate", "mpll", CLK_IS_CRITICAL }, /* SDRAM */
  78. [ASPEED_CLK_GATE_VCLK] = { 3, 6, "vclk-gate", NULL, 0 }, /* Video Capture */
  79. [ASPEED_CLK_GATE_BCLK] = { 4, 10, "bclk-gate", "bclk", 0 }, /* PCIe/PCI */
  80. [ASPEED_CLK_GATE_DCLK] = { 5, -1, "dclk-gate", NULL, 0 }, /* DAC */
  81. [ASPEED_CLK_GATE_REFCLK] = { 6, -1, "refclk-gate", "clkin", CLK_IS_CRITICAL },
  82. [ASPEED_CLK_GATE_USBPORT2CLK] = { 7, 3, "usb-port2-gate", NULL, 0 }, /* USB2.0 Host port 2 */
  83. [ASPEED_CLK_GATE_LCLK] = { 8, 5, "lclk-gate", NULL, 0 }, /* LPC */
  84. [ASPEED_CLK_GATE_USBUHCICLK] = { 9, 15, "usb-uhci-gate", NULL, 0 }, /* USB1.1 (requires port 2 enabled) */
  85. [ASPEED_CLK_GATE_D1CLK] = { 10, 13, "d1clk-gate", NULL, 0 }, /* GFX CRT */
  86. [ASPEED_CLK_GATE_YCLK] = { 13, 4, "yclk-gate", NULL, 0 }, /* HAC */
  87. [ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate", NULL, 0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */
  88. [ASPEED_CLK_GATE_UART1CLK] = { 15, -1, "uart1clk-gate", "uart", 0 }, /* UART1 */
  89. [ASPEED_CLK_GATE_UART2CLK] = { 16, -1, "uart2clk-gate", "uart", 0 }, /* UART2 */
  90. [ASPEED_CLK_GATE_UART5CLK] = { 17, -1, "uart5clk-gate", "uart", 0 }, /* UART5 */
  91. [ASPEED_CLK_GATE_ESPICLK] = { 19, -1, "espiclk-gate", NULL, 0 }, /* eSPI */
  92. [ASPEED_CLK_GATE_MAC1CLK] = { 20, 11, "mac1clk-gate", "mac", 0 }, /* MAC1 */
  93. [ASPEED_CLK_GATE_MAC2CLK] = { 21, 12, "mac2clk-gate", "mac", 0 }, /* MAC2 */
  94. [ASPEED_CLK_GATE_RSACLK] = { 24, -1, "rsaclk-gate", NULL, 0 }, /* RSA */
  95. [ASPEED_CLK_GATE_UART3CLK] = { 25, -1, "uart3clk-gate", "uart", 0 }, /* UART3 */
  96. [ASPEED_CLK_GATE_UART4CLK] = { 26, -1, "uart4clk-gate", "uart", 0 }, /* UART4 */
  97. [ASPEED_CLK_GATE_SDCLKCLK] = { 27, 16, "sdclk-gate", NULL, 0 }, /* SDIO/SD */
  98. [ASPEED_CLK_GATE_LHCCLK] = { 28, -1, "lhclk-gate", "lhclk", 0 }, /* LPC master/LPC+ */
  99. };
  100. static const struct clk_div_table ast2500_mac_div_table[] = {
  101. { 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */
  102. { 0x1, 4 },
  103. { 0x2, 6 },
  104. { 0x3, 8 },
  105. { 0x4, 10 },
  106. { 0x5, 12 },
  107. { 0x6, 14 },
  108. { 0x7, 16 },
  109. { 0 }
  110. };
  111. static const struct clk_div_table ast2400_div_table[] = {
  112. { 0x0, 2 },
  113. { 0x1, 4 },
  114. { 0x2, 6 },
  115. { 0x3, 8 },
  116. { 0x4, 10 },
  117. { 0x5, 12 },
  118. { 0x6, 14 },
  119. { 0x7, 16 },
  120. { 0 }
  121. };
  122. static const struct clk_div_table ast2500_div_table[] = {
  123. { 0x0, 4 },
  124. { 0x1, 8 },
  125. { 0x2, 12 },
  126. { 0x3, 16 },
  127. { 0x4, 20 },
  128. { 0x5, 24 },
  129. { 0x6, 28 },
  130. { 0x7, 32 },
  131. { 0 }
  132. };
  133. static struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val)
  134. {
  135. unsigned int mult, div;
  136. if (val & AST2400_HPLL_BYPASS_EN) {
  137. /* Pass through mode */
  138. mult = div = 1;
  139. } else {
  140. /* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */
  141. u32 n = (val >> 5) & 0x3f;
  142. u32 od = (val >> 4) & 0x1;
  143. u32 d = val & 0xf;
  144. mult = (2 - od) * (n + 2);
  145. div = d + 1;
  146. }
  147. return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
  148. mult, div);
  149. };
  150. static struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val)
  151. {
  152. unsigned int mult, div;
  153. if (val & AST2500_HPLL_BYPASS_EN) {
  154. /* Pass through mode */
  155. mult = div = 1;
  156. } else {
  157. /* F = clkin * [(M+1) / (N+1)] / (P + 1) */
  158. u32 p = (val >> 13) & 0x3f;
  159. u32 m = (val >> 5) & 0xff;
  160. u32 n = val & 0x1f;
  161. mult = (m + 1) / (n + 1);
  162. div = p + 1;
  163. }
  164. return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
  165. mult, div);
  166. }
  167. struct aspeed_clk_soc_data {
  168. const struct clk_div_table *div_table;
  169. const struct clk_div_table *mac_div_table;
  170. struct clk_hw *(*calc_pll)(const char *name, u32 val);
  171. };
  172. static const struct aspeed_clk_soc_data ast2500_data = {
  173. .div_table = ast2500_div_table,
  174. .mac_div_table = ast2500_mac_div_table,
  175. .calc_pll = aspeed_ast2500_calc_pll,
  176. };
  177. static const struct aspeed_clk_soc_data ast2400_data = {
  178. .div_table = ast2400_div_table,
  179. .mac_div_table = ast2400_div_table,
  180. .calc_pll = aspeed_ast2400_calc_pll,
  181. };
  182. static int aspeed_clk_is_enabled(struct clk_hw *hw)
  183. {
  184. struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
  185. u32 clk = BIT(gate->clock_idx);
  186. u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
  187. u32 reg;
  188. regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
  189. return ((reg & clk) == enval) ? 1 : 0;
  190. }
  191. static int aspeed_clk_enable(struct clk_hw *hw)
  192. {
  193. struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
  194. unsigned long flags;
  195. u32 clk = BIT(gate->clock_idx);
  196. u32 rst = BIT(gate->reset_idx);
  197. u32 enval;
  198. spin_lock_irqsave(gate->lock, flags);
  199. if (aspeed_clk_is_enabled(hw)) {
  200. spin_unlock_irqrestore(gate->lock, flags);
  201. return 0;
  202. }
  203. if (gate->reset_idx >= 0) {
  204. /* Put IP in reset */
  205. regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
  206. /* Delay 100us */
  207. udelay(100);
  208. }
  209. /* Enable clock */
  210. enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
  211. regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
  212. if (gate->reset_idx >= 0) {
  213. /* A delay of 10ms is specified by the ASPEED docs */
  214. mdelay(10);
  215. /* Take IP out of reset */
  216. regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
  217. }
  218. spin_unlock_irqrestore(gate->lock, flags);
  219. return 0;
  220. }
  221. static void aspeed_clk_disable(struct clk_hw *hw)
  222. {
  223. struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
  224. unsigned long flags;
  225. u32 clk = BIT(gate->clock_idx);
  226. u32 enval;
  227. spin_lock_irqsave(gate->lock, flags);
  228. enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? clk : 0;
  229. regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
  230. spin_unlock_irqrestore(gate->lock, flags);
  231. }
  232. static const struct clk_ops aspeed_clk_gate_ops = {
  233. .enable = aspeed_clk_enable,
  234. .disable = aspeed_clk_disable,
  235. .is_enabled = aspeed_clk_is_enabled,
  236. };
  237. /**
  238. * struct aspeed_reset - Aspeed reset controller
  239. * @map: regmap to access the containing system controller
  240. * @rcdev: reset controller device
  241. */
  242. struct aspeed_reset {
  243. struct regmap *map;
  244. struct reset_controller_dev rcdev;
  245. };
  246. #define to_aspeed_reset(p) container_of((p), struct aspeed_reset, rcdev)
  247. static const u8 aspeed_resets[] = {
  248. [ASPEED_RESET_XDMA] = 25,
  249. [ASPEED_RESET_MCTP] = 24,
  250. [ASPEED_RESET_ADC] = 23,
  251. [ASPEED_RESET_JTAG_MASTER] = 22,
  252. [ASPEED_RESET_MIC] = 18,
  253. [ASPEED_RESET_PWM] = 9,
  254. [ASPEED_RESET_PCIVGA] = 8,
  255. [ASPEED_RESET_I2C] = 2,
  256. [ASPEED_RESET_AHB] = 1,
  257. };
  258. static int aspeed_reset_deassert(struct reset_controller_dev *rcdev,
  259. unsigned long id)
  260. {
  261. struct aspeed_reset *ar = to_aspeed_reset(rcdev);
  262. u32 rst = BIT(aspeed_resets[id]);
  263. return regmap_update_bits(ar->map, ASPEED_RESET_CTRL, rst, 0);
  264. }
  265. static int aspeed_reset_assert(struct reset_controller_dev *rcdev,
  266. unsigned long id)
  267. {
  268. struct aspeed_reset *ar = to_aspeed_reset(rcdev);
  269. u32 rst = BIT(aspeed_resets[id]);
  270. return regmap_update_bits(ar->map, ASPEED_RESET_CTRL, rst, rst);
  271. }
  272. static int aspeed_reset_status(struct reset_controller_dev *rcdev,
  273. unsigned long id)
  274. {
  275. struct aspeed_reset *ar = to_aspeed_reset(rcdev);
  276. u32 val, rst = BIT(aspeed_resets[id]);
  277. int ret;
  278. ret = regmap_read(ar->map, ASPEED_RESET_CTRL, &val);
  279. if (ret)
  280. return ret;
  281. return !!(val & rst);
  282. }
  283. static const struct reset_control_ops aspeed_reset_ops = {
  284. .assert = aspeed_reset_assert,
  285. .deassert = aspeed_reset_deassert,
  286. .status = aspeed_reset_status,
  287. };
  288. static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
  289. const char *name, const char *parent_name, unsigned long flags,
  290. struct regmap *map, u8 clock_idx, u8 reset_idx,
  291. u8 clk_gate_flags, spinlock_t *lock)
  292. {
  293. struct aspeed_clk_gate *gate;
  294. struct clk_init_data init;
  295. struct clk_hw *hw;
  296. int ret;
  297. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  298. if (!gate)
  299. return ERR_PTR(-ENOMEM);
  300. init.name = name;
  301. init.ops = &aspeed_clk_gate_ops;
  302. init.flags = flags;
  303. init.parent_names = parent_name ? &parent_name : NULL;
  304. init.num_parents = parent_name ? 1 : 0;
  305. gate->map = map;
  306. gate->clock_idx = clock_idx;
  307. gate->reset_idx = reset_idx;
  308. gate->flags = clk_gate_flags;
  309. gate->lock = lock;
  310. gate->hw.init = &init;
  311. hw = &gate->hw;
  312. ret = clk_hw_register(dev, hw);
  313. if (ret) {
  314. kfree(gate);
  315. hw = ERR_PTR(ret);
  316. }
  317. return hw;
  318. }
  319. static int aspeed_clk_probe(struct platform_device *pdev)
  320. {
  321. const struct aspeed_clk_soc_data *soc_data;
  322. struct device *dev = &pdev->dev;
  323. struct aspeed_reset *ar;
  324. struct regmap *map;
  325. struct clk_hw *hw;
  326. u32 val, rate;
  327. int i, ret;
  328. map = syscon_node_to_regmap(dev->of_node);
  329. if (IS_ERR(map)) {
  330. dev_err(dev, "no syscon regmap\n");
  331. return PTR_ERR(map);
  332. }
  333. ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
  334. if (!ar)
  335. return -ENOMEM;
  336. ar->map = map;
  337. ar->rcdev.owner = THIS_MODULE;
  338. ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
  339. ar->rcdev.ops = &aspeed_reset_ops;
  340. ar->rcdev.of_node = dev->of_node;
  341. ret = devm_reset_controller_register(dev, &ar->rcdev);
  342. if (ret) {
  343. dev_err(dev, "could not register reset controller\n");
  344. return ret;
  345. }
  346. /* SoC generations share common layouts but have different divisors */
  347. soc_data = of_device_get_match_data(dev);
  348. if (!soc_data) {
  349. dev_err(dev, "no match data for platform\n");
  350. return -EINVAL;
  351. }
  352. /* UART clock div13 setting */
  353. regmap_read(map, ASPEED_MISC_CTRL, &val);
  354. if (val & UART_DIV13_EN)
  355. rate = 24000000 / 13;
  356. else
  357. rate = 24000000;
  358. /* TODO: Find the parent data for the uart clock */
  359. hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
  360. if (IS_ERR(hw))
  361. return PTR_ERR(hw);
  362. aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
  363. /*
  364. * Memory controller (M-PLL) PLL. This clock is configured by the
  365. * bootloader, and is exposed to Linux as a read-only clock rate.
  366. */
  367. regmap_read(map, ASPEED_MPLL_PARAM, &val);
  368. hw = soc_data->calc_pll("mpll", val);
  369. if (IS_ERR(hw))
  370. return PTR_ERR(hw);
  371. aspeed_clk_data->hws[ASPEED_CLK_MPLL] = hw;
  372. /* SD/SDIO clock divider (TODO: There's a gate too) */
  373. hw = clk_hw_register_divider_table(dev, "sdio", "hpll", 0,
  374. scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
  375. soc_data->div_table,
  376. &aspeed_clk_lock);
  377. if (IS_ERR(hw))
  378. return PTR_ERR(hw);
  379. aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
  380. /* MAC AHB bus clock divider */
  381. hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
  382. scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
  383. soc_data->mac_div_table,
  384. &aspeed_clk_lock);
  385. if (IS_ERR(hw))
  386. return PTR_ERR(hw);
  387. aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
  388. /* LPC Host (LHCLK) clock divider */
  389. hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
  390. scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
  391. soc_data->div_table,
  392. &aspeed_clk_lock);
  393. if (IS_ERR(hw))
  394. return PTR_ERR(hw);
  395. aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
  396. /* P-Bus (BCLK) clock divider */
  397. hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
  398. scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
  399. soc_data->div_table,
  400. &aspeed_clk_lock);
  401. if (IS_ERR(hw))
  402. return PTR_ERR(hw);
  403. aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
  404. /*
  405. * TODO: There are a number of clocks that not included in this driver
  406. * as more information is required:
  407. * D2-PLL
  408. * D-PLL
  409. * YCLK
  410. * RGMII
  411. * RMII
  412. * UART[1..5] clock source mux
  413. * Video Engine (ECLK) mux and clock divider
  414. */
  415. for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
  416. const struct aspeed_gate_data *gd = &aspeed_gates[i];
  417. u32 gate_flags;
  418. /* Special case: the USB port 1 clock (bit 14) is always
  419. * working the opposite way from the other ones.
  420. */
  421. gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
  422. hw = aspeed_clk_hw_register_gate(dev,
  423. gd->name,
  424. gd->parent_name,
  425. gd->flags,
  426. map,
  427. gd->clock_idx,
  428. gd->reset_idx,
  429. gate_flags,
  430. &aspeed_clk_lock);
  431. if (IS_ERR(hw))
  432. return PTR_ERR(hw);
  433. aspeed_clk_data->hws[i] = hw;
  434. }
  435. return 0;
  436. };
  437. static const struct of_device_id aspeed_clk_dt_ids[] = {
  438. { .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
  439. { .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
  440. { }
  441. };
  442. static struct platform_driver aspeed_clk_driver = {
  443. .probe = aspeed_clk_probe,
  444. .driver = {
  445. .name = "aspeed-clk",
  446. .of_match_table = aspeed_clk_dt_ids,
  447. .suppress_bind_attrs = true,
  448. },
  449. };
  450. builtin_platform_driver(aspeed_clk_driver);
  451. static void __init aspeed_ast2400_cc(struct regmap *map)
  452. {
  453. struct clk_hw *hw;
  454. u32 val, freq, div;
  455. /*
  456. * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
  457. * strapping
  458. */
  459. regmap_read(map, ASPEED_STRAP, &val);
  460. if (val & CLKIN_25MHZ_EN)
  461. freq = 25000000;
  462. else if (val & AST2400_CLK_SOURCE_SEL)
  463. freq = 48000000;
  464. else
  465. freq = 24000000;
  466. hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
  467. pr_debug("clkin @%u MHz\n", freq / 1000000);
  468. /*
  469. * High-speed PLL clock derived from the crystal. This the CPU clock,
  470. * and we assume that it is enabled
  471. */
  472. regmap_read(map, ASPEED_HPLL_PARAM, &val);
  473. WARN(val & AST2400_HPLL_STRAPPED, "hpll is strapped not configured");
  474. aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2400_calc_pll("hpll", val);
  475. /*
  476. * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
  477. * 00: Select CPU:AHB = 1:1
  478. * 01: Select CPU:AHB = 2:1
  479. * 10: Select CPU:AHB = 4:1
  480. * 11: Select CPU:AHB = 3:1
  481. */
  482. regmap_read(map, ASPEED_STRAP, &val);
  483. val = (val >> 10) & 0x3;
  484. div = val + 1;
  485. if (div == 3)
  486. div = 4;
  487. else if (div == 4)
  488. div = 3;
  489. hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
  490. aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
  491. /* APB clock clock selection register SCU08 (aka PCLK) */
  492. hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
  493. scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
  494. ast2400_div_table,
  495. &aspeed_clk_lock);
  496. aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
  497. }
  498. static void __init aspeed_ast2500_cc(struct regmap *map)
  499. {
  500. struct clk_hw *hw;
  501. u32 val, freq, div;
  502. /* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
  503. regmap_read(map, ASPEED_STRAP, &val);
  504. if (val & CLKIN_25MHZ_EN)
  505. freq = 25000000;
  506. else
  507. freq = 24000000;
  508. hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
  509. pr_debug("clkin @%u MHz\n", freq / 1000000);
  510. /*
  511. * High-speed PLL clock derived from the crystal. This the CPU clock,
  512. * and we assume that it is enabled
  513. */
  514. regmap_read(map, ASPEED_HPLL_PARAM, &val);
  515. aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
  516. /* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
  517. regmap_read(map, ASPEED_STRAP, &val);
  518. val = (val >> 9) & 0x7;
  519. WARN(val == 0, "strapping is zero: cannot determine ahb clock");
  520. div = 2 * (val + 1);
  521. hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
  522. aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
  523. /* APB clock clock selection register SCU08 (aka PCLK) */
  524. regmap_read(map, ASPEED_CLK_SELECTION, &val);
  525. val = (val >> 23) & 0x7;
  526. div = 4 * (val + 1);
  527. hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
  528. aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
  529. };
  530. static void __init aspeed_cc_init(struct device_node *np)
  531. {
  532. struct regmap *map;
  533. u32 val;
  534. int ret;
  535. int i;
  536. scu_base = of_iomap(np, 0);
  537. if (!scu_base)
  538. return;
  539. aspeed_clk_data = kzalloc(sizeof(*aspeed_clk_data) +
  540. sizeof(*aspeed_clk_data->hws) * ASPEED_NUM_CLKS,
  541. GFP_KERNEL);
  542. if (!aspeed_clk_data)
  543. return;
  544. /*
  545. * This way all clocks fetched before the platform device probes,
  546. * except those we assign here for early use, will be deferred.
  547. */
  548. for (i = 0; i < ASPEED_NUM_CLKS; i++)
  549. aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
  550. map = syscon_node_to_regmap(np);
  551. if (IS_ERR(map)) {
  552. pr_err("no syscon regmap\n");
  553. return;
  554. }
  555. /*
  556. * We check that the regmap works on this very first access,
  557. * but as this is an MMIO-backed regmap, subsequent regmap
  558. * access is not going to fail and we skip error checks from
  559. * this point.
  560. */
  561. ret = regmap_read(map, ASPEED_STRAP, &val);
  562. if (ret) {
  563. pr_err("failed to read strapping register\n");
  564. return;
  565. }
  566. if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
  567. aspeed_ast2400_cc(map);
  568. else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
  569. aspeed_ast2500_cc(map);
  570. else
  571. pr_err("unknown platform, failed to add clocks\n");
  572. aspeed_clk_data->num = ASPEED_NUM_CLKS;
  573. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
  574. if (ret)
  575. pr_err("failed to add DT provider: %d\n", ret);
  576. };
  577. CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
  578. CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);