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