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