1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _I8042_SPARCIO_H
3 #define _I8042_SPARCIO_H
4 
5 #include <linux/of_device.h>
6 #include <linux/types.h>
7 
8 #include <asm/io.h>
9 #include <asm/oplib.h>
10 #include <asm/prom.h>
11 
12 static int i8042_kbd_irq = -1;
13 static int i8042_aux_irq = -1;
14 #define I8042_KBD_IRQ i8042_kbd_irq
15 #define I8042_AUX_IRQ i8042_aux_irq
16 
17 #define I8042_KBD_PHYS_DESC "sparcps2/serio0"
18 #define I8042_AUX_PHYS_DESC "sparcps2/serio1"
19 #define I8042_MUX_PHYS_DESC "sparcps2/serio%d"
20 
21 static void __iomem *kbd_iobase;
22 
23 #define I8042_COMMAND_REG	(kbd_iobase + 0x64UL)
24 #define I8042_DATA_REG		(kbd_iobase + 0x60UL)
25 
26 static inline int i8042_read_data(void)
27 {
28 	return readb(kbd_iobase + 0x60UL);
29 }
30 
31 static inline int i8042_read_status(void)
32 {
33 	return readb(kbd_iobase + 0x64UL);
34 }
35 
36 static inline void i8042_write_data(int val)
37 {
38 	writeb(val, kbd_iobase + 0x60UL);
39 }
40 
41 static inline void i8042_write_command(int val)
42 {
43 	writeb(val, kbd_iobase + 0x64UL);
44 }
45 
46 #ifdef CONFIG_PCI
47 
48 static struct resource *kbd_res;
49 
50 #define OBP_PS2KBD_NAME1	"kb_ps2"
51 #define OBP_PS2KBD_NAME2	"keyboard"
52 #define OBP_PS2MS_NAME1		"kdmouse"
53 #define OBP_PS2MS_NAME2		"mouse"
54 
55 static int sparc_i8042_probe(struct platform_device *op)
56 {
57 	struct device_node *dp;
58 
59 	for_each_child_of_node(op->dev.of_node, dp) {
60 		if (of_node_name_eq(dp, OBP_PS2KBD_NAME1) ||
61 		    of_node_name_eq(dp, OBP_PS2KBD_NAME2)) {
62 			struct platform_device *kbd = of_find_device_by_node(dp);
63 			unsigned int irq = kbd->archdata.irqs[0];
64 			if (irq == 0xffffffff)
65 				irq = op->archdata.irqs[0];
66 			i8042_kbd_irq = irq;
67 			kbd_iobase = of_ioremap(&kbd->resource[0],
68 						0, 8, "kbd");
69 			kbd_res = &kbd->resource[0];
70 		} else if (of_node_name_eq(dp, OBP_PS2MS_NAME1) ||
71 			   of_node_name_eq(dp, OBP_PS2MS_NAME2)) {
72 			struct platform_device *ms = of_find_device_by_node(dp);
73 			unsigned int irq = ms->archdata.irqs[0];
74 			if (irq == 0xffffffff)
75 				irq = op->archdata.irqs[0];
76 			i8042_aux_irq = irq;
77 		}
78 	}
79 
80 	return 0;
81 }
82 
83 static int sparc_i8042_remove(struct platform_device *op)
84 {
85 	of_iounmap(kbd_res, kbd_iobase, 8);
86 
87 	return 0;
88 }
89 
90 static const struct of_device_id sparc_i8042_match[] = {
91 	{
92 		.name = "8042",
93 	},
94 	{},
95 };
96 MODULE_DEVICE_TABLE(of, sparc_i8042_match);
97 
98 static struct platform_driver sparc_i8042_driver = {
99 	.driver = {
100 		.name = "i8042",
101 		.of_match_table = sparc_i8042_match,
102 	},
103 	.probe		= sparc_i8042_probe,
104 	.remove		= sparc_i8042_remove,
105 };
106 
107 static bool i8042_is_mr_coffee(void)
108 {
109 	struct device_node *root;
110 	const char *name;
111 	bool is_mr_coffee;
112 
113 	root = of_find_node_by_path("/");
114 
115 	name = of_get_property(root, "name", NULL);
116 	is_mr_coffee = name && !strcmp(name, "SUNW,JavaStation-1");
117 
118 	of_node_put(root);
119 
120 	return is_mr_coffee;
121 }
122 
123 static int __init i8042_platform_init(void)
124 {
125 	if (i8042_is_mr_coffee()) {
126 		/* Hardcoded values for MrCoffee.  */
127 		i8042_kbd_irq = i8042_aux_irq = 13 | 0x20;
128 		kbd_iobase = ioremap(0x71300060, 8);
129 		if (!kbd_iobase)
130 			return -ENODEV;
131 	} else {
132 		int err = platform_driver_register(&sparc_i8042_driver);
133 		if (err)
134 			return err;
135 
136 		if (i8042_kbd_irq == -1 ||
137 		    i8042_aux_irq == -1) {
138 			if (kbd_iobase) {
139 				of_iounmap(kbd_res, kbd_iobase, 8);
140 				kbd_iobase = (void __iomem *) NULL;
141 			}
142 			return -ENODEV;
143 		}
144 	}
145 
146 	i8042_reset = I8042_RESET_ALWAYS;
147 
148 	return 0;
149 }
150 
151 static inline void i8042_platform_exit(void)
152 {
153 	if (!i8042_is_mr_coffee())
154 		platform_driver_unregister(&sparc_i8042_driver);
155 }
156 
157 #else /* !CONFIG_PCI */
158 static int __init i8042_platform_init(void)
159 {
160 	return -ENODEV;
161 }
162 
163 static inline void i8042_platform_exit(void)
164 {
165 }
166 #endif /* !CONFIG_PCI */
167 
168 #endif /* _I8042_SPARCIO_H */
169