1 /*-
2  * Copyright (c) 2019 Justin Hibbits
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/bus.h>
31 #include <sys/malloc.h>
32 #include <sys/smp.h>
33 
34 #include <machine/intr_machdep.h>
35 #include <machine/platform.h>
36 #include <machine/platformvar.h>
37 
38 #include <dev/ofw/openfirm.h>
39 #include <dev/ofw/ofw_bus.h>
40 
41 #include <powerpc/mpc85xx/mpc85xx.h>
42 
43 #include "platform_if.h"
44 
45 static void aeon_pbutton_intr(void *_unused);
46 static void aeon_setup_intr(void *unused);
47 
48 static int aeon_probe(platform_t);
49 static int aeon_attach(platform_t);
50 
51 static platform_method_t aeon_methods[] = {
52 	PLATFORMMETHOD(platform_probe,		aeon_probe),
53 	PLATFORMMETHOD(platform_attach,		aeon_attach),
54 	PLATFORMMETHOD_END
55 };
56 
57 DEFINE_CLASS_1(aeon, aeon_platform, aeon_methods, 0, mpc85xx_platform);
58 
59 PLATFORM_DEF(aeon_platform);
60 
61 static bool is_aeon;
62 
63 static int
64 aeon_probe(platform_t plat)
65 {
66 	phandle_t rootnode;
67 	char model[32];
68 
69 	rootnode = OF_finddevice("/");
70 
71 	if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
72 		if (strncmp(model, "varisys,", strlen("varisys,")) == 0)
73 			return (BUS_PROBE_SPECIFIC);
74 	}
75 
76 	return (ENXIO);
77 }
78 
79 static int
80 aeon_attach(platform_t plat)
81 {
82 	int error;
83 
84 	error = mpc85xx_attach(plat);
85 	if (error)
86 		return (error);
87 
88 	is_aeon = true;
89 
90 	return (0);
91 }
92 
93 /* Notify devd(8) that the power button was pressed (IRQ#4 on A-Eon machines). */
94 static void
95 aeon_pbutton_intr(void *_unused)
96 {
97 	devctl_notify("AEON", "power", "press", NULL);
98 }
99 
100 /* Manually configure the power button IRQ handler. */
101 static void
102 aeon_setup_intr(void *unused)
103 {
104 	int irq;
105 
106 	if (!is_aeon)
107 		return;
108 
109 	if (bootverbose)
110 		printf("Configuring AmigaOne power button.\n");
111 
112 	irq = 4; /* From TRM, IRQ4 is raised when power button is pressed. */
113 
114 	/* Get us the root PIC. */
115 	irq = MAP_IRQ(0, irq);
116 	powerpc_config_intr(irq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
117 	powerpc_setup_intr("power_button", irq, NULL, aeon_pbutton_intr, NULL,
118 	    INTR_TYPE_MISC, NULL, 0);
119 }
120 
121 SYSINIT(aeon_setup_intr, SI_SUB_CONFIGURE, SI_ORDER_ANY, aeon_setup_intr, NULL);
122