Subversion Repositories Kolibri OS

Rev

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