summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/haleakala/startup/bspstart.c
blob: 2bf2f18c1b919eb3fea975f020c382581189a294 (plain) (blame)
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
/*  bsp_start()
 *
 *  This routine starts the application.  It includes application,
 *  board, and monitor specific initialization and configuration.
 *  The generic CPU dependent initialization has been performed
 *  before this routine is invoked.
 *
 *  INPUT:  NONE
 *
 *  OUTPUT: NONE
 *
 *  Author:	Thomas Doerfler <td@imd.m.isar.de>
 *              IMD Ingenieurbuero fuer Microcomputertechnik
 *
 *  COPYRIGHT (c) 1998 by IMD
 *
 *  Changes from IMD are covered by the original distributions terms.
 *  This file has been derived from the papyrus BSP:
 *
 *  Author:	Andrew Bray <andy@i-cubed.co.uk>
 *
 *  COPYRIGHT (c) 1995 by i-cubed ltd.
 *
 *  To anyone who acknowledges that this file is provided "AS IS"
 *  without any express or implied warranty:
 *      permission to use, copy, modify, and distribute this file
 *      for any purpose is hereby granted without fee, provided that
 *      the above copyright notice and this notice appears in all
 *      copies, and that the name of i-cubed limited not be used in
 *      advertising or publicity pertaining to distribution of the
 *      software without specific, written prior permission.
 *      i-cubed limited makes no representations about the suitability
 *      of this software for any purpose.
 *
 *  Modifications for spooling console driver and control of memory layout
 *  with linker command file by
 *              Thomas Doerfler <td@imd.m.isar.de>
 *  for these modifications:
 *  COPYRIGHT (c) 1997 by IMD, Puchheim, Germany.
 *
 *  To anyone who acknowledges that this file is provided "AS IS"
 *  without any express or implied warranty:
 *      permission to use, copy, modify, and distribute this file
 *      for any purpose is hereby granted without fee, provided that
 *      the above copyright notice and this notice appears in all
 *      copies. IMD makes no representations about the suitability
 *      of this software for any purpose.
 *
 *  Derived from c/src/lib/libbsp/no_cpu/no_bsp/startup/bspstart.c:
 *
 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  Modifications for PPC405GP by Dennis Ehlin
 *
 *  Further modified for the PPC405EX Haleakala board by
 *  Michael Hamel ADInstruments Ltd May 2008
 *
 *  $Id$
 */
#include <string.h>
#include <fcntl.h>

#include <bsp.h>
#include <bsp/uart.h>
#include <rtems/libio.h>
#include <rtems/libcsupport.h>
#include <bsp/irq.h>
#include <rtems/bspIo.h>
#include <libcpu/cpuIdent.h>
#include <libcpu/spr.h>
#include <rtems/powerpc/powerpc.h>
#include <ppc4xx/ppc405gp.h>
#include <ppc4xx/ppc405ex.h>

SPR_RW(SPRG0)
SPR_RW(SPRG1)

#include <stdio.h>

/*
 *  Driver configuration parameters
 */

/* Expected by clock.c */
uint32_t	bsp_clicks_per_usec;
boolean		bsp_timer_internal_clock;   /* TRUE, when timer runs with CPU clk */
uint32_t	bsp_timer_least_valid;
uint32_t	bsp_timer_average_overhead;


/*      Initialize whatever libc we are using
 *      called from postdriver hook
 */

void bsp_postdriver_hook(void);
void bsp_libc_init( void *, uint32_t, int );

/*
 *
 *  bsp_predriver_hook
 *
 *  Before drivers are setup.
 */

void bsp_predriver_hook(void)
{
  
}

/*
 *  Function:   bsp_pretasking_hook
 *  Created:    95/03/10
 *
 *  Description:
 *      BSP pretasking hook.  Called just before drivers are initialized.
 *      Used to setup libc and install any BSP extensions.
 *
 *  NOTES:
 *      Must not use libc (to do io) from here, since drivers are
 *      not yet initialized.
 *
 */


extern uint8_t	_RAMEnd;		/* Defined in linkcmds */

void bsp_pretasking_hook(void)
/* Initialise libc with the address and size of the heap, which runs
   from the end of the RTEMS workspace to the top of RAM */
{
    uint32_t heap_start;
 
    heap_start = ( (uint32_t)Configuration.work_space_start +
					rtems_configuration_get_work_space_size() );

    bsp_libc_init((void *)heap_start, (uint32_t)(&_RAMEnd) - heap_start, 0);

	#ifdef RTEMS_DEBUG
		rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
	#endif
}

/*-------------------- Haleakala-specific UART setup -------------------------*/

static void
EarlyUARTInit(int baudRate)
{
	uint8_t* up = (uint8_t*)(BSP_UART_IOBASE_COM1);
	int divider = BSP_UART_BAUD_BASE / baudRate;
	up[LCR] = DLAB;		/* Access DLM/DLL */
	up[DLL] = divider & 0x0FF;
	up[DLM] = divider >> 8;
	up[LCR] = CHR_8_BITS;
	up[MCR] = DTR | RTS;
	up[FCR] = FIFO_EN | XMIT_RESET | RCV_RESET;
	up[THR] = '+';
}


static void
InitUARTClock()
{
	uint32_t reg;
	mfsdr(SDR0_UART0,reg);
	reg &= ~0x008000FF;
	reg |= 0x00800001;		/* Ext clock, div 1 */
	mtsdr(SDR0_UART0,reg);
}

void GPIO_AlternateSelect(int bitnum, int source)
/* PPC405EX: select a GPIO function for the specified pin */
{
	int shift;
	unsigned long value, mask;
	GPIORegisters* gpioPtr = (GPIORegisters*)(GPIOAddress);

	shift = (31 - bitnum) & 0xF;
	value = (source & 3) << (shift*2);
	mask  = 3 << (shift*2);
	if (bitnum <= 15) {
		gpioPtr->OSRL = (gpioPtr->OSRL & ~mask) | value;
		gpioPtr->TSRL = (gpioPtr->TSRL & ~mask) | value;
	} else {
		gpioPtr->OSRH = (gpioPtr->OSRH & ~mask) | value;
		gpioPtr->TSRH = (gpioPtr->TSRH & ~mask) | value;
	}
}

void Init_FPGA()
{
	/* Have to write to the FPGA to enable the UART drivers */
	/* Have to enable CS2 as an output in GPIO to get the FPGA working */
	mtebc(EBC0_B2CR,0xF0018000);	/* Set up CS2 at 0xF0000000 */
	mtebc(EBC0_B2AP,0x9400C800);
	GPIO_AlternateSelect(9,1);		/* GPIO9 = PerCS2 */
	{
		unsigned long *fpgaPtr = (unsigned long*)(0xF0000000);
		unsigned long n;
		n = *(fpgaPtr);
		n &= ~0x00100;		/* User LEDs on */
		n |=  0x30000;		/* UART 0 and 1 transcievers on! */
		*fpgaPtr = n;
	}
}

/*===================================================================*/

static void
DirectUARTWrite(const char c)
{
	uint8_t* up = (uint8_t*)(BSP_UART_IOBASE_COM1);
	while ((up[LSR] & THRE) == 0) { ; }
	up[THR] = c;
	if (c=='\n')
		DirectUARTWrite('\r');
}

/* We will provide our own printk output function as it may get used early */
BSP_output_char_function_type BSP_output_char = DirectUARTWrite;


/*===================================================================*/


/*
 *  bsp_start
 *
 *  This routine does the bulk of the system initialization.
 */


void bsp_start( void )
{
	extern unsigned long *intrStackPtr;
	register unsigned char* intrStack;
	ppc_cpu_id_t myCpu;
	ppc_cpu_revision_t myCpuRevision;

	/* Get the UART clock initialized first in case we call printk */

	InitUARTClock();
	Init_FPGA();
	EarlyUARTInit(115200);

	/*
	* Get CPU identification dynamically. Note that the get_ppc_cpu_type() 
	* function store the result in global variables 
	* so that it can be used later...
	*/
	myCpu 	    = get_ppc_cpu_type();
	myCpuRevision = get_ppc_cpu_revision();

	/*
	*  initialize the device driver parameters
	*/

	/* Set globals visible to clock.c */
	bsp_clicks_per_usec = 400;	/* timebase register ticks/microsecond = CPU Clk in MHz */
	bsp_timer_internal_clock  = TRUE;
	bsp_timer_average_overhead = 2;
	bsp_timer_least_valid = 3;
	
	/*
	* Initialize some SPRG registers related to irq handling
	*/

	intrStack = (((unsigned char*)&intrStackPtr) - PPC_MINIMUM_STACK_FRAME_SIZE);
	_write_SPRG1((unsigned int)intrStack);
	/* signal them that we have fixed PR288 - eventually, this should go away */
	/*
	* Initialize default raw exception handlers. 
	*/
	initialize_exceptions();

	/*
	* Install our own set of exception vectors
	*/
	BSP_rtems_irq_mng_init(0);

	/*
	*  Allocate the memory for the RTEMS Work Space.  This can come from
	*  a variety of places: hard coded address, malloc'ed from outside
	*  RTEMS world (e.g. simulator or primitive memory manager), or (as
	*  typically done by stock BSPs) by subtracting the required amount
	*  of work space from the last physical address on the CPU board.
	*/
	/* In this case we allocate space at an address defined in linkcmds
	   which points to a block above the stack and below the heap */
	{
		extern uint8_t	_WorkspaceStart;	
		Configuration.work_space_start = &_WorkspaceStart;
	}

}

void BSP_ask_for_reset(void)
{
  printk("system stopped, press RESET");
  while(1) {};
}

void BSP_panic(char *s)
{
  printk("%s PANIC %s\n",_RTEMS_version, s);
  BSP_ask_for_reset();
}

void _BSP_Fatal_error(unsigned int v)
{
  printk("%s PANIC ERROR %x\n",_RTEMS_version, v);
  BSP_ask_for_reset();
}