summaryrefslogtreecommitdiff
path: root/kernel/serial.c
blob: 86bbd87135c90a8e16654ee0819ffee6fca0ec78 (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
#include <asm/io.h>
#include <kernel/con.h>

/* 115200 baud */
#define DIVISOR 1

#define RSBASE 0x3F8

#define RSDATA    RSBASE
#define RSINTEN   (RSBASE + 1)
#define RSFIFOC   (RSBASE + 2)
#define RSLINEC   (RSBASE + 3)
#define RSMODEMC  (RSBASE + 4)
#define RSLINES   (RSBASE + 5)
#define RSMODEMS  (RSBASE + 6)
#define RSSCRATCH (RSBASE + 7)

#define rswait()\
  while((inb(RSLINES) & 0x20) == 0);

int rsputs(char *s) {
  while(*s) {
    rswait();
    if(*s == '\n') {
      outb(RSDATA, '\n');
      rswait();
      outb(RSDATA, '\r');
      s++;
    } else {
      outb(RSDATA, *s++);
    }
  }

  return 0;
}

void serial_init(void) {
  outb(RSSCRATCH, 0x00);
  outb(RSSCRATCH, 0xAA);
  if(inb(RSSCRATCH) != 0xAA) {
    printk("[rs] COM0 not detected!\n");
    return;
  }

  /* enable access to the divisor */
  outb(RSLINEC, inb(RSLINEC) | 0x80);
  /* write the divisor */
  outb(RSBASE, DIVISOR);
  outb(RSBASE + 1, DIVISOR >> 8);
  /* restore access to the divisor */
  outb(RSLINEC, inb(RSLINEC) & 0x7F);
  /* configure port parameters */
  outb(RSLINEC, 0x03);
  outb(RSINTEN, 0x00);

  rsputs("[kernel] Serial monitor initialized!\n\r");
  printk("[rs] Serial port COM0 initialized!\n");
}