fm801.c revision 108064
1218822Sdim/* 294536Sobrien * Copyright (c) 2000 Dmitry Dicky diwil@dataart.com 333965Sjdp * All rights reserved. 433965Sjdp * 533965Sjdp * Redistribution and use in source and binary forms, with or without 633965Sjdp * modification, are permitted provided that the following conditions 7130561Sobrien * are met: 833965Sjdp * 1. Redistributions of source code must retain the above copyright 933965Sjdp * notice, this list of conditions and the following disclaimer. 1033965Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1133965Sjdp * notice, this list of conditions and the following disclaimer in the 1233965Sjdp * documentation and/or other materials provided with the distribution. 1333965Sjdp * 1433965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND 1533965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1633965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1733965Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1833965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1933965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2033965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2194536Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2294536Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23130561Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24130561Sobrien * SUCH DAMAGE. 2533965Sjdp */ 2694536Sobrien 27218822Sdim#include <dev/sound/pcm/sound.h> 28218822Sdim#include <dev/sound/pcm/ac97.h> 29218822Sdim#include <pci/pcireg.h> 30218822Sdim#include <pci/pcivar.h> 31218822Sdim 32218822SdimSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/fm801.c 108064 2002-12-18 22:53:24Z semenu $"); 33218822Sdim 3494536Sobrien#define PCI_VENDOR_FORTEMEDIA 0x1319 35218822Sdim#define PCI_DEVICE_FORTEMEDIA1 0x08011319 3694536Sobrien#define PCI_DEVICE_FORTEMEDIA2 0x08021319 /* ??? have no idea what's this... */ 3794536Sobrien 38218822Sdim#define FM_PCM_VOLUME 0x00 3994536Sobrien#define FM_FM_VOLUME 0x02 4094536Sobrien#define FM_I2S_VOLUME 0x04 41218822Sdim#define FM_RECORD_SOURCE 0x06 4294536Sobrien 4394536Sobrien#define FM_PLAY_CTL 0x08 44218822Sdim#define FM_PLAY_RATE_MASK 0x0f00 4594536Sobrien#define FM_PLAY_BUF1_LAST 0x0001 4694536Sobrien#define FM_PLAY_BUF2_LAST 0x0002 47218822Sdim#define FM_PLAY_START 0x0020 4894536Sobrien#define FM_PLAY_PAUSE 0x0040 4994536Sobrien#define FM_PLAY_STOPNOW 0x0080 50218822Sdim#define FM_PLAY_16BIT 0x4000 51130561Sobrien#define FM_PLAY_STEREO 0x8000 52130561Sobrien 53130561Sobrien#define FM_PLAY_DMALEN 0x0a 54218822Sdim#define FM_PLAY_DMABUF1 0x0c 55218822Sdim#define FM_PLAY_DMABUF2 0x10 56218822Sdim 57218822Sdim 58130561Sobrien#define FM_REC_CTL 0x14 59218822Sdim#define FM_REC_RATE_MASK 0x0f00 60130561Sobrien#define FM_REC_BUF1_LAST 0x0001 61218822Sdim#define FM_REC_BUF2_LAST 0x0002 62130561Sobrien#define FM_REC_START 0x0020 63218822Sdim#define FM_REC_PAUSE 0x0040 64130561Sobrien#define FM_REC_STOPNOW 0x0080 65130561Sobrien#define FM_REC_16BIT 0x4000 6694536Sobrien#define FM_REC_STEREO 0x8000 6794536Sobrien 6833965Sjdp 6933965Sjdp#define FM_REC_DMALEN 0x16 7033965Sjdp#define FM_REC_DMABUF1 0x18 7133965Sjdp#define FM_REC_DMABUF2 0x1c 7233965Sjdp 7333965Sjdp#define FM_CODEC_CTL 0x22 7433965Sjdp#define FM_VOLUME 0x26 7533965Sjdp#define FM_VOLUME_MUTE 0x8000 7633965Sjdp 7733965Sjdp#define FM_CODEC_CMD 0x2a 7833965Sjdp#define FM_CODEC_CMD_READ 0x0080 7933965Sjdp#define FM_CODEC_CMD_VALID 0x0100 8033965Sjdp#define FM_CODEC_CMD_BUSY 0x0200 8133965Sjdp 8233965Sjdp#define FM_CODEC_DATA 0x2c 8333965Sjdp 8433965Sjdp#define FM_IO_CTL 0x52 8533965Sjdp#define FM_CARD_CTL 0x54 8633965Sjdp 8733965Sjdp#define FM_INTMASK 0x56 8833965Sjdp#define FM_INTMASK_PLAY 0x0001 8933965Sjdp#define FM_INTMASK_REC 0x0002 9033965Sjdp#define FM_INTMASK_VOL 0x0040 9133965Sjdp#define FM_INTMASK_MPU 0x0080 9233965Sjdp 9333965Sjdp#define FM_INTSTATUS 0x5a 9433965Sjdp#define FM_INTSTATUS_PLAY 0x0100 9533965Sjdp#define FM_INTSTATUS_REC 0x0200 9633965Sjdp#define FM_INTSTATUS_VOL 0x4000 9733965Sjdp#define FM_INTSTATUS_MPU 0x8000 9833965Sjdp 9933965Sjdp#define FM801_DEFAULT_BUFSZ 4096 /* Other values do not work!!! */ 10033965Sjdp 10133965Sjdp/* debug purposes */ 10233965Sjdp#define DPRINT if(0) printf 10333965Sjdp 10433965Sjdp/* 10533965Sjdpstatic int fm801ch_setup(struct pcm_channel *c); 10633965Sjdp*/ 10733965Sjdp 10833965Sjdpstatic u_int32_t fmts[] = { 10933965Sjdp AFMT_U8, 11033965Sjdp AFMT_STEREO | AFMT_U8, 11133965Sjdp AFMT_S16_LE, 11233965Sjdp AFMT_STEREO | AFMT_S16_LE, 11333965Sjdp 0 11433965Sjdp}; 11533965Sjdp 11633965Sjdpstatic struct pcmchan_caps fm801ch_caps = { 11733965Sjdp 4000, 48000, 11833965Sjdp fmts, 0 11933965Sjdp}; 12033965Sjdp 12133965Sjdpstruct fm801_info; 12233965Sjdp 12333965Sjdpstruct fm801_chinfo { 12433965Sjdp struct fm801_info *parent; 12533965Sjdp struct pcm_channel *channel; 12633965Sjdp struct snd_dbuf *buffer; 12733965Sjdp u_int32_t spd, dir, fmt; /* speed, direction, format */ 12833965Sjdp u_int32_t shift; 12933965Sjdp}; 13033965Sjdp 13133965Sjdpstruct fm801_info { 13233965Sjdp int type; 13333965Sjdp bus_space_tag_t st; 13433965Sjdp bus_space_handle_t sh; 13533965Sjdp bus_dma_tag_t parent_dmat; 13633965Sjdp 13733965Sjdp device_t dev; 13833965Sjdp int num; 13933965Sjdp u_int32_t unit; 14033965Sjdp 14133965Sjdp struct resource *reg, *irq; 14233965Sjdp int regtype, regid, irqid; 14333965Sjdp void *ih; 14433965Sjdp 14533965Sjdp u_int32_t play_flip, 14633965Sjdp play_nextblk, 14733965Sjdp play_start, 14833965Sjdp play_blksize, 14933965Sjdp play_fmt, 15033965Sjdp play_shift, 15133965Sjdp play_size; 15233965Sjdp 15333965Sjdp u_int32_t rec_flip, 15433965Sjdp rec_nextblk, 15533965Sjdp rec_start, 15633965Sjdp rec_blksize, 15733965Sjdp rec_fmt, 15833965Sjdp rec_shift, 15933965Sjdp rec_size; 16033965Sjdp 16133965Sjdp unsigned int bufsz; 16233965Sjdp 16333965Sjdp struct fm801_chinfo pch, rch; 16433965Sjdp 16533965Sjdp device_t radio; 16633965Sjdp}; 16733965Sjdp 16833965Sjdp/* Bus Read / Write routines */ 16933965Sjdpstatic u_int32_t 17033965Sjdpfm801_rd(struct fm801_info *fm801, int regno, int size) 17133965Sjdp{ 17233965Sjdp switch(size) { 17333965Sjdp case 1: 17433965Sjdp return (bus_space_read_1(fm801->st, fm801->sh, regno)); 17533965Sjdp case 2: 176130561Sobrien return (bus_space_read_2(fm801->st, fm801->sh, regno)); 177130561Sobrien case 4: 178130561Sobrien return (bus_space_read_4(fm801->st, fm801->sh, regno)); 17933965Sjdp default: 180130561Sobrien return 0xffffffff; 181130561Sobrien } 182130561Sobrien} 183130561Sobrien 184130561Sobrienstatic void 185130561Sobrienfm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size) 186130561Sobrien{ 187130561Sobrien 188130561Sobrien switch(size) { 189130561Sobrien case 1: 190130561Sobrien bus_space_write_1(fm801->st, fm801->sh, regno, data); 191130561Sobrien break; 192130561Sobrien case 2: 193130561Sobrien bus_space_write_2(fm801->st, fm801->sh, regno, data); 19433965Sjdp break; 19533965Sjdp case 4: 19633965Sjdp bus_space_write_4(fm801->st, fm801->sh, regno, data); 19738889Sjdp break; 19838889Sjdp } 19938889Sjdp} 20038889Sjdp 20133965Sjdp/* -------------------------------------------------------------------- */ 20238889Sjdp/* 20338889Sjdp * ac97 codec routines 20438889Sjdp */ 20538889Sjdp#define TIMO 50 20638889Sjdpstatic int 20738889Sjdpfm801_rdcd(kobj_t obj, void *devinfo, int regno) 20838889Sjdp{ 20938889Sjdp struct fm801_info *fm801 = (struct fm801_info *)devinfo; 21033965Sjdp int i; 21133965Sjdp 21233965Sjdp for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 21333965Sjdp DELAY(10000); 21433965Sjdp DPRINT("fm801 rdcd: 1 - DELAY\n"); 21533965Sjdp } 21633965Sjdp if (i >= TIMO) { 21733965Sjdp printf("fm801 rdcd: codec busy\n"); 218130561Sobrien return 0; 219130561Sobrien } 220130561Sobrien 22133965Sjdp fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2); 22233965Sjdp 22333965Sjdp for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++) 22433965Sjdp { 22533965Sjdp DELAY(10000); 22633965Sjdp DPRINT("fm801 rdcd: 2 - DELAY\n"); 22733965Sjdp } 22833965Sjdp if (i >= TIMO) { 22933965Sjdp printf("fm801 rdcd: write codec invalid\n"); 23033965Sjdp return 0; 23133965Sjdp } 23233965Sjdp 23333965Sjdp return fm801_rd(fm801,FM_CODEC_DATA,2); 23433965Sjdp} 23533965Sjdp 23633965Sjdpstatic int 23733965Sjdpfm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data) 23833965Sjdp{ 23933965Sjdp struct fm801_info *fm801 = (struct fm801_info *)devinfo; 24033965Sjdp int i; 24133965Sjdp 24233965Sjdp DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data); 24333965Sjdp/* 24433965Sjdp if(regno == AC97_REG_RECSEL) return; 24533965Sjdp*/ 24633965Sjdp /* Poll until codec is ready */ 24733965Sjdp for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 24833965Sjdp DELAY(10000); 24933965Sjdp DPRINT("fm801 rdcd: 1 - DELAY\n"); 25033965Sjdp } 25133965Sjdp if (i >= TIMO) { 25233965Sjdp printf("fm801 wrcd: read codec busy\n"); 25333965Sjdp return -1; 25433965Sjdp } 25533965Sjdp 25633965Sjdp fm801_wr(fm801,FM_CODEC_DATA,data, 2); 25733965Sjdp fm801_wr(fm801,FM_CODEC_CMD, regno,2); 25833965Sjdp 25933965Sjdp /* wait until codec is ready */ 26033965Sjdp for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 26133965Sjdp DELAY(10000); 26233965Sjdp DPRINT("fm801 wrcd: 2 - DELAY\n"); 26333965Sjdp } 26433965Sjdp if (i >= TIMO) { 26533965Sjdp printf("fm801 wrcd: read codec busy\n"); 26633965Sjdp return -1; 26733965Sjdp } 26833965Sjdp DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data); 26933965Sjdp return 0; 27033965Sjdp} 27133965Sjdp 27233965Sjdpstatic kobj_method_t fm801_ac97_methods[] = { 27333965Sjdp KOBJMETHOD(ac97_read, fm801_rdcd), 27433965Sjdp KOBJMETHOD(ac97_write, fm801_wrcd), 27533965Sjdp { 0, 0 } 27633965Sjdp}; 27733965SjdpAC97_DECLARE(fm801_ac97); 27833965Sjdp 27933965Sjdp/* -------------------------------------------------------------------- */ 28033965Sjdp 28133965Sjdp/* 28233965Sjdp * The interrupt handler 28333965Sjdp */ 28433965Sjdpstatic void 28533965Sjdpfm801_intr(void *p) 28633965Sjdp{ 28733965Sjdp struct fm801_info *fm801 = (struct fm801_info *)p; 28833965Sjdp u_int32_t intsrc = fm801_rd(fm801, FM_INTSTATUS, 2); 28933965Sjdp 29033965Sjdp DPRINT("\nfm801_intr intsrc 0x%x ", intsrc); 29133965Sjdp 29233965Sjdp if(intsrc & FM_INTSTATUS_PLAY) { 29333965Sjdp fm801->play_flip++; 29433965Sjdp if(fm801->play_flip & 1) { 29533965Sjdp fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4); 29633965Sjdp } else 29733965Sjdp fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4); 29833965Sjdp chn_intr(fm801->pch.channel); 29933965Sjdp } 30033965Sjdp 30133965Sjdp if(intsrc & FM_INTSTATUS_REC) { 30233965Sjdp fm801->rec_flip++; 30333965Sjdp if(fm801->rec_flip & 1) { 30433965Sjdp fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4); 30533965Sjdp } else 30633965Sjdp fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4); 30733965Sjdp chn_intr(fm801->rch.channel); 30833965Sjdp } 30933965Sjdp 31033965Sjdp if ( intsrc & FM_INTSTATUS_MPU ) { 31133965Sjdp /* This is a TODOish thing... */ 31233965Sjdp fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2); 31333965Sjdp } 31433965Sjdp 31533965Sjdp if ( intsrc & FM_INTSTATUS_VOL ) { 31633965Sjdp /* This is a TODOish thing... */ 31733965Sjdp fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2); 31833965Sjdp } 31933965Sjdp 32033965Sjdp DPRINT("fm801_intr clear\n\n"); 32133965Sjdp fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2); 32233965Sjdp} 32333965Sjdp 32433965Sjdp/* -------------------------------------------------------------------- */ 32533965Sjdp/* channel interface */ 326130561Sobrienstatic void * 32733965Sjdpfm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 32833965Sjdp{ 32933965Sjdp struct fm801_info *fm801 = (struct fm801_info *)devinfo; 33033965Sjdp struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch; 33133965Sjdp 332 DPRINT("fm801ch_init, direction = %d\n", dir); 333 ch->parent = fm801; 334 ch->channel = c; 335 ch->buffer = b; 336 ch->dir = dir; 337 if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, fm801->bufsz) == -1) return NULL; 338 return (void *)ch; 339} 340 341static int 342fm801ch_setformat(kobj_t obj, void *data, u_int32_t format) 343{ 344 struct fm801_chinfo *ch = data; 345 struct fm801_info *fm801 = ch->parent; 346 347 DPRINT("fm801ch_setformat 0x%x : %s, %s, %s, %s\n", format, 348 (format & AFMT_STEREO)?"stereo":"mono", 349 (format & (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)) ? "16bit":"8bit", 350 (format & AFMT_SIGNED)? "signed":"unsigned", 351 (format & AFMT_BIGENDIAN)?"bigendiah":"littleendian" ); 352 353 if(ch->dir == PCMDIR_PLAY) { 354 fm801->play_fmt = (format & AFMT_STEREO)? FM_PLAY_STEREO : 0; 355 fm801->play_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0; 356 return 0; 357 } 358 359 if(ch->dir == PCMDIR_REC ) { 360 fm801->rec_fmt = (format & AFMT_STEREO)? FM_REC_STEREO:0; 361 fm801->rec_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0; 362 return 0; 363 } 364 365 return 0; 366} 367 368struct { 369 int limit; 370 int rate; 371} fm801_rates[11] = { 372 { 6600, 5500 }, 373 { 8750, 8000 }, 374 { 10250, 9600 }, 375 { 13200, 11025 }, 376 { 17500, 16000 }, 377 { 20500, 19200 }, 378 { 26500, 22050 }, 379 { 35000, 32000 }, 380 { 41000, 38400 }, 381 { 46000, 44100 }, 382 { 48000, 48000 }, 383/* anything above -> 48000 */ 384}; 385 386static int 387fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed) 388{ 389 struct fm801_chinfo *ch = data; 390 struct fm801_info *fm801 = ch->parent; 391 register int i; 392 393 394 for (i = 0; i < 10 && fm801_rates[i].limit <= speed; i++) ; 395 396 if(ch->dir == PCMDIR_PLAY) { 397 fm801->pch.spd = fm801_rates[i].rate; 398 fm801->play_shift = (i<<8); 399 fm801->play_shift &= FM_PLAY_RATE_MASK; 400 } 401 402 if(ch->dir == PCMDIR_REC ) { 403 fm801->rch.spd = fm801_rates[i].rate; 404 fm801->rec_shift = (i<<8); 405 fm801->rec_shift &= FM_REC_RATE_MASK; 406 } 407 408 ch->spd = fm801_rates[i].rate; 409 410 return fm801_rates[i].rate; 411} 412 413static int 414fm801ch_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 415{ 416 struct fm801_chinfo *ch = data; 417 struct fm801_info *fm801 = ch->parent; 418 419 if(ch->dir == PCMDIR_PLAY) { 420 if(fm801->play_flip) return fm801->play_blksize; 421 fm801->play_blksize = blocksize; 422 } 423 424 if(ch->dir == PCMDIR_REC) { 425 if(fm801->rec_flip) return fm801->rec_blksize; 426 fm801->rec_blksize = blocksize; 427 } 428 429 DPRINT("fm801ch_setblocksize %d (dir %d)\n",blocksize, ch->dir); 430 431 return blocksize; 432} 433 434static int 435fm801ch_trigger(kobj_t obj, void *data, int go) 436{ 437 struct fm801_chinfo *ch = data; 438 struct fm801_info *fm801 = ch->parent; 439 u_int32_t baseaddr = vtophys(sndbuf_getbuf(ch->buffer)); 440 u_int32_t k1; 441 442 DPRINT("fm801ch_trigger go %d , ", go); 443 444 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) { 445 return 0; 446 } 447 448 if (ch->dir == PCMDIR_PLAY) { 449 if (go == PCMTRIG_START) { 450 451 fm801->play_start = baseaddr; 452 fm801->play_nextblk = fm801->play_start + fm801->play_blksize; 453 fm801->play_flip = 0; 454 fm801_wr(fm801, FM_PLAY_DMALEN, fm801->play_blksize - 1, 2); 455 fm801_wr(fm801, FM_PLAY_DMABUF1,fm801->play_start,4); 456 fm801_wr(fm801, FM_PLAY_DMABUF2,fm801->play_nextblk,4); 457 fm801_wr(fm801, FM_PLAY_CTL, 458 FM_PLAY_START | FM_PLAY_STOPNOW | fm801->play_fmt | fm801->play_shift, 459 2 ); 460 } else { 461 fm801->play_flip = 0; 462 k1 = fm801_rd(fm801, FM_PLAY_CTL,2); 463 fm801_wr(fm801, FM_PLAY_CTL, 464 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) | 465 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST, 2 ); 466 } 467 } else if(ch->dir == PCMDIR_REC) { 468 if (go == PCMTRIG_START) { 469 fm801->rec_start = baseaddr; 470 fm801->rec_nextblk = fm801->rec_start + fm801->rec_blksize; 471 fm801->rec_flip = 0; 472 fm801_wr(fm801, FM_REC_DMALEN, fm801->rec_blksize - 1, 2); 473 fm801_wr(fm801, FM_REC_DMABUF1,fm801->rec_start,4); 474 fm801_wr(fm801, FM_REC_DMABUF2,fm801->rec_nextblk,4); 475 fm801_wr(fm801, FM_REC_CTL, 476 FM_REC_START | FM_REC_STOPNOW | fm801->rec_fmt | fm801->rec_shift, 477 2 ); 478 } else { 479 fm801->rec_flip = 0; 480 k1 = fm801_rd(fm801, FM_REC_CTL,2); 481 fm801_wr(fm801, FM_REC_CTL, 482 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) | 483 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST, 2); 484 } 485 } 486 487 return 0; 488} 489 490/* Almost ALSA copy */ 491static int 492fm801ch_getptr(kobj_t obj, void *data) 493{ 494 struct fm801_chinfo *ch = data; 495 struct fm801_info *fm801 = ch->parent; 496 int result = 0; 497 498 if (ch->dir == PCMDIR_PLAY) { 499 result = fm801_rd(fm801, 500 (fm801->play_flip&1) ? 501 FM_PLAY_DMABUF2:FM_PLAY_DMABUF1, 4) - fm801->play_start; 502 } 503 504 if (ch->dir == PCMDIR_REC) { 505 result = fm801_rd(fm801, 506 (fm801->rec_flip&1) ? 507 FM_REC_DMABUF2:FM_REC_DMABUF1, 4) - fm801->rec_start; 508 } 509 510 return result; 511} 512 513static struct pcmchan_caps * 514fm801ch_getcaps(kobj_t obj, void *data) 515{ 516 return &fm801ch_caps; 517} 518 519static kobj_method_t fm801ch_methods[] = { 520 KOBJMETHOD(channel_init, fm801ch_init), 521 KOBJMETHOD(channel_setformat, fm801ch_setformat), 522 KOBJMETHOD(channel_setspeed, fm801ch_setspeed), 523 KOBJMETHOD(channel_setblocksize, fm801ch_setblocksize), 524 KOBJMETHOD(channel_trigger, fm801ch_trigger), 525 KOBJMETHOD(channel_getptr, fm801ch_getptr), 526 KOBJMETHOD(channel_getcaps, fm801ch_getcaps), 527 { 0, 0 } 528}; 529CHANNEL_DECLARE(fm801ch); 530 531/* -------------------------------------------------------------------- */ 532 533/* 534 * Init routine is taken from an original NetBSD driver 535 */ 536static int 537fm801_init(struct fm801_info *fm801) 538{ 539 u_int32_t k1; 540 541 /* reset codec */ 542 fm801_wr(fm801, FM_CODEC_CTL, 0x0020,2); 543 DELAY(100000); 544 fm801_wr(fm801, FM_CODEC_CTL, 0x0000,2); 545 DELAY(100000); 546 547 fm801_wr(fm801, FM_PCM_VOLUME, 0x0808,2); 548 fm801_wr(fm801, FM_FM_VOLUME, 0x0808,2); 549 fm801_wr(fm801, FM_I2S_VOLUME, 0x0808,2); 550 fm801_wr(fm801, 0x40,0x107f,2); /* enable legacy audio */ 551 552 fm801_wr((void *)fm801, FM_RECORD_SOURCE, 0x0000,2); 553 554 /* Unmask playback, record and mpu interrupts, mask the rest */ 555 k1 = fm801_rd((void *)fm801, FM_INTMASK,2); 556 fm801_wr(fm801, FM_INTMASK, 557 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) | 558 FM_INTMASK_VOL,2); 559 fm801_wr(fm801, FM_INTSTATUS, 560 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU | 561 FM_INTSTATUS_VOL,2); 562 563 DPRINT("FM801 init Ok\n"); 564 return 0; 565} 566 567static int 568fm801_pci_attach(device_t dev) 569{ 570 u_int32_t data; 571 struct ac97_info *codec = 0; 572 struct fm801_info *fm801; 573 int i; 574 int mapped = 0; 575 char status[SND_STATUSLEN]; 576 577 if ((fm801 = (struct fm801_info *)malloc(sizeof(*fm801), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) { 578 device_printf(dev, "cannot allocate softc\n"); 579 return ENXIO; 580 } 581 582 fm801->type = pci_get_devid(dev); 583 584 data = pci_read_config(dev, PCIR_COMMAND, 2); 585 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 586 pci_write_config(dev, PCIR_COMMAND, data, 2); 587 data = pci_read_config(dev, PCIR_COMMAND, 2); 588 589 for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) { 590 fm801->regid = PCIR_MAPS + i*4; 591 fm801->regtype = SYS_RES_MEMORY; 592 fm801->reg = bus_alloc_resource(dev, fm801->regtype, &fm801->regid, 593 0, ~0, 1, RF_ACTIVE); 594 if(!fm801->reg) 595 { 596 fm801->regtype = SYS_RES_IOPORT; 597 fm801->reg = bus_alloc_resource(dev, fm801->regtype, &fm801->regid, 598 0, ~0, 1, RF_ACTIVE); 599 } 600 601 if(fm801->reg) { 602 fm801->st = rman_get_bustag(fm801->reg); 603 fm801->sh = rman_get_bushandle(fm801->reg); 604 mapped++; 605 } 606 } 607 608 if (mapped == 0) { 609 device_printf(dev, "unable to map register space\n"); 610 goto oops; 611 } 612 613 fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536); 614 615 fm801_init(fm801); 616 617 codec = AC97_CREATE(dev, fm801, fm801_ac97); 618 if (codec == NULL) goto oops; 619 620 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto oops; 621 622 fm801->irqid = 0; 623 fm801->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &fm801->irqid, 624 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 625 if (!fm801->irq || snd_setup_intr(dev, fm801->irq, 0, fm801_intr, fm801, &fm801->ih)) { 626 device_printf(dev, "unable to map interrupt\n"); 627 goto oops; 628 } 629 630 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 631 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 632 /*highaddr*/BUS_SPACE_MAXADDR, 633 /*filter*/NULL, /*filterarg*/NULL, 634 /*maxsize*/fm801->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff, 635 /*flags*/0, &fm801->parent_dmat) != 0) { 636 device_printf(dev, "unable to create dma tag\n"); 637 goto oops; 638 } 639 640 snprintf(status, 64, "at %s 0x%lx irq %ld", 641 (fm801->regtype == SYS_RES_IOPORT)? "io" : "memory", 642 rman_get_start(fm801->reg), rman_get_start(fm801->irq)); 643 644#define FM801_MAXPLAYCH 1 645 if (pcm_register(dev, fm801, FM801_MAXPLAYCH, 1)) goto oops; 646 pcm_addchan(dev, PCMDIR_PLAY, &fm801ch_class, fm801); 647 pcm_addchan(dev, PCMDIR_REC, &fm801ch_class, fm801); 648 pcm_setstatus(dev, status); 649 650 fm801->radio = device_add_child(dev, "radio", -1); 651 bus_generic_attach(dev); 652 653 return 0; 654 655oops: 656 if (codec) ac97_destroy(codec); 657 if (fm801->reg) bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg); 658 if (fm801->ih) bus_teardown_intr(dev, fm801->irq, fm801->ih); 659 if (fm801->irq) bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq); 660 if (fm801->parent_dmat) bus_dma_tag_destroy(fm801->parent_dmat); 661 free(fm801, M_DEVBUF); 662 return ENXIO; 663} 664 665static int 666fm801_pci_detach(device_t dev) 667{ 668 int r; 669 struct fm801_info *fm801; 670 671 DPRINT("Forte Media FM801 detach\n"); 672 673 fm801 = pcm_getdevinfo(dev); 674 675 r = bus_generic_detach(dev); 676 if (r) 677 return r; 678 if (fm801->radio != NULL) { 679 r = device_delete_child(dev, fm801->radio); 680 if (r) 681 return r; 682 fm801->radio = NULL; 683 } 684 685 r = pcm_unregister(dev); 686 if (r) 687 return r; 688 689 bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg); 690 bus_teardown_intr(dev, fm801->irq, fm801->ih); 691 bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq); 692 bus_dma_tag_destroy(fm801->parent_dmat); 693 free(fm801, M_DEVBUF); 694 return 0; 695} 696 697static int 698fm801_pci_probe( device_t dev ) 699{ 700 u_int32_t data; 701 int id, regtype, regid, result; 702 struct resource *reg; 703 bus_space_tag_t st; 704 bus_space_handle_t sh; 705 706 result = ENXIO; 707 708 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA1 ) { 709 data = pci_read_config(dev, PCIR_COMMAND, 2); 710 data |= (PCIM_CMD_PORTEN|PCIM_CMD_BUSMASTEREN); 711 pci_write_config(dev, PCIR_COMMAND, data, 2); 712 data = pci_read_config(dev, PCIR_COMMAND, 2); 713 714 regid = PCIR_MAPS; 715 regtype = SYS_RES_IOPORT; 716 reg = bus_alloc_resource(dev, regtype, ®id, 0, ~0, 1, 717 RF_ACTIVE); 718 719 if (reg == NULL) 720 return ENXIO; 721 722 st = rman_get_bustag(reg); 723 sh = rman_get_bushandle(reg); 724 /* 725 * XXX: quick check that device actually has sound capabilities. 726 * The problem is that some cards built around FM801 chip only 727 * have radio tuner onboard, but no sound capabilities. There 728 * is no "official" way to quickly check this, because all 729 * IDs are exactly the same. The only difference is 0x28 730 * device control register, described in FM801 specification 731 * as "SRC/Mixer Test Control/DFC Status", but without 732 * any more detailed explanation. According to specs, and 733 * available sample cards (SF256-PCP-R and SF256-PCS-R) its 734 * power-on value should be `0', while on AC97-less tuner 735 * card (SF64-PCR) it was 0x80. 736 */ 737 if (bus_space_read_1(st, sh, 0x28) == 0) { 738 device_set_desc(dev, 739 "Forte Media FM801 Audio Controller"); 740 result = 0; 741 } 742 743 bus_release_resource(dev, regtype, regid, reg); 744 } 745/* 746 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA2 ) { 747 device_set_desc(dev, "Forte Media FM801 Joystick (Not Supported)"); 748 return ENXIO; 749 } 750*/ 751 return (result); 752} 753 754static struct resource * 755fm801_alloc_resource(device_t bus, device_t child, int type, int *rid, 756 u_long start, u_long end, u_long count, u_int flags) 757{ 758 struct fm801_info *fm801; 759 760 fm801 = pcm_getdevinfo(bus); 761 762 if (type == SYS_RES_IOPORT && *rid == PCIR_MAPS) 763 return (fm801->reg); 764 765 return (NULL); 766} 767 768static int 769fm801_release_resource(device_t bus, device_t child, int type, int rid, 770 struct resource *r) 771{ 772 return (0); 773} 774 775static device_method_t fm801_methods[] = { 776 /* Device interface */ 777 DEVMETHOD(device_probe, fm801_pci_probe), 778 DEVMETHOD(device_attach, fm801_pci_attach), 779 DEVMETHOD(device_detach, fm801_pci_detach), 780 DEVMETHOD(device_shutdown, bus_generic_shutdown), 781 DEVMETHOD(device_suspend, bus_generic_suspend), 782 DEVMETHOD(device_resume, bus_generic_resume), 783 784 /* Bus interface */ 785 DEVMETHOD(bus_print_child, bus_generic_print_child), 786 DEVMETHOD(bus_alloc_resource, fm801_alloc_resource), 787 DEVMETHOD(bus_release_resource, fm801_release_resource), 788 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 789 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 790 { 0, 0} 791}; 792 793static driver_t fm801_driver = { 794 "pcm", 795 fm801_methods, 796 PCM_SOFTC_SIZE, 797}; 798 799DRIVER_MODULE(snd_fm801, pci, fm801_driver, pcm_devclass, 0, 0); 800MODULE_DEPEND(snd_fm801, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER); 801MODULE_VERSION(snd_fm801, 1); 802