blob: 378cdff072feb8f4c991eab717e2ed6f5c01f61b [file] [log] [blame]
Lukasz Majewski4de44bb2019-06-24 15:50:45 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 *
6 * Copyright 2012 Freescale Semiconductor, Inc.
7 * Copyright 2012 Linaro Ltd.
8 *
9 * The code contained herein is licensed under the GNU General Public
10 * License. You may obtain a copy of the GNU General Public License
11 * Version 2 or later at the following locations:
12 *
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
15 */
16
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020017#include <asm/io.h>
18#include <malloc.h>
19#include <clk-uclass.h>
20#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020022#include <linux/clk-provider.h>
23#include <div64.h>
24#include <clk.h>
25#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070026#include <linux/err.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020027
28#define UBOOT_DM_CLK_IMX_PFD "imx_clk_pfd"
29
30struct clk_pfd {
31 struct clk clk;
32 void __iomem *reg;
33 u8 idx;
34};
35
36#define to_clk_pfd(_clk) container_of(_clk, struct clk_pfd, clk)
37
38#define SET 0x4
39#define CLR 0x8
40#define OTG 0xc
41
42static unsigned long clk_pfd_recalc_rate(struct clk *clk)
43{
44 struct clk_pfd *pfd =
45 to_clk_pfd(dev_get_clk_ptr(clk->dev));
46 unsigned long parent_rate = clk_get_parent_rate(clk);
47 u64 tmp = parent_rate;
48 u8 frac = (readl(pfd->reg) >> (pfd->idx * 8)) & 0x3f;
49
50 tmp *= 18;
51 do_div(tmp, frac);
52
53 return tmp;
54}
55
Giulio Benetti03eb95d2020-01-10 15:47:00 +010056static unsigned long clk_pfd_set_rate(struct clk *clk, unsigned long rate)
57{
58 struct clk_pfd *pfd = to_clk_pfd(clk);
59 unsigned long parent_rate = clk_get_parent_rate(clk);
60 u64 tmp = parent_rate;
61 u8 frac;
62
63 tmp = tmp * 18 + rate / 2;
64 do_div(tmp, rate);
65 frac = tmp;
66 if (frac < 12)
67 frac = 12;
68 else if (frac > 35)
69 frac = 35;
70
71 writel(0x3f << (pfd->idx * 8), pfd->reg + CLR);
72 writel(frac << (pfd->idx * 8), pfd->reg + SET);
73
74 return 0;
75}
76
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020077static const struct clk_ops clk_pfd_ops = {
78 .get_rate = clk_pfd_recalc_rate,
Giulio Benetti03eb95d2020-01-10 15:47:00 +010079 .set_rate = clk_pfd_set_rate,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020080};
81
82struct clk *imx_clk_pfd(const char *name, const char *parent_name,
83 void __iomem *reg, u8 idx)
84{
85 struct clk_pfd *pfd;
86 struct clk *clk;
87 int ret;
88
89 pfd = kzalloc(sizeof(*pfd), GFP_KERNEL);
90 if (!pfd)
91 return ERR_PTR(-ENOMEM);
92
93 pfd->reg = reg;
94 pfd->idx = idx;
95
96 /* register the clock */
97 clk = &pfd->clk;
98
99 ret = clk_register(clk, UBOOT_DM_CLK_IMX_PFD, name, parent_name);
100 if (ret) {
101 kfree(pfd);
102 return ERR_PTR(ret);
103 }
104
105 return clk;
106}
107
108U_BOOT_DRIVER(clk_pfd) = {
109 .name = UBOOT_DM_CLK_IMX_PFD,
110 .id = UCLASS_CLK,
111 .ops = &clk_pfd_ops,
112 .flags = DM_FLAG_PRE_RELOC,
113};