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