1447b2b13SYann Gautier /* 2*bcccdaccSPatrick Delaunay * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved 3447b2b13SYann Gautier * 4447b2b13SYann Gautier * SPDX-License-Identifier: BSD-3-Clause 5447b2b13SYann Gautier */ 6447b2b13SYann Gautier 7447b2b13SYann Gautier #include <errno.h> 8447b2b13SYann Gautier 952a616b4SAndre Przywara #include <common/fdt_wrappers.h> 1033667d29SYann Gautier #include <drivers/clk.h> 11447b2b13SYann Gautier #include <drivers/st/stm32_gpio.h> 12447b2b13SYann Gautier #include <drivers/st/stm32mp_clkfunc.h> 13*bcccdaccSPatrick Delaunay #include <libfdt.h> 14*bcccdaccSPatrick Delaunay 15*bcccdaccSPatrick Delaunay #include <platform_def.h> 16447b2b13SYann Gautier 17165ad556SNicolas Le Bayon #define DT_UART_COMPAT "st,stm32h7-uart" 18447b2b13SYann Gautier /* 19f66358afSYann Gautier * Get the frequency of an oscillator from its name in device tree. 20f66358afSYann Gautier * @param name: oscillator name 21f66358afSYann Gautier * @param freq: stores the frequency of the oscillator 22f66358afSYann Gautier * @return: 0 on success, and a negative FDT/ERRNO error code on failure. 23f66358afSYann Gautier */ 24f66358afSYann Gautier int fdt_osc_read_freq(const char *name, uint32_t *freq) 25f66358afSYann Gautier { 26f66358afSYann Gautier int node, subnode; 27f66358afSYann Gautier void *fdt; 28f66358afSYann Gautier 29f66358afSYann Gautier if (fdt_get_address(&fdt) == 0) { 30f66358afSYann Gautier return -ENOENT; 31f66358afSYann Gautier } 32f66358afSYann Gautier 33f66358afSYann Gautier node = fdt_path_offset(fdt, "/clocks"); 34f66358afSYann Gautier if (node < 0) { 35f66358afSYann Gautier return -FDT_ERR_NOTFOUND; 36f66358afSYann Gautier } 37f66358afSYann Gautier 38f66358afSYann Gautier fdt_for_each_subnode(subnode, fdt, node) { 39f66358afSYann Gautier const char *cchar; 40f66358afSYann Gautier int ret; 41f66358afSYann Gautier 42f66358afSYann Gautier cchar = fdt_get_name(fdt, subnode, &ret); 43f66358afSYann Gautier if (cchar == NULL) { 44f66358afSYann Gautier return ret; 45f66358afSYann Gautier } 46f66358afSYann Gautier 47*bcccdaccSPatrick Delaunay if ((strncmp(cchar, name, (size_t)ret) == 0) && 48*bcccdaccSPatrick Delaunay (fdt_get_status(subnode) != DT_DISABLED)) { 49f66358afSYann Gautier const fdt32_t *cuint; 50f66358afSYann Gautier 51f66358afSYann Gautier cuint = fdt_getprop(fdt, subnode, "clock-frequency", 52f66358afSYann Gautier &ret); 53f66358afSYann Gautier if (cuint == NULL) { 54f66358afSYann Gautier return ret; 55f66358afSYann Gautier } 56f66358afSYann Gautier 57f66358afSYann Gautier *freq = fdt32_to_cpu(*cuint); 58f66358afSYann Gautier 59f66358afSYann Gautier return 0; 60f66358afSYann Gautier } 61f66358afSYann Gautier } 62f66358afSYann Gautier 63f66358afSYann Gautier /* Oscillator not found, freq=0 */ 64f66358afSYann Gautier *freq = 0; 65f66358afSYann Gautier return 0; 66f66358afSYann Gautier } 67f66358afSYann Gautier 68f66358afSYann Gautier /* 69f66358afSYann Gautier * Check the presence of an oscillator property from its id. 70f66358afSYann Gautier * @param osc_id: oscillator ID 71f66358afSYann Gautier * @param prop_name: property name 72f66358afSYann Gautier * @return: true/false regarding search result. 73f66358afSYann Gautier */ 74f66358afSYann Gautier bool fdt_osc_read_bool(enum stm32mp_osc_id osc_id, const char *prop_name) 75f66358afSYann Gautier { 76f66358afSYann Gautier int node, subnode; 77f66358afSYann Gautier void *fdt; 78f66358afSYann Gautier 79f66358afSYann Gautier if (fdt_get_address(&fdt) == 0) { 80f66358afSYann Gautier return false; 81f66358afSYann Gautier } 82f66358afSYann Gautier 83f66358afSYann Gautier if (osc_id >= NB_OSC) { 84f66358afSYann Gautier return false; 85f66358afSYann Gautier } 86f66358afSYann Gautier 87f66358afSYann Gautier node = fdt_path_offset(fdt, "/clocks"); 88f66358afSYann Gautier if (node < 0) { 89f66358afSYann Gautier return false; 90f66358afSYann Gautier } 91f66358afSYann Gautier 92f66358afSYann Gautier fdt_for_each_subnode(subnode, fdt, node) { 93f66358afSYann Gautier const char *cchar; 94f66358afSYann Gautier int ret; 95f66358afSYann Gautier 96f66358afSYann Gautier cchar = fdt_get_name(fdt, subnode, &ret); 97f66358afSYann Gautier if (cchar == NULL) { 98f66358afSYann Gautier return false; 99f66358afSYann Gautier } 100f66358afSYann Gautier 101f66358afSYann Gautier if (strncmp(cchar, stm32mp_osc_node_label[osc_id], 102f66358afSYann Gautier (size_t)ret) != 0) { 103f66358afSYann Gautier continue; 104f66358afSYann Gautier } 105f66358afSYann Gautier 106f66358afSYann Gautier if (fdt_getprop(fdt, subnode, prop_name, NULL) != NULL) { 107f66358afSYann Gautier return true; 108f66358afSYann Gautier } 109f66358afSYann Gautier } 110f66358afSYann Gautier 111f66358afSYann Gautier return false; 112f66358afSYann Gautier } 113f66358afSYann Gautier 114f66358afSYann Gautier /* 115f66358afSYann Gautier * Get the value of a oscillator property from its ID. 116f66358afSYann Gautier * @param osc_id: oscillator ID 117f66358afSYann Gautier * @param prop_name: property name 118f66358afSYann Gautier * @param dflt_value: default value 119f66358afSYann Gautier * @return oscillator value on success, default value if property not found. 120f66358afSYann Gautier */ 121f66358afSYann Gautier uint32_t fdt_osc_read_uint32_default(enum stm32mp_osc_id osc_id, 122f66358afSYann Gautier const char *prop_name, uint32_t dflt_value) 123f66358afSYann Gautier { 124f66358afSYann Gautier int node, subnode; 125f66358afSYann Gautier void *fdt; 126f66358afSYann Gautier 127f66358afSYann Gautier if (fdt_get_address(&fdt) == 0) { 128f66358afSYann Gautier return dflt_value; 129f66358afSYann Gautier } 130f66358afSYann Gautier 131f66358afSYann Gautier if (osc_id >= NB_OSC) { 132f66358afSYann Gautier return dflt_value; 133f66358afSYann Gautier } 134f66358afSYann Gautier 135f66358afSYann Gautier node = fdt_path_offset(fdt, "/clocks"); 136f66358afSYann Gautier if (node < 0) { 137f66358afSYann Gautier return dflt_value; 138f66358afSYann Gautier } 139f66358afSYann Gautier 140f66358afSYann Gautier fdt_for_each_subnode(subnode, fdt, node) { 141f66358afSYann Gautier const char *cchar; 142f66358afSYann Gautier int ret; 143f66358afSYann Gautier 144f66358afSYann Gautier cchar = fdt_get_name(fdt, subnode, &ret); 145f66358afSYann Gautier if (cchar == NULL) { 146f66358afSYann Gautier return dflt_value; 147f66358afSYann Gautier } 148f66358afSYann Gautier 149f66358afSYann Gautier if (strncmp(cchar, stm32mp_osc_node_label[osc_id], 150f66358afSYann Gautier (size_t)ret) != 0) { 151f66358afSYann Gautier continue; 152f66358afSYann Gautier } 153f66358afSYann Gautier 154be858cffSAndre Przywara return fdt_read_uint32_default(fdt, subnode, prop_name, 155be858cffSAndre Przywara dflt_value); 156f66358afSYann Gautier } 157f66358afSYann Gautier 158f66358afSYann Gautier return dflt_value; 159f66358afSYann Gautier } 160f66358afSYann Gautier 161f66358afSYann Gautier /* 162447b2b13SYann Gautier * Get the RCC node offset from the device tree 163447b2b13SYann Gautier * @param fdt: Device tree reference 164447b2b13SYann Gautier * @return: Node offset or a negative value on error 165447b2b13SYann Gautier */ 166ff18c4cdSPatrick Delaunay static int fdt_get_rcc_node(void *fdt) 167447b2b13SYann Gautier { 168ba57711cSYann Gautier static int node; 169ba57711cSYann Gautier 170ba57711cSYann Gautier if (node <= 0) { 171ba57711cSYann Gautier node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_CLK_COMPAT); 172ba57711cSYann Gautier } 173ba57711cSYann Gautier 174ba57711cSYann Gautier return node; 175447b2b13SYann Gautier } 176447b2b13SYann Gautier 177447b2b13SYann Gautier /* 178447b2b13SYann Gautier * Read a series of parameters in rcc-clk section in device tree 179447b2b13SYann Gautier * @param prop_name: Name of the RCC property to be read 180447b2b13SYann Gautier * @param array: the array to store the property parameters 181447b2b13SYann Gautier * @param count: number of parameters to be read 182447b2b13SYann Gautier * @return: 0 on succes or a negative value on error 183447b2b13SYann Gautier */ 18452a616b4SAndre Przywara int fdt_rcc_read_uint32_array(const char *prop_name, uint32_t count, 18552a616b4SAndre Przywara uint32_t *array) 186447b2b13SYann Gautier { 187447b2b13SYann Gautier int node; 188447b2b13SYann Gautier void *fdt; 189447b2b13SYann Gautier 190447b2b13SYann Gautier if (fdt_get_address(&fdt) == 0) { 191447b2b13SYann Gautier return -ENOENT; 192447b2b13SYann Gautier } 193447b2b13SYann Gautier 194447b2b13SYann Gautier node = fdt_get_rcc_node(fdt); 195447b2b13SYann Gautier if (node < 0) { 196447b2b13SYann Gautier return -FDT_ERR_NOTFOUND; 197447b2b13SYann Gautier } 198447b2b13SYann Gautier 19952a616b4SAndre Przywara return fdt_read_uint32_array(fdt, node, prop_name, count, array); 200447b2b13SYann Gautier } 201447b2b13SYann Gautier 202447b2b13SYann Gautier /* 203447b2b13SYann Gautier * Get the subnode offset in rcc-clk section from its name in device tree 204447b2b13SYann Gautier * @param name: name of the RCC property 205447b2b13SYann Gautier * @return: offset on success, and a negative FDT/ERRNO error code on failure. 206447b2b13SYann Gautier */ 207447b2b13SYann Gautier int fdt_rcc_subnode_offset(const char *name) 208447b2b13SYann Gautier { 209447b2b13SYann Gautier int node, subnode; 210447b2b13SYann Gautier void *fdt; 211447b2b13SYann Gautier 212447b2b13SYann Gautier if (fdt_get_address(&fdt) == 0) { 213447b2b13SYann Gautier return -ENOENT; 214447b2b13SYann Gautier } 215447b2b13SYann Gautier 216447b2b13SYann Gautier node = fdt_get_rcc_node(fdt); 217447b2b13SYann Gautier if (node < 0) { 218447b2b13SYann Gautier return -FDT_ERR_NOTFOUND; 219447b2b13SYann Gautier } 220447b2b13SYann Gautier 221447b2b13SYann Gautier subnode = fdt_subnode_offset(fdt, node, name); 222447b2b13SYann Gautier if (subnode <= 0) { 223447b2b13SYann Gautier return -FDT_ERR_NOTFOUND; 224447b2b13SYann Gautier } 225447b2b13SYann Gautier 226447b2b13SYann Gautier return subnode; 227447b2b13SYann Gautier } 228447b2b13SYann Gautier 229447b2b13SYann Gautier /* 230447b2b13SYann Gautier * Get the pointer to a rcc-clk property from its name. 231447b2b13SYann Gautier * @param name: name of the RCC property 232447b2b13SYann Gautier * @param lenp: stores the length of the property. 233447b2b13SYann Gautier * @return: pointer to the property on success, and NULL value on failure. 234447b2b13SYann Gautier */ 235447b2b13SYann Gautier const fdt32_t *fdt_rcc_read_prop(const char *prop_name, int *lenp) 236447b2b13SYann Gautier { 237447b2b13SYann Gautier const fdt32_t *cuint; 238447b2b13SYann Gautier int node, len; 239447b2b13SYann Gautier void *fdt; 240447b2b13SYann Gautier 241447b2b13SYann Gautier if (fdt_get_address(&fdt) == 0) { 242447b2b13SYann Gautier return NULL; 243447b2b13SYann Gautier } 244447b2b13SYann Gautier 245447b2b13SYann Gautier node = fdt_get_rcc_node(fdt); 246447b2b13SYann Gautier if (node < 0) { 247447b2b13SYann Gautier return NULL; 248447b2b13SYann Gautier } 249447b2b13SYann Gautier 250447b2b13SYann Gautier cuint = fdt_getprop(fdt, node, prop_name, &len); 251447b2b13SYann Gautier if (cuint == NULL) { 252447b2b13SYann Gautier return NULL; 253447b2b13SYann Gautier } 254447b2b13SYann Gautier 255447b2b13SYann Gautier *lenp = len; 256447b2b13SYann Gautier return cuint; 257447b2b13SYann Gautier } 258447b2b13SYann Gautier 259447b2b13SYann Gautier /* 260447b2b13SYann Gautier * Get the secure status for rcc node in device tree. 261447b2b13SYann Gautier * @return: true if rcc is available from secure world, false if not. 262447b2b13SYann Gautier */ 263447b2b13SYann Gautier bool fdt_get_rcc_secure_status(void) 264447b2b13SYann Gautier { 265447b2b13SYann Gautier int node; 266447b2b13SYann Gautier void *fdt; 267447b2b13SYann Gautier 268447b2b13SYann Gautier if (fdt_get_address(&fdt) == 0) { 269447b2b13SYann Gautier return false; 270447b2b13SYann Gautier } 271447b2b13SYann Gautier 272447b2b13SYann Gautier node = fdt_get_rcc_node(fdt); 273447b2b13SYann Gautier if (node < 0) { 274447b2b13SYann Gautier return false; 275447b2b13SYann Gautier } 276447b2b13SYann Gautier 277447b2b13SYann Gautier return !!(fdt_get_status(node) & DT_SECURE); 278447b2b13SYann Gautier } 279447b2b13SYann Gautier 280447b2b13SYann Gautier /* 281447b2b13SYann Gautier * Get the clock ID of the given node in device tree. 282447b2b13SYann Gautier * @param node: node offset 283447b2b13SYann Gautier * @return: Clock ID on success, and a negative FDT/ERRNO error code on failure. 284447b2b13SYann Gautier */ 285447b2b13SYann Gautier int fdt_get_clock_id(int node) 286447b2b13SYann Gautier { 287447b2b13SYann Gautier const fdt32_t *cuint; 288447b2b13SYann Gautier void *fdt; 289447b2b13SYann Gautier 290447b2b13SYann Gautier if (fdt_get_address(&fdt) == 0) { 291447b2b13SYann Gautier return -ENOENT; 292447b2b13SYann Gautier } 293447b2b13SYann Gautier 294447b2b13SYann Gautier cuint = fdt_getprop(fdt, node, "clocks", NULL); 295447b2b13SYann Gautier if (cuint == NULL) { 296447b2b13SYann Gautier return -FDT_ERR_NOTFOUND; 297447b2b13SYann Gautier } 298447b2b13SYann Gautier 299447b2b13SYann Gautier cuint++; 300447b2b13SYann Gautier return (int)fdt32_to_cpu(*cuint); 301447b2b13SYann Gautier } 302165ad556SNicolas Le Bayon 303165ad556SNicolas Le Bayon /* 304165ad556SNicolas Le Bayon * Get the frequency of the specified UART instance. 305165ad556SNicolas Le Bayon * @param instance: UART interface registers base address. 306165ad556SNicolas Le Bayon * @return: clock frequency on success, 0 value on failure. 307165ad556SNicolas Le Bayon */ 308165ad556SNicolas Le Bayon unsigned long fdt_get_uart_clock_freq(uintptr_t instance) 309165ad556SNicolas Le Bayon { 310165ad556SNicolas Le Bayon void *fdt; 311165ad556SNicolas Le Bayon int node; 312165ad556SNicolas Le Bayon int clk_id; 313165ad556SNicolas Le Bayon 314165ad556SNicolas Le Bayon if (fdt_get_address(&fdt) == 0) { 315165ad556SNicolas Le Bayon return 0UL; 316165ad556SNicolas Le Bayon } 317165ad556SNicolas Le Bayon 318165ad556SNicolas Le Bayon /* Check for UART nodes */ 319165ad556SNicolas Le Bayon node = dt_match_instance_by_compatible(DT_UART_COMPAT, instance); 320165ad556SNicolas Le Bayon if (node < 0) { 321165ad556SNicolas Le Bayon return 0UL; 322165ad556SNicolas Le Bayon } 323165ad556SNicolas Le Bayon 324165ad556SNicolas Le Bayon clk_id = fdt_get_clock_id(node); 325165ad556SNicolas Le Bayon if (clk_id < 0) { 326165ad556SNicolas Le Bayon return 0UL; 327165ad556SNicolas Le Bayon } 328165ad556SNicolas Le Bayon 32933667d29SYann Gautier return clk_get_rate((unsigned long)clk_id); 330165ad556SNicolas Le Bayon } 331