summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/apps/loader/main.c
blob: 8c635b0d3bac06c5c919b2f7c5827b4a1439ca54 (plain)
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
/* boot loader for Calypso phones */

/* (C) 2010 by Ingo Albrecht <prom@berlin.ccc.de>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <debug.h>
#include <memory.h>
#include <rffe.h>
#include <keypad.h>
#include <board.h>
#include <console.h>

#include <abb/twl3025.h>
#include <display/st7558.h>
#include <rf/trf6151.h>

#include <comm/sercomm.h>

#include <calypso/clock.h>
#include <calypso/tpu.h>
#include <calypso/tsp.h>
#include <calypso/irq.h>
#include <calypso/misc.h>
#include <calypso/uart.h>
#include <calypso/timer.h>

#include <layer1/sync.h>
#include <layer1/tpu_window.h>

#include "protocol.h"

struct loader_mem_read {
	uint8_t cmd;
	uint8_t nbytes;
	uint32_t address;
	uint8_t  data[0];
} __attribute__((__packed__));

uint32_t htonl(uint32_t hostlong) {
#if BYTE_ORDER==LITTLE_ENDIAN
  return (hostlong>>24) | ((hostlong&0xff0000)>>8) |
          ((hostlong&0xff00)<<8) | (hostlong<<24);
#else
  return hostlong;
#endif
}

uint32_t ntohl(uint32_t hostlong) __attribute__((weak,alias("htonl")));

uint16_t htons(uint16_t hostshort) {
#if BYTE_ORDER==LITTLE_ENDIAN
  return ((hostshort>>8)&0xff) | (hostshort<<8);
#else
  return hostshort;
#endif
}

uint16_t ntohs(uint16_t hostshort) __attribute__((weak,alias("htons")));

#define SCAN

#ifdef SCAN
/* if scanning is enabled, scan from 0 ... 124 */
#define BASE_ARFCN	0
#else
/* fixed ARFCN in GSM1800 at which Harald has his GSM test license */
#define BASE_ARFCN	871
#endif

/* FIXME: We need proper calibrated delay loops at some point! */
void delay_us(unsigned int us)
{
	volatile unsigned int i;

	for (i= 0; i < us*4; i++) { i; }
}

void delay_ms(unsigned int ms)
{
	volatile unsigned int i;

	for (i= 0; i < ms*1300; i++) { i; }
}

/* Main Program */
const char *hr = "======================================================================\n";

static void key_handler(enum key_codes code, enum key_states state);
static void cmd_handler(uint8_t dlci, struct msgb *msg);

int flag = 0;

void poweroff(void) {
	unsigned i;
	for(i = 0; i < 10; i++) {
		uart_poll(SERCOMM_UART_NR);
		delay_ms(10);
	}
	twl3025_power_off();
}

int main(void)
{
	/* Always disable wdt (some platforms enable it on boot) */
	wdog_enable(0);

	/* Initialize TWL3025 for power control */
	twl3025_init();

	/* Initialize UART without interrupts */
	uart_init(SERCOMM_UART_NR, 0);
	uart_baudrate(SERCOMM_UART_NR, UART_115200);

	/* Initialize HDLC subsystem */
	sercomm_init();

	/* Say hi */
	puts("\n\nOSMOCOM Calypso loader\n");
	puts(hr);

	/* Set up a key handler for powering off */
	keypad_set_handler(&key_handler);

	/* Set up loader communications */
	sercomm_register_rx_cb(SC_DLCI_LOADER, &cmd_handler);

	/* Wait for events */
	while (1) {
		keypad_poll();
		uart_poll(SERCOMM_UART_NR);
	}

	/* NOT REACHED */

	twl3025_power_off();
}

static void cmd_handler(uint8_t dlci, struct msgb *msg) {
	if(msg->data_len < 1) {
		return;
	}

	uint8_t command = 0; //= msgb_get_u8(msg);

	printf("command %u\n", command);

	msgb_free(msg);

	return;

	uint8_t  nbytes;
	uint32_t address;

	struct msgb *reply;

	switch(command) {

	case LOADER_PING:

		printf("ping\n");

		//sercomm_sendmsg(dlci, msg);
		//msg = NULL;

		break;

	case LOADER_MEM_READ:

		nbytes = msgb_get_u8(msg);
		address = msgb_get_u32(msg);

		printf("mem read %u @ %p\n", nbytes, (void*)address);

		reply = sercomm_alloc_msgb(6 + nbytes);

		msgb_put_u8(reply, LOADER_MEM_READ);
		msgb_put_u8(reply, nbytes);
		msgb_put_u32(reply, address);

		memcpy(msgb_put(reply, nbytes), (void*)address, nbytes);

		sercomm_sendmsg(dlci, reply);

		break;

	}

	if(msg) {
		msgb_free(msg);
	}
}

static void key_handler(enum key_codes code, enum key_states state)
{
	if (state != PRESSED)
		return;

	switch (code) {
	case KEY_POWER:
		poweroff();
		break;
	default:
		break;
	}
}