Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * r8a7790 clock framework support |
| 3 | * |
| 4 | * Copyright (C) 2013 Renesas Solutions Corp. |
| 5 | * Copyright (C) 2013 Magnus Damm |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/sh_clk.h> |
| 24 | #include <linux/clkdev.h> |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 25 | #include <mach/clock.h> |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 26 | #include <mach/common.h> |
| 27 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 28 | /* |
| 29 | * MD EXTAL PLL0 PLL1 PLL3 |
| 30 | * 14 13 19 (MHz) *1 *1 |
| 31 | *--------------------------------------------------- |
| 32 | * 0 0 0 15 x 1 x172/2 x208/2 x106 |
| 33 | * 0 0 1 15 x 1 x172/2 x208/2 x88 |
| 34 | * 0 1 0 20 x 1 x130/2 x156/2 x80 |
| 35 | * 0 1 1 20 x 1 x130/2 x156/2 x66 |
| 36 | * 1 0 0 26 / 2 x200/2 x240/2 x122 |
| 37 | * 1 0 1 26 / 2 x200/2 x240/2 x102 |
| 38 | * 1 1 0 30 / 2 x172/2 x208/2 x106 |
| 39 | * 1 1 1 30 / 2 x172/2 x208/2 x88 |
| 40 | * |
| 41 | * *1 : Table 7.6 indicates VCO ouput (PLLx = VCO/2) |
| 42 | * see "p1 / 2" on R8A7790_CLOCK_ROOT() below |
| 43 | */ |
| 44 | |
| 45 | #define MD(nr) (1 << nr) |
| 46 | |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 47 | #define CPG_BASE 0xe6150000 |
| 48 | #define CPG_LEN 0x1000 |
| 49 | |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 50 | #define SMSTPCR2 0xe6150138 |
| 51 | #define SMSTPCR7 0xe615014c |
| 52 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 53 | #define MODEMR 0xE6160060 |
Kuninori Morimoto | 9f13ee6 | 2013-04-12 00:42:52 -0700 | [diff] [blame^] | 54 | #define SDCKCR 0xE6150074 |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 55 | |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 56 | static struct clk_mapping cpg_mapping = { |
| 57 | .phys = CPG_BASE, |
| 58 | .len = CPG_LEN, |
| 59 | }; |
| 60 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 61 | static struct clk extal_clk = { |
| 62 | /* .rate will be updated on r8a7790_clock_init() */ |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 63 | .mapping = &cpg_mapping, |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 66 | static struct sh_clk_ops followparent_clk_ops = { |
| 67 | .recalc = followparent_recalc, |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 68 | }; |
| 69 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 70 | static struct clk main_clk = { |
| 71 | /* .parent will be set r8a73a4_clock_init */ |
| 72 | .ops = &followparent_clk_ops, |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * clock ratio of these clock will be updated |
| 77 | * on r8a7790_clock_init() |
| 78 | */ |
| 79 | SH_FIXED_RATIO_CLK_SET(pll1_clk, main_clk, 1, 1); |
| 80 | SH_FIXED_RATIO_CLK_SET(pll3_clk, main_clk, 1, 1); |
| 81 | SH_FIXED_RATIO_CLK_SET(lb_clk, pll1_clk, 1, 1); |
| 82 | SH_FIXED_RATIO_CLK_SET(qspi_clk, pll1_clk, 1, 1); |
| 83 | |
| 84 | /* fixed ratio clock */ |
| 85 | SH_FIXED_RATIO_CLK_SET(extal_div2_clk, extal_clk, 1, 2); |
| 86 | SH_FIXED_RATIO_CLK_SET(cp_clk, extal_clk, 1, 2); |
| 87 | |
| 88 | SH_FIXED_RATIO_CLK_SET(pll1_div2_clk, pll1_clk, 1, 2); |
| 89 | SH_FIXED_RATIO_CLK_SET(zg_clk, pll1_clk, 1, 3); |
| 90 | SH_FIXED_RATIO_CLK_SET(zx_clk, pll1_clk, 1, 3); |
| 91 | SH_FIXED_RATIO_CLK_SET(zs_clk, pll1_clk, 1, 6); |
| 92 | SH_FIXED_RATIO_CLK_SET(hp_clk, pll1_clk, 1, 12); |
| 93 | SH_FIXED_RATIO_CLK_SET(i_clk, pll1_clk, 1, 2); |
| 94 | SH_FIXED_RATIO_CLK_SET(b_clk, pll1_clk, 1, 12); |
| 95 | SH_FIXED_RATIO_CLK_SET(p_clk, pll1_clk, 1, 24); |
| 96 | SH_FIXED_RATIO_CLK_SET(cl_clk, pll1_clk, 1, 48); |
| 97 | SH_FIXED_RATIO_CLK_SET(m2_clk, pll1_clk, 1, 8); |
| 98 | SH_FIXED_RATIO_CLK_SET(imp_clk, pll1_clk, 1, 4); |
| 99 | SH_FIXED_RATIO_CLK_SET(rclk_clk, pll1_clk, 1, (48 * 1024)); |
| 100 | SH_FIXED_RATIO_CLK_SET(oscclk_clk, pll1_clk, 1, (12 * 1024)); |
| 101 | |
| 102 | SH_FIXED_RATIO_CLK_SET(zb3_clk, pll3_clk, 1, 4); |
| 103 | SH_FIXED_RATIO_CLK_SET(zb3d2_clk, pll3_clk, 1, 8); |
| 104 | SH_FIXED_RATIO_CLK_SET(ddr_clk, pll3_clk, 1, 8); |
| 105 | SH_FIXED_RATIO_CLK_SET(mp_clk, pll1_div2_clk, 1, 15); |
| 106 | |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 107 | static struct clk *main_clks[] = { |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 108 | &extal_clk, |
| 109 | &extal_div2_clk, |
| 110 | &main_clk, |
| 111 | &pll1_clk, |
| 112 | &pll1_div2_clk, |
| 113 | &pll3_clk, |
| 114 | &lb_clk, |
| 115 | &qspi_clk, |
| 116 | &zg_clk, |
| 117 | &zx_clk, |
| 118 | &zs_clk, |
| 119 | &hp_clk, |
| 120 | &i_clk, |
| 121 | &b_clk, |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 122 | &p_clk, |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 123 | &cl_clk, |
| 124 | &m2_clk, |
| 125 | &imp_clk, |
| 126 | &rclk_clk, |
| 127 | &oscclk_clk, |
| 128 | &zb3_clk, |
| 129 | &zb3d2_clk, |
| 130 | &ddr_clk, |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 131 | &mp_clk, |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 132 | &cp_clk, |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 133 | }; |
| 134 | |
Kuninori Morimoto | 9f13ee6 | 2013-04-12 00:42:52 -0700 | [diff] [blame^] | 135 | /* SDHI (DIV4) clock */ |
| 136 | static int divisors[] = { 2, 3, 4, 6, 8, 12, 16, 18, 24, 0, 36, 48, 10 }; |
| 137 | |
| 138 | static struct clk_div_mult_table div4_div_mult_table = { |
| 139 | .divisors = divisors, |
| 140 | .nr_divisors = ARRAY_SIZE(divisors), |
| 141 | }; |
| 142 | |
| 143 | static struct clk_div4_table div4_table = { |
| 144 | .div_mult_table = &div4_div_mult_table, |
| 145 | }; |
| 146 | |
| 147 | enum { |
| 148 | DIV4_SDH, DIV4_SD0, DIV4_SD1, DIV4_NR |
| 149 | }; |
| 150 | |
| 151 | struct clk div4_clks[DIV4_NR] = { |
| 152 | [DIV4_SDH] = SH_CLK_DIV4(&pll1_clk, SDCKCR, 8, 0x0dff, CLK_ENABLE_ON_INIT), |
| 153 | [DIV4_SD0] = SH_CLK_DIV4(&pll1_clk, SDCKCR, 4, 0x1de0, CLK_ENABLE_ON_INIT), |
| 154 | [DIV4_SD1] = SH_CLK_DIV4(&pll1_clk, SDCKCR, 0, 0x1de0, CLK_ENABLE_ON_INIT), |
| 155 | }; |
| 156 | |
| 157 | /* MSTP */ |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 158 | enum { MSTP721, MSTP720, |
| 159 | MSTP216, MSTP207, MSTP206, MSTP204, MSTP203, MSTP202, MSTP_NR }; |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 160 | static struct clk mstp_clks[MSTP_NR] = { |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 161 | [MSTP721] = SH_CLK_MSTP32(&p_clk, SMSTPCR7, 21, 0), /* SCIF0 */ |
| 162 | [MSTP720] = SH_CLK_MSTP32(&p_clk, SMSTPCR7, 20, 0), /* SCIF1 */ |
| 163 | [MSTP216] = SH_CLK_MSTP32(&mp_clk, SMSTPCR2, 16, 0), /* SCIFB2 */ |
| 164 | [MSTP207] = SH_CLK_MSTP32(&mp_clk, SMSTPCR2, 7, 0), /* SCIFB1 */ |
| 165 | [MSTP206] = SH_CLK_MSTP32(&mp_clk, SMSTPCR2, 6, 0), /* SCIFB0 */ |
| 166 | [MSTP204] = SH_CLK_MSTP32(&mp_clk, SMSTPCR2, 4, 0), /* SCIFA0 */ |
| 167 | [MSTP203] = SH_CLK_MSTP32(&mp_clk, SMSTPCR2, 3, 0), /* SCIFA1 */ |
| 168 | [MSTP202] = SH_CLK_MSTP32(&mp_clk, SMSTPCR2, 2, 0), /* SCIFA2 */ |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | static struct clk_lookup lookups[] = { |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 172 | |
| 173 | /* main clocks */ |
| 174 | CLKDEV_CON_ID("extal", &extal_clk), |
| 175 | CLKDEV_CON_ID("extal_div2", &extal_div2_clk), |
| 176 | CLKDEV_CON_ID("main", &main_clk), |
| 177 | CLKDEV_CON_ID("pll1", &pll1_clk), |
| 178 | CLKDEV_CON_ID("pll1_div2", &pll1_div2_clk), |
| 179 | CLKDEV_CON_ID("pll3", &pll3_clk), |
| 180 | CLKDEV_CON_ID("zg", &zg_clk), |
| 181 | CLKDEV_CON_ID("zx", &zx_clk), |
| 182 | CLKDEV_CON_ID("zs", &zs_clk), |
| 183 | CLKDEV_CON_ID("hp", &hp_clk), |
| 184 | CLKDEV_CON_ID("i", &i_clk), |
| 185 | CLKDEV_CON_ID("b", &b_clk), |
| 186 | CLKDEV_CON_ID("lb", &lb_clk), |
| 187 | CLKDEV_CON_ID("p", &p_clk), |
| 188 | CLKDEV_CON_ID("cl", &cl_clk), |
| 189 | CLKDEV_CON_ID("m2", &m2_clk), |
| 190 | CLKDEV_CON_ID("imp", &imp_clk), |
| 191 | CLKDEV_CON_ID("rclk", &rclk_clk), |
| 192 | CLKDEV_CON_ID("oscclk", &oscclk_clk), |
| 193 | CLKDEV_CON_ID("zb3", &zb3_clk), |
| 194 | CLKDEV_CON_ID("zb3d2", &zb3d2_clk), |
| 195 | CLKDEV_CON_ID("ddr", &ddr_clk), |
| 196 | CLKDEV_CON_ID("mp", &mp_clk), |
| 197 | CLKDEV_CON_ID("qspi", &qspi_clk), |
| 198 | CLKDEV_CON_ID("cp", &cp_clk), |
| 199 | |
Kuninori Morimoto | 9f13ee6 | 2013-04-12 00:42:52 -0700 | [diff] [blame^] | 200 | /* DIV4 */ |
| 201 | CLKDEV_CON_ID("sdh", &div4_clks[DIV4_SDH]), |
| 202 | CLKDEV_CON_ID("sd0", &div4_clks[DIV4_SD0]), |
| 203 | CLKDEV_CON_ID("sd1", &div4_clks[DIV4_SD1]), |
| 204 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 205 | /* MSTP */ |
Magnus Damm | 55d9fab | 2013-03-28 00:49:44 +0900 | [diff] [blame] | 206 | CLKDEV_DEV_ID("sh-sci.0", &mstp_clks[MSTP204]), |
| 207 | CLKDEV_DEV_ID("sh-sci.1", &mstp_clks[MSTP203]), |
| 208 | CLKDEV_DEV_ID("sh-sci.2", &mstp_clks[MSTP206]), |
| 209 | CLKDEV_DEV_ID("sh-sci.3", &mstp_clks[MSTP207]), |
| 210 | CLKDEV_DEV_ID("sh-sci.4", &mstp_clks[MSTP216]), |
| 211 | CLKDEV_DEV_ID("sh-sci.5", &mstp_clks[MSTP202]), |
| 212 | CLKDEV_DEV_ID("sh-sci.6", &mstp_clks[MSTP721]), |
| 213 | CLKDEV_DEV_ID("sh-sci.7", &mstp_clks[MSTP720]), |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 214 | }; |
| 215 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 216 | #define R8A7790_CLOCK_ROOT(e, m, p0, p1, p30, p31) \ |
| 217 | extal_clk.rate = e * 1000 * 1000; \ |
| 218 | main_clk.parent = m; \ |
| 219 | SH_CLK_SET_RATIO(&pll1_clk_ratio, p1 / 2, 1); \ |
| 220 | if (mode & MD(19)) \ |
| 221 | SH_CLK_SET_RATIO(&pll3_clk_ratio, p31, 1); \ |
| 222 | else \ |
| 223 | SH_CLK_SET_RATIO(&pll3_clk_ratio, p30, 1) |
| 224 | |
| 225 | |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 226 | void __init r8a7790_clock_init(void) |
| 227 | { |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 228 | void __iomem *modemr = ioremap_nocache(MODEMR, PAGE_SIZE); |
| 229 | u32 mode; |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 230 | int k, ret = 0; |
| 231 | |
Kuninori Morimoto | 8d100c0 | 2013-04-12 00:42:22 -0700 | [diff] [blame] | 232 | BUG_ON(!modemr); |
| 233 | mode = ioread32(modemr); |
| 234 | iounmap(modemr); |
| 235 | |
| 236 | switch (mode & (MD(14) | MD(13))) { |
| 237 | case 0: |
| 238 | R8A7790_CLOCK_ROOT(15, &extal_clk, 172, 208, 106, 88); |
| 239 | break; |
| 240 | case MD(13): |
| 241 | R8A7790_CLOCK_ROOT(20, &extal_clk, 130, 156, 80, 66); |
| 242 | break; |
| 243 | case MD(14): |
| 244 | R8A7790_CLOCK_ROOT(26, &extal_div2_clk, 200, 240, 122, 102); |
| 245 | break; |
| 246 | case MD(13) | MD(14): |
| 247 | R8A7790_CLOCK_ROOT(30, &extal_div2_clk, 172, 208, 106, 88); |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | if (mode & (MD(18))) |
| 252 | SH_CLK_SET_RATIO(&lb_clk_ratio, 1, 36); |
| 253 | else |
| 254 | SH_CLK_SET_RATIO(&lb_clk_ratio, 1, 24); |
| 255 | |
| 256 | if ((mode & (MD(3) | MD(2) | MD(1))) == MD(2)) |
| 257 | SH_CLK_SET_RATIO(&qspi_clk_ratio, 1, 16); |
| 258 | else |
| 259 | SH_CLK_SET_RATIO(&qspi_clk_ratio, 1, 20); |
| 260 | |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 261 | for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++) |
| 262 | ret = clk_register(main_clks[k]); |
| 263 | |
| 264 | if (!ret) |
Kuninori Morimoto | 9f13ee6 | 2013-04-12 00:42:52 -0700 | [diff] [blame^] | 265 | ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table); |
| 266 | |
| 267 | if (!ret) |
Magnus Damm | 0468b2d | 2013-03-28 00:49:34 +0900 | [diff] [blame] | 268 | ret = sh_clk_mstp_register(mstp_clks, MSTP_NR); |
| 269 | |
| 270 | clkdev_add_table(lookups, ARRAY_SIZE(lookups)); |
| 271 | |
| 272 | if (!ret) |
| 273 | shmobile_clk_init(); |
| 274 | else |
| 275 | panic("failed to setup r8a7790 clocks\n"); |
| 276 | } |