1 /* $NetBSD: ibmcd.c,v 1.3 2017/01/20 12:25:07 maya Exp $ */ 2 3 /* 4 * Copyright (c) 2012 Marc Balmer <marc@msys.ch> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * Driver for the IBM 4810 BSP cash drawer port. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/device.h> 35 #include <sys/gpio.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/systm.h> 39 40 #include <dev/gpio/gpiovar.h> 41 42 #include <dev/pci/pcivar.h> 43 #include <dev/pci/pcireg.h> 44 #include <dev/pci/pcidevs.h> 45 46 /* registers */ 47 #define IBMCD_STATUS 0x00 48 #define IBMCD_CONTROL 0x01 49 50 #define IBMCD_CMD_OPEN 0x6d 51 #define IBMCD_DO_OPEN 0x01 52 53 #define IBMCD_CLOSED 0x80 54 #define IBMCD_NOT_CONNECTED 0x40 55 56 /* GPIO constants */ 57 #define IBMCD_NPINS 3 58 #define PIN_OPEN 0 59 #define PIN_STATUS 1 60 #define PIN_CONNECTED 2 61 62 struct ibmcd_softc { 63 bus_space_tag_t sc_iot; 64 bus_space_handle_t sc_ioh; 65 bus_size_t sc_iosize; 66 67 /* GPIO interface */ 68 struct gpio_chipset_tag sc_gpio_gc; 69 gpio_pin_t sc_gpio_pins[IBMCD_NPINS]; 70 }; 71 72 static int ibmcd_match(device_t, cfdata_t, void *); 73 static void ibmcd_attach(device_t, device_t, void *); 74 static int ibmcd_detach(device_t, int); 75 #if (__NetBSD_Version__ >= 600000000) 76 static bool ibmcd_suspend(device_t, const pmf_qual_t *); 77 static bool ibmcd_resume(device_t, const pmf_qual_t *); 78 #endif 79 int ibmcd_gpio_pin_read(void *, int); 80 void ibmcd_gpio_pin_write(void *, int, int); 81 void ibmcd_gpio_pin_ctl(void *, int, int); 82 83 CFATTACH_DECL2_NEW(ibmcd, sizeof(struct ibmcd_softc), ibmcd_match, 84 ibmcd_attach, ibmcd_detach, NULL, NULL, NULL); 85 86 static int 87 ibmcd_match(device_t parent, cfdata_t match, void *aux) 88 { 89 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 90 91 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_IBM && 92 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_IBM_4810_BSP) 93 return 1; 94 return 0; 95 } 96 97 void 98 ibmcd_attach(device_t parent, device_t self, void *aux) 99 { 100 struct ibmcd_softc *sc = device_private(self); 101 struct pci_attach_args *const pa = (struct pci_attach_args *)aux; 102 struct gpiobus_attach_args gba; 103 pcireg_t memtype; 104 105 aprint_naive("\n"); 106 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START); 107 if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &sc->sc_iot, 108 &sc->sc_ioh, NULL, &sc->sc_iosize)) { 109 aprint_error("\n"); 110 aprint_error_dev(self, "PCI %s region not found\n", 111 memtype == PCI_MAPREG_TYPE_IO ? "I/O" : "memory"); 112 return; 113 } 114 aprint_normal(": IBM 4810 BSP cash drawer\n"); 115 116 #if (__NetBSD_Version__ >= 600000000) 117 if (!pmf_device_register(self, ibmcd_suspend, ibmcd_resume)) 118 aprint_error_dev(self, "couldn't establish power handler\n"); 119 #endif 120 /* Initialize pins array */ 121 sc->sc_gpio_pins[PIN_OPEN].pin_num = 0; 122 sc->sc_gpio_pins[PIN_OPEN].pin_caps = GPIO_PIN_OUTPUT; 123 sc->sc_gpio_pins[PIN_STATUS].pin_num = 1; 124 sc->sc_gpio_pins[PIN_STATUS].pin_caps = GPIO_PIN_INPUT; 125 sc->sc_gpio_pins[PIN_CONNECTED].pin_num = 2; 126 sc->sc_gpio_pins[PIN_CONNECTED].pin_caps = GPIO_PIN_INPUT; 127 128 /* Create controller tag */ 129 sc->sc_gpio_gc.gp_cookie = sc; 130 sc->sc_gpio_gc.gp_pin_read = ibmcd_gpio_pin_read; 131 sc->sc_gpio_gc.gp_pin_write = ibmcd_gpio_pin_write; 132 sc->sc_gpio_gc.gp_pin_ctl = ibmcd_gpio_pin_ctl; 133 134 gba.gba_gc = &sc->sc_gpio_gc; 135 gba.gba_pins = sc->sc_gpio_pins; 136 gba.gba_npins = IBMCD_NPINS; 137 138 /* Attach GPIO framework */ 139 config_found_ia(self, "gpiobus", &gba, gpiobus_print); 140 141 } 142 143 static int 144 ibmcd_detach(device_t self, int flags) 145 { 146 struct ibmcd_softc *sc = device_private(self); 147 148 #if (__NetBSD_Version__ >= 600000000) 149 pmf_device_deregister(self); 150 #endif 151 if (sc->sc_iosize) 152 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize); 153 return 0; 154 } 155 156 #if (__NetBSD_Version__ >= 600000000) 157 static bool 158 ibmcd_resume(device_t self, const pmf_qual_t *qual) 159 { 160 return true; 161 } 162 163 static bool 164 ibmcd_suspend(device_t self, const pmf_qual_t *qual) 165 { 166 return true; 167 } 168 #endif 169 170 int 171 ibmcd_gpio_pin_read(void *arg, int pin) 172 { 173 struct ibmcd_softc *sc = arg; 174 uint8_t data; 175 176 data = bus_space_read_1(sc->sc_iot, sc->sc_ioh, IBMCD_STATUS); 177 178 switch (pin) { 179 case PIN_STATUS: 180 return data & IBMCD_CLOSED ? 0 : 1; 181 case PIN_CONNECTED: 182 return data & IBMCD_NOT_CONNECTED ? 0 : 1; 183 default: 184 return 0; 185 } 186 } 187 188 void 189 ibmcd_gpio_pin_write(void *arg, int pin, int value) 190 { 191 struct ibmcd_softc *sc = arg; 192 193 if (pin != PIN_OPEN) 194 return; 195 196 bus_space_write_1(sc->sc_iot, sc->sc_ioh, IBMCD_STATUS, 197 value ? IBMCD_DO_OPEN : 0); 198 bus_space_write_1(sc->sc_iot, sc->sc_ioh, IBMCD_CONTROL, 199 IBMCD_CMD_OPEN); 200 } 201 202 void 203 ibmcd_gpio_pin_ctl(void *arg, int pin, int flags) 204 { 205 /* We ignore pin control requests since the pin functions are fixed. */ 206 } 207 208 MODULE(MODULE_CLASS_DRIVER, ibmcd, "pci"); 209 210 #ifdef _MODULE 211 #include "ioconf.c" 212 #endif 213 214 static int 215 ibmcd_modcmd(modcmd_t cmd, void *opaque) 216 { 217 int error; 218 219 error = 0; 220 switch (cmd) { 221 case MODULE_CMD_INIT: 222 #ifdef _MODULE 223 error = config_init_component(cfdriver_ioconf_ibmcd, 224 cfattach_ioconf_ibmcd, cfdata_ioconf_ibmcd); 225 if (error) 226 aprint_error("%s: unable to init component\n", 227 ibmcd_cd.cd_name); 228 #endif 229 break; 230 case MODULE_CMD_FINI: 231 #ifdef _MODULE 232 config_fini_component(cfdriver_ioconf_ibmcd, 233 cfattach_ioconf_ibmcd, cfdata_ioconf_ibmcd); 234 #endif 235 break; 236 default: 237 error = ENOTTY; 238 } 239 return error; 240 } 241