Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OMAP34xx M2 divider clock code |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Texas Instruments, Inc. |
| 5 | * Copyright (C) 2007-2010 Nokia Corporation |
| 6 | * |
| 7 | * Paul Walmsley |
| 8 | * Jouni Högander |
| 9 | * |
| 10 | * Parts of this code are based on code written by |
| 11 | * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | */ |
| 17 | #undef DEBUG |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/io.h> |
| 23 | |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 24 | #include "clock.h" |
Paul Walmsley | 657ebfa | 2010-02-22 22:09:20 -0700 | [diff] [blame] | 25 | #include "clock3xxx.h" |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 26 | #include "clock34xx.h" |
| 27 | #include "sdrc.h" |
Tony Lindgren | bf027ca | 2012-10-29 13:54:06 -0700 | [diff] [blame^] | 28 | #include "sram.h" |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 29 | |
| 30 | #define CYCLES_PER_MHZ 1000000 |
| 31 | |
| 32 | /* |
| 33 | * CORE DPLL (DPLL3) M2 divider rate programming functions |
| 34 | * |
| 35 | * These call into SRAM code to do the actual CM writes, since the SDRAM |
| 36 | * is clocked from DPLL3. |
| 37 | */ |
| 38 | |
| 39 | /** |
| 40 | * omap3_core_dpll_m2_set_rate - set CORE DPLL M2 divider |
| 41 | * @clk: struct clk * of DPLL to set |
| 42 | * @rate: rounded target rate |
| 43 | * |
| 44 | * Program the DPLL M2 divider with the rounded target rate. Returns |
| 45 | * -EINVAL upon error, or 0 upon success. |
| 46 | */ |
| 47 | int omap3_core_dpll_m2_set_rate(struct clk *clk, unsigned long rate) |
| 48 | { |
| 49 | u32 new_div = 0; |
| 50 | u32 unlock_dll = 0; |
| 51 | u32 c; |
| 52 | unsigned long validrate, sdrcrate, _mpurate; |
| 53 | struct omap_sdrc_params *sdrc_cs0; |
| 54 | struct omap_sdrc_params *sdrc_cs1; |
| 55 | int ret; |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 56 | unsigned long clkrate; |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 57 | |
| 58 | if (!clk || !rate) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | validrate = omap2_clksel_round_rate_div(clk, rate, &new_div); |
| 62 | if (validrate != rate) |
| 63 | return -EINVAL; |
| 64 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 65 | sdrcrate = __clk_get_rate(sdrc_ick_p); |
| 66 | clkrate = __clk_get_rate(clk); |
| 67 | if (rate > clkrate) |
| 68 | sdrcrate <<= ((rate / clkrate) >> 1); |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 69 | else |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 70 | sdrcrate >>= ((clkrate / rate) >> 1); |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 71 | |
| 72 | ret = omap2_sdrc_get_params(sdrcrate, &sdrc_cs0, &sdrc_cs1); |
| 73 | if (ret) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | if (sdrcrate < MIN_SDRC_DLL_LOCK_FREQ) { |
| 77 | pr_debug("clock: will unlock SDRC DLL\n"); |
| 78 | unlock_dll = 1; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * XXX This only needs to be done when the CPU frequency changes |
| 83 | */ |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 84 | _mpurate = __clk_get_rate(arm_fck_p) / CYCLES_PER_MHZ; |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 85 | c = (_mpurate << SDRC_MPURATE_SCALE) >> SDRC_MPURATE_BASE_SHIFT; |
| 86 | c += 1; /* for safety */ |
| 87 | c *= SDRC_MPURATE_LOOPS; |
| 88 | c >>= SDRC_MPURATE_SCALE; |
| 89 | if (c == 0) |
| 90 | c = 1; |
| 91 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 92 | pr_debug("clock: changing CORE DPLL rate from %lu to %lu\n", |
| 93 | clkrate, validrate); |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 94 | pr_debug("clock: SDRC CS0 timing params used: RFR %08x CTRLA %08x CTRLB %08x MR %08x\n", |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 95 | sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla, |
| 96 | sdrc_cs0->actim_ctrlb, sdrc_cs0->mr); |
| 97 | if (sdrc_cs1) |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 98 | pr_debug("clock: SDRC CS1 timing params used: RFR %08x CTRLA %08x CTRLB %08x MR %08x\n", |
| 99 | sdrc_cs1->rfr_ctrl, sdrc_cs1->actim_ctrla, |
| 100 | sdrc_cs1->actim_ctrlb, sdrc_cs1->mr); |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 101 | |
| 102 | if (sdrc_cs1) |
| 103 | omap3_configure_core_dpll( |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 104 | new_div, unlock_dll, c, rate > clkrate, |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 105 | sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla, |
| 106 | sdrc_cs0->actim_ctrlb, sdrc_cs0->mr, |
| 107 | sdrc_cs1->rfr_ctrl, sdrc_cs1->actim_ctrla, |
| 108 | sdrc_cs1->actim_ctrlb, sdrc_cs1->mr); |
| 109 | else |
| 110 | omap3_configure_core_dpll( |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 111 | new_div, unlock_dll, c, rate > clkrate, |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 112 | sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla, |
| 113 | sdrc_cs0->actim_ctrlb, sdrc_cs0->mr, |
| 114 | 0, 0, 0, 0); |
Avinash H.M | 5fd2a84 | 2011-05-09 12:29:40 +0000 | [diff] [blame] | 115 | clk->rate = rate; |
Paul Walmsley | 35e424e | 2010-01-26 20:13:09 -0700 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |