OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [arm/] [sa11x0/] [var/] [v2_0/] [src/] [sa11x0_misc.c] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      sa11x0_misc.c
4
//
5
//      HAL misc board support code for StrongARM SA11x0
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):    gthomas
44
// Contributors: hmt
45
//               Travis C. Furrer <furrer@mit.edu>
46
// Date:         2000-05-08
47
// Purpose:      HAL board support
48
// Description:  Implementations of HAL board interfaces
49
//
50
//####DESCRIPTIONEND####
51
//
52
//========================================================================*/
53
 
54
#include <pkgconf/hal.h>
55
#include <pkgconf/system.h>
56
#include CYGBLD_HAL_PLATFORM_H
57
 
58
#include <cyg/infra/cyg_type.h>         // base types
59
#include <cyg/infra/cyg_trac.h>         // tracing macros
60
#include <cyg/infra/cyg_ass.h>          // assertion macros
61
 
62
#include <cyg/hal/hal_misc.h>           // Size constants
63
#include <cyg/hal/hal_io.h>             // IO macros
64
#include <cyg/hal/hal_arch.h>           // Register state info
65
#include <cyg/hal/hal_diag.h>
66
#include <cyg/hal/hal_intr.h>           // Interrupt names
67
#include <cyg/hal/hal_cache.h>          // Cache control
68
#include <cyg/hal/hal_sa11x0.h>         // Hardware definitions
69
#include <cyg/hal/hal_mm.h>             // MMap table definitions
70
 
71
#include <cyg/infra/diag.h>             // diag_printf
72
 
73
// Most initialization has already been done before we get here.
74
// All we do here is set up the interrupt environment.
75
// FIXME: some of the stuff in hal_platform_setup could be moved here.
76
 
77
externC void plf_hardware_init(void);
78
 
79
void hal_hardware_init(void)
80
{
81
    // Mask all interrupts
82
    *SA11X0_ICMR = 0;
83
 
84
    // Make all interrupts do IRQ and not FIQ
85
    // FIXME: Change this if you use FIQs.
86
    *SA11X0_ICLR = 0;
87
 
88
    // Prevent masked interrupts from bringing us out of idle mode
89
    *SA11X0_ICCR = 1;
90
 
91
    // Disable all GPIO interrupt sources
92
    *SA11X0_GPIO_RISING_EDGE_DETECT = 0;
93
    *SA11X0_GPIO_FALLING_EDGE_DETECT = 0;
94
    *SA11X0_GPIO_EDGE_DETECT_STATUS = 0x0FFFFFFF;
95
 
96
    // Perform any platform specific initializations
97
    plf_hardware_init();
98
 
99
    // Let the "OS" counter run
100
    *SA11X0_OSCR = 0;
101
    *SA11X0_OSMR0 = 0;
102
 
103
    // Set up eCos/ROM interfaces
104
    hal_if_init();
105
 
106
    // Enable caches
107
    HAL_DCACHE_ENABLE();
108
    HAL_ICACHE_ENABLE();
109
}
110
 
111
// -------------------------------------------------------------------------
112
static cyg_uint32  clock_period;
113
 
114
void hal_clock_initialize(cyg_uint32 period)
115
{
116
    // Load match value
117
    *SA11X0_OSMR0 = period;
118
    clock_period = period;
119
 
120
    // Start the counter
121
    *SA11X0_OSCR = 0;
122
 
123
    // Clear any pending interrupt
124
    *SA11X0_OSSR = SA11X0_OSSR_TIMER0;
125
 
126
    // Enable timer 0 interrupt    
127
    *SA11X0_OIER |= SA11X0_OIER_TIMER0;
128
 
129
    // Unmask timer 0 interrupt
130
    HAL_INTERRUPT_UNMASK( CYGNUM_HAL_INTERRUPT_TIMER0 );
131
 
132
    // That's all.
133
}
134
 
135
// This routine is called during a clock interrupt.
136
 
137
// Define this if you want to ensure that the clock is perfect (i.e. does
138
// not drift).  One reason to leave it turned off is that it costs some
139
// us per system clock interrupt for this maintenance.
140
#undef COMPENSATE_FOR_CLOCK_DRIFT
141
 
142
void hal_clock_reset(cyg_uint32 vector, cyg_uint32 period)
143
{
144
#ifdef COMPENSATE_FOR_CLOCK_DRIFT
145
    cyg_uint32 next = *SA11X0_OSMR0 + period;    // Next interrupt time
146
    *SA11X0_OSSR = SA11X0_OSSR_TIMER0;           // Clear any pending interrupt
147
    *SA11X0_OSMR0 = next;                        // Load new match value
148
    {
149
        cyg_uint32 ctr = *SA11X0_OSCR;
150
        clock_period = next - ctr;
151
        if ((clock_period - 1) >= period) {      // Adjust for missed interrupts
152
            *SA11X0_OSMR0 = ctr + period;
153
            *SA11X0_OSSR = SA11X0_OSSR_TIMER0;   // Clear pending interrupt
154
            clock_period = period;
155
        }
156
    }
157
#else
158
    *SA11X0_OSMR0 = *SA11X0_OSCR + period;       // Load new match value
159
    *SA11X0_OSSR = SA11X0_OSSR_TIMER0;           // Clear any pending interrupt
160
#endif
161
}
162
 
163
// Read the current value of the clock, returning the number of hardware
164
// "ticks" that have occurred (i.e. how far away the current value is from
165
// the start)
166
 
167
// Note: The "contract" for this function is that the value is the number
168
// of hardware clocks that have happened since the last interrupt (i.e.
169
// when it was reset).  This value is used to measure interrupt latencies.
170
// However, since the hardware counter runs freely, this routine computes
171
// the difference between the current clock period and the number of hardware
172
// ticks left before the next timer interrupt.
173
void hal_clock_read(cyg_uint32 *pvalue)
174
{
175
    int orig;
176
    HAL_DISABLE_INTERRUPTS(orig);
177
    *pvalue = clock_period + *SA11X0_OSCR - *SA11X0_OSMR0;
178
    HAL_RESTORE_INTERRUPTS(orig);
179
}
180
 
181
// This is to cope with the test read used by tm_basic with
182
// CYGVAR_KERNEL_COUNTERS_CLOCK_LATENCY defined; we read the count ASAP
183
// in the ISR, *before* resetting the clock.  Which returns 1tick +
184
// latency if we just use plain hal_clock_read().
185
void hal_clock_latency(cyg_uint32 *pvalue)
186
{
187
    int orig;
188
    HAL_DISABLE_INTERRUPTS(orig);
189
    *pvalue = *SA11X0_OSCR - *SA11X0_OSMR0;
190
    HAL_RESTORE_INTERRUPTS(orig);
191
}
192
 
193
//
194
// Delay for some number of micro-seconds
195
//
196
void hal_delay_us(cyg_int32 usecs)
197
{
198
    cyg_uint32 val = 0;
199
    cyg_uint32 ctr = *SA11X0_OSCR;
200
    while (usecs-- > 0) {
201
        do {
202
            if (ctr != *SA11X0_OSCR) {
203
                val += 271267;          // 271267ps (3.6865Mhz -> 271.267ns)
204
                ++ctr;
205
            }
206
        } while (val < 1000000);
207
        val -= 1000000;
208
    }
209
}
210
 
211
// -------------------------------------------------------------------------
212
 
213
// This routine is called to respond to a hardware interrupt (IRQ).  It
214
// should interrogate the hardware and return the IRQ vector number.
215
int hal_IRQ_handler(void)
216
{
217
    cyg_uint32 sources, index;
218
 
219
#ifdef HAL_EXTENDED_IRQ_HANDLER
220
    // Use platform specific IRQ handler, if defined
221
    // Note: this macro should do a 'return' with the appropriate
222
    // interrupt number if such an extended interrupt exists.  The
223
    // assumption is that the line after the macro starts 'normal' processing.
224
    HAL_EXTENDED_IRQ_HANDLER(index);
225
#endif
226
 
227
#if 0 // test FIQ and print alert if active - really for debugging
228
    sources = *SA11X0_ICFP;
229
    if ( 0 != sources )
230
        diag_printf( "FIQ source active!!! - fiqstatus %08x irqstatus %08x\n",
231
                     sources, *SA11X0_ICIP );
232
    else
233
#endif // Scan FIQ sources also
234
 
235
    sources = *SA11X0_ICIP;
236
 
237
// FIXME
238
// if we come to support FIQ properly...
239
//    if ( 0 == sources )
240
//        sources = *SA11X0_ICFP;
241
 
242
    // Nothing wrong with scanning them in any order we choose...
243
    // So here we try to make the serial devices steal fewer cycles.
244
    // So, knowing this is an ARM:
245
    if ( sources & 0xff0000 )
246
        index = 16;
247
    else if ( sources & 0xff00 )
248
        index = 8;
249
    else if ( sources & 0xff )
250
        index = 0;
251
    else // if ( sources & 0xff000000 )
252
        index = 24;
253
 
254
    do {
255
        if ( (1 << index) & sources ) {
256
            if (index == CYGNUM_HAL_INTERRUPT_GPIO) {
257
                // Special case of GPIO cascade.  Search for lowest set bit
258
                sources = *SA11X0_GPIO_EDGE_DETECT_STATUS & 0x0FFFF800;
259
                index = 11;
260
                do {
261
                    if (sources & (1 << index)) {
262
                        index += 32;
263
                        break;
264
                    }
265
                    index++;
266
                } while (index < 28);
267
            }
268
            return index;
269
        }
270
      index++;
271
    } while ( index & 7 );
272
 
273
    return CYGNUM_HAL_INTERRUPT_NONE; // This shouldn't happen!
274
}
275
 
276
//
277
// Interrupt control
278
//
279
 
280
void hal_interrupt_mask(int vector)
281
{
282
 
283
#ifdef HAL_EXTENDED_INTERRUPT_MASK
284
    // Use platform specific handling, if defined
285
    // Note: this macro should do a 'return' for "extended" values of 'vector'
286
    // Normal vectors are handled by code subsequent to the macro call.
287
    HAL_EXTENDED_INTERRUPT_MASK(vector);
288
#endif
289
 
290
    // Non-GPIO interrupt sources can be masked separately.
291
    // Note: masking any non-unique GPIO signal (31..11) results in
292
    // all GPIO signals (31..11) being masked as only the "lump"
293
    // source will be changed.
294
 
295
    if (vector >= CYGNUM_HAL_INTERRUPT_GPIO11) {
296
        vector = CYGNUM_HAL_INTERRUPT_GPIO;
297
    }
298
    *SA11X0_ICMR &= ~(1 << vector);
299
}
300
 
301
void hal_interrupt_unmask(int vector)
302
{
303
 
304
#ifdef HAL_EXTENDED_INTERRUPT_UNMASK
305
    // Use platform specific handling, if defined
306
    // Note: this macro should do a 'return' for "extended" values of 'vector'
307
    // Normal vectors are handled by code subsequent to the macro call.
308
    HAL_EXTENDED_INTERRUPT_UNMASK(vector);
309
#endif
310
 
311
    if (vector >= CYGNUM_HAL_INTERRUPT_GPIO11) {
312
        vector = CYGNUM_HAL_INTERRUPT_GPIO;
313
    }
314
    *SA11X0_ICMR |= (1 << vector);
315
}
316
 
317
void hal_interrupt_acknowledge(int vector)
318
{
319
 
320
#ifdef HAL_EXTENDED_INTERRUPT_UNMASK
321
    // Use platform specific handling, if defined
322
    // Note: this macro should do a 'return' for "extended" values of 'vector'
323
    // Normal vectors are handled by code subsequent to the macro call.
324
    HAL_EXTENDED_INTERRUPT_ACKNOWLEDGE(vector);
325
#endif
326
 
327
    // GPIO interrupts are driven by an edge detection mechanism.  This
328
    // is latching so these interrupts must be acknowledged directly.
329
    // All other interrupts simply go away when the interrupting unit
330
    // has been serviced by the ISR.
331
    if ((vector < CYGNUM_HAL_INTERRUPT_GPIO) ||
332
        (vector >= CYGNUM_HAL_INTERRUPT_GPIO11)) {
333
        *SA11X0_GPIO_EDGE_DETECT_STATUS  = (1 << (vector & 0x1F));
334
    } else {
335
        // Not a GPIO interrupt
336
        return;
337
    }
338
}
339
 
340
void hal_interrupt_configure(int vector, int level, int up)
341
{
342
 
343
#ifdef HAL_EXTENDED_INTERRUPT_CONFIGURE
344
    // Use platform specific handling, if defined
345
    // Note: this macro should do a 'return' for "extended" values of 'vector'
346
    // Normal vectors are handled by code subsequent to the macro call.
347
    HAL_EXTENDED_INTERRUPT_CONFIGURE(vector, level, up);
348
#endif
349
 
350
    // This function can be used to configure the GPIO interrupts.  All
351
    // of these pins can potentially generate an interrupt, but only
352
    // 0..10 are unique.  Thus the discontinuity in the numbers.
353
    // Also, if 'level' is true, then both edges are enabled if 'up' is
354
    // true, otherwise they will be disabled.
355
    // Non GPIO sources are ignored.
356
    if ((vector < CYGNUM_HAL_INTERRUPT_GPIO) ||
357
        (vector >= CYGNUM_HAL_INTERRUPT_GPIO11)) {
358
        if (level) {
359
            if (up) {
360
                // Enable both edges
361
                *SA11X0_GPIO_RISING_EDGE_DETECT |= (1 << (vector & 0x1F));
362
                *SA11X0_GPIO_FALLING_EDGE_DETECT |= (1 << (vector & 0x1F));
363
            } else {
364
                // Disable both edges
365
                *SA11X0_GPIO_RISING_EDGE_DETECT &= ~(1 << (vector & 0x1F));
366
                *SA11X0_GPIO_FALLING_EDGE_DETECT &= ~(1 << (vector & 0x1F));
367
            }
368
        } else {
369
            // Only interested in one edge
370
            if (up) {
371
                // Set rising edge detect and clear falling edge detect.
372
                *SA11X0_GPIO_RISING_EDGE_DETECT |= (1 << (vector & 0x1F));
373
                *SA11X0_GPIO_FALLING_EDGE_DETECT &= ~(1 << (vector & 0x1F));
374
            } else {
375
                // Set falling edge detect and clear rising edge detect.
376
                *SA11X0_GPIO_FALLING_EDGE_DETECT |= (1 << (vector & 0x1F));
377
                *SA11X0_GPIO_RISING_EDGE_DETECT &= ~(1 << (vector & 0x1F));
378
            }
379
        }
380
    }
381
}
382
 
383
void hal_interrupt_set_level(int vector, int level)
384
{
385
 
386
#ifdef HAL_EXTENDED_INTERRUPT_SET_LEVEL
387
    // Use platform specific handling, if defined
388
    // Note: this macro should do a 'return' for "extended" values of 'vector'
389
    // Normal vectors are handled by code subsequent to the macro call.
390
    HAL_EXTENDED_INTERRUPT_SET_LEVEL(vector, level);
391
#endif
392
 
393
    // Interrupt priorities are not configurable on the SA11X0.
394
}
395
 
396
/*------------------------------------------------------------------------*/
397
// These routines are for testing the equivalent efficient macros of the
398
// same names.  They actually inspect the MMap installed and tell the
399
// truth - including about the validity of the address at all.
400
 
401
cyg_uint32 hal_virt_to_phys_address( cyg_uint32 vaddr )
402
{
403
    register cyg_uint32 *ttb_base;
404
    cyg_uint32 noise;
405
    register union ARM_MMU_FIRST_LEVEL_DESCRIPTOR desc;
406
 
407
    // Get the TTB register
408
    asm volatile ("mrc  p15,0,%0,c2,c0,0;"
409
                  "mov  %0, %0, lsr #14;" // Lower 14 bits are undefined
410
                  "mov  %0, %0, asl #14;" // ...so clear them
411
                  : "=r"(ttb_base)
412
                  :
413
                  /*:*/);
414
 
415
    noise = vaddr & (SZ_1M - 1);
416
    vaddr /= SZ_1M; // Page size/Entry size is Mb.
417
 
418
    desc.word = *ARM_MMU_FIRST_LEVEL_DESCRIPTOR_ADDRESS( ttb_base, vaddr );
419
 
420
    // Is this a valid entry that we understand?
421
    if ( ARM_MMU_FIRST_LEVEL_SECTION_ID == desc.section.id )
422
        return noise + desc.section.base_address * SZ_1M;
423
 
424
    return 0; // Not available.
425
}
426
 
427
cyg_uint32 hal_phys_to_virt_address( cyg_uint32 paddr )
428
{
429
    cyg_uint32 *ttb_base;
430
    cyg_uint32 i, noise;
431
    register union ARM_MMU_FIRST_LEVEL_DESCRIPTOR desc;
432
    cyg_bool identity_found = false;
433
 
434
    // Get the TTB register
435
    asm volatile ("mrc  p15,0,%0,c2,c0,0;"
436
                  "mov  %0, %0, lsr #14;" // Lower 14 bits are undefined
437
                  "mov  %0, %0, asl #14;" // ...so clear them
438
                  : "=r"(ttb_base)
439
                  :
440
                  /*:*/);
441
 
442
 
443
    noise = paddr & (SZ_1M - 1);
444
    paddr /= SZ_1M; // Page size/Entry size is Mb.
445
 
446
    for ( i = 0; i <= 0xfff; i++ ) {
447
        desc.word = *ARM_MMU_FIRST_LEVEL_DESCRIPTOR_ADDRESS( ttb_base, i );
448
 
449
        // Is this a valid entry that we understand?
450
        if ( ARM_MMU_FIRST_LEVEL_SECTION_ID == desc.section.id ) {
451
            if ( paddr == desc.section.base_address ) {
452
                // Then the virtual address is i (in Mb).
453
                if ( i == paddr ) {
454
                    // We found a direct map first.  Do not report that
455
                    // immediately because it may be double mapped to a
456
                    // distinct virtual address, which we should return in
457
                    // preference.  But remember that we saw it.
458
                    identity_found = true;
459
                    continue;
460
                }
461
                // Otherwise report that we found it:
462
                return noise + i * SZ_1M;
463
            }
464
        }
465
    }
466
    // No non-identity matches were found.
467
    if ( identity_found )
468
        return noise + paddr * SZ_1M;
469
 
470
    return 0; // Not available.
471
}
472
 
473
cyg_uint32 hal_virt_to_uncached_address( cyg_uint32 vaddr )
474
{
475
    cyg_uint32 *ttb_base;
476
    cyg_uint32 noise, paddr, i;
477
    register union ARM_MMU_FIRST_LEVEL_DESCRIPTOR desc;
478
 
479
    // Get the TTB register
480
    asm volatile ("mrc  p15,0,%0,c2,c0,0;"
481
                  "mov  %0, %0, lsr #14;" // Lower 14 bits are undefined
482
                  "mov  %0, %0, asl #14;" // ...so clear them
483
                  : "=r"(ttb_base)
484
                  :
485
                  /*:*/);
486
 
487
 
488
    noise = vaddr & (SZ_1M - 1);
489
    vaddr /= SZ_1M; // Page size/Entry size is Mb.
490
 
491
    desc.word = *ARM_MMU_FIRST_LEVEL_DESCRIPTOR_ADDRESS( ttb_base, vaddr );
492
 
493
    // Is this a valid entry that we understand?
494
    if ( ARM_MMU_FIRST_LEVEL_SECTION_ID != desc.section.id )
495
        return 0; // Not available.
496
 
497
    // Is this very address uncacheable already?
498
    if ( ARM_UNCACHEABLE == desc.section.c )
499
        return noise + vaddr * SZ_1M;
500
 
501
    paddr = desc.section.base_address;
502
 
503
    // We could look straight at a direct mapped slot for the physical
504
    // address as per convention...
505
 
506
    // Now scan through for a virtual address that maps to the same
507
    // physical memory, but uncached.
508
    for ( i = 0; i <= 0xfff; i++ ) {
509
        desc.word = *ARM_MMU_FIRST_LEVEL_DESCRIPTOR_ADDRESS( ttb_base, i );
510
 
511
        // Is this a valid entry that we understand?
512
        if ( ARM_MMU_FIRST_LEVEL_SECTION_ID == desc.section.id )
513
            if ( paddr == desc.section.base_address )
514
                // Then the virtual address is i (in Mb).
515
                if ( ARM_UNCACHEABLE == desc.section.c )
516
                    // Then this one is not cacheable.
517
                    return noise + i * SZ_1M;
518
    }
519
 
520
    return 0; // Not available.
521
}
522
 
523
 
524
/*------------------------------------------------------------------------*/
525
// EOF sa11x0_misc.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.