18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
38c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
48c2ecf20Sopenharmony_ci#include <linux/slab.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <dt-bindings/clock/at91.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "pmc.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic const struct clk_master_characteristics mck_characteristics = {
118c2ecf20Sopenharmony_ci	.output = { .min = 0, .max = 166000000 },
128c2ecf20Sopenharmony_ci	.divisors = { 1, 2, 4, 3 },
138c2ecf20Sopenharmony_ci};
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic u8 plla_out[] = { 0 };
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic u16 plla_icpll[] = { 0 };
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic const struct clk_range plla_outputs[] = {
208c2ecf20Sopenharmony_ci	{ .min = 400000000, .max = 1000000000 },
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const struct clk_pll_characteristics plla_characteristics = {
248c2ecf20Sopenharmony_ci	.input = { .min = 8000000, .max = 50000000 },
258c2ecf20Sopenharmony_ci	.num_output = ARRAY_SIZE(plla_outputs),
268c2ecf20Sopenharmony_ci	.output = plla_outputs,
278c2ecf20Sopenharmony_ci	.icpll = plla_icpll,
288c2ecf20Sopenharmony_ci	.out = plla_out,
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic const struct clk_pcr_layout sama5d3_pcr_layout = {
328c2ecf20Sopenharmony_ci	.offset = 0x10c,
338c2ecf20Sopenharmony_ci	.cmd = BIT(12),
348c2ecf20Sopenharmony_ci	.pid_mask = GENMASK(6, 0),
358c2ecf20Sopenharmony_ci	.div_mask = GENMASK(17, 16),
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const struct {
398c2ecf20Sopenharmony_ci	char *n;
408c2ecf20Sopenharmony_ci	char *p;
418c2ecf20Sopenharmony_ci	u8 id;
428c2ecf20Sopenharmony_ci} sama5d3_systemck[] = {
438c2ecf20Sopenharmony_ci	{ .n = "ddrck", .p = "masterck", .id = 2 },
448c2ecf20Sopenharmony_ci	{ .n = "lcdck", .p = "masterck", .id = 3 },
458c2ecf20Sopenharmony_ci	{ .n = "smdck", .p = "smdclk",   .id = 4 },
468c2ecf20Sopenharmony_ci	{ .n = "uhpck", .p = "usbck",    .id = 6 },
478c2ecf20Sopenharmony_ci	{ .n = "udpck", .p = "usbck",    .id = 7 },
488c2ecf20Sopenharmony_ci	{ .n = "pck0",  .p = "prog0",    .id = 8 },
498c2ecf20Sopenharmony_ci	{ .n = "pck1",  .p = "prog1",    .id = 9 },
508c2ecf20Sopenharmony_ci	{ .n = "pck2",  .p = "prog2",    .id = 10 },
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic const struct {
548c2ecf20Sopenharmony_ci	char *n;
558c2ecf20Sopenharmony_ci	u8 id;
568c2ecf20Sopenharmony_ci	struct clk_range r;
578c2ecf20Sopenharmony_ci} sama5d3_periphck[] = {
588c2ecf20Sopenharmony_ci	{ .n = "dbgu_clk", .id = 2, },
598c2ecf20Sopenharmony_ci	{ .n = "hsmc_clk", .id = 5, },
608c2ecf20Sopenharmony_ci	{ .n = "pioA_clk", .id = 6, },
618c2ecf20Sopenharmony_ci	{ .n = "pioB_clk", .id = 7, },
628c2ecf20Sopenharmony_ci	{ .n = "pioC_clk", .id = 8, },
638c2ecf20Sopenharmony_ci	{ .n = "pioD_clk", .id = 9, },
648c2ecf20Sopenharmony_ci	{ .n = "pioE_clk", .id = 10, },
658c2ecf20Sopenharmony_ci	{ .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, },
668c2ecf20Sopenharmony_ci	{ .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, },
678c2ecf20Sopenharmony_ci	{ .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, },
688c2ecf20Sopenharmony_ci	{ .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, },
698c2ecf20Sopenharmony_ci	{ .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, },
708c2ecf20Sopenharmony_ci	{ .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, },
718c2ecf20Sopenharmony_ci	{ .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, },
728c2ecf20Sopenharmony_ci	{ .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, },
738c2ecf20Sopenharmony_ci	{ .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, },
748c2ecf20Sopenharmony_ci	{ .n = "mci0_clk", .id = 21, },
758c2ecf20Sopenharmony_ci	{ .n = "mci1_clk", .id = 22, },
768c2ecf20Sopenharmony_ci	{ .n = "mci2_clk", .id = 23, },
778c2ecf20Sopenharmony_ci	{ .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, },
788c2ecf20Sopenharmony_ci	{ .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, },
798c2ecf20Sopenharmony_ci	{ .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, },
808c2ecf20Sopenharmony_ci	{ .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, },
818c2ecf20Sopenharmony_ci	{ .n = "pwm_clk", .id = 28, },
828c2ecf20Sopenharmony_ci	{ .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, },
838c2ecf20Sopenharmony_ci	{ .n = "dma0_clk", .id = 30, },
848c2ecf20Sopenharmony_ci	{ .n = "dma1_clk", .id = 31, },
858c2ecf20Sopenharmony_ci	{ .n = "uhphs_clk", .id = 32, },
868c2ecf20Sopenharmony_ci	{ .n = "udphs_clk", .id = 33, },
878c2ecf20Sopenharmony_ci	{ .n = "macb0_clk", .id = 34, },
888c2ecf20Sopenharmony_ci	{ .n = "macb1_clk", .id = 35, },
898c2ecf20Sopenharmony_ci	{ .n = "lcdc_clk", .id = 36, },
908c2ecf20Sopenharmony_ci	{ .n = "isi_clk", .id = 37, },
918c2ecf20Sopenharmony_ci	{ .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, },
928c2ecf20Sopenharmony_ci	{ .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, },
938c2ecf20Sopenharmony_ci	{ .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, },
948c2ecf20Sopenharmony_ci	{ .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, },
958c2ecf20Sopenharmony_ci	{ .n = "sha_clk", .id = 42, },
968c2ecf20Sopenharmony_ci	{ .n = "aes_clk", .id = 43, },
978c2ecf20Sopenharmony_ci	{ .n = "tdes_clk", .id = 44, },
988c2ecf20Sopenharmony_ci	{ .n = "trng_clk", .id = 45, },
998c2ecf20Sopenharmony_ci	{ .n = "fuse_clk", .id = 48, },
1008c2ecf20Sopenharmony_ci	{ .n = "mpddr_clk", .id = 49, },
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void __init sama5d3_pmc_setup(struct device_node *np)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	const char *slck_name, *mainxtal_name;
1068c2ecf20Sopenharmony_ci	struct pmc_data *sama5d3_pmc;
1078c2ecf20Sopenharmony_ci	const char *parent_names[5];
1088c2ecf20Sopenharmony_ci	struct regmap *regmap;
1098c2ecf20Sopenharmony_ci	struct clk_hw *hw;
1108c2ecf20Sopenharmony_ci	int i;
1118c2ecf20Sopenharmony_ci	bool bypass;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "slow_clk");
1148c2ecf20Sopenharmony_ci	if (i < 0)
1158c2ecf20Sopenharmony_ci		return;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	slck_name = of_clk_get_parent_name(np, i);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "main_xtal");
1208c2ecf20Sopenharmony_ci	if (i < 0)
1218c2ecf20Sopenharmony_ci		return;
1228c2ecf20Sopenharmony_ci	mainxtal_name = of_clk_get_parent_name(np, i);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	regmap = device_node_to_regmap(np);
1258c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
1268c2ecf20Sopenharmony_ci		return;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	sama5d3_pmc = pmc_data_allocate(PMC_PLLACK + 1,
1298c2ecf20Sopenharmony_ci					nck(sama5d3_systemck),
1308c2ecf20Sopenharmony_ci					nck(sama5d3_periphck), 0, 3);
1318c2ecf20Sopenharmony_ci	if (!sama5d3_pmc)
1328c2ecf20Sopenharmony_ci		return;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
1358c2ecf20Sopenharmony_ci					   50000000);
1368c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1378c2ecf20Sopenharmony_ci		goto err_free;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	bypass = of_property_read_bool(np, "atmel,osc-bypass");
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
1428c2ecf20Sopenharmony_ci					bypass);
1438c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1448c2ecf20Sopenharmony_ci		goto err_free;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	parent_names[0] = "main_rc_osc";
1478c2ecf20Sopenharmony_ci	parent_names[1] = "main_osc";
1488c2ecf20Sopenharmony_ci	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
1498c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1508c2ecf20Sopenharmony_ci		goto err_free;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1538c2ecf20Sopenharmony_ci				   &sama5d3_pll_layout, &plla_characteristics);
1548c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1558c2ecf20Sopenharmony_ci		goto err_free;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
1588c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1598c2ecf20Sopenharmony_ci		goto err_free;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	sama5d3_pmc->chws[PMC_PLLACK] = hw;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck");
1648c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1658c2ecf20Sopenharmony_ci		goto err_free;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	sama5d3_pmc->chws[PMC_UTMI] = hw;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
1708c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1718c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
1728c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
1738c2ecf20Sopenharmony_ci	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
1748c2ecf20Sopenharmony_ci				      &at91sam9x5_master_layout,
1758c2ecf20Sopenharmony_ci				      &mck_characteristics);
1768c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1778c2ecf20Sopenharmony_ci		goto err_free;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	sama5d3_pmc->chws[PMC_MCK] = hw;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	parent_names[0] = "plladivck";
1828c2ecf20Sopenharmony_ci	parent_names[1] = "utmick";
1838c2ecf20Sopenharmony_ci	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
1848c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1858c2ecf20Sopenharmony_ci		goto err_free;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
1888c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1898c2ecf20Sopenharmony_ci		goto err_free;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
1928c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1938c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
1948c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
1958c2ecf20Sopenharmony_ci	parent_names[4] = "masterck";
1968c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++) {
1978c2ecf20Sopenharmony_ci		char name[6];
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "prog%d", i);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		hw = at91_clk_register_programmable(regmap, name,
2028c2ecf20Sopenharmony_ci						    parent_names, 5, i,
2038c2ecf20Sopenharmony_ci						    &at91sam9x5_programmable_layout,
2048c2ecf20Sopenharmony_ci						    NULL);
2058c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2068c2ecf20Sopenharmony_ci			goto err_free;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		sama5d3_pmc->pchws[i] = hw;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) {
2128c2ecf20Sopenharmony_ci		hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n,
2138c2ecf20Sopenharmony_ci					      sama5d3_systemck[i].p,
2148c2ecf20Sopenharmony_ci					      sama5d3_systemck[i].id);
2158c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2168c2ecf20Sopenharmony_ci			goto err_free;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) {
2228c2ecf20Sopenharmony_ci		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
2238c2ecf20Sopenharmony_ci							 &sama5d3_pcr_layout,
2248c2ecf20Sopenharmony_ci							 sama5d3_periphck[i].n,
2258c2ecf20Sopenharmony_ci							 "masterck",
2268c2ecf20Sopenharmony_ci							 sama5d3_periphck[i].id,
2278c2ecf20Sopenharmony_ci							 &sama5d3_periphck[i].r,
2288c2ecf20Sopenharmony_ci							 INT_MIN);
2298c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2308c2ecf20Sopenharmony_ci			goto err_free;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cierr_free:
2408c2ecf20Sopenharmony_ci	kfree(sama5d3_pmc);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci/*
2438c2ecf20Sopenharmony_ci * The TCB is used as the clocksource so its clock is needed early. This means
2448c2ecf20Sopenharmony_ci * this can't be a platform driver.
2458c2ecf20Sopenharmony_ci */
2468c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup);
247