mpc85xx_gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*-
  2. * Copyright (c) 2015 Justin Hibbits
  3. * Copyright (c) 2013 Thomas Skibo
  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 THE 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 THE 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. * $FreeBSD$
  28. */
  29. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/conf.h>
  34. #include <sys/bus.h>
  35. #include <sys/kernel.h>
  36. #include <sys/module.h>
  37. #include <sys/lock.h>
  38. #include <sys/mutex.h>
  39. #include <sys/resource.h>
  40. #include <sys/rman.h>
  41. #include <sys/gpio.h>
  42. #include <machine/bus.h>
  43. #include <machine/resource.h>
  44. #include <machine/stdarg.h>
  45. #include <dev/fdt/fdt_common.h>
  46. #include <dev/gpio/gpiobusvar.h>
  47. #include <dev/ofw/ofw_bus.h>
  48. #include <dev/ofw/ofw_bus_subr.h>
  49. #include "gpio_if.h"
  50. #define MAXPIN (7)
  51. #define VALID_PIN(u) ((u) >= 0 && (u) <= MAXPIN)
  52. #define GPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
  53. #define GPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
  54. #define GPIO_LOCK_INIT(sc) \
  55. mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
  56. "gpio", MTX_DEF)
  57. #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
  58. struct mpc85xx_gpio_softc {
  59. device_t dev;
  60. device_t busdev;
  61. struct mtx sc_mtx;
  62. struct resource *out_res; /* Memory resource */
  63. struct resource *in_res;
  64. };
  65. static device_t
  66. mpc85xx_gpio_get_bus(device_t dev)
  67. {
  68. struct mpc85xx_gpio_softc *sc;
  69. sc = device_get_softc(dev);
  70. return (sc->busdev);
  71. }
  72. static int
  73. mpc85xx_gpio_pin_max(device_t dev, int *maxpin)
  74. {
  75. *maxpin = MAXPIN;
  76. return (0);
  77. }
  78. /* Get a specific pin's capabilities. */
  79. static int
  80. mpc85xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
  81. {
  82. if (!VALID_PIN(pin))
  83. return (EINVAL);
  84. *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
  85. return (0);
  86. }
  87. /* Get a specific pin's name. */
  88. static int
  89. mpc85xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
  90. {
  91. if (!VALID_PIN(pin))
  92. return (EINVAL);
  93. snprintf(name, GPIOMAXNAME, "GPIO%d", pin);
  94. name[GPIOMAXNAME-1] = '\0';
  95. return (0);
  96. }
  97. /* Set a specific output pin's value. */
  98. static int
  99. mpc85xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
  100. {
  101. struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
  102. uint32_t outvals;
  103. uint8_t pinbit;
  104. if (!VALID_PIN(pin) || value > 1)
  105. return (EINVAL);
  106. GPIO_LOCK(sc);
  107. pinbit = 31 - pin;
  108. outvals = bus_read_4(sc->out_res, 0);
  109. outvals &= ~(1 << pinbit);
  110. outvals |= (value << pinbit);
  111. bus_write_4(sc->out_res, 0, outvals);
  112. GPIO_UNLOCK(sc);
  113. return (0);
  114. }
  115. /* Get a specific pin's input value. */
  116. static int
  117. mpc85xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
  118. {
  119. struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
  120. if (!VALID_PIN(pin))
  121. return (EINVAL);
  122. *value = (bus_read_4(sc->in_res, 0) >> (31 - pin)) & 1;
  123. return (0);
  124. }
  125. /* Toggle a pin's output value. */
  126. static int
  127. mpc85xx_gpio_pin_toggle(device_t dev, uint32_t pin)
  128. {
  129. struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
  130. uint32_t val;
  131. if (!VALID_PIN(pin))
  132. return (EINVAL);
  133. GPIO_LOCK(sc);
  134. val = bus_read_4(sc->out_res, 0);
  135. val ^= (1 << (31 - pin));
  136. bus_write_4(sc->out_res, 0, val);
  137. GPIO_UNLOCK(sc);
  138. return (0);
  139. }
  140. static int
  141. mpc85xx_gpio_probe(device_t dev)
  142. {
  143. uint32_t svr;
  144. if (!ofw_bus_status_okay(dev))
  145. return (ENXIO);
  146. if (!ofw_bus_is_compatible(dev, "gpio"))
  147. return (ENXIO);
  148. svr = mfspr(SPR_SVR);
  149. switch (SVR_VER(svr)) {
  150. case SVR_MPC8533:
  151. case SVR_MPC8533E:
  152. break;
  153. default:
  154. return (ENXIO);
  155. }
  156. device_set_desc(dev, "MPC85xx GPIO driver");
  157. return (0);
  158. }
  159. static int mpc85xx_gpio_detach(device_t dev);
  160. static int
  161. mpc85xx_gpio_attach(device_t dev)
  162. {
  163. struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
  164. int rid;
  165. sc->dev = dev;
  166. GPIO_LOCK_INIT(sc);
  167. /* Allocate memory. */
  168. rid = 0;
  169. sc->out_res = bus_alloc_resource_any(dev,
  170. SYS_RES_MEMORY, &rid, RF_ACTIVE);
  171. if (sc->out_res == NULL) {
  172. device_printf(dev, "Can't allocate memory for device output port");
  173. mpc85xx_gpio_detach(dev);
  174. return (ENOMEM);
  175. }
  176. rid = 1;
  177. sc->in_res = bus_alloc_resource_any(dev,
  178. SYS_RES_MEMORY, &rid, RF_ACTIVE);
  179. if (sc->in_res == NULL) {
  180. device_printf(dev, "Can't allocate memory for device input port");
  181. mpc85xx_gpio_detach(dev);
  182. return (ENOMEM);
  183. }
  184. sc->busdev = gpiobus_attach_bus(dev);
  185. if (sc->busdev == NULL) {
  186. mpc85xx_gpio_detach(dev);
  187. return (ENOMEM);
  188. }
  189. OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
  190. return (0);
  191. }
  192. static int
  193. mpc85xx_gpio_detach(device_t dev)
  194. {
  195. struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
  196. gpiobus_detach_bus(dev);
  197. if (sc->out_res != NULL) {
  198. /* Release output port resource. */
  199. bus_release_resource(dev, SYS_RES_MEMORY,
  200. rman_get_rid(sc->out_res), sc->out_res);
  201. }
  202. if (sc->in_res != NULL) {
  203. /* Release input port resource. */
  204. bus_release_resource(dev, SYS_RES_MEMORY,
  205. rman_get_rid(sc->in_res), sc->in_res);
  206. }
  207. GPIO_LOCK_DESTROY(sc);
  208. return (0);
  209. }
  210. static device_method_t mpc85xx_gpio_methods[] = {
  211. /* device_if */
  212. DEVMETHOD(device_probe, mpc85xx_gpio_probe),
  213. DEVMETHOD(device_attach, mpc85xx_gpio_attach),
  214. DEVMETHOD(device_detach, mpc85xx_gpio_detach),
  215. /* GPIO protocol */
  216. DEVMETHOD(gpio_get_bus, mpc85xx_gpio_get_bus),
  217. DEVMETHOD(gpio_pin_max, mpc85xx_gpio_pin_max),
  218. DEVMETHOD(gpio_pin_getname, mpc85xx_gpio_pin_getname),
  219. DEVMETHOD(gpio_pin_getcaps, mpc85xx_gpio_pin_getcaps),
  220. DEVMETHOD(gpio_pin_get, mpc85xx_gpio_pin_get),
  221. DEVMETHOD(gpio_pin_set, mpc85xx_gpio_pin_set),
  222. DEVMETHOD(gpio_pin_toggle, mpc85xx_gpio_pin_toggle),
  223. DEVMETHOD_END
  224. };
  225. static driver_t mpc85xx_gpio_driver = {
  226. "gpio",
  227. mpc85xx_gpio_methods,
  228. sizeof(struct mpc85xx_gpio_softc),
  229. };
  230. static devclass_t mpc85xx_gpio_devclass;
  231. EARLY_DRIVER_MODULE(mpc85xx_gpio, simplebus, mpc85xx_gpio_driver,
  232. mpc85xx_gpio_devclass, NULL, NULL,
  233. BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);