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" |
Harald Welte | 5276567 | 2020-12-15 18:35:42 +0100 | [diff] [blame] | 13 | #include "e1.h" |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 14 | |
| 15 | |
| 16 | struct misc { |
| 17 | uint32_t warmboot; |
| 18 | uint32_t gpio; |
| 19 | uint32_t e1_led; |
| 20 | uint32_t _rsvd; |
| 21 | struct { |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 22 | uint16_t rx; |
Sylvain Munaut | c1d117b | 2020-09-15 21:57:52 +0200 | [diff] [blame] | 23 | uint16_t tx; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 24 | } e1_tick[2]; |
Sylvain Munaut | 1ac458f | 2020-09-15 22:06:00 +0200 | [diff] [blame] | 25 | struct { |
| 26 | uint32_t pps; |
| 27 | uint32_t now; |
| 28 | } time; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 29 | uint32_t pdm[8]; |
| 30 | } __attribute__((packed,aligned(4))); |
| 31 | |
| 32 | static volatile struct misc * const misc_regs = (void*)(MISC_BASE); |
| 33 | |
| 34 | |
| 35 | static const int pdm_bits[5] = { 12, 12, 8, 8, 8 }; |
| 36 | |
| 37 | |
| 38 | void |
| 39 | pdm_set(int chan, bool enable, unsigned value, bool normalize) |
| 40 | { |
| 41 | if (normalize) |
| 42 | value >>= (16 - pdm_bits[chan]); |
| 43 | if (enable) |
| 44 | value |= 0x80000000; |
| 45 | misc_regs->pdm[chan] = value; |
| 46 | } |
| 47 | |
| 48 | |
Sylvain Munaut | 5e86047 | 2020-09-15 22:20:21 +0200 | [diff] [blame] | 49 | void |
| 50 | e1_led_set(bool enable, uint8_t cfg) |
| 51 | { |
| 52 | misc_regs->e1_led = (enable ? 0x100 : 0x000) | cfg; |
| 53 | } |
| 54 | |
Harald Welte | 5276567 | 2020-12-15 18:35:42 +0100 | [diff] [blame] | 55 | void |
Sylvain Munaut | 8f31a9e | 2022-01-03 22:09:03 +0100 | [diff] [blame] | 56 | e1_platform_led_set(int port, enum e1_platform_led led, |
Harald Welte | 5276567 | 2020-12-15 18:35:42 +0100 | [diff] [blame] | 57 | enum e1_platform_led_state state) |
| 58 | { |
| 59 | uint32_t tmp; |
| 60 | unsigned int shift; |
| 61 | |
| 62 | if (port >= 2) |
| 63 | return; |
| 64 | |
| 65 | shift = 4*port + 2*led; |
| 66 | |
| 67 | tmp = misc_regs->e1_led; |
| 68 | tmp &= ~(3 << shift); |
| 69 | tmp |= 0x100 | ((state & 3) << shift); |
| 70 | misc_regs->e1_led = tmp; |
| 71 | } |
| 72 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 73 | uint16_t |
Sylvain Munaut | 436b775 | 2022-01-03 22:09:54 +0100 | [diff] [blame] | 74 | e1_tick_read(int port) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 75 | { |
Sylvain Munaut | 436b775 | 2022-01-03 22:09:54 +0100 | [diff] [blame] | 76 | return misc_regs->e1_tick[port].tx; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 77 | } |
Sylvain Munaut | 46d6b41 | 2020-10-29 13:19:05 +0100 | [diff] [blame] | 78 | |
| 79 | void |
| 80 | reboot(int fw) |
| 81 | { |
| 82 | misc_regs->warmboot = (1 << 2) | (fw << 0); |
| 83 | } |