pmu.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2006 Michael Lorenz
  5. * Copyright 2008 by Nathan Whitehorn
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  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 the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  24. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/cdefs.h>
  31. __FBSDID("$FreeBSD$");
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/module.h>
  35. #include <sys/bus.h>
  36. #include <sys/conf.h>
  37. #include <sys/eventhandler.h>
  38. #include <sys/kernel.h>
  39. #include <sys/kthread.h>
  40. #include <sys/lock.h>
  41. #include <sys/mutex.h>
  42. #include <sys/clock.h>
  43. #include <sys/proc.h>
  44. #include <sys/reboot.h>
  45. #include <sys/sysctl.h>
  46. #include <dev/ofw/ofw_bus.h>
  47. #include <dev/ofw/openfirm.h>
  48. #include <dev/led/led.h>
  49. #include <machine/_inttypes.h>
  50. #include <machine/bus.h>
  51. #include <machine/cpu.h>
  52. #include <machine/hid.h>
  53. #include <machine/intr_machdep.h>
  54. #include <machine/md_var.h>
  55. #include <machine/pcb.h>
  56. #include <machine/pio.h>
  57. #include <machine/resource.h>
  58. #include <vm/vm.h>
  59. #include <vm/pmap.h>
  60. #include <sys/rman.h>
  61. #include <dev/adb/adb.h>
  62. #include "clock_if.h"
  63. #include "pmuvar.h"
  64. #include "viareg.h"
  65. #include "uninorthvar.h" /* For unin_chip_sleep()/unin_chip_wake() */
  66. #define PMU_DEFAULTS PMU_INT_TICK | PMU_INT_ADB | \
  67. PMU_INT_PCEJECT | PMU_INT_SNDBRT | \
  68. PMU_INT_BATTERY | PMU_INT_ENVIRONMENT
  69. /*
  70. * Bus interface
  71. */
  72. static int pmu_probe(device_t);
  73. static int pmu_attach(device_t);
  74. static int pmu_detach(device_t);
  75. /*
  76. * Clock interface
  77. */
  78. static int pmu_gettime(device_t dev, struct timespec *ts);
  79. static int pmu_settime(device_t dev, struct timespec *ts);
  80. /*
  81. * ADB Interface
  82. */
  83. static u_int pmu_adb_send(device_t dev, u_char command_byte, int len,
  84. u_char *data, u_char poll);
  85. static u_int pmu_adb_autopoll(device_t dev, uint16_t mask);
  86. static u_int pmu_poll(device_t dev);
  87. /*
  88. * Power interface
  89. */
  90. static void pmu_shutdown(void *xsc, int howto);
  91. static void pmu_set_sleepled(void *xsc, int onoff);
  92. static int pmu_server_mode(SYSCTL_HANDLER_ARGS);
  93. static int pmu_acline_state(SYSCTL_HANDLER_ARGS);
  94. static int pmu_query_battery(struct pmu_softc *sc, int batt,
  95. struct pmu_battstate *info);
  96. static int pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
  97. static int pmu_battmon(SYSCTL_HANDLER_ARGS);
  98. static void pmu_battquery_proc(void);
  99. static void pmu_battery_notify(struct pmu_battstate *batt,
  100. struct pmu_battstate *old);
  101. /*
  102. * List of battery-related sysctls we might ask for
  103. */
  104. enum {
  105. PMU_BATSYSCTL_PRESENT = 1 << 8,
  106. PMU_BATSYSCTL_CHARGING = 2 << 8,
  107. PMU_BATSYSCTL_CHARGE = 3 << 8,
  108. PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
  109. PMU_BATSYSCTL_CURRENT = 5 << 8,
  110. PMU_BATSYSCTL_VOLTAGE = 6 << 8,
  111. PMU_BATSYSCTL_TIME = 7 << 8,
  112. PMU_BATSYSCTL_LIFE = 8 << 8
  113. };
  114. static device_method_t pmu_methods[] = {
  115. /* Device interface */
  116. DEVMETHOD(device_probe, pmu_probe),
  117. DEVMETHOD(device_attach, pmu_attach),
  118. DEVMETHOD(device_detach, pmu_detach),
  119. DEVMETHOD(device_shutdown, bus_generic_shutdown),
  120. /* ADB bus interface */
  121. DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send),
  122. DEVMETHOD(adb_hb_controller_poll, pmu_poll),
  123. DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
  124. /* Clock interface */
  125. DEVMETHOD(clock_gettime, pmu_gettime),
  126. DEVMETHOD(clock_settime, pmu_settime),
  127. DEVMETHOD_END
  128. };
  129. static driver_t pmu_driver = {
  130. "pmu",
  131. pmu_methods,
  132. sizeof(struct pmu_softc),
  133. };
  134. static devclass_t pmu_devclass;
  135. EARLY_DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0,
  136. BUS_PASS_RESOURCE);
  137. DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
  138. static int pmuextint_probe(device_t);
  139. static int pmuextint_attach(device_t);
  140. static device_method_t pmuextint_methods[] = {
  141. /* Device interface */
  142. DEVMETHOD(device_probe, pmuextint_probe),
  143. DEVMETHOD(device_attach, pmuextint_attach),
  144. {0,0}
  145. };
  146. static driver_t pmuextint_driver = {
  147. "pmuextint",
  148. pmuextint_methods,
  149. 0
  150. };
  151. static devclass_t pmuextint_devclass;
  152. EARLY_DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass,
  153. 0, 0, BUS_PASS_RESOURCE);
  154. /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
  155. MODULE_DEPEND(pmu, usb, 1, 1, 1);
  156. static void pmu_intr(void *arg);
  157. static void pmu_in(struct pmu_softc *sc);
  158. static void pmu_out(struct pmu_softc *sc);
  159. static void pmu_ack_on(struct pmu_softc *sc);
  160. static void pmu_ack_off(struct pmu_softc *sc);
  161. static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
  162. int rlen, uint8_t *out_msg);
  163. static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
  164. static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
  165. static int pmu_intr_state(struct pmu_softc *);
  166. /* these values shows that number of data returned after 'send' cmd is sent */
  167. static signed char pm_send_cmd_type[] = {
  168. -1, -1, -1, -1, -1, -1, -1, -1,
  169. -1, -1, -1, -1, -1, -1, -1, -1,
  170. 0x01, 0x01, -1, -1, -1, -1, -1, -1,
  171. 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
  172. -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
  173. 0x00, -1, -1, -1, -1, -1, -1, -1,
  174. 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
  175. 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
  176. 0x01, 0x01, -1, -1, -1, -1, -1, -1,
  177. 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
  178. 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
  179. 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
  180. 0x02, -1, -1, -1, -1, -1, -1, -1,
  181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
  182. 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
  183. 0x00, 0x00, -1, -1, -1, 0x05, 0x04, 0x04,
  184. 0x04, -1, 0x00, -1, -1, -1, -1, -1,
  185. 0x00, -1, -1, -1, -1, -1, -1, -1,
  186. 0x01, 0x02, -1, -1, -1, -1, -1, -1,
  187. 0x00, 0x00, -1, -1, -1, -1, -1, -1,
  188. 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
  189. 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
  190. -1, -1, -1, -1, -1, -1, -1, -1,
  191. -1, -1, -1, -1, -1, -1, -1, -1,
  192. -1, -1, -1, -1, -1, -1, -1, -1,
  193. -1, -1, -1, -1, -1, -1, -1, -1,
  194. 0x00, -1, -1, -1, -1, -1, -1, -1,
  195. 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
  196. -1, 0x04, 0x00, -1, -1, -1, -1, -1,
  197. 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
  198. -1, -1, -1, -1, -1, -1, -1, -1,
  199. -1, -1, -1, -1, -1, -1, -1, -1
  200. };
  201. /* these values shows that number of data returned after 'receive' cmd is sent */
  202. static signed char pm_receive_cmd_type[] = {
  203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. -1, -1, -1, -1, -1, -1, -1, -1,
  205. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  206. 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. -1, -1, -1, -1, -1, -1, -1, -1,
  209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
  211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x02, 0x02, -1, -1, -1, -1, -1, -1,
  213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  216. 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
  217. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. -1, -1, -1, -1, -1, 0x01, 0x01, 0x01,
  219. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x06, -1, -1, -1, -1, -1, -1, -1,
  221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  222. 0x02, 0x02, -1, -1, -1, -1, -1, -1,
  223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
  225. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. -1, -1, -1, -1, -1, -1, -1, -1,
  227. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  228. -1, -1, -1, -1, -1, -1, -1, -1,
  229. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  230. 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
  231. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. -1, -1, 0x02, -1, -1, -1, -1, 0x00,
  233. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. -1, -1, -1, -1, -1, -1, -1, -1,
  235. };
  236. static int pmu_battmon_enabled = 1;
  237. static struct proc *pmubattproc;
  238. static struct kproc_desc pmu_batt_kp = {
  239. "pmu_batt",
  240. pmu_battquery_proc,
  241. &pmubattproc
  242. };
  243. /* We only have one of each device, so globals are safe */
  244. static device_t pmu = NULL;
  245. static device_t pmu_extint = NULL;
  246. static int
  247. pmuextint_probe(device_t dev)
  248. {
  249. const char *type = ofw_bus_get_type(dev);
  250. if (strcmp(type, "extint-gpio1") != 0)
  251. return (ENXIO);
  252. device_set_desc(dev, "Apple PMU99 External Interrupt");
  253. return (0);
  254. }
  255. static int
  256. pmu_probe(device_t dev)
  257. {
  258. const char *type = ofw_bus_get_type(dev);
  259. if (strcmp(type, "via-pmu") != 0)
  260. return (ENXIO);
  261. device_set_desc(dev, "Apple PMU99 Controller");
  262. return (0);
  263. }
  264. static int
  265. setup_pmu_intr(device_t dev, device_t extint)
  266. {
  267. struct pmu_softc *sc;
  268. sc = device_get_softc(dev);
  269. sc->sc_irqrid = 0;
  270. sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
  271. RF_ACTIVE);
  272. if (sc->sc_irq == NULL) {
  273. device_printf(dev, "could not allocate interrupt\n");
  274. return (ENXIO);
  275. }
  276. if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
  277. | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
  278. device_printf(dev, "could not setup interrupt\n");
  279. bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
  280. sc->sc_irq);
  281. return (ENXIO);
  282. }
  283. return (0);
  284. }
  285. static int
  286. pmuextint_attach(device_t dev)
  287. {
  288. pmu_extint = dev;
  289. if (pmu)
  290. return (setup_pmu_intr(pmu,dev));
  291. return (0);
  292. }
  293. static int
  294. pmu_attach(device_t dev)
  295. {
  296. struct pmu_softc *sc;
  297. int i;
  298. uint8_t reg;
  299. uint8_t cmd[2] = {2, 0};
  300. uint8_t resp[16];
  301. phandle_t node,child;
  302. struct sysctl_ctx_list *ctx;
  303. struct sysctl_oid *tree;
  304. sc = device_get_softc(dev);
  305. sc->sc_dev = dev;
  306. sc->sc_memrid = 0;
  307. sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
  308. &sc->sc_memrid, RF_ACTIVE);
  309. mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
  310. if (sc->sc_memr == NULL) {
  311. device_printf(dev, "Could not alloc mem resource!\n");
  312. return (ENXIO);
  313. }
  314. /*
  315. * Our interrupt is attached to a GPIO pin. Depending on probe order,
  316. * we may not have found it yet. If we haven't, it will find us, and
  317. * attach our interrupt then.
  318. */
  319. pmu = dev;
  320. if (pmu_extint != NULL) {
  321. if (setup_pmu_intr(dev,pmu_extint) != 0)
  322. return (ENXIO);
  323. }
  324. sc->sc_autopoll = 0;
  325. sc->sc_batteries = 0;
  326. sc->adb_bus = NULL;
  327. sc->sc_leddev = NULL;
  328. /* Init PMU */
  329. pmu_write_reg(sc, vBufB, pmu_read_reg(sc, vBufB) | vPB4);
  330. pmu_write_reg(sc, vDirB, (pmu_read_reg(sc, vDirB) | vPB4) & ~vPB3);
  331. reg = PMU_DEFAULTS;
  332. pmu_send(sc, PMU_SET_IMASK, 1, &reg, 16, resp);
  333. pmu_write_reg(sc, vIER, 0x94); /* make sure VIA interrupts are on */
  334. pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
  335. pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp);
  336. /* Initialize child buses (ADB) */
  337. node = ofw_bus_get_node(dev);
  338. for (child = OF_child(node); child != 0; child = OF_peer(child)) {
  339. char name[32];
  340. memset(name, 0, sizeof(name));
  341. OF_getprop(child, "name", name, sizeof(name));
  342. if (bootverbose)
  343. device_printf(dev, "PMU child <%s>\n",name);
  344. if (strncmp(name, "adb", 4) == 0) {
  345. sc->adb_bus = device_add_child(dev,"adb",-1);
  346. }
  347. if (strncmp(name, "power-mgt", 9) == 0) {
  348. uint32_t prim_info[9];
  349. if (OF_getprop(child, "prim-info", prim_info,
  350. sizeof(prim_info)) >= 7)
  351. sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
  352. if (bootverbose && sc->sc_batteries > 0)
  353. device_printf(dev, "%d batteries detected\n",
  354. sc->sc_batteries);
  355. }
  356. }
  357. /*
  358. * Set up sysctls
  359. */
  360. ctx = device_get_sysctl_ctx(dev);
  361. tree = device_get_sysctl_tree(dev);
  362. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  363. "server_mode", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
  364. pmu_server_mode, "I", "Enable reboot after power failure");
  365. if (sc->sc_batteries > 0) {
  366. struct sysctl_oid *oid, *battroot;
  367. char battnum[2];
  368. /* Only start the battery monitor if we have a battery. */
  369. kproc_start(&pmu_batt_kp);
  370. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  371. "monitor_batteries",
  372. CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
  373. pmu_battmon, "I", "Post battery events to devd");
  374. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  375. "acline", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  376. 0, pmu_acline_state, "I", "AC Line Status");
  377. battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  378. "batteries", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  379. "Battery Information");
  380. for (i = 0; i < sc->sc_batteries; i++) {
  381. battnum[0] = i + '0';
  382. battnum[1] = '\0';
  383. oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
  384. OID_AUTO, battnum, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  385. "Battery Information");
  386. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  387. "present",
  388. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  389. PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
  390. "I", "Battery present");
  391. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  392. "charging",
  393. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  394. PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
  395. "I", "Battery charging");
  396. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  397. "charge",
  398. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  399. PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
  400. "I", "Battery charge (mAh)");
  401. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  402. "maxcharge",
  403. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  404. PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
  405. "I", "Maximum battery capacity (mAh)");
  406. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  407. "rate",
  408. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  409. PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
  410. "I", "Battery discharge rate (mA)");
  411. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  412. "voltage",
  413. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  414. PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
  415. "I", "Battery voltage (mV)");
  416. /* Knobs for mental compatibility with ACPI */
  417. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  418. "time",
  419. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  420. PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
  421. "I", "Time Remaining (minutes)");
  422. SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  423. "life",
  424. CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
  425. PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
  426. "I", "Capacity remaining (percent)");
  427. }
  428. }
  429. /*
  430. * Set up LED interface
  431. */
  432. sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
  433. /*
  434. * Register RTC
  435. */
  436. clock_register(dev, 1000);
  437. /*
  438. * Register power control handler
  439. */
  440. EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc,
  441. SHUTDOWN_PRI_LAST);
  442. return (bus_generic_attach(dev));
  443. }
  444. static int
  445. pmu_detach(device_t dev)
  446. {
  447. struct pmu_softc *sc;
  448. sc = device_get_softc(dev);
  449. if (sc->sc_leddev != NULL)
  450. led_destroy(sc->sc_leddev);
  451. bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
  452. bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
  453. bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
  454. mtx_destroy(&sc->sc_mutex);
  455. return (bus_generic_detach(dev));
  456. }
  457. static uint8_t
  458. pmu_read_reg(struct pmu_softc *sc, u_int offset)
  459. {
  460. return (bus_read_1(sc->sc_memr, offset));
  461. }
  462. static void
  463. pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
  464. {
  465. bus_write_1(sc->sc_memr, offset, value);
  466. }
  467. static int
  468. pmu_send_byte(struct pmu_softc *sc, uint8_t data)
  469. {
  470. pmu_out(sc);
  471. pmu_write_reg(sc, vSR, data);
  472. pmu_ack_off(sc);
  473. /* wait for intr to come up */
  474. /* XXX should add a timeout and bail if it expires */
  475. do {} while (pmu_intr_state(sc) == 0);
  476. pmu_ack_on(sc);
  477. do {} while (pmu_intr_state(sc));
  478. pmu_ack_on(sc);
  479. return 0;
  480. }
  481. static inline int
  482. pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
  483. {
  484. volatile uint8_t scratch;
  485. pmu_in(sc);
  486. scratch = pmu_read_reg(sc, vSR);
  487. pmu_ack_off(sc);
  488. /* wait for intr to come up */
  489. do {} while (pmu_intr_state(sc) == 0);
  490. pmu_ack_on(sc);
  491. do {} while (pmu_intr_state(sc));
  492. *data = pmu_read_reg(sc, vSR);
  493. return 0;
  494. }
  495. static int
  496. pmu_intr_state(struct pmu_softc *sc)
  497. {
  498. return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
  499. }
  500. static int
  501. pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
  502. uint8_t *out_msg)
  503. {
  504. struct pmu_softc *sc = cookie;
  505. int i, rcv_len = -1;
  506. uint8_t out_len, intreg;
  507. intreg = pmu_read_reg(sc, vIER);
  508. intreg &= 0x10;
  509. pmu_write_reg(sc, vIER, intreg);
  510. /* wait idle */
  511. do {} while (pmu_intr_state(sc));
  512. /* send command */
  513. pmu_send_byte(sc, cmd);
  514. /* send length if necessary */
  515. if (pm_send_cmd_type[cmd] < 0) {
  516. pmu_send_byte(sc, length);
  517. }
  518. for (i = 0; i < length; i++) {
  519. pmu_send_byte(sc, in_msg[i]);
  520. }
  521. /* see if there's data to read */
  522. rcv_len = pm_receive_cmd_type[cmd];
  523. if (rcv_len == 0)
  524. goto done;
  525. /* read command */
  526. if (rcv_len == 1) {
  527. pmu_read_byte(sc, out_msg);
  528. goto done;
  529. } else
  530. out_msg[0] = cmd;
  531. if (rcv_len < 0) {
  532. pmu_read_byte(sc, &out_len);
  533. rcv_len = out_len + 1;
  534. }
  535. for (i = 1; i < min(rcv_len, rlen); i++)
  536. pmu_read_byte(sc, &out_msg[i]);
  537. done:
  538. pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
  539. return rcv_len;
  540. }
  541. static u_int
  542. pmu_poll(device_t dev)
  543. {
  544. pmu_intr(dev);
  545. return (0);
  546. }
  547. static void
  548. pmu_in(struct pmu_softc *sc)
  549. {
  550. uint8_t reg;
  551. reg = pmu_read_reg(sc, vACR);
  552. reg &= ~vSR_OUT;
  553. reg |= 0x0c;
  554. pmu_write_reg(sc, vACR, reg);
  555. }
  556. static void
  557. pmu_out(struct pmu_softc *sc)
  558. {
  559. uint8_t reg;
  560. reg = pmu_read_reg(sc, vACR);
  561. reg |= vSR_OUT;
  562. reg |= 0x0c;
  563. pmu_write_reg(sc, vACR, reg);
  564. }
  565. static void
  566. pmu_ack_off(struct pmu_softc *sc)
  567. {
  568. uint8_t reg;
  569. reg = pmu_read_reg(sc, vBufB);
  570. reg &= ~vPB4;
  571. pmu_write_reg(sc, vBufB, reg);
  572. }
  573. static void
  574. pmu_ack_on(struct pmu_softc *sc)
  575. {
  576. uint8_t reg;
  577. reg = pmu_read_reg(sc, vBufB);
  578. reg |= vPB4;
  579. pmu_write_reg(sc, vBufB, reg);
  580. }
  581. static void
  582. pmu_intr(void *arg)
  583. {
  584. device_t dev;
  585. struct pmu_softc *sc;
  586. unsigned int len;
  587. uint8_t resp[16];
  588. uint8_t junk[16];
  589. dev = (device_t)arg;
  590. sc = device_get_softc(dev);
  591. mtx_lock(&sc->sc_mutex);
  592. pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
  593. len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
  594. mtx_unlock(&sc->sc_mutex);
  595. if ((len < 1) || (resp[1] == 0)) {
  596. return;
  597. }
  598. if (resp[1] & PMU_INT_ADB) {
  599. /*
  600. * the PMU will turn off autopolling after each command that
  601. * it did not issue, so we assume any but TALK R0 is ours and
  602. * re-enable autopoll here whenever we receive an ACK for a
  603. * non TR0 command.
  604. */
  605. mtx_lock(&sc->sc_mutex);
  606. if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
  607. if (sc->sc_autopoll) {
  608. uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
  609. (sc->sc_autopoll >> 8) & 0xff,
  610. sc->sc_autopoll & 0xff};
  611. pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
  612. }
  613. }
  614. mtx_unlock(&sc->sc_mutex);
  615. adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
  616. len - 3,&resp[3]);
  617. }
  618. if (resp[1] & PMU_INT_ENVIRONMENT) {
  619. /* if the lid was just closed, notify devd. */
  620. if ((resp[2] & PMU_ENV_LID_CLOSED) && (!sc->lid_closed)) {
  621. sc->lid_closed = 1;
  622. devctl_notify("PMU", "lid", "close", NULL);
  623. }
  624. else if (!(resp[2] & PMU_ENV_LID_CLOSED) && (sc->lid_closed)) {
  625. /* if the lid was just opened, notify devd. */
  626. sc->lid_closed = 0;
  627. devctl_notify("PMU", "lid", "open", NULL);
  628. }
  629. if (resp[2] & PMU_ENV_POWER)
  630. devctl_notify("PMU", "Button", "pressed", NULL);
  631. }
  632. }
  633. static u_int
  634. pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
  635. u_char poll)
  636. {
  637. struct pmu_softc *sc = device_get_softc(dev);
  638. int i,replen;
  639. uint8_t packet[16], resp[16];
  640. /* construct an ADB command packet and send it */
  641. packet[0] = command_byte;
  642. packet[1] = 0;
  643. packet[2] = len;
  644. for (i = 0; i < len; i++)
  645. packet[i + 3] = data[i];
  646. mtx_lock(&sc->sc_mutex);
  647. replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
  648. mtx_unlock(&sc->sc_mutex);
  649. if (poll)
  650. pmu_poll(dev);
  651. return 0;
  652. }
  653. static u_int
  654. pmu_adb_autopoll(device_t dev, uint16_t mask)
  655. {
  656. struct pmu_softc *sc = device_get_softc(dev);
  657. /* magical incantation to re-enable autopolling */
  658. uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
  659. uint8_t resp[16];
  660. mtx_lock(&sc->sc_mutex);
  661. if (sc->sc_autopoll == mask) {
  662. mtx_unlock(&sc->sc_mutex);
  663. return 0;
  664. }
  665. sc->sc_autopoll = mask & 0xffff;
  666. if (mask)
  667. pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
  668. else
  669. pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
  670. mtx_unlock(&sc->sc_mutex);
  671. return 0;
  672. }
  673. static void
  674. pmu_shutdown(void *xsc, int howto)
  675. {
  676. struct pmu_softc *sc = xsc;
  677. uint8_t cmd[] = {'M', 'A', 'T', 'T'};
  678. if (howto & RB_HALT)
  679. pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL);
  680. else
  681. pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL);
  682. for (;;);
  683. }
  684. static void
  685. pmu_set_sleepled(void *xsc, int onoff)
  686. {
  687. struct pmu_softc *sc = xsc;
  688. uint8_t cmd[] = {4, 0, 0};
  689. cmd[2] = onoff;
  690. mtx_lock(&sc->sc_mutex);
  691. pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
  692. mtx_unlock(&sc->sc_mutex);
  693. }
  694. static int
  695. pmu_server_mode(SYSCTL_HANDLER_ARGS)
  696. {
  697. struct pmu_softc *sc = arg1;
  698. u_int server_mode = 0;
  699. uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
  700. uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
  701. uint8_t resp[3];
  702. int error, len;
  703. mtx_lock(&sc->sc_mutex);
  704. len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
  705. mtx_unlock(&sc->sc_mutex);
  706. if (len == 3)
  707. server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
  708. error = sysctl_handle_int(oidp, &server_mode, 0, req);
  709. if (len != 3)
  710. return (EINVAL);
  711. if (error || !req->newptr)
  712. return (error);
  713. if (server_mode == 1)
  714. setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
  715. else if (server_mode == 0)
  716. setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
  717. else
  718. return (EINVAL);
  719. setcmd[1] = resp[1];
  720. mtx_lock(&sc->sc_mutex);
  721. pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
  722. mtx_unlock(&sc->sc_mutex);
  723. return (0);
  724. }
  725. static int
  726. pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
  727. {
  728. uint8_t reg;
  729. uint8_t resp[16];
  730. int len;
  731. reg = batt + 1;
  732. mtx_lock(&sc->sc_mutex);
  733. len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, &reg, 16, resp);
  734. mtx_unlock(&sc->sc_mutex);
  735. if (len < 3)
  736. return (-1);
  737. /* All PMU battery info replies share a common header:
  738. * Byte 1 Payload Format
  739. * Byte 2 Battery Flags
  740. */
  741. info->state = resp[2];
  742. switch (resp[1]) {
  743. case 3:
  744. case 4:
  745. /*
  746. * Formats 3 and 4 appear to be the same:
  747. * Byte 3 Charge
  748. * Byte 4 Max Charge
  749. * Byte 5 Current
  750. * Byte 6 Voltage
  751. */
  752. info->charge = resp[3];
  753. info->maxcharge = resp[4];
  754. /* Current can be positive or negative */
  755. info->current = (int8_t)resp[5];
  756. info->voltage = resp[6];
  757. break;
  758. case 5:
  759. /*
  760. * Formats 5 is a wider version of formats 3 and 4
  761. * Byte 3-4 Charge
  762. * Byte 5-6 Max Charge
  763. * Byte 7-8 Current
  764. * Byte 9-10 Voltage
  765. */
  766. info->charge = (resp[3] << 8) | resp[4];
  767. info->maxcharge = (resp[5] << 8) | resp[6];
  768. /* Current can be positive or negative */
  769. info->current = (int16_t)((resp[7] << 8) | resp[8]);
  770. info->voltage = (resp[9] << 8) | resp[10];
  771. break;
  772. default:
  773. device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
  774. resp[1]);
  775. return (-1);
  776. }
  777. return (0);
  778. }
  779. static void
  780. pmu_battery_notify(struct pmu_battstate *batt, struct pmu_battstate *old)
  781. {
  782. char notify_buf[16];
  783. int new_acline, old_acline;
  784. new_acline = (batt->state & PMU_PWR_AC_PRESENT) ? 1 : 0;
  785. old_acline = (old->state & PMU_PWR_AC_PRESENT) ? 1 : 0;
  786. if (new_acline != old_acline) {
  787. snprintf(notify_buf, sizeof(notify_buf),
  788. "notify=0x%02x", new_acline);
  789. devctl_notify("PMU", "POWER", "ACLINE", notify_buf);
  790. }
  791. }
  792. static void
  793. pmu_battquery_proc()
  794. {
  795. struct pmu_softc *sc;
  796. struct pmu_battstate batt;
  797. struct pmu_battstate cur_batt;
  798. int error;
  799. sc = device_get_softc(pmu);
  800. bzero(&cur_batt, sizeof(cur_batt));
  801. while (1) {
  802. kproc_suspend_check(curproc);
  803. error = pmu_query_battery(sc, 0, &batt);
  804. pmu_battery_notify(&batt, &cur_batt);
  805. cur_batt = batt;
  806. pause("pmu_batt", hz);
  807. }
  808. }
  809. static int
  810. pmu_battmon(SYSCTL_HANDLER_ARGS)
  811. {
  812. struct pmu_softc *sc;
  813. int error, result;
  814. sc = arg1;
  815. result = pmu_battmon_enabled;
  816. error = sysctl_handle_int(oidp, &result, 0, req);
  817. if (error || !req->newptr)
  818. return (error);
  819. if (!result && pmu_battmon_enabled)
  820. error = kproc_suspend(pmubattproc, hz);
  821. else if (result && pmu_battmon_enabled == 0)
  822. error = kproc_resume(pmubattproc);
  823. pmu_battmon_enabled = (result != 0);
  824. return (error);
  825. }
  826. static int
  827. pmu_acline_state(SYSCTL_HANDLER_ARGS)
  828. {
  829. struct pmu_softc *sc;
  830. struct pmu_battstate batt;
  831. int error, result;
  832. sc = arg1;
  833. /* The PMU treats the AC line status as a property of the battery */
  834. error = pmu_query_battery(sc, 0, &batt);
  835. if (error != 0)
  836. return (error);
  837. result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
  838. error = sysctl_handle_int(oidp, &result, 0, req);
  839. return (error);
  840. }
  841. static int
  842. pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
  843. {
  844. struct pmu_softc *sc;
  845. struct pmu_battstate batt;
  846. int error, result;
  847. sc = arg1;
  848. error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
  849. if (error != 0)
  850. return (error);
  851. switch (arg2 & 0xff00) {
  852. case PMU_BATSYSCTL_PRESENT:
  853. result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
  854. break;
  855. case PMU_BATSYSCTL_CHARGING:
  856. result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
  857. break;
  858. case PMU_BATSYSCTL_CHARGE:
  859. result = batt.charge;
  860. break;
  861. case PMU_BATSYSCTL_MAXCHARGE:
  862. result = batt.maxcharge;
  863. break;
  864. case PMU_BATSYSCTL_CURRENT:
  865. result = batt.current;
  866. break;
  867. case PMU_BATSYSCTL_VOLTAGE:
  868. result = batt.voltage;
  869. break;
  870. case PMU_BATSYSCTL_TIME:
  871. /* Time remaining until full charge/discharge, in minutes */
  872. if (batt.current >= 0)
  873. result = (batt.maxcharge - batt.charge) /* mAh */ * 60
  874. / batt.current /* mA */;
  875. else
  876. result = (batt.charge /* mAh */ * 60)
  877. / (-batt.current /* mA */);
  878. break;
  879. case PMU_BATSYSCTL_LIFE:
  880. /* Battery charge fraction, in percent */
  881. result = (batt.charge * 100) / batt.maxcharge;
  882. break;
  883. default:
  884. /* This should never happen */
  885. result = -1;
  886. }
  887. error = sysctl_handle_int(oidp, &result, 0, req);
  888. return (error);
  889. }
  890. #define DIFF19041970 2082844800
  891. static int
  892. pmu_gettime(device_t dev, struct timespec *ts)
  893. {
  894. struct pmu_softc *sc = device_get_softc(dev);
  895. uint8_t resp[16];
  896. uint32_t sec;
  897. mtx_lock(&sc->sc_mutex);
  898. pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
  899. mtx_unlock(&sc->sc_mutex);
  900. memcpy(&sec, &resp[1], 4);
  901. ts->tv_sec = sec - DIFF19041970;
  902. ts->tv_nsec = 0;
  903. return (0);
  904. }
  905. static int
  906. pmu_settime(device_t dev, struct timespec *ts)
  907. {
  908. struct pmu_softc *sc = device_get_softc(dev);
  909. uint32_t sec;
  910. sec = ts->tv_sec + DIFF19041970;
  911. mtx_lock(&sc->sc_mutex);
  912. pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL);
  913. mtx_unlock(&sc->sc_mutex);
  914. return (0);
  915. }
  916. int
  917. pmu_set_speed(int low_speed)
  918. {
  919. struct pmu_softc *sc;
  920. uint8_t sleepcmd[] = {'W', 'O', 'O', 'F', 0};
  921. uint8_t resp[16];
  922. sc = device_get_softc(pmu);
  923. pmu_write_reg(sc, vIER, 0x10);
  924. spinlock_enter();
  925. mtdec(0x7fffffff);
  926. mb();
  927. mtdec(0x7fffffff);
  928. sleepcmd[4] = low_speed;
  929. pmu_send(sc, PMU_CPU_SPEED, 5, sleepcmd, 16, resp);
  930. unin_chip_sleep(NULL, 1);
  931. platform_sleep();
  932. unin_chip_wake(NULL);
  933. mtdec(1); /* Force a decrementer exception */
  934. spinlock_exit();
  935. pmu_write_reg(sc, vIER, 0x90);
  936. return (0);
  937. }