xref: /freebsd/sys/arm/freescale/imx/imx_machdep.c (revision 3dae01840d99bab646d39b9487adca44ab96ba22)
1ec4081c1SIan Lepore /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni  *
4ec4081c1SIan Lepore  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
5ec4081c1SIan Lepore  * All rights reserved.
6ec4081c1SIan Lepore  *
7ec4081c1SIan Lepore  * Redistribution and use in source and binary forms, with or without
8ec4081c1SIan Lepore  * modification, are permitted provided that the following conditions
9ec4081c1SIan Lepore  * are met:
10ec4081c1SIan Lepore  * 1. Redistributions of source code must retain the above copyright
11ec4081c1SIan Lepore  *    notice, this list of conditions and the following disclaimer.
12ec4081c1SIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
13ec4081c1SIan Lepore  *    notice, this list of conditions and the following disclaimer in the
14ec4081c1SIan Lepore  *    documentation and/or other materials provided with the distribution.
15ec4081c1SIan Lepore  *
16ec4081c1SIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ec4081c1SIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ec4081c1SIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ec4081c1SIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ec4081c1SIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ec4081c1SIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ec4081c1SIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ec4081c1SIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ec4081c1SIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ec4081c1SIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ec4081c1SIan Lepore  * SUCH DAMAGE.
27ec4081c1SIan Lepore  */
28ec4081c1SIan Lepore 
29ec4081c1SIan Lepore #include "opt_platform.h"
30ec4081c1SIan Lepore 
31ec4081c1SIan Lepore #include <sys/param.h>
32ec4081c1SIan Lepore #include <sys/systm.h>
33ec4081c1SIan Lepore #include <sys/reboot.h>
34ec4081c1SIan Lepore 
35ec4081c1SIan Lepore #include <vm/vm.h>
36ec4081c1SIan Lepore #include <vm/pmap.h>
37ec4081c1SIan Lepore 
38ec4081c1SIan Lepore #include <machine/armreg.h>
39ec4081c1SIan Lepore #include <machine/bus.h>
404dbbaf20SIan Lepore #include <machine/cpu.h>
41ec4081c1SIan Lepore #include <machine/machdep.h>
42ec4081c1SIan Lepore 
43ec4081c1SIan Lepore #include <arm/freescale/imx/imx_machdep.h>
44ec4081c1SIan Lepore #include <arm/freescale/imx/imx_wdogreg.h>
45ec4081c1SIan Lepore 
467029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, imx, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
477029da5cSPawel Biernacki     "i.MX container");
48bd6b2f9bSIan Lepore 
49bd6b2f9bSIan Lepore static int last_reset_status;
50bd6b2f9bSIan Lepore SYSCTL_UINT(_hw_imx, OID_AUTO, last_reset_status, CTLFLAG_RD,
51bd6b2f9bSIan Lepore     &last_reset_status, 0, "Last reset status register");
52bd6b2f9bSIan Lepore 
53bd6b2f9bSIan Lepore SYSCTL_STRING(_hw_imx, OID_AUTO, last_reset_reason, CTLFLAG_RD,
54bd6b2f9bSIan Lepore     "unknown", 0, "Last reset reason");
55bd6b2f9bSIan Lepore 
565e503d28SIan Lepore /*
575e503d28SIan Lepore  * This code which manipulates the watchdog hardware is here to implement
585e503d28SIan Lepore  * cpu_reset() because the watchdog is the only way for software to reset the
595e503d28SIan Lepore  * chip.  Why here and not in imx_wdog.c?  Because there's no requirement that
605e503d28SIan Lepore  * the watchdog driver be compiled in, but it's nice to be able to reboot even
615e503d28SIan Lepore  * if it's not.
625e503d28SIan Lepore  */
63ec4081c1SIan Lepore void
imx_wdog_cpu_reset(vm_offset_t wdcr_physaddr)64ec4081c1SIan Lepore imx_wdog_cpu_reset(vm_offset_t wdcr_physaddr)
65ec4081c1SIan Lepore {
664dbbaf20SIan Lepore 	volatile uint16_t cr, *pcr;
674dbbaf20SIan Lepore 
68*3dae0184SAndrew Turner 	if ((pcr = pmap_mapdev(wdcr_physaddr, sizeof(*pcr))) == NULL) {
694dbbaf20SIan Lepore 		printf("imx_wdog_cpu_reset(): "
704dbbaf20SIan Lepore 		    "cannot find control register... locking up now.");
714dbbaf20SIan Lepore 		for (;;)
724dbbaf20SIan Lepore 			cpu_spinwait();
734dbbaf20SIan Lepore 	}
744dbbaf20SIan Lepore 	cr = *pcr;
75ec4081c1SIan Lepore 
76ec4081c1SIan Lepore 	/*
774dbbaf20SIan Lepore 	 * If the watchdog hardware has been set up to trigger an external reset
784dbbaf20SIan Lepore 	 * signal on watchdog timeout, then we do software-requested rebooting
794dbbaf20SIan Lepore 	 * the same way, by asserting the external reset signal.
8083988d94SIan Lepore 	 *
814dbbaf20SIan Lepore 	 * Asserting external reset is supposed to result in some external
824dbbaf20SIan Lepore 	 * component asserting the POR pin on the SoC, possibly after adjusting
834dbbaf20SIan Lepore 	 * and stabilizing system voltages, or taking other system-wide reset
844dbbaf20SIan Lepore 	 * actions.  Just in case there is some kind of misconfiguration, we
854dbbaf20SIan Lepore 	 * hang out and do nothing for a full second, then continue on into
864dbbaf20SIan Lepore 	 * the code to assert a software reset as well.
874dbbaf20SIan Lepore 	 */
884dbbaf20SIan Lepore 	if (cr & WDOG_CR_WDT) {
894dbbaf20SIan Lepore 		cr &= ~WDOG_CR_WDA; /* Assert active-low ext reset bit. */
904dbbaf20SIan Lepore 		*pcr = cr;
914dbbaf20SIan Lepore 		DELAY(1000000);
924dbbaf20SIan Lepore 		printf("imx_wdog_cpu_reset(): "
934dbbaf20SIan Lepore 		    "External reset failed, trying internal cpu-reset\n");
944dbbaf20SIan Lepore 		DELAY(10000); /* Time for printf to appear */
954dbbaf20SIan Lepore 	}
964dbbaf20SIan Lepore 
974dbbaf20SIan Lepore 	/*
9883988d94SIan Lepore 	 * Imx6 erratum ERR004346 says the SRS bit has to be cleared twice
9983988d94SIan Lepore 	 * within the same cycle of the 32khz clock to reliably trigger the
10083988d94SIan Lepore 	 * reset.  Writing it 3 times in a row ensures at least 2 of the writes
10183988d94SIan Lepore 	 * happen in the same 32k clock cycle.
102ec4081c1SIan Lepore 	 */
1034dbbaf20SIan Lepore 	cr &= ~WDOG_CR_SRS; /* Assert active-low software reset bit. */
1044dbbaf20SIan Lepore 	*pcr = cr;
1054dbbaf20SIan Lepore 	*pcr = cr;
1064dbbaf20SIan Lepore 	*pcr = cr;
1074dbbaf20SIan Lepore 
1084dbbaf20SIan Lepore 	/* Reset happens on the next tick of the 32khz clock, wait for it. */
1095e503d28SIan Lepore 	for (;;)
1104dbbaf20SIan Lepore 		cpu_spinwait();
111ec4081c1SIan Lepore }
112ec4081c1SIan Lepore 
113bd6b2f9bSIan Lepore void
imx_wdog_init_last_reset(vm_offset_t wdsr_phys)114bd6b2f9bSIan Lepore imx_wdog_init_last_reset(vm_offset_t wdsr_phys)
115bd6b2f9bSIan Lepore {
116bd6b2f9bSIan Lepore 	volatile uint16_t * psr;
117bd6b2f9bSIan Lepore 
118*3dae0184SAndrew Turner 	if ((psr = pmap_mapdev(wdsr_phys, sizeof(*psr))) == NULL)
119bd6b2f9bSIan Lepore 		return;
120bd6b2f9bSIan Lepore 	last_reset_status = *psr;
121bd6b2f9bSIan Lepore 	if (last_reset_status & WDOG_RSR_SFTW) {
122bd6b2f9bSIan Lepore 		sysctl___hw_imx_last_reset_reason.oid_arg1 = "SoftwareReset";
123bd6b2f9bSIan Lepore 	} else if (last_reset_status & WDOG_RSR_TOUT) {
124bd6b2f9bSIan Lepore 		sysctl___hw_imx_last_reset_reason.oid_arg1 = "WatchdogTimeout";
125bd6b2f9bSIan Lepore 	} else if (last_reset_status & WDOG_RSR_POR) {
126bd6b2f9bSIan Lepore 		sysctl___hw_imx_last_reset_reason.oid_arg1 = "PowerOnReset";
127bd6b2f9bSIan Lepore 	}
128*3dae0184SAndrew Turner 	pmap_unmapdev((void *)(uintptr_t)psr, sizeof(*psr));
129bd6b2f9bSIan Lepore }
130