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