1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
7 #include <linux/clk-provider.h>
11 #include <linux/of_address.h>
13 #define MHZ (1000 * 1000)
15 #define BASE_CPU_SHIFT 1
16 #define BASE_CPU_MASK 0x7F
18 #define CPU_AHB_SHIFT 12
19 #define CPU_AHB_MASK 0x07
21 #define FIXED_BASE_SHIFT 8
22 #define FIXED_BASE_MASK 0x01
24 #define CLASSIC_BASE_SHIFT 16
25 #define CLASSIC_BASE_MASK 0x1F
27 #define CX_BASE_SHIFT 15
28 #define CX_BASE_MASK 0x3F
30 #define CX_UNKNOWN_SHIFT 21
31 #define CX_UNKNOWN_MASK 0x03
33 struct nspire_clk_info
{
40 #define EXTRACT(var, prop) (((var)>>prop##_SHIFT) & prop##_MASK)
41 static void nspire_clkinfo_cx(u32 val
, struct nspire_clk_info
*clk
)
43 if (EXTRACT(val
, FIXED_BASE
))
44 clk
->base_clock
= 48 * MHZ
;
46 clk
->base_clock
= 6 * EXTRACT(val
, CX_BASE
) * MHZ
;
48 clk
->base_cpu_ratio
= EXTRACT(val
, BASE_CPU
) * EXTRACT(val
, CX_UNKNOWN
);
49 clk
->base_ahb_ratio
= clk
->base_cpu_ratio
* (EXTRACT(val
, CPU_AHB
) + 1);
52 static void nspire_clkinfo_classic(u32 val
, struct nspire_clk_info
*clk
)
54 if (EXTRACT(val
, FIXED_BASE
))
55 clk
->base_clock
= 27 * MHZ
;
57 clk
->base_clock
= (300 - 6 * EXTRACT(val
, CLASSIC_BASE
)) * MHZ
;
59 clk
->base_cpu_ratio
= EXTRACT(val
, BASE_CPU
) * 2;
60 clk
->base_ahb_ratio
= clk
->base_cpu_ratio
* (EXTRACT(val
, CPU_AHB
) + 1);
63 static void __init
nspire_ahbdiv_setup(struct device_node
*node
,
64 void (*get_clkinfo
)(u32
, struct nspire_clk_info
*))
69 const char *clk_name
= node
->name
;
70 const char *parent_name
;
71 struct nspire_clk_info info
;
73 io
= of_iomap(node
, 0);
79 get_clkinfo(val
, &info
);
81 of_property_read_string(node
, "clock-output-names", &clk_name
);
82 parent_name
= of_clk_get_parent_name(node
, 0);
84 hw
= clk_hw_register_fixed_factor(NULL
, clk_name
, parent_name
, 0,
85 1, info
.base_ahb_ratio
);
87 of_clk_add_hw_provider(node
, of_clk_hw_simple_get
, hw
);
90 static void __init
nspire_ahbdiv_setup_cx(struct device_node
*node
)
92 nspire_ahbdiv_setup(node
, nspire_clkinfo_cx
);
95 static void __init
nspire_ahbdiv_setup_classic(struct device_node
*node
)
97 nspire_ahbdiv_setup(node
, nspire_clkinfo_classic
);
100 CLK_OF_DECLARE(nspire_ahbdiv_cx
, "lsi,nspire-cx-ahb-divider",
101 nspire_ahbdiv_setup_cx
);
102 CLK_OF_DECLARE(nspire_ahbdiv_classic
, "lsi,nspire-classic-ahb-divider",
103 nspire_ahbdiv_setup_classic
);
105 static void __init
nspire_clk_setup(struct device_node
*node
,
106 void (*get_clkinfo
)(u32
, struct nspire_clk_info
*))
111 const char *clk_name
= node
->name
;
112 struct nspire_clk_info info
;
114 io
= of_iomap(node
, 0);
120 get_clkinfo(val
, &info
);
122 of_property_read_string(node
, "clock-output-names", &clk_name
);
124 hw
= clk_hw_register_fixed_rate(NULL
, clk_name
, NULL
, 0,
127 of_clk_add_hw_provider(node
, of_clk_hw_simple_get
, hw
);
131 pr_info("TI-NSPIRE Base: %uMHz CPU: %uMHz AHB: %uMHz\n",
132 info
.base_clock
/ MHZ
,
133 info
.base_clock
/ info
.base_cpu_ratio
/ MHZ
,
134 info
.base_clock
/ info
.base_ahb_ratio
/ MHZ
);
137 static void __init
nspire_clk_setup_cx(struct device_node
*node
)
139 nspire_clk_setup(node
, nspire_clkinfo_cx
);
142 static void __init
nspire_clk_setup_classic(struct device_node
*node
)
144 nspire_clk_setup(node
, nspire_clkinfo_classic
);
147 CLK_OF_DECLARE(nspire_clk_cx
, "lsi,nspire-cx-clock", nspire_clk_setup_cx
);
148 CLK_OF_DECLARE(nspire_clk_classic
, "lsi,nspire-classic-clock",
149 nspire_clk_setup_classic
);