tx39io.c revision 1.10 1 /* $NetBSD: tx39io.c,v 1.10 2002/01/28 05:40:58 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_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 struct tx39io_softc *sc = arg->hc_sc;
304 tx_chipset_tag_t tc;
305 txreg_t reg, pos, iostat;
306
307 KASSERT(sc);
308 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
309
310 tc = sc->sc_tc;
311
312 /* IO [0:6] */
313 pos = 1 << port;
314 #ifdef DIAGNOSTIC
315 if (!(sc->sc_stat_io.dir & pos))
316 panic("%s: IO%d is not output port.\n", sc->sc_dev.dv_xname,
317 port);
318 #endif
319 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
320 iostat = TX391X_IOCTRL_IODOUT(reg);
321 if (onoff)
322 iostat |= pos;
323 else
324 iostat &= ~pos;
325 TX391X_IOCTRL_IODOUT_CLR(reg);
326 reg = TX391X_IOCTRL_IODOUT_SET(reg, iostat);
327 tx_conf_write(tc, TX39_IOCTRL_REG, reg);
328 }
329
330 void
331 tx391x_io_update(hpcio_chip_t arg)
332 {
333 struct tx39io_softc *sc = arg->hc_sc;
334 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
335 tx_chipset_tag_t tc = sc->sc_tc;
336 txreg_t reg;
337
338 /* IO [0:6] */
339 sc->sc_ostat_io = *stat_io; /* save old status */
340 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
341 stat_io->dir = TX391X_IOCTRL_IODIREC(reg);
342 stat_io->in = TX391X_IOCTRL_IODIN(reg);
343 stat_io->out = TX391X_IOCTRL_IODOUT(reg);
344 stat_io->u.debounce = TX391X_IOCTRL_IODEBSEL(reg);
345 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
346 stat_io->power = TX391X_IOIOPOWERDWN_IOPD(reg);
347 }
348
349 int
350 tx391x_io_intr_map(int *src, int port, int mode)
351 {
352
353 if (mode & HPCIO_INTR_POSEDGE) {
354 *src = MAKEINTR(5, (1 << (port + 7)));
355 return (0);
356 } else if (mode & HPCIO_INTR_NEGEDGE) {
357 *src = MAKEINTR(5, (1 << port));
358 return (0);
359 }
360
361 DPRINTF(("invalid interrupt mode.\n"));
362
363 return (1);
364 }
365 #endif /* TX391X */
366
367 #ifdef TX392X
368 /*
369 * TMPR3922 specific
370 */
371 int
372 tx392x_io_in(hpcio_chip_t arg, int port)
373 {
374 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
375 txreg_t reg = tx_conf_read(sc->sc_tc, TX392X_IODATAINOUT_REG);
376
377 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
378
379 return (TX392X_IODATAINOUT_DIN(reg) & (1 << port));
380 }
381
382 void
383 tx392x_io_out(hpcio_chip_t arg, int port, int onoff)
384 {
385 struct tx39io_softc *sc = arg->hc_sc;
386 #ifdef DIAGNOSTIC
387 const char *devname = sc->sc_dev.dv_xname;
388 #endif
389 tx_chipset_tag_t tc = sc->sc_tc;
390 txreg_t reg, pos, iostat;
391
392 DPRINTF(("%s: port #%d\n", __FUNCTION__, port));
393 /* IO [0:15] */
394 pos = 1 << port;
395 #ifdef DIAGNOSTIC
396 if (!(sc->sc_status.dir_io & pos))
397 panic("%s: IO%d is not output port.\n", devname, port);
398 #endif
399 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
400 iostat = TX392X_IODATAINOUT_DOUT(reg);
401 if (onoff)
402 iostat |= pos;
403 else
404 iostat &= ~pos;
405 TX392X_IODATAINOUT_DOUT_CLR(reg);
406 reg = TX392X_IODATAINOUT_DOUT_SET(reg, iostat);
407 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
408 }
409
410 int
411 tx392x_io_intr_map(int *src, int port, int mode)
412 {
413
414 if (mode & HPCIO_INTR_POSEDGE) {
415 *src = MAKEINTR(8, (1 << (port + 16)));
416 return (0);
417 } else if (mode & HPCIO_INTR_NEGEDGE) {
418 *src = MAKEINTR(8, (1 << port));
419 return (0);
420 }
421
422 DPRINTF(("invalid interrupt mode.\n"));
423
424 return (1);
425 }
426
427 void
428 tx392x_io_update(hpcio_chip_t arg)
429 {
430 struct tx39io_softc *sc = arg->hc_sc;
431 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
432 tx_chipset_tag_t tc = sc->sc_tc;
433 txreg_t reg;
434
435 sc->sc_ostat_io = *stat_io; /* save old status */
436 /* IO [0:15] */
437 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
438 stat_io->dir = TX392X_IOCTRL_IODIREC(reg);
439 stat_io->u.debounce = TX392X_IOCTRL_IODEBSEL(reg);
440 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
441 stat_io->in = TX392X_IODATAINOUT_DIN(reg);
442 stat_io->out = TX392X_IODATAINOUT_DOUT(reg);
443 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
444 stat_io->power = TX392X_IOIOPOWERDWN_IOPD(reg);
445 }
446
447 #endif /* TX392X */
448
449 static const char *line = "--------------------------------------------------"
450 "------------\n";
451 static void
452 mfio_dump(hpcio_chip_t arg)
453 {
454 struct tx39io_softc *sc = arg->hc_sc;
455 const struct tx39io_mfio_map *map = tx39io_get_mfio_map(tc);
456 struct tx39io_port_status *stat;
457 int i;
458
459 printf("%s", line);
460 stat = &sc->sc_stat_mfio;
461 for (i = TX39_IO_MFIO_MAX - 1; i >= 0 ; i--) {
462 /* MFIO port has power down state */
463 printf("MFIO %2d: - ", i);
464 __print_port_status(stat, i);
465 printf(ISSET(stat->u.select, i) ? " MFIO(%s)\n" : " %s\n",
466 map[i].std_pin_name);
467 }
468 printf("%s", line);
469 }
470
471 static void
472 io_dump(hpcio_chip_t arg)
473 {
474 struct tx39io_softc *sc = arg->hc_sc;
475 struct tx39io_port_status *stat;
476 int i;
477
478 printf("%s Debounce Direction DataOut DataIn PowerDown Select"
479 "\n%s", line, line);
480 stat = &sc->sc_stat_io;
481 for (i = tx39io_get_io_max(tc) - 1; i >= 0 ; i--) {
482 /* IO port has debouncer */
483 printf("IO %2d: %s ", i,
484 ISSET(stat->u.debounce, i) ? "On " : "Off");
485 __print_port_status(stat, i);
486 printf(" -\n");
487 }
488 }
489
490 static void
491 __print_port_status(struct tx39io_port_status *stat, int i)
492 {
493 printf("%s %d %d %s",
494 ISSET(stat->dir, i) ? "Out" : "In ",
495 ISSET(stat->out, i) ? 1 : 0,
496 ISSET(stat->in, i) ? 1 : 0,
497 ISSET(stat->power, i) ? "Down ": "Active");
498 }
499
500