From patchwork Thu Aug 28 07:01:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Horman X-Patchwork-Id: 4801361 Return-Path: X-Original-To: patchwork-ltsi-dev@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B98E3C0338 for ; Thu, 28 Aug 2014 08:58:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A2326201B9 for ; Thu, 28 Aug 2014 08:58:18 +0000 (UTC) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 68BE12015A for ; Thu, 28 Aug 2014 08:58:17 +0000 (UTC) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id CE6A217A8; Thu, 28 Aug 2014 07:38:29 +0000 (UTC) X-Original-To: ltsi-dev@lists.linuxfoundation.org Delivered-To: ltsi-dev@mail.linuxfoundation.org Received: from smtp2.linuxfoundation.org (smtp2.linux-foundation.org [172.17.192.36]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 007DD1694 for ; Thu, 28 Aug 2014 07:37:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from kirsty.vergenet.net (kirsty.vergenet.net [202.4.237.240]) by smtp2.linuxfoundation.org (Postfix) with ESMTP id CB4211DB99 for ; Thu, 28 Aug 2014 07:37:24 +0000 (UTC) Received: from ayumi.isobedori.kobe.vergenet.net (p4222-ipbfp1605kobeminato.hyogo.ocn.ne.jp [114.154.95.222]) by kirsty.vergenet.net (Postfix) with ESMTP id F315D267393; Thu, 28 Aug 2014 17:08:46 +1000 (EST) Received: by ayumi.isobedori.kobe.vergenet.net (Postfix, from userid 7100) id 75124EDE5DA; Thu, 28 Aug 2014 16:08:45 +0900 (JST) From: Simon Horman To: ltsi-dev@lists.linuxfoundation.org Date: Thu, 28 Aug 2014 16:01:54 +0900 Message-Id: <1409209620-24487-589-git-send-email-horms+renesas@verge.net.au> X-Mailer: git-send-email 2.0.1 In-Reply-To: <1409209620-24487-1-git-send-email-horms+renesas@verge.net.au> References: <1409209620-24487-1-git-send-email-horms+renesas@verge.net.au> X-Spam-Status: No, score=-4.2 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 Cc: Magnus Damm Subject: [LTSI-dev] [PATCH LTSI-3.14 588/894] clocksource: sh_tmu: Add support for multiple channels per device X-BeenThere: ltsi-dev@lists.linuxfoundation.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "A list to discuss patches, development, and other things related to the LTSI project" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ltsi-dev-bounces@lists.linuxfoundation.org Errors-To: ltsi-dev-bounces@lists.linuxfoundation.org X-Virus-Scanned: ClamAV using ClamSMTP From: Laurent Pinchart TMU hardware devices can support multiple channels, with global registers and per-channel registers. The sh_tmu driver currently models the hardware with one Linux device per channel. This model makes it difficult to handle global registers in a clean way. Add support for a new model that uses one Linux device per timer with multiple channels per device. This requires changes to platform data, add new channel configuration fields. Support for the legacy model is kept and will be removed after all platforms switch to the new model. Signed-off-by: Laurent Pinchart (cherry picked from commit 8c7f21e6739ad836f30561d641393a8417abdad3) Signed-off-by: Simon Horman --- drivers/clocksource/sh_tmu.c | 213 ++++++++++++++++++++++++++++++------------- 1 file changed, 152 insertions(+), 61 deletions(-) diff --git a/drivers/clocksource/sh_tmu.c b/drivers/clocksource/sh_tmu.c index fec9bed..0306d31 100644 --- a/drivers/clocksource/sh_tmu.c +++ b/drivers/clocksource/sh_tmu.c @@ -35,6 +35,12 @@ #include #include +enum sh_tmu_model { + SH_TMU_LEGACY, + SH_TMU, + SH_TMU_SH3, +}; + struct sh_tmu_device; struct sh_tmu_channel { @@ -58,8 +64,13 @@ struct sh_tmu_device { void __iomem *mapbase; struct clk *clk; + enum sh_tmu_model model; + struct sh_tmu_channel *channels; unsigned int num_channels; + + bool has_clockevent; + bool has_clocksource; }; static DEFINE_RAW_SPINLOCK(sh_tmu_lock); @@ -82,8 +93,16 @@ static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr) { unsigned long offs; - if (reg_nr == TSTR) - return ioread8(ch->tmu->mapbase); + if (reg_nr == TSTR) { + switch (ch->tmu->model) { + case SH_TMU_LEGACY: + return ioread8(ch->tmu->mapbase); + case SH_TMU_SH3: + return ioread8(ch->tmu->mapbase + 2); + case SH_TMU: + return ioread8(ch->tmu->mapbase + 4); + } + } offs = reg_nr << 2; @@ -99,8 +118,14 @@ static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr, unsigned long offs; if (reg_nr == TSTR) { - iowrite8(value, ch->tmu->mapbase); - return; + switch (ch->tmu->model) { + case SH_TMU_LEGACY: + return iowrite8(value, ch->tmu->mapbase); + case SH_TMU_SH3: + return iowrite8(value, ch->tmu->mapbase + 2); + case SH_TMU: + return iowrite8(value, ch->tmu->mapbase + 4); + } } offs = reg_nr << 2; @@ -435,31 +460,49 @@ static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch, static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name, bool clockevent, bool clocksource) { - if (clockevent) + if (clockevent) { + ch->tmu->has_clockevent = true; sh_tmu_register_clockevent(ch, name); - else if (clocksource) + } else if (clocksource) { + ch->tmu->has_clocksource = true; sh_tmu_register_clocksource(ch, name); + } return 0; } -static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, +static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int index, + bool clockevent, bool clocksource, struct sh_tmu_device *tmu) { - struct sh_timer_config *cfg = tmu->pdev->dev.platform_data; + /* Skip unused channels. */ + if (!clockevent && !clocksource) + return 0; ch->tmu = tmu; - /* - * The SH3 variant (SH770x, SH7705, SH7710 and SH7720) maps channel - * registers blocks at base + 2 + 12 * index, while all other variants - * map them at base + 4 + 12 * index. We can compute the index by just - * dividing by 12, the 2 bytes or 4 bytes offset being hidden by the - * integer division. - */ - ch->index = cfg->channel_offset / 12; + if (tmu->model == SH_TMU_LEGACY) { + struct sh_timer_config *cfg = tmu->pdev->dev.platform_data; + + /* + * The SH3 variant (SH770x, SH7705, SH7710 and SH7720) maps + * channel registers blocks at base + 2 + 12 * index, while all + * other variants map them at base + 4 + 12 * index. We can + * compute the index by just dividing by 12, the 2 bytes or 4 + * bytes offset being hidden by the integer division. + */ + ch->index = cfg->channel_offset / 12; + ch->base = tmu->mapbase + cfg->channel_offset; + } else { + ch->index = index; + + if (tmu->model == SH_TMU_SH3) + ch->base = tmu->mapbase + 4 + ch->index * 12; + else + ch->base = tmu->mapbase + 8 + ch->index * 12; + } - ch->irq = platform_get_irq(tmu->pdev, 0); + ch->irq = platform_get_irq(tmu->pdev, ch->index); if (ch->irq < 0) { dev_err(&tmu->pdev->dev, "ch%u: failed to get irq\n", ch->index); @@ -470,88 +513,127 @@ static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, ch->enable_count = 0; return sh_tmu_register(ch, dev_name(&tmu->pdev->dev), - cfg->clockevent_rating != 0, - cfg->clocksource_rating != 0); + clockevent, clocksource); } -static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) +static int sh_tmu_map_memory(struct sh_tmu_device *tmu) { - struct sh_timer_config *cfg = pdev->dev.platform_data; struct resource *res; - void __iomem *base; - int ret; - ret = -ENXIO; - - tmu->pdev = pdev; - - if (!cfg) { - dev_err(&tmu->pdev->dev, "missing platform data\n"); - goto err0; - } - - platform_set_drvdata(pdev, tmu); res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0); if (!res) { dev_err(&tmu->pdev->dev, "failed to get I/O memory\n"); - goto err0; + return -ENXIO; } + tmu->mapbase = ioremap_nocache(res->start, resource_size(res)); + if (tmu->mapbase == NULL) + return -ENXIO; + /* - * Map memory, let base point to our channel and mapbase to the - * start/stop shared register. + * In legacy platform device configuration (with one device per channel) + * the resource points to the channel base address. */ - base = ioremap_nocache(res->start, resource_size(res)); - if (base == NULL) { - dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n"); - goto err0; + if (tmu->model == SH_TMU_LEGACY) { + struct sh_timer_config *cfg = tmu->pdev->dev.platform_data; + tmu->mapbase -= cfg->channel_offset; } - tmu->mapbase = base - cfg->channel_offset; + return 0; +} - /* get hold of clock */ +static void sh_tmu_unmap_memory(struct sh_tmu_device *tmu) +{ + if (tmu->model == SH_TMU_LEGACY) { + struct sh_timer_config *cfg = tmu->pdev->dev.platform_data; + tmu->mapbase += cfg->channel_offset; + } + + iounmap(tmu->mapbase); +} + +static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) +{ + struct sh_timer_config *cfg = pdev->dev.platform_data; + const struct platform_device_id *id = pdev->id_entry; + unsigned int i; + int ret; + + if (!cfg) { + dev_err(&tmu->pdev->dev, "missing platform data\n"); + return -ENXIO; + } + + tmu->pdev = pdev; + tmu->model = id->driver_data; + + /* Get hold of clock. */ tmu->clk = clk_get(&tmu->pdev->dev, "tmu_fck"); if (IS_ERR(tmu->clk)) { dev_err(&tmu->pdev->dev, "cannot get clock\n"); - ret = PTR_ERR(tmu->clk); - goto err1; + return PTR_ERR(tmu->clk); } ret = clk_prepare(tmu->clk); if (ret < 0) - goto err2; + goto err_clk_put; + + /* Map the memory resource. */ + ret = sh_tmu_map_memory(tmu); + if (ret < 0) { + dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n"); + goto err_clk_unprepare; + } - tmu->channels = kzalloc(sizeof(*tmu->channels), GFP_KERNEL); + /* Allocate and setup the channels. */ + if (tmu->model == SH_TMU_LEGACY) + tmu->num_channels = 1; + else + tmu->num_channels = hweight8(cfg->channels_mask); + + tmu->channels = kzalloc(sizeof(*tmu->channels) * tmu->num_channels, + GFP_KERNEL); if (tmu->channels == NULL) { ret = -ENOMEM; - goto err3; + goto err_unmap; } - tmu->num_channels = 1; - - tmu->channels[0].base = base; + if (tmu->model == SH_TMU_LEGACY) { + ret = sh_tmu_channel_setup(&tmu->channels[0], 0, + cfg->clockevent_rating != 0, + cfg->clocksource_rating != 0, tmu); + if (ret < 0) + goto err_unmap; + } else { + /* + * Use the first channel as a clock event device and the second + * channel as a clock source. + */ + for (i = 0; i < tmu->num_channels; ++i) { + ret = sh_tmu_channel_setup(&tmu->channels[i], i, + i == 0, i == 1, tmu); + if (ret < 0) + goto err_unmap; + } + } - ret = sh_tmu_channel_setup(&tmu->channels[0], tmu); - if (ret < 0) - goto err3; + platform_set_drvdata(pdev, tmu); return 0; - err3: +err_unmap: kfree(tmu->channels); + sh_tmu_unmap_memory(tmu); +err_clk_unprepare: clk_unprepare(tmu->clk); - err2: +err_clk_put: clk_put(tmu->clk); - err1: - iounmap(base); - err0: return ret; } static int sh_tmu_probe(struct platform_device *pdev) { struct sh_tmu_device *tmu = platform_get_drvdata(pdev); - struct sh_timer_config *cfg = pdev->dev.platform_data; int ret; if (!is_early_platform_device(pdev)) { @@ -580,7 +662,7 @@ static int sh_tmu_probe(struct platform_device *pdev) return 0; out: - if (cfg->clockevent_rating || cfg->clocksource_rating) + if (tmu->has_clockevent || tmu->has_clocksource) pm_runtime_irq_safe(&pdev->dev); else pm_runtime_idle(&pdev->dev); @@ -593,12 +675,21 @@ static int sh_tmu_remove(struct platform_device *pdev) return -EBUSY; /* cannot unregister clockevent and clocksource */ } +static const struct platform_device_id sh_tmu_id_table[] = { + { "sh_tmu", SH_TMU_LEGACY }, + { "sh-tmu", SH_TMU }, + { "sh-tmu-sh3", SH_TMU_SH3 }, + { } +}; +MODULE_DEVICE_TABLE(platform, sh_tmu_id_table); + static struct platform_driver sh_tmu_device_driver = { .probe = sh_tmu_probe, .remove = sh_tmu_remove, .driver = { .name = "sh_tmu", - } + }, + .id_table = sh_tmu_id_table, }; static int __init sh_tmu_init(void)