i2c.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * 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. *
  6. * (C) Copyright 2001
  7. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. *
  27. * The original I2C interface was
  28. * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
  29. * AIRVENT SAM s.p.a - RIMINI(ITALY)
  30. * but has been changed substantially.
  31. */
  32. #ifndef _I2C_H_
  33. #define _I2C_H_
  34. /*
  35. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  36. *
  37. * The implementation MUST NOT use static or global variables if the
  38. * I2C routines are used to read SDRAM configuration information
  39. * because this is done before the memories are initialized. Limited
  40. * use of stack-based variables are OK (the initial stack size is
  41. * limited).
  42. *
  43. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  44. */
  45. /*
  46. * Configuration items.
  47. */
  48. #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
  49. #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
  50. /* no muxes used bus = i2c adapters */
  51. #define CONFIG_SYS_I2C_DIRECT_BUS 1
  52. #define CONFIG_SYS_I2C_MAX_HOPS 0
  53. #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
  54. #else
  55. /* we use i2c muxes */
  56. #undef CONFIG_SYS_I2C_DIRECT_BUS
  57. #endif
  58. /* define the I2C bus number for RTC and DTT if not already done */
  59. #if !defined(CONFIG_SYS_RTC_BUS_NUM)
  60. #define CONFIG_SYS_RTC_BUS_NUM 0
  61. #endif
  62. #if !defined(CONFIG_SYS_DTT_BUS_NUM)
  63. #define CONFIG_SYS_DTT_BUS_NUM 0
  64. #endif
  65. #if !defined(CONFIG_SYS_SPD_BUS_NUM)
  66. #define CONFIG_SYS_SPD_BUS_NUM 0
  67. #endif
  68. struct i2c_adapter {
  69. void (*init)(struct i2c_adapter *adap, int speed,
  70. int slaveaddr);
  71. int (*probe)(struct i2c_adapter *adap, uint8_t chip);
  72. int (*read)(struct i2c_adapter *adap, uint8_t chip,
  73. uint addr, int alen, uint8_t *buffer,
  74. int len);
  75. int (*write)(struct i2c_adapter *adap, uint8_t chip,
  76. uint addr, int alen, uint8_t *buffer,
  77. int len);
  78. uint (*set_bus_speed)(struct i2c_adapter *adap,
  79. uint speed);
  80. int speed;
  81. int slaveaddr;
  82. int init_done;
  83. int hwadapnr;
  84. char *name;
  85. };
  86. #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  87. _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
  88. { \
  89. .init = _init, \
  90. .probe = _probe, \
  91. .read = _read, \
  92. .write = _write, \
  93. .set_bus_speed = _set_speed, \
  94. .speed = _speed, \
  95. .slaveaddr = _slaveaddr, \
  96. .init_done = 0, \
  97. .hwadapnr = _hwadapnr, \
  98. .name = #_name \
  99. };
  100. #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
  101. _set_speed, _speed, _slaveaddr, _hwadapnr) \
  102. ll_entry_declare(struct i2c_adapter, _name, i2c) = \
  103. U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  104. _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
  105. struct i2c_adapter *i2c_get_adapter(int index);
  106. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  107. struct i2c_mux {
  108. int id;
  109. char name[16];
  110. };
  111. struct i2c_next_hop {
  112. struct i2c_mux mux;
  113. uint8_t chip;
  114. uint8_t channel;
  115. };
  116. struct i2c_bus_hose {
  117. int adapter;
  118. struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
  119. };
  120. #define I2C_NULL_HOP {{-1, ""}, 0, 0}
  121. extern struct i2c_bus_hose i2c_bus[];
  122. #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
  123. #else
  124. #define I2C_ADAPTER(bus) bus
  125. #endif
  126. #define I2C_BUS gd->cur_i2c_bus
  127. #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
  128. #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
  129. #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
  130. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  131. #define I2C_MUX_PCA9540_ID 1
  132. #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
  133. #define I2C_MUX_PCA9542_ID 2
  134. #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
  135. #define I2C_MUX_PCA9544_ID 3
  136. #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
  137. #define I2C_MUX_PCA9547_ID 4
  138. #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
  139. #endif
  140. #ifndef I2C_SOFT_DECLARATIONS
  141. # if defined(CONFIG_MPC8260)
  142. # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
  143. # elif defined(CONFIG_8xx)
  144. # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  145. # elif (defined(CONFIG_AT91RM9200) || \
  146. defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
  147. defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY)
  148. # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  149. # else
  150. # define I2C_SOFT_DECLARATIONS
  151. # endif
  152. #endif
  153. #ifdef CONFIG_8xx
  154. /* Set default value for the I2C bus speed on 8xx. In the
  155. * future, we'll define these in all 8xx board config files.
  156. */
  157. #ifndef CONFIG_SYS_I2C_SPEED
  158. #define CONFIG_SYS_I2C_SPEED 50000
  159. #endif
  160. #endif
  161. /*
  162. * Many boards/controllers/drivers don't support an I2C slave interface so
  163. * provide a default slave address for them for use in common code. A real
  164. * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
  165. * support a slave interface.
  166. */
  167. #ifndef CONFIG_SYS_I2C_SLAVE
  168. #define CONFIG_SYS_I2C_SLAVE 0xfe
  169. #endif
  170. /*
  171. * Initialization, must be called once on start up, may be called
  172. * repeatedly to change the speed and slave addresses.
  173. */
  174. void i2c_init(int speed, int slaveaddr);
  175. void i2c_init_board(void);
  176. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  177. void i2c_board_late_init(void);
  178. #endif
  179. #if defined(CONFIG_I2C_MUX)
  180. typedef struct _mux {
  181. uchar chip;
  182. uchar channel;
  183. char *name;
  184. struct _mux *next;
  185. } I2C_MUX;
  186. typedef struct _mux_device {
  187. int busid;
  188. I2C_MUX *mux; /* List of muxes, to reach the device */
  189. struct _mux_device *next;
  190. } I2C_MUX_DEVICE;
  191. I2C_MUX_DEVICE *i2c_mux_search_device(int id);
  192. I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf);
  193. int i2x_mux_select_mux(int bus);
  194. int i2c_mux_ident_muxstring_f (uchar *buf);
  195. #endif
  196. #ifdef CONFIG_SYS_I2C
  197. /*
  198. * i2c_get_bus_num:
  199. *
  200. * Returns index of currently active I2C bus. Zero-based.
  201. */
  202. unsigned int i2c_get_bus_num(void);
  203. /*
  204. * i2c_set_bus_num:
  205. *
  206. * Change the active I2C bus. Subsequent read/write calls will
  207. * go to this one.
  208. *
  209. * bus - bus index, zero based
  210. *
  211. * Returns: 0 on success, not 0 on failure
  212. *
  213. */
  214. int i2c_set_bus_num(unsigned int bus);
  215. /*
  216. * i2c_init_all():
  217. *
  218. * Initializes all I2C adapters in the system. All i2c_adap structures must
  219. * be initialized beforehead with function pointers and data, including
  220. * speed and slaveaddr. Returns 0 on success, non-0 on failure.
  221. */
  222. void i2c_init_all(void);
  223. /*
  224. * Probe the given I2C chip address. Returns 0 if a chip responded,
  225. * not 0 on failure.
  226. */
  227. int i2c_probe(uint8_t chip);
  228. /*
  229. * Read/Write interface:
  230. * chip: I2C chip address, range 0..127
  231. * addr: Memory (register) address within the chip
  232. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  233. * memories, 0 for register type devices with only one
  234. * register)
  235. * buffer: Where to read/write the data
  236. * len: How many bytes to read/write
  237. *
  238. * Returns: 0 on success, not 0 on failure
  239. */
  240. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  241. uint8_t *buffer, int len);
  242. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  243. uint8_t *buffer, int len);
  244. /*
  245. * Utility routines to read/write registers.
  246. */
  247. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
  248. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
  249. /*
  250. * i2c_set_bus_speed:
  251. *
  252. * Change the speed of the active I2C bus
  253. *
  254. * speed - bus speed in Hz
  255. *
  256. * Returns: new bus speed
  257. *
  258. */
  259. unsigned int i2c_set_bus_speed(unsigned int speed);
  260. /*
  261. * i2c_get_bus_speed:
  262. *
  263. * Returns speed of currently active I2C bus in Hz
  264. */
  265. unsigned int i2c_get_bus_speed(void);
  266. /*
  267. * i2c_reloc_fixup:
  268. *
  269. * Adjusts I2C pointers after U-Boot is relocated to DRAM
  270. */
  271. void i2c_reloc_fixup(void);
  272. #else
  273. /*
  274. * Probe the given I2C chip address. Returns 0 if a chip responded,
  275. * not 0 on failure.
  276. */
  277. int i2c_probe(uchar chip);
  278. /*
  279. * Read/Write interface:
  280. * chip: I2C chip address, range 0..127
  281. * addr: Memory (register) address within the chip
  282. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  283. * memories, 0 for register type devices with only one
  284. * register)
  285. * buffer: Where to read/write the data
  286. * len: How many bytes to read/write
  287. *
  288. * Returns: 0 on success, not 0 on failure
  289. */
  290. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
  291. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
  292. /*
  293. * Utility routines to read/write registers.
  294. */
  295. static inline u8 i2c_reg_read(u8 addr, u8 reg)
  296. {
  297. u8 buf;
  298. #ifdef CONFIG_8xx
  299. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  300. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  301. #endif
  302. #ifdef DEBUG
  303. printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
  304. #endif
  305. i2c_read(addr, reg, 1, &buf, 1);
  306. return buf;
  307. }
  308. static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
  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. #ifdef DEBUG
  315. printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  316. __func__, addr, reg, val);
  317. #endif
  318. i2c_write(addr, reg, 1, &val, 1);
  319. }
  320. /*
  321. * Functions for setting the current I2C bus and its speed
  322. */
  323. /*
  324. * i2c_set_bus_num:
  325. *
  326. * Change the active I2C bus. Subsequent read/write calls will
  327. * go to this one.
  328. *
  329. * bus - bus index, zero based
  330. *
  331. * Returns: 0 on success, not 0 on failure
  332. *
  333. */
  334. int i2c_set_bus_num(unsigned int bus);
  335. /*
  336. * i2c_get_bus_num:
  337. *
  338. * Returns index of currently active I2C bus. Zero-based.
  339. */
  340. unsigned int i2c_get_bus_num(void);
  341. /*
  342. * i2c_set_bus_speed:
  343. *
  344. * Change the speed of the active I2C bus
  345. *
  346. * speed - bus speed in Hz
  347. *
  348. * Returns: 0 on success, not 0 on failure
  349. *
  350. */
  351. int i2c_set_bus_speed(unsigned int);
  352. /*
  353. * i2c_get_bus_speed:
  354. *
  355. * Returns speed of currently active I2C bus in Hz
  356. */
  357. unsigned int i2c_get_bus_speed(void);
  358. #endif /* CONFIG_SYS_I2C */
  359. /*
  360. * only for backwardcompatibility, should go away if we switched
  361. * completely to new multibus support.
  362. */
  363. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  364. # if !defined(CONFIG_SYS_MAX_I2C_BUS)
  365. # define CONFIG_SYS_MAX_I2C_BUS 2
  366. # endif
  367. # define I2C_MULTI_BUS 0
  368. #else
  369. # define CONFIG_SYS_MAX_I2C_BUS 1
  370. # define I2C_MULTI_BUS 0
  371. #endif
  372. /* NOTE: These two functions MUST be always_inline to avoid code growth! */
  373. static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
  374. static inline unsigned int I2C_GET_BUS(void)
  375. {
  376. return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
  377. }
  378. static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
  379. static inline void I2C_SET_BUS(unsigned int bus)
  380. {
  381. if (I2C_MULTI_BUS)
  382. i2c_set_bus_num(bus);
  383. }
  384. /* Multi I2C definitions */
  385. enum {
  386. I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
  387. I2C_8, I2C_9, I2C_10,
  388. };
  389. /* Multi I2C busses handling */
  390. #ifdef CONFIG_SOFT_I2C_MULTI_BUS
  391. extern int get_multi_scl_pin(void);
  392. extern int get_multi_sda_pin(void);
  393. extern int multi_i2c_init(void);
  394. #endif
  395. /**
  396. * Get FDT values for i2c bus.
  397. *
  398. * @param blob Device tree blbo
  399. * @return the number of I2C bus
  400. */
  401. void board_i2c_init(const void *blob);
  402. /**
  403. * Find the I2C bus number by given a FDT I2C node.
  404. *
  405. * @param blob Device tree blbo
  406. * @param node FDT I2C node to find
  407. * @return the number of I2C bus (zero based), or -1 on error
  408. */
  409. int i2c_get_bus_num_fdt(int node);
  410. /**
  411. * Reset the I2C bus represented by the given a FDT I2C node.
  412. *
  413. * @param blob Device tree blbo
  414. * @param node FDT I2C node to find
  415. * @return 0 if port was reset, -1 if not found
  416. */
  417. int i2c_reset_port_fdt(const void *blob, int node);
  418. #endif /* _I2C_H_ */