1261413Sbr/*-
2261413Sbr * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3261413Sbr * All rights reserved.
4261413Sbr *
5261413Sbr * Redistribution and use in source and binary forms, with or without
6261413Sbr * modification, are permitted provided that the following conditions
7261413Sbr * are met:
8261413Sbr * 1. Redistributions of source code must retain the above copyright
9261413Sbr *    notice, this list of conditions and the following disclaimer.
10261413Sbr * 2. Redistributions in binary form must reproduce the above copyright
11261413Sbr *    notice, this list of conditions and the following disclaimer in the
12261413Sbr *    documentation and/or other materials provided with the distribution.
13261413Sbr *
14261413Sbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15261413Sbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16261413Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17261413Sbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18261413Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19261413Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20261413Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21261413Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22261413Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23261413Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24261413Sbr * SUCH DAMAGE.
25261413Sbr */
26261413Sbr
27261413Sbr/*
28261413Sbr * Vybrid Family Timing Controller (TCON)
29261413Sbr * Chapter 58, Vybrid Reference Manual, Rev. 5, 07/2013
30261413Sbr */
31261413Sbr
32261413Sbr#include <sys/cdefs.h>
33261413Sbr__FBSDID("$FreeBSD$");
34261413Sbr
35261413Sbr#include <sys/param.h>
36261413Sbr#include <sys/systm.h>
37261413Sbr#include <sys/bus.h>
38261413Sbr#include <sys/kernel.h>
39261413Sbr#include <sys/module.h>
40261413Sbr#include <sys/malloc.h>
41261413Sbr#include <sys/rman.h>
42261413Sbr#include <sys/timeet.h>
43261413Sbr#include <sys/timetc.h>
44261413Sbr#include <sys/watchdog.h>
45261413Sbr
46261413Sbr#include <dev/fdt/fdt_common.h>
47261413Sbr#include <dev/ofw/openfirm.h>
48261413Sbr#include <dev/ofw/ofw_bus.h>
49261413Sbr#include <dev/ofw/ofw_bus_subr.h>
50261413Sbr
51261413Sbr#include <machine/bus.h>
52261413Sbr#include <machine/fdt.h>
53261413Sbr#include <machine/cpu.h>
54261413Sbr#include <machine/intr.h>
55261413Sbr
56261413Sbr#include <arm/freescale/vybrid/vf_common.h>
57261413Sbr
58261413Sbr#define	TCON0_CTRL1	0x00
59266155Sian#define	TCON_BYPASS	(1 << 29)
60261413Sbr
61261413Sbrstruct tcon_softc {
62261413Sbr	struct resource		*res[1];
63261413Sbr	bus_space_tag_t		bst;
64261413Sbr	bus_space_handle_t	bsh;
65261413Sbr};
66261413Sbr
67261413Sbrstruct tcon_softc *tcon_sc;
68261413Sbr
69261413Sbrstatic struct resource_spec tcon_spec[] = {
70261413Sbr	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
71261413Sbr	{ -1, 0 }
72261413Sbr};
73261413Sbr
74261413Sbruint32_t
75261413Sbrtcon_bypass(void)
76261413Sbr{
77261413Sbr	struct tcon_softc *sc;
78261413Sbr
79261413Sbr	if (tcon_sc == NULL)
80261413Sbr		return (1);
81261413Sbr
82261413Sbr	sc = tcon_sc;
83261413Sbr
84266155Sian	WRITE4(tcon_sc, TCON0_CTRL1, TCON_BYPASS);
85261413Sbr
86261413Sbr	return (0);
87261413Sbr}
88261413Sbr
89261413Sbrstatic int
90261413Sbrtcon_probe(device_t dev)
91261413Sbr{
92261413Sbr
93261413Sbr	if (!ofw_bus_status_okay(dev))
94261413Sbr		return (ENXIO);
95261413Sbr
96261413Sbr	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-tcon"))
97261413Sbr		return (ENXIO);
98261413Sbr
99261413Sbr	device_set_desc(dev, "Vybrid Family Timing Controller (TCON)");
100261413Sbr	return (BUS_PROBE_DEFAULT);
101261413Sbr}
102261413Sbr
103261413Sbrstatic int
104261413Sbrtcon_attach(device_t dev)
105261413Sbr{
106261413Sbr	struct tcon_softc *sc;
107261413Sbr
108261413Sbr	sc = device_get_softc(dev);
109261413Sbr
110261413Sbr	if (bus_alloc_resources(dev, tcon_spec, sc->res)) {
111261413Sbr		device_printf(dev, "could not allocate resources\n");
112261413Sbr		return (ENXIO);
113261413Sbr	}
114261413Sbr
115261413Sbr	/* Memory interface */
116261413Sbr	sc->bst = rman_get_bustag(sc->res[0]);
117261413Sbr	sc->bsh = rman_get_bushandle(sc->res[0]);
118261413Sbr
119261413Sbr	tcon_sc = sc;
120261413Sbr
121261413Sbr	return (0);
122261413Sbr}
123261413Sbr
124261413Sbrstatic device_method_t tcon_methods[] = {
125261413Sbr	DEVMETHOD(device_probe,		tcon_probe),
126261413Sbr	DEVMETHOD(device_attach,	tcon_attach),
127261413Sbr	{ 0, 0 }
128261413Sbr};
129261413Sbr
130261413Sbrstatic driver_t tcon_driver = {
131261413Sbr	"tcon",
132261413Sbr	tcon_methods,
133261413Sbr	sizeof(struct tcon_softc),
134261413Sbr};
135261413Sbr
136261413Sbrstatic devclass_t tcon_devclass;
137261413Sbr
138261413SbrDRIVER_MODULE(tcon, simplebus, tcon_driver, tcon_devclass, 0, 0);
139