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