blob: 79d5a6064ad04cf3053e6b68140caa56d474a465 [file] [log] [blame]
David Brazdil3d7b88b2019-07-22 17:19:35 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "hf/io.h"
18#include "hf/mm.h"
19#include "hf/mpool.h"
20#include "hf/plat/console.h"
21
22/* clang-format off */
23
24#define GPFSEL1 IO32_C(GPIO_BASE + 0x4)
25
26#define AUX_ENABLES IO32_C(AUX_BASE + 0x4)
27#define AUX_MU_IO_REG IO32_C(AUX_BASE + 0x40)
28#define AUX_MU_IER_REG IO32_C(AUX_BASE + 0x44)
29#define AUX_MU_LCR_REG IO32_C(AUX_BASE + 0x4c)
30#define AUX_MU_MCR_REG IO32_C(AUX_BASE + 0x50)
31#define AUX_MU_LSR_REG IO32_C(AUX_BASE + 0x54)
32#define AUX_MU_CNTL_REG IO32_C(AUX_BASE + 0x60)
33#define AUX_MU_BAUD_REG IO32_C(AUX_BASE + 0x68)
34
35#define AUX_MU_LSR_TX_EMPTY (UINT32_C(1) << 5)
36#define AUX_MU_LSR_TX_IDLE (UINT32_C(1) << 6)
37
38#define MHZ_TO_HZ UINT32_C(1000000)
39
40/* clang-format on */
41
42void plat_console_init(void)
43{
44 uint32_t selector;
45
46 selector = io_read32(GPFSEL1);
47 /* Set GPIO14 to function 5. */
48 selector &= ~(7 << 12);
49 selector |= 2 << 12;
50 /* Set GPIO15 to function 5 */
51 selector &= ~(7 << 15);
52 selector |= 2 << 15;
53 io_write32(GPFSEL1, selector);
54
55 /*
56 * With 8-times oversampling, baudrate is calculated as:
57 * baudrate = system_clock_freq / (8 * (baudrate_reg + 1))
58 * Therefore:
59 * baudrate_reg = (system_clock_freq / (8 * baudrate)) -1
60 */
61 uint32_t system_clock_freq = UINT32_C(CORE_FREQ_MHZ) * MHZ_TO_HZ;
62 uint32_t oversampled_baudrate = UINT32_C(8) * UINT32_C(BAUDRATE);
63 uint32_t baudrate_reg =
64 (system_clock_freq / oversampled_baudrate) - UINT32_C(1);
65
66 /* Enable Mini UART and access to its registers. */
67 io_write32(AUX_ENABLES, 1);
68 /* Disable auto flow control and disable receiver and transmitter. */
69 io_write32(AUX_MU_CNTL_REG, 0);
70 /* Disable receive and transmit interrupts. */
71 io_write32(AUX_MU_IER_REG, 0);
72 /* Enable 8 bit mode. */
73 io_write32(AUX_MU_LCR_REG, 3);
74 /* Set RTS line to be always high. */
75 io_write32(AUX_MU_MCR_REG, 0);
76 /* Set baud rate. */
77 io_write32(AUX_MU_BAUD_REG, baudrate_reg);
78 /* Enable transmitter and receiver. */
79 io_write32(AUX_MU_CNTL_REG, 3);
80
81 memory_ordering_barrier();
82}
83
84void plat_console_mm_init(struct mm_stage1_locked stage1_locked,
85 struct mpool *ppool)
86{
87 mm_identity_map(stage1_locked, pa_init(GPIO_BASE),
88 pa_add(pa_init(GPIO_BASE), PAGE_SIZE),
89 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool);
90 mm_identity_map(stage1_locked, pa_init(AUX_BASE),
91 pa_add(pa_init(AUX_BASE), PAGE_SIZE),
92 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool);
93}
94
95void plat_console_putchar(char c)
96{
97 /* Print a carriage-return as well. */
98 if (c == '\n') {
99 plat_console_putchar('\r');
100 }
101
102 /* Wait for the transmitter to be ready to accept a byte. */
103 while ((io_read32(AUX_MU_LSR_REG) & AUX_MU_LSR_TX_EMPTY) == 0) {
104 }
105
106 /* Write data to transmitter FIFO. */
107 memory_ordering_barrier();
108 io_write32(AUX_MU_IO_REG, c);
109 memory_ordering_barrier();
110
111 /* Wait until the transmitter is no longer busy. */
112 while ((io_read32(AUX_MU_LSR_REG) & AUX_MU_LSR_TX_IDLE) == 0) {
113 }
114}