| 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 |
#include <libpayload-config.h> |
| 31 |
#include <libpayload.h> |
| 32 |
|
| 33 |
static struct cb_serial cb_serial; |
| 34 |
|
| 35 |
#define IOBASE cb_serial.baseaddr |
| 36 |
#define MEMBASE (phys_to_virt(IOBASE)) |
| 37 |
|
| 38 |
static int serial_hardware_is_present = 0; |
| 39 |
static int serial_is_mem_mapped = 0; |
| 40 |
|
| 41 |
static uint8_t serial_read_reg(int offset) |
| 42 |
{ |
| 43 |
offset *= cb_serial.regwidth; |
| 44 |
|
| 45 |
#if CONFIG(LP_IO_ADDRESS_SPACE) |
| 46 |
if (!serial_is_mem_mapped) |
| 47 |
return inb(IOBASE + offset); |
| 48 |
else |
| 49 |
#endif |
| 50 |
if (cb_serial.regwidth == 4) |
| 51 |
return readl(MEMBASE + offset) & 0xff; |
| 52 |
else |
| 53 |
return readb(MEMBASE + offset); |
| 54 |
} |
| 55 |
|
| 56 |
static void serial_write_reg(uint8_t val, int offset) |
| 57 |
{ |
| 58 |
offset *= cb_serial.regwidth; |
| 59 |
|
| 60 |
#if CONFIG(LP_IO_ADDRESS_SPACE) |
| 61 |
if (!serial_is_mem_mapped) |
| 62 |
outb(val, IOBASE + offset); |
| 63 |
else |
| 64 |
#endif |
| 65 |
if (cb_serial.regwidth == 4) |
| 66 |
writel(val & 0xff, MEMBASE + offset); |
| 67 |
else |
| 68 |
writeb(val, MEMBASE + offset); |
| 69 |
} |
| 70 |
|
| 71 |
#if CONFIG(LP_SERIAL_SET_SPEED) |
| 72 |
static void serial_hardware_init(int speed, int word_bits, |
| 73 |
int parity, int stop_bits) |
| 74 |
{ |
| 75 |
#if !CONFIG(LP_PL011_SERIAL_CONSOLE) |
| 76 |
unsigned char reg; |
| 77 |
|
| 78 |
|
| 79 |
serial_write_reg(0, 0x01); |
| 80 |
|
| 81 |
|
| 82 |
serial_write_reg(3, 0x04); |
| 83 |
|
| 84 |
|
| 85 |
reg = serial_read_reg(0x03); |
| 86 |
serial_write_reg(reg | 0x80, 0x03); |
| 87 |
|
| 88 |
|
| 89 |
uint16_t divisor = 115200 / speed; |
| 90 |
serial_write_reg(divisor & 0xFF, 0x00); |
| 91 |
serial_write_reg(divisor >> 8, 0x01); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
serial_write_reg((reg & ~0x80) | 3, 0x03); |
| 96 |
#endif |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
static struct console_input_driver consin = { |
| 101 |
.havekey = &serial_havechar, |
| 102 |
.getchar = &serial_getchar, |
| 103 |
.input_type = CONSOLE_INPUT_TYPE_UART, |
| 104 |
}; |
| 105 |
|
| 106 |
static struct console_output_driver consout = { |
| 107 |
.putchar = &serial_putchar |
| 108 |
}; |
| 109 |
|
| 110 |
void serial_init(void) |
| 111 |
{ |
| 112 |
serial_is_mem_mapped = (cb_serial.type == CB_SERIAL_TYPE_MEMORY_MAPPED); |
| 113 |
|
| 114 |
if (!serial_is_mem_mapped) { |
| 115 |
#if CONFIG(LP_IO_ADDRESS_SPACE) |
| 116 |
if ((inb(IOBASE + 0x05) == 0xFF) && |
| 117 |
(inb(IOBASE + 0x06) == 0xFF)) { |
| 118 |
printf("IO space mapped serial not present."); |
| 119 |
return; |
| 120 |
} |
| 121 |
#else |
| 122 |
printf("IO space mapped serial not supported."); |
| 123 |
return; |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
#if CONFIG(LP_SERIAL_SET_SPEED) |
| 128 |
serial_hardware_init(CONFIG_LP_SERIAL_BAUD_RATE, 8, 0, 1); |
| 129 |
#endif |
| 130 |
serial_hardware_is_present = 1; |
| 131 |
} |
| 132 |
|
| 133 |
void serial_console_init(void) |
| 134 |
{ |
| 135 |
if (!lib_sysinfo.cb_serial) |
| 136 |
return; |
| 137 |
cb_serial = *(struct cb_serial *)phys_to_virt(lib_sysinfo.cb_serial); |
| 138 |
|
| 139 |
serial_init(); |
| 140 |
|
| 141 |
console_add_input_driver(&consin); |
| 142 |
console_add_output_driver(&consout); |
| 143 |
} |
| 144 |
|
| 145 |
void serial_putchar(unsigned int c) |
| 146 |
{ |
| 147 |
if (!serial_hardware_is_present) |
| 148 |
return; |
| 149 |
#if !CONFIG(LP_PL011_SERIAL_CONSOLE) |
| 150 |
while ((serial_read_reg(0x05) & 0x20) == 0) ; |
| 151 |
#endif |
| 152 |
serial_write_reg(c, 0x00); |
| 153 |
if (c == '\n') |
| 154 |
serial_putchar('\r'); |
| 155 |
} |
| 156 |
|
| 157 |
int serial_havechar(void) |
| 158 |
{ |
| 159 |
if (!serial_hardware_is_present) |
| 160 |
return 0; |
| 161 |
return serial_read_reg(0x05) & 0x01; |
| 162 |
} |
| 163 |
|
| 164 |
int serial_getchar(void) |
| 165 |
{ |
| 166 |
if (!serial_hardware_is_present) |
| 167 |
return -1; |
| 168 |
while (!serial_havechar()) ; |
| 169 |
return serial_read_reg(0x00); |
| 170 |
} |