blob: e8dfd2ceb0b1476dbff743506865e03b885bc664 [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>
wdenk8bde7f72003-06-27 21:31:46 +000029#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000030
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
wdenk731215e2004-10-10 18:41:04 +000060 for (i = 0; i < delay; i ++) gd->bd->bi_arch_number = MACH_TYPE_TRAB;
wdenkc6097192002-11-03 00:24:07 +000061}
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;
wdenk48b42612003-06-19 23:01:32 +000074 S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
75 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
wdenkc6097192002-11-03 00:24:07 +000076
77 /* memory and cpu-speed are setup before relocation */
78#ifdef CONFIG_TRAB_50MHZ
79 /* change the clock to be 50 MHz 1:1:1 */
80 /* MDIV:0x5c PDIV:4 SDIV:2 */
wdenk48b42612003-06-19 23:01:32 +000081 clk_power->MPLLCON = 0x5c042;
82 clk_power->CLKDIVN = 0;
wdenkc6097192002-11-03 00:24:07 +000083#else
84 /* change the clock to be 133 MHz 1:2:4 */
85 /* MDIV:0x7d PDIV:4 SDIV:1 */
wdenk48b42612003-06-19 23:01:32 +000086 clk_power->MPLLCON = 0x7d041;
87 clk_power->CLKDIVN = 3;
wdenkc6097192002-11-03 00:24:07 +000088#endif
89
90 /* set up the I/O ports */
wdenk48b42612003-06-19 23:01:32 +000091 gpio->PACON = 0x3ffff;
92 gpio->PBCON = 0xaaaaaaaa;
93 gpio->PBUP = 0xffff;
wdenkc6097192002-11-03 00:24:07 +000094 /* INPUT nCTS0 nRTS0 TXD[1] TXD[0] RXD[1] RXD[0] */
95 /* 00, 10, 10, 10, 10, 10, 10 */
wdenk48b42612003-06-19 23:01:32 +000096 gpio->PFCON = (2<<0) | (2<<2) | (2<<4) | (2<<6) | (2<<8) | (2<<10);
wdenkc6097192002-11-03 00:24:07 +000097#ifdef CONFIG_HWFLOW
98 /* do not pull up RXD0, RXD1, TXD0, TXD1, CTS0, RTS0 */
wdenk48b42612003-06-19 23:01:32 +000099 gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5);
wdenkc6097192002-11-03 00:24:07 +0000100#else
101 /* do not pull up RXD0, RXD1, TXD0, TXD1 */
wdenk48b42612003-06-19 23:01:32 +0000102 gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3);
wdenkc6097192002-11-03 00:24:07 +0000103#endif
wdenk48b42612003-06-19 23:01:32 +0000104 gpio->PGCON = 0x0;
105 gpio->PGUP = 0x0;
106 gpio->OPENCR= 0x0;
wdenkc6097192002-11-03 00:24:07 +0000107
wdenk149dded2003-09-10 18:20:28 +0000108 /* suppress flicker of the VFDs */
109 gpio->MISCCR = 0x40;
110 gpio->PFCON |= (2<<12);
111
wdenk731215e2004-10-10 18:41:04 +0000112 gd->bd->bi_arch_number = MACH_TYPE_TRAB;
wdenkc6097192002-11-03 00:24:07 +0000113
114 /* adress of boot parameters */
115 gd->bd->bi_boot_params = 0x0c000100;
116
wdenk1cb8e982003-03-06 21:55:29 +0000117 /* Make sure both buzzers are turned off */
wdenk48b42612003-06-19 23:01:32 +0000118 gpio->PDCON |= 0x5400;
119 gpio->PDDAT &= ~0xE0;
wdenk1cb8e982003-03-06 21:55:29 +0000120
wdenka6c7ad22002-12-03 21:28:10 +0000121#ifdef CONFIG_VFD
wdenk6069ff22003-02-28 00:49:47 +0000122 vfd_init_clocks();
wdenka6c7ad22002-12-03 21:28:10 +0000123#endif /* CONFIG_VFD */
wdenkc6097192002-11-03 00:24:07 +0000124
wdenk6069ff22003-02-28 00:49:47 +0000125#ifdef CONFIG_MODEM_SUPPORT
wdenke95b61c2002-11-04 16:02:40 +0000126 udelay_no_timer (KBD_MDELAY);
wdenkc6097192002-11-03 00:24:07 +0000127
128 if (key_pressed()) {
129 disable_putc(); /* modem doesn't understand banner etc */
130 do_mdm_init = 1;
131 }
132#endif /* CONFIG_MODEM_SUPPORT */
133
wdenk6dff5522003-07-15 07:45:49 +0000134#ifdef CONFIG_DRIVER_S3C24X0_I2C
135 /* Configure I/O ports PG5 und PG6 for I2C */
136 gpio->PGCON = (gpio->PGCON & 0x003c00) | 0x003c00;
137#endif /* CONFIG_DRIVER_S3C24X0_I2C */
138
wdenkc6097192002-11-03 00:24:07 +0000139 return 0;
140}
141
142int dram_init (void)
143{
144 DECLARE_GLOBAL_DATA_PTR;
145
146 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
147 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
148 return 0;
149}
150
151/*-----------------------------------------------------------------------
152 * Keyboard Controller
153 */
154
155/* Maximum key number */
156#define KEYBD_KEY_NUM 4
157
158#define KBD_DATA (((*(volatile ulong *)0x04020000) >> 16) & 0xF)
159
160static uchar *key_match (ulong);
161
162int misc_init_r (void)
163{
164 ulong kbd_data = KBD_DATA;
165 uchar keybd_env[KEYBD_KEY_NUM + 1];
166 uchar *str;
167 int i;
168
wdenkf54ebdf2003-09-17 15:10:32 +0000169#ifdef CONFIG_AUTO_UPDATE
170 extern int do_auto_update(void);
171 /* this has priority over all else */
172 do_auto_update();
173#endif
174
wdenkc6097192002-11-03 00:24:07 +0000175 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
176 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
177 }
178 keybd_env[i] = '\0';
179 debug ("** Setting keybd=\"%s\"\n", keybd_env);
180 setenv ("keybd", keybd_env);
181
182 str = strdup (key_match (kbd_data)); /* decode keys */
183
184#ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
185 debug ("** Setting preboot=\"%s\"\n", str);
186 setenv ("preboot", str); /* set or delete definition */
187#endif /* CONFIG_PREBOOT */
188 if (str != NULL) {
189 free (str);
190 }
191
wdenk1cb8e982003-03-06 21:55:29 +0000192#ifdef CFG_BRIGHTNESS
193 tsc2000_set_brightness();
194#endif
wdenkc6097192002-11-03 00:24:07 +0000195 return (0);
196}
197
198#ifdef CONFIG_PREBOOT
199
200static uchar kbd_magic_prefix[] = "key_magic";
201static uchar kbd_command_prefix[] = "key_cmd";
202
203static int compare_magic (ulong kbd_data, uchar *str)
204{
205 uchar key_mask;
206
207 debug ("compare_magic: kbd: %04lx str: \"%s\"\n",kbd_data,str);
208 for (; *str; str++)
209 {
210 uchar c = *str - '1';
211
212 if (c >= KEYBD_KEY_NUM) /* bad key number */
213 return -1;
214
215 key_mask = 1 << c;
216
217 if (!(kbd_data & key_mask)) { /* key not pressed */
218 debug ( "compare_magic: "
219 "kbd: %04lx mask: %04lx - key not pressed\n",
220 kbd_data, key_mask );
221 return -1;
222 }
223
224 kbd_data &= ~key_mask;
225 }
226
227 if (kbd_data) { /* key(s) not released */
228 debug ( "compare_magic: "
229 "kbd: %04lx - key(s) not released\n", kbd_data);
230 return -1;
231 }
232
233 return 0;
234}
235
236/*-----------------------------------------------------------------------
237 * Check if pressed key(s) match magic sequence,
238 * and return the command string associated with that key(s).
239 *
240 * If no key press was decoded, NULL is returned.
241 *
242 * Note: the first character of the argument will be overwritten with
243 * the "magic charcter code" of the decoded key(s), or '\0'.
244 *
245 *
246 * Note: the string points to static environment data and must be
247 * saved before you call any function that modifies the environment.
248 */
249static uchar *key_match (ulong kbd_data)
250{
251 uchar magic[sizeof (kbd_magic_prefix) + 1];
252 uchar cmd_name[sizeof (kbd_command_prefix) + 1];
253 uchar *suffix;
254 uchar *kbd_magic_keys;
255
256 /*
257 * The following string defines the characters that can pe appended
258 * to "key_magic" to form the names of environment variables that
259 * hold "magic" key codes, i. e. such key codes that can cause
260 * pre-boot actions. If the string is empty (""), then only
261 * "key_magic" is checked (old behaviour); the string "125" causes
262 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
263 */
264 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
265 kbd_magic_keys = "";
266
267 debug ("key_match: magic_keys=\"%s\"\n", kbd_magic_keys);
268
269 /* loop over all magic keys;
270 * use '\0' suffix in case of empty string
271 */
272 for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix)
273 {
274 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
275
276 debug ("key_match: magic=\"%s\"\n",
277 getenv(magic) ? getenv(magic) : "<UNDEFINED>");
278
279 if (compare_magic(kbd_data, getenv(magic)) == 0)
280 {
281 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
282 debug ("key_match: cmdname %s=\"%s\"\n",
283 cmd_name,
284 getenv (cmd_name) ?
285 getenv (cmd_name) :
286 "<UNDEFINED>");
287 return (getenv (cmd_name));
288 }
289 }
290 debug ("key_match: no match\n");
291 return (NULL);
292}
293#endif /* CONFIG_PREBOOT */
294
295/* Read Keyboard status */
296int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
297{
298 ulong kbd_data = KBD_DATA;
299 uchar keybd_env[KEYBD_KEY_NUM + 1];
300 int i;
301
302 puts ("Keys:");
303 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
304 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
305 printf (" %c", keybd_env[i]);
306 }
307 keybd_env[i] = '\0';
308 putc ('\n');
309 setenv ("keybd", keybd_env);
310 return 0;
311}
312
wdenk0d498392003-07-01 21:06:45 +0000313U_BOOT_CMD(
314 kbd, 1, 1, do_kbd,
wdenk8bde7f72003-06-27 21:31:46 +0000315 "kbd - read keyboard status\n",
316 NULL
317);
318
wdenkc6097192002-11-03 00:24:07 +0000319#ifdef CONFIG_MODEM_SUPPORT
320static int key_pressed(void)
321{
322 return (compare_magic(KBD_DATA, CONFIG_MODEM_KEY_MAGIC) == 0);
323}
324#endif /* CONFIG_MODEM_SUPPORT */
wdenk1cb8e982003-03-06 21:55:29 +0000325
326#ifdef CFG_BRIGHTNESS
327
wdenk48b42612003-06-19 23:01:32 +0000328static inline void SET_CS_TOUCH(void)
329{
330 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
331
332 gpio->PDDAT &= 0x5FF;
333}
334
335static inline void CLR_CS_TOUCH(void)
336{
337 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
338
339 gpio->PDDAT |= 0x200;
340}
wdenk1cb8e982003-03-06 21:55:29 +0000341
342static void spi_init(void)
343{
wdenk48b42612003-06-19 23:01:32 +0000344 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
345 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
wdenk1cb8e982003-03-06 21:55:29 +0000346 int i;
347
348 /* Configure I/O ports. */
wdenk48b42612003-06-19 23:01:32 +0000349 gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
350 gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
351 gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
352 gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
wdenk1cb8e982003-03-06 21:55:29 +0000353
wdenk48b42612003-06-19 23:01:32 +0000354 CLR_CS_TOUCH();
wdenk1cb8e982003-03-06 21:55:29 +0000355
wdenk48b42612003-06-19 23:01:32 +0000356 spi->ch[0].SPPRE = 0x1F; /* Baudrate ca. 514kHz */
357 spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
358 spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0, CPHA=1 */
wdenk1cb8e982003-03-06 21:55:29 +0000359
360 /* Dummy byte ensures clock to be low. */
361 for (i = 0; i < 10; i++) {
wdenk48b42612003-06-19 23:01:32 +0000362 spi->ch[0].SPTDAT = 0xFF;
wdenk1cb8e982003-03-06 21:55:29 +0000363 }
wdenk82226bf2003-05-20 20:49:01 +0000364 wait_transmit_done();
wdenk1cb8e982003-03-06 21:55:29 +0000365}
366
367static void wait_transmit_done(void)
368{
wdenk48b42612003-06-19 23:01:32 +0000369 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
370
371 while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
wdenk1cb8e982003-03-06 21:55:29 +0000372}
373
wdenk82226bf2003-05-20 20:49:01 +0000374static void tsc2000_write(unsigned int page, unsigned int reg,
wdenk1cb8e982003-03-06 21:55:29 +0000375 unsigned int data)
376{
wdenk48b42612003-06-19 23:01:32 +0000377 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
wdenk1cb8e982003-03-06 21:55:29 +0000378 unsigned int command;
379
wdenk48b42612003-06-19 23:01:32 +0000380 SET_CS_TOUCH();
wdenk1cb8e982003-03-06 21:55:29 +0000381 command = 0x0000;
382 command |= (page << 11);
383 command |= (reg << 5);
384
wdenk48b42612003-06-19 23:01:32 +0000385 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
wdenk1cb8e982003-03-06 21:55:29 +0000386 wait_transmit_done();
wdenk48b42612003-06-19 23:01:32 +0000387 spi->ch[0].SPTDAT = (command & 0x00FF);
wdenk1cb8e982003-03-06 21:55:29 +0000388 wait_transmit_done();
wdenk48b42612003-06-19 23:01:32 +0000389 spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
wdenk1cb8e982003-03-06 21:55:29 +0000390 wait_transmit_done();
wdenk48b42612003-06-19 23:01:32 +0000391 spi->ch[0].SPTDAT = (data & 0x00FF);
wdenk1cb8e982003-03-06 21:55:29 +0000392 wait_transmit_done();
393
wdenk48b42612003-06-19 23:01:32 +0000394 CLR_CS_TOUCH();
wdenk1cb8e982003-03-06 21:55:29 +0000395}
396
397static void tsc2000_set_brightness(void)
398{
399 uchar tmp[10];
400 int i, br;
401
402 spi_init();
403 tsc2000_write(1, 2, 0x0); /* Power up DAC */
404
405 i = getenv_r("brightness", tmp, sizeof(tmp));
406 br = (i > 0)
407 ? (int) simple_strtoul (tmp, NULL, 10)
408 : CFG_BRIGHTNESS;
409
410 tsc2000_write(0, 0xb, br & 0xff);
411}
412#endif