Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * misc.c |
| 3 | * |
| 4 | * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include "config.h" |
| 12 | #include "misc.h" |
| 13 | |
| 14 | |
| 15 | struct misc { |
| 16 | uint32_t warmboot; |
| 17 | uint32_t gpio; |
| 18 | uint32_t e1_led; |
| 19 | uint32_t _rsvd; |
| 20 | struct { |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 21 | uint16_t rx; |
Sylvain Munaut | c1d117b | 2020-09-15 21:57:52 +0200 | [diff] [blame] | 22 | uint16_t tx; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 23 | } e1_tick[2]; |
Sylvain Munaut | 1ac458f | 2020-09-15 22:06:00 +0200 | [diff] [blame] | 24 | struct { |
| 25 | uint32_t pps; |
| 26 | uint32_t now; |
| 27 | } time; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 28 | uint32_t pdm[8]; |
| 29 | } __attribute__((packed,aligned(4))); |
| 30 | |
| 31 | static volatile struct misc * const misc_regs = (void*)(MISC_BASE); |
| 32 | |
| 33 | |
| 34 | static const int pdm_bits[5] = { 12, 12, 8, 8, 8 }; |
| 35 | |
| 36 | |
| 37 | void |
| 38 | pdm_set(int chan, bool enable, unsigned value, bool normalize) |
| 39 | { |
| 40 | if (normalize) |
| 41 | value >>= (16 - pdm_bits[chan]); |
| 42 | if (enable) |
| 43 | value |= 0x80000000; |
| 44 | misc_regs->pdm[chan] = value; |
| 45 | } |
| 46 | |
| 47 | |
Sylvain Munaut | 5e86047 | 2020-09-15 22:20:21 +0200 | [diff] [blame] | 48 | void |
| 49 | e1_led_set(bool enable, uint8_t cfg) |
| 50 | { |
| 51 | misc_regs->e1_led = (enable ? 0x100 : 0x000) | cfg; |
| 52 | } |
| 53 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 54 | uint16_t |
| 55 | e1_tick_read(void) |
| 56 | { |
| 57 | return misc_regs->e1_tick[0].tx; |
| 58 | } |
Sylvain Munaut | 46d6b41 | 2020-10-29 13:19:05 +0100 | [diff] [blame] | 59 | |
| 60 | void |
| 61 | reboot(int fw) |
| 62 | { |
| 63 | misc_regs->warmboot = (1 << 2) | (fw << 0); |
| 64 | } |