lwmon.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /***********************************************************************
  2. *
  3. M* Modul: lwmon.c
  4. M*
  5. M* Content: LWMON specific U-Boot commands.
  6. *
  7. * (C) Copyright 2001, 2002
  8. * DENX Software Engineering
  9. * Wolfgang Denk, wd@denx.de
  10. * All rights reserved.
  11. *
  12. D* Design: wd@denx.de
  13. C* Coding: wd@denx.de
  14. V* Verification: dzu@denx.de
  15. *
  16. * See file CREDITS for list of people who contributed to this
  17. * project.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License as
  21. * published by the Free Software Foundation; either version 2 of
  22. * the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. ***********************************************************************/
  34. /*---------------------------- Headerfiles ----------------------------*/
  35. #include <common.h>
  36. #include <mpc8xx.h>
  37. #include <commproc.h>
  38. #include <i2c.h>
  39. #include <command.h>
  40. #include <cmd_bsp.h>
  41. #include <malloc.h>
  42. #include <post.h>
  43. #include <linux/types.h>
  44. #include <linux/string.h> /* for strdup */
  45. /*------------------------ Local prototypes ---------------------------*/
  46. static long int dram_size (long int, long int *, long int);
  47. 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(CFG_MEMORY_75) || defined(CFG_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 CFG_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: Litronic Monitor IV\n");
  180. return (0);
  181. }
  182. /***********************************************************************
  183. F* Function: long int 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. long int initdram (int board_type)
  201. {
  202. volatile immap_t *immr = (immap_t *) CFG_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 = CFG_MPTPR;
  212. /* burst length=4, burst type=sequential, CAS latency=2 */
  213. memctl->memc_mar = CFG_MAR;
  214. /*
  215. * Map controller bank 3 to the SDRAM bank at preliminary address.
  216. */
  217. memctl->memc_or3 = CFG_OR3_PRELIM;
  218. memctl->memc_br3 = CFG_BR3_PRELIM;
  219. /* initialize memory address register */
  220. memctl->memc_mamr = CFG_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_PTBE; /* 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 (CFG_MAMR_8COL, (ulong *)SDRAM_BASE3_PRELIM, SDRAM_MAX_SIZE);
  247. udelay (1000);
  248. /*
  249. * try 9 column mode
  250. */
  251. size9 = dram_size (CFG_MAMR_9COL, (ulong *)SDRAM_BASE3_PRELIM, SDRAM_MAX_SIZE);
  252. if (size8 < size9) { /* leave configuration at 9 columns */
  253. size_b0 = size9;
  254. memctl->memc_mamr = CFG_MAMR_9COL | MAMR_PTBE;
  255. udelay (500);
  256. } else { /* back to 8 columns */
  257. size_b0 = size8;
  258. memctl->memc_mamr = CFG_MAMR_8COL | MAMR_PTBE;
  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 = (CFG_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 *) CFG_IMMR;
  299. volatile memctl8xx_t *memctl = &immr->im_memctl;
  300. volatile long int *addr;
  301. ulong cnt, val;
  302. ulong save[32]; /* to make test non-destructive */
  303. unsigned char i = 0;
  304. memctl->memc_mamr = mamr_value;
  305. for (cnt = maxsize / sizeof (long); cnt > 0; cnt >>= 1) {
  306. addr = base + cnt; /* pointer arith! */
  307. save[i++] = *addr;
  308. *addr = ~cnt;
  309. }
  310. /* write 0 to base address */
  311. addr = base;
  312. save[i] = *addr;
  313. *addr = 0;
  314. /* check at base address */
  315. if ((val = *addr) != 0) {
  316. *addr = save[i];
  317. return (0);
  318. }
  319. for (cnt = 1; cnt <= maxsize / sizeof (long); cnt <<= 1) {
  320. addr = base + cnt; /* pointer arith! */
  321. val = *addr;
  322. *addr = save[--i];
  323. if (val != (~cnt)) {
  324. return (cnt * sizeof (long));
  325. }
  326. }
  327. return (maxsize);
  328. }
  329. /* ------------------------------------------------------------------------- */
  330. #ifndef PB_ENET_TENA
  331. # define PB_ENET_TENA ((uint)0x00002000) /* PB 18 */
  332. #endif
  333. /***********************************************************************
  334. F* Function: int board_pre_init (void) P*A*Z*
  335. *
  336. P* Parameters: none
  337. P*
  338. P* Returnvalue: int
  339. P* - 0 is always returned.
  340. *
  341. Z* Intention: This function is the board_pre_init() method implementation
  342. Z* for the lwmon board.
  343. Z* Disable Ethernet TENA on Port B.
  344. *
  345. D* Design: wd@denx.de
  346. C* Coding: wd@denx.de
  347. V* Verification: dzu@denx.de
  348. ***********************************************************************/
  349. int board_pre_init (void)
  350. {
  351. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  352. /* Disable Ethernet TENA on Port B
  353. * Necessary because of pull up in COM3 port.
  354. *
  355. * This is just a preliminary fix, intended to turn off TENA
  356. * as soon as possible to avoid noise on the network. Once
  357. * I²C is running we will make sure the interface is
  358. * correctly initialized.
  359. */
  360. immr->im_cpm.cp_pbpar &= ~PB_ENET_TENA;
  361. immr->im_cpm.cp_pbodr &= ~PB_ENET_TENA;
  362. immr->im_cpm.cp_pbdat &= ~PB_ENET_TENA; /* set to 0 = disabled */
  363. immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
  364. return (0);
  365. }
  366. /* ------------------------------------------------------------------------- */
  367. /***********************************************************************
  368. F* Function: void reset_phy (void) P*A*Z*
  369. *
  370. P* Parameters: none
  371. P*
  372. P* Returnvalue: none
  373. *
  374. Z* Intention: Reset the PHY. In the lwmon case we do this by the
  375. Z* signaling the PIC I/O expander.
  376. *
  377. D* Design: wd@denx.de
  378. C* Coding: wd@denx.de
  379. V* Verification: dzu@denx.de
  380. ***********************************************************************/
  381. void reset_phy (void)
  382. {
  383. uchar c;
  384. #ifdef DEBUG
  385. printf ("### Switch on Ethernet for SCC2 ###\n");
  386. #endif
  387. c = pic_read (0x61);
  388. #ifdef DEBUG
  389. printf ("Old PIC read: reg_61 = 0x%02x\n", c);
  390. #endif
  391. c |= 0x40; /* disable COM3 */
  392. c &= ~0x80; /* enable Ethernet */
  393. pic_write (0x61, c);
  394. #ifdef DEBUG
  395. c = pic_read (0x61);
  396. printf ("New PIC read: reg_61 = 0x%02x\n", c);
  397. #endif
  398. udelay (1000);
  399. }
  400. /*------------------------- Keyboard controller -----------------------*/
  401. /* command codes */
  402. #define KEYBD_CMD_READ_KEYS 0x01
  403. #define KEYBD_CMD_READ_VERSION 0x02
  404. #define KEYBD_CMD_READ_STATUS 0x03
  405. #define KEYBD_CMD_RESET_ERRORS 0x10
  406. /* status codes */
  407. #define KEYBD_STATUS_MASK 0x3F
  408. #define KEYBD_STATUS_H_RESET 0x20
  409. #define KEYBD_STATUS_BROWNOUT 0x10
  410. #define KEYBD_STATUS_WD_RESET 0x08
  411. #define KEYBD_STATUS_OVERLOAD 0x04
  412. #define KEYBD_STATUS_ILLEGAL_WR 0x02
  413. #define KEYBD_STATUS_ILLEGAL_RD 0x01
  414. /* Number of bytes returned from Keyboard Controller */
  415. #define KEYBD_VERSIONLEN 2 /* version information */
  416. #define KEYBD_DATALEN 9 /* normal key scan data */
  417. /* maximum number of "magic" key codes that can be assigned */
  418. static uchar kbd_addr = CFG_I2C_KEYBD_ADDR;
  419. static uchar *key_match (uchar *);
  420. #define KEYBD_SET_DEBUGMODE '#' /* Magic key to enable debug output */
  421. /***********************************************************************
  422. F* Function: int board_postclk_init (void) P*A*Z*
  423. *
  424. P* Parameters: none
  425. P*
  426. P* Returnvalue: int
  427. P* - 0 is always returned.
  428. *
  429. Z* Intention: This function is the board_postclk_init() method implementation
  430. Z* for the lwmon board.
  431. *
  432. ***********************************************************************/
  433. int board_postclk_init (void)
  434. {
  435. DECLARE_GLOBAL_DATA_PTR;
  436. kbd_init();
  437. #ifdef CONFIG_MODEM_SUPPORT
  438. if (key_pressed()) {
  439. disable_putc(); /* modem doesn't understand banner etc */
  440. gd->do_mdm_init = 1;
  441. }
  442. #endif
  443. return (0);
  444. }
  445. static void kbd_init (void)
  446. {
  447. DECLARE_GLOBAL_DATA_PTR;
  448. uchar kbd_data[KEYBD_DATALEN];
  449. uchar tmp_data[KEYBD_DATALEN];
  450. uchar val, errcd;
  451. int i;
  452. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  453. gd->kbd_status = 0;
  454. /* Read initial keyboard error code */
  455. val = KEYBD_CMD_READ_STATUS;
  456. i2c_write (kbd_addr, 0, 0, &val, 1);
  457. i2c_read (kbd_addr, 0, 0, &errcd, 1);
  458. /* clear unused bits */
  459. errcd &= KEYBD_STATUS_MASK;
  460. /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
  461. errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
  462. if (errcd) {
  463. gd->kbd_status |= errcd << 8;
  464. }
  465. /* Reset error code and verify */
  466. val = KEYBD_CMD_RESET_ERRORS;
  467. i2c_write (kbd_addr, 0, 0, &val, 1);
  468. udelay(1000); /* delay NEEDED by keyboard PIC !!! */
  469. val = KEYBD_CMD_READ_STATUS;
  470. i2c_write (kbd_addr, 0, 0, &val, 1);
  471. i2c_read (kbd_addr, 0, 0, &val, 1);
  472. val &= KEYBD_STATUS_MASK; /* clear unused bits */
  473. if (val) { /* permanent error, report it */
  474. gd->kbd_status |= val;
  475. return;
  476. }
  477. /*
  478. * Read current keyboard state.
  479. *
  480. * After the error reset it may take some time before the
  481. * keyboard PIC picks up a valid keyboard scan - the total
  482. * scan time is approx. 1.6 ms (information by Martin Rajek,
  483. * 28 Sep 2002). We read a couple of times for the keyboard
  484. * to stabilize, using a big enough delay.
  485. * 10 times should be enough. If the data is still changing,
  486. * we use what we get :-(
  487. */
  488. memset (tmp_data, 0xFF, KEYBD_DATALEN); /* impossible value */
  489. for (i=0; i<10; ++i) {
  490. val = KEYBD_CMD_READ_KEYS;
  491. i2c_write (kbd_addr, 0, 0, &val, 1);
  492. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  493. if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
  494. /* consistent state, done */
  495. break;
  496. }
  497. /* remeber last state, delay, and retry */
  498. memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
  499. udelay (5000);
  500. }
  501. }
  502. /***********************************************************************
  503. F* Function: int misc_init_r (void) P*A*Z*
  504. *
  505. P* Parameters: none
  506. P*
  507. P* Returnvalue: int
  508. P* - 0 is always returned, even in the case of a keyboard
  509. P* error.
  510. *
  511. Z* Intention: This function is the misc_init_r() method implementation
  512. Z* for the lwmon board.
  513. Z* The keyboard controller is initialized and the result
  514. Z* of a read copied to the environment variable "keybd".
  515. Z* If KEYBD_SET_DEBUGMODE is defined, a check is made for
  516. Z* this key, and if found display to the LCD will be enabled.
  517. Z* The keys in "keybd" are checked against the magic
  518. Z* keycommands defined in the environment.
  519. Z* See also key_match().
  520. *
  521. D* Design: wd@denx.de
  522. C* Coding: wd@denx.de
  523. V* Verification: dzu@denx.de
  524. ***********************************************************************/
  525. int misc_init_r (void)
  526. {
  527. DECLARE_GLOBAL_DATA_PTR;
  528. uchar kbd_data[KEYBD_DATALEN];
  529. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  530. uchar kbd_init_status = gd->kbd_status >> 8;
  531. uchar kbd_status = gd->kbd_status;
  532. uchar val;
  533. uchar *str;
  534. int i;
  535. if (kbd_init_status) {
  536. printf ("KEYBD: Error %02X\n", kbd_init_status);
  537. }
  538. if (kbd_status) { /* permanent error, report it */
  539. printf ("*** Keyboard error code %02X ***\n", kbd_status);
  540. sprintf (keybd_env, "%02X", kbd_status);
  541. setenv ("keybd", keybd_env);
  542. return 0;
  543. }
  544. /*
  545. * Now we know that we have a working keyboard, so disable
  546. * all output to the LCD except when a key press is detected.
  547. */
  548. if ((console_assign (stdout, "serial") < 0) ||
  549. (console_assign (stderr, "serial") < 0)) {
  550. printf ("Can't assign serial port as output device\n");
  551. }
  552. /* Read Version */
  553. val = KEYBD_CMD_READ_VERSION;
  554. i2c_write (kbd_addr, 0, 0, &val, 1);
  555. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_VERSIONLEN);
  556. printf ("KEYBD: Version %d.%d\n", kbd_data[0], kbd_data[1]);
  557. /* Read current keyboard state */
  558. val = KEYBD_CMD_READ_KEYS;
  559. i2c_write (kbd_addr, 0, 0, &val, 1);
  560. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  561. for (i = 0; i < KEYBD_DATALEN; ++i) {
  562. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  563. }
  564. setenv ("keybd", keybd_env);
  565. str = strdup (key_match (kbd_data)); /* decode keys */
  566. #ifdef KEYBD_SET_DEBUGMODE
  567. if (kbd_data[0] == KEYBD_SET_DEBUGMODE) { /* set debug mode */
  568. if ((console_assign (stdout, "lcd") < 0) ||
  569. (console_assign (stderr, "lcd") < 0)) {
  570. printf ("Can't assign LCD display as output device\n");
  571. }
  572. }
  573. #endif /* KEYBD_SET_DEBUGMODE */
  574. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  575. setenv ("preboot", str); /* set or delete definition */
  576. #endif /* CONFIG_PREBOOT */
  577. if (str != NULL) {
  578. free (str);
  579. }
  580. return (0);
  581. }
  582. #ifdef CONFIG_PREBOOT
  583. static uchar kbd_magic_prefix[] = "key_magic";
  584. static uchar kbd_command_prefix[] = "key_cmd";
  585. static int compare_magic (uchar *kbd_data, uchar *str)
  586. {
  587. uchar compare[KEYBD_DATALEN-1];
  588. uchar *nxt;
  589. int i;
  590. /* Don't include modifier byte */
  591. memcpy (compare, kbd_data+1, KEYBD_DATALEN-1);
  592. for (; str != NULL; str = (*nxt) ? nxt+1 : nxt) {
  593. uchar c;
  594. int k;
  595. c = (uchar) simple_strtoul (str, (char **) (&nxt), 16);
  596. if (str == nxt) { /* invalid character */
  597. break;
  598. }
  599. /*
  600. * Check if this key matches the input.
  601. * Set matches to zero, so they match only once
  602. * and we can find duplicates or extra keys
  603. */
  604. for (k = 0; k < sizeof(compare); ++k) {
  605. if (compare[k] == '\0') /* only non-zero entries */
  606. continue;
  607. if (c == compare[k]) { /* found matching key */
  608. compare[k] = '\0';
  609. break;
  610. }
  611. }
  612. if (k == sizeof(compare)) {
  613. return -1; /* unmatched key */
  614. }
  615. }
  616. /*
  617. * A full match leaves no keys in the `compare' array,
  618. */
  619. for (i = 0; i < sizeof(compare); ++i) {
  620. if (compare[i])
  621. {
  622. return -1;
  623. }
  624. }
  625. return 0;
  626. }
  627. /***********************************************************************
  628. F* Function: static uchar *key_match (uchar *kbd_data) P*A*Z*
  629. *
  630. P* Parameters: uchar *kbd_data
  631. P* - The keys to match against our magic definitions
  632. P*
  633. P* Returnvalue: uchar *
  634. P* - != NULL: Pointer to the corresponding command(s)
  635. P* NULL: No magic is about to happen
  636. *
  637. Z* Intention: Check if pressed key(s) match magic sequence,
  638. Z* and return the command string associated with that key(s).
  639. Z*
  640. Z* If no key press was decoded, NULL is returned.
  641. Z*
  642. Z* Note: the first character of the argument will be
  643. Z* overwritten with the "magic charcter code" of the
  644. Z* decoded key(s), or '\0'.
  645. Z*
  646. Z* Note: the string points to static environment data
  647. Z* and must be saved before you call any function that
  648. Z* modifies the environment.
  649. *
  650. D* Design: wd@denx.de
  651. C* Coding: wd@denx.de
  652. V* Verification: dzu@denx.de
  653. ***********************************************************************/
  654. static uchar *key_match (uchar *kbd_data)
  655. {
  656. uchar magic[sizeof (kbd_magic_prefix) + 1];
  657. uchar *suffix;
  658. uchar *kbd_magic_keys;
  659. /*
  660. * The following string defines the characters that can pe appended
  661. * to "key_magic" to form the names of environment variables that
  662. * hold "magic" key codes, i. e. such key codes that can cause
  663. * pre-boot actions. If the string is empty (""), then only
  664. * "key_magic" is checked (old behaviour); the string "125" causes
  665. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  666. */
  667. if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
  668. kbd_magic_keys = "";
  669. /* loop over all magic keys;
  670. * use '\0' suffix in case of empty string
  671. */
  672. for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix) {
  673. sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
  674. #if 0
  675. printf ("### Check magic \"%s\"\n", magic);
  676. #endif
  677. if (compare_magic(kbd_data, getenv(magic)) == 0) {
  678. uchar cmd_name[sizeof (kbd_command_prefix) + 1];
  679. char *cmd;
  680. sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
  681. cmd = getenv (cmd_name);
  682. #if 0
  683. printf ("### Set PREBOOT to $(%s): \"%s\"\n",
  684. cmd_name, cmd ? cmd : "<<NULL>>");
  685. #endif
  686. *kbd_data = *suffix;
  687. return (cmd);
  688. }
  689. }
  690. #if 0
  691. printf ("### Delete PREBOOT\n");
  692. #endif
  693. *kbd_data = '\0';
  694. return (NULL);
  695. }
  696. #endif /* CONFIG_PREBOOT */
  697. /*---------------Board Special Commands: PIC read/write ---------------*/
  698. #if (CONFIG_COMMANDS & CFG_CMD_BSP)
  699. /***********************************************************************
  700. F* Function: int do_pic (cmd_tbl_t *cmdtp, int flag,
  701. F* int argc, char *argv[]) P*A*Z*
  702. *
  703. P* Parameters: cmd_tbl_t *cmdtp
  704. P* - Pointer to our command table entry
  705. P* int flag
  706. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  707. P* a repetition
  708. P* int argc
  709. P* - Argument count
  710. P* char *argv[]
  711. P* - Array of the actual arguments
  712. P*
  713. P* Returnvalue: int
  714. P* - 0 The command was handled successfully
  715. P* 1 An error occurred
  716. *
  717. Z* Intention: Implement the "pic [read|write]" commands.
  718. Z* The read subcommand takes one argument, the register,
  719. Z* whereas the write command takes two, the register and
  720. Z* the new value.
  721. *
  722. D* Design: wd@denx.de
  723. C* Coding: wd@denx.de
  724. V* Verification: dzu@denx.de
  725. ***********************************************************************/
  726. int do_pic (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  727. {
  728. uchar reg, val;
  729. switch (argc) {
  730. case 3: /* PIC read reg */
  731. if (strcmp (argv[1], "read") != 0)
  732. break;
  733. reg = simple_strtoul (argv[2], NULL, 16);
  734. printf ("PIC read: reg %02x: %02x\n\n", reg, pic_read (reg));
  735. return 0;
  736. case 4: /* PIC write reg val */
  737. if (strcmp (argv[1], "write") != 0)
  738. break;
  739. reg = simple_strtoul (argv[2], NULL, 16);
  740. val = simple_strtoul (argv[3], NULL, 16);
  741. printf ("PIC write: reg %02x val 0x%02x: %02x => ",
  742. reg, val, pic_read (reg));
  743. pic_write (reg, val);
  744. printf ("%02x\n\n", pic_read (reg));
  745. return 0;
  746. default:
  747. break;
  748. }
  749. printf ("Usage:\n%s\n", cmdtp->usage);
  750. return 1;
  751. }
  752. /***********************************************************************
  753. F* Function: int do_kbd (cmd_tbl_t *cmdtp, int flag,
  754. F* int argc, char *argv[]) P*A*Z*
  755. *
  756. P* Parameters: cmd_tbl_t *cmdtp
  757. P* - Pointer to our command table entry
  758. P* int flag
  759. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  760. P* a repetition
  761. P* int argc
  762. P* - Argument count
  763. P* char *argv[]
  764. P* - Array of the actual arguments
  765. P*
  766. P* Returnvalue: int
  767. P* - 0 is always returned.
  768. *
  769. Z* Intention: Implement the "kbd" command.
  770. Z* The keyboard status is read. The result is printed on
  771. Z* the console and written into the "keybd" environment
  772. Z* variable.
  773. *
  774. D* Design: wd@denx.de
  775. C* Coding: wd@denx.de
  776. V* Verification: dzu@denx.de
  777. ***********************************************************************/
  778. int do_kbd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  779. {
  780. uchar kbd_data[KEYBD_DATALEN];
  781. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  782. uchar val;
  783. int i;
  784. #if 0 /* Done in kbd_init */
  785. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  786. #endif
  787. /* Read keys */
  788. val = KEYBD_CMD_READ_KEYS;
  789. i2c_write (kbd_addr, 0, 0, &val, 1);
  790. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  791. puts ("Keys:");
  792. for (i = 0; i < KEYBD_DATALEN; ++i) {
  793. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  794. printf (" %02x", kbd_data[i]);
  795. }
  796. putc ('\n');
  797. setenv ("keybd", keybd_env);
  798. return 0;
  799. }
  800. /* Read and set LSB switch */
  801. #define CFG_PC_TXD1_ENA 0x0008 /* PC.12 */
  802. /***********************************************************************
  803. F* Function: int do_lsb (cmd_tbl_t *cmdtp, int flag,
  804. F* int argc, char *argv[]) P*A*Z*
  805. *
  806. P* Parameters: cmd_tbl_t *cmdtp
  807. P* - Pointer to our command table entry
  808. P* int flag
  809. P* - If the CMD_FLAG_REPEAT bit is set, then this call is
  810. P* a repetition
  811. P* int argc
  812. P* - Argument count
  813. P* char *argv[]
  814. P* - Array of the actual arguments
  815. P*
  816. P* Returnvalue: int
  817. P* - 0 The command was handled successfully
  818. P* 1 An error occurred
  819. *
  820. Z* Intention: Implement the "lsb [on|off]" commands.
  821. Z* The lsb is switched according to the first parameter by
  822. Z* by signaling the PIC I/O expander.
  823. Z* Called with no arguments, the current setting is
  824. Z* printed.
  825. *
  826. D* Design: wd@denx.de
  827. C* Coding: wd@denx.de
  828. V* Verification: dzu@denx.de
  829. ***********************************************************************/
  830. int do_lsb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  831. {
  832. uchar val;
  833. immap_t *immr = (immap_t *) CFG_IMMR;
  834. switch (argc) {
  835. case 1: /* lsb - print setting */
  836. val = pic_read (0x60);
  837. printf ("LSB is o%s\n", (val & 0x20) ? "n" : "ff");
  838. return 0;
  839. case 2: /* lsb on or lsb off - set switch */
  840. val = pic_read (0x60);
  841. if (strcmp (argv[1], "on") == 0) {
  842. val |= 0x20;
  843. immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
  844. immr->im_ioport.iop_pcdat |= CFG_PC_TXD1_ENA;
  845. immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
  846. } else if (strcmp (argv[1], "off") == 0) {
  847. val &= ~0x20;
  848. immr->im_ioport.iop_pcpar &= ~(CFG_PC_TXD1_ENA);
  849. immr->im_ioport.iop_pcdat &= ~(CFG_PC_TXD1_ENA);
  850. immr->im_ioport.iop_pcdir |= CFG_PC_TXD1_ENA;
  851. } else {
  852. break;
  853. }
  854. pic_write (0x60, val);
  855. return 0;
  856. default:
  857. break;
  858. }
  859. printf ("Usage:\n%s\n", cmdtp->usage);
  860. return 1;
  861. }
  862. #endif /* CFG_CMD_BSP */
  863. /*----------------------------- Utilities -----------------------------*/
  864. /***********************************************************************
  865. F* Function: uchar pic_read (uchar reg) P*A*Z*
  866. *
  867. P* Parameters: uchar reg
  868. P* - Register to read
  869. P*
  870. P* Returnvalue: uchar
  871. P* - Value read from register
  872. *
  873. Z* Intention: Read a register from the PIC I/O expander.
  874. *
  875. D* Design: wd@denx.de
  876. C* Coding: wd@denx.de
  877. V* Verification: dzu@denx.de
  878. ***********************************************************************/
  879. uchar pic_read (uchar reg)
  880. {
  881. return (i2c_reg_read (CFG_I2C_PICIO_ADDR, reg));
  882. }
  883. /***********************************************************************
  884. F* Function: void pic_write (uchar reg, uchar val) P*A*Z*
  885. *
  886. P* Parameters: uchar reg
  887. P* - Register to read
  888. P* uchar val
  889. P* - Value to write
  890. P*
  891. P* Returnvalue: none
  892. *
  893. Z* Intention: Write to a register on the PIC I/O expander.
  894. *
  895. D* Design: wd@denx.de
  896. C* Coding: wd@denx.de
  897. V* Verification: dzu@denx.de
  898. ***********************************************************************/
  899. void pic_write (uchar reg, uchar val)
  900. {
  901. i2c_reg_write (CFG_I2C_PICIO_ADDR, reg, val);
  902. }
  903. /*---------------------- Board Control Functions ----------------------*/
  904. /***********************************************************************
  905. F* Function: void board_poweroff (void) P*A*Z*
  906. *
  907. P* Parameters: none
  908. P*
  909. P* Returnvalue: none
  910. *
  911. Z* Intention: Turn off the battery power and loop endless, so this
  912. Z* should better be the last function you call...
  913. *
  914. D* Design: wd@denx.de
  915. C* Coding: wd@denx.de
  916. V* Verification: dzu@denx.de
  917. ***********************************************************************/
  918. void board_poweroff (void)
  919. {
  920. /* Turn battery off */
  921. ((volatile immap_t *)CFG_IMMR)->im_ioport.iop_pcdat &= ~(1 << (31 - 13));
  922. while (1);
  923. }
  924. #ifdef CONFIG_MODEM_SUPPORT
  925. static int key_pressed(void)
  926. {
  927. uchar kbd_data[KEYBD_DATALEN];
  928. uchar val;
  929. /* Read keys */
  930. val = KEYBD_CMD_READ_KEYS;
  931. i2c_write (kbd_addr, 0, 0, &val, 1);
  932. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  933. return (compare_magic(kbd_data, CONFIG_MODEM_KEY_MAGIC) == 0);
  934. }
  935. #endif /* CONFIG_MODEM_SUPPORT */