xref: /netbsd-src/sys/arch/hpcmips/tx/tx39sib.c (revision 81e0d2b0af8485d94ed5da487d4253841a2e6e45)
1 /*	$NetBSD: tx39sib.c,v 1.14 2003/07/15 02:29:33 lukem 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  * 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 /*
40  * TX39 SIB (Serial Interface Bus) module.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: tx39sib.c,v 1.14 2003/07/15 02:29:33 lukem Exp $");
45 
46 #undef TX39SIBDEBUG
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54 
55 #include <hpcmips/tx/tx39var.h>
56 #include <hpcmips/tx/tx39icureg.h>
57 #include <hpcmips/tx/tx39sibvar.h>
58 #include <hpcmips/tx/tx39sibreg.h>
59 
60 #include "locators.h"
61 
62 #ifdef TX39SIBDEBUG
63 int	tx39sibdebug = 0;
64 #define	DPRINTF(arg) if (tx39sibdebug) printf arg;
65 #else
66 #define	DPRINTF(arg)
67 #endif
68 
69 int	tx39sib_match(struct device *, struct cfdata *, void *);
70 void	tx39sib_attach(struct device *, struct device *, void *);
71 int	tx39sib_print(void *, const char *);
72 int	tx39sib_search(struct device *, struct cfdata *, void *);
73 
74 #define TX39_CLK2X	18432000
75 const int sibsclk_divide_table[8] = {
76 	2, 3, 4, 5, 6, 8, 10, 12
77 };
78 
79 struct tx39sib_param {
80 	/* SIB clock rate */
81 	int sp_clock;
82 /*
83  *	SIBMCLK = 18.432MHz = (CLK2X /4)
84  *	SIBSCLK = SIBMCLK / sp_clock
85  *	sp_clock	start	end	divide module
86  *	0		7	8	2
87  *	1		6	8	3
88  *	2		6	9	4
89  *	3		5	9	5
90  *	4		5	10	6
91  *	5		4	11	8
92  *	6		3	12	10
93  *	7		2	13	12
94  */
95 	/* sampling rate */
96 	int sp_snd_rate; /* SNDFSDIV + 1 */
97 	int sp_tel_rate; /* TELFSDIV + 1 */
98 /*
99  *	Fs = (SIBSCLK * 2) / ((FSDIV + 1) * 64)
100  *	FSDIV + 1	sampling rate
101  *	15		19.2k		(1.6% error vs. CD-XA)
102  *	13		22.154k		(0.47% error vs. CD-Audio)
103  *	22		7.85k		(1.8% error vs. 8k)
104  */
105 	/* data format 16/8bit */
106 	int sp_sf0sndmode;
107 	int sp_sf0telmode;
108 };
109 
110 struct tx39sib_param tx39sib_param_default_3912 = {
111 	0,			/* SIBSCLK = 9.216MHz (div2) */
112 #if 0 /* setting sample */
113 	40,			/* audio: 7.2kHz */
114 	26,			/* audio: CD-Audio(/4) 11.077kHz*/
115 	6,			/* audio: 48kHz */
116 #endif
117 	13,			/* audio: CD-Audio(/2 = 22.050) 22.154kHz*/
118 	40,			/* telecom: 7.2kHz */
119 	TX39_SIBCTRL_SND16,	/* Audio 16bit mono */
120 	TX39_SIBCTRL_TEL16	/* Telecom 16bit mono */
121 };
122 
123 struct tx39sib_param tx39sib_param_default_3922 = {
124 	7,			/* SIBSCLK = 9.216MHz (div1) */
125 	13,			/* audio: CD-Audio(/2 = 22.050) 22.154kHz*/
126 	40,			/* telecom: 7.2kHz */
127 	TX39_SIBCTRL_SND16,	/* Audio 16bit mono */
128 	TX39_SIBCTRL_TEL16	/* Telecom 16bit mono */
129 };
130 
131 struct tx39sib_softc {
132 	struct	device sc_dev;
133 	tx_chipset_tag_t sc_tc;
134 
135 	struct tx39sib_param sc_param;
136 	int sc_attached;
137 };
138 
139 __inline int	__txsibsf0_ready(tx_chipset_tag_t);
140 #ifdef TX39SIBDEBUG
141 void	tx39sib_dump(struct tx39sib_softc *);
142 #endif
143 
144 CFATTACH_DECL(tx39sib, sizeof(struct tx39sib_softc),
145     tx39sib_match, tx39sib_attach, NULL, NULL);
146 
147 int
148 tx39sib_match(struct device *parent, struct cfdata *cf, void *aux)
149 {
150 	return (ATTACH_FIRST);
151 }
152 
153 void
154 tx39sib_attach(struct device *parent, struct device *self, void *aux)
155 {
156 	struct txsim_attach_args *ta = aux;
157 	struct tx39sib_softc *sc = (void*)self;
158 	tx_chipset_tag_t tc;
159 
160 	sc->sc_tc = tc = ta->ta_tc;
161 
162 	/* set default param */
163 #ifdef TX391X
164 	sc->sc_param = tx39sib_param_default_3912;
165 #endif /* TX391X */
166 #ifdef TX392X
167 	sc->sc_param = tx39sib_param_default_3922;
168 #endif /* TX392X */
169 
170 #define MHZ(a) ((a) / 1000000), (((a) % 1000000) / 1000)
171 	printf(": %d.%03d MHz", MHZ(tx39sib_clock(self)));
172 
173 	printf("\n");
174 #ifdef TX39SIBDEBUG
175 	if (tx39sibdebug)
176 		tx39sib_dump(sc);
177 #endif
178 	/* enable subframe0 */
179 	tx39sib_enable1(self);
180 	/* enable SIB */
181 	tx39sib_enable2(self);
182 
183 #ifdef TX39SIBDEBUG
184 	if (tx39sibdebug)
185 		tx39sib_dump(sc);
186 #endif
187 
188 	config_search(tx39sib_search, self, tx39sib_print);
189 }
190 
191 void
192 tx39sib_enable1(struct device *dev)
193 {
194 	struct tx39sib_softc *sc = (void*)dev;
195 	struct tx39sib_param *param = &sc->sc_param;
196 	tx_chipset_tag_t tc = sc->sc_tc;
197 
198 	txreg_t reg;
199 
200 	/* disable SIB */
201 	tx39sib_disable(dev);
202 
203 	/* setup */
204 	reg = 0;
205 	/*  SIB clock rate */
206 	reg = TX39_SIBCTRL_SCLKDIV_SET(reg, param->sp_clock);
207 	/*  sampling rate (sound) */
208 	reg = TX39_SIBCTRL_SNDFSDIV_SET(reg, param->sp_snd_rate - 1);
209 	/*  sampling rate (telecom) */
210 	reg = TX39_SIBCTRL_TELFSDIV_SET(reg, param->sp_tel_rate - 1);
211 	/*  data format (8/16bit) */
212 	reg |= param->sp_sf0sndmode;
213 	reg |= param->sp_sf0telmode;
214 	tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
215 
216 	/* DMA */
217 	reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
218 	reg &= ~(TX39_SIBDMACTRL_ENDMARXSND |
219 	    TX39_SIBDMACTRL_ENDMATXSND |
220 	    TX39_SIBDMACTRL_ENDMARXTEL |
221 	    TX39_SIBDMACTRL_ENDMATXTEL);
222 	tx_conf_write(tc, TX39_SIBDMACTRL_REG, reg);
223 
224 	/*
225 	 * Enable subframe0 (BETTY)
226 	 */
227 	reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
228 	reg |= TX39_SIBCTRL_ENSF0;
229 	tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
230 }
231 
232 void
233 tx39sib_enable2(struct device *dev)
234 {
235 	struct tx39sib_softc *sc = (void*)dev;
236 	tx_chipset_tag_t tc = sc->sc_tc;
237 	txreg_t reg;
238 
239 	reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
240 	reg |= TX39_SIBCTRL_ENSIB;
241 	tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
242 }
243 
244 void
245 tx39sib_disable(struct device *dev)
246 {
247 	struct tx39sib_softc *sc = (void*)dev;
248 	tx_chipset_tag_t tc = sc->sc_tc;
249 	txreg_t reg;
250 	/* disable codec side */
251 	/* notyet */
252 
253 	/* disable TX39 side */
254 	reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
255 	reg &= ~(TX39_SIBCTRL_ENTEL | TX39_SIBCTRL_ENSND);
256 	tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
257 
258 	/*
259 	 * Disable subframe0/1 (BETTY/external codec)
260 	 */
261 	reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
262 	reg &= ~TX39_SIBCTRL_ENSF0;
263 	reg &= ~(TX39_SIBCTRL_ENSF1 | TX39_SIBCTRL_SELTELSF1 |
264 	    TX39_SIBCTRL_SELSNDSF1);
265 	tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
266 
267 	/* disable TX39SIB module */
268 	reg &= ~TX39_SIBCTRL_ENSIB;
269 	tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
270 }
271 
272 int
273 tx39sib_clock(struct device *dev)
274 {
275 	struct tx39sib_softc *sc = (void*)dev;
276 
277 	return (TX39_CLK2X / sibsclk_divide_table[sc->sc_param.sp_clock]);
278 }
279 
280 int
281 tx39sib_search(struct device *parent, struct cfdata *cf, void *aux)
282 {
283 	struct tx39sib_softc *sc = (void*)parent;
284 	struct txsib_attach_args sa;
285 
286 	sa.sa_tc	= sc->sc_tc;
287 	sa.sa_slot	= cf->cf_loc[TXSIBIFCF_SLOT];
288 	sa.sa_snd_rate	= sc->sc_param.sp_snd_rate;
289 	sa.sa_tel_rate	= sc->sc_param.sp_tel_rate;
290 
291 	if (sa.sa_slot == TXSIBIFCF_SLOT_DEFAULT) {
292 		printf("tx39sib_search: wildcarded slot, skipping\n");
293 		return (0);
294 	}
295 
296 	if (!(sc->sc_attached & (1 << sa.sa_slot)) &&/* not attached slot */
297 	    config_match(parent, cf, &sa)) {
298 		config_attach(parent, cf, &sa, tx39sib_print);
299 		sc->sc_attached |= (1 << sa.sa_slot);
300 	}
301 
302 	return (0);
303 }
304 
305 int
306 tx39sib_print(void *aux, const char *pnp)
307 {
308 	struct txsib_attach_args *sa = aux;
309 
310 	aprint_normal(" slot %d", sa->sa_slot);
311 
312 	return (QUIET);
313 }
314 
315 /*
316  * sync access method. don't use runtime.
317  */
318 
319 __inline__ int
320 __txsibsf0_ready(tx_chipset_tag_t tc)
321 {
322 	int i;
323 
324 	tx_conf_write(tc, TX39_INTRSTATUS1_REG, TX39_INTRSTATUS1_SIBSF0INT);
325 	for (i = 0; (!(tx_conf_read(tc, TX39_INTRSTATUS1_REG) &
326 	    TX39_INTRSTATUS1_SIBSF0INT)) && i < 1000; i++) {
327 		if (i > 100 && !(i % 100)) {
328 			printf("sf0 busy loop: retry count %d\n", i);
329 		}
330 	}
331 
332 	if (i >= 1000) {
333 		printf("sf0 busy\n");
334 		return (0);
335 	}
336 
337 	return (1);
338 }
339 
340 void
341 txsibsf0_reg_write(tx_chipset_tag_t tc, int addr, u_int16_t val)
342 {
343 	txreg_t reg;
344 
345 	reg = txsibsf0_read(tc, addr);
346 	reg |= TX39_SIBSF0_WRITE;
347 	TX39_SIBSF0_REGDATA_CLR(reg);
348 	reg = TX39_SIBSF0_REGDATA_SET(reg, val);
349 
350 	__txsibsf0_ready(tc);
351 	tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
352 }
353 
354 u_int16_t
355 txsibsf0_reg_read(tx_chipset_tag_t tc, int addr)
356 {
357 	return (TX39_SIBSF0_REGDATA(txsibsf0_read(tc, addr)));
358 }
359 
360 u_int32_t
361 txsibsf0_read(tx_chipset_tag_t tc, int addr)
362 {
363 	txreg_t reg;
364 	int retry = 3;
365 
366 	do {
367 		reg = TX39_SIBSF0_REGADDR_SET(0, addr);
368 		__txsibsf0_ready(tc);
369 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
370 
371 		__txsibsf0_ready(tc);
372 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
373 
374 	} while ((TX39_SIBSF0_REGADDR(reg) != addr) && --retry > 0);
375 
376 	if (retry <= 0)
377 		printf("txsibsf0_read: command failed\n");
378 
379 	return (reg);
380 }
381 
382 #ifdef TX39SIBDEBUG
383 #define ISSETPRINT_CTRL(r, m)						\
384 	dbg_bitmask_print(r, TX39_SIBCTRL_##m, #m)
385 #define ISSETPRINT_DMACTRL(r, m)					\
386 	dbg_bitmask_print(r, TX39_SIBDMACTRL_##m, #m)
387 
388 void
389 tx39sib_dump(struct tx39sib_softc *sc)
390 {
391 	tx_chipset_tag_t tc = sc->sc_tc;
392 	txreg_t reg;
393 
394 	reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
395 	ISSETPRINT_CTRL(reg, SIBIRQ);
396 	ISSETPRINT_CTRL(reg, ENCNTTEST);
397 	ISSETPRINT_CTRL(reg, ENDMATEST);
398 	ISSETPRINT_CTRL(reg, SNDMONO);
399 	ISSETPRINT_CTRL(reg, RMONOSNDIN);
400 	ISSETPRINT_CTRL(reg, TEL16);
401 	ISSETPRINT_CTRL(reg, SND16);
402 	ISSETPRINT_CTRL(reg, SELTELSF1);
403 	ISSETPRINT_CTRL(reg, SELSNDSF1);
404 	ISSETPRINT_CTRL(reg, ENTEL);
405 	ISSETPRINT_CTRL(reg, ENSND);
406 	ISSETPRINT_CTRL(reg, SIBLOOP);
407 	ISSETPRINT_CTRL(reg, ENSF1);
408 	ISSETPRINT_CTRL(reg, ENSF0);
409 	ISSETPRINT_CTRL(reg, ENSIB);
410 	printf("\n");
411 	printf("SCLKDIV %d\n", TX39_SIBCTRL_SCLKDIV(reg));
412 	printf("TELFSDIV %d\n", TX39_SIBCTRL_TELFSDIV(reg));
413 	printf("SNDFSDIV %d\n", TX39_SIBCTRL_SNDFSDIV(reg));
414 
415 	reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
416 	ISSETPRINT_DMACTRL(reg, SNDBUFF1TIME);
417 	ISSETPRINT_DMACTRL(reg, SNDDMALOOP);
418 	ISSETPRINT_DMACTRL(reg, ENDMARXSND);
419 	ISSETPRINT_DMACTRL(reg, ENDMATXSND);
420 	ISSETPRINT_DMACTRL(reg, TELBUFF1TIME);
421 	ISSETPRINT_DMACTRL(reg, TELDMALOOP);
422 	ISSETPRINT_DMACTRL(reg, ENDMARXTEL);
423 	ISSETPRINT_DMACTRL(reg, ENDMATXTEL);
424 	printf("\n");
425 	printf("SNDDMAPTR %d\n", TX39_SIBDMACTRL_TELDMAPTR(reg));
426 	printf("TELDMAPTR %d\n", TX39_SIBDMACTRL_SNDDMAPTR(reg));
427 
428 }
429 #endif /* TX39SIBDEBUG */
430