1 /******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12 
13 #include <cpu.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <unistd.h>
19 #include <rtas.h>
20 #include <hw.h>
21 #include "rtas_board.h"
22 
23 volatile unsigned char *uart;
24 volatile unsigned char u4Flag;
25 
26 void io_init(void);
27 unsigned long check_flash_image(unsigned long rombase, unsigned long length,
28 				unsigned long start_crc);
29 
30 void
31 io_init(void)
32 {
33 	// read ID register: only if it is a PC87427, enable serial2
34 	store8_ci(0xf400002e, 0x20);
35 	if (load8_ci(0xf400002f) != 0xf2) {
36 		uart = (volatile unsigned char *) 0xf40003f8;
37 		u4Flag = 0;
38 	} else {
39 		uart = (volatile unsigned char *) 0xf40002f8;
40 		u4Flag = 1;
41 	}
42 }
43 
44 static void
45 display_char(char ch)
46 {
47 	volatile int i = 0;
48 	volatile unsigned char *uart = (volatile unsigned char *) 0xf40002f8;
49 	int cnt = 2;
50 	while (cnt--) {
51 		set_ci();
52 		while (!(uart[5] & 0x20)) {
53 			i++;
54 		}
55 		uart[0] = ch;
56 		clr_ci();
57 		uart += 0x100;
58 	}
59 }
60 
61 ssize_t
62 write(int fd __attribute((unused)), const void *buf, size_t cnt)
63 {
64 	while (cnt--) {
65 		display_char(*(char *) buf);
66 		if (*(char *) buf++ == '\n')
67 			display_char('\r');
68 	}
69 	return 0;
70 }
71 
72 void *
73 sbrk(int incr __attribute((unused)))
74 {
75 	return (void *) -1;
76 }
77 
78 
79 
80 void
81 rtas_display_character(rtas_args_t * pArgs)
82 {
83 	int retVal = 0;
84 	display_char((char) pArgs->args[0]);
85 	pArgs->args[1] = retVal;
86 }
87 
88 unsigned long
89 check_flash_image(unsigned long rombase, unsigned long length,
90 		  unsigned long start_crc)
91 {
92 	const uint32_t CrcTableHigh[16] = {
93 		0x00000000, 0x4C11DB70, 0x9823B6E0, 0xD4326D90,
94 		0x34867077, 0x7897AB07, 0xACA5C697, 0xE0B41DE7,
95 		0x690CE0EE, 0x251D3B9E, 0xF12F560E, 0xBD3E8D7E,
96 		0x5D8A9099, 0x119B4BE9, 0xC5A92679, 0x89B8FD09
97 	};
98 	const uint32_t CrcTableLow[16] = {
99 		0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9,
100 		0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005,
101 		0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
102 		0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD
103 	};
104 
105 	char *Buffer = (char *) rombase;
106 	uint32_t AccumCRC = start_crc;
107 	char val;
108 	uint32_t Temp;
109 	while (length-- > 0) {
110 		set_ci();
111 		val = *Buffer;
112 		clr_ci();
113 		Temp = ((AccumCRC >> 24) ^ val) & 0x000000ff;
114 		AccumCRC <<= 8;
115 		AccumCRC ^= CrcTableHigh[Temp / 16];
116 		AccumCRC ^= CrcTableLow[Temp % 16];
117 		++Buffer;
118 	}
119 	return AccumCRC;
120 }
121