Skip to content

Commit

Permalink
Decompile errorled.c, GPIO register defines
Browse files Browse the repository at this point in the history
  • Loading branch information
Thar0 committed Apr 28, 2024
1 parent ade805e commit 3f24e70
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 38 deletions.
38 changes: 34 additions & 4 deletions include/PR/bcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,40 @@
//! ?
#define PI_5C_REG (PI_BASE_REG + 0x5C)



//! PI_MISC_REG ?
#define PI_60_REG (PI_BASE_REG + 0x60)
/**
* [31:16] Box ID
* [31:30] ?? (osInitialize checks this and sets __osBbIsBb to 2 if != 0)
* [29:27] ?? (unused so far)
* [26:25] ?? (system clock speed identifier?)
* [24:22] ?? (bootrom, checked against MI_10_REG and copied there if mismatch)
* [ 7: 6] RTC mask
* [5] Error LED mask
* [4] Power Control mask
* [ 3: 2] RTC control
* [1] Error LED (1=on, 0=off)
* [0] Power Control (1=on, 0=off)
*/
#define PI_GPIO_REG (PI_BASE_REG + 0x60)

#define PI_GPIO_GET_BOXID(reg) ((reg) >> 16)
#define PI_GPIO_GET_PWR(reg) (((reg) >> 0) & 1)
#define PI_GPIO_GET_LED(reg) (((reg) >> 1) & 1)

/* Box ID masks */
#define PI_GPIO_BOXID_MASK_30_31 (3 << 30)

/* Enable masks */
#define PI_GPIO_MASK_PWR (1 << 4)
#define PI_GPIO_MASK_LED (2 << 4)
#define PI_GPIO_MASK_RTC_0 (4 << 4)
#define PI_GPIO_MASK_RTC_1 (8 << 4)
/* RTC (TODO) */
/* LED */
#define PI_GPIO_LED_ON (1 << 1)
#define PI_GPIO_LED_OFF (0 << 1)
/* Power */
#define PI_GPIO_PWR_ON (1 << 0)
#define PI_GPIO_PWR_OFF (0 << 0)



Expand Down
2 changes: 1 addition & 1 deletion src/bb/misc/boxid.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
#include "PR/bcp.h"

u32 osBbGetBoxId(void) {
return IO_READ(PI_60_REG) >> 0x10;
return PI_GPIO_GET_BOXID(IO_READ(PI_GPIO_REG));
}
14 changes: 14 additions & 0 deletions src/bb/misc/errorled.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "PR/os_internal.h"
#include "PR/bcp.h"

void osBbSetErrorLed(u32 value) {
u32 mask = IO_READ(PI_GPIO_REG);
mask &= ~PI_GPIO_LED_ON;
mask &= ~PI_GPIO_MASK_LED;
IO_WRITE(PI_GPIO_REG, mask | ((value == 0) ? PI_GPIO_LED_ON : PI_GPIO_LED_OFF) | PI_GPIO_MASK_LED);
}

u32 osBbGetErrorLed(void) {
u32 v = PI_GPIO_GET_LED(IO_READ(PI_GPIO_REG));
return v;
}
6 changes: 2 additions & 4 deletions src/bb/misc/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
#include "PR/bcp.h"

void osBbPowerOn(void) {
// Power control = 1, Power mask = 1
IO_WRITE(PI_60_REG, 0x11);
IO_WRITE(PI_GPIO_REG, PI_GPIO_MASK_PWR | PI_GPIO_PWR_ON);
}

void osBbPowerOff(void) {
// Power control = 0, Power mask = 1
IO_WRITE(PI_60_REG, 0x10);
IO_WRITE(PI_GPIO_REG, PI_GPIO_MASK_PWR | PI_GPIO_PWR_OFF);
}
56 changes: 28 additions & 28 deletions src/bb/misc/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
void __osBbDelay(u32 usec);

static void write_rtc(u32 x) {
IO_WRITE(PI_60_REG, x);
IO_WRITE(PI_GPIO_REG, x);
__osBbDelay(2);
}

static void send_start(u8 write) {
u32 i;
u32 j;
u32 mask = IO_READ(PI_60_REG) & ~0xCC;
u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC);
u8 byte[2];

byte[0] = (!write) ? 0xD1 : 0xD0;
byte[1] = 0;

write_rtc(mask | 0xC4);
write_rtc(mask | 0xC0);
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 4));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0));

for (i = 0; i < write + 1; i++) {
for (j = 0; j < 8; j++) {
u32 b = ((byte[i] >> (7 - j)) & 1) ? 8 : 0;
u32 b = ((byte[i] >> (7 - j)) & 1) ? (2 << 2) : (0 << 2);

write_rtc(mask | (0x80 | b) | 0x40);
write_rtc(mask | (0x80 | b) | 0x44);
write_rtc(mask | (0x80 | b) | 0x40);
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0));
}
write_rtc(mask | 0x40);
write_rtc(mask | 0x44);
Expand All @@ -36,61 +36,61 @@ static void send_start(u8 write) {
}

static void send_stop(void) {
u32 mask = IO_READ(PI_60_REG) & ~0xCC;
write_rtc(mask | 0x80 | 0x40);
write_rtc(mask | 0x80 | 0x44);
write_rtc(mask | 0x80 | 0x4C);
u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_1 | PI_GPIO_MASK_RTC_0 | 0xC);
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0x0));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0x4));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0xC));
}

static void read_bytes(u8* bytes, u8 len) {
u32 ack;
u32 i;
u32 mask = IO_READ(PI_60_REG) & ~0xCC;
u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC);

while (len-- > 0) {
u32 x = 0;

for (i = 0; i < 8; i++) {
write_rtc(mask | 0x40);
write_rtc(mask | 0x44);
write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0));
write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4));
x <<= 1;
x |= (IO_READ(PI_60_REG) >> 3) & 1;
x |= (IO_READ(PI_GPIO_REG) >> 3) & 1;
}
*(bytes++) = x;

ack = (len == 0) ? 0x88 : 0x80;
ack = (len == 0) ? (PI_GPIO_MASK_RTC_1 | 8) : (PI_GPIO_MASK_RTC_1 | 0);

write_rtc(mask | 0x40 | 0x80);
write_rtc(mask | 0x44 | ack);
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0));
write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4) | ack);
}
send_stop();
}

static void write_bytes(u8* bytes, u8 len) {
u32 i;
u32 mask = IO_READ(PI_60_REG) & ~0xCC;
u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC);

while (len-- > 0) {
u32 x = *(bytes++);

for (i = 0; i < 8; i++) {
u32 b = (x & 0x80) ? 8 : 0;

write_rtc(mask | (0x80 | b) | 0x40);
write_rtc(mask | (0x80 | b) | 0x44);
write_rtc(mask | (0x80 | b) | 0x44);
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4));
write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4));
x <<= 1;
}
write_rtc(mask | 0x40);
write_rtc(mask | 0x44);
IO_READ(PI_60_REG);
write_rtc(mask | 0x40);
write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0));
write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4));
IO_READ(PI_GPIO_REG);
write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0));
}
send_stop();
}

void osBbRtcInit(void) {
write_rtc(IO_READ(PI_60_REG) | 0xCC);
write_rtc(IO_READ(PI_GPIO_REG) | PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC);
}

void osBbRtcSet(u8 year, u8 month, u8 day, u8 dow, u8 hour, u8 min, u8 sec) {
Expand Down
2 changes: 1 addition & 1 deletion src/os/initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void INITIALIZE_FUNC() {
}

if (__osBbIsBb) {
if (IO_READ(PI_60_REG) & 0xC0000000) {
if (IO_READ(PI_GPIO_REG) & PI_GPIO_BOXID_MASK_30_31) {
__osBbIsBb = 2;
}
}
Expand Down

0 comments on commit 3f24e70

Please sign in to comment.