-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.c
418 lines (332 loc) · 7.78 KB
/
tools.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "tools.h"
#define IPOD_PP5002_LCD_BASE 0xc0001000
#define IPOD_PP5002_RTC 0xcf001110
#define IPOD_PP5020_LCD_BASE 0x70003000
#define IPOD_PP5020_RTC 0x60005010
#define LCD_DATA 0x10
#define LCD_CMD 0x08
#define IPOD_STD_LCD_WIDTH 160
#define IPOD_STD_LCD_HEIGHT 128
#define IPOD_MINI_LCD_WIDTH 138
#define IPOD_MINI_LCD_HEIGHT 110
#define IPOD_PHOTO_LCD_WIDTH 220
#define IPOD_PHOTO_LCD_HEIGHT 176
#define IPOD_NANO_LCD_WIDTH 176
#define IPOD_NANO_LCD_HEIGHT 132
#define HW_REV_MINI 4
#define HW_REV_4G 5
#define HW_REV_PHOTO 6
#define HW_REV_MINI_2 7
#define HW_REV_NANO 0xc
static unsigned long ipod_rtc_reg;
static unsigned long lcd_base;
static unsigned long lcd_busy_mask;
static unsigned long lcd_width;
static unsigned long lcd_height;
extern int ipod_ver;
/* find out which ipod revision we're running on */
void
get_ipod_rev()
{
unsigned long rev;
if (inl(0x2000) == (unsigned)"gfCS") {
rev = inl(0x2084) >> 16;
} else {
rev = inl(0x405c) >> 16;
}
lcd_busy_mask = 0x8000;
lcd_width = IPOD_STD_LCD_WIDTH;
lcd_height = IPOD_STD_LCD_HEIGHT;
if (rev > 3) {
lcd_base = IPOD_PP5020_LCD_BASE;
ipod_rtc_reg = IPOD_PP5020_RTC;
} else {
lcd_base = IPOD_PP5002_LCD_BASE;
ipod_rtc_reg = IPOD_PP5002_RTC;
}
switch (rev) {
case HW_REV_MINI:
case HW_REV_MINI_2:
lcd_width = IPOD_MINI_LCD_WIDTH;
lcd_height = IPOD_MINI_LCD_HEIGHT;
break;
case HW_REV_4G:
break;
case HW_REV_PHOTO:
lcd_width = IPOD_PHOTO_LCD_WIDTH;
lcd_height = IPOD_PHOTO_LCD_HEIGHT;
lcd_busy_mask = 0x80000000;
break;
case HW_REV_NANO:
lcd_width = IPOD_NANO_LCD_WIDTH;
lcd_height = IPOD_NANO_LCD_HEIGHT;
lcd_busy_mask = 0x80000000;
break;
}
ipod_ver = rev;
}
/* get current usec counter */
int
timer_get_current()
{
return inl(ipod_rtc_reg);
}
/* check if number of seconds has past */
int
timer_check(int clock_start, int usecs)
{
if ((inl(ipod_rtc_reg) - clock_start) >= usecs) {
return 1;
} else {
return 0;
}
}
/* wait for r0 useconds */
int
wait_usec(int usecs)
{
int start = inl(ipod_rtc_reg);
while (timer_check(start, usecs) == 0) {
// empty
}
return 0;
}
/* wait for LCD with timeout */
void
lcd_wait_write()
{
if ((inl(lcd_base) & lcd_busy_mask) != 0) {
int start = timer_get_current();
do {
if ((inl(lcd_base) & lcd_busy_mask) == 0) break;
} while (timer_check(start, 1000) == 0);
}
}
/* send LCD data */
void
lcd_send_data(int data_lo, int data_hi)
{
lcd_wait_write();
if (ipod_ver == HW_REV_MINI_2) {
outl((inl(0x70003000) & ~0x1F00000) | 0x1700000, 0x70003000);
outl(data_hi | (data_lo << 8) | 0x760000, 0x70003008);
}
else {
outl(data_lo, lcd_base + LCD_DATA);
lcd_wait_write();
outl(data_hi, lcd_base + LCD_DATA);
}
}
/* send LCD command */
void
lcd_prepare_cmd(int cmd)
{
lcd_wait_write();
if (ipod_ver == HW_REV_MINI_2) {
outl((inl(0x70003000) & ~0x1F00000) | 0x1700000, 0x70003000);
outl(cmd | 0x740000, 0x70003008);
}
else {
outl(0x0, lcd_base + LCD_CMD);
lcd_wait_write();
outl(cmd, lcd_base + LCD_CMD);
}
}
/* send LCD command and data */
void
lcd_cmd_and_data(int cmd, int data_lo, int data_hi)
{
lcd_prepare_cmd(cmd);
lcd_send_data(data_lo, data_hi);
}
static unsigned char patterns[] = {
0x00,
0x03,
0x0c,
0x0f,
0x30,
0x33,
0x3c,
0x3f,
0xc0,
0xc3,
0xcc,
0xcf,
0xf0,
0xf3,
0xfc,
0xff
};
void
display_image(img *img, int draw_bg)
{
unsigned int height_off_diff, width_off_diff, vert_space;
unsigned short cursor_pos;
unsigned char r7;
/* b&w models only */
switch (ipod_ver) {
case 1:
case 2:
case 3:
case HW_REV_MINI:
case HW_REV_MINI_2:
case HW_REV_4G:
break;
default:
return;
}
if ( img == 0x0 ) return;
height_off_diff = img->height - img->offy;
width_off_diff = img->width - img->offx;
// center the image vertically
vert_space = (lcd_height/2) - (height_off_diff / 2);
for ( cursor_pos = 0; vert_space > cursor_pos; cursor_pos = (cursor_pos + 1) & 0xff ) {
int bg = 0;
unsigned char r6;
// move the cursor
lcd_cmd_and_data(0x11, (cursor_pos << 5) >> 8, (cursor_pos << 5) & 0xff);
// setup for print command
lcd_prepare_cmd(0x12);
// use a background pattern?
if ( draw_bg != 0 ) {
// background pattern
if ( cursor_pos & 1 ) {
bg = 0x33;
} else {
bg = 0xcc;
}
}
/* print out line line of background */
for ( r6 = 0; r6 < lcd_width; r6 += 8 ) {
// display background character
lcd_send_data(bg, bg);
}
}
/* top half background is now drawn/cleared */
cursor_pos = (vert_space << 5) & 0xffff;
for ( r7 = 0; r7 < height_off_diff; r7++ ) {
int bg = 0;
unsigned char *img_data;
unsigned char r6;
// move the cursor
lcd_cmd_and_data(0x11, cursor_pos >> 8, cursor_pos & 0xff);
// setup for printing
lcd_prepare_cmd(0x12);
// use a background pattern?
if ( draw_bg != 0 ) {
// background pattern
if ( r7 & 1 ) {
bg = 0x33;
} else {
bg = 0xcc;
}
}
for ( r6 = 0; r6 < (lcd_width/2) - (width_off_diff / 2); r6 += 8 ) {
// display background character
lcd_send_data(bg, bg);
}
// cursor pos * image data width
img_data = &img->data[r7 * img->data_width];
for ( r6 = 0; r6 < ((width_off_diff + 7) / 8); r6++ ) {
if ( img->img_type == 1 ) {
// display a character
lcd_cmd_and_data(0x12, patterns[*img_data >> 4], patterns[*img_data & 0xf]);
img_data++;
}
else if ( img->img_type == 2 ) {
// display a character
lcd_cmd_and_data(0x12, *img_data, *(img_data + 1));
img_data += 2;
}
}
for ( r6 = (lcd_width/2) + (width_off_diff / 2); r6 <= lcd_width; r6 += 8 ) {
// display background character
lcd_send_data(bg, bg);
}
// update cursor pos counter
cursor_pos += 32;
}
/* image is drawn */
/* background the bottom half */
for ( cursor_pos = 64 + (height_off_diff / 2); cursor_pos <= lcd_height; cursor_pos++ ) {
int bg = 0;
unsigned char r6;
// move the cursor
lcd_cmd_and_data(0x11, (cursor_pos << 5) >> 8, (cursor_pos << 5) & 0xff);
// setup for printing
lcd_prepare_cmd(0x12);
// use a background pattern?
if ( draw_bg != 0 ) {
// background pattern
if ( cursor_pos & 1 ) {
bg = 0x33;
} else {
bg = 0xcc;
}
}
/* print out a line of background */
for ( r6 = 0; r6 <= lcd_width; r6 += 8 ) {
// display background character
lcd_send_data(bg, bg);
}
}
lcd_cmd_and_data(0x11, 0, 0);
lcd_send_data(0xff, 0xff);
wait_usec(15);
}
static void
ser_opto_keypad_cfg(int val)
{
int start_time;
outl(inl(0x6000d004) & ~0x80, 0x6000d004);
outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
outl(val, 0x7000c120);
outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
outl(inl(0x6000d024) & ~0x10, 0x6000d024);
outl(inl(0x6000d014) | 0x10, 0x6000d014);
start_time = timer_get_current();
do {
if ((inl(0x7000c104) & 0x80000000) == 0) {
break;
}
} while (timer_check(start_time, 1500) != 0);
outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
outl(inl(0x6000d004) | 0x80, 0x6000d004);
outl(inl(0x6000d024) | 0x10, 0x6000d024);
outl(inl(0x6000d014) & ~0x10, 0x6000d014);
outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
}
int
opto_keypad_read()
{
int loop_cnt, had_io = 0;
for (loop_cnt = 5; loop_cnt != 0;)
{
int key_pressed = 0, start_time, key_pad_val;
ser_opto_keypad_cfg(0x8000023a);
start_time = timer_get_current();
do {
if (inl(0x7000c104) & 0x4000000) {
had_io = 1;
break;
}
if (had_io != 0) {
break;
}
} while (timer_check(start_time, 1500) != 0);
key_pad_val = inl(0x7000c140);
if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
loop_cnt--;
} else {
key_pad_val = (key_pad_val << 11) >> 27;
key_pressed = 1;
}
outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
if (key_pressed != 0) {
return key_pad_val ^ 0x1f;
}
}
return 0;
}