Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 6f98b75 | 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 | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __REGMAP_H |
| 8 | #define __REGMAP_H |
| 9 | |
| 10 | /** |
| 11 | * struct regmap_range - a register map range |
| 12 | * |
| 13 | * @start: Start address |
| 14 | * @size: Size in bytes |
| 15 | */ |
| 16 | struct regmap_range { |
| 17 | ulong start; |
| 18 | ulong size; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * struct regmap - a way of accessing hardware/bus registers |
| 23 | * |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 24 | * @range_count: Number of ranges available within the map |
| 25 | * @ranges: Array of ranges |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 26 | */ |
| 27 | struct regmap { |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 28 | int range_count; |
Masahiro Yamada | 8c1de5e | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 29 | struct regmap_range ranges[0]; |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * Interface to provide access to registers either through a direct memory |
| 34 | * bus or through a peripheral bus like I2C, SPI. |
| 35 | */ |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 36 | |
| 37 | /** |
| 38 | * regmap_write() - Write a 32-bit value to a regmap |
| 39 | * |
| 40 | * @map: Regmap to write to |
| 41 | * @offset: Offset in the regmap to write to |
| 42 | * @val: Data to write to the regmap at the specified offset |
| 43 | * |
| 44 | * Return: 0 if OK, -ve on error |
| 45 | */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 46 | int regmap_write(struct regmap *map, uint offset, uint val); |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 47 | |
| 48 | /** |
| 49 | * regmap_read() - Read a 32-bit value from a regmap |
| 50 | * |
| 51 | * @map: Regmap to read from |
| 52 | * @offset: Offset in the regmap to read from |
| 53 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 54 | * at the specified offset |
| 55 | * |
| 56 | * Return: 0 if OK, -ve on error |
| 57 | */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 58 | int regmap_read(struct regmap *map, uint offset, uint *valp); |
| 59 | |
| 60 | #define regmap_write32(map, ptr, member, val) \ |
| 61 | regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val) |
| 62 | |
| 63 | #define regmap_read32(map, ptr, member, valp) \ |
| 64 | regmap_read(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), valp) |
| 65 | |
| 66 | /** |
Neil Armstrong | 285cbcf | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 67 | * regmap_update_bits() - Perform a read/modify/write using a mask |
| 68 | * |
| 69 | * @map: The map returned by regmap_init_mem*() |
| 70 | * @offset: Offset of the memory |
| 71 | * @mask: Mask to apply to the read value |
| 72 | * @val: Value to apply to the value to write |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 73 | * Return: 0 if OK, -ve on error |
Neil Armstrong | 285cbcf | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 74 | */ |
| 75 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val); |
| 76 | |
| 77 | /** |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 78 | * regmap_init_mem() - Set up a new register map that uses memory access |
| 79 | * |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 80 | * @node: Device node that uses this map |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 81 | * @mapp: Returns allocated map |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 82 | * Return: 0 if OK, -ve on error |
| 83 | * |
| 84 | * Use regmap_uninit() to free it. |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 85 | */ |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 86 | int regmap_init_mem(ofnode node, struct regmap **mapp); |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 87 | |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 88 | /** |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 89 | * regmap_init_mem_platdata() - Set up a new memory register map for |
| 90 | * of-platdata |
| 91 | * |
| 92 | * @dev: Device that uses this map |
| 93 | * @reg: List of address, size pairs |
| 94 | * @count: Number of pairs (e.g. 1 if the regmap has a single entry) |
| 95 | * @mapp: Returns allocated map |
| 96 | * Return: 0 if OK, -ve on error |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 97 | * |
| 98 | * This creates a new regmap with a list of regions passed in, rather than |
| 99 | * using the device tree. It only supports 32-bit machines. |
| 100 | * |
| 101 | * Use regmap_uninit() to free it. |
| 102 | * |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 103 | */ |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 104 | int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, |
Simon Glass | 3b2a29e | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 105 | struct regmap **mapp); |
| 106 | |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 107 | /** |
| 108 | * regmap_get_range() - Obtain the base memory address of a regmap range |
| 109 | * |
| 110 | * @map: Regmap to query |
| 111 | * @range_num: Range to look up |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 112 | * Return: Pointer to the range in question if OK, NULL on error |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 113 | */ |
| 114 | void *regmap_get_range(struct regmap *map, unsigned int range_num); |
| 115 | |
| 116 | /** |
| 117 | * regmap_uninit() - free a previously inited regmap |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame^] | 118 | * |
| 119 | * @map: Regmap to free |
| 120 | * Return: 0 if OK, -ve on error |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 121 | */ |
| 122 | int regmap_uninit(struct regmap *map); |
| 123 | |
| 124 | #endif |