112dc8d3bSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
212dc8d3bSAlexandre Belloni #include <linux/clk-provider.h>
312dc8d3bSAlexandre Belloni #include <linux/mfd/syscon.h>
412dc8d3bSAlexandre Belloni #include <linux/slab.h>
512dc8d3bSAlexandre Belloni
612dc8d3bSAlexandre Belloni #include <dt-bindings/clock/at91.h>
712dc8d3bSAlexandre Belloni
812dc8d3bSAlexandre Belloni #include "pmc.h"
912dc8d3bSAlexandre Belloni
107a110b91SClaudiu Beznea static DEFINE_SPINLOCK(at91sam9g45_mck_lock);
117a110b91SClaudiu Beznea
1212dc8d3bSAlexandre Belloni static const struct clk_master_characteristics mck_characteristics = {
1312dc8d3bSAlexandre Belloni .output = { .min = 0, .max = 133333333 },
1412dc8d3bSAlexandre Belloni .divisors = { 1, 2, 4, 3 },
1512dc8d3bSAlexandre Belloni };
1612dc8d3bSAlexandre Belloni
1712dc8d3bSAlexandre Belloni static u8 plla_out[] = { 0, 1, 2, 3, 0, 1, 2, 3 };
1812dc8d3bSAlexandre Belloni
1912dc8d3bSAlexandre Belloni static u16 plla_icpll[] = { 0, 0, 0, 0, 1, 1, 1, 1 };
2012dc8d3bSAlexandre Belloni
2112dc8d3bSAlexandre Belloni static const struct clk_range plla_outputs[] = {
2212dc8d3bSAlexandre Belloni { .min = 745000000, .max = 800000000 },
2312dc8d3bSAlexandre Belloni { .min = 695000000, .max = 750000000 },
2412dc8d3bSAlexandre Belloni { .min = 645000000, .max = 700000000 },
2512dc8d3bSAlexandre Belloni { .min = 595000000, .max = 650000000 },
2612dc8d3bSAlexandre Belloni { .min = 545000000, .max = 600000000 },
2712dc8d3bSAlexandre Belloni { .min = 495000000, .max = 555000000 },
2812dc8d3bSAlexandre Belloni { .min = 445000000, .max = 500000000 },
2912dc8d3bSAlexandre Belloni { .min = 400000000, .max = 450000000 },
3012dc8d3bSAlexandre Belloni };
3112dc8d3bSAlexandre Belloni
3212dc8d3bSAlexandre Belloni static const struct clk_pll_characteristics plla_characteristics = {
3312dc8d3bSAlexandre Belloni .input = { .min = 2000000, .max = 32000000 },
3412dc8d3bSAlexandre Belloni .num_output = ARRAY_SIZE(plla_outputs),
3512dc8d3bSAlexandre Belloni .output = plla_outputs,
3612dc8d3bSAlexandre Belloni .icpll = plla_icpll,
3712dc8d3bSAlexandre Belloni .out = plla_out,
3812dc8d3bSAlexandre Belloni };
3912dc8d3bSAlexandre Belloni
4012dc8d3bSAlexandre Belloni static const struct {
4112dc8d3bSAlexandre Belloni char *n;
4212dc8d3bSAlexandre Belloni char *p;
4368b3b6f1SClaudiu Beznea unsigned long flags;
4412dc8d3bSAlexandre Belloni u8 id;
4512dc8d3bSAlexandre Belloni } at91sam9g45_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 },
5112dc8d3bSAlexandre Belloni { .n = "uhpck", .p = "usbck", .id = 6 },
5212dc8d3bSAlexandre Belloni { .n = "pck0", .p = "prog0", .id = 8 },
5312dc8d3bSAlexandre Belloni { .n = "pck1", .p = "prog1", .id = 9 },
5412dc8d3bSAlexandre Belloni };
5512dc8d3bSAlexandre Belloni
5612dc8d3bSAlexandre Belloni struct pck {
5712dc8d3bSAlexandre Belloni char *n;
5812dc8d3bSAlexandre Belloni u8 id;
5912dc8d3bSAlexandre Belloni };
6012dc8d3bSAlexandre Belloni
6112dc8d3bSAlexandre Belloni static const struct pck at91sam9g45_periphck[] = {
6212dc8d3bSAlexandre Belloni { .n = "pioA_clk", .id = 2, },
6312dc8d3bSAlexandre Belloni { .n = "pioB_clk", .id = 3, },
6412dc8d3bSAlexandre Belloni { .n = "pioC_clk", .id = 4, },
6512dc8d3bSAlexandre Belloni { .n = "pioDE_clk", .id = 5, },
6612dc8d3bSAlexandre Belloni { .n = "trng_clk", .id = 6, },
6712dc8d3bSAlexandre Belloni { .n = "usart0_clk", .id = 7, },
6812dc8d3bSAlexandre Belloni { .n = "usart1_clk", .id = 8, },
6912dc8d3bSAlexandre Belloni { .n = "usart2_clk", .id = 9, },
7012dc8d3bSAlexandre Belloni { .n = "usart3_clk", .id = 10, },
7112dc8d3bSAlexandre Belloni { .n = "mci0_clk", .id = 11, },
7212dc8d3bSAlexandre Belloni { .n = "twi0_clk", .id = 12, },
7312dc8d3bSAlexandre Belloni { .n = "twi1_clk", .id = 13, },
7412dc8d3bSAlexandre Belloni { .n = "spi0_clk", .id = 14, },
7512dc8d3bSAlexandre Belloni { .n = "spi1_clk", .id = 15, },
7612dc8d3bSAlexandre Belloni { .n = "ssc0_clk", .id = 16, },
7712dc8d3bSAlexandre Belloni { .n = "ssc1_clk", .id = 17, },
7812dc8d3bSAlexandre Belloni { .n = "tcb0_clk", .id = 18, },
7912dc8d3bSAlexandre Belloni { .n = "pwm_clk", .id = 19, },
8012dc8d3bSAlexandre Belloni { .n = "adc_clk", .id = 20, },
8112dc8d3bSAlexandre Belloni { .n = "dma0_clk", .id = 21, },
8212dc8d3bSAlexandre Belloni { .n = "uhphs_clk", .id = 22, },
8312dc8d3bSAlexandre Belloni { .n = "lcd_clk", .id = 23, },
8412dc8d3bSAlexandre Belloni { .n = "ac97_clk", .id = 24, },
8512dc8d3bSAlexandre Belloni { .n = "macb0_clk", .id = 25, },
8612dc8d3bSAlexandre Belloni { .n = "isi_clk", .id = 26, },
8712dc8d3bSAlexandre Belloni { .n = "udphs_clk", .id = 27, },
8812dc8d3bSAlexandre Belloni { .n = "aestdessha_clk", .id = 28, },
8912dc8d3bSAlexandre Belloni { .n = "mci1_clk", .id = 29, },
9012dc8d3bSAlexandre Belloni { .n = "vdec_clk", .id = 30, },
9112dc8d3bSAlexandre Belloni };
9212dc8d3bSAlexandre Belloni
at91sam9g45_pmc_setup(struct device_node * np)9312dc8d3bSAlexandre Belloni static void __init at91sam9g45_pmc_setup(struct device_node *np)
9412dc8d3bSAlexandre Belloni {
9512dc8d3bSAlexandre Belloni const char *slck_name, *mainxtal_name;
9612dc8d3bSAlexandre Belloni struct pmc_data *at91sam9g45_pmc;
9712dc8d3bSAlexandre Belloni const char *parent_names[6];
9812dc8d3bSAlexandre Belloni struct regmap *regmap;
9912dc8d3bSAlexandre Belloni struct clk_hw *hw;
10012dc8d3bSAlexandre Belloni int i;
10112dc8d3bSAlexandre Belloni bool bypass;
10212dc8d3bSAlexandre Belloni
10312dc8d3bSAlexandre Belloni i = of_property_match_string(np, "clock-names", "slow_clk");
10412dc8d3bSAlexandre Belloni if (i < 0)
10512dc8d3bSAlexandre Belloni return;
10612dc8d3bSAlexandre Belloni
10712dc8d3bSAlexandre Belloni slck_name = of_clk_get_parent_name(np, i);
10812dc8d3bSAlexandre Belloni
10912dc8d3bSAlexandre Belloni i = of_property_match_string(np, "clock-names", "main_xtal");
11012dc8d3bSAlexandre Belloni if (i < 0)
11112dc8d3bSAlexandre Belloni return;
11212dc8d3bSAlexandre Belloni mainxtal_name = of_clk_get_parent_name(np, i);
11312dc8d3bSAlexandre Belloni
114153bc1c6SAhmad Fatoum regmap = device_node_to_regmap(np);
11512dc8d3bSAlexandre Belloni if (IS_ERR(regmap))
11612dc8d3bSAlexandre Belloni return;
11712dc8d3bSAlexandre Belloni
11803a1ee1dSMichał Mirosław at91sam9g45_pmc = pmc_data_allocate(PMC_PLLACK + 1,
11912dc8d3bSAlexandre Belloni nck(at91sam9g45_systemck),
12099767cd4SMichał Mirosław nck(at91sam9g45_periphck), 0, 2);
12112dc8d3bSAlexandre Belloni if (!at91sam9g45_pmc)
12212dc8d3bSAlexandre Belloni return;
12312dc8d3bSAlexandre Belloni
12412dc8d3bSAlexandre Belloni bypass = of_property_read_bool(np, "atmel,osc-bypass");
12512dc8d3bSAlexandre Belloni
126b5105e37SClaudiu Beznea hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
12712dc8d3bSAlexandre Belloni bypass);
12812dc8d3bSAlexandre Belloni if (IS_ERR(hw))
12912dc8d3bSAlexandre Belloni goto err_free;
13012dc8d3bSAlexandre Belloni
131b5105e37SClaudiu Beznea hw = at91_clk_register_rm9200_main(regmap, "mainck", "main_osc", NULL);
13212dc8d3bSAlexandre Belloni if (IS_ERR(hw))
13312dc8d3bSAlexandre Belloni goto err_free;
13412dc8d3bSAlexandre Belloni
13512dc8d3bSAlexandre Belloni at91sam9g45_pmc->chws[PMC_MAIN] = hw;
13612dc8d3bSAlexandre Belloni
13712dc8d3bSAlexandre Belloni hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
13812dc8d3bSAlexandre Belloni &at91rm9200_pll_layout, &plla_characteristics);
13912dc8d3bSAlexandre Belloni if (IS_ERR(hw))
14012dc8d3bSAlexandre Belloni goto err_free;
14112dc8d3bSAlexandre Belloni
14212dc8d3bSAlexandre Belloni hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
14312dc8d3bSAlexandre Belloni if (IS_ERR(hw))
14412dc8d3bSAlexandre Belloni goto err_free;
14512dc8d3bSAlexandre Belloni
14603a1ee1dSMichał Mirosław at91sam9g45_pmc->chws[PMC_PLLACK] = hw;
14703a1ee1dSMichał Mirosław
148*077782e3SClaudiu Beznea hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck", NULL);
14912dc8d3bSAlexandre Belloni if (IS_ERR(hw))
15012dc8d3bSAlexandre Belloni goto err_free;
15112dc8d3bSAlexandre Belloni
15212dc8d3bSAlexandre Belloni at91sam9g45_pmc->chws[PMC_UTMI] = hw;
15312dc8d3bSAlexandre Belloni
15412dc8d3bSAlexandre Belloni parent_names[0] = slck_name;
15512dc8d3bSAlexandre Belloni parent_names[1] = "mainck";
15612dc8d3bSAlexandre Belloni parent_names[2] = "plladivck";
15712dc8d3bSAlexandre Belloni parent_names[3] = "utmick";
1587a110b91SClaudiu Beznea hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
159171e502cSClaudiu Beznea parent_names, NULL,
16012dc8d3bSAlexandre Belloni &at91rm9200_master_layout,
1617a110b91SClaudiu Beznea &mck_characteristics,
1628e842f02SClaudiu Beznea &at91sam9g45_mck_lock);
1637a110b91SClaudiu Beznea if (IS_ERR(hw))
1647a110b91SClaudiu Beznea goto err_free;
1657a110b91SClaudiu Beznea
1667a110b91SClaudiu Beznea hw = at91_clk_register_master_div(regmap, "masterck_div",
167171e502cSClaudiu Beznea "masterck_pres", NULL,
1687a110b91SClaudiu Beznea &at91rm9200_master_layout,
1697a110b91SClaudiu Beznea &mck_characteristics,
1707a110b91SClaudiu Beznea &at91sam9g45_mck_lock,
1717029db09SClaudiu Beznea CLK_SET_RATE_GATE, 0);
17212dc8d3bSAlexandre Belloni if (IS_ERR(hw))
17312dc8d3bSAlexandre Belloni goto err_free;
17412dc8d3bSAlexandre Belloni
17512dc8d3bSAlexandre Belloni at91sam9g45_pmc->chws[PMC_MCK] = hw;
17612dc8d3bSAlexandre Belloni
17712dc8d3bSAlexandre Belloni parent_names[0] = "plladivck";
17812dc8d3bSAlexandre Belloni parent_names[1] = "utmick";
17912dc8d3bSAlexandre Belloni hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
18012dc8d3bSAlexandre Belloni if (IS_ERR(hw))
18112dc8d3bSAlexandre Belloni goto err_free;
18212dc8d3bSAlexandre Belloni
18312dc8d3bSAlexandre Belloni parent_names[0] = slck_name;
18412dc8d3bSAlexandre Belloni parent_names[1] = "mainck";
18512dc8d3bSAlexandre Belloni parent_names[2] = "plladivck";
18612dc8d3bSAlexandre Belloni parent_names[3] = "utmick";
1877a110b91SClaudiu Beznea parent_names[4] = "masterck_div";
18812dc8d3bSAlexandre Belloni for (i = 0; i < 2; i++) {
18912dc8d3bSAlexandre Belloni char name[6];
19012dc8d3bSAlexandre Belloni
19112dc8d3bSAlexandre Belloni snprintf(name, sizeof(name), "prog%d", i);
19212dc8d3bSAlexandre Belloni
19312dc8d3bSAlexandre Belloni hw = at91_clk_register_programmable(regmap, name,
1941a2669dfSClaudiu Beznea parent_names, NULL, 5, i,
195c57aaaa2SClaudiu Beznea &at91sam9g45_programmable_layout,
196c57aaaa2SClaudiu Beznea NULL);
19712dc8d3bSAlexandre Belloni if (IS_ERR(hw))
19812dc8d3bSAlexandre Belloni goto err_free;
19999767cd4SMichał Mirosław
20099767cd4SMichał Mirosław at91sam9g45_pmc->pchws[i] = hw;
20112dc8d3bSAlexandre Belloni }
20212dc8d3bSAlexandre Belloni
20312dc8d3bSAlexandre Belloni for (i = 0; i < ARRAY_SIZE(at91sam9g45_systemck); i++) {
20412dc8d3bSAlexandre Belloni hw = at91_clk_register_system(regmap, at91sam9g45_systemck[i].n,
2051a537f62SClaudiu Beznea at91sam9g45_systemck[i].p, NULL,
20668b3b6f1SClaudiu Beznea at91sam9g45_systemck[i].id,
20768b3b6f1SClaudiu Beznea at91sam9g45_systemck[i].flags);
20812dc8d3bSAlexandre Belloni if (IS_ERR(hw))
20912dc8d3bSAlexandre Belloni goto err_free;
21012dc8d3bSAlexandre Belloni
21112dc8d3bSAlexandre Belloni at91sam9g45_pmc->shws[at91sam9g45_systemck[i].id] = hw;
21212dc8d3bSAlexandre Belloni }
21312dc8d3bSAlexandre Belloni
21412dc8d3bSAlexandre Belloni for (i = 0; i < ARRAY_SIZE(at91sam9g45_periphck); i++) {
21512dc8d3bSAlexandre Belloni hw = at91_clk_register_peripheral(regmap,
21612dc8d3bSAlexandre Belloni at91sam9g45_periphck[i].n,
217c2f2ca0bSClaudiu Beznea "masterck_div", NULL,
21812dc8d3bSAlexandre Belloni at91sam9g45_periphck[i].id);
21912dc8d3bSAlexandre Belloni if (IS_ERR(hw))
22012dc8d3bSAlexandre Belloni goto err_free;
22112dc8d3bSAlexandre Belloni
22212dc8d3bSAlexandre Belloni at91sam9g45_pmc->phws[at91sam9g45_periphck[i].id] = hw;
22312dc8d3bSAlexandre Belloni }
22412dc8d3bSAlexandre Belloni
22512dc8d3bSAlexandre Belloni of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91sam9g45_pmc);
22612dc8d3bSAlexandre Belloni
22712dc8d3bSAlexandre Belloni return;
22812dc8d3bSAlexandre Belloni
22912dc8d3bSAlexandre Belloni err_free:
2307425f246SMichał Mirosław kfree(at91sam9g45_pmc);
23112dc8d3bSAlexandre Belloni }
23212dc8d3bSAlexandre Belloni /*
23312dc8d3bSAlexandre Belloni * The TCB is used as the clocksource so its clock is needed early. This means
23412dc8d3bSAlexandre Belloni * this can't be a platform driver.
23512dc8d3bSAlexandre Belloni */
236428d97e1STudor Ambarus CLK_OF_DECLARE(at91sam9g45_pmc, "atmel,at91sam9g45-pmc", at91sam9g45_pmc_setup);
237