]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/trab/trab.c
Merge with /home/sr/git/u-boot/4xx-sdram
[karo-tx-uboot.git] / board / trab / trab.c
1 /*
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 <malloc.h>
28 #include <s3c2400.h>
29 #include <command.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 #ifdef CFG_BRIGHTNESS
34 static void spi_init(void);
35 static void wait_transmit_done(void);
36 static void tsc2000_write(unsigned int page, unsigned int reg,
37                                                   unsigned int data);
38 static void tsc2000_set_brightness(void);
39 #endif
40 #ifdef CONFIG_MODEM_SUPPORT
41 static int key_pressed(void);
42 extern void disable_putc(void);
43 extern 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  */
52 #define KBD_MDELAY      5000
53 static void udelay_no_timer (int usec)
54 {
55         int i;
56         int delay = usec * 3;
57
58         for (i = 0; i < delay; i ++) gd->bd->bi_arch_number = MACH_TYPE_TRAB;
59 }
60 #endif /* CONFIG_MODEM_SUPPORT */
61
62 /*
63  * Miscellaneous platform dependent initialisations
64  */
65
66 int board_init ()
67 {
68 #if defined(CONFIG_VFD)
69         extern int vfd_init_clocks(void);
70 #endif
71         S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
72         S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
73
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 */
78         clk_power->MPLLCON = 0x5c042;
79         clk_power->CLKDIVN = 0;
80 #else
81         /* change the clock to be 133 MHz 1:2:4 */
82         /* MDIV:0x7d PDIV:4 SDIV:1 */
83         clk_power->MPLLCON = 0x7d041;
84         clk_power->CLKDIVN = 3;
85 #endif
86
87         /* set up the I/O ports */
88         gpio->PACON = 0x3ffff;
89         gpio->PBCON = 0xaaaaaaaa;
90         gpio->PBUP  = 0xffff;
91         /* INPUT nCTS0 nRTS0 TXD[1] TXD[0] RXD[1] RXD[0]        */
92         /*  00,    10,      10,      10,      10,      10,      10      */
93         gpio->PFCON = (2<<0) | (2<<2) | (2<<4) | (2<<6) | (2<<8) | (2<<10);
94 #ifdef CONFIG_HWFLOW
95         /* do not pull up RXD0, RXD1, TXD0, TXD1, CTS0, RTS0 */
96         gpio->PFUP  = (1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5);
97 #else
98         /* do not pull up RXD0, RXD1, TXD0, TXD1 */
99         gpio->PFUP  = (1<<0) | (1<<1) | (1<<2) | (1<<3);
100 #endif
101         gpio->PGCON = 0x0;
102         gpio->PGUP  = 0x0;
103         gpio->OPENCR= 0x0;
104
105         /* suppress flicker of the VFDs */
106         gpio->MISCCR = 0x40;
107         gpio->PFCON |= (2<<12);
108
109         gd->bd->bi_arch_number = MACH_TYPE_TRAB;
110
111         /* adress of boot parameters */
112         gd->bd->bi_boot_params = 0x0c000100;
113
114         /* Make sure both buzzers are turned off */
115         gpio->PDCON |= 0x5400;
116         gpio->PDDAT &= ~0xE0;
117
118 #ifdef CONFIG_VFD
119         vfd_init_clocks();
120 #endif /* CONFIG_VFD */
121
122 #ifdef CONFIG_MODEM_SUPPORT
123         udelay_no_timer (KBD_MDELAY);
124
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 #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
136         return 0;
137 }
138
139 int dram_init (void)
140 {
141         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
155 static uchar *key_match (ulong);
156
157 int 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
164 #ifdef CONFIG_AUTO_UPDATE
165         extern int do_auto_update(void);
166         /* this has priority over all else */
167         do_auto_update();
168 #endif
169
170         for (i = 0; i < KEYBD_KEY_NUM; ++i) {
171                 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
172         }
173         keybd_env[i] = '\0';
174         debug ("** Setting keybd=\"%s\"\n", keybd_env);
175         setenv ("keybd", keybd_env);
176
177         str = strdup (key_match (kbd_data));    /* decode keys */
178
179 #ifdef CONFIG_PREBOOT   /* automatically configure "preboot" command on key match */
180         debug ("** Setting preboot=\"%s\"\n", str);
181         setenv ("preboot", str);        /* set or delete definition */
182 #endif /* CONFIG_PREBOOT */
183         if (str != NULL) {
184                 free (str);
185         }
186
187 #ifdef CFG_BRIGHTNESS
188         tsc2000_set_brightness();
189 #endif
190         return (0);
191 }
192
193 #ifdef CONFIG_PREBOOT
194
195 static uchar kbd_magic_prefix[] = "key_magic";
196 static uchar kbd_command_prefix[] = "key_cmd";
197
198 static int compare_magic (ulong kbd_data, uchar *str)
199 {
200         uchar key_mask;
201
202         debug ("compare_magic: kbd: %04lx  str: \"%s\"\n",kbd_data,str);
203         for (; *str; str++)
204         {
205                 uchar c = *str - '1';
206
207                 if (c >= KEYBD_KEY_NUM)         /* bad key number */
208                         return -1;
209
210                 key_mask = 1 << c;
211
212                 if (!(kbd_data & key_mask)) {   /* key not pressed */
213                         debug ( "compare_magic: "
214                                 "kbd: %04lx mask: %04lx - key not pressed\n",
215                                 kbd_data, key_mask );
216                         return -1;
217                 }
218
219                 kbd_data &= ~key_mask;
220         }
221
222         if (kbd_data) {                         /* key(s) not released */
223                 debug ( "compare_magic: "
224                         "kbd: %04lx - key(s) not released\n", kbd_data);
225                 return -1;
226         }
227
228         return 0;
229 }
230
231 /*-----------------------------------------------------------------------
232  * Check if pressed key(s) match magic sequence,
233  * and return the command string associated with that key(s).
234  *
235  * If no key press was decoded, NULL is returned.
236  *
237  * Note: the first character of the argument will be overwritten with
238  * the "magic charcter code" of the decoded key(s), or '\0'.
239  *
240  *
241  * Note: the string points to static environment data and must be
242  * saved before you call any function that modifies the environment.
243  */
244 static uchar *key_match (ulong kbd_data)
245 {
246         uchar magic[sizeof (kbd_magic_prefix) + 1];
247         uchar cmd_name[sizeof (kbd_command_prefix) + 1];
248         uchar *suffix;
249         uchar *kbd_magic_keys;
250
251         /*
252          * The following string defines the characters that can pe appended
253          * to "key_magic" to form the names of environment variables that
254          * hold "magic" key codes, i. e. such key codes that can cause
255          * pre-boot actions. If the string is empty (""), then only
256          * "key_magic" is checked (old behaviour); the string "125" causes
257          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
258          */
259         if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
260                 kbd_magic_keys = "";
261
262         debug ("key_match: magic_keys=\"%s\"\n", kbd_magic_keys);
263
264         /* loop over all magic keys;
265          * use '\0' suffix in case of empty string
266          */
267         for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix)
268         {
269                 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
270
271                 debug ("key_match: magic=\"%s\"\n",
272                         getenv(magic) ? getenv(magic) : "<UNDEFINED>");
273
274                 if (compare_magic(kbd_data, getenv(magic)) == 0)
275                 {
276                         sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
277                         debug ("key_match: cmdname %s=\"%s\"\n",
278                                 cmd_name,
279                                 getenv (cmd_name) ?
280                                         getenv (cmd_name) :
281                                         "<UNDEFINED>");
282                         return (getenv (cmd_name));
283                 }
284         }
285         debug ("key_match: no match\n");
286         return (NULL);
287 }
288 #endif                                                  /* CONFIG_PREBOOT */
289
290 /* Read Keyboard status */
291 int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
292 {
293         ulong kbd_data = KBD_DATA;
294         uchar keybd_env[KEYBD_KEY_NUM + 1];
295         int i;
296
297         puts ("Keys:");
298         for (i = 0; i < KEYBD_KEY_NUM; ++i) {
299                 keybd_env[i] = '0' + ((kbd_data >> i) & 1);
300                 printf (" %c", keybd_env[i]);
301         }
302         keybd_env[i] = '\0';
303         putc ('\n');
304         setenv ("keybd", keybd_env);
305         return 0;
306 }
307
308 U_BOOT_CMD(
309         kbd,    1,      1,      do_kbd,
310         "kbd     - read keyboard status\n",
311         NULL
312 );
313
314 #ifdef CONFIG_MODEM_SUPPORT
315 static int key_pressed(void)
316 {
317         return (compare_magic(KBD_DATA, CONFIG_MODEM_KEY_MAGIC) == 0);
318 }
319 #endif  /* CONFIG_MODEM_SUPPORT */
320
321 #ifdef CFG_BRIGHTNESS
322
323 static inline void SET_CS_TOUCH(void)
324 {
325         S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
326
327         gpio->PDDAT &= 0x5FF;
328 }
329
330 static inline void CLR_CS_TOUCH(void)
331 {
332         S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
333
334         gpio->PDDAT |= 0x200;
335 }
336
337 static void spi_init(void)
338 {
339         S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
340         S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
341         int i;
342
343         /* Configure I/O ports. */
344         gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
345         gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
346         gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
347         gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
348
349         CLR_CS_TOUCH();
350
351         spi->ch[0].SPPRE = 0x1F; /* Baudrate ca. 514kHz */
352         spi->ch[0].SPPIN = 0x01;  /* SPI-MOSI holds Level after last bit */
353         spi->ch[0].SPCON = 0x1A;  /* Polling, Prescaler, Master, CPOL=0, CPHA=1 */
354
355         /* Dummy byte ensures clock to be low. */
356         for (i = 0; i < 10; i++) {
357                 spi->ch[0].SPTDAT = 0xFF;
358         }
359         wait_transmit_done();
360 }
361
362 static void wait_transmit_done(void)
363 {
364         S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
365
366         while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
367 }
368
369 static void tsc2000_write(unsigned int page, unsigned int reg,
370                                                   unsigned int data)
371 {
372         S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
373         unsigned int command;
374
375         SET_CS_TOUCH();
376         command = 0x0000;
377         command |= (page << 11);
378         command |= (reg << 5);
379
380         spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
381         wait_transmit_done();
382         spi->ch[0].SPTDAT = (command & 0x00FF);
383         wait_transmit_done();
384         spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
385         wait_transmit_done();
386         spi->ch[0].SPTDAT = (data & 0x00FF);
387         wait_transmit_done();
388
389         CLR_CS_TOUCH();
390 }
391
392 static void tsc2000_set_brightness(void)
393 {
394         uchar tmp[10];
395         int i, br;
396
397         spi_init();
398         tsc2000_write(1, 2, 0x0); /* Power up DAC */
399
400         i = getenv_r("brightness", tmp, sizeof(tmp));
401         br = (i > 0)
402                 ? (int) simple_strtoul (tmp, NULL, 10)
403                 : CFG_BRIGHTNESS;
404
405         tsc2000_write(0, 0xb, br & 0xff);
406 }
407 #endif