summaryrefslogtreecommitdiff
path: root/src/soc/nvidia/tegra132/funitcfg.c
blob: 7c3a9d9b3b2e8f7834a5d90934a88701b2a3c1c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * This file is part of the coreboot project.
 *
 * Copyright 2014 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <arch/io.h>
#include <soc/addressmap.h>
#include <soc/funitcfg.h>
#include <soc/clock.h>
#include <soc/padconfig.h>
#include <string.h>

struct clk_set_data {
	size_t clk_enb_set_offset;
	size_t rst_dev_clr_offset;
};

struct funit_cfg_data {
	const char *name;
	size_t clk_src_offset;
	uint8_t clk_data_index;
	uint32_t clk_enb_val;
};

enum {
	CLK_L_SET = 0,
	CLK_H_SET = 1,
	CLK_U_SET = 2,
	CLK_V_SET = 3,
	CLK_W_SET = 4,
	CLK_X_SET = 5,
};

#define CLK_SET_OFFSETS(x)						\
	{								\
	offsetof(struct clk_rst_ctlr, clk_enb_##x##_set),		\
	offsetof(struct clk_rst_ctlr, rst_dev_##x##_clr)		\
	}

static const struct clk_set_data clk_data_arr[] = {
	[CLK_L_SET] = CLK_SET_OFFSETS(l),
	[CLK_H_SET] = CLK_SET_OFFSETS(h),
	[CLK_U_SET] = CLK_SET_OFFSETS(u),
	[CLK_V_SET] = CLK_SET_OFFSETS(v),
	[CLK_W_SET] = CLK_SET_OFFSETS(w),
	[CLK_X_SET] = CLK_SET_OFFSETS(x),
};

static struct clk_rst_ctlr *clk_rst = (void *)TEGRA_CLK_RST_BASE;

static const struct funit_cfg_data funit_data[] =  {
	[FUNIT_SBC1] = {"sbc1", offsetof(struct clk_rst_ctlr, clk_src_sbc1),
			CLK_H_SET,
			CLK_H_SBC1},
	[FUNIT_SBC4] = {"sbc4", offsetof(struct clk_rst_ctlr, clk_src_sbc4),
			CLK_U_SET,
			CLK_U_SBC4},
	[FUNIT_I2C2] = {"i2c2", offsetof(struct clk_rst_ctlr, clk_src_i2c2),
			CLK_H_SET,
			CLK_H_I2C2},
	[FUNIT_I2C3] = {"i2c3", offsetof(struct clk_rst_ctlr, clk_src_i2c3),
			CLK_U_SET,
			CLK_U_I2C3},
	[FUNIT_I2C5] = {"i2c5", offsetof(struct clk_rst_ctlr, clk_src_i2c5),
			CLK_H_SET,
			CLK_H_I2C5},
	[FUNIT_SDMMC3] = {"sdmmc3", offsetof(struct clk_rst_ctlr, clk_src_sdmmc3),
			  CLK_U_SET,
			  CLK_U_SDMMC3},
	[FUNIT_SDMMC4] = {"sdmmc4", offsetof(struct clk_rst_ctlr, clk_src_sdmmc4),
			  CLK_L_SET,
			  CLK_L_SDMMC4},
};

static inline uint32_t get_clk_src_freq(uint32_t clk_src)
{
	uint32_t freq = 0;

	switch(clk_src) {
	case CLK_M:
		freq = TEGRA_CLK_M_KHZ;
		break;
	case PLLP:
		freq = TEGRA_PLLP_KHZ;
		break;
	default:
		printk(BIOS_SPEW, "%s ERROR: Unknown clk_src %d\n",
		       __func__,clk_src);
	}

	return freq;
}

void soc_configure_funits(const struct funit_cfg * const entries, size_t num)
{
	size_t i;
	const char *funit_i2c = "i2c";
	uint32_t clk_div;
	uint32_t clk_div_mask;


	for (i = 0; i < num; i++) {
		uint8_t *rst_base = (uint8_t*)clk_rst;
		const struct funit_cfg * const entry = &entries[i];
		const struct funit_cfg_data *funit;
		const struct clk_set_data *clk_data;
		uint32_t *clk_src_reg, *clk_enb_set_reg, *rst_dev_clr_reg;
		uint32_t clk_src_freq;

		if (entry->funit_index >= FUNIT_INDEX_MAX) {
			printk(BIOS_ERR, "Error: Index out of bounds\n");
			continue;
		}

		funit = &funit_data[entry->funit_index];
		clk_data = &clk_data_arr[funit->clk_data_index];

		clk_src_reg = (uint32_t*)(rst_base + funit->clk_src_offset);
		clk_enb_set_reg = (uint32_t*)(rst_base
					      + clk_data->clk_enb_set_offset);
		rst_dev_clr_reg = (uint32_t*)(rst_base
					      + clk_data->rst_dev_clr_offset);

		clk_src_freq = get_clk_src_freq(entry->clk_src_id);

		if (strncmp(funit->name,funit_i2c,strlen(funit_i2c)) == 0) {
			/* I2C funit */
			clk_div = get_i2c_clk_div(clk_src_freq,
						  entry->clk_dev_freq_khz);
			clk_div_mask = CLK_DIV_MASK_I2C;
		} else {
			/* Non I2C */
			clk_div = get_clk_div(clk_src_freq,entry->clk_dev_freq_khz);
			clk_div_mask = CLK_DIV_MASK;
		}

		_clock_set_div(clk_src_reg,funit->name,clk_div,
			       clk_div_mask,entry->clk_src_id);

		clock_grp_enable_clear_reset(funit->clk_enb_val,
					     clk_enb_set_reg,
					     rst_dev_clr_reg);

		soc_configure_pads(entry->pad_cfg,entry->pad_cfg_size);
	}
}