xref: /openbmc/linux/drivers/clk/at91/sama5d3.c (revision c57aaaa2)
10969b242SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
20969b242SAlexandre Belloni #include <linux/clk-provider.h>
30969b242SAlexandre Belloni #include <linux/mfd/syscon.h>
40969b242SAlexandre Belloni #include <linux/slab.h>
50969b242SAlexandre Belloni 
60969b242SAlexandre Belloni #include <dt-bindings/clock/at91.h>
70969b242SAlexandre Belloni 
80969b242SAlexandre Belloni #include "pmc.h"
90969b242SAlexandre Belloni 
100969b242SAlexandre Belloni static const struct clk_master_characteristics mck_characteristics = {
110969b242SAlexandre Belloni 	.output = { .min = 0, .max = 166000000 },
120969b242SAlexandre Belloni 	.divisors = { 1, 2, 4, 3 },
130969b242SAlexandre Belloni };
140969b242SAlexandre Belloni 
150969b242SAlexandre Belloni static u8 plla_out[] = { 0 };
160969b242SAlexandre Belloni 
170969b242SAlexandre Belloni static u16 plla_icpll[] = { 0 };
180969b242SAlexandre Belloni 
190969b242SAlexandre Belloni static const struct clk_range plla_outputs[] = {
200969b242SAlexandre Belloni 	{ .min = 400000000, .max = 1000000000 },
210969b242SAlexandre Belloni };
220969b242SAlexandre Belloni 
230969b242SAlexandre Belloni static const struct clk_pll_characteristics plla_characteristics = {
240969b242SAlexandre Belloni 	.input = { .min = 8000000, .max = 50000000 },
250969b242SAlexandre Belloni 	.num_output = ARRAY_SIZE(plla_outputs),
260969b242SAlexandre Belloni 	.output = plla_outputs,
270969b242SAlexandre Belloni 	.icpll = plla_icpll,
280969b242SAlexandre Belloni 	.out = plla_out,
290969b242SAlexandre Belloni };
300969b242SAlexandre Belloni 
310969b242SAlexandre Belloni static const struct clk_pcr_layout sama5d3_pcr_layout = {
320969b242SAlexandre Belloni 	.offset = 0x10c,
330969b242SAlexandre Belloni 	.cmd = BIT(12),
340969b242SAlexandre Belloni 	.pid_mask = GENMASK(6, 0),
350969b242SAlexandre Belloni 	.div_mask = GENMASK(17, 16),
360969b242SAlexandre Belloni };
370969b242SAlexandre Belloni 
380969b242SAlexandre Belloni static const struct {
390969b242SAlexandre Belloni 	char *n;
400969b242SAlexandre Belloni 	char *p;
410969b242SAlexandre Belloni 	u8 id;
420969b242SAlexandre Belloni } sama5d3_systemck[] = {
430969b242SAlexandre Belloni 	{ .n = "ddrck", .p = "masterck", .id = 2 },
440969b242SAlexandre Belloni 	{ .n = "lcdck", .p = "masterck", .id = 3 },
450969b242SAlexandre Belloni 	{ .n = "smdck", .p = "smdclk",   .id = 4 },
460969b242SAlexandre Belloni 	{ .n = "uhpck", .p = "usbck",    .id = 6 },
470969b242SAlexandre Belloni 	{ .n = "udpck", .p = "usbck",    .id = 7 },
480969b242SAlexandre Belloni 	{ .n = "pck0",  .p = "prog0",    .id = 8 },
490969b242SAlexandre Belloni 	{ .n = "pck1",  .p = "prog1",    .id = 9 },
500969b242SAlexandre Belloni 	{ .n = "pck2",  .p = "prog2",    .id = 10 },
510969b242SAlexandre Belloni };
520969b242SAlexandre Belloni 
530969b242SAlexandre Belloni static const struct {
540969b242SAlexandre Belloni 	char *n;
550969b242SAlexandre Belloni 	u8 id;
560969b242SAlexandre Belloni 	struct clk_range r;
570969b242SAlexandre Belloni } sama5d3_periphck[] = {
580969b242SAlexandre Belloni 	{ .n = "dbgu_clk", .id = 2, },
590969b242SAlexandre Belloni 	{ .n = "hsmc_clk", .id = 5, },
600969b242SAlexandre Belloni 	{ .n = "pioA_clk", .id = 6, },
610969b242SAlexandre Belloni 	{ .n = "pioB_clk", .id = 7, },
620969b242SAlexandre Belloni 	{ .n = "pioC_clk", .id = 8, },
630969b242SAlexandre Belloni 	{ .n = "pioD_clk", .id = 9, },
640969b242SAlexandre Belloni 	{ .n = "pioE_clk", .id = 10, },
650969b242SAlexandre Belloni 	{ .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, },
660969b242SAlexandre Belloni 	{ .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, },
670969b242SAlexandre Belloni 	{ .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, },
680969b242SAlexandre Belloni 	{ .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, },
690969b242SAlexandre Belloni 	{ .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, },
700969b242SAlexandre Belloni 	{ .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, },
710969b242SAlexandre Belloni 	{ .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, },
720969b242SAlexandre Belloni 	{ .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, },
730969b242SAlexandre Belloni 	{ .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, },
740969b242SAlexandre Belloni 	{ .n = "mci0_clk", .id = 21, },
750969b242SAlexandre Belloni 	{ .n = "mci1_clk", .id = 22, },
760969b242SAlexandre Belloni 	{ .n = "mci2_clk", .id = 23, },
770969b242SAlexandre Belloni 	{ .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, },
780969b242SAlexandre Belloni 	{ .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, },
790969b242SAlexandre Belloni 	{ .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, },
800969b242SAlexandre Belloni 	{ .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, },
810969b242SAlexandre Belloni 	{ .n = "pwm_clk", .id = 28, },
820969b242SAlexandre Belloni 	{ .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, },
830969b242SAlexandre Belloni 	{ .n = "dma0_clk", .id = 30, },
840969b242SAlexandre Belloni 	{ .n = "dma1_clk", .id = 31, },
850969b242SAlexandre Belloni 	{ .n = "uhphs_clk", .id = 32, },
860969b242SAlexandre Belloni 	{ .n = "udphs_clk", .id = 33, },
870969b242SAlexandre Belloni 	{ .n = "macb0_clk", .id = 34, },
880969b242SAlexandre Belloni 	{ .n = "macb1_clk", .id = 35, },
890969b242SAlexandre Belloni 	{ .n = "lcdc_clk", .id = 36, },
900969b242SAlexandre Belloni 	{ .n = "isi_clk", .id = 37, },
910969b242SAlexandre Belloni 	{ .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, },
920969b242SAlexandre Belloni 	{ .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, },
930969b242SAlexandre Belloni 	{ .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, },
940969b242SAlexandre Belloni 	{ .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, },
950969b242SAlexandre Belloni 	{ .n = "sha_clk", .id = 42, },
960969b242SAlexandre Belloni 	{ .n = "aes_clk", .id = 43, },
970969b242SAlexandre Belloni 	{ .n = "tdes_clk", .id = 44, },
980969b242SAlexandre Belloni 	{ .n = "trng_clk", .id = 45, },
990969b242SAlexandre Belloni 	{ .n = "fuse_clk", .id = 48, },
1000969b242SAlexandre Belloni 	{ .n = "mpddr_clk", .id = 49, },
1010969b242SAlexandre Belloni };
1020969b242SAlexandre Belloni 
1030969b242SAlexandre Belloni static void __init sama5d3_pmc_setup(struct device_node *np)
1040969b242SAlexandre Belloni {
1050969b242SAlexandre Belloni 	const char *slck_name, *mainxtal_name;
1060969b242SAlexandre Belloni 	struct pmc_data *sama5d3_pmc;
1070969b242SAlexandre Belloni 	const char *parent_names[5];
1080969b242SAlexandre Belloni 	struct regmap *regmap;
1090969b242SAlexandre Belloni 	struct clk_hw *hw;
1100969b242SAlexandre Belloni 	int i;
1110969b242SAlexandre Belloni 	bool bypass;
1120969b242SAlexandre Belloni 
1130969b242SAlexandre Belloni 	i = of_property_match_string(np, "clock-names", "slow_clk");
1140969b242SAlexandre Belloni 	if (i < 0)
1150969b242SAlexandre Belloni 		return;
1160969b242SAlexandre Belloni 
1170969b242SAlexandre Belloni 	slck_name = of_clk_get_parent_name(np, i);
1180969b242SAlexandre Belloni 
1190969b242SAlexandre Belloni 	i = of_property_match_string(np, "clock-names", "main_xtal");
1200969b242SAlexandre Belloni 	if (i < 0)
1210969b242SAlexandre Belloni 		return;
1220969b242SAlexandre Belloni 	mainxtal_name = of_clk_get_parent_name(np, i);
1230969b242SAlexandre Belloni 
124153bc1c6SAhmad Fatoum 	regmap = device_node_to_regmap(np);
1250969b242SAlexandre Belloni 	if (IS_ERR(regmap))
1260969b242SAlexandre Belloni 		return;
1270969b242SAlexandre Belloni 
12803a1ee1dSMichał Mirosław 	sama5d3_pmc = pmc_data_allocate(PMC_PLLACK + 1,
1290969b242SAlexandre Belloni 					nck(sama5d3_systemck),
13099767cd4SMichał Mirosław 					nck(sama5d3_periphck), 0, 3);
1310969b242SAlexandre Belloni 	if (!sama5d3_pmc)
1320969b242SAlexandre Belloni 		return;
1330969b242SAlexandre Belloni 
1340969b242SAlexandre Belloni 	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
1350969b242SAlexandre Belloni 					   50000000);
1360969b242SAlexandre Belloni 	if (IS_ERR(hw))
1370969b242SAlexandre Belloni 		goto err_free;
1380969b242SAlexandre Belloni 
1390969b242SAlexandre Belloni 	bypass = of_property_read_bool(np, "atmel,osc-bypass");
1400969b242SAlexandre Belloni 
1410969b242SAlexandre Belloni 	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
1420969b242SAlexandre Belloni 					bypass);
1430969b242SAlexandre Belloni 	if (IS_ERR(hw))
1440969b242SAlexandre Belloni 		goto err_free;
1450969b242SAlexandre Belloni 
1460969b242SAlexandre Belloni 	parent_names[0] = "main_rc_osc";
1470969b242SAlexandre Belloni 	parent_names[1] = "main_osc";
1480969b242SAlexandre Belloni 	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
1490969b242SAlexandre Belloni 	if (IS_ERR(hw))
1500969b242SAlexandre Belloni 		goto err_free;
1510969b242SAlexandre Belloni 
1520969b242SAlexandre Belloni 	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1530969b242SAlexandre Belloni 				   &sama5d3_pll_layout, &plla_characteristics);
1540969b242SAlexandre Belloni 	if (IS_ERR(hw))
1550969b242SAlexandre Belloni 		goto err_free;
1560969b242SAlexandre Belloni 
1570969b242SAlexandre Belloni 	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
1580969b242SAlexandre Belloni 	if (IS_ERR(hw))
1590969b242SAlexandre Belloni 		goto err_free;
1600969b242SAlexandre Belloni 
16103a1ee1dSMichał Mirosław 	sama5d3_pmc->chws[PMC_PLLACK] = hw;
16203a1ee1dSMichał Mirosław 
1630969b242SAlexandre Belloni 	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck");
1640969b242SAlexandre Belloni 	if (IS_ERR(hw))
1650969b242SAlexandre Belloni 		goto err_free;
1660969b242SAlexandre Belloni 
1670969b242SAlexandre Belloni 	sama5d3_pmc->chws[PMC_UTMI] = hw;
1680969b242SAlexandre Belloni 
1690969b242SAlexandre Belloni 	parent_names[0] = slck_name;
1700969b242SAlexandre Belloni 	parent_names[1] = "mainck";
1710969b242SAlexandre Belloni 	parent_names[2] = "plladivck";
1720969b242SAlexandre Belloni 	parent_names[3] = "utmick";
1730969b242SAlexandre Belloni 	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
1740969b242SAlexandre Belloni 				      &at91sam9x5_master_layout,
1750969b242SAlexandre Belloni 				      &mck_characteristics);
1760969b242SAlexandre Belloni 	if (IS_ERR(hw))
1770969b242SAlexandre Belloni 		goto err_free;
1780969b242SAlexandre Belloni 
1790969b242SAlexandre Belloni 	sama5d3_pmc->chws[PMC_MCK] = hw;
1800969b242SAlexandre Belloni 
1810969b242SAlexandre Belloni 	parent_names[0] = "plladivck";
1820969b242SAlexandre Belloni 	parent_names[1] = "utmick";
1830969b242SAlexandre Belloni 	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
1840969b242SAlexandre Belloni 	if (IS_ERR(hw))
1850969b242SAlexandre Belloni 		goto err_free;
1860969b242SAlexandre Belloni 
1870969b242SAlexandre Belloni 	hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
1880969b242SAlexandre Belloni 	if (IS_ERR(hw))
1890969b242SAlexandre Belloni 		goto err_free;
1900969b242SAlexandre Belloni 
1910969b242SAlexandre Belloni 	parent_names[0] = slck_name;
1920969b242SAlexandre Belloni 	parent_names[1] = "mainck";
1930969b242SAlexandre Belloni 	parent_names[2] = "plladivck";
1940969b242SAlexandre Belloni 	parent_names[3] = "utmick";
1950969b242SAlexandre Belloni 	parent_names[4] = "masterck";
1960969b242SAlexandre Belloni 	for (i = 0; i < 3; i++) {
1970969b242SAlexandre Belloni 		char name[6];
1980969b242SAlexandre Belloni 
1990969b242SAlexandre Belloni 		snprintf(name, sizeof(name), "prog%d", i);
2000969b242SAlexandre Belloni 
2010969b242SAlexandre Belloni 		hw = at91_clk_register_programmable(regmap, name,
2020969b242SAlexandre Belloni 						    parent_names, 5, i,
203c57aaaa2SClaudiu Beznea 						    &at91sam9x5_programmable_layout,
204c57aaaa2SClaudiu Beznea 						    NULL);
2050969b242SAlexandre Belloni 		if (IS_ERR(hw))
2060969b242SAlexandre Belloni 			goto err_free;
20799767cd4SMichał Mirosław 
20899767cd4SMichał Mirosław 		sama5d3_pmc->pchws[i] = hw;
2090969b242SAlexandre Belloni 	}
2100969b242SAlexandre Belloni 
2110969b242SAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) {
2120969b242SAlexandre Belloni 		hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n,
2130969b242SAlexandre Belloni 					      sama5d3_systemck[i].p,
2140969b242SAlexandre Belloni 					      sama5d3_systemck[i].id);
2150969b242SAlexandre Belloni 		if (IS_ERR(hw))
2160969b242SAlexandre Belloni 			goto err_free;
2170969b242SAlexandre Belloni 
2180969b242SAlexandre Belloni 		sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw;
2190969b242SAlexandre Belloni 	}
2200969b242SAlexandre Belloni 
2210969b242SAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) {
2220969b242SAlexandre Belloni 		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
2230969b242SAlexandre Belloni 							 &sama5d3_pcr_layout,
2240969b242SAlexandre Belloni 							 sama5d3_periphck[i].n,
2250969b242SAlexandre Belloni 							 "masterck",
2260969b242SAlexandre Belloni 							 sama5d3_periphck[i].id,
227b4c115c7SClaudiu Beznea 							 &sama5d3_periphck[i].r,
228b4c115c7SClaudiu Beznea 							 INT_MIN);
2290969b242SAlexandre Belloni 		if (IS_ERR(hw))
2300969b242SAlexandre Belloni 			goto err_free;
2310969b242SAlexandre Belloni 
2320969b242SAlexandre Belloni 		sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw;
2330969b242SAlexandre Belloni 	}
2340969b242SAlexandre Belloni 
2350969b242SAlexandre Belloni 	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc);
2360969b242SAlexandre Belloni 
2370969b242SAlexandre Belloni 	return;
2380969b242SAlexandre Belloni 
2390969b242SAlexandre Belloni err_free:
2407425f246SMichał Mirosław 	kfree(sama5d3_pmc);
2410969b242SAlexandre Belloni }
2420969b242SAlexandre Belloni /*
2430969b242SAlexandre Belloni  * The TCB is used as the clocksource so its clock is needed early. This means
2440969b242SAlexandre Belloni  * this can't be a platform driver.
2450969b242SAlexandre Belloni  */
2460969b242SAlexandre Belloni CLK_OF_DECLARE_DRIVER(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup);
247