2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-12-02 22:07:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
|
|
|
|
*/
|
|
|
|
|
2021-10-11 19:27:05 +08:00
|
|
|
#include <linux/clk.h>
|
2013-12-02 22:07:02 +08:00
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/clkdev.h>
|
|
|
|
#include <linux/clk/at91_pmc.h>
|
|
|
|
#include <linux/of.h>
|
2021-10-11 19:27:06 +08:00
|
|
|
#include <linux/of_address.h>
|
2014-09-05 15:54:13 +08:00
|
|
|
#include <linux/mfd/syscon.h>
|
2017-06-08 08:36:47 +08:00
|
|
|
#include <linux/platform_device.h>
|
2014-09-07 14:14:29 +08:00
|
|
|
#include <linux/regmap.h>
|
2017-06-08 08:36:47 +08:00
|
|
|
#include <linux/syscore_ops.h>
|
2013-12-02 22:07:02 +08:00
|
|
|
|
|
|
|
#include <asm/proc-fns.h>
|
|
|
|
|
|
|
|
#include "pmc.h"
|
|
|
|
|
2017-06-08 08:36:47 +08:00
|
|
|
#define PMC_MAX_IDS 128
|
2017-12-12 00:55:35 +08:00
|
|
|
#define PMC_MAX_PCKS 8
|
2017-06-08 08:36:47 +08:00
|
|
|
|
2013-12-02 22:07:02 +08:00
|
|
|
int of_at91_get_clk_range(struct device_node *np, const char *propname,
|
|
|
|
struct clk_range *range)
|
|
|
|
{
|
|
|
|
u32 min, max;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32_index(np, propname, 0, &min);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32_index(np, propname, 1, &max);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (range) {
|
|
|
|
range->min = min;
|
|
|
|
range->max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
|
2017-06-08 08:36:47 +08:00
|
|
|
|
2018-10-16 22:21:47 +08:00
|
|
|
struct clk_hw *of_clk_hw_pmc_get(struct of_phandle_args *clkspec, void *data)
|
|
|
|
{
|
|
|
|
unsigned int type = clkspec->args[0];
|
|
|
|
unsigned int idx = clkspec->args[1];
|
|
|
|
struct pmc_data *pmc_data = data;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case PMC_TYPE_CORE:
|
|
|
|
if (idx < pmc_data->ncore)
|
|
|
|
return pmc_data->chws[idx];
|
|
|
|
break;
|
|
|
|
case PMC_TYPE_SYSTEM:
|
|
|
|
if (idx < pmc_data->nsystem)
|
|
|
|
return pmc_data->shws[idx];
|
|
|
|
break;
|
|
|
|
case PMC_TYPE_PERIPHERAL:
|
|
|
|
if (idx < pmc_data->nperiph)
|
|
|
|
return pmc_data->phws[idx];
|
|
|
|
break;
|
|
|
|
case PMC_TYPE_GCK:
|
|
|
|
if (idx < pmc_data->ngck)
|
|
|
|
return pmc_data->ghws[idx];
|
|
|
|
break;
|
2020-05-05 06:37:56 +08:00
|
|
|
case PMC_TYPE_PROGRAMMABLE:
|
|
|
|
if (idx < pmc_data->npck)
|
|
|
|
return pmc_data->pchws[idx];
|
|
|
|
break;
|
2018-10-16 22:21:47 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_err("%s: invalid type (%u) or index (%u)\n", __func__, type, idx);
|
|
|
|
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:21:45 +08:00
|
|
|
struct pmc_data *pmc_data_allocate(unsigned int ncore, unsigned int nsystem,
|
2020-05-05 06:37:56 +08:00
|
|
|
unsigned int nperiph, unsigned int ngck,
|
|
|
|
unsigned int npck)
|
2018-10-16 22:21:45 +08:00
|
|
|
{
|
2020-05-05 06:37:56 +08:00
|
|
|
unsigned int num_clks = ncore + nsystem + nperiph + ngck + npck;
|
2020-05-05 06:37:56 +08:00
|
|
|
struct pmc_data *pmc_data;
|
2018-10-16 22:21:45 +08:00
|
|
|
|
2020-05-05 06:37:56 +08:00
|
|
|
pmc_data = kzalloc(struct_size(pmc_data, hwtable, num_clks),
|
|
|
|
GFP_KERNEL);
|
2018-10-16 22:21:45 +08:00
|
|
|
if (!pmc_data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pmc_data->ncore = ncore;
|
2020-05-05 06:37:56 +08:00
|
|
|
pmc_data->chws = pmc_data->hwtable;
|
2018-10-16 22:21:45 +08:00
|
|
|
|
|
|
|
pmc_data->nsystem = nsystem;
|
2020-05-05 06:37:56 +08:00
|
|
|
pmc_data->shws = pmc_data->chws + ncore;
|
2018-10-16 22:21:45 +08:00
|
|
|
|
|
|
|
pmc_data->nperiph = nperiph;
|
2020-05-05 06:37:56 +08:00
|
|
|
pmc_data->phws = pmc_data->shws + nsystem;
|
2018-10-16 22:21:45 +08:00
|
|
|
|
|
|
|
pmc_data->ngck = ngck;
|
2020-05-05 06:37:56 +08:00
|
|
|
pmc_data->ghws = pmc_data->phws + nperiph;
|
2018-10-16 22:21:45 +08:00
|
|
|
|
2020-05-05 06:37:56 +08:00
|
|
|
pmc_data->npck = npck;
|
|
|
|
pmc_data->pchws = pmc_data->ghws + ngck;
|
|
|
|
|
2018-10-16 22:21:45 +08:00
|
|
|
return pmc_data;
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:36:47 +08:00
|
|
|
#ifdef CONFIG_PM
|
2021-10-11 19:27:06 +08:00
|
|
|
|
|
|
|
/* Address in SECURAM that say if we suspend to backup mode. */
|
|
|
|
static void __iomem *at91_pmc_backup_suspend;
|
|
|
|
|
2021-10-11 19:27:05 +08:00
|
|
|
static int at91_pmc_suspend(void)
|
2017-06-08 08:36:47 +08:00
|
|
|
{
|
2021-10-11 19:27:06 +08:00
|
|
|
unsigned int backup;
|
|
|
|
|
|
|
|
if (!at91_pmc_backup_suspend)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
backup = readl_relaxed(at91_pmc_backup_suspend);
|
|
|
|
if (!backup)
|
|
|
|
return 0;
|
|
|
|
|
2021-10-11 19:27:05 +08:00
|
|
|
return clk_save_context();
|
2017-06-08 08:36:47 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 19:27:05 +08:00
|
|
|
static void at91_pmc_resume(void)
|
2017-12-12 00:55:33 +08:00
|
|
|
{
|
2021-10-11 19:27:06 +08:00
|
|
|
unsigned int backup;
|
|
|
|
|
|
|
|
if (!at91_pmc_backup_suspend)
|
|
|
|
return;
|
|
|
|
|
|
|
|
backup = readl_relaxed(at91_pmc_backup_suspend);
|
|
|
|
if (!backup)
|
|
|
|
return;
|
|
|
|
|
2021-10-11 19:27:05 +08:00
|
|
|
clk_restore_context();
|
2017-06-08 08:36:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct syscore_ops pmc_syscore_ops = {
|
2021-10-11 19:27:05 +08:00
|
|
|
.suspend = at91_pmc_suspend,
|
|
|
|
.resume = at91_pmc_resume,
|
2017-06-08 08:36:47 +08:00
|
|
|
};
|
|
|
|
|
2021-10-11 19:27:10 +08:00
|
|
|
static const struct of_device_id pmc_dt_ids[] = {
|
2017-06-08 08:36:47 +08:00
|
|
|
{ .compatible = "atmel,sama5d2-pmc" },
|
2021-10-11 19:27:10 +08:00
|
|
|
{ .compatible = "microchip,sama7g5-pmc", },
|
2017-06-08 08:36:47 +08:00
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init pmc_register_ops(void)
|
|
|
|
{
|
|
|
|
struct device_node *np;
|
|
|
|
|
2021-10-11 19:27:10 +08:00
|
|
|
np = of_find_matching_node(NULL, pmc_dt_ids);
|
2020-05-03 20:18:08 +08:00
|
|
|
if (!np)
|
|
|
|
return -ENODEV;
|
2017-06-08 08:36:47 +08:00
|
|
|
|
2021-09-13 16:26:33 +08:00
|
|
|
if (!of_device_is_available(np)) {
|
|
|
|
of_node_put(np);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2020-05-03 20:18:09 +08:00
|
|
|
of_node_put(np);
|
2017-06-08 08:36:47 +08:00
|
|
|
|
2021-10-11 19:27:06 +08:00
|
|
|
np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-securam");
|
|
|
|
if (!np)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (!of_device_is_available(np)) {
|
|
|
|
of_node_put(np);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
of_node_put(np);
|
|
|
|
|
|
|
|
at91_pmc_backup_suspend = of_iomap(np, 0);
|
|
|
|
if (!at91_pmc_backup_suspend) {
|
|
|
|
pr_warn("%s(): unable to map securam\n", __func__);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:36:47 +08:00
|
|
|
register_syscore_ops(&pmc_syscore_ops);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* This has to happen before arch_initcall because of the tcb_clksrc driver */
|
|
|
|
postcore_initcall(pmc_register_ops);
|
|
|
|
#endif
|