From a8a39ba24e36119dce48a01c2139e0e5a25674e1 Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Tue, 25 Feb 2025 19:29:33 -0800
Subject: Add mailbox.

---
 src/uart.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

(limited to 'src/uart.c')

diff --git a/src/uart.c b/src/uart.c
index bb2a0ea..3b107f4 100644
--- a/src/uart.c
+++ b/src/uart.c
@@ -5,6 +5,7 @@ References:
 #include <uart.h>
 
 #include <gpio.h>
+#include <mailbox.h>
 #include <mmio.h>
 
 enum
@@ -31,15 +32,10 @@ enum
     UART0_ITIP   = (UART0_BASE + 0x84),
     UART0_ITOP   = (UART0_BASE + 0x88),
     UART0_TDR    = (UART0_BASE + 0x8C),
-    // The offsets for Mailbox registers.
-    MBOX_BASE    = 0xB880,
-    MBOX_READ    = (MBOX_BASE + 0x00),
-    MBOX_STATUS  = (MBOX_BASE + 0x18),
-    MBOX_WRITE   = (MBOX_BASE + 0x20)
 };
 
 // A mailbox message with set clock rate of PL011 to 3MHz tag.
-static volatile unsigned int  __attribute__((aligned(16))) mbox[9] = {
+static const uint32_t __attribute__((aligned(MAIL_ALIGN))) UART_SET_CLK[9] = {
   9*4, 0, 0x38002, 12, 8, 2, 3000000, 0, 0
 };
 
@@ -77,14 +73,9 @@ void uart_init(int raspi) {
   // For Raspi3 and 4 the UART_CLOCK is system-clock dependent by default.
   // Set it to 3Mhz so that we can consistently set the baud rate
   if (raspi >= 3) {
-    // UART_CLOCK = 30000000;
-    unsigned int r = (unsigned int) (((uint64_t)(&mbox) & ~0xF) | 8);
-    // Wait until we can talk to the VC.
-    while (mmio_read(MBOX_STATUS) & 0x80000000);
-    // Send our message to property channel and wait for the response.
-    mmio_write(MBOX_WRITE, r);
-    while ((mmio_read(MBOX_STATUS) & 0x40000000) ||
-           (mmio_read(MBOX_READ) != r));
+    // Send message over property channel to configure UART clock.
+    mbox_write(PROPERTY_CHANNEL, UART_SET_CLK);
+    mbox_read(PROPERTY_CHANNEL);
   }
 
   // Divider = 3000000 / (16 * 115200) = 1.627 = ~1.
-- 
cgit v1.2.3