power.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* $OpenBSD: power.c,v 1.15 2014/07/11 08:18:30 guenther Exp $ */
  2. /*
  3. * Copyright (c) 2007 Jasper Lievisse Adriaanse <jasper@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/systm.h>
  19. #include <sys/device.h>
  20. #include <sys/proc.h>
  21. #include <sys/signalvar.h>
  22. #include <machine/autoconf.h>
  23. #include <mips64/archtype.h>
  24. #include <dev/ic/ds1687reg.h>
  25. #include <sgi/dev/dsrtcvar.h>
  26. #include "power.h"
  27. /*
  28. * Power button driver for the SGI O2 and Octane.
  29. */
  30. int power_intr(void *);
  31. struct cfdriver power_cd = {
  32. NULL, "power", DV_DULL
  33. };
  34. #if NPOWER_MACEBUS > 0
  35. #include <sgi/localbus/macebusvar.h>
  36. void power_macebus_attach(struct device *, struct device *, void *);
  37. int power_macebus_match(struct device *, void *, void *);
  38. struct cfattach power_macebus_ca = {
  39. sizeof(struct device), power_macebus_match, power_macebus_attach
  40. };
  41. int
  42. power_macebus_match(struct device *parent, void *match, void *aux)
  43. {
  44. return (1);
  45. }
  46. void
  47. power_macebus_attach(struct device *parent, struct device *self, void *aux)
  48. {
  49. struct macebus_attach_args *maa = aux;
  50. /* Establish interrupt handler. */
  51. if (macebus_intr_establish(maa->maa_intr, maa->maa_mace_intr,
  52. IST_EDGE, IPL_TTY, power_intr, self, self->dv_xname))
  53. printf("\n");
  54. else
  55. printf(": unable to establish interrupt!\n");
  56. }
  57. #endif
  58. #if NPOWER_MAINBUS > 0
  59. #include <sgi/xbow/xbow.h>
  60. #include <sgi/xbow/xheartreg.h>
  61. void power_mainbus_attach(struct device *, struct device *, void *);
  62. int power_mainbus_match(struct device *, void *, void *);
  63. int power_mainbus_intr(void *);
  64. struct cfattach power_mainbus_ca = {
  65. sizeof(struct device), power_mainbus_match, power_mainbus_attach
  66. };
  67. int
  68. power_mainbus_match(struct device *parent, void *match, void *aux)
  69. {
  70. struct mainbus_attach_args *maa = aux;
  71. if (strcmp(maa->maa_name, power_cd.cd_name) != 0)
  72. return 0;
  73. return sys_config.system_type == SGI_OCTANE ? 1 : 0;
  74. }
  75. void
  76. power_mainbus_attach(struct device *parent, struct device *self, void *aux)
  77. {
  78. /* Establish interrupt handler. */
  79. if (xbow_intr_establish(power_mainbus_intr, self, HEART_ISR_POWER,
  80. IPL_TTY, self->dv_xname, NULL) != 0) {
  81. printf(": unable to establish interrupt!\n");
  82. return;
  83. }
  84. printf("\n");
  85. }
  86. int
  87. power_mainbus_intr(void *v)
  88. {
  89. /*
  90. * Clear interrupt condition; debouncing the kickstart bit will not
  91. * suffice.
  92. */
  93. xbow_intr_clear(HEART_ISR_POWER);
  94. return power_intr(v);
  95. }
  96. #endif
  97. int
  98. power_intr(void *unused)
  99. {
  100. extern int allowpowerdown;
  101. int val;
  102. /*
  103. * Prevent further interrupts by clearing the kickstart flag
  104. * in the DS1687's extended control register.
  105. */
  106. val = dsrtc_register_read(DS1687_EXT_CTRL);
  107. if (val == -1)
  108. return 1; /* no rtc attached */
  109. /* debounce condition */
  110. dsrtc_register_write(DS1687_EXT_CTRL, val & ~DS1687_KICKSTART);
  111. if (allowpowerdown == 1) {
  112. allowpowerdown = 0;
  113. prsignal(initprocess, SIGUSR2);
  114. }
  115. return 1;
  116. }