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