s3c2440.c revision 1.1.6.2 1 1.1.6.2 yamt /*-
2 1.1.6.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.1.6.2 yamt * All rights reserved.
4 1.1.6.2 yamt *
5 1.1.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
6 1.1.6.2 yamt * by Paul Fleischer <paul (at) xpg.dk>
7 1.1.6.2 yamt *
8 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.6.2 yamt * modification, are permitted provided that the following conditions
10 1.1.6.2 yamt * are met:
11 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.6.2 yamt *
17 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
28 1.1.6.2 yamt */
29 1.1.6.2 yamt
30 1.1.6.2 yamt /* Derived from s3c2410.c */
31 1.1.6.2 yamt /*
32 1.1.6.2 yamt * Copyright (c) 2003, 2005 Genetec corporation. All rights reserved.
33 1.1.6.2 yamt * Written by Hiroyuki Bessho for Genetec corporation.
34 1.1.6.2 yamt *
35 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
36 1.1.6.2 yamt * modification, are permitted provided that the following conditions
37 1.1.6.2 yamt * are met:
38 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
39 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
40 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
41 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
42 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
43 1.1.6.2 yamt * 3. The name of Genetec corporation may not be used to endorse
44 1.1.6.2 yamt * or promote products derived from this software without specific prior
45 1.1.6.2 yamt * written permission.
46 1.1.6.2 yamt *
47 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
48 1.1.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49 1.1.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP.
51 1.1.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 1.1.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 1.1.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 1.1.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
58 1.1.6.2 yamt */
59 1.1.6.2 yamt
60 1.1.6.2 yamt #include <sys/cdefs.h>
61 1.1.6.2 yamt __KERNEL_RCSID(0, "$NetBSD: s3c2440.c,v 1.1.6.2 2012/04/17 00:06:06 yamt Exp $");
62 1.1.6.2 yamt
63 1.1.6.2 yamt #include <sys/param.h>
64 1.1.6.2 yamt #include <sys/systm.h>
65 1.1.6.2 yamt #include <sys/device.h>
66 1.1.6.2 yamt #include <sys/kernel.h>
67 1.1.6.2 yamt #include <sys/reboot.h>
68 1.1.6.2 yamt
69 1.1.6.2 yamt #include <machine/cpu.h>
70 1.1.6.2 yamt #include <sys/bus.h>
71 1.1.6.2 yamt
72 1.1.6.2 yamt #include <arm/cpufunc.h>
73 1.1.6.2 yamt #include <arm/mainbus/mainbus.h>
74 1.1.6.2 yamt #include <arm/s3c2xx0/s3c2440reg.h>
75 1.1.6.2 yamt #include <arm/s3c2xx0/s3c2440var.h>
76 1.1.6.2 yamt #include <arm/s3c2xx0/s3c2440_dma.h>
77 1.1.6.2 yamt
78 1.1.6.2 yamt #include "locators.h"
79 1.1.6.2 yamt #include "opt_cpuoptions.h"
80 1.1.6.2 yamt
81 1.1.6.2 yamt /* prototypes */
82 1.1.6.2 yamt static int s3c2440_match(struct device *, struct cfdata *, void *);
83 1.1.6.2 yamt static void s3c2440_attach(struct device *, struct device *, void *);
84 1.1.6.2 yamt static int s3c2440_search(struct device *, struct cfdata *,
85 1.1.6.2 yamt const int *, void *);
86 1.1.6.2 yamt
87 1.1.6.2 yamt /* attach structures */
88 1.1.6.2 yamt CFATTACH_DECL_NEW(ssio, sizeof(struct s3c24x0_softc), s3c2440_match, s3c2440_attach,
89 1.1.6.2 yamt NULL, NULL);
90 1.1.6.2 yamt
91 1.1.6.2 yamt extern struct bus_space s3c2xx0_bs_tag;
92 1.1.6.2 yamt
93 1.1.6.2 yamt struct s3c2xx0_softc *s3c2xx0_softc;
94 1.1.6.2 yamt
95 1.1.6.2 yamt #ifdef DEBUG_PORTF
96 1.1.6.2 yamt volatile uint8_t *portf; /* for debug */
97 1.1.6.2 yamt #endif
98 1.1.6.2 yamt
99 1.1.6.2 yamt static int
100 1.1.6.2 yamt s3c2440_print(void *aux, const char *name)
101 1.1.6.2 yamt {
102 1.1.6.2 yamt struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args *) aux;
103 1.1.6.2 yamt
104 1.1.6.2 yamt if (sa->sa_size)
105 1.1.6.2 yamt aprint_normal(" addr 0x%lx", sa->sa_addr);
106 1.1.6.2 yamt if (sa->sa_size > 1)
107 1.1.6.2 yamt aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
108 1.1.6.2 yamt if (sa->sa_intr != SSIOCF_INTR_DEFAULT)
109 1.1.6.2 yamt aprint_normal(" intr %d", sa->sa_intr);
110 1.1.6.2 yamt if (sa->sa_index != SSIOCF_INDEX_DEFAULT)
111 1.1.6.2 yamt aprint_normal(" unit %d", sa->sa_index);
112 1.1.6.2 yamt
113 1.1.6.2 yamt return (UNCONF);
114 1.1.6.2 yamt }
115 1.1.6.2 yamt
116 1.1.6.2 yamt int
117 1.1.6.2 yamt s3c2440_match(struct device *parent, struct cfdata *match, void *aux)
118 1.1.6.2 yamt {
119 1.1.6.2 yamt return 1;
120 1.1.6.2 yamt }
121 1.1.6.2 yamt
122 1.1.6.2 yamt void
123 1.1.6.2 yamt s3c2440_attach(struct device *parent, struct device *self, void *aux)
124 1.1.6.2 yamt {
125 1.1.6.2 yamt struct s3c24x0_softc *sc = device_private(self);
126 1.1.6.2 yamt bus_space_tag_t iot;
127 1.1.6.2 yamt const char *which_registers; /* for panic message */
128 1.1.6.2 yamt
129 1.1.6.2 yamt #define FAIL(which) do { \
130 1.1.6.2 yamt which_registers=(which); goto abort; }while(/*CONSTCOND*/0)
131 1.1.6.2 yamt
132 1.1.6.2 yamt s3c2xx0_softc = &(sc->sc_sx);
133 1.1.6.2 yamt sc->sc_sx.sc_iot = iot = &s3c2xx0_bs_tag;
134 1.1.6.2 yamt
135 1.1.6.2 yamt if (bus_space_map(iot,
136 1.1.6.2 yamt S3C2440_INTCTL_BASE, S3C2440_INTCTL_SIZE,
137 1.1.6.2 yamt BUS_SPACE_MAP_LINEAR, &sc->sc_sx.sc_intctl_ioh))
138 1.1.6.2 yamt FAIL("intc");
139 1.1.6.2 yamt /* tell register addresses to interrupt handler */
140 1.1.6.2 yamt s3c2440_intr_init(sc);
141 1.1.6.2 yamt
142 1.1.6.2 yamt /* Map the GPIO registers */
143 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_GPIO_BASE, S3C2440_GPIO_SIZE,
144 1.1.6.2 yamt 0, &sc->sc_sx.sc_gpio_ioh))
145 1.1.6.2 yamt FAIL("GPIO");
146 1.1.6.2 yamt #ifdef DEBUG_PORTF
147 1.1.6.2 yamt {
148 1.1.6.2 yamt extern volatile uint8_t *portf;
149 1.1.6.2 yamt /* make all ports output */
150 1.1.6.2 yamt bus_space_write_2(iot, sc->sc_sx.sc_gpio_ioh, GPIO_PCONF, 0x5555);
151 1.1.6.2 yamt portf = (volatile uint8_t *)
152 1.1.6.2 yamt ((char *)bus_space_vaddr(iot, sc->sc_sx.sc_gpio_ioh) + GPIO_PDATF);
153 1.1.6.2 yamt }
154 1.1.6.2 yamt #endif
155 1.1.6.2 yamt
156 1.1.6.2 yamt #if 1
157 1.1.6.2 yamt /* Map the DMA controller registers */
158 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_DMAC_BASE, S3C2440_DMAC_SIZE,
159 1.1.6.2 yamt 0, &sc->sc_sx.sc_dmach))
160 1.1.6.2 yamt FAIL("DMAC");
161 1.1.6.2 yamt #endif
162 1.1.6.2 yamt
163 1.1.6.2 yamt /* Memory controller */
164 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_MEMCTL_BASE,
165 1.1.6.2 yamt S3C24X0_MEMCTL_SIZE, 0, &sc->sc_sx.sc_memctl_ioh))
166 1.1.6.2 yamt FAIL("MEMC");
167 1.1.6.2 yamt /* Clock manager */
168 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_CLKMAN_BASE,
169 1.1.6.2 yamt S3C24X0_CLKMAN_SIZE, 0, &sc->sc_sx.sc_clkman_ioh))
170 1.1.6.2 yamt FAIL("CLK");
171 1.1.6.2 yamt
172 1.1.6.2 yamt #if 0
173 1.1.6.2 yamt /* Real time clock */
174 1.1.6.2 yamt if (bus_space_map(iot, S3C2410_RTC_BASE,
175 1.1.6.2 yamt S3C24X0_RTC_SIZE, 0, &sc->sc_sx.sc_rtc_ioh))
176 1.1.6.2 yamt FAIL("RTC");
177 1.1.6.2 yamt #endif
178 1.1.6.2 yamt
179 1.1.6.2 yamt if (bus_space_map(iot, S3C2440_TIMER_BASE,
180 1.1.6.2 yamt S3C24X0_TIMER_SIZE, 0, &sc->sc_timer_ioh)) {
181 1.1.6.2 yamt FAIL("TIMER");
182 1.1.6.2 yamt }
183 1.1.6.2 yamt
184 1.1.6.2 yamt /* calculate current clock frequency */
185 1.1.6.2 yamt s3c24x0_clock_freq(&sc->sc_sx);
186 1.1.6.2 yamt aprint_normal(": fclk %d MHz hclk %d MHz pclk %d MHz\n",
187 1.1.6.2 yamt sc->sc_sx.sc_fclk / 1000000, sc->sc_sx.sc_hclk / 1000000,
188 1.1.6.2 yamt sc->sc_sx.sc_pclk / 1000000);
189 1.1.6.2 yamt
190 1.1.6.2 yamt aprint_naive("\n");
191 1.1.6.2 yamt
192 1.1.6.2 yamt /* get busdma tag for the platform */
193 1.1.6.2 yamt sc->sc_sx.sc_dmat = s3c2xx0_bus_dma_init(&s3c2xx0_bus_dma);
194 1.1.6.2 yamt
195 1.1.6.2 yamt s3c2440_dma_init();
196 1.1.6.2 yamt
197 1.1.6.2 yamt /*
198 1.1.6.2 yamt * Attach devices.
199 1.1.6.2 yamt */
200 1.1.6.2 yamt config_search_ia(s3c2440_search, self, "ssio", NULL);
201 1.1.6.2 yamt return;
202 1.1.6.2 yamt
203 1.1.6.2 yamt abort:
204 1.1.6.2 yamt panic("%s: unable to map %s registers",
205 1.1.6.2 yamt self->dv_xname, which_registers);
206 1.1.6.2 yamt
207 1.1.6.2 yamt #undef FAIL
208 1.1.6.2 yamt }
209 1.1.6.2 yamt
210 1.1.6.2 yamt int
211 1.1.6.2 yamt s3c2440_search(struct device * parent, struct cfdata * cf,
212 1.1.6.2 yamt const int *ldesc, void *aux)
213 1.1.6.2 yamt {
214 1.1.6.2 yamt struct s3c24x0_softc *sc = device_private(parent);
215 1.1.6.2 yamt struct s3c2xx0_attach_args aa;
216 1.1.6.2 yamt
217 1.1.6.2 yamt aa.sa_sc = sc;
218 1.1.6.2 yamt aa.sa_iot = sc->sc_sx.sc_iot;
219 1.1.6.2 yamt aa.sa_addr = cf->cf_loc[SSIOCF_ADDR];
220 1.1.6.2 yamt aa.sa_size = cf->cf_loc[SSIOCF_SIZE];
221 1.1.6.2 yamt aa.sa_index = cf->cf_loc[SSIOCF_INDEX];
222 1.1.6.2 yamt aa.sa_intr = cf->cf_loc[SSIOCF_INTR];
223 1.1.6.2 yamt
224 1.1.6.2 yamt aa.sa_dmat = sc->sc_sx.sc_dmat;
225 1.1.6.2 yamt
226 1.1.6.2 yamt if (config_match(parent, cf, &aa))
227 1.1.6.2 yamt config_attach(parent, cf, &aa, s3c2440_print);
228 1.1.6.2 yamt
229 1.1.6.2 yamt return 0;
230 1.1.6.2 yamt }
231 1.1.6.2 yamt
232 1.1.6.2 yamt /*
233 1.1.6.2 yamt * fill sc_pclk, sc_hclk, sc_fclk from values of clock controller register.
234 1.1.6.2 yamt *
235 1.1.6.2 yamt * s3c24x0_clock_freq2() is meant to be called from kernel startup routines.
236 1.1.6.2 yamt * s3c24x0_clock_freq() is for after kernel initialization is done.
237 1.1.6.2 yamt */
238 1.1.6.2 yamt void
239 1.1.6.2 yamt s3c24x0_clock_freq2(vaddr_t clkman_base, int *fclk, int *hclk, int *pclk)
240 1.1.6.2 yamt {
241 1.1.6.2 yamt uint32_t pllcon, divn, camdivn;
242 1.1.6.2 yamt int mdiv, pdiv, sdiv;
243 1.1.6.2 yamt uint32_t f, h, p;
244 1.1.6.2 yamt
245 1.1.6.2 yamt pllcon = *(volatile uint32_t *)(clkman_base + CLKMAN_MPLLCON);
246 1.1.6.2 yamt divn = *(volatile uint32_t *)(clkman_base + CLKMAN_CLKDIVN);
247 1.1.6.2 yamt camdivn = *(volatile uint32_t *)(clkman_base + CLKMAN_CAMDIVN);
248 1.1.6.2 yamt
249 1.1.6.2 yamt mdiv = (pllcon & PLLCON_MDIV_MASK) >> PLLCON_MDIV_SHIFT;
250 1.1.6.2 yamt pdiv = (pllcon & PLLCON_PDIV_MASK) >> PLLCON_PDIV_SHIFT;
251 1.1.6.2 yamt sdiv = (pllcon & PLLCON_SDIV_MASK) >> PLLCON_SDIV_SHIFT;
252 1.1.6.2 yamt
253 1.1.6.2 yamt f = ((mdiv + 8) * S3C2XX0_XTAL_CLK) / ((pdiv + 2) * (1 << sdiv)) * 2;
254 1.1.6.2 yamt h = f;
255 1.1.6.2 yamt
256 1.1.6.2 yamt /* HDIVN of CLKDIVN can have 4 distinct values */
257 1.1.6.2 yamt switch( (divn & CLKDIVN_HDIVN_MASK) >> CLKDIVN_HDIVN_SHIFT )
258 1.1.6.2 yamt {
259 1.1.6.2 yamt case 0:
260 1.1.6.2 yamt /* 00b: HCLK = FCLK/1*/
261 1.1.6.2 yamt break;
262 1.1.6.2 yamt case 1:
263 1.1.6.2 yamt /* 01b: HCLK = FCLK/2*/
264 1.1.6.2 yamt h /= 2;
265 1.1.6.2 yamt break;
266 1.1.6.2 yamt case 2:
267 1.1.6.2 yamt /* 10b: HCLK = FCLK/4 when CAMDIVN[9] (HCLK4_HALF) = 0
268 1.1.6.2 yamt * HCLK = FCLK/8 when CAMDIVN[9] (HCLK4_HALF) = 1 */
269 1.1.6.2 yamt if( camdivn & CLKCAMDIVN_HCLK4_HALF )
270 1.1.6.2 yamt h /= 8;
271 1.1.6.2 yamt else
272 1.1.6.2 yamt h /= 4;
273 1.1.6.2 yamt break;
274 1.1.6.2 yamt case 3:
275 1.1.6.2 yamt /* 11b: HCLK = FCLK/3 when CAMDIVN[8] (HCLK3_HALF) = 0
276 1.1.6.2 yamt * HCLK = FCLK/6 when CAMDIVN[8] (HCLK3_HALF) = 1 */
277 1.1.6.2 yamt if( camdivn & CLKCAMDIVN_HCLK3_HALF )
278 1.1.6.2 yamt h /= 6;
279 1.1.6.2 yamt else
280 1.1.6.2 yamt h /= 3;
281 1.1.6.2 yamt break;
282 1.1.6.2 yamt }
283 1.1.6.2 yamt
284 1.1.6.2 yamt p = h;
285 1.1.6.2 yamt
286 1.1.6.2 yamt if (divn & CLKDIVN_PDIVN)
287 1.1.6.2 yamt p /= 2;
288 1.1.6.2 yamt
289 1.1.6.2 yamt if (fclk) *fclk = f;
290 1.1.6.2 yamt if (hclk) *hclk = h;
291 1.1.6.2 yamt if (pclk) *pclk = p;
292 1.1.6.2 yamt
293 1.1.6.2 yamt }
294 1.1.6.2 yamt
295 1.1.6.2 yamt void
296 1.1.6.2 yamt s3c24x0_clock_freq(struct s3c2xx0_softc *sc)
297 1.1.6.2 yamt {
298 1.1.6.2 yamt s3c24x0_clock_freq2(
299 1.1.6.2 yamt (vaddr_t)bus_space_vaddr(sc->sc_iot, sc->sc_clkman_ioh),
300 1.1.6.2 yamt &sc->sc_fclk, &sc->sc_hclk, &sc->sc_pclk);
301 1.1.6.2 yamt }
302 1.1.6.2 yamt
303 1.1.6.2 yamt /*
304 1.1.6.2 yamt * Issue software reset command.
305 1.1.6.2 yamt * called with MMU off.
306 1.1.6.2 yamt *
307 1.1.6.2 yamt * S3C2410 doesn't have sowtware reset bit like S3C2800.
308 1.1.6.2 yamt * use watch dog timer and make it fire immediately.
309 1.1.6.2 yamt */
310 1.1.6.2 yamt void
311 1.1.6.2 yamt s3c2440_softreset(void)
312 1.1.6.2 yamt {
313 1.1.6.2 yamt disable_interrupts(I32_bit|F32_bit);
314 1.1.6.2 yamt
315 1.1.6.2 yamt *(volatile unsigned int *)(S3C2440_WDT_BASE + WDT_WTCON)
316 1.1.6.2 yamt = (0 << WTCON_PRESCALE_SHIFT) | WTCON_ENABLE |
317 1.1.6.2 yamt WTCON_CLKSEL_16 | WTCON_ENRST;
318 1.1.6.2 yamt }
319 1.1.6.2 yamt
320 1.1.6.2 yamt
321