clk-aspeed.c 19 KB

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