1/*-
2 * Copyright (c) 2011 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bus.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/malloc.h>
37#include <sys/rman.h>
38#include <machine/bus.h>
39#include <machine/intr.h>
40
41#include <dev/ofw/ofw_bus.h>
42#include <dev/ofw/ofw_bus_subr.h>
43
44#include <arm/lpc/lpcreg.h>
45#include <arm/lpc/lpcvar.h>
46
47struct lpc_pwr_softc {
48	device_t		dp_dev;
49	struct resource *	dp_mem_res;
50	bus_space_tag_t		dp_bst;
51	bus_space_handle_t	dp_bsh;
52};
53
54static struct lpc_pwr_softc *lpc_pwr_sc = NULL;
55
56static int lpc_pwr_probe(device_t);
57static int lpc_pwr_attach(device_t);
58
59#define	lpc_pwr_read_4(_sc, _reg)			\
60    bus_space_read_4((_sc)->dp_bst, (_sc)->dp_bsh, _reg)
61#define	lpc_pwr_write_4(_sc, _reg, _val)		\
62    bus_space_write_4((_sc)->dp_bst, (_sc)->dp_bsh, _reg, _val)
63
64static int
65lpc_pwr_probe(device_t dev)
66{
67
68	if (!ofw_bus_is_compatible(dev, "lpc,pwr"))
69		return (ENXIO);
70
71	device_set_desc(dev, "LPC32x0 Power Controller");
72	return (BUS_PROBE_DEFAULT);
73}
74
75static int
76lpc_pwr_attach(device_t dev)
77{
78	struct lpc_pwr_softc *sc = device_get_softc(dev);
79	int rid;
80
81	sc->dp_dev = dev;
82
83	rid = 0;
84	sc->dp_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
85	    RF_ACTIVE);
86	if (!sc->dp_mem_res) {
87		device_printf(dev, "cannot allocate memory window\n");
88		return (ENXIO);
89	}
90
91	sc->dp_bst = rman_get_bustag(sc->dp_mem_res);
92	sc->dp_bsh = rman_get_bushandle(sc->dp_mem_res);
93
94	lpc_pwr_sc = sc;
95
96	return (0);
97}
98
99uint32_t
100lpc_pwr_read(device_t dev, int reg)
101{
102	return (lpc_pwr_read_4(lpc_pwr_sc, reg));
103}
104
105void
106lpc_pwr_write(device_t dev, int reg, uint32_t value)
107{
108	lpc_pwr_write_4(lpc_pwr_sc, reg, value);
109}
110
111static device_method_t lpc_pwr_methods[] = {
112	/* Device interface */
113	DEVMETHOD(device_probe,		lpc_pwr_probe),
114	DEVMETHOD(device_attach,	lpc_pwr_attach),
115	{ 0, 0 }
116};
117
118static devclass_t lpc_pwr_devclass;
119
120static driver_t lpc_pwr_driver = {
121	"pwr",
122	lpc_pwr_methods,
123	sizeof(struct lpc_pwr_softc),
124};
125
126DRIVER_MODULE(pwr, simplebus, lpc_pwr_driver, lpc_pwr_devclass, 0, 0);
127