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