From patchwork Thu Aug 2 07:25:06 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rajeshwari Shinde X-Patchwork-Id: 10456 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 0A6BE2402A for ; Thu, 2 Aug 2012 07:19:02 +0000 (UTC) Received: from mail-gh0-f180.google.com (mail-gh0-f180.google.com [209.85.160.180]) by fiordland.canonical.com (Postfix) with ESMTP id B2144A18040 for ; Thu, 2 Aug 2012 07:19:01 +0000 (UTC) Received: by ghbz12 with SMTP id z12so8208348ghb.11 for ; Thu, 02 Aug 2012 00:19:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:x-auditid :from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-brightmail-tracker:x-tm-as-mml:x-gm-message-state; bh=XOOnthGxUmYjeXL1oZZxAL4aOIp8NvjjQr5NU86GkFw=; b=Xjk5xkbZyZ3B7+FRtqlGSucNYSGEaGltVIYRncjMMpiV9AnkXP8gW2EzfolFEiaU4m yZv4ZKq9SWVdnjSAecJP/0WZbRRggxR4v8p3yss/uyN0ak/SqXdws1DiwYFrlCXRBfua bn8vbpk3ExS5AL+phyz41CSvz0sW7oV2EfHXO0u1N5pgm0YDn6F9LH6w2cECitsQOwmr WYZrM3Pxr3KgYFZ7g7FT7qU7+Z4i+iF8vaP9lP1cEBR6QyVtigJ6HSlp8LptvcX4yDY8 /8wmV5MWuHjpokuJWoTQczTGlRn69uuO8UU3hX5KP0zTi0mltQhH9svQJYjk8afrJLjo IosQ== Received: by 10.42.54.133 with SMTP id r5mr2505373icg.9.1343891940943; Thu, 02 Aug 2012 00:19:00 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.50.87.40 with SMTP id u8csp239567igz; Thu, 2 Aug 2012 00:19:00 -0700 (PDT) Received: by 10.60.12.8 with SMTP id u8mr33841701oeb.46.1343891940189; Thu, 02 Aug 2012 00:19:00 -0700 (PDT) Received: from mailout1.samsung.com (mailout1.samsung.com. [203.254.224.24]) by mx.google.com with ESMTP id fx3si5782125obc.9.2012.08.02.00.18.58; Thu, 02 Aug 2012 00:19:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of rajeshwari.s@samsung.com designates 203.254.224.24 as permitted sender) client-ip=203.254.224.24; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of rajeshwari.s@samsung.com designates 203.254.224.24 as permitted sender) smtp.mail=rajeshwari.s@samsung.com Received: from epcpsbgm2.samsung.com (mailout1.samsung.com [203.254.224.24]) by mailout1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0M8400DV19MFQTO0@mailout1.samsung.com>; Thu, 02 Aug 2012 16:18:57 +0900 (KST) X-AuditID: cbfee61b-b7f566d000005c8a-01-501a29e1febf Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id F6.D5.23690.1E92A105; Thu, 02 Aug 2012 16:18:57 +0900 (KST) Received: from rajeshwari-linux.sisodomain.com ([107.108.215.115]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0M840099U9N7OKD0@mmp1.samsung.com>; Thu, 02 Aug 2012 16:18:57 +0900 (KST) From: Rajeshwari Shinde To: u-boot@lists.denx.de Cc: patches@linaro.org, alim.akhtar@samsung.com, sjg@chromium.org, mk7.kang@samsung.com, chander.kashyap@linaro.org, vapier@gentoo.org Subject: [PATCH 3/7 V4] EXYNOS: Add clock for SPI. Date: Thu, 02 Aug 2012 12:55:06 +0530 Message-id: <1343892310-21018-4-git-send-email-rajeshwari.s@samsung.com> X-Mailer: git-send-email 1.7.4.4 In-reply-to: <1343892310-21018-1-git-send-email-rajeshwari.s@samsung.com> References: <1343892310-21018-1-git-send-email-rajeshwari.s@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrBJMWRmVeSWpSXmKPExsVy+t9jAd2HmlIBBj9PGVg8XH+TxWLK4S8s Dkwed67tYQtgjOKySUnNySxLLdK3S+DKePqsi7XgpXpF84kelgbGe/JdjJwcEgImEisW/WaH sMUkLtxbz9bFyMUhJLCIUeLYxW3sEM5EJokzaxaygFSxCRhJbD05jRHEFhGQkPjVf5URpIhZ oINR4vC3HUwgCWGgorkT21lBbBYBVYmdr3+xgdi8Ah4SH9c9Z4ZYpyBxbOpXsBpOAU+Ji3NW AsU5gLZ5SHx7nzeBkXcBI8MqRtHUguSC4qT0XCO94sTc4tK8dL3k/NxNjGD/P5PewbiqweIQ owAHoxIP78pSyQAh1sSy4srcQ4wSHMxKIrx3JKQChHhTEiurUovy44tKc1KLDzFKc7AoifOa eH/1FxJITyxJzU5NLUgtgskycXBKNTCq/NnDJNB2Qq8p4L9u6v+PDK0GM997zngStfFJ2tnG NQt0xPyOXf32U2PivIsbZRVMlrdMPP0+PFOhat+8F7vC0z7GXTJRD/wuLs4yf9cubukVTDp3 HhqxPb7jPPNS8LUETpMFt9/FrFIwP3+vU2z2XSvXd0auj43iaxqUr9j2t/DuinEJZtFVYinO SDTUYi4qTgQADaUaofsBAAA= X-TM-AS-MML: No X-Gm-Message-State: ALoCoQmHfFolf5DysGQrd/2zgVPk5eei2G9IWY1W7y1jPnbn8GGaCxvDVOZubKmsBIOcx7qg4N6z This patch adds api to calculate and set the clock for SPI channels Signed-off-by: Simon Glass Signed-off-by: Rajeshwari Shinde --- Changes in V2: - None Changes in V3: - Corrected Warning messages. Changes in V4: - Rebased on Mainline u-boot.git. arch/arm/cpu/armv7/exynos/clock.c | 124 ++++++++++++++++++++++++++++++++ arch/arm/include/asm/arch-exynos/clk.h | 4 +- 2 files changed, 127 insertions(+), 1 deletions(-) diff --git a/arch/arm/cpu/armv7/exynos/clock.c b/arch/arm/cpu/armv7/exynos/clock.c index f7829b2..9b015a6 100644 --- a/arch/arm/cpu/armv7/exynos/clock.c +++ b/arch/arm/cpu/armv7/exynos/clock.c @@ -601,6 +601,122 @@ static unsigned long exynos5_get_i2c_clk(void) return aclk_66; } +/** + * Linearly searches for the most accurate main and fine stage clock scalars + * (divisors) for a specified target frequency and scalar bit sizes by checking + * all multiples of main_scalar_bits values. Will always return scalars up to or + * slower than target. + * + * @param main_scalar_bits Number of main scalar bits, must be > 0 and < 32 + * @param fine_scalar_bits Number of fine scalar bits, must be > 0 and < 32 + * @param input_freq Clock frequency to be scaled in Hz + * @param target_freq Desired clock frequency in Hz + * @param best_fine_scalar Pointer to store the fine stage divisor + * + * @return best_main_scalar Main scalar for desired frequency or -1 if none + * found + */ +static int clock_calc_best_scalar(unsigned int main_scaler_bits, + unsigned int fine_scalar_bits, unsigned int input_rate, + unsigned int target_rate, unsigned int *best_fine_scalar) +{ + int i; + int best_main_scalar = -1; + unsigned int best_error = target_rate; + const unsigned int cap = (1 << fine_scalar_bits) - 1; + const unsigned int loops = 1 << main_scaler_bits; + + debug("Input Rate is %u, Target is %u, Cap is %u\n", input_rate, + target_rate, cap); + + assert(best_fine_scalar != NULL); + assert(main_scaler_bits <= fine_scalar_bits); + + *best_fine_scalar = 1; + + if (input_rate == 0 || target_rate == 0) + return -1; + + if (target_rate >= input_rate) + return 1; + + for (i = 1; i <= loops; i++) { + const unsigned int effective_div = max(min(input_rate / i / + target_rate, cap), 1); + const unsigned int effective_rate = input_rate / i / + effective_div; + const int error = target_rate - effective_rate; + + debug("%d|effdiv:%u, effrate:%u, error:%d\n", i, effective_div, + effective_rate, error); + + if (error >= 0 && error <= best_error) { + best_error = error; + best_main_scalar = i; + *best_fine_scalar = effective_div; + } + } + + return best_main_scalar; +} + +static int exynos5_spi_set_clock_rate(enum periph_id periph_id, + unsigned int rate) +{ + struct exynos5_clock *clk = + (struct exynos5_clock *)samsung_get_base_clock(); + int main; + unsigned int fine; + unsigned shift, pre_shift; + unsigned mask = 0xff; + u32 *reg; + + main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine); + if (main < 0) { + debug("%s: Cannot set clock rate for periph %d", + __func__, periph_id); + return -1; + } + main = main - 1; + fine = fine - 1; + + switch (periph_id) { + case PERIPH_ID_SPI0: + reg = &clk->div_peric1; + shift = 0; + pre_shift = 8; + break; + case PERIPH_ID_SPI1: + reg = &clk->div_peric1; + shift = 16; + pre_shift = 24; + break; + case PERIPH_ID_SPI2: + reg = &clk->div_peric2; + shift = 0; + pre_shift = 8; + break; + case PERIPH_ID_SPI3: + reg = &clk->sclk_div_isp; + shift = 0; + pre_shift = 4; + break; + case PERIPH_ID_SPI4: + reg = &clk->sclk_div_isp; + shift = 12; + pre_shift = 16; + break; + default: + debug("%s: Unsupported peripheral ID %d\n", __func__, + periph_id); + return -1; + } + clrsetbits_le32(reg, mask << shift, (main & mask) << shift); + clrsetbits_le32(reg, mask << pre_shift, (fine & mask) << pre_shift); + + return 0; +} + unsigned long get_pll_clk(int pllreg) { if (cpu_is_exynos5()) @@ -670,3 +786,11 @@ void set_mipi_clk(void) if (cpu_is_exynos4()) exynos4_set_mipi_clk(); } + +int spi_set_clock_rate(enum periph_id periph_id, unsigned int rate) +{ + if (cpu_is_exynos5()) + return exynos5_spi_set_clock_rate(periph_id, rate); + else + return 0; +} diff --git a/arch/arm/include/asm/arch-exynos/clk.h b/arch/arm/include/asm/arch-exynos/clk.h index 72dc655..401931c 100644 --- a/arch/arm/include/asm/arch-exynos/clk.h +++ b/arch/arm/include/asm/arch-exynos/clk.h @@ -22,6 +22,8 @@ #ifndef __ASM_ARM_ARCH_CLK_H_ #define __ASM_ARM_ARCH_CLK_H_ +#include + #define APLL 0 #define MPLL 1 #define EPLL 2 @@ -37,5 +39,5 @@ void set_mmc_clk(int dev_index, unsigned int div); unsigned long get_lcd_clk(void); void set_lcd_clk(void); void set_mipi_clk(void); - +int spi_set_clock_rate(enum periph_id periph_id, unsigned int rate); #endif