2 * Clock driver for Keystone 2 based devices
4 * Copyright (C) 2013 Texas Instruments.
5 * Murali Karicheri <m-karicheri2@ti.com>
6 * Santosh Shilimkar <santosh.shilimkar@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/of_address.h>
20 #include <linux/module.h>
22 /* PSC register offsets */
30 /* PSC module states */
31 #define PSC_STATE_SWRSTDISABLE 0
32 #define PSC_STATE_SYNCRST 1
33 #define PSC_STATE_DISABLE 2
34 #define PSC_STATE_ENABLE 3
36 #define MDSTAT_STATE_MASK 0x3f
37 #define MDSTAT_MCKOUT BIT(12)
38 #define PDSTAT_STATE_MASK 0x1f
39 #define MDCTL_FORCE BIT(31)
40 #define MDCTL_LRESET BIT(8)
41 #define PDCTL_NEXT BIT(0)
43 /* Maximum timeout to bail out state transition for module */
44 #define STATE_TRANS_MAX_COUNT 0xffff
46 static void __iomem
*domain_transition_base
;
49 * struct clk_psc_data - PSC data
50 * @control_base: Base address for a PSC control
51 * @domain_base: Base address for a PSC domain
52 * @domain_id: PSC domain id number
55 void __iomem
*control_base
;
56 void __iomem
*domain_base
;
61 * struct clk_psc - PSC clock structure
62 * @hw: clk_hw for the psc
63 * @psc_data: PSC driver specific data
64 * @lock: Spinlock used by the driver
68 struct clk_psc_data
*psc_data
;
72 static DEFINE_SPINLOCK(psc_lock
);
74 #define to_clk_psc(_hw) container_of(_hw, struct clk_psc, hw)
76 static void psc_config(void __iomem
*control_base
, void __iomem
*domain_base
,
77 u32 next_state
, u32 domain_id
)
79 u32 ptcmd
, pdstat
, pdctl
, mdstat
, mdctl
, ptstat
;
80 u32 count
= STATE_TRANS_MAX_COUNT
;
82 mdctl
= readl(control_base
+ MDCTL
);
83 mdctl
&= ~MDSTAT_STATE_MASK
;
85 /* For disable, we always put the module in local reset */
86 if (next_state
== PSC_STATE_DISABLE
)
87 mdctl
&= ~MDCTL_LRESET
;
88 writel(mdctl
, control_base
+ MDCTL
);
90 pdstat
= readl(domain_base
+ PDSTAT
);
91 if (!(pdstat
& PDSTAT_STATE_MASK
)) {
92 pdctl
= readl(domain_base
+ PDCTL
);
94 writel(pdctl
, domain_base
+ PDCTL
);
97 ptcmd
= 1 << domain_id
;
98 writel(ptcmd
, domain_transition_base
+ PTCMD
);
100 ptstat
= readl(domain_transition_base
+ PTSTAT
);
101 } while (((ptstat
>> domain_id
) & 1) && count
--);
103 count
= STATE_TRANS_MAX_COUNT
;
105 mdstat
= readl(control_base
+ MDSTAT
);
106 } while (!((mdstat
& MDSTAT_STATE_MASK
) == next_state
) && count
--);
109 static int keystone_clk_is_enabled(struct clk_hw
*hw
)
111 struct clk_psc
*psc
= to_clk_psc(hw
);
112 struct clk_psc_data
*data
= psc
->psc_data
;
113 u32 mdstat
= readl(data
->control_base
+ MDSTAT
);
115 return (mdstat
& MDSTAT_MCKOUT
) ? 1 : 0;
118 static int keystone_clk_enable(struct clk_hw
*hw
)
120 struct clk_psc
*psc
= to_clk_psc(hw
);
121 struct clk_psc_data
*data
= psc
->psc_data
;
122 unsigned long flags
= 0;
125 spin_lock_irqsave(psc
->lock
, flags
);
127 psc_config(data
->control_base
, data
->domain_base
,
128 PSC_STATE_ENABLE
, data
->domain_id
);
131 spin_unlock_irqrestore(psc
->lock
, flags
);
136 static void keystone_clk_disable(struct clk_hw
*hw
)
138 struct clk_psc
*psc
= to_clk_psc(hw
);
139 struct clk_psc_data
*data
= psc
->psc_data
;
140 unsigned long flags
= 0;
143 spin_lock_irqsave(psc
->lock
, flags
);
145 psc_config(data
->control_base
, data
->domain_base
,
146 PSC_STATE_DISABLE
, data
->domain_id
);
149 spin_unlock_irqrestore(psc
->lock
, flags
);
152 static const struct clk_ops clk_psc_ops
= {
153 .enable
= keystone_clk_enable
,
154 .disable
= keystone_clk_disable
,
155 .is_enabled
= keystone_clk_is_enabled
,
159 * clk_register_psc - register psc clock
160 * @dev: device that is registering this clock
161 * @name: name of this clock
162 * @parent_name: name of clock's parent
163 * @psc_data: platform data to configure this clock
164 * @lock: spinlock used by this clock
166 static struct clk
*clk_register_psc(struct device
*dev
,
168 const char *parent_name
,
169 struct clk_psc_data
*psc_data
,
172 struct clk_init_data init
;
176 psc
= kzalloc(sizeof(*psc
), GFP_KERNEL
);
178 return ERR_PTR(-ENOMEM
);
181 init
.ops
= &clk_psc_ops
;
183 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
184 init
.num_parents
= (parent_name
? 1 : 0);
186 psc
->psc_data
= psc_data
;
188 psc
->hw
.init
= &init
;
190 clk
= clk_register(NULL
, &psc
->hw
);
198 * of_psc_clk_init - initialize psc clock through DT
199 * @node: device tree node for this clock
200 * @lock: spinlock used by this clock
202 static void __init
of_psc_clk_init(struct device_node
*node
, spinlock_t
*lock
)
204 const char *clk_name
= node
->name
;
205 const char *parent_name
;
206 struct clk_psc_data
*data
;
210 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
212 pr_err("%s: Out of memory\n", __func__
);
216 i
= of_property_match_string(node
, "reg-names", "control");
217 data
->control_base
= of_iomap(node
, i
);
218 if (!data
->control_base
) {
219 pr_err("%s: control ioremap failed\n", __func__
);
223 i
= of_property_match_string(node
, "reg-names", "domain");
224 data
->domain_base
= of_iomap(node
, i
);
225 if (!data
->domain_base
) {
226 pr_err("%s: domain ioremap failed\n", __func__
);
230 of_property_read_u32(node
, "domain-id", &data
->domain_id
);
232 /* Domain transition registers at fixed address space of domain_id 0 */
233 if (!domain_transition_base
&& !data
->domain_id
)
234 domain_transition_base
= data
->domain_base
;
236 of_property_read_string(node
, "clock-output-names", &clk_name
);
237 parent_name
= of_clk_get_parent_name(node
, 0);
239 pr_err("%s: Parent clock not found\n", __func__
);
243 clk
= clk_register_psc(NULL
, clk_name
, parent_name
, data
, lock
);
245 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
249 pr_err("%s: error registering clk %s\n", __func__
, node
->name
);
252 iounmap(data
->domain_base
);
254 iounmap(data
->control_base
);
261 * of_keystone_psc_clk_init - initialize psc clock through DT
262 * @node: device tree node for this clock
264 static void __init
of_keystone_psc_clk_init(struct device_node
*node
)
266 of_psc_clk_init(node
, &psc_lock
);
268 CLK_OF_DECLARE(keystone_gate_clk
, "ti,keystone,psc-clock",
269 of_keystone_psc_clk_init
);