1 /*        $NetBSD: ucb1200.c,v 1.21 2021/08/07 16:18:54 thorpej 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  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ucb1200.c,v 1.21 2021/08/07 16:18:54 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 
43 #include <machine/bus.h>
44 #include <machine/intr.h>
45 
46 #include <hpcmips/tx/tx39var.h>
47 #include <hpcmips/tx/tx39sibvar.h>
48 #include <hpcmips/tx/tx39sibreg.h>
49 
50 #include <hpcmips/dev/ucb1200var.h>
51 #include <hpcmips/dev/ucb1200reg.h>
52 
53 #ifdef    UCB1200_DEBUG
54 #define DPRINTF_ENABLE
55 #define DPRINTF_DEBUG         ucb1200_debug
56 #endif
57 #include <machine/debug.h>
58 
59 struct ucbchild_state {
60           int (*cs_busy)(void *);
61           void *cs_arg;
62 };
63 
64 struct ucb1200_softc {
65           device_t  sc_parent; /* parent (TX39 SIB module) */
66           tx_chipset_tag_t sc_tc;
67 
68           int                 sc_snd_rate; /* passed down from SIB module */
69           int                 sc_tel_rate;
70 
71           /* inquire child module state */
72           struct ucbchild_state sc_child[UCB1200_MODULE_MAX];
73 };
74 
75 int       ucb1200_match(device_t, cfdata_t, void *);
76 void      ucb1200_attach(device_t, device_t, void *);
77 int       ucb1200_print(void *, const char *);
78 int       ucb1200_search(device_t, cfdata_t, const int *, void *);
79 int       ucb1200_check_id(u_int16_t, int);
80 
81 #ifdef UCB1200_DEBUG
82 void      ucb1200_dump(struct ucb1200_softc *);
83 #endif
84 
85 CFATTACH_DECL_NEW(ucb, sizeof(struct ucb1200_softc),
86     ucb1200_match, ucb1200_attach, NULL, NULL);
87 
88 const struct ucb_id {
89           u_int16_t id;
90           const char          *product;
91 } ucb_id[] = {
92           { UCB1100_ID,       "PHILIPS UCB1100" },
93           { UCB1200_ID,       "PHILIPS UCB1200" },
94           { UCB1300_ID,       "PHILIPS UCB1300" },
95           { TC35413F_ID,      "TOSHIBA TC35413F" },
96           { 0, 0 }
97 };
98 
99 int
ucb1200_match(device_t parent,cfdata_t cf,void * aux)100 ucb1200_match(device_t parent, cfdata_t cf, void *aux)
101 {
102           struct txsib_attach_args *sa = aux;
103           u_int16_t reg;
104 
105           if (sa->sa_slot != 0) /* UCB1200 must be subframe 0 */
106                     return (0);
107           reg = txsibsf0_reg_read(sa->sa_tc, UCB1200_ID_REG);
108 
109           return (ucb1200_check_id(reg, 0));
110 }
111 
112 void
ucb1200_attach(device_t parent,device_t self,void * aux)113 ucb1200_attach(device_t parent, device_t self, void *aux)
114 {
115           struct txsib_attach_args *sa = aux;
116           struct ucb1200_softc *sc = device_private(self);
117           u_int16_t reg;
118 
119           printf(": ");
120           sc->sc_tc = sa->sa_tc;
121           sc->sc_parent = parent;
122           sc->sc_snd_rate = sa->sa_snd_rate;
123           sc->sc_tel_rate = sa->sa_tel_rate;
124 
125           tx39sib_enable1(sc->sc_parent);
126           tx39sib_enable2(sc->sc_parent);
127 
128 #ifdef UCB1200_DEBUG
129           if (ucb1200_debug)
130                     ucb1200_dump(sc);
131 #endif
132           reg = txsibsf0_reg_read(sa->sa_tc, UCB1200_ID_REG);
133           (void)ucb1200_check_id(reg, 1);
134           printf("\n");
135 
136           config_search(self, NULL,
137               CFARGS(.search = ucb1200_search));
138 }
139 
140 int
ucb1200_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)141 ucb1200_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
142 {
143           struct ucb1200_softc *sc = device_private(parent);
144           struct ucb1200_attach_args ucba;
145 
146           ucba.ucba_tc = sc->sc_tc;
147           ucba.ucba_snd_rate = sc->sc_snd_rate;
148           ucba.ucba_tel_rate = sc->sc_tel_rate;
149           ucba.ucba_sib          = sc->sc_parent;
150           ucba.ucba_ucb          = parent;
151 
152           if (config_probe(parent, cf, &ucba))
153                     config_attach(parent, cf, &ucba, ucb1200_print, CFARGS_NONE);
154 
155           return (0);
156 }
157 
158 int
ucb1200_print(void * aux,const char * pnp)159 ucb1200_print(void *aux, const char *pnp)
160 {
161 
162           return (pnp ? QUIET : UNCONF);
163 }
164 
165 int
ucb1200_check_id(u_int16_t idreg,int print)166 ucb1200_check_id(u_int16_t idreg, int print)
167 {
168           int i;
169 
170           for (i = 0; ucb_id[i].product; i++) {
171                     if (ucb_id[i].id == idreg) {
172                               if (print) {
173                                         printf("%s", ucb_id[i].product);
174                               }
175 
176                               return (1);
177                     }
178           }
179 
180           return (0);
181 }
182 
183 void
ucb1200_state_install(device_t dev,int (* sfun)(void *),void * sarg,int sid)184 ucb1200_state_install(device_t dev, int (*sfun)(void *), void *sarg,
185     int sid)
186 {
187           struct ucb1200_softc *sc = device_private(dev);
188 
189           sc->sc_child[sid].cs_busy = sfun;
190           sc->sc_child[sid].cs_arg = sarg;
191 }
192 
193 int
ucb1200_state_idle(device_t dev)194 ucb1200_state_idle(device_t dev)
195 {
196           struct ucb1200_softc *sc = device_private(dev);
197           struct ucbchild_state *cs;
198           int i;
199 
200           cs = sc->sc_child;
201           for (i = 0; i < UCB1200_MODULE_MAX; i++, cs++)
202                     if (cs->cs_busy)
203                               if ((*cs->cs_busy)(cs->cs_arg))
204                                         return (0);
205 
206           return (1); /* idle state */
207 }
208 
209 #ifdef UCB1200_DEBUG
210 void
ucb1200_dump(struct ucb1200_softc * sc)211 ucb1200_dump(struct ucb1200_softc *sc)
212 {
213           static const char *const regname[] = {
214                 "IO_DATA        ",
215                 "IO_DIR         ",
216                 "POSINTEN       ",
217                 "NEGINTEN       ",
218                 "INTSTAT        ",
219                 "TELECOMCTRLA   ",
220                 "TELECOMCTRLB   ",
221                 "AUDIOCTRLA     ",
222                 "AUDIOCTRLB     ",
223                 "TOUCHSCREENCTRL",
224                 "ADCCTRL        ",
225                 "ADCDATA        ",
226                 "ID             ",
227                 "MODE           ",
228                 "RESERVED       ",
229                 "NULL           "
230           };
231           tx_chipset_tag_t tc;
232           u_int16_t reg;
233           int i;
234 
235           tc = sc->sc_tc;
236 
237           printf("\n\t[UCB1200 register]\n");
238           for (i = 0; i < 16; i++) {
239                     reg = txsibsf0_reg_read(tc, i);
240                     printf("%s(%02d) 0x%04x ", regname[i], i, reg);
241                     dbg_bit_print(reg);
242           }
243 }
244 #endif /* UCB1200_DEBUG */
245