ingenic-sysost.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ingenic XBurst SoCs SYSOST clocks driver
  4. * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <[email protected]>
  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 long ingenic_ost_round_rate(struct clk_hw *hw, unsigned long req_rate,
  103. unsigned long *parent_rate)
  104. {
  105. unsigned long rate = *parent_rate;
  106. u8 prescale;
  107. if (req_rate > rate)
  108. return rate;
  109. prescale = ingenic_ost_get_prescale(rate, req_rate);
  110. return rate >> (prescale * 2);
  111. }
  112. static int ingenic_ost_percpu_timer_set_rate(struct clk_hw *hw, unsigned long req_rate,
  113. unsigned long parent_rate)
  114. {
  115. struct ingenic_ost_clk *ost_clk = to_ost_clk(hw);
  116. const struct ingenic_ost_clk_info *info = ost_clk->info;
  117. u8 prescale = ingenic_ost_get_prescale(parent_rate, req_rate);
  118. int val;
  119. val = readl(ost_clk->ost->base + info->ostccr_reg);
  120. val &= ~OSTCCR_PRESCALE1_MASK;
  121. val |= FIELD_PREP(OSTCCR_PRESCALE1_MASK, prescale);
  122. writel(val, ost_clk->ost->base + info->ostccr_reg);
  123. return 0;
  124. }
  125. static int ingenic_ost_global_timer_set_rate(struct clk_hw *hw, unsigned long req_rate,
  126. unsigned long parent_rate)
  127. {
  128. struct ingenic_ost_clk *ost_clk = to_ost_clk(hw);
  129. const struct ingenic_ost_clk_info *info = ost_clk->info;
  130. u8 prescale = ingenic_ost_get_prescale(parent_rate, req_rate);
  131. int val;
  132. val = readl(ost_clk->ost->base + info->ostccr_reg);
  133. val &= ~OSTCCR_PRESCALE2_MASK;
  134. val |= FIELD_PREP(OSTCCR_PRESCALE2_MASK, prescale);
  135. writel(val, ost_clk->ost->base + info->ostccr_reg);
  136. return 0;
  137. }
  138. static const struct clk_ops ingenic_ost_percpu_timer_ops = {
  139. .recalc_rate = ingenic_ost_percpu_timer_recalc_rate,
  140. .round_rate = ingenic_ost_round_rate,
  141. .set_rate = ingenic_ost_percpu_timer_set_rate,
  142. };
  143. static const struct clk_ops ingenic_ost_global_timer_ops = {
  144. .recalc_rate = ingenic_ost_global_timer_recalc_rate,
  145. .round_rate = ingenic_ost_round_rate,
  146. .set_rate = ingenic_ost_global_timer_set_rate,
  147. };
  148. static const char * const ingenic_ost_clk_parents[] = { "ext" };
  149. static const struct ingenic_ost_clk_info x1000_ost_clk_info[] = {
  150. [OST_CLK_PERCPU_TIMER] = {
  151. .init_data = {
  152. .name = "percpu timer",
  153. .parent_names = ingenic_ost_clk_parents,
  154. .num_parents = ARRAY_SIZE(ingenic_ost_clk_parents),
  155. .ops = &ingenic_ost_percpu_timer_ops,
  156. .flags = CLK_SET_RATE_UNGATE,
  157. },
  158. .ostccr_reg = OST_REG_OSTCCR,
  159. },
  160. [OST_CLK_GLOBAL_TIMER] = {
  161. .init_data = {
  162. .name = "global timer",
  163. .parent_names = ingenic_ost_clk_parents,
  164. .num_parents = ARRAY_SIZE(ingenic_ost_clk_parents),
  165. .ops = &ingenic_ost_global_timer_ops,
  166. .flags = CLK_SET_RATE_UNGATE,
  167. },
  168. .ostccr_reg = OST_REG_OSTCCR,
  169. },
  170. };
  171. static u64 notrace ingenic_ost_global_timer_read_cntl(void)
  172. {
  173. struct ingenic_ost *ost = ingenic_ost;
  174. unsigned int count;
  175. count = readl(ost->base + OST_REG_OST2CNTL);
  176. return count;
  177. }
  178. static u64 notrace ingenic_ost_clocksource_read(struct clocksource *cs)
  179. {
  180. return ingenic_ost_global_timer_read_cntl();
  181. }
  182. static inline struct ingenic_ost *to_ingenic_ost(struct clock_event_device *evt)
  183. {
  184. return container_of(evt, struct ingenic_ost, cevt);
  185. }
  186. static int ingenic_ost_cevt_set_state_shutdown(struct clock_event_device *evt)
  187. {
  188. struct ingenic_ost *ost = to_ingenic_ost(evt);
  189. writel(OSTECR_OST1ENC, ost->base + OST_REG_OSTECR);
  190. return 0;
  191. }
  192. static int ingenic_ost_cevt_set_next(unsigned long next,
  193. struct clock_event_device *evt)
  194. {
  195. struct ingenic_ost *ost = to_ingenic_ost(evt);
  196. writel((u32)~OSTFR_FFLAG, ost->base + OST_REG_OSTFR);
  197. writel(next, ost->base + OST_REG_OST1DFR);
  198. writel(OSTCR_OST1CLR, ost->base + OST_REG_OSTCR);
  199. writel(OSTESR_OST1ENS, ost->base + OST_REG_OSTESR);
  200. writel((u32)~OSTMR_FMASK, ost->base + OST_REG_OSTMR);
  201. return 0;
  202. }
  203. static irqreturn_t ingenic_ost_cevt_cb(int irq, void *dev_id)
  204. {
  205. struct clock_event_device *evt = dev_id;
  206. struct ingenic_ost *ost = to_ingenic_ost(evt);
  207. writel(OSTECR_OST1ENC, ost->base + OST_REG_OSTECR);
  208. if (evt->event_handler)
  209. evt->event_handler(evt);
  210. return IRQ_HANDLED;
  211. }
  212. static int __init ingenic_ost_register_clock(struct ingenic_ost *ost,
  213. unsigned int idx, const struct ingenic_ost_clk_info *info,
  214. struct clk_hw_onecell_data *clocks)
  215. {
  216. struct ingenic_ost_clk *ost_clk;
  217. int val, err;
  218. ost_clk = kzalloc(sizeof(*ost_clk), GFP_KERNEL);
  219. if (!ost_clk)
  220. return -ENOMEM;
  221. ost_clk->hw.init = &info->init_data;
  222. ost_clk->idx = idx;
  223. ost_clk->info = info;
  224. ost_clk->ost = ost;
  225. /* Reset clock divider */
  226. val = readl(ost->base + info->ostccr_reg);
  227. val &= ~(OSTCCR_PRESCALE1_MASK | OSTCCR_PRESCALE2_MASK);
  228. writel(val, ost->base + info->ostccr_reg);
  229. err = clk_hw_register(NULL, &ost_clk->hw);
  230. if (err) {
  231. kfree(ost_clk);
  232. return err;
  233. }
  234. clocks->hws[idx] = &ost_clk->hw;
  235. return 0;
  236. }
  237. static struct clk * __init ingenic_ost_get_clock(struct device_node *np, int id)
  238. {
  239. struct of_phandle_args args;
  240. args.np = np;
  241. args.args_count = 1;
  242. args.args[0] = id;
  243. return of_clk_get_from_provider(&args);
  244. }
  245. static int __init ingenic_ost_percpu_timer_init(struct device_node *np,
  246. struct ingenic_ost *ost)
  247. {
  248. unsigned int timer_virq, channel = OST_CLK_PERCPU_TIMER;
  249. unsigned long rate;
  250. int err;
  251. ost->percpu_timer_clk = ingenic_ost_get_clock(np, channel);
  252. if (IS_ERR(ost->percpu_timer_clk))
  253. return PTR_ERR(ost->percpu_timer_clk);
  254. err = clk_prepare_enable(ost->percpu_timer_clk);
  255. if (err)
  256. goto err_clk_put;
  257. rate = clk_get_rate(ost->percpu_timer_clk);
  258. if (!rate) {
  259. err = -EINVAL;
  260. goto err_clk_disable;
  261. }
  262. timer_virq = of_irq_get(np, 0);
  263. if (!timer_virq) {
  264. err = -EINVAL;
  265. goto err_clk_disable;
  266. }
  267. snprintf(ost->name, sizeof(ost->name), "OST percpu timer");
  268. err = request_irq(timer_virq, ingenic_ost_cevt_cb, IRQF_TIMER,
  269. ost->name, &ost->cevt);
  270. if (err)
  271. goto err_irq_dispose_mapping;
  272. ost->cevt.cpumask = cpumask_of(smp_processor_id());
  273. ost->cevt.features = CLOCK_EVT_FEAT_ONESHOT;
  274. ost->cevt.name = ost->name;
  275. ost->cevt.rating = 400;
  276. ost->cevt.set_state_shutdown = ingenic_ost_cevt_set_state_shutdown;
  277. ost->cevt.set_next_event = ingenic_ost_cevt_set_next;
  278. clockevents_config_and_register(&ost->cevt, rate, 4, 0xffffffff);
  279. return 0;
  280. err_irq_dispose_mapping:
  281. irq_dispose_mapping(timer_virq);
  282. err_clk_disable:
  283. clk_disable_unprepare(ost->percpu_timer_clk);
  284. err_clk_put:
  285. clk_put(ost->percpu_timer_clk);
  286. return err;
  287. }
  288. static int __init ingenic_ost_global_timer_init(struct device_node *np,
  289. struct ingenic_ost *ost)
  290. {
  291. unsigned int channel = OST_CLK_GLOBAL_TIMER;
  292. struct clocksource *cs = &ost->cs;
  293. unsigned long rate;
  294. int err;
  295. ost->global_timer_clk = ingenic_ost_get_clock(np, channel);
  296. if (IS_ERR(ost->global_timer_clk))
  297. return PTR_ERR(ost->global_timer_clk);
  298. err = clk_prepare_enable(ost->global_timer_clk);
  299. if (err)
  300. goto err_clk_put;
  301. rate = clk_get_rate(ost->global_timer_clk);
  302. if (!rate) {
  303. err = -EINVAL;
  304. goto err_clk_disable;
  305. }
  306. /* Clear counter CNT registers */
  307. writel(OSTCR_OST2CLR, ost->base + OST_REG_OSTCR);
  308. /* Enable OST channel */
  309. writel(OSTESR_OST2ENS, ost->base + OST_REG_OSTESR);
  310. cs->name = "ingenic-ost";
  311. cs->rating = 400;
  312. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  313. cs->mask = CLOCKSOURCE_MASK(32);
  314. cs->read = ingenic_ost_clocksource_read;
  315. err = clocksource_register_hz(cs, rate);
  316. if (err)
  317. goto err_clk_disable;
  318. return 0;
  319. err_clk_disable:
  320. clk_disable_unprepare(ost->global_timer_clk);
  321. err_clk_put:
  322. clk_put(ost->global_timer_clk);
  323. return err;
  324. }
  325. static const struct ingenic_soc_info x1000_soc_info = {
  326. .num_channels = 2,
  327. };
  328. static const struct of_device_id __maybe_unused ingenic_ost_of_matches[] __initconst = {
  329. { .compatible = "ingenic,x1000-ost", .data = &x1000_soc_info },
  330. { /* sentinel */ }
  331. };
  332. static int __init ingenic_ost_probe(struct device_node *np)
  333. {
  334. const struct of_device_id *id = of_match_node(ingenic_ost_of_matches, np);
  335. struct ingenic_ost *ost;
  336. unsigned int i;
  337. int ret;
  338. ost = kzalloc(sizeof(*ost), GFP_KERNEL);
  339. if (!ost)
  340. return -ENOMEM;
  341. ost->base = of_io_request_and_map(np, 0, of_node_full_name(np));
  342. if (IS_ERR(ost->base)) {
  343. pr_err("%s: Failed to map OST registers\n", __func__);
  344. ret = PTR_ERR(ost->base);
  345. goto err_free_ost;
  346. }
  347. ost->clk = of_clk_get_by_name(np, "ost");
  348. if (IS_ERR(ost->clk)) {
  349. ret = PTR_ERR(ost->clk);
  350. pr_crit("%s: Cannot get OST clock\n", __func__);
  351. goto err_free_ost;
  352. }
  353. ret = clk_prepare_enable(ost->clk);
  354. if (ret) {
  355. pr_crit("%s: Unable to enable OST clock\n", __func__);
  356. goto err_put_clk;
  357. }
  358. ost->soc_info = id->data;
  359. ost->clocks = kzalloc(struct_size(ost->clocks, hws, ost->soc_info->num_channels),
  360. GFP_KERNEL);
  361. if (!ost->clocks) {
  362. ret = -ENOMEM;
  363. goto err_clk_disable;
  364. }
  365. ost->clocks->num = ost->soc_info->num_channels;
  366. for (i = 0; i < ost->clocks->num; i++) {
  367. ret = ingenic_ost_register_clock(ost, i, &x1000_ost_clk_info[i], ost->clocks);
  368. if (ret) {
  369. pr_crit("%s: Cannot register clock %d\n", __func__, i);
  370. goto err_unregister_ost_clocks;
  371. }
  372. }
  373. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, ost->clocks);
  374. if (ret) {
  375. pr_crit("%s: Cannot add OF clock provider\n", __func__);
  376. goto err_unregister_ost_clocks;
  377. }
  378. ingenic_ost = ost;
  379. return 0;
  380. err_unregister_ost_clocks:
  381. for (i = 0; i < ost->clocks->num; i++)
  382. if (ost->clocks->hws[i])
  383. clk_hw_unregister(ost->clocks->hws[i]);
  384. kfree(ost->clocks);
  385. err_clk_disable:
  386. clk_disable_unprepare(ost->clk);
  387. err_put_clk:
  388. clk_put(ost->clk);
  389. err_free_ost:
  390. kfree(ost);
  391. return ret;
  392. }
  393. static int __init ingenic_ost_init(struct device_node *np)
  394. {
  395. struct ingenic_ost *ost;
  396. unsigned long rate;
  397. int ret;
  398. ret = ingenic_ost_probe(np);
  399. if (ret) {
  400. pr_crit("%s: Failed to initialize OST clocks: %d\n", __func__, ret);
  401. return ret;
  402. }
  403. of_node_clear_flag(np, OF_POPULATED);
  404. ost = ingenic_ost;
  405. if (IS_ERR(ost))
  406. return PTR_ERR(ost);
  407. ret = ingenic_ost_global_timer_init(np, ost);
  408. if (ret) {
  409. pr_crit("%s: Unable to init global timer: %x\n", __func__, ret);
  410. goto err_free_ingenic_ost;
  411. }
  412. ret = ingenic_ost_percpu_timer_init(np, ost);
  413. if (ret)
  414. goto err_ost_global_timer_cleanup;
  415. /* Register the sched_clock at the end as there's no way to undo it */
  416. rate = clk_get_rate(ost->global_timer_clk);
  417. sched_clock_register(ingenic_ost_global_timer_read_cntl, 32, rate);
  418. return 0;
  419. err_ost_global_timer_cleanup:
  420. clocksource_unregister(&ost->cs);
  421. clk_disable_unprepare(ost->global_timer_clk);
  422. clk_put(ost->global_timer_clk);
  423. err_free_ingenic_ost:
  424. kfree(ost);
  425. return ret;
  426. }
  427. TIMER_OF_DECLARE(x1000_ost, "ingenic,x1000-ost", ingenic_ost_init);