clk-gemini.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cortina Gemini SoC Clock Controller driver
  4. * Copyright (c) 2017 Linus Walleij <linus.walleij@linaro.org>
  5. */
  6. #define pr_fmt(fmt) "clk-gemini: " fmt
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/regmap.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/reset-controller.h>
  20. #include <dt-bindings/reset/cortina,gemini-reset.h>
  21. #include <dt-bindings/clock/cortina,gemini-clock.h>
  22. /* Globally visible clocks */
  23. static DEFINE_SPINLOCK(gemini_clk_lock);
  24. #define GEMINI_GLOBAL_STATUS 0x04
  25. #define PLL_OSC_SEL BIT(30)
  26. #define AHBSPEED_SHIFT (15)
  27. #define AHBSPEED_MASK 0x07
  28. #define CPU_AHB_RATIO_SHIFT (18)
  29. #define CPU_AHB_RATIO_MASK 0x03
  30. #define GEMINI_GLOBAL_PLL_CONTROL 0x08
  31. #define GEMINI_GLOBAL_SOFT_RESET 0x0c
  32. #define GEMINI_GLOBAL_MISC_CONTROL 0x30
  33. #define PCI_CLK_66MHZ BIT(18)
  34. #define GEMINI_GLOBAL_CLOCK_CONTROL 0x34
  35. #define PCI_CLKRUN_EN BIT(16)
  36. #define TVC_HALFDIV_SHIFT (24)
  37. #define TVC_HALFDIV_MASK 0x1f
  38. #define SECURITY_CLK_SEL BIT(29)
  39. #define GEMINI_GLOBAL_PCI_DLL_CONTROL 0x44
  40. #define PCI_DLL_BYPASS BIT(31)
  41. #define PCI_DLL_TAP_SEL_MASK 0x1f
  42. /**
  43. * struct gemini_gate_data - Gemini gated clocks
  44. * @bit_idx: the bit used to gate this clock in the clock register
  45. * @name: the clock name
  46. * @parent_name: the name of the parent clock
  47. * @flags: standard clock framework flags
  48. */
  49. struct gemini_gate_data {
  50. u8 bit_idx;
  51. const char *name;
  52. const char *parent_name;
  53. unsigned long flags;
  54. };
  55. /**
  56. * struct clk_gemini_pci - Gemini PCI clock
  57. * @hw: corresponding clock hardware entry
  58. * @map: regmap to access the registers
  59. */
  60. struct clk_gemini_pci {
  61. struct clk_hw hw;
  62. struct regmap *map;
  63. };
  64. /**
  65. * struct gemini_reset - gemini reset controller
  66. * @map: regmap to access the containing system controller
  67. * @rcdev: reset controller device
  68. */
  69. struct gemini_reset {
  70. struct regmap *map;
  71. struct reset_controller_dev rcdev;
  72. };
  73. /* Keeps track of all clocks */
  74. static struct clk_hw_onecell_data *gemini_clk_data;
  75. static const struct gemini_gate_data gemini_gates[] = {
  76. { 1, "security-gate", "secdiv", 0 },
  77. { 2, "gmac0-gate", "ahb", 0 },
  78. { 3, "gmac1-gate", "ahb", 0 },
  79. { 4, "sata0-gate", "ahb", 0 },
  80. { 5, "sata1-gate", "ahb", 0 },
  81. { 6, "usb0-gate", "ahb", 0 },
  82. { 7, "usb1-gate", "ahb", 0 },
  83. { 8, "ide-gate", "ahb", 0 },
  84. { 9, "pci-gate", "ahb", 0 },
  85. /*
  86. * The DDR controller may never have a driver, but certainly must
  87. * not be gated off.
  88. */
  89. { 10, "ddr-gate", "ahb", CLK_IS_CRITICAL },
  90. /*
  91. * The flash controller must be on to access NOR flash through the
  92. * memory map.
  93. */
  94. { 11, "flash-gate", "ahb", CLK_IGNORE_UNUSED },
  95. { 12, "tvc-gate", "ahb", 0 },
  96. { 13, "boot-gate", "apb", 0 },
  97. };
  98. #define to_pciclk(_hw) container_of(_hw, struct clk_gemini_pci, hw)
  99. #define to_gemini_reset(p) container_of((p), struct gemini_reset, rcdev)
  100. static unsigned long gemini_pci_recalc_rate(struct clk_hw *hw,
  101. unsigned long parent_rate)
  102. {
  103. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  104. u32 val;
  105. regmap_read(pciclk->map, GEMINI_GLOBAL_MISC_CONTROL, &val);
  106. if (val & PCI_CLK_66MHZ)
  107. return 66000000;
  108. return 33000000;
  109. }
  110. static long gemini_pci_round_rate(struct clk_hw *hw, unsigned long rate,
  111. unsigned long *prate)
  112. {
  113. /* We support 33 and 66 MHz */
  114. if (rate < 48000000)
  115. return 33000000;
  116. return 66000000;
  117. }
  118. static int gemini_pci_set_rate(struct clk_hw *hw, unsigned long rate,
  119. unsigned long parent_rate)
  120. {
  121. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  122. if (rate == 33000000)
  123. return regmap_update_bits(pciclk->map,
  124. GEMINI_GLOBAL_MISC_CONTROL,
  125. PCI_CLK_66MHZ, 0);
  126. if (rate == 66000000)
  127. return regmap_update_bits(pciclk->map,
  128. GEMINI_GLOBAL_MISC_CONTROL,
  129. 0, PCI_CLK_66MHZ);
  130. return -EINVAL;
  131. }
  132. static int gemini_pci_enable(struct clk_hw *hw)
  133. {
  134. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  135. regmap_update_bits(pciclk->map, GEMINI_GLOBAL_CLOCK_CONTROL,
  136. 0, PCI_CLKRUN_EN);
  137. return 0;
  138. }
  139. static void gemini_pci_disable(struct clk_hw *hw)
  140. {
  141. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  142. regmap_update_bits(pciclk->map, GEMINI_GLOBAL_CLOCK_CONTROL,
  143. PCI_CLKRUN_EN, 0);
  144. }
  145. static int gemini_pci_is_enabled(struct clk_hw *hw)
  146. {
  147. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  148. unsigned int val;
  149. regmap_read(pciclk->map, GEMINI_GLOBAL_CLOCK_CONTROL, &val);
  150. return !!(val & PCI_CLKRUN_EN);
  151. }
  152. static const struct clk_ops gemini_pci_clk_ops = {
  153. .recalc_rate = gemini_pci_recalc_rate,
  154. .round_rate = gemini_pci_round_rate,
  155. .set_rate = gemini_pci_set_rate,
  156. .enable = gemini_pci_enable,
  157. .disable = gemini_pci_disable,
  158. .is_enabled = gemini_pci_is_enabled,
  159. };
  160. static struct clk_hw *gemini_pci_clk_setup(const char *name,
  161. const char *parent_name,
  162. struct regmap *map)
  163. {
  164. struct clk_gemini_pci *pciclk;
  165. struct clk_init_data init;
  166. int ret;
  167. pciclk = kzalloc(sizeof(*pciclk), GFP_KERNEL);
  168. if (!pciclk)
  169. return ERR_PTR(-ENOMEM);
  170. init.name = name;
  171. init.ops = &gemini_pci_clk_ops;
  172. init.flags = 0;
  173. init.parent_names = &parent_name;
  174. init.num_parents = 1;
  175. pciclk->map = map;
  176. pciclk->hw.init = &init;
  177. ret = clk_hw_register(NULL, &pciclk->hw);
  178. if (ret) {
  179. kfree(pciclk);
  180. return ERR_PTR(ret);
  181. }
  182. return &pciclk->hw;
  183. }
  184. /*
  185. * This is a self-deasserting reset controller.
  186. */
  187. static int gemini_reset(struct reset_controller_dev *rcdev,
  188. unsigned long id)
  189. {
  190. struct gemini_reset *gr = to_gemini_reset(rcdev);
  191. /* Manual says to always set BIT 30 (CPU1) to 1 */
  192. return regmap_write(gr->map,
  193. GEMINI_GLOBAL_SOFT_RESET,
  194. BIT(GEMINI_RESET_CPU1) | BIT(id));
  195. }
  196. static int gemini_reset_assert(struct reset_controller_dev *rcdev,
  197. unsigned long id)
  198. {
  199. return 0;
  200. }
  201. static int gemini_reset_deassert(struct reset_controller_dev *rcdev,
  202. unsigned long id)
  203. {
  204. return 0;
  205. }
  206. static int gemini_reset_status(struct reset_controller_dev *rcdev,
  207. unsigned long id)
  208. {
  209. struct gemini_reset *gr = to_gemini_reset(rcdev);
  210. u32 val;
  211. int ret;
  212. ret = regmap_read(gr->map, GEMINI_GLOBAL_SOFT_RESET, &val);
  213. if (ret)
  214. return ret;
  215. return !!(val & BIT(id));
  216. }
  217. static const struct reset_control_ops gemini_reset_ops = {
  218. .reset = gemini_reset,
  219. .assert = gemini_reset_assert,
  220. .deassert = gemini_reset_deassert,
  221. .status = gemini_reset_status,
  222. };
  223. static int gemini_clk_probe(struct platform_device *pdev)
  224. {
  225. /* Gives the fracions 1x, 1.5x, 1.85x and 2x */
  226. unsigned int cpu_ahb_mult[4] = { 1, 3, 24, 2 };
  227. unsigned int cpu_ahb_div[4] = { 1, 2, 13, 1 };
  228. void __iomem *base;
  229. struct gemini_reset *gr;
  230. struct regmap *map;
  231. struct clk_hw *hw;
  232. struct device *dev = &pdev->dev;
  233. struct device_node *np = dev->of_node;
  234. unsigned int mult, div;
  235. u32 val;
  236. int ret;
  237. int i;
  238. gr = devm_kzalloc(dev, sizeof(*gr), GFP_KERNEL);
  239. if (!gr)
  240. return -ENOMEM;
  241. /* Remap the system controller for the exclusive register */
  242. base = devm_platform_ioremap_resource(pdev, 0);
  243. if (IS_ERR(base))
  244. return PTR_ERR(base);
  245. map = syscon_node_to_regmap(np);
  246. if (IS_ERR(map)) {
  247. dev_err(dev, "no syscon regmap\n");
  248. return PTR_ERR(map);
  249. }
  250. gr->map = map;
  251. gr->rcdev.owner = THIS_MODULE;
  252. gr->rcdev.nr_resets = 32;
  253. gr->rcdev.ops = &gemini_reset_ops;
  254. gr->rcdev.of_node = np;
  255. ret = devm_reset_controller_register(dev, &gr->rcdev);
  256. if (ret) {
  257. dev_err(dev, "could not register reset controller\n");
  258. return ret;
  259. }
  260. /* RTC clock 32768 Hz */
  261. hw = clk_hw_register_fixed_rate(NULL, "rtc", NULL, 0, 32768);
  262. gemini_clk_data->hws[GEMINI_CLK_RTC] = hw;
  263. /* CPU clock derived as a fixed ratio from the AHB clock */
  264. regmap_read(map, GEMINI_GLOBAL_STATUS, &val);
  265. val >>= CPU_AHB_RATIO_SHIFT;
  266. val &= CPU_AHB_RATIO_MASK;
  267. hw = clk_hw_register_fixed_factor(NULL, "cpu", "ahb", 0,
  268. cpu_ahb_mult[val],
  269. cpu_ahb_div[val]);
  270. gemini_clk_data->hws[GEMINI_CLK_CPU] = hw;
  271. /* Security clock is 1:1 or 0.75 of APB */
  272. regmap_read(map, GEMINI_GLOBAL_CLOCK_CONTROL, &val);
  273. if (val & SECURITY_CLK_SEL) {
  274. mult = 1;
  275. div = 1;
  276. } else {
  277. mult = 3;
  278. div = 4;
  279. }
  280. hw = clk_hw_register_fixed_factor(NULL, "secdiv", "ahb", 0, mult, div);
  281. /*
  282. * These are the leaf gates, at boot no clocks are gated.
  283. */
  284. for (i = 0; i < ARRAY_SIZE(gemini_gates); i++) {
  285. const struct gemini_gate_data *gd;
  286. gd = &gemini_gates[i];
  287. gemini_clk_data->hws[GEMINI_CLK_GATES + i] =
  288. clk_hw_register_gate(NULL, gd->name,
  289. gd->parent_name,
  290. gd->flags,
  291. base + GEMINI_GLOBAL_CLOCK_CONTROL,
  292. gd->bit_idx,
  293. CLK_GATE_SET_TO_DISABLE,
  294. &gemini_clk_lock);
  295. }
  296. /*
  297. * The TV Interface Controller has a 5-bit half divider register.
  298. * This clock is supposed to be 27MHz as this is an exact multiple
  299. * of PAL and NTSC frequencies. The register is undocumented :(
  300. * FIXME: figure out the parent and how the divider works.
  301. */
  302. mult = 1;
  303. div = ((val >> TVC_HALFDIV_SHIFT) & TVC_HALFDIV_MASK);
  304. dev_dbg(dev, "TVC half divider value = %d\n", div);
  305. div += 1;
  306. hw = clk_hw_register_fixed_rate(NULL, "tvcdiv", "xtal", 0, 27000000);
  307. gemini_clk_data->hws[GEMINI_CLK_TVC] = hw;
  308. /* FIXME: very unclear what the parent is */
  309. hw = gemini_pci_clk_setup("PCI", "xtal", map);
  310. gemini_clk_data->hws[GEMINI_CLK_PCI] = hw;
  311. /* FIXME: very unclear what the parent is */
  312. hw = clk_hw_register_fixed_rate(NULL, "uart", "xtal", 0, 48000000);
  313. gemini_clk_data->hws[GEMINI_CLK_UART] = hw;
  314. return 0;
  315. }
  316. static const struct of_device_id gemini_clk_dt_ids[] = {
  317. { .compatible = "cortina,gemini-syscon", },
  318. { /* sentinel */ },
  319. };
  320. static struct platform_driver gemini_clk_driver = {
  321. .probe = gemini_clk_probe,
  322. .driver = {
  323. .name = "gemini-clk",
  324. .of_match_table = gemini_clk_dt_ids,
  325. .suppress_bind_attrs = true,
  326. },
  327. };
  328. builtin_platform_driver(gemini_clk_driver);
  329. static void __init gemini_cc_init(struct device_node *np)
  330. {
  331. struct regmap *map;
  332. struct clk_hw *hw;
  333. unsigned long freq;
  334. unsigned int mult, div;
  335. u32 val;
  336. int ret;
  337. int i;
  338. gemini_clk_data = kzalloc(struct_size(gemini_clk_data, hws,
  339. GEMINI_NUM_CLKS),
  340. GFP_KERNEL);
  341. if (!gemini_clk_data)
  342. return;
  343. gemini_clk_data->num = GEMINI_NUM_CLKS;
  344. /*
  345. * This way all clock fetched before the platform device probes,
  346. * except those we assign here for early use, will be deferred.
  347. */
  348. for (i = 0; i < GEMINI_NUM_CLKS; i++)
  349. gemini_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
  350. map = syscon_node_to_regmap(np);
  351. if (IS_ERR(map)) {
  352. pr_err("no syscon regmap\n");
  353. return;
  354. }
  355. /*
  356. * We check that the regmap works on this very first access,
  357. * but as this is an MMIO-backed regmap, subsequent regmap
  358. * access is not going to fail and we skip error checks from
  359. * this point.
  360. */
  361. ret = regmap_read(map, GEMINI_GLOBAL_STATUS, &val);
  362. if (ret) {
  363. pr_err("failed to read global status register\n");
  364. return;
  365. }
  366. /*
  367. * XTAL is the crystal oscillator, 60 or 30 MHz selected from
  368. * strap pin E6
  369. */
  370. if (val & PLL_OSC_SEL)
  371. freq = 30000000;
  372. else
  373. freq = 60000000;
  374. hw = clk_hw_register_fixed_rate(NULL, "xtal", NULL, 0, freq);
  375. pr_debug("main crystal @%lu MHz\n", freq / 1000000);
  376. /* VCO clock derived from the crystal */
  377. mult = 13 + ((val >> AHBSPEED_SHIFT) & AHBSPEED_MASK);
  378. div = 2;
  379. /* If we run on 30 MHz crystal we have to multiply with two */
  380. if (val & PLL_OSC_SEL)
  381. mult *= 2;
  382. hw = clk_hw_register_fixed_factor(NULL, "vco", "xtal", 0, mult, div);
  383. /* The AHB clock is always 1/3 of the VCO */
  384. hw = clk_hw_register_fixed_factor(NULL, "ahb", "vco", 0, 1, 3);
  385. gemini_clk_data->hws[GEMINI_CLK_AHB] = hw;
  386. /* The APB clock is always 1/6 of the AHB */
  387. hw = clk_hw_register_fixed_factor(NULL, "apb", "ahb", 0, 1, 6);
  388. gemini_clk_data->hws[GEMINI_CLK_APB] = hw;
  389. /* Register the clocks to be accessed by the device tree */
  390. of_clk_add_hw_provider(np, of_clk_hw_onecell_get, gemini_clk_data);
  391. }
  392. CLK_OF_DECLARE_DRIVER(gemini_cc, "cortina,gemini-syscon", gemini_cc_init);