blob: 6c9971f3b60046acdbdcf33334c3115004170004 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/* #define DEBUG */
25
26#include <common.h>
27#include <cmd_bsp.h>
28#include <malloc.h>
29#include <s3c2400.h>
30
31/* ------------------------------------------------------------------------- */
32
wdenk1cb8e982003-03-06 21:55:29 +000033#ifdef CFG_BRIGHTNESS
34static void spi_init(void);
35static void wait_transmit_done(void);
wdenk82226bf2003-05-20 20:49:01 +000036static void tsc2000_write(unsigned int page, unsigned int reg,
wdenk1cb8e982003-03-06 21:55:29 +000037 unsigned int data);
38static void tsc2000_set_brightness(void);
39#endif
wdenkc6097192002-11-03 00:24:07 +000040#ifdef CONFIG_MODEM_SUPPORT
41static int key_pressed(void);
42extern void disable_putc(void);
43extern int do_mdm_init; /* defined in common/main.c */
44
45/*
46 * We need a delay of at least 500 us after turning on the VFD clock
47 * before we can read any useful information for the CPLD controlling
48 * the keyboard switches. Let's play safe and wait 5 ms. The problem
49 * is that timers are not available yet, so we use a manually timed
50 * loop.
51 */
wdenke95b61c2002-11-04 16:02:40 +000052#define KBD_MDELAY 5000
53static void udelay_no_timer (int usec)
wdenkc6097192002-11-03 00:24:07 +000054{
55 DECLARE_GLOBAL_DATA_PTR;
56
57 int i;
wdenke95b61c2002-11-04 16:02:40 +000058 int delay = usec * 3;
wdenkc6097192002-11-03 00:24:07 +000059
60 for (i = 0; i < delay; i ++) gd->bd->bi_arch_number = 145;
61}
62#endif /* CONFIG_MODEM_SUPPORT */
63
64/*
65 * Miscellaneous platform dependent initialisations
66 */
67
68int board_init ()
69{
wdenk6069ff22003-02-28 00:49:47 +000070#if defined(CONFIG_VFD)
71 extern int vfd_init_clocks(void);
wdenka6c7ad22002-12-03 21:28:10 +000072#endif
wdenkc6097192002-11-03 00:24:07 +000073 DECLARE_GLOBAL_DATA_PTR;
74
75 /* memory and cpu-speed are setup before relocation */
76#ifdef CONFIG_TRAB_50MHZ
77 /* change the clock to be 50 MHz 1:1:1 */
78 /* MDIV:0x5c PDIV:4 SDIV:2 */
79 rMPLLCON = 0x5c042;
80 rCLKDIVN = 0;
81#else
82 /* change the clock to be 133 MHz 1:2:4 */
83 /* MDIV:0x7d PDIV:4 SDIV:1 */
84 rMPLLCON = 0x7d041;
85 rCLKDIVN = 3;
86#endif
87
88 /* set up the I/O ports */
89 rPACON = 0x3ffff;
90 rPBCON = 0xaaaaaaaa;
91 rPBUP = 0xffff;
92 /* INPUT nCTS0 nRTS0 TXD[1] TXD[0] RXD[1] RXD[0] */
93 /* 00, 10, 10, 10, 10, 10, 10 */
94 rPFCON = (2<<0) | (2<<2) | (2<<4) | (2<<6) | (2<<8) | (2<<10);
95#ifdef CONFIG_HWFLOW
96 /* do not pull up RXD0, RXD1, TXD0, TXD1, CTS0, RTS0 */
97 rPFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5);
98#else
99 /* do not pull up RXD0, RXD1, TXD0, TXD1 */
100 rPFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3);
101#endif
102 rPGCON = 0x0;
103 rPGUP = 0x0;
104 rOPENCR= 0x0;
105
106 /* arch number of SAMSUNG-Board */
107 /* MACH_TYPE_SMDK2400 */
108 /* XXX this isn't really correct, but keep it for now */
109 gd->bd->bi_arch_number = 145;
110
111 /* adress of boot parameters */
112 gd->bd->bi_boot_params = 0x0c000100;
113
wdenk1cb8e982003-03-06 21:55:29 +0000114 /* Make sure both buzzers are turned off */
115 rPDCON |= 0x5400;
116 rPDDAT &= ~0xE0;
117
wdenka6c7ad22002-12-03 21:28:10 +0000118#ifdef CONFIG_VFD
wdenk6069ff22003-02-28 00:49:47 +0000119 vfd_init_clocks();
wdenka6c7ad22002-12-03 21:28:10 +0000120#endif /* CONFIG_VFD */
wdenkc6097192002-11-03 00:24:07 +0000121
wdenk6069ff22003-02-28 00:49:47 +0000122#ifdef CONFIG_MODEM_SUPPORT
wdenke95b61c2002-11-04 16:02:40 +0000123 udelay_no_timer (KBD_MDELAY);
wdenkc6097192002-11-03 00:24:07 +0000124
125 if (key_pressed()) {
126 disable_putc(); /* modem doesn't understand banner etc */
127 do_mdm_init = 1;
128 }
129#endif /* CONFIG_MODEM_SUPPORT */
130
131 return 0;
132}
133
134int dram_init (void)
135{
136 DECLARE_GLOBAL_DATA_PTR;
137
138 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
139 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
140 return 0;
141}
142
143/*-----------------------------------------------------------------------
144 * Keyboard Controller
145 */
146
147/* Maximum key number */
148#define KEYBD_KEY_NUM 4
149
150#define KBD_DATA (((*(volatile ulong *)0x04020000) >> 16) & 0xF)
151
152static uchar *key_match (ulong);
153
154int misc_init_r (void)
155{
156 ulong kbd_data = KBD_DATA;
157 uchar keybd_env[KEYBD_KEY_NUM + 1];
158 uchar *str;
159 int i;
160
161 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
162 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
163 }
164 keybd_env[i] = '\0';
165 debug ("** Setting keybd=\"%s\"\n", keybd_env);
166 setenv ("keybd", keybd_env);
167
168 str = strdup (key_match (kbd_data)); /* decode keys */
169
170#ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
171 debug ("** Setting preboot=\"%s\"\n", str);
172 setenv ("preboot", str); /* set or delete definition */
173#endif /* CONFIG_PREBOOT */
174 if (str != NULL) {
175 free (str);
176 }
177
wdenk1cb8e982003-03-06 21:55:29 +0000178#ifdef CFG_BRIGHTNESS
179 tsc2000_set_brightness();
180#endif
wdenkc6097192002-11-03 00:24:07 +0000181 return (0);
182}
183
184#ifdef CONFIG_PREBOOT
185
186static uchar kbd_magic_prefix[] = "key_magic";
187static uchar kbd_command_prefix[] = "key_cmd";
188
189static int compare_magic (ulong kbd_data, uchar *str)
190{
191 uchar key_mask;
192
193 debug ("compare_magic: kbd: %04lx str: \"%s\"\n",kbd_data,str);
194 for (; *str; str++)
195 {
196 uchar c = *str - '1';
197
198 if (c >= KEYBD_KEY_NUM) /* bad key number */
199 return -1;
200
201 key_mask = 1 << c;
202
203 if (!(kbd_data & key_mask)) { /* key not pressed */
204 debug ( "compare_magic: "
205 "kbd: %04lx mask: %04lx - key not pressed\n",
206 kbd_data, key_mask );
207 return -1;
208 }
209
210 kbd_data &= ~key_mask;
211 }
212
213 if (kbd_data) { /* key(s) not released */
214 debug ( "compare_magic: "
215 "kbd: %04lx - key(s) not released\n", kbd_data);
216 return -1;
217 }
218
219 return 0;
220}
221
222/*-----------------------------------------------------------------------
223 * Check if pressed key(s) match magic sequence,
224 * and return the command string associated with that key(s).
225 *
226 * If no key press was decoded, NULL is returned.
227 *
228 * Note: the first character of the argument will be overwritten with
229 * the "magic charcter code" of the decoded key(s), or '\0'.
230 *
231 *
232 * Note: the string points to static environment data and must be
233 * saved before you call any function that modifies the environment.
234 */
235static uchar *key_match (ulong kbd_data)
236{
237 uchar magic[sizeof (kbd_magic_prefix) + 1];
238 uchar cmd_name[sizeof (kbd_command_prefix) + 1];
239 uchar *suffix;
240 uchar *kbd_magic_keys;
241
242 /*
243 * The following string defines the characters that can pe appended
244 * to "key_magic" to form the names of environment variables that
245 * hold "magic" key codes, i. e. such key codes that can cause
246 * pre-boot actions. If the string is empty (""), then only
247 * "key_magic" is checked (old behaviour); the string "125" causes
248 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
249 */
250 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
251 kbd_magic_keys = "";
252
253 debug ("key_match: magic_keys=\"%s\"\n", kbd_magic_keys);
254
255 /* loop over all magic keys;
256 * use '\0' suffix in case of empty string
257 */
258 for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix)
259 {
260 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
261
262 debug ("key_match: magic=\"%s\"\n",
263 getenv(magic) ? getenv(magic) : "<UNDEFINED>");
264
265 if (compare_magic(kbd_data, getenv(magic)) == 0)
266 {
267 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
268 debug ("key_match: cmdname %s=\"%s\"\n",
269 cmd_name,
270 getenv (cmd_name) ?
271 getenv (cmd_name) :
272 "<UNDEFINED>");
273 return (getenv (cmd_name));
274 }
275 }
276 debug ("key_match: no match\n");
277 return (NULL);
278}
279#endif /* CONFIG_PREBOOT */
280
281/* Read Keyboard status */
282int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
283{
284 ulong kbd_data = KBD_DATA;
285 uchar keybd_env[KEYBD_KEY_NUM + 1];
286 int i;
287
288 puts ("Keys:");
289 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
290 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
291 printf (" %c", keybd_env[i]);
292 }
293 keybd_env[i] = '\0';
294 putc ('\n');
295 setenv ("keybd", keybd_env);
296 return 0;
297}
298
299#ifdef CONFIG_MODEM_SUPPORT
300static int key_pressed(void)
301{
302 return (compare_magic(KBD_DATA, CONFIG_MODEM_KEY_MAGIC) == 0);
303}
304#endif /* CONFIG_MODEM_SUPPORT */
wdenk1cb8e982003-03-06 21:55:29 +0000305
306#ifdef CFG_BRIGHTNESS
307
308#define SET_CS_TOUCH (rPDDAT &= 0x5FF)
309#define CLR_CS_TOUCH (rPDDAT |= 0x200)
310
311static void spi_init(void)
312{
313 int i;
314
315 /* Configure I/O ports. */
316 rPDCON = (rPDCON & 0xF3FFFF) | 0x040000;
317 rPGCON = (rPGCON & 0x0F3FFF) | 0x008000;
318 rPGCON = (rPGCON & 0x0CFFFF) | 0x020000;
319 rPGCON = (rPGCON & 0x03FFFF) | 0x080000;
320
321 CLR_CS_TOUCH;
322
323 rSPPRE = 0x1F; /* Baudrate ca. 514kHz */
324 rSPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
325 rSPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0, CPHA=1 */
326
327 /* Dummy byte ensures clock to be low. */
328 for (i = 0; i < 10; i++) {
329 rSPTDAT = 0xFF;
330 }
wdenk82226bf2003-05-20 20:49:01 +0000331 wait_transmit_done();
wdenk1cb8e982003-03-06 21:55:29 +0000332}
333
334static void wait_transmit_done(void)
335{
336 while (!(rSPSTA & 0x01)); /* wait until transfer is done */
337}
338
wdenk82226bf2003-05-20 20:49:01 +0000339static void tsc2000_write(unsigned int page, unsigned int reg,
wdenk1cb8e982003-03-06 21:55:29 +0000340 unsigned int data)
341{
342 unsigned int command;
343
344 SET_CS_TOUCH;
345 command = 0x0000;
346 command |= (page << 11);
347 command |= (reg << 5);
348
349 rSPTDAT = (command & 0xFF00) >> 8;
350 wait_transmit_done();
351 rSPTDAT = (command & 0x00FF);
352 wait_transmit_done();
353 rSPTDAT = (data & 0xFF00) >> 8;
354 wait_transmit_done();
355 rSPTDAT = (data & 0x00FF);
356 wait_transmit_done();
357
358 CLR_CS_TOUCH;
359}
360
361static void tsc2000_set_brightness(void)
362{
363 uchar tmp[10];
364 int i, br;
365
366 spi_init();
367 tsc2000_write(1, 2, 0x0); /* Power up DAC */
368
369 i = getenv_r("brightness", tmp, sizeof(tmp));
370 br = (i > 0)
371 ? (int) simple_strtoul (tmp, NULL, 10)
372 : CFG_BRIGHTNESS;
373
374 tsc2000_write(0, 0xb, br & 0xff);
375}
376#endif