-
Notifications
You must be signed in to change notification settings - Fork 12
/
3002-applesmc-make-io-port-base-addr-dynamic.patch
312 lines (266 loc) · 8.73 KB
/
3002-applesmc-make-io-port-base-addr-dynamic.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
From d0b5f668d43281eda94dd64d48e39973139d107c Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <[email protected]>
Date: Sun, 17 Nov 2019 23:11:56 +0100
Subject: [PATCH 2/9] applesmc: make io port base addr dynamic
This change makes the port base runtime configurable.
The reason why this change is made is so that when we switch to an
acpi_device we can resolve the port base addr from ACPI.
This change is not strictly required for T2 support - the base
address is still 0x300 on T2 Macs.
Signed-off-by: Aun-Ali Zaidi <[email protected]>
---
drivers/hwmon/applesmc.c | 91 +++++++++++++++++++++-------------------
1 file changed, 49 insertions(+), 42 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 7fb40738d..a599db68b 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -35,10 +35,11 @@
#include <linux/err.h>
#include <linux/bits.h>
+#define APPLESMC_PORT_BASE 0x300
/* data port used by Apple SMC */
-#define APPLESMC_DATA_PORT 0x300
+#define APPLESMC_DATA_PORT 0
/* command/status port used by Apple SMC */
-#define APPLESMC_CMD_PORT 0x304
+#define APPLESMC_CMD_PORT 4
#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
@@ -140,6 +141,8 @@ struct applesmc_device {
struct platform_device *dev;
struct applesmc_registers reg;
+ u16 port_base;
+
s16 rest_x;
s16 rest_y;
@@ -169,7 +172,7 @@ static const int debug;
* run out past 500ms.
*/
-static int wait_status(u8 val, u8 mask)
+static int wait_status(struct applesmc_device *smc, u8 val, u8 mask)
{
u8 status;
int us;
@@ -177,7 +180,7 @@ static int wait_status(u8 val, u8 mask)
us = APPLESMC_MIN_WAIT;
for (i = 0; i < 24 ; i++) {
- status = inb(APPLESMC_CMD_PORT);
+ status = inb(smc->port_base + APPLESMC_CMD_PORT);
if ((status & mask) == val)
return 0;
usleep_range(us, us * 2);
@@ -189,11 +192,11 @@ static int wait_status(u8 val, u8 mask)
/* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
-static int send_byte(u8 cmd, u16 port)
+static int send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
{
int status;
- status = wait_status(0, SMC_STATUS_IB_CLOSED);
+ status = wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
if (status)
return status;
/*
@@ -202,24 +205,24 @@ static int send_byte(u8 cmd, u16 port)
* this extra read may not happen if status returns both
* simultaneously and this would appear to be required.
*/
- status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
+ status = wait_status(smc, SMC_STATUS_BUSY, SMC_STATUS_BUSY);
if (status)
return status;
- outb(cmd, port);
+ outb(cmd, smc->port_base + port);
return 0;
}
/* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
-static int send_command(u8 cmd)
+static int send_command(struct applesmc_device *smc, u8 cmd)
{
int ret;
- ret = wait_status(0, SMC_STATUS_IB_CLOSED);
+ ret = wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
if (ret)
return ret;
- outb(cmd, APPLESMC_CMD_PORT);
+ outb(cmd, smc->port_base + APPLESMC_CMD_PORT);
return 0;
}
@@ -229,108 +232,112 @@ static int send_command(u8 cmd)
* If busy is stuck high after the command then the SMC is jammed.
*/
-static int smc_sane(void)
+static int smc_sane(struct applesmc_device *smc)
{
int ret;
- ret = wait_status(0, SMC_STATUS_BUSY);
+ ret = wait_status(smc, 0, SMC_STATUS_BUSY);
if (!ret)
return ret;
- ret = send_command(APPLESMC_READ_CMD);
+ ret = send_command(smc, APPLESMC_READ_CMD);
if (ret)
return ret;
- return wait_status(0, SMC_STATUS_BUSY);
+ return wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int send_argument(const char *key)
+static int send_argument(struct applesmc_device *smc, const char *key)
{
int i;
for (i = 0; i < 4; i++)
- if (send_byte(key[i], APPLESMC_DATA_PORT))
+ if (send_byte(smc, key[i], APPLESMC_DATA_PORT))
return -EIO;
return 0;
}
-static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
+static int read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+ u8 *buffer, u8 len)
{
u8 status, data = 0;
int i;
int ret;
- ret = smc_sane();
+ ret = smc_sane(smc);
if (ret)
return ret;
- if (send_command(cmd) || send_argument(key)) {
+ if (send_command(smc, cmd) || send_argument(smc, key)) {
pr_warn("%.4s: read arg fail\n", key);
return -EIO;
}
/* This has no effect on newer (2012) SMCs */
- if (send_byte(len, APPLESMC_DATA_PORT)) {
+ if (send_byte(smc, len, APPLESMC_DATA_PORT)) {
pr_warn("%.4s: read len fail\n", key);
return -EIO;
}
for (i = 0; i < len; i++) {
- if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
+ if (wait_status(smc,
+ SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
pr_warn("%.4s: read data[%d] fail\n", key, i);
return -EIO;
}
- buffer[i] = inb(APPLESMC_DATA_PORT);
+ buffer[i] = inb(smc->port_base + APPLESMC_DATA_PORT);
}
/* Read the data port until bit0 is cleared */
for (i = 0; i < 16; i++) {
udelay(APPLESMC_MIN_WAIT);
- status = inb(APPLESMC_CMD_PORT);
+ status = inb(smc->port_base + APPLESMC_CMD_PORT);
if (!(status & SMC_STATUS_AWAITING_DATA))
break;
- data = inb(APPLESMC_DATA_PORT);
+ data = inb(smc->port_base + APPLESMC_DATA_PORT);
}
if (i)
pr_warn("flushed %d bytes, last value is: %d\n", i, data);
- return wait_status(0, SMC_STATUS_BUSY);
+ return wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
+static int write_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+ const u8 *buffer, u8 len)
{
int i;
int ret;
- ret = smc_sane();
+ ret = smc_sane(smc);
if (ret)
return ret;
- if (send_command(cmd) || send_argument(key)) {
+ if (send_command(smc, cmd) || send_argument(smc, key)) {
pr_warn("%s: write arg fail\n", key);
return -EIO;
}
- if (send_byte(len, APPLESMC_DATA_PORT)) {
+ if (send_byte(smc, len, APPLESMC_DATA_PORT)) {
pr_warn("%.4s: write len fail\n", key);
return -EIO;
}
for (i = 0; i < len; i++) {
- if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
+ if (send_byte(smc, buffer[i], APPLESMC_DATA_PORT)) {
pr_warn("%s: write data fail\n", key);
return -EIO;
}
}
- return wait_status(0, SMC_STATUS_BUSY);
+ return wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int read_register_count(unsigned int *count)
+static int read_register_count(struct applesmc_device *smc,
+ unsigned int *count)
{
__be32 be;
int ret;
- ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
+ ret = read_smc(smc, APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
if (ret)
return ret;
@@ -353,7 +360,7 @@ static int applesmc_read_entry(struct applesmc_device *smc,
if (entry->len != len)
return -EINVAL;
mutex_lock(&smc->reg.mutex);
- ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
+ ret = read_smc(smc, APPLESMC_READ_CMD, entry->key, buf, len);
mutex_unlock(&smc->reg.mutex);
return ret;
@@ -367,7 +374,7 @@ static int applesmc_write_entry(struct applesmc_device *smc,
if (entry->len != len)
return -EINVAL;
mutex_lock(&smc->reg.mutex);
- ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
+ ret = write_smc(smc, APPLESMC_WRITE_CMD, entry->key, buf, len);
mutex_unlock(&smc->reg.mutex);
return ret;
}
@@ -388,10 +395,10 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(
if (cache->valid)
goto out;
be = cpu_to_be32(index);
- ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
+ ret = read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
if (ret)
goto out;
- ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
+ ret = read_smc(smc, APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
if (ret)
goto out;
@@ -589,7 +596,7 @@ static int applesmc_init_smcreg_try(struct applesmc_device *smc)
if (s->init_complete)
return 0;
- ret = read_register_count(&count);
+ ret = read_register_count(smc, &count);
if (ret)
return ret;
@@ -1472,7 +1479,7 @@ static int __init applesmc_init(void)
goto out;
}
- if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
+ if (!request_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS,
"applesmc")) {
ret = -ENXIO;
goto out;
@@ -1494,7 +1501,7 @@ static int __init applesmc_init(void)
out_driver:
platform_driver_unregister(&applesmc_driver);
out_region:
- release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
+ release_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS);
out:
pr_warn("driver init failed (ret=%d)!\n", ret);
return ret;
@@ -1504,7 +1511,7 @@ static void __exit applesmc_exit(void)
{
platform_device_unregister(pdev);
platform_driver_unregister(&applesmc_driver);
- release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
+ release_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS);
}
module_init(applesmc_init);
--
2.45.2