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