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