xref: /openbmc/u-boot/board/freescale/common/zm7300.c (revision ad5b5801)
1 /*
2  * Copyright 2013 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 /* Power-One ZM7300 DPM */
8 #include "zm7300.h"
9 
10 #define DPM_WP 0x96
11 #define WRP_OPCODE 0x01
12 #define WRM_OPCODE 0x02
13 #define RRP_OPCODE 0x11
14 
15 #define DPM_SUCCESS 0x01
16 #define DPM_EXEC_FAIL 0x00
17 
18 static const uint16_t hex_to_1_10mv[] = {
19 	5000,
20 	5125,
21 	5250,
22 	5375,
23 	5500,
24 	5625,
25 	5750,
26 	5875,
27 	6000,
28 	6125,
29 	6250,
30 	6375,
31 	6500,
32 	6625,
33 	6750,
34 	6875,
35 	7000,
36 	7125,
37 	7250,
38 	7375,
39 	7500,
40 	7625,
41 	7750,
42 	7875,
43 	8000,
44 	8125,
45 	8250,
46 	8375,
47 	8500,
48 	8625,
49 	8750,
50 	8875,
51 	9000,
52 	9125,
53 	9250,
54 	9375,
55 	9500,  /* 0.95mV */
56 	9625,
57 	9750,
58 	9875,
59 	10000,  /* 1.0V */
60 	10125,
61 	10250,
62 	10375,
63 	10500,
64 	10625,
65 	10750,
66 	10875,
67 	11000,
68 	11125,
69 	11250,
70 	11375,
71 	11500,
72 	11625,
73 	11750,
74 	11875,
75 	12000,
76 	12125,
77 	12250,
78 	12375,
79 	0,	/* reserved */
80 };
81 
82 
83 /* Read Data d from Register r of POL p */
84 u8 dpm_rrp(uchar r)
85 {
86 	u8 ret[5];
87 
88 	ret[0] = RRP_OPCODE;
89 	/* POL is 0 */
90 	ret[1] = 0;
91 	ret[2] = r;
92 	i2c_read(I2C_DPM_ADDR, 0, -3, ret, 2);
93 	if (ret[1] == DPM_SUCCESS) { /* the DPM returned success as status */
94 		debug("RRP_OPCODE returned success data is %x\n", ret[0]);
95 		return ret[0];
96 	} else {
97 		return -1;
98 	}
99 }
100 
101 /* Write Data d into DPM register r (RAM) */
102 int dpm_wrm(u8 r, u8 d)
103 {
104 	u8 ret[5];
105 
106 	ret[0] = WRM_OPCODE;
107 	ret[1] = r;
108 	ret[2] = d;
109 	i2c_read(I2C_DPM_ADDR, 0, -3, ret, 1);
110 	if (ret[0] == DPM_SUCCESS) { /* the DPM returned success as status */
111 		debug("WRM_OPCODE returned success data is %x\n", ret[0]);
112 		return ret[0];
113 	} else {
114 		return -1;
115 	}
116 }
117 
118 /* Write Data d into Register r of POL(s) a */
119 int dpm_wrp(u8 r, u8 d)
120 {
121 	u8 ret[7];
122 
123 	ret[0] = WRP_OPCODE;
124 	/* only POL0 is present */
125 	ret[1] = 0x01;
126 	ret[2] = 0x00;
127 	ret[3] = 0x00;
128 	ret[4] = 0x00;
129 	ret[5] = r;
130 	ret[6] = d;
131 	i2c_read(I2C_DPM_ADDR, 0, -7, ret, 1);
132 	if (ret[0] == DPM_SUCCESS) { /* the DPM returned success as status */
133 		debug("WRP_OPCODE returned success data is %x\n", ret[0]);
134 		return 0;
135 	} else {
136 		return -1;
137 	}
138 }
139 
140 /* Uses the DPM command RRP */
141 u8 zm_read(uchar reg)
142 {
143 	u8 d;
144 	d = dpm_rrp(reg);
145 	return d;
146 }
147 
148 /* ZM_write --
149 	Steps:
150 	a. Write data to the register
151 	b. Read data from register and compare to written value
152 	c. Return return_code & voltage_read
153 */
154 u8 zm_write(u8 reg, u8 data)
155 {
156 	u8 d;
157 
158 	/* write data to register */
159 	dpm_wrp(reg, data);
160 
161 	/* read register and compare to written value */
162 	d = dpm_rrp(reg);
163 	if (d != data) {
164 		printf("zm_write : Comparison register data failed\n");
165 		return -1;
166 	}
167 
168 	return d;
169 }
170 
171 /* zm_write_out_voltage
172  * voltage in 1/10 mV
173  */
174 int zm_write_voltage(int voltage)
175 {
176 	u8 reg = 0x7, vid;
177 	uint16_t voltage_read;
178 	u8 ret;
179 
180 	vid =  (voltage - 5000) / ZM_STEP;
181 
182 	ret = zm_write(reg, vid);
183 	if (ret != -1) {
184 		voltage_read = hex_to_1_10mv[ret];
185 		debug("voltage set to %dmV\n", voltage_read/10);
186 		return voltage_read;
187 	}
188 	return -1;
189 }
190 
191 /* zm_read_out_voltage
192  * voltage in 1/10 mV
193  */
194 int zm_read_voltage(void)
195 {
196 	u8 reg = 0x7;
197 	u8 ret;
198 	int voltage;
199 
200 	ret = zm_read(reg);
201 	if (ret != -1) {
202 		voltage =  hex_to_1_10mv[ret];
203 		debug("Voltage read is %dmV\n", voltage/10);
204 		return voltage;
205 	} else {
206 		return -1;
207 	}
208 }
209 
210 int zm_disable_wp()
211 {
212 	u8 new_wp_value;
213 
214 	/* Disable using Write-Protect register 0x96 */
215 	new_wp_value = 0x8;
216 	if ((dpm_wrm(DPM_WP, new_wp_value)) < 0) {
217 		printf("Disable Write-Protect register failed\n");
218 		return -1;
219 	}
220 	return 0;
221 }
222 
223 int zm_enable_wp()
224 {
225 	u8 orig_wp_value;
226 	orig_wp_value = 0x0;
227 
228 	/* Enable using Write-Protect register 0x96 */
229 	if ((dpm_wrm(DPM_WP, orig_wp_value)) < 0) {
230 		printf("Enable Write-Protect register failed\n");
231 		return -1;
232 	}
233 	return 0;
234 }
235 
236