blob: 18f52d26df2e06c9d3223bf5a1817c878f25083f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass599447c2014-10-04 11:29:37 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass599447c2014-10-04 11:29:37 -06004 */
5
6#include <common.h>
Bin Meng1a43b232021-03-14 20:15:02 +08007#include <asm/global_data.h>
Simon Glass599447c2014-10-04 11:29:37 -06008#include <dm.h>
Simon Glass7db06832020-12-19 10:40:03 -07009#include <dm/simple_bus.h>
Bin Meng1a43b232021-03-14 20:15:02 +080010#include <fdt_support.h>
11
12DECLARE_GLOBAL_DATA_PTR;
Simon Glass59b61962015-07-07 20:53:44 -060013
14fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr)
15{
Simon Glass71fa5b42020-12-03 16:55:18 -070016 struct simple_bus_plat *plat = dev_get_uclass_plat(dev);
Simon Glass59b61962015-07-07 20:53:44 -060017
18 if (addr >= plat->base && addr < plat->base + plat->size)
19 addr = (addr - plat->base) + plat->target;
20
21 return addr;
22}
23
Simon Glass599447c2014-10-04 11:29:37 -060024static int simple_bus_post_bind(struct udevice *dev)
25{
Kever Yang22d088c2017-01-18 20:25:44 +080026#if CONFIG_IS_ENABLED(OF_PLATDATA)
27 return 0;
28#else
Bin Meng1a43b232021-03-14 20:15:02 +080029 struct simple_bus_plat *plat = dev_get_uclass_plat(dev);
Simon Glass59b61962015-07-07 20:53:44 -060030 int ret;
31
Bin Meng1a43b232021-03-14 20:15:02 +080032 if (CONFIG_IS_ENABLED(SIMPLE_BUS_CORRECT_RANGE)) {
33 uint64_t caddr, paddr, len;
34
35 /* only read range index 0 */
36 ret = fdt_read_range((void *)gd->fdt_blob, dev_of_offset(dev),
37 0, &caddr, &paddr, &len);
38 if (!ret) {
39 plat->base = caddr;
40 plat->target = paddr;
41 plat->size = len;
42 }
43 } else {
44 u32 cell[3];
Simon Glass59b61962015-07-07 20:53:44 -060045
Bin Meng1a43b232021-03-14 20:15:02 +080046 ret = dev_read_u32_array(dev, "ranges", cell,
47 ARRAY_SIZE(cell));
48 if (!ret) {
49 plat->base = cell[0];
50 plat->target = cell[1];
51 plat->size = cell[2];
52 }
Simon Glass59b61962015-07-07 20:53:44 -060053 }
54
Simon Glass09128c52016-07-05 17:10:09 -060055 return dm_scan_fdt_dev(dev);
Kever Yang22d088c2017-01-18 20:25:44 +080056#endif
Simon Glass599447c2014-10-04 11:29:37 -060057}
58
59UCLASS_DRIVER(simple_bus) = {
60 .id = UCLASS_SIMPLE_BUS,
61 .name = "simple_bus",
62 .post_bind = simple_bus_post_bind,
Simon Glass71fa5b42020-12-03 16:55:18 -070063 .per_device_plat_auto = sizeof(struct simple_bus_plat),
Simon Glass599447c2014-10-04 11:29:37 -060064};
65
Simon Glass76404d82020-12-22 19:30:24 -070066#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass599447c2014-10-04 11:29:37 -060067static const struct udevice_id generic_simple_bus_ids[] = {
68 { .compatible = "simple-bus" },
Masahiro Yamada4fe976c2016-03-01 11:51:48 +090069 { .compatible = "simple-mfd" },
Simon Glass599447c2014-10-04 11:29:37 -060070 { }
71};
Simon Glass76404d82020-12-22 19:30:24 -070072#endif
Simon Glass599447c2014-10-04 11:29:37 -060073
Walter Lozano2901ac62020-06-25 01:10:04 -030074U_BOOT_DRIVER(simple_bus) = {
75 .name = "simple_bus",
Simon Glass599447c2014-10-04 11:29:37 -060076 .id = UCLASS_SIMPLE_BUS,
Simon Glass76404d82020-12-22 19:30:24 -070077 .of_match = of_match_ptr(generic_simple_bus_ids),
Lukas Auer54310eb2019-04-10 14:46:07 +020078 .flags = DM_FLAG_PRE_RELOC,
Simon Glass599447c2014-10-04 11:29:37 -060079};