aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/ti/ti_prcm.c
blob: 2664b8f71ec43c32747f7d9a927ee6a148171a00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
 * All rights reserved.
 *
 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/* Based on sys/arm/ti/am335x/am335x_prcm.c */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/timeet.h>
#include <sys/timetc.h>
#include <sys/watchdog.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/intr.h>

#include <arm/ti/ti_cpuid.h>
#include <arm/ti/ti_prcm.h>
#include <arm/ti/tivar.h>

#include <dev/fdt/simplebus.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include "clkdev_if.h"

#if 0
#define DPRINTF(dev, msg...) device_printf(dev, msg)
#else
#define DPRINTF(dev, msg...)
#endif

struct ti_prcm_softc {
	struct simplebus_softc  sc_simplebus;
	device_t		dev;
	struct resource *	mem_res;
	bus_space_tag_t		bst;
	bus_space_handle_t	bsh;
	int			attach_done;
	struct mtx		mtx;
};

static struct ti_prcm_softc *ti_prcm_sc = NULL;
static void omap4_prcm_reset(void);
static void am335x_prcm_reset(void);

#define TI_AM3_PRCM		18
#define TI_AM4_PRCM		17
#define TI_OMAP2_PRCM		16
#define TI_OMAP3_PRM		15
#define TI_OMAP3_CM		14
#define TI_OMAP4_CM1		13
#define TI_OMAP4_PRM		12
#define TI_OMAP4_CM2		11
#define TI_OMAP4_SCRM		10
#define TI_OMAP5_PRM		9
#define TI_OMAP5_CM_CORE_AON	8
#define TI_OMAP5_SCRM		7
#define TI_OMAP5_CM_CORE	6
#define TI_DRA7_PRM		5
#define TI_DRA7_CM_CORE_AON	4
#define TI_DRA7_CM_CORE		3
#define TI_DM814_PRCM		2
#define TI_DM816_PRCM		1
#define TI_PRCM_END		0

static struct ofw_compat_data compat_data[] = {
	{ "ti,am3-prcm",		TI_AM3_PRCM },
	{ "ti,am4-prcm",		TI_AM4_PRCM },
	{ "ti,omap2-prcm",		TI_OMAP2_PRCM },
	{ "ti,omap3-prm",		TI_OMAP3_PRM },
	{ "ti,omap3-cm",		TI_OMAP3_CM },
	{ "ti,omap4-cm1",		TI_OMAP4_CM1 },
	{ "ti,omap4-prm",		TI_OMAP4_PRM },
	{ "ti,omap4-cm2",		TI_OMAP4_CM2 },
	{ "ti,omap4-scrm",		TI_OMAP4_SCRM },
	{ "ti,omap5-prm",		TI_OMAP5_PRM },
	{ "ti,omap5-cm-core-aon",	TI_OMAP5_CM_CORE_AON },
	{ "ti,omap5-scrm",		TI_OMAP5_SCRM },
	{ "ti,omap5-cm-core",		TI_OMAP5_CM_CORE },
	{ "ti,dra7-prm",		TI_DRA7_PRM },
	{ "ti,dra7-cm-core-aon",	TI_DRA7_CM_CORE_AON },
	{ "ti,dra7-cm-core",		TI_DRA7_CM_CORE },
	{ "ti,dm814-prcm",		TI_DM814_PRCM },
	{ "ti,dm816-prcm",		TI_DM816_PRCM },
	{ NULL,				TI_PRCM_END}
};

static int
ti_prcm_probe(device_t dev)
{
	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) {
		return (ENXIO);
	}

	device_set_desc(dev, "TI Power and Clock Management");
	return(BUS_PROBE_DEFAULT);
}

static int
ti_prcm_attach(device_t dev)
{
	struct ti_prcm_softc *sc;
	phandle_t node, child;
	int rid;

	sc = device_get_softc(dev);
	sc->dev = dev;

	node = ofw_bus_get_node(sc->dev);
	simplebus_init(sc->dev, node);

	if (simplebus_fill_ranges(node, &sc->sc_simplebus) < 0) {
		device_printf(sc->dev, "could not get ranges\n");
		return (ENXIO);
	}
	if (sc->sc_simplebus.nranges == 0) {
		device_printf(sc->dev, "nranges == 0\n");
		return (ENXIO);
	}

	sc->mem_res = bus_alloc_resource(sc->dev, SYS_RES_MEMORY, &rid,
		sc->sc_simplebus.ranges[0].host,
		(sc->sc_simplebus.ranges[0].host +
			sc->sc_simplebus.ranges[0].size - 1),
		sc->sc_simplebus.ranges[0].size,
		RF_ACTIVE | RF_SHAREABLE);

	if (sc->mem_res == NULL) {
		return (ENXIO);
	}

	sc->bst = rman_get_bustag(sc->mem_res);
	sc->bsh = rman_get_bushandle(sc->mem_res);

	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);

	/* Fixme: for xxx_prcm_reset functions.
	 * Get rid of global variables?
	 */
	ti_prcm_sc = sc;

	switch(ti_chip()) {
#ifdef SOC_OMAP4
	case CHIP_OMAP_4:
		ti_cpu_reset = omap4_prcm_reset;
		break;
#endif
#ifdef SOC_TI_AM335X
	case CHIP_AM335X:
		ti_cpu_reset = am335x_prcm_reset;
		break;
#endif
	}

	bus_generic_probe(sc->dev);
	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
		simplebus_add_device(dev, child, 0, NULL, -1, NULL);
	}

	return (bus_generic_attach(sc->dev));
}

int
ti_prcm_write_4(device_t dev, bus_addr_t addr, uint32_t val)
{
	struct ti_prcm_softc *sc;

	sc = device_get_softc(dev);
	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
	bus_space_write_4(sc->bst, sc->bsh, addr, val);
	return (0);
}
int
ti_prcm_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
{
	struct ti_prcm_softc *sc;

	sc = device_get_softc(dev);

	*val = bus_space_read_4(sc->bst, sc->bsh, addr);
	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
	return (0);
}

int
ti_prcm_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
{
	struct ti_prcm_softc *sc;
	uint32_t reg;

	sc = device_get_softc(dev);

	reg = bus_space_read_4(sc->bst, sc->bsh, addr);
	reg &= ~clr;
	reg |= set;
	bus_space_write_4(sc->bst, sc->bsh, addr, reg);
	DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set);

	return (0);
}

void
ti_prcm_device_lock(device_t dev)
{
	struct ti_prcm_softc *sc;

	sc = device_get_softc(dev);
	mtx_lock(&sc->mtx);
}

void
ti_prcm_device_unlock(device_t dev)
{
	struct ti_prcm_softc *sc;

	sc = device_get_softc(dev);
	mtx_unlock(&sc->mtx);
}

static device_method_t ti_prcm_methods[] = {
	DEVMETHOD(device_probe,		ti_prcm_probe),
	DEVMETHOD(device_attach,	ti_prcm_attach),

	/* clkdev interface */
	DEVMETHOD(clkdev_write_4,	ti_prcm_write_4),
	DEVMETHOD(clkdev_read_4,	ti_prcm_read_4),
	DEVMETHOD(clkdev_modify_4,	ti_prcm_modify_4),
	DEVMETHOD(clkdev_device_lock,	ti_prcm_device_lock),
	DEVMETHOD(clkdev_device_unlock, ti_prcm_device_unlock),

	DEVMETHOD_END
};

DEFINE_CLASS_1(ti_prcm, ti_prcm_driver, ti_prcm_methods,
    sizeof(struct ti_prcm_softc), simplebus_driver);

static devclass_t ti_prcm_devclass;

EARLY_DRIVER_MODULE(ti_prcm, ofwbus, ti_prcm_driver,
	ti_prcm_devclass, 0, 0, BUS_PASS_BUS);
EARLY_DRIVER_MODULE(ti_prcm, simplebus, ti_prcm_driver,
	ti_prcm_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
MODULE_VERSION(ti_prcm, 1);
MODULE_DEPEND(ti_prcm, ti_scm, 1, 1, 1);

/* From sys/arm/ti/am335x/am335x_prcm.c
 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
 */
#define PRM_DEVICE_OFFSET		0xF00
#define AM335x_PRM_RSTCTRL		(PRM_DEVICE_OFFSET + 0x00)

static void
am335x_prcm_reset(void)
{
	ti_prcm_write_4(ti_prcm_sc->dev, AM335x_PRM_RSTCTRL, (1<<1));
}

/* FIXME: Is this correct - or should the license part be ontop? */

/* From sys/arm/ti/omap4/omap4_prcm_clks.c */
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2011
 *      Ben Gray <ben.r.gray@gmail.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the company nor the name of the author may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#define PRM_RSTCTRL		0x1b00
#define PRM_RSTCTRL_RESET	0x2

static void
omap4_prcm_reset(void)
{
	uint32_t reg;

	ti_prcm_read_4(ti_prcm_sc->dev, PRM_RSTCTRL, &reg);
	reg = reg | PRM_RSTCTRL_RESET;
	ti_prcm_write_4(ti_prcm_sc->dev, PRM_RSTCTRL, reg);
	ti_prcm_read_4(ti_prcm_sc->dev, PRM_RSTCTRL, &reg);
}