blob: 6b55ec59d6dfdd6456e26ddb3cd38ef8d6d8f861 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yang9e5935c2016-07-20 17:55:12 +08002/*
3 * Copyright (C) 2016 Atmel Corporation
4 * Wenyou.Yang <wenyou.yang@atmel.com>
Wenyou Yang9e5935c2016-07-20 17:55:12 +08005 */
6
7#include <common.h>
8#include <clk-uclass.h>
Simon Glass9d922452017-05-17 17:18:03 -06009#include <dm.h>
Wenyou Yang9e5935c2016-07-20 17:55:12 +080010#include <dm/lists.h>
Heiko Stübner27326c72017-02-18 19:46:21 +010011#include <dm/util.h>
Wenyou Yang9e5935c2016-07-20 17:55:12 +080012#include "pmc.h"
13
14DECLARE_GLOBAL_DATA_PTR;
15
Wenyou Yang9e5935c2016-07-20 17:55:12 +080016static const struct udevice_id at91_pmc_match[] = {
Wenyou Yang3fea8092017-04-14 14:53:24 +080017 { .compatible = "atmel,at91rm9200-pmc" },
18 { .compatible = "atmel,at91sam9260-pmc" },
19 { .compatible = "atmel,at91sam9g45-pmc" },
20 { .compatible = "atmel,at91sam9n12-pmc" },
21 { .compatible = "atmel,at91sam9x5-pmc" },
22 { .compatible = "atmel,sama5d3-pmc" },
Wenyou Yang9e5935c2016-07-20 17:55:12 +080023 { .compatible = "atmel,sama5d2-pmc" },
24 {}
25};
26
27U_BOOT_DRIVER(at91_pmc) = {
Wenyou Yangb892b052016-09-13 10:25:55 +080028 .name = "at91-pmc",
29 .id = UCLASS_SIMPLE_BUS,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080030 .of_match = at91_pmc_match,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080031};
32
Wenyou Yang6cadaa02016-09-27 11:00:29 +080033/*---------------------------------------------------------*/
34
Wenyou Yang9e5935c2016-07-20 17:55:12 +080035int at91_pmc_core_probe(struct udevice *dev)
36{
37 struct pmc_platdata *plat = dev_get_platdata(dev);
38
39 dev = dev_get_parent(dev);
40
Simon Glassa821c4a2017-05-17 17:18:05 -060041 plat->reg_base = (struct at91_pmc *)devfdt_get_addr_ptr(dev);
Wenyou Yang9e5935c2016-07-20 17:55:12 +080042
43 return 0;
44}
45
Wenyou Yang6cadaa02016-09-27 11:00:29 +080046/**
47 * at91_clk_sub_device_bind() - for the at91 clock driver
48 * Recursively bind its children as clk devices.
49 *
50 * @return: 0 on success, or negative error code on failure
51 */
52int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
Wenyou Yang9e5935c2016-07-20 17:55:12 +080053{
54 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -070055 int offset = dev_of_offset(dev);
Wenyou Yang6cadaa02016-09-27 11:00:29 +080056 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Wenyou Yang9e5935c2016-07-20 17:55:12 +080057 const char *name;
58 int ret;
59
60 for (offset = fdt_first_subnode(fdt, offset);
61 offset > 0;
62 offset = fdt_next_subnode(fdt, offset)) {
Wenyou Yang6cadaa02016-09-27 11:00:29 +080063 if (pre_reloc_only &&
Patrick Delaunay7bb94ab2019-03-20 18:21:24 +010064 !dm_ofnode_pre_reloc(offset_to_ofnode(offset)))
Wenyou Yang6cadaa02016-09-27 11:00:29 +080065 continue;
66 /*
67 * If this node has "compatible" property, this is not
68 * a clock sub-node, but a normal device. skip.
69 */
70 fdt_get_property(fdt, offset, "compatible", &ret);
71 if (ret >= 0)
72 continue;
73
74 if (ret != -FDT_ERR_NOTFOUND)
75 return ret;
76
Wenyou Yang9e5935c2016-07-20 17:55:12 +080077 name = fdt_get_name(fdt, offset, NULL);
78 if (!name)
79 return -EINVAL;
Wenyou Yang6cadaa02016-09-27 11:00:29 +080080 ret = device_bind_driver_to_node(dev, drv_name, name,
Simon Glass45a26862017-05-18 20:09:07 -060081 offset_to_ofnode(offset), NULL);
Wenyou Yang9e5935c2016-07-20 17:55:12 +080082 if (ret)
83 return ret;
84 }
85
86 return 0;
87}
88
Simon Glassa4e0ef52017-05-18 20:09:40 -060089int at91_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
Wenyou Yang6cadaa02016-09-27 11:00:29 +080090{
91 int periph;
92
93 if (args->args_count) {
94 debug("Invalid args_count: %d\n", args->args_count);
95 return -EINVAL;
96 }
97
Simon Glasse160f7d2017-01-17 16:52:55 -070098 periph = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(clk->dev), "reg",
99 -1);
Wenyou Yang6cadaa02016-09-27 11:00:29 +0800100 if (periph < 0)
101 return -EINVAL;
102
103 clk->id = periph;
104
105 return 0;
106}
107
108int at91_clk_probe(struct udevice *dev)
109{
110 struct udevice *dev_periph_container, *dev_pmc;
111 struct pmc_platdata *plat = dev_get_platdata(dev);
112
113 dev_periph_container = dev_get_parent(dev);
114 dev_pmc = dev_get_parent(dev_periph_container);
115
Simon Glassa821c4a2017-05-17 17:18:05 -0600116 plat->reg_base = (struct at91_pmc *)devfdt_get_addr_ptr(dev_pmc);
Wenyou Yang6cadaa02016-09-27 11:00:29 +0800117
118 return 0;
119}