blob: 76ba91af81209296585eae984e6143b3719c0db0 [file] [log] [blame]
Wenyou Yang9e5935c2016-07-20 17:55:12 +08001/*
2 * Copyright (C) 2016 Atmel Corporation
3 * Wenyou.Yang <wenyou.yang@atmel.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <clk-uclass.h>
10#include <dm/device.h>
11#include <dm/lists.h>
12#include <dm/root.h>
13#include "pmc.h"
14
15DECLARE_GLOBAL_DATA_PTR;
16
Wenyou Yang9e5935c2016-07-20 17:55:12 +080017static const struct udevice_id at91_pmc_match[] = {
18 { .compatible = "atmel,sama5d2-pmc" },
19 {}
20};
21
22U_BOOT_DRIVER(at91_pmc) = {
Wenyou Yangb892b052016-09-13 10:25:55 +080023 .name = "at91-pmc",
24 .id = UCLASS_SIMPLE_BUS,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080025 .of_match = at91_pmc_match,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080026};
27
Wenyou Yang6cadaa02016-09-27 11:00:29 +080028/*---------------------------------------------------------*/
29
Wenyou Yang9e5935c2016-07-20 17:55:12 +080030int at91_pmc_core_probe(struct udevice *dev)
31{
32 struct pmc_platdata *plat = dev_get_platdata(dev);
33
34 dev = dev_get_parent(dev);
35
36 plat->reg_base = (struct at91_pmc *)dev_get_addr_ptr(dev);
37
38 return 0;
39}
40
Wenyou Yang6cadaa02016-09-27 11:00:29 +080041/**
42 * at91_clk_sub_device_bind() - for the at91 clock driver
43 * Recursively bind its children as clk devices.
44 *
45 * @return: 0 on success, or negative error code on failure
46 */
47int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
Wenyou Yang9e5935c2016-07-20 17:55:12 +080048{
49 const void *fdt = gd->fdt_blob;
50 int offset = dev->of_offset;
Wenyou Yang6cadaa02016-09-27 11:00:29 +080051 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Wenyou Yang9e5935c2016-07-20 17:55:12 +080052 const char *name;
53 int ret;
54
55 for (offset = fdt_first_subnode(fdt, offset);
56 offset > 0;
57 offset = fdt_next_subnode(fdt, offset)) {
Wenyou Yang6cadaa02016-09-27 11:00:29 +080058 if (pre_reloc_only &&
59 !fdt_getprop(fdt, offset, "u-boot,dm-pre-reloc", NULL))
60 continue;
61 /*
62 * If this node has "compatible" property, this is not
63 * a clock sub-node, but a normal device. skip.
64 */
65 fdt_get_property(fdt, offset, "compatible", &ret);
66 if (ret >= 0)
67 continue;
68
69 if (ret != -FDT_ERR_NOTFOUND)
70 return ret;
71
Wenyou Yang9e5935c2016-07-20 17:55:12 +080072 name = fdt_get_name(fdt, offset, NULL);
73 if (!name)
74 return -EINVAL;
Wenyou Yang6cadaa02016-09-27 11:00:29 +080075 ret = device_bind_driver_to_node(dev, drv_name, name,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080076 offset, NULL);
77 if (ret)
78 return ret;
79 }
80
81 return 0;
82}
83
Wenyou Yang6cadaa02016-09-27 11:00:29 +080084int at91_clk_of_xlate(struct clk *clk, struct fdtdec_phandle_args *args)
85{
86 int periph;
87
88 if (args->args_count) {
89 debug("Invalid args_count: %d\n", args->args_count);
90 return -EINVAL;
91 }
92
93 periph = fdtdec_get_uint(gd->fdt_blob, clk->dev->of_offset, "reg", -1);
94 if (periph < 0)
95 return -EINVAL;
96
97 clk->id = periph;
98
99 return 0;
100}
101
102int at91_clk_probe(struct udevice *dev)
103{
104 struct udevice *dev_periph_container, *dev_pmc;
105 struct pmc_platdata *plat = dev_get_platdata(dev);
106
107 dev_periph_container = dev_get_parent(dev);
108 dev_pmc = dev_get_parent(dev_periph_container);
109
110 plat->reg_base = (struct at91_pmc *)dev_get_addr_ptr(dev_pmc);
111
112 return 0;
113}