trab.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <garyj@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. /* #define DEBUG */
  24. #include <common.h>
  25. #include <malloc.h>
  26. #include <s3c2400.h>
  27. #include <command.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #ifdef CONFIG_SYS_BRIGHTNESS
  30. static void spi_init(void);
  31. static void wait_transmit_done(void);
  32. static void tsc2000_write(unsigned int page, unsigned int reg,
  33. unsigned int data);
  34. static void tsc2000_set_brightness(void);
  35. #endif
  36. #ifdef CONFIG_MODEM_SUPPORT
  37. static int key_pressed(void);
  38. extern void disable_putc(void);
  39. extern int do_mdm_init; /* defined in common/main.c */
  40. /*
  41. * We need a delay of at least 500 us after turning on the VFD clock
  42. * before we can read any useful information for the CPLD controlling
  43. * the keyboard switches. Let's play safe and wait 5 ms. The problem
  44. * is that timers are not available yet, so we use a manually timed
  45. * loop.
  46. */
  47. #define KBD_MDELAY 5000
  48. static void udelay_no_timer (int usec)
  49. {
  50. int i;
  51. int delay = usec * 3;
  52. for (i = 0; i < delay; i ++) gd->bd->bi_arch_number = MACH_TYPE_TRAB;
  53. }
  54. #endif /* CONFIG_MODEM_SUPPORT */
  55. /*
  56. * Miscellaneous platform dependent initialisations
  57. */
  58. int board_init ()
  59. {
  60. #if defined(CONFIG_VFD)
  61. extern int vfd_init_clocks(void);
  62. #endif
  63. S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
  64. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  65. /* memory and cpu-speed are setup before relocation */
  66. #ifdef CONFIG_TRAB_50MHZ
  67. /* change the clock to be 50 MHz 1:1:1 */
  68. /* MDIV:0x5c PDIV:4 SDIV:2 */
  69. clk_power->MPLLCON = 0x5c042;
  70. clk_power->CLKDIVN = 0;
  71. #else
  72. /* change the clock to be 133 MHz 1:2:4 */
  73. /* MDIV:0x7d PDIV:4 SDIV:1 */
  74. clk_power->MPLLCON = 0x7d041;
  75. clk_power->CLKDIVN = 3;
  76. #endif
  77. /* set up the I/O ports */
  78. gpio->PACON = 0x3ffff;
  79. gpio->PBCON = 0xaaaaaaaa;
  80. gpio->PBUP = 0xffff;
  81. /* INPUT nCTS0 nRTS0 TXD[1] TXD[0] RXD[1] RXD[0] */
  82. /* 00, 10, 10, 10, 10, 10, 10 */
  83. gpio->PFCON = (2<<0) | (2<<2) | (2<<4) | (2<<6) | (2<<8) | (2<<10);
  84. #ifdef CONFIG_HWFLOW
  85. /* do not pull up RXD0, RXD1, TXD0, TXD1, CTS0, RTS0 */
  86. gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5);
  87. #else
  88. /* do not pull up RXD0, RXD1, TXD0, TXD1 */
  89. gpio->PFUP = (1<<0) | (1<<1) | (1<<2) | (1<<3);
  90. #endif
  91. gpio->PGCON = 0x0;
  92. gpio->PGUP = 0x0;
  93. gpio->OPENCR= 0x0;
  94. /* suppress flicker of the VFDs */
  95. gpio->MISCCR = 0x40;
  96. gpio->PFCON |= (2<<12);
  97. gd->bd->bi_arch_number = MACH_TYPE_TRAB;
  98. /* adress of boot parameters */
  99. gd->bd->bi_boot_params = 0x0c000100;
  100. /* Make sure both buzzers are turned off */
  101. gpio->PDCON |= 0x5400;
  102. gpio->PDDAT &= ~0xE0;
  103. #ifdef CONFIG_VFD
  104. vfd_init_clocks();
  105. #endif /* CONFIG_VFD */
  106. #ifdef CONFIG_MODEM_SUPPORT
  107. udelay_no_timer (KBD_MDELAY);
  108. if (key_pressed()) {
  109. disable_putc(); /* modem doesn't understand banner etc */
  110. do_mdm_init = 1;
  111. }
  112. #endif /* CONFIG_MODEM_SUPPORT */
  113. #ifdef CONFIG_DRIVER_S3C24X0_I2C
  114. /* Configure I/O ports PG5 und PG6 for I2C */
  115. gpio->PGCON = (gpio->PGCON & 0x003c00) | 0x003c00;
  116. #endif /* CONFIG_DRIVER_S3C24X0_I2C */
  117. return 0;
  118. }
  119. int dram_init (void)
  120. {
  121. gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
  122. gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
  123. return 0;
  124. }
  125. /*-----------------------------------------------------------------------
  126. * Keyboard Controller
  127. */
  128. /* Maximum key number */
  129. #define KEYBD_KEY_NUM 4
  130. #define KBD_DATA (((*(volatile ulong *)0x04020000) >> 16) & 0xF)
  131. static char *key_match (ulong);
  132. int misc_init_r (void)
  133. {
  134. ulong kbd_data = KBD_DATA;
  135. char *str;
  136. char keybd_env[KEYBD_KEY_NUM + 1];
  137. int i;
  138. #ifdef CONFIG_VERSION_VARIABLE
  139. {
  140. /* Set version variable. Please note, that this variable is
  141. * also set in main_loop() later in the boot process. The
  142. * version variable has to be set this early, because so it
  143. * could be used in script files on an usb stick, which
  144. * might be called during do_auto_update() */
  145. extern char version_string[];
  146. setenv ("ver", version_string);
  147. }
  148. #endif /* CONFIG_VERSION_VARIABLE */
  149. #ifdef CONFIG_AUTO_UPDATE
  150. {
  151. extern int do_auto_update(void);
  152. /* this has priority over all else */
  153. do_auto_update();
  154. }
  155. #endif
  156. for (i = 0; i < KEYBD_KEY_NUM; ++i) {
  157. keybd_env[i] = '0' + ((kbd_data >> i) & 1);
  158. }
  159. keybd_env[i] = '\0';
  160. debug ("** Setting keybd=\"%s\"\n", keybd_env);
  161. setenv ("keybd", keybd_env);
  162. str = strdup (key_match (kbd_data)); /* decode keys */
  163. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  164. debug ("** Setting preboot=\"%s\"\n", str);
  165. setenv ("preboot", str); /* set or delete definition */
  166. #endif /* CONFIG_PREBOOT */
  167. if (str != NULL) {
  168. free (str);
  169. }
  170. #ifdef CONFIG_SYS_BRIGHTNESS
  171. tsc2000_set_brightness();
  172. #endif
  173. return (0);
  174. }
  175. #ifdef CONFIG_PREBOOT
  176. static uchar kbd_magic_prefix[] = "key_magic";
  177. static uchar kbd_command_prefix[] = "key_cmd";
  178. static int compare_magic (ulong kbd_data, char *str)
  179. {
  180. uchar key_mask;
  181. debug ("compare_magic: kbd: %04lx str: \"%s\"\n",kbd_data,str);
  182. for (; *str; str++)
  183. {
  184. uchar c = *str - '1';
  185. if (c >= KEYBD_KEY_NUM) /* bad key number */
  186. return -1;
  187. key_mask = 1 << c;
  188. if (!(kbd_data & key_mask)) { /* key not pressed */
  189. debug ( "compare_magic: "
  190. "kbd: %04lx mask: %04lx - key not pressed\n",
  191. kbd_data, key_mask );
  192. return -1;
  193. }
  194. kbd_data &= ~key_mask;
  195. }
  196. if (kbd_data) { /* key(s) not released */
  197. debug ( "compare_magic: "
  198. "kbd: %04lx - key(s) not released\n", kbd_data);
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. /*-----------------------------------------------------------------------
  204. * Check if pressed key(s) match magic sequence,
  205. * and return the command string associated with that key(s).
  206. *
  207. * If no key press was decoded, NULL is returned.
  208. *
  209. * Note: the first character of the argument will be overwritten with
  210. * the "magic charcter code" of the decoded key(s), or '\0'.
  211. *
  212. *
  213. * Note: the string points to static environment data and must be
  214. * saved before you call any function that modifies the environment.
  215. */
  216. static char *key_match (ulong kbd_data)
  217. {
  218. char magic[sizeof (kbd_magic_prefix) + 1];
  219. char cmd_name[sizeof (kbd_command_prefix) + 1];
  220. char *suffix;
  221. char *kbd_magic_keys;
  222. /*
  223. * The following string defines the characters that can pe appended
  224. * to "key_magic" to form the names of environment variables that
  225. * hold "magic" key codes, i. e. such key codes that can cause
  226. * pre-boot actions. If the string is empty (""), then only
  227. * "key_magic" is checked (old behaviour); the string "125" causes
  228. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  229. */
  230. if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
  231. kbd_magic_keys = "";
  232. debug ("key_match: magic_keys=\"%s\"\n", kbd_magic_keys);
  233. /* loop over all magic keys;
  234. * use '\0' suffix in case of empty string
  235. */
  236. for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix)
  237. {
  238. sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
  239. debug ("key_match: magic=\"%s\"\n",
  240. getenv(magic) ? getenv(magic) : "<UNDEFINED>");
  241. if (compare_magic(kbd_data, getenv(magic)) == 0)
  242. {
  243. sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
  244. debug ("key_match: cmdname %s=\"%s\"\n",
  245. cmd_name,
  246. getenv (cmd_name) ?
  247. getenv (cmd_name) :
  248. "<UNDEFINED>");
  249. return (getenv (cmd_name));
  250. }
  251. }
  252. debug ("key_match: no match\n");
  253. return (NULL);
  254. }
  255. #endif /* CONFIG_PREBOOT */
  256. /* Read Keyboard status */
  257. int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  258. {
  259. ulong kbd_data = KBD_DATA;
  260. char keybd_env[KEYBD_KEY_NUM + 1];
  261. int i;
  262. puts ("Keys:");
  263. for (i = 0; i < KEYBD_KEY_NUM; ++i) {
  264. keybd_env[i] = '0' + ((kbd_data >> i) & 1);
  265. printf (" %c", keybd_env[i]);
  266. }
  267. keybd_env[i] = '\0';
  268. putc ('\n');
  269. setenv ("keybd", keybd_env);
  270. return 0;
  271. }
  272. U_BOOT_CMD(
  273. kbd, 1, 1, do_kbd,
  274. "read keyboard status",
  275. NULL
  276. );
  277. #ifdef CONFIG_MODEM_SUPPORT
  278. static int key_pressed(void)
  279. {
  280. return (compare_magic(KBD_DATA, CONFIG_MODEM_KEY_MAGIC) == 0);
  281. }
  282. #endif /* CONFIG_MODEM_SUPPORT */
  283. #ifdef CONFIG_SYS_BRIGHTNESS
  284. static inline void SET_CS_TOUCH(void)
  285. {
  286. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  287. gpio->PDDAT &= 0x5FF;
  288. }
  289. static inline void CLR_CS_TOUCH(void)
  290. {
  291. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  292. gpio->PDDAT |= 0x200;
  293. }
  294. static void spi_init(void)
  295. {
  296. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  297. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  298. int i;
  299. /* Configure I/O ports. */
  300. gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
  301. gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
  302. gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
  303. gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
  304. CLR_CS_TOUCH();
  305. spi->ch[0].SPPRE = 0x1F; /* Baudrate ca. 514kHz */
  306. spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
  307. spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0, CPHA=1 */
  308. /* Dummy byte ensures clock to be low. */
  309. for (i = 0; i < 10; i++) {
  310. spi->ch[0].SPTDAT = 0xFF;
  311. }
  312. wait_transmit_done();
  313. }
  314. static void wait_transmit_done(void)
  315. {
  316. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  317. while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
  318. }
  319. static void tsc2000_write(unsigned int page, unsigned int reg,
  320. unsigned int data)
  321. {
  322. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  323. unsigned int command;
  324. SET_CS_TOUCH();
  325. command = 0x0000;
  326. command |= (page << 11);
  327. command |= (reg << 5);
  328. spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
  329. wait_transmit_done();
  330. spi->ch[0].SPTDAT = (command & 0x00FF);
  331. wait_transmit_done();
  332. spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
  333. wait_transmit_done();
  334. spi->ch[0].SPTDAT = (data & 0x00FF);
  335. wait_transmit_done();
  336. CLR_CS_TOUCH();
  337. }
  338. static void tsc2000_set_brightness(void)
  339. {
  340. char tmp[10];
  341. int i, br;
  342. spi_init();
  343. tsc2000_write(1, 2, 0x0); /* Power up DAC */
  344. i = getenv_r("brightness", tmp, sizeof(tmp));
  345. br = (i > 0)
  346. ? (int) simple_strtoul (tmp, NULL, 10)
  347. : CONFIG_SYS_BRIGHTNESS;
  348. tsc2000_write(0, 0xb, br & 0xff);
  349. }
  350. #endif