102ff48e4SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
202ff48e4SAlexandre Belloni #include <linux/clk-provider.h>
302ff48e4SAlexandre Belloni #include <linux/mfd/syscon.h>
402ff48e4SAlexandre Belloni #include <linux/slab.h>
502ff48e4SAlexandre Belloni
602ff48e4SAlexandre Belloni #include <dt-bindings/clock/at91.h>
702ff48e4SAlexandre Belloni
802ff48e4SAlexandre Belloni #include "pmc.h"
902ff48e4SAlexandre Belloni
107a110b91SClaudiu Beznea static DEFINE_SPINLOCK(rm9200_mck_lock);
117a110b91SClaudiu Beznea
1202ff48e4SAlexandre Belloni struct sck {
1302ff48e4SAlexandre Belloni char *n;
1402ff48e4SAlexandre Belloni char *p;
1502ff48e4SAlexandre Belloni u8 id;
1602ff48e4SAlexandre Belloni };
1702ff48e4SAlexandre Belloni
1802ff48e4SAlexandre Belloni struct pck {
1902ff48e4SAlexandre Belloni char *n;
2002ff48e4SAlexandre Belloni u8 id;
2102ff48e4SAlexandre Belloni };
2202ff48e4SAlexandre Belloni
2302ff48e4SAlexandre Belloni static const struct clk_master_characteristics rm9200_mck_characteristics = {
2402ff48e4SAlexandre Belloni .output = { .min = 0, .max = 80000000 },
2502ff48e4SAlexandre Belloni .divisors = { 1, 2, 3, 4 },
2602ff48e4SAlexandre Belloni };
2702ff48e4SAlexandre Belloni
2802ff48e4SAlexandre Belloni static u8 rm9200_pll_out[] = { 0, 2 };
2902ff48e4SAlexandre Belloni
3002ff48e4SAlexandre Belloni static const struct clk_range rm9200_pll_outputs[] = {
3102ff48e4SAlexandre Belloni { .min = 80000000, .max = 160000000 },
3202ff48e4SAlexandre Belloni { .min = 150000000, .max = 180000000 },
3302ff48e4SAlexandre Belloni };
3402ff48e4SAlexandre Belloni
3502ff48e4SAlexandre Belloni static const struct clk_pll_characteristics rm9200_pll_characteristics = {
3602ff48e4SAlexandre Belloni .input = { .min = 1000000, .max = 32000000 },
3702ff48e4SAlexandre Belloni .num_output = ARRAY_SIZE(rm9200_pll_outputs),
3802ff48e4SAlexandre Belloni .output = rm9200_pll_outputs,
3902ff48e4SAlexandre Belloni .out = rm9200_pll_out,
4002ff48e4SAlexandre Belloni };
4102ff48e4SAlexandre Belloni
4202ff48e4SAlexandre Belloni static const struct sck at91rm9200_systemck[] = {
4357976762SMichael Grzeschik { .n = "udpck", .p = "usbck", .id = 1 },
4402ff48e4SAlexandre Belloni { .n = "uhpck", .p = "usbck", .id = 4 },
4502ff48e4SAlexandre Belloni { .n = "pck0", .p = "prog0", .id = 8 },
4602ff48e4SAlexandre Belloni { .n = "pck1", .p = "prog1", .id = 9 },
4702ff48e4SAlexandre Belloni { .n = "pck2", .p = "prog2", .id = 10 },
4802ff48e4SAlexandre Belloni { .n = "pck3", .p = "prog3", .id = 11 },
4902ff48e4SAlexandre Belloni };
5002ff48e4SAlexandre Belloni
5102ff48e4SAlexandre Belloni static const struct pck at91rm9200_periphck[] = {
5202ff48e4SAlexandre Belloni { .n = "pioA_clk", .id = 2 },
5302ff48e4SAlexandre Belloni { .n = "pioB_clk", .id = 3 },
5402ff48e4SAlexandre Belloni { .n = "pioC_clk", .id = 4 },
5502ff48e4SAlexandre Belloni { .n = "pioD_clk", .id = 5 },
5602ff48e4SAlexandre Belloni { .n = "usart0_clk", .id = 6 },
5702ff48e4SAlexandre Belloni { .n = "usart1_clk", .id = 7 },
5802ff48e4SAlexandre Belloni { .n = "usart2_clk", .id = 8 },
5902ff48e4SAlexandre Belloni { .n = "usart3_clk", .id = 9 },
6002ff48e4SAlexandre Belloni { .n = "mci0_clk", .id = 10 },
6102ff48e4SAlexandre Belloni { .n = "udc_clk", .id = 11 },
6202ff48e4SAlexandre Belloni { .n = "twi0_clk", .id = 12 },
6302ff48e4SAlexandre Belloni { .n = "spi0_clk", .id = 13 },
6402ff48e4SAlexandre Belloni { .n = "ssc0_clk", .id = 14 },
6502ff48e4SAlexandre Belloni { .n = "ssc1_clk", .id = 15 },
6602ff48e4SAlexandre Belloni { .n = "ssc2_clk", .id = 16 },
6702ff48e4SAlexandre Belloni { .n = "tc0_clk", .id = 17 },
6802ff48e4SAlexandre Belloni { .n = "tc1_clk", .id = 18 },
6902ff48e4SAlexandre Belloni { .n = "tc2_clk", .id = 19 },
7002ff48e4SAlexandre Belloni { .n = "tc3_clk", .id = 20 },
7102ff48e4SAlexandre Belloni { .n = "tc4_clk", .id = 21 },
7202ff48e4SAlexandre Belloni { .n = "tc5_clk", .id = 22 },
7302ff48e4SAlexandre Belloni { .n = "ohci_clk", .id = 23 },
7402ff48e4SAlexandre Belloni { .n = "macb0_clk", .id = 24 },
7502ff48e4SAlexandre Belloni };
7602ff48e4SAlexandre Belloni
at91rm9200_pmc_setup(struct device_node * np)7702ff48e4SAlexandre Belloni static void __init at91rm9200_pmc_setup(struct device_node *np)
7802ff48e4SAlexandre Belloni {
7902ff48e4SAlexandre Belloni const char *slowxtal_name, *mainxtal_name;
8002ff48e4SAlexandre Belloni struct pmc_data *at91rm9200_pmc;
8102ff48e4SAlexandre Belloni u32 usb_div[] = { 1, 2, 0, 0 };
8202ff48e4SAlexandre Belloni const char *parent_names[6];
8302ff48e4SAlexandre Belloni struct regmap *regmap;
8402ff48e4SAlexandre Belloni struct clk_hw *hw;
8502ff48e4SAlexandre Belloni int i;
8602ff48e4SAlexandre Belloni bool bypass;
8702ff48e4SAlexandre Belloni
8802ff48e4SAlexandre Belloni i = of_property_match_string(np, "clock-names", "slow_xtal");
8902ff48e4SAlexandre Belloni if (i < 0)
9002ff48e4SAlexandre Belloni return;
9102ff48e4SAlexandre Belloni
9202ff48e4SAlexandre Belloni slowxtal_name = of_clk_get_parent_name(np, i);
9302ff48e4SAlexandre Belloni
9402ff48e4SAlexandre Belloni i = of_property_match_string(np, "clock-names", "main_xtal");
9502ff48e4SAlexandre Belloni if (i < 0)
9602ff48e4SAlexandre Belloni return;
9702ff48e4SAlexandre Belloni mainxtal_name = of_clk_get_parent_name(np, i);
9802ff48e4SAlexandre Belloni
9902ff48e4SAlexandre Belloni regmap = device_node_to_regmap(np);
10002ff48e4SAlexandre Belloni if (IS_ERR(regmap))
10102ff48e4SAlexandre Belloni return;
10202ff48e4SAlexandre Belloni
10303a1ee1dSMichał Mirosław at91rm9200_pmc = pmc_data_allocate(PMC_PLLBCK + 1,
10402ff48e4SAlexandre Belloni nck(at91rm9200_systemck),
10599767cd4SMichał Mirosław nck(at91rm9200_periphck), 0, 4);
10602ff48e4SAlexandre Belloni if (!at91rm9200_pmc)
10702ff48e4SAlexandre Belloni return;
10802ff48e4SAlexandre Belloni
10902ff48e4SAlexandre Belloni bypass = of_property_read_bool(np, "atmel,osc-bypass");
11002ff48e4SAlexandre Belloni
111b5105e37SClaudiu Beznea hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name, NULL,
11202ff48e4SAlexandre Belloni bypass);
11302ff48e4SAlexandre Belloni if (IS_ERR(hw))
11402ff48e4SAlexandre Belloni goto err_free;
11502ff48e4SAlexandre Belloni
116b5105e37SClaudiu Beznea hw = at91_clk_register_rm9200_main(regmap, "mainck", "main_osc", NULL);
11702ff48e4SAlexandre Belloni if (IS_ERR(hw))
11802ff48e4SAlexandre Belloni goto err_free;
11902ff48e4SAlexandre Belloni
12002ff48e4SAlexandre Belloni at91rm9200_pmc->chws[PMC_MAIN] = hw;
12102ff48e4SAlexandre Belloni
12202ff48e4SAlexandre Belloni hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
12302ff48e4SAlexandre Belloni &at91rm9200_pll_layout,
12402ff48e4SAlexandre Belloni &rm9200_pll_characteristics);
12502ff48e4SAlexandre Belloni if (IS_ERR(hw))
12602ff48e4SAlexandre Belloni goto err_free;
12702ff48e4SAlexandre Belloni
12803a1ee1dSMichał Mirosław at91rm9200_pmc->chws[PMC_PLLACK] = hw;
12903a1ee1dSMichał Mirosław
13002ff48e4SAlexandre Belloni hw = at91_clk_register_pll(regmap, "pllbck", "mainck", 1,
13102ff48e4SAlexandre Belloni &at91rm9200_pll_layout,
13202ff48e4SAlexandre Belloni &rm9200_pll_characteristics);
13302ff48e4SAlexandre Belloni if (IS_ERR(hw))
13402ff48e4SAlexandre Belloni goto err_free;
13502ff48e4SAlexandre Belloni
13603a1ee1dSMichał Mirosław at91rm9200_pmc->chws[PMC_PLLBCK] = hw;
13703a1ee1dSMichał Mirosław
13802ff48e4SAlexandre Belloni parent_names[0] = slowxtal_name;
13902ff48e4SAlexandre Belloni parent_names[1] = "mainck";
14002ff48e4SAlexandre Belloni parent_names[2] = "pllack";
14102ff48e4SAlexandre Belloni parent_names[3] = "pllbck";
1427a110b91SClaudiu Beznea hw = at91_clk_register_master_pres(regmap, "masterck_pres", 4,
143171e502cSClaudiu Beznea parent_names, NULL,
14402ff48e4SAlexandre Belloni &at91rm9200_master_layout,
1457a110b91SClaudiu Beznea &rm9200_mck_characteristics,
1468e842f02SClaudiu Beznea &rm9200_mck_lock);
1477a110b91SClaudiu Beznea if (IS_ERR(hw))
1487a110b91SClaudiu Beznea goto err_free;
1497a110b91SClaudiu Beznea
1507a110b91SClaudiu Beznea hw = at91_clk_register_master_div(regmap, "masterck_div",
151171e502cSClaudiu Beznea "masterck_pres", NULL,
1527a110b91SClaudiu Beznea &at91rm9200_master_layout,
1537a110b91SClaudiu Beznea &rm9200_mck_characteristics,
1547029db09SClaudiu Beznea &rm9200_mck_lock, CLK_SET_RATE_GATE, 0);
15502ff48e4SAlexandre Belloni if (IS_ERR(hw))
15602ff48e4SAlexandre Belloni goto err_free;
15702ff48e4SAlexandre Belloni
15802ff48e4SAlexandre Belloni at91rm9200_pmc->chws[PMC_MCK] = hw;
15902ff48e4SAlexandre Belloni
16002ff48e4SAlexandre Belloni hw = at91rm9200_clk_register_usb(regmap, "usbck", "pllbck", usb_div);
16102ff48e4SAlexandre Belloni if (IS_ERR(hw))
16202ff48e4SAlexandre Belloni goto err_free;
16302ff48e4SAlexandre Belloni
16402ff48e4SAlexandre Belloni parent_names[0] = slowxtal_name;
16502ff48e4SAlexandre Belloni parent_names[1] = "mainck";
16602ff48e4SAlexandre Belloni parent_names[2] = "pllack";
16702ff48e4SAlexandre Belloni parent_names[3] = "pllbck";
16802ff48e4SAlexandre Belloni for (i = 0; i < 4; i++) {
16902ff48e4SAlexandre Belloni char name[6];
17002ff48e4SAlexandre Belloni
17102ff48e4SAlexandre Belloni snprintf(name, sizeof(name), "prog%d", i);
17202ff48e4SAlexandre Belloni
17302ff48e4SAlexandre Belloni hw = at91_clk_register_programmable(regmap, name,
1741a2669dfSClaudiu Beznea parent_names, NULL, 4, i,
175c57aaaa2SClaudiu Beznea &at91rm9200_programmable_layout,
176c57aaaa2SClaudiu Beznea NULL);
17702ff48e4SAlexandre Belloni if (IS_ERR(hw))
17802ff48e4SAlexandre Belloni goto err_free;
17999767cd4SMichał Mirosław
18099767cd4SMichał Mirosław at91rm9200_pmc->pchws[i] = hw;
18102ff48e4SAlexandre Belloni }
18202ff48e4SAlexandre Belloni
18302ff48e4SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(at91rm9200_systemck); i++) {
18402ff48e4SAlexandre Belloni hw = at91_clk_register_system(regmap, at91rm9200_systemck[i].n,
185*1a537f62SClaudiu Beznea at91rm9200_systemck[i].p, NULL,
18668b3b6f1SClaudiu Beznea at91rm9200_systemck[i].id, 0);
18702ff48e4SAlexandre Belloni if (IS_ERR(hw))
18802ff48e4SAlexandre Belloni goto err_free;
18902ff48e4SAlexandre Belloni
19002ff48e4SAlexandre Belloni at91rm9200_pmc->shws[at91rm9200_systemck[i].id] = hw;
19102ff48e4SAlexandre Belloni }
19202ff48e4SAlexandre Belloni
19302ff48e4SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(at91rm9200_periphck); i++) {
19402ff48e4SAlexandre Belloni hw = at91_clk_register_peripheral(regmap,
19502ff48e4SAlexandre Belloni at91rm9200_periphck[i].n,
196c2f2ca0bSClaudiu Beznea "masterck_div", NULL,
19702ff48e4SAlexandre Belloni at91rm9200_periphck[i].id);
19802ff48e4SAlexandre Belloni if (IS_ERR(hw))
19902ff48e4SAlexandre Belloni goto err_free;
20002ff48e4SAlexandre Belloni
20102ff48e4SAlexandre Belloni at91rm9200_pmc->phws[at91rm9200_periphck[i].id] = hw;
20202ff48e4SAlexandre Belloni }
20302ff48e4SAlexandre Belloni
20402ff48e4SAlexandre Belloni of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91rm9200_pmc);
20502ff48e4SAlexandre Belloni
20602ff48e4SAlexandre Belloni return;
20702ff48e4SAlexandre Belloni
20802ff48e4SAlexandre Belloni err_free:
2097425f246SMichał Mirosław kfree(at91rm9200_pmc);
21002ff48e4SAlexandre Belloni }
21102ff48e4SAlexandre Belloni /*
21202ff48e4SAlexandre Belloni * While the TCB can be used as the clocksource, the system timer is most likely
21302ff48e4SAlexandre Belloni * to be used instead. However, the pinctrl driver doesn't support probe
21402ff48e4SAlexandre Belloni * deferring properly. Once this is fixed, this can be switched to a platform
21502ff48e4SAlexandre Belloni * driver.
21602ff48e4SAlexandre Belloni */
217428d97e1STudor Ambarus CLK_OF_DECLARE(at91rm9200_pmc, "atmel,at91rm9200-pmc", at91rm9200_pmc_setup);
218