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: releng/10.2/sys/arm/lpc/lpc_pwr.c 266152 2014-05-15 16:11:06Z ian $");
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
68266152Sian	if (!ofw_bus_status_okay(dev))
69266152Sian		return (ENXIO);
70266152Sian
71239278Sgonzo	if (!ofw_bus_is_compatible(dev, "lpc,pwr"))
72239278Sgonzo		return (ENXIO);
73239278Sgonzo
74239278Sgonzo	device_set_desc(dev, "LPC32x0 Power Controller");
75239278Sgonzo	return (BUS_PROBE_DEFAULT);
76239278Sgonzo}
77239278Sgonzo
78239278Sgonzostatic int
79239278Sgonzolpc_pwr_attach(device_t dev)
80239278Sgonzo{
81239278Sgonzo	struct lpc_pwr_softc *sc = device_get_softc(dev);
82239278Sgonzo	int rid;
83239278Sgonzo
84239278Sgonzo	sc->dp_dev = dev;
85239278Sgonzo
86239278Sgonzo	rid = 0;
87239278Sgonzo	sc->dp_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
88239278Sgonzo	    RF_ACTIVE);
89239278Sgonzo	if (!sc->dp_mem_res) {
90239278Sgonzo		device_printf(dev, "cannot allocate memory window\n");
91239278Sgonzo		return (ENXIO);
92239278Sgonzo	}
93239278Sgonzo
94239278Sgonzo	sc->dp_bst = rman_get_bustag(sc->dp_mem_res);
95239278Sgonzo	sc->dp_bsh = rman_get_bushandle(sc->dp_mem_res);
96239278Sgonzo
97239278Sgonzo	lpc_pwr_sc = sc;
98239278Sgonzo
99239278Sgonzo	return (0);
100239278Sgonzo}
101239278Sgonzo
102239278Sgonzouint32_t
103239278Sgonzolpc_pwr_read(device_t dev, int reg)
104239278Sgonzo{
105239278Sgonzo	return (lpc_pwr_read_4(lpc_pwr_sc, reg));
106239278Sgonzo}
107239278Sgonzo
108239278Sgonzovoid
109239278Sgonzolpc_pwr_write(device_t dev, int reg, uint32_t value)
110239278Sgonzo{
111239278Sgonzo	lpc_pwr_write_4(lpc_pwr_sc, reg, value);
112239278Sgonzo}
113239278Sgonzo
114239278Sgonzostatic device_method_t lpc_pwr_methods[] = {
115239278Sgonzo	/* Device interface */
116239278Sgonzo	DEVMETHOD(device_probe,		lpc_pwr_probe),
117239278Sgonzo	DEVMETHOD(device_attach,	lpc_pwr_attach),
118239278Sgonzo	{ 0, 0 }
119239278Sgonzo};
120239278Sgonzo
121239278Sgonzostatic devclass_t lpc_pwr_devclass;
122239278Sgonzo
123239278Sgonzostatic driver_t lpc_pwr_driver = {
124239278Sgonzo	"pwr",
125239278Sgonzo	lpc_pwr_methods,
126239278Sgonzo	sizeof(struct lpc_pwr_softc),
127239278Sgonzo};
128239278Sgonzo
129239278SgonzoDRIVER_MODULE(pwr, simplebus, lpc_pwr_driver, lpc_pwr_devclass, 0, 0);
130