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 = 125000000, .max = 200000000 },
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 = 600000000, .max = 1200000000 },
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const struct clk_pll_characteristics plla_characteristics = {
248c2ecf20Sopenharmony_ci	.input = { .min = 12000000, .max = 12000000 },
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 sama5d4_pcr_layout = {
328c2ecf20Sopenharmony_ci	.offset = 0x10c,
338c2ecf20Sopenharmony_ci	.cmd = BIT(12),
348c2ecf20Sopenharmony_ci	.pid_mask = GENMASK(6, 0),
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic const struct {
388c2ecf20Sopenharmony_ci	char *n;
398c2ecf20Sopenharmony_ci	char *p;
408c2ecf20Sopenharmony_ci	u8 id;
418c2ecf20Sopenharmony_ci} sama5d4_systemck[] = {
428c2ecf20Sopenharmony_ci	{ .n = "ddrck", .p = "masterck", .id = 2 },
438c2ecf20Sopenharmony_ci	{ .n = "lcdck", .p = "masterck", .id = 3 },
448c2ecf20Sopenharmony_ci	{ .n = "smdck", .p = "smdclk",   .id = 4 },
458c2ecf20Sopenharmony_ci	{ .n = "uhpck", .p = "usbck",    .id = 6 },
468c2ecf20Sopenharmony_ci	{ .n = "udpck", .p = "usbck",    .id = 7 },
478c2ecf20Sopenharmony_ci	{ .n = "pck0",  .p = "prog0",    .id = 8 },
488c2ecf20Sopenharmony_ci	{ .n = "pck1",  .p = "prog1",    .id = 9 },
498c2ecf20Sopenharmony_ci	{ .n = "pck2",  .p = "prog2",    .id = 10 },
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const struct {
538c2ecf20Sopenharmony_ci	char *n;
548c2ecf20Sopenharmony_ci	u8 id;
558c2ecf20Sopenharmony_ci} sama5d4_periph32ck[] = {
568c2ecf20Sopenharmony_ci	{ .n = "pioD_clk", .id = 5 },
578c2ecf20Sopenharmony_ci	{ .n = "usart0_clk", .id = 6 },
588c2ecf20Sopenharmony_ci	{ .n = "usart1_clk", .id = 7 },
598c2ecf20Sopenharmony_ci	{ .n = "icm_clk", .id = 9 },
608c2ecf20Sopenharmony_ci	{ .n = "aes_clk", .id = 12 },
618c2ecf20Sopenharmony_ci	{ .n = "tdes_clk", .id = 14 },
628c2ecf20Sopenharmony_ci	{ .n = "sha_clk", .id = 15 },
638c2ecf20Sopenharmony_ci	{ .n = "matrix1_clk", .id = 17 },
648c2ecf20Sopenharmony_ci	{ .n = "hsmc_clk", .id = 22 },
658c2ecf20Sopenharmony_ci	{ .n = "pioA_clk", .id = 23 },
668c2ecf20Sopenharmony_ci	{ .n = "pioB_clk", .id = 24 },
678c2ecf20Sopenharmony_ci	{ .n = "pioC_clk", .id = 25 },
688c2ecf20Sopenharmony_ci	{ .n = "pioE_clk", .id = 26 },
698c2ecf20Sopenharmony_ci	{ .n = "uart0_clk", .id = 27 },
708c2ecf20Sopenharmony_ci	{ .n = "uart1_clk", .id = 28 },
718c2ecf20Sopenharmony_ci	{ .n = "usart2_clk", .id = 29 },
728c2ecf20Sopenharmony_ci	{ .n = "usart3_clk", .id = 30 },
738c2ecf20Sopenharmony_ci	{ .n = "usart4_clk", .id = 31 },
748c2ecf20Sopenharmony_ci	{ .n = "twi0_clk", .id = 32 },
758c2ecf20Sopenharmony_ci	{ .n = "twi1_clk", .id = 33 },
768c2ecf20Sopenharmony_ci	{ .n = "twi2_clk", .id = 34 },
778c2ecf20Sopenharmony_ci	{ .n = "mci0_clk", .id = 35 },
788c2ecf20Sopenharmony_ci	{ .n = "mci1_clk", .id = 36 },
798c2ecf20Sopenharmony_ci	{ .n = "spi0_clk", .id = 37 },
808c2ecf20Sopenharmony_ci	{ .n = "spi1_clk", .id = 38 },
818c2ecf20Sopenharmony_ci	{ .n = "spi2_clk", .id = 39 },
828c2ecf20Sopenharmony_ci	{ .n = "tcb0_clk", .id = 40 },
838c2ecf20Sopenharmony_ci	{ .n = "tcb1_clk", .id = 41 },
848c2ecf20Sopenharmony_ci	{ .n = "tcb2_clk", .id = 42 },
858c2ecf20Sopenharmony_ci	{ .n = "pwm_clk", .id = 43 },
868c2ecf20Sopenharmony_ci	{ .n = "adc_clk", .id = 44 },
878c2ecf20Sopenharmony_ci	{ .n = "dbgu_clk", .id = 45 },
888c2ecf20Sopenharmony_ci	{ .n = "uhphs_clk", .id = 46 },
898c2ecf20Sopenharmony_ci	{ .n = "udphs_clk", .id = 47 },
908c2ecf20Sopenharmony_ci	{ .n = "ssc0_clk", .id = 48 },
918c2ecf20Sopenharmony_ci	{ .n = "ssc1_clk", .id = 49 },
928c2ecf20Sopenharmony_ci	{ .n = "trng_clk", .id = 53 },
938c2ecf20Sopenharmony_ci	{ .n = "macb0_clk", .id = 54 },
948c2ecf20Sopenharmony_ci	{ .n = "macb1_clk", .id = 55 },
958c2ecf20Sopenharmony_ci	{ .n = "fuse_clk", .id = 57 },
968c2ecf20Sopenharmony_ci	{ .n = "securam_clk", .id = 59 },
978c2ecf20Sopenharmony_ci	{ .n = "smd_clk", .id = 61 },
988c2ecf20Sopenharmony_ci	{ .n = "twi3_clk", .id = 62 },
998c2ecf20Sopenharmony_ci	{ .n = "catb_clk", .id = 63 },
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic const struct {
1038c2ecf20Sopenharmony_ci	char *n;
1048c2ecf20Sopenharmony_ci	u8 id;
1058c2ecf20Sopenharmony_ci} sama5d4_periphck[] = {
1068c2ecf20Sopenharmony_ci	{ .n = "dma0_clk", .id = 8 },
1078c2ecf20Sopenharmony_ci	{ .n = "cpkcc_clk", .id = 10 },
1088c2ecf20Sopenharmony_ci	{ .n = "aesb_clk", .id = 13 },
1098c2ecf20Sopenharmony_ci	{ .n = "mpddr_clk", .id = 16 },
1108c2ecf20Sopenharmony_ci	{ .n = "matrix0_clk", .id = 18 },
1118c2ecf20Sopenharmony_ci	{ .n = "vdec_clk", .id = 19 },
1128c2ecf20Sopenharmony_ci	{ .n = "dma1_clk", .id = 50 },
1138c2ecf20Sopenharmony_ci	{ .n = "lcdc_clk", .id = 51 },
1148c2ecf20Sopenharmony_ci	{ .n = "isi_clk", .id = 52 },
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void __init sama5d4_pmc_setup(struct device_node *np)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct clk_range range = CLK_RANGE(0, 0);
1208c2ecf20Sopenharmony_ci	const char *slck_name, *mainxtal_name;
1218c2ecf20Sopenharmony_ci	struct pmc_data *sama5d4_pmc;
1228c2ecf20Sopenharmony_ci	const char *parent_names[5];
1238c2ecf20Sopenharmony_ci	struct regmap *regmap;
1248c2ecf20Sopenharmony_ci	struct clk_hw *hw;
1258c2ecf20Sopenharmony_ci	int i;
1268c2ecf20Sopenharmony_ci	bool bypass;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "slow_clk");
1298c2ecf20Sopenharmony_ci	if (i < 0)
1308c2ecf20Sopenharmony_ci		return;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	slck_name = of_clk_get_parent_name(np, i);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "main_xtal");
1358c2ecf20Sopenharmony_ci	if (i < 0)
1368c2ecf20Sopenharmony_ci		return;
1378c2ecf20Sopenharmony_ci	mainxtal_name = of_clk_get_parent_name(np, i);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	regmap = device_node_to_regmap(np);
1408c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
1418c2ecf20Sopenharmony_ci		return;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	sama5d4_pmc = pmc_data_allocate(PMC_PLLACK + 1,
1448c2ecf20Sopenharmony_ci					nck(sama5d4_systemck),
1458c2ecf20Sopenharmony_ci					nck(sama5d4_periph32ck), 0, 3);
1468c2ecf20Sopenharmony_ci	if (!sama5d4_pmc)
1478c2ecf20Sopenharmony_ci		return;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
1508c2ecf20Sopenharmony_ci					   100000000);
1518c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1528c2ecf20Sopenharmony_ci		goto err_free;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	bypass = of_property_read_bool(np, "atmel,osc-bypass");
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
1578c2ecf20Sopenharmony_ci					bypass);
1588c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1598c2ecf20Sopenharmony_ci		goto err_free;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	parent_names[0] = "main_rc_osc";
1628c2ecf20Sopenharmony_ci	parent_names[1] = "main_osc";
1638c2ecf20Sopenharmony_ci	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
1648c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1658c2ecf20Sopenharmony_ci		goto err_free;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1688c2ecf20Sopenharmony_ci				   &sama5d3_pll_layout, &plla_characteristics);
1698c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1708c2ecf20Sopenharmony_ci		goto err_free;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
1738c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1748c2ecf20Sopenharmony_ci		goto err_free;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	sama5d4_pmc->chws[PMC_PLLACK] = hw;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck");
1798c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1808c2ecf20Sopenharmony_ci		goto err_free;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	sama5d4_pmc->chws[PMC_UTMI] = hw;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
1858c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1868c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
1878c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
1888c2ecf20Sopenharmony_ci	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
1898c2ecf20Sopenharmony_ci				      &at91sam9x5_master_layout,
1908c2ecf20Sopenharmony_ci				      &mck_characteristics);
1918c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1928c2ecf20Sopenharmony_ci		goto err_free;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	sama5d4_pmc->chws[PMC_MCK] = hw;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	hw = at91_clk_register_h32mx(regmap, "h32mxck", "masterck");
1978c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1988c2ecf20Sopenharmony_ci		goto err_free;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	sama5d4_pmc->chws[PMC_MCK2] = hw;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	parent_names[0] = "plladivck";
2038c2ecf20Sopenharmony_ci	parent_names[1] = "utmick";
2048c2ecf20Sopenharmony_ci	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
2058c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2068c2ecf20Sopenharmony_ci		goto err_free;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	parent_names[0] = "plladivck";
2098c2ecf20Sopenharmony_ci	parent_names[1] = "utmick";
2108c2ecf20Sopenharmony_ci	hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
2118c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2128c2ecf20Sopenharmony_ci		goto err_free;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
2158c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
2168c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
2178c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
2188c2ecf20Sopenharmony_ci	parent_names[4] = "masterck";
2198c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++) {
2208c2ecf20Sopenharmony_ci		char name[6];
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "prog%d", i);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		hw = at91_clk_register_programmable(regmap, name,
2258c2ecf20Sopenharmony_ci						    parent_names, 5, i,
2268c2ecf20Sopenharmony_ci						    &at91sam9x5_programmable_layout,
2278c2ecf20Sopenharmony_ci						    NULL);
2288c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2298c2ecf20Sopenharmony_ci			goto err_free;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci		sama5d4_pmc->pchws[i] = hw;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d4_systemck); i++) {
2358c2ecf20Sopenharmony_ci		hw = at91_clk_register_system(regmap, sama5d4_systemck[i].n,
2368c2ecf20Sopenharmony_ci					      sama5d4_systemck[i].p,
2378c2ecf20Sopenharmony_ci					      sama5d4_systemck[i].id);
2388c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2398c2ecf20Sopenharmony_ci			goto err_free;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		sama5d4_pmc->shws[sama5d4_systemck[i].id] = hw;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d4_periphck); i++) {
2458c2ecf20Sopenharmony_ci		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
2468c2ecf20Sopenharmony_ci							 &sama5d4_pcr_layout,
2478c2ecf20Sopenharmony_ci							 sama5d4_periphck[i].n,
2488c2ecf20Sopenharmony_ci							 "masterck",
2498c2ecf20Sopenharmony_ci							 sama5d4_periphck[i].id,
2508c2ecf20Sopenharmony_ci							 &range, INT_MIN);
2518c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2528c2ecf20Sopenharmony_ci			goto err_free;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		sama5d4_pmc->phws[sama5d4_periphck[i].id] = hw;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d4_periph32ck); i++) {
2588c2ecf20Sopenharmony_ci		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
2598c2ecf20Sopenharmony_ci							 &sama5d4_pcr_layout,
2608c2ecf20Sopenharmony_ci							 sama5d4_periph32ck[i].n,
2618c2ecf20Sopenharmony_ci							 "h32mxck",
2628c2ecf20Sopenharmony_ci							 sama5d4_periph32ck[i].id,
2638c2ecf20Sopenharmony_ci							 &range, INT_MIN);
2648c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2658c2ecf20Sopenharmony_ci			goto err_free;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci		sama5d4_pmc->phws[sama5d4_periph32ck[i].id] = hw;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d4_pmc);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cierr_free:
2758c2ecf20Sopenharmony_ci	kfree(sama5d4_pmc);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(sama5d4_pmc, "atmel,sama5d4-pmc", sama5d4_pmc_setup);
278