Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | 8c772bd | 2016-07-20 17:55:12 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Atmel Corporation |
| 4 | * Wenyou.Yang <wenyou.yang@atmel.com> |
Wenyou Yang | 8c772bd | 2016-07-20 17:55:12 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Claudiu Beznea | 3f203a1 | 2020-09-07 17:46:39 +0300 | [diff] [blame^] | 7 | #include <asm/io.h> |
Wenyou Yang | 8c772bd | 2016-07-20 17:55:12 +0800 | [diff] [blame] | 8 | #include <common.h> |
Claudiu Beznea | 8fdb425 | 2020-09-07 17:46:38 +0300 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * pmc_read() - read content at address base + off into val |
| 12 | * |
| 13 | * @base: base address |
| 14 | * @off: offset to read from |
| 15 | * @val: where the content of base + off is stored |
| 16 | * |
| 17 | * @return: void |
| 18 | */ |
| 19 | void pmc_read(void __iomem *base, unsigned int off, unsigned int *val) |
| 20 | { |
| 21 | *val = readl(base + off); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * pmc_write() - write content of val at address base + off |
| 26 | * |
| 27 | * @base: base address |
| 28 | * @off: offset to write to |
| 29 | * @val: content to be written at base + off |
| 30 | * |
| 31 | * @return: void |
| 32 | */ |
| 33 | void pmc_write(void __iomem *base, unsigned int off, unsigned int val) |
| 34 | { |
| 35 | writel(val, base + off); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * pmc_update_bits() - update a set of bits at address base + off |
| 40 | * |
| 41 | * @base: base address |
| 42 | * @off: offset to be updated |
| 43 | * @mask: mask of bits to be updated |
| 44 | * @bits: the new value to be updated |
| 45 | * |
| 46 | * @return: void |
| 47 | */ |
| 48 | void pmc_update_bits(void __iomem *base, unsigned int off, |
| 49 | unsigned int mask, unsigned int bits) |
| 50 | { |
| 51 | unsigned int tmp; |
| 52 | |
| 53 | tmp = readl(base + off); |
| 54 | tmp &= ~mask; |
| 55 | writel(tmp | (bits & mask), base + off); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * at91_clk_mux_val_to_index() - get parent index in mux table |
| 60 | * |
| 61 | * @table: clock mux table |
| 62 | * @num_parents: clock number of parents |
| 63 | * @val: clock id who's mux index should be retrieved |
| 64 | * |
| 65 | * @return: clock index in mux table or a negative error number in case of |
| 66 | * failure |
| 67 | */ |
| 68 | int at91_clk_mux_val_to_index(const u32 *table, u32 num_parents, u32 val) |
| 69 | { |
| 70 | int i; |
| 71 | |
| 72 | if (!table || !num_parents) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | for (i = 0; i < num_parents; i++) { |
| 76 | if (table[i] == val) |
| 77 | return i; |
| 78 | } |
| 79 | |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * at91_clk_mux_index_to_val() - get parent ID corresponding to an entry in |
| 85 | * clock's mux table |
| 86 | * |
| 87 | * @table: clock's mux table |
| 88 | * @num_parents: clock's number of parents |
| 89 | * @index: index in mux table which clock's ID should be retrieved |
| 90 | * |
| 91 | * @return: clock ID or a negative error number in case of failure |
| 92 | */ |
| 93 | int at91_clk_mux_index_to_val(const u32 *table, u32 num_parents, u32 index) |
| 94 | { |
| 95 | if (!table || !num_parents || index < 0 || index > num_parents) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | return table[index]; |
| 99 | } |