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 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/smp.h>
35 
36 #include <machine/intr_machdep.h>
37 #include <machine/platform.h>
38 #include <machine/platformvar.h>
39 
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_bus.h>
42 
43 #include <powerpc/mpc85xx/mpc85xx.h>
44 
45 #include "platform_if.h"
46 
47 static void aeon_pbutton_intr(void *_unused);
48 static void aeon_setup_intr(void *unused);
49 
50 static int aeon_probe(platform_t);
51 static int aeon_attach(platform_t);
52 
53 static platform_method_t aeon_methods[] = {
54 	PLATFORMMETHOD(platform_probe,		aeon_probe),
55 	PLATFORMMETHOD(platform_attach,		aeon_attach),
56 	PLATFORMMETHOD_END
57 };
58 
59 DEFINE_CLASS_1(aeon, aeon_platform, aeon_methods, 0, mpc85xx_platform);
60 
61 PLATFORM_DEF(aeon_platform);
62 
63 static bool is_aeon;
64 
65 static int
66 aeon_probe(platform_t plat)
67 {
68 	phandle_t rootnode;
69 	char model[32];
70 
71 	rootnode = OF_finddevice("/");
72 
73 	if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
74 		if (strncmp(model, "varisys,", strlen("varisys,")) == 0)
75 			return (BUS_PROBE_SPECIFIC);
76 	}
77 
78 	return (ENXIO);
79 }
80 
81 static int
82 aeon_attach(platform_t plat)
83 {
84 	int error;
85 
86 	error = mpc85xx_attach(plat);
87 	if (error)
88 		return (error);
89 
90 	is_aeon = true;
91 
92 	return (0);
93 }
94 
95 /* Notify devd(8) that the power button was pressed (IRQ#4 on A-Eon machines). */
96 static void
97 aeon_pbutton_intr(void *_unused)
98 {
99 	devctl_notify("AEON", "power", "press", NULL);
100 }
101 
102 /* Manually configure the power button IRQ handler. */
103 static void
104 aeon_setup_intr(void *unused)
105 {
106 	int irq;
107 
108 	if (!is_aeon)
109 		return;
110 
111 	if (bootverbose)
112 		printf("Configuring AmigaOne power button.\n");
113 
114 	irq = 4; /* From TRM, IRQ4 is raised when power button is pressed. */
115 
116 	/* Get us the root PIC. */
117 	irq = MAP_IRQ(0, irq);
118 	powerpc_config_intr(irq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
119 	powerpc_setup_intr("power_button", irq, NULL, aeon_pbutton_intr, NULL,
120 	    INTR_TYPE_MISC, NULL, 0);
121 }
122 
123 SYSINIT(aeon_setup_intr, SI_SUB_CONFIGURE, SI_ORDER_ANY, aeon_setup_intr, NULL);
124