Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/mach-ep93xx/clock.c |
| 3 | * Clock control for Cirrus EP93xx chips. |
| 4 | * |
| 5 | * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org> |
| 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; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | */ |
| 12 | |
Hartley Sweeten | 99acbb9 | 2010-01-11 18:30:41 +0100 | [diff] [blame] | 13 | #define pr_fmt(fmt) "ep93xx " KBUILD_MODNAME ": " fmt |
| 14 | |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/err.h> |
Lennert Buytenhek | 51dd249 | 2007-02-04 22:45:33 +0100 | [diff] [blame] | 18 | #include <linux/module.h> |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 19 | #include <linux/string.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 20 | #include <linux/io.h> |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
| 22 | |
| 23 | #include <mach/hardware.h> |
Russell King | ae696fd | 2008-11-30 17:11:49 +0000 | [diff] [blame] | 24 | |
| 25 | #include <asm/clkdev.h> |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 26 | #include <asm/div64.h> |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 27 | |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 28 | |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 29 | struct clk { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 30 | struct clk *parent; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 31 | unsigned long rate; |
| 32 | int users; |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 33 | int sw_locked; |
Hartley Sweeten | c3e3bad | 2009-07-06 17:40:53 +0100 | [diff] [blame] | 34 | void __iomem *enable_reg; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 35 | u32 enable_mask; |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 36 | |
| 37 | unsigned long (*get_rate)(struct clk *clk); |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 38 | int (*set_rate)(struct clk *clk, unsigned long rate); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 41 | |
| 42 | static unsigned long get_uart_rate(struct clk *clk); |
| 43 | |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 44 | static int set_keytchclk_rate(struct clk *clk, unsigned long rate); |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 45 | static int set_div_rate(struct clk *clk, unsigned long rate); |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 46 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 47 | |
| 48 | static struct clk clk_xtali = { |
| 49 | .rate = EP93XX_EXT_CLK_RATE, |
| 50 | }; |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 51 | static struct clk clk_uart1 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 52 | .parent = &clk_xtali, |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 53 | .sw_locked = 1, |
Hartley Sweeten | 02239f0 | 2009-07-08 02:00:49 +0100 | [diff] [blame] | 54 | .enable_reg = EP93XX_SYSCON_DEVCFG, |
| 55 | .enable_mask = EP93XX_SYSCON_DEVCFG_U1EN, |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 56 | .get_rate = get_uart_rate, |
| 57 | }; |
| 58 | static struct clk clk_uart2 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 59 | .parent = &clk_xtali, |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 60 | .sw_locked = 1, |
Hartley Sweeten | 02239f0 | 2009-07-08 02:00:49 +0100 | [diff] [blame] | 61 | .enable_reg = EP93XX_SYSCON_DEVCFG, |
| 62 | .enable_mask = EP93XX_SYSCON_DEVCFG_U2EN, |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 63 | .get_rate = get_uart_rate, |
| 64 | }; |
| 65 | static struct clk clk_uart3 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 66 | .parent = &clk_xtali, |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 67 | .sw_locked = 1, |
Hartley Sweeten | 02239f0 | 2009-07-08 02:00:49 +0100 | [diff] [blame] | 68 | .enable_reg = EP93XX_SYSCON_DEVCFG, |
| 69 | .enable_mask = EP93XX_SYSCON_DEVCFG_U3EN, |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 70 | .get_rate = get_uart_rate, |
Russell King | ed519de | 2007-04-22 12:30:41 +0100 | [diff] [blame] | 71 | }; |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 72 | static struct clk clk_pll1 = { |
| 73 | .parent = &clk_xtali, |
| 74 | }; |
| 75 | static struct clk clk_f = { |
| 76 | .parent = &clk_pll1, |
| 77 | }; |
| 78 | static struct clk clk_h = { |
| 79 | .parent = &clk_pll1, |
| 80 | }; |
| 81 | static struct clk clk_p = { |
| 82 | .parent = &clk_pll1, |
| 83 | }; |
| 84 | static struct clk clk_pll2 = { |
| 85 | .parent = &clk_xtali, |
| 86 | }; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 87 | static struct clk clk_usb_host = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 88 | .parent = &clk_pll2, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 89 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 90 | .enable_mask = EP93XX_SYSCON_PWRCNT_USH_EN, |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 91 | }; |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 92 | static struct clk clk_keypad = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 93 | .parent = &clk_xtali, |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 94 | .sw_locked = 1, |
| 95 | .enable_reg = EP93XX_SYSCON_KEYTCHCLKDIV, |
| 96 | .enable_mask = EP93XX_SYSCON_KEYTCHCLKDIV_KEN, |
| 97 | .set_rate = set_keytchclk_rate, |
| 98 | }; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 99 | static struct clk clk_pwm = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 100 | .parent = &clk_xtali, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 101 | .rate = EP93XX_EXT_CLK_RATE, |
| 102 | }; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 103 | |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 104 | static struct clk clk_video = { |
| 105 | .sw_locked = 1, |
| 106 | .enable_reg = EP93XX_SYSCON_VIDCLKDIV, |
| 107 | .enable_mask = EP93XX_SYSCON_CLKDIV_ENABLE, |
| 108 | .set_rate = set_div_rate, |
| 109 | }; |
| 110 | |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 111 | /* DMA Clocks */ |
| 112 | static struct clk clk_m2p0 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 113 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 114 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 115 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P0, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 116 | }; |
| 117 | static struct clk clk_m2p1 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 118 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 119 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 120 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P1, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 121 | }; |
| 122 | static struct clk clk_m2p2 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 123 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 124 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 125 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P2, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 126 | }; |
| 127 | static struct clk clk_m2p3 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 128 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 129 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 130 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P3, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 131 | }; |
| 132 | static struct clk clk_m2p4 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 133 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 134 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 135 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P4, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 136 | }; |
| 137 | static struct clk clk_m2p5 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 138 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 139 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 140 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P5, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 141 | }; |
| 142 | static struct clk clk_m2p6 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 143 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 144 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 145 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P6, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 146 | }; |
| 147 | static struct clk clk_m2p7 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 148 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 149 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 150 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P7, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 151 | }; |
| 152 | static struct clk clk_m2p8 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 153 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 154 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 155 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P8, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 156 | }; |
| 157 | static struct clk clk_m2p9 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 158 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 159 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 160 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P9, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 161 | }; |
| 162 | static struct clk clk_m2m0 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 163 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 164 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 165 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M0, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 166 | }; |
| 167 | static struct clk clk_m2m1 = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 168 | .parent = &clk_h, |
Hartley Sweeten | 4070243 | 2009-05-28 20:07:03 +0100 | [diff] [blame] | 169 | .enable_reg = EP93XX_SYSCON_PWRCNT, |
| 170 | .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M1, |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 171 | }; |
| 172 | |
Russell King | ae696fd | 2008-11-30 17:11:49 +0000 | [diff] [blame] | 173 | #define INIT_CK(dev,con,ck) \ |
| 174 | { .dev_id = dev, .con_id = con, .clk = ck } |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 175 | |
Russell King | ae696fd | 2008-11-30 17:11:49 +0000 | [diff] [blame] | 176 | static struct clk_lookup clocks[] = { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 177 | INIT_CK(NULL, "xtali", &clk_xtali), |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 178 | INIT_CK("apb:uart1", NULL, &clk_uart1), |
| 179 | INIT_CK("apb:uart2", NULL, &clk_uart2), |
| 180 | INIT_CK("apb:uart3", NULL, &clk_uart3), |
| 181 | INIT_CK(NULL, "pll1", &clk_pll1), |
| 182 | INIT_CK(NULL, "fclk", &clk_f), |
| 183 | INIT_CK(NULL, "hclk", &clk_h), |
| 184 | INIT_CK(NULL, "pclk", &clk_p), |
| 185 | INIT_CK(NULL, "pll2", &clk_pll2), |
| 186 | INIT_CK("ep93xx-ohci", NULL, &clk_usb_host), |
| 187 | INIT_CK("ep93xx-keypad", NULL, &clk_keypad), |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 188 | INIT_CK("ep93xx-fb", NULL, &clk_video), |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 189 | INIT_CK(NULL, "pwm_clk", &clk_pwm), |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 190 | INIT_CK(NULL, "m2p0", &clk_m2p0), |
| 191 | INIT_CK(NULL, "m2p1", &clk_m2p1), |
| 192 | INIT_CK(NULL, "m2p2", &clk_m2p2), |
| 193 | INIT_CK(NULL, "m2p3", &clk_m2p3), |
| 194 | INIT_CK(NULL, "m2p4", &clk_m2p4), |
| 195 | INIT_CK(NULL, "m2p5", &clk_m2p5), |
| 196 | INIT_CK(NULL, "m2p6", &clk_m2p6), |
| 197 | INIT_CK(NULL, "m2p7", &clk_m2p7), |
| 198 | INIT_CK(NULL, "m2p8", &clk_m2p8), |
| 199 | INIT_CK(NULL, "m2p9", &clk_m2p9), |
| 200 | INIT_CK(NULL, "m2m0", &clk_m2m0), |
| 201 | INIT_CK(NULL, "m2m1", &clk_m2m1), |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 202 | }; |
| 203 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 204 | static DEFINE_SPINLOCK(clk_lock); |
| 205 | |
| 206 | static void __clk_enable(struct clk *clk) |
| 207 | { |
| 208 | if (!clk->users++) { |
| 209 | if (clk->parent) |
| 210 | __clk_enable(clk->parent); |
| 211 | |
| 212 | if (clk->enable_reg) { |
| 213 | u32 v; |
| 214 | |
| 215 | v = __raw_readl(clk->enable_reg); |
| 216 | v |= clk->enable_mask; |
| 217 | if (clk->sw_locked) |
| 218 | ep93xx_syscon_swlocked_write(v, clk->enable_reg); |
| 219 | else |
| 220 | __raw_writel(v, clk->enable_reg); |
| 221 | } |
| 222 | } |
| 223 | } |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 224 | |
| 225 | int clk_enable(struct clk *clk) |
| 226 | { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 227 | unsigned long flags; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 228 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 229 | if (!clk) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | spin_lock_irqsave(&clk_lock, flags); |
| 233 | __clk_enable(clk); |
| 234 | spin_unlock_irqrestore(&clk_lock, flags); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
Dmitry Baryshkov | 0c5d5b7 | 2008-07-10 14:44:23 +0100 | [diff] [blame] | 238 | EXPORT_SYMBOL(clk_enable); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 239 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 240 | static void __clk_disable(struct clk *clk) |
| 241 | { |
| 242 | if (!--clk->users) { |
| 243 | if (clk->enable_reg) { |
| 244 | u32 v; |
| 245 | |
| 246 | v = __raw_readl(clk->enable_reg); |
| 247 | v &= ~clk->enable_mask; |
| 248 | if (clk->sw_locked) |
| 249 | ep93xx_syscon_swlocked_write(v, clk->enable_reg); |
| 250 | else |
| 251 | __raw_writel(v, clk->enable_reg); |
| 252 | } |
| 253 | |
| 254 | if (clk->parent) |
| 255 | __clk_disable(clk->parent); |
| 256 | } |
| 257 | } |
| 258 | |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 259 | void clk_disable(struct clk *clk) |
| 260 | { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 261 | unsigned long flags; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 262 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 263 | if (!clk) |
| 264 | return; |
| 265 | |
| 266 | spin_lock_irqsave(&clk_lock, flags); |
| 267 | __clk_disable(clk); |
| 268 | spin_unlock_irqrestore(&clk_lock, flags); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 269 | } |
Dmitry Baryshkov | 0c5d5b7 | 2008-07-10 14:44:23 +0100 | [diff] [blame] | 270 | EXPORT_SYMBOL(clk_disable); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 271 | |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 272 | static unsigned long get_uart_rate(struct clk *clk) |
| 273 | { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 274 | unsigned long rate = clk_get_rate(clk->parent); |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 275 | u32 value; |
| 276 | |
Matthias Kaehlcke | ca8cbc8 | 2009-06-11 19:57:34 +0100 | [diff] [blame] | 277 | value = __raw_readl(EP93XX_SYSCON_PWRCNT); |
| 278 | if (value & EP93XX_SYSCON_PWRCNT_UARTBAUD) |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 279 | return rate; |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 280 | else |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 281 | return rate / 2; |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 284 | unsigned long clk_get_rate(struct clk *clk) |
| 285 | { |
Hartley Sweeten | ff05c03 | 2009-05-07 18:41:47 +0100 | [diff] [blame] | 286 | if (clk->get_rate) |
| 287 | return clk->get_rate(clk); |
| 288 | |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 289 | return clk->rate; |
| 290 | } |
Dmitry Baryshkov | 0c5d5b7 | 2008-07-10 14:44:23 +0100 | [diff] [blame] | 291 | EXPORT_SYMBOL(clk_get_rate); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 292 | |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 293 | static int set_keytchclk_rate(struct clk *clk, unsigned long rate) |
| 294 | { |
| 295 | u32 val; |
| 296 | u32 div_bit; |
| 297 | |
| 298 | val = __raw_readl(clk->enable_reg); |
| 299 | |
| 300 | /* |
| 301 | * The Key Matrix and ADC clocks are configured using the same |
| 302 | * System Controller register. The clock used will be either |
| 303 | * 1/4 or 1/16 the external clock rate depending on the |
| 304 | * EP93XX_SYSCON_KEYTCHCLKDIV_KDIV/EP93XX_SYSCON_KEYTCHCLKDIV_ADIV |
| 305 | * bit being set or cleared. |
| 306 | */ |
| 307 | div_bit = clk->enable_mask >> 15; |
| 308 | |
| 309 | if (rate == EP93XX_KEYTCHCLK_DIV4) |
| 310 | val |= div_bit; |
| 311 | else if (rate == EP93XX_KEYTCHCLK_DIV16) |
| 312 | val &= ~div_bit; |
| 313 | else |
| 314 | return -EINVAL; |
| 315 | |
| 316 | ep93xx_syscon_swlocked_write(val, clk->enable_reg); |
| 317 | clk->rate = rate; |
| 318 | return 0; |
| 319 | } |
| 320 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 321 | static int calc_clk_div(struct clk *clk, unsigned long rate, |
| 322 | int *psel, int *esel, int *pdiv, int *div) |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 323 | { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 324 | struct clk *mclk; |
| 325 | unsigned long max_rate, actual_rate, mclk_rate, rate_err = -1; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 326 | int i, found = 0, __div = 0, __pdiv = 0; |
| 327 | |
| 328 | /* Don't exceed the maximum rate */ |
| 329 | max_rate = max(max(clk_pll1.rate / 4, clk_pll2.rate / 4), |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 330 | clk_xtali.rate / 4); |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 331 | rate = min(rate, max_rate); |
| 332 | |
| 333 | /* |
| 334 | * Try the two pll's and the external clock |
| 335 | * Because the valid predividers are 2, 2.5 and 3, we multiply |
| 336 | * all the clocks by 2 to avoid floating point math. |
| 337 | * |
| 338 | * This is based on the algorithm in the ep93xx raster guide: |
| 339 | * http://be-a-maverick.com/en/pubs/appNote/AN269REV1.pdf |
| 340 | * |
| 341 | */ |
| 342 | for (i = 0; i < 3; i++) { |
| 343 | if (i == 0) |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 344 | mclk = &clk_xtali; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 345 | else if (i == 1) |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 346 | mclk = &clk_pll1; |
| 347 | else |
| 348 | mclk = &clk_pll2; |
| 349 | mclk_rate = mclk->rate * 2; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 350 | |
| 351 | /* Try each predivider value */ |
| 352 | for (__pdiv = 4; __pdiv <= 6; __pdiv++) { |
| 353 | __div = mclk_rate / (rate * __pdiv); |
| 354 | if (__div < 2 || __div > 127) |
| 355 | continue; |
| 356 | |
| 357 | actual_rate = mclk_rate / (__pdiv * __div); |
| 358 | |
| 359 | if (!found || abs(actual_rate - rate) < rate_err) { |
| 360 | *pdiv = __pdiv - 3; |
| 361 | *div = __div; |
| 362 | *psel = (i == 2); |
| 363 | *esel = (i != 0); |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 364 | clk->parent = mclk; |
| 365 | clk->rate = actual_rate; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 366 | rate_err = abs(actual_rate - rate); |
| 367 | found = 1; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (!found) |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 373 | return -EINVAL; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 374 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 375 | return 0; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int set_div_rate(struct clk *clk, unsigned long rate) |
| 379 | { |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 380 | int err, psel = 0, esel = 0, pdiv = 0, div = 0; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 381 | u32 val; |
| 382 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 383 | err = calc_clk_div(clk, rate, &psel, &esel, &pdiv, &div); |
| 384 | if (err) |
| 385 | return err; |
Ryan Mallon | c601218 | 2009-09-22 16:47:09 -0700 | [diff] [blame] | 386 | |
| 387 | /* Clear the esel, psel, pdiv and div bits */ |
| 388 | val = __raw_readl(clk->enable_reg); |
| 389 | val &= ~0x7fff; |
| 390 | |
| 391 | /* Set the new esel, psel, pdiv and div bits for the new clock rate */ |
| 392 | val |= (esel ? EP93XX_SYSCON_CLKDIV_ESEL : 0) | |
| 393 | (psel ? EP93XX_SYSCON_CLKDIV_PSEL : 0) | |
| 394 | (pdiv << EP93XX_SYSCON_CLKDIV_PDIV_SHIFT) | div; |
| 395 | ep93xx_syscon_swlocked_write(val, clk->enable_reg); |
| 396 | return 0; |
| 397 | } |
| 398 | |
Hartley Sweeten | 701fac8 | 2009-06-30 23:06:43 +0100 | [diff] [blame] | 399 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 400 | { |
| 401 | if (clk->set_rate) |
| 402 | return clk->set_rate(clk, rate); |
| 403 | |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | EXPORT_SYMBOL(clk_set_rate); |
| 407 | |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 408 | |
| 409 | static char fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 }; |
| 410 | static char hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 }; |
| 411 | static char pclk_divisors[] = { 1, 2, 4, 8 }; |
| 412 | |
| 413 | /* |
| 414 | * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS |
| 415 | */ |
| 416 | static unsigned long calc_pll_rate(u32 config_word) |
| 417 | { |
| 418 | unsigned long long rate; |
| 419 | int i; |
| 420 | |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 421 | rate = clk_xtali.rate; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 422 | rate *= ((config_word >> 11) & 0x1f) + 1; /* X1FBD */ |
| 423 | rate *= ((config_word >> 5) & 0x3f) + 1; /* X2FBD */ |
| 424 | do_div(rate, (config_word & 0x1f) + 1); /* X2IPD */ |
| 425 | for (i = 0; i < ((config_word >> 16) & 3); i++) /* PS */ |
| 426 | rate >>= 1; |
| 427 | |
| 428 | return (unsigned long)rate; |
| 429 | } |
| 430 | |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 431 | static void __init ep93xx_dma_clock_init(void) |
| 432 | { |
| 433 | clk_m2p0.rate = clk_h.rate; |
| 434 | clk_m2p1.rate = clk_h.rate; |
| 435 | clk_m2p2.rate = clk_h.rate; |
| 436 | clk_m2p3.rate = clk_h.rate; |
| 437 | clk_m2p4.rate = clk_h.rate; |
| 438 | clk_m2p5.rate = clk_h.rate; |
| 439 | clk_m2p6.rate = clk_h.rate; |
| 440 | clk_m2p7.rate = clk_h.rate; |
| 441 | clk_m2p8.rate = clk_h.rate; |
| 442 | clk_m2p9.rate = clk_h.rate; |
| 443 | clk_m2m0.rate = clk_h.rate; |
| 444 | clk_m2m1.rate = clk_h.rate; |
| 445 | } |
| 446 | |
Lennert Buytenhek | 51dd249 | 2007-02-04 22:45:33 +0100 | [diff] [blame] | 447 | static int __init ep93xx_clock_init(void) |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 448 | { |
| 449 | u32 value; |
Russell King | ae696fd | 2008-11-30 17:11:49 +0000 | [diff] [blame] | 450 | int i; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 451 | |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 452 | /* Determine the bootloader configured pll1 rate */ |
| 453 | value = __raw_readl(EP93XX_SYSCON_CLKSET1); |
| 454 | if (!(value & EP93XX_SYSCON_CLKSET1_NBYP1)) |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 455 | clk_pll1.rate = clk_xtali.rate; |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 456 | else |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 457 | clk_pll1.rate = calc_pll_rate(value); |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 458 | |
| 459 | /* Initialize the pll1 derived clocks */ |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 460 | clk_f.rate = clk_pll1.rate / fclk_divisors[(value >> 25) & 0x7]; |
| 461 | clk_h.rate = clk_pll1.rate / hclk_divisors[(value >> 20) & 0x7]; |
| 462 | clk_p.rate = clk_h.rate / pclk_divisors[(value >> 18) & 0x3]; |
Ryan Mallon | 1c8daab | 2009-02-25 22:22:38 +0100 | [diff] [blame] | 463 | ep93xx_dma_clock_init(); |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 464 | |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 465 | /* Determine the bootloader configured pll2 rate */ |
Hartley Sweeten | ba7c6a3 | 2010-02-23 21:20:31 +0100 | [diff] [blame^] | 466 | value = __raw_readl(EP93XX_SYSCON_CLKSET2); |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 467 | if (!(value & EP93XX_SYSCON_CLKSET2_NBYP2)) |
Hartley Sweeten | ebd00c0 | 2009-10-08 23:44:41 +0100 | [diff] [blame] | 468 | clk_pll2.rate = clk_xtali.rate; |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 469 | else if (value & EP93XX_SYSCON_CLKSET2_PLL2_EN) |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 470 | clk_pll2.rate = calc_pll_rate(value); |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 471 | else |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 472 | clk_pll2.rate = 0; |
Hartley Sweeten | 346e34a | 2010-01-11 21:41:29 +0100 | [diff] [blame] | 473 | |
| 474 | /* Initialize the pll2 derived clocks */ |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 475 | clk_usb_host.rate = clk_pll2.rate / (((value >> 28) & 0xf) + 1); |
| 476 | |
Hartley Sweeten | 99acbb9 | 2010-01-11 18:30:41 +0100 | [diff] [blame] | 477 | pr_info("PLL1 running at %ld MHz, PLL2 at %ld MHz\n", |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 478 | clk_pll1.rate / 1000000, clk_pll2.rate / 1000000); |
Hartley Sweeten | 99acbb9 | 2010-01-11 18:30:41 +0100 | [diff] [blame] | 479 | pr_info("FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n", |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 480 | clk_f.rate / 1000000, clk_h.rate / 1000000, |
| 481 | clk_p.rate / 1000000); |
Lennert Buytenhek | 51dd249 | 2007-02-04 22:45:33 +0100 | [diff] [blame] | 482 | |
Russell King | ae696fd | 2008-11-30 17:11:49 +0000 | [diff] [blame] | 483 | for (i = 0; i < ARRAY_SIZE(clocks); i++) |
| 484 | clkdev_add(&clocks[i]); |
Lennert Buytenhek | 51dd249 | 2007-02-04 22:45:33 +0100 | [diff] [blame] | 485 | return 0; |
Lennert Buytenhek | 1d81eed | 2006-06-24 10:33:02 +0100 | [diff] [blame] | 486 | } |
Lennert Buytenhek | 51dd249 | 2007-02-04 22:45:33 +0100 | [diff] [blame] | 487 | arch_initcall(ep93xx_clock_init); |