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