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