Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Masahiro Yamada | 7f8e753 | 2017-12-06 14:16:34 +0900 | [diff] [blame] | 7 | #include <linux/bitfield.h> |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 8 | #include <linux/bitops.h> |
Masahiro Yamada | d9a7036 | 2017-01-21 18:05:25 +0900 | [diff] [blame] | 9 | #include <linux/delay.h> |
Masahiro Yamada | f2ce50b | 2017-12-06 14:16:33 +0900 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Masahiro Yamada | d9a7036 | 2017-01-21 18:05:25 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 12 | #include <linux/io.h> |
| 13 | #include <linux/sizes.h> |
| 14 | |
Masahiro Yamada | 739ba41 | 2019-07-10 20:07:41 +0900 | [diff] [blame] | 15 | #include "../sc64-regs.h" |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 16 | #include "pll.h" |
| 17 | |
| 18 | /* PLL type: SSC */ |
| 19 | #define SC_PLLCTRL_SSC_DK_MASK GENMASK(14, 0) |
| 20 | #define SC_PLLCTRL_SSC_EN BIT(31) |
| 21 | #define SC_PLLCTRL2_NRSTDS BIT(28) |
| 22 | #define SC_PLLCTRL2_SSC_JK_MASK GENMASK(26, 0) |
Masahiro Yamada | bc64795 | 2017-02-21 23:00:35 +0900 | [diff] [blame] | 23 | #define SC_PLLCTRL3_REGI_MASK GENMASK(19, 16) |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 24 | |
| 25 | /* PLL type: VPLL27 */ |
| 26 | #define SC_VPLL27CTRL_WP BIT(0) |
| 27 | #define SC_VPLL27CTRL3_K_LD BIT(28) |
| 28 | |
| 29 | /* PLL type: DSPLL */ |
| 30 | #define SC_DSPLLCTRL2_K_LD BIT(28) |
| 31 | |
| 32 | int uniphier_ld20_sscpll_init(unsigned long reg_base, unsigned int freq, |
| 33 | unsigned int ssc_rate, unsigned int divn) |
| 34 | { |
Masahiro Yamada | 739ba41 | 2019-07-10 20:07:41 +0900 | [diff] [blame] | 35 | void __iomem *base = sc_base + reg_base; |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 36 | u32 tmp; |
| 37 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 38 | if (freq != UNIPHIER_PLL_FREQ_DEFAULT) { |
| 39 | tmp = readl(base); /* SSCPLLCTRL */ |
| 40 | tmp &= ~SC_PLLCTRL_SSC_DK_MASK; |
Masahiro Yamada | 7f8e753 | 2017-12-06 14:16:34 +0900 | [diff] [blame] | 41 | tmp |= FIELD_PREP(SC_PLLCTRL_SSC_DK_MASK, |
| 42 | DIV_ROUND_CLOSEST(487UL * freq * ssc_rate, |
| 43 | divn * 512)); |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 44 | writel(tmp, base); |
| 45 | |
| 46 | tmp = readl(base + 4); |
| 47 | tmp &= ~SC_PLLCTRL2_SSC_JK_MASK; |
Masahiro Yamada | 7f8e753 | 2017-12-06 14:16:34 +0900 | [diff] [blame] | 48 | tmp |= FIELD_PREP(SC_PLLCTRL2_SSC_JK_MASK, |
| 49 | DIV_ROUND_CLOSEST(21431887UL * freq, |
| 50 | divn * 512)); |
Dai Okamura | c30c44e | 2017-12-06 14:16:32 +0900 | [diff] [blame] | 51 | writel(tmp, base + 4); |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 52 | |
| 53 | udelay(50); |
| 54 | } |
| 55 | |
| 56 | tmp = readl(base + 4); /* SSCPLLCTRL2 */ |
| 57 | tmp |= SC_PLLCTRL2_NRSTDS; |
| 58 | writel(tmp, base + 4); |
| 59 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int uniphier_ld20_sscpll_ssc_en(unsigned long reg_base) |
| 64 | { |
Masahiro Yamada | 739ba41 | 2019-07-10 20:07:41 +0900 | [diff] [blame] | 65 | void __iomem *base = sc_base + reg_base; |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 66 | u32 tmp; |
| 67 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 68 | tmp = readl(base); /* SSCPLLCTRL */ |
| 69 | tmp |= SC_PLLCTRL_SSC_EN; |
| 70 | writel(tmp, base); |
| 71 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Masahiro Yamada | bc64795 | 2017-02-21 23:00:35 +0900 | [diff] [blame] | 75 | int uniphier_ld20_sscpll_set_regi(unsigned long reg_base, unsigned regi) |
| 76 | { |
Masahiro Yamada | 739ba41 | 2019-07-10 20:07:41 +0900 | [diff] [blame] | 77 | void __iomem *base = sc_base + reg_base; |
Masahiro Yamada | bc64795 | 2017-02-21 23:00:35 +0900 | [diff] [blame] | 78 | u32 tmp; |
| 79 | |
Dai Okamura | 4013bbb | 2017-08-28 21:57:15 +0900 | [diff] [blame] | 80 | tmp = readl(base + 8); /* SSCPLLCTRL3 */ |
Masahiro Yamada | bc64795 | 2017-02-21 23:00:35 +0900 | [diff] [blame] | 81 | tmp &= ~SC_PLLCTRL3_REGI_MASK; |
Masahiro Yamada | 7f8e753 | 2017-12-06 14:16:34 +0900 | [diff] [blame] | 82 | tmp |= FIELD_PREP(SC_PLLCTRL3_REGI_MASK, regi); |
Masahiro Yamada | bc64795 | 2017-02-21 23:00:35 +0900 | [diff] [blame] | 83 | writel(tmp, base + 8); |
| 84 | |
Masahiro Yamada | bc64795 | 2017-02-21 23:00:35 +0900 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 88 | int uniphier_ld20_vpll27_init(unsigned long reg_base) |
| 89 | { |
Masahiro Yamada | 739ba41 | 2019-07-10 20:07:41 +0900 | [diff] [blame] | 90 | void __iomem *base = sc_base + reg_base; |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 91 | u32 tmp; |
| 92 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 93 | tmp = readl(base); /* VPLL27CTRL */ |
| 94 | tmp |= SC_VPLL27CTRL_WP; /* write protect off */ |
| 95 | writel(tmp, base); |
| 96 | |
| 97 | tmp = readl(base + 8); /* VPLL27CTRL3 */ |
| 98 | tmp |= SC_VPLL27CTRL3_K_LD; |
| 99 | writel(tmp, base + 8); |
| 100 | |
| 101 | tmp = readl(base); /* VPLL27CTRL */ |
| 102 | tmp &= ~SC_VPLL27CTRL_WP; /* write protect on */ |
| 103 | writel(tmp, base); |
| 104 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int uniphier_ld20_dspll_init(unsigned long reg_base) |
| 109 | { |
Masahiro Yamada | 739ba41 | 2019-07-10 20:07:41 +0900 | [diff] [blame] | 110 | void __iomem *base = sc_base + reg_base; |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 111 | u32 tmp; |
| 112 | |
Dai Okamura | 4013bbb | 2017-08-28 21:57:15 +0900 | [diff] [blame] | 113 | tmp = readl(base + 4); /* DSPLLCTRL2 */ |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 114 | tmp |= SC_DSPLLCTRL2_K_LD; |
Dai Okamura | 4013bbb | 2017-08-28 21:57:15 +0900 | [diff] [blame] | 115 | writel(tmp, base + 4); |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 116 | |
Masahiro Yamada | 682e09f | 2016-09-17 03:33:11 +0900 | [diff] [blame] | 117 | return 0; |
| 118 | } |