1/*-
2 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
3 *
4 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29/* Based on sys/arm/ti/ti_sysc.c */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bus.h>
34#include <sys/fbio.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/rman.h>
38#include <sys/resource.h>
39#include <machine/bus.h>
40#include <vm/vm.h>
41#include <vm/vm_extern.h>
42#include <vm/vm_kern.h>
43#include <vm/pmap.h>
44
45#include <dev/fdt/simplebus.h>
46
47#include <dev/ofw/ofw_bus.h>
48#include <dev/ofw/ofw_bus_subr.h>
49
50#define TI_AM3_SCM			14
51#define TI_AM4_SCM			13
52#define TI_DM814_SCRM			12
53#define TI_DM816_SCRM			11
54#define TI_OMAP2_SCM			10
55#define TI_OMAP3_SCM			9
56#define TI_OMAP4_SCM_CORE		8
57#define TI_OMAP4_SCM_PADCONF_CORE	7
58#define TI_OMAP4_SCM_WKUP		6
59#define TI_OMAP4_SCM_PADCONF_WKUP	5
60#define TI_OMAP5_SCM_CORE		4
61#define TI_OMAP5_SCM_PADCONF_CORE	3
62#define TI_OMAP5_SCM_WKUP_PAD_CONF	2
63#define TI_DRA7_SCM_CORE		1
64#define TI_SCM_END			0
65
66static struct ofw_compat_data compat_data[] = {
67	{ "ti,am3-scm",			TI_AM3_SCM },
68	{ "ti,am4-scm",			TI_AM4_SCM },
69	{ "ti,dm814-scrm",		TI_DM814_SCRM },
70	{ "ti,dm816-scrm",		TI_DM816_SCRM },
71	{ "ti,omap2-scm",		TI_OMAP2_SCM },
72	{ "ti,omap3-scm",		TI_OMAP3_SCM },
73	{ "ti,omap4-scm-core",		TI_OMAP4_SCM_CORE },
74	{ "ti,omap4-scm-padconf-core",	TI_OMAP4_SCM_PADCONF_CORE },
75	{ "ti,omap4-scm-wkup",		TI_OMAP4_SCM_WKUP },
76	{ "ti,omap4-scm-padconf-wkup",	TI_OMAP4_SCM_PADCONF_WKUP },
77	{ "ti,omap5-scm-core",		TI_OMAP5_SCM_CORE },
78	{ "ti,omap5-scm-padconf-core",	TI_OMAP5_SCM_PADCONF_CORE },
79	{ "ti,omap5-scm-wkup-pad-conf",	TI_OMAP5_SCM_WKUP_PAD_CONF },
80	{ "ti,dra7-scm-core",		TI_DRA7_SCM_CORE },
81	{ NULL,				TI_SCM_END }
82};
83
84struct ti_scm_softc {
85	struct simplebus_softc	sc;
86	device_t		dev;
87};
88
89static int ti_scm_probe(device_t dev);
90static int ti_scm_attach(device_t dev);
91static int ti_scm_detach(device_t dev);
92
93static int
94ti_scm_probe(device_t dev)
95{
96	if (!ofw_bus_status_okay(dev))
97		return (ENXIO);
98
99	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
100		return (ENXIO);
101
102	device_set_desc(dev, "TI OMAP Control Module");
103
104	return (BUS_PROBE_DEFAULT);
105}
106
107static int
108ti_scm_attach(device_t dev)
109{
110	struct ti_scm_softc *sc;
111	device_t cdev;
112	phandle_t node, child;
113
114	sc = device_get_softc(dev);
115	sc->dev = dev;
116	node = ofw_bus_get_node(dev);
117
118	simplebus_init(dev, node);
119	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
120		device_printf(dev, "could not get ranges\n");
121		return (ENXIO);
122	}
123
124	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
125		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
126		if (cdev != NULL)
127			device_probe_and_attach(cdev);
128	}
129
130	return (bus_generic_attach(dev));
131}
132
133static int
134ti_scm_detach(device_t dev)
135{
136	return (EBUSY);
137}
138
139static device_method_t ti_scm_methods[] = {
140	/* Device interface */
141	DEVMETHOD(device_probe,		ti_scm_probe),
142	DEVMETHOD(device_attach,	ti_scm_attach),
143	DEVMETHOD(device_detach,	ti_scm_detach),
144
145	DEVMETHOD_END
146};
147
148DEFINE_CLASS_1(ti_scm, ti_scm_driver, ti_scm_methods,
149    sizeof(struct ti_scm_softc), simplebus_driver);
150
151EARLY_DRIVER_MODULE(ti_scm, simplebus, ti_scm_driver, 0, 0,
152    BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
153MODULE_VERSION(ti_scm, 1);
154MODULE_DEPEND(ti_scm, ti_sysc, 1, 1, 1);
155