source: trunk/softs/tsar_boot/src/reset_utils.c @ 688

Last change on this file since 688 was 655, checked in by cfuguet, 11 years ago

Fixing bug introduced in last commit :

  • When using IO bridge, some header files were missing in the reset_utils.h file
File size: 5.3 KB
Line 
1/**
2 * \file    : reset_utils.c
3 * \date    : August 2012
4 * \author  : Cesar Fuguet
5 *
6 * Definition of utilities functions used by the TSAR pre-loader
7 */
8
9#include <reset_utils.h>
10
11/********************************************************************
12 * proctime()
13 *
14 * Returns processor local time.
15 ********************************************************************/
16inline unsigned int proctime() 
17{
18    unsigned int ret;
19    asm volatile ("mfc0   %0,        $9":"=r" (ret));
20    return ret;
21}
22
23/********************************************************************
24 * memcpy( _dst, _src, size )
25 *
26 * Transfer data between two memory buffers.
27 *
28 * \param _dst   : Destination buffer base address
29 * \param _src   : Source buffer base address
30 * \param size   : Number of bytes to transfer
31 *
32 ********************************************************************/
33void * memcpy(void *_dst, const void *_src, unsigned int size)
34{
35    unsigned int *dst = _dst;
36    const unsigned int *src = _src;
37    if ( !((unsigned int)dst & 3) && !((unsigned int)src & 3) )
38    {
39        while (size > 3) 
40        {
41            *dst++ = *src++;
42            size -= 4;
43        }
44    }
45
46    unsigned char *cdst = (unsigned char*) dst;
47    unsigned char *csrc = (unsigned char*) src;
48
49    while (size--) 
50    {
51        *cdst++ = *csrc++;
52    }
53    return _dst;
54}
55
56/********************************************************************
57 * memset( _dst, value, size )
58 *
59 * Initialize memory buffers with predefined value.
60 *
61 * \param _dst   : Destination buffer base address
62 * \param value  : Initialization value
63 * \param size   : Number of bytes to initialize
64 *
65 ********************************************************************/
66void * memset(void *_dst, const int value, unsigned int size)
67{
68    unsigned char val = (unsigned char) value;
69    int word = (val << 24) || (val << 16) ||
70               (val << 8 ) || (val      );
71
72    /*
73     * Write 4 bytes when destination buffer is aligned to 4 bytes
74     * and size is greater or equal to 4
75     */
76    unsigned int *dst = _dst;
77    if ( !((unsigned int)dst & 3) )
78    {
79        while (size > 3) 
80        {
81            *dst++ = word;
82            size -= 4;
83        }
84    }
85
86    /*
87     * Write 1 byte when destination buffer is not aligned to 4 bytes
88     * or size is smaller than 4
89     */
90    char* cdst = (char*) _dst;
91    while(size--)
92    {
93        *cdst++ = (char) value;
94    }
95
96    return _dst;
97}
98
99/********************************************************************
100 * reset_print_elf_phdr( elf_phdr_ptr )
101 *
102 * Print some fields of a ELF program header
103 *
104 * \param elf_phdr_ptr : Pointer to the ELF program header to print
105 *
106 ********************************************************************/
107void reset_print_elf_phdr(Elf32_Phdr * elf_phdr_ptr)
108{
109    reset_puts("- type   : ");
110    reset_putx(elf_phdr_ptr->p_type);
111
112    reset_puts("\n- offset : ");
113    reset_putx(elf_phdr_ptr->p_offset);
114
115    reset_puts("\n- vaddr  : ");
116    reset_putx(elf_phdr_ptr->p_vaddr);
117
118    reset_puts("\n- paddr  : ");
119    reset_putx(elf_phdr_ptr->p_paddr);
120
121    reset_puts("\n- filesz : ");
122    reset_putx(elf_phdr_ptr->p_filesz);
123
124    reset_puts("\n- memsz  : ");
125    reset_putx(elf_phdr_ptr->p_memsz);
126
127    reset_puts("\n- flags  : ");
128    reset_putx(elf_phdr_ptr->p_flags);
129
130    reset_puts("\n- align  : ");
131    reset_putx(elf_phdr_ptr->p_align);
132}
133
134
135/********************************************************************
136 * reset_mcc_inval()
137 *
138 * Invalidate all data cache lines corresponding to a memory buffer
139 * (identified by an address and a size) in L2 cache.
140 ********************************************************************/
141#if USE_IOB
142void reset_mcc_invalidate ( const void * buffer,
143                            unsigned int size)
144{
145    unsigned int * mcc_address = (unsigned int *)MCC_PADDR_BASE;
146
147    // get the hard lock assuring exclusive access to MEMC
148    while (ioread32(&mcc_address[MCC_LOCK]));
149
150    // write invalidate paremeters on the memory cache this preloader
151    // use only the cluster 0 and then the HI bits are not used
152   
153    iowrite32(&mcc_address[MCC_ADDR_LO], (unsigned int) buffer);
154    iowrite32(&mcc_address[MCC_ADDR_HI], (unsigned int) 0);
155    iowrite32(&mcc_address[MCC_LENGTH] , (unsigned int) size);
156    iowrite32(&mcc_address[MCC_CMD]    , (unsigned int) MCC_CMD_INVAL);
157
158    // release the lock protecting MEMC
159    iowrite32(&mcc_address[MCC_LOCK], (unsigned int) 0);
160}
161#endif
162
163/********************************************************************
164 * reset_dcache_buf_invalidate()
165 *
166 * Invalidate all data cache lines corresponding to a memory buffer
167 * (identified by an address and a size) in L1 cache and L2 cache.
168 ********************************************************************/
169#if (CACHE_COHERENCE == 0) || USE_IOB
170void reset_buf_invalidate ( const void * buffer,
171                            unsigned int line_size,
172                            unsigned int size)
173{
174    unsigned int i;
175
176    // iterate on cache lines
177    for (i = 0; i <= size; i += line_size) 
178    {
179        asm volatile(
180            " cache %0, %1"
181            :// no outputs
182            :"i" (0x11), "R" (*((unsigned char *) buffer + i))
183            );
184    }
185
186#if USE_IOB
187    reset_mcc_invalidate(buffer, size);
188#endif
189}
190#endif
191
192// vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab
Note: See TracBrowser for help on using the repository browser.