]>
Commit | Line | Data |
---|---|---|
385c9ef5 HS |
1 | /* |
2 | * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net> | |
3 | * | |
4 | * (C) Copyright 2012 | |
5 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. | |
6 | * | |
7 | * Multibus/multiadapter I2C core functions (wrappers) | |
8 | * | |
8dde4ca9 | 9 | * SPDX-License-Identifier: GPL-2.0+ |
385c9ef5 HS |
10 | */ |
11 | #include <common.h> | |
12 | #include <i2c.h> | |
13 | ||
14 | struct i2c_adapter *i2c_get_adapter(int index) | |
15 | { | |
16 | struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter, | |
17 | i2c); | |
18 | int max = ll_entry_count(struct i2c_adapter, i2c); | |
19 | int i; | |
20 | ||
21 | if (index >= max) { | |
22 | printf("Error, wrong i2c adapter %d max %d possible\n", | |
23 | index, max); | |
24 | return i2c_adap_p; | |
25 | } | |
26 | if (index == 0) | |
27 | return i2c_adap_p; | |
28 | ||
29 | for (i = 0; i < index; i++) | |
30 | i2c_adap_p++; | |
31 | ||
32 | return i2c_adap_p; | |
33 | } | |
34 | ||
35 | #if !defined(CONFIG_SYS_I2C_DIRECT_BUS) | |
36 | struct i2c_bus_hose i2c_bus[CONFIG_SYS_NUM_I2C_BUSES] = | |
37 | CONFIG_SYS_I2C_BUSES; | |
38 | #endif | |
39 | ||
40 | DECLARE_GLOBAL_DATA_PTR; | |
41 | ||
42 | void i2c_reloc_fixup(void) | |
43 | { | |
44 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) | |
45 | struct i2c_adapter *i2c_adap_p = ll_entry_start(struct i2c_adapter, | |
46 | i2c); | |
47 | struct i2c_adapter *tmp = i2c_adap_p; | |
48 | int max = ll_entry_count(struct i2c_adapter, i2c); | |
49 | int i; | |
50 | unsigned long addr; | |
51 | ||
52 | if (gd->reloc_off == 0) | |
53 | return; | |
54 | ||
55 | for (i = 0; i < max; i++) { | |
56 | /* adapter itself */ | |
57 | addr = (unsigned long)i2c_adap_p; | |
58 | addr += gd->reloc_off; | |
59 | i2c_adap_p = (struct i2c_adapter *)addr; | |
60 | /* i2c_init() */ | |
61 | addr = (unsigned long)i2c_adap_p->init; | |
62 | addr += gd->reloc_off; | |
63 | i2c_adap_p->init = (void (*)(int, int))addr; | |
64 | /* i2c_probe() */ | |
65 | addr = (unsigned long)i2c_adap_p->probe; | |
66 | addr += gd->reloc_off; | |
67 | i2c_adap_p->probe = (int (*)(uint8_t))addr; | |
68 | /* i2c_read() */ | |
69 | addr = (unsigned long)i2c_adap_p->read; | |
70 | addr += gd->reloc_off; | |
71 | i2c_adap_p->read = (int (*)(uint8_t, uint, int, uint8_t *, | |
72 | int))addr; | |
73 | /* i2c_write() */ | |
74 | addr = (unsigned long)i2c_adap_p->write; | |
75 | addr += gd->reloc_off; | |
76 | i2c_adap_p->write = (int (*)(uint8_t, uint, int, uint8_t *, | |
77 | int))addr; | |
78 | /* i2c_set_bus_speed() */ | |
79 | addr = (unsigned long)i2c_adap_p->set_bus_speed; | |
80 | addr += gd->reloc_off; | |
81 | i2c_adap_p->set_bus_speed = (uint (*)(uint))addr; | |
82 | /* name */ | |
83 | addr = (unsigned long)i2c_adap_p->name; | |
84 | addr += gd->reloc_off; | |
85 | i2c_adap_p->name = (char *)addr; | |
86 | tmp++; | |
87 | i2c_adap_p = tmp; | |
88 | } | |
89 | #endif | |
90 | } | |
91 | ||
92 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS | |
93 | /* | |
94 | * i2c_mux_set() | |
95 | * ------------- | |
96 | * | |
97 | * This turns on the given channel on I2C multiplexer chip connected to | |
98 | * a given I2C adapter directly or via other multiplexers. In the latter | |
99 | * case the entire multiplexer chain must be initialized first starting | |
100 | * with the one connected directly to the adapter. When disabling a chain | |
101 | * muxes must be programmed in reverse order, starting with the one | |
102 | * farthest from the adapter. | |
103 | * | |
104 | * mux_id is the multiplexer chip type from defined in i2c.h. So far only | |
105 | * NXP (Philips) PCA954x multiplexers are supported. Switches are NOT | |
106 | * supported (anybody uses them?) | |
107 | */ | |
108 | ||
109 | static int i2c_mux_set(struct i2c_adapter *adap, int mux_id, int chip, | |
110 | int channel) | |
111 | { | |
112 | uint8_t buf; | |
113 | int ret; | |
114 | ||
115 | /* channel < 0 - turn off the mux */ | |
116 | if (channel < 0) { | |
117 | buf = 0; | |
118 | ret = adap->write(adap, chip, 0, 0, &buf, 1); | |
119 | if (ret) | |
120 | printf("%s: Could not turn off the mux.\n", __func__); | |
121 | return ret; | |
122 | } | |
123 | ||
124 | switch (mux_id) { | |
125 | case I2C_MUX_PCA9540_ID: | |
126 | case I2C_MUX_PCA9542_ID: | |
127 | if (channel > 1) | |
128 | return -1; | |
129 | buf = (uint8_t)((channel & 0x01) | (1 << 2)); | |
130 | break; | |
131 | case I2C_MUX_PCA9544_ID: | |
132 | if (channel > 3) | |
133 | return -1; | |
134 | buf = (uint8_t)((channel & 0x03) | (1 << 2)); | |
135 | break; | |
136 | case I2C_MUX_PCA9547_ID: | |
137 | if (channel > 7) | |
138 | return -1; | |
139 | buf = (uint8_t)((channel & 0x07) | (1 << 3)); | |
140 | break; | |
e6658749 MB |
141 | case I2C_MUX_PCA9548_ID: |
142 | if (channel > 7) | |
143 | return -1; | |
144 | buf = (uint8_t)(0x01 << channel); | |
145 | break; | |
385c9ef5 HS |
146 | default: |
147 | printf("%s: wrong mux id: %d\n", __func__, mux_id); | |
148 | return -1; | |
149 | } | |
150 | ||
151 | ret = adap->write(adap, chip, 0, 0, &buf, 1); | |
152 | if (ret) | |
153 | printf("%s: could not set mux: id: %d chip: %x channel: %d\n", | |
154 | __func__, mux_id, chip, channel); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | static int i2c_mux_set_all(void) | |
159 | { | |
160 | struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS]; | |
161 | int i; | |
162 | ||
163 | /* Connect requested bus if behind muxes */ | |
164 | if (i2c_bus_tmp->next_hop[0].chip != 0) { | |
165 | /* Set all muxes along the path to that bus */ | |
166 | for (i = 0; i < CONFIG_SYS_I2C_MAX_HOPS; i++) { | |
167 | int ret; | |
168 | ||
169 | if (i2c_bus_tmp->next_hop[i].chip == 0) | |
170 | break; | |
171 | ||
172 | ret = i2c_mux_set(I2C_ADAP, | |
173 | i2c_bus_tmp->next_hop[i].mux.id, | |
174 | i2c_bus_tmp->next_hop[i].chip, | |
175 | i2c_bus_tmp->next_hop[i].channel); | |
176 | if (ret != 0) | |
177 | return ret; | |
178 | } | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static int i2c_mux_disconnet_all(void) | |
184 | { | |
185 | struct i2c_bus_hose *i2c_bus_tmp = &i2c_bus[I2C_BUS]; | |
186 | int i; | |
187 | uint8_t buf; | |
188 | ||
189 | if (I2C_ADAP->init_done == 0) | |
190 | return 0; | |
191 | ||
192 | /* Disconnect current bus (turn off muxes if any) */ | |
193 | if ((i2c_bus_tmp->next_hop[0].chip != 0) && | |
194 | (I2C_ADAP->init_done != 0)) { | |
195 | i = CONFIG_SYS_I2C_MAX_HOPS; | |
196 | do { | |
197 | uint8_t chip; | |
198 | int ret; | |
199 | ||
200 | chip = i2c_bus_tmp->next_hop[--i].chip; | |
201 | if (chip == 0) | |
202 | continue; | |
203 | ||
204 | ret = I2C_ADAP->write(I2C_ADAP, chip, 0, 0, &buf, 1); | |
205 | if (ret != 0) { | |
206 | printf("i2c: mux diconnect error\n"); | |
207 | return ret; | |
208 | } | |
209 | } while (i > 0); | |
210 | } | |
211 | ||
212 | return 0; | |
213 | } | |
214 | #endif | |
215 | ||
216 | /* | |
217 | * i2c_init_bus(): | |
218 | * --------------- | |
219 | * | |
220 | * Initializes one bus. Will initialize the parent adapter. No current bus | |
221 | * changes, no mux (if any) setup. | |
222 | */ | |
223 | static void i2c_init_bus(unsigned int bus_no, int speed, int slaveaddr) | |
224 | { | |
225 | if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) | |
226 | return; | |
227 | ||
228 | I2C_ADAP->init(I2C_ADAP, speed, slaveaddr); | |
229 | ||
230 | if (gd->flags & GD_FLG_RELOC) { | |
231 | I2C_ADAP->init_done = 1; | |
232 | I2C_ADAP->speed = speed; | |
233 | I2C_ADAP->slaveaddr = slaveaddr; | |
234 | } | |
235 | } | |
236 | ||
237 | /* implement possible board specific board init */ | |
238 | static void __def_i2c_init_board(void) | |
239 | { | |
240 | } | |
241 | void i2c_init_board(void) | |
242 | __attribute__((weak, alias("__def_i2c_init_board"))); | |
243 | ||
244 | /* | |
245 | * i2c_init_all(): | |
246 | * | |
247 | * not longer needed, will deleted. Actual init the SPD_BUS | |
248 | * for compatibility. | |
249 | * i2c_adap[] must be initialized beforehead with function pointers and | |
250 | * data, including speed and slaveaddr. | |
251 | */ | |
252 | void i2c_init_all(void) | |
253 | { | |
254 | i2c_init_board(); | |
255 | i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM); | |
256 | return; | |
257 | } | |
258 | ||
259 | /* | |
260 | * i2c_get_bus_num(): | |
261 | * ------------------ | |
262 | * | |
263 | * Returns index of currently active I2C bus. Zero-based. | |
264 | */ | |
265 | unsigned int i2c_get_bus_num(void) | |
266 | { | |
267 | return gd->cur_i2c_bus; | |
268 | } | |
269 | ||
270 | /* | |
271 | * i2c_set_bus_num(): | |
272 | * ------------------ | |
273 | * | |
274 | * Change the active I2C bus. Subsequent read/write calls will | |
275 | * go to this one. Sets all of the muxes in a proper condition | |
276 | * if that bus is behind muxes. | |
277 | * If previously selected bus is behind the muxes turns off all the | |
278 | * muxes along the path to that bus. | |
279 | * | |
280 | * bus - bus index, zero based | |
281 | * | |
282 | * Returns: 0 on success, not 0 on failure | |
283 | */ | |
284 | int i2c_set_bus_num(unsigned int bus) | |
285 | { | |
13c2433c HS |
286 | int max; |
287 | ||
288 | if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0)) | |
289 | return 0; | |
385c9ef5 | 290 | |
385c9ef5 HS |
291 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS |
292 | if (bus >= CONFIG_SYS_NUM_I2C_BUSES) | |
293 | return -1; | |
294 | #endif | |
295 | ||
13c2433c HS |
296 | max = ll_entry_count(struct i2c_adapter, i2c); |
297 | if (I2C_ADAPTER(bus) >= max) { | |
298 | printf("Error, wrong i2c adapter %d max %d possible\n", | |
299 | I2C_ADAPTER(bus), max); | |
300 | return -2; | |
301 | } | |
385c9ef5 HS |
302 | |
303 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS | |
304 | i2c_mux_disconnet_all(); | |
305 | #endif | |
306 | ||
307 | gd->cur_i2c_bus = bus; | |
308 | if (I2C_ADAP->init_done == 0) | |
309 | i2c_init_bus(bus, I2C_ADAP->speed, I2C_ADAP->slaveaddr); | |
310 | ||
311 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS | |
312 | i2c_mux_set_all(); | |
313 | #endif | |
314 | return 0; | |
315 | } | |
316 | ||
317 | /* | |
318 | * Probe the given I2C chip address. Returns 0 if a chip responded, | |
319 | * not 0 on failure. | |
320 | */ | |
321 | int i2c_probe(uint8_t chip) | |
322 | { | |
323 | return I2C_ADAP->probe(I2C_ADAP, chip); | |
324 | } | |
325 | ||
326 | /* | |
327 | * Read/Write interface: | |
328 | * chip: I2C chip address, range 0..127 | |
329 | * addr: Memory (register) address within the chip | |
330 | * alen: Number of bytes to use for addr (typically 1, 2 for larger | |
331 | * memories, 0 for register type devices with only one | |
332 | * register) | |
333 | * buffer: Where to read/write the data | |
334 | * len: How many bytes to read/write | |
335 | * | |
336 | * Returns: 0 on success, not 0 on failure | |
337 | */ | |
338 | int i2c_read(uint8_t chip, unsigned int addr, int alen, | |
339 | uint8_t *buffer, int len) | |
340 | { | |
341 | return I2C_ADAP->read(I2C_ADAP, chip, addr, alen, buffer, len); | |
342 | } | |
343 | ||
344 | int i2c_write(uint8_t chip, unsigned int addr, int alen, | |
345 | uint8_t *buffer, int len) | |
346 | { | |
347 | return I2C_ADAP->write(I2C_ADAP, chip, addr, alen, buffer, len); | |
348 | } | |
349 | ||
350 | unsigned int i2c_set_bus_speed(unsigned int speed) | |
351 | { | |
352 | unsigned int ret; | |
353 | ||
354 | if (I2C_ADAP->set_bus_speed == NULL) | |
355 | return 0; | |
356 | ret = I2C_ADAP->set_bus_speed(I2C_ADAP, speed); | |
357 | if (gd->flags & GD_FLG_RELOC) | |
358 | I2C_ADAP->speed = ret; | |
359 | ||
360 | return ret; | |
361 | } | |
362 | ||
363 | unsigned int i2c_get_bus_speed(void) | |
364 | { | |
365 | struct i2c_adapter *cur = I2C_ADAP; | |
366 | return cur->speed; | |
367 | } | |
368 | ||
369 | uint8_t i2c_reg_read(uint8_t addr, uint8_t reg) | |
370 | { | |
371 | uint8_t buf; | |
372 | ||
373 | #ifdef CONFIG_8xx | |
374 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ | |
375 | /* maybe it is now the time for it ... */ | |
376 | i2c_set_bus_num(i2c_get_bus_num()); | |
377 | #endif | |
378 | i2c_read(addr, reg, 1, &buf, 1); | |
379 | ||
380 | #ifdef DEBUG | |
381 | printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n", | |
382 | __func__, i2c_get_bus_num(), addr, reg, buf); | |
383 | #endif | |
384 | ||
385 | return buf; | |
386 | } | |
387 | ||
388 | void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val) | |
389 | { | |
390 | #ifdef CONFIG_8xx | |
391 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ | |
392 | /* maybe it is now the time for it ... */ | |
393 | i2c_set_bus_num(i2c_get_bus_num()); | |
394 | #endif | |
395 | ||
396 | #ifdef DEBUG | |
397 | printf("%s: bus=%d addr=0x%02x, reg=0x%02x, val=0x%02x\n", | |
398 | __func__, i2c_get_bus_num(), addr, reg, val); | |
399 | #endif | |
400 | ||
401 | i2c_write(addr, reg, 1, &val, 1); | |
402 | } | |
403 | ||
404 | void __i2c_init(int speed, int slaveaddr) | |
405 | { | |
406 | i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr); | |
407 | } | |
408 | void i2c_init(int speed, int slaveaddr) | |
409 | __attribute__((weak, alias("__i2c_init"))); |