blob: fb57f5bcfa31c94f53083c10b8dba2b275daa2b0 [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
31/* ------------------------------------------------------------------------- */
32
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{
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{
wdenk4fc95692003-02-28 00:49:47 +000070#if defined(CONFIG_VFD)
71 extern int vfd_init_clocks(void);
wdenk9dd2b882002-12-03 21:28:10 +000072#endif
wdenkc6097192002-11-03 00:24:07 +000073 DECLARE_GLOBAL_DATA_PTR;
wdenk7539dea2003-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 */
wdenk7539dea2003-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 */
wdenk7539dea2003-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 */
wdenk7539dea2003-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 */
wdenk7539dea2003-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 */
wdenk7539dea2003-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 */
wdenk7539dea2003-06-19 23:01:32 +0000102 gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3);
wdenkc6097192002-11-03 00:24:07 +0000103#endif
wdenk7539dea2003-06-19 23:01:32 +0000104 gpio->PGCON = 0x0;
105 gpio->PGUP = 0x0;
106 gpio->OPENCR= 0x0;
wdenkc6097192002-11-03 00:24:07 +0000107
wdenkde887eb2003-09-10 18:20:28 +0000108 /* suppress flicker of the VFDs */
109 gpio->MISCCR = 0x40;
110 gpio->PFCON |= (2<<12);
111
wdenkc6097192002-11-03 00:24:07 +0000112 /* arch number of SAMSUNG-Board */
113 /* MACH_TYPE_SMDK2400 */
114 /* XXX this isn't really correct, but keep it for now */
115 gd->bd->bi_arch_number = 145;
116
117 /* adress of boot parameters */
118 gd->bd->bi_boot_params = 0x0c000100;
119
wdenk1fe2c702003-03-06 21:55:29 +0000120 /* Make sure both buzzers are turned off */
wdenk7539dea2003-06-19 23:01:32 +0000121 gpio->PDCON |= 0x5400;
122 gpio->PDDAT &= ~0xE0;
wdenk1fe2c702003-03-06 21:55:29 +0000123
wdenk9dd2b882002-12-03 21:28:10 +0000124#ifdef CONFIG_VFD
wdenk4fc95692003-02-28 00:49:47 +0000125 vfd_init_clocks();
wdenk9dd2b882002-12-03 21:28:10 +0000126#endif /* CONFIG_VFD */
wdenkc6097192002-11-03 00:24:07 +0000127
wdenk4fc95692003-02-28 00:49:47 +0000128#ifdef CONFIG_MODEM_SUPPORT
wdenke95b61c2002-11-04 16:02:40 +0000129 udelay_no_timer (KBD_MDELAY);
wdenkc6097192002-11-03 00:24:07 +0000130
131 if (key_pressed()) {
132 disable_putc(); /* modem doesn't understand banner etc */
133 do_mdm_init = 1;
134 }
135#endif /* CONFIG_MODEM_SUPPORT */
136
wdenkca9bc762003-07-15 07:45:49 +0000137#ifdef CONFIG_DRIVER_S3C24X0_I2C
138 /* Configure I/O ports PG5 und PG6 for I2C */
139 gpio->PGCON = (gpio->PGCON & 0x003c00) | 0x003c00;
140#endif /* CONFIG_DRIVER_S3C24X0_I2C */
141
wdenkc6097192002-11-03 00:24:07 +0000142 return 0;
143}
144
145int dram_init (void)
146{
147 DECLARE_GLOBAL_DATA_PTR;
148
149 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
150 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
151 return 0;
152}
153
154/*-----------------------------------------------------------------------
155 * Keyboard Controller
156 */
157
158/* Maximum key number */
159#define KEYBD_KEY_NUM 4
160
161#define KBD_DATA (((*(volatile ulong *)0x04020000) >> 16) & 0xF)
162
163static uchar *key_match (ulong);
164
165int misc_init_r (void)
166{
167 ulong kbd_data = KBD_DATA;
168 uchar keybd_env[KEYBD_KEY_NUM + 1];
169 uchar *str;
170 int i;
171
wdenkf6a6ac12003-09-17 15:10:32 +0000172#ifdef CONFIG_AUTO_UPDATE
173 extern int do_auto_update(void);
174 /* this has priority over all else */
175 do_auto_update();
176#endif
177
wdenkc6097192002-11-03 00:24:07 +0000178 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
179 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
180 }
181 keybd_env[i] = '\0';
182 debug ("** Setting keybd=\"%s\"\n", keybd_env);
183 setenv ("keybd", keybd_env);
184
185 str = strdup (key_match (kbd_data)); /* decode keys */
186
187#ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
188 debug ("** Setting preboot=\"%s\"\n", str);
189 setenv ("preboot", str); /* set or delete definition */
190#endif /* CONFIG_PREBOOT */
191 if (str != NULL) {
192 free (str);
193 }
194
wdenk1fe2c702003-03-06 21:55:29 +0000195#ifdef CFG_BRIGHTNESS
196 tsc2000_set_brightness();
197#endif
wdenkc6097192002-11-03 00:24:07 +0000198 return (0);
199}
200
201#ifdef CONFIG_PREBOOT
202
203static uchar kbd_magic_prefix[] = "key_magic";
204static uchar kbd_command_prefix[] = "key_cmd";
205
206static int compare_magic (ulong kbd_data, uchar *str)
207{
208 uchar key_mask;
209
210 debug ("compare_magic: kbd: %04lx str: \"%s\"\n",kbd_data,str);
211 for (; *str; str++)
212 {
213 uchar c = *str - '1';
214
215 if (c >= KEYBD_KEY_NUM) /* bad key number */
216 return -1;
217
218 key_mask = 1 << c;
219
220 if (!(kbd_data & key_mask)) { /* key not pressed */
221 debug ( "compare_magic: "
222 "kbd: %04lx mask: %04lx - key not pressed\n",
223 kbd_data, key_mask );
224 return -1;
225 }
226
227 kbd_data &= ~key_mask;
228 }
229
230 if (kbd_data) { /* key(s) not released */
231 debug ( "compare_magic: "
232 "kbd: %04lx - key(s) not released\n", kbd_data);
233 return -1;
234 }
235
236 return 0;
237}
238
239/*-----------------------------------------------------------------------
240 * Check if pressed key(s) match magic sequence,
241 * and return the command string associated with that key(s).
242 *
243 * If no key press was decoded, NULL is returned.
244 *
245 * Note: the first character of the argument will be overwritten with
246 * the "magic charcter code" of the decoded key(s), or '\0'.
247 *
248 *
249 * Note: the string points to static environment data and must be
250 * saved before you call any function that modifies the environment.
251 */
252static uchar *key_match (ulong kbd_data)
253{
254 uchar magic[sizeof (kbd_magic_prefix) + 1];
255 uchar cmd_name[sizeof (kbd_command_prefix) + 1];
256 uchar *suffix;
257 uchar *kbd_magic_keys;
258
259 /*
260 * The following string defines the characters that can pe appended
261 * to "key_magic" to form the names of environment variables that
262 * hold "magic" key codes, i. e. such key codes that can cause
263 * pre-boot actions. If the string is empty (""), then only
264 * "key_magic" is checked (old behaviour); the string "125" causes
265 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
266 */
267 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
268 kbd_magic_keys = "";
269
270 debug ("key_match: magic_keys=\"%s\"\n", kbd_magic_keys);
271
272 /* loop over all magic keys;
273 * use '\0' suffix in case of empty string
274 */
275 for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix)
276 {
277 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
278
279 debug ("key_match: magic=\"%s\"\n",
280 getenv(magic) ? getenv(magic) : "<UNDEFINED>");
281
282 if (compare_magic(kbd_data, getenv(magic)) == 0)
283 {
284 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
285 debug ("key_match: cmdname %s=\"%s\"\n",
286 cmd_name,
287 getenv (cmd_name) ?
288 getenv (cmd_name) :
289 "<UNDEFINED>");
290 return (getenv (cmd_name));
291 }
292 }
293 debug ("key_match: no match\n");
294 return (NULL);
295}
296#endif /* CONFIG_PREBOOT */
297
298/* Read Keyboard status */
299int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
300{
301 ulong kbd_data = KBD_DATA;
302 uchar keybd_env[KEYBD_KEY_NUM + 1];
303 int i;
304
305 puts ("Keys:");
306 for (i = 0; i < KEYBD_KEY_NUM; ++i) {
307 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
308 printf (" %c", keybd_env[i]);
309 }
310 keybd_env[i] = '\0';
311 putc ('\n');
312 setenv ("keybd", keybd_env);
313 return 0;
314}
315
wdenkf287a242003-07-01 21:06:45 +0000316U_BOOT_CMD(
317 kbd, 1, 1, do_kbd,
wdenk57b2d802003-06-27 21:31:46 +0000318 "kbd - read keyboard status\n",
319 NULL
320);
321
wdenkc6097192002-11-03 00:24:07 +0000322#ifdef CONFIG_MODEM_SUPPORT
323static int key_pressed(void)
324{
325 return (compare_magic(KBD_DATA, CONFIG_MODEM_KEY_MAGIC) == 0);
326}
327#endif /* CONFIG_MODEM_SUPPORT */
wdenk1fe2c702003-03-06 21:55:29 +0000328
329#ifdef CFG_BRIGHTNESS
330
wdenk7539dea2003-06-19 23:01:32 +0000331static inline void SET_CS_TOUCH(void)
332{
333 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
334
335 gpio->PDDAT &= 0x5FF;
336}
337
338static inline void CLR_CS_TOUCH(void)
339{
340 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
341
342 gpio->PDDAT |= 0x200;
343}
wdenk1fe2c702003-03-06 21:55:29 +0000344
345static void spi_init(void)
346{
wdenk7539dea2003-06-19 23:01:32 +0000347 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
348 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
wdenk1fe2c702003-03-06 21:55:29 +0000349 int i;
350
351 /* Configure I/O ports. */
wdenk7539dea2003-06-19 23:01:32 +0000352 gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
353 gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
354 gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
355 gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
wdenk1fe2c702003-03-06 21:55:29 +0000356
wdenk7539dea2003-06-19 23:01:32 +0000357 CLR_CS_TOUCH();
wdenk1fe2c702003-03-06 21:55:29 +0000358
wdenk7539dea2003-06-19 23:01:32 +0000359 spi->ch[0].SPPRE = 0x1F; /* Baudrate ca. 514kHz */
360 spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
361 spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0, CPHA=1 */
wdenk1fe2c702003-03-06 21:55:29 +0000362
363 /* Dummy byte ensures clock to be low. */
364 for (i = 0; i < 10; i++) {
wdenk7539dea2003-06-19 23:01:32 +0000365 spi->ch[0].SPTDAT = 0xFF;
wdenk1fe2c702003-03-06 21:55:29 +0000366 }
wdenk26991552003-05-20 20:49:01 +0000367 wait_transmit_done();
wdenk1fe2c702003-03-06 21:55:29 +0000368}
369
370static void wait_transmit_done(void)
371{
wdenk7539dea2003-06-19 23:01:32 +0000372 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
373
374 while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
wdenk1fe2c702003-03-06 21:55:29 +0000375}
376
wdenk26991552003-05-20 20:49:01 +0000377static void tsc2000_write(unsigned int page, unsigned int reg,
wdenk1fe2c702003-03-06 21:55:29 +0000378 unsigned int data)
379{
wdenk7539dea2003-06-19 23:01:32 +0000380 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
wdenk1fe2c702003-03-06 21:55:29 +0000381 unsigned int command;
382
wdenk7539dea2003-06-19 23:01:32 +0000383 SET_CS_TOUCH();
wdenk1fe2c702003-03-06 21:55:29 +0000384 command = 0x0000;
385 command |= (page << 11);
386 command |= (reg << 5);
387
wdenk7539dea2003-06-19 23:01:32 +0000388 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
wdenk1fe2c702003-03-06 21:55:29 +0000389 wait_transmit_done();
wdenk7539dea2003-06-19 23:01:32 +0000390 spi->ch[0].SPTDAT = (command & 0x00FF);
wdenk1fe2c702003-03-06 21:55:29 +0000391 wait_transmit_done();
wdenk7539dea2003-06-19 23:01:32 +0000392 spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
wdenk1fe2c702003-03-06 21:55:29 +0000393 wait_transmit_done();
wdenk7539dea2003-06-19 23:01:32 +0000394 spi->ch[0].SPTDAT = (data & 0x00FF);
wdenk1fe2c702003-03-06 21:55:29 +0000395 wait_transmit_done();
396
wdenk7539dea2003-06-19 23:01:32 +0000397 CLR_CS_TOUCH();
wdenk1fe2c702003-03-06 21:55:29 +0000398}
399
400static void tsc2000_set_brightness(void)
401{
402 uchar tmp[10];
403 int i, br;
404
405 spi_init();
406 tsc2000_write(1, 2, 0x0); /* Power up DAC */
407
408 i = getenv_r("brightness", tmp, sizeof(tmp));
409 br = (i > 0)
410 ? (int) simple_strtoul (tmp, NULL, 10)
411 : CFG_BRIGHTNESS;
412
413 tsc2000_write(0, 0xb, br & 0xff);
414}
415#endif