Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Imagination Technologies |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk-uclass.h> |
| 8 | #include <dm.h> |
| 9 | #include <dt-bindings/clock/boston-clock.h> |
| 10 | #include <regmap.h> |
| 11 | #include <syscon.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 13 | #include <linux/printk.h> |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 14 | |
| 15 | struct clk_boston { |
| 16 | struct regmap *regmap; |
| 17 | }; |
| 18 | |
| 19 | #define BOSTON_PLAT_MMCMDIV 0x30 |
| 20 | # define BOSTON_PLAT_MMCMDIV_CLK0DIV (0xff << 0) |
| 21 | # define BOSTON_PLAT_MMCMDIV_INPUT (0xff << 8) |
| 22 | # define BOSTON_PLAT_MMCMDIV_MUL (0xff << 16) |
| 23 | # define BOSTON_PLAT_MMCMDIV_CLK1DIV (0xff << 24) |
| 24 | |
| 25 | static uint32_t ext_field(uint32_t val, uint32_t mask) |
| 26 | { |
| 27 | return (val & mask) >> (ffs(mask) - 1); |
| 28 | } |
| 29 | |
| 30 | static ulong clk_boston_get_rate(struct clk *clk) |
| 31 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 32 | struct clk_boston *state = dev_get_plat(clk->dev); |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 33 | uint32_t in_rate, mul, div; |
| 34 | uint mmcmdiv; |
| 35 | int err; |
| 36 | |
| 37 | err = regmap_read(state->regmap, BOSTON_PLAT_MMCMDIV, &mmcmdiv); |
| 38 | if (err) |
| 39 | return 0; |
| 40 | |
| 41 | in_rate = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_INPUT); |
| 42 | mul = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_MUL); |
| 43 | |
| 44 | switch (clk->id) { |
| 45 | case BOSTON_CLK_SYS: |
| 46 | div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK0DIV); |
| 47 | break; |
| 48 | case BOSTON_CLK_CPU: |
| 49 | div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK1DIV); |
| 50 | break; |
| 51 | default: |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | return (in_rate * mul * 1000000) / div; |
| 56 | } |
| 57 | |
| 58 | const struct clk_ops clk_boston_ops = { |
| 59 | .get_rate = clk_boston_get_rate, |
| 60 | }; |
| 61 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 62 | static int clk_boston_of_to_plat(struct udevice *dev) |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 63 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 64 | struct clk_boston *state = dev_get_plat(dev); |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 65 | struct udevice *syscon; |
| 66 | int err; |
| 67 | |
| 68 | err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, |
| 69 | "regmap", &syscon); |
| 70 | if (err) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 71 | pr_err("unable to find syscon device\n"); |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 72 | return err; |
| 73 | } |
| 74 | |
| 75 | state->regmap = syscon_get_regmap(syscon); |
| 76 | if (!state->regmap) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 77 | pr_err("unable to find regmap\n"); |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 78 | return -ENODEV; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static const struct udevice_id clk_boston_match[] = { |
| 85 | { |
| 86 | .compatible = "img,boston-clock", |
| 87 | }, |
| 88 | { /* sentinel */ } |
| 89 | }; |
| 90 | |
| 91 | U_BOOT_DRIVER(clk_boston) = { |
| 92 | .name = "boston_clock", |
| 93 | .id = UCLASS_CLK, |
| 94 | .of_match = clk_boston_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 95 | .of_to_plat = clk_boston_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 96 | .plat_auto = sizeof(struct clk_boston), |
Paul Burton | 0399f44 | 2016-09-08 07:47:38 +0100 | [diff] [blame] | 97 | .ops = &clk_boston_ops, |
| 98 | }; |