18f8f484bSPrashant Gaikwad /* 28f8f484bSPrashant Gaikwad * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 38f8f484bSPrashant Gaikwad * 48f8f484bSPrashant Gaikwad * This program is free software; you can redistribute it and/or modify it 58f8f484bSPrashant Gaikwad * under the terms and conditions of the GNU General Public License, 68f8f484bSPrashant Gaikwad * version 2, as published by the Free Software Foundation. 78f8f484bSPrashant Gaikwad * 88f8f484bSPrashant Gaikwad * This program is distributed in the hope it will be useful, but WITHOUT 98f8f484bSPrashant Gaikwad * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 108f8f484bSPrashant Gaikwad * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 118f8f484bSPrashant Gaikwad * more details. 128f8f484bSPrashant Gaikwad * 138f8f484bSPrashant Gaikwad * You should have received a copy of the GNU General Public License 148f8f484bSPrashant Gaikwad * along with this program. If not, see <http://www.gnu.org/licenses/>. 158f8f484bSPrashant Gaikwad */ 168f8f484bSPrashant Gaikwad 178f8f484bSPrashant Gaikwad #include <linux/clk.h> 188f8f484bSPrashant Gaikwad #include <linux/clk-provider.h> 198f8f484bSPrashant Gaikwad #include <linux/slab.h> 208f8f484bSPrashant Gaikwad #include <linux/io.h> 218f8f484bSPrashant Gaikwad #include <linux/delay.h> 228f8f484bSPrashant Gaikwad #include <linux/err.h> 23*306a7f91SThierry Reding 24*306a7f91SThierry Reding #include <soc/tegra/fuse.h> 258f8f484bSPrashant Gaikwad 268f8f484bSPrashant Gaikwad #include "clk.h" 278f8f484bSPrashant Gaikwad 288f8f484bSPrashant Gaikwad static DEFINE_SPINLOCK(periph_ref_lock); 298f8f484bSPrashant Gaikwad 308f8f484bSPrashant Gaikwad /* Macros to assist peripheral gate clock */ 318f8f484bSPrashant Gaikwad #define read_enb(gate) \ 328f8f484bSPrashant Gaikwad readl_relaxed(gate->clk_base + (gate->regs->enb_reg)) 338f8f484bSPrashant Gaikwad #define write_enb_set(val, gate) \ 348f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg)) 358f8f484bSPrashant Gaikwad #define write_enb_clr(val, gate) \ 368f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg)) 378f8f484bSPrashant Gaikwad 388f8f484bSPrashant Gaikwad #define read_rst(gate) \ 398f8f484bSPrashant Gaikwad readl_relaxed(gate->clk_base + (gate->regs->rst_reg)) 408f8f484bSPrashant Gaikwad #define write_rst_clr(val, gate) \ 418f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg)) 428f8f484bSPrashant Gaikwad 435a88b0d1SYen Lin #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32)) 448f8f484bSPrashant Gaikwad 45fdcccbd8SPeter De Schrijver #define LVL2_CLK_GATE_OVRE 0x554 46fdcccbd8SPeter De Schrijver 478f8f484bSPrashant Gaikwad /* Peripheral gate clock ops */ 488f8f484bSPrashant Gaikwad static int clk_periph_is_enabled(struct clk_hw *hw) 498f8f484bSPrashant Gaikwad { 508f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); 518f8f484bSPrashant Gaikwad int state = 1; 528f8f484bSPrashant Gaikwad 538f8f484bSPrashant Gaikwad if (!(read_enb(gate) & periph_clk_to_bit(gate))) 548f8f484bSPrashant Gaikwad state = 0; 558f8f484bSPrashant Gaikwad 568f8f484bSPrashant Gaikwad if (!(gate->flags & TEGRA_PERIPH_NO_RESET)) 578f8f484bSPrashant Gaikwad if (read_rst(gate) & periph_clk_to_bit(gate)) 588f8f484bSPrashant Gaikwad state = 0; 598f8f484bSPrashant Gaikwad 608f8f484bSPrashant Gaikwad return state; 618f8f484bSPrashant Gaikwad } 628f8f484bSPrashant Gaikwad 638f8f484bSPrashant Gaikwad static int clk_periph_enable(struct clk_hw *hw) 648f8f484bSPrashant Gaikwad { 658f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); 668f8f484bSPrashant Gaikwad unsigned long flags = 0; 678f8f484bSPrashant Gaikwad 688f8f484bSPrashant Gaikwad spin_lock_irqsave(&periph_ref_lock, flags); 698f8f484bSPrashant Gaikwad 708f8f484bSPrashant Gaikwad gate->enable_refcnt[gate->clk_num]++; 718f8f484bSPrashant Gaikwad if (gate->enable_refcnt[gate->clk_num] > 1) { 728f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 738f8f484bSPrashant Gaikwad return 0; 748f8f484bSPrashant Gaikwad } 758f8f484bSPrashant Gaikwad 768f8f484bSPrashant Gaikwad write_enb_set(periph_clk_to_bit(gate), gate); 778f8f484bSPrashant Gaikwad udelay(2); 788f8f484bSPrashant Gaikwad 798f8f484bSPrashant Gaikwad if (!(gate->flags & TEGRA_PERIPH_NO_RESET) && 808f8f484bSPrashant Gaikwad !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) { 818f8f484bSPrashant Gaikwad if (read_rst(gate) & periph_clk_to_bit(gate)) { 828f8f484bSPrashant Gaikwad udelay(5); /* reset propogation delay */ 838f8f484bSPrashant Gaikwad write_rst_clr(periph_clk_to_bit(gate), gate); 848f8f484bSPrashant Gaikwad } 858f8f484bSPrashant Gaikwad } 868f8f484bSPrashant Gaikwad 87fdcccbd8SPeter De Schrijver if (gate->flags & TEGRA_PERIPH_WAR_1005168) { 88fdcccbd8SPeter De Schrijver writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE); 89fdcccbd8SPeter De Schrijver writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE); 90fdcccbd8SPeter De Schrijver udelay(1); 91fdcccbd8SPeter De Schrijver writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE); 92fdcccbd8SPeter De Schrijver } 93fdcccbd8SPeter De Schrijver 948f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 958f8f484bSPrashant Gaikwad 968f8f484bSPrashant Gaikwad return 0; 978f8f484bSPrashant Gaikwad } 988f8f484bSPrashant Gaikwad 998f8f484bSPrashant Gaikwad static void clk_periph_disable(struct clk_hw *hw) 1008f8f484bSPrashant Gaikwad { 1018f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); 1028f8f484bSPrashant Gaikwad unsigned long flags = 0; 1038f8f484bSPrashant Gaikwad 1048f8f484bSPrashant Gaikwad spin_lock_irqsave(&periph_ref_lock, flags); 1058f8f484bSPrashant Gaikwad 1068f8f484bSPrashant Gaikwad gate->enable_refcnt[gate->clk_num]--; 1078f8f484bSPrashant Gaikwad if (gate->enable_refcnt[gate->clk_num] > 0) { 1088f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 1098f8f484bSPrashant Gaikwad return; 1108f8f484bSPrashant Gaikwad } 1118f8f484bSPrashant Gaikwad 1128f8f484bSPrashant Gaikwad /* 1138f8f484bSPrashant Gaikwad * If peripheral is in the APB bus then read the APB bus to 1148f8f484bSPrashant Gaikwad * flush the write operation in apb bus. This will avoid the 1158f8f484bSPrashant Gaikwad * peripheral access after disabling clock 1168f8f484bSPrashant Gaikwad */ 1178f8f484bSPrashant Gaikwad if (gate->flags & TEGRA_PERIPH_ON_APB) 1188f8f484bSPrashant Gaikwad tegra_read_chipid(); 1198f8f484bSPrashant Gaikwad 1208f8f484bSPrashant Gaikwad write_enb_clr(periph_clk_to_bit(gate), gate); 1218f8f484bSPrashant Gaikwad 1228f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 1238f8f484bSPrashant Gaikwad } 1248f8f484bSPrashant Gaikwad 1258f8f484bSPrashant Gaikwad const struct clk_ops tegra_clk_periph_gate_ops = { 1268f8f484bSPrashant Gaikwad .is_enabled = clk_periph_is_enabled, 1278f8f484bSPrashant Gaikwad .enable = clk_periph_enable, 1288f8f484bSPrashant Gaikwad .disable = clk_periph_disable, 1298f8f484bSPrashant Gaikwad }; 1308f8f484bSPrashant Gaikwad 1318f8f484bSPrashant Gaikwad struct clk *tegra_clk_register_periph_gate(const char *name, 1328f8f484bSPrashant Gaikwad const char *parent_name, u8 gate_flags, void __iomem *clk_base, 133d5ff89a8SPeter De Schrijver unsigned long flags, int clk_num, int *enable_refcnt) 1348f8f484bSPrashant Gaikwad { 1358f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate; 1368f8f484bSPrashant Gaikwad struct clk *clk; 1378f8f484bSPrashant Gaikwad struct clk_init_data init; 138d5ff89a8SPeter De Schrijver struct tegra_clk_periph_regs *pregs; 139d5ff89a8SPeter De Schrijver 140d5ff89a8SPeter De Schrijver pregs = get_reg_bank(clk_num); 141d5ff89a8SPeter De Schrijver if (!pregs) 142d5ff89a8SPeter De Schrijver return ERR_PTR(-EINVAL); 1438f8f484bSPrashant Gaikwad 1448f8f484bSPrashant Gaikwad gate = kzalloc(sizeof(*gate), GFP_KERNEL); 1458f8f484bSPrashant Gaikwad if (!gate) { 1468f8f484bSPrashant Gaikwad pr_err("%s: could not allocate periph gate clk\n", __func__); 1478f8f484bSPrashant Gaikwad return ERR_PTR(-ENOMEM); 1488f8f484bSPrashant Gaikwad } 1498f8f484bSPrashant Gaikwad 1508f8f484bSPrashant Gaikwad init.name = name; 1518f8f484bSPrashant Gaikwad init.flags = flags; 1528f8f484bSPrashant Gaikwad init.parent_names = parent_name ? &parent_name : NULL; 1538f8f484bSPrashant Gaikwad init.num_parents = parent_name ? 1 : 0; 1548f8f484bSPrashant Gaikwad init.ops = &tegra_clk_periph_gate_ops; 1558f8f484bSPrashant Gaikwad 1568f8f484bSPrashant Gaikwad gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC; 1578f8f484bSPrashant Gaikwad gate->clk_base = clk_base; 1588f8f484bSPrashant Gaikwad gate->clk_num = clk_num; 1598f8f484bSPrashant Gaikwad gate->flags = gate_flags; 1608f8f484bSPrashant Gaikwad gate->enable_refcnt = enable_refcnt; 1618f8f484bSPrashant Gaikwad gate->regs = pregs; 1628f8f484bSPrashant Gaikwad 1638f8f484bSPrashant Gaikwad /* Data in .init is copied by clk_register(), so stack variable OK */ 1648f8f484bSPrashant Gaikwad gate->hw.init = &init; 1658f8f484bSPrashant Gaikwad 1668f8f484bSPrashant Gaikwad clk = clk_register(NULL, &gate->hw); 1678f8f484bSPrashant Gaikwad if (IS_ERR(clk)) 1688f8f484bSPrashant Gaikwad kfree(gate); 1698f8f484bSPrashant Gaikwad 1708f8f484bSPrashant Gaikwad return clk; 1718f8f484bSPrashant Gaikwad } 172