blob: 875bf293f9cdd637ccf47955a3733a4592a0f8d0 [file] [log] [blame]
Wenyou Yang9e5935c2016-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 Glass9d922452017-05-17 17:18:03 -060010#include <dm.h>
Wenyou Yange7c83152017-09-05 18:30:07 +080011#include <syscon.h>
Wenyou Yang9e5935c2016-07-20 17:55:12 +080012#include <linux/io.h>
13#include <mach/at91_pmc.h>
Wenyou Yange7c83152017-09-05 18:30:07 +080014#include <mach/sama5_sfr.h>
Wenyou Yang9e5935c2016-07-20 17:55:12 +080015#include "pmc.h"
16
17DECLARE_GLOBAL_DATA_PTR;
18
Wenyou Yange7c83152017-09-05 18:30:07 +080019/*
20 * The purpose of this clock is to generate a 480 MHz signal. A different
21 * rate can't be configured.
22 */
23#define UTMI_RATE 480000000
Wenyou Yang9e5935c2016-07-20 17:55:12 +080024
25static int utmi_clk_enable(struct clk *clk)
26{
27 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
28 struct at91_pmc *pmc = plat->reg_base;
Wenyou Yange7c83152017-09-05 18:30:07 +080029 struct clk clk_dev;
30 ulong clk_rate;
31 u32 utmi_ref_clk_freq;
Wenyou Yang9e5935c2016-07-20 17:55:12 +080032 u32 tmp;
Wenyou Yange7c83152017-09-05 18:30:07 +080033 int err;
Wenyou Yang9e5935c2016-07-20 17:55:12 +080034
35 if (readl(&pmc->sr) & AT91_PMC_LOCKU)
36 return 0;
37
Wenyou Yange7c83152017-09-05 18:30:07 +080038 /*
39 * If mainck rate is different from 12 MHz, we have to configure the
40 * FREQ field of the SFR_UTMICKTRIM register to generate properly
41 * the utmi clock.
42 */
43 err = clk_get_by_index(clk->dev, 0, &clk_dev);
44 if (err)
45 return -EINVAL;
46
47 clk_rate = clk_get_rate(&clk_dev);
48 switch (clk_rate) {
49 case 12000000:
50 utmi_ref_clk_freq = 0;
51 break;
52 case 16000000:
53 utmi_ref_clk_freq = 1;
54 break;
55 case 24000000:
56 utmi_ref_clk_freq = 2;
57 break;
58 /*
59 * Not supported on SAMA5D2 but it's not an issue since MAINCK
60 * maximum value is 24 MHz.
61 */
62 case 48000000:
63 utmi_ref_clk_freq = 3;
64 break;
65 default:
66 printf("UTMICK: unsupported mainck rate\n");
67 return -EINVAL;
68 }
69
70 if (plat->regmap_sfr) {
71 err = regmap_read(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, &tmp);
72 if (err)
73 return -EINVAL;
74
75 tmp &= ~AT91_UTMICKTRIM_FREQ;
76 tmp |= utmi_ref_clk_freq;
77 err = regmap_write(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, tmp);
78 if (err)
79 return -EINVAL;
80 } else if (utmi_ref_clk_freq) {
81 printf("UTMICK: sfr node required\n");
82 return -EINVAL;
83 }
84
Wenyou Yang9e5935c2016-07-20 17:55:12 +080085 tmp = readl(&pmc->uckr);
86 tmp |= AT91_PMC_UPLLEN |
87 AT91_PMC_UPLLCOUNT |
88 AT91_PMC_BIASEN;
89 writel(tmp, &pmc->uckr);
90
91 while (!(readl(&pmc->sr) & AT91_PMC_LOCKU))
92 ;
93
94 return 0;
95}
96
97static ulong utmi_clk_get_rate(struct clk *clk)
98{
Wenyou Yange7c83152017-09-05 18:30:07 +080099 /* UTMI clk rate is fixed. */
100 return UTMI_RATE;
Wenyou Yang9e5935c2016-07-20 17:55:12 +0800101}
102
103static struct clk_ops utmi_clk_ops = {
104 .enable = utmi_clk_enable,
105 .get_rate = utmi_clk_get_rate,
106};
107
Wenyou Yange7c83152017-09-05 18:30:07 +0800108static int utmi_clk_ofdata_to_platdata(struct udevice *dev)
109{
110 struct pmc_platdata *plat = dev_get_platdata(dev);
111 struct udevice *syscon;
112
113 uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
114 "regmap-sfr", &syscon);
115
116 if (syscon)
117 plat->regmap_sfr = syscon_get_regmap(syscon);
118
119 return 0;
120}
121
Wenyou Yang9e5935c2016-07-20 17:55:12 +0800122static int utmi_clk_probe(struct udevice *dev)
123{
124 return at91_pmc_core_probe(dev);
125}
126
127static const struct udevice_id utmi_clk_match[] = {
128 { .compatible = "atmel,at91sam9x5-clk-utmi" },
129 {}
130};
131
132U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
133 .name = "at91sam9x5-utmi-clk",
134 .id = UCLASS_CLK,
135 .of_match = utmi_clk_match,
136 .probe = utmi_clk_probe,
Wenyou Yange7c83152017-09-05 18:30:07 +0800137 .ofdata_to_platdata = utmi_clk_ofdata_to_platdata,
Wenyou Yang9e5935c2016-07-20 17:55:12 +0800138 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
139 .ops = &utmi_clk_ops,
140};