lwmon.c 31 KB

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