summaryrefslogtreecommitdiff
path: root/board/nvidia/common/pmu.c
blob: d18c8d71e8fa61594af68cdc3ba6d46f3d017b4b (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) 2011 The Chromium OS Authors.
 * (C) Copyright 2010,2011 NVIDIA Corporation <www.nvidia.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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; either version 2 of
 * the License, or (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <asm/io.h>
#include <asm/arch-tegra/bitfield.h>
#include <asm/arch/tegra.h>
#include <asm/arch/sys_proto.h>

#include <asm/arch/pmu.h>
#include <asm/arch-tegra/ap20.h>
#include <asm/arch-tegra/pmc.h>
#include <i2c.h>
#include "pmu.h"

/*
 * abs() handles unsigned ints, shorts and chars and returns a signed long.
 */
#define abs(x) ({						\
		long ret;					\
		{						\
			typeof((x)) __x = (x);			\
			ret = (__x < 0) ? -__x : __x;		\
		}						\
		ret;						\
	})

#define stp(x, y) ((x < y) ? VDD_TRANSITION_STEP : -VDD_TRANSITION_STEP)

int pmu_read(int reg)
{
	int	i;
	uchar	data;
	int	retval = -1;
	int	old_bus_num;

	old_bus_num = i2c_get_bus_num();
	i2c_set_bus_num(DVC_I2C_BUS_NUMBER);

	for (i = 0; i < MAX_I2C_RETRY; ++i) {
		if (!i2c_read(PMU_I2C_ADDRESS, reg, 1, &data, 1)) {
			retval = (int)data;
			goto exit;
		}

		/* i2c access failed, retry */
		udelay(100);
	}

exit:
	i2c_set_bus_num(old_bus_num);
	return retval;
}

int pmu_write(int reg, uchar *data, uint len)
{
	int	i;
	int	retval = -1;
	int	old_bus_num;

	old_bus_num = i2c_get_bus_num();
	i2c_set_bus_num(DVC_I2C_BUS_NUMBER);

	for (i = 0; i < MAX_I2C_RETRY; ++i) {
		if (!i2c_write(PMU_I2C_ADDRESS, reg, 1, data, len)) {
			retval = 0;
			goto exit;
		}

		/* i2c access failed, retry */
		udelay(100);
	}

exit:
	i2c_set_bus_num(old_bus_num);
	return retval;
}

#ifdef CONFIG_TEGRA_CLOCK_SCALING
struct vdd_settings {
	int	data;
	int	nominal;
};

enum vdd_type {
	core = 0,
	cpu = 1,
};

static struct vdd_settings vdd_current[] = {
	/* vdd core */
	{.data		= 0,
	 .nominal	= 0,
	},

	/* vdd cpu */
	{.data		= 0,
	 .nominal	= 0,
	},
};

static int vdd_init_nominal_table(void)
{
	/* by default, the table has been filled with T25 settings */
	switch (tegra_get_chip_type()) {
	case TEGRA_SOC_T20:
		vdd_current[core].nominal = VDD_CORE_NOMINAL_T20;
		vdd_current[cpu].nominal = VDD_CPU_NOMINAL_T20;
		break;
	case TEGRA_SOC_T25:
		vdd_current[core].nominal = VDD_CORE_NOMINAL_T25;
		vdd_current[cpu].nominal = VDD_CPU_NOMINAL_T25;
		break;
	default:
		/* unknown chip type */
		return -1;
	}
	return 0;
}

/* get current vdd_core and vdd_cpu */
static int tegra2_get_voltage(void)
{
	int	reg;
	int	data1, data2;

	/*
	 * Each vdd has two supply sources, ie, v1 and v2.
	 * The supply control reg1 and reg2 determine the current selection.
	 */
	/* get supply control reg1 and reg2 */
	if ((data1 = pmu_read(PMU_SUPPLY_CONTROL_REG1)) == -1)
		return -1;

	if ((data2 = pmu_read(PMU_SUPPLY_CONTROL_REG2)) == -1)
		return -1;

	reg = PMU_CORE_VOLTAGE_REG;	/* set default to v1 */
	if ((data1 | data2) & VDD_CORE_SUPPLY2_SEL)
		reg++;	/* v2 is selected*/

	/* get vdd_core */
	if ((vdd_current[core].data = pmu_read(reg)) == -1)
		return -1;

	reg = PMU_CPU_VOLTAGE_REG;	/* set default to v1 */
	if ((data1 | data2) & VDD_CPU_SUPPLY2_SEL)
		reg++;	/* v2 is selected*/

	/* get vdd_cpu */
	if ((vdd_current[cpu].data = pmu_read(reg)) == -1)
		return -1;

	return 0;
}

static int tegra2_set_voltage(int reg, int data, int control)
{
	uchar	data_buffer[3];
	uchar	control_bit = (uchar)control;

	/*
	 * only one supply is needed in u-boot. set both v1 and v2 to
	 * same value.
	 *
	 * when both v1 and v2 are set to same value, we just need to set
	 * control1 reg to trigger the supply selection.
	 */
	data_buffer[0] = data_buffer[1] = (uchar)data;
	data_buffer[2] = VDD_TRANSITION_RATE;

	if (!pmu_write(reg, data_buffer, 3) &&		/* v1, v2 and rate */
	    !pmu_write(PMU_SUPPLY_CONTROL_REG1, &control_bit, 1)) /* trigger */
		return 0;
	return -1;
}

int pmu_is_voltage_nominal(void)
{
	if ((vdd_current[core].data == vdd_current[core].nominal) &&
	    (vdd_current[cpu].data == vdd_current[cpu].nominal))
		return 1;
	return 0;
}

static void calculate_next_voltage(int *data, int nominal, int step)
{
	if (abs(nominal - *data) > VDD_TRANSITION_STEP)
		*data += step;
	else
		*data = nominal;
}

/*
 * It is requird by tegra2 soc that vdd_core must be higher than vdd_cpu
 * with certain range at all time. If current settings doesn't meet this
 * condition, pmu_adjust_voltage just simply returns without setting any
 * voltage.
 */
static int pmu_adjust_voltage(void)
{
	int step_core, step_cpu;
	int adjust_vdd_core_late;

	/*
	 * if vdd_core < vdd_cpu + rel
	 *    skip
	 *
	 * This condition may happen when system reboots due to kernel crash.
	 */
	if (vdd_current[core].data < (vdd_current[cpu].data + VDD_RELATION))
		return -1;

	/*
	 * Since vdd_core and vdd_cpu may both stand at either greater or less
	 * than their nominal voltage, the adjustment may go either directions.
	 *
	 * Make sure vdd_core is always higher than vdd_cpu with certain margin.
	 * So, find out which vdd to adjust first in each step.
	 *
	 * case 1: both vdd_core and vdd_cpu need to move up
	 *              adjust vdd_core before vdd_cpu
	 *
	 * case 2: both vdd_core and vdd_cpu need to move down
	 *              adjust vdd_cpu before vdd_core
	 *
	 * case 3: vdd_core moves down and vdd_cpu moves up
	 *              adjusting either one first is fine.
	 */
	step_core = stp(vdd_current[core].data, vdd_current[core].nominal);
	step_cpu = stp(vdd_current[cpu].data, vdd_current[cpu].nominal);

	/*
	 * Adjust vdd_core and vdd_cpu one step at a time until they reach
	 * their nominal values.
	 */
	while ((vdd_current[core].data != vdd_current[core].nominal) ||
	       (vdd_current[cpu].data != vdd_current[cpu].nominal)) {

		adjust_vdd_core_late = 0;

		/* if vdd_core hasn't reached its nominal value? */
		if (vdd_current[core].data != vdd_current[core].nominal) {

			calculate_next_voltage(&vdd_current[core].data,
					       vdd_current[core].nominal,
					       step_core);

			/*
			 * if case 1 and case 3, set new vdd_core first.
			 * otherwise, hold down until new vdd_cpu is set.
			 */
			if (step_cpu > 0) {
				/* adjust vdd_core first */
				if (tegra2_set_voltage(PMU_CORE_VOLTAGE_REG,
						vdd_current[core].data,
						VDD_CORE_SUPPLY_CONTROL))
					return -1;
			} else
				/* set flag to adjust vdd_core later */
				adjust_vdd_core_late = 1;
		}

		/* if vdd_cpu hasn't reached its nominal value? */
		if (vdd_current[cpu].data != vdd_current[cpu].nominal) {

			calculate_next_voltage(&vdd_current[cpu].data,
					       vdd_current[cpu].nominal,
					       step_cpu);

			/* adjust vdd_cpu */
			if (tegra2_set_voltage(PMU_CPU_VOLTAGE_REG,
					       vdd_current[cpu].data,
					       VDD_CPU_SUPPLY_CONTROL))
				return -1;
		}

		/*
		 * if vdd_core late flag is set
		 */
		if (adjust_vdd_core_late) {
			/* adjust vdd_core */
			if (tegra2_set_voltage(PMU_CORE_VOLTAGE_REG,
					       vdd_current[core].data,
					       VDD_CORE_SUPPLY_CONTROL))
				return -1;
		}
	}
	return 0;
}

static int pmu_set_pwm_mode(int smx)
{
	int ret = 0;
	uchar val = 0;

	ret = pmu_read(PMU_PWM_PFM_MODE_REG);
	if (ret == -1)
		return ret;

	val = (uchar)ret;
	val |= (1 << smx);

	ret = pmu_write(PMU_PWM_PFM_MODE_REG, &val, 1);
	if (ret == -1)
		return ret;

	return 0;
}

int pmu_set_nominal(void)
{
	/* fill in nominal values based on chip type */
	if (vdd_init_nominal_table())
		return -1;

	/* Set SM1 in PWM-only mode */
	if (pmu_set_pwm_mode(SM1_PWM_BIT))
		return -1;

	/* get current voltage settings */
	if (tegra2_get_voltage())
		return -1;

	/* if current voltage is already set to nominal, skip */
	if (pmu_is_voltage_nominal())
		return 0;

	/* adjust vdd_core and/or vdd_cpu */
	return pmu_adjust_voltage();
}
#endif /* CONFIG_TEGRA_CLOCK_SCALING */