gpio_intel: Change GPIOcfg bits when needed to enable RX for interrupt pins.
[dragonfly.git] / sys / bus / gpio / gpio_intel / gpio_cherryview.c
1 /*
2  * Copyright (c) 2016 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Imre Vadász <imre@vdsz.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * Cherryview GPIO support.
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/errno.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/bus.h>
46
47 #include <sys/rman.h>
48
49 #include "opt_acpi.h"
50 #include "acpi.h"
51 #include <dev/acpica/acpivar.h>
52
53 #include "gpio_intel_var.h"
54
55 #include "gpio_if.h"
56
57 #define CHV_GPIO_REG_IS         0x300
58 #define CHV_GPIO_REG_MASK       0x380
59 #define CHV_GPIO_REG_PINS       0x4400  /* start of pin control registers */
60
61 #define CHV_GPIO_REGOFF_CTL0    0x0
62 #define CHV_GPIO_REGOFF_CTL1    0x4
63
64 #define CHV_GPIO_CTL0_RXSTATE   0x00000001u
65 #define CHV_GPIO_CTL0_TXSTATE   0x00000002u
66 #define CHV_GPIO_CTL0_GPIOCFG_MASK 0x00000700u
67 #define CHV_GPIO_CTL0_GPIOEN    0x00008000u
68 #define CHV_GPIO_CTL0_PULLUP    0x00800000u
69 #define CHV_GPIO_CTL1_INTCFG_MASK 0x00000007u
70 #define CHV_GPIO_CTL1_INVRXDATA 0x00000040u
71
72 #define CHV_GPIO_PINSIZE        0x8     /* 8 bytes for each pin */
73 #define CHV_GPIO_PINCHUNK       15      /* 15 pins at a time */
74 #define CHV_GPIO_PININC         0x400   /* every 0x400 bytes */
75
76 #define PIN_ADDRESS(x)                                  \
77     (CHV_GPIO_REG_PINS +                                \
78      ((x) / CHV_GPIO_PINCHUNK) * CHV_GPIO_PININC +      \
79      ((x) % CHV_GPIO_PINCHUNK) * CHV_GPIO_PINSIZE)
80
81 #define PIN_CTL0(x)             (PIN_ADDRESS(x) + CHV_GPIO_REGOFF_CTL0)
82 #define PIN_CTL1(x)             (PIN_ADDRESS(x) + CHV_GPIO_REGOFF_CTL1)
83
84 static void     gpio_cherryview_init(struct gpio_intel_softc *sc);
85 static void     gpio_cherryview_intr(void *arg);
86 static int      gpio_cherryview_map_intr(struct gpio_intel_softc *sc,
87                     uint16_t pin, int trigger, int polarity, int termination,
88                     void *arg, driver_intr_t *handler);
89 static void     gpio_cherryview_unmap_intr(struct gpio_intel_softc *sc,
90                     uint16_t pin);
91 static int      gpio_cherryview_read_pin(struct gpio_intel_softc *sc,
92                     uint16_t pin);
93 static void     gpio_cherryview_write_pin(struct gpio_intel_softc *sc,
94                     uint16_t pin, int value);
95
96 static struct gpio_intel_fns gpio_cherryview_fns = {
97         .init = gpio_cherryview_init,
98         .intr = gpio_cherryview_intr,
99         .map_intr = gpio_cherryview_map_intr,
100         .unmap_intr = gpio_cherryview_unmap_intr,
101         .read_pin = gpio_cherryview_read_pin,
102         .write_pin = gpio_cherryview_write_pin,
103 };
104
105 /* _UID=1 */
106 static struct pinrange chv_sw_ranges[] = {
107         { 0, 7 },
108         { 15, 22 },
109         { 30, 37 },
110         { 45, 52 },
111         { 60, 67 },
112         { 75, 82 },
113         { 90, 97 },
114         { -1, -1 }
115 };
116
117 /* _UID=2 */
118 static struct pinrange chv_n_ranges[] = {
119         { 0, 8 },
120         { 15, 27 },
121         { 30, 41 },
122         { 45, 56 },
123         { 60, 72 },
124         { -1, -1 }
125 };
126
127 /* _UID=3 */
128 static struct pinrange chv_e_ranges[] = {
129         { 0, 11 },
130         { 15, 26 },
131         { -1, -1 }
132 };
133
134 /* _UID=4 */
135 static struct pinrange chv_se_ranges[] = {
136         { 0, 7 },
137         { 15, 26 },
138         { 30, 35 },
139         { 45, 52 },
140         { 60, 69 },
141         { 75, 85 },
142         { -1, -1 }
143 };
144
145 int
146 gpio_cherryview_matchuid(struct gpio_intel_softc *sc)
147 {
148         ACPI_HANDLE handle;
149
150         handle = acpi_get_handle(sc->dev);
151         if (acpi_MatchUid(handle, "1")) {
152                 sc->ranges = chv_sw_ranges;
153         } else if (acpi_MatchUid(handle, "2")) {
154                 sc->ranges = chv_n_ranges;
155         } else if (acpi_MatchUid(handle, "3")) {
156                 sc->ranges = chv_e_ranges;
157         } else if (acpi_MatchUid(handle, "4")) {
158                 sc->ranges = chv_se_ranges;
159         } else {
160                 return (ENXIO);
161         }
162
163         sc->fns = &gpio_cherryview_fns;
164
165         return (0);
166 }
167
168 static void
169 gpio_cherryview_init(struct gpio_intel_softc *sc)
170 {
171         /* mask and clear all interrupt lines */
172         bus_write_4(sc->mem_res, CHV_GPIO_REG_MASK, 0);
173         bus_write_4(sc->mem_res, CHV_GPIO_REG_IS, 0xffff);
174 }
175
176 static void
177 gpio_cherryview_intr(void *arg)
178 {
179         struct gpio_intel_softc *sc = (struct gpio_intel_softc *)arg;
180         struct pin_intr_map *mapping;
181         uint32_t status;
182         int i;
183
184         status = bus_read_4(sc->mem_res, CHV_GPIO_REG_IS);
185         bus_write_4(sc->mem_res, CHV_GPIO_REG_IS, status);
186         for (i = 0; i < 16; i++) {
187                 if (status & (1 << i)) {
188                         mapping = &sc->intrmaps[i];
189                         if (mapping->pin != -1 && mapping->handler != NULL)
190                                 mapping->handler(mapping->arg);
191                 }
192         }
193 }
194
195 /* XXX Add shared/exclusive argument. */
196 static int
197 gpio_cherryview_map_intr(struct gpio_intel_softc *sc, uint16_t pin, int trigger,
198     int polarity, int termination, void *arg, driver_intr_t *handler)
199 {
200         uint32_t reg, reg1, reg2;
201         uint32_t intcfg, new_intcfg, gpiocfg, new_gpiocfg;
202         int i;
203
204         reg1 = bus_read_4(sc->mem_res, PIN_CTL0(pin));
205         reg2 = bus_read_4(sc->mem_res, PIN_CTL1(pin));
206         device_printf(sc->dev,
207             "pin=%d trigger=%d polarity=%d ctrl0=0x%08x ctrl1=0x%08x\n",
208             pin, trigger, polarity, reg1, reg2);
209
210         new_intcfg = intcfg = reg2 & CHV_GPIO_CTL1_INTCFG_MASK;
211         new_gpiocfg = gpiocfg = reg1 & CHV_GPIO_CTL0_GPIOCFG_MASK;
212
213         /*
214          * Sanity Checks, for now we just abort if the configuration doesn't
215          * match our expectations.
216          */
217         if (!(reg1 & CHV_GPIO_CTL0_GPIOEN)) {
218                 device_printf(sc->dev, "GPIO mode is disabled\n");
219                 return (ENXIO);
220         }
221         if (gpiocfg != 0x0 && gpiocfg != 0x200) {
222                 device_printf(sc->dev, "RX is disabled\n");
223                 if (gpiocfg == 0x100)
224                         new_gpiocfg = 0x000;
225                 else if (gpiocfg == 0x300)
226                         new_gpiocfg = 0x200;
227                 else
228                         return (ENXIO);
229         }
230         if (trigger == ACPI_LEVEL_SENSITIVE) {
231                 if (intcfg != 4) {
232                         device_printf(sc->dev,
233                             "trigger is %x, should be 4 (Level)\n", intcfg);
234                         return (ENXIO);
235                 }
236                 if (polarity == ACPI_ACTIVE_BOTH) {
237                         device_printf(sc->dev,
238                             "ACTIVE_BOTH incompatible with level trigger\n");
239                         return (ENXIO);
240                 } else if (polarity == ACPI_ACTIVE_LOW) {
241                         if (!(reg2 & CHV_GPIO_CTL1_INVRXDATA)) {
242                                 device_printf(sc->dev,
243                                     "Invert RX not enabled (needed for "
244                                     "level/low trigger/polarity)\n");
245                                 return (ENXIO);
246                         }
247                 } else {
248                         if (reg2 & CHV_GPIO_CTL1_INVRXDATA) {
249                                 device_printf(sc->dev,
250                                     "Invert RX should not be enabled for "
251                                     "level/high trigger/polarity\n");
252                                 return (ENXIO);
253                         }
254                 }
255         } else {
256                 /*
257                  * For edge-triggered interrupts it's definitely harmless to
258                  * change between rising-edge, falling-edge and both-edges
259                  * triggering.
260                  */
261                 if (polarity == ACPI_ACTIVE_HIGH && intcfg != 2) {
262                         device_printf(sc->dev,
263                             "Wrong interrupt configuration, is 0x%x should "
264                             "be 0x%x\n", intcfg, 2);
265                         if (intcfg == 1 || intcfg == 3)
266                                 new_intcfg = 2;
267                         else
268                                 return (ENXIO);
269                 } else if (polarity == ACPI_ACTIVE_LOW && intcfg != 1) {
270                         device_printf(sc->dev,
271                             "Wrong interrupt configuration, is 0x%x should "
272                             "be 0x%x\n", intcfg, 1);
273                         if (intcfg == 2 || intcfg == 3)
274                                 new_intcfg = 1;
275                         else
276                                 return (ENXIO);
277                 } else if (polarity == ACPI_ACTIVE_BOTH && intcfg != 3) {
278                         device_printf(sc->dev,
279                             "Wrong interrupt configuration, is 0x%x should "
280                             "be 0x%x\n", intcfg, 3);
281                         if (intcfg == 1 || intcfg == 2)
282                                 new_intcfg = 3;
283                         else
284                                 return (ENXIO);
285                 }
286         }
287         if (termination == ACPI_PIN_CONFIG_PULLUP &&
288             !(reg1 & CHV_GPIO_CTL0_PULLUP)) {
289                 device_printf(sc->dev,
290                     "Wrong termination, is pull-down, should be pull-up\n");
291                 return (ENXIO);
292         } else if (termination == ACPI_PIN_CONFIG_PULLDOWN &&
293             (reg1 & CHV_GPIO_CTL0_PULLUP)) {
294                 device_printf(sc->dev,
295                     "Wrong termination, is pull-up, should be pull-down\n");
296                 return (ENXIO);
297         }
298
299         /* Check if the interrupt/line configured by BIOS/UEFI is unused */
300         i = (reg1 >> 28) & 0xf;
301         if (sc->intrmaps[i].pin != -1) {
302                 device_printf(sc->dev, "Interrupt line %d already used\n", i);
303                 return (ENXIO);
304         }
305
306         if (new_intcfg != intcfg) {
307                 device_printf(sc->dev,
308                     "Switching interrupt configuration from 0x%x to 0x%x\n",
309                     intcfg, new_intcfg);
310                 reg = reg2 & ~CHV_GPIO_CTL1_INTCFG_MASK;
311                 reg |= (new_intcfg & CHV_GPIO_CTL1_INTCFG_MASK) << 0;
312                 bus_write_4(sc->mem_res, PIN_CTL1(pin), reg);
313         }
314
315         if (new_gpiocfg != gpiocfg) {
316                 device_printf(sc->dev,
317                     "Switching gpio configuration from 0x%x to 0x%x\n",
318                     gpiocfg, new_gpiocfg);
319                 reg = reg1 & ~CHV_GPIO_CTL0_GPIOCFG_MASK;
320                 reg |= (new_gpiocfg & CHV_GPIO_CTL0_GPIOCFG_MASK) << 0;
321                 bus_write_4(sc->mem_res, PIN_CTL0(pin), reg);
322         }
323
324         sc->intrmaps[i].pin = pin;
325         sc->intrmaps[i].arg = arg;
326         sc->intrmaps[i].handler = handler;
327         sc->intrmaps[i].orig_intcfg = intcfg;
328         sc->intrmaps[i].orig_gpiocfg = gpiocfg;
329
330         /* unmask interrupt */
331         reg = bus_read_4(sc->mem_res, CHV_GPIO_REG_MASK);
332         reg |= (1 << i);
333         bus_write_4(sc->mem_res, CHV_GPIO_REG_MASK, reg);
334
335         return (0);
336 }
337
338 static void
339 gpio_cherryview_unmap_intr(struct gpio_intel_softc *sc, uint16_t pin)
340 {
341         uint32_t reg, intcfg, gpiocfg;
342         int i;
343
344         for (i = 0; i < 16; i++) {
345                 if (sc->intrmaps[i].pin == pin) {
346                         intcfg = sc->intrmaps[i].orig_intcfg;
347                         intcfg &= CHV_GPIO_CTL1_INTCFG_MASK;
348
349                         gpiocfg = sc->intrmaps[i].orig_gpiocfg;
350                         gpiocfg &= CHV_GPIO_CTL0_GPIOCFG_MASK;
351
352                         sc->intrmaps[i].pin = -1;
353                         sc->intrmaps[i].arg = NULL;
354                         sc->intrmaps[i].handler = NULL;
355                         sc->intrmaps[i].orig_intcfg = 0;
356                         sc->intrmaps[i].orig_gpiocfg = 0;
357
358                         /* mask interrupt line */
359                         reg = bus_read_4(sc->mem_res, CHV_GPIO_REG_MASK);
360                         reg &= ~(1 << i);
361                         bus_write_4(sc->mem_res, CHV_GPIO_REG_MASK, reg);
362
363                         /* Restore interrupt configuration if needed */
364                         reg = bus_read_4(sc->mem_res, PIN_CTL1(pin));
365                         if ((reg & CHV_GPIO_CTL1_INTCFG_MASK) != intcfg) {
366                                 reg &= ~CHV_GPIO_CTL1_INTCFG_MASK;
367                                 reg |= intcfg;
368                                 bus_write_4(sc->mem_res, PIN_CTL1(pin), reg);
369                         }
370
371                         /* Restore gpio configuration if needed */
372                         reg = bus_read_4(sc->mem_res, PIN_CTL0(pin));
373                         if ((reg & CHV_GPIO_CTL0_GPIOCFG_MASK) != gpiocfg) {
374                                 reg &= ~CHV_GPIO_CTL0_GPIOCFG_MASK;
375                                 reg |= gpiocfg;
376                                 bus_write_4(sc->mem_res, PIN_CTL0(pin), reg);
377                         }
378                 }
379         }
380 }
381
382 static int
383 gpio_cherryview_read_pin(struct gpio_intel_softc *sc, uint16_t pin)
384 {
385         uint32_t reg;
386         int val;
387
388         reg = bus_read_4(sc->mem_res, PIN_CTL0(pin));
389         /* Verify that RX is enabled */
390         KKASSERT((reg & CHV_GPIO_CTL0_GPIOCFG_MASK) == 0x0 ||
391             (reg & CHV_GPIO_CTL0_GPIOCFG_MASK) == 0x200);
392
393         if (reg & CHV_GPIO_CTL0_RXSTATE)
394                 val = 1;
395         else
396                 val = 0;
397
398         return (val);
399 }
400
401 static void
402 gpio_cherryview_write_pin(struct gpio_intel_softc *sc, uint16_t pin, int value)
403 {
404         uint32_t reg1, reg2;
405
406         reg2 = bus_read_4(sc->mem_res, PIN_CTL1(pin));
407         /* Verify that interrupt is disabled */
408         KKASSERT((reg2 & CHV_GPIO_CTL1_INTCFG_MASK) == 0);
409
410         reg1 = bus_read_4(sc->mem_res, PIN_CTL0(pin));
411         /* Verify that TX is enabled */
412         KKASSERT((reg1 & CHV_GPIO_CTL0_GPIOCFG_MASK) == 0 ||
413             (reg1 & CHV_GPIO_CTL0_GPIOCFG_MASK) == 0x100);
414
415         if (value)
416                 reg1 |= CHV_GPIO_CTL0_TXSTATE;
417         else
418                 reg1 &= ~CHV_GPIO_CTL0_TXSTATE;
419         bus_write_4(sc->mem_res, PIN_CTL0(pin), reg1);
420 }