epsoc.c revision 1.14 1 1.14 thorpej /* $NetBSD: epsoc.c,v 1.14 2021/04/24 23:36:26 thorpej Exp $ */
2 1.1 joff
3 1.1 joff /*
4 1.1 joff * Copyright (c) 2004 Jesse Off
5 1.1 joff * All rights reserved.
6 1.1 joff *
7 1.1 joff * Redistribution and use in source and binary forms, with or without
8 1.1 joff * modification, are permitted provided that the following conditions
9 1.1 joff * are met:
10 1.1 joff * 1. Redistributions of source code must retain the above copyright
11 1.1 joff * notice, this list of conditions and the following disclaimer.
12 1.1 joff * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 joff * notice, this list of conditions and the following disclaimer in the
14 1.1 joff * documentation and/or other materials provided with the distribution.
15 1.1 joff *
16 1.1 joff * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 joff * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 joff * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 joff * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 joff * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 joff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 joff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 joff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 joff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 joff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 joff * POSSIBILITY OF SUCH DAMAGE.
27 1.1 joff */
28 1.1 joff
29 1.1 joff #include <sys/cdefs.h>
30 1.14 thorpej __KERNEL_RCSID(0, "$NetBSD: epsoc.c,v 1.14 2021/04/24 23:36:26 thorpej Exp $");
31 1.1 joff
32 1.1 joff #include <sys/types.h>
33 1.1 joff #include <sys/param.h>
34 1.1 joff #include <sys/systm.h>
35 1.1 joff #include <sys/kernel.h>
36 1.1 joff #include <sys/time.h>
37 1.1 joff #include <sys/device.h>
38 1.1 joff
39 1.11 dyoung #include <sys/bus.h>
40 1.1 joff #include <machine/intr.h>
41 1.1 joff
42 1.1 joff #include <arm/cpufunc.h>
43 1.1 joff
44 1.1 joff #include <arm/ep93xx/ep93xxreg.h>
45 1.1 joff #include <arm/ep93xx/ep93xxvar.h>
46 1.1 joff #include <arm/ep93xx/epsocvar.h>
47 1.1 joff
48 1.1 joff #include "locators.h"
49 1.1 joff
50 1.12 chs static int epsoc_match(device_t, cfdata_t, void *);
51 1.12 chs static void epsoc_attach(device_t, device_t, void *);
52 1.12 chs static int epsoc_search(device_t, cfdata_t, const int *, void *);
53 1.1 joff static int epsoc_print(void *, const char *);
54 1.12 chs static int epsoc_submatch(device_t, cfdata_t, const int *, void *);
55 1.1 joff
56 1.12 chs CFATTACH_DECL_NEW(epsoc, sizeof(struct epsoc_softc),
57 1.1 joff epsoc_match, epsoc_attach, NULL, NULL);
58 1.1 joff
59 1.1 joff struct epsoc_softc *epsoc_sc = NULL;
60 1.1 joff
61 1.1 joff static int
62 1.12 chs epsoc_match(device_t parent, cfdata_t match, void *aux)
63 1.1 joff {
64 1.1 joff bus_space_handle_t ioh;
65 1.13 skrll uint32_t id, ret = 0;
66 1.1 joff
67 1.1 joff bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
68 1.1 joff EP93XX_APB_SYSCON_SIZE, 0, &ioh);
69 1.1 joff id = bus_space_read_4(&ep93xx_bs_tag, ioh, EP93XX_SYSCON_ChipID);
70 1.1 joff if ((id & 0x01faffff) == 0x9213) ret = 1;
71 1.1 joff bus_space_unmap(&ep93xx_bs_tag, ioh, EP93XX_APB_SYSCON_SIZE);
72 1.1 joff return ret;
73 1.1 joff }
74 1.1 joff
75 1.1 joff static void
76 1.12 chs epsoc_attach(device_t parent, device_t self, void *aux)
77 1.1 joff {
78 1.1 joff struct epsoc_softc *sc;
79 1.13 skrll uint64_t fclk, pclk, hclk;
80 1.13 skrll uint32_t id, clkset1;
81 1.2 he const char *rev;
82 1.5 hamajima int locs[EPSOCCF_NLOCS];
83 1.5 hamajima struct epsoc_attach_args sa;
84 1.1 joff
85 1.12 chs sc = device_private(self);
86 1.1 joff
87 1.1 joff if (epsoc_sc == NULL)
88 1.1 joff epsoc_sc = sc;
89 1.1 joff
90 1.1 joff sc->sc_iot = &ep93xx_bs_tag;
91 1.1 joff bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
92 1.1 joff EP93XX_APB_SYSCON_SIZE, 0, &sc->sc_ioh);
93 1.1 joff id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EP93XX_SYSCON_ChipID);
94 1.1 joff switch(id >> 28) {
95 1.1 joff case 0:
96 1.1 joff rev = "A";
97 1.1 joff break;
98 1.1 joff case 1:
99 1.1 joff rev = "B";
100 1.1 joff break;
101 1.1 joff case 2:
102 1.1 joff rev = "C";
103 1.1 joff break;
104 1.1 joff case 3:
105 1.1 joff rev = "D0";
106 1.1 joff break;
107 1.1 joff case 4:
108 1.1 joff rev = "D1";
109 1.1 joff break;
110 1.1 joff case 5:
111 1.1 joff rev = "E0";
112 1.1 joff break;
113 1.1 joff case 6:
114 1.1 joff rev = "E1";
115 1.1 joff break;
116 1.1 joff default:
117 1.1 joff rev = ">E1";
118 1.1 joff break;
119 1.1 joff }
120 1.1 joff printf(": Cirrus Logic EP93xx SoC rev %s\n", rev);
121 1.1 joff
122 1.1 joff clkset1 = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
123 1.1 joff EP93XX_SYSCON_ClkSet1);
124 1.1 joff {
125 1.1 joff int fclkdiv, hclkdiv, pclkdiv, pll1_ps;
126 1.1 joff int pll1x1fbd1, pll1x2fbd2, pll1x2ipd;
127 1.1 joff int hclkdivisors[] = {1, 2, 4, 5, 6, 8, 16, 32};
128 1.1 joff
129 1.1 joff fclkdiv = (clkset1 & 0x0e000000) >> 25;
130 1.1 joff hclkdiv = (clkset1 & 0x00700000) >> 20;
131 1.1 joff pclkdiv = (clkset1 & 0x000c0000) >> 18;
132 1.1 joff pll1_ps = (clkset1 & 0x00030000) >> 16;
133 1.1 joff pll1x1fbd1 = (clkset1 & 0x0000f800) >> 11;
134 1.1 joff pll1x2fbd2 = (clkset1 & 0x000007e0) >> 5;
135 1.1 joff pll1x2ipd = (clkset1 & 0x0000001f);
136 1.1 joff fclk = 14745600ULL * ((pll1x1fbd1 + 1) * (pll1x2fbd2 + 1)) /
137 1.1 joff ((pll1x2ipd + 1) * (1 << pll1_ps));
138 1.1 joff hclk = fclk / hclkdivisors[hclkdiv];
139 1.1 joff pclk = hclk >> pclkdiv;
140 1.1 joff if (fclkdiv > 4) fclkdiv = 0;
141 1.1 joff if (clkset1 & 0x00800000) /* nBYP1 bypasses PLL */
142 1.1 joff fclk = fclk >> fclkdiv;
143 1.1 joff else
144 1.1 joff fclk = 14745600ULL;
145 1.1 joff }
146 1.6 lukem printf("%s: fclk %lld.%02lld MHz hclk %lld.%02lld MHz pclk %lld.%02lld MHz\n",
147 1.12 chs device_xname(self),
148 1.1 joff fclk / 1000000, (fclk % 1000000 + 5000) / 10000,
149 1.1 joff hclk / 1000000, (hclk % 1000000 + 5000) / 10000,
150 1.1 joff pclk / 1000000, (pclk % 1000000 + 5000) / 10000);
151 1.1 joff
152 1.1 joff sc->sc_fclk = fclk;
153 1.1 joff sc->sc_hclk = hclk;
154 1.1 joff sc->sc_pclk = pclk;
155 1.1 joff
156 1.1 joff /* get busdma tag for the platform */
157 1.1 joff sc->sc_dmat = ep93xx_bus_dma_init(&ep93xx_bus_dma);
158 1.1 joff
159 1.1 joff /*
160 1.1 joff * Attach each devices
161 1.5 hamajima * some device is used by other system device. so attach first.
162 1.1 joff */
163 1.5 hamajima sa.sa_iot = sc->sc_iot;
164 1.5 hamajima sa.sa_dmat = sc->sc_dmat;
165 1.5 hamajima sa.sa_hclk = sc->sc_hclk;
166 1.5 hamajima sa.sa_pclk = sc->sc_pclk;
167 1.5 hamajima locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_TIMERS;
168 1.14 thorpej config_found(self, &sa, epsoc_print,
169 1.14 thorpej CFARG_SUBMATCH, epsoc_submatch,
170 1.14 thorpej CFARG_LOCATORS, locs,
171 1.14 thorpej CFARG_EOL);
172 1.14 thorpej
173 1.5 hamajima locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_GPIO;
174 1.12 chs sa.sa_gpio = device_private(
175 1.14 thorpej config_found(self, &sa, epsoc_print,
176 1.14 thorpej CFARG_SUBMATCH, epsoc_submatch,
177 1.14 thorpej CFARG_LOCATORS, locs,
178 1.14 thorpej CFARG_EOL));
179 1.14 thorpej
180 1.14 thorpej config_search(self, &sa,
181 1.14 thorpej CFARG_SEARCH, epsoc_search,
182 1.14 thorpej CFARG_EOL);
183 1.1 joff }
184 1.1 joff
185 1.1 joff int
186 1.12 chs epsoc_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
187 1.5 hamajima {
188 1.5 hamajima struct epsoc_attach_args *sa = aux;
189 1.5 hamajima
190 1.5 hamajima if (cf->cf_loc[EPSOCCF_ADDR] == ldesc[EPSOCCF_ADDR]) {
191 1.5 hamajima sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
192 1.5 hamajima sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
193 1.5 hamajima sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
194 1.5 hamajima return (config_match(parent, cf, aux));
195 1.5 hamajima } else
196 1.5 hamajima return (0);
197 1.5 hamajima }
198 1.5 hamajima
199 1.5 hamajima int
200 1.12 chs epsoc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
201 1.1 joff {
202 1.5 hamajima struct epsoc_attach_args *sa = aux;
203 1.1 joff
204 1.5 hamajima sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
205 1.5 hamajima sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
206 1.5 hamajima sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
207 1.1 joff
208 1.14 thorpej if (config_probe(parent, cf, aux))
209 1.14 thorpej config_attach(parent, cf, aux, epsoc_print, CFARG_EOL);
210 1.1 joff
211 1.1 joff return (0);
212 1.1 joff }
213 1.1 joff
214 1.1 joff static int
215 1.9 dsl epsoc_print(void *aux, const char *name)
216 1.1 joff {
217 1.12 chs struct epsoc_attach_args *sa = aux;
218 1.1 joff
219 1.1 joff if (sa->sa_size)
220 1.1 joff aprint_normal(" addr 0x%lx", sa->sa_addr);
221 1.1 joff if (sa->sa_size > 1)
222 1.1 joff aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
223 1.1 joff if (sa->sa_intr > 1)
224 1.1 joff aprint_normal(" intr %d", sa->sa_intr);
225 1.1 joff
226 1.1 joff return (UNCONF);
227 1.1 joff }
228