clk-gemini.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 int gemini_pci_determine_rate(struct clk_hw *hw,
  111. struct clk_rate_request *req)
  112. {
  113. /* We support 33 and 66 MHz */
  114. if (req->rate < 48000000)
  115. req->rate = 33000000;
  116. else
  117. req->rate = 66000000;
  118. return 0;
  119. }
  120. static int gemini_pci_set_rate(struct clk_hw *hw, unsigned long rate,
  121. unsigned long parent_rate)
  122. {
  123. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  124. if (rate == 33000000)
  125. return regmap_update_bits(pciclk->map,
  126. GEMINI_GLOBAL_MISC_CONTROL,
  127. PCI_CLK_66MHZ, 0);
  128. if (rate == 66000000)
  129. return regmap_update_bits(pciclk->map,
  130. GEMINI_GLOBAL_MISC_CONTROL,
  131. 0, PCI_CLK_66MHZ);
  132. return -EINVAL;
  133. }
  134. static int gemini_pci_enable(struct clk_hw *hw)
  135. {
  136. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  137. regmap_update_bits(pciclk->map, GEMINI_GLOBAL_CLOCK_CONTROL,
  138. 0, PCI_CLKRUN_EN);
  139. return 0;
  140. }
  141. static void gemini_pci_disable(struct clk_hw *hw)
  142. {
  143. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  144. regmap_update_bits(pciclk->map, GEMINI_GLOBAL_CLOCK_CONTROL,
  145. PCI_CLKRUN_EN, 0);
  146. }
  147. static int gemini_pci_is_enabled(struct clk_hw *hw)
  148. {
  149. struct clk_gemini_pci *pciclk = to_pciclk(hw);
  150. unsigned int val;
  151. regmap_read(pciclk->map, GEMINI_GLOBAL_CLOCK_CONTROL, &val);
  152. return !!(val & PCI_CLKRUN_EN);
  153. }
  154. static const struct clk_ops gemini_pci_clk_ops = {
  155. .recalc_rate = gemini_pci_recalc_rate,
  156. .determine_rate = gemini_pci_determine_rate,
  157. .set_rate = gemini_pci_set_rate,
  158. .enable = gemini_pci_enable,
  159. .disable = gemini_pci_disable,
  160. .is_enabled = gemini_pci_is_enabled,
  161. };
  162. static struct clk_hw *gemini_pci_clk_setup(const char *name,
  163. const char *parent_name,
  164. struct regmap *map)
  165. {
  166. struct clk_gemini_pci *pciclk;
  167. struct clk_init_data init;
  168. int ret;
  169. pciclk = kzalloc_obj(*pciclk);
  170. if (!pciclk)
  171. return ERR_PTR(-ENOMEM);
  172. init.name = name;
  173. init.ops = &gemini_pci_clk_ops;
  174. init.flags = 0;
  175. init.parent_names = &parent_name;
  176. init.num_parents = 1;
  177. pciclk->map = map;
  178. pciclk->hw.init = &init;
  179. ret = clk_hw_register(NULL, &pciclk->hw);
  180. if (ret) {
  181. kfree(pciclk);
  182. return ERR_PTR(ret);
  183. }
  184. return &pciclk->hw;
  185. }
  186. /*
  187. * This is a self-deasserting reset controller.
  188. */
  189. static int gemini_reset(struct reset_controller_dev *rcdev,
  190. unsigned long id)
  191. {
  192. struct gemini_reset *gr = to_gemini_reset(rcdev);
  193. /* Manual says to always set BIT 30 (CPU1) to 1 */
  194. return regmap_write(gr->map,
  195. GEMINI_GLOBAL_SOFT_RESET,
  196. BIT(GEMINI_RESET_CPU1) | BIT(id));
  197. }
  198. static int gemini_reset_assert(struct reset_controller_dev *rcdev,
  199. unsigned long id)
  200. {
  201. return 0;
  202. }
  203. static int gemini_reset_deassert(struct reset_controller_dev *rcdev,
  204. unsigned long id)
  205. {
  206. return 0;
  207. }
  208. static int gemini_reset_status(struct reset_controller_dev *rcdev,
  209. unsigned long id)
  210. {
  211. struct gemini_reset *gr = to_gemini_reset(rcdev);
  212. u32 val;
  213. int ret;
  214. ret = regmap_read(gr->map, GEMINI_GLOBAL_SOFT_RESET, &val);
  215. if (ret)
  216. return ret;
  217. return !!(val & BIT(id));
  218. }
  219. static const struct reset_control_ops gemini_reset_ops = {
  220. .reset = gemini_reset,
  221. .assert = gemini_reset_assert,
  222. .deassert = gemini_reset_deassert,
  223. .status = gemini_reset_status,
  224. };
  225. static int gemini_clk_probe(struct platform_device *pdev)
  226. {
  227. /* Gives the fracions 1x, 1.5x, 1.85x and 2x */
  228. unsigned int cpu_ahb_mult[4] = { 1, 3, 24, 2 };
  229. unsigned int cpu_ahb_div[4] = { 1, 2, 13, 1 };
  230. void __iomem *base;
  231. struct gemini_reset *gr;
  232. struct regmap *map;
  233. struct clk_hw *hw;
  234. struct device *dev = &pdev->dev;
  235. struct device_node *np = dev->of_node;
  236. unsigned int mult, div;
  237. u32 val;
  238. int ret;
  239. int i;
  240. gr = devm_kzalloc(dev, sizeof(*gr), GFP_KERNEL);
  241. if (!gr)
  242. return -ENOMEM;
  243. /* Remap the system controller for the exclusive register */
  244. base = devm_platform_ioremap_resource(pdev, 0);
  245. if (IS_ERR(base))
  246. return PTR_ERR(base);
  247. map = syscon_node_to_regmap(np);
  248. if (IS_ERR(map)) {
  249. dev_err(dev, "no syscon regmap\n");
  250. return PTR_ERR(map);
  251. }
  252. gr->map = map;
  253. gr->rcdev.owner = THIS_MODULE;
  254. gr->rcdev.nr_resets = 32;
  255. gr->rcdev.ops = &gemini_reset_ops;
  256. gr->rcdev.of_node = np;
  257. ret = devm_reset_controller_register(dev, &gr->rcdev);
  258. if (ret) {
  259. dev_err(dev, "could not register reset controller\n");
  260. return ret;
  261. }
  262. /* RTC clock 32768 Hz */
  263. hw = clk_hw_register_fixed_rate(NULL, "rtc", NULL, 0, 32768);
  264. gemini_clk_data->hws[GEMINI_CLK_RTC] = hw;
  265. /* CPU clock derived as a fixed ratio from the AHB clock */
  266. regmap_read(map, GEMINI_GLOBAL_STATUS, &val);
  267. val >>= CPU_AHB_RATIO_SHIFT;
  268. val &= CPU_AHB_RATIO_MASK;
  269. hw = clk_hw_register_fixed_factor(NULL, "cpu", "ahb", 0,
  270. cpu_ahb_mult[val],
  271. cpu_ahb_div[val]);
  272. gemini_clk_data->hws[GEMINI_CLK_CPU] = hw;
  273. /* Security clock is 1:1 or 0.75 of APB */
  274. regmap_read(map, GEMINI_GLOBAL_CLOCK_CONTROL, &val);
  275. if (val & SECURITY_CLK_SEL) {
  276. mult = 1;
  277. div = 1;
  278. } else {
  279. mult = 3;
  280. div = 4;
  281. }
  282. hw = clk_hw_register_fixed_factor(NULL, "secdiv", "ahb", 0, mult, div);
  283. /*
  284. * These are the leaf gates, at boot no clocks are gated.
  285. */
  286. for (i = 0; i < ARRAY_SIZE(gemini_gates); i++) {
  287. const struct gemini_gate_data *gd;
  288. gd = &gemini_gates[i];
  289. gemini_clk_data->hws[GEMINI_CLK_GATES + i] =
  290. clk_hw_register_gate(NULL, gd->name,
  291. gd->parent_name,
  292. gd->flags,
  293. base + GEMINI_GLOBAL_CLOCK_CONTROL,
  294. gd->bit_idx,
  295. CLK_GATE_SET_TO_DISABLE,
  296. &gemini_clk_lock);
  297. }
  298. /*
  299. * The TV Interface Controller has a 5-bit half divider register.
  300. * This clock is supposed to be 27MHz as this is an exact multiple
  301. * of PAL and NTSC frequencies. The register is undocumented :(
  302. * FIXME: figure out the parent and how the divider works.
  303. */
  304. mult = 1;
  305. div = ((val >> TVC_HALFDIV_SHIFT) & TVC_HALFDIV_MASK);
  306. dev_dbg(dev, "TVC half divider value = %d\n", div);
  307. div += 1;
  308. hw = clk_hw_register_fixed_rate(NULL, "tvcdiv", "xtal", 0, 27000000);
  309. gemini_clk_data->hws[GEMINI_CLK_TVC] = hw;
  310. /* FIXME: very unclear what the parent is */
  311. hw = gemini_pci_clk_setup("PCI", "xtal", map);
  312. gemini_clk_data->hws[GEMINI_CLK_PCI] = hw;
  313. /* FIXME: very unclear what the parent is */
  314. hw = clk_hw_register_fixed_rate(NULL, "uart", "xtal", 0, 48000000);
  315. gemini_clk_data->hws[GEMINI_CLK_UART] = hw;
  316. return 0;
  317. }
  318. static const struct of_device_id gemini_clk_dt_ids[] = {
  319. { .compatible = "cortina,gemini-syscon", },
  320. { /* sentinel */ },
  321. };
  322. static struct platform_driver gemini_clk_driver = {
  323. .probe = gemini_clk_probe,
  324. .driver = {
  325. .name = "gemini-clk",
  326. .of_match_table = gemini_clk_dt_ids,
  327. .suppress_bind_attrs = true,
  328. },
  329. };
  330. builtin_platform_driver(gemini_clk_driver);
  331. static void __init gemini_cc_init(struct device_node *np)
  332. {
  333. struct regmap *map;
  334. struct clk_hw *hw;
  335. unsigned long freq;
  336. unsigned int mult, div;
  337. u32 val;
  338. int ret;
  339. int i;
  340. gemini_clk_data = kzalloc_flex(*gemini_clk_data, hws, GEMINI_NUM_CLKS);
  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);