Subversion Repositories Kolibri OS

Rev

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