1128e3872SIan Lepore /*-
2128e3872SIan Lepore * Copyright (c) 2016 Ian Lepore <ian@FreeBSD.org>
3128e3872SIan Lepore * All rights reserved.
4128e3872SIan Lepore *
5128e3872SIan Lepore * Redistribution and use in source and binary forms, with or without
6128e3872SIan Lepore * modification, are permitted provided that the following conditions
7128e3872SIan Lepore * are met:
8128e3872SIan Lepore * 1. Redistributions of source code must retain the above copyright
9128e3872SIan Lepore * notice, this list of conditions and the following disclaimer.
10128e3872SIan Lepore * 2. Redistributions in binary form must reproduce the above copyright
11128e3872SIan Lepore * notice, this list of conditions and the following disclaimer in the
12128e3872SIan Lepore * documentation and/or other materials provided with the distribution.
13128e3872SIan Lepore *
14128e3872SIan Lepore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15128e3872SIan Lepore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16128e3872SIan Lepore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17128e3872SIan Lepore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18128e3872SIan Lepore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19128e3872SIan Lepore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20128e3872SIan Lepore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21128e3872SIan Lepore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22128e3872SIan Lepore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23128e3872SIan Lepore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24128e3872SIan Lepore * SUCH DAMAGE.
25128e3872SIan Lepore */
26128e3872SIan Lepore
27128e3872SIan Lepore #include <sys/param.h>
28128e3872SIan Lepore #include <sys/gpio.h>
29128e3872SIan Lepore #include <sys/kernel.h>
30128e3872SIan Lepore #include <sys/lock.h>
31128e3872SIan Lepore #include <sys/module.h>
32128e3872SIan Lepore #include <sys/bus.h>
33128e3872SIan Lepore #include <sys/conf.h>
34128e3872SIan Lepore #include <sys/timepps.h>
35128e3872SIan Lepore
36128e3872SIan Lepore #include <dev/gpio/gpiobusvar.h>
37128e3872SIan Lepore
38128e3872SIan Lepore #include "opt_platform.h"
39128e3872SIan Lepore
40128e3872SIan Lepore #ifdef FDT
41128e3872SIan Lepore #include <dev/ofw/ofw_bus.h>
42128e3872SIan Lepore
43128e3872SIan Lepore static struct ofw_compat_data compat_data[] = {
44128e3872SIan Lepore {"pps-gpio", 1},
45128e3872SIan Lepore {NULL, 0}
46128e3872SIan Lepore };
47519b64e2SMark Johnston SIMPLEBUS_PNP_INFO(compat_data);
48128e3872SIan Lepore #endif /* FDT */
49128e3872SIan Lepore
50128e3872SIan Lepore struct pps_softc {
51128e3872SIan Lepore device_t dev;
52128e3872SIan Lepore gpio_pin_t gpin;
53128e3872SIan Lepore void *ihandler;
54128e3872SIan Lepore struct resource *ires;
55128e3872SIan Lepore int irid;
56128e3872SIan Lepore struct cdev *pps_cdev;
57128e3872SIan Lepore struct pps_state pps_state;
58128e3872SIan Lepore struct mtx pps_mtx;
59128e3872SIan Lepore bool falling_edge;
60128e3872SIan Lepore };
61128e3872SIan Lepore
62128e3872SIan Lepore #define PPS_CDEV_NAME "gpiopps"
63128e3872SIan Lepore
64128e3872SIan Lepore static int
gpiopps_open(struct cdev * dev,int flags,int fmt,struct thread * td)65128e3872SIan Lepore gpiopps_open(struct cdev *dev, int flags, int fmt, struct thread *td)
66128e3872SIan Lepore {
67128e3872SIan Lepore struct pps_softc *sc = dev->si_drv1;
68128e3872SIan Lepore
69128e3872SIan Lepore /* We can't be unloaded while open, so mark ourselves BUSY. */
70128e3872SIan Lepore mtx_lock(&sc->pps_mtx);
71128e3872SIan Lepore device_busy(sc->dev);
72128e3872SIan Lepore mtx_unlock(&sc->pps_mtx);
73128e3872SIan Lepore
74128e3872SIan Lepore return 0;
75128e3872SIan Lepore }
76128e3872SIan Lepore
77128e3872SIan Lepore static int
gpiopps_close(struct cdev * dev,int flags,int fmt,struct thread * td)78128e3872SIan Lepore gpiopps_close(struct cdev *dev, int flags, int fmt, struct thread *td)
79128e3872SIan Lepore {
80128e3872SIan Lepore struct pps_softc *sc = dev->si_drv1;
81128e3872SIan Lepore
82128e3872SIan Lepore mtx_lock(&sc->pps_mtx);
83128e3872SIan Lepore device_unbusy(sc->dev);
84128e3872SIan Lepore mtx_unlock(&sc->pps_mtx);
85128e3872SIan Lepore
86128e3872SIan Lepore return 0;
87128e3872SIan Lepore }
88128e3872SIan Lepore
89128e3872SIan Lepore static int
gpiopps_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)90128e3872SIan Lepore gpiopps_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
91128e3872SIan Lepore {
92128e3872SIan Lepore struct pps_softc *sc = dev->si_drv1;
93128e3872SIan Lepore int err;
94128e3872SIan Lepore
95128e3872SIan Lepore /* Let the kernel do the heavy lifting for ioctl. */
96128e3872SIan Lepore mtx_lock(&sc->pps_mtx);
97128e3872SIan Lepore err = pps_ioctl(cmd, data, &sc->pps_state);
98128e3872SIan Lepore mtx_unlock(&sc->pps_mtx);
99128e3872SIan Lepore
100128e3872SIan Lepore return err;
101128e3872SIan Lepore }
102128e3872SIan Lepore
103128e3872SIan Lepore static struct cdevsw pps_cdevsw = {
104128e3872SIan Lepore .d_version = D_VERSION,
1051c7d15b0SWarner Losh .d_flags = D_TRACKCLOSE,
106128e3872SIan Lepore .d_open = gpiopps_open,
107128e3872SIan Lepore .d_close = gpiopps_close,
108128e3872SIan Lepore .d_ioctl = gpiopps_ioctl,
109128e3872SIan Lepore .d_name = PPS_CDEV_NAME,
110128e3872SIan Lepore };
111128e3872SIan Lepore
112128e3872SIan Lepore static int
gpiopps_ifltr(void * arg)113128e3872SIan Lepore gpiopps_ifltr(void *arg)
114128e3872SIan Lepore {
115128e3872SIan Lepore struct pps_softc *sc = arg;
116128e3872SIan Lepore
117128e3872SIan Lepore /*
118128e3872SIan Lepore * There is no locking here by design... The kernel cleverly captures
119128e3872SIan Lepore * the current time into an area of the pps_state structure which is
120128e3872SIan Lepore * written only by the pps_capture() routine and read only by the
121128e3872SIan Lepore * pps_event() routine. We don't need lock-based management of access
122128e3872SIan Lepore * to the capture area because we have time-based access management: we
1235d785ad6SGordon Bergling * can't be reading and writing concurrently because we can't be running
124128e3872SIan Lepore * both the threaded and filter handlers concurrently (because a new
125128e3872SIan Lepore * hardware interrupt can't happen until the threaded handler for the
126128e3872SIan Lepore * current interrupt exits, after which the system does the EOI that
127128e3872SIan Lepore * enables a new hardware interrupt).
128128e3872SIan Lepore */
129128e3872SIan Lepore pps_capture(&sc->pps_state);
130128e3872SIan Lepore return (FILTER_SCHEDULE_THREAD);
131128e3872SIan Lepore }
132128e3872SIan Lepore
133128e3872SIan Lepore static void
gpiopps_ithrd(void * arg)134128e3872SIan Lepore gpiopps_ithrd(void *arg)
135128e3872SIan Lepore {
136128e3872SIan Lepore struct pps_softc *sc = arg;
137128e3872SIan Lepore
138128e3872SIan Lepore /*
139128e3872SIan Lepore * Go create a pps event from the data captured in the filter handler.
140128e3872SIan Lepore *
141128e3872SIan Lepore * Note that we DO need locking here, unlike the case with the filter
142128e3872SIan Lepore * handler. The pps_event() routine updates the non-capture part of the
143128e3872SIan Lepore * pps_state structure, and the ioctl() code could be accessing that
144128e3872SIan Lepore * data right now in a non-interrupt context, so we need an interlock.
145128e3872SIan Lepore */
146128e3872SIan Lepore mtx_lock(&sc->pps_mtx);
147128e3872SIan Lepore pps_event(&sc->pps_state, PPS_CAPTUREASSERT);
148128e3872SIan Lepore mtx_unlock(&sc->pps_mtx);
149128e3872SIan Lepore }
150128e3872SIan Lepore
151128e3872SIan Lepore static int
gpiopps_detach(device_t dev)152128e3872SIan Lepore gpiopps_detach(device_t dev)
153128e3872SIan Lepore {
154128e3872SIan Lepore struct pps_softc *sc = device_get_softc(dev);
155128e3872SIan Lepore
156128e3872SIan Lepore if (sc->pps_cdev != NULL)
157128e3872SIan Lepore destroy_dev(sc->pps_cdev);
158128e3872SIan Lepore if (sc->ihandler != NULL)
159128e3872SIan Lepore bus_teardown_intr(dev, sc->ires, sc->ihandler);
160128e3872SIan Lepore if (sc->ires != NULL)
161128e3872SIan Lepore bus_release_resource(dev, SYS_RES_IRQ, sc->irid, sc->ires);
162128e3872SIan Lepore if (sc->gpin != NULL)
163128e3872SIan Lepore gpiobus_release_pin(GPIO_GET_BUS(sc->gpin->dev), sc->gpin->pin);
164128e3872SIan Lepore return (0);
165128e3872SIan Lepore }
166128e3872SIan Lepore
167128e3872SIan Lepore #ifdef FDT
168128e3872SIan Lepore static int
gpiopps_fdt_attach(device_t dev)169128e3872SIan Lepore gpiopps_fdt_attach(device_t dev)
170128e3872SIan Lepore {
171128e3872SIan Lepore struct pps_softc *sc;
172128e3872SIan Lepore struct make_dev_args devargs;
173128e3872SIan Lepore phandle_t node;
174128e3872SIan Lepore uint32_t edge, pincaps;
175128e3872SIan Lepore int err;
176128e3872SIan Lepore
177128e3872SIan Lepore sc = device_get_softc(dev);
178128e3872SIan Lepore sc->dev = dev;
179128e3872SIan Lepore
180128e3872SIan Lepore mtx_init(&sc->pps_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
181128e3872SIan Lepore
182128e3872SIan Lepore /* Initialize the pps_state struct. */
183128e3872SIan Lepore sc->pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
184128e3872SIan Lepore sc->pps_state.driver_abi = PPS_ABI_VERSION;
185128e3872SIan Lepore sc->pps_state.driver_mtx = &sc->pps_mtx;
186128e3872SIan Lepore pps_init_abi(&sc->pps_state);
187128e3872SIan Lepore
188128e3872SIan Lepore /* Check which edge we're configured to capture (default is rising). */
189128e3872SIan Lepore if (ofw_bus_has_prop(dev, "assert-falling-edge"))
190128e3872SIan Lepore edge = GPIO_INTR_EDGE_FALLING;
191128e3872SIan Lepore else
192128e3872SIan Lepore edge = GPIO_INTR_EDGE_RISING;
193128e3872SIan Lepore
194128e3872SIan Lepore /*
195128e3872SIan Lepore * Look up the configured gpio pin and ensure it can be configured for
196128e3872SIan Lepore * the interrupt mode we need.
197128e3872SIan Lepore */
198128e3872SIan Lepore node = ofw_bus_get_node(dev);
199128e3872SIan Lepore if ((err = gpio_pin_get_by_ofw_idx(dev, node, 0, &sc->gpin)) != 0) {
200128e3872SIan Lepore device_printf(dev, "Cannot obtain gpio pin\n");
201128e3872SIan Lepore return (err);
202128e3872SIan Lepore }
203128e3872SIan Lepore device_printf(dev, "PPS input on %s pin %u\n",
204128e3872SIan Lepore device_get_nameunit(sc->gpin->dev), sc->gpin->pin);
205128e3872SIan Lepore
206128e3872SIan Lepore if ((err = gpio_pin_getcaps(sc->gpin, &pincaps)) != 0) {
207128e3872SIan Lepore device_printf(dev, "Cannot query capabilities of gpio pin\n");
208128e3872SIan Lepore gpiopps_detach(dev);
209128e3872SIan Lepore return (err);
210128e3872SIan Lepore }
211128e3872SIan Lepore if ((pincaps & edge) == 0) {
212128e3872SIan Lepore device_printf(dev, "Pin cannot be configured for the requested signal edge\n");
213128e3872SIan Lepore gpiopps_detach(dev);
214128e3872SIan Lepore return (ENOTSUP);
215128e3872SIan Lepore }
216128e3872SIan Lepore
217128e3872SIan Lepore /*
218128e3872SIan Lepore * Transform our 'gpios' property into an interrupt resource and set up
219128e3872SIan Lepore * the interrupt.
220128e3872SIan Lepore */
221128e3872SIan Lepore if ((sc->ires = gpio_alloc_intr_resource(dev, &sc->irid, RF_ACTIVE,
222128e3872SIan Lepore sc->gpin, edge)) == NULL) {
223128e3872SIan Lepore device_printf(dev, "Cannot allocate an IRQ for the GPIO\n");
224128e3872SIan Lepore gpiopps_detach(dev);
225128e3872SIan Lepore return (err);
226128e3872SIan Lepore }
227128e3872SIan Lepore
228128e3872SIan Lepore err = bus_setup_intr(dev, sc->ires, INTR_TYPE_CLK | INTR_MPSAFE,
229128e3872SIan Lepore gpiopps_ifltr, gpiopps_ithrd, sc, &sc->ihandler);
230128e3872SIan Lepore if (err != 0) {
231128e3872SIan Lepore device_printf(dev, "Unable to setup pps irq handler\n");
232128e3872SIan Lepore gpiopps_detach(dev);
233128e3872SIan Lepore return (err);
234128e3872SIan Lepore }
235128e3872SIan Lepore
236128e3872SIan Lepore /* Create the RFC 2783 pps-api cdev. */
237128e3872SIan Lepore make_dev_args_init(&devargs);
238128e3872SIan Lepore devargs.mda_devsw = &pps_cdevsw;
239128e3872SIan Lepore devargs.mda_uid = UID_ROOT;
240128e3872SIan Lepore devargs.mda_gid = GID_WHEEL;
241128e3872SIan Lepore devargs.mda_mode = 0660;
242128e3872SIan Lepore devargs.mda_si_drv1 = sc;
243128e3872SIan Lepore err = make_dev_s(&devargs, &sc->pps_cdev, PPS_CDEV_NAME "%d",
244128e3872SIan Lepore device_get_unit(dev));
245128e3872SIan Lepore if (err != 0) {
246128e3872SIan Lepore device_printf(dev, "Unable to create pps cdev\n");
247128e3872SIan Lepore gpiopps_detach(dev);
248128e3872SIan Lepore return (err);
249128e3872SIan Lepore }
250128e3872SIan Lepore
251128e3872SIan Lepore return (0);
252128e3872SIan Lepore }
253128e3872SIan Lepore
254128e3872SIan Lepore static int
gpiopps_fdt_probe(device_t dev)255128e3872SIan Lepore gpiopps_fdt_probe(device_t dev)
256128e3872SIan Lepore {
257128e3872SIan Lepore
258128e3872SIan Lepore if (!ofw_bus_status_okay(dev))
259128e3872SIan Lepore return (ENXIO);
260128e3872SIan Lepore
261128e3872SIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
262128e3872SIan Lepore device_set_desc(dev, "GPIO PPS");
263128e3872SIan Lepore return (BUS_PROBE_DEFAULT);
264128e3872SIan Lepore }
265128e3872SIan Lepore
266128e3872SIan Lepore return (ENXIO);
267128e3872SIan Lepore }
268128e3872SIan Lepore
269128e3872SIan Lepore static device_method_t pps_fdt_methods[] = {
270128e3872SIan Lepore DEVMETHOD(device_probe, gpiopps_fdt_probe),
271128e3872SIan Lepore DEVMETHOD(device_attach, gpiopps_fdt_attach),
272128e3872SIan Lepore DEVMETHOD(device_detach, gpiopps_detach),
273128e3872SIan Lepore
274128e3872SIan Lepore DEVMETHOD_END
275128e3872SIan Lepore };
276128e3872SIan Lepore
277128e3872SIan Lepore static driver_t pps_fdt_driver = {
278128e3872SIan Lepore "gpiopps",
279128e3872SIan Lepore pps_fdt_methods,
280128e3872SIan Lepore sizeof(struct pps_softc),
281128e3872SIan Lepore };
282128e3872SIan Lepore
283*84c5f982SJohn Baldwin DRIVER_MODULE(gpiopps, simplebus, pps_fdt_driver, 0, 0);
284128e3872SIan Lepore
285128e3872SIan Lepore #endif /* FDT */
286