[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>
|
---|
[c882505] | 38 | #include <genarch/drivers/pl011/pl011.h>
|
---|
[8f9d70b] | 39 | #include <genarch/drivers/bcm2835/irc.h>
|
---|
| 40 | #include <genarch/drivers/bcm2835/timer.h>
|
---|
[44b2b78] | 41 | #include <genarch/drivers/bcm2835/mbox.h>
|
---|
[8f9d70b] | 42 | #include <arch/mm/page.h>
|
---|
| 43 | #include <mm/page.h>
|
---|
| 44 | #include <mm/km.h>
|
---|
| 45 | #include <genarch/fb/fb.h>
|
---|
| 46 | #include <abi/fb/visuals.h>
|
---|
| 47 | #include <genarch/srln/srln.h>
|
---|
| 48 | #include <sysinfo/sysinfo.h>
|
---|
| 49 | #include <interrupt.h>
|
---|
| 50 | #include <ddi/ddi.h>
|
---|
| 51 | #include <ddi/device.h>
|
---|
| 52 |
|
---|
[44b2b78] | 53 | #define RPI_DEFAULT_MEMORY_START 0
|
---|
| 54 | #define RPI_DEFAULT_MEMORY_SIZE 0x08000000
|
---|
| 55 | #define RPI_MEMORY_SKIP 0x8000
|
---|
[8f9d70b] | 56 |
|
---|
| 57 | static void raspberrypi_init(void);
|
---|
| 58 | static void raspberrypi_timer_irq_start(void);
|
---|
| 59 | static void raspberrypi_cpu_halt(void);
|
---|
| 60 | static void raspberrypi_get_memory_extents(uintptr_t *start, size_t *size);
|
---|
| 61 | static void raspberrypi_irq_exception(unsigned int exc_no, istate_t *istate);
|
---|
| 62 | static void raspberrypi_frame_init(void);
|
---|
| 63 | static void raspberrypi_output_init(void);
|
---|
| 64 | static void raspberrypi_input_init(void);
|
---|
| 65 | static size_t raspberrypi_get_irq_count(void);
|
---|
| 66 | static const char *raspberrypi_get_platform_name(void);
|
---|
| 67 |
|
---|
| 68 | static struct {
|
---|
| 69 | pl011_uart_t uart;
|
---|
| 70 | bcm2835_irc_t *irc;
|
---|
| 71 | bcm2835_timer_t *timer;
|
---|
| 72 | } raspi;
|
---|
| 73 |
|
---|
| 74 | struct arm_machine_ops raspberrypi_machine_ops = {
|
---|
| 75 | raspberrypi_init,
|
---|
| 76 | raspberrypi_timer_irq_start,
|
---|
| 77 | raspberrypi_cpu_halt,
|
---|
| 78 | raspberrypi_get_memory_extents,
|
---|
| 79 | raspberrypi_irq_exception,
|
---|
| 80 | raspberrypi_frame_init,
|
---|
| 81 | raspberrypi_output_init,
|
---|
| 82 | raspberrypi_input_init,
|
---|
| 83 | raspberrypi_get_irq_count,
|
---|
| 84 | raspberrypi_get_platform_name
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 | static irq_ownership_t raspberrypi_timer_irq_claim(irq_t *irq)
|
---|
| 88 | {
|
---|
| 89 | return IRQ_ACCEPT;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | static void raspberrypi_timer_irq_handler(irq_t *irq)
|
---|
| 93 | {
|
---|
| 94 | bcm2835_timer_irq_ack(raspi.timer);
|
---|
| 95 | spinlock_unlock(&irq->lock);
|
---|
| 96 | clock();
|
---|
| 97 | spinlock_lock(&irq->lock);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | static void raspberrypi_init(void)
|
---|
| 101 | {
|
---|
| 102 | /* Initialize interrupt controller */
|
---|
| 103 | raspi.irc = (void *) km_map(BCM2835_IRC_ADDR, sizeof(bcm2835_irc_t),
|
---|
| 104 | PAGE_NOT_CACHEABLE);
|
---|
| 105 | ASSERT(raspi.irc);
|
---|
| 106 | bcm2835_irc_init(raspi.irc);
|
---|
| 107 |
|
---|
| 108 | /* Initialize system timer */
|
---|
| 109 | raspi.timer = (void *) km_map(BCM2835_TIMER_ADDR,
|
---|
| 110 | sizeof(bcm2835_timer_t),
|
---|
| 111 | PAGE_NOT_CACHEABLE);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | static void raspberrypi_timer_irq_start(void)
|
---|
| 115 | {
|
---|
| 116 | /* Initialize timer IRQ */
|
---|
| 117 | static irq_t timer_irq;
|
---|
| 118 | irq_initialize(&timer_irq);
|
---|
| 119 | timer_irq.devno = device_assign_devno();
|
---|
| 120 | timer_irq.inr = BCM2835_TIMER1_IRQ;
|
---|
| 121 | timer_irq.claim = raspberrypi_timer_irq_claim;
|
---|
| 122 | timer_irq.handler = raspberrypi_timer_irq_handler;
|
---|
| 123 | irq_register(&timer_irq);
|
---|
| 124 |
|
---|
| 125 | bcm2835_irc_enable(raspi.irc, BCM2835_TIMER1_IRQ);
|
---|
| 126 | bcm2835_timer_start(raspi.timer);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | static void raspberrypi_cpu_halt(void)
|
---|
| 130 | {
|
---|
| 131 | while (1) ;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /** Get extents of available memory.
|
---|
| 135 | *
|
---|
| 136 | * @param start Place to store memory start address (physical).
|
---|
| 137 | * @param size Place to store memory size.
|
---|
| 138 | */
|
---|
| 139 | static void raspberrypi_get_memory_extents(uintptr_t *start, size_t *size)
|
---|
| 140 | {
|
---|
[44b2b78] | 141 | uint32_t mbase, msize;
|
---|
| 142 |
|
---|
| 143 | if (bcm2835_prop_get_memory(&mbase, &msize)) {
|
---|
| 144 | *start = mbase + RPI_MEMORY_SKIP;
|
---|
| 145 | *size = msize - RPI_MEMORY_SKIP;
|
---|
| 146 | } else {
|
---|
| 147 | /* Stick to safe default values */
|
---|
| 148 | *start = RPI_DEFAULT_MEMORY_START + RPI_MEMORY_SKIP;
|
---|
| 149 | *size = RPI_DEFAULT_MEMORY_SIZE - RPI_MEMORY_SKIP;
|
---|
| 150 | }
|
---|
[8f9d70b] | 151 | }
|
---|
| 152 |
|
---|
| 153 | static void raspberrypi_irq_exception(unsigned int exc_no, istate_t *istate)
|
---|
| 154 | {
|
---|
| 155 | const unsigned inum = bcm2835_irc_inum_get(raspi.irc);
|
---|
| 156 |
|
---|
| 157 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
| 158 | if (irq) {
|
---|
| 159 | /* The IRQ handler was found. */
|
---|
| 160 | irq->handler(irq);
|
---|
| 161 | spinlock_unlock(&irq->lock);
|
---|
| 162 | } else {
|
---|
| 163 | /* Spurious interrupt.*/
|
---|
| 164 | printf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, inum);
|
---|
| 165 | bcm2835_irc_disable(raspi.irc, inum);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | static void raspberrypi_frame_init(void)
|
---|
| 170 | {
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | static void raspberrypi_output_init(void)
|
---|
| 174 | {
|
---|
[c882505] | 175 | #ifdef CONFIG_PL011_UART
|
---|
[8f9d70b] | 176 | if (pl011_uart_init(&raspi.uart, BCM2835_UART_IRQ,
|
---|
[c882505] | 177 | BCM2835_UART0_BASE_ADDRESS))
|
---|
[8f9d70b] | 178 | stdout_wire(&raspi.uart.outdev);
|
---|
| 179 | #endif
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | static void raspberrypi_input_init(void)
|
---|
| 183 | {
|
---|
| 184 | srln_instance_t *srln_instance = srln_init();
|
---|
| 185 | if (srln_instance) {
|
---|
| 186 | indev_t *sink = stdin_wire();
|
---|
| 187 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
| 188 |
|
---|
| 189 | pl011_uart_input_wire(&raspi.uart, srln);
|
---|
| 190 | bcm2835_irc_enable(raspi.irc, BCM2835_UART_IRQ);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | size_t raspberrypi_get_irq_count(void)
|
---|
| 195 | {
|
---|
| 196 | return BCM2835_IRQ_COUNT;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | const char *raspberrypi_get_platform_name(void)
|
---|
| 200 | {
|
---|
| 201 | return "raspberrypi";
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /** @}
|
---|
| 205 | */
|
---|