1 /*        $NetBSD: ucbio.c,v 1.12 2012/10/27 17:17:53 chs Exp $       */
2 
3 /*-
4  * Copyright (c) 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Device driver for PHILIPS UCB1200 Advanced modem/audio analog front-end
34  *        General Purpose I/O part.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ucbio.c,v 1.12 2012/10/27 17:17:53 chs Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 
47 #include <hpcmips/tx/tx39var.h>
48 #include <hpcmips/tx/tx39sibvar.h>
49 #include <hpcmips/tx/tx39sibreg.h>
50 
51 #include <hpcmips/dev/ucb1200var.h>
52 #include <hpcmips/dev/ucb1200reg.h>
53 
54 #include <dev/hpc/hpciovar.h> /* I/O manager */
55 
56 #include <machine/debug.h>
57 
58 int ucbio_match(device_t, cfdata_t, void *);
59 void ucbio_attach(device_t, device_t, void *);
60 
61 struct betty_port_status {
62           u_int16_t dir;
63           u_int16_t in, out;
64 };
65 
66 struct ucbio_softc {
67           device_t sc_dev;
68           tx_chipset_tag_t sc_tc;
69 
70           struct betty_port_status sc_stat, sc_ostat;
71           struct hpcio_chip sc_hc;
72 };
73 
74 CFATTACH_DECL_NEW(ucbio, sizeof(struct ucbio_softc),
75     ucbio_match, ucbio_attach, NULL, NULL);
76 
77 /* I/O */
78 static int betty_in(hpcio_chip_t, int);
79 static void betty_out(hpcio_chip_t, int, int);
80 /* interrupt */
81 static hpcio_intr_handle_t betty_intr_establish(hpcio_chip_t, int, int,
82     int (*)(void *), void *);
83 static void betty_intr_disestablish(hpcio_chip_t, void *);
84 /* debug */
85 static void betty_update(hpcio_chip_t);
86 static void betty_dump(hpcio_chip_t);
87 
88 int
ucbio_match(device_t parent,cfdata_t cf,void * aux)89 ucbio_match(device_t parent, cfdata_t cf, void *aux)
90 {
91           return (1);
92 }
93 
94 void
ucbio_attach(device_t parent,device_t self,void * aux)95 ucbio_attach(device_t parent, device_t self, void *aux)
96 {
97           struct ucb1200_attach_args *ucba = aux;
98           struct ucbio_softc *sc = device_private(self);
99           struct hpcio_chip *hc = &sc->sc_hc;
100 
101           sc->sc_dev = self;
102           sc->sc_tc = ucba->ucba_tc;
103           printf("\n");
104 
105           hc->hc_sc            = sc;
106           hc->hc_chipid                  = 2;
107           hc->hc_name                    = "UCB1200";
108           hc->hc_portread                = betty_in;
109           hc->hc_portwrite     = betty_out;
110           hc->hc_intr_establish          = betty_intr_establish;
111           hc->hc_intr_disestablish = betty_intr_disestablish;
112           hc->hc_update                  = betty_update;
113           hc->hc_dump                    = betty_dump;
114 
115           tx_conf_register_ioman(sc->sc_tc, hc);
116 
117           hpcio_update(hc);
118           hpcio_dump(hc);
119 }
120 
121 /* basic I/O */
122 static void
betty_out(hpcio_chip_t hc,int port,int onoff)123 betty_out(hpcio_chip_t hc, int port, int onoff)
124 {
125           struct ucbio_softc *sc = hc->hc_sc;
126           tx_chipset_tag_t tc = sc->sc_tc;
127           txreg_t reg, pos;
128 
129           pos = 1 << port;
130           reg = txsibsf0_reg_read(tc, UCB1200_IO_DATA_REG);
131           if (onoff)
132                     reg |= pos;
133           else
134                     reg &= ~pos;
135           txsibsf0_reg_write(tc, UCB1200_IO_DATA_REG, reg);
136 }
137 
138 static int
betty_in(hpcio_chip_t hc,int port)139 betty_in(hpcio_chip_t hc, int port)
140 {
141           struct ucbio_softc *sc = hc->hc_sc;
142           tx_chipset_tag_t tc = sc->sc_tc;
143           txreg_t reg = txsibsf0_reg_read(tc, UCB1200_IO_DATA_REG);
144 
145           return (reg & (1 << port));
146 }
147 
148 /* interrupt method not implemented */
149 static hpcio_intr_handle_t
betty_intr_establish(hpcio_chip_t hc,int port,int mode,int (* func)(void *),void * func_arg)150 betty_intr_establish(hpcio_chip_t hc, int port, int mode, int (*func)(void *),
151     void *func_arg)
152 {
153           struct ucbio_softc *sc = hc->hc_sc;
154 
155           printf("%s: %s not implemented.\n", device_xname(sc->sc_dev),
156               __func__);
157 
158           return (0);
159 }
160 
161 static void
betty_intr_disestablish(hpcio_chip_t hc,void * ih)162 betty_intr_disestablish(hpcio_chip_t hc, void *ih)
163 {
164           struct ucbio_softc *sc = hc->hc_sc;
165 
166           printf("%s: %s not implemented.\n", device_xname(sc->sc_dev),
167               __func__);
168 }
169 
170 /* debug */
171 static void
betty_update(hpcio_chip_t hc)172 betty_update(hpcio_chip_t hc)
173 {
174           struct ucbio_softc *sc = hc->hc_sc;
175           tx_chipset_tag_t tc = sc->sc_tc;
176           struct betty_port_status *stat = &sc->sc_stat;
177           u_int16_t dir, data;
178 
179           sc->sc_ostat = *stat; /* save old status */
180           dir = stat->dir = txsibsf0_reg_read(tc, UCB1200_IO_DIR_REG);
181           data = txsibsf0_reg_read(tc, UCB1200_IO_DATA_REG);
182           stat->out = data & dir;
183           stat->in = data & ~dir;
184 }
185 
186 static void
betty_dump(hpcio_chip_t hc)187 betty_dump(hpcio_chip_t hc)
188 {
189 #ifdef UCBIO_DEBUG
190           struct ucbio_softc *sc = hc->hc_sc;
191           struct betty_port_status *stat = &sc->sc_stat;
192 
193           printf("[BETTY I/O]\n");
194           printf("IN  ");
195           dbg_bit_print(stat->in);
196           printf("OUT ");
197           dbg_bit_print(stat->out);
198           printf("DIR ");
199           dbg_bit_print(stat->dir);
200 #endif /* UCBIO_DEBUG */
201 }
202