Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Bin Meng | 1a43b23 | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 7 | #include <asm/global_data.h> |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 7db0683 | 2020-12-19 10:40:03 -0700 | [diff] [blame] | 9 | #include <dm/simple_bus.h> |
Bin Meng | 1a43b23 | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 10 | #include <fdt_support.h> |
| 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 59b6196 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 13 | |
| 14 | fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr) |
| 15 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 16 | struct simple_bus_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | 59b6196 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 17 | |
| 18 | if (addr >= plat->base && addr < plat->base + plat->size) |
| 19 | addr = (addr - plat->base) + plat->target; |
| 20 | |
| 21 | return addr; |
| 22 | } |
| 23 | |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 24 | static int simple_bus_post_bind(struct udevice *dev) |
| 25 | { |
Kever Yang | 22d088c | 2017-01-18 20:25:44 +0800 | [diff] [blame] | 26 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 27 | return 0; |
| 28 | #else |
Bin Meng | 1a43b23 | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 29 | struct simple_bus_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | 59b6196 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 30 | int ret; |
| 31 | |
Bin Meng | 1a43b23 | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 32 | 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 Glass | 59b6196 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 45 | |
Bin Meng | 1a43b23 | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 46 | 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 Glass | 59b6196 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 53 | } |
| 54 | |
Simon Glass | 09128c5 | 2016-07-05 17:10:09 -0600 | [diff] [blame] | 55 | return dm_scan_fdt_dev(dev); |
Kever Yang | 22d088c | 2017-01-18 20:25:44 +0800 | [diff] [blame] | 56 | #endif |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | UCLASS_DRIVER(simple_bus) = { |
| 60 | .id = UCLASS_SIMPLE_BUS, |
| 61 | .name = "simple_bus", |
| 62 | .post_bind = simple_bus_post_bind, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 63 | .per_device_plat_auto = sizeof(struct simple_bus_plat), |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 64 | }; |
| 65 | |
Simon Glass | 76404d8 | 2020-12-22 19:30:24 -0700 | [diff] [blame] | 66 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 67 | static const struct udevice_id generic_simple_bus_ids[] = { |
| 68 | { .compatible = "simple-bus" }, |
Masahiro Yamada | 4fe976c | 2016-03-01 11:51:48 +0900 | [diff] [blame] | 69 | { .compatible = "simple-mfd" }, |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 70 | { } |
| 71 | }; |
Simon Glass | 76404d8 | 2020-12-22 19:30:24 -0700 | [diff] [blame] | 72 | #endif |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 73 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 74 | U_BOOT_DRIVER(simple_bus) = { |
| 75 | .name = "simple_bus", |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 76 | .id = UCLASS_SIMPLE_BUS, |
Simon Glass | 76404d8 | 2020-12-22 19:30:24 -0700 | [diff] [blame] | 77 | .of_match = of_match_ptr(generic_simple_bus_ids), |
Lukas Auer | 54310eb | 2019-04-10 14:46:07 +0200 | [diff] [blame] | 78 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | 599447c | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 79 | }; |