summaryrefslogtreecommitdiff
path: root/ecos/packages/io/pci/current/tests/pci1.c
blob: 70f1e37b57aee4d3a367b23f09236c6741f93f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//==========================================================================
//
//        pci1.c
//
//        Test PCI library API
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):     jskov
// Contributors:  jskov
// Date:          1999-03-17
// Description:   Simple test that:
//                  Checks API (compile time)
//                  Prints out information about found PCI devices.
//                  Allocates resources to devices (but does not enable
//                  them).
//####DESCRIPTIONEND####

#include <pkgconf/system.h>

#include <cyg/infra/diag.h>             // diag_printf
#include <cyg/infra/testcase.h>         // test macros
#include <cyg/infra/cyg_ass.h>          // assertion macros

// Package requirements
#if defined(CYGPKG_IO_PCI) && defined(CYGPKG_KERNEL) && defined(CYGPKG_ISOINFRA)

#include <pkgconf/kernel.h>
#include <pkgconf/io_pci.h>
#include <cyg/io/pci.h>
#include <cyg/hal/hal_arch.h>
#include <string.h>

// Package option requirements
#if defined(CYGFUN_KERNEL_API_C) && defined(CYG_PCI_PRESENT)

#include <cyg/kernel/kapi.h>

// If the target has limited memory resources, undef the below to
// avoid inclusion of the big PCI code tables.
//
// The header file is created from http://www.yourvote.com/pci
// maintained by Jim Boemler (jboemler@halcyon.com).
//
// If you have PCI devices not listed in this list, please consider
// registering the codes in the database.
#define USE_PCI_CODE_LIST

#ifdef USE_PCI_CODE_LIST
#include "pcihdr.h"
#endif

// You may want to use this code to do some simple testing of the
// devices on the PCI bus. By enabling the below definition, the
// devices will get PCI IO and MEM access activated after configuration
// so you can play with IO registers and display/set contents of MEM.
#define nENABLE_PCI_DEVICES

unsigned char stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
cyg_thread thread_data;
cyg_handle_t thread_handle;

void pci_scan( void );

cyg_bool
pci_api_test(int dummy)
{
    cyg_pci_device dev_info;
    cyg_pci_device_id devid = CYG_PCI_NULL_DEVID;
    cyg_bool ret;
    CYG_PCI_ADDRESS64 mem_base = 0;
    CYG_PCI_ADDRESS32 io_base = 0;
    CYG_ADDRWORD vec;
    cyg_uint8 var_uint8;
    cyg_uint16 var_uint16;
    cyg_uint32 var_uint32;


    // Always return...
    if (dummy)
        return true;

    CYG_TEST_FAIL_FINISH("Not reached");

    // PCI library API
    cyg_pci_init();

    cyg_pci_get_device_info (devid, &dev_info);
    cyg_pci_set_device_info (devid, &dev_info);

    ret = cyg_pci_find_device(0, 0, &devid);
    ret |= cyg_pci_find_class(0, &devid);
    ret |= cyg_pci_find_next(devid, &devid);

    ret |= cyg_pci_configure_device(&dev_info);

    cyg_pci_set_memory_base(mem_base);
    cyg_pci_set_io_base(io_base);

    ret |= cyg_pci_allocate_memory(&dev_info, 0, &mem_base);
    ret |= cyg_pci_allocate_io(&dev_info, 0, &io_base);

    ret |= cyg_pci_translate_interrupt(&dev_info, &vec);

    cyg_pci_read_config_uint8(devid, 0, &var_uint8);
    cyg_pci_read_config_uint16(devid, 0, &var_uint16);
    cyg_pci_read_config_uint32(devid, 0, &var_uint32);

    cyg_pci_write_config_uint8(devid, 0, var_uint8);
    cyg_pci_write_config_uint16(devid, 0, var_uint16);
    cyg_pci_write_config_uint32(devid, 0, var_uint32);

    return ret;
}

void
pci_test( void )
{
    cyg_pci_device dev_info;
    cyg_pci_device_id devid;
    CYG_ADDRWORD irq;
    int i;
#ifdef USE_PCI_CODE_LIST
    cyg_bool no_match = false;
    cyg_uint16 vendor, device;
    cyg_uint8  bc, sc, pi;
    PCI_VENTABLE* vtbl;
    PCI_DEVTABLE* dtbl;
    PCI_CLASSCODETABLE* ctbl;
#endif

    pci_api_test(1);

    cyg_pci_init();

    diag_printf( "========== Finding and configuring devices\n" );

    if (cyg_pci_find_next(CYG_PCI_NULL_DEVID, &devid)) {
        do {
            // Get device info
            cyg_pci_get_device_info(devid, &dev_info);


            // Print stuff
            diag_printf("Found device on bus %d, devfn 0x%02x:\n",
                        CYG_PCI_DEV_GET_BUS(devid),
                        CYG_PCI_DEV_GET_DEVFN(devid));

            if (dev_info.command & CYG_PCI_CFG_COMMAND_ACTIVE) {
                diag_printf(" Note that board is active. Probed"
                            " sizes and CPU addresses invalid!\n");
            }

            // Configure the device
            if (cyg_pci_configure_device(&dev_info)) {
                diag_printf(" Device configuration succeeded\n");
#ifdef ENABLE_PCI_DEVICES
                {
                    cyg_uint16 cmd;

                    // Don't use cyg_pci_set_device_info since it clears
                    // some of the fields we want to print out below.
                    cyg_pci_read_config_uint16(dev_info.devid,
                                               CYG_PCI_CFG_COMMAND, &cmd);
                    cmd |= CYG_PCI_CFG_COMMAND_IO|CYG_PCI_CFG_COMMAND_MEMORY;
                    cyg_pci_write_config_uint16(dev_info.devid,
                                                CYG_PCI_CFG_COMMAND, cmd);
                }
                diag_printf(" **** Device IO and MEM access enabled\n");
#endif
            } else {
                diag_printf(" Device configuration failed");
                if (dev_info.command & CYG_PCI_CFG_COMMAND_ACTIVE)
                    diag_printf(" - device already enabled\n");
                else
                    diag_printf(" - resource problem\n");
            }

            diag_printf(" Vendor    0x%04x", dev_info.vendor);
#ifdef USE_PCI_CODE_LIST
            vendor = dev_info.vendor;
            vtbl = PciVenTable;
            for (i = 0; i < PCI_VENTABLE_LEN; i++, vtbl++)
                if (vendor == vtbl->VenId) 
                    break;

            if (i < PCI_VENTABLE_LEN) {
                diag_printf(" [%s][%s]", vtbl->VenShort, vtbl->VenFull);
            } else {
                diag_printf(" [UNKNOWN]");
                no_match = true;
            }
#endif
            diag_printf("\n Device    0x%04x", dev_info.device);
#ifdef USE_PCI_CODE_LIST
            device = dev_info.device;
            dtbl = PciDevTable;
            for (i = 0; i < PCI_DEVTABLE_LEN; i++, dtbl++)
                if (vendor == dtbl->VenId && device == dtbl->DevId) 
                    break;

            if (i < PCI_DEVTABLE_LEN) {
                diag_printf(" [%s][%s]", dtbl->Chip, dtbl->ChipDesc);
            } else {
                diag_printf(" [UNKNOWN]");
                no_match = true;
            }
#endif

            diag_printf("\n Command   0x%04x, Status 0x%04x\n",
                        dev_info.command, dev_info.status);

            diag_printf(" Class/Rev 0x%08x", dev_info.class_rev);
#ifdef USE_PCI_CODE_LIST
            bc = (dev_info.class_rev >> 24) & 0xff;
            sc = (dev_info.class_rev >> 16) & 0xff;
            pi = (dev_info.class_rev >>  8) & 0xff;
            ctbl = PciClassCodeTable;
            for (i = 0; i < PCI_CLASSCODETABLE_LEN; i++, ctbl++)
                if (bc == ctbl->BaseClass 
                    && sc == ctbl->SubClass
                    && pi == ctbl->ProgIf)
                    break;

            if (i < PCI_CLASSCODETABLE_LEN) {
                diag_printf(" [%s][%s][%s]", ctbl->BaseDesc,
                            ctbl->SubDesc, ctbl->ProgDesc);
            } else {
                diag_printf(" [UNKNOWN]");
                no_match = true;
            }
#endif
            diag_printf("\n Header 0x%02x\n", dev_info.header_type);

            diag_printf(" SubVendor 0x%04x, Sub ID 0x%04x\n",
                        dev_info.header.normal.sub_vendor, 
                        dev_info.header.normal.sub_id);


            for(i = 0; i < CYG_PCI_MAX_BAR; i++) {
                diag_printf(" BAR[%d]    0x%08x /", i, dev_info.base_address[i]);
                diag_printf(" probed size 0x%08x / CPU addr 0x%08x\n",
                            dev_info.base_size[i], dev_info.base_map[i]);
            }
            
            if (cyg_pci_translate_interrupt(&dev_info, &irq))
                diag_printf(" Wired to HAL vector %d\n", irq);
            else
                diag_printf(" Does not generate interrupts.\n");

        } while (cyg_pci_find_next(devid, &devid));


#ifdef USE_PCI_CODE_LIST
        diag_printf("\nStrings in [] are from the PCI Code List at http://www.yourvote.com/pci\n");
        if (no_match) {
            diag_printf("It seems that some of the device information has not been registered in\n");
            diag_printf("the PCI Code List. Please consider improving the database by registering\n");
            diag_printf("the [UNKNOWN] information for your devices. Thanks.\n");
        }
#endif
    } else {
        diag_printf("No PCI devices found.");
    }

    pci_scan();

    CYG_TEST_PASS_FINISH("pci1 test OK");
}

void
pci_scan( void )
{
    cyg_pci_device dev_info;
    cyg_pci_device_id devid;
    CYG_ADDRWORD irq;
    int i;
#ifdef USE_PCI_CODE_LIST
    cyg_bool no_match = false;
    cyg_uint16 vendor, device;
    cyg_uint8  bc, sc, pi;
    PCI_VENTABLE* vtbl;
    PCI_DEVTABLE* dtbl;
    PCI_CLASSCODETABLE* ctbl;
#endif

    diag_printf( "========== Scanning initialized devices\n" );

    if (cyg_pci_find_next(CYG_PCI_NULL_DEVID, &devid)) {
        do {
            // Since we are NOT about to configure the device, set the
            // devinfo record so we don't mistake garbage for data.
            memset( &dev_info, 0xAAAAAAAAu, sizeof( dev_info ) );

            // Get device info
            cyg_pci_get_device_info(devid, &dev_info);

            // Print stuff
            diag_printf("Found device on bus %d, devfn 0x%02x:\n",
                        CYG_PCI_DEV_GET_BUS(devid),
                        CYG_PCI_DEV_GET_DEVFN(devid));

            if (dev_info.command & CYG_PCI_CFG_COMMAND_ACTIVE) {
                diag_printf(" Note that board is active. Probed"
                            " sizes and CPU addresses invalid!\n");
            }

            diag_printf(" Vendor    0x%04x", dev_info.vendor);
#ifdef USE_PCI_CODE_LIST
            vendor = dev_info.vendor;
            vtbl = PciVenTable;
            for (i = 0; i < PCI_VENTABLE_LEN; i++, vtbl++)
                if (vendor == vtbl->VenId) 
                    break;

            if (i < PCI_VENTABLE_LEN) {
                diag_printf(" [%s][%s]", vtbl->VenShort, vtbl->VenFull);
            } else {
                diag_printf(" [UNKNOWN]");
                no_match = true;
            }
#endif
            diag_printf("\n Device    0x%04x", dev_info.device);
#ifdef USE_PCI_CODE_LIST
            device = dev_info.device;
            dtbl = PciDevTable;
            for (i = 0; i < PCI_DEVTABLE_LEN; i++, dtbl++)
                if (vendor == dtbl->VenId && device == dtbl->DevId) 
                    break;

            if (i < PCI_DEVTABLE_LEN) {
                diag_printf(" [%s][%s]", dtbl->Chip, dtbl->ChipDesc);
            } else {
                diag_printf(" [UNKNOWN]");
                no_match = true;
            }
#endif

            diag_printf("\n Command   0x%04x, Status 0x%04x\n",
                        dev_info.command, dev_info.status);

            diag_printf(" Class/Rev 0x%08x", dev_info.class_rev);
#ifdef USE_PCI_CODE_LIST
            bc = (dev_info.class_rev >> 24) & 0xff;
            sc = (dev_info.class_rev >> 16) & 0xff;
            pi = (dev_info.class_rev >>  8) & 0xff;
            ctbl = PciClassCodeTable;
            for (i = 0; i < PCI_CLASSCODETABLE_LEN; i++, ctbl++)
                if (bc == ctbl->BaseClass 
                    && sc == ctbl->SubClass
                    && pi == ctbl->ProgIf)
                    break;

            if (i < PCI_CLASSCODETABLE_LEN) {
                diag_printf(" [%s][%s][%s]", ctbl->BaseDesc,
                            ctbl->SubDesc, ctbl->ProgDesc);
            } else {
                diag_printf(" [UNKNOWN]");
                no_match = true;
            }
#endif
            diag_printf("\n Header 0x%02x\n", dev_info.header_type);

            diag_printf(" SubVendor 0x%04x, Sub ID 0x%04x\n",
                        dev_info.header.normal.sub_vendor, 
                        dev_info.header.normal.sub_id);


            for(i = 0; i < CYG_PCI_MAX_BAR; i++) {
                diag_printf(" BAR[%d]    0x%08x /", i, dev_info.base_address[i]);
                diag_printf(" probed size 0x%08x / CPU addr 0x%08x\n",
                            dev_info.base_size[i], dev_info.base_map[i]);
            }
            
            if (cyg_pci_translate_interrupt(&dev_info, &irq))
                diag_printf(" Wired to HAL vector %d\n", irq);
            else
                diag_printf(" Does not generate interrupts.\n");

        } while (cyg_pci_find_next(devid, &devid));
    } else {
        diag_printf("No PCI devices found.");
    }
}

void
cyg_start(void)
{
    CYG_TEST_INIT();
    cyg_thread_create(10,                   // Priority - just a number
                      (cyg_thread_entry_t*)pci_test,         // entry
                      0,                    // 
                      "pci_thread",     // Name
                      &stack[0],            // Stack
                      CYGNUM_HAL_STACK_SIZE_TYPICAL,           // Size
                      &thread_handle,       // Handle
                      &thread_data          // Thread data structure
        );
    cyg_thread_resume(thread_handle);
    cyg_scheduler_start();
}

#else // CYGFUN_KERNEL_API_C && CYG_PCI_PRESENT
#define N_A_MSG "Needs kernel C API & PCI platform support"
#endif

#else // CYGPKG_IO_PCI && CYGPKG_KERNEL
#define N_A_MSG "Needs IO/PCI, ISOINFRA, and Kernel"
#endif

#ifdef N_A_MSG
void
cyg_start( void )
{
    CYG_TEST_INIT();
    CYG_TEST_NA( N_A_MSG);
}
#endif // N_A_MSG

// EOF pci1.c