blob: 1eed94e47a32d4eaa67e01a6ba22d05f5951ffe7 [file] [log] [blame]
Simon Glass6f98b752015-06-23 15:38:42 -06001/*
2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#ifndef __REGMAP_H
9#define __REGMAP_H
10
11/**
12 * struct regmap_range - a register map range
13 *
14 * @start: Start address
15 * @size: Size in bytes
16 */
17struct regmap_range {
18 ulong start;
19 ulong size;
20};
21
22/**
23 * struct regmap - a way of accessing hardware/bus registers
24 *
25 * @base: Base address of register map
26 * @range_count: Number of ranges available within the map
27 * @range: Pointer to the list of ranges, allocated if @range_count > 1
28 * @base_range: If @range_count is <= 1, @range points here
29 */
30struct regmap {
31 phys_addr_t base;
32 int range_count;
33 struct regmap_range *range, base_range;
34};
35
36/*
37 * Interface to provide access to registers either through a direct memory
38 * bus or through a peripheral bus like I2C, SPI.
39 */
40int regmap_write(struct regmap *map, uint offset, uint val);
41int regmap_read(struct regmap *map, uint offset, uint *valp);
42
43#define regmap_write32(map, ptr, member, val) \
44 regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val)
45
46#define regmap_read32(map, ptr, member, valp) \
47 regmap_read(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), valp)
48
49/**
50 * regmap_init_mem() - Set up a new register map that uses memory access
51 *
52 * Use regmap_uninit() to free it.
53 *
54 * @dev: Device that uses this map
55 * @mapp: Returns allocated map
56 */
57int regmap_init_mem(struct udevice *dev, struct regmap **mapp);
58
Simon Glass1e6ca1a2016-07-04 11:58:22 -060059/**
60 * regmap_init_mem_platdata() - Set up a new memory register map for of-platdata
61 *
62 * This creates a new regmap with a list of regions passed in, rather than
63 * using the device tree. It only supports 32-bit machines.
64 *
65 * Use regmap_uninit() to free it.
66 *
67 * @dev: Device that uses this map
68 * @reg: List of address, size pairs
69 * @count: Number of pairs (e.g. 1 if the regmap has a single entry)
70 * @mapp: Returns allocated map
71 */
72int regmap_init_mem_platdata(struct udevice *dev, u32 *reg, int count,
Simon Glass3b2a29e2016-07-04 11:57:59 -060073 struct regmap **mapp);
74
Simon Glass6f98b752015-06-23 15:38:42 -060075/**
76 * regmap_get_range() - Obtain the base memory address of a regmap range
77 *
78 * @map: Regmap to query
79 * @range_num: Range to look up
80 */
81void *regmap_get_range(struct regmap *map, unsigned int range_num);
82
83/**
84 * regmap_uninit() - free a previously inited regmap
85 */
86int regmap_uninit(struct regmap *map);
87
88#endif