blob: 77f6f520a0657fd50787ab2f5e22d32bba772c68 [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));
70 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Simon Glassad8f8ab2015-06-23 15:38:42 -060071 both_len = addr_len + size_len;
72
Masahiro Yamadae4873e32018-04-19 12:14:03 +090073 len = ofnode_read_size(node, "reg");
Simon Glasseeeb5192017-05-18 20:09:10 -060074 if (len < 0)
75 return len;
76 len /= sizeof(fdt32_t);
Simon Glassad8f8ab2015-06-23 15:38:42 -060077 count = len / both_len;
Simon Glasseeeb5192017-05-18 20:09:10 -060078 if (!count)
Simon Glassad8f8ab2015-06-23 15:38:42 -060079 return -EINVAL;
80
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090081 map = regmap_alloc(count);
Simon Glassad8f8ab2015-06-23 15:38:42 -060082 if (!map)
83 return -ENOMEM;
84
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090085 for (range = map->ranges, index = 0; count > 0;
Simon Glasseeeb5192017-05-18 20:09:10 -060086 count--, range++, index++) {
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +010087 fdt_size_t sz;
Simon Glasseeeb5192017-05-18 20:09:10 -060088 if (of_live_active()) {
89 of_address_to_resource(ofnode_to_np(node), index, &r);
90 range->start = r.start;
91 range->size = r.end - r.start + 1;
92 } else {
93 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
Masahiro Yamadae4873e32018-04-19 12:14:03 +090094 ofnode_to_offset(node), "reg", index,
Simon Glasseeeb5192017-05-18 20:09:10 -060095 addr_len, size_len, &sz, true);
96 range->size = sz;
97 }
Simon Glassad8f8ab2015-06-23 15:38:42 -060098 }
99
100 *mapp = map;
101
102 return 0;
103}
Simon Glassb9443452016-07-04 11:57:59 -0600104#endif
Simon Glassad8f8ab2015-06-23 15:38:42 -0600105
106void *regmap_get_range(struct regmap *map, unsigned int range_num)
107{
108 struct regmap_range *range;
109
110 if (range_num >= map->range_count)
111 return NULL;
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900112 range = &map->ranges[range_num];
Simon Glassad8f8ab2015-06-23 15:38:42 -0600113
114 return map_sysmem(range->start, range->size);
115}
116
117int regmap_uninit(struct regmap *map)
118{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600119 free(map);
120
121 return 0;
122}
Paul Burton39776032016-09-08 07:47:35 +0100123
124int regmap_read(struct regmap *map, uint offset, uint *valp)
125{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900126 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100127
128 *valp = le32_to_cpu(readl(ptr));
129
130 return 0;
131}
132
133int regmap_write(struct regmap *map, uint offset, uint val)
134{
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900135 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
Paul Burton39776032016-09-08 07:47:35 +0100136
137 writel(cpu_to_le32(val), ptr);
138
139 return 0;
140}
Neil Armstrong5444ec62018-04-27 11:56:14 +0200141
142int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
143{
144 uint reg;
145 int ret;
146
147 ret = regmap_read(map, offset, &reg);
148 if (ret)
149 return ret;
150
151 reg &= ~mask;
152
153 return regmap_write(map, offset, reg | val);
154}