rdcpcib.c revision 1.1.2.2 1 1.1.2.2 rmind /* $NetBSD: rdcpcib.c,v 1.1.2.2 2011/04/21 01:41:32 rmind Exp $ */
2 1.1.2.2 rmind
3 1.1.2.2 rmind /*
4 1.1.2.2 rmind * Copyright (c) 2011 Manuel Bouyer.
5 1.1.2.2 rmind *
6 1.1.2.2 rmind * Redistribution and use in source and binary forms, with or without
7 1.1.2.2 rmind * modification, are permitted provided that the following conditions
8 1.1.2.2 rmind * are met:
9 1.1.2.2 rmind * 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer.
11 1.1.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 rmind * documentation and/or other materials provided with the distribution.
14 1.1.2.2 rmind *
15 1.1.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.2.2 rmind * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1.2.2 rmind * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1.2.2 rmind * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1.2.2 rmind * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1.2.2 rmind * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1.2.2 rmind * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1.2.2 rmind * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1.2.2 rmind * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1.2.2 rmind * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1.2.2 rmind */
26 1.1.2.2 rmind
27 1.1.2.2 rmind /*
28 1.1.2.2 rmind * driver for the RDC vortex86/PMX-1000 SoC PCI-ISA bridge, which also drives
29 1.1.2.2 rmind * the watchdog timer
30 1.1.2.2 rmind */
31 1.1.2.2 rmind
32 1.1.2.2 rmind
33 1.1.2.2 rmind #include <sys/cdefs.h>
34 1.1.2.2 rmind __KERNEL_RCSID(0, "$NetBSD: rdcpcib.c,v 1.1.2.2 2011/04/21 01:41:32 rmind Exp $");
35 1.1.2.2 rmind
36 1.1.2.2 rmind #include <sys/types.h>
37 1.1.2.2 rmind #include <sys/param.h>
38 1.1.2.2 rmind #include <sys/systm.h>
39 1.1.2.2 rmind #include <sys/device.h>
40 1.1.2.2 rmind #include <sys/sysctl.h>
41 1.1.2.2 rmind #include <sys/timetc.h>
42 1.1.2.2 rmind #include <sys/gpio.h>
43 1.1.2.2 rmind #include <machine/bus.h>
44 1.1.2.2 rmind
45 1.1.2.2 rmind #include <dev/pci/pcivar.h>
46 1.1.2.2 rmind #include <dev/pci/pcireg.h>
47 1.1.2.2 rmind #include <dev/pci/pcidevs.h>
48 1.1.2.2 rmind
49 1.1.2.2 rmind #include <dev/gpio/gpiovar.h>
50 1.1.2.2 rmind #include <dev/sysmon/sysmonvar.h>
51 1.1.2.2 rmind
52 1.1.2.2 rmind #include "pcibvar.h"
53 1.1.2.2 rmind
54 1.1.2.2 rmind /*
55 1.1.2.2 rmind * special registers: iospace-registers for indirect access to timer and GPIO
56 1.1.2.2 rmind */
57 1.1.2.2 rmind #define RDC_IND_BASE 0x22
58 1.1.2.2 rmind #define RDC_IND_SIZE 0x2
59 1.1.2.2 rmind #define RDC_IND_ADDR 0
60 1.1.2.2 rmind #define RDC_IND_DATA 1
61 1.1.2.2 rmind
62 1.1.2.2 rmind struct rdcpcib_softc {
63 1.1.2.2 rmind struct pcib_softc rdc_pcib;
64 1.1.2.2 rmind
65 1.1.2.2 rmind /* indirect registers mapping */
66 1.1.2.2 rmind bus_space_tag_t rdc_iot;
67 1.1.2.2 rmind bus_space_handle_t rdc_ioh;
68 1.1.2.2 rmind
69 1.1.2.2 rmind /* Watchdog suppoprt */
70 1.1.2.2 rmind struct sysmon_wdog rdc_smw;
71 1.1.2.2 rmind };
72 1.1.2.2 rmind
73 1.1.2.2 rmind static int rdcpcibmatch(device_t, cfdata_t, void *);
74 1.1.2.2 rmind static void rdcpcibattach(device_t, device_t, void *);
75 1.1.2.2 rmind static int rdcpcibdetach(device_t, int);
76 1.1.2.2 rmind
77 1.1.2.2 rmind static uint8_t rdc_ind_read(struct rdcpcib_softc *, uint8_t);
78 1.1.2.2 rmind static void rdc_ind_write(struct rdcpcib_softc *, uint8_t, uint8_t);
79 1.1.2.2 rmind
80 1.1.2.2 rmind static void rdc_wdtimer_configure(device_t);
81 1.1.2.2 rmind static int rdc_wdtimer_unconfigure(device_t, int);
82 1.1.2.2 rmind static int rdc_wdtimer_setmode(struct sysmon_wdog *);
83 1.1.2.2 rmind static int rdc_wdtimer_tickle(struct sysmon_wdog *);
84 1.1.2.2 rmind static void rdc_wdtimer_stop(struct rdcpcib_softc *);
85 1.1.2.2 rmind static void rdc_wdtimer_start(struct rdcpcib_softc *);
86 1.1.2.2 rmind
87 1.1.2.2 rmind CFATTACH_DECL2_NEW(rdcpcib, sizeof(struct rdcpcib_softc),
88 1.1.2.2 rmind rdcpcibmatch, rdcpcibattach, rdcpcibdetach, NULL,
89 1.1.2.2 rmind pcibrescan, pcibchilddet);
90 1.1.2.2 rmind
91 1.1.2.2 rmind static int
92 1.1.2.2 rmind rdcpcibmatch(device_t parent, cfdata_t match, void *aux)
93 1.1.2.2 rmind {
94 1.1.2.2 rmind struct pci_attach_args *pa = aux;
95 1.1.2.2 rmind
96 1.1.2.2 rmind if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
97 1.1.2.2 rmind PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
98 1.1.2.2 rmind return 0;
99 1.1.2.2 rmind
100 1.1.2.2 rmind if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RDC &&
101 1.1.2.2 rmind PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RDC_PCIB)
102 1.1.2.2 rmind return 10;
103 1.1.2.2 rmind
104 1.1.2.2 rmind return 0;
105 1.1.2.2 rmind }
106 1.1.2.2 rmind
107 1.1.2.2 rmind static void
108 1.1.2.2 rmind rdcpcibattach(device_t parent, device_t self, void *aux)
109 1.1.2.2 rmind {
110 1.1.2.2 rmind struct rdcpcib_softc *sc = device_private(self);
111 1.1.2.2 rmind
112 1.1.2.2 rmind /* generic PCI/ISA bridge */
113 1.1.2.2 rmind pcibattach(parent, self, aux);
114 1.1.2.2 rmind
115 1.1.2.2 rmind /* map indirect registers */
116 1.1.2.2 rmind sc->rdc_iot = x86_bus_space_io;
117 1.1.2.2 rmind if (bus_space_map(sc->rdc_iot, RDC_IND_BASE, RDC_IND_SIZE, 0,
118 1.1.2.2 rmind &sc->rdc_ioh) != 0) {
119 1.1.2.2 rmind aprint_error_dev(self, "couldn't map indirect registers\n");
120 1.1.2.2 rmind return;
121 1.1.2.2 rmind }
122 1.1.2.2 rmind
123 1.1.2.2 rmind /* Set up the watchdog. */
124 1.1.2.2 rmind rdc_wdtimer_configure(self);
125 1.1.2.2 rmind
126 1.1.2.2 rmind /* Install power handler XXX */
127 1.1.2.2 rmind if (!pmf_device_register(self, NULL, NULL))
128 1.1.2.2 rmind aprint_error_dev(self, "couldn't establish power handler\n");
129 1.1.2.2 rmind }
130 1.1.2.2 rmind
131 1.1.2.2 rmind static int
132 1.1.2.2 rmind rdcpcibdetach(device_t self, int flags)
133 1.1.2.2 rmind {
134 1.1.2.2 rmind struct rdcpcib_softc *sc = device_private(self);
135 1.1.2.2 rmind int rc;
136 1.1.2.2 rmind
137 1.1.2.2 rmind pmf_device_deregister(self);
138 1.1.2.2 rmind
139 1.1.2.2 rmind if ((rc = rdc_wdtimer_unconfigure(self, flags)) != 0)
140 1.1.2.2 rmind return rc;
141 1.1.2.2 rmind
142 1.1.2.2 rmind bus_space_unmap(sc->rdc_iot, sc->rdc_ioh, RDC_IND_SIZE);
143 1.1.2.2 rmind return pcibdetach(self, flags);
144 1.1.2.2 rmind }
145 1.1.2.2 rmind
146 1.1.2.2 rmind /* indirect registers read/write */
147 1.1.2.2 rmind static uint8_t
148 1.1.2.2 rmind rdc_ind_read(struct rdcpcib_softc *sc, uint8_t addr)
149 1.1.2.2 rmind {
150 1.1.2.2 rmind bus_space_write_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_ADDR, addr);
151 1.1.2.2 rmind return bus_space_read_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_DATA);
152 1.1.2.2 rmind }
153 1.1.2.2 rmind
154 1.1.2.2 rmind static void
155 1.1.2.2 rmind rdc_ind_write(struct rdcpcib_softc *sc, uint8_t addr, uint8_t data)
156 1.1.2.2 rmind {
157 1.1.2.2 rmind bus_space_write_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_ADDR, addr);
158 1.1.2.2 rmind bus_space_write_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_DATA, data);
159 1.1.2.2 rmind }
160 1.1.2.2 rmind
161 1.1.2.2 rmind /*
162 1.1.2.2 rmind * watchdog timer registers
163 1.1.2.2 rmind */
164 1.1.2.2 rmind
165 1.1.2.2 rmind /* control */
166 1.1.2.2 rmind #define RDC_WDT0_CTRL 0x37
167 1.1.2.2 rmind #define RDC_WDT0_CTRL_EN 0x40
168 1.1.2.2 rmind
169 1.1.2.2 rmind /* signal select */
170 1.1.2.2 rmind #define RDC_WDT0_SSEL 0x38
171 1.1.2.2 rmind #define RDC_WDT0_SSEL_MSK 0xf0
172 1.1.2.2 rmind #define RDC_WDT0_SSEL_NMI 0xc0
173 1.1.2.2 rmind #define RDC_WDT0_SSEL_RST 0xd0
174 1.1.2.2 rmind
175 1.1.2.2 rmind /* counter */
176 1.1.2.2 rmind #define RDC_WDT0_CNTL 0x39
177 1.1.2.2 rmind #define RDC_WDT0_CNTH 0x3A
178 1.1.2.2 rmind #define RDC_WDT0_CNTU 0x3B
179 1.1.2.2 rmind #define RDC_WDT0_FREQ 32768 /* Hz */
180 1.1.2.2 rmind #define RDC_WDT0_PERIOD_MAX (1 << 24)
181 1.1.2.2 rmind
182 1.1.2.2 rmind /* clear counter */
183 1.1.2.2 rmind #define RDC_WDT0_CTRL1 0x3c
184 1.1.2.2 rmind #define RDC_WDT0_CTRL1_RELOAD 0x40
185 1.1.2.2 rmind #define RDC_WDT0_CRTL1_FIRE 0x80
186 1.1.2.2 rmind
187 1.1.2.2 rmind
188 1.1.2.2 rmind /*
189 1.1.2.2 rmind * Initialize the watchdog timer.
190 1.1.2.2 rmind */
191 1.1.2.2 rmind static void
192 1.1.2.2 rmind rdc_wdtimer_configure(device_t self)
193 1.1.2.2 rmind {
194 1.1.2.2 rmind struct rdcpcib_softc *sc = device_private(self);
195 1.1.2.2 rmind uint8_t reg;
196 1.1.2.2 rmind
197 1.1.2.2 rmind /* Explicitly stop the timer. */
198 1.1.2.2 rmind rdc_wdtimer_stop(sc);
199 1.1.2.2 rmind
200 1.1.2.2 rmind /*
201 1.1.2.2 rmind * Register the driver with the sysmon watchdog framework.
202 1.1.2.2 rmind */
203 1.1.2.2 rmind sc->rdc_smw.smw_name = device_xname(self);
204 1.1.2.2 rmind sc->rdc_smw.smw_cookie = sc;
205 1.1.2.2 rmind sc->rdc_smw.smw_setmode = rdc_wdtimer_setmode;
206 1.1.2.2 rmind sc->rdc_smw.smw_tickle = rdc_wdtimer_tickle;
207 1.1.2.2 rmind sc->rdc_smw.smw_period = RDC_WDT0_PERIOD_MAX / RDC_WDT0_FREQ;
208 1.1.2.2 rmind
209 1.1.2.2 rmind if (sysmon_wdog_register(&sc->rdc_smw)) {
210 1.1.2.2 rmind aprint_error_dev(self, "unable to register wdt"
211 1.1.2.2 rmind "as a sysmon watchdog device.\n");
212 1.1.2.2 rmind return;
213 1.1.2.2 rmind }
214 1.1.2.2 rmind
215 1.1.2.2 rmind aprint_verbose_dev(self, "watchdog timer configured.\n");
216 1.1.2.2 rmind reg = rdc_ind_read(sc, RDC_WDT0_CTRL1);
217 1.1.2.2 rmind if (reg & RDC_WDT0_CRTL1_FIRE) {
218 1.1.2.2 rmind aprint_error_dev(self, "watchdog fired bit set, clearing\n");
219 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CTRL1, reg & ~RDC_WDT0_CRTL1_FIRE);
220 1.1.2.2 rmind }
221 1.1.2.2 rmind }
222 1.1.2.2 rmind
223 1.1.2.2 rmind static int
224 1.1.2.2 rmind rdc_wdtimer_unconfigure(device_t self, int flags)
225 1.1.2.2 rmind {
226 1.1.2.2 rmind struct rdcpcib_softc *sc = device_private(self);
227 1.1.2.2 rmind int rc;
228 1.1.2.2 rmind
229 1.1.2.2 rmind if ((rc = sysmon_wdog_unregister(&sc->rdc_smw)) != 0) {
230 1.1.2.2 rmind if (rc == ERESTART)
231 1.1.2.2 rmind rc = EINTR;
232 1.1.2.2 rmind return rc;
233 1.1.2.2 rmind }
234 1.1.2.2 rmind
235 1.1.2.2 rmind /* Explicitly stop the timer. */
236 1.1.2.2 rmind rdc_wdtimer_stop(sc);
237 1.1.2.2 rmind
238 1.1.2.2 rmind return 0;
239 1.1.2.2 rmind }
240 1.1.2.2 rmind
241 1.1.2.2 rmind
242 1.1.2.2 rmind /*
243 1.1.2.2 rmind * Sysmon watchdog callbacks.
244 1.1.2.2 rmind */
245 1.1.2.2 rmind static int
246 1.1.2.2 rmind rdc_wdtimer_setmode(struct sysmon_wdog *smw)
247 1.1.2.2 rmind {
248 1.1.2.2 rmind struct rdcpcib_softc *sc = smw->smw_cookie;
249 1.1.2.2 rmind unsigned int period;
250 1.1.2.2 rmind
251 1.1.2.2 rmind if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
252 1.1.2.2 rmind /* Stop the timer. */
253 1.1.2.2 rmind rdc_wdtimer_stop(sc);
254 1.1.2.2 rmind } else {
255 1.1.2.2 rmind period = smw->smw_period * RDC_WDT0_FREQ;
256 1.1.2.2 rmind if (period < 1 ||
257 1.1.2.2 rmind period > RDC_WDT0_PERIOD_MAX)
258 1.1.2.2 rmind return EINVAL;
259 1.1.2.2 rmind period = period - 1;
260 1.1.2.2 rmind
261 1.1.2.2 rmind /* Stop the timer, */
262 1.1.2.2 rmind rdc_wdtimer_stop(sc);
263 1.1.2.2 rmind
264 1.1.2.2 rmind /* set the timeout, */
265 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CNTL, (period >> 0) & 0xff);
266 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CNTH, (period >> 8) & 0xff);
267 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CNTU, (period >> 16) & 0xff);
268 1.1.2.2 rmind
269 1.1.2.2 rmind /* and start the timer again */
270 1.1.2.2 rmind rdc_wdtimer_start(sc);
271 1.1.2.2 rmind }
272 1.1.2.2 rmind return 0;
273 1.1.2.2 rmind }
274 1.1.2.2 rmind
275 1.1.2.2 rmind static int
276 1.1.2.2 rmind rdc_wdtimer_tickle(struct sysmon_wdog *smw)
277 1.1.2.2 rmind {
278 1.1.2.2 rmind struct rdcpcib_softc *sc = smw->smw_cookie;
279 1.1.2.2 rmind uint8_t reg;
280 1.1.2.2 rmind
281 1.1.2.2 rmind reg = rdc_ind_read(sc, RDC_WDT0_CTRL1);
282 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CTRL1, reg | RDC_WDT0_CTRL1_RELOAD);
283 1.1.2.2 rmind return 0;
284 1.1.2.2 rmind }
285 1.1.2.2 rmind
286 1.1.2.2 rmind static void
287 1.1.2.2 rmind rdc_wdtimer_stop(struct rdcpcib_softc *sc)
288 1.1.2.2 rmind {
289 1.1.2.2 rmind uint8_t reg;
290 1.1.2.2 rmind reg = rdc_ind_read(sc, RDC_WDT0_CTRL);
291 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CTRL, reg & ~RDC_WDT0_CTRL_EN);
292 1.1.2.2 rmind }
293 1.1.2.2 rmind
294 1.1.2.2 rmind static void
295 1.1.2.2 rmind rdc_wdtimer_start(struct rdcpcib_softc *sc)
296 1.1.2.2 rmind {
297 1.1.2.2 rmind uint8_t reg;
298 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_SSEL, RDC_WDT0_SSEL_RST);
299 1.1.2.2 rmind reg = rdc_ind_read(sc, RDC_WDT0_CTRL);
300 1.1.2.2 rmind rdc_ind_write(sc, RDC_WDT0_CTRL, reg | RDC_WDT0_CTRL_EN);
301 1.1.2.2 rmind }
302