tslcd.c revision 1.16 1 /* $NetBSD: tslcd.c,v 1.16 2012/11/12 18:00:40 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jesse Off.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tslcd.c,v 1.16 2012/11/12 18:00:40 skrll Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/poll.h>
38 #include <sys/conf.h>
39 #include <sys/uio.h>
40 #include <sys/types.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/callout.h>
44 #include <sys/select.h>
45
46 #include <sys/bus.h>
47 #include <machine/autoconf.h>
48
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wscons_callbacks.h>
52
53 #include <arm/ep93xx/ep93xxreg.h>
54 #include <arm/ep93xx/epgpioreg.h>
55 #include <dev/ic/hd44780reg.h>
56 #include <dev/ic/hd44780var.h>
57 #include <evbarm/tsarm/tspldvar.h>
58 #include <evbarm/tsarm/tsarmreg.h>
59
60 struct tslcd_softc {
61 struct hd44780_chip sc_hlcd;
62 bus_space_tag_t sc_iot;
63 bus_space_handle_t sc_gpioh;
64 };
65
66 static int tslcd_match(device_t, cfdata_t, void *);
67 static void tslcd_attach(device_t, device_t, void *);
68
69 static void tslcd_writereg(struct hd44780_chip *, uint32_t, uint32_t, uint8_t);
70 static uint8_t tslcd_readreg(struct hd44780_chip *, uint32_t, uint32_t);
71
72 dev_type_open(tslcdopen);
73 dev_type_close(tslcdclose);
74 dev_type_read(tslcdread);
75 dev_type_write(tslcdwrite);
76 dev_type_ioctl(tslcdioctl);
77 dev_type_poll(tslcdpoll);
78
79 const struct cdevsw tslcd_cdevsw = {
80 tslcdopen, tslcdclose, tslcdread, tslcdwrite, tslcdioctl,
81 nostop, notty, tslcdpoll, nommap,
82 };
83
84 extern const struct wsdisplay_emulops hlcd_emulops;
85 extern const struct wsdisplay_accessops hlcd_accessops;
86 extern struct cfdriver tslcd_cd;
87
88 CFATTACH_DECL_NEW(tslcd, sizeof(struct tslcd_softc),
89 tslcd_match, tslcd_attach, NULL, NULL);
90
91 static const struct wsscreen_descr tslcd_stdscreen = {
92 "std_tslcd", 24, 2,
93 &hlcd_emulops,
94 5, 7,
95 0,
96 };
97
98 static const struct wsscreen_descr *_tslcd_scrlist[] = {
99 &tslcd_stdscreen,
100 };
101
102 static const struct wsscreen_list tslcd_screenlist = {
103 sizeof(_tslcd_scrlist) / sizeof(struct wsscreen_descr *),
104 _tslcd_scrlist,
105 };
106
107 static int
108 tslcd_match(device_t parent, cfdata_t match, void *aux)
109 {
110 return 1;
111 }
112
113 #define GPIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
114 (EP93XX_GPIO_ ## x))
115
116 #define GPIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
117 (EP93XX_GPIO_ ## x), (y))
118
119 #define GPIO_SETBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
120 (EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
121
122 #define GPIO_CLEARBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
123 (EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
124
125 static void
126 tslcd_attach(device_t parent, device_t self, void *aux)
127 {
128 struct tslcd_softc *sc = device_private(self);
129 struct tspld_attach_args *taa = aux;
130 struct wsemuldisplaydev_attach_args waa;
131
132 sc->sc_iot = taa->ta_iot;
133 if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
134 EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
135 panic("tslcd_attach: couldn't map GPIO registers");
136
137 sc->sc_hlcd.sc_dev_ok = 1;
138 sc->sc_hlcd.sc_cols = 24;
139 sc->sc_hlcd.sc_vcols = 40;
140 sc->sc_hlcd.sc_flags = HD_8BIT | HD_MULTILINE;
141 sc->sc_hlcd.sc_dev = self;
142
143 sc->sc_hlcd.sc_writereg = tslcd_writereg;
144 sc->sc_hlcd.sc_readreg = tslcd_readreg;
145
146 GPIO_SET(PADDR, 0); /* Port A to inputs */
147 GPIO_SETBITS(PHDDR, 0x38); /* Bits 3:5 of Port H to outputs */
148 GPIO_CLEARBITS(PHDR, 0x18); /* De-assert EN, De-assert RS */
149
150 printf("\n");
151
152 hd44780_attach_subr(&sc->sc_hlcd);
153
154 waa.console = 0;
155 waa.scrdata = &tslcd_screenlist;
156 waa.accessops = &hlcd_accessops;
157 waa.accesscookie = &sc->sc_hlcd.sc_screen;
158 config_found(self, &waa, wsemuldisplaydevprint);
159 }
160
161 static void
162 tslcd_writereg(struct hd44780_chip *hd, uint32_t en, uint32_t rs, uint8_t cmd)
163 {
164 struct tslcd_softc *sc = device_private(hd->sc_dev);
165 uint8_t ctrl;
166
167 if (hd->sc_dev_ok == 0)
168 return;
169
170 /* Step 1: Apply RS & WR, Send data */
171 ctrl = GPIO_GET(PHDR);
172 GPIO_SET(PADDR, 0xff); /* set port A to outputs */
173 GPIO_SET(PADR, cmd);
174 if (rs) {
175 ctrl |= 0x10; /* assert RS */
176 ctrl &= ~0x20; /* assert WR */
177 } else {
178 ctrl &= ~0x30; /* de-assert WR, de-assert RS */
179 }
180 GPIO_SET(PHDR, ctrl);
181
182 /* Step 2: setup time delay */
183 delay(1);
184
185 /* Step 3: assert EN */
186 ctrl |= 0x8;
187 GPIO_SET(PHDR, ctrl);
188
189 /* Step 4: pulse time delay */
190 delay(1);
191
192 /* Step 5: de-assert EN */
193 ctrl &= ~0x8;
194 GPIO_SET(PHDR, ctrl);
195
196 /* Step 6: hold time delay */
197 delay(1);
198
199 /* Step 7: de-assert WR */
200 ctrl |= 0x2;
201 GPIO_SET(PHDR, ctrl);
202
203 /* Step 8: minimum delay till next bus-cycle */
204 delay(1000);
205 }
206
207 static uint8_t
208 tslcd_readreg(struct hd44780_chip *hd, uint32_t en, uint32_t rs)
209 {
210 struct tslcd_softc *sc = device_private(hd->sc_dev);
211 uint8_t ret, ctrl;
212
213 if (hd->sc_dev_ok == 0)
214 return 0;
215
216 /* Step 1: Apply RS & WR, Send data */
217 ctrl = GPIO_GET(PHDR);
218 GPIO_SET(PADDR, 0x0); /* set port A to inputs */
219 if (rs) {
220 ctrl |= 0x30; /* de-assert WR, assert RS */
221 } else {
222 ctrl |= 0x20; /* de-assert WR */
223 ctrl &= ~0x10; /* de-assert RS */
224 }
225 GPIO_SET(PHDR, ctrl);
226
227 /* Step 2: setup time delay */
228 delay(1);
229
230 /* Step 3: assert EN */
231 ctrl |= 0x8;
232 GPIO_SET(PHDR, ctrl);
233
234 /* Step 4: pulse time delay */
235 delay(1);
236
237 /* Step 5: de-assert EN */
238 ret = GPIO_GET(PADR) & 0xff;
239 ctrl &= ~0x8;
240 GPIO_SET(PHDR, ctrl);
241
242 /* Step 6: hold time delay + min bus cycle interval*/
243 delay(1000);
244 return ret;
245 }
246
247 int
248 tslcdopen(dev_t dev, int flag, int mode, struct lwp *l)
249 {
250 struct tslcd_softc *sc = device_lookup_private(&tslcd_cd, minor(dev));
251
252 if (sc->sc_hlcd.sc_dev_ok == 0)
253 return hd44780_init(&sc->sc_hlcd);
254 else
255 return 0;
256 }
257
258 int
259 tslcdclose(dev_t dev, int flag, int mode, struct lwp *l)
260 {
261 return 0;
262 }
263
264 int
265 tslcdread(dev_t dev, struct uio *uio, int flag)
266 {
267 return EIO;
268 }
269
270 int
271 tslcdwrite(dev_t dev, struct uio *uio, int flag)
272 {
273 int error;
274 struct hd44780_io io;
275 struct tslcd_softc *sc = device_lookup_private(&tslcd_cd, minor(dev));
276
277 if (sc->sc_hlcd.sc_dev_ok == 0)
278 return EIO;
279
280 io.dat = 0;
281 io.len = uio->uio_resid;
282 if (io.len > HD_MAX_CHARS)
283 io.len = HD_MAX_CHARS;
284
285 if ((error = uiomove((void*)io.buf, io.len, uio)) != 0)
286 return error;
287
288 hd44780_ddram_redraw(&sc->sc_hlcd, sc->sc_hlcd.sc_curchip, &io);
289 return 0;
290 }
291
292 int
293 tslcdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
294 {
295 struct tslcd_softc *sc = device_lookup_private(&tslcd_cd, minor(dev));
296 return hd44780_ioctl_subr(&sc->sc_hlcd, cmd, data);
297 }
298
299 int
300 tslcdpoll(dev_t dev, int events, struct lwp *l)
301 {
302 return 0;
303 }
304