xref: /freebsd/sys/arm/freescale/vybrid/vf_port.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Vybrid Family Port control and interrupts (PORT)
31  * Chapter 6, Vybrid Reference Manual, Rev. 5, 07/2013
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/rman.h>
41 #include <sys/timeet.h>
42 #include <sys/timetc.h>
43 #include <sys/watchdog.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <machine/bus.h>
50 #include <machine/cpu.h>
51 #include <machine/intr.h>
52 
53 #include <arm/freescale/vybrid/vf_port.h>
54 #include <arm/freescale/vybrid/vf_common.h>
55 
56 /* Pin Control Register */
57 #define PORT_PCR(n)		(0x1000 * (n >> 5) + 0x4 * (n % 32))
58 #define	 PCR_IRQC_S	16
59 #define	 PCR_IRQC_M	0xF
60 #define	 PCR_DMA_RE	0x1
61 #define	 PCR_DMA_FE	0x2
62 #define	 PCR_DMA_EE	0x3
63 #define	 PCR_INT_LZ	0x8
64 #define	 PCR_INT_RE	0x9
65 #define	 PCR_INT_FE	0xA
66 #define	 PCR_INT_EE	0xB
67 #define	 PCR_INT_LO	0xC
68 #define	 PCR_ISF	(1 << 24)
69 #define	PORT0_ISFR	0xA0	/* Interrupt Status Flag Register */
70 #define	PORT0_DFER	0xC0	/* Digital Filter Enable Register */
71 #define	PORT0_DFCR	0xC4	/* Digital Filter Clock Register */
72 #define	PORT0_DFWR	0xC8	/* Digital Filter Width Register */
73 
74 struct port_event {
75 	uint32_t	enabled;
76 	uint32_t	mux_num;
77 	uint32_t	mux_src;
78 	uint32_t	mux_chn;
79 	void		(*ih) (void *);
80 	void		*ih_user;
81 	enum ev_type	pevt;
82 };
83 
84 static struct port_event event_map[NGPIO];
85 
86 struct port_softc {
87 	struct resource		*res[6];
88 	bus_space_tag_t		bst;
89 	bus_space_handle_t	bsh;
90 	void			*gpio_ih[NGPIO];
91 };
92 
93 struct port_softc *port_sc;
94 
95 static struct resource_spec port_spec[] = {
96 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
97 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
98 	{ SYS_RES_IRQ,		1,	RF_ACTIVE },
99 	{ SYS_RES_IRQ,		2,	RF_ACTIVE },
100 	{ SYS_RES_IRQ,		3,	RF_ACTIVE },
101 	{ SYS_RES_IRQ,		4,	RF_ACTIVE },
102 	{ -1, 0 }
103 };
104 
105 static int
106 port_intr(void *arg)
107 {
108 	struct port_event *pev;
109 	struct port_softc *sc;
110 	int reg;
111 	int i;
112 
113 	sc = arg;
114 
115 	for (i = 0; i < NGPIO; i++) {
116 		reg = READ4(sc, PORT_PCR(i));
117 		if (reg & PCR_ISF) {
118 			/* Clear interrupt */
119 			WRITE4(sc, PORT_PCR(i), reg);
120 
121 			/* Handle event */
122 			pev = &event_map[i];
123 			if (pev->enabled == 1) {
124 				if (pev->ih != NULL) {
125 					pev->ih(pev->ih_user);
126 				}
127 			}
128 		}
129 	}
130 
131 	return (FILTER_HANDLED);
132 }
133 
134 int
135 port_setup(int pnum, enum ev_type pevt, void (*ih)(void *), void *ih_user)
136 {
137 	struct port_event *pev;
138 	struct port_softc *sc;
139 	int reg;
140 	int val;
141 
142 	sc = port_sc;
143 
144 	switch (pevt) {
145 	case DMA_RISING_EDGE:
146 		val = PCR_DMA_RE;
147 		break;
148 	case DMA_FALLING_EDGE:
149 		val = PCR_DMA_FE;
150 		break;
151 	case DMA_EITHER_EDGE:
152 		val = PCR_DMA_EE;
153 		break;
154 	case INT_LOGIC_ZERO:
155 		val = PCR_INT_LZ;
156 		break;
157 	case INT_RISING_EDGE:
158 		val = PCR_INT_RE;
159 		break;
160 	case INT_FALLING_EDGE:
161 		val = PCR_INT_FE;
162 		break;
163 	case INT_EITHER_EDGE:
164 		val = PCR_INT_EE;
165 		break;
166 	case INT_LOGIC_ONE:
167 		val = PCR_INT_LO;
168 		break;
169 	default:
170 		return (-1);
171 	}
172 
173 	reg = READ4(sc, PORT_PCR(pnum));
174 	reg &= ~(PCR_IRQC_M << PCR_IRQC_S);
175 	reg |= (val << PCR_IRQC_S);
176 	WRITE4(sc, PORT_PCR(pnum), reg);
177 
178 	pev = &event_map[pnum];
179 	pev->ih = ih;
180 	pev->ih_user = ih_user;
181 	pev->pevt = pevt;
182 	pev->enabled = 1;
183 
184 	return (0);
185 }
186 
187 static int
188 port_probe(device_t dev)
189 {
190 
191 	if (!ofw_bus_status_okay(dev))
192 		return (ENXIO);
193 
194 	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-port"))
195 		return (ENXIO);
196 
197 	device_set_desc(dev, "Vybrid Family Port control and interrupts");
198 	return (BUS_PROBE_DEFAULT);
199 }
200 
201 static int
202 port_attach(device_t dev)
203 {
204 	struct port_softc *sc;
205 	int irq;
206 
207 	sc = device_get_softc(dev);
208 
209 	if (bus_alloc_resources(dev, port_spec, sc->res)) {
210 		device_printf(dev, "could not allocate resources\n");
211 		return (ENXIO);
212 	}
213 
214 	/* Memory interface */
215 	sc->bst = rman_get_bustag(sc->res[0]);
216 	sc->bsh = rman_get_bushandle(sc->res[0]);
217 
218 	port_sc = sc;
219 
220 	for (irq = 0; irq < NPORTS; irq ++) {
221 		if ((bus_setup_intr(dev, sc->res[1 + irq], INTR_TYPE_MISC,
222 		    port_intr, NULL, sc, &sc->gpio_ih[irq]))) {
223 			device_printf(dev,
224 			    "ERROR: Unable to register interrupt handler\n");
225 			return (ENXIO);
226 		}
227 	}
228 
229 	return (0);
230 }
231 
232 static device_method_t port_methods[] = {
233 	DEVMETHOD(device_probe,		port_probe),
234 	DEVMETHOD(device_attach,	port_attach),
235 	{ 0, 0 }
236 };
237 
238 static driver_t port_driver = {
239 	"port",
240 	port_methods,
241 	sizeof(struct port_softc),
242 };
243 
244 DRIVER_MODULE(port, simplebus, port_driver, 0, 0);
245