epsoc.c revision 1.7 1 1.7 martin /* $NetBSD: epsoc.c,v 1.7 2008/04/28 20:23:14 martin 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.7 martin __KERNEL_RCSID(0, "$NetBSD: epsoc.c,v 1.7 2008/04/28 20:23:14 martin 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.1 joff #include <machine/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.1 joff static int epsoc_match(struct device *, struct cfdata *, void *);
51 1.1 joff static void epsoc_attach(struct device *, struct device *, void *);
52 1.3 drochner static int epsoc_search(struct device *, struct cfdata *,
53 1.4 drochner const int *, void *);
54 1.1 joff static int epsoc_print(void *, const char *);
55 1.5 hamajima static int epsoc_submatch(struct device *, struct cfdata *,
56 1.5 hamajima const int *, void *);
57 1.1 joff
58 1.1 joff CFATTACH_DECL(epsoc, sizeof(struct epsoc_softc),
59 1.1 joff epsoc_match, epsoc_attach, NULL, NULL);
60 1.1 joff
61 1.1 joff struct epsoc_softc *epsoc_sc = NULL;
62 1.1 joff
63 1.1 joff static int
64 1.1 joff epsoc_match(struct device *parent, struct cfdata *match, void *aux)
65 1.1 joff {
66 1.1 joff bus_space_handle_t ioh;
67 1.1 joff u_int32_t id, ret = 0;
68 1.1 joff
69 1.1 joff bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
70 1.1 joff EP93XX_APB_SYSCON_SIZE, 0, &ioh);
71 1.1 joff id = bus_space_read_4(&ep93xx_bs_tag, ioh, EP93XX_SYSCON_ChipID);
72 1.1 joff if ((id & 0x01faffff) == 0x9213) ret = 1;
73 1.1 joff bus_space_unmap(&ep93xx_bs_tag, ioh, EP93XX_APB_SYSCON_SIZE);
74 1.1 joff return ret;
75 1.1 joff }
76 1.1 joff
77 1.1 joff static void
78 1.1 joff epsoc_attach(struct device *parent, struct device *self, void *aux)
79 1.1 joff {
80 1.1 joff struct epsoc_softc *sc;
81 1.1 joff u_int64_t fclk, pclk, hclk;
82 1.1 joff u_int32_t id, clkset1;
83 1.2 he const char *rev;
84 1.5 hamajima int locs[EPSOCCF_NLOCS];
85 1.5 hamajima struct epsoc_attach_args sa;
86 1.1 joff
87 1.1 joff sc = (struct epsoc_softc*) self;
88 1.1 joff
89 1.1 joff if (epsoc_sc == NULL)
90 1.1 joff epsoc_sc = sc;
91 1.1 joff
92 1.1 joff sc->sc_iot = &ep93xx_bs_tag;
93 1.1 joff bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
94 1.1 joff EP93XX_APB_SYSCON_SIZE, 0, &sc->sc_ioh);
95 1.1 joff id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EP93XX_SYSCON_ChipID);
96 1.1 joff switch(id >> 28) {
97 1.1 joff case 0:
98 1.1 joff rev = "A";
99 1.1 joff break;
100 1.1 joff case 1:
101 1.1 joff rev = "B";
102 1.1 joff break;
103 1.1 joff case 2:
104 1.1 joff rev = "C";
105 1.1 joff break;
106 1.1 joff case 3:
107 1.1 joff rev = "D0";
108 1.1 joff break;
109 1.1 joff case 4:
110 1.1 joff rev = "D1";
111 1.1 joff break;
112 1.1 joff case 5:
113 1.1 joff rev = "E0";
114 1.1 joff break;
115 1.1 joff case 6:
116 1.1 joff rev = "E1";
117 1.1 joff break;
118 1.1 joff default:
119 1.1 joff rev = ">E1";
120 1.1 joff break;
121 1.1 joff }
122 1.1 joff printf(": Cirrus Logic EP93xx SoC rev %s\n", rev);
123 1.1 joff
124 1.1 joff clkset1 = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
125 1.1 joff EP93XX_SYSCON_ClkSet1);
126 1.1 joff {
127 1.1 joff int fclkdiv, hclkdiv, pclkdiv, pll1_ps;
128 1.1 joff int pll1x1fbd1, pll1x2fbd2, pll1x2ipd;
129 1.1 joff int hclkdivisors[] = {1, 2, 4, 5, 6, 8, 16, 32};
130 1.1 joff
131 1.1 joff fclkdiv = (clkset1 & 0x0e000000) >> 25;
132 1.1 joff hclkdiv = (clkset1 & 0x00700000) >> 20;
133 1.1 joff pclkdiv = (clkset1 & 0x000c0000) >> 18;
134 1.1 joff pll1_ps = (clkset1 & 0x00030000) >> 16;
135 1.1 joff pll1x1fbd1 = (clkset1 & 0x0000f800) >> 11;
136 1.1 joff pll1x2fbd2 = (clkset1 & 0x000007e0) >> 5;
137 1.1 joff pll1x2ipd = (clkset1 & 0x0000001f);
138 1.1 joff fclk = 14745600ULL * ((pll1x1fbd1 + 1) * (pll1x2fbd2 + 1)) /
139 1.1 joff ((pll1x2ipd + 1) * (1 << pll1_ps));
140 1.1 joff hclk = fclk / hclkdivisors[hclkdiv];
141 1.1 joff pclk = hclk >> pclkdiv;
142 1.1 joff if (fclkdiv > 4) fclkdiv = 0;
143 1.1 joff if (clkset1 & 0x00800000) /* nBYP1 bypasses PLL */
144 1.1 joff fclk = fclk >> fclkdiv;
145 1.1 joff else
146 1.1 joff fclk = 14745600ULL;
147 1.1 joff }
148 1.6 lukem printf("%s: fclk %lld.%02lld MHz hclk %lld.%02lld MHz pclk %lld.%02lld MHz\n",
149 1.1 joff sc->sc_dev.dv_xname,
150 1.1 joff fclk / 1000000, (fclk % 1000000 + 5000) / 10000,
151 1.1 joff hclk / 1000000, (hclk % 1000000 + 5000) / 10000,
152 1.1 joff pclk / 1000000, (pclk % 1000000 + 5000) / 10000);
153 1.1 joff
154 1.1 joff sc->sc_fclk = fclk;
155 1.1 joff sc->sc_hclk = hclk;
156 1.1 joff sc->sc_pclk = pclk;
157 1.1 joff
158 1.1 joff /* get busdma tag for the platform */
159 1.1 joff sc->sc_dmat = ep93xx_bus_dma_init(&ep93xx_bus_dma);
160 1.1 joff
161 1.1 joff /*
162 1.1 joff * Attach each devices
163 1.5 hamajima * some device is used by other system device. so attach first.
164 1.1 joff */
165 1.5 hamajima sa.sa_iot = sc->sc_iot;
166 1.5 hamajima sa.sa_dmat = sc->sc_dmat;
167 1.5 hamajima sa.sa_hclk = sc->sc_hclk;
168 1.5 hamajima sa.sa_pclk = sc->sc_pclk;
169 1.5 hamajima locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_TIMERS;
170 1.5 hamajima config_found_sm_loc(self, "epsoc", locs, &sa,
171 1.5 hamajima epsoc_print, epsoc_submatch);
172 1.5 hamajima locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_GPIO;
173 1.5 hamajima sa.sa_gpio = (struct epgpio_softc *)
174 1.5 hamajima config_found_sm_loc(self, "epsoc", locs, &sa,
175 1.5 hamajima epsoc_print, epsoc_submatch);
176 1.5 hamajima config_search_ia(epsoc_search, self, "epsoc", &sa);
177 1.1 joff
178 1.1 joff }
179 1.1 joff
180 1.1 joff int
181 1.5 hamajima epsoc_submatch(parent, cf, ldesc, aux)
182 1.5 hamajima struct device *parent;
183 1.5 hamajima struct cfdata *cf;
184 1.5 hamajima const int *ldesc;
185 1.5 hamajima void *aux;
186 1.5 hamajima {
187 1.5 hamajima struct epsoc_attach_args *sa = aux;
188 1.5 hamajima
189 1.5 hamajima if (cf->cf_loc[EPSOCCF_ADDR] == ldesc[EPSOCCF_ADDR]) {
190 1.5 hamajima sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
191 1.5 hamajima sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
192 1.5 hamajima sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
193 1.5 hamajima return (config_match(parent, cf, aux));
194 1.5 hamajima } else
195 1.5 hamajima return (0);
196 1.5 hamajima }
197 1.5 hamajima
198 1.5 hamajima int
199 1.3 drochner epsoc_search(parent, cf, ldesc, aux)
200 1.1 joff struct device *parent;
201 1.1 joff struct cfdata *cf;
202 1.4 drochner const int *ldesc;
203 1.1 joff void *aux;
204 1.1 joff {
205 1.5 hamajima struct epsoc_attach_args *sa = aux;
206 1.1 joff
207 1.5 hamajima sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
208 1.5 hamajima sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
209 1.5 hamajima sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
210 1.1 joff
211 1.5 hamajima if (config_match(parent, cf, aux) > 0)
212 1.5 hamajima config_attach(parent, cf, aux, epsoc_print);
213 1.1 joff
214 1.1 joff return (0);
215 1.1 joff }
216 1.1 joff
217 1.1 joff static int
218 1.1 joff epsoc_print(aux, name)
219 1.1 joff void *aux;
220 1.1 joff const char *name;
221 1.1 joff {
222 1.1 joff struct epsoc_attach_args *sa = (struct epsoc_attach_args*)aux;
223 1.1 joff
224 1.1 joff if (sa->sa_size)
225 1.1 joff aprint_normal(" addr 0x%lx", sa->sa_addr);
226 1.1 joff if (sa->sa_size > 1)
227 1.1 joff aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
228 1.1 joff if (sa->sa_intr > 1)
229 1.1 joff aprint_normal(" intr %d", sa->sa_intr);
230 1.1 joff
231 1.1 joff return (UNCONF);
232 1.1 joff }
233