Rev 981 | Rev 1029 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
981 | serge | 1 | |
2 | |||
3 | #include "types.h" |
||
4 | |||
5 | #include "pci.h" |
||
6 | |||
7 | |||
8 | #include "geode.h" |
||
9 | |||
10 | #ifdef DEBUG |
||
11 | |||
12 | #else |
||
13 | #define DBG(format,...) |
||
14 | #endif |
||
15 | |||
16 | #define BM0_IRQ 0x04 |
||
17 | |||
18 | #define BITS_8_TO_16(x) ( ( (long) ((unsigned char) x - 128) ) << 8 ) |
||
19 | |||
20 | #define PCI_VENDOR_ID_NS 0x100b |
||
21 | |||
22 | |||
23 | |||
24 | |||
25 | |||
26 | #endif |
||
27 | #ifndef PCI_DEVICE_ID_AMD_CS5536_AUDIO |
||
28 | #define PCI_DEVICE_ID_AMD_CS5536_AUDIO 0x2093 |
||
29 | #endif |
||
30 | |||
31 | #define ID_DEV_1 ((PCI_DEVICE_ID_NS_CS5535_AUDIO << 16)|PCI_VENDOR_ID_NS) |
||
32 | |||
33 | |||
34 | #define SET 1 |
||
35 | |||
36 | |||
37 | int __stdcall srv_sound(ioctl_t *io); |
||
38 | |||
39 | |||
40 | |||
41 | |||
42 | |||
43 | |||
44 | |||
45 | PCITAG pciTag; |
||
46 | Bool is_iomapped; |
||
47 | addr_t F3BAR0; |
||
48 | |||
49 | Bool fAD1819A; |
||
50 | |||
51 | int CurrentPowerState; |
||
52 | |||
53 | addr_t buffer; |
||
54 | |||
55 | |||
56 | addr_t irq_line; |
||
57 | |||
58 | |||
59 | void __stdcall (*callback)(addr_t buffer); |
||
60 | |||
61 | |||
62 | geode_t geode; |
||
63 | |||
64 | |||
65 | |||
66 | |||
67 | reg+= geode.F3BAR0; |
||
68 | |||
69 | #ifdef FORCED_PIO |
||
70 | |||
71 | #else |
||
72 | if(geode.is_iomapped) |
||
73 | out32((u16_t)reg, data); |
||
74 | else |
||
75 | *(u32_t*)reg = data; |
||
76 | #endif |
||
77 | } |
||
78 | |||
79 | static inline u32_t ctrl_read_32(addr_t reg) |
||
80 | |||
81 | reg+= geode.F3BAR0; |
||
82 | #ifdef FORCED_PIO |
||
83 | return in32((u16_t)reg); |
||
84 | #else |
||
85 | if(geode.is_iomapped) |
||
86 | return in32((u16_t)reg); |
||
87 | else |
||
88 | return *(u32_t*)reg; |
||
89 | #endif |
||
90 | } |
||
91 | |||
92 | |||
93 | |||
94 | |||
95 | |||
96 | count_t timeout, |
||
97 | u32_t *pReturnValue) |
||
98 | { |
||
99 | volatile u32_t tmp; |
||
100 | |||
101 | tmp = ctrl_read_32(offset); |
||
102 | |||
103 | while (timeout) |
||
104 | |||
105 | if (Operation==CLEAR){ |
||
106 | if (!(tmp & Bit)) |
||
107 | break; |
||
108 | } else if (tmp & Bit) |
||
109 | break; |
||
110 | |||
111 | /*If the Bit is not clear yet, we wait for 10 milisecond and try again*/ |
||
112 | |||
113 | |||
114 | tmp = ctrl_read_32(offset); |
||
115 | |||
116 | timeout--; |
||
117 | |||
118 | |||
119 | if (pReturnValue) |
||
120 | |||
121 | |||
122 | if (!timeout) |
||
123 | |||
124 | |||
125 | return TRUE; |
||
126 | |||
127 | |||
128 | u16_t snd_hw_CodecRead ( u8_t CodecRegister ) |
||
129 | |||
130 | u32_t CodecRegister_data = 0; |
||
131 | u32_t timeout=10; |
||
132 | volatile u32_t val=0; |
||
133 | |||
134 | CodecRegister_data = ((u32_t)CodecRegister)<<24; |
||
135 | |||
136 | |||
137 | /* Set the bit. We are going to access the CODEC...*/ |
||
138 | |||
139 | |||
140 | /*Request the data*/ |
||
141 | |||
142 | |||
143 | /* Now we need to wait for BIT_5535_CODEC_COMMAND_NEW of the Codec control register to clear |
||
144 | |||
145 | if (!snd_hw_WaitForBit (CODEC_CONTROL_REG_5535, |
||
146 | BIT_5535_CODEC_COMMAND_NEW, CLEAR, 50, NULL)) |
||
147 | DBG("BIT_5535_CODEC_COMMAND_NEW did not clear!!\n"); |
||
148 | |||
149 | /* Wait for CODEC_STATUS_NEW and confirm the read of the requested register*/ |
||
150 | |||
151 | do |
||
152 | { |
||
153 | val = ctrl_read_32(CODEC_STATUS_REG_5535); |
||
154 | if ((val & BIT_5535_CODEC_STATUS_NEW) && |
||
155 | ((u32_t) CodecRegister == ((0xFF000000 & val)>>24))) |
||
156 | break; |
||
157 | else |
||
158 | /*Wait for 10 miliseconds and try again*/ |
||
159 | delay(10/10); |
||
160 | } while ( --timeout); |
||
161 | |||
162 | if (!timeout) |
||
163 | |||
164 | |||
165 | return( (u16_t)val ); |
||
166 | |||
167 | |||
168 | void snd_hw_CodecWrite( u8_t CodecRegister, u16_t CodecData ) |
||
169 | |||
170 | u32_t CodecRegister_data; |
||
171 | u32_t Temp, timeout; |
||
172 | |||
173 | CodecRegister_data = ((u32_t) CodecRegister)<<24; |
||
174 | |||
175 | CodecRegister_data &= CODEC_COMMAND_MASK; |
||
176 | |||
177 | /*Set the bit. We are going to access the CODEC...*/ |
||
178 | |||
179 | |||
180 | /*Write the data*/ |
||
181 | |||
182 | //OS_DbgMsg("Writing: %08X\n", CodecRegister_data); |
||
183 | |||
184 | /*We need to wait for bit16 of the Codec control register to clear*/ |
||
185 | |||
186 | |||
187 | timeout = 50; |
||
188 | |||
189 | while ((Temp & BIT_5535_CODEC_COMMAND_NEW) && timeout-- ) |
||
190 | |||
191 | |||
192 | if (!timeout) |
||
193 | |||
194 | "BIT_5535_CODEC_COMMAND_NEW did not clear!\n"); |
||
195 | } |
||
196 | |||
197 | |||
198 | |||
199 | |||
200 | u16_t val; |
||
201 | |||
202 | DBG("Rate: %d\n", SampleRate); |
||
203 | |||
204 | /*If Double-Rate is supported (Bit 2 on register 28h)...*/ |
||
205 | |||
206 | |||
207 | if (val & 0x02) |
||
208 | |||
209 | DBG("Codec supports Double rate.\n"); |
||
210 | val=snd_hw_CodecRead(EXT_AUDIO_CTRL_STAT); |
||
211 | |||
212 | if (SampleRate>48000) |
||
213 | |||
214 | snd_hw_CodecWrite(EXT_AUDIO_CTRL_STAT, (u16_t) (val|0x0002)); |
||
215 | SampleRate/=2; |
||
216 | } |
||
217 | else |
||
218 | snd_hw_CodecWrite(EXT_AUDIO_CTRL_STAT, (u16_t) (val&0xFFFD)); |
||
219 | } |
||
220 | if (geode.fAD1819A) |
||
221 | { |
||
222 | DBG("AD1819...\n"); |
||
223 | snd_hw_CodecWrite(AD1819A_PCM_SR0,(u16_t)SampleRate); |
||
224 | } |
||
225 | else |
||
226 | snd_hw_CodecWrite(PCM_FRONT_DAC_RATE,(u16_t)SampleRate); |
||
227 | } |
||
228 | |||
229 | Bool init_device() |
||
230 | |||
231 | u32_t io_base = pciReadLong(geode.pciTag, 0x10); |
||
232 | |||
233 | if( PCI_MAP_IS_IO(io_base)) |
||
234 | |||
235 | geode.is_iomapped = TRUE; |
||
236 | geode.F3BAR0 = PCIGETIO(io_base); |
||
237 | DBG("io mapped F3BAR0 %x\n", geode.F3BAR0); |
||
238 | } |
||
239 | else if(PCI_MAP_IS_MEM(io_base)) |
||
240 | { |
||
241 | geode.is_iomapped = FALSE; |
||
242 | io_base = PCIGETMEMORY(io_base); |
||
243 | geode.F3BAR0 = MapIoMem(io_base, 128, PG_SW+PG_NOCACHE); |
||
244 | DBG("memory mapped F3BAR0 %x\n", geode.F3BAR0); |
||
245 | } |
||
246 | |||
247 | geode.buffer = KernelAlloc(64*1024); |
||
248 | |||
249 | addr_t buffer = geode.buffer; |
||
250 | |||
251 | |||
252 | geode.prd_dma = (((addr_t)prd_tab) & 4095) + GetPgAddr((void*)prd_tab); |
||
253 | |||
254 | prd_tab[0].ulPhysAddr = dma; |
||
255 | |||
256 | |||
257 | prd_tab[1].ulPhysAddr = dma + 16384; |
||
258 | |||
259 | |||
260 | prd_tab[2].ulPhysAddr = dma + 16384*2; |
||
261 | |||
262 | |||
263 | prd_tab[3].ulPhysAddr = dma + 16384*3; |
||
264 | |||
265 | |||
266 | prd_tab[4].ulPhysAddr = geode.prd_dma; |
||
267 | |||
268 | |||
269 | ctrl_write_32(0x24, geode.prd_dma); |
||
270 | |||
271 | __clear((void*)buffer,64*1024); |
||
272 | |||
273 | |||
274 | // dbgprintf("Create primary buffer at %x dma at %x\n", geode.buffer, dma ); |
||
275 | |||
276 | // dbgprintf("Set prd dma %x, read prd dma %x\n", geode.prd_dma, tmp); |
||
277 | |||
278 | geode.irq_line = pciReadLong(geode.pciTag, 0x3C) & 0xFF; |
||
279 | |||
280 | |||
281 | DBG("Irq line %d, mask %x\n", geode.irq_line, geode.irq_mask); |
||
282 | |||
283 | |||
284 | |||
285 | |||
286 | geode.PRDTableAddress = geode.F3BAR0+0x24; |
||
287 | geode.DMAPointer = geode.F3BAR0+0x60; |
||
288 | |||
289 | geode.IRQControlRegister = geode.F3BAR0+0x1C; |
||
290 | |||
291 | geode.SMI_StatusRegister = geode.F3BAR0+0x21; |
||
292 | */ |
||
293 | |||
294 | |||
295 | |||
296 | |||
297 | |||
298 | |||
299 | DBG("reset codec...\n"); |
||
300 | |||
301 | ctrl_write_32(CODEC_CONTROL_REG_5535, 0x00030000 ); |
||
302 | |||
303 | if (!snd_hw_WaitForBit (CODEC_STATUS_REG_5535, BIT_CODEC_READY, SET, 40, NULL)) |
||
304 | |||
305 | DBG("Primary Codec NOT Ready...Aborting\n"); |
||
306 | return FALSE; |
||
307 | } |
||
308 | |||
309 | /*Check which codec is being used */ |
||
310 | |||
311 | snd_hw_CodecRead(AD1819A_VENDORID2) == 0x5303) |
||
312 | { |
||
313 | geode.fAD1819A = TRUE; |
||
314 | /* Enable non-48kHz sample rates. */ |
||
315 | snd_hw_CodecWrite (AD1819A_SER_CONF, |
||
316 | snd_hw_CodecRead(AD1819A_SER_CONF>>8) | |
||
317 | AD1819A_SER_CONF_DRQEN); |
||
318 | DBG("detect AD1819A audio codec\n"); |
||
319 | } |
||
320 | else |
||
321 | { |
||
322 | geode.fAD1819A = FALSE; |
||
323 | snd_hw_CodecWrite (EXT_AUDIO_CTRL_STAT, |
||
324 | (snd_hw_CodecRead(EXT_AUDIO_CTRL_STAT) | 0x0001)); |
||
325 | /* set the VRA bit to ON*/ |
||
326 | } |
||
327 | |||
328 | /* set default volume*/ |
||
329 | |||
330 | snd_hw_CodecWrite( PCM_OUT_VOL, 0x0808); |
||
331 | snd_hw_CodecWrite( PC_BEEP_VOLUME, 0x0000); |
||
332 | snd_hw_CodecWrite( PHONE_VOLUME, 0x8000); |
||
333 | snd_hw_CodecWrite( MIC_VOLUME, 0x8048); |
||
334 | snd_hw_CodecWrite( LINE_IN_VOLUME, 0x0808); |
||
335 | snd_hw_CodecWrite( CD_VOLUME, 0x8000); |
||
336 | snd_hw_CodecWrite( VIDEO_VOLUME, 0x8000); |
||
337 | snd_hw_CodecWrite( TV_VOLUME, 0x8000); |
||
338 | snd_hw_CodecWrite( RECORD_SELECT, 0x0000); |
||
339 | snd_hw_CodecWrite( RECORD_GAIN, 0x0a0a); |
||
340 | snd_hw_CodecWrite( GENERAL_PURPOSE, 0x0200); |
||
341 | snd_hw_CodecWrite( MASTER_VOLUME_MONO, 0x0000); |
||
342 | |||
343 | snd_hw_SetCodecRate(48000); |
||
344 | |||
345 | snd_hw_CodecWrite (POWERDOWN_CTRL_STAT, 0x0000); |
||
346 | geode.CurrentPowerState = GEODEAUDIO_D0; |
||
347 | // OS_DbgMsg("<--snd_hw_InitAudioRegs\n"); |
||
348 | |||
349 | |||
350 | |||
351 | |||
352 | |||
353 | static int snd_StartDMA () |
||
354 | |||
355 | |||
356 | #ifdef FORCED_PIO |
||
357 | |||
358 | #else |
||
359 | if (geode.is_iomapped) |
||
360 | out8( (u16_t)(geode.F3BAR0+0x20),PCI_READS | ENABLE_BUSMASTER); |
||
361 | else |
||
362 | *(u8_t*)(geode.F3BAR0+0x20)= PCI_READS | ENABLE_BUSMASTER; |
||
363 | #endif |
||
364 | return 0; |
||
365 | }; |
||
366 | |||
367 | static u8_t snd_hw_InterruptID () |
||
368 | |||
369 | volatile u8_t *TempInterruptID, ID; |
||
370 | |||
371 | #ifdef FORCED_PIO |
||
372 | |||
373 | #else |
||
374 | if (geode.is_iomapped) |
||
375 | ID=(u8_t) in16((u16_t)(geode.F3BAR0 + 0x12)); |
||
376 | else |
||
377 | { |
||
378 | TempInterruptID=(u8_t*)(geode.F3BAR0 + 0x12); |
||
379 | ID=*TempInterruptID; |
||
380 | } |
||
381 | #endif |
||
382 | return (ID); |
||
383 | } |
||
384 | |||
385 | |||
386 | |||
387 | |||
388 | volatile u8_t status; /*Volatile to force read-to-clear.*/ |
||
389 | |||
390 | /*Read to clear*/ |
||
391 | |||
392 | #ifdef FORCED_PIO |
||
393 | |||
394 | #else |
||
395 | if (geode.is_iomapped) |
||
396 | status = in8((u16_t) geode.F3BAR0 + 0x21); |
||
397 | else |
||
398 | status = *((u8_t*)geode.F3BAR0 + 0x21); |
||
399 | #endif |
||
400 | return status; |
||
401 | } |
||
402 | |||
403 | |||
404 | |||
405 | |||
406 | u8_t IntID; |
||
407 | |||
408 | IntID = snd_hw_InterruptID(); |
||
409 | |||
410 | // dbgprintf("IRQ id %x\n", IntID); |
||
411 | |||
412 | snd_hw_ClearStat(CHANNEL0_PLAYBACK); |
||
413 | |||
414 | |||
415 | if(IntID & BM0_IRQ) |
||
416 | |||
417 | addr_t prd, offset, base; |
||
418 | |||
419 | prd = ctrl_read_32(0x24); |
||
420 | |||
421 | |||
422 | base = geode.buffer + 16384*offset; |
||
423 | |||
424 | geode.callback(base); |
||
425 | |||
426 | |||
427 | // dbgprintf(">>BM0_IRQ prd %x offset %x base %x\n", prd, offset, base); |
||
428 | |||
429 | }; |
||
430 | |||
431 | Bool FindPciDevice() |
||
432 | |||
433 | u32_t bus, last_bus; |
||
434 | PCITAG tag; |
||
435 | |||
436 | if( (last_bus = PciApi(1))==-1) |
||
437 | |||
438 | |||
439 | for(bus=0;bus<=last_bus;bus++) |
||
440 | |||
441 | u32_t devfn; |
||
442 | |||
443 | for(devfn=0;devfn<256;devfn++) |
||
444 | |||
445 | u32_t pciId=0; |
||
446 | |||
447 | pciId = PciRead32(bus,devfn, 0); |
||
448 | |||
449 | if( (pciId == ID_DEV_1) || |
||
450 | |||
451 | { |
||
452 | DBG("detect companion audio device %x\n", pciId); |
||
453 | geode.pciTag = pciTag(bus,(devfn>>3)&0x1F,devfn&0x7); |
||
454 | return TRUE; |
||
455 | }; |
||
456 | }; |
||
457 | }; |
||
458 | return FALSE; |
||
459 | }; |
||
460 | |||
461 | |||
462 | |||
463 | |||
464 | u32_t retval; |
||
465 | |||
466 | int i; |
||
467 | |||
468 | if(action != 1) |
||
469 | |||
470 | |||
471 | #ifdef DEBUG |
||
472 | |||
473 | { |
||
474 | printf("Can't open /rd/1/drivers/geode.log\nExit\n"); |
||
475 | return 0; |
||
476 | } |
||
477 | #endif |
||
478 | |||
479 | if( FindPciDevice() == FALSE) |
||
480 | |||
481 | DBG("Device not found\n"); |
||
482 | return 0; |
||
483 | }; |
||
484 | |||
485 | init_device(); |
||
486 | |||
487 | retval = RegService("SOUND", srv_sound); |
||
488 | |||
489 | AttachIntHandler(geode.irq_line, snd_interrupt, 0); |
||
490 | |||
491 | DBG("reg service %s as: %x\n", "SOUND", retval); |
||
492 | |||
493 | return retval; |
||
494 | |||
495 | |||
496 | |||
497 | |||
498 | |||
499 | #define SRV_GETVERSION 0 |
||
500 | |||
501 | #define DEV_STOP 2 |
||
502 | #define DEV_CALLBACK 3 |
||
503 | #define DEV_SET_BUFF 4 |
||
504 | #define DEV_NOTIFY 5 |
||
505 | #define DEV_SET_MASTERVOL 6 |
||
506 | #define DEV_GET_MASTERVOL 7 |
||
507 | #define DEV_GET_INFO 8 |
||
508 | |||
509 | |||
510 | |||
511 | |||
512 | u32_t *inp; |
||
513 | u32_t *outp; |
||
514 | |||
515 | inp = io->input; |
||
516 | |||
517 | |||
518 | switch(io->io_code) |
||
519 | |||
520 | case SRV_GETVERSION: |
||
521 | if(io->out_size==4) |
||
522 | { |
||
523 | *outp = API_VERSION; |
||
524 | return 0; |
||
525 | } |
||
526 | break; |
||
527 | |||
528 | case DEV_PLAY: |
||
529 | |||
530 | break; |
||
531 | |||
532 | case DEV_STOP: |
||
533 | |||
534 | |||
535 | case DEV_CALLBACK: |
||
536 | |||
537 | { |
||
538 | geode.callback = (void*)(*inp); |
||
539 | return 0; |
||
540 | } |
||
541 | break; |
||
542 | |||
543 | default: |
||
544 | |||
545 | }; |
||
546 | return ERR_PARAM; |
||
547 | }256;devfn++) |
||
548 | >=last_bus;bus++) |
||
549 | >--snd_hw_InitAudioRegs\n"); |
||
550 | |||
551 |