1/*- 2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org> 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 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* Simple UART console driver for Allwinner A10 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD$"); 31 32#include <sys/types.h> 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/cons.h> 36#include <sys/consio.h> 37#include <sys/kernel.h> 38 39#ifndef A10_UART_BASE 40#define A10_UART_BASE 0xe1c28000 /* UART0 */ 41#endif 42 43#define REG_SHIFT 2 44 45#define UART_DLL 0 /* Out: Divisor Latch Low */ 46#define UART_DLM 1 /* Out: Divisor Latch High */ 47#define UART_FCR 2 /* Out: FIFO Control Register */ 48#define UART_LCR 3 /* Out: Line Control Register */ 49#define UART_MCR 4 /* Out: Modem Control Register */ 50#define UART_LSR 5 /* In: Line Status Register */ 51#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ 52#define UART_LSR_DR 0x01 /* Receiver data ready */ 53#define UART_MSR 6 /* In: Modem Status Register */ 54#define UART_SCR 7 /* I/O: Scratch Register */ 55 56static uint32_t 57uart_getreg(uint32_t *bas) 58{ 59 return *((volatile uint32_t *)(bas)) & 0xff; 60} 61 62static void 63uart_setreg(uint32_t *bas, uint32_t val) 64{ 65 *((volatile uint32_t *)(bas)) = val; 66} 67 68static int 69ub_getc(void) 70{ 71 while ((uart_getreg((uint32_t *)(A10_UART_BASE + 72 (UART_LSR << REG_SHIFT))) & UART_LSR_DR) == 0); 73 __asm __volatile("nop"); 74 75 return (uart_getreg((uint32_t *)A10_UART_BASE) & 0xff); 76} 77 78static void 79ub_putc(unsigned char c) 80{ 81 if (c == '\n') 82 ub_putc('\r'); 83 84 while ((uart_getreg((uint32_t *)(A10_UART_BASE + 85 (UART_LSR << REG_SHIFT))) & UART_LSR_THRE) == 0) 86 __asm __volatile("nop"); 87 88 uart_setreg((uint32_t *)A10_UART_BASE, c); 89} 90 91static cn_probe_t uart_cnprobe; 92static cn_init_t uart_cninit; 93static cn_term_t uart_cnterm; 94static cn_getc_t uart_cngetc; 95static cn_putc_t uart_cnputc; 96static cn_grab_t uart_cngrab; 97static cn_ungrab_t uart_cnungrab; 98 99static void 100uart_cngrab(struct consdev *cp) 101{ 102} 103 104static void 105uart_cnungrab(struct consdev *cp) 106{ 107} 108 109 110static void 111uart_cnprobe(struct consdev *cp) 112{ 113 sprintf(cp->cn_name, "uart"); 114 cp->cn_pri = CN_NORMAL; 115} 116 117static void 118uart_cninit(struct consdev *cp) 119{ 120 uart_setreg((uint32_t *)(A10_UART_BASE + 121 (UART_FCR << REG_SHIFT)), 0x06); 122} 123 124void 125uart_cnputc(struct consdev *cp, int c) 126{ 127 ub_putc(c); 128} 129 130int 131uart_cngetc(struct consdev * cp) 132{ 133 return ub_getc(); 134} 135 136static void 137uart_cnterm(struct consdev * cp) 138{ 139} 140 141CONSOLE_DRIVER(uart); 142 143