// SPDX-License-Identifier: GPL-2.0-only1/*2* AM33XX CM functions3*4* Copyright (C) 2011-2012 Texas Instruments Incorporated - https://www.ti.com/5* Vaibhav Hiremath <[email protected]>6*7* Reference taken from OMAP4 cminst44xx.c8*/910#include <linux/kernel.h>11#include <linux/types.h>12#include <linux/errno.h>13#include <linux/err.h>14#include <linux/io.h>1516#include "clockdomain.h"17#include "cm.h"18#include "cm33xx.h"19#include "cm-regbits-34xx.h"20#include "cm-regbits-33xx.h"21#include "prm33xx.h"22#if IS_ENABLED(CONFIG_SUSPEND)23#include <linux/suspend.h>24#endif2526/*27* CLKCTRL_IDLEST_*: possible values for the CM_*_CLKCTRL.IDLEST bitfield:28*29* 0x0 func: Module is fully functional, including OCP30* 0x1 trans: Module is performing transition: wakeup, or sleep, or sleep31* abortion32* 0x2 idle: Module is in Idle mode (only OCP part). It is functional if33* using separate functional clock34* 0x3 disabled: Module is disabled and cannot be accessed35*36*/37#define CLKCTRL_IDLEST_FUNCTIONAL 0x038#define CLKCTRL_IDLEST_INTRANSITION 0x139#define CLKCTRL_IDLEST_INTERFACE_IDLE 0x240#define CLKCTRL_IDLEST_DISABLED 0x34142/* Private functions */4344/* Read a register in a CM instance */45static inline u32 am33xx_cm_read_reg(u16 inst, u16 idx)46{47return readl_relaxed(cm_base.va + inst + idx);48}4950/* Write into a register in a CM */51static inline void am33xx_cm_write_reg(u32 val, u16 inst, u16 idx)52{53writel_relaxed(val, cm_base.va + inst + idx);54}5556/* Read-modify-write a register in CM */57static inline u32 am33xx_cm_rmw_reg_bits(u32 mask, u32 bits, s16 inst, s16 idx)58{59u32 v;6061v = am33xx_cm_read_reg(inst, idx);62v &= ~mask;63v |= bits;64am33xx_cm_write_reg(v, inst, idx);6566return v;67}6869static inline u32 am33xx_cm_read_reg_bits(u16 inst, s16 idx, u32 mask)70{71u32 v;7273v = am33xx_cm_read_reg(inst, idx);74v &= mask;75v >>= __ffs(mask);7677return v;78}7980/**81* _clkctrl_idlest - read a CM_*_CLKCTRL register; mask & shift IDLEST bitfield82* @inst: CM instance register offset (*_INST macro)83* @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)84*85* Return the IDLEST bitfield of a CM_*_CLKCTRL register, shifted down to86* bit 0.87*/88static u32 _clkctrl_idlest(u16 inst, u16 clkctrl_offs)89{90u32 v = am33xx_cm_read_reg(inst, clkctrl_offs);91v &= AM33XX_IDLEST_MASK;92v >>= AM33XX_IDLEST_SHIFT;93return v;94}9596/**97* _is_module_ready - can module registers be accessed without causing an abort?98* @inst: CM instance register offset (*_INST macro)99* @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)100*101* Returns true if the module's CM_*_CLKCTRL.IDLEST bitfield is either102* *FUNCTIONAL or *INTERFACE_IDLE; false otherwise.103*/104static bool _is_module_ready(u16 inst, u16 clkctrl_offs)105{106u32 v;107108v = _clkctrl_idlest(inst, clkctrl_offs);109110return (v == CLKCTRL_IDLEST_FUNCTIONAL ||111v == CLKCTRL_IDLEST_INTERFACE_IDLE) ? true : false;112}113114/**115* _clktrctrl_write - write @c to a CM_CLKSTCTRL.CLKTRCTRL register bitfield116* @c: CLKTRCTRL register bitfield (LSB = bit 0, i.e., unshifted)117* @inst: CM instance register offset (*_INST macro)118* @cdoffs: Clockdomain register offset (*_CDOFFS macro)119*120* @c must be the unshifted value for CLKTRCTRL - i.e., this function121* will handle the shift itself.122*/123static void _clktrctrl_write(u8 c, u16 inst, u16 cdoffs)124{125u32 v;126127v = am33xx_cm_read_reg(inst, cdoffs);128v &= ~AM33XX_CLKTRCTRL_MASK;129v |= c << AM33XX_CLKTRCTRL_SHIFT;130am33xx_cm_write_reg(v, inst, cdoffs);131}132133/* Public functions */134135/**136* am33xx_cm_is_clkdm_in_hwsup - is a clockdomain in hwsup idle mode?137* @inst: CM instance register offset (*_INST macro)138* @cdoffs: Clockdomain register offset (*_CDOFFS macro)139*140* Returns true if the clockdomain referred to by (@inst, @cdoffs)141* is in hardware-supervised idle mode, or 0 otherwise.142*/143static bool am33xx_cm_is_clkdm_in_hwsup(u16 inst, u16 cdoffs)144{145u32 v;146147v = am33xx_cm_read_reg(inst, cdoffs);148v &= AM33XX_CLKTRCTRL_MASK;149v >>= AM33XX_CLKTRCTRL_SHIFT;150151return (v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ? true : false;152}153154/**155* am33xx_cm_clkdm_enable_hwsup - put a clockdomain in hwsup-idle mode156* @inst: CM instance register offset (*_INST macro)157* @cdoffs: Clockdomain register offset (*_CDOFFS macro)158*159* Put a clockdomain referred to by (@inst, @cdoffs) into160* hardware-supervised idle mode. No return value.161*/162static void am33xx_cm_clkdm_enable_hwsup(u16 inst, u16 cdoffs)163{164_clktrctrl_write(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, inst, cdoffs);165}166167/**168* am33xx_cm_clkdm_disable_hwsup - put a clockdomain in swsup-idle mode169* @inst: CM instance register offset (*_INST macro)170* @cdoffs: Clockdomain register offset (*_CDOFFS macro)171*172* Put a clockdomain referred to by (@inst, @cdoffs) into173* software-supervised idle mode, i.e., controlled manually by the174* Linux OMAP clockdomain code. No return value.175*/176static void am33xx_cm_clkdm_disable_hwsup(u16 inst, u16 cdoffs)177{178_clktrctrl_write(OMAP34XX_CLKSTCTRL_DISABLE_AUTO, inst, cdoffs);179}180181/**182* am33xx_cm_clkdm_force_sleep - try to put a clockdomain into idle183* @inst: CM instance register offset (*_INST macro)184* @cdoffs: Clockdomain register offset (*_CDOFFS macro)185*186* Put a clockdomain referred to by (@inst, @cdoffs) into idle187* No return value.188*/189static void am33xx_cm_clkdm_force_sleep(u16 inst, u16 cdoffs)190{191_clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_SLEEP, inst, cdoffs);192}193194/**195* am33xx_cm_clkdm_force_wakeup - try to take a clockdomain out of idle196* @inst: CM instance register offset (*_INST macro)197* @cdoffs: Clockdomain register offset (*_CDOFFS macro)198*199* Take a clockdomain referred to by (@inst, @cdoffs) out of idle,200* waking it up. No return value.201*/202static void am33xx_cm_clkdm_force_wakeup(u16 inst, u16 cdoffs)203{204_clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP, inst, cdoffs);205}206207/*208*209*/210211/**212* am33xx_cm_wait_module_ready - wait for a module to be in 'func' state213* @part: PRCM partition, ignored for AM33xx214* @inst: CM instance register offset (*_INST macro)215* @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)216* @bit_shift: bit shift for the register, ignored for AM33xx217*218* Wait for the module IDLEST to be functional. If the idle state is in any219* the non functional state (trans, idle or disabled), module and thus the220* sysconfig cannot be accessed and will probably lead to an "imprecise221* external abort"222*/223static int am33xx_cm_wait_module_ready(u8 part, s16 inst, u16 clkctrl_offs,224u8 bit_shift)225{226int i = 0;227228omap_test_timeout(_is_module_ready(inst, clkctrl_offs),229MAX_MODULE_READY_TIME, i);230231return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;232}233234/**235* am33xx_cm_wait_module_idle - wait for a module to be in 'disabled'236* state237* @part: CM partition, ignored for AM33xx238* @inst: CM instance register offset (*_INST macro)239* @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)240* @bit_shift: bit shift for the register, ignored for AM33xx241*242* Wait for the module IDLEST to be disabled. Some PRCM transition,243* like reset assertion or parent clock de-activation must wait the244* module to be fully disabled.245*/246static int am33xx_cm_wait_module_idle(u8 part, s16 inst, u16 clkctrl_offs,247u8 bit_shift)248{249int i = 0;250251omap_test_timeout((_clkctrl_idlest(inst, clkctrl_offs) ==252CLKCTRL_IDLEST_DISABLED),253MAX_MODULE_READY_TIME, i);254255return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;256}257258/**259* am33xx_cm_module_enable - Enable the modulemode inside CLKCTRL260* @mode: Module mode (SW or HW)261* @part: CM partition, ignored for AM33xx262* @inst: CM instance register offset (*_INST macro)263* @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)264*265* No return value.266*/267static void am33xx_cm_module_enable(u8 mode, u8 part, u16 inst,268u16 clkctrl_offs)269{270u32 v;271272v = am33xx_cm_read_reg(inst, clkctrl_offs);273v &= ~AM33XX_MODULEMODE_MASK;274v |= mode << AM33XX_MODULEMODE_SHIFT;275am33xx_cm_write_reg(v, inst, clkctrl_offs);276}277278/**279* am33xx_cm_module_disable - Disable the module inside CLKCTRL280* @part: CM partition, ignored for AM33xx281* @inst: CM instance register offset (*_INST macro)282* @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)283*284* No return value.285*/286static void am33xx_cm_module_disable(u8 part, u16 inst, u16 clkctrl_offs)287{288u32 v;289290v = am33xx_cm_read_reg(inst, clkctrl_offs);291v &= ~AM33XX_MODULEMODE_MASK;292am33xx_cm_write_reg(v, inst, clkctrl_offs);293}294295/*296* Clockdomain low-level functions297*/298299static int am33xx_clkdm_sleep(struct clockdomain *clkdm)300{301am33xx_cm_clkdm_force_sleep(clkdm->cm_inst, clkdm->clkdm_offs);302return 0;303}304305static int am33xx_clkdm_wakeup(struct clockdomain *clkdm)306{307am33xx_cm_clkdm_force_wakeup(clkdm->cm_inst, clkdm->clkdm_offs);308return 0;309}310311static void am33xx_clkdm_allow_idle(struct clockdomain *clkdm)312{313am33xx_cm_clkdm_enable_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);314}315316static void am33xx_clkdm_deny_idle(struct clockdomain *clkdm)317{318am33xx_cm_clkdm_disable_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);319}320321static int am33xx_clkdm_clk_enable(struct clockdomain *clkdm)322{323if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)324return am33xx_clkdm_wakeup(clkdm);325326return 0;327}328329static int am33xx_clkdm_clk_disable(struct clockdomain *clkdm)330{331bool hwsup = false;332333#if IS_ENABLED(CONFIG_SUSPEND)334/*335* In case of standby, Don't put the l4ls clk domain to sleep.336* Since CM3 PM FW doesn't wake-up/enable the l4ls clk domain337* upon wake-up, CM3 PM FW fails to wake-up th MPU.338*/339if (pm_suspend_target_state == PM_SUSPEND_STANDBY &&340(clkdm->flags & CLKDM_STANDBY_FORCE_WAKEUP))341return 0;342#endif343hwsup = am33xx_cm_is_clkdm_in_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);344if (!hwsup && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))345am33xx_clkdm_sleep(clkdm);346347return 0;348}349350static u32 am33xx_cm_xlate_clkctrl(u8 part, u16 inst, u16 offset)351{352return cm_base.pa + inst + offset;353}354355/**356* am33xx_clkdm_save_context - Save the clockdomain transition context357* @clkdm: The clockdomain pointer whose context needs to be saved358*359* Save the clockdomain transition context.360*/361static int am33xx_clkdm_save_context(struct clockdomain *clkdm)362{363clkdm->context = am33xx_cm_read_reg_bits(clkdm->cm_inst,364clkdm->clkdm_offs,365AM33XX_CLKTRCTRL_MASK);366367return 0;368}369370/**371* am33xx_clkdm_restore_context - Restore the clockdomain transition context372* @clkdm: The clockdomain pointer whose context needs to be restored373*374* Restore the clockdomain transition context.375*/376static int am33xx_clkdm_restore_context(struct clockdomain *clkdm)377{378switch (clkdm->context) {379case OMAP34XX_CLKSTCTRL_DISABLE_AUTO:380am33xx_clkdm_deny_idle(clkdm);381break;382case OMAP34XX_CLKSTCTRL_FORCE_SLEEP:383am33xx_clkdm_sleep(clkdm);384break;385case OMAP34XX_CLKSTCTRL_FORCE_WAKEUP:386am33xx_clkdm_wakeup(clkdm);387break;388case OMAP34XX_CLKSTCTRL_ENABLE_AUTO:389am33xx_clkdm_allow_idle(clkdm);390break;391}392return 0;393}394395struct clkdm_ops am33xx_clkdm_operations = {396.clkdm_sleep = am33xx_clkdm_sleep,397.clkdm_wakeup = am33xx_clkdm_wakeup,398.clkdm_allow_idle = am33xx_clkdm_allow_idle,399.clkdm_deny_idle = am33xx_clkdm_deny_idle,400.clkdm_clk_enable = am33xx_clkdm_clk_enable,401.clkdm_clk_disable = am33xx_clkdm_clk_disable,402.clkdm_save_context = am33xx_clkdm_save_context,403.clkdm_restore_context = am33xx_clkdm_restore_context,404};405406static const struct cm_ll_data am33xx_cm_ll_data = {407.wait_module_ready = &am33xx_cm_wait_module_ready,408.wait_module_idle = &am33xx_cm_wait_module_idle,409.module_enable = &am33xx_cm_module_enable,410.module_disable = &am33xx_cm_module_disable,411.xlate_clkctrl = &am33xx_cm_xlate_clkctrl,412};413414int __init am33xx_cm_init(const struct omap_prcm_init_data *data)415{416return cm_register(&am33xx_cm_ll_data);417}418419static void __exit am33xx_cm_exit(void)420{421cm_unregister(&am33xx_cm_ll_data);422}423__exitcall(am33xx_cm_exit);424425426