blob: 887f9f0801435de6c63589786279d920bd412d5e [file] [log] [blame]
Patrick Georgi40a3e322015-06-22 19:41:29 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Patrick Georgi40a3e322015-06-22 19:41:29 +020014 */
15
Elyes HAOUAS20eaef02019-03-29 17:45:28 +010016#include <console/console.h>
Patrick Georgi40a3e322015-06-22 19:41:29 +020017#include <soc/addressmap.h>
18#include <soc/clock.h>
19#include <soc/funitcfg.h>
20#include <soc/nvidia/tegra/usb.h>
21#include <soc/padconfig.h>
22#include <string.h>
23
24struct clk_dev_control {
25 uint32_t *clk_enb_set;
26 uint32_t *rst_dev_clr;
27};
28
29struct funit_cfg_data {
30 const char *name;
31 void *ctlr_base;
32 uint32_t *clk_src_reg;
33 const struct clk_dev_control * const dev_control;
34 uint32_t clk_enb_val;
35};
36
37enum {
38 CLK_L_SET = 0,
39 CLK_H_SET = 1,
40 CLK_U_SET = 2,
41 CLK_V_SET = 3,
42 CLK_W_SET = 4,
43 CLK_X_SET = 5,
44 CLK_Y_SET = 6,
45};
46
47#define CLK_SET_REGS(x) \
48 { \
49 CLK_RST_REG(clk_enb_##x##_set), \
50 CLK_RST_REG(rst_dev_##x##_clr), \
51 }
52
53static const struct clk_dev_control clk_data_arr[] = {
54 [CLK_L_SET] = CLK_SET_REGS(l),
55 [CLK_H_SET] = CLK_SET_REGS(h),
56 [CLK_U_SET] = CLK_SET_REGS(u),
57 [CLK_V_SET] = CLK_SET_REGS(v),
58 [CLK_W_SET] = CLK_SET_REGS(w),
59 [CLK_X_SET] = CLK_SET_REGS(x),
60 [CLK_Y_SET] = CLK_SET_REGS(y),
61};
62
63#define FUNIT_DATA(funit_, loname_, clk_set_) \
64 [FUNIT_INDEX(funit_)] = { \
65 .name = STRINGIFY(loname_), \
66 .ctlr_base = (void *)(uintptr_t)TEGRA_##funit_##_BASE, \
67 .clk_src_reg = CLK_RST_REG(clk_src_##loname_), \
68 .dev_control = &clk_data_arr[CLK_##clk_set_##_SET], \
69 .clk_enb_val = CLK_##clk_set_##_##funit_, \
70 }
71
72#define FUNIT_DATA_USB(funit_, clk_set_) \
73 [FUNIT_INDEX(funit_)] = { \
74 .name = STRINGIFY(funit_), \
75 .ctlr_base = (void *)(uintptr_t)TEGRA_##funit_##_BASE, \
76 .dev_control = &clk_data_arr[CLK_##clk_set_##_SET], \
77 .clk_enb_val = CLK_##clk_set_##_##funit_, \
78 }
79
80static const struct funit_cfg_data funit_data[] = {
81 FUNIT_DATA(I2C1, i2c1, L),
82 FUNIT_DATA(I2C2, i2c2, H),
83 FUNIT_DATA(I2C3, i2c3, U),
84 FUNIT_DATA(I2C5, i2c5, H),
85 FUNIT_DATA(I2C6, i2c6, X),
86 FUNIT_DATA(SDMMC1, sdmmc1, L),
87 FUNIT_DATA(SDMMC4, sdmmc4, L),
88 FUNIT_DATA_USB(USBD, L),
89 FUNIT_DATA_USB(USB2, H),
90 FUNIT_DATA(QSPI, qspi, Y),
91 FUNIT_DATA(I2S1, i2s1, L),
92};
93_Static_assert(ARRAY_SIZE(funit_data) == FUNIT_INDEX_MAX,
94 "funit_cfg_data array not filled out!");
95
96static inline uint32_t get_clk_src_freq(uint32_t clk_src_freq_id)
97{
98 uint32_t freq = 0;
99
100 switch (clk_src_freq_id) {
101 case CLK_M:
102 freq = TEGRA_CLK_M_KHZ;
103 break;
104 case PLLP:
105 freq = TEGRA_PLLP_KHZ;
106 break;
107 default:
108 printk(BIOS_SPEW, "%s ERROR: Unknown clk_src %d\n",
109 __func__, clk_src_freq_id);
110 }
111
112 return freq;
113}
114
115static void configure_clock(const struct funit_cfg * const entry,
116 const struct funit_cfg_data * const funit)
117{
118 const char *funit_i2c = "i2c";
119 uint32_t clk_div;
120 uint32_t clk_div_mask;
121 uint32_t clk_src_freq;
122
123 clk_src_freq = get_clk_src_freq(entry->clk_src_freq_id);
124
125 if (strncmp(funit->name, funit_i2c, strlen(funit_i2c)) == 0) {
126 /* I2C funit */
127 clk_div = get_i2c_clk_div(clk_src_freq,
128 entry->clk_dev_freq_khz);
129 clk_div_mask = CLK_DIV_MASK_I2C;
130 } else {
131 /* Non I2C */
132 clk_div = get_clk_div(clk_src_freq, entry->clk_dev_freq_khz);
133 clk_div_mask = CLK_DIV_MASK;
134 }
135
136 _clock_set_div(funit->clk_src_reg, funit->name, clk_div,
137 clk_div_mask, entry->clk_src_id);
138}
139
140static inline int is_usb(uint32_t idx)
141{
142 return (idx == FUNIT_USBD || idx == FUNIT_USB2);
143}
144
145void soc_configure_funits(const struct funit_cfg * const entries, size_t num)
146{
147 size_t i;
148
149 for (i = 0; i < num; i++) {
150 const struct funit_cfg * const entry = &entries[i];
151 const struct funit_cfg_data *funit;
152 const struct clk_dev_control *dev_control;
153 int funit_usb = is_usb(entry->funit_index);
154
155 if (entry->funit_index >= FUNIT_INDEX_MAX) {
156 printk(BIOS_ERR, "Error: Index out of bounds\n");
157 continue;
158 }
159
160 funit = &funit_data[entry->funit_index];
161 dev_control = funit->dev_control;
162
163 /* USB controllers have a fixed clock source. */
164 if (!funit_usb)
165 configure_clock(entry, funit);
166
167 clock_grp_enable_clear_reset(funit->clk_enb_val,
168 dev_control->clk_enb_set,
169 dev_control->rst_dev_clr);
170
171 if (funit_usb)
172 usb_setup_utmip(funit->ctlr_base);
173
174 soc_configure_pads(entry->pad_cfg,entry->pad_cfg_size);
175 }
176}
177
Aaron Durbin64031672018-04-21 14:45:32 -0600178void __weak usb_setup_utmip(void *usb_base)
Patrick Georgi40a3e322015-06-22 19:41:29 +0200179{
180 /* default empty implementation required if usb.c is not included */
181 printk(BIOS_ERR, "USB setup is not supported in current stage\n");
182}