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 /* Register access type. */ 36 37 class Cpm_regs 38 { 39 Hw::Register_block<32> _regs; 40 41 public: 42 uint32_t exclk_freq; 43 44 explicit Cpm_regs(l4_addr_t addr, uint32_t exclk_freq); 45 46 // Utility methods. 47 48 uint32_t get_field(uint32_t reg, uint32_t mask, uint8_t shift); 49 void set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value); 50 }; 51 52 /* A simple abstraction for accessing the CPM registers. 53 * A proper device could inherit from Hw::Device and use an 54 * Int_property for _exclk_freq. */ 55 56 class Cpm_x1600_chip : public Cpm_chip 57 { 58 private: 59 Hw::Register_block<32> _regs; 60 uint32_t _exclk_freq; 61 Cpm_regs _cpm_regs; 62 63 public: 64 Cpm_x1600_chip(l4_addr_t addr, uint32_t exclk_freq); 65 66 int have_clock(enum Clock_identifiers clock); 67 void start_clock(enum Clock_identifiers clock); 68 void stop_clock(enum Clock_identifiers clock); 69 70 // Clock dividers. 71 72 uint32_t get_divider(enum Clock_identifiers clock); 73 void set_divider(enum Clock_identifiers clock, uint32_t divider); 74 75 // Clock sources. 76 77 uint8_t get_source(enum Clock_identifiers clock); 78 void set_source(enum Clock_identifiers clock, uint8_t source); 79 80 // Source frequencies. 81 82 uint32_t get_source_frequency(enum Clock_identifiers clock); 83 84 // Output clock frequencies. 85 86 uint32_t get_frequency(enum Clock_identifiers clock); 87 void set_frequency(enum Clock_identifiers clock, uint32_t frequency); 88 89 // Other operations. 90 91 void set_pll_parameters(enum Clock_identifiers clock, uint16_t multiplier, 92 uint8_t in_divider, uint8_t out_divider); 93 }; 94 95 #endif /* __cplusplus */ 96 97 98 99 /* C language interface. */ 100 101 EXTERN_C_BEGIN 102 103 void *x1600_cpm_init(l4_addr_t cpm_base); 104 105 int x1600_cpm_have_clock(void *cpm, enum Clock_identifiers clock); 106 void x1600_cpm_start_clock(void *cpm, enum Clock_identifiers clock); 107 void x1600_cpm_stop_clock(void *cpm, enum Clock_identifiers clock); 108 109 uint32_t x1600_cpm_get_divider(void *cpm, enum Clock_identifiers clock); 110 void x1600_cpm_set_divider(void *cpm, enum Clock_identifiers clock, uint32_t divider); 111 112 uint8_t x1600_cpm_get_source(void *cpm, enum Clock_identifiers clock); 113 void x1600_cpm_set_source(void *cpm, enum Clock_identifiers clock, uint8_t source); 114 115 uint32_t x1600_cpm_get_source_frequency(void *cpm, enum Clock_identifiers clock); 116 117 uint32_t x1600_cpm_get_frequency(void *cpm, enum Clock_identifiers clock); 118 void x1600_cpm_set_frequency(void *cpm, enum Clock_identifiers clock, uint32_t frequency); 119 120 void x1600_cpm_set_mpll_parameters(void *cpm, uint16_t multiplier, uint8_t in_divider, uint8_t out_divider); 121 122 EXTERN_C_END