Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: sunxi: mod0: Introduce MMC proper phase handling

The MMC clock we thought we had until now are actually not one but three
different clocks.

The main one is unchanged, and will have three outputs:
- The clock fed into the MMC
- a sample and output clocks, to deal with when should we output/sample data
to/from the MMC bus

The phase control we had are actually controlling the two latter clocks, but
the main MMC one is unchanged.

We can adjust the phase with a 3 bits value, from 0 to 7, 0 meaning a 180 phase
shift, and the other values being the number of periods from the MMC parent
clock to outphase the clock of.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Hans de Goede <hdegoede@redhat.com>

+191
+2
Documentation/devicetree/bindings/clock/sunxi.txt
··· 47 47 "allwinner,sun6i-a31-apb2-gates-clk" - for the APB2 gates on A31 48 48 "allwinner,sun8i-a23-apb2-gates-clk" - for the APB2 gates on A23 49 49 "allwinner,sun5i-a13-mbus-clk" - for the MBUS clock on A13 50 + "allwinner,sun4i-a10-mmc-output-clk" - for the MMC output clock on A10 51 + "allwinner,sun4i-a10-mmc-sample-clk" - for the MMC sample clock on A10 50 52 "allwinner,sun4i-a10-mod0-clk" - for the module 0 family of clocks 51 53 "allwinner,sun7i-a20-out-clk" - for the external output clocks 52 54 "allwinner,sun7i-a20-gmac-clk" - for the GMAC clock module on A20/A31
+189
drivers/clk/sunxi/clk-mod0.c
··· 16 16 17 17 #include <linux/clk-provider.h> 18 18 #include <linux/clkdev.h> 19 + #include <linux/of_address.h> 19 20 20 21 #include "clk-factors.h" 21 22 ··· 93 92 clk_prepare_enable(mbus); 94 93 } 95 94 CLK_OF_DECLARE(sun5i_a13_mbus, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup); 95 + 96 + struct mmc_phase_data { 97 + u8 offset; 98 + }; 99 + 100 + struct mmc_phase { 101 + struct clk_hw hw; 102 + void __iomem *reg; 103 + struct mmc_phase_data *data; 104 + spinlock_t *lock; 105 + }; 106 + 107 + #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw) 108 + 109 + static int mmc_get_phase(struct clk_hw *hw) 110 + { 111 + struct clk *mmc, *mmc_parent, *clk = hw->clk; 112 + struct mmc_phase *phase = to_mmc_phase(hw); 113 + unsigned int mmc_rate, mmc_parent_rate; 114 + u16 step, mmc_div; 115 + u32 value; 116 + u8 delay; 117 + 118 + value = readl(phase->reg); 119 + delay = (value >> phase->data->offset) & 0x3; 120 + 121 + if (!delay) 122 + return 180; 123 + 124 + /* Get the main MMC clock */ 125 + mmc = clk_get_parent(clk); 126 + if (!mmc) 127 + return -EINVAL; 128 + 129 + /* And its rate */ 130 + mmc_rate = clk_get_rate(mmc); 131 + if (!mmc_rate) 132 + return -EINVAL; 133 + 134 + /* Now, get the MMC parent (most likely some PLL) */ 135 + mmc_parent = clk_get_parent(mmc); 136 + if (!mmc_parent) 137 + return -EINVAL; 138 + 139 + /* And its rate */ 140 + mmc_parent_rate = clk_get_rate(mmc_parent); 141 + if (!mmc_parent_rate) 142 + return -EINVAL; 143 + 144 + /* Get MMC clock divider */ 145 + mmc_div = mmc_parent_rate / mmc_rate; 146 + 147 + step = DIV_ROUND_CLOSEST(360, mmc_div); 148 + return delay * step; 149 + } 150 + 151 + static int mmc_set_phase(struct clk_hw *hw, int degrees) 152 + { 153 + struct clk *mmc, *mmc_parent, *clk = hw->clk; 154 + struct mmc_phase *phase = to_mmc_phase(hw); 155 + unsigned int mmc_rate, mmc_parent_rate; 156 + unsigned long flags; 157 + u32 value; 158 + u8 delay; 159 + 160 + /* Get the main MMC clock */ 161 + mmc = clk_get_parent(clk); 162 + if (!mmc) 163 + return -EINVAL; 164 + 165 + /* And its rate */ 166 + mmc_rate = clk_get_rate(mmc); 167 + if (!mmc_rate) 168 + return -EINVAL; 169 + 170 + /* Now, get the MMC parent (most likely some PLL) */ 171 + mmc_parent = clk_get_parent(mmc); 172 + if (!mmc_parent) 173 + return -EINVAL; 174 + 175 + /* And its rate */ 176 + mmc_parent_rate = clk_get_rate(mmc_parent); 177 + if (!mmc_parent_rate) 178 + return -EINVAL; 179 + 180 + if (degrees != 180) { 181 + u16 step, mmc_div; 182 + 183 + /* Get MMC clock divider */ 184 + mmc_div = mmc_parent_rate / mmc_rate; 185 + 186 + /* 187 + * We can only outphase the clocks by multiple of the 188 + * PLL's period. 189 + * 190 + * Since the MMC clock in only a divider, and the 191 + * formula to get the outphasing in degrees is deg = 192 + * 360 * delta / period 193 + * 194 + * If we simplify this formula, we can see that the 195 + * only thing that we're concerned about is the number 196 + * of period we want to outphase our clock from, and 197 + * the divider set by the MMC clock. 198 + */ 199 + step = DIV_ROUND_CLOSEST(360, mmc_div); 200 + delay = DIV_ROUND_CLOSEST(degrees, step); 201 + } else { 202 + delay = 0; 203 + } 204 + 205 + spin_lock_irqsave(phase->lock, flags); 206 + value = readl(phase->reg); 207 + value &= ~GENMASK(phase->data->offset + 3, phase->data->offset); 208 + value |= delay << phase->data->offset; 209 + writel(value, phase->reg); 210 + spin_unlock_irqrestore(phase->lock, flags); 211 + 212 + return 0; 213 + } 214 + 215 + static const struct clk_ops mmc_clk_ops = { 216 + .get_phase = mmc_get_phase, 217 + .set_phase = mmc_set_phase, 218 + }; 219 + 220 + static void __init sun4i_a10_mmc_phase_setup(struct device_node *node, 221 + struct mmc_phase_data *data) 222 + { 223 + const char *parent_names[1] = { of_clk_get_parent_name(node, 0) }; 224 + struct clk_init_data init = { 225 + .num_parents = 1, 226 + .parent_names = parent_names, 227 + .ops = &mmc_clk_ops, 228 + }; 229 + 230 + struct mmc_phase *phase; 231 + struct clk *clk; 232 + 233 + phase = kmalloc(sizeof(*phase), GFP_KERNEL); 234 + if (!phase) 235 + return; 236 + 237 + phase->hw.init = &init; 238 + 239 + phase->reg = of_iomap(node, 0); 240 + if (!phase->reg) 241 + goto err_free; 242 + 243 + phase->data = data; 244 + phase->lock = &sun4i_a10_mod0_lock; 245 + 246 + if (of_property_read_string(node, "clock-output-names", &init.name)) 247 + init.name = node->name; 248 + 249 + clk = clk_register(NULL, &phase->hw); 250 + if (IS_ERR(clk)) 251 + goto err_unmap; 252 + 253 + of_clk_add_provider(node, of_clk_src_simple_get, clk); 254 + 255 + return; 256 + 257 + err_unmap: 258 + iounmap(phase->reg); 259 + err_free: 260 + kfree(phase); 261 + } 262 + 263 + 264 + static struct mmc_phase_data mmc_output_clk = { 265 + .offset = 8, 266 + }; 267 + 268 + static struct mmc_phase_data mmc_sample_clk = { 269 + .offset = 20, 270 + }; 271 + 272 + static void __init sun4i_a10_mmc_output_setup(struct device_node *node) 273 + { 274 + sun4i_a10_mmc_phase_setup(node, &mmc_output_clk); 275 + } 276 + CLK_OF_DECLARE(sun4i_a10_mmc_output, "allwinner,sun4i-a10-mmc-output-clk", sun4i_a10_mmc_output_setup); 277 + 278 + static void __init sun4i_a10_mmc_sample_setup(struct device_node *node) 279 + { 280 + sun4i_a10_mmc_phase_setup(node, &mmc_sample_clk); 281 + } 282 + CLK_OF_DECLARE(sun4i_a10_mmc_sample, "allwinner,sun4i-a10-mmc-sample-clk", sun4i_a10_mmc_sample_setup);