paul@160 | 1 | /* |
paul@160 | 2 | * CPM (clock and power management) support for the X1600. |
paul@160 | 3 | * |
paul@160 | 4 | * Copyright (C) 2017, 2018, 2020, 2021, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@160 | 5 | * |
paul@160 | 6 | * This program is free software; you can redistribute it and/or |
paul@160 | 7 | * modify it under the terms of the GNU General Public License as |
paul@160 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@160 | 9 | * the License, or (at your option) any later version. |
paul@160 | 10 | * |
paul@160 | 11 | * This program is distributed in the hope that it will be useful, |
paul@160 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@160 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@160 | 14 | * GNU General Public License for more details. |
paul@160 | 15 | * |
paul@160 | 16 | * You should have received a copy of the GNU General Public License |
paul@160 | 17 | * along with this program; if not, write to the Free Software |
paul@160 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@160 | 19 | * Boston, MA 02110-1301, USA |
paul@160 | 20 | */ |
paul@160 | 21 | |
paul@160 | 22 | #pragma once |
paul@160 | 23 | |
paul@160 | 24 | #include <l4/devices/cpm.h> |
paul@160 | 25 | |
paul@160 | 26 | #include <l4/sys/types.h> |
paul@160 | 27 | #include <stdint.h> |
paul@160 | 28 | |
paul@160 | 29 | |
paul@160 | 30 | |
paul@160 | 31 | #ifdef __cplusplus |
paul@160 | 32 | |
paul@160 | 33 | #include <l4/devices/hw_register_block.h> |
paul@160 | 34 | |
paul@161 | 35 | enum Clock_input_identifiers |
paul@161 | 36 | { |
paul@161 | 37 | Clock_input_ahb2_apb, |
paul@161 | 38 | Clock_input_external, |
paul@161 | 39 | Clock_input_main, |
paul@161 | 40 | Clock_input_none, |
paul@161 | 41 | Clock_input_pll_A, |
paul@161 | 42 | Clock_input_pll_E, |
paul@161 | 43 | Clock_input_pll_M, |
paul@161 | 44 | Clock_input_identifier_count, /* not a clock: limit for array definition */ |
paul@161 | 45 | }; |
paul@161 | 46 | |
paul@161 | 47 | |
paul@161 | 48 | |
paul@160 | 49 | /* A simple abstraction for accessing the CPM registers. |
paul@160 | 50 | * A proper device could inherit from Hw::Device and use an |
paul@160 | 51 | * Int_property for _exclk_freq. */ |
paul@160 | 52 | |
paul@160 | 53 | class Cpm_x1600_chip : public Cpm_chip |
paul@160 | 54 | { |
paul@160 | 55 | private: |
paul@160 | 56 | Hw::Register_block<32> _regs; |
paul@160 | 57 | uint32_t _exclk_freq; |
paul@160 | 58 | |
paul@160 | 59 | // Utility methods. |
paul@160 | 60 | |
paul@160 | 61 | uint32_t get_field(uint32_t reg, uint32_t mask, uint8_t shift); |
paul@160 | 62 | void set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value); |
paul@160 | 63 | |
paul@160 | 64 | // PLL control. |
paul@160 | 65 | |
paul@160 | 66 | int have_pll(uint32_t pll_reg); |
paul@160 | 67 | int pll_enabled(uint32_t pll_reg); |
paul@160 | 68 | int pll_bypassed(uint32_t pll_reg); |
paul@160 | 69 | void pll_disable(uint32_t pll_reg); |
paul@160 | 70 | void pll_enable(uint32_t pll_reg); |
paul@160 | 71 | |
paul@160 | 72 | // General frequency modifiers. |
paul@160 | 73 | |
paul@160 | 74 | uint16_t get_multiplier(uint32_t pll_reg); |
paul@160 | 75 | void set_multiplier(uint32_t pll_reg, uint16_t multiplier); |
paul@160 | 76 | uint8_t get_input_division(uint32_t pll_reg); |
paul@160 | 77 | void set_input_division(uint32_t pll_reg, uint8_t divider); |
paul@160 | 78 | uint8_t get_output_division(uint32_t pll_reg); |
paul@160 | 79 | void set_output_division(uint32_t pll_reg, uint8_t divider); |
paul@160 | 80 | |
paul@160 | 81 | // Input frequencies. |
paul@160 | 82 | |
paul@160 | 83 | uint32_t get_pll_frequency(uint32_t pll_reg); |
paul@161 | 84 | uint32_t get_input_frequency(enum Clock_input_identifiers clock); |
paul@160 | 85 | |
paul@160 | 86 | // Clock control. |
paul@160 | 87 | |
paul@161 | 88 | void change_disable(enum Clock_identifiers clock); |
paul@161 | 89 | void change_enable(enum Clock_identifiers clock); |
paul@161 | 90 | void wait_busy(enum Clock_identifiers clock); |
paul@160 | 91 | |
paul@160 | 92 | public: |
paul@160 | 93 | Cpm_x1600_chip(l4_addr_t addr, uint32_t exclk_freq); |
paul@160 | 94 | |
paul@160 | 95 | int have_clock(enum Clock_identifiers clock); |
paul@160 | 96 | void start_clock(enum Clock_identifiers clock); |
paul@160 | 97 | void stop_clock(enum Clock_identifiers clock); |
paul@160 | 98 | |
paul@161 | 99 | // Clock dividers. |
paul@160 | 100 | |
paul@161 | 101 | uint32_t get_divider(enum Clock_identifiers clock); |
paul@161 | 102 | void set_divider(enum Clock_identifiers clock, uint32_t divider); |
paul@160 | 103 | |
paul@161 | 104 | // Clock sources. |
paul@160 | 105 | |
paul@161 | 106 | uint8_t get_source(enum Clock_identifiers clock); |
paul@161 | 107 | void set_source(enum Clock_identifiers clock, uint8_t source); |
paul@160 | 108 | |
paul@161 | 109 | // Source frequencies. |
paul@160 | 110 | |
paul@161 | 111 | uint32_t get_source_frequency(enum Clock_identifiers clock); |
paul@160 | 112 | |
paul@161 | 113 | // Output clock frequencies. |
paul@160 | 114 | |
paul@160 | 115 | uint32_t get_frequency(enum Clock_identifiers clock); |
paul@160 | 116 | void set_frequency(enum Clock_identifiers clock, uint32_t frequency); |
paul@161 | 117 | |
paul@161 | 118 | // Other operations. |
paul@161 | 119 | |
paul@161 | 120 | void set_pll_parameters(uint32_t pll_reg, uint16_t multiplier, uint8_t in_divider, uint8_t out_divider); |
paul@160 | 121 | }; |
paul@160 | 122 | |
paul@160 | 123 | #endif /* __cplusplus */ |
paul@160 | 124 | |
paul@160 | 125 | |
paul@160 | 126 | |
paul@160 | 127 | /* C language interface. */ |
paul@160 | 128 | |
paul@160 | 129 | EXTERN_C_BEGIN |
paul@160 | 130 | |
paul@160 | 131 | void *x1600_cpm_init(l4_addr_t cpm_base); |
paul@160 | 132 | |
paul@160 | 133 | int x1600_cpm_have_clock(void *cpm, enum Clock_identifiers clock); |
paul@160 | 134 | void x1600_cpm_start_clock(void *cpm, enum Clock_identifiers clock); |
paul@160 | 135 | void x1600_cpm_stop_clock(void *cpm, enum Clock_identifiers clock); |
paul@160 | 136 | |
paul@161 | 137 | uint32_t x1600_cpm_get_divider(void *cpm, enum Clock_identifiers clock); |
paul@161 | 138 | void x1600_cpm_set_divider(void *cpm, enum Clock_identifiers clock, uint32_t divider); |
paul@160 | 139 | |
paul@161 | 140 | uint8_t x1600_cpm_get_source(void *cpm, enum Clock_identifiers clock); |
paul@161 | 141 | void x1600_cpm_set_source(void *cpm, enum Clock_identifiers clock, uint8_t source); |
paul@160 | 142 | |
paul@161 | 143 | uint32_t x1600_cpm_get_source_frequency(void *cpm, enum Clock_identifiers clock); |
paul@160 | 144 | |
paul@160 | 145 | uint32_t x1600_cpm_get_frequency(void *cpm, enum Clock_identifiers clock); |
paul@160 | 146 | void x1600_cpm_set_frequency(void *cpm, enum Clock_identifiers clock, uint32_t frequency); |
paul@160 | 147 | |
paul@160 | 148 | void x1600_cpm_set_mpll_parameters(void *cpm, uint16_t multiplier, uint8_t in_divider, uint8_t out_divider); |
paul@160 | 149 | |
paul@160 | 150 | EXTERN_C_END |