lwmon.c 30 KB

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