]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/ti/ti_gpio.c
Bring in the last round of updates before adding the interrupt support.
[FreeBSD/FreeBSD.git] / sys / arm / ti / ti_gpio.c
1 /*-
2  * Copyright (c) 2011
3  *      Ben Gray <ben.r.gray@gmail.com>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /**
29  *      Very simple GPIO (general purpose IO) driver module for TI OMAP SoC's.
30  *
31  *      Currently this driver only does the basics, get a value on a pin & set a
32  *      value on a pin. Hopefully over time I'll expand this to be a bit more generic
33  *      and support interrupts and other various bits on the SoC can do ... in the
34  *      meantime this is all you get.
35  *
36  *      Beware the OMA datasheet(s) lists GPIO banks 1-6, whereas I've used 0-5 here
37  *      in the code.
38  *
39  *
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/bus.h>
48
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/rman.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/gpio.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58
59 #include <arm/ti/ti_cpuid.h>
60 #include <arm/ti/ti_gpio.h>
61 #include <arm/ti/ti_scm.h>
62 #include <arm/ti/ti_prcm.h>
63
64 #include <dev/fdt/fdt_common.h>
65 #include <dev/ofw/openfirm.h>
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68
69 #include "gpio_if.h"
70 #include "ti_gpio_if.h"
71
72 #if !defined(SOC_OMAP4) && !defined(SOC_TI_AM335X)
73 #error "Unknown SoC"
74 #endif
75
76 /* Register definitions */
77 #define TI_GPIO_REVISION                0x0000
78 #define TI_GPIO_SYSCONFIG               0x0010
79 #define TI_GPIO_IRQSTATUS_RAW_0         0x0024
80 #define TI_GPIO_IRQSTATUS_RAW_1         0x0028
81 #define TI_GPIO_IRQSTATUS_0             0x002C
82 #define TI_GPIO_IRQSTATUS_1             0x0030
83 #define TI_GPIO_IRQSTATUS_SET_0         0x0034
84 #define TI_GPIO_IRQSTATUS_SET_1         0x0038
85 #define TI_GPIO_IRQSTATUS_CLR_0         0x003C
86 #define TI_GPIO_IRQSTATUS_CLR_1         0x0040
87 #define TI_GPIO_IRQWAKEN_0              0x0044
88 #define TI_GPIO_IRQWAKEN_1              0x0048
89 #define TI_GPIO_SYSSTATUS               0x0114
90 #define TI_GPIO_IRQSTATUS1              0x0118
91 #define TI_GPIO_IRQENABLE1              0x011C
92 #define TI_GPIO_WAKEUPENABLE            0x0120
93 #define TI_GPIO_IRQSTATUS2              0x0128
94 #define TI_GPIO_IRQENABLE2              0x012C
95 #define TI_GPIO_CTRL                    0x0130
96 #define TI_GPIO_OE                      0x0134
97 #define TI_GPIO_DATAIN                  0x0138
98 #define TI_GPIO_DATAOUT                 0x013C
99 #define TI_GPIO_LEVELDETECT0            0x0140
100 #define TI_GPIO_LEVELDETECT1            0x0144
101 #define TI_GPIO_RISINGDETECT            0x0148
102 #define TI_GPIO_FALLINGDETECT           0x014C
103 #define TI_GPIO_DEBOUNCENABLE           0x0150
104 #define TI_GPIO_DEBOUNCINGTIME          0x0154
105 #define TI_GPIO_CLEARWKUPENA            0x0180
106 #define TI_GPIO_SETWKUENA               0x0184
107 #define TI_GPIO_CLEARDATAOUT            0x0190
108 #define TI_GPIO_SETDATAOUT              0x0194
109
110 /* Other SoC Specific definitions */
111 #define OMAP4_MAX_GPIO_BANKS            6
112 #define OMAP4_FIRST_GPIO_BANK           1
113 #define OMAP4_INTR_PER_BANK             1
114 #define OMAP4_GPIO_REV                  0x50600801
115 #define AM335X_MAX_GPIO_BANKS           4
116 #define AM335X_FIRST_GPIO_BANK          0
117 #define AM335X_INTR_PER_BANK            2
118 #define AM335X_GPIO_REV                 0x50600801
119 #define PINS_PER_BANK                   32
120 #define TI_GPIO_BANK(p)                 ((p) / PINS_PER_BANK)
121 #define TI_GPIO_MASK(p)                 (1U << ((p) % PINS_PER_BANK))
122
123 static u_int
124 ti_max_gpio_banks(void)
125 {
126         switch(ti_chip()) {
127 #ifdef SOC_OMAP4
128         case CHIP_OMAP_4:
129                 return (OMAP4_MAX_GPIO_BANKS);
130 #endif
131 #ifdef SOC_TI_AM335X
132         case CHIP_AM335X:
133                 return (AM335X_MAX_GPIO_BANKS);
134 #endif
135         }
136         return (0);
137 }
138
139 static u_int
140 ti_max_gpio_intrs(void)
141 {
142         switch(ti_chip()) {
143 #ifdef SOC_OMAP4
144         case CHIP_OMAP_4:
145                 return (OMAP4_MAX_GPIO_BANKS * OMAP4_INTR_PER_BANK);
146 #endif
147 #ifdef SOC_TI_AM335X
148         case CHIP_AM335X:
149                 return (AM335X_MAX_GPIO_BANKS * AM335X_INTR_PER_BANK);
150 #endif
151         }
152         return (0);
153 }
154
155 static u_int
156 ti_first_gpio_bank(void)
157 {
158         switch(ti_chip()) {
159 #ifdef SOC_OMAP4
160         case CHIP_OMAP_4:
161                 return (OMAP4_FIRST_GPIO_BANK);
162 #endif
163 #ifdef SOC_TI_AM335X
164         case CHIP_AM335X:
165                 return (AM335X_FIRST_GPIO_BANK);
166 #endif
167         }
168         return (0);
169 }
170
171 static uint32_t
172 ti_gpio_rev(void)
173 {
174         switch(ti_chip()) {
175 #ifdef SOC_OMAP4
176         case CHIP_OMAP_4:
177                 return (OMAP4_GPIO_REV);
178 #endif
179 #ifdef SOC_TI_AM335X
180         case CHIP_AM335X:
181                 return (AM335X_GPIO_REV);
182 #endif
183         }
184         return (0);
185 }
186
187 /**
188  *      ti_gpio_mem_spec - Resource specification used when allocating resources
189  *      ti_gpio_irq_spec - Resource specification used when allocating resources
190  *
191  *      This driver module can have up to six independent memory regions, each
192  *      region typically controls 32 GPIO pins.
193  *
194  *      On OMAP3 and OMAP4 there is only one physical interrupt line per bank,
195  *      but there are two set of registers which control the interrupt delivery
196  *      to internal subsystems.  The first set of registers control the
197  *      interrupts delivery to the MPU and the second set control the
198  *      interrupts delivery to the DSP.
199  *
200  *      On AM335x there are two physical interrupt lines for each GPIO module.
201  *      Each interrupt line is controlled by a set of registers.
202  */
203 static struct resource_spec ti_gpio_mem_spec[] = {
204         { SYS_RES_MEMORY,   0,  RF_ACTIVE },
205         { SYS_RES_MEMORY,   1,  RF_ACTIVE | RF_OPTIONAL },
206         { SYS_RES_MEMORY,   2,  RF_ACTIVE | RF_OPTIONAL },
207         { SYS_RES_MEMORY,   3,  RF_ACTIVE | RF_OPTIONAL },
208 #if !defined(SOC_TI_AM335X)
209         { SYS_RES_MEMORY,   4,  RF_ACTIVE | RF_OPTIONAL },
210         { SYS_RES_MEMORY,   5,  RF_ACTIVE | RF_OPTIONAL },
211 #endif
212         { -1,               0,  0 }
213 };
214 static struct resource_spec ti_gpio_irq_spec[] = {
215         { SYS_RES_IRQ,      0,  RF_ACTIVE },
216         { SYS_RES_IRQ,      1,  RF_ACTIVE | RF_OPTIONAL },
217         { SYS_RES_IRQ,      2,  RF_ACTIVE | RF_OPTIONAL },
218         { SYS_RES_IRQ,      3,  RF_ACTIVE | RF_OPTIONAL },
219         { SYS_RES_IRQ,      4,  RF_ACTIVE | RF_OPTIONAL },
220         { SYS_RES_IRQ,      5,  RF_ACTIVE | RF_OPTIONAL },
221 #if defined(SOC_TI_AM335X)
222         { SYS_RES_IRQ,      6,  RF_ACTIVE | RF_OPTIONAL },
223         { SYS_RES_IRQ,      7,  RF_ACTIVE | RF_OPTIONAL },
224 #endif
225         { -1,               0,  0 }
226 };
227
228 /**
229  *      Macros for driver mutex locking
230  */
231 #define TI_GPIO_LOCK(_sc)               mtx_lock(&(_sc)->sc_mtx)
232 #define TI_GPIO_UNLOCK(_sc)             mtx_unlock(&(_sc)->sc_mtx)
233 #define TI_GPIO_LOCK_INIT(_sc)          \
234         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
235             "ti_gpio", MTX_DEF)
236 #define TI_GPIO_LOCK_DESTROY(_sc)       mtx_destroy(&_sc->sc_mtx)
237 #define TI_GPIO_ASSERT_LOCKED(_sc)      mtx_assert(&_sc->sc_mtx, MA_OWNED)
238 #define TI_GPIO_ASSERT_UNLOCKED(_sc)    mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)
239
240 /**
241  *      ti_gpio_read_4 - reads a 32-bit value from one of the GPIO registers
242  *      @sc: GPIO device context
243  *      @bank: The bank to read from
244  *      @off: The offset of a register from the GPIO register address range
245  *
246  *
247  *      RETURNS:
248  *      32-bit value read from the register.
249  */
250 static inline uint32_t
251 ti_gpio_read_4(struct ti_gpio_softc *sc, unsigned int bank, bus_size_t off)
252 {
253         return (bus_read_4(sc->sc_mem_res[bank], off));
254 }
255
256 /**
257  *      ti_gpio_write_4 - writes a 32-bit value to one of the GPIO registers
258  *      @sc: GPIO device context
259  *      @bank: The bank to write to
260  *      @off: The offset of a register from the GPIO register address range
261  *      @val: The value to write into the register
262  *
263  *      RETURNS:
264  *      nothing
265  */
266 static inline void
267 ti_gpio_write_4(struct ti_gpio_softc *sc, unsigned int bank, bus_size_t off,
268                  uint32_t val)
269 {
270         bus_write_4(sc->sc_mem_res[bank], off, val);
271 }
272
273 static inline void
274 ti_gpio_intr_clr(struct ti_gpio_softc *sc, unsigned int bank, uint32_t mask)
275 {
276
277         /* We clear both set of registers. */
278         ti_gpio_write_4(sc, bank, TI_GPIO_IRQSTATUS_CLR_0, mask);
279         ti_gpio_write_4(sc, bank, TI_GPIO_IRQSTATUS_CLR_1, mask);
280 }
281
282 /**
283  *      ti_gpio_pin_max - Returns the maximum number of GPIO pins
284  *      @dev: gpio device handle
285  *      @maxpin: pointer to a value that upon return will contain the maximum number
286  *               of pins in the device.
287  *
288  *
289  *      LOCKING:
290  *      No locking required, returns static data.
291  *
292  *      RETURNS:
293  *      Returns 0 on success otherwise an error code
294  */
295 static int
296 ti_gpio_pin_max(device_t dev, int *maxpin)
297 {
298
299         *maxpin = ti_max_gpio_banks() * PINS_PER_BANK - 1;
300
301         return (0);
302 }
303
304 static int
305 ti_gpio_valid_pin(struct ti_gpio_softc *sc, int pin)
306 {
307
308         if (pin > sc->sc_maxpin ||
309             TI_GPIO_BANK(pin) >= ti_max_gpio_banks() ||
310             sc->sc_mem_res[TI_GPIO_BANK(pin)] == NULL) {
311                 return (EINVAL);
312         }
313
314         return (0);
315 }
316
317 /**
318  *      ti_gpio_pin_getcaps - Gets the capabilties of a given pin
319  *      @dev: gpio device handle
320  *      @pin: the number of the pin
321  *      @caps: pointer to a value that upon return will contain the capabilities
322  *
323  *      Currently all pins have the same capability, notably:
324  *        - GPIO_PIN_INPUT
325  *        - GPIO_PIN_OUTPUT
326  *        - GPIO_PIN_PULLUP
327  *        - GPIO_PIN_PULLDOWN
328  *
329  *      LOCKING:
330  *      No locking required, returns static data.
331  *
332  *      RETURNS:
333  *      Returns 0 on success otherwise an error code
334  */
335 static int
336 ti_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
337 {
338         struct ti_gpio_softc *sc;
339
340         sc = device_get_softc(dev);
341         if (ti_gpio_valid_pin(sc, pin) != 0)
342                 return (EINVAL);
343
344         *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP |
345             GPIO_PIN_PULLDOWN);
346
347         return (0);
348 }
349
350 /**
351  *      ti_gpio_pin_getflags - Gets the current flags of a given pin
352  *      @dev: gpio device handle
353  *      @pin: the number of the pin
354  *      @flags: upon return will contain the current flags of the pin
355  *
356  *      Reads the current flags of a given pin, here we actually read the H/W
357  *      registers to determine the flags, rather than storing the value in the
358  *      setflags call.
359  *
360  *      LOCKING:
361  *      Internally locks the context
362  *
363  *      RETURNS:
364  *      Returns 0 on success otherwise an error code
365  */
366 static int
367 ti_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
368 {
369         struct ti_gpio_softc *sc;
370
371         sc = device_get_softc(dev);
372         if (ti_gpio_valid_pin(sc, pin) != 0)
373                 return (EINVAL);
374
375         /* Get the current pin state */
376         TI_GPIO_LOCK(sc);
377         TI_GPIO_GET_FLAGS(dev, pin, flags);
378         TI_GPIO_UNLOCK(sc);
379
380         return (0);
381 }
382
383 /**
384  *      ti_gpio_pin_getname - Gets the name of a given pin
385  *      @dev: gpio device handle
386  *      @pin: the number of the pin
387  *      @name: buffer to put the name in
388  *
389  *      The driver simply calls the pins gpio_n, where 'n' is obviously the number
390  *      of the pin.
391  *
392  *      LOCKING:
393  *      No locking required, returns static data.
394  *
395  *      RETURNS:
396  *      Returns 0 on success otherwise an error code
397  */
398 static int
399 ti_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
400 {
401         struct ti_gpio_softc *sc;
402
403         sc = device_get_softc(dev);
404         if (ti_gpio_valid_pin(sc, pin) != 0)
405                 return (EINVAL);
406
407         /* Set a very simple name */
408         snprintf(name, GPIOMAXNAME, "gpio_%u", pin);
409         name[GPIOMAXNAME - 1] = '\0';
410
411         return (0);
412 }
413
414 /**
415  *      ti_gpio_pin_setflags - Sets the flags for a given pin
416  *      @dev: gpio device handle
417  *      @pin: the number of the pin
418  *      @flags: the flags to set
419  *
420  *      The flags of the pin correspond to things like input/output mode, pull-ups,
421  *      pull-downs, etc.  This driver doesn't support all flags, only the following:
422  *        - GPIO_PIN_INPUT
423  *        - GPIO_PIN_OUTPUT
424  *        - GPIO_PIN_PULLUP
425  *        - GPIO_PIN_PULLDOWN
426  *
427  *      LOCKING:
428  *      Internally locks the context
429  *
430  *      RETURNS:
431  *      Returns 0 on success otherwise an error code
432  */
433 static int
434 ti_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
435 {
436         struct ti_gpio_softc *sc;
437         uint32_t oe;
438
439         sc = device_get_softc(dev);
440         if (ti_gpio_valid_pin(sc, pin) != 0)
441                 return (EINVAL);
442
443         /* Set the GPIO mode and state */
444         TI_GPIO_LOCK(sc);
445         if (TI_GPIO_SET_FLAGS(dev, pin, flags) != 0) {
446                 TI_GPIO_UNLOCK(sc);
447                 return (EINVAL);
448         }
449
450         /* If configuring as an output set the "output enable" bit */
451         oe = ti_gpio_read_4(sc, TI_GPIO_BANK(pin), TI_GPIO_OE);
452         if (flags & GPIO_PIN_INPUT)
453                 oe |= TI_GPIO_MASK(pin);
454         else
455                 oe &= ~TI_GPIO_MASK(pin);
456         ti_gpio_write_4(sc, TI_GPIO_BANK(pin), TI_GPIO_OE, oe);
457         TI_GPIO_UNLOCK(sc);
458         
459         return (0);
460 }
461
462 /**
463  *      ti_gpio_pin_set - Sets the current level on a GPIO pin
464  *      @dev: gpio device handle
465  *      @pin: the number of the pin
466  *      @value: non-zero value will drive the pin high, otherwise the pin is
467  *              driven low.
468  *
469  *
470  *      LOCKING:
471  *      Internally locks the context
472  *
473  *      RETURNS:
474  *      Returns 0 on success otherwise a error code
475  */
476 static int
477 ti_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
478 {
479         struct ti_gpio_softc *sc;
480         uint32_t reg;
481
482         sc = device_get_softc(dev);
483         if (ti_gpio_valid_pin(sc, pin) != 0)
484                 return (EINVAL);
485
486         TI_GPIO_LOCK(sc);
487         if (value == GPIO_PIN_LOW)
488                 reg = TI_GPIO_CLEARDATAOUT;
489         else
490                 reg = TI_GPIO_SETDATAOUT;
491         ti_gpio_write_4(sc, TI_GPIO_BANK(pin), reg, TI_GPIO_MASK(pin));
492         TI_GPIO_UNLOCK(sc);
493
494         return (0);
495 }
496
497 /**
498  *      ti_gpio_pin_get - Gets the current level on a GPIO pin
499  *      @dev: gpio device handle
500  *      @pin: the number of the pin
501  *      @value: pointer to a value that upond return will contain the pin value
502  *
503  *      The pin must be configured as an input pin beforehand, otherwise this
504  *      function will fail.
505  *
506  *      LOCKING:
507  *      Internally locks the context
508  *
509  *      RETURNS:
510  *      Returns 0 on success otherwise a error code
511  */
512 static int
513 ti_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
514 {
515         struct ti_gpio_softc *sc;
516         uint32_t oe, reg, val;
517
518         sc = device_get_softc(dev);
519         if (ti_gpio_valid_pin(sc, pin) != 0)
520                 return (EINVAL);
521
522         /*
523          * Return data from output latch when set as output and from the 
524          * input register otherwise.
525          */
526         TI_GPIO_LOCK(sc);
527         oe = ti_gpio_read_4(sc, TI_GPIO_BANK(pin), TI_GPIO_OE);
528         if (oe & TI_GPIO_MASK(pin))
529                 reg = TI_GPIO_DATAIN;
530         else
531                 reg = TI_GPIO_DATAOUT;
532         val = ti_gpio_read_4(sc, TI_GPIO_BANK(pin), reg);
533         *value = (val & TI_GPIO_MASK(pin)) ? 1 : 0;
534         TI_GPIO_UNLOCK(sc);
535
536         return (0);
537 }
538
539 /**
540  *      ti_gpio_pin_toggle - Toggles a given GPIO pin
541  *      @dev: gpio device handle
542  *      @pin: the number of the pin
543  *
544  *
545  *      LOCKING:
546  *      Internally locks the context
547  *
548  *      RETURNS:
549  *      Returns 0 on success otherwise a error code
550  */
551 static int
552 ti_gpio_pin_toggle(device_t dev, uint32_t pin)
553 {
554         struct ti_gpio_softc *sc;
555         uint32_t reg, val;
556
557         sc = device_get_softc(dev);
558         if (ti_gpio_valid_pin(sc, pin) != 0)
559                 return (EINVAL);
560
561         /* Toggle the pin */
562         TI_GPIO_LOCK(sc);
563         val = ti_gpio_read_4(sc, TI_GPIO_BANK(pin), TI_GPIO_DATAOUT);
564         if (val & TI_GPIO_MASK(pin))
565                 reg = TI_GPIO_CLEARDATAOUT;
566         else
567                 reg = TI_GPIO_SETDATAOUT;
568         ti_gpio_write_4(sc, TI_GPIO_BANK(pin), reg, TI_GPIO_MASK(pin));
569         TI_GPIO_UNLOCK(sc);
570
571         return (0);
572 }
573
574 /**
575  *      ti_gpio_intr - ISR for all GPIO modules
576  *      @arg: the soft context pointer
577  *
578  *      Unsused
579  *
580  *      LOCKING:
581  *      Internally locks the context
582  *
583  */
584 static void
585 ti_gpio_intr(void *arg)
586 {
587         struct ti_gpio_softc *sc = arg;
588
589         TI_GPIO_LOCK(sc);
590         /* TODO: something useful */
591         TI_GPIO_UNLOCK(sc);
592 }
593
594 static int
595 ti_gpio_attach_intr(device_t dev)
596 {
597         int i;
598         struct ti_gpio_softc *sc;
599
600         sc = device_get_softc(dev);
601         for (i = 0; i < ti_max_gpio_intrs(); i++) {
602                 if (sc->sc_irq_res[i] == NULL)
603                         break;
604
605                 /*
606                  * Register our interrupt handler for each of the IRQ resources.
607                  */
608                 if (bus_setup_intr(dev, sc->sc_irq_res[i],
609                     INTR_TYPE_MISC | INTR_MPSAFE, NULL, ti_gpio_intr, sc,
610                     &sc->sc_irq_hdl[i]) != 0) {
611                         device_printf(dev,
612                             "WARNING: unable to register interrupt handler\n");
613                         return (-1);
614                 }
615         }
616
617         return (0);
618 }
619
620 static int
621 ti_gpio_detach_intr(device_t dev)
622 {
623         int i;
624         struct ti_gpio_softc *sc;
625
626         /* Teardown our interrupt handlers. */
627         sc = device_get_softc(dev);
628         for (i = 0; i < ti_max_gpio_intrs(); i++) {
629                 if (sc->sc_irq_res[i] == NULL)
630                         break;
631
632                 if (sc->sc_irq_hdl[i]) {
633                         bus_teardown_intr(dev, sc->sc_irq_res[i],
634                             sc->sc_irq_hdl[i]);
635                 }
636         }
637
638         return (0);
639 }
640
641 static int
642 ti_gpio_bank_init(device_t dev, int bank)
643 {
644         int pin;
645         struct ti_gpio_softc *sc;
646         uint32_t flags, reg_oe, rev;
647
648         sc = device_get_softc(dev);
649
650         /* Enable the interface and functional clocks for the module. */
651         ti_prcm_clk_enable(GPIO0_CLK + ti_first_gpio_bank() + bank);
652
653         /*
654          * Read the revision number of the module.  TI don't publish the
655          * actual revision numbers, so instead the values have been
656          * determined by experimentation.
657          */
658         rev = ti_gpio_read_4(sc, bank, TI_GPIO_REVISION);
659
660         /* Check the revision. */
661         if (rev != ti_gpio_rev()) {
662                 device_printf(dev, "Warning: could not determine the revision "
663                     "of GPIO module %d (revision:0x%08x)\n", bank, rev);
664                 return (EINVAL);
665         }
666
667         /* Disable interrupts for all pins. */
668         ti_gpio_intr_clr(sc, bank, 0xffffffff);
669
670         /* Init OE register based on pads configuration. */
671         reg_oe = 0xffffffff;
672         for (pin = 0; pin < PINS_PER_BANK; pin++) {
673                 TI_GPIO_GET_FLAGS(dev, PINS_PER_BANK * bank + pin, &flags);
674                 if (flags & GPIO_PIN_OUTPUT)
675                         reg_oe &= ~(1UL << pin);
676         }
677         ti_gpio_write_4(sc, bank, TI_GPIO_OE, reg_oe);
678
679         return (0);
680 }
681
682 /**
683  *      ti_gpio_attach - attach function for the driver
684  *      @dev: gpio device handle
685  *
686  *      Allocates and sets up the driver context for all GPIO banks.  This function
687  *      expects the memory ranges and IRQs to already be allocated to the driver.
688  *
689  *      LOCKING:
690  *      None
691  *
692  *      RETURNS:
693  *      Always returns 0
694  */
695 static int
696 ti_gpio_attach(device_t dev)
697 {
698         struct ti_gpio_softc *sc;
699         unsigned int i;
700         int err;
701
702         sc = device_get_softc(dev);
703         sc->sc_dev = dev;
704         TI_GPIO_LOCK_INIT(sc);
705         ti_gpio_pin_max(dev, &sc->sc_maxpin);
706
707         /* There are up to 6 different GPIO register sets located in different
708          * memory areas on the chip.  The memory range should have been set for
709          * the driver when it was added as a child.
710          */
711         if (bus_alloc_resources(dev, ti_gpio_mem_spec, sc->sc_mem_res) != 0) {
712                 device_printf(dev, "Error: could not allocate mem resources\n");
713                 return (ENXIO);
714         }
715
716         /* Request the IRQ resources */
717         if (bus_alloc_resources(dev, ti_gpio_irq_spec, sc->sc_irq_res) != 0) {
718                 bus_release_resources(dev, ti_gpio_mem_spec, sc->sc_mem_res);
719                 device_printf(dev, "Error: could not allocate irq resources\n");
720                 return (ENXIO);
721         }
722
723         /* Setup the IRQ resources */
724         if (ti_gpio_attach_intr(dev) != 0) {
725                 ti_gpio_detach_intr(dev);
726                 bus_release_resources(dev, ti_gpio_irq_spec, sc->sc_irq_res);
727                 bus_release_resources(dev, ti_gpio_mem_spec, sc->sc_mem_res);
728                 return (ENXIO);
729         }
730
731         /* We need to go through each block and ensure the clocks are running and
732          * the module is enabled.  It might be better to do this only when the
733          * pins are configured which would result in less power used if the GPIO
734          * pins weren't used ... 
735          */
736         for (i = 0; i < ti_max_gpio_banks(); i++) {
737                 if (sc->sc_mem_res[i] != NULL) {
738                         /* Initialize the GPIO module. */
739                         err = ti_gpio_bank_init(dev, i);
740                         if (err != 0) {
741                                 ti_gpio_detach_intr(dev);
742                                 bus_release_resources(dev, ti_gpio_irq_spec,
743                                     sc->sc_irq_res);
744                                 bus_release_resources(dev, ti_gpio_mem_spec,
745                                     sc->sc_mem_res);
746                                 return (err);
747                         }
748                 }
749         }
750
751         /* Finish of the probe call */
752         device_add_child(dev, "gpioc", -1);
753         device_add_child(dev, "gpiobus", -1);
754
755         return (bus_generic_attach(dev));
756 }
757
758 /**
759  *      ti_gpio_detach - detach function for the driver
760  *      @dev: scm device handle
761  *
762  *      Allocates and sets up the driver context, this simply entails creating a
763  *      bus mappings for the SCM register set.
764  *
765  *      LOCKING:
766  *      None
767  *
768  *      RETURNS:
769  *      Always returns 0
770  */
771 static int
772 ti_gpio_detach(device_t dev)
773 {
774         struct ti_gpio_softc *sc = device_get_softc(dev);
775         unsigned int i;
776
777         KASSERT(mtx_initialized(&sc->sc_mtx), ("gpio mutex not initialized"));
778
779         /* Disable all interrupts */
780         for (i = 0; i < ti_max_gpio_banks(); i++) {
781                 if (sc->sc_mem_res[i] != NULL)
782                         ti_gpio_intr_clr(sc, i, 0xffffffff);
783         }
784
785         bus_generic_detach(dev);
786
787         /* Release the memory and IRQ resources. */
788         ti_gpio_detach_intr(dev);
789         bus_release_resources(dev, ti_gpio_irq_spec, sc->sc_irq_res);
790         bus_release_resources(dev, ti_gpio_mem_spec, sc->sc_mem_res);
791
792         TI_GPIO_LOCK_DESTROY(sc);
793
794         return (0);
795 }
796
797 static phandle_t
798 ti_gpio_get_node(device_t bus, device_t dev)
799 {
800
801         /* We only have one child, the GPIO bus, which needs our own node. */
802         return (ofw_bus_get_node(bus));
803 }
804
805 static device_method_t ti_gpio_methods[] = {
806         DEVMETHOD(device_attach, ti_gpio_attach),
807         DEVMETHOD(device_detach, ti_gpio_detach),
808
809         /* GPIO protocol */
810         DEVMETHOD(gpio_pin_max, ti_gpio_pin_max),
811         DEVMETHOD(gpio_pin_getname, ti_gpio_pin_getname),
812         DEVMETHOD(gpio_pin_getflags, ti_gpio_pin_getflags),
813         DEVMETHOD(gpio_pin_getcaps, ti_gpio_pin_getcaps),
814         DEVMETHOD(gpio_pin_setflags, ti_gpio_pin_setflags),
815         DEVMETHOD(gpio_pin_get, ti_gpio_pin_get),
816         DEVMETHOD(gpio_pin_set, ti_gpio_pin_set),
817         DEVMETHOD(gpio_pin_toggle, ti_gpio_pin_toggle),
818
819         /* ofw_bus interface */
820         DEVMETHOD(ofw_bus_get_node, ti_gpio_get_node),
821
822         {0, 0},
823 };
824
825 driver_t ti_gpio_driver = {
826         "gpio",
827         ti_gpio_methods,
828         sizeof(struct ti_gpio_softc),
829 };