lwmon.c 31 KB

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