1 /* $NetBSD: imx_ccm_div.c,v 1.1 2020/12/23 14:42:38 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: imx_ccm_div.c,v 1.1 2020/12/23 14:42:38 skrll Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34
35 #include <dev/clk/clk_backend.h>
36
37 #include <arm/nxp/imx_ccm.h>
38
39 #include <dev/fdt/fdtvar.h>
40
41 u_int
imx_ccm_div_get_rate(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk)42 imx_ccm_div_get_rate(struct imx_ccm_softc *sc,
43 struct imx_ccm_clk *clk)
44 {
45 struct imx_ccm_div *div = &clk->u.div;
46 struct clk *clkp, *clkp_parent;
47
48 KASSERT(clk->type == IMX_CCM_DIV);
49
50 clkp = &clk->base;
51 clkp_parent = clk_get_parent(clkp);
52 if (clkp_parent == NULL)
53 return 0;
54
55 const u_int prate = clk_get_rate(clkp_parent);
56 if (prate == 0)
57 return 0;
58
59 const uint32_t val = CCM_READ(sc, clk->regidx, div->reg);
60 const u_int n = __SHIFTOUT(val, div->mask);
61
62 return prate / (n + 1);
63 }
64
65 int
imx_ccm_div_set_rate(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk,u_int rate)66 imx_ccm_div_set_rate(struct imx_ccm_softc *sc,
67 struct imx_ccm_clk *clk, u_int rate)
68 {
69 struct imx_ccm_div *div = &clk->u.div;
70 struct clk *clkp, *clkp_parent;
71 int best_diff = INT_MAX;
72 u_int n, best_n;
73 uint32_t val;
74
75 KASSERT(clk->type == IMX_CCM_DIV);
76
77 clkp = &clk->base;
78 clkp_parent = clk_get_parent(clkp);
79 if (clkp_parent == NULL)
80 return ENXIO;
81
82 if ((div->flags & IMX_DIV_SET_RATE_PARENT) != 0)
83 return clk_set_rate(clkp, rate);
84
85 const u_int prate = clk_get_rate(clkp_parent);
86 if (prate == 0)
87 return EIO;
88
89 for (n = 0; n < __SHIFTOUT_MASK(div->mask); n++) {
90 const u_int tmp_rate = prate / (n + 1);
91 const int diff = (int)rate - (int)tmp_rate;
92 if ((div->flags & IMX_DIV_ROUND_DOWN) != 0) {
93 if (diff >= 0 && diff < best_diff) {
94 best_n = n;
95 best_diff = diff;
96 }
97 } else {
98 if (abs(diff) < abs(best_diff)) {
99 best_n = n;
100 best_diff = abs(diff);
101 }
102 }
103 }
104 if (best_diff == INT_MAX)
105 return EIO;
106
107 val = CCM_READ(sc, clk->regidx, div->reg);
108 val &= ~div->mask;
109 val |= __SHIFTIN(best_n, div->mask);
110 CCM_WRITE(sc, clk->regidx, div->reg, val);
111
112 return 0;
113 }
114
115 const char *
imx_ccm_div_get_parent(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk)116 imx_ccm_div_get_parent(struct imx_ccm_softc *sc,
117 struct imx_ccm_clk *clk)
118 {
119 struct imx_ccm_div *div = &clk->u.div;
120
121 KASSERT(clk->type == IMX_CCM_DIV);
122
123 return div->parent;
124 }
125