tslcd.c revision 1.17 1 /* $NetBSD: tslcd.c,v 1.17 2014/03/16 05:20:24 dholland 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.17 2014/03/16 05:20:24 dholland 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 .d_open = tslcdopen,
81 .d_close = tslcdclose,
82 .d_read = tslcdread,
83 .d_write = tslcdwrite,
84 .d_ioctl = tslcdioctl,
85 .d_stop = nostop,
86 .d_tty = notty,
87 .d_poll = tslcdpoll,
88 .d_mmap = nommap,
89 .d_kqfilter = nokqfilter,
90 .d_flag = 0
91 };
92
93 extern const struct wsdisplay_emulops hlcd_emulops;
94 extern const struct wsdisplay_accessops hlcd_accessops;
95 extern struct cfdriver tslcd_cd;
96
97 CFATTACH_DECL_NEW(tslcd, sizeof(struct tslcd_softc),
98 tslcd_match, tslcd_attach, NULL, NULL);
99
100 static const struct wsscreen_descr tslcd_stdscreen = {
101 "std_tslcd", 24, 2,
102 &hlcd_emulops,
103 5, 7,
104 0,
105 };
106
107 static const struct wsscreen_descr *_tslcd_scrlist[] = {
108 &tslcd_stdscreen,
109 };
110
111 static const struct wsscreen_list tslcd_screenlist = {
112 sizeof(_tslcd_scrlist) / sizeof(struct wsscreen_descr *),
113 _tslcd_scrlist,
114 };
115
116 static int
117 tslcd_match(device_t parent, cfdata_t match, void *aux)
118 {
119 return 1;
120 }
121
122 #define GPIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
123 (EP93XX_GPIO_ ## x))
124
125 #define GPIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
126 (EP93XX_GPIO_ ## x), (y))
127
128 #define GPIO_SETBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
129 (EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
130
131 #define GPIO_CLEARBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
132 (EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
133
134 static void
135 tslcd_attach(device_t parent, device_t self, void *aux)
136 {
137 struct tslcd_softc *sc = device_private(self);
138 struct tspld_attach_args *taa = aux;
139 struct wsemuldisplaydev_attach_args waa;
140
141 sc->sc_iot = taa->ta_iot;
142 if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
143 EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
144 panic("tslcd_attach: couldn't map GPIO registers");
145
146 sc->sc_hlcd.sc_dev_ok = 1;
147 sc->sc_hlcd.sc_cols = 24;
148 sc->sc_hlcd.sc_vcols = 40;
149 sc->sc_hlcd.sc_flags = HD_8BIT | HD_MULTILINE;
150 sc->sc_hlcd.sc_dev = self;
151
152 sc->sc_hlcd.sc_writereg = tslcd_writereg;
153 sc->sc_hlcd.sc_readreg = tslcd_readreg;
154
155 GPIO_SET(PADDR, 0); /* Port A to inputs */
156 GPIO_SETBITS(PHDDR, 0x38); /* Bits 3:5 of Port H to outputs */
157 GPIO_CLEARBITS(PHDR, 0x18); /* De-assert EN, De-assert RS */
158
159 printf("\n");
160
161 hd44780_attach_subr(&sc->sc_hlcd);
162
163 waa.console = 0;
164 waa.scrdata = &tslcd_screenlist;
165 waa.accessops = &hlcd_accessops;
166 waa.accesscookie = &sc->sc_hlcd.sc_screen;
167 config_found(self, &waa, wsemuldisplaydevprint);
168 }
169
170 static void
171 tslcd_writereg(struct hd44780_chip *hd, uint32_t en, uint32_t rs, uint8_t cmd)
172 {
173 struct tslcd_softc *sc = device_private(hd->sc_dev);
174 uint8_t ctrl;
175
176 if (hd->sc_dev_ok == 0)
177 return;
178
179 /* Step 1: Apply RS & WR, Send data */
180 ctrl = GPIO_GET(PHDR);
181 GPIO_SET(PADDR, 0xff); /* set port A to outputs */
182 GPIO_SET(PADR, cmd);
183 if (rs) {
184 ctrl |= 0x10; /* assert RS */
185 ctrl &= ~0x20; /* assert WR */
186 } else {
187 ctrl &= ~0x30; /* de-assert WR, de-assert RS */
188 }
189 GPIO_SET(PHDR, ctrl);
190
191 /* Step 2: setup time delay */
192 delay(1);
193
194 /* Step 3: assert EN */
195 ctrl |= 0x8;
196 GPIO_SET(PHDR, ctrl);
197
198 /* Step 4: pulse time delay */
199 delay(1);
200
201 /* Step 5: de-assert EN */
202 ctrl &= ~0x8;
203 GPIO_SET(PHDR, ctrl);
204
205 /* Step 6: hold time delay */
206 delay(1);
207
208 /* Step 7: de-assert WR */
209 ctrl |= 0x2;
210 GPIO_SET(PHDR, ctrl);
211
212 /* Step 8: minimum delay till next bus-cycle */
213 delay(1000);
214 }
215
216 static uint8_t
217 tslcd_readreg(struct hd44780_chip *hd, uint32_t en, uint32_t rs)
218 {
219 struct tslcd_softc *sc = device_private(hd->sc_dev);
220 uint8_t ret, ctrl;
221
222 if (hd->sc_dev_ok == 0)
223 return 0;
224
225 /* Step 1: Apply RS & WR, Send data */
226 ctrl = GPIO_GET(PHDR);
227 GPIO_SET(PADDR, 0x0); /* set port A to inputs */
228 if (rs) {
229 ctrl |= 0x30; /* de-assert WR, assert RS */
230 } else {
231 ctrl |= 0x20; /* de-assert WR */
232 ctrl &= ~0x10; /* de-assert RS */
233 }
234 GPIO_SET(PHDR, ctrl);
235
236 /* Step 2: setup time delay */
237 delay(1);
238
239 /* Step 3: assert EN */
240 ctrl |= 0x8;
241 GPIO_SET(PHDR, ctrl);
242
243 /* Step 4: pulse time delay */
244 delay(1);
245
246 /* Step 5: de-assert EN */
247 ret = GPIO_GET(PADR) & 0xff;
248 ctrl &= ~0x8;
249 GPIO_SET(PHDR, ctrl);
250
251 /* Step 6: hold time delay + min bus cycle interval*/
252 delay(1000);
253 return ret;
254 }
255
256 int
257 tslcdopen(dev_t dev, int flag, int mode, struct lwp *l)
258 {
259 struct tslcd_softc *sc = device_lookup_private(&tslcd_cd, minor(dev));
260
261 if (sc->sc_hlcd.sc_dev_ok == 0)
262 return hd44780_init(&sc->sc_hlcd);
263 else
264 return 0;
265 }
266
267 int
268 tslcdclose(dev_t dev, int flag, int mode, struct lwp *l)
269 {
270 return 0;
271 }
272
273 int
274 tslcdread(dev_t dev, struct uio *uio, int flag)
275 {
276 return EIO;
277 }
278
279 int
280 tslcdwrite(dev_t dev, struct uio *uio, int flag)
281 {
282 int error;
283 struct hd44780_io io;
284 struct tslcd_softc *sc = device_lookup_private(&tslcd_cd, minor(dev));
285
286 if (sc->sc_hlcd.sc_dev_ok == 0)
287 return EIO;
288
289 io.dat = 0;
290 io.len = uio->uio_resid;
291 if (io.len > HD_MAX_CHARS)
292 io.len = HD_MAX_CHARS;
293
294 if ((error = uiomove((void*)io.buf, io.len, uio)) != 0)
295 return error;
296
297 hd44780_ddram_redraw(&sc->sc_hlcd, sc->sc_hlcd.sc_curchip, &io);
298 return 0;
299 }
300
301 int
302 tslcdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
303 {
304 struct tslcd_softc *sc = device_lookup_private(&tslcd_cd, minor(dev));
305 return hd44780_ioctl_subr(&sc->sc_hlcd, cmd, data);
306 }
307
308 int
309 tslcdpoll(dev_t dev, int events, struct lwp *l)
310 {
311 return 0;
312 }
313