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

Last change on this file since 691 was 691, checked in by cfuguet, 10 years ago

Bugfix in tsar_boot:
Logical OR replace by Bitwise OR to build word

File size: 5.2 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) | (val << 8 ) | val;
70
71    /*
72     * Write 4 bytes when destination buffer is aligned to 4 bytes
73     * and size is greater or equal to 4
74     */
75    unsigned int *dst = _dst;
76    if ( !((unsigned int)dst & 3) )
77    {
78        while (size > 3) 
79        {
80            *dst++ = word;
81            size -= 4;
82        }
83    }
84
85    /*
86     * Write 1 byte when destination buffer is not aligned to 4 bytes
87     * or size is smaller than 4
88     */
89    char* cdst = (char*) _dst;
90    while(size--)
91    {
92        *cdst++ = (char) value;
93    }
94
95    return _dst;
96}
97
98/********************************************************************
99 * reset_print_elf_phdr( elf_phdr_ptr )
100 *
101 * Print some fields of a ELF program header
102 *
103 * \param elf_phdr_ptr : Pointer to the ELF program header to print
104 *
105 ********************************************************************/
106void reset_print_elf_phdr(Elf32_Phdr * elf_phdr_ptr)
107{
108    reset_puts("- type   : ");
109    reset_putx(elf_phdr_ptr->p_type);
110
111    reset_puts("\n- offset : ");
112    reset_putx(elf_phdr_ptr->p_offset);
113
114    reset_puts("\n- vaddr  : ");
115    reset_putx(elf_phdr_ptr->p_vaddr);
116
117    reset_puts("\n- paddr  : ");
118    reset_putx(elf_phdr_ptr->p_paddr);
119
120    reset_puts("\n- filesz : ");
121    reset_putx(elf_phdr_ptr->p_filesz);
122
123    reset_puts("\n- memsz  : ");
124    reset_putx(elf_phdr_ptr->p_memsz);
125
126    reset_puts("\n- flags  : ");
127    reset_putx(elf_phdr_ptr->p_flags);
128
129    reset_puts("\n- align  : ");
130    reset_putx(elf_phdr_ptr->p_align);
131}
132
133
134/********************************************************************
135 * reset_mcc_inval()
136 *
137 * Invalidate all data cache lines corresponding to a memory buffer
138 * (identified by an address and a size) in L2 cache.
139 ********************************************************************/
140#if USE_IOB
141void reset_mcc_invalidate ( const void * buffer,
142                            unsigned int size)
143{
144    unsigned int * mcc_address = (unsigned int *)MCC_PADDR_BASE;
145
146    // get the hard lock assuring exclusive access to MEMC
147    while (ioread32(&mcc_address[MCC_LOCK]));
148
149    // write invalidate paremeters on the memory cache this preloader
150    // use only the cluster 0 and then the HI bits are not used
151   
152    iowrite32(&mcc_address[MCC_ADDR_LO], (unsigned int) buffer);
153    iowrite32(&mcc_address[MCC_ADDR_HI], (unsigned int) 0);
154    iowrite32(&mcc_address[MCC_LENGTH] , (unsigned int) size);
155    iowrite32(&mcc_address[MCC_CMD]    , (unsigned int) MCC_CMD_INVAL);
156
157    // release the lock protecting MEMC
158    iowrite32(&mcc_address[MCC_LOCK], (unsigned int) 0);
159}
160#endif
161
162/********************************************************************
163 * reset_dcache_buf_invalidate()
164 *
165 * Invalidate all data cache lines corresponding to a memory buffer
166 * (identified by an address and a size) in L1 cache and L2 cache.
167 ********************************************************************/
168#if (CACHE_COHERENCE == 0) || USE_IOB
169void reset_buf_invalidate ( const void * buffer,
170                            unsigned int line_size,
171                            unsigned int size)
172{
173    unsigned int i;
174
175    // iterate on cache lines
176    for (i = 0; i <= size; i += line_size) 
177    {
178        asm volatile(
179            " cache %0, %1"
180            :// no outputs
181            :"i" (0x11), "R" (*((unsigned char *) buffer + i))
182            );
183    }
184
185#if USE_IOB
186    reset_mcc_invalidate(buffer, size);
187#endif
188}
189#endif
190
191// vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab
Note: See TracBrowser for help on using the repository browser.