Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
Ulf Jordan | b4d4bac | 2008-08-11 20:34:28 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 Ulf Jordan <jordan@chalmers.se> |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Jordan Crouse | db8c0ab | 2008-11-24 17:54:46 +0000 | [diff] [blame] | 31 | #include <libpayload-config.h> |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 32 | #include <libpayload.h> |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 33 | |
Gabe Black | 5ab2005 | 2012-08-28 16:38:34 -0700 | [diff] [blame] | 34 | #define IOBASE lib_sysinfo.serial->baseaddr |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 35 | #define MEMBASE (phys_to_virt(IOBASE)) |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 36 | |
Kyösti Mälkki | 48e9eb8 | 2014-05-13 12:57:26 +0300 | [diff] [blame] | 37 | static int serial_hardware_is_present = 0; |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 38 | static int serial_is_mem_mapped = 0; |
Dave Frodin | 5056b6e | 2012-12-15 13:24:02 -0700 | [diff] [blame] | 39 | |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 40 | static uint8_t serial_read_reg(int offset) |
Stefan Reinauer | e75c3d8 | 2008-09-26 18:36:26 +0000 | [diff] [blame] | 41 | { |
Vadim Bendebury | 6cc5e52 | 2015-01-09 16:54:19 -0800 | [diff] [blame] | 42 | offset *= lib_sysinfo.serial->regwidth; |
| 43 | |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 44 | #if IS_ENABLED(CONFIG_LP_IO_ADDRESS_SPACE) |
Gabe Black | b7b57d9 | 2013-01-18 18:37:29 -0800 | [diff] [blame] | 45 | if (!serial_is_mem_mapped) |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 46 | return inb(IOBASE + offset); |
Gabe Black | b7b57d9 | 2013-01-18 18:37:29 -0800 | [diff] [blame] | 47 | else |
| 48 | #endif |
Patrick Georgi | d4adf58 | 2016-02-08 21:17:12 +0100 | [diff] [blame] | 49 | if (lib_sysinfo.serial->regwidth == 4) |
| 50 | return readl(MEMBASE + offset) & 0xff; |
| 51 | else |
| 52 | return readb(MEMBASE + offset); |
Stefan Reinauer | e75c3d8 | 2008-09-26 18:36:26 +0000 | [diff] [blame] | 53 | } |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 54 | |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 55 | static void serial_write_reg(uint8_t val, int offset) |
| 56 | { |
Vadim Bendebury | 6cc5e52 | 2015-01-09 16:54:19 -0800 | [diff] [blame] | 57 | offset *= lib_sysinfo.serial->regwidth; |
| 58 | |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 59 | #if IS_ENABLED(CONFIG_LP_IO_ADDRESS_SPACE) |
Gabe Black | b7b57d9 | 2013-01-18 18:37:29 -0800 | [diff] [blame] | 60 | if (!serial_is_mem_mapped) |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 61 | outb(val, IOBASE + offset); |
Gabe Black | b7b57d9 | 2013-01-18 18:37:29 -0800 | [diff] [blame] | 62 | else |
| 63 | #endif |
Patrick Georgi | d4adf58 | 2016-02-08 21:17:12 +0100 | [diff] [blame] | 64 | if (lib_sysinfo.serial->regwidth == 4) |
| 65 | writel(val & 0xff, MEMBASE + offset); |
| 66 | else |
| 67 | writeb(val, MEMBASE + offset); |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 70 | #if IS_ENABLED(CONFIG_LP_SERIAL_SET_SPEED) |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 71 | static void serial_hardware_init(int speed, int word_bits, |
| 72 | int parity, int stop_bits) |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 73 | { |
| 74 | unsigned char reg; |
| 75 | |
Nico Huber | a7c609c | 2015-09-09 13:27:09 +0200 | [diff] [blame] | 76 | #if !IS_ENABLED(CONFIG_LP_PL011_SERIAL_CONSOLE) |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 77 | /* Disable interrupts. */ |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 78 | serial_write_reg(0, 0x01); |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 79 | |
| 80 | /* Assert RTS and DTR. */ |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 81 | serial_write_reg(3, 0x04); |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 82 | |
| 83 | /* Set the divisor latch. */ |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 84 | reg = serial_read_reg(0x03); |
| 85 | serial_write_reg(reg | 0x80, 0x03); |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 86 | |
| 87 | /* Write the divisor. */ |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 88 | uint16_t divisor = 115200 / speed; |
| 89 | serial_write_reg(divisor & 0xFF, 0x00); |
| 90 | serial_write_reg(divisor >> 8, 0x01); |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 91 | |
Anton Kochkov | 93a8b27 | 2012-09-26 22:31:04 +0400 | [diff] [blame] | 92 | /* Restore the previous value of the divisor. |
| 93 | * And set 8 bits per character */ |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 94 | serial_write_reg((reg & ~0x80) | 3, 0x03); |
Nico Huber | a7c609c | 2015-09-09 13:27:09 +0200 | [diff] [blame] | 95 | #endif |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 96 | } |
| 97 | #endif |
| 98 | |
Patrick Georgi | 657a6dc | 2008-10-21 15:08:18 +0000 | [diff] [blame] | 99 | static struct console_input_driver consin = { |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 100 | .havekey = &serial_havechar, |
| 101 | .getchar = &serial_getchar |
Patrick Georgi | 657a6dc | 2008-10-21 15:08:18 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static struct console_output_driver consout = { |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 105 | .putchar = &serial_putchar |
Patrick Georgi | 657a6dc | 2008-10-21 15:08:18 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 108 | void serial_init(void) |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 109 | { |
Gabe Black | 5ab2005 | 2012-08-28 16:38:34 -0700 | [diff] [blame] | 110 | if (!lib_sysinfo.serial) |
| 111 | return; |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 112 | |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 113 | serial_is_mem_mapped = |
| 114 | (lib_sysinfo.serial->type == CB_SERIAL_TYPE_MEMORY_MAPPED); |
| 115 | |
Gabe Black | d8d4d11 | 2013-01-18 18:24:46 -0800 | [diff] [blame] | 116 | if (!serial_is_mem_mapped) { |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 117 | #if IS_ENABLED(CONFIG_LP_IO_ADDRESS_SPACE) |
Gabe Black | d8d4d11 | 2013-01-18 18:24:46 -0800 | [diff] [blame] | 118 | if ((inb(IOBASE + 0x05) == 0xFF) && |
| 119 | (inb(IOBASE + 0x06) == 0xFF)) { |
Kyösti Mälkki | 48e9eb8 | 2014-05-13 12:57:26 +0300 | [diff] [blame] | 120 | printf("IO space mapped serial not present."); |
| 121 | return; |
Gabe Black | d8d4d11 | 2013-01-18 18:24:46 -0800 | [diff] [blame] | 122 | } |
| 123 | #else |
| 124 | printf("IO space mapped serial not supported."); |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 125 | return; |
Gabe Black | d8d4d11 | 2013-01-18 18:24:46 -0800 | [diff] [blame] | 126 | #endif |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 129 | #if IS_ENABLED(CONFIG_LP_SERIAL_SET_SPEED) |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 130 | serial_hardware_init(CONFIG_LP_SERIAL_BAUD_RATE, 8, 0, 1); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 131 | #endif |
Gabe Black | 9ed8a82 | 2013-12-03 21:25:35 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void serial_console_init(void) |
| 135 | { |
| 136 | if (!lib_sysinfo.serial) |
| 137 | return; |
| 138 | |
| 139 | serial_init(); |
| 140 | |
Patrick Georgi | 657a6dc | 2008-10-21 15:08:18 +0000 | [diff] [blame] | 141 | console_add_input_driver(&consin); |
| 142 | console_add_output_driver(&consout); |
Kyösti Mälkki | 48e9eb8 | 2014-05-13 12:57:26 +0300 | [diff] [blame] | 143 | serial_hardware_is_present = 1; |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 144 | } |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 145 | |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 146 | void serial_putchar(unsigned int c) |
| 147 | { |
Kyösti Mälkki | 48e9eb8 | 2014-05-13 12:57:26 +0300 | [diff] [blame] | 148 | if (!serial_hardware_is_present) |
| 149 | return; |
Nico Huber | a7c609c | 2015-09-09 13:27:09 +0200 | [diff] [blame] | 150 | #if !IS_ENABLED(CONFIG_LP_PL011_SERIAL_CONSOLE) |
Kyösti Mälkki | 48e9eb8 | 2014-05-13 12:57:26 +0300 | [diff] [blame] | 151 | while ((serial_read_reg(0x05) & 0x20) == 0) ; |
Nico Huber | a7c609c | 2015-09-09 13:27:09 +0200 | [diff] [blame] | 152 | #endif |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 153 | serial_write_reg(c, 0x00); |
Julius Werner | 2fe505b | 2014-04-17 20:00:20 -0700 | [diff] [blame] | 154 | if (c == '\n') |
| 155 | serial_putchar('\r'); |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int serial_havechar(void) |
| 159 | { |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 160 | if (!serial_hardware_is_present) |
Gabe Black | 5ab2005 | 2012-08-28 16:38:34 -0700 | [diff] [blame] | 161 | return 0; |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 162 | return serial_read_reg(0x05) & 0x01; |
Stefan Reinauer | 4e67731 | 2011-04-16 00:13:17 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 165 | int serial_getchar(void) |
| 166 | { |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 167 | if (!serial_hardware_is_present) |
Gabe Black | 5ab2005 | 2012-08-28 16:38:34 -0700 | [diff] [blame] | 168 | return -1; |
Gabe Black | 1c11712 | 2013-01-18 18:04:44 -0800 | [diff] [blame] | 169 | while (!serial_havechar()) ; |
| 170 | return serial_read_reg(0x00); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 171 | } |