arch: add mcu_set_clock_prescalar helper

A more advanced version would disable interrupts if they are enabled to
avoid an interrupt mid-function breaking the back-to-back instruction
requirement.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
1 files changed, 16 insertions(+), 0 deletions(-)

M arch.h
M arch.h +16 -0
@@ 41,6 41,22 @@ 
 #define mmregs ((volatile struct mmregs_layout * volatile) (void *) 0x0)
 
 /*
+ * MCU clock
+ */
+static inline void mcu_set_clock_prescalar(uint16_t scale)
+{
+	asm volatile(
+		"st Z, %1\n"
+		"st Z, %2\n"
+		: /* out */
+		: /* in */
+		  "z" (&mmregs->clkpr),
+		  "r" (REG_CLKPR_CLKPCE),
+		  "r" (mcu_clock_prescalar_to_clkps(scale))
+	);
+}
+
+/*
  * GPIO pin setting helper
  */
 #define PORTB   (offsetof(struct mmregs_layout, portb) - 0x20)