blob: c1b4d116ea843b5b2086911d9e712f74a4a72271 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/* most of this is taken from the file */
2/* hal/powerpc/cogent/current/src/hal_diag.c in the */
3/* Cygnus eCos source. Here is the copyright notice: */
4/* */
5/*============================================================================= */
6/* */
7/* hal_diag.c */
8/* */
9/* HAL diagnostic output code */
10/* */
11/*============================================================================= */
12/*####COPYRIGHTBEGIN#### */
13/* */
14/* ------------------------------------------- */
15/* The contents of this file are subject to the Cygnus eCos Public License */
16/* Version 1.0 (the "License"); you may not use this file except in */
17/* compliance with the License. You may obtain a copy of the License at */
18/* http://sourceware.cygnus.com/ecos */
19/* */
20/* Software distributed under the License is distributed on an "AS IS" */
21/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the */
22/* License for the specific language governing rights and limitations under */
23/* the License. */
24/* */
25/* The Original Code is eCos - Embedded Cygnus Operating System, released */
26/* September 30, 1998. */
27/* */
28/* The Initial Developer of the Original Code is Cygnus. Portions created */
29/* by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. */
30/* ------------------------------------------- */
31/* */
32/*####COPYRIGHTEND#### */
33/*============================================================================= */
34/*#####DESCRIPTIONBEGIN#### */
35/* */
36/* Author(s): nickg, jskov */
37/* Contributors: nickg, jskov */
38/* Date: 1999-03-23 */
39/* Purpose: HAL diagnostic output */
40/* Description: Implementations of HAL diagnostic output support. */
41/* */
42/*####DESCRIPTIONEND#### */
43/* */
44/*============================================================================= */
45
46/*----------------------------------------------------------------------------- */
47/* Cogent board specific LCD code */
48
49#include <common.h>
50#include <stdarg.h>
51#include <board/cogent/lcd.h>
52
53static char lines[2][LCD_LINE_LENGTH+1];
54static int curline;
55static int linepos;
56static int heartbeat_active;
57/* make the next two strings exactly LCD_LINE_LENGTH (16) chars long */
58/* pad to the right with spaces if necessary */
59static char init_line0[LCD_LINE_LENGTH+1] = "U-Boot Cogent ";
60static char init_line1[LCD_LINE_LENGTH+1] = "mjj, 11 Aug 2000";
61
62static inline unsigned char
63lcd_read_status(cma_mb_lcd *clp)
64{
65 /* read the Busy Status Register */
66 return (cma_mb_reg_read(&clp->lcd_bsr));
67}
68
69static inline void
70lcd_wait_not_busy(cma_mb_lcd *clp)
71{
72 /*
73 * wait for not busy
74 * Note: It seems that the LCD isn't quite ready to process commands
75 * when it clears the BUSY flag. Reading the status address an extra
76 * time seems to give it enough breathing room.
77 */
78
79 while (lcd_read_status(clp) & LCD_STAT_BUSY)
80 ;
81
82 (void)lcd_read_status(clp);
83}
84
85static inline void
86lcd_write_command(cma_mb_lcd *clp, unsigned char cmd)
87{
88 lcd_wait_not_busy(clp);
89
90 /* write the Command Register */
91 cma_mb_reg_write(&clp->lcd_cmd, cmd);
92}
93
94static inline void
95lcd_write_data(cma_mb_lcd *clp, unsigned char data)
96{
97 lcd_wait_not_busy(clp);
98
99 /* write the Current Character Register */
100 cma_mb_reg_write(&clp->lcd_ccr, data);
101}
102
103static inline void
104lcd_dis(int addr, char *string)
105{
106 cma_mb_lcd *clp = (cma_mb_lcd *)CMA_MB_LCD_BASE;
107 int pos, linelen;
108
109 linelen = LCD_LINE_LENGTH;
110 if (heartbeat_active && addr == LCD_LINE0)
111 linelen--;
112
113 lcd_write_command(clp, LCD_CMD_ADD + addr);
114 for (pos = 0; *string != '\0' && pos < linelen; pos++)
wdenk57b2d802003-06-27 21:31:46 +0000115 lcd_write_data(clp, *string++);
wdenkfe8c2802002-11-03 00:38:21 +0000116}
117
118void
119lcd_init(void)
120{
121 cma_mb_lcd *clp = (cma_mb_lcd *)CMA_MB_LCD_BASE;
122 int i;
123
124 /* configure the lcd for 8 bits/char, 2 lines and 5x7 dot matrix */
125 lcd_write_command(clp, LCD_CMD_MODE);
126
127 /* turn the LCD display on */
128 lcd_write_command(clp, LCD_CMD_DON);
129
130 curline = 0;
131 linepos = 0;
132
133 for (i = 0; i < LCD_LINE_LENGTH; i++) {
wdenk57b2d802003-06-27 21:31:46 +0000134 lines[0][i] = init_line0[i];
wdenkfe8c2802002-11-03 00:38:21 +0000135 lines[1][i] = init_line1[i];
136 }
137
138 lines[0][LCD_LINE_LENGTH] = lines[1][LCD_LINE_LENGTH] = 0;
139
140 lcd_dis(LCD_LINE0, lines[0]);
141 lcd_dis(LCD_LINE1, lines[1]);
142
143 printf("HD44780 2 line x %d char display\n", LCD_LINE_LENGTH);
144}
145
146void
147lcd_write_char(const char c)
148{
149 int i, linelen;
150
151 /* ignore CR */
152 if (c == '\r')
153 return;
154
155 linelen = LCD_LINE_LENGTH;
156 if (heartbeat_active && curline == 0)
157 linelen--;
158
159 if (c == '\n') {
wdenk57b2d802003-06-27 21:31:46 +0000160 lcd_dis(LCD_LINE0, &lines[curline^1][0]);
161 lcd_dis(LCD_LINE1, &lines[curline][0]);
wdenkfe8c2802002-11-03 00:38:21 +0000162
wdenk57b2d802003-06-27 21:31:46 +0000163 /* Do a line feed */
164 curline ^= 1;
wdenkfe8c2802002-11-03 00:38:21 +0000165 linelen = LCD_LINE_LENGTH;
166 if (heartbeat_active && curline == 0)
167 linelen--;
wdenk57b2d802003-06-27 21:31:46 +0000168 linepos = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000169
wdenk57b2d802003-06-27 21:31:46 +0000170 for (i = 0; i < linelen; i++)
171 lines[curline][i] = ' ';
wdenkfe8c2802002-11-03 00:38:21 +0000172
wdenk57b2d802003-06-27 21:31:46 +0000173 return;
wdenkfe8c2802002-11-03 00:38:21 +0000174 }
175
176 /* Only allow to be output if there is room on the LCD line */
177 if (linepos < linelen)
wdenk57b2d802003-06-27 21:31:46 +0000178 lines[curline][linepos++] = c;
wdenkfe8c2802002-11-03 00:38:21 +0000179}
180
181void
182lcd_flush(void)
183{
184 lcd_dis(LCD_LINE1, &lines[curline][0]);
185}
186
187void
188lcd_write_string(const char *s)
189{
190 char *p;
191
192 for (p = (char *)s; *p != '\0'; p++)
193 lcd_write_char(*p);
194}
195
196void
197lcd_printf(const char *fmt, ...)
198{
199 va_list args;
200 char buf[CFG_PBSIZE];
201
202 va_start(args, fmt);
203 (void)vsprintf(buf, fmt, args);
204 va_end(args);
205
206 lcd_write_string(buf);
207}
208
209void
210lcd_heartbeat(void)
211{
212 cma_mb_lcd *clp = (cma_mb_lcd *)CMA_MB_LCD_BASE;
213#if 0
214 static char rotchars[] = { '|', '/', '-', '\\' };
215#else
216 /* HD44780 Rom Code A00 has no backslash */
217 static char rotchars[] = { '|', '/', '-', '\315' };
218#endif
219 static int rotator_index = 0;
220
221 heartbeat_active = 1;
222
223 /* write the address */
224 lcd_write_command(clp, LCD_CMD_ADD + LCD_LINE0 + (LCD_LINE_LENGTH - 1));
225
226 /* write the next char in the sequence */
227 lcd_write_data(clp, rotchars[rotator_index]);
228
229 if (++rotator_index >= (sizeof rotchars / sizeof rotchars[0]))
230 rotator_index = 0;
231}