18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * AM33XX CM functions
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2011-2012 Texas Instruments Incorporated - https://www.ti.com/
58c2ecf20Sopenharmony_ci * Vaibhav Hiremath <hvaibhav@ti.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Reference taken from from OMAP4 cminst44xx.c
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
108c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any
148c2ecf20Sopenharmony_ci * kind, whether express or implied; without even the implied warranty
158c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
168c2ecf20Sopenharmony_ci * GNU General Public License for more details.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/types.h>
218c2ecf20Sopenharmony_ci#include <linux/errno.h>
228c2ecf20Sopenharmony_ci#include <linux/err.h>
238c2ecf20Sopenharmony_ci#include <linux/io.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "clockdomain.h"
268c2ecf20Sopenharmony_ci#include "cm.h"
278c2ecf20Sopenharmony_ci#include "cm33xx.h"
288c2ecf20Sopenharmony_ci#include "cm-regbits-34xx.h"
298c2ecf20Sopenharmony_ci#include "cm-regbits-33xx.h"
308c2ecf20Sopenharmony_ci#include "prm33xx.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * CLKCTRL_IDLEST_*: possible values for the CM_*_CLKCTRL.IDLEST bitfield:
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci *   0x0 func:     Module is fully functional, including OCP
368c2ecf20Sopenharmony_ci *   0x1 trans:    Module is performing transition: wakeup, or sleep, or sleep
378c2ecf20Sopenharmony_ci *                 abortion
388c2ecf20Sopenharmony_ci *   0x2 idle:     Module is in Idle mode (only OCP part). It is functional if
398c2ecf20Sopenharmony_ci *                 using separate functional clock
408c2ecf20Sopenharmony_ci *   0x3 disabled: Module is disabled and cannot be accessed
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#define CLKCTRL_IDLEST_FUNCTIONAL		0x0
448c2ecf20Sopenharmony_ci#define CLKCTRL_IDLEST_INTRANSITION		0x1
458c2ecf20Sopenharmony_ci#define CLKCTRL_IDLEST_INTERFACE_IDLE		0x2
468c2ecf20Sopenharmony_ci#define CLKCTRL_IDLEST_DISABLED			0x3
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Private functions */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* Read a register in a CM instance */
518c2ecf20Sopenharmony_cistatic inline u32 am33xx_cm_read_reg(u16 inst, u16 idx)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	return readl_relaxed(cm_base.va + inst + idx);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Write into a register in a CM */
578c2ecf20Sopenharmony_cistatic inline void am33xx_cm_write_reg(u32 val, u16 inst, u16 idx)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	writel_relaxed(val, cm_base.va + inst + idx);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Read-modify-write a register in CM */
638c2ecf20Sopenharmony_cistatic inline u32 am33xx_cm_rmw_reg_bits(u32 mask, u32 bits, s16 inst, s16 idx)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	u32 v;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	v = am33xx_cm_read_reg(inst, idx);
688c2ecf20Sopenharmony_ci	v &= ~mask;
698c2ecf20Sopenharmony_ci	v |= bits;
708c2ecf20Sopenharmony_ci	am33xx_cm_write_reg(v, inst, idx);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return v;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic inline u32 am33xx_cm_read_reg_bits(u16 inst, s16 idx, u32 mask)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	u32 v;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	v = am33xx_cm_read_reg(inst, idx);
808c2ecf20Sopenharmony_ci	v &= mask;
818c2ecf20Sopenharmony_ci	v >>= __ffs(mask);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return v;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/**
878c2ecf20Sopenharmony_ci * _clkctrl_idlest - read a CM_*_CLKCTRL register; mask & shift IDLEST bitfield
888c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
898c2ecf20Sopenharmony_ci * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * Return the IDLEST bitfield of a CM_*_CLKCTRL register, shifted down to
928c2ecf20Sopenharmony_ci * bit 0.
938c2ecf20Sopenharmony_ci */
948c2ecf20Sopenharmony_cistatic u32 _clkctrl_idlest(u16 inst, u16 clkctrl_offs)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	u32 v = am33xx_cm_read_reg(inst, clkctrl_offs);
978c2ecf20Sopenharmony_ci	v &= AM33XX_IDLEST_MASK;
988c2ecf20Sopenharmony_ci	v >>= AM33XX_IDLEST_SHIFT;
998c2ecf20Sopenharmony_ci	return v;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/**
1038c2ecf20Sopenharmony_ci * _is_module_ready - can module registers be accessed without causing an abort?
1048c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
1058c2ecf20Sopenharmony_ci * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * Returns true if the module's CM_*_CLKCTRL.IDLEST bitfield is either
1088c2ecf20Sopenharmony_ci * *FUNCTIONAL or *INTERFACE_IDLE; false otherwise.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cistatic bool _is_module_ready(u16 inst, u16 clkctrl_offs)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	u32 v;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	v = _clkctrl_idlest(inst, clkctrl_offs);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return (v == CLKCTRL_IDLEST_FUNCTIONAL ||
1178c2ecf20Sopenharmony_ci		v == CLKCTRL_IDLEST_INTERFACE_IDLE) ? true : false;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/**
1218c2ecf20Sopenharmony_ci * _clktrctrl_write - write @c to a CM_CLKSTCTRL.CLKTRCTRL register bitfield
1228c2ecf20Sopenharmony_ci * @c: CLKTRCTRL register bitfield (LSB = bit 0, i.e., unshifted)
1238c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
1248c2ecf20Sopenharmony_ci * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * @c must be the unshifted value for CLKTRCTRL - i.e., this function
1278c2ecf20Sopenharmony_ci * will handle the shift itself.
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_cistatic void _clktrctrl_write(u8 c, u16 inst, u16 cdoffs)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	u32 v;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	v = am33xx_cm_read_reg(inst, cdoffs);
1348c2ecf20Sopenharmony_ci	v &= ~AM33XX_CLKTRCTRL_MASK;
1358c2ecf20Sopenharmony_ci	v |= c << AM33XX_CLKTRCTRL_SHIFT;
1368c2ecf20Sopenharmony_ci	am33xx_cm_write_reg(v, inst, cdoffs);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* Public functions */
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/**
1428c2ecf20Sopenharmony_ci * am33xx_cm_is_clkdm_in_hwsup - is a clockdomain in hwsup idle mode?
1438c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
1448c2ecf20Sopenharmony_ci * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * Returns true if the clockdomain referred to by (@inst, @cdoffs)
1478c2ecf20Sopenharmony_ci * is in hardware-supervised idle mode, or 0 otherwise.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistatic bool am33xx_cm_is_clkdm_in_hwsup(u16 inst, u16 cdoffs)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	u32 v;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	v = am33xx_cm_read_reg(inst, cdoffs);
1548c2ecf20Sopenharmony_ci	v &= AM33XX_CLKTRCTRL_MASK;
1558c2ecf20Sopenharmony_ci	v >>= AM33XX_CLKTRCTRL_SHIFT;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return (v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ? true : false;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/**
1618c2ecf20Sopenharmony_ci * am33xx_cm_clkdm_enable_hwsup - put a clockdomain in hwsup-idle mode
1628c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
1638c2ecf20Sopenharmony_ci * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * Put a clockdomain referred to by (@inst, @cdoffs) into
1668c2ecf20Sopenharmony_ci * hardware-supervised idle mode.  No return value.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_cistatic void am33xx_cm_clkdm_enable_hwsup(u16 inst, u16 cdoffs)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	_clktrctrl_write(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, inst, cdoffs);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/**
1748c2ecf20Sopenharmony_ci * am33xx_cm_clkdm_disable_hwsup - put a clockdomain in swsup-idle mode
1758c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
1768c2ecf20Sopenharmony_ci * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * Put a clockdomain referred to by (@inst, @cdoffs) into
1798c2ecf20Sopenharmony_ci * software-supervised idle mode, i.e., controlled manually by the
1808c2ecf20Sopenharmony_ci * Linux OMAP clockdomain code.  No return value.
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_cistatic void am33xx_cm_clkdm_disable_hwsup(u16 inst, u16 cdoffs)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	_clktrctrl_write(OMAP34XX_CLKSTCTRL_DISABLE_AUTO, inst, cdoffs);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/**
1888c2ecf20Sopenharmony_ci * am33xx_cm_clkdm_force_sleep - try to put a clockdomain into idle
1898c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
1908c2ecf20Sopenharmony_ci * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * Put a clockdomain referred to by (@inst, @cdoffs) into idle
1938c2ecf20Sopenharmony_ci * No return value.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_cistatic void am33xx_cm_clkdm_force_sleep(u16 inst, u16 cdoffs)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	_clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_SLEEP, inst, cdoffs);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/**
2018c2ecf20Sopenharmony_ci * am33xx_cm_clkdm_force_wakeup - try to take a clockdomain out of idle
2028c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
2038c2ecf20Sopenharmony_ci * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * Take a clockdomain referred to by (@inst, @cdoffs) out of idle,
2068c2ecf20Sopenharmony_ci * waking it up.  No return value.
2078c2ecf20Sopenharmony_ci */
2088c2ecf20Sopenharmony_cistatic void am33xx_cm_clkdm_force_wakeup(u16 inst, u16 cdoffs)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	_clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP, inst, cdoffs);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/*
2148c2ecf20Sopenharmony_ci *
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/**
2188c2ecf20Sopenharmony_ci * am33xx_cm_wait_module_ready - wait for a module to be in 'func' state
2198c2ecf20Sopenharmony_ci * @part: PRCM partition, ignored for AM33xx
2208c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
2218c2ecf20Sopenharmony_ci * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
2228c2ecf20Sopenharmony_ci * @bit_shift: bit shift for the register, ignored for AM33xx
2238c2ecf20Sopenharmony_ci *
2248c2ecf20Sopenharmony_ci * Wait for the module IDLEST to be functional. If the idle state is in any
2258c2ecf20Sopenharmony_ci * the non functional state (trans, idle or disabled), module and thus the
2268c2ecf20Sopenharmony_ci * sysconfig cannot be accessed and will probably lead to an "imprecise
2278c2ecf20Sopenharmony_ci * external abort"
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_cistatic int am33xx_cm_wait_module_ready(u8 part, s16 inst, u16 clkctrl_offs,
2308c2ecf20Sopenharmony_ci				       u8 bit_shift)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	int i = 0;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	omap_test_timeout(_is_module_ready(inst, clkctrl_offs),
2358c2ecf20Sopenharmony_ci			  MAX_MODULE_READY_TIME, i);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/**
2418c2ecf20Sopenharmony_ci * am33xx_cm_wait_module_idle - wait for a module to be in 'disabled'
2428c2ecf20Sopenharmony_ci * state
2438c2ecf20Sopenharmony_ci * @part: CM partition, ignored for AM33xx
2448c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
2458c2ecf20Sopenharmony_ci * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
2468c2ecf20Sopenharmony_ci * @bit_shift: bit shift for the register, ignored for AM33xx
2478c2ecf20Sopenharmony_ci *
2488c2ecf20Sopenharmony_ci * Wait for the module IDLEST to be disabled. Some PRCM transition,
2498c2ecf20Sopenharmony_ci * like reset assertion or parent clock de-activation must wait the
2508c2ecf20Sopenharmony_ci * module to be fully disabled.
2518c2ecf20Sopenharmony_ci */
2528c2ecf20Sopenharmony_cistatic int am33xx_cm_wait_module_idle(u8 part, s16 inst, u16 clkctrl_offs,
2538c2ecf20Sopenharmony_ci				      u8 bit_shift)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	int i = 0;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	omap_test_timeout((_clkctrl_idlest(inst, clkctrl_offs) ==
2588c2ecf20Sopenharmony_ci				CLKCTRL_IDLEST_DISABLED),
2598c2ecf20Sopenharmony_ci				MAX_MODULE_READY_TIME, i);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/**
2658c2ecf20Sopenharmony_ci * am33xx_cm_module_enable - Enable the modulemode inside CLKCTRL
2668c2ecf20Sopenharmony_ci * @mode: Module mode (SW or HW)
2678c2ecf20Sopenharmony_ci * @part: CM partition, ignored for AM33xx
2688c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
2698c2ecf20Sopenharmony_ci * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
2708c2ecf20Sopenharmony_ci *
2718c2ecf20Sopenharmony_ci * No return value.
2728c2ecf20Sopenharmony_ci */
2738c2ecf20Sopenharmony_cistatic void am33xx_cm_module_enable(u8 mode, u8 part, u16 inst,
2748c2ecf20Sopenharmony_ci				    u16 clkctrl_offs)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	u32 v;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	v = am33xx_cm_read_reg(inst, clkctrl_offs);
2798c2ecf20Sopenharmony_ci	v &= ~AM33XX_MODULEMODE_MASK;
2808c2ecf20Sopenharmony_ci	v |= mode << AM33XX_MODULEMODE_SHIFT;
2818c2ecf20Sopenharmony_ci	am33xx_cm_write_reg(v, inst, clkctrl_offs);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/**
2858c2ecf20Sopenharmony_ci * am33xx_cm_module_disable - Disable the module inside CLKCTRL
2868c2ecf20Sopenharmony_ci * @part: CM partition, ignored for AM33xx
2878c2ecf20Sopenharmony_ci * @inst: CM instance register offset (*_INST macro)
2888c2ecf20Sopenharmony_ci * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
2898c2ecf20Sopenharmony_ci *
2908c2ecf20Sopenharmony_ci * No return value.
2918c2ecf20Sopenharmony_ci */
2928c2ecf20Sopenharmony_cistatic void am33xx_cm_module_disable(u8 part, u16 inst, u16 clkctrl_offs)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	u32 v;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	v = am33xx_cm_read_reg(inst, clkctrl_offs);
2978c2ecf20Sopenharmony_ci	v &= ~AM33XX_MODULEMODE_MASK;
2988c2ecf20Sopenharmony_ci	am33xx_cm_write_reg(v, inst, clkctrl_offs);
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/*
3028c2ecf20Sopenharmony_ci * Clockdomain low-level functions
3038c2ecf20Sopenharmony_ci */
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic int am33xx_clkdm_sleep(struct clockdomain *clkdm)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	am33xx_cm_clkdm_force_sleep(clkdm->cm_inst, clkdm->clkdm_offs);
3088c2ecf20Sopenharmony_ci	return 0;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic int am33xx_clkdm_wakeup(struct clockdomain *clkdm)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	am33xx_cm_clkdm_force_wakeup(clkdm->cm_inst, clkdm->clkdm_offs);
3148c2ecf20Sopenharmony_ci	return 0;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic void am33xx_clkdm_allow_idle(struct clockdomain *clkdm)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	am33xx_cm_clkdm_enable_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic void am33xx_clkdm_deny_idle(struct clockdomain *clkdm)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	am33xx_cm_clkdm_disable_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic int am33xx_clkdm_clk_enable(struct clockdomain *clkdm)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
3308c2ecf20Sopenharmony_ci		return am33xx_clkdm_wakeup(clkdm);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic int am33xx_clkdm_clk_disable(struct clockdomain *clkdm)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	bool hwsup = false;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	hwsup = am33xx_cm_is_clkdm_in_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (!hwsup && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))
3428c2ecf20Sopenharmony_ci		am33xx_clkdm_sleep(clkdm);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic u32 am33xx_cm_xlate_clkctrl(u8 part, u16 inst, u16 offset)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	return cm_base.pa + inst + offset;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci/**
3538c2ecf20Sopenharmony_ci * am33xx_clkdm_save_context - Save the clockdomain transition context
3548c2ecf20Sopenharmony_ci * @clkdm: The clockdomain pointer whose context needs to be saved
3558c2ecf20Sopenharmony_ci *
3568c2ecf20Sopenharmony_ci * Save the clockdomain transition context.
3578c2ecf20Sopenharmony_ci */
3588c2ecf20Sopenharmony_cistatic int am33xx_clkdm_save_context(struct clockdomain *clkdm)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	clkdm->context = am33xx_cm_read_reg_bits(clkdm->cm_inst,
3618c2ecf20Sopenharmony_ci						 clkdm->clkdm_offs,
3628c2ecf20Sopenharmony_ci						 AM33XX_CLKTRCTRL_MASK);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci/**
3688c2ecf20Sopenharmony_ci * am33xx_restore_save_context - Restore the clockdomain transition context
3698c2ecf20Sopenharmony_ci * @clkdm: The clockdomain pointer whose context needs to be restored
3708c2ecf20Sopenharmony_ci *
3718c2ecf20Sopenharmony_ci * Restore the clockdomain transition context.
3728c2ecf20Sopenharmony_ci */
3738c2ecf20Sopenharmony_cistatic int am33xx_clkdm_restore_context(struct clockdomain *clkdm)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	switch (clkdm->context) {
3768c2ecf20Sopenharmony_ci	case OMAP34XX_CLKSTCTRL_DISABLE_AUTO:
3778c2ecf20Sopenharmony_ci		am33xx_clkdm_deny_idle(clkdm);
3788c2ecf20Sopenharmony_ci		break;
3798c2ecf20Sopenharmony_ci	case OMAP34XX_CLKSTCTRL_FORCE_SLEEP:
3808c2ecf20Sopenharmony_ci		am33xx_clkdm_sleep(clkdm);
3818c2ecf20Sopenharmony_ci		break;
3828c2ecf20Sopenharmony_ci	case OMAP34XX_CLKSTCTRL_FORCE_WAKEUP:
3838c2ecf20Sopenharmony_ci		am33xx_clkdm_wakeup(clkdm);
3848c2ecf20Sopenharmony_ci		break;
3858c2ecf20Sopenharmony_ci	case OMAP34XX_CLKSTCTRL_ENABLE_AUTO:
3868c2ecf20Sopenharmony_ci		am33xx_clkdm_allow_idle(clkdm);
3878c2ecf20Sopenharmony_ci		break;
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci	return 0;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistruct clkdm_ops am33xx_clkdm_operations = {
3938c2ecf20Sopenharmony_ci	.clkdm_sleep		= am33xx_clkdm_sleep,
3948c2ecf20Sopenharmony_ci	.clkdm_wakeup		= am33xx_clkdm_wakeup,
3958c2ecf20Sopenharmony_ci	.clkdm_allow_idle	= am33xx_clkdm_allow_idle,
3968c2ecf20Sopenharmony_ci	.clkdm_deny_idle	= am33xx_clkdm_deny_idle,
3978c2ecf20Sopenharmony_ci	.clkdm_clk_enable	= am33xx_clkdm_clk_enable,
3988c2ecf20Sopenharmony_ci	.clkdm_clk_disable	= am33xx_clkdm_clk_disable,
3998c2ecf20Sopenharmony_ci	.clkdm_save_context	= am33xx_clkdm_save_context,
4008c2ecf20Sopenharmony_ci	.clkdm_restore_context	= am33xx_clkdm_restore_context,
4018c2ecf20Sopenharmony_ci};
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic const struct cm_ll_data am33xx_cm_ll_data = {
4048c2ecf20Sopenharmony_ci	.wait_module_ready	= &am33xx_cm_wait_module_ready,
4058c2ecf20Sopenharmony_ci	.wait_module_idle	= &am33xx_cm_wait_module_idle,
4068c2ecf20Sopenharmony_ci	.module_enable		= &am33xx_cm_module_enable,
4078c2ecf20Sopenharmony_ci	.module_disable		= &am33xx_cm_module_disable,
4088c2ecf20Sopenharmony_ci	.xlate_clkctrl		= &am33xx_cm_xlate_clkctrl,
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ciint __init am33xx_cm_init(const struct omap_prcm_init_data *data)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	return cm_register(&am33xx_cm_ll_data);
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic void __exit am33xx_cm_exit(void)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	cm_unregister(&am33xx_cm_ll_data);
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci__exitcall(am33xx_cm_exit);
421