clk-gemini.c 12 KB

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