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 | |
Mario Six | 0aa5299 | 2018-10-04 09:00:42 +0200 | [diff] [blame] | 20 | /** |
| 21 | * regmap_alloc() - Allocate a regmap with a given number of ranges. |
| 22 | * |
| 23 | * @count: Number of ranges to be allocated for the regmap. |
| 24 | * Return: A pointer to the newly allocated regmap, or NULL on error. |
| 25 | */ |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 26 | static struct regmap *regmap_alloc(int count) |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 27 | { |
| 28 | struct regmap *map; |
| 29 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 30 | map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count); |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 31 | if (!map) |
| 32 | return NULL; |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 33 | map->range_count = count; |
| 34 | |
| 35 | return map; |
| 36 | } |
| 37 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 38 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 39 | 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] | 40 | struct regmap **mapp) |
| 41 | { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 42 | struct regmap_range *range; |
| 43 | struct regmap *map; |
| 44 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 45 | map = regmap_alloc(count); |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 46 | if (!map) |
| 47 | return -ENOMEM; |
| 48 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 49 | for (range = map->ranges; count > 0; reg += 2, range++, count--) { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 50 | range->start = *reg; |
| 51 | range->size = reg[1]; |
| 52 | } |
| 53 | |
| 54 | *mapp = map; |
| 55 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | #else |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 59 | int regmap_init_mem(ofnode node, struct regmap **mapp) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 60 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 61 | struct regmap_range *range; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 62 | struct regmap *map; |
| 63 | int count; |
| 64 | int addr_len, size_len, both_len; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 65 | int len; |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 66 | int index; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 67 | struct resource r; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 68 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 69 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 70 | if (addr_len < 0) { |
| 71 | debug("%s: Error while reading the addr length (ret = %d)\n", |
| 72 | ofnode_get_name(node), addr_len); |
| 73 | return addr_len; |
| 74 | } |
| 75 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 76 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 77 | if (size_len < 0) { |
| 78 | debug("%s: Error while reading the size length: (ret = %d)\n", |
| 79 | ofnode_get_name(node), size_len); |
| 80 | return size_len; |
| 81 | } |
| 82 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 83 | both_len = addr_len + size_len; |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 84 | if (!both_len) { |
| 85 | debug("%s: Both addr and size length are zero\n", |
| 86 | ofnode_get_name(node)); |
| 87 | return -EINVAL; |
| 88 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 89 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 90 | len = ofnode_read_size(node, "reg"); |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 91 | if (len < 0) |
| 92 | return len; |
| 93 | len /= sizeof(fdt32_t); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 94 | count = len / both_len; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 95 | if (!count) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 96 | return -EINVAL; |
| 97 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 98 | map = regmap_alloc(count); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 99 | if (!map) |
| 100 | return -ENOMEM; |
| 101 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 102 | for (range = map->ranges, index = 0; count > 0; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 103 | count--, range++, index++) { |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 104 | fdt_size_t sz; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 105 | if (of_live_active()) { |
| 106 | of_address_to_resource(ofnode_to_np(node), index, &r); |
| 107 | range->start = r.start; |
| 108 | range->size = r.end - r.start + 1; |
| 109 | } else { |
| 110 | range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 111 | ofnode_to_offset(node), "reg", index, |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 112 | addr_len, size_len, &sz, true); |
| 113 | range->size = sz; |
| 114 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | *mapp = map; |
| 118 | |
| 119 | return 0; |
| 120 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 121 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 122 | |
| 123 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 124 | { |
| 125 | struct regmap_range *range; |
| 126 | |
| 127 | if (range_num >= map->range_count) |
| 128 | return NULL; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 129 | range = &map->ranges[range_num]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 130 | |
| 131 | return map_sysmem(range->start, range->size); |
| 132 | } |
| 133 | |
| 134 | int regmap_uninit(struct regmap *map) |
| 135 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 136 | free(map); |
| 137 | |
| 138 | return 0; |
| 139 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 140 | |
| 141 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
| 142 | { |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 143 | u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 144 | |
| 145 | *valp = le32_to_cpu(readl(ptr)); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 151 | { |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 152 | u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 153 | |
| 154 | writel(cpu_to_le32(val), ptr); |
| 155 | |
| 156 | return 0; |
| 157 | } |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 158 | |
| 159 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) |
| 160 | { |
| 161 | uint reg; |
| 162 | int ret; |
| 163 | |
| 164 | ret = regmap_read(map, offset, ®); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | reg &= ~mask; |
| 169 | |
| 170 | return regmap_write(map, offset, reg | val); |
| 171 | } |