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