// SPDX-License-Identifier: GPL-2.0-only1/*2* OMAP2+ common Clock Management (CM) IP block functions3*4* Copyright (C) 2012 Texas Instruments, Inc.5* Paul Walmsley6*7* XXX This code should eventually be moved to a CM driver.8*/910#include <linux/kernel.h>11#include <linux/init.h>12#include <linux/errno.h>13#include <linux/bug.h>14#include <linux/of.h>15#include <linux/of_address.h>1617#include "cm2xxx.h"18#include "cm3xxx.h"19#include "cm33xx.h"20#include "cm44xx.h"21#include "clock.h"2223/*24* cm_ll_data: function pointers to SoC-specific implementations of25* common CM functions26*/27static struct cm_ll_data null_cm_ll_data;28static const struct cm_ll_data *cm_ll_data = &null_cm_ll_data;2930/* cm_base: base virtual address of the CM IP block */31struct omap_domain_base cm_base;3233/* cm2_base: base virtual address of the CM2 IP block (OMAP44xx only) */34struct omap_domain_base cm2_base;3536#define CM_NO_CLOCKS 0x137#define CM_SINGLE_INSTANCE 0x23839/**40* cm_split_idlest_reg - split CM_IDLEST reg addr into its components41* @idlest_reg: CM_IDLEST* virtual address42* @prcm_inst: pointer to an s16 to return the PRCM instance offset43* @idlest_reg_id: pointer to a u8 to return the CM_IDLESTx register ID44*45* Given an absolute CM_IDLEST register address @idlest_reg, passes46* the PRCM instance offset and IDLEST register ID back to the caller47* via the @prcm_inst and @idlest_reg_id. Returns -EINVAL upon error,48* or 0 upon success. XXX This function is only needed until absolute49* register addresses are removed from the OMAP struct clk records.50*/51int cm_split_idlest_reg(struct clk_omap_reg *idlest_reg, s16 *prcm_inst,52u8 *idlest_reg_id)53{54int ret;55if (!cm_ll_data->split_idlest_reg) {56WARN_ONCE(1, "cm: %s: no low-level function defined\n",57__func__);58return -EINVAL;59}6061ret = cm_ll_data->split_idlest_reg(idlest_reg, prcm_inst,62idlest_reg_id);63*prcm_inst -= cm_base.offset;64return ret;65}6667/**68* omap_cm_wait_module_ready - wait for a module to leave idle or standby69* @part: PRCM partition70* @prcm_mod: PRCM module offset71* @idlest_reg: CM_IDLESTx register72* @idlest_shift: shift of the bit in the CM_IDLEST* register to check73*74* Wait for the PRCM to indicate that the module identified by75* (@prcm_mod, @idlest_id, @idlest_shift) is clocked. Return 0 upon76* success, -EBUSY if the module doesn't enable in time, or -EINVAL if77* no per-SoC wait_module_ready() function pointer has been registered78* or if the idlest register is unknown on the SoC.79*/80int omap_cm_wait_module_ready(u8 part, s16 prcm_mod, u16 idlest_reg,81u8 idlest_shift)82{83if (!cm_ll_data->wait_module_ready) {84WARN_ONCE(1, "cm: %s: no low-level function defined\n",85__func__);86return -EINVAL;87}8889return cm_ll_data->wait_module_ready(part, prcm_mod, idlest_reg,90idlest_shift);91}9293/**94* omap_cm_wait_module_idle - wait for a module to enter idle or standby95* @part: PRCM partition96* @prcm_mod: PRCM module offset97* @idlest_reg: CM_IDLESTx register98* @idlest_shift: shift of the bit in the CM_IDLEST* register to check99*100* Wait for the PRCM to indicate that the module identified by101* (@prcm_mod, @idlest_id, @idlest_shift) is no longer clocked. Return102* 0 upon success, -EBUSY if the module doesn't enable in time, or103* -EINVAL if no per-SoC wait_module_idle() function pointer has been104* registered or if the idlest register is unknown on the SoC.105*/106int omap_cm_wait_module_idle(u8 part, s16 prcm_mod, u16 idlest_reg,107u8 idlest_shift)108{109if (!cm_ll_data->wait_module_idle) {110WARN_ONCE(1, "cm: %s: no low-level function defined\n",111__func__);112return -EINVAL;113}114115return cm_ll_data->wait_module_idle(part, prcm_mod, idlest_reg,116idlest_shift);117}118119/**120* omap_cm_module_enable - enable a module121* @mode: target mode for the module122* @part: PRCM partition123* @inst: PRCM instance124* @clkctrl_offs: CM_CLKCTRL register offset for the module125*126* Enables clocks for a module identified by (@part, @inst, @clkctrl_offs)127* making its IO space accessible. Return 0 upon success, -EINVAL if no128* per-SoC module_enable() function pointer has been registered.129*/130int omap_cm_module_enable(u8 mode, u8 part, u16 inst, u16 clkctrl_offs)131{132if (!cm_ll_data->module_enable) {133WARN_ONCE(1, "cm: %s: no low-level function defined\n",134__func__);135return -EINVAL;136}137138cm_ll_data->module_enable(mode, part, inst, clkctrl_offs);139return 0;140}141142/**143* omap_cm_module_disable - disable a module144* @part: PRCM partition145* @inst: PRCM instance146* @clkctrl_offs: CM_CLKCTRL register offset for the module147*148* Disables clocks for a module identified by (@part, @inst, @clkctrl_offs)149* makings its IO space inaccessible. Return 0 upon success, -EINVAL if150* no per-SoC module_disable() function pointer has been registered.151*/152int omap_cm_module_disable(u8 part, u16 inst, u16 clkctrl_offs)153{154if (!cm_ll_data->module_disable) {155WARN_ONCE(1, "cm: %s: no low-level function defined\n",156__func__);157return -EINVAL;158}159160cm_ll_data->module_disable(part, inst, clkctrl_offs);161return 0;162}163164u32 omap_cm_xlate_clkctrl(u8 part, u16 inst, u16 clkctrl_offs)165{166if (!cm_ll_data->xlate_clkctrl) {167WARN_ONCE(1, "cm: %s: no low-level function defined\n",168__func__);169return 0;170}171return cm_ll_data->xlate_clkctrl(part, inst, clkctrl_offs);172}173174/**175* cm_register - register per-SoC low-level data with the CM176* @cld: low-level per-SoC OMAP CM data & function pointers to register177*178* Register per-SoC low-level OMAP CM data and function pointers with179* the OMAP CM common interface. The caller must keep the data180* pointed to by @cld valid until it calls cm_unregister() and181* it returns successfully. Returns 0 upon success, -EINVAL if @cld182* is NULL, or -EEXIST if cm_register() has already been called183* without an intervening cm_unregister().184*/185int cm_register(const struct cm_ll_data *cld)186{187if (!cld)188return -EINVAL;189190if (cm_ll_data != &null_cm_ll_data)191return -EEXIST;192193cm_ll_data = cld;194195return 0;196}197198/**199* cm_unregister - unregister per-SoC low-level data & function pointers200* @cld: low-level per-SoC OMAP CM data & function pointers to unregister201*202* Unregister per-SoC low-level OMAP CM data and function pointers203* that were previously registered with cm_register(). The204* caller may not destroy any of the data pointed to by @cld until205* this function returns successfully. Returns 0 upon success, or206* -EINVAL if @cld is NULL or if @cld does not match the struct207* cm_ll_data * previously registered by cm_register().208*/209int cm_unregister(const struct cm_ll_data *cld)210{211if (!cld || cm_ll_data != cld)212return -EINVAL;213214cm_ll_data = &null_cm_ll_data;215216return 0;217}218219#if defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_SOC_OMAP5) || \220defined(CONFIG_SOC_DRA7XX)221static struct omap_prcm_init_data cm_data __initdata = {222.index = TI_CLKM_CM,223.init = omap4_cm_init,224};225226static struct omap_prcm_init_data cm2_data __initdata = {227.index = TI_CLKM_CM2,228.init = omap4_cm_init,229};230#endif231232#ifdef CONFIG_ARCH_OMAP2233static struct omap_prcm_init_data omap2_prcm_data __initdata = {234.index = TI_CLKM_CM,235.init = omap2xxx_cm_init,236.flags = CM_NO_CLOCKS | CM_SINGLE_INSTANCE,237};238#endif239240#ifdef CONFIG_ARCH_OMAP3241static struct omap_prcm_init_data omap3_cm_data __initdata = {242.index = TI_CLKM_CM,243.init = omap3xxx_cm_init,244.flags = CM_SINGLE_INSTANCE,245246/*247* IVA2 offset is a negative value, must offset the cm_base address248* by this to get it to positive side on the iomap249*/250.offset = -OMAP3430_IVA2_MOD,251};252#endif253254#if defined(CONFIG_SOC_AM33XX) || defined(CONFIG_SOC_TI81XX)255static struct omap_prcm_init_data am3_prcm_data __initdata = {256.index = TI_CLKM_CM,257.flags = CM_NO_CLOCKS | CM_SINGLE_INSTANCE,258.init = am33xx_cm_init,259};260#endif261262#ifdef CONFIG_SOC_AM43XX263static struct omap_prcm_init_data am4_prcm_data __initdata = {264.index = TI_CLKM_CM,265.flags = CM_NO_CLOCKS | CM_SINGLE_INSTANCE,266.init = omap4_cm_init,267};268#endif269270static const struct of_device_id omap_cm_dt_match_table[] __initconst = {271#ifdef CONFIG_ARCH_OMAP2272{ .compatible = "ti,omap2-prcm", .data = &omap2_prcm_data },273#endif274#ifdef CONFIG_ARCH_OMAP3275{ .compatible = "ti,omap3-cm", .data = &omap3_cm_data },276#endif277#ifdef CONFIG_ARCH_OMAP4278{ .compatible = "ti,omap4-cm1", .data = &cm_data },279{ .compatible = "ti,omap4-cm2", .data = &cm2_data },280#endif281#ifdef CONFIG_SOC_OMAP5282{ .compatible = "ti,omap5-cm-core-aon", .data = &cm_data },283{ .compatible = "ti,omap5-cm-core", .data = &cm2_data },284#endif285#ifdef CONFIG_SOC_DRA7XX286{ .compatible = "ti,dra7-cm-core-aon", .data = &cm_data },287{ .compatible = "ti,dra7-cm-core", .data = &cm2_data },288#endif289#ifdef CONFIG_SOC_AM33XX290{ .compatible = "ti,am3-prcm", .data = &am3_prcm_data },291#endif292#ifdef CONFIG_SOC_AM43XX293{ .compatible = "ti,am4-prcm", .data = &am4_prcm_data },294#endif295#ifdef CONFIG_SOC_TI81XX296{ .compatible = "ti,dm814-prcm", .data = &am3_prcm_data },297{ .compatible = "ti,dm816-prcm", .data = &am3_prcm_data },298#endif299{ }300};301302/**303* omap2_cm_base_init - initialize iomappings for the CM drivers304*305* Detects and initializes the iomappings for the CM driver, based306* on the DT data. Returns 0 in success, negative error value307* otherwise.308*/309int __init omap2_cm_base_init(void)310{311struct device_node *np;312const struct of_device_id *match;313struct omap_prcm_init_data *data;314struct resource res;315int ret;316struct omap_domain_base *mem = NULL;317318for_each_matching_node_and_match(np, omap_cm_dt_match_table, &match) {319data = (struct omap_prcm_init_data *)match->data;320321ret = of_address_to_resource(np, 0, &res);322if (ret) {323of_node_put(np);324return ret;325}326327if (data->index == TI_CLKM_CM)328mem = &cm_base;329330if (data->index == TI_CLKM_CM2)331mem = &cm2_base;332333data->mem = ioremap(res.start, resource_size(&res));334335if (mem) {336mem->pa = res.start + data->offset;337mem->va = data->mem + data->offset;338mem->offset = data->offset;339}340341data->np = np;342343if (data->init && (data->flags & CM_SINGLE_INSTANCE ||344(cm_base.va && cm2_base.va)))345data->init(data);346}347348return 0;349}350351/**352* omap_cm_init - low level init for the CM drivers353*354* Initializes the low level clock infrastructure for CM drivers.355* Returns 0 in success, negative error value in failure.356*/357int __init omap_cm_init(void)358{359struct device_node *np;360const struct of_device_id *match;361const struct omap_prcm_init_data *data;362int ret;363364for_each_matching_node_and_match(np, omap_cm_dt_match_table, &match) {365data = match->data;366367if (data->flags & CM_NO_CLOCKS)368continue;369370ret = omap2_clk_provider_init(np, data->index, NULL, data->mem);371if (ret) {372of_node_put(np);373return ret;374}375}376377return 0;378}379380381