blob: f402bb5d6748f3c9b492da78a0538d4e22c334fb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassda802b92014-10-04 11:29:37 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassda802b92014-10-04 11:29:37 -06004 */
5
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_SIMPLE_BUS
7
Bin Meng80279fa2021-03-14 20:15:02 +08008#include <asm/global_data.h>
Simon Glassda802b92014-10-04 11:29:37 -06009#include <dm.h>
Simon Glass6563b202020-12-19 10:40:03 -070010#include <dm/simple_bus.h>
Bin Meng80279fa2021-03-14 20:15:02 +080011#include <fdt_support.h>
12
13DECLARE_GLOBAL_DATA_PTR;
Simon Glassf3301772015-07-07 20:53:44 -060014
15fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr)
16{
Simon Glasscaa4daa2020-12-03 16:55:18 -070017 struct simple_bus_plat *plat = dev_get_uclass_plat(dev);
Simon Glassf3301772015-07-07 20:53:44 -060018
19 if (addr >= plat->base && addr < plat->base + plat->size)
20 addr = (addr - plat->base) + plat->target;
21
22 return addr;
23}
24
Simon Glassda802b92014-10-04 11:29:37 -060025static int simple_bus_post_bind(struct udevice *dev)
26{
Kever Yang6943ee12017-01-18 20:25:44 +080027#if CONFIG_IS_ENABLED(OF_PLATDATA)
28 return 0;
29#else
Bin Meng80279fa2021-03-14 20:15:02 +080030 struct simple_bus_plat *plat = dev_get_uclass_plat(dev);
Simon Glassf3301772015-07-07 20:53:44 -060031 int ret;
32
Bin Meng80279fa2021-03-14 20:15:02 +080033 if (CONFIG_IS_ENABLED(SIMPLE_BUS_CORRECT_RANGE)) {
34 uint64_t caddr, paddr, len;
Simon Glassf3301772015-07-07 20:53:44 -060035
Bin Meng80279fa2021-03-14 20:15:02 +080036 /* only read range index 0 */
37 ret = fdt_read_range((void *)gd->fdt_blob, dev_of_offset(dev),
38 0, &caddr, &paddr, &len);
39 if (!ret) {
40 plat->base = caddr;
41 plat->target = paddr;
42 plat->size = len;
43 }
44 } else {
45 u32 cell[3];
46
47 ret = dev_read_u32_array(dev, "ranges", cell,
48 ARRAY_SIZE(cell));
49 if (!ret) {
50 plat->base = cell[0];
51 plat->target = cell[1];
52 plat->size = cell[2];
53 }
Simon Glassf3301772015-07-07 20:53:44 -060054 }
55
Simon Glass2e3f1ff2016-07-05 17:10:09 -060056 return dm_scan_fdt_dev(dev);
Kever Yang6943ee12017-01-18 20:25:44 +080057#endif
Simon Glassda802b92014-10-04 11:29:37 -060058}
59
60UCLASS_DRIVER(simple_bus) = {
61 .id = UCLASS_SIMPLE_BUS,
62 .name = "simple_bus",
63 .post_bind = simple_bus_post_bind,
Simon Glasscaa4daa2020-12-03 16:55:18 -070064 .per_device_plat_auto = sizeof(struct simple_bus_plat),
Simon Glassda802b92014-10-04 11:29:37 -060065};
66
Simon Glass95397382021-08-07 07:24:04 -060067#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassda802b92014-10-04 11:29:37 -060068static const struct udevice_id generic_simple_bus_ids[] = {
69 { .compatible = "simple-bus" },
Masahiro Yamada9f569172016-03-01 11:51:48 +090070 { .compatible = "simple-mfd" },
Simon Glassda802b92014-10-04 11:29:37 -060071 { }
72};
Simon Glass79ea8f72020-12-22 19:30:24 -070073#endif
Simon Glassda802b92014-10-04 11:29:37 -060074
Walter Lozanoe3e24702020-06-25 01:10:04 -030075U_BOOT_DRIVER(simple_bus) = {
76 .name = "simple_bus",
Simon Glassda802b92014-10-04 11:29:37 -060077 .id = UCLASS_SIMPLE_BUS,
Simon Glass79ea8f72020-12-22 19:30:24 -070078 .of_match = of_match_ptr(generic_simple_bus_ids),
Lukas Auer3a7c45f2019-04-10 14:46:07 +020079 .flags = DM_FLAG_PRE_RELOC,
Simon Glassda802b92014-10-04 11:29:37 -060080};