lwmon.c 28 KB

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