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