1 /* $NetBSD: ibmcd.c,v 1.2 2016/07/14 10:19:06 msaitoh 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 pmf_device_register(self, ibmcd_suspend, ibmcd_resume); 118 #endif 119 /* Initialize pins array */ 120 sc->sc_gpio_pins[PIN_OPEN].pin_num = 0; 121 sc->sc_gpio_pins[PIN_OPEN].pin_caps = GPIO_PIN_OUTPUT; 122 sc->sc_gpio_pins[PIN_STATUS].pin_num = 1; 123 sc->sc_gpio_pins[PIN_STATUS].pin_caps = GPIO_PIN_INPUT; 124 sc->sc_gpio_pins[PIN_CONNECTED].pin_num = 2; 125 sc->sc_gpio_pins[PIN_CONNECTED].pin_caps = GPIO_PIN_INPUT; 126 127 /* Create controller tag */ 128 sc->sc_gpio_gc.gp_cookie = sc; 129 sc->sc_gpio_gc.gp_pin_read = ibmcd_gpio_pin_read; 130 sc->sc_gpio_gc.gp_pin_write = ibmcd_gpio_pin_write; 131 sc->sc_gpio_gc.gp_pin_ctl = ibmcd_gpio_pin_ctl; 132 133 gba.gba_gc = &sc->sc_gpio_gc; 134 gba.gba_pins = sc->sc_gpio_pins; 135 gba.gba_npins = IBMCD_NPINS; 136 137 /* Attach GPIO framework */ 138 config_found_ia(self, "gpiobus", &gba, gpiobus_print); 139 140 } 141 142 static int 143 ibmcd_detach(device_t self, int flags) 144 { 145 struct ibmcd_softc *sc = device_private(self); 146 147 #if (__NetBSD_Version__ >= 600000000) 148 pmf_device_deregister(self); 149 #endif 150 if (sc->sc_iosize) 151 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize); 152 return 0; 153 } 154 155 #if (__NetBSD_Version__ >= 600000000) 156 static bool 157 ibmcd_resume(device_t self, const pmf_qual_t *qual) 158 { 159 return true; 160 } 161 162 static bool 163 ibmcd_suspend(device_t self, const pmf_qual_t *qual) 164 { 165 return true; 166 } 167 #endif 168 169 int 170 ibmcd_gpio_pin_read(void *arg, int pin) 171 { 172 struct ibmcd_softc *sc = arg; 173 uint8_t data; 174 175 data = bus_space_read_1(sc->sc_iot, sc->sc_ioh, IBMCD_STATUS); 176 177 switch (pin) { 178 case PIN_STATUS: 179 return data & IBMCD_CLOSED ? 0 : 1; 180 case PIN_CONNECTED: 181 return data & IBMCD_NOT_CONNECTED ? 0 : 1; 182 default: 183 return 0; 184 } 185 } 186 187 void 188 ibmcd_gpio_pin_write(void *arg, int pin, int value) 189 { 190 struct ibmcd_softc *sc = arg; 191 192 if (pin != PIN_OPEN) 193 return; 194 195 bus_space_write_1(sc->sc_iot, sc->sc_ioh, IBMCD_STATUS, 196 value ? IBMCD_DO_OPEN : 0); 197 bus_space_write_1(sc->sc_iot, sc->sc_ioh, IBMCD_CONTROL, 198 IBMCD_CMD_OPEN); 199 } 200 201 void 202 ibmcd_gpio_pin_ctl(void *arg, int pin, int flags) 203 { 204 /* We ignore pin control requests since the pin functions are fixed. */ 205 } 206 207 MODULE(MODULE_CLASS_DRIVER, ibmcd, "pci"); 208 209 #ifdef _MODULE 210 #include "ioconf.c" 211 #endif 212 213 static int 214 ibmcd_modcmd(modcmd_t cmd, void *opaque) 215 { 216 int error; 217 218 error = 0; 219 switch (cmd) { 220 case MODULE_CMD_INIT: 221 #ifdef _MODULE 222 error = config_init_component(cfdriver_ioconf_ibmcd, 223 cfattach_ioconf_ibmcd, cfdata_ioconf_ibmcd); 224 if (error) 225 aprint_error("%s: unable to init component\n", 226 ibmcd_cd.cd_name); 227 #endif 228 break; 229 case MODULE_CMD_FINI: 230 #ifdef _MODULE 231 config_fini_component(cfdriver_ioconf_ibmcd, 232 cfattach_ioconf_ibmcd, cfdata_ioconf_ibmcd); 233 #endif 234 break; 235 default: 236 error = ENOTTY; 237 } 238 return error; 239 } 240