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