Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk-provider.h> |
| 12 | #include <linux/clkdev.h> |
| 13 | #include <linux/clk/at91_pmc.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/io.h> |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 17 | #include <linux/irq.h> |
| 18 | #include <linux/of_irq.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <linux/sched.h> |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 22 | #include <linux/mfd/syscon.h> |
| 23 | #include <linux/regmap.h> |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 24 | |
| 25 | #include "pmc.h" |
| 26 | |
| 27 | #define SYSTEM_MAX_ID 31 |
| 28 | |
| 29 | #define SYSTEM_MAX_NAME_SZ 32 |
| 30 | |
| 31 | #define to_clk_system(hw) container_of(hw, struct clk_system, hw) |
| 32 | struct clk_system { |
| 33 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 34 | struct regmap *regmap; |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 35 | unsigned int irq; |
| 36 | wait_queue_head_t wait; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 37 | u8 id; |
| 38 | }; |
| 39 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 40 | static inline int is_pck(int id) |
| 41 | { |
| 42 | return (id >= 8) && (id <= 15); |
| 43 | } |
| 44 | static irqreturn_t clk_system_irq_handler(int irq, void *dev_id) |
| 45 | { |
| 46 | struct clk_system *sys = (struct clk_system *)dev_id; |
| 47 | |
| 48 | wake_up(&sys->wait); |
| 49 | disable_irq_nosync(sys->irq); |
| 50 | |
| 51 | return IRQ_HANDLED; |
| 52 | } |
| 53 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 54 | static inline bool clk_system_ready(struct regmap *regmap, int id) |
| 55 | { |
| 56 | unsigned int status; |
| 57 | |
| 58 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 59 | |
| 60 | return status & (1 << id) ? 1 : 0; |
| 61 | } |
| 62 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 63 | static int clk_system_prepare(struct clk_hw *hw) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 64 | { |
| 65 | struct clk_system *sys = to_clk_system(hw); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 66 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 67 | regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id); |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 68 | |
| 69 | if (!is_pck(sys->id)) |
| 70 | return 0; |
| 71 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 72 | while (!clk_system_ready(sys->regmap, sys->id)) { |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 73 | if (sys->irq) { |
| 74 | enable_irq(sys->irq); |
| 75 | wait_event(sys->wait, |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 76 | clk_system_ready(sys->regmap, sys->id)); |
| 77 | } else { |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 78 | cpu_relax(); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 79 | } |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 80 | } |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 84 | static void clk_system_unprepare(struct clk_hw *hw) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 85 | { |
| 86 | struct clk_system *sys = to_clk_system(hw); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 87 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 88 | regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 91 | static int clk_system_is_prepared(struct clk_hw *hw) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 92 | { |
| 93 | struct clk_system *sys = to_clk_system(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 94 | unsigned int status; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 95 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 96 | regmap_read(sys->regmap, AT91_PMC_SCSR, &status); |
| 97 | |
| 98 | if (!(status & (1 << sys->id))) |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 99 | return 0; |
| 100 | |
| 101 | if (!is_pck(sys->id)) |
| 102 | return 1; |
| 103 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 104 | regmap_read(sys->regmap, AT91_PMC_SR, &status); |
| 105 | |
| 106 | return status & (1 << sys->id) ? 1 : 0; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static const struct clk_ops system_ops = { |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 110 | .prepare = clk_system_prepare, |
| 111 | .unprepare = clk_system_unprepare, |
| 112 | .is_prepared = clk_system_is_prepared, |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static struct clk * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 116 | at91_clk_register_system(struct regmap *regmap, const char *name, |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 117 | const char *parent_name, u8 id, int irq) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 118 | { |
| 119 | struct clk_system *sys; |
| 120 | struct clk *clk = NULL; |
| 121 | struct clk_init_data init; |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 122 | int ret; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 123 | |
| 124 | if (!parent_name || id > SYSTEM_MAX_ID) |
| 125 | return ERR_PTR(-EINVAL); |
| 126 | |
| 127 | sys = kzalloc(sizeof(*sys), GFP_KERNEL); |
| 128 | if (!sys) |
| 129 | return ERR_PTR(-ENOMEM); |
| 130 | |
| 131 | init.name = name; |
| 132 | init.ops = &system_ops; |
| 133 | init.parent_names = &parent_name; |
| 134 | init.num_parents = 1; |
Alexandre Belloni | b736bcb | 2014-07-08 18:21:16 +0200 | [diff] [blame] | 135 | init.flags = CLK_SET_RATE_PARENT; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 136 | |
| 137 | sys->id = id; |
| 138 | sys->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 139 | sys->regmap = regmap; |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 140 | sys->irq = irq; |
| 141 | if (irq) { |
| 142 | init_waitqueue_head(&sys->wait); |
| 143 | irq_set_status_flags(sys->irq, IRQ_NOAUTOEN); |
| 144 | ret = request_irq(sys->irq, clk_system_irq_handler, |
| 145 | IRQF_TRIGGER_HIGH, name, sys); |
David Dueck | c76a024 | 2015-06-26 15:30:22 +0200 | [diff] [blame] | 146 | if (ret) { |
| 147 | kfree(sys); |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 148 | return ERR_PTR(ret); |
David Dueck | c76a024 | 2015-06-26 15:30:22 +0200 | [diff] [blame] | 149 | } |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 150 | } |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 151 | |
| 152 | clk = clk_register(NULL, &sys->hw); |
David Dueck | c76a024 | 2015-06-26 15:30:22 +0200 | [diff] [blame] | 153 | if (IS_ERR(clk)) { |
Alexandre Belloni | a97cea2 | 2015-09-11 16:34:07 +0200 | [diff] [blame] | 154 | if (irq) |
| 155 | free_irq(sys->irq, sys); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 156 | kfree(sys); |
David Dueck | c76a024 | 2015-06-26 15:30:22 +0200 | [diff] [blame] | 157 | } |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 158 | |
| 159 | return clk; |
| 160 | } |
| 161 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 162 | static void __init of_at91rm9200_clk_sys_setup(struct device_node *np) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 163 | { |
| 164 | int num; |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 165 | int irq = 0; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 166 | u32 id; |
| 167 | struct clk *clk; |
| 168 | const char *name; |
| 169 | struct device_node *sysclknp; |
| 170 | const char *parent_name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 171 | struct regmap *regmap; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 172 | |
| 173 | num = of_get_child_count(np); |
| 174 | if (num > (SYSTEM_MAX_ID + 1)) |
| 175 | return; |
| 176 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 177 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 178 | if (IS_ERR(regmap)) |
| 179 | return; |
| 180 | |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 181 | for_each_child_of_node(np, sysclknp) { |
| 182 | if (of_property_read_u32(sysclknp, "reg", &id)) |
| 183 | continue; |
| 184 | |
| 185 | if (of_property_read_string(np, "clock-output-names", &name)) |
| 186 | name = sysclknp->name; |
| 187 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 188 | if (is_pck(id)) |
| 189 | irq = irq_of_parse_and_map(sysclknp, 0); |
| 190 | |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 191 | parent_name = of_clk_get_parent_name(sysclknp, 0); |
| 192 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 193 | clk = at91_clk_register_system(regmap, name, parent_name, id, |
| 194 | irq); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 195 | if (IS_ERR(clk)) |
| 196 | continue; |
| 197 | |
| 198 | of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk); |
| 199 | } |
| 200 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame^] | 201 | CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system", |
| 202 | of_at91rm9200_clk_sys_setup); |