[f92976f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Matteo Facchinetti
|
---|
[4c754f6] | 3 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
[f92976f] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 | /** @addtogroup arm32beaglebone
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief BeagleBone platform driver.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <arch/exception.h>
|
---|
| 37 | #include <arch/mach/beaglebone/beaglebone.h>
|
---|
[0fb70e1] | 38 | #include <genarch/drivers/am335x/irc.h>
|
---|
| 39 | #include <genarch/drivers/am335x/uart.h>
|
---|
[6f07226] | 40 | #include <genarch/drivers/am335x/timer.h>
|
---|
[4c754f6] | 41 | #include <genarch/drivers/am335x/cm_per.h>
|
---|
| 42 | #include <genarch/drivers/am335x/cm_dpll.h>
|
---|
| 43 | #include <genarch/drivers/am335x/ctrl_module.h>
|
---|
[0fb70e1] | 44 | #include <genarch/srln/srln.h>
|
---|
[f92976f] | 45 | #include <interrupt.h>
|
---|
| 46 | #include <ddi/ddi.h>
|
---|
| 47 | #include <ddi/device.h>
|
---|
[a829a5b] | 48 | #include <mm/km.h>
|
---|
[f92976f] | 49 |
|
---|
[47bd0f8] | 50 | #define BBONE_MEMORY_START 0x80000000 /* physical */
|
---|
| 51 | #define BBONE_MEMORY_SIZE 0x10000000 /* 256 MB */
|
---|
| 52 |
|
---|
[f92976f] | 53 | static void bbone_init(void);
|
---|
| 54 | static void bbone_timer_irq_start(void);
|
---|
| 55 | static void bbone_cpu_halt(void);
|
---|
| 56 | static void bbone_get_memory_extents(uintptr_t *start, size_t *size);
|
---|
| 57 | static void bbone_irq_exception(unsigned int exc_no, istate_t *istate);
|
---|
| 58 | static void bbone_frame_init(void);
|
---|
| 59 | static void bbone_output_init(void);
|
---|
| 60 | static void bbone_input_init(void);
|
---|
| 61 | static size_t bbone_get_irq_count(void);
|
---|
| 62 | static const char *bbone_get_platform_name(void);
|
---|
| 63 |
|
---|
[a829a5b] | 64 | static struct beaglebone {
|
---|
[3c9646b] | 65 | omap_irc_regs_t *irc_addr;
|
---|
[4c754f6] | 66 | am335x_cm_per_regs_t *cm_per_addr;
|
---|
| 67 | am335x_cm_dpll_regs_t *cm_dpll_addr;
|
---|
| 68 | am335x_ctrl_module_t *ctrl_module;
|
---|
[6f07226] | 69 | am335x_timer_t timer;
|
---|
[b38c079] | 70 | omap_uart_t uart;
|
---|
[a829a5b] | 71 | } bbone;
|
---|
| 72 |
|
---|
[f92976f] | 73 | struct arm_machine_ops bbone_machine_ops = {
|
---|
[0fb70e1] | 74 | .machine_init = bbone_init,
|
---|
| 75 | .machine_timer_irq_start = bbone_timer_irq_start,
|
---|
| 76 | .machine_cpu_halt = bbone_cpu_halt,
|
---|
| 77 | .machine_get_memory_extents = bbone_get_memory_extents,
|
---|
| 78 | .machine_irq_exception = bbone_irq_exception,
|
---|
| 79 | .machine_frame_init = bbone_frame_init,
|
---|
| 80 | .machine_output_init = bbone_output_init,
|
---|
| 81 | .machine_input_init = bbone_input_init,
|
---|
| 82 | .machine_get_irq_count = bbone_get_irq_count,
|
---|
| 83 | .machine_get_platform_name = bbone_get_platform_name,
|
---|
[f92976f] | 84 | };
|
---|
| 85 |
|
---|
| 86 | static void bbone_init(void)
|
---|
| 87 | {
|
---|
[a829a5b] | 88 | bbone.irc_addr = (void *) km_map(AM335x_IRC_BASE_ADDRESS,
|
---|
| 89 | AM335x_IRC_SIZE, PAGE_NOT_CACHEABLE);
|
---|
| 90 |
|
---|
[4c754f6] | 91 | bbone.cm_per_addr = (void *) km_map(AM335x_CM_PER_BASE_ADDRESS,
|
---|
| 92 | AM335x_CM_PER_SIZE, PAGE_NOT_CACHEABLE);
|
---|
| 93 |
|
---|
| 94 | bbone.cm_dpll_addr = (void *) km_map(AM335x_CM_DPLL_BASE_ADDRESS,
|
---|
| 95 | AM335x_CM_DPLL_SIZE, PAGE_NOT_CACHEABLE);
|
---|
| 96 |
|
---|
| 97 | bbone.ctrl_module = (void *) km_map(AM335x_CTRL_MODULE_BASE_ADDRESS,
|
---|
| 98 | AM335x_CTRL_MODULE_SIZE, PAGE_NOT_CACHEABLE);
|
---|
| 99 |
|
---|
[9c56996] | 100 | ASSERT(bbone.irc_addr != NULL);
|
---|
| 101 | ASSERT(bbone.cm_per_addr != NULL);
|
---|
| 102 | ASSERT(bbone.cm_dpll_addr != NULL);
|
---|
| 103 | ASSERT(bbone.ctrl_module != NULL);
|
---|
| 104 |
|
---|
[4c754f6] | 105 | /* Initialize the interrupt controller */
|
---|
[3c9646b] | 106 | omap_irc_init(bbone.irc_addr);
|
---|
[f92976f] | 107 | }
|
---|
| 108 |
|
---|
[6f07226] | 109 | static irq_ownership_t bbone_timer_irq_claim(irq_t *irq)
|
---|
| 110 | {
|
---|
| 111 | return IRQ_ACCEPT;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | static void bbone_timer_irq_handler(irq_t *irq)
|
---|
| 115 | {
|
---|
| 116 | am335x_timer_intr_ack(&bbone.timer);
|
---|
| 117 | spinlock_unlock(&irq->lock);
|
---|
| 118 | clock();
|
---|
| 119 | spinlock_lock(&irq->lock);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[f92976f] | 122 | static void bbone_timer_irq_start(void)
|
---|
| 123 | {
|
---|
[0f66886] | 124 | unsigned sysclk_freq;
|
---|
[9c56996] | 125 | int rc;
|
---|
[0f66886] | 126 |
|
---|
[6f07226] | 127 | /* Initialize the IRQ */
|
---|
| 128 | static irq_t timer_irq;
|
---|
| 129 | irq_initialize(&timer_irq);
|
---|
| 130 | timer_irq.devno = device_assign_devno();
|
---|
[4c754f6] | 131 | timer_irq.inr = AM335x_DMTIMER2_IRQ;
|
---|
[6f07226] | 132 | timer_irq.claim = bbone_timer_irq_claim;
|
---|
| 133 | timer_irq.handler = bbone_timer_irq_handler;
|
---|
| 134 | irq_register(&timer_irq);
|
---|
| 135 |
|
---|
[4c754f6] | 136 | /* Enable the DMTIMER2 clock module */
|
---|
| 137 | am335x_clock_module_enable(bbone.cm_per_addr, DMTIMER2);
|
---|
| 138 | /* Select the SYSCLK as the clock source for the dmtimer2 module */
|
---|
| 139 | am335x_clock_source_select(bbone.cm_dpll_addr, DMTIMER2,
|
---|
| 140 | CLK_SRC_M_OSC);
|
---|
| 141 | /* Initialize the DMTIMER2 */
|
---|
[0f66886] | 142 | if (am335x_ctrl_module_clock_freq_get(bbone.ctrl_module,
|
---|
| 143 | &sysclk_freq) != EOK) {
|
---|
| 144 | printf("Cannot get the system clock frequency!\n");
|
---|
| 145 | return;
|
---|
[ddb3051] | 146 | } else
|
---|
| 147 | printf("system clock running at %u hz\n", sysclk_freq);
|
---|
[0f66886] | 148 |
|
---|
[9c56996] | 149 | rc = am335x_timer_init(&bbone.timer, DMTIMER2, HZ, sysclk_freq);
|
---|
| 150 | if (rc != EOK) {
|
---|
| 151 | printf("Timer initialization failed\n");
|
---|
| 152 | return;
|
---|
| 153 | }
|
---|
[6f07226] | 154 | /* Enable the interrupt */
|
---|
[3c9646b] | 155 | omap_irc_enable(bbone.irc_addr, AM335x_DMTIMER2_IRQ);
|
---|
[6f07226] | 156 | /* Start the timer */
|
---|
| 157 | am335x_timer_start(&bbone.timer);
|
---|
[f92976f] | 158 | }
|
---|
| 159 |
|
---|
| 160 | static void bbone_cpu_halt(void)
|
---|
| 161 | {
|
---|
[6f07226] | 162 | while (1);
|
---|
[f92976f] | 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Get extents of available memory.
|
---|
| 166 | *
|
---|
| 167 | * @param start Place to store memory start address (physical).
|
---|
| 168 | * @param size Place to store memory size.
|
---|
| 169 | */
|
---|
| 170 | static void bbone_get_memory_extents(uintptr_t *start, size_t *size)
|
---|
| 171 | {
|
---|
[47bd0f8] | 172 | *start = BBONE_MEMORY_START;
|
---|
| 173 | *size = BBONE_MEMORY_SIZE;
|
---|
[f92976f] | 174 | }
|
---|
| 175 |
|
---|
| 176 | static void bbone_irq_exception(unsigned int exc_no, istate_t *istate)
|
---|
| 177 | {
|
---|
[3c9646b] | 178 | const unsigned inum = omap_irc_inum_get(bbone.irc_addr);
|
---|
[47bd0f8] | 179 |
|
---|
| 180 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
| 181 | if (irq) {
|
---|
| 182 | /* The IRQ handler was found. */
|
---|
| 183 | irq->handler(irq);
|
---|
| 184 | spinlock_unlock(&irq->lock);
|
---|
| 185 | } else {
|
---|
| 186 | printf("Spurious interrupt\n");
|
---|
| 187 | }
|
---|
[a940f1d] | 188 |
|
---|
[3c9646b] | 189 | omap_irc_irq_ack(bbone.irc_addr);
|
---|
[f92976f] | 190 | }
|
---|
| 191 |
|
---|
| 192 | static void bbone_frame_init(void)
|
---|
| 193 | {
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | static void bbone_output_init(void)
|
---|
| 197 | {
|
---|
[69483af] | 198 | #ifdef CONFIG_OMAP_UART
|
---|
[b38c079] | 199 | const bool ok = omap_uart_init(&bbone.uart,
|
---|
[0fb70e1] | 200 | AM335x_UART0_IRQ, AM335x_UART0_BASE_ADDRESS,
|
---|
| 201 | AM335x_UART0_SIZE);
|
---|
| 202 |
|
---|
[06c4609] | 203 | if (ok)
|
---|
[0fb70e1] | 204 | stdout_wire(&bbone.uart.outdev);
|
---|
[69483af] | 205 | #endif
|
---|
[f92976f] | 206 | }
|
---|
| 207 |
|
---|
| 208 | static void bbone_input_init(void)
|
---|
| 209 | {
|
---|
[69483af] | 210 | #ifdef CONFIG_OMAP_UART
|
---|
[0fb70e1] | 211 | srln_instance_t *srln_instance = srln_init();
|
---|
| 212 | if (srln_instance) {
|
---|
| 213 | indev_t *sink = stdin_wire();
|
---|
| 214 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
[b38c079] | 215 | omap_uart_input_wire(&bbone.uart, srln);
|
---|
[3c9646b] | 216 | omap_irc_enable(bbone.irc_addr, AM335x_UART0_IRQ);
|
---|
[0fb70e1] | 217 | }
|
---|
[69483af] | 218 | #endif
|
---|
[f92976f] | 219 | }
|
---|
| 220 |
|
---|
| 221 | size_t bbone_get_irq_count(void)
|
---|
| 222 | {
|
---|
[0fb70e1] | 223 | return AM335x_IRC_IRQ_COUNT;
|
---|
[f92976f] | 224 | }
|
---|
| 225 |
|
---|
| 226 | const char *bbone_get_platform_name(void)
|
---|
| 227 | {
|
---|
| 228 | return "beaglebone";
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * @}
|
---|
| 233 | */
|
---|
| 234 |
|
---|