1/* $NetBSD: toaster.c,v 1.10 2009/05/12 09:10:16 cegger 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: toaster.c,v 1.10 2009/05/12 09:10:16 cegger Exp $"); 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/sysctl.h> 37#include <sys/proc.h> 38#include <sys/poll.h> 39#include <sys/conf.h> 40#include <sys/uio.h> 41#include <sys/types.h> 42#include <sys/kernel.h> 43#include <sys/device.h> 44#include <sys/callout.h> 45#include <sys/select.h> 46 47#include <sys/bus.h> 48#include <machine/autoconf.h> 49 50#include <dev/isa/tsdiovar.h> 51#include <dev/isa/tsdioreg.h> 52 53struct toaster_softc { 54 device_t sc_dev; 55 bus_space_tag_t sc_iot; 56 bus_space_handle_t sc_gpioh; 57 u_int32_t latch; 58 u_int32_t burner; 59 u_int32_t led_width[4]; 60 u_int32_t led_duty[4]; 61 u_int32_t led_width_sysctl[4]; 62 u_int32_t led_duty_sysctl[4]; 63 callout_t led_callout[4]; 64}; 65 66static int toaster_match(device_t, cfdata_t, void *); 67static void toaster_attach(device_t, device_t, void *); 68 69extern struct cfdriver toaster_cd; 70 71CFATTACH_DECL_NEW(toaster, sizeof(struct toaster_softc), 72 toaster_match, toaster_attach, NULL, NULL); 73 74static struct toaster_softc *toaster_sc = NULL; 75 76static int 77toaster_match(device_t parent, cfdata_t match, void *aux) 78{ 79 /* No more than one toaster per system */ 80 if (toaster_sc == NULL) 81 return 1; 82 else 83 return 0; 84} 85 86#define TSDIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \ 87 (TSDIO_ ## x)) 88 89#define TSDIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \ 90 (TSDIO_ ## x), (y)) 91 92#define TSDIO_SETBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \ 93 (TSDIO_ ## x), TSDIO_GET(x) | (y)) 94 95#define TSDIO_CLEARBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \ 96 (TSDIO_ ## x), TSDIO_GET(x) & (~(y))) 97 98#define LEDCALLOUT_DECL(x) static void led ## x ## _on(void *); \ 99static void led ## x ## _off(void *); \ 100static void \ 101led ## x ## _on(void *arg) \ 102{ \ 103 struct toaster_softc *sc = arg; \ 104 \ 105 if (sc->led_duty[(x)]) { \ 106 TSDIO_CLEARBITS(PBDR, (1 << (4 + (x)))); \ 107 callout_reset(&sc->led_callout[(x)], \ 108 sc->led_duty[(x)], led ## x ## _off, arg); \ 109 } else { \ 110 TSDIO_SETBITS(PBDR, (1 << (4 + (x)))); \ 111 } \ 112} \ 113 \ 114static void \ 115led ## x ## _off(void *arg) \ 116{ \ 117 struct toaster_softc *sc = arg; \ 118 int offtime = sc->led_width[(x)] - sc->led_duty[(x)]; \ 119 \ 120 if (offtime > 0) { \ 121 TSDIO_SETBITS(PBDR, (1 << (4 + (x)))); \ 122 callout_reset(&sc->led_callout[(x)], offtime, \ 123 led ## x ## _on, arg); \ 124 } \ 125} 126 127LEDCALLOUT_DECL(0) 128LEDCALLOUT_DECL(1) 129LEDCALLOUT_DECL(2) 130LEDCALLOUT_DECL(3) 131 132static int 133led_sysctl(SYSCTLFN_ARGS) 134{ 135 int error, t; 136 struct sysctlnode node; 137 struct toaster_softc *sc = toaster_sc; 138 139 node = *rnode; 140 t = *(int*)rnode->sysctl_data; 141 node.sysctl_data = &t; 142 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 143 if (error || newp == NULL) 144 return (error); 145 146 if (t < 0) return EINVAL; 147 148 *(int*)rnode->sysctl_data = t; 149 150 if (node.sysctl_num == sc->led_width_sysctl[0] || 151 node.sysctl_num == sc->led_duty_sysctl[0]) 152 led0_on(sc); 153 if (node.sysctl_num == sc->led_width_sysctl[1] || 154 node.sysctl_num == sc->led_duty_sysctl[1]) 155 led1_on(sc); 156 if (node.sysctl_num == sc->led_width_sysctl[2] || 157 node.sysctl_num == sc->led_duty_sysctl[2]) 158 led2_on(sc); 159 if (node.sysctl_num == sc->led_width_sysctl[3] || 160 node.sysctl_num == sc->led_duty_sysctl[3]) 161 led3_on(sc); 162 163 return (0); 164} 165 166static int 167latch_sysctl(SYSCTLFN_ARGS) 168{ 169 int error, t; 170 struct sysctlnode node; 171 struct toaster_softc *sc = toaster_sc; 172 173 node = *rnode; 174 t = *(int*)rnode->sysctl_data; 175 node.sysctl_data = &t; 176 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 177 if (error || newp == NULL) 178 return (error); 179 180 if (t != 0 && t != 1) return EINVAL; 181 182 *(int*)rnode->sysctl_data = t; 183 184 if (t) 185 TSDIO_SETBITS(PADR, 0x1); 186 else 187 TSDIO_CLEARBITS(PADR, 0x1); 188 189 return (0); 190} 191 192static int 193burner_sysctl(SYSCTLFN_ARGS) 194{ 195 int error, t; 196 struct sysctlnode node; 197 struct toaster_softc *sc = toaster_sc; 198 199 node = *rnode; 200 t = *(int*)rnode->sysctl_data; 201 node.sysctl_data = &t; 202 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 203 if (error || newp == NULL) 204 return (error); 205 206 if (t != 0 && t != 1) return EINVAL; 207 208 *(int*)rnode->sysctl_data = t; 209 210 if (t) 211 TSDIO_SETBITS(PADR, 0x2); 212 else 213 TSDIO_CLEARBITS(PADR, 0x2); 214 215 return (0); 216} 217 218 219static void 220toaster_attach(device_t parent, device_t self, void *aux) 221{ 222 struct toaster_softc *sc = device_private(self); 223 struct tsdio_attach_args *taa = aux; 224 const struct sysctlnode *node, *datnode; 225 int i; 226 227 sc->sc_dev = self; 228 toaster_sc = sc; 229 sc->sc_iot = taa->ta_iot; 230 sc->sc_gpioh = taa->ta_ioh; 231 232 TSDIO_SETBITS(DDR, 0x2); /* Port B as outputs */ 233 TSDIO_SETBITS(PBDR, 0xf0); /* Turn off LED's */ 234 235 aprint_normal(": internal toaster control outputs\n"); 236 aprint_normal_dev(sc->sc_dev, "using port B, bits 4-7 for front panel LEDs\n"); 237 aprint_normal_dev(sc->sc_dev, "using port A, bit 0 for magnetic latch\n"); 238 aprint_normal_dev(sc->sc_dev, "using port A, bit 1 for burner element\n"); 239 240 callout_init(&sc->led_callout[0], 0); 241 callout_init(&sc->led_callout[1], 0); 242 callout_init(&sc->led_callout[2], 0); 243 callout_init(&sc->led_callout[3], 0); 244 sc->led_duty[0] = sc->led_width[0] = 0; 245 sc->led_duty[1] = sc->led_width[1] = 0; 246 sc->led_duty[2] = sc->led_width[2] = 0; 247 sc->led_duty[3] = sc->led_width[3] = 0; 248 249 sc->burner = 0; 250 sc->latch = 0; 251 252 if (sysctl_createv(NULL, 0, NULL, NULL, 253 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", 254 NULL, NULL, 0, NULL, 0, 255 CTL_HW, CTL_EOL) != 0) { 256 aprint_error_dev(sc->sc_dev, "could not create sysctl\n"); 257 return; 258 } 259 if (sysctl_createv(NULL, 0, NULL, &node, 260 0, CTLTYPE_NODE, device_xname(sc->sc_dev), 261 NULL, 262 NULL, 0, NULL, 0, 263 CTL_HW, CTL_CREATE, CTL_EOL) != 0) { 264 aprint_error_dev(sc->sc_dev, "could not create sysctl\n"); 265 return; 266 } 267 268#define LEDSYSCTL_SETUP(x) if ((i = sysctl_createv(NULL, \ 269 0, NULL, &datnode, \ 270 CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \ 271 CTLTYPE_INT, \ 272 "led" #x "_duty", \ 273 SYSCTL_DESCR( \ 274 "LED duty cycle in HZ tick units"), \ 275 led_sysctl, 0, &sc->led_duty[(x)], 0, \ 276 CTL_HW, node->sysctl_num, \ 277 CTL_CREATE, CTL_EOL)) \ 278 != 0) { \ 279 aprint_error_dev(sc->sc_dev, "could not create sysctl\n"); \ 280 return; \ 281 } \ 282 sc->led_duty_sysctl[(x)] = datnode->sysctl_num; \ 283 \ 284 if ((i = sysctl_createv(NULL, 0, NULL, &datnode, \ 285 CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \ 286 CTLTYPE_INT, \ 287 "led" #x "_width", \ 288 SYSCTL_DESCR( \ 289 "LED cycle width in HZ tick units"), \ 290 led_sysctl, 0, &sc->led_width[(x)], 0, \ 291 CTL_HW, node->sysctl_num, \ 292 CTL_CREATE, CTL_EOL)) \ 293 != 0) { \ 294 aprint_error_dev(sc->sc_dev, "could not create sysctl\n"); \ 295 return; \ 296 } \ 297 sc->led_width_sysctl[(x)] = datnode->sysctl_num; 298 299 LEDSYSCTL_SETUP(0); 300 LEDSYSCTL_SETUP(1); 301 LEDSYSCTL_SETUP(2); 302 LEDSYSCTL_SETUP(3); 303 304 if ((i = sysctl_createv(NULL, 0, NULL, &datnode, 305 CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, 306 CTLTYPE_INT, 307 "magnetic_latch", 308 SYSCTL_DESCR( 309 "magnetic latch that holds the toast down"), 310 latch_sysctl, 0, &sc->latch, 0, 311 CTL_HW, node->sysctl_num, 312 CTL_CREATE, CTL_EOL)) 313 != 0) { 314 aprint_error_dev(sc->sc_dev, "could not create sysctl\n"); 315 return; 316 } 317 318 if ((i = sysctl_createv(NULL, 0, NULL, &datnode, 319 CTLFLAG_READWRITE, CTLTYPE_INT, 320 "burner_element", 321 SYSCTL_DESCR( 322 "800-watt burner element control for toasting"), 323 burner_sysctl, 0, &sc->burner, 0, 324 CTL_HW, node->sysctl_num, 325 CTL_CREATE, CTL_EOL)) 326 != 0) { 327 aprint_error_dev(sc->sc_dev, "could not create sysctl\n"); 328 return; 329 } 330} 331