Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SIMPLE_BUS |
| 7 | |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 8 | #include <common.h> |
Bin Meng | 80279fa | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 6563b20 | 2020-12-19 10:40:03 -0700 | [diff] [blame] | 11 | #include <dm/simple_bus.h> |
Bin Meng | 80279fa | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 12 | #include <fdt_support.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | f330177 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 15 | |
| 16 | fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr) |
| 17 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 18 | struct simple_bus_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | f330177 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 19 | |
| 20 | if (addr >= plat->base && addr < plat->base + plat->size) |
| 21 | addr = (addr - plat->base) + plat->target; |
| 22 | |
| 23 | return addr; |
| 24 | } |
| 25 | |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 26 | static int simple_bus_post_bind(struct udevice *dev) |
| 27 | { |
Kever Yang | 6943ee1 | 2017-01-18 20:25:44 +0800 | [diff] [blame] | 28 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 29 | return 0; |
| 30 | #else |
Bin Meng | 80279fa | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 31 | struct simple_bus_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | f330177 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 32 | int ret; |
| 33 | |
Bin Meng | 80279fa | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 34 | if (CONFIG_IS_ENABLED(SIMPLE_BUS_CORRECT_RANGE)) { |
| 35 | uint64_t caddr, paddr, len; |
Simon Glass | f330177 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 36 | |
Bin Meng | 80279fa | 2021-03-14 20:15:02 +0800 | [diff] [blame] | 37 | /* only read range index 0 */ |
| 38 | ret = fdt_read_range((void *)gd->fdt_blob, dev_of_offset(dev), |
| 39 | 0, &caddr, &paddr, &len); |
| 40 | if (!ret) { |
| 41 | plat->base = caddr; |
| 42 | plat->target = paddr; |
| 43 | plat->size = len; |
| 44 | } |
| 45 | } else { |
| 46 | u32 cell[3]; |
| 47 | |
| 48 | ret = dev_read_u32_array(dev, "ranges", cell, |
| 49 | ARRAY_SIZE(cell)); |
| 50 | if (!ret) { |
| 51 | plat->base = cell[0]; |
| 52 | plat->target = cell[1]; |
| 53 | plat->size = cell[2]; |
| 54 | } |
Simon Glass | f330177 | 2015-07-07 20:53:44 -0600 | [diff] [blame] | 55 | } |
| 56 | |
Simon Glass | 2e3f1ff | 2016-07-05 17:10:09 -0600 | [diff] [blame] | 57 | return dm_scan_fdt_dev(dev); |
Kever Yang | 6943ee1 | 2017-01-18 20:25:44 +0800 | [diff] [blame] | 58 | #endif |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | UCLASS_DRIVER(simple_bus) = { |
| 62 | .id = UCLASS_SIMPLE_BUS, |
| 63 | .name = "simple_bus", |
| 64 | .post_bind = simple_bus_post_bind, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 65 | .per_device_plat_auto = sizeof(struct simple_bus_plat), |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 66 | }; |
| 67 | |
Simon Glass | 9539738 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 68 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 69 | static const struct udevice_id generic_simple_bus_ids[] = { |
| 70 | { .compatible = "simple-bus" }, |
Masahiro Yamada | 9f56917 | 2016-03-01 11:51:48 +0900 | [diff] [blame] | 71 | { .compatible = "simple-mfd" }, |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 72 | { } |
| 73 | }; |
Simon Glass | 79ea8f7 | 2020-12-22 19:30:24 -0700 | [diff] [blame] | 74 | #endif |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 75 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 76 | U_BOOT_DRIVER(simple_bus) = { |
| 77 | .name = "simple_bus", |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 78 | .id = UCLASS_SIMPLE_BUS, |
Simon Glass | 79ea8f7 | 2020-12-22 19:30:24 -0700 | [diff] [blame] | 79 | .of_match = of_match_ptr(generic_simple_bus_ids), |
Lukas Auer | 3a7c45f | 2019-04-10 14:46:07 +0200 | [diff] [blame] | 80 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | da802b9 | 2014-10-04 11:29:37 -0600 | [diff] [blame] | 81 | }; |