lpc_pwr.c revision 239278
1239278Sgonzo/*-
2239278Sgonzo * Copyright (c) 2011 Jakub Wojciech Klama <jceel@FreeBSD.org>
3239278Sgonzo * All rights reserved.
4239278Sgonzo *
5239278Sgonzo * Redistribution and use in source and binary forms, with or without
6239278Sgonzo * modification, are permitted provided that the following conditions
7239278Sgonzo * are met:
8239278Sgonzo * 1. Redistributions of source code must retain the above copyright
9239278Sgonzo *    notice, this list of conditions and the following disclaimer.
10239278Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
11239278Sgonzo *    notice, this list of conditions and the following disclaimer in the
12239278Sgonzo *    documentation and/or other materials provided with the distribution.
13239278Sgonzo *
14239278Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15239278Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16239278Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17239278Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18239278Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19239278Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20239278Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21239278Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22239278Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23239278Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24239278Sgonzo * SUCH DAMAGE.
25239278Sgonzo *
26239278Sgonzo */
27239278Sgonzo
28239278Sgonzo#include <sys/cdefs.h>
29239278Sgonzo__FBSDID("$FreeBSD: head/sys/arm/lpc/lpc_pwr.c 239278 2012-08-15 05:37:10Z gonzo $");
30239278Sgonzo
31239278Sgonzo#include <sys/param.h>
32239278Sgonzo#include <sys/systm.h>
33239278Sgonzo#include <sys/bus.h>
34239278Sgonzo#include <sys/kernel.h>
35239278Sgonzo#include <sys/module.h>
36239278Sgonzo#include <sys/malloc.h>
37239278Sgonzo#include <sys/rman.h>
38239278Sgonzo#include <machine/bus.h>
39239278Sgonzo#include <machine/intr.h>
40239278Sgonzo
41239278Sgonzo#include <dev/ofw/ofw_bus.h>
42239278Sgonzo#include <dev/ofw/ofw_bus_subr.h>
43239278Sgonzo
44239278Sgonzo#include <arm/lpc/lpcreg.h>
45239278Sgonzo#include <arm/lpc/lpcvar.h>
46239278Sgonzo
47239278Sgonzostruct lpc_pwr_softc {
48239278Sgonzo	device_t		dp_dev;
49239278Sgonzo	struct resource *	dp_mem_res;
50239278Sgonzo	bus_space_tag_t		dp_bst;
51239278Sgonzo	bus_space_handle_t	dp_bsh;
52239278Sgonzo};
53239278Sgonzo
54239278Sgonzostatic struct lpc_pwr_softc *lpc_pwr_sc = NULL;
55239278Sgonzo
56239278Sgonzostatic int lpc_pwr_probe(device_t);
57239278Sgonzostatic int lpc_pwr_attach(device_t);
58239278Sgonzo
59239278Sgonzo#define	lpc_pwr_read_4(_sc, _reg)			\
60239278Sgonzo    bus_space_read_4((_sc)->dp_bst, (_sc)->dp_bsh, _reg)
61239278Sgonzo#define	lpc_pwr_write_4(_sc, _reg, _val)		\
62239278Sgonzo    bus_space_write_4((_sc)->dp_bst, (_sc)->dp_bsh, _reg, _val)
63239278Sgonzo
64239278Sgonzostatic int
65239278Sgonzolpc_pwr_probe(device_t dev)
66239278Sgonzo{
67239278Sgonzo
68239278Sgonzo	if (!ofw_bus_is_compatible(dev, "lpc,pwr"))
69239278Sgonzo		return (ENXIO);
70239278Sgonzo
71239278Sgonzo	device_set_desc(dev, "LPC32x0 Power Controller");
72239278Sgonzo	return (BUS_PROBE_DEFAULT);
73239278Sgonzo}
74239278Sgonzo
75239278Sgonzostatic int
76239278Sgonzolpc_pwr_attach(device_t dev)
77239278Sgonzo{
78239278Sgonzo	struct lpc_pwr_softc *sc = device_get_softc(dev);
79239278Sgonzo	int rid;
80239278Sgonzo
81239278Sgonzo	sc->dp_dev = dev;
82239278Sgonzo
83239278Sgonzo	rid = 0;
84239278Sgonzo	sc->dp_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
85239278Sgonzo	    RF_ACTIVE);
86239278Sgonzo	if (!sc->dp_mem_res) {
87239278Sgonzo		device_printf(dev, "cannot allocate memory window\n");
88239278Sgonzo		return (ENXIO);
89239278Sgonzo	}
90239278Sgonzo
91239278Sgonzo	sc->dp_bst = rman_get_bustag(sc->dp_mem_res);
92239278Sgonzo	sc->dp_bsh = rman_get_bushandle(sc->dp_mem_res);
93239278Sgonzo
94239278Sgonzo	lpc_pwr_sc = sc;
95239278Sgonzo
96239278Sgonzo	return (0);
97239278Sgonzo}
98239278Sgonzo
99239278Sgonzouint32_t
100239278Sgonzolpc_pwr_read(device_t dev, int reg)
101239278Sgonzo{
102239278Sgonzo	return (lpc_pwr_read_4(lpc_pwr_sc, reg));
103239278Sgonzo}
104239278Sgonzo
105239278Sgonzovoid
106239278Sgonzolpc_pwr_write(device_t dev, int reg, uint32_t value)
107239278Sgonzo{
108239278Sgonzo	lpc_pwr_write_4(lpc_pwr_sc, reg, value);
109239278Sgonzo}
110239278Sgonzo
111239278Sgonzostatic device_method_t lpc_pwr_methods[] = {
112239278Sgonzo	/* Device interface */
113239278Sgonzo	DEVMETHOD(device_probe,		lpc_pwr_probe),
114239278Sgonzo	DEVMETHOD(device_attach,	lpc_pwr_attach),
115239278Sgonzo	{ 0, 0 }
116239278Sgonzo};
117239278Sgonzo
118239278Sgonzostatic devclass_t lpc_pwr_devclass;
119239278Sgonzo
120239278Sgonzostatic driver_t lpc_pwr_driver = {
121239278Sgonzo	"pwr",
122239278Sgonzo	lpc_pwr_methods,
123239278Sgonzo	sizeof(struct lpc_pwr_softc),
124239278Sgonzo};
125239278Sgonzo
126239278SgonzoDRIVER_MODULE(pwr, simplebus, lpc_pwr_driver, lpc_pwr_devclass, 0, 0);
127