From patchwork Fri Nov 20 08:42:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jisheng Zhang X-Patchwork-Id: 7665961 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CCCB29F2EC for ; Fri, 20 Nov 2015 08:50:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AC3422041D for ; Fri, 20 Nov 2015 08:50:39 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 94615203E9 for ; Fri, 20 Nov 2015 08:50:38 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZzhN5-0005hZ-2L; Fri, 20 Nov 2015 08:48:43 +0000 Received: from mx0b-0016f401.pphosted.com ([67.231.156.173]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZzhLK-0004e5-F7 for linux-arm-kernel@lists.infradead.org; Fri, 20 Nov 2015 08:47:05 +0000 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.15.0.59/8.15.0.59) with SMTP id tAK8ihXd018357; Fri, 20 Nov 2015 00:46:21 -0800 Received: from sc-exch03.marvell.com ([199.233.58.183]) by mx0b-0016f401.pphosted.com with ESMTP id 1y9w9mgf8w-1 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 20 Nov 2015 00:46:21 -0800 Received: from SC-EXCH03.marvell.com (10.93.176.83) by SC-EXCH03.marvell.com (10.93.176.83) with Microsoft SMTP Server (TLS) id 15.0.1044.25; Fri, 20 Nov 2015 00:46:19 -0800 Received: from maili.marvell.com (10.93.176.43) by SC-EXCH03.marvell.com (10.93.176.83) with Microsoft SMTP Server id 15.0.1044.25 via Frontend Transport; Fri, 20 Nov 2015 00:46:19 -0800 Received: from xhacker.marvell.com (unknown [10.37.135.134]) by maili.marvell.com (Postfix) with ESMTP id 633553F703F; Fri, 20 Nov 2015 00:46:16 -0800 (PST) From: Jisheng Zhang To: , , , , , , , , , , Subject: [PATCH v2 2/6] clk: berlin: add common clk driver for newer SoCs Date: Fri, 20 Nov 2015 16:42:28 +0800 Message-ID: <1448008952-1787-3-git-send-email-jszhang@marvell.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1448008952-1787-1-git-send-email-jszhang@marvell.com> References: <1448008952-1787-1-git-send-email-jszhang@marvell.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2015-11-20_03:, , signatures=0 X-Proofpoint-Spam-Details: rule=inbound_notspam policy=inbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1507310000 definitions=main-1511200157 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151120_004655_066079_EA7F90E1 X-CRM114-Status: GOOD ( 18.35 ) X-Spam-Score: -1.8 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jisheng Zhang , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add common clk driver for Marvell SoCs newer than BG2, BG2CD, BG2Q. berlin_clk_setup() is provided to setup and register such kind of clks. Signed-off-by: Jisheng Zhang --- drivers/clk/berlin/Makefile | 2 +- drivers/clk/berlin/clk.c | 203 ++++++++++++++++++++++++++++++++++++++++++++ drivers/clk/berlin/clk.h | 33 +++++++ 3 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 drivers/clk/berlin/clk.c create mode 100644 drivers/clk/berlin/clk.h diff --git a/drivers/clk/berlin/Makefile b/drivers/clk/berlin/Makefile index eee42b0..ee2e09d 100644 --- a/drivers/clk/berlin/Makefile +++ b/drivers/clk/berlin/Makefile @@ -1,5 +1,5 @@ obj-y += berlin2-avpll.o berlin2-pll.o berlin2-div.o -obj-y += pll.o +obj-y += pll.o clk.o obj-$(CONFIG_MACH_BERLIN_BG2) += bg2.o obj-$(CONFIG_MACH_BERLIN_BG2CD) += bg2.o obj-$(CONFIG_MACH_BERLIN_BG2Q) += bg2q.o diff --git a/drivers/clk/berlin/clk.c b/drivers/clk/berlin/clk.c new file mode 100644 index 0000000..70f2b9d --- /dev/null +++ b/drivers/clk/berlin/clk.c @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2015 Marvell Technology Group Ltd. + * + * Author: Jisheng Zhang + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "clk.h" + +#define CLKEN (1 << 0) +#define CLKPLLSEL_MASK 7 +#define CLKPLLSEL_SHIFT 1 +#define CLKPLLSWITCH (1 << 4) +#define CLKSWITCH (1 << 5) +#define CLKD3SWITCH (1 << 6) +#define CLKSEL_MASK 7 +#define CLKSEL_SHIFT 7 + +#define CLK_SOURCE_MAX 5 + +struct berlin_clk { + struct clk_hw hw; + void __iomem *base; +}; + +#define to_berlin_clk(hw) container_of(hw, struct berlin_clk, hw) + +static u8 clk_div[] = {1, 2, 4, 6, 8, 12, 1, 1}; + +static unsigned long berlin_clk_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + u32 val, divider; + struct berlin_clk *clk = to_berlin_clk(hw); + + val = readl_relaxed(clk->base); + if (val & CLKD3SWITCH) + divider = 3; + else { + if (val & CLKSWITCH) { + val >>= CLKSEL_SHIFT; + val &= CLKSEL_MASK; + divider = clk_div[val]; + } else + divider = 1; + } + + return parent_rate / divider; +} + +static u8 berlin_clk_get_parent(struct clk_hw *hw) +{ + u32 val; + struct berlin_clk *clk = to_berlin_clk(hw); + + val = readl_relaxed(clk->base); + if (val & CLKPLLSWITCH) { + val >>= CLKPLLSEL_SHIFT; + val &= CLKPLLSEL_MASK; + return val; + } + + return 0; +} + +static int berlin_clk_enable(struct clk_hw *hw) +{ + u32 val; + struct berlin_clk *clk = to_berlin_clk(hw); + + val = readl_relaxed(clk->base); + val |= CLKEN; + writel_relaxed(val, clk->base); + + return 0; +} + +static void berlin_clk_disable(struct clk_hw *hw) +{ + u32 val; + struct berlin_clk *clk = to_berlin_clk(hw); + + val = readl_relaxed(clk->base); + val &= ~CLKEN; + writel_relaxed(val, clk->base); +} + +static int berlin_clk_is_enabled(struct clk_hw *hw) +{ + u32 val; + struct berlin_clk *clk = to_berlin_clk(hw); + + val = readl_relaxed(clk->base); + val &= CLKEN; + + return val ? 1 : 0; +} + +static const struct clk_ops berlin_clk_ops = { + .recalc_rate = berlin_clk_recalc_rate, + .get_parent = berlin_clk_get_parent, + .enable = berlin_clk_enable, + .disable = berlin_clk_disable, + .is_enabled = berlin_clk_is_enabled, +}; + +static struct clk * __init +berlin_clk_register(const char *name, int num_parents, + const char **parent_names, unsigned long flags, + void __iomem *base) +{ + struct clk *clk; + struct berlin_clk *bclk; + struct clk_init_data init; + + bclk = kzalloc(sizeof(*bclk), GFP_KERNEL); + if (!bclk) + return ERR_PTR(-ENOMEM); + + init.name = name; + init.ops = &berlin_clk_ops; + init.parent_names = parent_names; + init.num_parents = num_parents; + init.flags = flags; + + bclk->base = base; + bclk->hw.init = &init; + + clk = clk_register(NULL, &bclk->hw); + if (IS_ERR(clk)) + kfree(bclk); + + return clk; +} + +void __init berlin_clk_setup(struct device_node *np, + const struct clk_desc *descs, + struct clk_onecell_data *clk_data, + int n) +{ + int i, ret, num_parents; + void __iomem *base; + struct clk **clks; + const char *parent_names[CLK_SOURCE_MAX]; + + num_parents = of_clk_get_parent_count(np); + if (num_parents <= 0 || num_parents > CLK_SOURCE_MAX) + return; + + of_clk_parent_fill(np, parent_names, num_parents); + + clks = kcalloc(n, sizeof(struct clk *), GFP_KERNEL); + if (!clks) + return; + + base = of_iomap(np, 0); + if (WARN_ON(!base)) + goto err_iomap; + + for (i = 0; i < n; i++) { + struct clk *clk; + + clk = berlin_clk_register(descs[i].name, + num_parents, parent_names, + descs[i].flags, + base + descs[i].offset); + if (WARN_ON(IS_ERR(clks[i]))) + goto err_clk_register; + clks[i] = clk; + } + + clk_data->clks = clks; + clk_data->clk_num = i; + + ret = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data); + if (WARN_ON(ret)) + goto err_clk_register; + return; + +err_clk_register: + for (i = 0; i < n; i++) + clk_unregister(clks[i]); + iounmap(base); +err_iomap: + kfree(clks); +} diff --git a/drivers/clk/berlin/clk.h b/drivers/clk/berlin/clk.h new file mode 100644 index 0000000..5e5680e --- /dev/null +++ b/drivers/clk/berlin/clk.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015 Marvell Technology Group Ltd. + * + * Author: Jisheng Zhang + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __BERLIN_CLK_H +#define __BERLIN_CLK_H + +struct clk_desc { + const char *name; + u32 offset; + unsigned long flags; +}; + +void __init berlin_clk_setup(struct device_node *np, + const struct clk_desc *desc, + struct clk_onecell_data *clk_data, + int n); + +#endif