lwmon.c 30 KB

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