xref: /NextBSD/sys/arm/lpc/lpc_pwr.c (revision 95f7c2f56c7268d6ed9c2a56d357aeeac260363b)
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 
47 struct 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 
54 static struct lpc_pwr_softc *lpc_pwr_sc = NULL;
55 
56 static int lpc_pwr_probe(device_t);
57 static 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 
64 static int
lpc_pwr_probe(device_t dev)65 lpc_pwr_probe(device_t dev)
66 {
67 
68 	if (!ofw_bus_status_okay(dev))
69 		return (ENXIO);
70 
71 	if (!ofw_bus_is_compatible(dev, "lpc,pwr"))
72 		return (ENXIO);
73 
74 	device_set_desc(dev, "LPC32x0 Power Controller");
75 	return (BUS_PROBE_DEFAULT);
76 }
77 
78 static int
lpc_pwr_attach(device_t dev)79 lpc_pwr_attach(device_t dev)
80 {
81 	struct lpc_pwr_softc *sc = device_get_softc(dev);
82 	int rid;
83 
84 	sc->dp_dev = dev;
85 
86 	rid = 0;
87 	sc->dp_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
88 	    RF_ACTIVE);
89 	if (!sc->dp_mem_res) {
90 		device_printf(dev, "cannot allocate memory window\n");
91 		return (ENXIO);
92 	}
93 
94 	sc->dp_bst = rman_get_bustag(sc->dp_mem_res);
95 	sc->dp_bsh = rman_get_bushandle(sc->dp_mem_res);
96 
97 	lpc_pwr_sc = sc;
98 
99 	return (0);
100 }
101 
102 uint32_t
lpc_pwr_read(device_t dev,int reg)103 lpc_pwr_read(device_t dev, int reg)
104 {
105 	return (lpc_pwr_read_4(lpc_pwr_sc, reg));
106 }
107 
108 void
lpc_pwr_write(device_t dev,int reg,uint32_t value)109 lpc_pwr_write(device_t dev, int reg, uint32_t value)
110 {
111 	lpc_pwr_write_4(lpc_pwr_sc, reg, value);
112 }
113 
114 static device_method_t lpc_pwr_methods[] = {
115 	/* Device interface */
116 	DEVMETHOD(device_probe,		lpc_pwr_probe),
117 	DEVMETHOD(device_attach,	lpc_pwr_attach),
118 	{ 0, 0 }
119 };
120 
121 static devclass_t lpc_pwr_devclass;
122 
123 static driver_t lpc_pwr_driver = {
124 	"pwr",
125 	lpc_pwr_methods,
126 	sizeof(struct lpc_pwr_softc),
127 };
128 
129 DRIVER_MODULE(pwr, simplebus, lpc_pwr_driver, lpc_pwr_devclass, 0, 0);
130