common/lcd.c: cleanup use of global variables
[oweals/u-boot.git] / board / mcc200 / lcd.c
1 /*
2  * (C) Copyright 2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA
19  */
20
21 #include <common.h>
22 #include <lcd.h>
23 #include <mpc5xxx.h>
24 #include <malloc.h>
25
26 #ifdef CONFIG_LCD
27
28 #undef SWAPPED_LCD /* For the previous h/w version */
29 /*
30  *  The name of the device used for communication
31  * with the PSoC.
32  */
33 #define PSOC_PSC        MPC5XXX_PSC2
34 #define PSOC_BAUD       230400UL
35
36 #define RTS_ASSERT      1
37 #define RTS_NEGATE      0
38 #define CTS_ASSERT      1
39 #define CTS_NEGATE      0
40
41 /*
42  * Dimensions in pixels
43  */
44 #define LCD_WIDTH       160
45 #define LCD_HEIGHT      100
46
47 /*
48  * Dimensions in bytes
49  */
50 #define LCD_BUF_SIZE    ((LCD_WIDTH*LCD_HEIGHT)>>3)
51
52 #if LCD_BPP != LCD_MONOCHROME
53 #error "MCC200 support only monochrome displays (1 bpp)!"
54 #endif
55
56 #define PSOC_RETRIES    10      /* each of PSOC_WAIT_TIME */
57 #define PSOC_WAIT_TIME  10      /* usec */
58
59 #include <video_font.h>
60 #define FONT_WIDTH      VIDEO_FONT_WIDTH
61
62 DECLARE_GLOBAL_DATA_PTR;
63
64 /*
65  * LCD information
66  */
67 vidinfo_t panel_info = {
68         LCD_WIDTH, LCD_HEIGHT, LCD_BPP
69 };
70
71 int lcd_line_length;
72
73 /*
74  * Frame buffer memory information
75  */
76 void *lcd_base;                 /* Start of framebuffer memory  */
77 void *lcd_console_address;      /* Start of console buffer      */
78
79 short console_col = 0;
80 short console_row = 0;
81
82 /*
83  *  The device we use to communicate with PSoC
84  */
85 int serial_inited = 0;
86
87 /*
88  * Exported functions
89  */
90 void lcd_initcolregs (void);
91 void lcd_ctrl_init (void *lcdbase);
92 void lcd_enable (void);
93
94 /*
95  *  Imported functions to support the PSoC protocol
96  */
97 extern int serial_init_dev (unsigned long dev_base);
98 extern void serial_setrts_dev (unsigned long dev_base, int s);
99 extern int serial_getcts_dev (unsigned long dev_base);
100 extern void serial_putc_raw_dev(unsigned long dev_base, const char c);
101
102 /*
103  *  Just stubs for our driver, needed for compiling compabilty with
104  * the common LCD driver code.
105  */
106 void lcd_initcolregs (void)
107 {
108 }
109
110 void lcd_ctrl_init (void *lcdbase)
111 {
112 }
113
114 /*
115  * Function sends the contents of the frame-buffer to the LCD
116  */
117 void lcd_enable (void)
118 {
119         int i, retries, fb_size;
120
121         if (!serial_inited) {
122                 unsigned long baud;
123
124                 baud = gd->baudrate;
125                 gd->baudrate = PSOC_BAUD;
126                 serial_init_dev(PSOC_PSC);
127                 gd->baudrate = baud;
128                 serial_setrts_dev (PSOC_PSC, RTS_ASSERT);
129                 serial_inited = 1;
130         }
131
132         /*
133          *  Implement PSoC communication protocol:
134          * 1. Assert RTS, wait CTS assertion
135          * 2. Transmit data
136          * 3. Negate RTS, wait CTS negation
137          */
138
139         /* 1 */
140         serial_setrts_dev (PSOC_PSC, RTS_ASSERT);
141         for (retries = PSOC_RETRIES; retries; retries--) {
142                 if (serial_getcts_dev(PSOC_PSC) == CTS_ASSERT)
143                         break;
144                 udelay (PSOC_WAIT_TIME);
145         }
146         if (!retries) {
147                 printf ("%s Error: PSoC doesn't respond on "
148                         "RTS ASSERT\n", __FUNCTION__);
149         }
150
151         /* 2 */
152         fb_size = panel_info.vl_row * (panel_info.vl_col >> 3);
153
154 #if !defined(SWAPPED_LCD)
155         for (i=0; i<fb_size; i++) {
156                 serial_putc_raw_dev (PSOC_PSC, ((char *)lcd_base)[i]);
157         }
158 #else
159     {
160         int x, y, pwidth;
161         char *p = (char *)lcd_base;
162
163         pwidth = ((panel_info.vl_col+7) >> 3);
164         for (y=0; y<panel_info.vl_row; y++) {
165                 i = y * pwidth;
166                 for (x=0; x<pwidth; x+=5) {
167                         serial_putc_raw_dev (PSOC_PSC, (p[i+x+2]<<4 & 0xF0) | (p[i+x+3]>>4 & 0x0F));
168                         serial_putc_raw_dev (PSOC_PSC, (p[i+x+3]<<4 & 0xF0) | (p[i+x+4]>>4 & 0x0F));
169                         serial_putc_raw_dev (PSOC_PSC, (p[i+x+4]<<4 & 0xF0) | (p[i+x]>>4 & 0x0F));
170                         serial_putc_raw_dev (PSOC_PSC, (p[i+x]<<4 & 0xF0) | (p[i+x+1]>>4 & 0x0F));
171                         serial_putc_raw_dev (PSOC_PSC, (p[i+x+1]<<4 & 0xF0) | (p[i+x+2]>>4 & 0x0F));
172                 }
173         }
174     }
175 #endif
176
177         /* 3 */
178         serial_setrts_dev (PSOC_PSC, RTS_NEGATE);
179         for (retries = PSOC_RETRIES; retries; retries--) {
180                 if (serial_getcts_dev(PSOC_PSC) == CTS_NEGATE)
181                         break;
182                 udelay (PSOC_WAIT_TIME);
183         }
184
185         return;
186 }
187 #ifdef CONFIG_PROGRESSBAR
188
189 void show_progress (int size, int tot)
190 {
191         int cnt;
192         int i;
193         static int rc = 0;
194
195         rc += size;
196
197         cnt = ((LCD_WIDTH/FONT_WIDTH) * rc) / tot;
198
199         rc -= (cnt * tot) / (LCD_WIDTH/FONT_WIDTH);
200
201         for (i = 0; i < cnt; i++) {
202                 lcd_putc(0xdc);
203         }
204
205         if (cnt) {
206                 lcd_enable(); /* MCC200-specific - send the framebuffer to PSoC */
207         }
208 }
209
210 #endif
211
212 int bmp_display(ulong addr, int x, int y)
213 {
214         int ret;
215         bmp_image_t *bmp = (bmp_image_t *)addr;
216
217         if (!bmp) {
218                 printf("There is no valid bmp file at the given address\n");
219                 return 1;
220         }
221
222         ret = lcd_display_bitmap((ulong)bmp, x, y);
223
224         if ((unsigned long)bmp != addr)
225                 free(bmp);
226
227         return ret;
228 }
229
230 #endif /* CONFIG_LCD */