clk-aspeed.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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_PROGRAMMED 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", CLK_IS_CRITICAL }, /* PCIe/PCI */
  82. [ASPEED_CLK_GATE_DCLK] = { 5, -1, "dclk-gate", NULL, CLK_IS_CRITICAL }, /* 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_SDCLK] = { 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 rst = BIT(gate->reset_idx);
  189. u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
  190. u32 reg;
  191. /*
  192. * If the IP is in reset, treat the clock as not enabled,
  193. * this happens with some clocks such as the USB one when
  194. * coming from cold reset. Without this, aspeed_clk_enable()
  195. * will fail to lift the reset.
  196. */
  197. if (gate->reset_idx >= 0) {
  198. regmap_read(gate->map, ASPEED_RESET_CTRL, &reg);
  199. if (reg & rst)
  200. return 0;
  201. }
  202. regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, &reg);
  203. return ((reg & clk) == enval) ? 1 : 0;
  204. }
  205. static int aspeed_clk_enable(struct clk_hw *hw)
  206. {
  207. struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
  208. unsigned long flags;
  209. u32 clk = BIT(gate->clock_idx);
  210. u32 rst = BIT(gate->reset_idx);
  211. u32 enval;
  212. spin_lock_irqsave(gate->lock, flags);
  213. if (aspeed_clk_is_enabled(hw)) {
  214. spin_unlock_irqrestore(gate->lock, flags);
  215. return 0;
  216. }
  217. if (gate->reset_idx >= 0) {
  218. /* Put IP in reset */
  219. regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);
  220. /* Delay 100us */
  221. udelay(100);
  222. }
  223. /* Enable clock */
  224. enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
  225. regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
  226. if (gate->reset_idx >= 0) {
  227. /* A delay of 10ms is specified by the ASPEED docs */
  228. mdelay(10);
  229. /* Take IP out of reset */
  230. regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0);
  231. }
  232. spin_unlock_irqrestore(gate->lock, flags);
  233. return 0;
  234. }
  235. static void aspeed_clk_disable(struct clk_hw *hw)
  236. {
  237. struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
  238. unsigned long flags;
  239. u32 clk = BIT(gate->clock_idx);
  240. u32 enval;
  241. spin_lock_irqsave(gate->lock, flags);
  242. enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? clk : 0;
  243. regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, enval);
  244. spin_unlock_irqrestore(gate->lock, flags);
  245. }
  246. static const struct clk_ops aspeed_clk_gate_ops = {
  247. .enable = aspeed_clk_enable,
  248. .disable = aspeed_clk_disable,
  249. .is_enabled = aspeed_clk_is_enabled,
  250. };
  251. /**
  252. * struct aspeed_reset - Aspeed reset controller
  253. * @map: regmap to access the containing system controller
  254. * @rcdev: reset controller device
  255. */
  256. struct aspeed_reset {
  257. struct regmap *map;
  258. struct reset_controller_dev rcdev;
  259. };
  260. #define to_aspeed_reset(p) container_of((p), struct aspeed_reset, rcdev)
  261. static const u8 aspeed_resets[] = {
  262. /* SCU04 resets */
  263. [ASPEED_RESET_XDMA] = 25,
  264. [ASPEED_RESET_MCTP] = 24,
  265. [ASPEED_RESET_ADC] = 23,
  266. [ASPEED_RESET_JTAG_MASTER] = 22,
  267. [ASPEED_RESET_MIC] = 18,
  268. [ASPEED_RESET_PWM] = 9,
  269. [ASPEED_RESET_PECI] = 10,
  270. [ASPEED_RESET_I2C] = 2,
  271. [ASPEED_RESET_AHB] = 1,
  272. /*
  273. * SCUD4 resets start at an offset to separate them from
  274. * the SCU04 resets.
  275. */
  276. [ASPEED_RESET_CRT1] = ASPEED_RESET2_OFFSET + 5,
  277. };
  278. static int aspeed_reset_deassert(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), 0);
  289. }
  290. static int aspeed_reset_assert(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. if (bit >= ASPEED_RESET2_OFFSET) {
  297. bit -= ASPEED_RESET2_OFFSET;
  298. reg = ASPEED_RESET_CTRL2;
  299. }
  300. return regmap_update_bits(ar->map, reg, BIT(bit), BIT(bit));
  301. }
  302. static int aspeed_reset_status(struct reset_controller_dev *rcdev,
  303. unsigned long id)
  304. {
  305. struct aspeed_reset *ar = to_aspeed_reset(rcdev);
  306. u32 reg = ASPEED_RESET_CTRL;
  307. u32 bit = aspeed_resets[id];
  308. int ret, val;
  309. if (bit >= ASPEED_RESET2_OFFSET) {
  310. bit -= ASPEED_RESET2_OFFSET;
  311. reg = ASPEED_RESET_CTRL2;
  312. }
  313. ret = regmap_read(ar->map, reg, &val);
  314. if (ret)
  315. return ret;
  316. return !!(val & BIT(bit));
  317. }
  318. static const struct reset_control_ops aspeed_reset_ops = {
  319. .assert = aspeed_reset_assert,
  320. .deassert = aspeed_reset_deassert,
  321. .status = aspeed_reset_status,
  322. };
  323. static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev,
  324. const char *name, const char *parent_name, unsigned long flags,
  325. struct regmap *map, u8 clock_idx, u8 reset_idx,
  326. u8 clk_gate_flags, spinlock_t *lock)
  327. {
  328. struct aspeed_clk_gate *gate;
  329. struct clk_init_data init;
  330. struct clk_hw *hw;
  331. int ret;
  332. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  333. if (!gate)
  334. return ERR_PTR(-ENOMEM);
  335. init.name = name;
  336. init.ops = &aspeed_clk_gate_ops;
  337. init.flags = flags;
  338. init.parent_names = parent_name ? &parent_name : NULL;
  339. init.num_parents = parent_name ? 1 : 0;
  340. gate->map = map;
  341. gate->clock_idx = clock_idx;
  342. gate->reset_idx = reset_idx;
  343. gate->flags = clk_gate_flags;
  344. gate->lock = lock;
  345. gate->hw.init = &init;
  346. hw = &gate->hw;
  347. ret = clk_hw_register(dev, hw);
  348. if (ret) {
  349. kfree(gate);
  350. hw = ERR_PTR(ret);
  351. }
  352. return hw;
  353. }
  354. static int aspeed_clk_probe(struct platform_device *pdev)
  355. {
  356. const struct aspeed_clk_soc_data *soc_data;
  357. struct device *dev = &pdev->dev;
  358. struct aspeed_reset *ar;
  359. struct regmap *map;
  360. struct clk_hw *hw;
  361. u32 val, rate;
  362. int i, ret;
  363. map = syscon_node_to_regmap(dev->of_node);
  364. if (IS_ERR(map)) {
  365. dev_err(dev, "no syscon regmap\n");
  366. return PTR_ERR(map);
  367. }
  368. ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
  369. if (!ar)
  370. return -ENOMEM;
  371. ar->map = map;
  372. ar->rcdev.owner = THIS_MODULE;
  373. ar->rcdev.nr_resets = ARRAY_SIZE(aspeed_resets);
  374. ar->rcdev.ops = &aspeed_reset_ops;
  375. ar->rcdev.of_node = dev->of_node;
  376. ret = devm_reset_controller_register(dev, &ar->rcdev);
  377. if (ret) {
  378. dev_err(dev, "could not register reset controller\n");
  379. return ret;
  380. }
  381. /* SoC generations share common layouts but have different divisors */
  382. soc_data = of_device_get_match_data(dev);
  383. if (!soc_data) {
  384. dev_err(dev, "no match data for platform\n");
  385. return -EINVAL;
  386. }
  387. /* UART clock div13 setting */
  388. regmap_read(map, ASPEED_MISC_CTRL, &val);
  389. if (val & UART_DIV13_EN)
  390. rate = 24000000 / 13;
  391. else
  392. rate = 24000000;
  393. /* TODO: Find the parent data for the uart clock */
  394. hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
  395. if (IS_ERR(hw))
  396. return PTR_ERR(hw);
  397. aspeed_clk_data->hws[ASPEED_CLK_UART] = hw;
  398. /*
  399. * Memory controller (M-PLL) PLL. This clock is configured by the
  400. * bootloader, and is exposed to Linux as a read-only clock rate.
  401. */
  402. regmap_read(map, ASPEED_MPLL_PARAM, &val);
  403. hw = soc_data->calc_pll("mpll", val);
  404. if (IS_ERR(hw))
  405. return PTR_ERR(hw);
  406. aspeed_clk_data->hws[ASPEED_CLK_MPLL] = hw;
  407. /* SD/SDIO clock divider (TODO: There's a gate too) */
  408. hw = clk_hw_register_divider_table(dev, "sdio", "hpll", 0,
  409. scu_base + ASPEED_CLK_SELECTION, 12, 3, 0,
  410. soc_data->div_table,
  411. &aspeed_clk_lock);
  412. if (IS_ERR(hw))
  413. return PTR_ERR(hw);
  414. aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw;
  415. /* MAC AHB bus clock divider */
  416. hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0,
  417. scu_base + ASPEED_CLK_SELECTION, 16, 3, 0,
  418. soc_data->mac_div_table,
  419. &aspeed_clk_lock);
  420. if (IS_ERR(hw))
  421. return PTR_ERR(hw);
  422. aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw;
  423. /* LPC Host (LHCLK) clock divider */
  424. hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
  425. scu_base + ASPEED_CLK_SELECTION, 20, 3, 0,
  426. soc_data->div_table,
  427. &aspeed_clk_lock);
  428. if (IS_ERR(hw))
  429. return PTR_ERR(hw);
  430. aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
  431. /* P-Bus (BCLK) clock divider */
  432. hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0,
  433. scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0,
  434. soc_data->div_table,
  435. &aspeed_clk_lock);
  436. if (IS_ERR(hw))
  437. return PTR_ERR(hw);
  438. aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw;
  439. /* Fixed 24MHz clock */
  440. hw = clk_hw_register_fixed_rate(NULL, "fixed-24m", "clkin",
  441. 0, 24000000);
  442. if (IS_ERR(hw))
  443. return PTR_ERR(hw);
  444. aspeed_clk_data->hws[ASPEED_CLK_24M] = hw;
  445. /*
  446. * TODO: There are a number of clocks that not included in this driver
  447. * as more information is required:
  448. * D2-PLL
  449. * D-PLL
  450. * YCLK
  451. * RGMII
  452. * RMII
  453. * UART[1..5] clock source mux
  454. * Video Engine (ECLK) mux and clock divider
  455. */
  456. for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) {
  457. const struct aspeed_gate_data *gd = &aspeed_gates[i];
  458. u32 gate_flags;
  459. /* Special case: the USB port 1 clock (bit 14) is always
  460. * working the opposite way from the other ones.
  461. */
  462. gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
  463. hw = aspeed_clk_hw_register_gate(dev,
  464. gd->name,
  465. gd->parent_name,
  466. gd->flags,
  467. map,
  468. gd->clock_idx,
  469. gd->reset_idx,
  470. gate_flags,
  471. &aspeed_clk_lock);
  472. if (IS_ERR(hw))
  473. return PTR_ERR(hw);
  474. aspeed_clk_data->hws[i] = hw;
  475. }
  476. return 0;
  477. };
  478. static const struct of_device_id aspeed_clk_dt_ids[] = {
  479. { .compatible = "aspeed,ast2400-scu", .data = &ast2400_data },
  480. { .compatible = "aspeed,ast2500-scu", .data = &ast2500_data },
  481. { }
  482. };
  483. static struct platform_driver aspeed_clk_driver = {
  484. .probe = aspeed_clk_probe,
  485. .driver = {
  486. .name = "aspeed-clk",
  487. .of_match_table = aspeed_clk_dt_ids,
  488. .suppress_bind_attrs = true,
  489. },
  490. };
  491. builtin_platform_driver(aspeed_clk_driver);
  492. static void __init aspeed_ast2400_cc(struct regmap *map)
  493. {
  494. struct clk_hw *hw;
  495. u32 val, div, clkin, hpll;
  496. const u16 hpll_rates[][4] = {
  497. {384, 360, 336, 408},
  498. {400, 375, 350, 425},
  499. };
  500. int rate;
  501. /*
  502. * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by
  503. * strapping
  504. */
  505. regmap_read(map, ASPEED_STRAP, &val);
  506. rate = (val >> 8) & 3;
  507. if (val & CLKIN_25MHZ_EN) {
  508. clkin = 25000000;
  509. hpll = hpll_rates[1][rate];
  510. } else if (val & AST2400_CLK_SOURCE_SEL) {
  511. clkin = 48000000;
  512. hpll = hpll_rates[0][rate];
  513. } else {
  514. clkin = 24000000;
  515. hpll = hpll_rates[0][rate];
  516. }
  517. hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, clkin);
  518. pr_debug("clkin @%u MHz\n", clkin / 1000000);
  519. /*
  520. * High-speed PLL clock derived from the crystal. This the CPU clock,
  521. * and we assume that it is enabled. It can be configured through the
  522. * HPLL_PARAM register, or set to a specified frequency by strapping.
  523. */
  524. regmap_read(map, ASPEED_HPLL_PARAM, &val);
  525. if (val & AST2400_HPLL_PROGRAMMED)
  526. hw = aspeed_ast2400_calc_pll("hpll", val);
  527. else
  528. hw = clk_hw_register_fixed_rate(NULL, "hpll", "clkin", 0,
  529. hpll * 1000000);
  530. aspeed_clk_data->hws[ASPEED_CLK_HPLL] = hw;
  531. /*
  532. * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK)
  533. * 00: Select CPU:AHB = 1:1
  534. * 01: Select CPU:AHB = 2:1
  535. * 10: Select CPU:AHB = 4:1
  536. * 11: Select CPU:AHB = 3:1
  537. */
  538. regmap_read(map, ASPEED_STRAP, &val);
  539. val = (val >> 10) & 0x3;
  540. div = val + 1;
  541. if (div == 3)
  542. div = 4;
  543. else if (div == 4)
  544. div = 3;
  545. hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
  546. aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
  547. /* APB clock clock selection register SCU08 (aka PCLK) */
  548. hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0,
  549. scu_base + ASPEED_CLK_SELECTION, 23, 3, 0,
  550. ast2400_div_table,
  551. &aspeed_clk_lock);
  552. aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
  553. }
  554. static void __init aspeed_ast2500_cc(struct regmap *map)
  555. {
  556. struct clk_hw *hw;
  557. u32 val, freq, div;
  558. /* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */
  559. regmap_read(map, ASPEED_STRAP, &val);
  560. if (val & CLKIN_25MHZ_EN)
  561. freq = 25000000;
  562. else
  563. freq = 24000000;
  564. hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq);
  565. pr_debug("clkin @%u MHz\n", freq / 1000000);
  566. /*
  567. * High-speed PLL clock derived from the crystal. This the CPU clock,
  568. * and we assume that it is enabled
  569. */
  570. regmap_read(map, ASPEED_HPLL_PARAM, &val);
  571. aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val);
  572. /* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/
  573. regmap_read(map, ASPEED_STRAP, &val);
  574. val = (val >> 9) & 0x7;
  575. WARN(val == 0, "strapping is zero: cannot determine ahb clock");
  576. div = 2 * (val + 1);
  577. hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div);
  578. aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw;
  579. /* APB clock clock selection register SCU08 (aka PCLK) */
  580. regmap_read(map, ASPEED_CLK_SELECTION, &val);
  581. val = (val >> 23) & 0x7;
  582. div = 4 * (val + 1);
  583. hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div);
  584. aspeed_clk_data->hws[ASPEED_CLK_APB] = hw;
  585. };
  586. static void __init aspeed_cc_init(struct device_node *np)
  587. {
  588. struct regmap *map;
  589. u32 val;
  590. int ret;
  591. int i;
  592. scu_base = of_iomap(np, 0);
  593. if (!scu_base)
  594. return;
  595. aspeed_clk_data = kzalloc(struct_size(aspeed_clk_data, hws,
  596. ASPEED_NUM_CLKS),
  597. GFP_KERNEL);
  598. if (!aspeed_clk_data)
  599. return;
  600. /*
  601. * This way all clocks fetched before the platform device probes,
  602. * except those we assign here for early use, will be deferred.
  603. */
  604. for (i = 0; i < ASPEED_NUM_CLKS; i++)
  605. aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
  606. map = syscon_node_to_regmap(np);
  607. if (IS_ERR(map)) {
  608. pr_err("no syscon regmap\n");
  609. return;
  610. }
  611. /*
  612. * We check that the regmap works on this very first access,
  613. * but as this is an MMIO-backed regmap, subsequent regmap
  614. * access is not going to fail and we skip error checks from
  615. * this point.
  616. */
  617. ret = regmap_read(map, ASPEED_STRAP, &val);
  618. if (ret) {
  619. pr_err("failed to read strapping register\n");
  620. return;
  621. }
  622. if (of_device_is_compatible(np, "aspeed,ast2400-scu"))
  623. aspeed_ast2400_cc(map);
  624. else if (of_device_is_compatible(np, "aspeed,ast2500-scu"))
  625. aspeed_ast2500_cc(map);
  626. else
  627. pr_err("unknown platform, failed to add clocks\n");
  628. aspeed_clk_data->num = ASPEED_NUM_CLKS;
  629. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data);
  630. if (ret)
  631. pr_err("failed to add DT provider: %d\n", ret);
  632. };
  633. CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init);
  634. CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init);