tx39io.c revision 1.7 1 /* $NetBSD: tx39io.c,v 1.7 2000/10/22 10:42:32 uch Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 #undef TX39IODEBUG
39 #include "opt_tx39_debug.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44
45 #include <machine/bus.h>
46
47 #include <hpcmips/tx/tx39var.h>
48 #include <hpcmips/tx/tx39icureg.h>
49 #define __TX39IO_PRIVATE
50 #include <hpcmips/tx/tx39iovar.h>
51 #include <hpcmips/tx/tx39ioreg.h>
52
53 #ifdef TX39IODEBUG
54 #define DPRINTF(arg) printf arg
55 #else
56 #define DPRINTF(arg)
57 #endif
58
59 #define ISSET(x, s) ((x) & (1 << (s)))
60
61 int tx39io_match(struct device *, struct cfdata *, void *);
62 void tx39io_attach(struct device *, struct device *, void *);
63
64 struct cfattach tx39io_ca = {
65 sizeof(struct tx39io_softc), tx39io_match, tx39io_attach
66 };
67
68 /* IO/MFIO common */
69 static void *port_intr_establish(void *, int, int (*)(void *), void *);
70 static void port_intr_disestablish(void *, void *);
71 /* MFIO */
72 static int mfio_in(void *, int);
73 static void mfio_out(void *, int, int);
74 static void mfio_intr_map(void *, int, int *, int *);
75 static void mfio_dump(void *);
76 static void mfio_update(void *);
77 /* IO */
78 #ifdef TX391X
79 static int tx391x_io_in(void *, int);
80 static void tx391x_io_out(void *, int, int);
81 static void tx391x_io_update(void *);
82 static void tx391x_io_intr_map(void *, int, int *, int *);
83 #endif
84 #ifdef TX392X
85 static int tx392x_io_in(void *, int);
86 static void tx392x_io_out(void *, int, int);
87 static void tx392x_io_update(void *);
88 static void tx392x_io_intr_map(void *, int, int *, int *);
89 #endif
90 static void io_dump(void *);
91
92 static void __print_port_status(struct tx39io_port_status *, int);
93
94 int
95 tx39io_match(struct device *parent, struct cfdata *cf, void *aux)
96 {
97 return ATTACH_FIRST; /* 1st attach group of txsim */
98 }
99
100 void
101 tx39io_attach(struct device *parent, struct device *self, void *aux)
102 {
103 struct txsim_attach_args *ta = aux;
104 struct tx39io_softc *sc = (void *)self;
105 tx_chipset_tag_t tc;
106 struct txio_ops *ops_io = &sc->sc_io_ops;
107 struct txio_ops *ops_mfio = &sc->sc_mfio_ops;
108
109 tc = sc->sc_tc = ta->ta_tc;
110
111 printf("\n");
112 sc->sc_stat_io_mask = ~(1 << 5); /* exclude Plum2 INT */
113 sc->sc_stat_mfio_mask = ~(0x3|(0x3 << 23));
114
115 /* IO */
116 ops_io->_v = sc;
117 ops_io->_group = IO;
118 ops_io->_intr_establish = port_intr_establish;
119 ops_io->_intr_disestablish = port_intr_disestablish;
120 ops_io->_dump = io_dump;
121 if (IS_TX391X(tc)) {
122 #ifdef TX391X
123 ops_io->_in = tx391x_io_in;
124 ops_io->_out = tx391x_io_out;
125 ops_io->_intr_map = tx391x_io_intr_map;
126 ops_io->_update = tx391x_io_update;
127 #endif
128 } else if (IS_TX392X(tc)) {
129 #ifdef TX392X
130 ops_io->_in = tx392x_io_in;
131 ops_io->_out = tx392x_io_out;
132 ops_io->_intr_map = tx392x_io_intr_map;
133 ops_io->_update = tx392x_io_update;
134 #endif
135 }
136 tx_conf_register_ioman(tc, ops_io);
137
138 /* MFIO */
139 ops_mfio->_v = sc;
140 ops_mfio->_group = MFIO;
141 ops_mfio->_in = mfio_in;
142 ops_mfio->_out = mfio_out;
143 ops_mfio->_intr_map = mfio_intr_map;
144 ops_mfio->_intr_establish = port_intr_establish;
145 ops_mfio->_intr_disestablish = port_intr_disestablish;
146 ops_mfio->_update = mfio_update;
147 ops_mfio->_dump = mfio_dump;
148
149 tx_conf_register_ioman(tc, ops_mfio);
150
151 tx_ioman_update(IO);
152 tx_ioman_update(MFIO);
153
154 #ifdef TX39IODEBUG
155 tx_ioman_dump(IO);
156 tx_ioman_dump(MFIO);
157 #else
158 printf("IO i0x%08x o0x%08x MFIO i0x%08x o0x%08x\n",
159 sc->sc_stat_io.in, sc->sc_stat_io.out,
160 sc->sc_stat_mfio.in, sc->sc_stat_mfio.out);
161 #endif
162 }
163
164 /*
165 * TX391X, TX392X common
166 */
167 static void *
168 port_intr_establish(void *arg, int edge, int (*func)(void *), void *func_arg)
169 {
170 struct tx39io_softc *sc = arg;
171 return tx_intr_establish(sc->sc_tc, edge, IST_EDGE, IPL_CLOCK, func,
172 func_arg);
173 }
174
175 static void
176 port_intr_disestablish(void *arg, void *ih)
177 {
178 struct tx39io_softc *sc = arg;
179 tx_intr_disestablish(sc->sc_tc, ih);
180 }
181
182 static void
183 mfio_out(void *arg, int port, int onoff)
184 {
185 struct tx39io_softc *sc = arg;
186 tx_chipset_tag_t tc;
187 txreg_t reg, pos;
188
189 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
190 tc = sc->sc_tc;
191 /* MFIO */
192 pos = 1 << port;
193 #ifdef DIAGNOSTIC
194 if (!(sc->sc_stat_mfio.dir & pos)) {
195 panic("%s: MFIO%d is not output port.\n",
196 sc->sc_dev.dv_xname, port);
197 }
198 #endif
199 reg = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
200 if (onoff)
201 reg |= pos;
202 else
203 reg &= ~pos;
204 tx_conf_write(tc, TX39_IOMFIODATAOUT_REG, reg);
205 }
206
207 static int
208 mfio_in(void *arg, int port)
209 {
210 struct tx39io_softc *sc __attribute__((__unused__)) = arg ;
211 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
212 return tx_conf_read(sc->sc_tc, TX39_IOMFIODATAIN_REG) & (1 << port);
213 }
214
215 static void
216 mfio_intr_map(void *arg, int port, int *pedge, int *nedge)
217 {
218 if (pedge)
219 *pedge = MAKEINTR(3, (1 << port));
220 if (nedge)
221 *nedge = MAKEINTR(4, (1 << port));
222 }
223
224 static void
225 mfio_update(void *arg)
226 {
227 struct tx39io_softc *sc = arg;
228 tx_chipset_tag_t tc = sc->sc_tc;
229 struct tx39io_port_status *stat_mfio = &sc->sc_stat_mfio;
230
231 sc->sc_ostat_mfio = *stat_mfio; /* save old status */
232 stat_mfio->dir = tx_conf_read(tc, TX39_IOMFIODATADIR_REG);
233 stat_mfio->in = tx_conf_read(tc, TX39_IOMFIODATAIN_REG);
234 stat_mfio->out = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
235 stat_mfio->power = tx_conf_read(tc, TX39_IOMFIOPOWERDWN_REG);
236 stat_mfio->u.select = tx_conf_read(tc, TX39_IOMFIODATASEL_REG);
237 }
238
239 #ifdef TX391X
240 /*
241 * TMPR3912 specific
242 */
243 int
244 tx391x_io_in(void *arg, int port)
245 {
246 struct tx39io_softc *sc __attribute__((__unused__)) = arg;
247 txreg_t reg = tx_conf_read(sc->sc_tc, TX39_IOCTRL_REG);
248
249 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
250 return TX391X_IOCTRL_IODIN(reg) & (1 << port);
251 }
252
253 void
254 tx391x_io_out(void *arg, int port, int onoff)
255 {
256 #ifdef DIAGNOSTIC
257 const char *devname;
258 #endif
259 struct tx39io_softc *sc = arg;
260 tx_chipset_tag_t tc;
261 txreg_t reg, pos, iostat;
262
263 KASSERT(sc);
264 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
265
266 devname = sc->sc_dev.dv_xname;
267 tc = sc->sc_tc;
268
269 /* IO [0:6] */
270 pos = 1 << port;
271 #ifdef DIAGNOSTIC
272 if (!(sc->sc_stat_io.dir & pos))
273 panic("%s: IO%d is not output port.\n", devname, port);
274 #endif
275 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
276 iostat = TX391X_IOCTRL_IODOUT(reg);
277 if (onoff)
278 iostat |= pos;
279 else
280 iostat &= ~pos;
281 TX391X_IOCTRL_IODOUT_CLR(reg);
282 reg = TX391X_IOCTRL_IODOUT_SET(reg, iostat);
283 tx_conf_write(tc, TX39_IOCTRL_REG, reg);
284 }
285
286 void
287 tx391x_io_intr_map(void *arg, int port, int *pedge, int *nedge)
288 {
289 if (pedge)
290 *pedge = MAKEINTR(5, (1 << (port + 7)));
291 if (nedge)
292 *nedge = MAKEINTR(5, (1 << port));
293 }
294
295 void
296 tx391x_io_update(void *arg)
297 {
298 struct tx39io_softc *sc = arg;
299 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
300 tx_chipset_tag_t tc = sc->sc_tc;
301 txreg_t reg;
302
303 /* IO [0:6] */
304 sc->sc_ostat_io = *stat_io; /* save old status */
305 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
306 stat_io->dir = TX391X_IOCTRL_IODIREC(reg);
307 stat_io->in = TX391X_IOCTRL_IODIN(reg);
308 stat_io->out = TX391X_IOCTRL_IODOUT(reg);
309 stat_io->u.debounce = TX391X_IOCTRL_IODEBSEL(reg);
310 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
311 stat_io->power = TX391X_IOIOPOWERDWN_IOPD(reg);
312 }
313 #endif /* TX391X */
314
315 #ifdef TX392X
316 /*
317 * TMPR3922 specific
318 */
319 int
320 tx392x_io_in(void *arg, int port)
321 {
322 struct tx39io_softc *sc __attribute__((__unused__)) = arg;
323 txreg_t reg = tx_conf_read(sc->sc_tc, TX392X_IODATAINOUT_REG);
324 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
325
326 return TX392X_IODATAINOUT_DIN(reg) & (1 << port);
327 }
328
329 void
330 tx392x_io_out(void *arg, int port, int onoff)
331 {
332 struct tx39io_softc *sc = arg;
333 #ifdef DIAGNOSTIC
334 const char *devname = sc->sc_dev.dv_xname;
335 #endif
336 tx_chipset_tag_t tc = sc->sc_tc;
337 txreg_t reg, pos, iostat;
338
339 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
340 /* IO [0:15] */
341 pos = 1 << port;
342 #ifdef DIAGNOSTIC
343 if (!(sc->sc_status.dir_io & pos))
344 panic("%s: IO%d is not output port.\n", devname, port);
345 #endif
346 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
347 iostat = TX392X_IODATAINOUT_DOUT(reg);
348 if (onoff)
349 iostat |= pos;
350 else
351 iostat &= ~pos;
352 TX392X_IODATAINOUT_DOUT_CLR(reg);
353 reg = TX392X_IODATAINOUT_DOUT_SET(reg, iostat);
354 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
355 }
356
357 void
358 tx392x_io_intr_map(void *arg, int port, int *pedge, int *nedge)
359 {
360 if (pedge)
361 *pedge = MAKEINTR(8, (1 << (port + 16)));
362 if (nedge)
363 *nedge = MAKEINTR(8, (1 << port));
364 }
365
366 void
367 tx392x_io_update(void *arg)
368 {
369 struct tx39io_softc *sc = arg;
370 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
371 tx_chipset_tag_t tc = sc->sc_tc;
372 txreg_t reg;
373
374 sc->sc_ostat_io = *stat_io; /* save old status */
375 /* IO [0:15] */
376 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
377 stat_io->dir = TX392X_IOCTRL_IODIREC(reg);
378 stat_io->u.debounce = TX392X_IOCTRL_IODEBSEL(reg);
379 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
380 stat_io->in = TX392X_IODATAINOUT_DIN(reg);
381 stat_io->out = TX392X_IODATAINOUT_DOUT(reg);
382 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
383 stat_io->power = TX392X_IOIOPOWERDWN_IOPD(reg);
384 }
385
386 #endif /* TX392X */
387
388 static const char *line = "--------------------------------------------------"
389 "------------\n";
390 static void
391 mfio_dump(void *arg)
392 {
393 struct tx39io_softc *sc = arg;
394 const struct tx39io_mfio_map *map = tx39io_get_mfio_map(tc);
395 struct tx39io_port_status *stat;
396 int i;
397
398 printf("%s", line);
399 stat = &sc->sc_stat_mfio;
400 for (i = TX39_IO_MFIO_MAX - 1; i >= 0 ; i--) {
401 /* MFIO port has power down state */
402 printf("MFIO %2d: - ", i);
403 __print_port_status(stat, i);
404 printf(ISSET(stat->u.select, i) ? " MFIO(%s)\n" : " %s\n",
405 map[i].std_pin_name);
406 }
407 printf("%s", line);
408 }
409
410 static void
411 io_dump(void *arg)
412 {
413 struct tx39io_softc *sc = arg;
414 struct tx39io_port_status *stat;
415 int i;
416
417 printf("%s Debounce Direction DataOut DataIn PowerDown Select"
418 "\n%s", line, line);
419 stat = &sc->sc_stat_io;
420 for (i = tx39io_get_io_max(tc) - 1; i >= 0 ; i--) {
421 /* IO port has debouncer */
422 printf("IO %2d: %s ", i,
423 ISSET(stat->u.debounce, i) ? "On " : "Off");
424 __print_port_status(stat, i);
425 printf(" -\n");
426 }
427 }
428
429 static void
430 __print_port_status(struct tx39io_port_status *stat, int i)
431 {
432 printf("%s %d %d %s",
433 ISSET(stat->dir, i) ? "Out" : "In ",
434 ISSET(stat->out, i) ? 1 : 0,
435 ISSET(stat->in, i) ? 1 : 0,
436 ISSET(stat->power, i) ? "Down ": "Active");
437 }
438
439