xref: /openbmc/linux/drivers/clk/at91/sama5d3.c (revision 077782e3)
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 
107a110b91SClaudiu Beznea static DEFINE_SPINLOCK(mck_lock);
117a110b91SClaudiu Beznea 
120969b242SAlexandre Belloni static const struct clk_master_characteristics mck_characteristics = {
130969b242SAlexandre Belloni 	.output = { .min = 0, .max = 166000000 },
140969b242SAlexandre Belloni 	.divisors = { 1, 2, 4, 3 },
150969b242SAlexandre Belloni };
160969b242SAlexandre Belloni 
170969b242SAlexandre Belloni static u8 plla_out[] = { 0 };
180969b242SAlexandre Belloni 
190969b242SAlexandre Belloni static u16 plla_icpll[] = { 0 };
200969b242SAlexandre Belloni 
210969b242SAlexandre Belloni static const struct clk_range plla_outputs[] = {
220969b242SAlexandre Belloni 	{ .min = 400000000, .max = 1000000000 },
230969b242SAlexandre Belloni };
240969b242SAlexandre Belloni 
250969b242SAlexandre Belloni static const struct clk_pll_characteristics plla_characteristics = {
260969b242SAlexandre Belloni 	.input = { .min = 8000000, .max = 50000000 },
270969b242SAlexandre Belloni 	.num_output = ARRAY_SIZE(plla_outputs),
280969b242SAlexandre Belloni 	.output = plla_outputs,
290969b242SAlexandre Belloni 	.icpll = plla_icpll,
300969b242SAlexandre Belloni 	.out = plla_out,
310969b242SAlexandre Belloni };
320969b242SAlexandre Belloni 
330969b242SAlexandre Belloni static const struct clk_pcr_layout sama5d3_pcr_layout = {
340969b242SAlexandre Belloni 	.offset = 0x10c,
350969b242SAlexandre Belloni 	.cmd = BIT(12),
360969b242SAlexandre Belloni 	.pid_mask = GENMASK(6, 0),
370969b242SAlexandre Belloni 	.div_mask = GENMASK(17, 16),
380969b242SAlexandre Belloni };
390969b242SAlexandre Belloni 
400969b242SAlexandre Belloni static const struct {
410969b242SAlexandre Belloni 	char *n;
420969b242SAlexandre Belloni 	char *p;
4368b3b6f1SClaudiu Beznea 	unsigned long flags;
440969b242SAlexandre Belloni 	u8 id;
450969b242SAlexandre Belloni } sama5d3_systemck[] = {
4668b3b6f1SClaudiu Beznea 	/*
4768b3b6f1SClaudiu Beznea 	 * ddrck feeds DDR controller and is enabled by bootloader thus we need
4868b3b6f1SClaudiu Beznea 	 * to keep it enabled in case there is no Linux consumer for it.
4968b3b6f1SClaudiu Beznea 	 */
5068b3b6f1SClaudiu Beznea 	{ .n = "ddrck", .p = "masterck_div", .id = 2, .flags = CLK_IS_CRITICAL },
517a110b91SClaudiu Beznea 	{ .n = "lcdck", .p = "masterck_div", .id = 3 },
520969b242SAlexandre Belloni 	{ .n = "smdck", .p = "smdclk",       .id = 4 },
530969b242SAlexandre Belloni 	{ .n = "uhpck", .p = "usbck",        .id = 6 },
540969b242SAlexandre Belloni 	{ .n = "udpck", .p = "usbck",        .id = 7 },
550969b242SAlexandre Belloni 	{ .n = "pck0",  .p = "prog0",        .id = 8 },
560969b242SAlexandre Belloni 	{ .n = "pck1",  .p = "prog1",        .id = 9 },
570969b242SAlexandre Belloni 	{ .n = "pck2",  .p = "prog2",        .id = 10 },
580969b242SAlexandre Belloni };
590969b242SAlexandre Belloni 
600969b242SAlexandre Belloni static const struct {
610969b242SAlexandre Belloni 	char *n;
620969b242SAlexandre Belloni 	u8 id;
630969b242SAlexandre Belloni 	struct clk_range r;
6468b3b6f1SClaudiu Beznea 	unsigned long flags;
650969b242SAlexandre Belloni } sama5d3_periphck[] = {
660969b242SAlexandre Belloni 	{ .n = "dbgu_clk", .id = 2, },
670969b242SAlexandre Belloni 	{ .n = "hsmc_clk", .id = 5, },
680969b242SAlexandre Belloni 	{ .n = "pioA_clk", .id = 6, },
690969b242SAlexandre Belloni 	{ .n = "pioB_clk", .id = 7, },
700969b242SAlexandre Belloni 	{ .n = "pioC_clk", .id = 8, },
710969b242SAlexandre Belloni 	{ .n = "pioD_clk", .id = 9, },
720969b242SAlexandre Belloni 	{ .n = "pioE_clk", .id = 10, },
730969b242SAlexandre Belloni 	{ .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, },
740969b242SAlexandre Belloni 	{ .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, },
750969b242SAlexandre Belloni 	{ .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, },
760969b242SAlexandre Belloni 	{ .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, },
770969b242SAlexandre Belloni 	{ .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, },
780969b242SAlexandre Belloni 	{ .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, },
790969b242SAlexandre Belloni 	{ .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, },
800969b242SAlexandre Belloni 	{ .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, },
810969b242SAlexandre Belloni 	{ .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, },
820969b242SAlexandre Belloni 	{ .n = "mci0_clk", .id = 21, },
830969b242SAlexandre Belloni 	{ .n = "mci1_clk", .id = 22, },
840969b242SAlexandre Belloni 	{ .n = "mci2_clk", .id = 23, },
850969b242SAlexandre Belloni 	{ .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, },
860969b242SAlexandre Belloni 	{ .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, },
870969b242SAlexandre Belloni 	{ .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, },
880969b242SAlexandre Belloni 	{ .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, },
890969b242SAlexandre Belloni 	{ .n = "pwm_clk", .id = 28, },
900969b242SAlexandre Belloni 	{ .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, },
910969b242SAlexandre Belloni 	{ .n = "dma0_clk", .id = 30, },
920969b242SAlexandre Belloni 	{ .n = "dma1_clk", .id = 31, },
930969b242SAlexandre Belloni 	{ .n = "uhphs_clk", .id = 32, },
940969b242SAlexandre Belloni 	{ .n = "udphs_clk", .id = 33, },
950969b242SAlexandre Belloni 	{ .n = "macb0_clk", .id = 34, },
960969b242SAlexandre Belloni 	{ .n = "macb1_clk", .id = 35, },
970969b242SAlexandre Belloni 	{ .n = "lcdc_clk", .id = 36, },
980969b242SAlexandre Belloni 	{ .n = "isi_clk", .id = 37, },
990969b242SAlexandre Belloni 	{ .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, },
1000969b242SAlexandre Belloni 	{ .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, },
1010969b242SAlexandre Belloni 	{ .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, },
1020969b242SAlexandre Belloni 	{ .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, },
1030969b242SAlexandre Belloni 	{ .n = "sha_clk", .id = 42, },
1040969b242SAlexandre Belloni 	{ .n = "aes_clk", .id = 43, },
1050969b242SAlexandre Belloni 	{ .n = "tdes_clk", .id = 44, },
1060969b242SAlexandre Belloni 	{ .n = "trng_clk", .id = 45, },
1070969b242SAlexandre Belloni 	{ .n = "fuse_clk", .id = 48, },
10868b3b6f1SClaudiu Beznea 	/*
10968b3b6f1SClaudiu Beznea 	 * mpddr_clk feeds DDR controller and is enabled by bootloader thus we
11068b3b6f1SClaudiu Beznea 	 * need to keep it enabled in case there is no Linux consumer for it.
11168b3b6f1SClaudiu Beznea 	 */
11268b3b6f1SClaudiu Beznea 	{ .n = "mpddr_clk", .id = 49, .flags = CLK_IS_CRITICAL },
1130969b242SAlexandre Belloni };
1140969b242SAlexandre Belloni 
sama5d3_pmc_setup(struct device_node * np)1150969b242SAlexandre Belloni static void __init sama5d3_pmc_setup(struct device_node *np)
1160969b242SAlexandre Belloni {
1170969b242SAlexandre Belloni 	const char *slck_name, *mainxtal_name;
1180969b242SAlexandre Belloni 	struct pmc_data *sama5d3_pmc;
1190969b242SAlexandre Belloni 	const char *parent_names[5];
1200969b242SAlexandre Belloni 	struct regmap *regmap;
1210969b242SAlexandre Belloni 	struct clk_hw *hw;
1220969b242SAlexandre Belloni 	int i;
1230969b242SAlexandre Belloni 	bool bypass;
1240969b242SAlexandre Belloni 
1250969b242SAlexandre Belloni 	i = of_property_match_string(np, "clock-names", "slow_clk");
1260969b242SAlexandre Belloni 	if (i < 0)
1270969b242SAlexandre Belloni 		return;
1280969b242SAlexandre Belloni 
1290969b242SAlexandre Belloni 	slck_name = of_clk_get_parent_name(np, i);
1300969b242SAlexandre Belloni 
1310969b242SAlexandre Belloni 	i = of_property_match_string(np, "clock-names", "main_xtal");
1320969b242SAlexandre Belloni 	if (i < 0)
1330969b242SAlexandre Belloni 		return;
1340969b242SAlexandre Belloni 	mainxtal_name = of_clk_get_parent_name(np, i);
1350969b242SAlexandre Belloni 
136153bc1c6SAhmad Fatoum 	regmap = device_node_to_regmap(np);
1370969b242SAlexandre Belloni 	if (IS_ERR(regmap))
1380969b242SAlexandre Belloni 		return;
1390969b242SAlexandre Belloni 
14003a1ee1dSMichał Mirosław 	sama5d3_pmc = pmc_data_allocate(PMC_PLLACK + 1,
1410969b242SAlexandre Belloni 					nck(sama5d3_systemck),
14299767cd4SMichał Mirosław 					nck(sama5d3_periphck), 0, 3);
1430969b242SAlexandre Belloni 	if (!sama5d3_pmc)
1440969b242SAlexandre Belloni 		return;
1450969b242SAlexandre Belloni 
1460969b242SAlexandre Belloni 	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
1470969b242SAlexandre Belloni 					   50000000);
1480969b242SAlexandre Belloni 	if (IS_ERR(hw))
1490969b242SAlexandre Belloni 		goto err_free;
1500969b242SAlexandre Belloni 
1510969b242SAlexandre Belloni 	bypass = of_property_read_bool(np, "atmel,osc-bypass");
1520969b242SAlexandre Belloni 
153b5105e37SClaudiu Beznea 	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
1540969b242SAlexandre Belloni 					bypass);
1550969b242SAlexandre Belloni 	if (IS_ERR(hw))
1560969b242SAlexandre Belloni 		goto err_free;
1570969b242SAlexandre Belloni 
1580969b242SAlexandre Belloni 	parent_names[0] = "main_rc_osc";
1590969b242SAlexandre Belloni 	parent_names[1] = "main_osc";
160b5105e37SClaudiu Beznea 	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, NULL, 2);
1610969b242SAlexandre Belloni 	if (IS_ERR(hw))
1620969b242SAlexandre Belloni 		goto err_free;
1630969b242SAlexandre Belloni 
1640969b242SAlexandre Belloni 	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1650969b242SAlexandre Belloni 				   &sama5d3_pll_layout, &plla_characteristics);
1660969b242SAlexandre Belloni 	if (IS_ERR(hw))
1670969b242SAlexandre Belloni 		goto err_free;
1680969b242SAlexandre Belloni 
1690969b242SAlexandre Belloni 	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
1700969b242SAlexandre Belloni 	if (IS_ERR(hw))
1710969b242SAlexandre Belloni 		goto err_free;
1720969b242SAlexandre Belloni 
17303a1ee1dSMichał Mirosław 	sama5d3_pmc->chws[PMC_PLLACK] = hw;
17403a1ee1dSMichał Mirosław 
175*077782e3SClaudiu Beznea 	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck", NULL);
1760969b242SAlexandre Belloni 	if (IS_ERR(hw))
1770969b242SAlexandre Belloni 		goto err_free;
1780969b242SAlexandre Belloni 
1790969b242SAlexandre Belloni 	sama5d3_pmc->chws[PMC_UTMI] = hw;
1800969b242SAlexandre Belloni 
1810969b242SAlexandre Belloni 	parent_names[0] = slck_name;
1820969b242SAlexandre Belloni 	parent_names[1] = "mainck";
1830969b242SAlexandre Belloni 	parent_names[2] = "plladivck";
1840969b242SAlexandre Belloni 	parent_names[3] = "utmick";
1857a110b91SClaudiu Beznea 	hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
186171e502cSClaudiu Beznea 					   parent_names, NULL,
1870969b242SAlexandre Belloni 					   &at91sam9x5_master_layout,
1888e842f02SClaudiu Beznea 					   &mck_characteristics, &mck_lock);
1897a110b91SClaudiu Beznea 	if (IS_ERR(hw))
1907a110b91SClaudiu Beznea 		goto err_free;
1917a110b91SClaudiu Beznea 
1927a110b91SClaudiu Beznea 	hw = at91_clk_register_master_div(regmap, "masterck_div",
193171e502cSClaudiu Beznea 					  "masterck_pres", NULL,
1947a110b91SClaudiu Beznea 					  &at91sam9x5_master_layout,
1957a110b91SClaudiu Beznea 					  &mck_characteristics, &mck_lock,
1967029db09SClaudiu Beznea 					  CLK_SET_RATE_GATE, 0);
1970969b242SAlexandre Belloni 	if (IS_ERR(hw))
1980969b242SAlexandre Belloni 		goto err_free;
1990969b242SAlexandre Belloni 
2000969b242SAlexandre Belloni 	sama5d3_pmc->chws[PMC_MCK] = hw;
2010969b242SAlexandre Belloni 
2020969b242SAlexandre Belloni 	parent_names[0] = "plladivck";
2030969b242SAlexandre Belloni 	parent_names[1] = "utmick";
2040969b242SAlexandre Belloni 	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
2050969b242SAlexandre Belloni 	if (IS_ERR(hw))
2060969b242SAlexandre Belloni 		goto err_free;
2070969b242SAlexandre Belloni 
2080969b242SAlexandre Belloni 	hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
2090969b242SAlexandre Belloni 	if (IS_ERR(hw))
2100969b242SAlexandre Belloni 		goto err_free;
2110969b242SAlexandre Belloni 
2120969b242SAlexandre Belloni 	parent_names[0] = slck_name;
2130969b242SAlexandre Belloni 	parent_names[1] = "mainck";
2140969b242SAlexandre Belloni 	parent_names[2] = "plladivck";
2150969b242SAlexandre Belloni 	parent_names[3] = "utmick";
2167a110b91SClaudiu Beznea 	parent_names[4] = "masterck_div";
2170969b242SAlexandre Belloni 	for (i = 0; i < 3; i++) {
2180969b242SAlexandre Belloni 		char name[6];
2190969b242SAlexandre Belloni 
2200969b242SAlexandre Belloni 		snprintf(name, sizeof(name), "prog%d", i);
2210969b242SAlexandre Belloni 
2220969b242SAlexandre Belloni 		hw = at91_clk_register_programmable(regmap, name,
2231a2669dfSClaudiu Beznea 						    parent_names, NULL, 5, i,
224c57aaaa2SClaudiu Beznea 						    &at91sam9x5_programmable_layout,
225c57aaaa2SClaudiu Beznea 						    NULL);
2260969b242SAlexandre Belloni 		if (IS_ERR(hw))
2270969b242SAlexandre Belloni 			goto err_free;
22899767cd4SMichał Mirosław 
22999767cd4SMichał Mirosław 		sama5d3_pmc->pchws[i] = hw;
2300969b242SAlexandre Belloni 	}
2310969b242SAlexandre Belloni 
2320969b242SAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) {
2330969b242SAlexandre Belloni 		hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n,
2341a537f62SClaudiu Beznea 					      sama5d3_systemck[i].p, NULL,
23568b3b6f1SClaudiu Beznea 					      sama5d3_systemck[i].id,
23668b3b6f1SClaudiu Beznea 					      sama5d3_systemck[i].flags);
2370969b242SAlexandre Belloni 		if (IS_ERR(hw))
2380969b242SAlexandre Belloni 			goto err_free;
2390969b242SAlexandre Belloni 
2400969b242SAlexandre Belloni 		sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw;
2410969b242SAlexandre Belloni 	}
2420969b242SAlexandre Belloni 
2430969b242SAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) {
2440969b242SAlexandre Belloni 		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
2450969b242SAlexandre Belloni 							 &sama5d3_pcr_layout,
2460969b242SAlexandre Belloni 							 sama5d3_periphck[i].n,
247c2f2ca0bSClaudiu Beznea 							 "masterck_div", NULL,
2480969b242SAlexandre Belloni 							 sama5d3_periphck[i].id,
249b4c115c7SClaudiu Beznea 							 &sama5d3_periphck[i].r,
25068b3b6f1SClaudiu Beznea 							 INT_MIN,
25168b3b6f1SClaudiu Beznea 							 sama5d3_periphck[i].flags);
2520969b242SAlexandre Belloni 		if (IS_ERR(hw))
2530969b242SAlexandre Belloni 			goto err_free;
2540969b242SAlexandre Belloni 
2550969b242SAlexandre Belloni 		sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw;
2560969b242SAlexandre Belloni 	}
2570969b242SAlexandre Belloni 
2580969b242SAlexandre Belloni 	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc);
2590969b242SAlexandre Belloni 
2600969b242SAlexandre Belloni 	return;
2610969b242SAlexandre Belloni 
2620969b242SAlexandre Belloni err_free:
2637425f246SMichał Mirosław 	kfree(sama5d3_pmc);
2640969b242SAlexandre Belloni }
2650969b242SAlexandre Belloni /*
2660969b242SAlexandre Belloni  * The TCB is used as the clocksource so its clock is needed early. This means
2670969b242SAlexandre Belloni  * this can't be a platform driver.
2680969b242SAlexandre Belloni  */
269428d97e1STudor Ambarus CLK_OF_DECLARE(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup);
270