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