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