ingenic-sysost.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ingenic XBurst SoCs SYSOST clocks driver
  4. * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/sched_clock.h>
  17. #include <linux/slab.h>
  18. #include <linux/syscore_ops.h>
  19. #include <dt-bindings/clock/ingenic,sysost.h>
  20. /* OST register offsets */
  21. #define OST_REG_OSTCCR 0x00
  22. #define OST_REG_OSTCR 0x08
  23. #define OST_REG_OSTFR 0x0c
  24. #define OST_REG_OSTMR 0x10
  25. #define OST_REG_OST1DFR 0x14
  26. #define OST_REG_OST1CNT 0x18
  27. #define OST_REG_OST2CNTL 0x20
  28. #define OST_REG_OSTCNT2HBUF 0x24
  29. #define OST_REG_OSTESR 0x34
  30. #define OST_REG_OSTECR 0x38
  31. /* bits within the OSTCCR register */
  32. #define OSTCCR_PRESCALE1_MASK 0x3
  33. #define OSTCCR_PRESCALE2_MASK 0xc
  34. /* bits within the OSTCR register */
  35. #define OSTCR_OST1CLR BIT(0)
  36. #define OSTCR_OST2CLR BIT(1)
  37. /* bits within the OSTFR register */
  38. #define OSTFR_FFLAG BIT(0)
  39. /* bits within the OSTMR register */
  40. #define OSTMR_FMASK BIT(0)
  41. /* bits within the OSTESR register */
  42. #define OSTESR_OST1ENS BIT(0)
  43. #define OSTESR_OST2ENS BIT(1)
  44. /* bits within the OSTECR register */
  45. #define OSTECR_OST1ENC BIT(0)
  46. #define OSTECR_OST2ENC BIT(1)
  47. struct ingenic_soc_info {
  48. unsigned int num_channels;
  49. };
  50. struct ingenic_ost_clk_info {
  51. struct clk_init_data init_data;
  52. u8 ostccr_reg;
  53. };
  54. struct ingenic_ost_clk {
  55. struct clk_hw hw;
  56. unsigned int idx;
  57. struct ingenic_ost *ost;
  58. const struct ingenic_ost_clk_info *info;
  59. };
  60. struct ingenic_ost {
  61. void __iomem *base;
  62. const struct ingenic_soc_info *soc_info;
  63. struct clk *clk, *percpu_timer_clk, *global_timer_clk;
  64. struct clock_event_device cevt;
  65. struct clocksource cs;
  66. char name[20];
  67. struct clk_hw_onecell_data *clocks;
  68. };
  69. static struct ingenic_ost *ingenic_ost;
  70. static inline struct ingenic_ost_clk *to_ost_clk(struct clk_hw *hw)
  71. {
  72. return container_of(hw, struct ingenic_ost_clk, hw);
  73. }
  74. static unsigned long ingenic_ost_percpu_timer_recalc_rate(struct clk_hw *hw,
  75. unsigned long parent_rate)
  76. {
  77. struct ingenic_ost_clk *ost_clk = to_ost_clk(hw);
  78. const struct ingenic_ost_clk_info *info = ost_clk->info;
  79. unsigned int prescale;
  80. prescale = readl(ost_clk->ost->base + info->ostccr_reg);
  81. prescale = FIELD_GET(OSTCCR_PRESCALE1_MASK, prescale);
  82. return parent_rate >> (prescale * 2);
  83. }
  84. static unsigned long ingenic_ost_global_timer_recalc_rate(struct clk_hw *hw,
  85. unsigned long parent_rate)
  86. {
  87. struct ingenic_ost_clk *ost_clk = to_ost_clk(hw);
  88. const struct ingenic_ost_clk_info *info = ost_clk->info;
  89. unsigned int prescale;
  90. prescale = readl(ost_clk->ost->base + info->ostccr_reg);
  91. prescale = FIELD_GET(OSTCCR_PRESCALE2_MASK, prescale);
  92. return parent_rate >> (prescale * 2);
  93. }
  94. static u8 ingenic_ost_get_prescale(unsigned long rate, unsigned long req_rate)
  95. {
  96. u8 prescale;
  97. for (prescale = 0; prescale < 2; prescale++)
  98. if ((rate >> (prescale * 2)) <= req_rate)
  99. return prescale;
  100. return 2; /* /16 divider */
  101. }
  102. static int ingenic_ost_determine_rate(struct clk_hw *hw,
  103. struct clk_rate_request *req)
  104. {
  105. unsigned long rate = req->best_parent_rate;
  106. u8 prescale;
  107. if (req->rate > rate) {
  108. req->rate = rate;
  109. return 0;
  110. }
  111. prescale = ingenic_ost_get_prescale(rate, req->rate);
  112. req->rate = rate >> (prescale * 2);
  113. return 0;
  114. }
  115. static int ingenic_ost_percpu_timer_set_rate(struct clk_hw *hw, unsigned long req_rate,
  116. unsigned long parent_rate)
  117. {
  118. struct ingenic_ost_clk *ost_clk = to_ost_clk(hw);
  119. const struct ingenic_ost_clk_info *info = ost_clk->info;
  120. u8 prescale = ingenic_ost_get_prescale(parent_rate, req_rate);
  121. int val;
  122. val = readl(ost_clk->ost->base + info->ostccr_reg);
  123. val &= ~OSTCCR_PRESCALE1_MASK;
  124. val |= FIELD_PREP(OSTCCR_PRESCALE1_MASK, prescale);
  125. writel(val, ost_clk->ost->base + info->ostccr_reg);
  126. return 0;
  127. }
  128. static int ingenic_ost_global_timer_set_rate(struct clk_hw *hw, unsigned long req_rate,
  129. unsigned long parent_rate)
  130. {
  131. struct ingenic_ost_clk *ost_clk = to_ost_clk(hw);
  132. const struct ingenic_ost_clk_info *info = ost_clk->info;
  133. u8 prescale = ingenic_ost_get_prescale(parent_rate, req_rate);
  134. int val;
  135. val = readl(ost_clk->ost->base + info->ostccr_reg);
  136. val &= ~OSTCCR_PRESCALE2_MASK;
  137. val |= FIELD_PREP(OSTCCR_PRESCALE2_MASK, prescale);
  138. writel(val, ost_clk->ost->base + info->ostccr_reg);
  139. return 0;
  140. }
  141. static const struct clk_ops ingenic_ost_percpu_timer_ops = {
  142. .recalc_rate = ingenic_ost_percpu_timer_recalc_rate,
  143. .determine_rate = ingenic_ost_determine_rate,
  144. .set_rate = ingenic_ost_percpu_timer_set_rate,
  145. };
  146. static const struct clk_ops ingenic_ost_global_timer_ops = {
  147. .recalc_rate = ingenic_ost_global_timer_recalc_rate,
  148. .determine_rate = ingenic_ost_determine_rate,
  149. .set_rate = ingenic_ost_global_timer_set_rate,
  150. };
  151. static const char * const ingenic_ost_clk_parents[] = { "ext" };
  152. static const struct ingenic_ost_clk_info x1000_ost_clk_info[] = {
  153. [OST_CLK_PERCPU_TIMER] = {
  154. .init_data = {
  155. .name = "percpu timer",
  156. .parent_names = ingenic_ost_clk_parents,
  157. .num_parents = ARRAY_SIZE(ingenic_ost_clk_parents),
  158. .ops = &ingenic_ost_percpu_timer_ops,
  159. .flags = CLK_SET_RATE_UNGATE,
  160. },
  161. .ostccr_reg = OST_REG_OSTCCR,
  162. },
  163. [OST_CLK_GLOBAL_TIMER] = {
  164. .init_data = {
  165. .name = "global timer",
  166. .parent_names = ingenic_ost_clk_parents,
  167. .num_parents = ARRAY_SIZE(ingenic_ost_clk_parents),
  168. .ops = &ingenic_ost_global_timer_ops,
  169. .flags = CLK_SET_RATE_UNGATE,
  170. },
  171. .ostccr_reg = OST_REG_OSTCCR,
  172. },
  173. };
  174. static u64 notrace ingenic_ost_global_timer_read_cntl(void)
  175. {
  176. struct ingenic_ost *ost = ingenic_ost;
  177. unsigned int count;
  178. count = readl(ost->base + OST_REG_OST2CNTL);
  179. return count;
  180. }
  181. static u64 notrace ingenic_ost_clocksource_read(struct clocksource *cs)
  182. {
  183. return ingenic_ost_global_timer_read_cntl();
  184. }
  185. static inline struct ingenic_ost *to_ingenic_ost(struct clock_event_device *evt)
  186. {
  187. return container_of(evt, struct ingenic_ost, cevt);
  188. }
  189. static int ingenic_ost_cevt_set_state_shutdown(struct clock_event_device *evt)
  190. {
  191. struct ingenic_ost *ost = to_ingenic_ost(evt);
  192. writel(OSTECR_OST1ENC, ost->base + OST_REG_OSTECR);
  193. return 0;
  194. }
  195. static int ingenic_ost_cevt_set_next(unsigned long next,
  196. struct clock_event_device *evt)
  197. {
  198. struct ingenic_ost *ost = to_ingenic_ost(evt);
  199. writel((u32)~OSTFR_FFLAG, ost->base + OST_REG_OSTFR);
  200. writel(next, ost->base + OST_REG_OST1DFR);
  201. writel(OSTCR_OST1CLR, ost->base + OST_REG_OSTCR);
  202. writel(OSTESR_OST1ENS, ost->base + OST_REG_OSTESR);
  203. writel((u32)~OSTMR_FMASK, ost->base + OST_REG_OSTMR);
  204. return 0;
  205. }
  206. static irqreturn_t ingenic_ost_cevt_cb(int irq, void *dev_id)
  207. {
  208. struct clock_event_device *evt = dev_id;
  209. struct ingenic_ost *ost = to_ingenic_ost(evt);
  210. writel(OSTECR_OST1ENC, ost->base + OST_REG_OSTECR);
  211. if (evt->event_handler)
  212. evt->event_handler(evt);
  213. return IRQ_HANDLED;
  214. }
  215. static int __init ingenic_ost_register_clock(struct ingenic_ost *ost,
  216. unsigned int idx, const struct ingenic_ost_clk_info *info,
  217. struct clk_hw_onecell_data *clocks)
  218. {
  219. struct ingenic_ost_clk *ost_clk;
  220. int val, err;
  221. ost_clk = kzalloc_obj(*ost_clk);
  222. if (!ost_clk)
  223. return -ENOMEM;
  224. ost_clk->hw.init = &info->init_data;
  225. ost_clk->idx = idx;
  226. ost_clk->info = info;
  227. ost_clk->ost = ost;
  228. /* Reset clock divider */
  229. val = readl(ost->base + info->ostccr_reg);
  230. val &= ~(OSTCCR_PRESCALE1_MASK | OSTCCR_PRESCALE2_MASK);
  231. writel(val, ost->base + info->ostccr_reg);
  232. err = clk_hw_register(NULL, &ost_clk->hw);
  233. if (err) {
  234. kfree(ost_clk);
  235. return err;
  236. }
  237. clocks->hws[idx] = &ost_clk->hw;
  238. return 0;
  239. }
  240. static struct clk * __init ingenic_ost_get_clock(struct device_node *np, int id)
  241. {
  242. struct of_phandle_args args;
  243. args.np = np;
  244. args.args_count = 1;
  245. args.args[0] = id;
  246. return of_clk_get_from_provider(&args);
  247. }
  248. static int __init ingenic_ost_percpu_timer_init(struct device_node *np,
  249. struct ingenic_ost *ost)
  250. {
  251. unsigned int timer_virq, channel = OST_CLK_PERCPU_TIMER;
  252. unsigned long rate;
  253. int err;
  254. ost->percpu_timer_clk = ingenic_ost_get_clock(np, channel);
  255. if (IS_ERR(ost->percpu_timer_clk))
  256. return PTR_ERR(ost->percpu_timer_clk);
  257. err = clk_prepare_enable(ost->percpu_timer_clk);
  258. if (err)
  259. goto err_clk_put;
  260. rate = clk_get_rate(ost->percpu_timer_clk);
  261. if (!rate) {
  262. err = -EINVAL;
  263. goto err_clk_disable;
  264. }
  265. timer_virq = of_irq_get(np, 0);
  266. if (!timer_virq) {
  267. err = -EINVAL;
  268. goto err_clk_disable;
  269. }
  270. snprintf(ost->name, sizeof(ost->name), "OST percpu timer");
  271. err = request_irq(timer_virq, ingenic_ost_cevt_cb, IRQF_TIMER,
  272. ost->name, &ost->cevt);
  273. if (err)
  274. goto err_irq_dispose_mapping;
  275. ost->cevt.cpumask = cpumask_of(smp_processor_id());
  276. ost->cevt.features = CLOCK_EVT_FEAT_ONESHOT;
  277. ost->cevt.name = ost->name;
  278. ost->cevt.rating = 400;
  279. ost->cevt.set_state_shutdown = ingenic_ost_cevt_set_state_shutdown;
  280. ost->cevt.set_next_event = ingenic_ost_cevt_set_next;
  281. clockevents_config_and_register(&ost->cevt, rate, 4, 0xffffffff);
  282. return 0;
  283. err_irq_dispose_mapping:
  284. irq_dispose_mapping(timer_virq);
  285. err_clk_disable:
  286. clk_disable_unprepare(ost->percpu_timer_clk);
  287. err_clk_put:
  288. clk_put(ost->percpu_timer_clk);
  289. return err;
  290. }
  291. static int __init ingenic_ost_global_timer_init(struct device_node *np,
  292. struct ingenic_ost *ost)
  293. {
  294. unsigned int channel = OST_CLK_GLOBAL_TIMER;
  295. struct clocksource *cs = &ost->cs;
  296. unsigned long rate;
  297. int err;
  298. ost->global_timer_clk = ingenic_ost_get_clock(np, channel);
  299. if (IS_ERR(ost->global_timer_clk))
  300. return PTR_ERR(ost->global_timer_clk);
  301. err = clk_prepare_enable(ost->global_timer_clk);
  302. if (err)
  303. goto err_clk_put;
  304. rate = clk_get_rate(ost->global_timer_clk);
  305. if (!rate) {
  306. err = -EINVAL;
  307. goto err_clk_disable;
  308. }
  309. /* Clear counter CNT registers */
  310. writel(OSTCR_OST2CLR, ost->base + OST_REG_OSTCR);
  311. /* Enable OST channel */
  312. writel(OSTESR_OST2ENS, ost->base + OST_REG_OSTESR);
  313. cs->name = "ingenic-ost";
  314. cs->rating = 400;
  315. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  316. cs->mask = CLOCKSOURCE_MASK(32);
  317. cs->read = ingenic_ost_clocksource_read;
  318. err = clocksource_register_hz(cs, rate);
  319. if (err)
  320. goto err_clk_disable;
  321. return 0;
  322. err_clk_disable:
  323. clk_disable_unprepare(ost->global_timer_clk);
  324. err_clk_put:
  325. clk_put(ost->global_timer_clk);
  326. return err;
  327. }
  328. static const struct ingenic_soc_info x1000_soc_info = {
  329. .num_channels = 2,
  330. };
  331. static const struct of_device_id __maybe_unused ingenic_ost_of_matches[] __initconst = {
  332. { .compatible = "ingenic,x1000-ost", .data = &x1000_soc_info },
  333. { /* sentinel */ }
  334. };
  335. static int __init ingenic_ost_probe(struct device_node *np)
  336. {
  337. const struct of_device_id *id = of_match_node(ingenic_ost_of_matches, np);
  338. struct ingenic_ost *ost;
  339. unsigned int i;
  340. int ret;
  341. ost = kzalloc_obj(*ost);
  342. if (!ost)
  343. return -ENOMEM;
  344. ost->base = of_io_request_and_map(np, 0, of_node_full_name(np));
  345. if (IS_ERR(ost->base)) {
  346. pr_err("%s: Failed to map OST registers\n", __func__);
  347. ret = PTR_ERR(ost->base);
  348. goto err_free_ost;
  349. }
  350. ost->clk = of_clk_get_by_name(np, "ost");
  351. if (IS_ERR(ost->clk)) {
  352. ret = PTR_ERR(ost->clk);
  353. pr_crit("%s: Cannot get OST clock\n", __func__);
  354. goto err_free_ost;
  355. }
  356. ret = clk_prepare_enable(ost->clk);
  357. if (ret) {
  358. pr_crit("%s: Unable to enable OST clock\n", __func__);
  359. goto err_put_clk;
  360. }
  361. ost->soc_info = id->data;
  362. ost->clocks = kzalloc_flex(*ost->clocks, hws,
  363. ost->soc_info->num_channels);
  364. if (!ost->clocks) {
  365. ret = -ENOMEM;
  366. goto err_clk_disable;
  367. }
  368. ost->clocks->num = ost->soc_info->num_channels;
  369. for (i = 0; i < ost->clocks->num; i++) {
  370. ret = ingenic_ost_register_clock(ost, i, &x1000_ost_clk_info[i], ost->clocks);
  371. if (ret) {
  372. pr_crit("%s: Cannot register clock %d\n", __func__, i);
  373. goto err_unregister_ost_clocks;
  374. }
  375. }
  376. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, ost->clocks);
  377. if (ret) {
  378. pr_crit("%s: Cannot add OF clock provider\n", __func__);
  379. goto err_unregister_ost_clocks;
  380. }
  381. ingenic_ost = ost;
  382. return 0;
  383. err_unregister_ost_clocks:
  384. for (i = 0; i < ost->clocks->num; i++)
  385. if (ost->clocks->hws[i])
  386. clk_hw_unregister(ost->clocks->hws[i]);
  387. kfree(ost->clocks);
  388. err_clk_disable:
  389. clk_disable_unprepare(ost->clk);
  390. err_put_clk:
  391. clk_put(ost->clk);
  392. err_free_ost:
  393. kfree(ost);
  394. return ret;
  395. }
  396. static int __init ingenic_ost_init(struct device_node *np)
  397. {
  398. struct ingenic_ost *ost;
  399. unsigned long rate;
  400. int ret;
  401. ret = ingenic_ost_probe(np);
  402. if (ret) {
  403. pr_crit("%s: Failed to initialize OST clocks: %d\n", __func__, ret);
  404. return ret;
  405. }
  406. of_node_clear_flag(np, OF_POPULATED);
  407. ost = ingenic_ost;
  408. if (IS_ERR(ost))
  409. return PTR_ERR(ost);
  410. ret = ingenic_ost_global_timer_init(np, ost);
  411. if (ret) {
  412. pr_crit("%s: Unable to init global timer: %x\n", __func__, ret);
  413. goto err_free_ingenic_ost;
  414. }
  415. ret = ingenic_ost_percpu_timer_init(np, ost);
  416. if (ret)
  417. goto err_ost_global_timer_cleanup;
  418. /* Register the sched_clock at the end as there's no way to undo it */
  419. rate = clk_get_rate(ost->global_timer_clk);
  420. sched_clock_register(ingenic_ost_global_timer_read_cntl, 32, rate);
  421. return 0;
  422. err_ost_global_timer_cleanup:
  423. clocksource_unregister(&ost->cs);
  424. clk_disable_unprepare(ost->global_timer_clk);
  425. clk_put(ost->global_timer_clk);
  426. err_free_ingenic_ost:
  427. kfree(ost);
  428. return ret;
  429. }
  430. TIMER_OF_DECLARE(x1000_ost, "ingenic,x1000-ost", ingenic_ost_init);