162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/clk-provider.h>
362306a36Sopenharmony_ci#include <linux/mfd/syscon.h>
462306a36Sopenharmony_ci#include <linux/slab.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <dt-bindings/clock/at91.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "pmc.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistatic DEFINE_SPINLOCK(mck_lock);
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistatic const struct clk_master_characteristics mck_characteristics = {
1362306a36Sopenharmony_ci	.output = { .min = 0, .max = 166000000 },
1462306a36Sopenharmony_ci	.divisors = { 1, 2, 4, 3 },
1562306a36Sopenharmony_ci};
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic u8 plla_out[] = { 0 };
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic u16 plla_icpll[] = { 0 };
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistatic const struct clk_range plla_outputs[] = {
2262306a36Sopenharmony_ci	{ .min = 400000000, .max = 1000000000 },
2362306a36Sopenharmony_ci};
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistatic const struct clk_pll_characteristics plla_characteristics = {
2662306a36Sopenharmony_ci	.input = { .min = 8000000, .max = 50000000 },
2762306a36Sopenharmony_ci	.num_output = ARRAY_SIZE(plla_outputs),
2862306a36Sopenharmony_ci	.output = plla_outputs,
2962306a36Sopenharmony_ci	.icpll = plla_icpll,
3062306a36Sopenharmony_ci	.out = plla_out,
3162306a36Sopenharmony_ci};
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic const struct clk_pcr_layout sama5d3_pcr_layout = {
3462306a36Sopenharmony_ci	.offset = 0x10c,
3562306a36Sopenharmony_ci	.cmd = BIT(12),
3662306a36Sopenharmony_ci	.pid_mask = GENMASK(6, 0),
3762306a36Sopenharmony_ci	.div_mask = GENMASK(17, 16),
3862306a36Sopenharmony_ci};
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic const struct {
4162306a36Sopenharmony_ci	char *n;
4262306a36Sopenharmony_ci	char *p;
4362306a36Sopenharmony_ci	unsigned long flags;
4462306a36Sopenharmony_ci	u8 id;
4562306a36Sopenharmony_ci} sama5d3_systemck[] = {
4662306a36Sopenharmony_ci	/*
4762306a36Sopenharmony_ci	 * ddrck feeds DDR controller and is enabled by bootloader thus we need
4862306a36Sopenharmony_ci	 * to keep it enabled in case there is no Linux consumer for it.
4962306a36Sopenharmony_ci	 */
5062306a36Sopenharmony_ci	{ .n = "ddrck", .p = "masterck_div", .id = 2, .flags = CLK_IS_CRITICAL },
5162306a36Sopenharmony_ci	{ .n = "lcdck", .p = "masterck_div", .id = 3 },
5262306a36Sopenharmony_ci	{ .n = "smdck", .p = "smdclk",       .id = 4 },
5362306a36Sopenharmony_ci	{ .n = "uhpck", .p = "usbck",        .id = 6 },
5462306a36Sopenharmony_ci	{ .n = "udpck", .p = "usbck",        .id = 7 },
5562306a36Sopenharmony_ci	{ .n = "pck0",  .p = "prog0",        .id = 8 },
5662306a36Sopenharmony_ci	{ .n = "pck1",  .p = "prog1",        .id = 9 },
5762306a36Sopenharmony_ci	{ .n = "pck2",  .p = "prog2",        .id = 10 },
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic const struct {
6162306a36Sopenharmony_ci	char *n;
6262306a36Sopenharmony_ci	u8 id;
6362306a36Sopenharmony_ci	struct clk_range r;
6462306a36Sopenharmony_ci	unsigned long flags;
6562306a36Sopenharmony_ci} sama5d3_periphck[] = {
6662306a36Sopenharmony_ci	{ .n = "dbgu_clk", .id = 2, },
6762306a36Sopenharmony_ci	{ .n = "hsmc_clk", .id = 5, },
6862306a36Sopenharmony_ci	{ .n = "pioA_clk", .id = 6, },
6962306a36Sopenharmony_ci	{ .n = "pioB_clk", .id = 7, },
7062306a36Sopenharmony_ci	{ .n = "pioC_clk", .id = 8, },
7162306a36Sopenharmony_ci	{ .n = "pioD_clk", .id = 9, },
7262306a36Sopenharmony_ci	{ .n = "pioE_clk", .id = 10, },
7362306a36Sopenharmony_ci	{ .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, },
7462306a36Sopenharmony_ci	{ .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, },
7562306a36Sopenharmony_ci	{ .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, },
7662306a36Sopenharmony_ci	{ .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, },
7762306a36Sopenharmony_ci	{ .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, },
7862306a36Sopenharmony_ci	{ .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, },
7962306a36Sopenharmony_ci	{ .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, },
8062306a36Sopenharmony_ci	{ .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, },
8162306a36Sopenharmony_ci	{ .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, },
8262306a36Sopenharmony_ci	{ .n = "mci0_clk", .id = 21, },
8362306a36Sopenharmony_ci	{ .n = "mci1_clk", .id = 22, },
8462306a36Sopenharmony_ci	{ .n = "mci2_clk", .id = 23, },
8562306a36Sopenharmony_ci	{ .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, },
8662306a36Sopenharmony_ci	{ .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, },
8762306a36Sopenharmony_ci	{ .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, },
8862306a36Sopenharmony_ci	{ .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, },
8962306a36Sopenharmony_ci	{ .n = "pwm_clk", .id = 28, },
9062306a36Sopenharmony_ci	{ .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, },
9162306a36Sopenharmony_ci	{ .n = "dma0_clk", .id = 30, },
9262306a36Sopenharmony_ci	{ .n = "dma1_clk", .id = 31, },
9362306a36Sopenharmony_ci	{ .n = "uhphs_clk", .id = 32, },
9462306a36Sopenharmony_ci	{ .n = "udphs_clk", .id = 33, },
9562306a36Sopenharmony_ci	{ .n = "macb0_clk", .id = 34, },
9662306a36Sopenharmony_ci	{ .n = "macb1_clk", .id = 35, },
9762306a36Sopenharmony_ci	{ .n = "lcdc_clk", .id = 36, },
9862306a36Sopenharmony_ci	{ .n = "isi_clk", .id = 37, },
9962306a36Sopenharmony_ci	{ .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, },
10062306a36Sopenharmony_ci	{ .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, },
10162306a36Sopenharmony_ci	{ .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, },
10262306a36Sopenharmony_ci	{ .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, },
10362306a36Sopenharmony_ci	{ .n = "sha_clk", .id = 42, },
10462306a36Sopenharmony_ci	{ .n = "aes_clk", .id = 43, },
10562306a36Sopenharmony_ci	{ .n = "tdes_clk", .id = 44, },
10662306a36Sopenharmony_ci	{ .n = "trng_clk", .id = 45, },
10762306a36Sopenharmony_ci	{ .n = "fuse_clk", .id = 48, },
10862306a36Sopenharmony_ci	/*
10962306a36Sopenharmony_ci	 * mpddr_clk feeds DDR controller and is enabled by bootloader thus we
11062306a36Sopenharmony_ci	 * need to keep it enabled in case there is no Linux consumer for it.
11162306a36Sopenharmony_ci	 */
11262306a36Sopenharmony_ci	{ .n = "mpddr_clk", .id = 49, .flags = CLK_IS_CRITICAL },
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic void __init sama5d3_pmc_setup(struct device_node *np)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	const char *slck_name, *mainxtal_name;
11862306a36Sopenharmony_ci	struct pmc_data *sama5d3_pmc;
11962306a36Sopenharmony_ci	const char *parent_names[5];
12062306a36Sopenharmony_ci	struct regmap *regmap;
12162306a36Sopenharmony_ci	struct clk_hw *hw;
12262306a36Sopenharmony_ci	int i;
12362306a36Sopenharmony_ci	bool bypass;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "slow_clk");
12662306a36Sopenharmony_ci	if (i < 0)
12762306a36Sopenharmony_ci		return;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	slck_name = of_clk_get_parent_name(np, i);
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "main_xtal");
13262306a36Sopenharmony_ci	if (i < 0)
13362306a36Sopenharmony_ci		return;
13462306a36Sopenharmony_ci	mainxtal_name = of_clk_get_parent_name(np, i);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	regmap = device_node_to_regmap(np);
13762306a36Sopenharmony_ci	if (IS_ERR(regmap))
13862306a36Sopenharmony_ci		return;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	sama5d3_pmc = pmc_data_allocate(PMC_PLLACK + 1,
14162306a36Sopenharmony_ci					nck(sama5d3_systemck),
14262306a36Sopenharmony_ci					nck(sama5d3_periphck), 0, 3);
14362306a36Sopenharmony_ci	if (!sama5d3_pmc)
14462306a36Sopenharmony_ci		return;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
14762306a36Sopenharmony_ci					   50000000);
14862306a36Sopenharmony_ci	if (IS_ERR(hw))
14962306a36Sopenharmony_ci		goto err_free;
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	bypass = of_property_read_bool(np, "atmel,osc-bypass");
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
15462306a36Sopenharmony_ci					bypass);
15562306a36Sopenharmony_ci	if (IS_ERR(hw))
15662306a36Sopenharmony_ci		goto err_free;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	parent_names[0] = "main_rc_osc";
15962306a36Sopenharmony_ci	parent_names[1] = "main_osc";
16062306a36Sopenharmony_ci	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, NULL, 2);
16162306a36Sopenharmony_ci	if (IS_ERR(hw))
16262306a36Sopenharmony_ci		goto err_free;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
16562306a36Sopenharmony_ci				   &sama5d3_pll_layout, &plla_characteristics);
16662306a36Sopenharmony_ci	if (IS_ERR(hw))
16762306a36Sopenharmony_ci		goto err_free;
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
17062306a36Sopenharmony_ci	if (IS_ERR(hw))
17162306a36Sopenharmony_ci		goto err_free;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	sama5d3_pmc->chws[PMC_PLLACK] = hw;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck", NULL);
17662306a36Sopenharmony_ci	if (IS_ERR(hw))
17762306a36Sopenharmony_ci		goto err_free;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	sama5d3_pmc->chws[PMC_UTMI] = hw;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	parent_names[0] = slck_name;
18262306a36Sopenharmony_ci	parent_names[1] = "mainck";
18362306a36Sopenharmony_ci	parent_names[2] = "plladivck";
18462306a36Sopenharmony_ci	parent_names[3] = "utmick";
18562306a36Sopenharmony_ci	hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
18662306a36Sopenharmony_ci					   parent_names, NULL,
18762306a36Sopenharmony_ci					   &at91sam9x5_master_layout,
18862306a36Sopenharmony_ci					   &mck_characteristics, &mck_lock);
18962306a36Sopenharmony_ci	if (IS_ERR(hw))
19062306a36Sopenharmony_ci		goto err_free;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	hw = at91_clk_register_master_div(regmap, "masterck_div",
19362306a36Sopenharmony_ci					  "masterck_pres", NULL,
19462306a36Sopenharmony_ci					  &at91sam9x5_master_layout,
19562306a36Sopenharmony_ci					  &mck_characteristics, &mck_lock,
19662306a36Sopenharmony_ci					  CLK_SET_RATE_GATE, 0);
19762306a36Sopenharmony_ci	if (IS_ERR(hw))
19862306a36Sopenharmony_ci		goto err_free;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	sama5d3_pmc->chws[PMC_MCK] = hw;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	parent_names[0] = "plladivck";
20362306a36Sopenharmony_ci	parent_names[1] = "utmick";
20462306a36Sopenharmony_ci	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
20562306a36Sopenharmony_ci	if (IS_ERR(hw))
20662306a36Sopenharmony_ci		goto err_free;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
20962306a36Sopenharmony_ci	if (IS_ERR(hw))
21062306a36Sopenharmony_ci		goto err_free;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	parent_names[0] = slck_name;
21362306a36Sopenharmony_ci	parent_names[1] = "mainck";
21462306a36Sopenharmony_ci	parent_names[2] = "plladivck";
21562306a36Sopenharmony_ci	parent_names[3] = "utmick";
21662306a36Sopenharmony_ci	parent_names[4] = "masterck_div";
21762306a36Sopenharmony_ci	for (i = 0; i < 3; i++) {
21862306a36Sopenharmony_ci		char name[6];
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci		snprintf(name, sizeof(name), "prog%d", i);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci		hw = at91_clk_register_programmable(regmap, name,
22362306a36Sopenharmony_ci						    parent_names, NULL, 5, i,
22462306a36Sopenharmony_ci						    &at91sam9x5_programmable_layout,
22562306a36Sopenharmony_ci						    NULL);
22662306a36Sopenharmony_ci		if (IS_ERR(hw))
22762306a36Sopenharmony_ci			goto err_free;
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci		sama5d3_pmc->pchws[i] = hw;
23062306a36Sopenharmony_ci	}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) {
23362306a36Sopenharmony_ci		hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n,
23462306a36Sopenharmony_ci					      sama5d3_systemck[i].p, NULL,
23562306a36Sopenharmony_ci					      sama5d3_systemck[i].id,
23662306a36Sopenharmony_ci					      sama5d3_systemck[i].flags);
23762306a36Sopenharmony_ci		if (IS_ERR(hw))
23862306a36Sopenharmony_ci			goto err_free;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci		sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw;
24162306a36Sopenharmony_ci	}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) {
24462306a36Sopenharmony_ci		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
24562306a36Sopenharmony_ci							 &sama5d3_pcr_layout,
24662306a36Sopenharmony_ci							 sama5d3_periphck[i].n,
24762306a36Sopenharmony_ci							 "masterck_div", NULL,
24862306a36Sopenharmony_ci							 sama5d3_periphck[i].id,
24962306a36Sopenharmony_ci							 &sama5d3_periphck[i].r,
25062306a36Sopenharmony_ci							 INT_MIN,
25162306a36Sopenharmony_ci							 sama5d3_periphck[i].flags);
25262306a36Sopenharmony_ci		if (IS_ERR(hw))
25362306a36Sopenharmony_ci			goto err_free;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci		sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw;
25662306a36Sopenharmony_ci	}
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc);
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	return;
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cierr_free:
26362306a36Sopenharmony_ci	kfree(sama5d3_pmc);
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_ci/*
26662306a36Sopenharmony_ci * The TCB is used as the clocksource so its clock is needed early. This means
26762306a36Sopenharmony_ci * this can't be a platform driver.
26862306a36Sopenharmony_ci */
26962306a36Sopenharmony_ciCLK_OF_DECLARE(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup);
270