[8f9d70b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Beniamino Galvani
|
---|
| 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 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup arm32raspberrypi
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief Raspberry PI platform driver.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <arch/exception.h>
|
---|
| 37 | #include <arch/mach/raspberrypi/raspberrypi.h>
|
---|
| 38 | #include <genarch/drivers/bcm2835/pl011_uart.h>
|
---|
| 39 | #include <genarch/drivers/bcm2835/irc.h>
|
---|
| 40 | #include <genarch/drivers/bcm2835/timer.h>
|
---|
| 41 | #include <arch/mm/page.h>
|
---|
| 42 | #include <mm/page.h>
|
---|
| 43 | #include <mm/km.h>
|
---|
| 44 | #include <genarch/fb/fb.h>
|
---|
| 45 | #include <abi/fb/visuals.h>
|
---|
| 46 | #include <genarch/srln/srln.h>
|
---|
| 47 | #include <sysinfo/sysinfo.h>
|
---|
| 48 | #include <interrupt.h>
|
---|
| 49 | #include <ddi/ddi.h>
|
---|
| 50 | #include <ddi/device.h>
|
---|
| 51 |
|
---|
| 52 | #define RPI_MEMORY_START 0
|
---|
| 53 | /*
|
---|
| 54 | * TODO: size of available memory depends on hw model and
|
---|
| 55 | * bootloader configuration, we should detect it somehow.
|
---|
| 56 | * 128MB should be a safe value for now.
|
---|
| 57 | * */
|
---|
| 58 | #define RPI_MEMORY_SIZE 0x08000000
|
---|
| 59 | #define RPI_MEMORY_SKIP 0x8000
|
---|
| 60 |
|
---|
| 61 | static void raspberrypi_init(void);
|
---|
| 62 | static void raspberrypi_timer_irq_start(void);
|
---|
| 63 | static void raspberrypi_cpu_halt(void);
|
---|
| 64 | static void raspberrypi_get_memory_extents(uintptr_t *start, size_t *size);
|
---|
| 65 | static void raspberrypi_irq_exception(unsigned int exc_no, istate_t *istate);
|
---|
| 66 | static void raspberrypi_frame_init(void);
|
---|
| 67 | static void raspberrypi_output_init(void);
|
---|
| 68 | static void raspberrypi_input_init(void);
|
---|
| 69 | static size_t raspberrypi_get_irq_count(void);
|
---|
| 70 | static const char *raspberrypi_get_platform_name(void);
|
---|
| 71 |
|
---|
| 72 | static struct {
|
---|
| 73 | pl011_uart_t uart;
|
---|
| 74 | bcm2835_irc_t *irc;
|
---|
| 75 | bcm2835_timer_t *timer;
|
---|
| 76 | } raspi;
|
---|
| 77 |
|
---|
| 78 | struct arm_machine_ops raspberrypi_machine_ops = {
|
---|
| 79 | raspberrypi_init,
|
---|
| 80 | raspberrypi_timer_irq_start,
|
---|
| 81 | raspberrypi_cpu_halt,
|
---|
| 82 | raspberrypi_get_memory_extents,
|
---|
| 83 | raspberrypi_irq_exception,
|
---|
| 84 | raspberrypi_frame_init,
|
---|
| 85 | raspberrypi_output_init,
|
---|
| 86 | raspberrypi_input_init,
|
---|
| 87 | raspberrypi_get_irq_count,
|
---|
| 88 | raspberrypi_get_platform_name
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | static irq_ownership_t raspberrypi_timer_irq_claim(irq_t *irq)
|
---|
| 92 | {
|
---|
| 93 | return IRQ_ACCEPT;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | static void raspberrypi_timer_irq_handler(irq_t *irq)
|
---|
| 97 | {
|
---|
| 98 | bcm2835_timer_irq_ack(raspi.timer);
|
---|
| 99 | spinlock_unlock(&irq->lock);
|
---|
| 100 | clock();
|
---|
| 101 | spinlock_lock(&irq->lock);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static void raspberrypi_init(void)
|
---|
| 105 | {
|
---|
| 106 | /* Initialize interrupt controller */
|
---|
| 107 | raspi.irc = (void *) km_map(BCM2835_IRC_ADDR, sizeof(bcm2835_irc_t),
|
---|
| 108 | PAGE_NOT_CACHEABLE);
|
---|
| 109 | ASSERT(raspi.irc);
|
---|
| 110 | bcm2835_irc_init(raspi.irc);
|
---|
| 111 |
|
---|
| 112 | /* Initialize system timer */
|
---|
| 113 | raspi.timer = (void *) km_map(BCM2835_TIMER_ADDR,
|
---|
| 114 | sizeof(bcm2835_timer_t),
|
---|
| 115 | PAGE_NOT_CACHEABLE);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | static void raspberrypi_timer_irq_start(void)
|
---|
| 119 | {
|
---|
| 120 | /* Initialize timer IRQ */
|
---|
| 121 | static irq_t timer_irq;
|
---|
| 122 | irq_initialize(&timer_irq);
|
---|
| 123 | timer_irq.devno = device_assign_devno();
|
---|
| 124 | timer_irq.inr = BCM2835_TIMER1_IRQ;
|
---|
| 125 | timer_irq.claim = raspberrypi_timer_irq_claim;
|
---|
| 126 | timer_irq.handler = raspberrypi_timer_irq_handler;
|
---|
| 127 | irq_register(&timer_irq);
|
---|
| 128 |
|
---|
| 129 | bcm2835_irc_enable(raspi.irc, BCM2835_TIMER1_IRQ);
|
---|
| 130 | bcm2835_timer_start(raspi.timer);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | static void raspberrypi_cpu_halt(void)
|
---|
| 134 | {
|
---|
| 135 | while (1) ;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** Get extents of available memory.
|
---|
| 139 | *
|
---|
| 140 | * @param start Place to store memory start address (physical).
|
---|
| 141 | * @param size Place to store memory size.
|
---|
| 142 | */
|
---|
| 143 | static void raspberrypi_get_memory_extents(uintptr_t *start, size_t *size)
|
---|
| 144 | {
|
---|
| 145 | *start = RPI_MEMORY_START + RPI_MEMORY_SKIP;
|
---|
| 146 | *size = RPI_MEMORY_SIZE - RPI_MEMORY_SKIP;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | static void raspberrypi_irq_exception(unsigned int exc_no, istate_t *istate)
|
---|
| 150 | {
|
---|
| 151 | const unsigned inum = bcm2835_irc_inum_get(raspi.irc);
|
---|
| 152 |
|
---|
| 153 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
| 154 | if (irq) {
|
---|
| 155 | /* The IRQ handler was found. */
|
---|
| 156 | irq->handler(irq);
|
---|
| 157 | spinlock_unlock(&irq->lock);
|
---|
| 158 | } else {
|
---|
| 159 | /* Spurious interrupt.*/
|
---|
| 160 | printf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, inum);
|
---|
| 161 | bcm2835_irc_disable(raspi.irc, inum);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | static void raspberrypi_frame_init(void)
|
---|
| 166 | {
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | static void raspberrypi_output_init(void)
|
---|
| 170 | {
|
---|
| 171 | #ifdef CONFIG_BCM2835_UART
|
---|
| 172 | if (pl011_uart_init(&raspi.uart, BCM2835_UART_IRQ,
|
---|
| 173 | PL011_UART0_BASE_ADDRESS,
|
---|
| 174 | sizeof(pl011_uart_regs_t)))
|
---|
| 175 | stdout_wire(&raspi.uart.outdev);
|
---|
| 176 | #endif
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | static void raspberrypi_input_init(void)
|
---|
| 180 | {
|
---|
| 181 | srln_instance_t *srln_instance = srln_init();
|
---|
| 182 | if (srln_instance) {
|
---|
| 183 | indev_t *sink = stdin_wire();
|
---|
| 184 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
| 185 |
|
---|
| 186 | pl011_uart_input_wire(&raspi.uart, srln);
|
---|
| 187 | bcm2835_irc_enable(raspi.irc, BCM2835_UART_IRQ);
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | size_t raspberrypi_get_irq_count(void)
|
---|
| 192 | {
|
---|
| 193 | return BCM2835_IRQ_COUNT;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | const char *raspberrypi_get_platform_name(void)
|
---|
| 197 | {
|
---|
| 198 | return "raspberrypi";
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** @}
|
---|
| 202 | */
|
---|