Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <mapmem.h> |
| 13 | #include <regmap.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 15 | #include <dm/of_addr.h> |
| 16 | #include <linux/ioport.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 17 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 20 | static struct regmap *regmap_alloc(int count) |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 21 | { |
| 22 | struct regmap *map; |
| 23 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 24 | map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count); |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 25 | if (!map) |
| 26 | return NULL; |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 27 | map->range_count = count; |
| 28 | |
| 29 | return map; |
| 30 | } |
| 31 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 32 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 33 | int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 34 | struct regmap **mapp) |
| 35 | { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 36 | struct regmap_range *range; |
| 37 | struct regmap *map; |
| 38 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 39 | map = regmap_alloc(count); |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 40 | if (!map) |
| 41 | return -ENOMEM; |
| 42 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 43 | for (range = map->ranges; count > 0; reg += 2, range++, count--) { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 44 | range->start = *reg; |
| 45 | range->size = reg[1]; |
| 46 | } |
| 47 | |
| 48 | *mapp = map; |
| 49 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | #else |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 53 | int regmap_init_mem(ofnode node, struct regmap **mapp) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 54 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 55 | struct regmap_range *range; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 56 | struct regmap *map; |
| 57 | int count; |
| 58 | int addr_len, size_len, both_len; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 59 | int len; |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 60 | int index; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 61 | struct resource r; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 62 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 63 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
| 64 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 65 | both_len = addr_len + size_len; |
| 66 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 67 | len = ofnode_read_size(node, "reg"); |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 68 | if (len < 0) |
| 69 | return len; |
| 70 | len /= sizeof(fdt32_t); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 71 | count = len / both_len; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 72 | if (!count) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 73 | return -EINVAL; |
| 74 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 75 | map = regmap_alloc(count); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 76 | if (!map) |
| 77 | return -ENOMEM; |
| 78 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 79 | for (range = map->ranges, index = 0; count > 0; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 80 | count--, range++, index++) { |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 81 | fdt_size_t sz; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 82 | if (of_live_active()) { |
| 83 | of_address_to_resource(ofnode_to_np(node), index, &r); |
| 84 | range->start = r.start; |
| 85 | range->size = r.end - r.start + 1; |
| 86 | } else { |
| 87 | range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 88 | ofnode_to_offset(node), "reg", index, |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 89 | addr_len, size_len, &sz, true); |
| 90 | range->size = sz; |
| 91 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | *mapp = map; |
| 95 | |
| 96 | return 0; |
| 97 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 98 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 99 | |
| 100 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 101 | { |
| 102 | struct regmap_range *range; |
| 103 | |
| 104 | if (range_num >= map->range_count) |
| 105 | return NULL; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 106 | range = &map->ranges[range_num]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 107 | |
| 108 | return map_sysmem(range->start, range->size); |
| 109 | } |
| 110 | |
| 111 | int regmap_uninit(struct regmap *map) |
| 112 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 113 | free(map); |
| 114 | |
| 115 | return 0; |
| 116 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 117 | |
| 118 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
| 119 | { |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 120 | u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 121 | |
| 122 | *valp = le32_to_cpu(readl(ptr)); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 128 | { |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 129 | u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 130 | |
| 131 | writel(cpu_to_le32(val), ptr); |
| 132 | |
| 133 | return 0; |
| 134 | } |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame^] | 135 | |
| 136 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) |
| 137 | { |
| 138 | uint reg; |
| 139 | int ret; |
| 140 | |
| 141 | ret = regmap_read(map, offset, ®); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | reg &= ~mask; |
| 146 | |
| 147 | return regmap_write(map, offset, reg | val); |
| 148 | } |