From patchwork Thu Oct 1 14:37:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 7309231 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Original-To: patchwork-linux-sh@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 019E79F314 for ; Thu, 1 Oct 2015 14:37:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9FC2F20697 for ; Thu, 1 Oct 2015 14:37:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3D2AB20620 for ; Thu, 1 Oct 2015 14:37:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753148AbbJAOhe (ORCPT ); Thu, 1 Oct 2015 10:37:34 -0400 Received: from mail-pa0-f46.google.com ([209.85.220.46]:36402 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750899AbbJAOhd (ORCPT ); Thu, 1 Oct 2015 10:37:33 -0400 Received: by pablk4 with SMTP id lk4so75906589pab.3; Thu, 01 Oct 2015 07:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:date:message-id:in-reply-to:references:subject; bh=Cnjlk/yc42Z7np2akyL5dnM0chcIZnAZ62L0ySJwfyQ=; b=hw+1FEsUN7df40OqhDIxXZAULIidv9pTXBWBLs+2y3T7x0Tu90+OnlDT2jCYqa200v XQBl15rR0NypYwyFK6bV0GFKcI8NDr+uc2Jua+ea+BT/Va3UW5LQOOHajV4DkncSwyyY IlioaOI0mX21DY2a7MNgKTdbWeoa/Xd+TRiUTFxse7Vjkwn8r/ynveQhORKGYgbomM73 7RCHX4GEiXfSQRJvRpawrY+/KqEqMsHRFETMml5IxRao1N3TJpq/NQ7/zvKPxRopUFhQ /5AaR4HOy5RCwjLuYwFuVIrjgBqfBojUQUs4geUtRU9il7uNLT7GNHWViGasl593A7Dd 7mkg== X-Received: by 10.69.1.5 with SMTP id bc5mr12623568pbd.151.1443710252663; Thu, 01 Oct 2015 07:37:32 -0700 (PDT) Received: from [127.0.0.1] (s214090.ppp.asahi-net.or.jp. [220.157.214.90]) by smtp.gmail.com with ESMTPSA id li11sm7105588pab.43.2015.10.01.07.37.29 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Oct 2015 07:37:31 -0700 (PDT) From: Magnus Damm To: linux-clk@vger.kernel.org Cc: kuninori.morimoto.gx@renesas.com, linux-sh@vger.kernel.org, mturquette@baylibre.com, gaku.inami.xw@bp.renesas.com, sboyd@codeaurora.org, horms@verge.net.au, geert@linux-m68k.org, laurent.pinchart@ideasonboard.com, Magnus Damm Date: Thu, 01 Oct 2015 23:37:48 +0900 Message-Id: <20151001143748.20618.7279.sendpatchset@little-apple> In-Reply-To: <20151001143717.20618.26365.sendpatchset@little-apple> References: <20151001143717.20618.26365.sendpatchset@little-apple> Subject: [PATCH v8 03/05] clk: shmobile: Add Renesas Module Standby and Reset driver Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham 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 From: Geert Uytterhoeven Add a driver for the Renesas Module Standby and Software Reset module found in several Renesas SoCs. This driver is based on the existing R-Car MSTP driver, and is intended to replace it. The existing driver is limited to Module Standby, and has bindings that are difficult to extend to more register sets for e.g. reset control. TODO: - Implement module reset handling, - Write binding documentation. Signed-off-by: Geert Uytterhoeven Signed-off-by: Magnus Damm --- Changes since V7: (Magnus Damm ) - New patch, took local v3 patch from Geert and removed r8a7791 references. drivers/clk/shmobile/clk-mssr.c | 392 +++++++++++++++++++++++++++++++++++++++ include/linux/clk/shmobile.h | 2 2 files changed, 394 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe linux-sh" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- /dev/null +++ work/drivers/clk/shmobile/clk-mssr.c 2015-10-01 18:38:50.320513000 +0900 @@ -0,0 +1,392 @@ +/* + * Renesas Module Standby and Software Reset + * + * Based on clk-mstp.c + * + * Copyright (C) 2013 Ideas On Board SPRL + * Copyright (C) 2015 Glider bvba + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * MSTP clocks. We can't use standard gate clocks as we need to poll on the + * status register when enabling the clock. + */ + +/* + * Module Standby and Software Reset register offets. + * + * If the registers exist, these are valid for SH-Mobile, R-Mobile, + * R-Car Gen 2, and R-Car Gen 3. + * These are NOT valid for R-Car Gen1 and RZ/A1! + */ + +/* + * Module stop status register offsets + */ + +static const u16 mstpsr[] = { + 0x030, 0x038, 0x040, 0x048, 0x04C, 0x03C, 0x1C0, 0x1C4, + 0x9A0, 0x9A4, 0x9A8, 0x9AC, +}; + +#define MSTPSR(i) mstpsr[i] + + +/* + * System module stop control register offsets + */ + +static const u16 smstpcr[] = { + 0x130, 0x134, 0x138, 0x13C, 0x140, 0x144, 0x148, 0x14C, + 0x990, 0x994, 0x998, 0x99C, +}; + +#define SMSTPCR(i) smstpcr[i] + + +/* + * Software reset register offsets + */ + +static const u16 srcr[] = { + 0x0A0, 0x0A8, 0x0B0, 0x0B8, 0x0BC, 0x0C4, 0x1C8, 0x1CC, + 0x920, 0x924, 0x928, 0x92C, +}; + +#define SRCR(i) srcr[i] + + +/* Realtime module stop control register offsets */ +#define RMSTPCR(i) (smstpcr[i] - 0x20) + +/* Modem module stop control register offsets (r8a73a4) */ +#define MMSTPCR(i) (smstpcr[i] + 0x20) + +/* Software reset clearing register offsets */ +#define SRSTCLR(i) (0x940 + (i) * 4) + + +#define MSTP_MAX_REGS ARRAY_SIZE(smstpcr) +#define MSTP_MAX_CLOCKS (MSTP_MAX_REGS * 32) + + +/** + * struct mssr_group - Module standby and software reset group + * + * @data: clocks in this group + * @base: CPG/MSSR register block base address + * @lock: protects writes to SMSTPCR + */ +struct mssr_group { + struct clk_onecell_data data; + void __iomem *base; + spinlock_t lock; + // TODO Add reset controller data +}; + +/** + * struct mstp_clock - MSTP gating clock + * @hw: handle between common and hardware-specific interfaces + * @index: MSTP clock number + * @group: MSTP clocks group + */ +struct mstp_clock { + struct clk_hw hw; + u32 index; + struct mssr_group *group; +}; + +#define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw) + +static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable) +{ + struct mstp_clock *clock = to_mstp_clock(hw); + struct mssr_group *group = clock->group; + unsigned int reg = clock->index / 32; + unsigned int bit = clock->index % 32; + u32 bitmask = BIT(bit); + unsigned long flags; + unsigned int i; + u32 value; + + spin_lock_irqsave(&group->lock, flags); + + value = clk_readl(group->base + SMSTPCR(reg)); + if (enable) + value &= ~bitmask; + else + value |= bitmask; + clk_writel(value, group->base + SMSTPCR(reg)); + + spin_unlock_irqrestore(&group->lock, flags); + + if (!enable) + return 0; + + for (i = 1000; i > 0; --i) { + if (!(clk_readl(group->base + MSTPSR(reg)) & + bitmask)) + break; + cpu_relax(); + } + + if (!i) { + pr_err("%s: failed to enable %p[%d]\n", __func__, + group->base + SMSTPCR(reg), bit); + return -ETIMEDOUT; + } + + return 0; +} + +static int cpg_mstp_clock_enable(struct clk_hw *hw) +{ + return cpg_mstp_clock_endisable(hw, true); +} + +static void cpg_mstp_clock_disable(struct clk_hw *hw) +{ + cpg_mstp_clock_endisable(hw, false); +} + +static int cpg_mstp_clock_is_enabled(struct clk_hw *hw) +{ + struct mstp_clock *clock = to_mstp_clock(hw); + struct mssr_group *group = clock->group; + u32 value; + + value = clk_readl(group->base + MSTPSR(clock->index / 32)); + + return !(value & BIT(clock->index % 32)); +} + +static const struct clk_ops cpg_mstp_clock_ops = { + .enable = cpg_mstp_clock_enable, + .disable = cpg_mstp_clock_disable, + .is_enabled = cpg_mstp_clock_is_enabled, +}; + +static struct clk * __init +cpg_mstp_clock_register(const char *name, const char *parent_name, + unsigned int index, struct mssr_group *group) +{ + struct clk_init_data init; + struct mstp_clock *clock; + struct clk *clk; + + clock = kzalloc(sizeof(*clock), GFP_KERNEL); + if (!clock) + return ERR_PTR(-ENOMEM); + + init.name = name; + init.ops = &cpg_mstp_clock_ops; + init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT; + init.parent_names = &parent_name; + init.num_parents = 1; + + clock->index = index; + clock->group = group; + clock->hw.init = &init; + + clk = clk_register(NULL, &clock->hw); + + if (IS_ERR(clk)) + kfree(clock); + + return clk; +} + +static struct clk *cpg_mssr_clk_src_get(struct of_phandle_args *clkspec, + void *data) +{ + struct clk_onecell_data *clk_data = data; + unsigned int clkidx = clkspec->args[0]; + unsigned int reg = clkidx / 100; + unsigned int bit = clkidx % 100; + unsigned int idx; + + /* Translate from sparse base-100 to packed index space */ + reg = clkidx / 100; + bit = clkidx % 100; + idx = reg * 32 + bit; + if (bit > 31 || idx >= clk_data->clk_num) { + pr_err("%s: invalid clock index %u\n", __func__, clkidx); + return ERR_PTR(-EINVAL); + } + + return clk_data->clks[idx]; +} + +static void __init cpg_mssr_init(struct device_node *np) +{ + struct mssr_group *group; + struct clk **clks; + unsigned int i; + + group = kzalloc(sizeof(*group), GFP_KERNEL); + clks = kmalloc_array(MSTP_MAX_CLOCKS, sizeof(*clks), GFP_KERNEL); + if (group == NULL || clks == NULL) { + kfree(group); + kfree(clks); + return; + } + + spin_lock_init(&group->lock); + group->data.clks = clks; + + group->base = of_iomap(np, 0); + + if (group->base == NULL) { + pr_err("%s: failed to remap CPG/MSSR\n", __func__); + kfree(group); + kfree(clks); + return; + } + + for (i = 0; i < MSTP_MAX_CLOCKS; ++i) + clks[i] = ERR_PTR(-ENOENT); + + for (i = 0; i < MSTP_MAX_CLOCKS; ++i) { + const char *parent_name; + unsigned int reg, bit; + const char *name; + u32 clkidx; + int ret; + + /* Skip clocks with no name. */ + ret = of_property_read_string_index(np, "clock-output-names", + i, &name); + if (ret < 0 || strlen(name) == 0) + continue; + + parent_name = of_clk_get_parent_name(np, i); + ret = of_property_read_u32_index(np, "clock-indices", i, + &clkidx); + if (parent_name == NULL || ret < 0) + break; + + /* Translate from sparse base-100 to packed index space */ + reg = clkidx / 100; + bit = clkidx % 100; + if (reg > MSTP_MAX_REGS || bit > 31) { + pr_err("%s: invalid clock %s index %u\n", __func__, + name, clkidx); + continue; + } + + clkidx = reg * 32 + bit; + + clks[clkidx] = cpg_mstp_clock_register(name, parent_name, + clkidx, group); + if (!IS_ERR(clks[clkidx])) { + group->data.clk_num = max(group->data.clk_num, + clkidx + 1); + } else { + pr_err("%s: failed to register %s %s clock (%ld)\n", + __func__, np->name, name, PTR_ERR(clks[clkidx])); + } + } + + of_clk_add_provider(np, cpg_mssr_clk_src_get, &group->data); + + // TODO Register reset controller +} + + +#ifdef CONFIG_PM_GENERIC_DOMAINS_OF +static int cpg_mssr_attach_dev(struct generic_pm_domain *domain, + struct device *dev) +{ + struct device_node *np = dev->of_node; + struct of_phandle_args clkspec; + struct clk *clk; + int i = 0; + int error; + + while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i, + &clkspec)) { + if (of_device_is_compatible(clkspec.np, + "renesas,r8a7791-cpg-mssr")) + goto found; + + of_node_put(clkspec.np); + i++; + } + + return 0; + +found: + clk = of_clk_get_from_provider(&clkspec); + of_node_put(clkspec.np); + + if (IS_ERR(clk)) + return PTR_ERR(clk); + + error = pm_clk_create(dev); + if (error) { + dev_err(dev, "pm_clk_create failed %d\n", error); + goto fail_put; + } + + error = pm_clk_add_clk(dev, clk); + if (error) { + dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error); + goto fail_destroy; + } + + return 0; + +fail_destroy: + pm_clk_destroy(dev); +fail_put: + clk_put(clk); + return error; +} + +static void cpg_mssr_detach_dev(struct generic_pm_domain *domain, + struct device *dev) +{ + if (!list_empty(&dev->power.subsys_data->clock_list)) + pm_clk_destroy(dev); +} + +void __init cpg_mssr_add_clk_domain(struct device_node *np) +{ + struct generic_pm_domain *pd; + u32 ncells; + + if (of_property_read_u32(np, "#power-domain-cells", &ncells)) { + pr_warn("%s lacks #power-domain-cells\n", np->full_name); + return; + } + + pd = kzalloc(sizeof(*pd), GFP_KERNEL); + if (!pd) + return; + + pd->name = np->name; + + pd->flags = GENPD_FLAG_PM_CLK; + pm_genpd_init(pd, &simple_qos_governor, false); + pd->attach_dev = cpg_mssr_attach_dev; + pd->detach_dev = cpg_mssr_detach_dev; + + of_genpd_add_provider_simple(np, pd); +} +#endif /* !CONFIG_PM_GENERIC_DOMAINS_OF */ --- 0001/include/linux/clk/shmobile.h +++ work/include/linux/clk/shmobile.h 2015-10-01 18:37:56.290513000 +0900 @@ -28,8 +28,10 @@ void rcar_gen2_clocks_init(u32 mode); void cpg_mstp_add_clk_domain(struct device_node *np); int cpg_mstp_attach_dev(struct generic_pm_domain *domain, struct device *dev); void cpg_mstp_detach_dev(struct generic_pm_domain *domain, struct device *dev); +void cpg_mssr_add_clk_domain(struct device_node *np); #else static inline void cpg_mstp_add_clk_domain(struct device_node *np) {} +static inline void cpg_mssr_add_clk_domain(struct device_node *np) {} #endif #endif