summaryrefslogtreecommitdiff
path: root/arch/x86/machine/uart.c
blob: be9ad72562966e31b563ab8e22fffb80211e32ec (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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (c) 2017 Richard Braun.
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <errno.h>
#include <stdint.h>

#include <kern/arg.h>
#include <kern/console.h>
#include <kern/init.h>
#include <kern/intr.h>
#include <kern/log.h>
#include <kern/macros.h>
#include <machine/biosmem.h>
#include <machine/io.h>
#include <machine/uart.h>

#define UART_BDA_COM1_OFFSET 0

#define UART_REG_DAT            0
#define UART_REG_DLL            0
#define UART_REG_IER            1
#define UART_REG_DLH            1
#define UART_REG_IIR            2
#define UART_REG_LCR            3
#define UART_REG_MCR            4
#define UART_REG_LSR            5
#define UART_REG_MSR            6
#define UART_NR_REGS            7

#define UART_IER_RX             0x1

#define UART_IIR_NOT_PENDING    0x1
#define UART_IIR_SRC_RX         0x4
#define UART_IIR_SRC_MASK       0xe

#define UART_LCR_8BITS          0x03
#define UART_LCR_1S             0x00
#define UART_LCR_NP             0x00
#define UART_LCR_OP             0x08
#define UART_LCR_EP             0x18
#define UART_LCR_BEN            0x40
#define UART_LCR_DLAB           0x80

#define UART_MCR_DTR            0x01
#define UART_MCR_RTS            0x02
#define UART_MCR_AUX2           0x04

#define UART_LSR_DATA_READY     0x01
#define UART_LSR_TX_EMPTY       0x20

#define UART_MAX_DEVS           4

#define UART_SPEED_MAX          115200
#define UART_SPEED_DEFAULT      UART_SPEED_MAX

enum {
    UART_PARITY_NONE,
    UART_PARITY_ODD,
    UART_PARITY_EVEN,
};

#define UART_PARITY_DEFAULT     UART_PARITY_NONE

#define UART_DATA_BITS_DEFAULT  8

struct uart {
    struct console console;
    uint16_t port;
    uint16_t intr;
};

static struct uart uart_devs[UART_MAX_DEVS];

static uint16_t uart_intrs[UART_MAX_DEVS] = { 4, 3, 4, 3 };

static size_t
uart_get_id(const struct uart *uart)
{
    size_t id;

    id = uart - uart_devs;
    assert(id < ARRAY_SIZE(uart_devs));
    return id;
}

static uint16_t
uart_get_addr(const struct uart *uart, uint16_t reg)
{
    assert(reg < UART_NR_REGS);
    return uart->port + reg;
}

static uint8_t
uart_read(struct uart *uart, uint16_t reg)
{
    return io_read_byte(uart_get_addr(uart, reg));
}

static void
uart_write(struct uart *uart, uint16_t reg, uint8_t byte)
{
    io_write_byte(uart_get_addr(uart, reg), byte);
}

static void
uart_set(struct uart *uart, uint16_t reg, uint8_t mask)
{
    uint16_t addr;
    uint8_t byte;

    addr = uart_get_addr(uart, reg);
    byte = io_read_byte(addr);
    byte |= mask;
    io_write_byte(addr, byte);
}

static void
uart_clear(struct uart *uart, uint16_t reg, uint8_t mask)
{
    uint16_t addr;
    uint8_t byte;

    addr = uart_get_addr(uart, reg);
    byte = io_read_byte(addr);
    byte &= ~mask;
    io_write_byte(addr, byte);
}

static void
uart_recv_intr(struct uart *uart)
{
    uint8_t byte;
    char tmp[2];

    tmp[1] = '\0';

    for (;;) {
        byte = uart_read(uart, UART_REG_LSR);

        if (!(byte & UART_LSR_DATA_READY)) {
            break;
        }

        byte = uart_read(uart, UART_REG_DAT);
        tmp[0] = (char)byte;
        console_intr(&uart->console, tmp);
    }
}

static int
uart_intr(void *arg)
{
    struct uart *uart;
    uint8_t byte;

    uart = arg;

    byte = uart_read(uart, UART_REG_IIR);

    if (byte & UART_IIR_NOT_PENDING) {
        return EAGAIN;
    }

    byte &= UART_IIR_SRC_MASK;

    if (byte == UART_IIR_SRC_RX) {
        uart_recv_intr(uart);
    }

    return 0;
}

static void __init
uart_enable_intr(struct uart *uart)
{
    int error;

    error = intr_register(uart->intr, uart_intr, uart);

    if (error) {
        log_err("uart%zu: unable to register interrupt %u",
                 uart_get_id(uart), uart->intr);
        return;
    }

    uart_write(uart, UART_REG_IER, UART_IER_RX);
}

static void
uart_tx_wait(struct uart *uart)
{
    uint8_t byte;

    for (;;) {
        byte = uart_read(uart, UART_REG_LSR);

        if (byte & UART_LSR_TX_EMPTY) {
            break;
        }
    }
}

static void
uart_write_char_common(struct uart *uart, char c)
{
    uart_tx_wait(uart);
    uart_write(uart, UART_REG_DAT, (uint8_t)c);
}

static void
uart_write_char(struct uart *uart, char c)
{
    if (c == '\n') {
        uart_write_char_common(uart, '\r');
    }

    uart_write_char_common(uart, c);
}

static struct uart *
uart_get_dev(size_t i)
{
    assert(i < ARRAY_SIZE(uart_devs));
    return &uart_devs[i];
}

static struct uart *
uart_get_from_console(struct console *console)
{
    return structof(console, struct uart, console);
}

static void
uart_console_putc(struct console *console, char c)
{
    uart_write_char(uart_get_from_console(console), c);
}

static const struct console_ops uart_console_ops = {
    .putc = uart_console_putc,
};

static void __init
uart_init_default(unsigned int *speed, unsigned int *parity,
                  unsigned int *data_bits)
{
    *speed = UART_SPEED_DEFAULT;
    *parity = UART_PARITY_DEFAULT;
    *data_bits = UART_DATA_BITS_DEFAULT;
}

static int __init
uart_init_check_speed(unsigned int speed)
{
    if (speed > UART_SPEED_MAX) {
        return EINVAL;
    }

    return 0;
}

static int __init
uart_init_convert_parity_char(char c, unsigned int *parity)
{
    switch (c) {
    case 'n':
        *parity = UART_PARITY_NONE;
        break;
    case 'o':
        *parity = UART_PARITY_ODD;
        break;
    case 'e':
        *parity = UART_PARITY_EVEN;
        break;
    default:
        return EINVAL;
    }

    return 0;
}

static int __init
uart_init_check_data_bits(unsigned int data_bits)
{
    switch (data_bits) {
    case 5 ... 8:
        break;
    default:
        return EINVAL;
    }

    return 0;
}

static void __init
uart_init_args(const struct uart *uart, const char *arg_str,
               unsigned int *speed, unsigned int *parity,
               unsigned int *data_bits)
{
    char parity_char;
    int ret, error;

    ret = sscanf(arg_str, "%u%c%1u", speed, &parity_char, data_bits);

    if (ret < 1) {
        goto set_defaults;
    }

    error = uart_init_check_speed(*speed);

    if (error) {
        goto set_defaults;
    } else if (ret < 2) {
        return;
    }

    error = uart_init_convert_parity_char(parity_char, parity);

    if (error) {
        goto set_defaults;
    } else if (ret < 3) {
        return;
    }

    error = uart_init_check_data_bits(*data_bits);

    if (error) {
        goto set_defaults;
    }

    return;

set_defaults:
    log_warning("uart%zu: invalid serial configuration, using defaults",
                uart_get_id(uart));
    uart_init_default(speed, parity, data_bits);
}

static void __init
uart_init(struct uart *uart, uint16_t port, uint16_t intr)
{
    unsigned int speed, parity, data_bits;
    const char *arg_str;
    char name[CONSOLE_NAME_SIZE];
    uint16_t divisor;
    uint8_t byte;

    snprintf(name, sizeof(name), "uart%zu", uart_get_id(uart));
    arg_str = arg_value(name);

    uart_init_default(&speed, &parity, &data_bits);

    if (arg_str != NULL) {
        uart_init_args(uart, arg_str, &speed, &parity, &data_bits);
    }

    log_debug("uart%zu: speed:%u parity:%u data_bits:%u",
              uart_get_id(uart), speed, parity, data_bits);

    uart->port = port;
    uart->intr = intr;

    uart_write(uart, UART_REG_IER, 0);

    divisor = UART_SPEED_MAX / speed;

    uart_set(uart, UART_REG_LCR, UART_LCR_DLAB);
    uart_write(uart, UART_REG_DLH, divisor >> 8);
    uart_write(uart, UART_REG_DLL, divisor & 0xff);
    uart_clear(uart, UART_REG_LCR, UART_LCR_DLAB);

    uart_write(uart, UART_REG_MCR, UART_MCR_AUX2 | UART_MCR_RTS | UART_MCR_DTR);

    byte = UART_LCR_1S;

    switch (parity) {
    case UART_PARITY_NONE:
        byte |= UART_LCR_NP;
        break;
    case UART_PARITY_ODD:
        byte |= UART_LCR_OP;
        break;
    case UART_PARITY_EVEN:
        byte |= UART_LCR_EP;
        break;
    }

    byte |= (data_bits - 5);
    uart_write(uart, UART_REG_LCR, byte);

    console_init(&uart->console, name, &uart_console_ops);
    console_register(&uart->console);
}

static void __init
uart_log_info(void)
{
    const struct uart *uart;
    size_t i;

    for (i = 0; i < ARRAY_SIZE(uart_devs); i++) {
        uart = uart_get_dev(i);

        if (uart->port != 0) {
            log_info("uart%zu: port:%#x irq:%u", i, (unsigned int)uart->port,
                     (unsigned int)uart->intr);
        }
    }
}

static int __init
uart_bootstrap(void)
{
    const uint16_t *ptr;
    size_t i;

    ptr = biosmem_get_bda() + UART_BDA_COM1_OFFSET;

    for (i = 0; i < UART_MAX_DEVS; i++) {
        if (ptr[i] == 0) {
            continue;
        }

        uart_init(uart_get_dev(i), ptr[i], uart_intrs[i]);
    }

    uart_log_info();
    return 0;
}

INIT_OP_DEFINE(uart_bootstrap,
               INIT_OP_DEP(arg_setup, true),
               INIT_OP_DEP(console_bootstrap, true),
               INIT_OP_DEP(log_setup, true));

static int __init
uart_setup(void)
{
    struct uart *uart;
    size_t i;

    for (i = 0; i < ARRAY_SIZE(uart_devs); i++) {
        uart = uart_get_dev(i);

        if (uart->port == 0) {
            continue;
        }

        uart_enable_intr(uart);
    }

    return 0;
}

INIT_OP_DEFINE(uart_setup,
               INIT_OP_DEP(intr_setup, true),
               INIT_OP_DEP(uart_bootstrap, true));