tx39sib.c revision 1.2 1 1.2 uch /* $NetBSD: tx39sib.c,v 1.2 2000/01/12 14:56:19 uch Exp $ */
2 1.1 uch
3 1.1 uch /*
4 1.1 uch * Copyright (c) 2000, by UCHIYAMA Yasushi
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. The name of the developer may NOT be used to endorse or promote products
13 1.1 uch * derived from this software without specific prior written permission.
14 1.1 uch *
15 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 uch * SUCH DAMAGE.
26 1.1 uch *
27 1.1 uch */
28 1.1 uch
29 1.1 uch /*
30 1.1 uch * TX39 SIB (Serial Interface Bus) module.
31 1.1 uch */
32 1.2 uch #undef TX39SIBDEBUG
33 1.1 uch #include "opt_tx39_debug.h"
34 1.1 uch
35 1.1 uch #include <sys/param.h>
36 1.1 uch #include <sys/systm.h>
37 1.1 uch #include <sys/device.h>
38 1.1 uch
39 1.1 uch #include <machine/bus.h>
40 1.1 uch #include <machine/intr.h>
41 1.1 uch
42 1.1 uch #include <hpcmips/tx/tx39var.h>
43 1.1 uch #include <hpcmips/tx/tx39icureg.h>
44 1.1 uch #include <hpcmips/tx/tx39sibvar.h>
45 1.1 uch #include <hpcmips/tx/tx39sibreg.h>
46 1.1 uch
47 1.1 uch #include "locators.h"
48 1.1 uch
49 1.1 uch #ifdef TX39SIBDEBUG
50 1.1 uch int tx39sib_debug = 0;
51 1.1 uch #define DPRINTF(arg) if (vrpiu_debug) printf arg;
52 1.1 uch #else
53 1.1 uch #define DPRINTF(arg)
54 1.1 uch #endif
55 1.1 uch
56 1.1 uch int tx39sib_match __P((struct device*, struct cfdata*, void*));
57 1.1 uch void tx39sib_attach __P((struct device*, struct device*, void*));
58 1.1 uch int tx39sib_print __P((void*, const char*));
59 1.1 uch int tx39sib_search __P((struct device*, struct cfdata*, void*));
60 1.1 uch
61 1.2 uch #define TX39_CLK2X 18432000
62 1.2 uch const int sibsclk_divide_table[8] = {
63 1.2 uch 2, 3, 4, 5, 6, 8, 10, 12
64 1.2 uch };
65 1.2 uch
66 1.2 uch struct tx39sib_param {
67 1.2 uch /* SIB clock rate */
68 1.2 uch int sp_clock;
69 1.2 uch /*
70 1.2 uch * SIBMCLK = 18.432MHz = (CLK2X /4)
71 1.2 uch * SIBSCLK = SIBMCLK / sp_clock
72 1.2 uch * sp_clock start end divide module
73 1.2 uch * 0 7 8 2
74 1.2 uch * 1 6 8 3
75 1.2 uch * 2 6 9 4
76 1.2 uch * 3 5 9 5
77 1.2 uch * 4 5 10 6
78 1.2 uch * 5 4 11 8
79 1.2 uch * 6 3 12 10
80 1.2 uch * 7 2 13 12
81 1.2 uch */
82 1.2 uch /* sampling rate */
83 1.2 uch int sp_snd_rate; /* SNDFSDIV + 1 */
84 1.2 uch int sp_tel_rate; /* TELFSDIV + 1 */
85 1.2 uch /*
86 1.2 uch * Fs = (SIBSCLK * 2) / ((FSDIV + 1) * 64
87 1.2 uch * FSDIV + 1 sampling rate
88 1.2 uch * 15 19.2k (1.6% error vs. CD-XA)
89 1.2 uch * 13 22.154k (0.47% error vs. CD-Audio)
90 1.2 uch * 22 7.85k (1.8% error vs. 8k)
91 1.2 uch */
92 1.2 uch /* data format 16/8bit */
93 1.2 uch int sp_sf0sndmode;
94 1.2 uch int sp_sf0telmode;
95 1.2 uch };
96 1.2 uch
97 1.2 uch struct tx39sib_param tx39sib_param_default = {
98 1.2 uch 0, /* SIBSCLK = 9.216MHz */
99 1.2 uch 13, /* audio: CD-Audio */
100 1.2 uch 40, /* telecom: 7.2kHz */
101 1.2 uch TX39_SIBCTRL_SND16, /* Audio 16bit mono */
102 1.2 uch TX39_SIBCTRL_TEL16 /* Telecom 16bit mono */
103 1.2 uch };
104 1.2 uch
105 1.1 uch struct tx39sib_softc {
106 1.1 uch struct device sc_dev;
107 1.1 uch tx_chipset_tag_t sc_tc;
108 1.1 uch
109 1.2 uch struct tx39sib_param sc_param;
110 1.1 uch int sc_attached;
111 1.1 uch };
112 1.1 uch
113 1.1 uch __inline int __txsibsf0_ready __P((tx_chipset_tag_t));
114 1.1 uch void tx39sib_dump __P((struct tx39sib_softc*));
115 1.1 uch
116 1.1 uch struct cfattach tx39sib_ca = {
117 1.1 uch sizeof(struct tx39sib_softc), tx39sib_match, tx39sib_attach
118 1.1 uch };
119 1.1 uch
120 1.1 uch int
121 1.1 uch tx39sib_match(parent, cf, aux)
122 1.1 uch struct device *parent;
123 1.1 uch struct cfdata *cf;
124 1.1 uch void *aux;
125 1.1 uch {
126 1.1 uch return 1;
127 1.1 uch }
128 1.1 uch
129 1.1 uch void
130 1.1 uch tx39sib_attach(parent, self, aux)
131 1.1 uch struct device *parent;
132 1.1 uch struct device *self;
133 1.1 uch void *aux;
134 1.1 uch {
135 1.1 uch struct txsim_attach_args *ta = aux;
136 1.1 uch struct tx39sib_softc *sc = (void*)self;
137 1.1 uch tx_chipset_tag_t tc;
138 1.2 uch
139 1.1 uch sc->sc_tc = tc = ta->ta_tc;
140 1.1 uch
141 1.2 uch /* set default param */
142 1.2 uch sc->sc_param = tx39sib_param_default;
143 1.2 uch #define MHZ(a) ((a) / 1000000), (((a) % 1000000) / 1000)
144 1.2 uch printf(": %d.%03d MHz", MHZ(tx39sib_clock(self)));
145 1.2 uch
146 1.1 uch printf("\n");
147 1.1 uch #ifdef TX39SIBDEBUG
148 1.1 uch tx39sib_dump(sc);
149 1.1 uch #endif
150 1.2 uch /* enable subframe0 */
151 1.2 uch tx39sib_enable1(self);
152 1.2 uch /* enable SIB */
153 1.2 uch tx39sib_enable2(self);
154 1.2 uch
155 1.2 uch #ifdef TX39SIBDEBUG
156 1.2 uch tx39sib_dump(sc);
157 1.2 uch #endif
158 1.2 uch
159 1.2 uch config_search(tx39sib_search, self, tx39sib_print);
160 1.2 uch }
161 1.2 uch
162 1.2 uch void
163 1.2 uch tx39sib_enable1(dev)
164 1.2 uch struct device *dev;
165 1.2 uch {
166 1.2 uch struct tx39sib_softc *sc = (void*)dev;
167 1.2 uch struct tx39sib_param *param = &sc->sc_param;
168 1.2 uch tx_chipset_tag_t tc = sc->sc_tc;
169 1.2 uch
170 1.2 uch txreg_t reg;
171 1.2 uch
172 1.2 uch /* disable SIB */
173 1.2 uch tx39sib_disable(dev);
174 1.2 uch
175 1.2 uch /* setup */
176 1.2 uch reg = 0;
177 1.2 uch /* SIB clock rate */
178 1.2 uch reg = TX39_SIBCTRL_SCLKDIV_SET(reg, param->sp_clock);
179 1.2 uch /* sampling rate (sound) */
180 1.2 uch reg = TX39_SIBCTRL_SNDFSDIV_SET(reg, param->sp_snd_rate - 1);
181 1.2 uch /* sampling rate (telecom) */
182 1.2 uch reg = TX39_SIBCTRL_TELFSDIV_SET(reg, param->sp_tel_rate - 1);
183 1.2 uch /* data format (8/16bit) */
184 1.2 uch reg |= param->sp_sf0sndmode;
185 1.2 uch reg |= param->sp_sf0telmode;
186 1.2 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
187 1.2 uch
188 1.2 uch /* DMA */
189 1.2 uch reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
190 1.2 uch reg &= ~(TX39_SIBDMACTRL_ENDMARXSND |
191 1.2 uch TX39_SIBDMACTRL_ENDMATXSND |
192 1.2 uch TX39_SIBDMACTRL_ENDMARXTEL |
193 1.2 uch TX39_SIBDMACTRL_ENDMATXTEL);
194 1.2 uch tx_conf_write(tc, TX39_SIBDMACTRL_REG, reg);
195 1.2 uch
196 1.1 uch /*
197 1.2 uch * Enable subframe0 (BETTY)
198 1.1 uch */
199 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
200 1.1 uch reg |= TX39_SIBCTRL_ENSF0;
201 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
202 1.2 uch }
203 1.2 uch
204 1.2 uch void
205 1.2 uch tx39sib_enable2(dev)
206 1.2 uch struct device *dev;
207 1.2 uch {
208 1.2 uch struct tx39sib_softc *sc = (void*)dev;
209 1.2 uch tx_chipset_tag_t tc = sc->sc_tc;
210 1.2 uch txreg_t reg;
211 1.2 uch
212 1.2 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
213 1.2 uch reg |= TX39_SIBCTRL_ENSIB;
214 1.2 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
215 1.2 uch }
216 1.1 uch
217 1.2 uch void
218 1.2 uch tx39sib_disable(dev)
219 1.2 uch struct device *dev;
220 1.2 uch {
221 1.2 uch struct tx39sib_softc *sc = (void*)dev;
222 1.2 uch tx_chipset_tag_t tc = sc->sc_tc;
223 1.2 uch txreg_t reg;
224 1.2 uch /* disable codec side */
225 1.2 uch /* notyet */
226 1.2 uch
227 1.2 uch /* disable TX39 side */
228 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
229 1.2 uch reg &= ~(TX39_SIBCTRL_ENTEL | TX39_SIBCTRL_ENSND);
230 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
231 1.1 uch
232 1.2 uch /*
233 1.2 uch * Disable subframe0/1 (BETTY/external codec)
234 1.1 uch */
235 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
236 1.2 uch reg &= ~TX39_SIBCTRL_ENSF0;
237 1.2 uch reg &= ~(TX39_SIBCTRL_ENSF1 | TX39_SIBCTRL_SELTELSF1 |
238 1.2 uch TX39_SIBCTRL_SELSNDSF1);
239 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
240 1.1 uch
241 1.2 uch /* disable TX39SIB module */
242 1.2 uch reg &= ~TX39_SIBCTRL_ENSIB;
243 1.2 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
244 1.1 uch }
245 1.1 uch
246 1.2 uch int
247 1.2 uch tx39sib_clock(dev)
248 1.2 uch struct device *dev;
249 1.2 uch {
250 1.2 uch struct tx39sib_softc *sc = (void*)dev;
251 1.2 uch
252 1.2 uch return TX39_CLK2X / sibsclk_divide_table[sc->sc_param.sp_clock];
253 1.2 uch }
254 1.1 uch
255 1.1 uch int
256 1.1 uch tx39sib_search(parent, cf, aux)
257 1.1 uch struct device *parent;
258 1.1 uch struct cfdata *cf;
259 1.1 uch void *aux;
260 1.1 uch {
261 1.1 uch struct tx39sib_softc *sc = (void*)parent;
262 1.1 uch struct txsib_attach_args sa;
263 1.1 uch
264 1.1 uch sa.sa_tc = sc->sc_tc;
265 1.1 uch sa.sa_slot = cf->cf_loc[TXSIBIFCF_SLOT];
266 1.2 uch sa.sa_snd_rate = sc->sc_param.sp_snd_rate;
267 1.2 uch sa.sa_tel_rate = sc->sc_param.sp_tel_rate;
268 1.1 uch
269 1.1 uch if (sa.sa_slot == TXSIBIFCF_SLOT_DEFAULT) {
270 1.1 uch printf("tx39sib_search: wildcarded slot, skipping\n");
271 1.1 uch return 0;
272 1.1 uch }
273 1.1 uch
274 1.1 uch if (!(sc->sc_attached & (1 << sa.sa_slot)) &&/* not attached slot */
275 1.1 uch (*cf->cf_attach->ca_match)(parent, cf, &sa)) {
276 1.1 uch config_attach(parent, cf, &sa, tx39sib_print);
277 1.1 uch sc->sc_attached |= (1 << sa.sa_slot);
278 1.1 uch }
279 1.1 uch
280 1.1 uch return 0;
281 1.1 uch }
282 1.1 uch
283 1.1 uch int
284 1.1 uch tx39sib_print(aux, pnp)
285 1.1 uch void *aux;
286 1.1 uch const char *pnp;
287 1.1 uch {
288 1.1 uch struct txsib_attach_args *sa = aux;
289 1.1 uch
290 1.1 uch printf(" slot %d", sa->sa_slot);
291 1.1 uch
292 1.1 uch return QUIET;
293 1.1 uch }
294 1.1 uch
295 1.1 uch /*
296 1.1 uch * sync access method. don't use runtime.
297 1.1 uch */
298 1.1 uch
299 1.1 uch __inline int
300 1.1 uch __txsibsf0_ready(tc)
301 1.1 uch tx_chipset_tag_t tc;
302 1.1 uch {
303 1.1 uch int i;
304 1.1 uch
305 1.1 uch tx_conf_write(tc, TX39_INTRSTATUS1_REG, TX39_INTRSTATUS1_SIBSF0INT);
306 1.1 uch for (i = 0; (!(tx_conf_read(tc, TX39_INTRSTATUS1_REG) &
307 1.1 uch TX39_INTRSTATUS1_SIBSF0INT)) && i < 100; i++)
308 1.1 uch ;
309 1.1 uch if (i == 100) {
310 1.1 uch printf("sf0 busy\n");
311 1.1 uch return 0;
312 1.1 uch } else if (i > 50) {
313 1.1 uch printf("sf0 busy loop:%d\n", i);
314 1.1 uch return 0;
315 1.1 uch }
316 1.1 uch
317 1.1 uch return 1;
318 1.1 uch }
319 1.1 uch
320 1.1 uch void
321 1.1 uch txsibsf0_reg_write(tc, addr, val)
322 1.1 uch tx_chipset_tag_t tc;
323 1.1 uch int addr;
324 1.1 uch u_int16_t val;
325 1.1 uch {
326 1.1 uch txreg_t reg;
327 1.1 uch
328 1.1 uch reg = txsibsf0_read(tc, addr);
329 1.1 uch reg |= TX39_SIBSF0_WRITE;
330 1.1 uch TX39_SIBSF0_REGDATA_CLR(reg);
331 1.1 uch reg = TX39_SIBSF0_REGDATA_SET(reg, val);
332 1.1 uch
333 1.1 uch __txsibsf0_ready(tc);
334 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
335 1.1 uch }
336 1.1 uch
337 1.1 uch u_int16_t
338 1.1 uch txsibsf0_reg_read(tc, addr)
339 1.1 uch tx_chipset_tag_t tc;
340 1.1 uch int addr;
341 1.1 uch {
342 1.1 uch return TX39_SIBSF0_REGDATA(txsibsf0_read(tc, addr));
343 1.1 uch }
344 1.1 uch
345 1.1 uch u_int32_t
346 1.1 uch txsibsf0_read(tc, addr)
347 1.1 uch tx_chipset_tag_t tc;
348 1.1 uch int addr;
349 1.1 uch {
350 1.1 uch txreg_t reg;
351 1.1 uch int retry = 3;
352 1.1 uch
353 1.1 uch do {
354 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(0, addr);
355 1.1 uch __txsibsf0_ready(tc);
356 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
357 1.1 uch
358 1.1 uch __txsibsf0_ready(tc);
359 1.1 uch reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
360 1.1 uch
361 1.1 uch } while ((TX39_SIBSF0_REGADDR(reg) != addr) && --retry > 0);
362 1.1 uch
363 1.1 uch if (retry <= 0)
364 1.1 uch printf("txsibsf0_read: command failed\n");
365 1.1 uch
366 1.1 uch return reg;
367 1.1 uch }
368 1.1 uch
369 1.1 uch /*
370 1.1 uch * debug functions.
371 1.1 uch */
372 1.1 uch
373 1.1 uch #define ISSETPRINT_CTRL(r, m) \
374 1.1 uch __is_set_print(r, TX39_SIBCTRL_##m, #m)
375 1.1 uch #define ISSETPRINT_DMACTRL(r, m) \
376 1.1 uch __is_set_print(r, TX39_SIBDMACTRL_##m, #m)
377 1.1 uch
378 1.1 uch void
379 1.1 uch tx39sib_dump(sc)
380 1.1 uch struct tx39sib_softc *sc;
381 1.1 uch {
382 1.1 uch tx_chipset_tag_t tc = sc->sc_tc;
383 1.1 uch txreg_t reg;
384 1.1 uch
385 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
386 1.1 uch ISSETPRINT_CTRL(reg, SIBIRQ);
387 1.1 uch ISSETPRINT_CTRL(reg, ENCNTTEST);
388 1.1 uch ISSETPRINT_CTRL(reg, ENDMATEST);
389 1.1 uch ISSETPRINT_CTRL(reg, SNDMONO);
390 1.1 uch ISSETPRINT_CTRL(reg, RMONOSNDIN);
391 1.1 uch ISSETPRINT_CTRL(reg, TEL16);
392 1.1 uch ISSETPRINT_CTRL(reg, SND16);
393 1.1 uch ISSETPRINT_CTRL(reg, SELTELSF1);
394 1.1 uch ISSETPRINT_CTRL(reg, SELSNDSF1);
395 1.1 uch ISSETPRINT_CTRL(reg, ENTEL);
396 1.1 uch ISSETPRINT_CTRL(reg, ENSND);
397 1.1 uch ISSETPRINT_CTRL(reg, SIBLOOP);
398 1.1 uch ISSETPRINT_CTRL(reg, ENSF1);
399 1.1 uch ISSETPRINT_CTRL(reg, ENSF0);
400 1.1 uch ISSETPRINT_CTRL(reg, ENSIB);
401 1.1 uch printf("\n");
402 1.1 uch printf("SCLKDIV %d\n", TX39_SIBCTRL_SCLKDIV(reg));
403 1.1 uch printf("TELFSDIV %d\n", TX39_SIBCTRL_TELFSDIV(reg));
404 1.1 uch printf("SNDFSDIV %d\n", TX39_SIBCTRL_SNDFSDIV(reg));
405 1.1 uch
406 1.1 uch reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
407 1.1 uch ISSETPRINT_DMACTRL(reg, SNDBUFF1TIME);
408 1.1 uch ISSETPRINT_DMACTRL(reg, SNDDMALOOP);
409 1.1 uch ISSETPRINT_DMACTRL(reg, ENDMARXSND);
410 1.1 uch ISSETPRINT_DMACTRL(reg, ENDMATXSND);
411 1.1 uch ISSETPRINT_DMACTRL(reg, TELBUFF1TIME);
412 1.1 uch ISSETPRINT_DMACTRL(reg, TELDMALOOP);
413 1.1 uch ISSETPRINT_DMACTRL(reg, ENDMARXTEL);
414 1.1 uch ISSETPRINT_DMACTRL(reg, ENDMATXTEL);
415 1.1 uch printf("\n");
416 1.1 uch printf("SNDDMAPTR %d\n", TX39_SIBDMACTRL_TELDMAPTR(reg));
417 1.1 uch printf("TELDMAPTR %d\n", TX39_SIBDMACTRL_SNDDMAPTR(reg));
418 1.1 uch
419 1.1 uch }
420