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> 238f8f484bSPrashant Gaikwad #include <linux/tegra-soc.h> 248f8f484bSPrashant Gaikwad 258f8f484bSPrashant Gaikwad #include "clk.h" 268f8f484bSPrashant Gaikwad 278f8f484bSPrashant Gaikwad static DEFINE_SPINLOCK(periph_ref_lock); 288f8f484bSPrashant Gaikwad 298f8f484bSPrashant Gaikwad /* Macros to assist peripheral gate clock */ 308f8f484bSPrashant Gaikwad #define read_enb(gate) \ 318f8f484bSPrashant Gaikwad readl_relaxed(gate->clk_base + (gate->regs->enb_reg)) 328f8f484bSPrashant Gaikwad #define write_enb_set(val, gate) \ 338f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg)) 348f8f484bSPrashant Gaikwad #define write_enb_clr(val, gate) \ 358f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg)) 368f8f484bSPrashant Gaikwad 378f8f484bSPrashant Gaikwad #define read_rst(gate) \ 388f8f484bSPrashant Gaikwad readl_relaxed(gate->clk_base + (gate->regs->rst_reg)) 398f8f484bSPrashant Gaikwad #define write_rst_set(val, gate) \ 408f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->rst_set_reg)) 418f8f484bSPrashant Gaikwad #define write_rst_clr(val, gate) \ 428f8f484bSPrashant Gaikwad writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg)) 438f8f484bSPrashant Gaikwad 445a88b0d1SYen Lin #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32)) 458f8f484bSPrashant Gaikwad 46fdcccbd8SPeter De Schrijver #define LVL2_CLK_GATE_OVRE 0x554 47fdcccbd8SPeter De Schrijver 488f8f484bSPrashant Gaikwad /* Peripheral gate clock ops */ 498f8f484bSPrashant Gaikwad static int clk_periph_is_enabled(struct clk_hw *hw) 508f8f484bSPrashant Gaikwad { 518f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); 528f8f484bSPrashant Gaikwad int state = 1; 538f8f484bSPrashant Gaikwad 548f8f484bSPrashant Gaikwad if (!(read_enb(gate) & periph_clk_to_bit(gate))) 558f8f484bSPrashant Gaikwad state = 0; 568f8f484bSPrashant Gaikwad 578f8f484bSPrashant Gaikwad if (!(gate->flags & TEGRA_PERIPH_NO_RESET)) 588f8f484bSPrashant Gaikwad if (read_rst(gate) & periph_clk_to_bit(gate)) 598f8f484bSPrashant Gaikwad state = 0; 608f8f484bSPrashant Gaikwad 618f8f484bSPrashant Gaikwad return state; 628f8f484bSPrashant Gaikwad } 638f8f484bSPrashant Gaikwad 648f8f484bSPrashant Gaikwad static int clk_periph_enable(struct clk_hw *hw) 658f8f484bSPrashant Gaikwad { 668f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); 678f8f484bSPrashant Gaikwad unsigned long flags = 0; 688f8f484bSPrashant Gaikwad 698f8f484bSPrashant Gaikwad spin_lock_irqsave(&periph_ref_lock, flags); 708f8f484bSPrashant Gaikwad 718f8f484bSPrashant Gaikwad gate->enable_refcnt[gate->clk_num]++; 728f8f484bSPrashant Gaikwad if (gate->enable_refcnt[gate->clk_num] > 1) { 738f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 748f8f484bSPrashant Gaikwad return 0; 758f8f484bSPrashant Gaikwad } 768f8f484bSPrashant Gaikwad 778f8f484bSPrashant Gaikwad write_enb_set(periph_clk_to_bit(gate), gate); 788f8f484bSPrashant Gaikwad udelay(2); 798f8f484bSPrashant Gaikwad 808f8f484bSPrashant Gaikwad if (!(gate->flags & TEGRA_PERIPH_NO_RESET) && 818f8f484bSPrashant Gaikwad !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) { 828f8f484bSPrashant Gaikwad if (read_rst(gate) & periph_clk_to_bit(gate)) { 838f8f484bSPrashant Gaikwad udelay(5); /* reset propogation delay */ 848f8f484bSPrashant Gaikwad write_rst_clr(periph_clk_to_bit(gate), gate); 858f8f484bSPrashant Gaikwad } 868f8f484bSPrashant Gaikwad } 878f8f484bSPrashant Gaikwad 88fdcccbd8SPeter De Schrijver if (gate->flags & TEGRA_PERIPH_WAR_1005168) { 89fdcccbd8SPeter De Schrijver writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE); 90fdcccbd8SPeter De Schrijver writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE); 91fdcccbd8SPeter De Schrijver udelay(1); 92fdcccbd8SPeter De Schrijver writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE); 93fdcccbd8SPeter De Schrijver } 94fdcccbd8SPeter De Schrijver 958f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 968f8f484bSPrashant Gaikwad 978f8f484bSPrashant Gaikwad return 0; 988f8f484bSPrashant Gaikwad } 998f8f484bSPrashant Gaikwad 1008f8f484bSPrashant Gaikwad static void clk_periph_disable(struct clk_hw *hw) 1018f8f484bSPrashant Gaikwad { 1028f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); 1038f8f484bSPrashant Gaikwad unsigned long flags = 0; 1048f8f484bSPrashant Gaikwad 1058f8f484bSPrashant Gaikwad spin_lock_irqsave(&periph_ref_lock, flags); 1068f8f484bSPrashant Gaikwad 1078f8f484bSPrashant Gaikwad gate->enable_refcnt[gate->clk_num]--; 1088f8f484bSPrashant Gaikwad if (gate->enable_refcnt[gate->clk_num] > 0) { 1098f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 1108f8f484bSPrashant Gaikwad return; 1118f8f484bSPrashant Gaikwad } 1128f8f484bSPrashant Gaikwad 1138f8f484bSPrashant Gaikwad /* 1148f8f484bSPrashant Gaikwad * If peripheral is in the APB bus then read the APB bus to 1158f8f484bSPrashant Gaikwad * flush the write operation in apb bus. This will avoid the 1168f8f484bSPrashant Gaikwad * peripheral access after disabling clock 1178f8f484bSPrashant Gaikwad */ 1188f8f484bSPrashant Gaikwad if (gate->flags & TEGRA_PERIPH_ON_APB) 1198f8f484bSPrashant Gaikwad tegra_read_chipid(); 1208f8f484bSPrashant Gaikwad 1218f8f484bSPrashant Gaikwad write_enb_clr(periph_clk_to_bit(gate), gate); 1228f8f484bSPrashant Gaikwad 1238f8f484bSPrashant Gaikwad spin_unlock_irqrestore(&periph_ref_lock, flags); 1248f8f484bSPrashant Gaikwad } 1258f8f484bSPrashant Gaikwad 1268f8f484bSPrashant Gaikwad void tegra_periph_reset(struct tegra_clk_periph_gate *gate, bool assert) 1278f8f484bSPrashant Gaikwad { 1288f8f484bSPrashant Gaikwad if (gate->flags & TEGRA_PERIPH_NO_RESET) 1298f8f484bSPrashant Gaikwad return; 1308f8f484bSPrashant Gaikwad 1318f8f484bSPrashant Gaikwad if (assert) { 1328f8f484bSPrashant Gaikwad /* 1338f8f484bSPrashant Gaikwad * If peripheral is in the APB bus then read the APB bus to 1348f8f484bSPrashant Gaikwad * flush the write operation in apb bus. This will avoid the 1358f8f484bSPrashant Gaikwad * peripheral access after disabling clock 1368f8f484bSPrashant Gaikwad */ 1378f8f484bSPrashant Gaikwad if (gate->flags & TEGRA_PERIPH_ON_APB) 1388f8f484bSPrashant Gaikwad tegra_read_chipid(); 1398f8f484bSPrashant Gaikwad 1408f8f484bSPrashant Gaikwad write_rst_set(periph_clk_to_bit(gate), gate); 1418f8f484bSPrashant Gaikwad } else { 1428f8f484bSPrashant Gaikwad write_rst_clr(periph_clk_to_bit(gate), gate); 1438f8f484bSPrashant Gaikwad } 1448f8f484bSPrashant Gaikwad } 1458f8f484bSPrashant Gaikwad 1468f8f484bSPrashant Gaikwad const struct clk_ops tegra_clk_periph_gate_ops = { 1478f8f484bSPrashant Gaikwad .is_enabled = clk_periph_is_enabled, 1488f8f484bSPrashant Gaikwad .enable = clk_periph_enable, 1498f8f484bSPrashant Gaikwad .disable = clk_periph_disable, 1508f8f484bSPrashant Gaikwad }; 1518f8f484bSPrashant Gaikwad 1528f8f484bSPrashant Gaikwad struct clk *tegra_clk_register_periph_gate(const char *name, 1538f8f484bSPrashant Gaikwad const char *parent_name, u8 gate_flags, void __iomem *clk_base, 154*d5ff89a8SPeter De Schrijver unsigned long flags, int clk_num, int *enable_refcnt) 1558f8f484bSPrashant Gaikwad { 1568f8f484bSPrashant Gaikwad struct tegra_clk_periph_gate *gate; 1578f8f484bSPrashant Gaikwad struct clk *clk; 1588f8f484bSPrashant Gaikwad struct clk_init_data init; 159*d5ff89a8SPeter De Schrijver struct tegra_clk_periph_regs *pregs; 160*d5ff89a8SPeter De Schrijver 161*d5ff89a8SPeter De Schrijver pregs = get_reg_bank(clk_num); 162*d5ff89a8SPeter De Schrijver if (!pregs) 163*d5ff89a8SPeter De Schrijver return ERR_PTR(-EINVAL); 1648f8f484bSPrashant Gaikwad 1658f8f484bSPrashant Gaikwad gate = kzalloc(sizeof(*gate), GFP_KERNEL); 1668f8f484bSPrashant Gaikwad if (!gate) { 1678f8f484bSPrashant Gaikwad pr_err("%s: could not allocate periph gate clk\n", __func__); 1688f8f484bSPrashant Gaikwad return ERR_PTR(-ENOMEM); 1698f8f484bSPrashant Gaikwad } 1708f8f484bSPrashant Gaikwad 1718f8f484bSPrashant Gaikwad init.name = name; 1728f8f484bSPrashant Gaikwad init.flags = flags; 1738f8f484bSPrashant Gaikwad init.parent_names = parent_name ? &parent_name : NULL; 1748f8f484bSPrashant Gaikwad init.num_parents = parent_name ? 1 : 0; 1758f8f484bSPrashant Gaikwad init.ops = &tegra_clk_periph_gate_ops; 1768f8f484bSPrashant Gaikwad 1778f8f484bSPrashant Gaikwad gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC; 1788f8f484bSPrashant Gaikwad gate->clk_base = clk_base; 1798f8f484bSPrashant Gaikwad gate->clk_num = clk_num; 1808f8f484bSPrashant Gaikwad gate->flags = gate_flags; 1818f8f484bSPrashant Gaikwad gate->enable_refcnt = enable_refcnt; 1828f8f484bSPrashant Gaikwad gate->regs = pregs; 1838f8f484bSPrashant Gaikwad 1848f8f484bSPrashant Gaikwad /* Data in .init is copied by clk_register(), so stack variable OK */ 1858f8f484bSPrashant Gaikwad gate->hw.init = &init; 1868f8f484bSPrashant Gaikwad 1878f8f484bSPrashant Gaikwad clk = clk_register(NULL, &gate->hw); 1888f8f484bSPrashant Gaikwad if (IS_ERR(clk)) 1898f8f484bSPrashant Gaikwad kfree(gate); 1908f8f484bSPrashant Gaikwad 1918f8f484bSPrashant Gaikwad return clk; 1928f8f484bSPrashant Gaikwad } 193