1248557Sray/*-
2250357Sray * Copyright (c) 2012, 2013 The FreeBSD Foundation
3248557Sray * All rights reserved.
4248557Sray *
5248557Sray * This software was developed by Oleksandr Rybalko under sponsorship
6248557Sray * from the FreeBSD Foundation.
7248557Sray *
8248557Sray * Redistribution and use in source and binary forms, with or without
9248557Sray * modification, are permitted provided that the following conditions
10248557Sray * are met:
11248557Sray * 1.	Redistributions of source code must retain the above copyright
12248557Sray *	notice, this list of conditions and the following disclaimer.
13248557Sray * 2.	Redistributions in binary form must reproduce the above copyright
14248557Sray *	notice, this list of conditions and the following disclaimer in the
15248557Sray *	documentation and/or other materials provided with the distribution.
16248557Sray *
17248557Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18248557Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19248557Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20248557Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21248557Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22248557Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23248557Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24248557Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25248557Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26248557Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27248557Sray * SUCH DAMAGE.
28248557Sray */
29248557Sray
30248557Sray#include <sys/cdefs.h>
31248557Sray__FBSDID("$FreeBSD: releng/10.3/sys/arm/freescale/imx/imx_wdog.c 287079 2015-08-23 20:16:13Z ian $");
32248557Sray
33248557Sray#include <sys/param.h>
34248557Sray#include <sys/systm.h>
35248557Sray#include <sys/kernel.h>
36248557Sray#include <sys/module.h>
37248557Sray#include <sys/time.h>
38248557Sray#include <sys/bus.h>
39248557Sray#include <sys/resource.h>
40248557Sray#include <sys/rman.h>
41248557Sray#include <sys/watchdog.h>
42248557Sray
43248557Sray#include <machine/bus.h>
44248557Sray#include <machine/intr.h>
45248557Sray
46248557Sray#include <dev/fdt/fdt_common.h>
47248557Sray#include <dev/ofw/openfirm.h>
48248557Sray#include <dev/ofw/ofw_bus.h>
49248557Sray#include <dev/ofw/ofw_bus_subr.h>
50248557Sray#include <machine/fdt.h>
51248557Sray
52248557Sray#include <arm/freescale/imx/imx_wdogreg.h>
53248557Sray
54248557Sraystruct imx_wdog_softc {
55248557Sray	struct mtx		sc_mtx;
56248557Sray	device_t		sc_dev;
57248557Sray	bus_space_tag_t		sc_bst;
58248557Sray	bus_space_handle_t	sc_bsh;
59248557Sray	struct resource		*sc_res[2];
60248557Sray	uint32_t		sc_timeout;
61248557Sray};
62248557Sray
63248557Sraystatic struct resource_spec imx_wdog_spec[] = {
64248557Sray	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
65248557Sray	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
66248557Sray	{ -1, 0 }
67248557Sray};
68248557Sray
69287079Sianstatic struct ofw_compat_data compat_data[] = {
70287079Sian	{"fsl,imx6sx-wdt", 1},
71287079Sian	{"fsl,imx6sl-wdt", 1},
72287079Sian	{"fsl,imx6q-wdt",  1},
73287079Sian	{"fsl,imx53-wdt",  1},
74287079Sian	{"fsl,imx51-wdt",  1},
75287079Sian	{"fsl,imx50-wdt",  1},
76287079Sian	{"fsl,imx35-wdt",  1},
77287079Sian	{"fsl,imx27-wdt",  1},
78287079Sian	{"fsl,imx25-wdt",  1},
79287079Sian	{"fsl,imx21-wdt",  1},
80287079Sian	{NULL,             0}
81287079Sian};
82287079Sian
83248557Sraystatic void	imx_watchdog(void *, u_int, int *);
84248557Sraystatic int	imx_wdog_probe(device_t);
85248557Sraystatic int	imx_wdog_attach(device_t);
86248557Sray
87248557Sraystatic device_method_t imx_wdog_methods[] = {
88248557Sray	DEVMETHOD(device_probe,		imx_wdog_probe),
89248557Sray	DEVMETHOD(device_attach,	imx_wdog_attach),
90248557Sray	DEVMETHOD_END
91248557Sray};
92248557Sray
93248557Sraystatic driver_t imx_wdog_driver = {
94248557Sray	"imx_wdog",
95248557Sray	imx_wdog_methods,
96248557Sray	sizeof(struct imx_wdog_softc),
97248557Sray};
98248557Sraystatic devclass_t imx_wdog_devclass;
99248557SrayDRIVER_MODULE(imx_wdog, simplebus, imx_wdog_driver, imx_wdog_devclass, 0, 0);
100248557Sray
101287079Sian#define	RD2(_sc, _r)							\
102287079Sian		bus_space_read_2((_sc)->sc_bst, (_sc)->sc_bsh, (_r))
103287079Sian#define	WR2(_sc, _r, _v)						\
104287079Sian		bus_space_write_2((_sc)->sc_bst, (_sc)->sc_bsh, (_r), (_v))
105248557Sray
106248557Sraystatic void
107248557Srayimx_watchdog(void *arg, u_int cmd, int *error)
108248557Sray{
109248557Sray	struct imx_wdog_softc *sc;
110248557Sray	uint16_t reg;
111287079Sian	u_int timeout;
112248557Sray
113248557Sray	sc = arg;
114248557Sray	mtx_lock(&sc->sc_mtx);
115287079Sian	if (cmd == 0) {
116287079Sian		if (bootverbose)
117287079Sian			device_printf(sc->sc_dev, "Can not be disabled.\n");
118287079Sian		*error = EOPNOTSUPP;
119287079Sian	} else {
120287079Sian		timeout = (u_int)((1ULL << (cmd & WD_INTERVAL)) / 1000000000U);
121287079Sian		if (timeout > 1 && timeout < 128) {
122287079Sian			if (timeout != sc->sc_timeout) {
123287079Sian				sc->sc_timeout = timeout;
124287079Sian				reg = RD2(sc, WDOG_CR_REG);
125287079Sian				reg &= ~WDOG_CR_WT_MASK;
126287079Sian				reg |= (timeout << (WDOG_CR_WT_SHIFT + 1)) &
127287079Sian				    WDOG_CR_WT_MASK;
128287079Sian				WR2(sc, WDOG_CR_REG, reg | WDOG_CR_WDE);
129287079Sian			}
130248557Sray			/* Refresh counter */
131287079Sian			WR2(sc, WDOG_SR_REG, WDOG_SR_STEP1);
132287079Sian			WR2(sc, WDOG_SR_REG, WDOG_SR_STEP2);
133248557Sray			*error = 0;
134248557Sray		}
135248557Sray	}
136248557Sray	mtx_unlock(&sc->sc_mtx);
137248557Sray}
138248557Sray
139248557Sraystatic int
140248557Srayimx_wdog_probe(device_t dev)
141248557Sray{
142248557Sray
143266152Sian	if (!ofw_bus_status_okay(dev))
144266152Sian		return (ENXIO);
145266152Sian
146287079Sian	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
147248557Sray		return (ENXIO);
148248557Sray
149287079Sian	device_set_desc(dev, "Freescale i.MX Watchdog");
150248557Sray	return (0);
151248557Sray}
152248557Sray
153248557Sraystatic int
154248557Srayimx_wdog_attach(device_t dev)
155248557Sray{
156248557Sray	struct imx_wdog_softc *sc;
157248557Sray
158248557Sray	sc = device_get_softc(dev);
159248557Sray	sc->sc_dev = dev;
160248557Sray
161248557Sray	if (bus_alloc_resources(dev, imx_wdog_spec, sc->sc_res)) {
162248557Sray		device_printf(dev, "could not allocate resources\n");
163248557Sray		return (ENXIO);
164248557Sray	}
165248557Sray
166248557Sray	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "imx_wdt", MTX_DEF);
167248557Sray
168248557Sray	sc->sc_dev = dev;
169248557Sray	sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
170248557Sray	sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
171248557Sray
172248557Sray	/* TODO: handle interrupt */
173248557Sray
174248557Sray	EVENTHANDLER_REGISTER(watchdog_list, imx_watchdog, sc, 0);
175248557Sray	return (0);
176248557Sray}
177