1 /* 2 * Hisilicon clock driver 3 * 4 * Copyright (c) 2013-2017 Hisilicon Limited. 5 * Copyright (c) 2017 Linaro Limited. 6 * 7 * Author: Kai Zhao <zhaokai1@hisilicon.com> 8 * Tao Wang <kevin.wangtao@hisilicon.com> 9 * Leo Yan <leo.yan@linaro.org> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 */ 22 23 #include <linux/clk-provider.h> 24 #include <linux/device.h> 25 #include <linux/err.h> 26 #include <linux/init.h> 27 #include <linux/io.h> 28 #include <linux/mailbox_client.h> 29 #include <linux/module.h> 30 #include <linux/of.h> 31 #include <linux/platform_device.h> 32 #include <dt-bindings/clock/hi3660-clock.h> 33 34 #define HI3660_STUB_CLOCK_DATA (0x70) 35 #define MHZ (1000 * 1000) 36 37 #define DEFINE_CLK_STUB(_id, _cmd, _name) \ 38 { \ 39 .id = (_id), \ 40 .cmd = (_cmd), \ 41 .hw.init = &(struct clk_init_data) { \ 42 .name = #_name, \ 43 .ops = &hi3660_stub_clk_ops, \ 44 .num_parents = 0, \ 45 .flags = CLK_GET_RATE_NOCACHE, \ 46 }, \ 47 }, 48 49 #define to_stub_clk(_hw) container_of(_hw, struct hi3660_stub_clk, hw) 50 51 struct hi3660_stub_clk_chan { 52 struct mbox_client cl; 53 struct mbox_chan *mbox; 54 }; 55 56 struct hi3660_stub_clk { 57 unsigned int id; 58 struct clk_hw hw; 59 unsigned int cmd; 60 unsigned int msg[8]; 61 unsigned int rate; 62 }; 63 64 static void __iomem *freq_reg; 65 static struct hi3660_stub_clk_chan stub_clk_chan; 66 67 static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw, 68 unsigned long parent_rate) 69 { 70 struct hi3660_stub_clk *stub_clk = to_stub_clk(hw); 71 72 /* 73 * LPM3 writes back the CPU frequency in shared SRAM so read 74 * back the frequency. 75 */ 76 stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ; 77 return stub_clk->rate; 78 } 79 80 static long hi3660_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate, 81 unsigned long *prate) 82 { 83 /* 84 * LPM3 handles rate rounding so just return whatever 85 * rate is requested. 86 */ 87 return rate; 88 } 89 90 static int hi3660_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate, 91 unsigned long parent_rate) 92 { 93 struct hi3660_stub_clk *stub_clk = to_stub_clk(hw); 94 95 stub_clk->msg[0] = stub_clk->cmd; 96 stub_clk->msg[1] = rate / MHZ; 97 98 dev_dbg(stub_clk_chan.cl.dev, "set rate msg[0]=0x%x msg[1]=0x%x\n", 99 stub_clk->msg[0], stub_clk->msg[1]); 100 101 mbox_send_message(stub_clk_chan.mbox, stub_clk->msg); 102 mbox_client_txdone(stub_clk_chan.mbox, 0); 103 104 stub_clk->rate = rate; 105 return 0; 106 } 107 108 static const struct clk_ops hi3660_stub_clk_ops = { 109 .recalc_rate = hi3660_stub_clk_recalc_rate, 110 .round_rate = hi3660_stub_clk_round_rate, 111 .set_rate = hi3660_stub_clk_set_rate, 112 }; 113 114 static struct hi3660_stub_clk hi3660_stub_clks[HI3660_CLK_STUB_NUM] = { 115 DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER0, 0x0001030A, "cpu-cluster.0") 116 DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER1, 0x0002030A, "cpu-cluster.1") 117 DEFINE_CLK_STUB(HI3660_CLK_STUB_GPU, 0x0003030A, "clk-g3d") 118 DEFINE_CLK_STUB(HI3660_CLK_STUB_DDR, 0x00040309, "clk-ddrc") 119 }; 120 121 static struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec, 122 void *data) 123 { 124 unsigned int idx = clkspec->args[0]; 125 126 if (idx >= HI3660_CLK_STUB_NUM) { 127 pr_err("%s: invalid index %u\n", __func__, idx); 128 return ERR_PTR(-EINVAL); 129 } 130 131 return &hi3660_stub_clks[idx].hw; 132 } 133 134 static int hi3660_stub_clk_probe(struct platform_device *pdev) 135 { 136 struct device *dev = &pdev->dev; 137 struct resource *res; 138 unsigned int i; 139 int ret; 140 141 /* Use mailbox client without blocking */ 142 stub_clk_chan.cl.dev = dev; 143 stub_clk_chan.cl.tx_done = NULL; 144 stub_clk_chan.cl.tx_block = false; 145 stub_clk_chan.cl.knows_txdone = false; 146 147 /* Allocate mailbox channel */ 148 stub_clk_chan.mbox = mbox_request_channel(&stub_clk_chan.cl, 0); 149 if (IS_ERR(stub_clk_chan.mbox)) 150 return PTR_ERR(stub_clk_chan.mbox); 151 152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 153 if (!res) 154 return -EINVAL; 155 freq_reg = devm_ioremap(dev, res->start, resource_size(res)); 156 if (!freq_reg) 157 return -ENOMEM; 158 159 freq_reg += HI3660_STUB_CLOCK_DATA; 160 161 for (i = 0; i < HI3660_CLK_STUB_NUM; i++) { 162 ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw); 163 if (ret) 164 return ret; 165 } 166 167 return devm_of_clk_add_hw_provider(&pdev->dev, hi3660_stub_clk_hw_get, 168 hi3660_stub_clks); 169 } 170 171 static const struct of_device_id hi3660_stub_clk_of_match[] = { 172 { .compatible = "hisilicon,hi3660-stub-clk", }, 173 {} 174 }; 175 176 static struct platform_driver hi3660_stub_clk_driver = { 177 .probe = hi3660_stub_clk_probe, 178 .driver = { 179 .name = "hi3660-stub-clk", 180 .of_match_table = hi3660_stub_clk_of_match, 181 }, 182 }; 183 184 static int __init hi3660_stub_clk_init(void) 185 { 186 return platform_driver_register(&hi3660_stub_clk_driver); 187 } 188 subsys_initcall(hi3660_stub_clk_init); 189