From: philippe.langlais@stericsson.com (Philippe Langlais) Date: Thu, 27 May 2010 10:27:28 +0200 Subject: [PATCH 2/6] U6715 clocks gating management U6 clock generic driver & U6715 cgu clock specific (V2) In-Reply-To: <1274948852-9179-1-git-send-email-philippe.langlais@stericsson.com> References: <1274948852-9179-1-git-send-email-philippe.langlais@stericsson.com> Message-ID: <1274948852-9179-3-git-send-email-philippe.langlais@stericsson.com> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org Signed-off-by: Philippe Langlais --- arch/arm/mach-u67xx/Makefile | 2 +- arch/arm/mach-u67xx/cgu.c | 760 ++++++++++++++++++++++++++ arch/arm/mach-u67xx/clock_u67xx.h | 876 ++++++++++++++++++++++++++++++ arch/arm/plat-u6xxx/Makefile | 2 +- arch/arm/plat-u6xxx/clock.c | 650 ++++++++++++++++++++++ arch/arm/plat-u6xxx/include/mach/clock.h | 376 +++++++++++++ arch/arm/plat-u6xxx/timer.c | 1 + 7 files changed, 2665 insertions(+), 2 deletions(-) create mode 100644 arch/arm/mach-u67xx/cgu.c create mode 100644 arch/arm/mach-u67xx/clock_u67xx.h create mode 100644 arch/arm/plat-u6xxx/clock.c create mode 100644 arch/arm/plat-u6xxx/include/mach/clock.h diff --git a/arch/arm/mach-u67xx/Makefile b/arch/arm/mach-u67xx/Makefile index 38cf624..0572db8 100644 --- a/arch/arm/mach-u67xx/Makefile +++ b/arch/arm/mach-u67xx/Makefile @@ -5,7 +5,7 @@ ## Object file lists. # Common support -obj-y := devices.o +obj-y := devices.o cgu.o # Specific board support obj-$(CONFIG_MACH_U67XX_WAVEC_2GB) += board_u67xx_wavex.o diff --git a/arch/arm/mach-u67xx/cgu.c b/arch/arm/mach-u67xx/cgu.c new file mode 100644 index 0000000..58a6a7a --- /dev/null +++ b/arch/arm/mach-u67xx/cgu.c @@ -0,0 +1,760 @@ +/* + * arch/arm/mach-u67xx/cgu.c + * + * Copyright (C) ST-Ericsson SA 2010 + * Author: Arnaud Troel for ST-Ericsson. + * License terms: GNU General Public License (GPL), version 2 + * + * U67XX Clock Gating Unit basic driver/wrapper + * Based on the code from Michel Jaouen + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "clock_u67xx.h" + +#define MODULE_NAME "U6_CGU" +#define PKMOD MODULE_NAME ": " + +#if defined(DEBUG_CGU) +#define debug(fmt, args...) \ + printk(PKMOD fmt, ## args) +#else +#define debug(fmt, args...) +#endif + +/** + * U6 hw low level access clock functions + **/ +static void u6_clk_disable(struct clk *clk); +static int u6_clk_enable(struct clk *clk); + +/** + * HW enable clock function. + **/ + +static int u67xx_cgu_enable_fake_clock(struct clk *clk) +{ + debug("u67xx_cgu_enable_fake_clock for %s\n", clk->name); + return 0; +} + +static int u67xx_cgu_enable_hw_clock(struct clk *clk) +{ + unsigned long value; + debug("u67xx_cgu_enable_hw_clock for %s\n", clk->name); + + if (unlikely(clk->enable_reg == 0)) { + printk(KERN_ERR "Enable for %s without enable register\n", + clk->name); + return 0; + } + + value = readl(clk->enable_reg); + value |= 1 << clk->enable_bit; + writel(value, clk->enable_reg); + + return 0; +} + +static int u67xx_cgu_enable_tvo_pll_clock(struct clk *clk) +{ + unsigned long value; + + debug("u67xx_cgu_enable_tvo_pll_clock for %s\n", clk->name); + + if (unlikely(clk->enable_reg == 0)) { + printk(KERN_ERR "Enable for %s without enable register\n", + clk->name); + return 0; + } + + value = readl(clk->enable_reg); + value |= 1 << clk->enable_bit; + writel(value, clk->enable_reg); + + /* wait pll lock */ + do { + value = readl(clk->enable_reg); + } while (!(value & (0x1 << 16))); + + return 0; +} + +static int u67xx_cgu_enable_shared_clock(struct clk *clk) +{ + unsigned long value; + + debug("u67xx_cgu_enable_shared_clock for %s\n", clk->name); + + if (unlikely(clk->enable_reg == 0)) { + printk(KERN_ERR "Enable for %s without enable register\n", + clk->name); + return 0; + } + + value = readl(clk->enable_reg); + value |= 1 << clk->enable_bit; + writel(value, clk->enable_reg); + + return 0; +} + +static int u67xx_cgu_enable_clock(struct clk *clk) +{ + printk(PKMOD "No enable clock method for %s." + " You should use new one !!!\n", clk->name); + return 0; +} + +static int u67xx_cgu_enable_camout(struct clk *clk) +{ + unsigned long value; + debug("u67xx_cgu_enable_hw_clock for %s\n", clk->name); + + if (unlikely(clk->enable_reg == 0)) { + printk(KERN_ERR "Enable for %s without enable register\n", + clk->name); + return 0; + } + + u6_clk_enable(&fix_ck); + u6_clk_enable(&plltv_ck); + + value = readl(clk->enable_reg); + value |= 1 << clk->enable_bit; + writel(value, clk->enable_reg); + + udelay(1); + + u6_clk_disable(&fix_ck); + u6_clk_disable(&plltv_ck); + + return 0; +} + +/** + * HW disable clock function. + **/ + +static void u67xx_cgu_disable_fake_clock(struct clk *clk) +{ + debug("u67xx_cgu_disable_fake_clock for %s\n", clk->name); +} + +static void u67xx_cgu_disable_hw_clock(struct clk *clk) +{ + unsigned long value; + + debug("u67xx_cgu_disable_hw_clock for %s\n", clk->name); + + if (unlikely(clk->enable_reg == 0)) { + printk(KERN_ERR "Disable for %s without enable register\n", + clk->name); + return; + } + + value = readl(clk->enable_reg); + value &= ~(1 << clk->enable_bit); + writel(value, clk->enable_reg); + + return; +} + +static void u67xx_cgu_disable_shared_clock(struct clk *clk) +{ + unsigned long value; + + debug("u67xx_cgu_disable_shared_clock for %s\n", clk->name); + + if (unlikely(clk->enable_reg == 0)) { + printk(KERN_ERR "Disable for %s without enable register\n", + clk->name); + return; + } + + value = readl(clk->enable_reg); + value &= ~(1 << clk->enable_bit); + writel(value, clk->enable_reg); +} + +static void u67xx_cgu_disable_clock(struct clk *clk) +{ + printk(PKMOD + " No disable clock method for %s. You should use one !!!\n", + clk->name); +} + +/** + * set rate clock function. + **/ + +static int u6_clk_set_rate_fci(struct clk *clk, unsigned long rate) +{ + int ret = 0; + unsigned long ratio = 0; + unsigned long reg; + + if (!rate) + ratio = 31; + else { + if (rate > CLK_FC_CLK_MAX_FREQ) + rate = CLK_FC_CLK_MAX_FREQ; + + /* compute diviser ratio */ + ratio = (clk->parent->rate / rate) - 1; + if (ratio < 2) + ratio = 2; + if (ratio > 31) + ratio = 31; + } + clk->rate = clk->parent->rate / (ratio + 1); + + /* for voltage working point only */ + /* u67xx_set_freq(FC_CLK, clk->rate/1000 , clk->rate/1000); */ + + reg = readl(CGU_FIXCON_REG); + reg &= ~(0x1F << 22); + reg |= (ratio << 22); + writel(reg, CGU_FIXCON_REG); + + reg = readl(CGU_SCCON_REG); + writel(reg, CGU_SCCON_REG); + + return ret; +} + +static int u6_clk_set_rate_camout(struct clk *clk, unsigned long rate) +{ + int ret = 0; + unsigned long ratio = 0; + unsigned long reg; + + if (!rate) + return -EINVAL; + + if (rate > CLK_CAM_CLK_MAX_FREQ) + rate = CLK_CAM_CLK_MAX_FREQ; + + /* compute diviser ratio */ + ratio = (clk->parent->rate / rate) - 1; + if (ratio < 1) + ratio = 1; + if (ratio > 63) + ratio = 63; + + clk->rate = clk->parent->rate / (ratio + 1); + + /* for voltage working point only */ + /* u67xx_set_freq(CAM_CLK, clk->rate/1000 , clk->rate/1000); */ + + reg = readl(CGU_CAMCON_REG); + + reg &= ~(0x3F << 0); + reg |= (ratio << 0); + + writel(reg, CGU_CAMCON_REG); + + return ret; +} + +/** + * set parent clock function. + **/ +static int u6_clk_set_parent_camout(struct clk *clk, struct clk *parent) +{ + int ret = 0; + unsigned long srce = 0; + unsigned long reg; + + if (!strcmp(parent->name, "TVOPLL")) + srce = 0; + else if (!strcmp(parent->name, "fix_ck")) + srce = 1; + else + return -EINVAL; + + u6_clk_enable(&fix_ck); + u6_clk_enable(&plltv_ck); + + reg = readl(CGU_CAMCON_REG); + + reg &= ~(0x1 << 6); + reg |= (srce << 6); + + writel(reg, CGU_CAMCON_REG); + + udelay(1); + + u6_clk_disable(&fix_ck); + u6_clk_disable(&plltv_ck); + + clk->parent = parent; + + return ret; +} + +static int u6_clk_set_parent_uart(struct clk *clk, struct clk *parent) +{ + + if (!strcmp(parent->name, "pclk2_ck")) { + clk->parent = parent; + return 0; + } else if (!strcmp(parent->name, "clk26m_ck")) { + clk->parent = parent; + return 0; + } else if (!strcmp(parent->name, "clk13m_ck")) { + clk->parent = parent; + return 0; + } else { + return -1; + } +} + +/** + * HW init clock function. + **/ + +static void u6_clk_dflt_init(struct clk *clk) +{ + if (clk->usecount == 0) + u67xx_cgu_disable_hw_clock(clk); + else + u67xx_cgu_enable_hw_clock(clk); + /* default rate to parent one */ + clk->rate = clk->parent->rate; +} + +static void u6_clk_init_hclk2(struct clk *clk) +{ + /* nothing to do */ +} + +static void u6_clk_init_fci(struct clk *clk) +{ + u6_clk_set_rate_fci(clk, clk->rate); +} + +static void u6_clk_init_camout(struct clk *clk) +{ + u6_clk_set_parent_camout(clk, &fix_ck); + u6_clk_set_rate_camout(clk, clk->rate); +} + +/* + * Update clock rate + */ +static void followparent_ivs_recalc(struct clk *clk) +{ + clk->rate = clk->parent->rate; + propagate_rate(clk); +} + +/* + * U6 platform access clock functions + */ + +/** + * @brief Disable a device clock. + * @param clk clock description structure + */ +static void u6_clk_disable(struct clk *clk) +{ + /* BUG_ON(!clk->ref_count); */ + if ((clk->flags & ALWAYS_ENABLED) == ALWAYS_ENABLED) + return; + + if (clk->usecount > 0 && !(--clk->usecount)) { + if (clk->disable) + clk->disable(clk); + else + u67xx_cgu_disable_clock(clk); + + if (likely((u32) clk->parent)) + u6_clk_disable(clk->parent); + } +} + +/** + * @brief Enable a device clock. + * @param clk clock description structure + */ +static int u6_clk_enable(struct clk *clk) +{ + int ret = 0; + + /* BUG_ON(!clk->ref_count); */ + if ((clk->flags & ALWAYS_ENABLED) == ALWAYS_ENABLED) + return 0; + + if (clk->usecount++ == 0) { + if (likely((u32) clk->parent)) + ret = u6_clk_enable(clk->parent); + + if (unlikely(ret != 0)) { + clk->usecount--; + return ret; + } + + if (clk->enable) + ret = clk->enable(clk); + else + ret = u67xx_cgu_enable_clock(clk); + + if (unlikely(ret != 0) && clk->parent) { + u6_clk_disable(clk->parent); + clk->usecount--; + } + } + return 0; +} + +static long u6_clk_round_rate(struct clk *clk, unsigned long rate) +{ + if (clk->round_rate) + return clk->round_rate(clk, rate); + else + return rate; +} + +static int u6_clk_set_rate(struct clk *clk, unsigned long rate) +{ + int ret = 0; + + /* BUG_ON(!clk->ref_count); */ + if (clk->usecount > 0) { + if (clk->disable) + clk->disable(clk); + else + u67xx_cgu_disable_clock(clk); + } + + if (clk->set_rate) + ret = clk->set_rate(clk, rate); + else + ret = -EINVAL; + + if (clk->usecount > 0) { + if (clk->enable) + clk->enable(clk); + else + u67xx_cgu_enable_clock(clk); + } + + /* propagate rate */ + propagate_rate(clk); + + return ret; +} + +static int u6_clk_set_parent(struct clk *clk, struct clk *parent) +{ + int ret = 0; + int old_rate; + + if (clk->usecount > 0) { + if (clk->disable) + clk->disable(clk); + else + u67xx_cgu_disable_clock(clk); + + if (likely((u32) clk->parent)) + u6_clk_disable(clk->parent); + } + + old_rate = clk->rate; + + if (clk->set_parent) + ret = clk->set_parent(clk, parent); + else + ret = -EINVAL; + + if (clk->round_rate) + old_rate = clk->round_rate(clk, old_rate); + + u6_clk_set_rate(clk, old_rate); + + if (clk->usecount > 0) { + if (likely((u32) clk->parent)) + u6_clk_enable(clk->parent); + + if (clk->enable) + ret = clk->enable(clk); + else + ret = u67xx_cgu_enable_clock(clk); + } + + /* propagate rate */ + if (clk->flags & RATE_PROPAGATES) + propagate_rate(clk); + + return ret; +} + +static struct clk *u6_clk_get_parent(struct clk *clk) +{ + return clk->parent; +} + +/* + * U6 platform clock constraint functions + */ + +unsigned long clk_get_hw_constraint(unsigned long reg_addr) +{ + unsigned long reg; + + reg = readl(reg_addr); + + return reg; +} + +ssize_t u6_clk_show_hw_rates(struct kobject *kobj, char *buf) +{ + int size = 0; + unsigned long reg, rate, msc, psc, mclk; + + size += sprintf(&buf[size], "--HW-- : \n"); + + mclk = clk_get_hw_constraint(CGU_FIXCON_REG); + mclk = osc_ck.rate / 1000000 / (((mclk & (0x3 << 17)) >> 17) + 1); + + reg = clk_get_hw_constraint(CGU_SCCON_REG); + + msc = (((reg & (0xF << 25)) >> 25) + 1); + psc = (((reg & (0x3 << 15)) >> 15) + 1); + size += sprintf(&buf[size], "arm_ck is "); + if (reg & (1 << 17)) { + if (reg & (1 << 18)) + rate = 312; + else + rate = 156; + rate = rate / msc / psc; + size += sprintf(&buf[size], "%lu", rate); + size += sprintf(&buf[size], "Mhz fix_ck pll\n"); + } else { + rate = mclk * (((reg & (0xFF << 3)) >> 3) + 1) + / (((reg & (0xF << 11)) >> 11) + 1); + rate = rate / msc / psc; + size += sprintf(&buf[size], "%lu", rate); + size += sprintf(&buf[size], "Mhz sc_ck pll\n"); + } + + rate = rate / (((reg & (0xF << 21)) >> 21) + 1); + + size += sprintf(&buf[size], "hclk_ck is %luMhz\n", rate); + + reg = clk_get_hw_constraint(CGU_FIXCON_REG); + + rate = 13 * (((reg & (0x3 << 20)) >> 20) + 1); + + size += sprintf(&buf[size], "pclk2_ck is %luMhz\n", rate); + + if (reg & (1 << 31)) { + if (reg & (1 << 29)) + rate = 104; + else + rate = 78; + } else + rate = 13 + (13 * ((reg & (0x7 << 29)) >> 29)); + + size += sprintf(&buf[size], "hclk2_ck is %luMhz\n", rate); + + reg = clk_get_hw_constraint(CGU_SDMCON_REG); + + msc = (((reg & (0x7 << 0)) >> 0) + 1); + + size += sprintf(&buf[size], "sdm_ck is "); + if (reg & (1 << 17)) { + if (reg & (1 << 18)) + rate = 312; + else + rate = 156; + size += sprintf(&buf[size], "%lu", rate / msc); + size += sprintf(&buf[size], "Mhz fix_ck pll\n"); + } else { + rate = mclk * 2 * (((reg & (0xFF << 3)) >> 3) + 1) + / (((reg & (0x3F << 11)) >> 11) + 1); + size += sprintf(&buf[size], "%lu", rate / msc); + size += sprintf(&buf[size], "Mhz sdm pll\n"); + } + + if (reg & (1 << 23)) + size += sprintf(&buf[size], "ivs-sdm concentrator is enable\n"); + else + size += sprintf(&buf[size], + "ivs-sdm concentrator is disable\n"); + + return size; +} + +unsigned char *cgu_name_sc1[32] = { + "EBI", "01", "NFI", "SDI", "04", "MSI", "UCC", "JDI", + "08", "09", "10", "DMAU", "RFSM1", "IIS", "USBD", "FCI", + "USIM", "GEAC", "PWM3", "PWM2", "PWM1", "KBS", "GPIO", "UART2", + "UART1", "IIC2", "IIC1", "SPI2", "SPI1", "SCTU", "EXTINT", "INTC" +}; + +unsigned char *cgu_name_sc2[32] = { + "32", "33", "34", "35", "36", "37", "38", "39", + "40", "41", "42", "43", "44", "45", "46", "47", + "48", "49", "50", "51", "52", "53", "BBIP", "55", + "56", "57", "58", "ETB", "60", "MMTU", "62", "CAE" +}; + +ssize_t u6_clk_show_sc_constraints(struct kobject *kobj, char *buf) +{ + int i, size = 0; + unsigned long reg; + + size += sprintf(&buf[size], "--SC-- : \n"); + + reg = clk_get_hw_constraint(CGU_GATESC1_REG); + + for (i = 0; i < 32; i++) { + if ((reg >> i) & 0x1) + size += + sprintf(&buf[size], "%s ,", cgu_name_sc1[31 - i]); + } + + size += sprintf(&buf[size], "\n"); + + reg = clk_get_hw_constraint(CGU_GATESC2_REG); + + for (i = 0; i < 32; i++) { + if ((reg >> i) & 0x1) + size += + sprintf(&buf[size], "%s ,", cgu_name_sc2[31 - i]); + } + + size += sprintf(&buf[size], "\n"); + + return size; +} + +unsigned char *cgu_name_ivs[32] = { + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "CAMJPE", "TVO", "JDU", "VDC", "VEC", "IPP", "VDE", "CAM" +}; + +ssize_t u6_clk_show_ivs_constraints(struct kobject *kobj, char *buf) +{ + int i, size = 0; + unsigned long reg; + + size += sprintf(&buf[size], "--IVS-- : \n"); + + reg = clk_get_hw_constraint(CGU_GATEIVS_REG); + + for (i = 0; i < 32; i++) { + if ((reg >> i) & 0x1) + size += + sprintf(&buf[size], "%s ,", cgu_name_ivs[31 - i]); + } + + size += sprintf(&buf[size], "\n"); + + return size; +} + +ssize_t u6_clk_show_tvo_constraints(struct kobject *kobj, char *buf) +{ + int size = 0; + unsigned long reg; + + size += sprintf(&buf[size], "--TVO-- : \n"); + + reg = clk_get_hw_constraint(CGU_TVCON_REG); + + if ((reg >> 15) & 0x1) + size += sprintf(&buf[size], "tvclk_ck pll enable\n"); + else + size += sprintf(&buf[size], "tvclk_ck pll disable\n"); + + if ((reg >> 12) & 0x1) + size += sprintf(&buf[size], "TVOPLL enable\n"); + else + size += sprintf(&buf[size], "TVOPLL disable\n"); + + return size; +} + +ssize_t u6_clk_show_cam_constraints(struct kobject *kobj, char *buf) +{ + int size = 0; + unsigned long reg, rate, div; + + size += sprintf(&buf[size], "--CAM-- : \n"); + + reg = clk_get_hw_constraint(CGU_CAMCON_REG); + + if ((reg >> 6) & 0x1) { + size += sprintf(&buf[size], "fix_clock is source\n"); + rate = 312; + } else { + size += sprintf(&buf[size], "TVOPLL is source\n"); + rate = 216; + } + + div = ((reg & (0x3F << 0)) >> 0); + + size += sprintf(&buf[size], "camo_ck is %luMhz\n", rate / (div + 1)); + + if ((reg >> 7) & 0x1) + size += sprintf(&buf[size], "camo_ck enable\n"); + else + size += sprintf(&buf[size], "camo_ck disable\n"); + + return size; +} + +/* + * U6 clock reset and init functions + */ + +static struct clk_functions u6_clk_functions = { + .clk_enable = u6_clk_enable, + .clk_disable = u6_clk_disable, + .clk_round_rate = u6_clk_round_rate, + .clk_set_rate = u6_clk_set_rate, + .clk_set_parent = u6_clk_set_parent, + .clk_get_parent = u6_clk_get_parent, +}; + +/** + * @brief Attempts to connect the primary description structure for DMA case. + * + * We don't do any initialization since we expect the primary OS to have done + * it for us. + */ +static int __init u67xx_cgu_init(void) +{ + struct clk **clkp; + printk(PKMOD "clk management init\n"); + + if (mpurate) + sc_ck.rate = mpurate; + + /* init clock function pointer table */ + clk_init(&u6_clk_functions); + + /* register and init clock elements */ + for (clkp = onchip_clks; clkp < onchip_clks + ARRAY_SIZE(onchip_clks); + clkp++) { + clk_register(*clkp); + } + + return 0; +} + +arch_initcall(u67xx_cgu_init); diff --git a/arch/arm/mach-u67xx/clock_u67xx.h b/arch/arm/mach-u67xx/clock_u67xx.h new file mode 100644 index 0000000..d92e454 --- /dev/null +++ b/arch/arm/mach-u67xx/clock_u67xx.h @@ -0,0 +1,876 @@ +/* + * linux/arch/arm/mach-u67xx/clock_u67xx.h + * + * Copyright (C) ST-Ericsson SA 2010 + * Author: Loic Pallardy + * Copyright (C) 2010 ST-Ericsson SA + * + * Modified for omap shared clock framework by Tony Lindgren + * + * Modified for u6 shared clock framework by Loic Pallardy @ ST-Ericsson.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#ifdef CONFIG_U6_POWER_SYSFS +#include "power_sysfs.h" +#endif + +LIST_HEAD(clocks); + +static DEFINE_MUTEX(clocks_mutex); +DEFINE_SPINLOCK(clockfw_lock); + +static struct clk_functions *arch_clock; + +/* + * Additionnal specific clock functions define in include/mach/clock.h + */ + +/** + * clk_register_notifier - register a driver with clock mngt + * @nb: notifier function to register + * @clk: clock element rate change to be notified + * + * Add a driver to one of two lists: either a list of drivers that + * are notified about clock rate changes (once before and once after + * the transition), + * This function may sleep, and has the same return conditions as + * sru_blocking_notifier_chain_register. + */ +int clk_register_notifier(struct notifier_block *nb, struct clk *clk) +{ + int ret; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + mutex_lock(&clocks_mutex); + if (clk->notifier) + ret = srcu_notifier_chain_register(clk->notifier, nb); + else + ret = -EINVAL; + mutex_unlock(&clocks_mutex); + + return ret; +} +EXPORT_SYMBOL(clk_register_notifier); + +/** + * clk_unregister_notifier - unregister a driver with clock mngt + * @nb: notifier function to be unregistered + * @clk: clock element rate change to be notified + * + * Remove a driver from the clock mngt notifier list. + * + * This function may sleep, and has the same return conditions as + * sru_blocking_notifier_chain_unregister. + */ +int clk_unregister_notifier(struct notifier_block *nb, struct clk *clk) +{ + int ret; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + mutex_lock(&clocks_mutex); + if (clk->notifier) + ret = srcu_notifier_chain_unregister(clk->notifier, nb); + else + ret = -EINVAL; + mutex_unlock(&clocks_mutex); + + return ret; +} +EXPORT_SYMBOL(clk_unregister_notifier); + +/** + * clk_notify_transition - call notifier chain + * + * This function calls the transition notifiers and the and set rate + * It is called twice on all frequency changes that have + * external effects. + */ +int clk_notify_transition(struct clk *clk, unsigned int state, void *data) +{ + BUG_ON(irqs_disabled()); + + if (clk == ERR_PTR(-ENOENT)) + return NOTIFY_BAD; + + switch (state) { + + case CLK_RATE_PRECHANGE: + case CLK_RATE_POSTCHANGE: +/* printk("notif %u of frequency transition of %s to %lu Hz\n", + state, clk->name, (unsigned long)data); +*/ + break; + + case CLK_SRCE_PRECHANGE: + case CLK_SRCE_POSTCHANGE: +/* printk("notif %u of srce transition of %s to %s\n", + state, clk->name, ((struct clk *)data)->name); +*/ + break; + default: + printk(KERN_ERR "Unknown notification srce transition\n"); + return NOTIFY_BAD; + break; + } + + return srcu_notifier_call_chain(clk->notifier, state, data); +} + +/* + * Standard clock functions defined in include/linux/clk.h + */ + +struct clk *clk_get(struct device *dev, const char *id) +{ + struct clk *p, *clk = ERR_PTR(-ENOENT); + + mutex_lock(&clocks_mutex); + list_for_each_entry(p, &clocks, node) { + if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) { + clk = p; + break; + } + } + mutex_unlock(&clocks_mutex); + + return clk; +} +EXPORT_SYMBOL(clk_get); + +int clk_enable(struct clk *clk) +{ + unsigned long flags; + int ret = 0; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_enable) + ret = arch_clock->clk_enable(clk); + spin_unlock_irqrestore(&clockfw_lock, flags); + + return ret; +} +EXPORT_SYMBOL(clk_enable); + +void clk_disable(struct clk *clk) +{ + unsigned long flags; + + if (clk == ERR_PTR(-ENOENT)) + return; + + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_disable) + arch_clock->clk_disable(clk); + spin_unlock_irqrestore(&clockfw_lock, flags); +} +EXPORT_SYMBOL(clk_disable); + +int clk_get_usecount(struct clk *clk) +{ + unsigned long flags; + int ret = 0; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + spin_lock_irqsave(&clockfw_lock, flags); + ret = clk->usecount; + spin_unlock_irqrestore(&clockfw_lock, flags); + + return ret; +} +EXPORT_SYMBOL(clk_get_usecount); + +unsigned long clk_get_rate(struct clk *clk) +{ + unsigned long flags; + unsigned long ret = 0; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + spin_lock_irqsave(&clockfw_lock, flags); + ret = clk->rate; + spin_unlock_irqrestore(&clockfw_lock, flags); + + return ret; +} +EXPORT_SYMBOL(clk_get_rate); + +void clk_put(struct clk *clk) +{ + if (clk && !IS_ERR(clk)) + module_put(clk->owner); +} +EXPORT_SYMBOL(clk_put); + +/* + * Optional clock functions defined in include/linux/clk.h + */ + +long clk_round_rate(struct clk *clk, unsigned long rate) +{ + unsigned long flags; + long ret = rate; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_round_rate) + ret = arch_clock->clk_round_rate(clk, rate); + spin_unlock_irqrestore(&clockfw_lock, flags); + + return ret; +} +EXPORT_SYMBOL(clk_round_rate); + +int clk_set_rate(struct clk *clk, unsigned long rate) +{ + unsigned long flags; + int ret = 0; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + /* notification pre change */ + if (clk->notifier) + ret = clk_notify_transition(clk, + CLK_RATE_PRECHANGE, (void *)rate); + + if (!(ret & NOTIFY_STOP_MASK)) { + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_set_rate) + ret = arch_clock->clk_set_rate(clk, rate); + spin_unlock_irqrestore(&clockfw_lock, flags); + + /* notification post change */ + if (clk->notifier) + clk_notify_transition(clk, + CLK_RATE_POSTCHANGE, + (void *)clk->rate); + } + + return ret; +} +EXPORT_SYMBOL(clk_set_rate); + +int clk_set_parent(struct clk *clk, struct clk *parent) +{ + unsigned long flags; + int ret = 0; + + if (clk == ERR_PTR(-ENOENT)) + return -EINVAL; + + /* notification pre change */ + if (clk->notifier) + ret = clk_notify_transition(clk, + CLK_SRCE_PRECHANGE, (void *)parent); + + if (!(ret & NOTIFY_STOP_MASK)) { + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_set_parent) + ret = arch_clock->clk_set_parent(clk, parent); + spin_unlock_irqrestore(&clockfw_lock, flags); + + /* notification post change */ + if (clk->notifier) + clk_notify_transition(clk, + CLK_SRCE_POSTCHANGE, + (void *)clk->parent); + } + + return ret; +} +EXPORT_SYMBOL(clk_set_parent); + +struct clk *clk_get_parent(struct clk *clk) +{ + unsigned long flags; + struct clk *ret = ERR_PTR(-ENOENT); + + if (clk == ERR_PTR(-ENOENT)) + return ret; + + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_get_parent) + ret = arch_clock->clk_get_parent(clk); + spin_unlock_irqrestore(&clockfw_lock, flags); + + return ret; +} +EXPORT_SYMBOL(clk_get_parent); + +/* + * Internal clock functions + */ + +unsigned int __initdata mpurate; +/* + * By default we use the rate set by the bootloader. + * You can override this with mpurate= cmdline option. + */ +static int __init u6_clk_setup(char *str) +{ + unsigned int tmp_rate; + + get_option(&str, &tmp_rate); + + if (!tmp_rate) + return 1; + + mpurate = tmp_rate; + + return 1; +} + +__setup("mpurate=", u6_clk_setup); + +/* Used for clocks that always have same value as the parent clock */ +void followparent_recalc(struct clk *clk) +{ + clk->rate = clk->parent->rate; +} + +/* Propagate rate to children */ +void propagate_rate(struct clk *tclk) +{ + struct clk *clkp; + + list_for_each_entry(clkp, &clocks, node) { + if (likely(clkp->parent != tclk)) + continue; + if (likely((u32) clkp->recalc)) + clkp->recalc(clkp); + } +} + +int clk_register(struct clk *clk) +{ + mutex_lock(&clocks_mutex); + list_add(&clk->node, &clocks); + if (clk->init) + clk->init(clk); + mutex_unlock(&clocks_mutex); + + return 0; +} +EXPORT_SYMBOL(clk_register); + +void clk_unregister(struct clk *clk) +{ + mutex_lock(&clocks_mutex); + list_del(&clk->node); + mutex_unlock(&clocks_mutex); +} +EXPORT_SYMBOL(clk_unregister); + +void clk_deny_idle(struct clk *clk) +{ + unsigned long flags; + + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_deny_idle) + arch_clock->clk_deny_idle(clk); + spin_unlock_irqrestore(&clockfw_lock, flags); +} +EXPORT_SYMBOL(clk_deny_idle); + +void clk_allow_idle(struct clk *clk) +{ + unsigned long flags; + + spin_lock_irqsave(&clockfw_lock, flags); + if (arch_clock->clk_allow_idle) + arch_clock->clk_allow_idle(clk); + spin_unlock_irqrestore(&clockfw_lock, flags); +} +EXPORT_SYMBOL(clk_allow_idle); + +#ifdef CONFIG_U6_POWER_SYSFS +/* + * SysFs interface + */ + +/*** Clock mngt sysfs interface **/ +static char clk_name[64] = "MMTU"; + +static ssize_t show_select(struct kobject *kobj, char *buf) +{ + return sprintf(buf, "%s \n", clk_name); +} + +static ssize_t store_select(struct kobject *kobj, const char *buf, size_t size) +{ + strcpy(clk_name, buf); + + clk_name[strlen(clk_name) - 1] = 0; + + return size; +} + +static ssize_t show_enable(struct kobject *kobj, char *buf) +{ + struct clk *clk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return sprintf(buf, "unable to get clk\n"); + clk_put(clk); + return sprintf(buf, "%d \n", clk_get_usecount(clk)); +} + +static ssize_t store_enable(struct kobject *kobj, const char *buf, size_t size) +{ + struct clk *clk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return size; + clk_enable(clk); + clk_put(clk); + return size; +} + +static ssize_t show_disable(struct kobject *kobj, char *buf) +{ + struct clk *clk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return sprintf(buf, "unable to get clk\n"); + clk_put(clk); + return sprintf(buf, "%d \n", clk_get_usecount(clk)); +} + +static ssize_t store_disable(struct kobject *kobj, const char *buf, size_t size) +{ + struct clk *clk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return size; + clk_disable(clk); + clk_put(clk); + return size; +} + +static ssize_t show_usecount(struct kobject *kobj, char *buf) +{ + struct clk *clk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return sprintf(buf, "unable to get clk\n"); + clk_put(clk); + return sprintf(buf, "%d \n", clk_get_usecount(clk)); +} + +static ssize_t show_rate(struct kobject *kobj, char *buf) +{ + struct clk *clk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return sprintf(buf, "unable to get clk\n"); + clk_put(clk); + return sprintf(buf, "%lu \n", clk_get_rate(clk)); +} + +static ssize_t store_rate(struct kobject *kobj, const char *buf, size_t size) +{ + struct clk *clk; + unsigned long rate; + if (strict_strtoul(buf, 10, &rate) == 0) { + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return size; + clk_set_rate(clk, rate); + clk_put(clk); + } + return size; +} + +static ssize_t show_parent(struct kobject *kobj, char *buf) +{ + struct clk *clk, *pclk; + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return sprintf(buf, "unable to get clk\n"); + pclk = clk_get_parent(clk); + clk_put(clk); + if ((pclk) && (pclk->name)) + return sprintf(buf, "%s\n", pclk->name); + else + return sprintf(buf, "root\n"); +} + +static ssize_t store_parent(struct kobject *kobj, const char *buf, size_t size) +{ + struct clk *clk, *pclk; + char parent[32]; + strcpy(parent, buf); + parent[strlen(parent) - 1] = 0; + + clk = clk_get(NULL, clk_name); + if (clk == ERR_PTR(-ENOENT)) + return size; + + pclk = clk_get(NULL, parent); + if (pclk == ERR_PTR(-ENOENT)) { + clk_put(clk); + return size; + } + clk_put(pclk); + clk_set_parent(clk, pclk); + clk_put(clk); + return size; +} + +static ssize_t show_list(struct kobject *kobj, char *buf) +{ + int size = 0; + + struct clk *p; + + list_for_each_entry(p, &clocks, node) { + size += sprintf(&buf[size], "%s\n", p->name); + } + return size; +} + +/*** Clock mngt hw debug ***/ +static ssize_t show_hw_constraints(struct kobject *kobj, char *buf) +{ + int size = 0; + + size += u6_clk_show_hw_rates(kobj, &buf[size]); + + size += u6_clk_show_sc_constraints(kobj, &buf[size]); + + size += u6_clk_show_ivs_constraints(kobj, &buf[size]); + + size += u6_clk_show_tvo_constraints(kobj, &buf[size]); + + size += u6_clk_show_cam_constraints(kobj, &buf[size]); + + return size; +} + +static ssize_t show_hw_rates(struct kobject *kobj, char *buf) +{ + int size = 0; + + size += u6_clk_show_hw_rates(kobj, buf); + + return size; +} + +define_one_ro(hw_rates); +define_one_ro(hw_constraints); +define_one_rw(parent); +define_one_rw(rate); +define_one_ro(usecount); +define_one_rw(disable); +define_one_rw(enable); +define_one_rw(select); +define_one_ro(list); + +static struct attribute *dbs_attributes_clock[] = { + + &hw_rates.attr, + &hw_constraints.attr, + &parent.attr, + &rate.attr, + &usecount.attr, + &disable.attr, + &enable.attr, + &select.attr, + &list.attr, + NULL +}; + +static struct attribute_group dbs_attr_clock_group = { + .attrs = dbs_attributes_clock, + .name = "clk_mngt", +}; + +#endif + +/* + * Init functions + */ + +int __init clk_init(struct clk_functions *custom_clocks) +{ + if (!custom_clocks) { + printk(KERN_ERR "No custom clock functions registered\n"); + BUG(); + } + + arch_clock = custom_clocks; + + return 0; +} + +#ifdef CONFIG_U6_POWER_SYSFS +static int __init u6_clock_init_sysfs(void) +{ + if (sysfs_create_group(&u6_power_kobj, &dbs_attr_clock_group)) + printk(KERN_ERR "Unable to register %s in sysfs\n", + dbs_attr_clock_group.name); + + return 0; +} + +module_init(u6_clock_init_sysfs); +#endif + diff --git a/arch/arm/plat-u6xxx/include/mach/clock.h b/arch/arm/plat-u6xxx/include/mach/clock.h new file mode 100644 index 0000000..bc83414 --- /dev/null +++ b/arch/arm/plat-u6xxx/include/mach/clock.h @@ -0,0 +1,376 @@ +/* + * linux/arch/arm/plat-u6xxx/include/mach/clock.h + * + * Copyright (C) ST-Ericsson SA 2010 + * Author: Loic Pallardy for ST-Ericsson + * License terms: GNU General Public License (GPL), version 2 + * + * Based on clocks.h by Tony Lindgren, Gordon McNutt and RidgeRun, Inc + */ + +#ifndef __PLAT_CLOCK_H +#define __PLAT_CLOCK_H + +struct module; + +struct clk { + struct list_head node; + struct module *owner; + const char *name; + struct clk *parent; + unsigned long rate; + __u32 flags; + u32 enable_reg; + __u8 enable_bit; + __u8 rate_offset; + __u8 src_offset; + __s8 usecount; + void (*recalc)(struct clk *); + int (*set_rate)(struct clk *, unsigned long); + long (*round_rate)(struct clk *, unsigned long); + void (*init)(struct clk *); + int (*enable)(struct clk *); + void (*disable)(struct clk *); + int (*set_parent)(struct clk *, struct clk *); + struct srcu_notifier_head *notifier; +}; + +struct clk_functions { + int (*clk_enable)(struct clk *clk); + void (*clk_disable)(struct clk *clk); + long (*clk_round_rate)(struct clk *clk, unsigned long rate); + int (*clk_set_rate)(struct clk *clk, unsigned long rate); + int (*clk_set_parent)(struct clk *clk, struct clk *parent); + struct clk * (*clk_get_parent)(struct clk *clk); + void (*clk_allow_idle)(struct clk *clk); + void (*clk_deny_idle)(struct clk *clk); +}; + +extern unsigned int mpurate; +extern struct list_head clocks; +extern spinlock_t clockfw_lock; + +extern int clk_init(struct clk_functions *custom_clocks); +extern int clk_register(struct clk *clk); +extern void clk_unregister(struct clk *clk); + +extern void propagate_rate(struct clk *clk); +extern void followparent_recalc(struct clk *clk); +extern void clk_allow_idle(struct clk *clk); +extern void clk_deny_idle(struct clk *clk); +extern int clk_get_usecount(struct clk *clk); + +extern ssize_t u6_clk_show_hw_rates(struct kobject *kobj, char *buf); +extern ssize_t u6_clk_show_sc_constraints(struct kobject *kobj, char *buf); +extern ssize_t u6_clk_show_usb_constraints(struct kobject *kobj, char *buf); +extern ssize_t u6_clk_show_ivs_constraints(struct kobject *kobj, char *buf); +extern ssize_t u6_clk_show_tvo_constraints(struct kobject *kobj, char *buf); +extern ssize_t u6_clk_show_cam_constraints(struct kobject *kobj, char *buf); + +extern unsigned long clk_get_hw_constraint(unsigned long reg_addr); + + +/* Clock flags */ +#define RATE_CKCTL (1 << 0) /* Main fixed ratio clocks */ +#define RATE_FIXED (1 << 1) /* Fixed clock rate */ +#define RATE_PROPAGATES (1 << 2) /* Program children too */ +#define VIRTUAL_CLOCK (1 << 3) /* Composite clock from table */ +#define ALWAYS_ENABLED (1 << 4) /* Clock cannot be disabled */ +#define ENABLE_REG_32BIT (1 << 5) /* Use 32-bit access */ +#define VIRTUAL_IO_ADDRESS (1 << 6) /* Clock in virtual address */ +#define CLOCK_IDLE_CONTROL (1 << 7) +#define CLOCK_NO_IDLE_PARENT (1 << 8) +#define DELAYED_APP (1 << 9) /* Delay application of clock */ +#define CONFIG_PARTICIPANT (1 << 10) /* Fundamental clock */ + + +#define CLK_HCLK2_MAX_FREQ 104000000 +#define CLK_FC_CLK_MAX_FREQ 104000000 +#define CLK_CAM_CLK_MAX_FREQ 78000000 + +/** + * clk_register_notifier - register a driver with clock mngt + * @nb: notifier function to register + * @clk: clock element rate change to be notified + */ +extern int clk_register_notifier(struct notifier_block *nb, struct clk *clk); + +/** + * clk_unregister_notifier - unregister a driver with clock mngt + * @nb: notifier function to register + * @clk: clock element rate change to be notified + */ +extern int clk_unregister_notifier(struct notifier_block *nb, struct clk *clk); + +/** + * clk_notify_transition - call notifier chain + * + * This function calls the transition notifiers and the and set rate + * It is called twice on all frequency changes that have + * external effects. + */ +extern int clk_notify_transition(struct clk *clk, + unsigned int state, void *data); + +#define CLK_RATE_PRECHANGE 0 +#define CLK_RATE_POSTCHANGE 1 +#define CLK_SRCE_PRECHANGE 2 +#define CLK_SRCE_POSTCHANGE 3 + +/* Hardware Register Definitions CGU (Clock Gating Unit) */ + +/* CGU DSP1CON Register (32 bits) */ +#define CGU_DSP1CON_OFFSET 0x0 +#define CGU_DSP1CON_REG IO_ADDRESS(CGU_BASE + CGU_DSP1CON_OFFSET) + +/* CGU SCCON Register (32 bits) */ +#define CGU_SCCON_OFFSET 0x4 +#define CGU_SCCON_REG IO_ADDRESS(CGU_BASE + CGU_SCCON_OFFSET) + +/* CGU GATESC1 Register (32 bits) */ +#define CGU_GATESC1_OFFSET 0x8 +#define CGU_GATESC1_REG IO_ADDRESS(CGU_BASE + CGU_GATESC1_OFFSET) + +/* CGU SLEEPSC Register (32 bits) */ +#define CGU_SLEEPSC_OFFSET 0xC +#define CGU_SLEEPSC_REG IO_ADDRESS(CGU_BASE + CGU_SLEEPSC_OFFSET) + +/* CGU FDIV Register (32 bits) */ +#define CGU_FDIV_OFFSET 0x10 +#define CGU_FDIV_REG IO_ADDRESS(CGU_BASE + CGU_FDIV_OFFSET) + +/* CGU GATEIVS Register (32 bits) */ +#define CGU_GATEIVS_OFFSET 0x14 +#define CGU_GATEIVS_REG IO_ADDRESS(CGU_BASE + CGU_GATEIVS_OFFSET) + +/* CGU DSP2CON Register (32 bits) */ +#define CGU_DSP2CON_OFFSET 0x18 +#define CGU_DSP2CON_REG IO_ADDRESS(CGU_BASE + CGU_DSP2CON_OFFSET) + +/* CGU CAMCON Register (32 bits) */ +#define CGU_CAMCON_OFFSET 0x1C +#define CGU_CAMCON_REG IO_ADDRESS(CGU_BASE + CGU_CAMCON_OFFSET) + +/* CGU GATESC2 Register (32 bits) */ +#define CGU_GATESC2_OFFSET 0x20 +#define CGU_GATESC2_REG IO_ADDRESS(CGU_BASE + CGU_GATESC2_OFFSET) + +/* CGU SDMCON Register (32 bits) */ +#define CGU_SDMCON_OFFSET 0x24 +#define CGU_SDMCON_REG IO_ADDRESS(CGU_BASE + CGU_SDMCON_OFFSET) + +/* CGU TVCON Register (32 bits) */ +#define CGU_TVCON_OFFSET 0x28 +#define CGU_TVCON_REG IO_ADDRESS(CGU_BASE + CGU_TVCON_OFFSET) + +/* CGU IDLESC Register (32 bits) */ +#define CGU_IDLESC_OFFSET 0x2C +#define CGU_IDLESC_REG IO_ADDRESS(CGU_BASE + CGU_IDLESC_OFFSET) + +/* CGU FIXCON Register (32 bits) */ +#define CGU_FIXCON_OFFSET 0x30 +#define CGU_FIXCON_REG IO_ADDRESS(CGU_BASE + CGU_FIXCON_OFFSET) + +/* CGU CNTCON Register (32 bits) */ +#define CGU_CNTCON_OFFSET 0x38 +#define CGU_CNTCON_REG IO_ADDRESS(CGU_BASE + CGU_CNTCON_OFFSET) + +/* Register description for GATESC1 */ + +/* Bits definition for register CGU_GATESC1 */ +#define CGU_EBIEN_SHIFT 31 +#define CGU_EBIEN_FIELD (0xFFFFFFFF - (0x1UL< #include #include +#include #include -- 1.7.1