lwmon.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /***********************************************************************
  2. *
  3. M* Modul: lwmon.c
  4. M*
  5. M* Content: LWMON specific U-Boot commands.
  6. *
  7. * (C) Copyright 2001, 2002
  8. * DENX Software Engineering
  9. * Wolfgang Denk, wd@denx.de
  10. * All rights reserved.
  11. *
  12. D* Design: wd@denx.de
  13. C* Coding: wd@denx.de
  14. V* Verification: dzu@denx.de
  15. *
  16. * See file CREDITS for list of people who contributed to this
  17. * project.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License as
  21. * published by the Free Software Foundation; either version 2 of
  22. * the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. ***********************************************************************/
  34. /*---------------------------- Headerfiles ----------------------------*/
  35. #include <common.h>
  36. #include <mpc8xx.h>
  37. #include <commproc.h>
  38. #include <i2c.h>
  39. #include <command.h>
  40. #include <malloc.h>
  41. #include <post.h>
  42. #include <serial.h>
  43. #include <linux/types.h>
  44. #include <linux/string.h> /* for strdup */
  45. DECLARE_GLOBAL_DATA_PTR;
  46. /*------------------------ Local prototypes ---------------------------*/
  47. static long int dram_size (long int, long int *, long int);
  48. static void kbd_init (void);
  49. static int compare_magic (uchar *kbd_data, uchar *str);
  50. /*--------------------- Local macros and constants --------------------*/
  51. #define _NOT_USED_ 0xFFFFFFFF
  52. #ifdef CONFIG_MODEM_SUPPORT
  53. static int key_pressed(void);
  54. extern void disable_putc(void);
  55. #endif /* CONFIG_MODEM_SUPPORT */
  56. /*
  57. * 66 MHz SDRAM access using UPM A
  58. */
  59. const uint sdram_table[] =
  60. {
  61. #if defined(CFG_MEMORY_75) || defined(CFG_MEMORY_8E)
  62. /*
  63. * Single Read. (Offset 0 in UPM RAM)
  64. */
  65. 0x1F0DFC04, 0xEEAFBC04, 0x11AF7C04, 0xEFBAFC00,
  66. 0x1FF5FC47, /* last */
  67. /*
  68. * SDRAM Initialization (offset 5 in UPM RAM)
  69. *
  70. * This is no UPM entry point. The following definition uses
  71. * the remaining space to establish an initialization
  72. * sequence, which is executed by a RUN command.
  73. *
  74. */
  75. 0x1FF5FC34, 0xEFEABC34, 0x1FB57C35, /* last */
  76. /*
  77. * Burst Read. (Offset 8 in UPM RAM)
  78. */
  79. 0x1F0DFC04, 0xEEAFBC04, 0x10AF7C04, 0xF0AFFC00,
  80. 0xF0AFFC00, 0xF1AFFC00, 0xEFBAFC00, 0x1FF5FC47, /* last */
  81. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  82. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  83. /*
  84. * Single Write. (Offset 18 in UPM RAM)
  85. */
  86. 0x1F2DFC04, 0xEEABBC00, 0x01B27C04, 0x1FF5FC47, /* last */
  87. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  88. /*
  89. * Burst Write. (Offset 20 in UPM RAM)
  90. */
  91. 0x1F0DFC04, 0xEEABBC00, 0x10A77C00, 0xF0AFFC00,
  92. 0xF0AFFC00, 0xE1BAFC04, 0x01FF5FC47, /* last */
  93. _NOT_USED_,
  94. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  95. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  96. /*
  97. * Refresh (Offset 30 in UPM RAM)
  98. */
  99. 0x1FFD7C84, 0xFFFFFC04, 0xFFFFFC04, 0xFFFFFC04,
  100. 0xFFFFFC84, 0xFFFFFC07, /* last */
  101. _NOT_USED_, _NOT_USED_,
  102. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  103. /*
  104. * Exception. (Offset 3c in UPM RAM)
  105. */
  106. 0x7FFFFC07, /* last */
  107. 0xFFFFFCFF, 0xFFFFFCFF, 0xFFFFFCFF,
  108. #endif
  109. #ifdef CFG_MEMORY_7E
  110. /*
  111. * Single Read. (Offset 0 in UPM RAM)
  112. */
  113. 0x0E2DBC04, 0x11AF7C04, 0xEFBAFC00, 0x1FF5FC47, /* last */
  114. _NOT_USED_,
  115. /*
  116. * SDRAM Initialization (offset 5 in UPM RAM)
  117. *
  118. * This is no UPM entry point. The following definition uses
  119. * the remaining space to establish an initialization
  120. * sequence, which is executed by a RUN command.
  121. *
  122. */
  123. 0x1FF5FC34, 0xEFEABC34, 0x1FB57C35, /* last */
  124. /*
  125. * Burst Read. (Offset 8 in UPM RAM)
  126. */
  127. 0x0E2DBC04, 0x10AF7C04, 0xF0AFFC00, 0xF0AFFC00,
  128. 0xF1AFFC00, 0xEFBAFC00, 0x1FF5FC47, /* last */
  129. _NOT_USED_,
  130. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  131. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  132. /*
  133. * Single Write. (Offset 18 in UPM RAM)
  134. */
  135. 0x0E29BC04, 0x01B27C04, 0x1FF5FC47, /* last */
  136. _NOT_USED_,
  137. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  138. /*
  139. * Burst Write. (Offset 20 in UPM RAM)
  140. */
  141. 0x0E29BC04, 0x10A77C00, 0xF0AFFC00, 0xF0AFFC00,
  142. 0xE1BAFC04, 0x1FF5FC47, /* last */
  143. _NOT_USED_, _NOT_USED_,
  144. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  145. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  146. /*
  147. * Refresh (Offset 30 in UPM RAM)
  148. */
  149. 0x1FFD7C84, 0xFFFFFC04, 0xFFFFFC04, 0xFFFFFC04,
  150. 0xFFFFFC84, 0xFFFFFC07, /* last */
  151. _NOT_USED_, _NOT_USED_,
  152. _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
  153. /*
  154. * Exception. (Offset 3c in UPM RAM)
  155. */
  156. 0x7FFFFC07, /* last */
  157. 0xFFFFFCFF, 0xFFFFFCFF, 0xFFFFFCFF,
  158. #endif
  159. };
  160. /*
  161. * Check Board Identity:
  162. *
  163. */
  164. /***********************************************************************
  165. F* Function: int checkboard (void) P*A*Z*
  166. *
  167. P* Parameters: none
  168. P*
  169. P* Returnvalue: int - 0 is always returned
  170. *
  171. Z* Intention: This function is the checkboard() method implementation
  172. Z* for the lwmon board. Only a standard message is printed.
  173. *
  174. D* Design: wd@denx.de
  175. C* Coding: wd@denx.de
  176. V* Verification: dzu@denx.de
  177. ***********************************************************************/
  178. int checkboard (void)
  179. {
  180. puts ("Board: LICCON Konsole LCD3\n");
  181. return (0);
  182. }
  183. /***********************************************************************
  184. F* Function: long int initdram (int board_type) P*A*Z*
  185. *
  186. P* Parameters: int board_type
  187. P* - Usually type of the board - ignored here.
  188. P*
  189. P* Returnvalue: long int
  190. P* - Size of initialized memory
  191. *
  192. Z* Intention: This function is the initdram() method implementation
  193. Z* for the lwmon board.
  194. Z* The memory controller is initialized to access the
  195. Z* DRAM.
  196. *
  197. D* Design: wd@denx.de
  198. C* Coding: wd@denx.de
  199. V* Verification: dzu@denx.de
  200. ***********************************************************************/
  201. long int initdram (int board_type)
  202. {
  203. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  204. volatile memctl8xx_t *memctl = &immr->im_memctl;
  205. long int size_b0;
  206. long int size8, size9;
  207. int i;
  208. /*
  209. * Configure UPMA for SDRAM
  210. */
  211. upmconfig (UPMA, (uint *)sdram_table, sizeof(sdram_table)/sizeof(uint));
  212. memctl->memc_mptpr = CFG_MPTPR;
  213. /* burst length=4, burst type=sequential, CAS latency=2 */
  214. memctl->memc_mar = CFG_MAR;
  215. /*
  216. * Map controller bank 3 to the SDRAM bank at preliminary address.
  217. */
  218. memctl->memc_or3 = CFG_OR3_PRELIM;
  219. memctl->memc_br3 = CFG_BR3_PRELIM;
  220. /* initialize memory address register */
  221. memctl->memc_mamr = CFG_MAMR_8COL; /* refresh not enabled yet */
  222. /* mode initialization (offset 5) */
  223. udelay (200); /* 0x80006105 */
  224. memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x05);
  225. /* run 2 refresh sequence with 4-beat refresh burst (offset 0x30) */
  226. udelay (1); /* 0x80006130 */
  227. memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x30);
  228. udelay (1); /* 0x80006130 */
  229. memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x30);
  230. udelay (1); /* 0x80006106 */
  231. memctl->memc_mcr = MCR_OP_RUN | MCR_MB_CS3 | MCR_MLCF (1) | MCR_MAD (0x06);
  232. memctl->memc_mamr |= MAMR_PTAE; /* refresh enabled */
  233. udelay (200);
  234. /* Need at least 10 DRAM accesses to stabilize */
  235. for (i = 0; i < 10; ++i) {
  236. volatile unsigned long *addr =
  237. (volatile unsigned long *) SDRAM_BASE3_PRELIM;
  238. unsigned long val;
  239. val = *(addr + i);
  240. *(addr + i) = val;
  241. }
  242. /*
  243. * Check Bank 0 Memory Size for re-configuration
  244. *
  245. * try 8 column mode
  246. */
  247. size8 = dram_size (CFG_MAMR_8COL, (long *)SDRAM_BASE3_PRELIM, SDRAM_MAX_SIZE);
  248. udelay (1000);
  249. /*
  250. * try 9 column mode
  251. */
  252. size9 = dram_size (CFG_MAMR_9COL, (long *)SDRAM_BASE3_PRELIM, SDRAM_MAX_SIZE);
  253. if (size8 < size9) { /* leave configuration at 9 columns */
  254. size_b0 = size9;
  255. memctl->memc_mamr = CFG_MAMR_9COL | MAMR_PTAE;
  256. udelay (500);
  257. } else { /* back to 8 columns */
  258. size_b0 = size8;
  259. memctl->memc_mamr = CFG_MAMR_8COL | MAMR_PTAE;
  260. udelay (500);
  261. }
  262. /*
  263. * Final mapping:
  264. */
  265. memctl->memc_or3 = ((-size_b0) & 0xFFFF0000) |
  266. OR_CSNT_SAM | OR_G5LS | SDRAM_TIMING;
  267. memctl->memc_br3 = (CFG_SDRAM_BASE & BR_BA_MSK) | BR_MS_UPMA | BR_V;
  268. udelay (1000);
  269. return (size_b0);
  270. }
  271. /***********************************************************************
  272. F* Function: static long int dram_size (long int mamr_value,
  273. F* long int *base,
  274. F* long int maxsize) P*A*Z*
  275. *
  276. P* Parameters: long int mamr_value
  277. P* - Value for MAMR for the test
  278. P* long int *base
  279. P* - Base address for the test
  280. P* long int maxsize
  281. P* - Maximum size to test for
  282. P*
  283. P* Returnvalue: long int
  284. P* - Size of probed memory
  285. *
  286. Z* Intention: Check memory range for valid RAM. A simple memory test
  287. Z* determines the actually available RAM size between
  288. Z* addresses `base' and `base + maxsize'. Some (not all)
  289. Z* hardware errors are detected:
  290. Z* - short between address lines
  291. Z* - short between data lines
  292. *
  293. D* Design: wd@denx.de
  294. C* Coding: wd@denx.de
  295. V* Verification: dzu@denx.de
  296. ***********************************************************************/
  297. static long int dram_size (long int mamr_value, long int *base, long int maxsize)
  298. {
  299. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  300. volatile memctl8xx_t *memctl = &immr->im_memctl;
  301. memctl->memc_mamr = mamr_value;
  302. return (get_ram_size(base, maxsize));
  303. }
  304. /* ------------------------------------------------------------------------- */
  305. #ifndef PB_ENET_TENA
  306. # define PB_ENET_TENA ((uint)0x00002000) /* PB 18 */
  307. #endif
  308. /***********************************************************************
  309. F* Function: int board_early_init_f (void) P*A*Z*
  310. *
  311. P* Parameters: none
  312. P*
  313. P* Returnvalue: int
  314. P* - 0 is always returned.
  315. *
  316. Z* Intention: This function is the board_early_init_f() method implementation
  317. Z* for the lwmon board.
  318. Z* Disable Ethernet TENA on Port B.
  319. *
  320. D* Design: wd@denx.de
  321. C* Coding: wd@denx.de
  322. V* Verification: dzu@denx.de
  323. ***********************************************************************/
  324. int board_early_init_f (void)
  325. {
  326. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  327. /* Disable Ethernet TENA on Port B
  328. * Necessary because of pull up in COM3 port.
  329. *
  330. * This is just a preliminary fix, intended to turn off TENA
  331. * as soon as possible to avoid noise on the network. Once
  332. * I²C is running we will make sure the interface is
  333. * correctly initialized.
  334. */
  335. immr->im_cpm.cp_pbpar &= ~PB_ENET_TENA;
  336. immr->im_cpm.cp_pbodr &= ~PB_ENET_TENA;
  337. immr->im_cpm.cp_pbdat &= ~PB_ENET_TENA; /* set to 0 = disabled */
  338. immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
  339. return (0);
  340. }
  341. /* ------------------------------------------------------------------------- */
  342. /***********************************************************************
  343. F* Function: void reset_phy (void) P*A*Z*
  344. *
  345. P* Parameters: none
  346. P*
  347. P* Returnvalue: none
  348. *
  349. Z* Intention: Reset the PHY. In the lwmon case we do this by the
  350. Z* signaling the PIC I/O expander.
  351. *
  352. D* Design: wd@denx.de
  353. C* Coding: wd@denx.de
  354. V* Verification: dzu@denx.de
  355. ***********************************************************************/
  356. void reset_phy (void)
  357. {
  358. uchar c;
  359. #ifdef DEBUG
  360. printf ("### Switch on Ethernet for SCC2 ###\n");
  361. #endif
  362. c = pic_read (0x61);
  363. #ifdef DEBUG
  364. printf ("Old PIC read: reg_61 = 0x%02x\n", c);
  365. #endif
  366. c |= 0x40; /* disable COM3 */
  367. c &= ~0x80; /* enable Ethernet */
  368. pic_write (0x61, c);
  369. #ifdef DEBUG
  370. c = pic_read (0x61);
  371. printf ("New PIC read: reg_61 = 0x%02x\n", c);
  372. #endif
  373. udelay (1000);
  374. }
  375. /*------------------------- Keyboard controller -----------------------*/
  376. /* command codes */
  377. #define KEYBD_CMD_READ_KEYS 0x01
  378. #define KEYBD_CMD_READ_VERSION 0x02
  379. #define KEYBD_CMD_READ_STATUS 0x03
  380. #define KEYBD_CMD_RESET_ERRORS 0x10
  381. /* status codes */
  382. #define KEYBD_STATUS_MASK 0x3F
  383. #define KEYBD_STATUS_H_RESET 0x20
  384. #define KEYBD_STATUS_BROWNOUT 0x10
  385. #define KEYBD_STATUS_WD_RESET 0x08
  386. #define KEYBD_STATUS_OVERLOAD 0x04
  387. #define KEYBD_STATUS_ILLEGAL_WR 0x02
  388. #define KEYBD_STATUS_ILLEGAL_RD 0x01
  389. /* Number of bytes returned from Keyboard Controller */
  390. #define KEYBD_VERSIONLEN 2 /* version information */
  391. #define KEYBD_DATALEN 9 /* normal key scan data */
  392. /* maximum number of "magic" key codes that can be assigned */
  393. static uchar kbd_addr = CFG_I2C_KEYBD_ADDR;
  394. static uchar *key_match (uchar *);
  395. #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
  396. /***********************************************************************
  397. F* Function: int board_postclk_init (void) P*A*Z*
  398. *
  399. P* Parameters: none
  400. P*
  401. P* Returnvalue: int
  402. P* - 0 is always returned.
  403. *
  404. Z* Intention: This function is the board_postclk_init() method implementation
  405. Z* for the lwmon board.
  406. *
  407. ***********************************************************************/
  408. int board_postclk_init (void)
  409. {
  410. kbd_init();
  411. #ifdef CONFIG_MODEM_SUPPORT
  412. if (key_pressed()) {
  413. disable_putc(); /* modem doesn't understand banner etc */
  414. gd->do_mdm_init = 1;
  415. }
  416. #endif
  417. return (0);
  418. }
  419. struct serial_device * default_serial_console (void)
  420. {
  421. return gd->do_mdm_init ? &serial_scc_device : &serial_smc_device;
  422. }
  423. static void kbd_init (void)
  424. {
  425. uchar kbd_data[KEYBD_DATALEN];
  426. uchar tmp_data[KEYBD_DATALEN];
  427. uchar val, errcd;
  428. int i;
  429. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  430. gd->kbd_status = 0;
  431. /* Forced by PIC. Delays <= 175us loose */
  432. udelay(1000);
  433. /* Read initial keyboard error code */
  434. val = KEYBD_CMD_READ_STATUS;
  435. i2c_write (kbd_addr, 0, 0, &val, 1);
  436. i2c_read (kbd_addr, 0, 0, &errcd, 1);
  437. /* clear unused bits */
  438. errcd &= KEYBD_STATUS_MASK;
  439. /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
  440. errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
  441. if (errcd) {
  442. gd->kbd_status |= errcd << 8;
  443. }
  444. /* Reset error code and verify */
  445. val = KEYBD_CMD_RESET_ERRORS;
  446. i2c_write (kbd_addr, 0, 0, &val, 1);
  447. udelay(1000); /* delay NEEDED by keyboard PIC !!! */
  448. val = KEYBD_CMD_READ_STATUS;
  449. i2c_write (kbd_addr, 0, 0, &val, 1);
  450. i2c_read (kbd_addr, 0, 0, &val, 1);
  451. val &= KEYBD_STATUS_MASK; /* clear unused bits */
  452. if (val) { /* permanent error, report it */
  453. gd->kbd_status |= val;
  454. return;
  455. }
  456. /*
  457. * Read current keyboard state.
  458. *
  459. * After the error reset it may take some time before the
  460. * keyboard PIC picks up a valid keyboard scan - the total
  461. * scan time is approx. 1.6 ms (information by Martin Rajek,
  462. * 28 Sep 2002). We read a couple of times for the keyboard
  463. * to stabilize, using a big enough delay.
  464. * 10 times should be enough. If the data is still changing,
  465. * we use what we get :-(
  466. */
  467. memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
  468. for (i=0; i<10; ++i) {
  469. val = KEYBD_CMD_READ_KEYS;
  470. i2c_write (kbd_addr, 0, 0, &val, 1);
  471. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  472. if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
  473. /* consistent state, done */
  474. break;
  475. }
  476. /* remeber last state, delay, and retry */
  477. memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
  478. udelay (5000);
  479. }
  480. }
  481. /***********************************************************************
  482. F* Function: int misc_init_r (void) P*A*Z*
  483. *
  484. P* Parameters: none
  485. P*
  486. P* Returnvalue: int
  487. P* - 0 is always returned, even in the case of a keyboard
  488. P* error.
  489. *
  490. Z* Intention: This function is the misc_init_r() method implementation
  491. Z* for the lwmon board.
  492. Z* The keyboard controller is initialized and the result
  493. Z* of a read copied to the environment variable "keybd".
  494. Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
  495. Z* this key, and if found display to the LCD will be enabled.
  496. Z* The keys in "keybd" are checked against the magic
  497. Z* keycommands defined in the environment.
  498. Z* See also key_match().
  499. *
  500. D* Design: wd@denx.de
  501. C* Coding: wd@denx.de
  502. V* Verification: dzu@denx.de
  503. ***********************************************************************/
  504. int misc_init_r (void)
  505. {
  506. uchar kbd_data[KEYBD_DATALEN];
  507. char keybd_env[2 * KEYBD_DATALEN + 1];
  508. uchar kbd_init_status = gd->kbd_status >> 8;
  509. uchar kbd_status = gd->kbd_status;
  510. uchar val;
  511. char *str;
  512. int i;
  513. if (kbd_init_status) {
  514. printf ("KEYBD: Error %02X\n", kbd_init_status);
  515. }
  516. if (kbd_status) { /* permanent error, report it */
  517. printf ("*** Keyboard error code %02X ***\n", kbd_status);
  518. sprintf (keybd_env, "%02X", kbd_status);
  519. setenv ("keybd", keybd_env);
  520. return 0;
  521. }
  522. /*
  523. * Now we know that we have a working keyboard, so disable
  524. * all output to the LCD except when a key press is detected.
  525. */
  526. if ((console_assign (stdout, "serial") < 0) ||
  527. (console_assign (stderr, "serial") < 0)) {
  528. printf ("Can't assign serial port as output device\n");
  529. }
  530. /* Read Version */
  531. val = KEYBD_CMD_READ_VERSION;
  532. i2c_write (kbd_addr, 0, 0, &val, 1);
  533. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
  534. printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
  535. /* Read current keyboard state */
  536. val = KEYBD_CMD_READ_KEYS;
  537. i2c_write (kbd_addr, 0, 0, &val, 1);
  538. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  539. for (i = 0; i < KEYBD_DATALEN; ++i) {
  540. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  541. }
  542. setenv ("keybd", keybd_env);
  543. str = strdup ((char *)key_match (kbd_data)); /* decode keys */
  544. #ifdef KEYBD_SET_DEBUGMODE
  545. if (kbd_data[0] == KEYBD_SET_DEBUGMODE) { /* set debug mode */
  546. if ((console_assign (stdout, "lcd") < 0) ||
  547. (console_assign (stderr, "lcd") < 0)) {
  548. printf ("Can't assign LCD display as output device\n");
  549. }
  550. }
  551. #endif /* KEYBD_SET_DEBUGMODE */
  552. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  553. setenv ("preboot", str); /* set or delete definition */
  554. #endif /* CONFIG_PREBOOT */
  555. if (str != NULL) {
  556. free (str);
  557. }
  558. return (0);
  559. }
  560. #ifdef CONFIG_PREBOOT
  561. static uchar kbd_magic_prefix[] = "key_magic";
  562. static uchar kbd_command_prefix[] = "key_cmd";
  563. static int compare_magic (uchar *kbd_data, uchar *str)
  564. {
  565. uchar compare[KEYBD_DATALEN-1];
  566. char *nxt;
  567. int i;
  568. /* Don't include modifier byte */
  569. memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
  570. for (; str != NULL; str = (*nxt) ? (uchar *)(nxt+1) : (uchar *)nxt) {
  571. uchar c;
  572. int k;
  573. c = (uchar) simple_strtoul ((char *)str, (char **) (&nxt), 16);
  574. if (str == (uchar *)nxt) { /* invalid character */
  575. break;
  576. }
  577. /*
  578. * Check if this key matches the input.
  579. * Set matches to zero, so they match only once
  580. * and we can find duplicates or extra keys
  581. */
  582. for (k = 0; k < sizeof(compare); ++k) {
  583. if (compare[k] == '\0') /* only non-zero entries */
  584. continue;
  585. if (c == compare[k]) { /* found matching key */
  586. compare[k] = '\0';
  587. break;
  588. }
  589. }
  590. if (k == sizeof(compare)) {
  591. return -1; /* unmatched key */
  592. }
  593. }
  594. /*
  595. * A full match leaves no keys in the `compare' array,
  596. */
  597. for (i = 0; i < sizeof(compare); ++i) {
  598. if (compare[i])
  599. {
  600. return -1;
  601. }
  602. }
  603. return 0;
  604. }
  605. /***********************************************************************
  606. F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
  607. *
  608. P* Parameters: uchar *kbd_data
  609. P* - The keys to match against our magic definitions
  610. P*
  611. P* Returnvalue: uchar *
  612. P* - != NULL: Pointer to the corresponding command(s)
  613. P* NULL: No magic is about to happen
  614. *
  615. Z* Intention: Check if pressed key(s) match magic sequence,
  616. Z* and return the command string associated with that key(s).
  617. Z*
  618. Z* If no key press was decoded, NULL is returned.
  619. Z*
  620. Z* Note: the first character of the argument will be
  621. Z* overwritten with the "magic charcter code" of the
  622. Z* decoded key(s), or '\0'.
  623. Z*
  624. Z* Note: the string points to static environment data
  625. Z* and must be saved before you call any function that
  626. Z* modifies the environment.
  627. *
  628. D* Design: wd@denx.de
  629. C* Coding: wd@denx.de
  630. V* Verification: dzu@denx.de
  631. ***********************************************************************/
  632. static uchar *key_match (uchar *kbd_data)
  633. {
  634. char magic[sizeof (kbd_magic_prefix) + 1];
  635. uchar *suffix;
  636. char *kbd_magic_keys;
  637. /*
  638. * The following string defines the characters that can pe appended
  639. * to "key_magic" to form the names of environment variables that
  640. * hold "magic" key codes, i. e. such key codes that can cause
  641. * pre-boot actions. If the string is empty (""), then only
  642. * "key_magic" is checked (old behaviour); the string "125" causes
  643. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  644. */
  645. if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
  646. kbd_magic_keys = "";
  647. /* loop over all magic keys;
  648. * use '\0' suffix in case of empty string
  649. */
  650. for (suffix=(uchar *)kbd_magic_keys; *suffix || suffix==(uchar *)kbd_magic_keys; ++suffix) {
  651. sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
  652. #if 0
  653. printf ("### Check magic \"%s\"\n", magic);
  654. #endif
  655. if (compare_magic(kbd_data, (uchar *)getenv(magic)) == 0) {
  656. char cmd_name[sizeof (kbd_command_prefix) + 1];
  657. char *cmd;
  658. sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
  659. cmd = getenv (cmd_name);
  660. #if 0
  661. printf ("### Set PREBOOT to $(%s): \"%s\"\n",
  662. cmd_name, cmd ? cmd : "<<NULL>>");
  663. #endif
  664. *kbd_data = *suffix;
  665. return ((uchar *)cmd);
  666. }
  667. }
  668. #if 0
  669. printf ("### Delete PREBOOT\n");
  670. #endif
  671. *kbd_data = '\0';
  672. return (NULL);
  673. }
  674. #endif /* CONFIG_PREBOOT */
  675. /*---------------Board Special Commands: PIC read/write ---------------*/
  676. #if (CONFIG_COMMANDS & CFG_CMD_BSP)
  677. /***********************************************************************
  678. F* Function: int do_pic (cmd_tbl_t *cmdtp, int flag,
  679. F* int argc, char *argv[]) P*A*Z*
  680. *
  681. P* Parameters: cmd_tbl_t *cmdtp
  682. P* - Pointer to our command table entry
  683. P* int flag
  684. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  685. P* a repetition
  686. P* int argc
  687. P* - Argument count
  688. P* char *argv[]
  689. P* - Array of the actual arguments
  690. P*
  691. P* Returnvalue: int
  692. P* - 0 The command was handled successfully
  693. P* 1 An error occurred
  694. *
  695. Z* Intention: Implement the "pic [read|write]" commands.
  696. Z* The read subcommand takes one argument, the register,
  697. Z* whereas the write command takes two, the register and
  698. Z* the new value.
  699. *
  700. D* Design: wd@denx.de
  701. C* Coding: wd@denx.de
  702. V* Verification: dzu@denx.de
  703. ***********************************************************************/
  704. int do_pic (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  705. {
  706. uchar reg, val;
  707. switch (argc) {
  708. case 3: /* PIC read reg */
  709. if (strcmp (argv[1], "read") != 0)
  710. break;
  711. reg = simple_strtoul (argv[2], NULL, 16);
  712. printf ("PIC read: reg %02x: %02x\n\n", reg, pic_read (reg));
  713. return 0;
  714. case 4: /* PIC write reg val */
  715. if (strcmp (argv[1], "write") != 0)
  716. break;
  717. reg = simple_strtoul (argv[2], NULL, 16);
  718. val = simple_strtoul (argv[3], NULL, 16);
  719. printf ("PIC write: reg %02x val 0x%02x: %02x => ",
  720. reg, val, pic_read (reg));
  721. pic_write (reg, val);
  722. printf ("%02x\n\n", pic_read (reg));
  723. return 0;
  724. default:
  725. break;
  726. }
  727. printf ("Usage:\n%s\n", cmdtp->usage);
  728. return 1;
  729. }
  730. U_BOOT_CMD(
  731. pic, 4, 1, do_pic,
  732. "pic - read and write PIC registers\n",
  733. "read reg - read PIC register `reg'\n"
  734. "pic write reg val - write value `val' to PIC register `reg'\n"
  735. );
  736. /***********************************************************************
  737. F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
  738. F* int argc, char *argv[]) P*A*Z*
  739. *
  740. P* Parameters: cmd_tbl_t *cmdtp
  741. P* - Pointer to our command table entry
  742. P* int flag
  743. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  744. P* a repetition
  745. P* int argc
  746. P* - Argument count
  747. P* char *argv[]
  748. P* - Array of the actual arguments
  749. P*
  750. P* Returnvalue: int
  751. P* - 0 is always returned.
  752. *
  753. Z* Intention: Implement the "kbd" command.
  754. Z* The keyboard status is read. The result is printed on
  755. Z* the console and written into the "keybd" environment
  756. Z* variable.
  757. *
  758. D* Design: wd@denx.de
  759. C* Coding: wd@denx.de
  760. V* Verification: dzu@denx.de
  761. ***********************************************************************/
  762. int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  763. {
  764. uchar kbd_data[KEYBD_DATALEN];
  765. char keybd_env[2 * KEYBD_DATALEN + 1];
  766. uchar val;
  767. int i;
  768. #if 0 /* Done in kbd_init */
  769. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  770. #endif
  771. /* Read keys */
  772. val = KEYBD_CMD_READ_KEYS;
  773. i2c_write (kbd_addr, 0, 0, &val, 1);
  774. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  775. puts ("Keys:");
  776. for (i = 0; i < KEYBD_DATALEN; ++i) {
  777. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  778. printf (" %02x", kbd_data[i]);
  779. }
  780. putc ('\n');
  781. setenv ("keybd", keybd_env);
  782. return 0;
  783. }
  784. U_BOOT_CMD(
  785. kbd, 1, 1, do_kbd,
  786. "kbd - read keyboard status\n",
  787. NULL
  788. );
  789. /* Read and set LSB switch */
  790. #define CFG_PC_TXD1_ENA 0x0008 /* PC.12 */
  791. /***********************************************************************
  792. F* Function: int do_lsb (cmd_tbl_t *cmdtp, int flag,
  793. F* int argc, char *argv[]) P*A*Z*
  794. *
  795. P* Parameters: cmd_tbl_t *cmdtp
  796. P* - Pointer to our command table entry
  797. P* int flag
  798. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  799. P* a repetition
  800. P* int argc
  801. P* - Argument count
  802. P* char *argv[]
  803. P* - Array of the actual arguments
  804. P*
  805. P* Returnvalue: int
  806. P* - 0 The command was handled successfully
  807. P* 1 An error occurred
  808. *
  809. Z* Intention: Implement the "lsb [on|off]" commands.
  810. Z* The lsb is switched according to the first parameter by
  811. Z* by signaling the PIC I/O expander.
  812. Z* Called with no arguments, the current setting is
  813. Z* printed.
  814. *
  815. D* Design: wd@denx.de
  816. C* Coding: wd@denx.de
  817. V* Verification: dzu@denx.de
  818. ***********************************************************************/
  819. int do_lsb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  820. {
  821. uchar val;
  822. immap_t *immr = (immap_t *) CFG_IMMR;
  823. switch (argc) {
  824. case 1: /* lsb - print setting */
  825. val = pic_read (0x60);
  826. printf ("LSB is o%s\n", (val & 0x20) ? "n" : "ff");
  827. return 0;
  828. case 2: /* lsb on or lsb off - set switch */
  829. val = pic_read (0x60);
  830. if (strcmp (argv[1], "on") == 0) {
  831. val |= 0x20;
  832. immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
  833. immr->im_ioport.iop_pcdat |= CFG_PC_TXD1_ENA;
  834. immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
  835. } else if (strcmp (argv[1], "off") == 0) {
  836. val &= ~0x20;
  837. immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
  838. immr->im_ioport.iop_pcdat &= ~(CFG_PC_TXD1_ENA);
  839. immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
  840. } else {
  841. break;
  842. }
  843. pic_write (0x60, val);
  844. return 0;
  845. default:
  846. break;
  847. }
  848. printf ("Usage:\n%s\n", cmdtp->usage);
  849. return 1;
  850. }
  851. U_BOOT_CMD(
  852. lsb, 2, 1, do_lsb,
  853. "lsb - check and set LSB switch\n",
  854. "on - switch LSB on\n"
  855. "lsb off - switch LSB off\n"
  856. "lsb - print current setting\n"
  857. );
  858. #endif /* CFG_CMD_BSP */
  859. /*----------------------------- Utilities -----------------------------*/
  860. /***********************************************************************
  861. F* Function: uchar pic_read (uchar reg) P*A*Z*
  862. *
  863. P* Parameters: uchar reg
  864. P* - Register to read
  865. P*
  866. P* Returnvalue: uchar
  867. P* - Value read from register
  868. *
  869. Z* Intention: Read a register from the PIC I/O expander.
  870. *
  871. D* Design: wd@denx.de
  872. C* Coding: wd@denx.de
  873. V* Verification: dzu@denx.de
  874. ***********************************************************************/
  875. uchar pic_read (uchar reg)
  876. {
  877. return (i2c_reg_read (CFG_I2C_PICIO_ADDR, reg));
  878. }
  879. /***********************************************************************
  880. F* Function: void pic_write (uchar reg, uchar val) P*A*Z*
  881. *
  882. P* Parameters: uchar reg
  883. P* - Register to read
  884. P* uchar val
  885. P* - Value to write
  886. P*
  887. P* Returnvalue: none
  888. *
  889. Z* Intention: Write to a register on the PIC I/O expander.
  890. *
  891. D* Design: wd@denx.de
  892. C* Coding: wd@denx.de
  893. V* Verification: dzu@denx.de
  894. ***********************************************************************/
  895. void pic_write (uchar reg, uchar val)
  896. {
  897. i2c_reg_write (CFG_I2C_PICIO_ADDR, reg, val);
  898. }
  899. /*---------------------- Board Control Functions ----------------------*/
  900. /***********************************************************************
  901. F* Function: void board_poweroff (void) P*A*Z*
  902. *
  903. P* Parameters: none
  904. P*
  905. P* Returnvalue: none
  906. *
  907. Z* Intention: Turn off the battery power and loop endless, so this
  908. Z* should better be the last function you call...
  909. *
  910. D* Design: wd@denx.de
  911. C* Coding: wd@denx.de
  912. V* Verification: dzu@denx.de
  913. ***********************************************************************/
  914. void board_poweroff (void)
  915. {
  916. /* Turn battery off */
  917. ((volatile immap_t *)CFG_IMMR)->im_ioport.iop_pcdat &= ~(1 << (31 - 13));
  918. while (1);
  919. }
  920. #ifdef CONFIG_MODEM_SUPPORT
  921. static int key_pressed(void)
  922. {
  923. uchar kbd_data[KEYBD_DATALEN];
  924. uchar val;
  925. /* Read keys */
  926. val = KEYBD_CMD_READ_KEYS;
  927. i2c_write (kbd_addr, 0, 0, &val, 1);
  928. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  929. return (compare_magic(kbd_data, (uchar *)CONFIG_MODEM_KEY_MAGIC) == 0);
  930. }
  931. #endif /* CONFIG_MODEM_SUPPORT */
  932. #ifdef CONFIG_POST
  933. /*
  934. * Returns 1 if keys pressed to start the power-on long-running tests
  935. * Called from board_init_f().
  936. */
  937. int post_hotkeys_pressed(void)
  938. {
  939. uchar kbd_data[KEYBD_DATALEN];
  940. uchar val;
  941. /* Read keys */
  942. val = KEYBD_CMD_READ_KEYS;
  943. i2c_write (kbd_addr, 0, 0, &val, 1);
  944. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  945. return (compare_magic(kbd_data, (uchar *)CONFIG_POST_KEY_MAGIC) == 0);
  946. }
  947. #endif