3 * \brief ARM pl011 UART kernel-level driver.
7 * Copyright (c) 2007, 2008, ETH Zurich.
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
15 #include <arch/arm/lpuart.h>
17 #include <paging_kernel_arch.h>
18 #include <arch/arm/arm.h>
19 #include <arch/arm/platform.h>
21 #include <dev/lpuart_dev.h>
23 static lpuart_t uarts[LPUART_MAX_PORTS];
25 // Mask all interrupts in the IMSC register
26 #define INTERRUPTS_MASK 0
28 #define MSG(format, ...) printk( LOG_NOTE, "lpuart: "format, ## __VA_ARGS__ )
31 * \brief Configure the serial interface, from a caller that knows
32 * that this is a bunch of PL011s, and furthermore where they are in
33 * the physical address space.
35 errval_t serial_early_init(unsigned n)
37 assert(!paging_mmu_enabled());
38 assert(n < serial_num_physical_ports);
40 lpuart_initialize(&uarts[n],
41 (mackerel_addr_t)local_phys_to_mem(platform_uart_base[n]));
43 // Make sure that the UART is enabled and transmitting - not all platforms
45 lpuart_baud_rawwr(&uarts[n], 0x402008b);
46 lpuart_ctrl_te_wrf(&uarts[n], 1);
47 lpuart_ctrl_re_wrf(&uarts[n], 1);
53 * \brief Configure the serial interface, from a caller that knows
54 * that this is a bunch of PL011s, and furthermore where they are in
55 * the physical address space.
57 errval_t serial_early_init_mmu_enabled(unsigned n)
59 assert(paging_mmu_enabled());
60 assert(n < serial_num_physical_ports);
62 lpuart_initialize(&uarts[n],
63 (mackerel_addr_t)local_phys_to_mem(platform_uart_base[n]));
65 // Make sure that the UART is enabled and transmitting - not all platforms
67 lpuart_baud_rawwr(&uarts[n], 0x402008b);
68 lpuart_ctrl_te_wrf(&uarts[n], 1);
69 lpuart_ctrl_re_wrf(&uarts[n], 1);
74 * \brief Initialize a serial port. The MMU is turned on.
76 void lpuart_init(unsigned port, lvaddr_t base, bool hwinit)
78 assert(paging_mmu_enabled());
79 assert(port < serial_num_physical_ports);
81 lpuart_t *u = &uarts[port];
83 // [Re]initialize the Mackerel state for the UART
84 lpuart_initialize(u, (mackerel_addr_t) base);
87 lpuart_baud_rawwr(u, 0x402008b);
88 lpuart_ctrl_t ctrl = (lpuart_ctrl_t)0;
89 ctrl = lpuart_ctrl_te_insert(ctrl, 1);
90 ctrl = lpuart_ctrl_re_insert(ctrl, 1);
91 lpuart_ctrl_rawwr(u, ctrl);
93 //TODO: Figure out more advanced configuration (FIFO etc.)
98 * \brief Put a character to the port
100 void serial_putchar(unsigned port, char c)
102 assert(port < LPUART_MAX_PORTS);
103 lpuart_t *u = &uarts[port];
104 assert(u->base != 0);
106 while(lpuart_stat_tdre_rdf(u) == 0);
107 lpuart_data_buf_wrf(u, c);
111 * \brief Read a character from a port
113 char serial_getchar(unsigned port)
115 assert(port < LPUART_MAX_PORTS);
116 lpuart_t *u = &uarts[port];
117 assert(u->base != 0);
120 while(lpuart_stat_rdrf_rdf(u) == 0);
122 return (char)lpuart_data_buf_rdf(u);