1 /* 2 * Copyright (c) 2014, Linaro Limited 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include <assert.h> 28 #include <drivers/pl011.h> 29 #include <io.h> 30 #include <util.h> 31 32 #define UART_DR 0x00 /* data register */ 33 #define UART_RSR_ECR 0x04 /* receive status or error clear */ 34 #define UART_DMAWM 0x08 /* DMA watermark configure */ 35 #define UART_TIMEOUT 0x0C /* Timeout period */ 36 /* reserved space */ 37 #define UART_FR 0x18 /* flag register */ 38 #define UART_ILPR 0x20 /* IrDA low-poer */ 39 #define UART_IBRD 0x24 /* integer baud register */ 40 #define UART_FBRD 0x28 /* fractional baud register */ 41 #define UART_LCR_H 0x2C /* line control register */ 42 #define UART_CR 0x30 /* control register */ 43 #define UART_IFLS 0x34 /* interrupt FIFO level select */ 44 #define UART_IMSC 0x38 /* interrupt mask set/clear */ 45 #define UART_RIS 0x3C /* raw interrupt register */ 46 #define UART_MIS 0x40 /* masked interrupt register */ 47 #define UART_ICR 0x44 /* interrupt clear register */ 48 #define UART_DMACR 0x48 /* DMA control register */ 49 50 /* flag register bits */ 51 #define UART_FR_RTXDIS (1 << 13) 52 #define UART_FR_TERI (1 << 12) 53 #define UART_FR_DDCD (1 << 11) 54 #define UART_FR_DDSR (1 << 10) 55 #define UART_FR_DCTS (1 << 9) 56 #define UART_FR_RI (1 << 8) 57 #define UART_FR_TXFE (1 << 7) 58 #define UART_FR_RXFF (1 << 6) 59 #define UART_FR_TXFF (1 << 5) 60 #define UART_FR_RXFE (1 << 4) 61 #define UART_FR_BUSY (1 << 3) 62 #define UART_FR_DCD (1 << 2) 63 #define UART_FR_DSR (1 << 1) 64 #define UART_FR_CTS (1 << 0) 65 66 /* transmit/receive line register bits */ 67 #define UART_LCRH_SPS (1 << 7) 68 #define UART_LCRH_WLEN_8 (3 << 5) 69 #define UART_LCRH_WLEN_7 (2 << 5) 70 #define UART_LCRH_WLEN_6 (1 << 5) 71 #define UART_LCRH_WLEN_5 (0 << 5) 72 #define UART_LCRH_FEN (1 << 4) 73 #define UART_LCRH_STP2 (1 << 3) 74 #define UART_LCRH_EPS (1 << 2) 75 #define UART_LCRH_PEN (1 << 1) 76 #define UART_LCRH_BRK (1 << 0) 77 78 /* control register bits */ 79 #define UART_CR_CTSEN (1 << 15) 80 #define UART_CR_RTSEN (1 << 14) 81 #define UART_CR_OUT2 (1 << 13) 82 #define UART_CR_OUT1 (1 << 12) 83 #define UART_CR_RTS (1 << 11) 84 #define UART_CR_DTR (1 << 10) 85 #define UART_CR_RXE (1 << 9) 86 #define UART_CR_TXE (1 << 8) 87 #define UART_CR_LPE (1 << 7) 88 #define UART_CR_OVSFACT (1 << 3) 89 #define UART_CR_UARTEN (1 << 0) 90 91 #define UART_IMSC_RTIM (1 << 6) 92 #define UART_IMSC_RXIM (1 << 4) 93 94 static vaddr_t chip_to_base(struct serial_chip *chip) 95 { 96 struct pl011_data *pd = 97 container_of(chip, struct pl011_data, chip); 98 99 return io_pa_or_va(&pd->base); 100 } 101 102 static void pl011_flush(struct serial_chip *chip) 103 { 104 vaddr_t base = chip_to_base(chip); 105 106 while (!(read32(base + UART_FR) & UART_FR_TXFE)) 107 ; 108 } 109 110 static bool pl011_have_rx_data(struct serial_chip *chip) 111 { 112 vaddr_t base = chip_to_base(chip); 113 114 return !(read32(base + UART_FR) & UART_FR_RXFE); 115 } 116 117 static int pl011_getchar(struct serial_chip *chip) 118 { 119 vaddr_t base = chip_to_base(chip); 120 121 while (!pl011_have_rx_data(chip)) 122 ; 123 return read32(base + UART_DR) & 0xff; 124 } 125 126 static void pl011_putc(struct serial_chip *chip, int ch) 127 { 128 vaddr_t base = chip_to_base(chip); 129 130 /* Wait until there is space in the FIFO */ 131 while (read32(base + UART_FR) & UART_FR_TXFF) 132 ; 133 134 /* Send the character */ 135 write32(ch, base + UART_DR); 136 } 137 138 static const struct serial_ops pl011_ops = { 139 .flush = pl011_flush, 140 .getchar = pl011_getchar, 141 .have_rx_data = pl011_have_rx_data, 142 .putc = pl011_putc, 143 }; 144 145 void pl011_init(struct pl011_data *pd, paddr_t base, uint32_t uart_clk, 146 uint32_t baud_rate) 147 { 148 pd->base.pa = base; 149 pd->chip.ops = &pl011_ops; 150 151 /* Clear all errors */ 152 write32(0, base + UART_RSR_ECR); 153 /* Disable everything */ 154 write32(0, base + UART_CR); 155 156 if (baud_rate) { 157 uint32_t divisor = (uart_clk * 4) / baud_rate; 158 159 write32(divisor >> 6, base + UART_IBRD); 160 write32(divisor & 0x3f, base + UART_FBRD); 161 } 162 163 /* Configure TX to 8 bits, 1 stop bit, no parity, fifo disabled. */ 164 write32(UART_LCRH_WLEN_8, base + UART_LCR_H); 165 166 /* Enable interrupts for receive and receive timeout */ 167 write32(UART_IMSC_RXIM | UART_IMSC_RTIM, base + UART_IMSC); 168 169 /* Enable UART and RX/TX */ 170 write32(UART_CR_UARTEN | UART_CR_TXE | UART_CR_RXE, base + UART_CR); 171 172 pl011_flush(&pd->chip); 173 } 174 175