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 10*7a110b91SClaudiu Beznea static DEFINE_SPINLOCK(mck_lock); 11*7a110b91SClaudiu 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; 430969b242SAlexandre Belloni u8 id; 440969b242SAlexandre Belloni } sama5d3_systemck[] = { 45*7a110b91SClaudiu Beznea { .n = "ddrck", .p = "masterck_div", .id = 2 }, 46*7a110b91SClaudiu Beznea { .n = "lcdck", .p = "masterck_div", .id = 3 }, 470969b242SAlexandre Belloni { .n = "smdck", .p = "smdclk", .id = 4 }, 480969b242SAlexandre Belloni { .n = "uhpck", .p = "usbck", .id = 6 }, 490969b242SAlexandre Belloni { .n = "udpck", .p = "usbck", .id = 7 }, 500969b242SAlexandre Belloni { .n = "pck0", .p = "prog0", .id = 8 }, 510969b242SAlexandre Belloni { .n = "pck1", .p = "prog1", .id = 9 }, 520969b242SAlexandre Belloni { .n = "pck2", .p = "prog2", .id = 10 }, 530969b242SAlexandre Belloni }; 540969b242SAlexandre Belloni 550969b242SAlexandre Belloni static const struct { 560969b242SAlexandre Belloni char *n; 570969b242SAlexandre Belloni u8 id; 580969b242SAlexandre Belloni struct clk_range r; 590969b242SAlexandre Belloni } sama5d3_periphck[] = { 600969b242SAlexandre Belloni { .n = "dbgu_clk", .id = 2, }, 610969b242SAlexandre Belloni { .n = "hsmc_clk", .id = 5, }, 620969b242SAlexandre Belloni { .n = "pioA_clk", .id = 6, }, 630969b242SAlexandre Belloni { .n = "pioB_clk", .id = 7, }, 640969b242SAlexandre Belloni { .n = "pioC_clk", .id = 8, }, 650969b242SAlexandre Belloni { .n = "pioD_clk", .id = 9, }, 660969b242SAlexandre Belloni { .n = "pioE_clk", .id = 10, }, 670969b242SAlexandre Belloni { .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, }, 680969b242SAlexandre Belloni { .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, }, 690969b242SAlexandre Belloni { .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, }, 700969b242SAlexandre Belloni { .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, }, 710969b242SAlexandre Belloni { .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, }, 720969b242SAlexandre Belloni { .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, }, 730969b242SAlexandre Belloni { .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, }, 740969b242SAlexandre Belloni { .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, }, 750969b242SAlexandre Belloni { .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, }, 760969b242SAlexandre Belloni { .n = "mci0_clk", .id = 21, }, 770969b242SAlexandre Belloni { .n = "mci1_clk", .id = 22, }, 780969b242SAlexandre Belloni { .n = "mci2_clk", .id = 23, }, 790969b242SAlexandre Belloni { .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, }, 800969b242SAlexandre Belloni { .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, }, 810969b242SAlexandre Belloni { .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, }, 820969b242SAlexandre Belloni { .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, }, 830969b242SAlexandre Belloni { .n = "pwm_clk", .id = 28, }, 840969b242SAlexandre Belloni { .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, }, 850969b242SAlexandre Belloni { .n = "dma0_clk", .id = 30, }, 860969b242SAlexandre Belloni { .n = "dma1_clk", .id = 31, }, 870969b242SAlexandre Belloni { .n = "uhphs_clk", .id = 32, }, 880969b242SAlexandre Belloni { .n = "udphs_clk", .id = 33, }, 890969b242SAlexandre Belloni { .n = "macb0_clk", .id = 34, }, 900969b242SAlexandre Belloni { .n = "macb1_clk", .id = 35, }, 910969b242SAlexandre Belloni { .n = "lcdc_clk", .id = 36, }, 920969b242SAlexandre Belloni { .n = "isi_clk", .id = 37, }, 930969b242SAlexandre Belloni { .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, }, 940969b242SAlexandre Belloni { .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, }, 950969b242SAlexandre Belloni { .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, }, 960969b242SAlexandre Belloni { .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, }, 970969b242SAlexandre Belloni { .n = "sha_clk", .id = 42, }, 980969b242SAlexandre Belloni { .n = "aes_clk", .id = 43, }, 990969b242SAlexandre Belloni { .n = "tdes_clk", .id = 44, }, 1000969b242SAlexandre Belloni { .n = "trng_clk", .id = 45, }, 1010969b242SAlexandre Belloni { .n = "fuse_clk", .id = 48, }, 1020969b242SAlexandre Belloni { .n = "mpddr_clk", .id = 49, }, 1030969b242SAlexandre Belloni }; 1040969b242SAlexandre Belloni 1050969b242SAlexandre Belloni static void __init sama5d3_pmc_setup(struct device_node *np) 1060969b242SAlexandre Belloni { 1070969b242SAlexandre Belloni const char *slck_name, *mainxtal_name; 1080969b242SAlexandre Belloni struct pmc_data *sama5d3_pmc; 1090969b242SAlexandre Belloni const char *parent_names[5]; 1100969b242SAlexandre Belloni struct regmap *regmap; 1110969b242SAlexandre Belloni struct clk_hw *hw; 1120969b242SAlexandre Belloni int i; 1130969b242SAlexandre Belloni bool bypass; 1140969b242SAlexandre Belloni 1150969b242SAlexandre Belloni i = of_property_match_string(np, "clock-names", "slow_clk"); 1160969b242SAlexandre Belloni if (i < 0) 1170969b242SAlexandre Belloni return; 1180969b242SAlexandre Belloni 1190969b242SAlexandre Belloni slck_name = of_clk_get_parent_name(np, i); 1200969b242SAlexandre Belloni 1210969b242SAlexandre Belloni i = of_property_match_string(np, "clock-names", "main_xtal"); 1220969b242SAlexandre Belloni if (i < 0) 1230969b242SAlexandre Belloni return; 1240969b242SAlexandre Belloni mainxtal_name = of_clk_get_parent_name(np, i); 1250969b242SAlexandre Belloni 126153bc1c6SAhmad Fatoum regmap = device_node_to_regmap(np); 1270969b242SAlexandre Belloni if (IS_ERR(regmap)) 1280969b242SAlexandre Belloni return; 1290969b242SAlexandre Belloni 13003a1ee1dSMichał Mirosław sama5d3_pmc = pmc_data_allocate(PMC_PLLACK + 1, 1310969b242SAlexandre Belloni nck(sama5d3_systemck), 13299767cd4SMichał Mirosław nck(sama5d3_periphck), 0, 3); 1330969b242SAlexandre Belloni if (!sama5d3_pmc) 1340969b242SAlexandre Belloni return; 1350969b242SAlexandre Belloni 1360969b242SAlexandre Belloni hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000, 1370969b242SAlexandre Belloni 50000000); 1380969b242SAlexandre Belloni if (IS_ERR(hw)) 1390969b242SAlexandre Belloni goto err_free; 1400969b242SAlexandre Belloni 1410969b242SAlexandre Belloni bypass = of_property_read_bool(np, "atmel,osc-bypass"); 1420969b242SAlexandre Belloni 1430969b242SAlexandre Belloni hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, 1440969b242SAlexandre Belloni bypass); 1450969b242SAlexandre Belloni if (IS_ERR(hw)) 1460969b242SAlexandre Belloni goto err_free; 1470969b242SAlexandre Belloni 1480969b242SAlexandre Belloni parent_names[0] = "main_rc_osc"; 1490969b242SAlexandre Belloni parent_names[1] = "main_osc"; 1500969b242SAlexandre Belloni hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2); 1510969b242SAlexandre Belloni if (IS_ERR(hw)) 1520969b242SAlexandre Belloni goto err_free; 1530969b242SAlexandre Belloni 1540969b242SAlexandre Belloni hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0, 1550969b242SAlexandre Belloni &sama5d3_pll_layout, &plla_characteristics); 1560969b242SAlexandre Belloni if (IS_ERR(hw)) 1570969b242SAlexandre Belloni goto err_free; 1580969b242SAlexandre Belloni 1590969b242SAlexandre Belloni hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack"); 1600969b242SAlexandre Belloni if (IS_ERR(hw)) 1610969b242SAlexandre Belloni goto err_free; 1620969b242SAlexandre Belloni 16303a1ee1dSMichał Mirosław sama5d3_pmc->chws[PMC_PLLACK] = hw; 16403a1ee1dSMichał Mirosław 1650969b242SAlexandre Belloni hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck"); 1660969b242SAlexandre Belloni if (IS_ERR(hw)) 1670969b242SAlexandre Belloni goto err_free; 1680969b242SAlexandre Belloni 1690969b242SAlexandre Belloni sama5d3_pmc->chws[PMC_UTMI] = hw; 1700969b242SAlexandre Belloni 1710969b242SAlexandre Belloni parent_names[0] = slck_name; 1720969b242SAlexandre Belloni parent_names[1] = "mainck"; 1730969b242SAlexandre Belloni parent_names[2] = "plladivck"; 1740969b242SAlexandre Belloni parent_names[3] = "utmick"; 175*7a110b91SClaudiu Beznea hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4, 176*7a110b91SClaudiu Beznea parent_names, 1770969b242SAlexandre Belloni &at91sam9x5_master_layout, 178*7a110b91SClaudiu Beznea &mck_characteristics, &mck_lock, 179*7a110b91SClaudiu Beznea CLK_SET_RATE_GATE, INT_MIN); 180*7a110b91SClaudiu Beznea if (IS_ERR(hw)) 181*7a110b91SClaudiu Beznea goto err_free; 182*7a110b91SClaudiu Beznea 183*7a110b91SClaudiu Beznea hw = at91_clk_register_master_div(regmap, "masterck_div", 184*7a110b91SClaudiu Beznea "masterck_pres", 185*7a110b91SClaudiu Beznea &at91sam9x5_master_layout, 186*7a110b91SClaudiu Beznea &mck_characteristics, &mck_lock, 187*7a110b91SClaudiu Beznea CLK_SET_RATE_GATE); 1880969b242SAlexandre Belloni if (IS_ERR(hw)) 1890969b242SAlexandre Belloni goto err_free; 1900969b242SAlexandre Belloni 1910969b242SAlexandre Belloni sama5d3_pmc->chws[PMC_MCK] = hw; 1920969b242SAlexandre Belloni 1930969b242SAlexandre Belloni parent_names[0] = "plladivck"; 1940969b242SAlexandre Belloni parent_names[1] = "utmick"; 1950969b242SAlexandre Belloni hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2); 1960969b242SAlexandre Belloni if (IS_ERR(hw)) 1970969b242SAlexandre Belloni goto err_free; 1980969b242SAlexandre Belloni 1990969b242SAlexandre Belloni hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2); 2000969b242SAlexandre Belloni if (IS_ERR(hw)) 2010969b242SAlexandre Belloni goto err_free; 2020969b242SAlexandre Belloni 2030969b242SAlexandre Belloni parent_names[0] = slck_name; 2040969b242SAlexandre Belloni parent_names[1] = "mainck"; 2050969b242SAlexandre Belloni parent_names[2] = "plladivck"; 2060969b242SAlexandre Belloni parent_names[3] = "utmick"; 207*7a110b91SClaudiu Beznea parent_names[4] = "masterck_div"; 2080969b242SAlexandre Belloni for (i = 0; i < 3; i++) { 2090969b242SAlexandre Belloni char name[6]; 2100969b242SAlexandre Belloni 2110969b242SAlexandre Belloni snprintf(name, sizeof(name), "prog%d", i); 2120969b242SAlexandre Belloni 2130969b242SAlexandre Belloni hw = at91_clk_register_programmable(regmap, name, 2140969b242SAlexandre Belloni parent_names, 5, i, 215c57aaaa2SClaudiu Beznea &at91sam9x5_programmable_layout, 216c57aaaa2SClaudiu Beznea NULL); 2170969b242SAlexandre Belloni if (IS_ERR(hw)) 2180969b242SAlexandre Belloni goto err_free; 21999767cd4SMichał Mirosław 22099767cd4SMichał Mirosław sama5d3_pmc->pchws[i] = hw; 2210969b242SAlexandre Belloni } 2220969b242SAlexandre Belloni 2230969b242SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) { 2240969b242SAlexandre Belloni hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n, 2250969b242SAlexandre Belloni sama5d3_systemck[i].p, 2260969b242SAlexandre Belloni sama5d3_systemck[i].id); 2270969b242SAlexandre Belloni if (IS_ERR(hw)) 2280969b242SAlexandre Belloni goto err_free; 2290969b242SAlexandre Belloni 2300969b242SAlexandre Belloni sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw; 2310969b242SAlexandre Belloni } 2320969b242SAlexandre Belloni 2330969b242SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) { 2340969b242SAlexandre Belloni hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock, 2350969b242SAlexandre Belloni &sama5d3_pcr_layout, 2360969b242SAlexandre Belloni sama5d3_periphck[i].n, 237*7a110b91SClaudiu Beznea "masterck_div", 2380969b242SAlexandre Belloni sama5d3_periphck[i].id, 239b4c115c7SClaudiu Beznea &sama5d3_periphck[i].r, 240b4c115c7SClaudiu Beznea INT_MIN); 2410969b242SAlexandre Belloni if (IS_ERR(hw)) 2420969b242SAlexandre Belloni goto err_free; 2430969b242SAlexandre Belloni 2440969b242SAlexandre Belloni sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw; 2450969b242SAlexandre Belloni } 2460969b242SAlexandre Belloni 2470969b242SAlexandre Belloni of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc); 2480969b242SAlexandre Belloni 2490969b242SAlexandre Belloni return; 2500969b242SAlexandre Belloni 2510969b242SAlexandre Belloni err_free: 2527425f246SMichał Mirosław kfree(sama5d3_pmc); 2530969b242SAlexandre Belloni } 2540969b242SAlexandre Belloni /* 2550969b242SAlexandre Belloni * The TCB is used as the clocksource so its clock is needed early. This means 2560969b242SAlexandre Belloni * this can't be a platform driver. 2570969b242SAlexandre Belloni */ 2580969b242SAlexandre Belloni CLK_OF_DECLARE_DRIVER(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup); 259