blob: 346406eaad41195415756977789cbf1450c3dbb9 [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>
wdenkc6097192002-11-03 00:24:07 +000027#include <malloc.h>
28#include <s3c2400.h>
wdenk57b2d802003-06-27 21:31:46 +000029#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000030
Wolfgang Denk6405a152006-03-31 18:32:53 +020031DECLARE_GLOBAL_DATA_PTR;
wdenkc6097192002-11-03 00:24:07 +000032
wdenk1fe2c702003-03-06 21:55:29 +000033#ifdef CFG_BRIGHTNESS
34static void spi_init(void);
35static void wait_transmit_done(void);
wdenk26991552003-05-20 20:49:01 +000036static void tsc2000_write(unsigned int page, unsigned int reg,
wdenk1fe2c702003-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{
wdenkc6097192002-11-03 00:24:07 +000055 int i;
wdenke95b61c2002-11-04 16:02:40 +000056 int delay = usec * 3;
wdenkc6097192002-11-03 00:24:07 +000057
wdenk767fbd42004-10-10 18:41:04 +000058 for (i = 0; i < delay; i ++) gd->bd->bi_arch_number = MACH_TYPE_TRAB;
wdenkc6097192002-11-03 00:24:07 +000059}
60#endif /* CONFIG_MODEM_SUPPORT */
61
62/*
63 * Miscellaneous platform dependent initialisations
64 */
65
66int board_init ()
67{
wdenk4fc95692003-02-28 00:49:47 +000068#if defined(CONFIG_VFD)
69 extern int vfd_init_clocks(void);
wdenk9dd2b882002-12-03 21:28:10 +000070#endif
wdenk7539dea2003-06-19 23:01:32 +000071 S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
72 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
wdenkc6097192002-11-03 00:24:07 +000073
74 /* memory and cpu-speed are setup before relocation */
75#ifdef CONFIG_TRAB_50MHZ
76 /* change the clock to be 50 MHz 1:1:1 */
77 /* MDIV:0x5c PDIV:4 SDIV:2 */
wdenk7539dea2003-06-19 23:01:32 +000078 clk_power->MPLLCON = 0x5c042;
79 clk_power->CLKDIVN = 0;
wdenkc6097192002-11-03 00:24:07 +000080#else
81 /* change the clock to be 133 MHz 1:2:4 */
82 /* MDIV:0x7d PDIV:4 SDIV:1 */
wdenk7539dea2003-06-19 23:01:32 +000083 clk_power->MPLLCON = 0x7d041;
84 clk_power->CLKDIVN = 3;
wdenkc6097192002-11-03 00:24:07 +000085#endif
86
87 /* set up the I/O ports */
wdenk7539dea2003-06-19 23:01:32 +000088 gpio->PACON = 0x3ffff;
89 gpio->PBCON = 0xaaaaaaaa;
90 gpio->PBUP = 0xffff;
wdenkc6097192002-11-03 00:24:07 +000091 /* INPUT nCTS0 nRTS0 TXD[1] TXD[0] RXD[1] RXD[0] */
92 /* 00, 10, 10, 10, 10, 10, 10 */
wdenk7539dea2003-06-19 23:01:32 +000093 gpio->PFCON = (2<<0) | (2<<2) | (2<<4) | (2<<6) | (2<<8) | (2<<10);
wdenkc6097192002-11-03 00:24:07 +000094#ifdef CONFIG_HWFLOW
95 /* do not pull up RXD0, RXD1, TXD0, TXD1, CTS0, RTS0 */
wdenk7539dea2003-06-19 23:01:32 +000096 gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5);
wdenkc6097192002-11-03 00:24:07 +000097#else
98 /* do not pull up RXD0, RXD1, TXD0, TXD1 */
wdenk7539dea2003-06-19 23:01:32 +000099 gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3);
wdenkc6097192002-11-03 00:24:07 +0000100#endif
wdenk7539dea2003-06-19 23:01:32 +0000101 gpio->PGCON = 0x0;
102 gpio->PGUP = 0x0;
103 gpio->OPENCR= 0x0;
wdenkc6097192002-11-03 00:24:07 +0000104
wdenkde887eb2003-09-10 18:20:28 +0000105 /* suppress flicker of the VFDs */
106 gpio->MISCCR = 0x40;
107 gpio->PFCON |= (2<<12);
108
wdenk767fbd42004-10-10 18:41:04 +0000109 gd->bd->bi_arch_number = MACH_TYPE_TRAB;
wdenkc6097192002-11-03 00:24:07 +0000110
111 /* adress of boot parameters */
112 gd->bd->bi_boot_params = 0x0c000100;
113
wdenk1fe2c702003-03-06 21:55:29 +0000114 /* Make sure both buzzers are turned off */
wdenk7539dea2003-06-19 23:01:32 +0000115 gpio->PDCON |= 0x5400;
116 gpio->PDDAT &= ~0xE0;
wdenk1fe2c702003-03-06 21:55:29 +0000117
wdenk9dd2b882002-12-03 21:28:10 +0000118#ifdef CONFIG_VFD
wdenk4fc95692003-02-28 00:49:47 +0000119 vfd_init_clocks();
wdenk9dd2b882002-12-03 21:28:10 +0000120#endif /* CONFIG_VFD */
wdenkc6097192002-11-03 00:24:07 +0000121
wdenk4fc95692003-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
wdenkca9bc762003-07-15 07:45:49 +0000131#ifdef CONFIG_DRIVER_S3C24X0_I2C
132 /* Configure I/O ports PG5 und PG6 for I2C */
133 gpio->PGCON = (gpio->PGCON & 0x003c00) | 0x003c00;
134#endif /* CONFIG_DRIVER_S3C24X0_I2C */
135
wdenkc6097192002-11-03 00:24:07 +0000136 return 0;
137}
138
139int dram_init (void)
140{
wdenkc6097192002-11-03 00:24:07 +0000141 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
142 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
143 return 0;
144}
145
146/*-----------------------------------------------------------------------
147 * Keyboard Controller
148 */
149
150/* Maximum key number */
151#define KEYBD_KEY_NUM 4
152
153#define KBD_DATA (((*(volatile ulong *)0x04020000) >> 16) & 0xF)
154
155static uchar *key_match (ulong);
156
157int misc_init_r (void)
158{
159 ulong kbd_data = KBD_DATA;
160 uchar keybd_env[KEYBD_KEY_NUM + 1];
161 uchar *str;
162 int i;
163
Wolfgang Denkfe287cb2006-06-16 15:56:12 +0200164#ifdef CONFIG_VERSION_VARIABLE
165 {
166 /* Set version variable. Please note, that this variable is
167 * also set in main_loop() later in the boot process. The
168 * version variable has to be set this early, because so it
169 * could be used in script files on an usb stick, which
170 * might be called during do_auto_update() */
171 extern char version_string[];
172
173 setenv ("ver", version_string);
174 }
175#endif /* CONFIG_VERSION_VARIABLE */
176
wdenkf6a6ac12003-09-17 15:10:32 +0000177#ifdef CONFIG_AUTO_UPDATE
178 extern int do_auto_update(void);
179 /* this has priority over all else */
180 do_auto_update();
181#endif
182
wdenkc6097192002-11-03 00:24:07 +0000183 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
184 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
185 }
186 keybd_env[i] = '\0';
187 debug ("** Setting keybd=\"%s\"\n", keybd_env);
188 setenv ("keybd", keybd_env);
189
190 str = strdup (key_match (kbd_data)); /* decode keys */
191
192#ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
193 debug ("** Setting preboot=\"%s\"\n", str);
194 setenv ("preboot", str); /* set or delete definition */
195#endif /* CONFIG_PREBOOT */
196 if (str != NULL) {
197 free (str);
198 }
199
wdenk1fe2c702003-03-06 21:55:29 +0000200#ifdef CFG_BRIGHTNESS
201 tsc2000_set_brightness();
202#endif
wdenkc6097192002-11-03 00:24:07 +0000203 return (0);
204}
205
206#ifdef CONFIG_PREBOOT
207
208static uchar kbd_magic_prefix[] = "key_magic";
209static uchar kbd_command_prefix[] = "key_cmd";
210
211static int compare_magic (ulong kbd_data, uchar *str)
212{
213 uchar key_mask;
214
215 debug ("compare_magic: kbd: %04lx str: \"%s\"\n",kbd_data,str);
216 for (; *str; str++)
217 {
218 uchar c = *str - '1';
219
220 if (c >= KEYBD_KEY_NUM) /* bad key number */
221 return -1;
222
223 key_mask = 1 << c;
224
225 if (!(kbd_data & key_mask)) { /* key not pressed */
226 debug ( "compare_magic: "
227 "kbd: %04lx mask: %04lx - key not pressed\n",
228 kbd_data, key_mask );
229 return -1;
230 }
231
232 kbd_data &= ~key_mask;
233 }
234
235 if (kbd_data) { /* key(s) not released */
236 debug ( "compare_magic: "
237 "kbd: %04lx - key(s) not released\n", kbd_data);
238 return -1;
239 }
240
241 return 0;
242}
243
244/*-----------------------------------------------------------------------
245 * Check if pressed key(s) match magic sequence,
246 * and return the command string associated with that key(s).
247 *
248 * If no key press was decoded, NULL is returned.
249 *
250 * Note: the first character of the argument will be overwritten with
251 * the "magic charcter code" of the decoded key(s), or '\0'.
252 *
253 *
254 * Note: the string points to static environment data and must be
255 * saved before you call any function that modifies the environment.
256 */
257static uchar *key_match (ulong kbd_data)
258{
259 uchar magic[sizeof (kbd_magic_prefix) + 1];
260 uchar cmd_name[sizeof (kbd_command_prefix) + 1];
261 uchar *suffix;
262 uchar *kbd_magic_keys;
263
264 /*
265 * The following string defines the characters that can pe appended
266 * to "key_magic" to form the names of environment variables that
267 * hold "magic" key codes, i. e. such key codes that can cause
268 * pre-boot actions. If the string is empty (""), then only
269 * "key_magic" is checked (old behaviour); the string "125" causes
270 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
271 */
272 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
273 kbd_magic_keys = "";
274
275 debug ("key_match: magic_keys=\"%s\"\n", kbd_magic_keys);
276
277 /* loop over all magic keys;
278 * use '\0' suffix in case of empty string
279 */
280 for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix)
281 {
282 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
283
284 debug ("key_match: magic=\"%s\"\n",
285 getenv(magic) ? getenv(magic) : "<UNDEFINED>");
286
287 if (compare_magic(kbd_data, getenv(magic)) == 0)
288 {
289 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
290 debug ("key_match: cmdname %s=\"%s\"\n",
291 cmd_name,
292 getenv (cmd_name) ?
293 getenv (cmd_name) :
294 "<UNDEFINED>");
295 return (getenv (cmd_name));
296 }
297 }
298 debug ("key_match: no match\n");
299 return (NULL);
300}
301#endif /* CONFIG_PREBOOT */
302
303/* Read Keyboard status */
304int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
305{
306 ulong kbd_data = KBD_DATA;
307 uchar keybd_env[KEYBD_KEY_NUM + 1];
308 int i;
309
310 puts ("Keys:");
311 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
312 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
313 printf (" %c", keybd_env[i]);
314 }
315 keybd_env[i] = '\0';
316 putc ('\n');
317 setenv ("keybd", keybd_env);
318 return 0;
319}
320
wdenkf287a242003-07-01 21:06:45 +0000321U_BOOT_CMD(
322 kbd, 1, 1, do_kbd,
wdenk57b2d802003-06-27 21:31:46 +0000323 "kbd - read keyboard status\n",
324 NULL
325);
326
wdenkc6097192002-11-03 00:24:07 +0000327#ifdef CONFIG_MODEM_SUPPORT
328static int key_pressed(void)
329{
330 return (compare_magic(KBD_DATA, CONFIG_MODEM_KEY_MAGIC) == 0);
331}
332#endif /* CONFIG_MODEM_SUPPORT */
wdenk1fe2c702003-03-06 21:55:29 +0000333
334#ifdef CFG_BRIGHTNESS
335
wdenk7539dea2003-06-19 23:01:32 +0000336static inline void SET_CS_TOUCH(void)
337{
338 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
339
340 gpio->PDDAT &= 0x5FF;
341}
342
343static inline void CLR_CS_TOUCH(void)
344{
345 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
346
347 gpio->PDDAT |= 0x200;
348}
wdenk1fe2c702003-03-06 21:55:29 +0000349
350static void spi_init(void)
351{
wdenk7539dea2003-06-19 23:01:32 +0000352 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
353 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
wdenk1fe2c702003-03-06 21:55:29 +0000354 int i;
355
356 /* Configure I/O ports. */
wdenk7539dea2003-06-19 23:01:32 +0000357 gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
358 gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
359 gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
360 gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
wdenk1fe2c702003-03-06 21:55:29 +0000361
wdenk7539dea2003-06-19 23:01:32 +0000362 CLR_CS_TOUCH();
wdenk1fe2c702003-03-06 21:55:29 +0000363
wdenk7539dea2003-06-19 23:01:32 +0000364 spi->ch[0].SPPRE = 0x1F; /* Baudrate ca. 514kHz */
365 spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
366 spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0, CPHA=1 */
wdenk1fe2c702003-03-06 21:55:29 +0000367
368 /* Dummy byte ensures clock to be low. */
369 for (i = 0; i < 10; i++) {
wdenk7539dea2003-06-19 23:01:32 +0000370 spi->ch[0].SPTDAT = 0xFF;
wdenk1fe2c702003-03-06 21:55:29 +0000371 }
wdenk26991552003-05-20 20:49:01 +0000372 wait_transmit_done();
wdenk1fe2c702003-03-06 21:55:29 +0000373}
374
375static void wait_transmit_done(void)
376{
wdenk7539dea2003-06-19 23:01:32 +0000377 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
378
379 while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
wdenk1fe2c702003-03-06 21:55:29 +0000380}
381
wdenk26991552003-05-20 20:49:01 +0000382static void tsc2000_write(unsigned int page, unsigned int reg,
wdenk1fe2c702003-03-06 21:55:29 +0000383 unsigned int data)
384{
wdenk7539dea2003-06-19 23:01:32 +0000385 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
wdenk1fe2c702003-03-06 21:55:29 +0000386 unsigned int command;
387
wdenk7539dea2003-06-19 23:01:32 +0000388 SET_CS_TOUCH();
wdenk1fe2c702003-03-06 21:55:29 +0000389 command = 0x0000;
390 command |= (page << 11);
391 command |= (reg << 5);
392
wdenk7539dea2003-06-19 23:01:32 +0000393 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
wdenk1fe2c702003-03-06 21:55:29 +0000394 wait_transmit_done();
wdenk7539dea2003-06-19 23:01:32 +0000395 spi->ch[0].SPTDAT = (command & 0x00FF);
wdenk1fe2c702003-03-06 21:55:29 +0000396 wait_transmit_done();
wdenk7539dea2003-06-19 23:01:32 +0000397 spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
wdenk1fe2c702003-03-06 21:55:29 +0000398 wait_transmit_done();
wdenk7539dea2003-06-19 23:01:32 +0000399 spi->ch[0].SPTDAT = (data & 0x00FF);
wdenk1fe2c702003-03-06 21:55:29 +0000400 wait_transmit_done();
401
wdenk7539dea2003-06-19 23:01:32 +0000402 CLR_CS_TOUCH();
wdenk1fe2c702003-03-06 21:55:29 +0000403}
404
405static void tsc2000_set_brightness(void)
406{
407 uchar tmp[10];
408 int i, br;
409
410 spi_init();
411 tsc2000_write(1, 2, 0x0); /* Power up DAC */
412
413 i = getenv_r("brightness", tmp, sizeof(tmp));
414 br = (i > 0)
415 ? (int) simple_strtoul (tmp, NULL, 10)
416 : CFG_BRIGHTNESS;
417
418 tsc2000_write(0, 0xb, br & 0xff);
419}
420#endif