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