tx39io.c revision 1.7.4.1 1 /* $NetBSD: tx39io.c,v 1.7.4.1 2001/06/21 19:24:30 nathanw 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_disestablish(hpcio_chip_t, hpcio_intr_handle_t);
70 static void port_intr_clear(hpcio_chip_t, hpcio_intr_handle_t);
71 /* MFIO */
72 static void *mfio_intr_establish(hpcio_chip_t, int, int, int (*)(void *),
73 void *);
74 static int mfio_in(hpcio_chip_t, int);
75 static void mfio_out(hpcio_chip_t, int, int);
76 static int mfio_intr_map(int *, int, int);
77 static void mfio_dump(hpcio_chip_t);
78 static void mfio_update(hpcio_chip_t);
79 /* IO */
80 static void *io_intr_establish(hpcio_chip_t, int, int, int (*)(void *),
81 void *);
82 #ifdef TX391X
83 static int tx391x_io_in(hpcio_chip_t, int);
84 static void tx391x_io_out(hpcio_chip_t, int, int);
85 static void tx391x_io_update(hpcio_chip_t);
86 static int tx391x_io_intr_map(int *, int, int);
87 #endif
88 #ifdef TX392X
89 static int tx392x_io_in(hpcio_chip_t, int);
90 static void tx392x_io_out(hpcio_chip_t, int, int);
91 static void tx392x_io_update(hpcio_chip_t);
92 static int tx392x_io_intr_map(int *, int, int);
93 #endif
94 #if defined TX391X && defined TX392X
95 #define tx39_io_intr_map(t, s, p, m) \
96 (IS_TX391X(t)
97 ? tx391x_io_intr_map(s, p, m) : tx392x_io_intr_map(s, p, m))
98 #elif defined TX391X
99 #define tx39io_intr_map(t, s, p, m) tx391x_io_intr_map(s, p, m)
100 #elif defined TX392X
101 #define tx39io_intr_map(t, s, p, m) tx392x_io_intr_map(s, p, m)
102 #endif
103 static void io_dump(hpcio_chip_t);
104
105 static void __print_port_status(struct tx39io_port_status *, int);
106
107 int
108 tx39io_match(struct device *parent, struct cfdata *cf, void *aux)
109 {
110 return (ATTACH_FIRST); /* 1st attach group of txsim */
111 }
112
113 void
114 tx39io_attach(struct device *parent, struct device *self, void *aux)
115 {
116 struct txsim_attach_args *ta = aux;
117 struct tx39io_softc *sc = (void *)self;
118 tx_chipset_tag_t tc;
119 struct hpcio_chip *io_hc = &sc->sc_io_ops;
120 struct hpcio_chip *mfio_hc = &sc->sc_mfio_ops;
121
122 tc = sc->sc_tc = ta->ta_tc;
123
124 printf("\n");
125 sc->sc_stat_io_mask = ~(1 << 5); /* exclude Plum2 INT */
126 sc->sc_stat_mfio_mask = ~(0x3|(0x3 << 23));
127
128 /* IO */
129 io_hc->hc_chipid = IO;
130 io_hc->hc_name = "IO";
131 io_hc->hc_sc = sc;
132 io_hc->hc_intr_establish = io_intr_establish;
133 io_hc->hc_intr_disestablish = port_intr_disestablish;
134 io_hc->hc_intr_clear = port_intr_clear;
135 io_hc->hc_dump = io_dump;
136 if (IS_TX391X(tc)) {
137 #ifdef TX391X
138 io_hc->hc_portread = tx391x_io_in;
139 io_hc->hc_portwrite = tx391x_io_out;
140 io_hc->hc_update = tx391x_io_update;
141 #endif
142 } else if (IS_TX392X(tc)) {
143 #ifdef TX392X
144 io_hc->hc_portread = tx392x_io_in;
145 io_hc->hc_portwrite = tx392x_io_out;
146 io_hc->hc_update = tx392x_io_update;
147 #endif
148 }
149 tx_conf_register_ioman(tc, io_hc);
150
151 /* MFIO */
152 mfio_hc->hc_chipid = MFIO;
153 mfio_hc->hc_name = "MFIO";
154 mfio_hc->hc_sc = sc;
155 mfio_hc->hc_portread = mfio_in;
156 mfio_hc->hc_portwrite = mfio_out;
157 mfio_hc->hc_intr_establish = mfio_intr_establish;
158 mfio_hc->hc_intr_disestablish = port_intr_disestablish;
159 mfio_hc->hc_update = mfio_update;
160 mfio_hc->hc_dump = mfio_dump;
161
162 tx_conf_register_ioman(tc, mfio_hc);
163
164 hpcio_update(io_hc);
165 hpcio_update(mfio_hc);
166
167 #ifdef TX39IODEBUG
168 hpcio_dump(io_hc);
169 hpcio_dump(mfio_hc);
170 #else
171 printf("IO i0x%08x o0x%08x MFIO i0x%08x o0x%08x\n",
172 sc->sc_stat_io.in, sc->sc_stat_io.out,
173 sc->sc_stat_mfio.in, sc->sc_stat_mfio.out);
174 #endif
175 }
176
177 /*
178 * TX391X, TX392X common
179 */
180 static void *
181 io_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
182 void *func_arg)
183 {
184 struct tx39io_softc *sc = arg->hc_sc;
185 int src;
186
187 if (tx39io_intr_map(sc->sc_tc, &src, port, mode) != 0)
188 return (0);
189
190 return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
191 func_arg));
192 }
193
194 static void *
195 mfio_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
196 void *func_arg)
197 {
198 struct tx39io_softc *sc = arg->hc_sc;
199 int src;
200
201 if (mfio_intr_map(&src, port, mode) != 0)
202 return (0);
203
204 return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
205 func_arg));
206 }
207
208 static void
209 port_intr_disestablish(hpcio_chip_t arg, void *ih)
210 {
211 struct tx39io_softc *sc = arg->hc_sc;
212 tx_intr_disestablish(sc->sc_tc, ih);
213 }
214
215 static void
216 port_intr_clear(hpcio_chip_t arg, void *ih)
217 {
218 }
219
220 static void
221 mfio_out(hpcio_chip_t arg, int port, int onoff)
222 {
223 struct tx39io_softc *sc = arg->hc_sc;
224 tx_chipset_tag_t tc;
225 txreg_t reg, pos;
226
227 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
228 tc = sc->sc_tc;
229 /* MFIO */
230 pos = 1 << port;
231 #ifdef DIAGNOSTIC
232 if (!(sc->sc_stat_mfio.dir & pos)) {
233 panic("%s: MFIO%d is not output port.\n",
234 sc->sc_dev.dv_xname, port);
235 }
236 #endif
237 reg = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
238 if (onoff)
239 reg |= pos;
240 else
241 reg &= ~pos;
242 tx_conf_write(tc, TX39_IOMFIODATAOUT_REG, reg);
243 }
244
245 static int
246 mfio_in(hpcio_chip_t arg, int port)
247 {
248 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc ;
249
250 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
251 return (tx_conf_read(sc->sc_tc, TX39_IOMFIODATAIN_REG) & (1 << port));
252 }
253
254 static int
255 mfio_intr_map(int *src, int port, int mode)
256 {
257
258 if (mode & HPCIO_INTR_POSEDGE) {
259 *src = MAKEINTR(3, (1 << port));
260 return (0);
261 } else if (mode & HPCIO_INTR_NEGEDGE) {
262 *src = MAKEINTR(4, (1 << port));
263 return (0);
264 }
265
266 DPRINTF(("invalid interrupt mode.\n"));
267
268 return (1);
269 }
270
271 static void
272 mfio_update(hpcio_chip_t arg)
273 {
274 struct tx39io_softc *sc = arg->hc_sc;
275 tx_chipset_tag_t tc = sc->sc_tc;
276 struct tx39io_port_status *stat_mfio = &sc->sc_stat_mfio;
277
278 sc->sc_ostat_mfio = *stat_mfio; /* save old status */
279 stat_mfio->dir = tx_conf_read(tc, TX39_IOMFIODATADIR_REG);
280 stat_mfio->in = tx_conf_read(tc, TX39_IOMFIODATAIN_REG);
281 stat_mfio->out = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
282 stat_mfio->power = tx_conf_read(tc, TX39_IOMFIOPOWERDWN_REG);
283 stat_mfio->u.select = tx_conf_read(tc, TX39_IOMFIODATASEL_REG);
284 }
285
286 #ifdef TX391X
287 /*
288 * TMPR3912 specific
289 */
290 int
291 tx391x_io_in(hpcio_chip_t arg, int port)
292 {
293 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
294 txreg_t reg = tx_conf_read(sc->sc_tc, TX39_IOCTRL_REG);
295
296 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
297 return (TX391X_IOCTRL_IODIN(reg) & (1 << port));
298 }
299
300 void
301 tx391x_io_out(hpcio_chip_t arg, int port, int onoff)
302 {
303 #ifdef DIAGNOSTIC
304 const char *devname;
305 #endif
306 struct tx39io_softc *sc = arg->hc_sc;
307 tx_chipset_tag_t tc;
308 txreg_t reg, pos, iostat;
309
310 KASSERT(sc);
311 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
312
313 devname = sc->sc_dev.dv_xname;
314 tc = sc->sc_tc;
315
316 /* IO [0:6] */
317 pos = 1 << port;
318 #ifdef DIAGNOSTIC
319 if (!(sc->sc_stat_io.dir & pos))
320 panic("%s: IO%d is not output port.\n", devname, port);
321 #endif
322 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
323 iostat = TX391X_IOCTRL_IODOUT(reg);
324 if (onoff)
325 iostat |= pos;
326 else
327 iostat &= ~pos;
328 TX391X_IOCTRL_IODOUT_CLR(reg);
329 reg = TX391X_IOCTRL_IODOUT_SET(reg, iostat);
330 tx_conf_write(tc, TX39_IOCTRL_REG, reg);
331 }
332
333 void
334 tx391x_io_update(hpcio_chip_t arg)
335 {
336 struct tx39io_softc *sc = arg->hc_sc;
337 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
338 tx_chipset_tag_t tc = sc->sc_tc;
339 txreg_t reg;
340
341 /* IO [0:6] */
342 sc->sc_ostat_io = *stat_io; /* save old status */
343 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
344 stat_io->dir = TX391X_IOCTRL_IODIREC(reg);
345 stat_io->in = TX391X_IOCTRL_IODIN(reg);
346 stat_io->out = TX391X_IOCTRL_IODOUT(reg);
347 stat_io->u.debounce = TX391X_IOCTRL_IODEBSEL(reg);
348 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
349 stat_io->power = TX391X_IOIOPOWERDWN_IOPD(reg);
350 }
351
352 int
353 tx391x_io_intr_map(int *src, int port, int mode)
354 {
355
356 if (mode & HPCIO_INTR_POSEDGE) {
357 *src = MAKEINTR(5, (1 << (port + 7)));
358 return (0);
359 } else if (mode & HPCIO_INTR_NEGEDGE) {
360 *src = MAKEINTR(5, (1 << port));
361 return (0);
362 }
363
364 DPRINTF(("invalid interrupt mode.\n"));
365
366 return (1);
367 }
368 #endif /* TX391X */
369
370 #ifdef TX392X
371 /*
372 * TMPR3922 specific
373 */
374 int
375 tx392x_io_in(hpcio_chip_t arg, int port)
376 {
377 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
378 txreg_t reg = tx_conf_read(sc->sc_tc, TX392X_IODATAINOUT_REG);
379
380 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
381
382 return (TX392X_IODATAINOUT_DIN(reg) & (1 << port));
383 }
384
385 void
386 tx392x_io_out(hpcio_chip_t arg, int port, int onoff)
387 {
388 struct tx39io_softc *sc = arg->hc_sc;
389 #ifdef DIAGNOSTIC
390 const char *devname = sc->sc_dev.dv_xname;
391 #endif
392 tx_chipset_tag_t tc = sc->sc_tc;
393 txreg_t reg, pos, iostat;
394
395 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
396 /* IO [0:15] */
397 pos = 1 << port;
398 #ifdef DIAGNOSTIC
399 if (!(sc->sc_status.dir_io & pos))
400 panic("%s: IO%d is not output port.\n", devname, port);
401 #endif
402 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
403 iostat = TX392X_IODATAINOUT_DOUT(reg);
404 if (onoff)
405 iostat |= pos;
406 else
407 iostat &= ~pos;
408 TX392X_IODATAINOUT_DOUT_CLR(reg);
409 reg = TX392X_IODATAINOUT_DOUT_SET(reg, iostat);
410 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
411 }
412
413 int
414 tx392x_io_intr_map(int *src, int port, int mode)
415 {
416
417 if (mode & HPCIO_INTR_POSEDGE) {
418 *src = MAKEINTR(8, (1 << (port + 16)));
419 return (0);
420 } else if (mode & HPCIO_INTR_NEGEDGE) {
421 *src = MAKEINTR(8, (1 << port));
422 return (0);
423 }
424
425 DPRINTF(("invalid interrupt mode.\n"));
426
427 return (1);
428 }
429
430 void
431 tx392x_io_update(hpcio_chip_t arg)
432 {
433 struct tx39io_softc *sc = arg->hc_sc;
434 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
435 tx_chipset_tag_t tc = sc->sc_tc;
436 txreg_t reg;
437
438 sc->sc_ostat_io = *stat_io; /* save old status */
439 /* IO [0:15] */
440 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
441 stat_io->dir = TX392X_IOCTRL_IODIREC(reg);
442 stat_io->u.debounce = TX392X_IOCTRL_IODEBSEL(reg);
443 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
444 stat_io->in = TX392X_IODATAINOUT_DIN(reg);
445 stat_io->out = TX392X_IODATAINOUT_DOUT(reg);
446 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
447 stat_io->power = TX392X_IOIOPOWERDWN_IOPD(reg);
448 }
449
450 #endif /* TX392X */
451
452 static const char *line = "--------------------------------------------------"
453 "------------\n";
454 static void
455 mfio_dump(hpcio_chip_t arg)
456 {
457 struct tx39io_softc *sc = arg->hc_sc;
458 const struct tx39io_mfio_map *map = tx39io_get_mfio_map(tc);
459 struct tx39io_port_status *stat;
460 int i;
461
462 printf("%s", line);
463 stat = &sc->sc_stat_mfio;
464 for (i = TX39_IO_MFIO_MAX - 1; i >= 0 ; i--) {
465 /* MFIO port has power down state */
466 printf("MFIO %2d: - ", i);
467 __print_port_status(stat, i);
468 printf(ISSET(stat->u.select, i) ? " MFIO(%s)\n" : " %s\n",
469 map[i].std_pin_name);
470 }
471 printf("%s", line);
472 }
473
474 static void
475 io_dump(hpcio_chip_t arg)
476 {
477 struct tx39io_softc *sc = arg->hc_sc;
478 struct tx39io_port_status *stat;
479 int i;
480
481 printf("%s Debounce Direction DataOut DataIn PowerDown Select"
482 "\n%s", line, line);
483 stat = &sc->sc_stat_io;
484 for (i = tx39io_get_io_max(tc) - 1; i >= 0 ; i--) {
485 /* IO port has debouncer */
486 printf("IO %2d: %s ", i,
487 ISSET(stat->u.debounce, i) ? "On " : "Off");
488 __print_port_status(stat, i);
489 printf(" -\n");
490 }
491 }
492
493 static void
494 __print_port_status(struct tx39io_port_status *stat, int i)
495 {
496 printf("%s %d %d %s",
497 ISSET(stat->dir, i) ? "Out" : "In ",
498 ISSET(stat->out, i) ? 1 : 0,
499 ISSET(stat->in, i) ? 1 : 0,
500 ISSET(stat->power, i) ? "Down ": "Active");
501 }
502
503