|
20 | 20 | #include <heap.h> |
21 | 21 | #include <limits.h> |
22 | 22 | #include <memory.h> |
| 23 | +#include <mutex.h> |
| 24 | +#include <process.h> |
| 25 | +#include <signal.h> |
23 | 26 |
|
24 | 27 | #include <device/device.h> |
25 | 28 | #include <device/driver.h> |
| 29 | +#include <device/irq.h> |
26 | 30 |
|
27 | 31 | #include <device/serial.h> |
28 | 32 |
|
|
31 | 35 |
|
32 | 36 | #include <asm/io.h> /* ioread/iowrite macros */ |
33 | 37 |
|
| 38 | +#define SERIAL_BUFFER_SIZE 80 |
| 39 | + |
34 | 40 | void *__uart_vaddr = (void *) CONFIG_UART_LL_PADDR; |
35 | 41 |
|
| 42 | +static volatile char serial_buffer[SERIAL_BUFFER_SIZE]; |
| 43 | +static volatile uint32_t prod = 0, cons = 0; |
| 44 | + |
| 45 | +extern mutex_t read_lock; |
| 46 | + |
36 | 47 | typedef struct { |
37 | 48 | addr_t base; |
38 | | - bcm283x_mu_t *dev; |
| 49 | + irq_def_t irq_def; |
| 50 | + bool irq_enabled; |
39 | 51 | } bcm283x_mu_dev_t; |
40 | 52 |
|
41 | 53 | static bcm283x_mu_dev_t bcm283x_mu_dev = { |
@@ -75,40 +87,162 @@ void __ll_put_byte(char c) |
75 | 87 | static char bcm283x_mu_get_byte(bool polling) |
76 | 88 | { |
77 | 89 | bcm283x_mu_t *bcm283x_mu = (bcm283x_mu_t *) bcm283x_mu_dev.base; |
| 90 | + char tmp; |
| 91 | + |
| 92 | + /* Without an RX interrupt there is no producer filling the ring, so the |
| 93 | + * only thing we can do is spin on the FIFO — the behaviour this driver |
| 94 | + * had before interrupts were wired up. |
| 95 | + */ |
| 96 | + if (polling || !bcm283x_mu_dev.irq_enabled) { |
| 97 | + while (!(ioread32(&bcm283x_mu->lsr) & UART_LSR_RX_READY)) |
| 98 | + ; |
78 | 99 |
|
79 | | - while (!(ioread32(&bcm283x_mu->lsr) & UART_LSR_RX_READY)) |
80 | | - ; |
| 100 | + return (char) ioread32(&bcm283x_mu->io); |
| 101 | + } |
| 102 | + |
| 103 | + while (prod == cons) { |
| 104 | + /* Ctrl-C arrived while we were blocked reading: abandon the |
| 105 | + * read and report ETX so console_getc cancels the line. */ |
| 106 | + if (serial_intr) { |
| 107 | + serial_intr = false; |
| 108 | + return 3; /* ETX */ |
| 109 | + } |
| 110 | + |
| 111 | + schedule(); |
| 112 | + |
| 113 | + smp_mb(); |
| 114 | + wfi(); |
| 115 | + } |
| 116 | + |
| 117 | + tmp = serial_buffer[cons]; |
| 118 | + cons = (cons + 1) % SERIAL_BUFFER_SIZE; |
| 119 | + |
| 120 | + return tmp; |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | + * Drain the RX FIFO into the serial buffer. Reading MU_IO is what clears the |
| 125 | + * interrupt, so we loop on LSR rather than decoding IIR — that also sidesteps |
| 126 | + * the mini UART's muddled IIR documentation. |
| 127 | + * |
| 128 | + * Ctrl-C handling mirrors pl011_int(): if a thread holds read_lock it is |
| 129 | + * blocked reading the console (e.g. the shell), so we only cancel its line |
| 130 | + * instead of killing it; otherwise a foreground app is running and gets SIGINT. |
| 131 | + */ |
| 132 | +static irq_return_t bcm283x_mu_int(int irq, void *dummy) |
| 133 | +{ |
| 134 | + bcm283x_mu_t *bcm283x_mu = (bcm283x_mu_t *) bcm283x_mu_dev.base; |
| 135 | + char c; |
| 136 | + |
| 137 | + while (ioread32(&bcm283x_mu->lsr) & UART_LSR_RX_READY) { |
| 138 | + c = (char) ioread32(&bcm283x_mu->io); |
| 139 | + |
| 140 | + /* Check for SIGINT to be raised on Ctrl^C */ |
| 141 | + if (c == 3) { |
| 142 | + bcm283x_mu_put_byte('^'); |
| 143 | + bcm283x_mu_put_byte('C'); |
| 144 | + bcm283x_mu_put_byte('\n'); |
| 145 | + |
| 146 | +#ifdef CONFIG_IPC_SIGNAL |
| 147 | + if (mutex_is_locked(&read_lock)) { |
| 148 | + serial_intr = true; |
| 149 | + } else { |
| 150 | + /* Use fg_pcb (the shell's foreground job), NOT |
| 151 | + * current(): in IRQ context current() is just the |
| 152 | + * thread running when the key arrived (usually the |
| 153 | + * idle thread, since the foreground app is typically |
| 154 | + * asleep). Fall back to current() if no foreground is |
| 155 | + * tracked yet. */ |
| 156 | + pcb_t *target = fg_pcb ? fg_pcb : current()->pcb; |
| 157 | + if (target != NULL) |
| 158 | + sys_do_kill(target->pid, SIGINT); |
| 159 | + } |
| 160 | +#endif |
| 161 | + /* Already echoed above; keep it out of the ring. */ |
| 162 | + continue; |
| 163 | + } |
81 | 164 |
|
82 | | - return (char) ioread32(&bcm283x_mu->io); |
| 165 | + serial_buffer[prod] = c; |
| 166 | + prod = (prod + 1) % SERIAL_BUFFER_SIZE; |
| 167 | + } |
| 168 | + |
| 169 | + return IRQ_COMPLETED; |
| 170 | +} |
| 171 | + |
| 172 | +static void bcm283x_mu_enable_irq(void) |
| 173 | +{ |
| 174 | + irq_ops.enable(bcm283x_mu_dev.irq_def.irqnr); |
| 175 | +} |
| 176 | + |
| 177 | +static void bcm283x_mu_disable_irq(void) |
| 178 | +{ |
| 179 | + irq_ops.disable(bcm283x_mu_dev.irq_def.irqnr); |
83 | 180 | } |
84 | 181 |
|
85 | 182 | static int bcm283x_mu_init(dev_t *dev, int fdt_offset) |
86 | 183 | { |
87 | 184 | const struct fdt_property *prop; |
88 | 185 | int prop_len; |
| 186 | + addr_t new_base_vaddr; |
| 187 | + bcm283x_mu_t *bcm283x_mu; |
89 | 188 |
|
90 | 189 | /* Pins multiplexing skipped here for simplicity (done by bootloader) */ |
91 | 190 | /* Clocks init skipped here for simplicity (done by bootloader) */ |
92 | 191 |
|
93 | | - /* Initialize UART controller */ |
94 | | - memset(&bcm283x_mu_dev, 0, sizeof(bcm283x_mu_dev_t)); |
95 | | - |
96 | 192 | prop = fdt_get_property(__fdt_addr, fdt_offset, "reg", &prop_len); |
97 | 193 | BUG_ON(!prop); |
98 | 194 |
|
99 | 195 | BUG_ON(prop_len != 2 * sizeof(unsigned long)); |
100 | 196 |
|
| 197 | + /* Keep the boot-time UART base usable until io_map() has returned, so |
| 198 | + * that io_map() itself can still print. */ |
101 | 199 | #ifdef CONFIG_ARCH_ARM32 |
102 | | - bcm283x_mu_dev.base = |
| 200 | + new_base_vaddr = |
103 | 201 | io_map(fdt32_to_cpu(((const fdt32_t *) prop->data)[0]), fdt32_to_cpu(((const fdt32_t *) prop->data)[1])); |
104 | 202 | #else |
105 | | - bcm283x_mu_dev.base = |
| 203 | + new_base_vaddr = |
106 | 204 | io_map(fdt64_to_cpu(((const fdt64_t *) prop->data)[0]), fdt64_to_cpu(((const fdt64_t *) prop->data)[1])); |
107 | 205 | #endif |
| 206 | + BUG_ON(!new_base_vaddr); |
| 207 | + |
| 208 | + /* Initialize UART controller */ |
| 209 | + memset(&bcm283x_mu_dev, 0, sizeof(bcm283x_mu_dev_t)); |
| 210 | + bcm283x_mu_dev.base = new_base_vaddr; |
108 | 211 |
|
109 | 212 | serial_ops.put_byte = bcm283x_mu_put_byte; |
110 | 213 | serial_ops.get_byte = bcm283x_mu_get_byte; |
111 | 214 |
|
| 215 | + /* The interrupt is optional: fdt_interrupt_node() would BUG_ON a |
| 216 | + * missing "interrupts", and a device tree that does not declare one |
| 217 | + * simply keeps the polled console this driver started out with. Ctrl-C |
| 218 | + * then cannot work, since nothing scans the RX line asynchronously. |
| 219 | + */ |
| 220 | + prop = fdt_get_property(__fdt_addr, fdt_offset, "interrupts", &prop_len); |
| 221 | + if (!prop) |
| 222 | + return 0; |
| 223 | + |
| 224 | + fdt_interrupt_node(fdt_offset, &bcm283x_mu_dev.irq_def); |
| 225 | + |
| 226 | +#ifndef CONFIG_AVZ |
| 227 | + /* Same reasoning as pl011_init(): under AVZ the RX interrupt belongs to |
| 228 | + * the agency guest, whose own console driver attaches a handler. Binding |
| 229 | + * one here would let gic_handle() consume and EOI the IRQ before the |
| 230 | + * guest ever sees it. AVZ only needs put_byte for output. |
| 231 | + */ |
| 232 | + irq_bind(bcm283x_mu_dev.irq_def.irqnr, bcm283x_mu_int, NULL, NULL); |
| 233 | +#endif |
| 234 | + |
| 235 | + serial_ops.enable_irq = bcm283x_mu_enable_irq; |
| 236 | + serial_ops.disable_irq = bcm283x_mu_disable_irq; |
| 237 | + |
| 238 | + /* Enable RX interrupts at the controller, then at the GIC. */ |
| 239 | + bcm283x_mu = (bcm283x_mu_t *) bcm283x_mu_dev.base; |
| 240 | + iowrite32(&bcm283x_mu->ier, UART_IER_RX_ENABLE); |
| 241 | + |
| 242 | + serial_ops.enable_irq(); |
| 243 | + |
| 244 | + bcm283x_mu_dev.irq_enabled = true; |
| 245 | + |
112 | 246 | return 0; |
113 | 247 | } |
114 | 248 |
|
|
0 commit comments