2 * cma101.c -- lo-level support for Cogent CMA101 development board.
4 * Copyright (c) 1996, 2001, 2002 Cygnus Support
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice is included verbatim in any distributions. No written agreement,
10 * license, or royalty fee is required for any of the authorized uses.
11 * Modifications to this software may be copyrighted by their authors
12 * and need not follow the licensing terms described here, provided that
13 * the new terms are clearly indicated on the first page of each file where
18 /* The assembler portions of this file need to be re-written to
19 support mips16, if and when that seems useful.
21 #error cma101.c can not be compiled -mips16
25 #include <time.h> /* standard ANSI time routines */
27 /* Normally these would appear in a header file for external
28 use. However, we are only building a simple example world at the
34 #define BYTEREG(b,o) ((volatile unsigned char *)(PHYS_TO_K1((b) + (o) + 7)))
37 #define BYTEREG(b,o) ((volatile unsigned char *)(PHYS_TO_K1((b) + (o))))
41 #define RTCLOCK_BASE (0x0E800000) /* Mk48T02 NVRAM/RTC */
42 #define UART_BASE (0x0E900000) /* NS16C552 DUART */
43 #define LCD_BASE (0x0EB00000) /* Alphanumeric display */
45 /* LCD panel manifests: */
46 #define LCD_DATA BYTEREG(LCD_BASE,0)
47 #define LCD_CMD BYTEREG(LCD_BASE,8)
49 #define LCD_STAT_BUSY (0x80)
50 #define LCD_SET_DDADDR (0x80)
53 /* The lo-offsets are the NVRAM locations (0x7F8 bytes) */
54 #define RTC_CONTROL BYTEREG(RTCLOCK_BASE,0x3FC0)
55 #define RTC_SECS BYTEREG(RTCLOCK_BASE,0x3FC8)
56 #define RTC_MINS BYTEREG(RTCLOCK_BASE,0x3FD0)
57 #define RTC_HOURS BYTEREG(RTCLOCK_BASE,0x3FD8)
58 #define RTC_DAY BYTEREG(RTCLOCK_BASE,0x3FE0)
59 #define RTC_DATE BYTEREG(RTCLOCK_BASE,0x3FE8)
60 #define RTC_MONTH BYTEREG(RTCLOCK_BASE,0x3FF0)
61 #define RTC_YEAR BYTEREG(RTCLOCK_BASE,0x3FF8)
63 #define RTC_CTL_LOCK_READ (0x40) /* lock RTC whilst reading */
64 #define RTC_CTL_LOCK_WRITE (0x80) /* lock RTC whilst writing */
66 /* Macro to force out-standing memory transfers to complete before
67 next sequence. For the moment we assume that the processor in the
68 CMA101 board supports at least ISA II. */
69 #define DOSYNC() asm(" .set mips2 ; sync ; .set mips0")
71 /* We disable interrupts by writing zero to all of the masks, and the
72 global interrupt enable bit: */
73 #define INTDISABLE(sr,tmp) asm("\
80 .set mips0" : "=d" (sr), "=d" (tmp))
81 #define INTRESTORE(sr) asm("\
84 .set mips0" : : "d" (sr))
86 /* TODO:FIXME: The CPU card support should be in separate source file
87 from the standard CMA101 support provided in this file. */
89 /* The CMA101 board being used contains a CMA257 Vr4300 CPU:
90 MasterClock is at 33MHz. PClock is derived from MasterClock by
91 multiplying by the ratio defined by the DivMode pins:
92 DivMode(1:0) MasterClock PClock Ratio
94 01 100MHz 150MHz 1.5:1
98 Are these pins reflected in the EC bits in the CONFIG register? or
99 is that talking about a different clock multiplier?
104 (all other values are undefined)
107 #define MASTERCLOCK (33) /* ticks per uS */
108 unsigned int pclock
; /* number of PClock ticks per uS */
113 asm volatile ("mfc0 %0,$16 ; nop ; nop" : "=r" (config
)); /* nasty CP0 register constant */
114 switch ((config
>> 28) & 0x7) {
115 case 0x7 : /* 1.5:1 */
116 pclock
= (MASTERCLOCK
+ (MASTERCLOCK
/ 2));
120 pclock
= (2 * MASTERCLOCK
);
124 pclock
= (3 * MASTERCLOCK
);
128 default : /* invalid configuration, so assume the lowest */
129 pclock
= MASTERCLOCK
;
136 #define PCLOCK_WAIT(x) __cpu_timer_poll((x) * pclock)
138 /* NOTE: On the Cogent CMA101 board the LCD controller will sometimes
139 return not-busy, even though it is. The work-around is to perform a
140 ~50uS delay before checking the busy signal. */
145 PCLOCK_WAIT(50); /* 50uS delay */
146 return(*LCD_CMD
& LCD_STAT_BUSY
);
149 /* Note: This code *ASSUMES* that the LCD has already been initialised
150 by the monitor. It only provides code to write to the LCD, and is
151 not a complete device driver. */
154 lcd_display (int line
, const char *msg
)
161 *LCD_CMD
= (LCD_SET_DDADDR
| (line
== 1 ? 0x40 : 0x00));
163 for (n
= 0; n
< 16; n
++) {
175 #define SM_PATTERN (0x55AA55AA)
176 #define SM_INCR ((256 << 10) / sizeof(unsigned int)) /* 64K words */
178 extern unsigned int __buserr_count(void);
179 extern void __default_buserr_handler(void);
180 extern void __restore_buserr_handler(void);
182 /* Allow the user to provide his/her own defaults. */
183 unsigned int __sizemem_default
;
188 volatile unsigned int *base
;
189 volatile unsigned int *probe
;
190 unsigned int baseorig
;
193 char *endptr
= (char *)&end
;
196 /* If the linker script provided a value for the memory size (or the user
197 overrode it in a debugger), use that. */
198 if (__sizemem_default
)
199 return __sizemem_default
;
201 /* If we are running in kernel segment 0 (possibly cached), try sizing memory
202 in kernel segment 1 (uncached) to avoid some problems with monitors. */
203 if (endptr
>= K0BASE_ADDR
&& endptr
< K1BASE_ADDR
)
204 endptr
= (endptr
- K0BASE_ADDR
) + K1BASE_ADDR
;
206 INTDISABLE(sr
,baseorig
); /* disable all interrupt masks */
208 __default_buserr_handler();
213 /* _end is the end of the user program. _end may not be properly aligned
214 for an int pointer, so we adjust the address to make sure it is safe.
215 We use void * arithmetic to avoid accidentally truncating the pointer. */
217 extra
= ((int) endptr
& (sizeof (int) - 1));
218 base
= ((void *) endptr
+ sizeof (int) - extra
);
222 /* This assumes that the instructions fetched between the store, and
223 the following read will have changed the data bus contents: */
224 if (*base
== SM_PATTERN
) {
227 unsigned int probeorig
;
230 /* Check if a bus error occurred: */
231 if (!__buserr_count()) {
234 if (*probe
== SM_PATTERN
) {
235 *probe
= ~SM_PATTERN
;
237 if (*probe
== ~SM_PATTERN
) {
238 if (*base
== SM_PATTERN
) {
251 __restore_buserr_handler();
256 INTRESTORE(sr
); /* restore interrupt mask to entry state */
258 return((probe
- base
) * sizeof(unsigned int));
261 /* Provided as a function, so as to avoid reading the I/O location
267 return ((((byte
>> 4) & 0xF) * 10) + (byte
& 0xF));
276 *RTC_CONTROL
|= RTC_CTL_LOCK_READ
;
279 tm
.tm_sec
= convertbcd(*RTC_SECS
);
280 tm
.tm_min
= convertbcd(*RTC_MINS
);
281 tm
.tm_hour
= convertbcd(*RTC_HOURS
);
282 tm
.tm_mday
= convertbcd(*RTC_DATE
);
283 tm
.tm_mon
= convertbcd(*RTC_MONTH
);
284 tm
.tm_year
= convertbcd(*RTC_YEAR
);
287 *RTC_CONTROL
&= ~(RTC_CTL_LOCK_READ
| RTC_CTL_LOCK_WRITE
);
291 /* Check for invalid time information */
292 if ((tm
.tm_sec
< 60) && (tm
.tm_min
< 60) && (tm
.tm_hour
< 24)
293 && (tm
.tm_mday
< 32) && (tm
.tm_mon
< 13)) {
295 /* Get the correct year number, but keep it in YEAR-1900 form: */
299 #if 0 /* NOTE: mon_printf() can only accept 4 arguments (format string + 3 fields) */
300 mon_printf("[DBG: s=%d m=%d h=%d]", tm
.tm_sec
, tm
.tm_min
, tm
.tm_hour
);
301 mon_printf("[DBG: d=%d m=%d y=%d]", tm
.tm_mday
, tm
.tm_mon
, tm
.tm_year
);
304 /* Convert the time-structure into a second count */
305 result
= mktime (&tm
);