aboutsummaryrefslogtreecommitdiff
path: root/smc.c
blob: a875b2cbbaef92763c3f3d72abeaa7624ab217df (plain)
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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2019 Evgeny Zinoviev <me@ch1p.io>
 *
 * SMC interacting code is based on applesmc linux driver by:
 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdlib.h>
#include <sys/io.h>
#include <errno.h>
#include <stdio.h>
#include "smc.h"

/*
 * wait_read - Wait for a byte to appear on SMC port. Callers must
 * hold applesmc_lock.
 */
int wait_read(void)
{
	uint8_t status;
	int us;
	for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
		usleep(us);
		status = inb(APPLESMC_CMD_PORT);

		/* read: wait for smc to settle */
		if (status & 0x01)
			return 0;
	}

	fprintf(stderr, "%s() fail: 0x%02x\n", __func__, status);
	return -EIO;
}

/*
 * send_byte - Write to SMC port, retrying when necessary. Callers
 * must hold applesmc_lock.
 */
int send_byte(uint8_t cmd, uint16_t port)
{
	uint8_t status;
	int us;

	outb(cmd, port);
	for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
		usleep(us);
		status = inb(APPLESMC_CMD_PORT);
		/* write: wait for smc to settle */
		if (status & 0x02)
			continue;
		/* ready: cmd accepted, return */
		if (status & 0x04)
			return 0;
		/* timeout: give up */
		if (us << 1 == APPLESMC_MAX_WAIT)
			break;
		/* busy: long wait and resend */
		usleep(APPLESMC_RETRY_WAIT);
		outb(cmd, port);
	}

	fprintf(stderr, "%s(0x%02x, 0x%04x) fail: 0x%02x\n",
		__func__, cmd, port, status);
	return -EIO;
}

int send_command(uint8_t cmd)
{
	return send_byte(cmd, APPLESMC_CMD_PORT);
}

int send_argument(const char *key)
{
	int i;

	for (i = 0; i < 4; i++)
		if (send_byte(key[i], APPLESMC_DATA_PORT))
			return -EIO;
	return 0;
}

int read_smc(uint8_t cmd, const char *key, uint8_t *buffer, uint8_t len)
{
	uint8_t status, data = 0;
	int i;

	if (send_command(cmd) || send_argument(key)) {
		fprintf(stderr, "%.4s: read arg fail\n", key);
		return -EIO;
	}

	/* This has no effect on newer (2012) SMCs */
	if (send_byte(len, APPLESMC_DATA_PORT)) {
		fprintf(stderr, "%.4s: read len fail\n", key);
		return -EIO;
	}

	for (i = 0; i < len; i++) {
		if (wait_read()) {
			fprintf(stderr, "%.4s: read data[%d] fail\n", key, i);
			return -EIO;
		}
		buffer[i] = inb(APPLESMC_DATA_PORT);
	}

	/* Read the data port until bit0 is cleared */
	for (i = 0; i < 16; i++) {
		usleep(APPLESMC_MIN_WAIT);
		status = inb(APPLESMC_CMD_PORT);
		if (!(status & 0x01))
			break;
		data = inb(APPLESMC_DATA_PORT);
	}

	if (i)
		fprintf(stderr, "flushed %d bytes, last value is: %d\n",
			i, data);

	return 0;
}

int write_smc(uint8_t cmd, const char *key, const uint8_t *buffer, uint8_t len)
{
	int i;

	if (send_command(cmd) || send_argument(key)) {
		fprintf(stderr, "%s: write arg fail\n", key);
		return -EIO;
	}

	if (send_byte(len, APPLESMC_DATA_PORT)) {
		fprintf(stderr, "%.4s: write len fail\n", key);
		return -EIO;
	}

	for (i = 0; i < len; i++) {
		if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
			fprintf(stderr, "%s: write data fail\n", key);
			return -EIO;
		}
	}

	return 0;
}