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