tx39io.c revision 1.15.6.3 1 /* $NetBSD: tx39io.c,v 1.15.6.3 2004/09/21 13:16:13 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1999-2001 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
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: tx39io.c,v 1.15.6.3 2004/09/21 13:16:13 skrll Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45
46 #include <machine/bus.h>
47
48 #include <hpcmips/tx/tx39var.h>
49 #include <hpcmips/tx/tx39icureg.h>
50 #define __TX39IO_PRIVATE
51 #include <hpcmips/tx/tx39iovar.h>
52 #include <hpcmips/tx/tx39ioreg.h>
53
54 #ifdef TX39IO_DEBUG
55 #define DPRINTF_ENABLE
56 #define DPRINTF_DEBUG tx39io_debug
57 #endif
58 #include <machine/debug.h>
59
60 #define ISSET(x, s) ((x) & (1 << (s)))
61
62 int tx39io_match(struct device *, struct cfdata *, void *);
63 void tx39io_attach(struct device *, struct device *, void *);
64
65 CFATTACH_DECL(tx39io, sizeof(struct tx39io_softc),
66 tx39io_match, tx39io_attach, NULL, NULL);
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 TX39IO_DEBUG
168 hpcio_dump(io_hc);
169 hpcio_dump(mfio_hc);
170 printf("IO i0x%08x o0x%08x MFIO i0x%08x o0x%08x\n",
171 sc->sc_stat_io.in, sc->sc_stat_io.out,
172 sc->sc_stat_mfio.in, sc->sc_stat_mfio.out);
173 #endif /* TX39IO_DEBUG */
174 }
175
176 /*
177 * TX391X, TX392X common
178 */
179 static void *
180 io_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
181 void *func_arg)
182 {
183 struct tx39io_softc *sc = arg->hc_sc;
184 int src;
185
186 if (tx39io_intr_map(sc->sc_tc, &src, port, mode) != 0)
187 return (0);
188
189 return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
190 func_arg));
191 }
192
193 static void *
194 mfio_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
195 void *func_arg)
196 {
197 struct tx39io_softc *sc = arg->hc_sc;
198 int src;
199
200 if (mfio_intr_map(&src, port, mode) != 0)
201 return (0);
202
203 return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
204 func_arg));
205 }
206
207 static void
208 port_intr_disestablish(hpcio_chip_t arg, void *ih)
209 {
210 struct tx39io_softc *sc = arg->hc_sc;
211 tx_intr_disestablish(sc->sc_tc, ih);
212 }
213
214 static void
215 port_intr_clear(hpcio_chip_t arg, void *ih)
216 {
217 }
218
219 static void
220 mfio_out(hpcio_chip_t arg, int port, int onoff)
221 {
222 struct tx39io_softc *sc = arg->hc_sc;
223 tx_chipset_tag_t tc;
224 txreg_t reg, pos;
225
226 DPRINTF("port #%d\n", port);
227 tc = sc->sc_tc;
228 /* MFIO */
229 pos = 1 << port;
230 #ifdef DIAGNOSTIC
231 if (!(sc->sc_stat_mfio.dir & pos)) {
232 panic("%s: MFIO%d is not output port.",
233 sc->sc_dev.dv_xname, port);
234 }
235 #endif
236 reg = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
237 if (onoff)
238 reg |= pos;
239 else
240 reg &= ~pos;
241 tx_conf_write(tc, TX39_IOMFIODATAOUT_REG, reg);
242 }
243
244 static int
245 mfio_in(hpcio_chip_t arg, int port)
246 {
247 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc ;
248
249 DPRINTF("port #%d\n", port);
250 return (tx_conf_read(sc->sc_tc, TX39_IOMFIODATAIN_REG) & (1 << port));
251 }
252
253 static int
254 mfio_intr_map(int *src, int port, int mode)
255 {
256
257 if (mode & HPCIO_INTR_POSEDGE) {
258 *src = MAKEINTR(3, (1 << port));
259 return (0);
260 } else if (mode & HPCIO_INTR_NEGEDGE) {
261 *src = MAKEINTR(4, (1 << port));
262 return (0);
263 }
264
265 DPRINTF("invalid interrupt mode.\n");
266
267 return (1);
268 }
269
270 static void
271 mfio_update(hpcio_chip_t arg)
272 {
273 struct tx39io_softc *sc = arg->hc_sc;
274 tx_chipset_tag_t tc = sc->sc_tc;
275 struct tx39io_port_status *stat_mfio = &sc->sc_stat_mfio;
276
277 sc->sc_ostat_mfio = *stat_mfio; /* save old status */
278 stat_mfio->dir = tx_conf_read(tc, TX39_IOMFIODATADIR_REG);
279 stat_mfio->in = tx_conf_read(tc, TX39_IOMFIODATAIN_REG);
280 stat_mfio->out = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
281 stat_mfio->power = tx_conf_read(tc, TX39_IOMFIOPOWERDWN_REG);
282 stat_mfio->u.select = tx_conf_read(tc, TX39_IOMFIODATASEL_REG);
283 }
284
285 #ifdef TX391X
286 /*
287 * TMPR3912 specific
288 */
289 int
290 tx391x_io_in(hpcio_chip_t arg, int port)
291 {
292 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
293 txreg_t reg = tx_conf_read(sc->sc_tc, TX39_IOCTRL_REG);
294
295 DPRINTF("port #%d\n", port);
296 return (TX391X_IOCTRL_IODIN(reg) & (1 << port));
297 }
298
299 void
300 tx391x_io_out(hpcio_chip_t arg, int port, int onoff)
301 {
302 struct tx39io_softc *sc = arg->hc_sc;
303 tx_chipset_tag_t tc;
304 txreg_t reg, pos, iostat;
305
306 KASSERT(sc);
307 DPRINTF("port #%d\n", port);
308
309 tc = sc->sc_tc;
310
311 /* IO [0:6] */
312 pos = 1 << port;
313 #ifdef DIAGNOSTIC
314 if (!(sc->sc_stat_io.dir & pos))
315 panic("%s: IO%d is not output port.", sc->sc_dev.dv_xname,
316 port);
317 #endif
318 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
319 iostat = TX391X_IOCTRL_IODOUT(reg);
320 if (onoff)
321 iostat |= pos;
322 else
323 iostat &= ~pos;
324 TX391X_IOCTRL_IODOUT_CLR(reg);
325 reg = TX391X_IOCTRL_IODOUT_SET(reg, iostat);
326 tx_conf_write(tc, TX39_IOCTRL_REG, reg);
327 }
328
329 void
330 tx391x_io_update(hpcio_chip_t arg)
331 {
332 struct tx39io_softc *sc = arg->hc_sc;
333 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
334 tx_chipset_tag_t tc = sc->sc_tc;
335 txreg_t reg;
336
337 /* IO [0:6] */
338 sc->sc_ostat_io = *stat_io; /* save old status */
339 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
340 stat_io->dir = TX391X_IOCTRL_IODIREC(reg);
341 stat_io->in = TX391X_IOCTRL_IODIN(reg);
342 stat_io->out = TX391X_IOCTRL_IODOUT(reg);
343 stat_io->u.debounce = TX391X_IOCTRL_IODEBSEL(reg);
344 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
345 stat_io->power = TX391X_IOIOPOWERDWN_IOPD(reg);
346 }
347
348 int
349 tx391x_io_intr_map(int *src, int port, int mode)
350 {
351
352 if (mode & HPCIO_INTR_POSEDGE) {
353 *src = MAKEINTR(5, (1 << (port + 7)));
354 return (0);
355 } else if (mode & HPCIO_INTR_NEGEDGE) {
356 *src = MAKEINTR(5, (1 << port));
357 return (0);
358 }
359
360 DPRINTF("invalid interrupt mode.\n");
361
362 return (1);
363 }
364 #endif /* TX391X */
365
366 #ifdef TX392X
367 /*
368 * TMPR3922 specific
369 */
370 int
371 tx392x_io_in(hpcio_chip_t arg, int port)
372 {
373 struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
374 txreg_t reg = tx_conf_read(sc->sc_tc, TX392X_IODATAINOUT_REG);
375
376 DPRINTF("port #%d\n", port);
377
378 return (TX392X_IODATAINOUT_DIN(reg) & (1 << port));
379 }
380
381 void
382 tx392x_io_out(hpcio_chip_t arg, int port, int onoff)
383 {
384 struct tx39io_softc *sc = arg->hc_sc;
385 #ifdef DIAGNOSTIC
386 const char *devname = sc->sc_dev.dv_xname;
387 #endif
388 tx_chipset_tag_t tc = sc->sc_tc;
389 txreg_t reg, pos, iostat;
390
391 DPRINTF("port #%d\n", port);
392 /* IO [0:15] */
393 pos = 1 << port;
394 #ifdef DIAGNOSTIC
395 if (!(sc->sc_stat_io.dir & pos))
396 panic("%s: IO%d is not output port.", devname, port);
397 #endif
398 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
399 iostat = TX392X_IODATAINOUT_DOUT(reg);
400 if (onoff)
401 iostat |= pos;
402 else
403 iostat &= ~pos;
404 TX392X_IODATAINOUT_DOUT_CLR(reg);
405 reg = TX392X_IODATAINOUT_DOUT_SET(reg, iostat);
406 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
407 }
408
409 int
410 tx392x_io_intr_map(int *src, int port, int mode)
411 {
412
413 if (mode & HPCIO_INTR_POSEDGE) {
414 *src = MAKEINTR(8, (1 << (port + 16)));
415 return (0);
416 } else if (mode & HPCIO_INTR_NEGEDGE) {
417 *src = MAKEINTR(8, (1 << port));
418 return (0);
419 }
420
421 DPRINTF("invalid interrupt mode.\n");
422
423 return (1);
424 }
425
426 void
427 tx392x_io_update(hpcio_chip_t arg)
428 {
429 struct tx39io_softc *sc = arg->hc_sc;
430 struct tx39io_port_status *stat_io = &sc->sc_stat_io;
431 tx_chipset_tag_t tc = sc->sc_tc;
432 txreg_t reg;
433
434 sc->sc_ostat_io = *stat_io; /* save old status */
435 /* IO [0:15] */
436 reg = tx_conf_read(tc, TX39_IOCTRL_REG);
437 stat_io->dir = TX392X_IOCTRL_IODIREC(reg);
438 stat_io->u.debounce = TX392X_IOCTRL_IODEBSEL(reg);
439 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
440 stat_io->in = TX392X_IODATAINOUT_DIN(reg);
441 stat_io->out = TX392X_IODATAINOUT_DOUT(reg);
442 reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
443 stat_io->power = TX392X_IOIOPOWERDWN_IOPD(reg);
444 }
445
446 #endif /* TX392X */
447
448 static const char *line = "--------------------------------------------------"
449 "------------\n";
450 static void
451 mfio_dump(hpcio_chip_t arg)
452 {
453 struct tx39io_softc *sc = arg->hc_sc;
454 const struct tx39io_mfio_map *map = tx39io_get_mfio_map(tc);
455 struct tx39io_port_status *stat;
456 int i;
457
458 printf("%s", line);
459 stat = &sc->sc_stat_mfio;
460 for (i = TX39_IO_MFIO_MAX - 1; i >= 0 ; i--) {
461 /* MFIO port has power down state */
462 printf("MFIO %2d: - ", i);
463 __print_port_status(stat, i);
464 printf(ISSET(stat->u.select, i) ? " MFIO(%s)\n" : " %s\n",
465 map[i].std_pin_name);
466 }
467 printf("%s", line);
468 }
469
470 static void
471 io_dump(hpcio_chip_t arg)
472 {
473 struct tx39io_softc *sc = arg->hc_sc;
474 struct tx39io_port_status *stat;
475 int i;
476
477 printf("%s Debounce Direction DataOut DataIn PowerDown Select"
478 "\n%s", line, line);
479 stat = &sc->sc_stat_io;
480 for (i = tx39io_get_io_max(tc) - 1; i >= 0 ; i--) {
481 /* IO port has debouncer */
482 printf("IO %2d: %s ", i,
483 ISSET(stat->u.debounce, i) ? "On " : "Off");
484 __print_port_status(stat, i);
485 printf(" -\n");
486 }
487 }
488
489 static void
490 __print_port_status(struct tx39io_port_status *stat, int i)
491 {
492 printf("%s %d %d %s",
493 ISSET(stat->dir, i) ? "Out" : "In ",
494 ISSET(stat->out, i) ? 1 : 0,
495 ISSET(stat->in, i) ? 1 : 0,
496 ISSET(stat->power, i) ? "Down ": "Active");
497 }
498