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