lwmon.c 31 KB

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