blob: 54970b949fac05efe5776ae289c4744fcafbf12f [file] [log] [blame]
Wenyou Yang8c772bd2016-07-20 17:55:12 +08001/*
2 * Copyright (C) 2016 Atmel Corporation
3 * Wenyou.Yang <wenyou.yang@atmel.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <clk-uclass.h>
Simon Glass11c89f32017-05-17 17:18:03 -060010#include <dm.h>
Wenyou Yang6b66b922017-09-05 18:30:07 +080011#include <syscon.h>
Wenyou Yang8c772bd2016-07-20 17:55:12 +080012#include <linux/io.h>
13#include <mach/at91_pmc.h>
Wenyou Yang6b66b922017-09-05 18:30:07 +080014#include <mach/sama5_sfr.h>
Wenyou Yang8c772bd2016-07-20 17:55:12 +080015#include "pmc.h"
16
Wenyou Yang6b66b922017-09-05 18:30:07 +080017/*
18 * The purpose of this clock is to generate a 480 MHz signal. A different
19 * rate can't be configured.
20 */
21#define UTMI_RATE 480000000
Wenyou Yang8c772bd2016-07-20 17:55:12 +080022
23static int utmi_clk_enable(struct clk *clk)
24{
25 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
26 struct at91_pmc *pmc = plat->reg_base;
Wenyou Yang6b66b922017-09-05 18:30:07 +080027 struct clk clk_dev;
28 ulong clk_rate;
29 u32 utmi_ref_clk_freq;
Wenyou Yang8c772bd2016-07-20 17:55:12 +080030 u32 tmp;
Wenyou Yang6b66b922017-09-05 18:30:07 +080031 int err;
Wenyou Yang8c772bd2016-07-20 17:55:12 +080032
33 if (readl(&pmc->sr) & AT91_PMC_LOCKU)
34 return 0;
35
Wenyou Yang6b66b922017-09-05 18:30:07 +080036 /*
37 * If mainck rate is different from 12 MHz, we have to configure the
38 * FREQ field of the SFR_UTMICKTRIM register to generate properly
39 * the utmi clock.
40 */
41 err = clk_get_by_index(clk->dev, 0, &clk_dev);
42 if (err)
43 return -EINVAL;
44
45 clk_rate = clk_get_rate(&clk_dev);
46 switch (clk_rate) {
47 case 12000000:
48 utmi_ref_clk_freq = 0;
49 break;
50 case 16000000:
51 utmi_ref_clk_freq = 1;
52 break;
53 case 24000000:
54 utmi_ref_clk_freq = 2;
55 break;
56 /*
57 * Not supported on SAMA5D2 but it's not an issue since MAINCK
58 * maximum value is 24 MHz.
59 */
60 case 48000000:
61 utmi_ref_clk_freq = 3;
62 break;
63 default:
64 printf("UTMICK: unsupported mainck rate\n");
65 return -EINVAL;
66 }
67
68 if (plat->regmap_sfr) {
69 err = regmap_read(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, &tmp);
70 if (err)
71 return -EINVAL;
72
73 tmp &= ~AT91_UTMICKTRIM_FREQ;
74 tmp |= utmi_ref_clk_freq;
75 err = regmap_write(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, tmp);
76 if (err)
77 return -EINVAL;
78 } else if (utmi_ref_clk_freq) {
79 printf("UTMICK: sfr node required\n");
80 return -EINVAL;
81 }
82
Wenyou Yang8c772bd2016-07-20 17:55:12 +080083 tmp = readl(&pmc->uckr);
84 tmp |= AT91_PMC_UPLLEN |
85 AT91_PMC_UPLLCOUNT |
86 AT91_PMC_BIASEN;
87 writel(tmp, &pmc->uckr);
88
89 while (!(readl(&pmc->sr) & AT91_PMC_LOCKU))
90 ;
91
92 return 0;
93}
94
95static ulong utmi_clk_get_rate(struct clk *clk)
96{
Wenyou Yang6b66b922017-09-05 18:30:07 +080097 /* UTMI clk rate is fixed. */
98 return UTMI_RATE;
Wenyou Yang8c772bd2016-07-20 17:55:12 +080099}
100
101static struct clk_ops utmi_clk_ops = {
102 .enable = utmi_clk_enable,
103 .get_rate = utmi_clk_get_rate,
104};
105
Wenyou Yang6b66b922017-09-05 18:30:07 +0800106static int utmi_clk_ofdata_to_platdata(struct udevice *dev)
107{
108 struct pmc_platdata *plat = dev_get_platdata(dev);
109 struct udevice *syscon;
110
111 uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
112 "regmap-sfr", &syscon);
113
114 if (syscon)
115 plat->regmap_sfr = syscon_get_regmap(syscon);
116
117 return 0;
118}
119
Wenyou Yang8c772bd2016-07-20 17:55:12 +0800120static int utmi_clk_probe(struct udevice *dev)
121{
122 return at91_pmc_core_probe(dev);
123}
124
125static const struct udevice_id utmi_clk_match[] = {
126 { .compatible = "atmel,at91sam9x5-clk-utmi" },
127 {}
128};
129
130U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
131 .name = "at91sam9x5-utmi-clk",
132 .id = UCLASS_CLK,
133 .of_match = utmi_clk_match,
134 .probe = utmi_clk_probe,
Wenyou Yang6b66b922017-09-05 18:30:07 +0800135 .ofdata_to_platdata = utmi_clk_ofdata_to_platdata,
Wenyou Yang8c772bd2016-07-20 17:55:12 +0800136 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
137 .ops = &utmi_clk_ops,
138};