blob: fcd693a2f6fc6904d7ca1f6a388e60e60f9f94dd [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>
Heiko Stübner27326c72017-02-18 19:46:21 +010013#include <dm/util.h>
Wenyou Yang9e5935c2016-07-20 17:55:12 +080014#include "pmc.h"
15
16DECLARE_GLOBAL_DATA_PTR;
17
Wenyou Yang9e5935c2016-07-20 17:55:12 +080018static const struct udevice_id at91_pmc_match[] = {
19 { .compatible = "atmel,sama5d2-pmc" },
20 {}
21};
22
23U_BOOT_DRIVER(at91_pmc) = {
Wenyou Yangb892b052016-09-13 10:25:55 +080024 .name = "at91-pmc",
25 .id = UCLASS_SIMPLE_BUS,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080026 .of_match = at91_pmc_match,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080027};
28
Wenyou Yang6cadaa02016-09-27 11:00:29 +080029/*---------------------------------------------------------*/
30
Wenyou Yang9e5935c2016-07-20 17:55:12 +080031int at91_pmc_core_probe(struct udevice *dev)
32{
33 struct pmc_platdata *plat = dev_get_platdata(dev);
34
35 dev = dev_get_parent(dev);
36
37 plat->reg_base = (struct at91_pmc *)dev_get_addr_ptr(dev);
38
39 return 0;
40}
41
Wenyou Yang6cadaa02016-09-27 11:00:29 +080042/**
43 * at91_clk_sub_device_bind() - for the at91 clock driver
44 * Recursively bind its children as clk devices.
45 *
46 * @return: 0 on success, or negative error code on failure
47 */
48int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
Wenyou Yang9e5935c2016-07-20 17:55:12 +080049{
50 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -070051 int offset = dev_of_offset(dev);
Wenyou Yang6cadaa02016-09-27 11:00:29 +080052 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Wenyou Yang9e5935c2016-07-20 17:55:12 +080053 const char *name;
54 int ret;
55
56 for (offset = fdt_first_subnode(fdt, offset);
57 offset > 0;
58 offset = fdt_next_subnode(fdt, offset)) {
Wenyou Yang6cadaa02016-09-27 11:00:29 +080059 if (pre_reloc_only &&
Heiko Stübner27326c72017-02-18 19:46:21 +010060 !dm_fdt_pre_reloc(fdt, offset))
Wenyou Yang6cadaa02016-09-27 11:00:29 +080061 continue;
62 /*
63 * If this node has "compatible" property, this is not
64 * a clock sub-node, but a normal device. skip.
65 */
66 fdt_get_property(fdt, offset, "compatible", &ret);
67 if (ret >= 0)
68 continue;
69
70 if (ret != -FDT_ERR_NOTFOUND)
71 return ret;
72
Wenyou Yang9e5935c2016-07-20 17:55:12 +080073 name = fdt_get_name(fdt, offset, NULL);
74 if (!name)
75 return -EINVAL;
Wenyou Yang6cadaa02016-09-27 11:00:29 +080076 ret = device_bind_driver_to_node(dev, drv_name, name,
Wenyou Yang9e5935c2016-07-20 17:55:12 +080077 offset, NULL);
78 if (ret)
79 return ret;
80 }
81
82 return 0;
83}
84
Wenyou Yang6cadaa02016-09-27 11:00:29 +080085int at91_clk_of_xlate(struct clk *clk, struct fdtdec_phandle_args *args)
86{
87 int periph;
88
89 if (args->args_count) {
90 debug("Invalid args_count: %d\n", args->args_count);
91 return -EINVAL;
92 }
93
Simon Glasse160f7d2017-01-17 16:52:55 -070094 periph = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(clk->dev), "reg",
95 -1);
Wenyou Yang6cadaa02016-09-27 11:00:29 +080096 if (periph < 0)
97 return -EINVAL;
98
99 clk->id = periph;
100
101 return 0;
102}
103
104int at91_clk_probe(struct udevice *dev)
105{
106 struct udevice *dev_periph_container, *dev_pmc;
107 struct pmc_platdata *plat = dev_get_platdata(dev);
108
109 dev_periph_container = dev_get_parent(dev);
110 dev_pmc = dev_get_parent(dev_periph_container);
111
112 plat->reg_base = (struct at91_pmc *)dev_get_addr_ptr(dev_pmc);
113
114 return 0;
115}