clk-gemini.c 12 KB

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