blob: 58008b3e8bc175f8befb8126e99e5f55c57d2176 [file] [log] [blame]
Boris BREZILLON5fba62e2013-10-11 11:41:41 +02001/*
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 Hiblotcce6db82014-03-11 10:00:34 +010017#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 BREZILLON5fba62e2013-10-11 11:41:41 +020022
23#include "pmc.h"
24
25#define SYSTEM_MAX_ID 31
26
27#define SYSTEM_MAX_NAME_SZ 32
28
29#define to_clk_system(hw) container_of(hw, struct clk_system, hw)
30struct clk_system {
31 struct clk_hw hw;
32 struct at91_pmc *pmc;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010033 unsigned int irq;
34 wait_queue_head_t wait;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020035 u8 id;
36};
37
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010038static inline int is_pck(int id)
39{
40 return (id >= 8) && (id <= 15);
41}
42static irqreturn_t clk_system_irq_handler(int irq, void *dev_id)
43{
44 struct clk_system *sys = (struct clk_system *)dev_id;
45
46 wake_up(&sys->wait);
47 disable_irq_nosync(sys->irq);
48
49 return IRQ_HANDLED;
50}
51
52static int clk_system_prepare(struct clk_hw *hw)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020053{
54 struct clk_system *sys = to_clk_system(hw);
55 struct at91_pmc *pmc = sys->pmc;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010056 u32 mask = 1 << sys->id;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020057
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010058 pmc_write(pmc, AT91_PMC_SCER, mask);
59
60 if (!is_pck(sys->id))
61 return 0;
62
63 while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
64 if (sys->irq) {
65 enable_irq(sys->irq);
66 wait_event(sys->wait,
67 pmc_read(pmc, AT91_PMC_SR) & mask);
68 } else
69 cpu_relax();
70 }
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020071 return 0;
72}
73
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010074static void clk_system_unprepare(struct clk_hw *hw)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020075{
76 struct clk_system *sys = to_clk_system(hw);
77 struct at91_pmc *pmc = sys->pmc;
78
79 pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
80}
81
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010082static int clk_system_is_prepared(struct clk_hw *hw)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020083{
84 struct clk_system *sys = to_clk_system(hw);
85 struct at91_pmc *pmc = sys->pmc;
86
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010087 if (!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id)))
88 return 0;
89
90 if (!is_pck(sys->id))
91 return 1;
92
93 return !!(pmc_read(pmc, AT91_PMC_SR) & (1 << sys->id));
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020094}
95
96static const struct clk_ops system_ops = {
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010097 .prepare = clk_system_prepare,
98 .unprepare = clk_system_unprepare,
99 .is_prepared = clk_system_is_prepared,
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200100};
101
102static struct clk * __init
103at91_clk_register_system(struct at91_pmc *pmc, const char *name,
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100104 const char *parent_name, u8 id, int irq)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200105{
106 struct clk_system *sys;
107 struct clk *clk = NULL;
108 struct clk_init_data init;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100109 int ret;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200110
111 if (!parent_name || id > SYSTEM_MAX_ID)
112 return ERR_PTR(-EINVAL);
113
114 sys = kzalloc(sizeof(*sys), GFP_KERNEL);
115 if (!sys)
116 return ERR_PTR(-ENOMEM);
117
118 init.name = name;
119 init.ops = &system_ops;
120 init.parent_names = &parent_name;
121 init.num_parents = 1;
Alexandre Bellonib736bcb2014-07-08 18:21:16 +0200122 init.flags = CLK_SET_RATE_PARENT;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200123
124 sys->id = id;
125 sys->hw.init = &init;
126 sys->pmc = pmc;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100127 sys->irq = irq;
128 if (irq) {
129 init_waitqueue_head(&sys->wait);
130 irq_set_status_flags(sys->irq, IRQ_NOAUTOEN);
131 ret = request_irq(sys->irq, clk_system_irq_handler,
132 IRQF_TRIGGER_HIGH, name, sys);
David Dueckc76a0242015-06-26 15:30:22 +0200133 if (ret) {
134 kfree(sys);
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100135 return ERR_PTR(ret);
David Dueckc76a0242015-06-26 15:30:22 +0200136 }
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100137 }
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200138
139 clk = clk_register(NULL, &sys->hw);
David Dueckc76a0242015-06-26 15:30:22 +0200140 if (IS_ERR(clk)) {
141 free_irq(sys->irq, sys);
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200142 kfree(sys);
David Dueckc76a0242015-06-26 15:30:22 +0200143 }
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200144
145 return clk;
146}
147
148static void __init
149of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
150{
151 int num;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100152 int irq = 0;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200153 u32 id;
154 struct clk *clk;
155 const char *name;
156 struct device_node *sysclknp;
157 const char *parent_name;
158
159 num = of_get_child_count(np);
160 if (num > (SYSTEM_MAX_ID + 1))
161 return;
162
163 for_each_child_of_node(np, sysclknp) {
164 if (of_property_read_u32(sysclknp, "reg", &id))
165 continue;
166
167 if (of_property_read_string(np, "clock-output-names", &name))
168 name = sysclknp->name;
169
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100170 if (is_pck(id))
171 irq = irq_of_parse_and_map(sysclknp, 0);
172
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200173 parent_name = of_clk_get_parent_name(sysclknp, 0);
174
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100175 clk = at91_clk_register_system(pmc, name, parent_name, id, irq);
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200176 if (IS_ERR(clk))
177 continue;
178
179 of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
180 }
181}
182
183void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
184 struct at91_pmc *pmc)
185{
186 of_at91_clk_sys_setup(np, pmc);
187}