blob: 4ebab23349081f892323067c918cb834cc9a62c6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassad8f8ab2015-06-23 15:38:42 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassad8f8ab2015-06-23 15:38:42 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Simon Glassad8f8ab2015-06-23 15:38:42 -060011#include <malloc.h>
12#include <mapmem.h>
13#include <regmap.h>
Paul Burton39776032016-09-08 07:47:35 +010014#include <asm/io.h>
Simon Glasseeeb5192017-05-18 20:09:10 -060015#include <dm/of_addr.h>
16#include <linux/ioport.h>
Paul Burton39776032016-09-08 07:47:35 +010017
Simon Glassad8f8ab2015-06-23 15:38:42 -060018DECLARE_GLOBAL_DATA_PTR;
19
Mario Six0aa52992018-10-04 09:00:42 +020020/**
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 Yamada54c5ecb2018-04-19 12:14:01 +090026static struct regmap *regmap_alloc(int count)
Simon Glass30d73e82016-07-04 11:58:21 -060027{
28 struct regmap *map;
29
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090030 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
Simon Glass30d73e82016-07-04 11:58:21 -060031 if (!map)
32 return NULL;
Simon Glass30d73e82016-07-04 11:58:21 -060033 map->range_count = count;
34
35 return map;
36}
37
Simon Glassb9443452016-07-04 11:57:59 -060038#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1b1fe412017-08-29 14:15:50 -060039int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
Simon Glassb9443452016-07-04 11:57:59 -060040 struct regmap **mapp)
41{
Simon Glassb6114332016-07-04 11:58:22 -060042 struct regmap_range *range;
43 struct regmap *map;
44
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090045 map = regmap_alloc(count);
Simon Glassb6114332016-07-04 11:58:22 -060046 if (!map)
47 return -ENOMEM;
48
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090049 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
Simon Glassb6114332016-07-04 11:58:22 -060050 range->start = *reg;
51 range->size = reg[1];
52 }
53
54 *mapp = map;
55
Simon Glassb9443452016-07-04 11:57:59 -060056 return 0;
57}
58#else
Masahiro Yamadae4873e32018-04-19 12:14:03 +090059int regmap_init_mem(ofnode node, struct regmap **mapp)
Simon Glassad8f8ab2015-06-23 15:38:42 -060060{
Simon Glassad8f8ab2015-06-23 15:38:42 -060061 struct regmap_range *range;
Simon Glassad8f8ab2015-06-23 15:38:42 -060062 struct regmap *map;
63 int count;
64 int addr_len, size_len, both_len;
Simon Glassad8f8ab2015-06-23 15:38:42 -060065 int len;
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +010066 int index;
Simon Glasseeeb5192017-05-18 20:09:10 -060067 struct resource r;
Simon Glassad8f8ab2015-06-23 15:38:42 -060068
Masahiro Yamadae4873e32018-04-19 12:14:03 +090069 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +020070 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 Yamadae4873e32018-04-19 12:14:03 +090076 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +020077 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 Glassad8f8ab2015-06-23 15:38:42 -060083 both_len = addr_len + size_len;
Mario Six12321162018-10-04 09:00:43 +020084 if (!both_len) {
85 debug("%s: Both addr and size length are zero\n",
86 ofnode_get_name(node));
87 return -EINVAL;
88 }
Simon Glassad8f8ab2015-06-23 15:38:42 -060089
Masahiro Yamadae4873e32018-04-19 12:14:03 +090090 len = ofnode_read_size(node, "reg");
Simon Glasseeeb5192017-05-18 20:09:10 -060091 if (len < 0)
92 return len;
93 len /= sizeof(fdt32_t);
Simon Glassad8f8ab2015-06-23 15:38:42 -060094 count = len / both_len;
Simon Glasseeeb5192017-05-18 20:09:10 -060095 if (!count)
Simon Glassad8f8ab2015-06-23 15:38:42 -060096 return -EINVAL;
97
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090098 map = regmap_alloc(count);
Simon Glassad8f8ab2015-06-23 15:38:42 -060099 if (!map)
100 return -ENOMEM;
101
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900102 for (range = map->ranges, index = 0; count > 0;
Simon Glasseeeb5192017-05-18 20:09:10 -0600103 count--, range++, index++) {
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +0100104 fdt_size_t sz;
Simon Glasseeeb5192017-05-18 20:09:10 -0600105 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 Yamadae4873e32018-04-19 12:14:03 +0900111 ofnode_to_offset(node), "reg", index,
Simon Glasseeeb5192017-05-18 20:09:10 -0600112 addr_len, size_len, &sz, true);
113 range->size = sz;
114 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600115 }
116
117 *mapp = map;
118
119 return 0;
120}
Simon Glassb9443452016-07-04 11:57:59 -0600121#endif
Simon Glassad8f8ab2015-06-23 15:38:42 -0600122
123void *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 Yamada54c5ecb2018-04-19 12:14:01 +0900129 range = &map->ranges[range_num];
Simon Glassad8f8ab2015-06-23 15:38:42 -0600130
131 return map_sysmem(range->start, range->size);
132}
133
134int regmap_uninit(struct regmap *map)
135{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600136 free(map);
137
138 return 0;
139}
Paul Burton39776032016-09-08 07:47:35 +0100140
141int regmap_read(struct regmap *map, uint offset, uint *valp)
142{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900143 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100144
145 *valp = le32_to_cpu(readl(ptr));
146
147 return 0;
148}
149
150int regmap_write(struct regmap *map, uint offset, uint val)
151{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900152 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100153
154 writel(cpu_to_le32(val), ptr);
155
156 return 0;
157}
Neil Armstrong5444ec62018-04-27 11:56:14 +0200158
159int 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, &reg);
165 if (ret)
166 return ret;
167
168 reg &= ~mask;
169
170 return regmap_write(map, offset, reg | val);
171}