mb/google/nissa: Create pujjogatwin variant
[coreboot2.git] / payloads / libpayload / drivers / serial / 8250.c
blobf3d98a9b79cddc97dcd7d8d43023482e0c75802a
1 /*
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Ulf Jordan <jordan@chalmers.se>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <libpayload-config.h>
31 #include <libpayload.h>
33 static struct cb_serial cb_serial;
35 #define IOBASE cb_serial.baseaddr
36 #define MEMBASE (phys_to_virt(IOBASE))
38 static int serial_hardware_is_present = 0;
39 static int serial_is_mem_mapped = 0;
41 static uint8_t serial_read_reg(int offset)
43 offset *= cb_serial.regwidth;
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);
56 static void serial_write_reg(uint8_t val, int offset)
58 offset *= cb_serial.regwidth;
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);
71 #if CONFIG(LP_SERIAL_SET_SPEED)
72 static void serial_hardware_init(int speed, int word_bits,
73 int parity, int stop_bits)
75 #if !CONFIG(LP_PL011_SERIAL_CONSOLE)
76 unsigned char reg;
78 /* Disable interrupts. */
79 serial_write_reg(0, 0x01);
81 /* Assert RTS and DTR. */
82 serial_write_reg(3, 0x04);
84 /* Set the divisor latch. */
85 reg = serial_read_reg(0x03);
86 serial_write_reg(reg | 0x80, 0x03);
88 /* Write the divisor. */
89 uint16_t divisor = 115200 / speed;
90 serial_write_reg(divisor & 0xFF, 0x00);
91 serial_write_reg(divisor >> 8, 0x01);
93 /* Restore the previous value of the divisor.
94 * And set 8 bits per character */
95 serial_write_reg((reg & ~0x80) | 3, 0x03);
96 #endif
98 #endif
100 static struct console_input_driver consin = {
101 .havekey = &serial_havechar,
102 .getchar = &serial_getchar,
103 .input_type = CONSOLE_INPUT_TYPE_UART,
106 static struct console_output_driver consout = {
107 .putchar = &serial_putchar
110 void serial_init(void)
112 serial_is_mem_mapped = (cb_serial.type == CB_SERIAL_TYPE_MEMORY_MAPPED);
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;
121 #else
122 printf("IO space mapped serial not supported.");
123 return;
124 #endif
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;
133 void serial_console_init(void)
135 if (!lib_sysinfo.cb_serial)
136 return;
137 cb_serial = *(struct cb_serial *)phys_to_virt(lib_sysinfo.cb_serial);
139 serial_init();
141 console_add_input_driver(&consin);
142 console_add_output_driver(&consout);
145 void serial_putchar(unsigned int c)
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');
157 int serial_havechar(void)
159 if (!serial_hardware_is_present)
160 return 0;
161 return serial_read_reg(0x05) & 0x01;
164 int serial_getchar(void)
166 if (!serial_hardware_is_present)
167 return -1;
168 while (!serial_havechar()) ;
169 return serial_read_reg(0x00);