i2c.h 11 KB

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