xref: /freebsd/sys/arm/freescale/imx/imx6_machdep.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*-
2  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "opt_platform.h"
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/reboot.h>
36 
37 #include <vm/vm.h>
38 
39 #include <machine/bus.h>
40 #include <machine/devmap.h>
41 #include <machine/intr.h>
42 #include <machine/machdep.h>
43 #include <machine/platformvar.h>
44 
45 #include <arm/arm/mpcore_timervar.h>
46 #include <arm/freescale/imx/imx6_anatopreg.h>
47 #include <arm/freescale/imx/imx6_anatopvar.h>
48 #include <arm/freescale/imx/imx_machdep.h>
49 
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/ofw/openfirm.h>
52 
53 #include "platform_if.h"
54 
55 struct fdt_fixup_entry fdt_fixup_table[] = {
56 	{ NULL, NULL }
57 };
58 
59 static uint32_t gpio1_node;
60 
61 /*
62  * Work around the linux workaround for imx6 erratum 006687, in which some
63  * ethernet interrupts don't go to the GPC and thus won't wake the system from
64  * Wait mode. We don't use Wait mode (which halts the GIC, leaving only GPC
65  * interrupts able to wake the system), so we don't experience the bug at all.
66  * The linux workaround is to reconfigure GPIO1_6 as the ENET interrupt by
67  * writing magic values to an undocumented IOMUX register, then letting the gpio
68  * interrupt driver notify the ethernet driver.  We'll be able to do all that
69  * (even though we don't need to) once the INTRNG project is committed and the
70  * imx_gpio driver becomes an interrupt driver.  Until then, this crazy little
71  * workaround watches for requests to map an interrupt 6 with the interrupt
72  * controller node referring to gpio1, and it substitutes the proper ffec
73  * interrupt number.
74  */
75 static int
76 imx6_decode_fdt(uint32_t iparent, uint32_t *intr, int *interrupt,
77     int *trig, int *pol)
78 {
79 
80 	if (fdt32_to_cpu(intr[0]) == 6 &&
81 	    OF_node_from_xref(iparent) == gpio1_node) {
82 		*interrupt = 150;
83 		*trig = INTR_TRIGGER_CONFORM;
84 		*pol  = INTR_POLARITY_CONFORM;
85 		return (0);
86 	}
87 	return (gic_decode_fdt(iparent, intr, interrupt, trig, pol));
88 }
89 
90 fdt_pic_decode_t fdt_pic_table[] = {
91 	&imx6_decode_fdt,
92 	NULL
93 };
94 
95 static vm_offset_t
96 imx6_lastaddr(platform_t plat)
97 {
98 
99 	return (arm_devmap_lastaddr());
100 }
101 
102 static int
103 imx6_attach(platform_t plat)
104 {
105 
106 	/* Inform the MPCore timer driver that its clock is variable. */
107 	arm_tmr_change_frequency(ARM_TMR_FREQUENCY_VARIES);
108 
109 	return (0);
110 }
111 
112 static void
113 imx6_late_init(platform_t plat)
114 {
115 
116 	/* Cache the gpio1 node handle for imx6_decode_fdt() workaround code. */
117 	gpio1_node = OF_node_from_xref(
118 	    OF_finddevice("/soc/aips-bus@02000000/gpio@0209c000"));
119 }
120 
121 /*
122  * Set up static device mappings.
123  *
124  * This attempts to cover the most-used devices with 1MB section mappings, which
125  * is good for performance (uses fewer TLB entries for device access).
126  *
127  * ARMMP covers the interrupt controller, MPCore timers, global timer, and the
128  * L2 cache controller.  Most of the 1MB range is unused reserved space.
129  *
130  * AIPS1/AIPS2 cover most of the on-chip devices such as uart, spi, i2c, etc.
131  *
132  * Notably not mapped right now are HDMI, GPU, and other devices below ARMMP in
133  * the memory map.  When we get support for graphics it might make sense to
134  * static map some of that area.  Be careful with other things in that area such
135  * as OCRAM that probably shouldn't be mapped as PTE_DEVICE memory.
136  */
137 static int
138 imx6_devmap_init(platform_t plat)
139 {
140 	const uint32_t IMX6_ARMMP_PHYS = 0x00a00000;
141 	const uint32_t IMX6_ARMMP_SIZE = 0x00100000;
142 	const uint32_t IMX6_AIPS1_PHYS = 0x02000000;
143 	const uint32_t IMX6_AIPS1_SIZE = 0x00100000;
144 	const uint32_t IMX6_AIPS2_PHYS = 0x02100000;
145 	const uint32_t IMX6_AIPS2_SIZE = 0x00100000;
146 
147 	arm_devmap_add_entry(IMX6_ARMMP_PHYS, IMX6_ARMMP_SIZE);
148 	arm_devmap_add_entry(IMX6_AIPS1_PHYS, IMX6_AIPS1_SIZE);
149 	arm_devmap_add_entry(IMX6_AIPS2_PHYS, IMX6_AIPS2_SIZE);
150 
151 	return (0);
152 }
153 
154 void
155 cpu_reset(void)
156 {
157 	const uint32_t IMX6_WDOG_CR_PHYS = 0x020bc000;
158 
159 	imx_wdog_cpu_reset(IMX6_WDOG_CR_PHYS);
160 }
161 
162 /*
163  * Determine what flavor of imx6 we're running on.
164  *
165  * This code is based on the way u-boot does it.  Information found on the web
166  * indicates that Freescale themselves were the original source of this logic,
167  * including the strange check for number of CPUs in the SCU configuration
168  * register, which is apparently needed on some revisions of the SOLO.
169  *
170  * According to the documentation, there is such a thing as an i.MX6 Dual
171  * (non-lite flavor).  However, Freescale doesn't seem to have assigned it a
172  * number or provided any logic to handle it in their detection code.
173  *
174  * Note that the ANALOG_DIGPROG and SCU configuration registers are not
175  * documented in the chip reference manual.  (SCU configuration is mentioned,
176  * but not mapped out in detail.)  I think the bottom two bits of the scu config
177  * register may be ncpu-1.
178  *
179  * This hasn't been tested yet on a dual[-lite].
180  *
181  * On a solo:
182  *      digprog    = 0x00610001
183  *      hwsoc      = 0x00000062
184  *      scu config = 0x00000500
185  * On a quad:
186  *      digprog    = 0x00630002
187  *      hwsoc      = 0x00000063
188  *      scu config = 0x00005503
189  */
190 u_int imx_soc_type()
191 {
192 	uint32_t digprog, hwsoc;
193 	uint32_t *pcr;
194 	static u_int soctype;
195 	const vm_offset_t SCU_CONFIG_PHYSADDR = 0x00a00004;
196 #define	HWSOC_MX6SL	0x60
197 #define	HWSOC_MX6DL	0x61
198 #define	HWSOC_MX6SOLO	0x62
199 #define	HWSOC_MX6Q	0x63
200 
201 	if (soctype != 0)
202 		return (soctype);
203 
204 	digprog = imx6_anatop_read_4(IMX6_ANALOG_DIGPROG_SL);
205 	hwsoc = (digprog >> IMX6_ANALOG_DIGPROG_SOCTYPE_SHIFT) &
206 	    IMX6_ANALOG_DIGPROG_SOCTYPE_MASK;
207 
208 	if (hwsoc != HWSOC_MX6SL) {
209 		digprog = imx6_anatop_read_4(IMX6_ANALOG_DIGPROG);
210 		hwsoc = (digprog & IMX6_ANALOG_DIGPROG_SOCTYPE_MASK) >>
211 		    IMX6_ANALOG_DIGPROG_SOCTYPE_SHIFT;
212 		/*printf("digprog = 0x%08x\n", digprog);*/
213 		if (hwsoc == HWSOC_MX6DL) {
214 			pcr = arm_devmap_ptov(SCU_CONFIG_PHYSADDR, 4);
215 			if (pcr != NULL) {
216 				/*printf("scu config = 0x%08x\n", *pcr);*/
217 				if ((*pcr & 0x03) == 0) {
218 					hwsoc = HWSOC_MX6SOLO;
219 				}
220 			}
221 		}
222 	}
223 	/* printf("hwsoc 0x%08x\n", hwsoc); */
224 
225 	switch (hwsoc) {
226 	case HWSOC_MX6SL:
227 		soctype = IMXSOC_6SL;
228 		break;
229 	case HWSOC_MX6SOLO:
230 		soctype = IMXSOC_6S;
231 		break;
232 	case HWSOC_MX6DL:
233 		soctype = IMXSOC_6DL;
234 		break;
235 	case HWSOC_MX6Q :
236 		soctype = IMXSOC_6Q;
237 		break;
238 	default:
239 		printf("imx_soc_type: Don't understand hwsoc 0x%02x, "
240 		    "digprog 0x%08x; assuming IMXSOC_6Q\n", hwsoc, digprog);
241 		soctype = IMXSOC_6Q;
242 		break;
243 	}
244 
245 	return (soctype);
246 }
247 
248 /*
249  * Early putc routine for EARLY_PRINTF support.  To use, add to kernel config:
250  *   option SOCDEV_PA=0x02000000
251  *   option SOCDEV_VA=0x02000000
252  *   option EARLY_PRINTF
253  * Resist the temptation to change the #if 0 to #ifdef EARLY_PRINTF here. It
254  * makes sense now, but if multiple SOCs do that it will make early_putc another
255  * duplicate symbol to be eliminated on the path to a generic kernel.
256  */
257 #if 0
258 static void
259 imx6_early_putc(int c)
260 {
261 	volatile uint32_t * UART_STAT_REG = (uint32_t *)0x02020098;
262 	volatile uint32_t * UART_TX_REG   = (uint32_t *)0x02020040;
263 	const uint32_t      UART_TXRDY    = (1 << 3);
264 
265 	while ((*UART_STAT_REG & UART_TXRDY) == 0)
266 		continue;
267 	*UART_TX_REG = c;
268 }
269 early_putc_t *early_putc = imx6_early_putc;
270 #endif
271 
272 static platform_method_t imx6_methods[] = {
273 	PLATFORMMETHOD(platform_attach,		imx6_attach),
274 	PLATFORMMETHOD(platform_lastaddr,	imx6_lastaddr),
275 	PLATFORMMETHOD(platform_devmap_init,	imx6_devmap_init),
276 	PLATFORMMETHOD(platform_late_init,	imx6_late_init),
277 
278 	PLATFORMMETHOD_END,
279 };
280 
281 FDT_PLATFORM_DEF2(imx6, imx6s, "i.MX6 Solo", 0, "fsl,imx6s");
282 FDT_PLATFORM_DEF2(imx6, imx6d, "i.MX6 Dual", 0, "fsl,imx6d");
283 FDT_PLATFORM_DEF2(imx6, imx6q, "i.MX6 Quad", 0, "fsl,imx6q");
284