imx6_clk.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: imx6_clk.c,v 1.1.2.2 2021/01/03 16:34:52 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2019 Genetec Corporation. All rights reserved.
5 1.1.2.2 thorpej * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 thorpej * SUCH DAMAGE.
27 1.1.2.2 thorpej */
28 1.1.2.2 thorpej
29 1.1.2.2 thorpej #include <sys/cdefs.h>
30 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: imx6_clk.c,v 1.1.2.2 2021/01/03 16:34:52 thorpej Exp $");
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #include "opt_fdt.h"
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #include <sys/types.h>
35 1.1.2.2 thorpej #include <sys/time.h>
36 1.1.2.2 thorpej #include <sys/bus.h>
37 1.1.2.2 thorpej #include <sys/device.h>
38 1.1.2.2 thorpej #include <sys/sysctl.h>
39 1.1.2.2 thorpej #include <sys/cpufreq.h>
40 1.1.2.2 thorpej #include <sys/malloc.h>
41 1.1.2.2 thorpej #include <sys/kmem.h>
42 1.1.2.2 thorpej #include <sys/param.h>
43 1.1.2.2 thorpej
44 1.1.2.2 thorpej #include <arm/nxp/imx6_ccmvar.h>
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej #include <dev/clk/clk_backend.h>
47 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
48 1.1.2.2 thorpej
49 1.1.2.2 thorpej static struct clk *imx6_clk_decode(device_t, int, const void *, size_t);
50 1.1.2.2 thorpej
51 1.1.2.2 thorpej static const struct fdtbus_clock_controller_func imx6_ccm_fdtclock_funcs = {
52 1.1.2.2 thorpej .decode = imx6_clk_decode
53 1.1.2.2 thorpej };
54 1.1.2.2 thorpej
55 1.1.2.2 thorpej static struct clk *
56 1.1.2.2 thorpej imx6_clk_decode(device_t dev, int cc_phandle, const void *data, size_t len)
57 1.1.2.2 thorpej {
58 1.1.2.2 thorpej struct clk *clk;
59 1.1.2.2 thorpej
60 1.1.2.2 thorpej /* #clock-cells should be 1 */
61 1.1.2.2 thorpej if (len != 4)
62 1.1.2.2 thorpej return NULL;
63 1.1.2.2 thorpej
64 1.1.2.2 thorpej const u_int clock_id = be32dec(data);
65 1.1.2.2 thorpej
66 1.1.2.2 thorpej clk = imx6_get_clock_by_id(clock_id);
67 1.1.2.2 thorpej if (clk)
68 1.1.2.2 thorpej return clk;
69 1.1.2.2 thorpej
70 1.1.2.2 thorpej return NULL;
71 1.1.2.2 thorpej }
72 1.1.2.2 thorpej
73 1.1.2.2 thorpej static void
74 1.1.2.2 thorpej imx6_clk_fixed_from_fdt(const char *name)
75 1.1.2.2 thorpej {
76 1.1.2.2 thorpej struct imx6_clk *iclk = (struct imx6_clk *)imx6_get_clock(name);
77 1.1.2.2 thorpej
78 1.1.2.2 thorpej KASSERT(iclk != NULL);
79 1.1.2.2 thorpej
80 1.1.2.2 thorpej char *path = kmem_asprintf("/clocks/%s", name);
81 1.1.2.2 thorpej int phandle = OF_finddevice(path);
82 1.1.2.2 thorpej kmem_free(path, strlen(path) + 1);
83 1.1.2.2 thorpej
84 1.1.2.2 thorpej if (of_getprop_uint32(phandle, "clock-frequency", &iclk->clk.fixed.rate) != 0)
85 1.1.2.2 thorpej iclk->clk.fixed.rate = 0;
86 1.1.2.2 thorpej }
87 1.1.2.2 thorpej
88 1.1.2.2 thorpej static int imx6ccm_match(device_t, cfdata_t, void *);
89 1.1.2.2 thorpej static void imx6ccm_attach(device_t, device_t, void *);
90 1.1.2.2 thorpej
91 1.1.2.2 thorpej CFATTACH_DECL_NEW(imx6ccm, sizeof(struct imx6ccm_softc),
92 1.1.2.2 thorpej imx6ccm_match, imx6ccm_attach, NULL, NULL);
93 1.1.2.2 thorpej
94 1.1.2.2 thorpej static int
95 1.1.2.2 thorpej imx6ccm_match(device_t parent, cfdata_t cfdata, void *aux)
96 1.1.2.2 thorpej {
97 1.1.2.2 thorpej const char * const compatible[] = { "fsl,imx6q-ccm", NULL };
98 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
99 1.1.2.2 thorpej
100 1.1.2.2 thorpej return of_match_compatible(faa->faa_phandle, compatible);
101 1.1.2.2 thorpej }
102 1.1.2.2 thorpej
103 1.1.2.2 thorpej static void
104 1.1.2.2 thorpej imx6ccm_attach(device_t parent, device_t self, void *aux)
105 1.1.2.2 thorpej {
106 1.1.2.2 thorpej struct imx6ccm_softc * const sc = device_private(self);
107 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
108 1.1.2.2 thorpej bus_addr_t addr;
109 1.1.2.2 thorpej bus_size_t size;
110 1.1.2.2 thorpej
111 1.1.2.2 thorpej if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
112 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
113 1.1.2.2 thorpej return;
114 1.1.2.2 thorpej }
115 1.1.2.2 thorpej
116 1.1.2.2 thorpej sc->sc_dev = self;
117 1.1.2.2 thorpej sc->sc_iot = faa->faa_bst;
118 1.1.2.2 thorpej
119 1.1.2.2 thorpej if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
120 1.1.2.2 thorpej aprint_error(": can't map ccm registers\n");
121 1.1.2.2 thorpej return;
122 1.1.2.2 thorpej }
123 1.1.2.2 thorpej
124 1.1.2.2 thorpej int phandle = OF_finddevice("/soc/aips-bus/anatop");
125 1.1.2.2 thorpej fdtbus_get_reg(phandle, 0, &addr, &size);
126 1.1.2.2 thorpej
127 1.1.2.2 thorpej if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh_analog)) {
128 1.1.2.2 thorpej aprint_error(": can't map anatop registers\n");
129 1.1.2.2 thorpej return;
130 1.1.2.2 thorpej }
131 1.1.2.2 thorpej
132 1.1.2.2 thorpej imx6ccm_attach_common(self);
133 1.1.2.2 thorpej
134 1.1.2.2 thorpej aprint_naive("\n");
135 1.1.2.2 thorpej aprint_normal(": Clock Control Module\n");
136 1.1.2.2 thorpej
137 1.1.2.2 thorpej imx6_clk_fixed_from_fdt("ckil");
138 1.1.2.2 thorpej imx6_clk_fixed_from_fdt("ckih");
139 1.1.2.2 thorpej imx6_clk_fixed_from_fdt("osc");
140 1.1.2.2 thorpej imx6_clk_fixed_from_fdt("anaclk1");
141 1.1.2.2 thorpej imx6_clk_fixed_from_fdt("anaclk2");
142 1.1.2.2 thorpej
143 1.1.2.2 thorpej fdtbus_register_clock_controller(self, faa->faa_phandle,
144 1.1.2.2 thorpej &imx6_ccm_fdtclock_funcs);
145 1.1.2.2 thorpej }
146 1.1.2.2 thorpej
147