pswitch.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (C) 2002 Benno Rice.
  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 Benno Rice ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #include "opt_ddb.h"
  30. #include <sys/param.h>
  31. #include <sys/systm.h>
  32. #include <sys/kdb.h>
  33. #include <sys/kernel.h>
  34. #include <sys/module.h>
  35. #include <sys/malloc.h>
  36. #include <sys/bus.h>
  37. #include <machine/bus.h>
  38. #include <sys/rman.h>
  39. #include <machine/resource.h>
  40. #include <dev/ofw/openfirm.h>
  41. #include <powerpc/powermac/maciovar.h>
  42. struct pswitch_softc {
  43. int sc_irqrid;
  44. struct resource *sc_irq;
  45. void *sc_ih;
  46. };
  47. static int pswitch_probe(device_t);
  48. static int pswitch_attach(device_t);
  49. static int pswitch_intr(void *);
  50. static device_method_t pswitch_methods[] = {
  51. /* Device interface */
  52. DEVMETHOD(device_probe, pswitch_probe),
  53. DEVMETHOD(device_attach, pswitch_attach),
  54. { 0, 0 }
  55. };
  56. static driver_t pswitch_driver = {
  57. "pswitch",
  58. pswitch_methods,
  59. sizeof(struct pswitch_softc)
  60. };
  61. static devclass_t pswitch_devclass;
  62. DRIVER_MODULE(pswitch, macio, pswitch_driver, pswitch_devclass, 0, 0);
  63. static int
  64. pswitch_probe(device_t dev)
  65. {
  66. char *type = macio_get_devtype(dev);
  67. if (strcmp(type, "gpio") != 0)
  68. return (ENXIO);
  69. device_set_desc(dev, "GPIO Programmer's Switch");
  70. return (0);
  71. }
  72. static int
  73. pswitch_attach(device_t dev)
  74. {
  75. struct pswitch_softc *sc;
  76. phandle_t node, child;
  77. char type[32];
  78. u_int irq[2];
  79. sc = device_get_softc(dev);
  80. node = macio_get_node(dev);
  81. for (child = OF_child(node); child != 0; child = OF_peer(child)) {
  82. if (OF_getprop(child, "device_type", type, 32) == -1)
  83. continue;
  84. if (strcmp(type, "programmer-switch") == 0)
  85. break;
  86. }
  87. if (child == 0) {
  88. device_printf(dev, "could not find correct node\n");
  89. return (ENXIO);
  90. }
  91. if (OF_getprop(child, "interrupts", irq, sizeof(irq)) == -1) {
  92. device_printf(dev, "could not get interrupt\n");
  93. return (ENXIO);
  94. }
  95. sc->sc_irqrid = 0;
  96. sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid,
  97. irq[0], irq[0], 1, RF_ACTIVE);
  98. if (sc->sc_irq == NULL) {
  99. device_printf(dev, "could not allocate interrupt\n");
  100. return (ENXIO);
  101. }
  102. if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
  103. pswitch_intr, NULL, dev, &sc->sc_ih) != 0) {
  104. device_printf(dev, "could not setup interrupt\n");
  105. bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
  106. sc->sc_irq);
  107. return (ENXIO);
  108. }
  109. return (0);
  110. }
  111. static int
  112. pswitch_intr(void *arg)
  113. {
  114. device_t dev;
  115. dev = (device_t)arg;
  116. kdb_enter(KDB_WHY_POWERPC, device_get_nameunit(dev));
  117. return (FILTER_HANDLED);
  118. }