summaryrefslogtreecommitdiff
path: root/src/mmio.c
blob: e082d663075d1b08e49eb91f497af42ba9ae65ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
References:
  https://wiki.osdev.org/Raspberry_Pi_Bare_Bones
  https://jsandler18.github.io/extra/peripheral.html
  https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
*/
#include <mmio.h>

// Peripheral base address.
static void* MMIO_BASE;

void mmio_init(int raspi) {
  switch (raspi) {
    case 2:
    case 3:  MMIO_BASE = (void*)0x3F000000; break; // raspi2 & 3
    case 4:  MMIO_BASE = (void*)0xFE000000; break; // raspi4
    default: MMIO_BASE = (void*)0x20000000; break; // raspi1, raspi zero, etc.
  }
}

// All MMIO registers are 32-bit.

#define REG_ADDR(reg) ((volatile uint32_t*)(MMIO_BASE + reg))

uint32_t mmio_read(uint32_t reg) {
  return *REG_ADDR(reg);
}

void mmio_write(uint32_t reg, uint32_t val) {
  *REG_ADDR(reg) = val;
}