summaryrefslogtreecommitdiffstats
path: root/drivers/serial/serial_efi.c
blob: f0a2b22c2bc2e2ddbeef4950024fd33a3a498d21 (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
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
/*
 * Copyright (C) 2017 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 *
 * Under GPLv2 Only
 */

#include <common.h>
#include <driver.h>
#include <init.h>
#include <malloc.h>
#include <efi.h>
#include <efi/efi.h>
#include <efi/efi-device.h>

/*
 * define for Control bits, grouped by read only, write only, and read write
 *
 * Read Only
 */
#define EFI_SERIAL_CLEAR_TO_SEND        0x00000010
#define EFI_SERIAL_DATA_SET_READY       0x00000020
#define EFI_SERIAL_RING_INDICATE        0x00000040
#define EFI_SERIAL_CARRIER_DETECT       0x00000080
#define EFI_SERIAL_INPUT_BUFFER_EMPTY   0x00000100
#define EFI_SERIAL_OUTPUT_BUFFER_EMPTY  0x00000200

/*
 * Write Only
 */
#define EFI_SERIAL_REQUEST_TO_SEND      0x00000002
#define EFI_SERIAL_DATA_TERMINAL_READY  0x00000001

/*
 * Read Write
 */
#define EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE     0x00001000
#define EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE     0x00002000
#define EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE 0x00004000

typedef enum {
	DefaultParity,
	NoParity,
	EvenParity,
	OddParity,
	MarkParity,
	SpaceParity
} efi_parity_type;

typedef enum {
	DefaultStopBits,
	OneStopBit,
	OneFiveStopBits,
	TwoStopBits
} efi_stop_bits_type;

struct efi_serial_io_mode {
	uint32_t controlmask;
	uint32_t timeout;
	uint64_t baudrate;
	uint32_t receivefifodepth;
	uint32_t databits;
	uint32_t parity;
	uint32_t stopbits;
};

struct efi_serial_io_protocol {
	uint32_t revision;

	efi_status_t (EFIAPI *reset) (struct efi_serial_io_protocol *This);
	efi_status_t (EFIAPI *set_attributes) (struct efi_serial_io_protocol *This,
			uint64_t baudrate, uint32_t receivefifodepth,
			uint32_t timeout, efi_parity_type parity,
			uint8_t databits, efi_stop_bits_type stopbits);
	efi_status_t (EFIAPI *setcontrol) (struct efi_serial_io_protocol *This,
			uint32_t control);
	efi_status_t (EFIAPI *getcontrol) (struct efi_serial_io_protocol *This,
			uint32_t *control);
	efi_status_t (EFIAPI *write) (struct efi_serial_io_protocol *This,
			unsigned long *buffersize, void *buffer);
	efi_status_t (EFIAPI *read) (struct efi_serial_io_protocol *This,
			unsigned long *buffersize, void *buffer);

	struct efi_serial_io_mode *mode;
};

/*
 * We wrap our port structure around the generic console_device.
 */
struct efi_serial_port {
	struct efi_serial_io_protocol *serial;
	struct console_device	uart;		/* uart */
	struct efi_device *efidev;
};

static inline struct efi_serial_port *
to_efi_serial_port(struct console_device *uart)
{
	return container_of(uart, struct efi_serial_port, uart);
}

static int efi_serial_setbaudrate(struct console_device *cdev, int baudrate)
{
	struct efi_serial_port *uart = to_efi_serial_port(cdev);
	struct efi_serial_io_protocol *serial = uart->serial;
	efi_status_t efiret;

	efiret = serial->set_attributes(serial, baudrate, 0, 0, NoParity, 8,
					OneStopBit);
	if (EFI_ERROR(efiret))
		return -efi_errno(efiret);

	return 0;
}

static void efi_serial_putc(struct console_device *cdev, char c)
{
	struct efi_serial_port *uart = to_efi_serial_port(cdev);
	struct efi_serial_io_protocol *serial = uart->serial;
	uint32_t control;
	efi_status_t efiret;
	unsigned long buffersize = sizeof(char);

	do {
		efiret = serial->getcontrol(serial, &control);
		if (EFI_ERROR(efiret))
			return;

	} while(!(control & EFI_SERIAL_CLEAR_TO_SEND));

	serial->write(serial, &buffersize, &c);
}

static int efi_serial_puts(struct console_device *cdev, const char *s)
{
	struct efi_serial_port *uart = to_efi_serial_port(cdev);
	struct efi_serial_io_protocol *serial = uart->serial;
	uint32_t control;
	efi_status_t efiret;
	unsigned long buffersize = strlen(s) * sizeof(char);

	do {
		efiret = serial->getcontrol(serial, &control);
		if (EFI_ERROR(efiret))
			return 0;

	} while(!(control & EFI_SERIAL_CLEAR_TO_SEND));

	serial->write(serial, &buffersize, (void*)s);

	return strlen(s);
}

static int efi_serial_getc(struct console_device *cdev)
{
	struct efi_serial_port *uart = to_efi_serial_port(cdev);
	struct efi_serial_io_protocol *serial = uart->serial;
	uint32_t control;
	efi_status_t efiret;
	unsigned long buffersize = sizeof(char);
	char c;

	do {
		efiret = serial->getcontrol(serial, &control);
		if (EFI_ERROR(efiret))
			return (int)-1;

	} while(!(control & EFI_SERIAL_DATA_SET_READY));

	serial->read(serial, &buffersize, &c);

	return (int)c;
}

static int efi_serial_tstc(struct console_device *cdev)
{
	struct efi_serial_port *uart = to_efi_serial_port(cdev);
	struct efi_serial_io_protocol *serial = uart->serial;
	uint32_t control;
	efi_status_t efiret;

	efiret = serial->getcontrol(serial, &control);
	if (EFI_ERROR(efiret))
		return 0;

	return !(control & EFI_SERIAL_INPUT_BUFFER_EMPTY);
}

static int efi_serial_probe(struct efi_device *efidev)
{
	struct efi_serial_port *uart;
	struct console_device *cdev;

	uart = xzalloc(sizeof(struct efi_serial_port));

	cdev = &uart->uart;
	cdev->dev = &efidev->dev;
	cdev->tstc = efi_serial_tstc;
	cdev->putc = efi_serial_putc;
	cdev->puts = efi_serial_puts;
	cdev->getc = efi_serial_getc;
	cdev->setbrg = efi_serial_setbaudrate;

	uart->serial = efidev->protocol;

	uart->serial->reset(uart->serial);

	/* Enable UART */

	console_register(cdev);

	return 0;
}

static struct efi_driver efi_serial_driver = {
        .driver = {
		.name  = "efi-serial",
	},
        .probe = efi_serial_probe,
	.guid = EFI_SERIAL_IO_PROTOCOL_GUID,
};
device_efi_driver(efi_serial_driver);