blob: 8fd17d190af7cd532a445b77aaadceee00967bdd [file] [log] [blame]
wdenk1f045212002-03-10 14:37:15 +00001/*
Heiko Schocher385c9ef2012-01-16 21:12:23 +00002 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3 * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
4 * Changes for multibus/multiadapter I2C support.
5 *
wdenk1f045212002-03-10 14:37:15 +00006 * (C) Copyright 2001
7 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk1f045212002-03-10 14:37:15 +000010 *
11 * The original I2C interface was
12 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
13 * AIRVENT SAM s.p.a - RIMINI(ITALY)
14 * but has been changed substantially.
15 */
16
17#ifndef _I2C_H_
18#define _I2C_H_
19
20/*
21 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
22 *
23 * The implementation MUST NOT use static or global variables if the
24 * I2C routines are used to read SDRAM configuration information
25 * because this is done before the memories are initialized. Limited
26 * use of stack-based variables are OK (the initial stack size is
27 * limited).
28 *
29 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
30 */
31
32/*
33 * Configuration items.
34 */
35#define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
36
Heiko Schocher385c9ef2012-01-16 21:12:23 +000037#if !defined(CONFIG_SYS_I2C_MAX_HOPS)
38/* no muxes used bus = i2c adapters */
39#define CONFIG_SYS_I2C_DIRECT_BUS 1
40#define CONFIG_SYS_I2C_MAX_HOPS 0
41#define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
Stefan Roese79b2d0b2007-02-20 10:27:08 +010042#else
Heiko Schocher385c9ef2012-01-16 21:12:23 +000043/* we use i2c muxes */
44#undef CONFIG_SYS_I2C_DIRECT_BUS
Stefan Roese79b2d0b2007-02-20 10:27:08 +010045#endif
46
Stefan Roese8c120452007-03-01 07:03:25 +010047/* define the I2C bus number for RTC and DTT if not already done */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020048#if !defined(CONFIG_SYS_RTC_BUS_NUM)
49#define CONFIG_SYS_RTC_BUS_NUM 0
Stefan Roese8c120452007-03-01 07:03:25 +010050#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#if !defined(CONFIG_SYS_DTT_BUS_NUM)
52#define CONFIG_SYS_DTT_BUS_NUM 0
Stefan Roese8c120452007-03-01 07:03:25 +010053#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054#if !defined(CONFIG_SYS_SPD_BUS_NUM)
55#define CONFIG_SYS_SPD_BUS_NUM 0
Matthias Fuchsd8a8ea52007-03-08 16:20:32 +010056#endif
Stefan Roese8c120452007-03-01 07:03:25 +010057
Heiko Schocher385c9ef2012-01-16 21:12:23 +000058struct i2c_adapter {
59 void (*init)(struct i2c_adapter *adap, int speed,
60 int slaveaddr);
61 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
62 int (*read)(struct i2c_adapter *adap, uint8_t chip,
63 uint addr, int alen, uint8_t *buffer,
64 int len);
65 int (*write)(struct i2c_adapter *adap, uint8_t chip,
66 uint addr, int alen, uint8_t *buffer,
67 int len);
68 uint (*set_bus_speed)(struct i2c_adapter *adap,
69 uint speed);
70 int speed;
71 int slaveaddr;
72 int init_done;
73 int hwadapnr;
74 char *name;
75};
76
77#define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
78 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
79 { \
80 .init = _init, \
81 .probe = _probe, \
82 .read = _read, \
83 .write = _write, \
84 .set_bus_speed = _set_speed, \
85 .speed = _speed, \
86 .slaveaddr = _slaveaddr, \
87 .init_done = 0, \
88 .hwadapnr = _hwadapnr, \
89 .name = #_name \
90};
91
92#define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
93 _set_speed, _speed, _slaveaddr, _hwadapnr) \
94 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
95 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
96 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
97
98struct i2c_adapter *i2c_get_adapter(int index);
99
100#ifndef CONFIG_SYS_I2C_DIRECT_BUS
101struct i2c_mux {
102 int id;
103 char name[16];
104};
105
106struct i2c_next_hop {
107 struct i2c_mux mux;
108 uint8_t chip;
109 uint8_t channel;
110};
111
112struct i2c_bus_hose {
113 int adapter;
114 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
115};
116#define I2C_NULL_HOP {{-1, ""}, 0, 0}
117extern struct i2c_bus_hose i2c_bus[];
118
119#define I2C_ADAPTER(bus) i2c_bus[bus].adapter
120#else
121#define I2C_ADAPTER(bus) bus
122#endif
123#define I2C_BUS gd->cur_i2c_bus
124
125#define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
126#define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
127#define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
128
129#ifndef CONFIG_SYS_I2C_DIRECT_BUS
130#define I2C_MUX_PCA9540_ID 1
131#define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
132#define I2C_MUX_PCA9542_ID 2
133#define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
134#define I2C_MUX_PCA9544_ID 3
135#define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
136#define I2C_MUX_PCA9547_ID 4
137#define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
138#endif
139
Heiko Schocher98aed372008-10-15 09:35:26 +0200140#ifndef I2C_SOFT_DECLARATIONS
141# if defined(CONFIG_MPC8260)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200142# define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
Heiko Schocher98aed372008-10-15 09:35:26 +0200143# elif defined(CONFIG_8xx)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200144# define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100145
146# elif (defined(CONFIG_AT91RM9200) || \
147 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
148 defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY)
esw@bus-elektronik.de78132272011-12-20 06:05:30 +0000149# define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
Heiko Schocher98aed372008-10-15 09:35:26 +0200150# else
151# define I2C_SOFT_DECLARATIONS
152# endif
153#endif
Timur Tabiecf5f072008-12-03 11:28:30 -0600154
155#ifdef CONFIG_8xx
Peter Tyser9c90a2c2009-04-24 15:34:05 -0500156/* Set default value for the I2C bus speed on 8xx. In the
Timur Tabiecf5f072008-12-03 11:28:30 -0600157 * future, we'll define these in all 8xx board config files.
158 */
159#ifndef CONFIG_SYS_I2C_SPEED
160#define CONFIG_SYS_I2C_SPEED 50000
161#endif
Timur Tabiecf5f072008-12-03 11:28:30 -0600162#endif
Peter Tyser9c90a2c2009-04-24 15:34:05 -0500163
164/*
165 * Many boards/controllers/drivers don't support an I2C slave interface so
166 * provide a default slave address for them for use in common code. A real
167 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
168 * support a slave interface.
169 */
170#ifndef CONFIG_SYS_I2C_SLAVE
171#define CONFIG_SYS_I2C_SLAVE 0xfe
Timur Tabiecf5f072008-12-03 11:28:30 -0600172#endif
173
wdenk1f045212002-03-10 14:37:15 +0000174/*
175 * Initialization, must be called once on start up, may be called
176 * repeatedly to change the speed and slave addresses.
177 */
178void i2c_init(int speed, int slaveaddr);
wdenk06d01db2003-03-14 20:47:52 +0000179void i2c_init_board(void);
Richard Retanubun26a33502010-04-12 15:08:17 -0400180#ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
181void i2c_board_late_init(void);
182#endif
wdenk1f045212002-03-10 14:37:15 +0000183
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000184#ifdef CONFIG_SYS_I2C
185/*
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000186 * i2c_get_bus_num:
187 *
188 * Returns index of currently active I2C bus. Zero-based.
189 */
190unsigned int i2c_get_bus_num(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200191
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000192/*
193 * i2c_set_bus_num:
194 *
195 * Change the active I2C bus. Subsequent read/write calls will
196 * go to this one.
197 *
198 * bus - bus index, zero based
199 *
200 * Returns: 0 on success, not 0 on failure
201 *
202 */
203int i2c_set_bus_num(unsigned int bus);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200204
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000205/*
206 * i2c_init_all():
207 *
208 * Initializes all I2C adapters in the system. All i2c_adap structures must
209 * be initialized beforehead with function pointers and data, including
210 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
211 */
212void i2c_init_all(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200213
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000214/*
215 * Probe the given I2C chip address. Returns 0 if a chip responded,
216 * not 0 on failure.
217 */
218int i2c_probe(uint8_t chip);
219
220/*
221 * Read/Write interface:
222 * chip: I2C chip address, range 0..127
223 * addr: Memory (register) address within the chip
224 * alen: Number of bytes to use for addr (typically 1, 2 for larger
225 * memories, 0 for register type devices with only one
226 * register)
227 * buffer: Where to read/write the data
228 * len: How many bytes to read/write
229 *
230 * Returns: 0 on success, not 0 on failure
231 */
232int i2c_read(uint8_t chip, unsigned int addr, int alen,
233 uint8_t *buffer, int len);
234
235int i2c_write(uint8_t chip, unsigned int addr, int alen,
236 uint8_t *buffer, int len);
237
238/*
239 * Utility routines to read/write registers.
240 */
241uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
242
243void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
244
245/*
246 * i2c_set_bus_speed:
247 *
248 * Change the speed of the active I2C bus
249 *
250 * speed - bus speed in Hz
251 *
252 * Returns: new bus speed
253 *
254 */
255unsigned int i2c_set_bus_speed(unsigned int speed);
256
257/*
258 * i2c_get_bus_speed:
259 *
260 * Returns speed of currently active I2C bus in Hz
261 */
262
263unsigned int i2c_get_bus_speed(void);
264
265/*
266 * i2c_reloc_fixup:
267 *
268 * Adjusts I2C pointers after U-Boot is relocated to DRAM
269 */
270void i2c_reloc_fixup(void);
Heiko Schocherea818db2013-01-29 08:53:15 +0100271#if defined(CONFIG_SYS_I2C_SOFT)
272void i2c_soft_init(void);
273void i2c_soft_active(void);
274void i2c_soft_tristate(void);
275int i2c_soft_read(void);
276void i2c_soft_sda(int bit);
277void i2c_soft_scl(int bit);
278void i2c_soft_delay(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200279#endif
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000280#else
Heiko Schocher67b23a32008-10-15 09:39:47 +0200281
wdenk1f045212002-03-10 14:37:15 +0000282/*
283 * Probe the given I2C chip address. Returns 0 if a chip responded,
284 * not 0 on failure.
285 */
286int i2c_probe(uchar chip);
287
288/*
289 * Read/Write interface:
290 * chip: I2C chip address, range 0..127
291 * addr: Memory (register) address within the chip
292 * alen: Number of bytes to use for addr (typically 1, 2 for larger
293 * memories, 0 for register type devices with only one
294 * register)
295 * buffer: Where to read/write the data
296 * len: How many bytes to read/write
297 *
298 * Returns: 0 on success, not 0 on failure
299 */
300int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
301int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
302
303/*
304 * Utility routines to read/write registers.
305 */
Timur Tabiecf5f072008-12-03 11:28:30 -0600306static inline u8 i2c_reg_read(u8 addr, u8 reg)
307{
308 u8 buf;
309
310#ifdef CONFIG_8xx
311 /* MPC8xx needs this. Maybe one day we can get rid of it. */
312 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
313#endif
314
315#ifdef DEBUG
316 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
317#endif
318
Timur Tabiecf5f072008-12-03 11:28:30 -0600319 i2c_read(addr, reg, 1, &buf, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600320
321 return buf;
322}
323
324static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
325{
326#ifdef CONFIG_8xx
327 /* MPC8xx needs this. Maybe one day we can get rid of it. */
328 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
329#endif
330
331#ifdef DEBUG
332 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
333 __func__, addr, reg, val);
334#endif
335
Timur Tabiecf5f072008-12-03 11:28:30 -0600336 i2c_write(addr, reg, 1, &val, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600337}
wdenk1f045212002-03-10 14:37:15 +0000338
Ben Warrenbb99ad62006-09-07 16:50:54 -0400339/*
340 * Functions for setting the current I2C bus and its speed
341 */
342
343/*
344 * i2c_set_bus_num:
345 *
346 * Change the active I2C bus. Subsequent read/write calls will
347 * go to this one.
348 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200349 * bus - bus index, zero based
Ben Warrenbb99ad62006-09-07 16:50:54 -0400350 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200351 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400352 *
353 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600354int i2c_set_bus_num(unsigned int bus);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400355
356/*
357 * i2c_get_bus_num:
358 *
359 * Returns index of currently active I2C bus. Zero-based.
360 */
361
Timur Tabi9ca880a2006-10-31 21:23:16 -0600362unsigned int i2c_get_bus_num(void);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400363
364/*
365 * i2c_set_bus_speed:
366 *
367 * Change the speed of the active I2C bus
368 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200369 * speed - bus speed in Hz
Ben Warrenbb99ad62006-09-07 16:50:54 -0400370 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200371 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400372 *
373 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600374int i2c_set_bus_speed(unsigned int);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400375
376/*
377 * i2c_get_bus_speed:
378 *
379 * Returns speed of currently active I2C bus in Hz
380 */
381
Timur Tabi9ca880a2006-10-31 21:23:16 -0600382unsigned int i2c_get_bus_speed(void);
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000383#endif /* CONFIG_SYS_I2C */
384
385/*
386 * only for backwardcompatibility, should go away if we switched
387 * completely to new multibus support.
388 */
389#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
390# if !defined(CONFIG_SYS_MAX_I2C_BUS)
391# define CONFIG_SYS_MAX_I2C_BUS 2
392# endif
Łukasz Majewskiea0f73a2013-08-16 15:31:45 +0200393# define I2C_MULTI_BUS 1
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000394#else
395# define CONFIG_SYS_MAX_I2C_BUS 1
396# define I2C_MULTI_BUS 0
397#endif
Ben Warrenbb99ad62006-09-07 16:50:54 -0400398
Marek Vasutcd7b4e82011-10-25 11:40:57 +0200399/* NOTE: These two functions MUST be always_inline to avoid code growth! */
400static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
401static inline unsigned int I2C_GET_BUS(void)
402{
403 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
404}
405
406static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
407static inline void I2C_SET_BUS(unsigned int bus)
408{
409 if (I2C_MULTI_BUS)
410 i2c_set_bus_num(bus);
411}
412
Łukasz Majewski7ca8f732012-09-04 23:15:20 +0000413/* Multi I2C definitions */
414enum {
415 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
416 I2C_8, I2C_9, I2C_10,
417};
418
419/* Multi I2C busses handling */
420#ifdef CONFIG_SOFT_I2C_MULTI_BUS
421extern int get_multi_scl_pin(void);
422extern int get_multi_sda_pin(void);
423extern int multi_i2c_init(void);
424#endif
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000425
426/**
427 * Get FDT values for i2c bus.
428 *
429 * @param blob Device tree blbo
430 * @return the number of I2C bus
431 */
432void board_i2c_init(const void *blob);
433
434/**
435 * Find the I2C bus number by given a FDT I2C node.
436 *
437 * @param blob Device tree blbo
438 * @param node FDT I2C node to find
439 * @return the number of I2C bus (zero based), or -1 on error
440 */
441int i2c_get_bus_num_fdt(int node);
442
443/**
444 * Reset the I2C bus represented by the given a FDT I2C node.
445 *
446 * @param blob Device tree blbo
447 * @param node FDT I2C node to find
448 * @return 0 if port was reset, -1 if not found
449 */
450int i2c_reset_port_fdt(const void *blob, int node);
wdenk1f045212002-03-10 14:37:15 +0000451#endif /* _I2C_H_ */