trab.c 9.9 KB

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