i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Copyright (c) 2016 - 2021, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <drivers/delay_timer.h>
  8. #include <i2c.h>
  9. #include <i2c_regs.h>
  10. #include <lib/mmio.h>
  11. #include <platform_def.h>
  12. /* Max instances */
  13. #define MAX_I2C 2U
  14. /* Transaction error codes defined in Master command register (0x30) */
  15. #define MSTR_STS_XACT_SUCCESS 0U
  16. #define MSTR_STS_LOST_ARB 1U
  17. #define MSTR_STS_NACK_FIRST_BYTE 2U
  18. /* NACK on a byte other than the first byte */
  19. #define MSTR_STS_NACK_NON_FIRST_BYTE 3U
  20. #define MSTR_STS_TTIMEOUT_EXCEEDED 4U
  21. #define MSTR_STS_TX_TLOW_MEXT_EXCEEDED 5U
  22. #define MSTR_STS_RX_TLOW_MEXT_EXCEEDED 6U
  23. /* SMBUS protocol values defined in register 0x30 */
  24. #define SMBUS_PROT_QUICK_CMD 0U
  25. #define SMBUS_PROT_SEND_BYTE 1U
  26. #define SMBUS_PROT_RECV_BYTE 2U
  27. #define SMBUS_PROT_WR_BYTE 3U
  28. #define SMBUS_PROT_RD_BYTE 4U
  29. #define SMBUS_PROT_WR_WORD 5U
  30. #define SMBUS_PROT_RD_WORD 6U
  31. #define SMBUS_PROT_BLK_WR 7U
  32. #define SMBUS_PROT_BLK_RD 8U
  33. #define SMBUS_PROT_PROC_CALL 9U
  34. #define SMBUS_PROT_BLK_WR_BLK_RD_PROC_CALL 10U
  35. /* Number can be changed later */
  36. #define BUS_BUSY_COUNT 100000U
  37. #define IPROC_I2C_INVALID_ADDR 0xFFU
  38. #define I2C_SMBUS_BLOCK_MAX 32U
  39. /*
  40. * Enum to specify clock speed. The user will provide it during initialization.
  41. * If needed, it can be changed dynamically
  42. */
  43. typedef enum iproc_smb_clk_freq {
  44. IPROC_SMB_SPEED_100KHz = 0,
  45. IPROC_SMB_SPEED_400KHz = 1,
  46. IPROC_SMB_SPEED_INVALID = 255
  47. } smb_clk_freq_t;
  48. /* Structure used to pass information to read/write functions. */
  49. struct iproc_xact_info {
  50. /* Bus Identifier */
  51. uint32_t bus_id;
  52. /* Device Address */
  53. uint8_t devaddr;
  54. /* Passed by caller to send SMBus command cod e*/
  55. uint8_t command;
  56. /* actual data passed by the caller */
  57. uint8_t *data;
  58. /* Size of data buffer passed */
  59. uint32_t size;
  60. /* Sent by caller specifying PEC, 10-bit addresses */
  61. uint16_t flags;
  62. /* SMBus protocol to use to perform transaction */
  63. uint8_t smb_proto;
  64. /* true if command field below is valid. Otherwise, false */
  65. uint32_t cmd_valid;
  66. };
  67. static const uintptr_t smbus_base_reg_addr[MAX_I2C] = {
  68. SMBUS0_REGS_BASE,
  69. SMBUS1_REGS_BASE
  70. };
  71. /* Function to read a value from specified register. */
  72. static uint32_t iproc_i2c_reg_read(uint32_t bus_id, unsigned long reg_addr)
  73. {
  74. uint32_t val;
  75. uintptr_t smbus;
  76. smbus = smbus_base_reg_addr[bus_id];
  77. val = mmio_read_32(smbus + reg_addr);
  78. VERBOSE("i2c %u: reg %p read 0x%x\n", bus_id,
  79. (void *)(smbus + reg_addr), val);
  80. return val;
  81. }
  82. /* Function to write a value ('val') in to a specified register. */
  83. static void iproc_i2c_reg_write(uint32_t bus_id,
  84. unsigned long reg_addr,
  85. uint32_t val)
  86. {
  87. uintptr_t smbus;
  88. smbus = smbus_base_reg_addr[bus_id];
  89. mmio_write_32((smbus + reg_addr), val);
  90. VERBOSE("i2c %u: reg %p wrote 0x%x\n", bus_id,
  91. (void *)(smbus + reg_addr), val);
  92. }
  93. /* Function to clear and set bits in a specified register. */
  94. static void iproc_i2c_reg_clearset(uint32_t bus_id,
  95. unsigned long reg_addr,
  96. uint32_t clear,
  97. uint32_t set)
  98. {
  99. uintptr_t smbus;
  100. smbus = smbus_base_reg_addr[bus_id];
  101. mmio_clrsetbits_32((smbus + reg_addr), clear, set);
  102. VERBOSE("i2c %u: reg %p clear 0x%x, set 0x%x\n", bus_id,
  103. (void *)(smbus + reg_addr), clear, set);
  104. }
  105. /* Function to dump all SMBUS register */
  106. #ifdef BCM_I2C_DEBUG
  107. static int iproc_dump_i2c_regs(uint32_t bus_id)
  108. {
  109. uint32_t regval;
  110. if (bus_id > MAX_I2C) {
  111. return -1;
  112. }
  113. INFO("----------------------------------------------\n");
  114. INFO("%s: Dumping SMBus %u registers...\n", __func__, bus_id);
  115. regval = iproc_i2c_reg_read(bus_id, SMB_CFG_REG);
  116. INFO("SMB_CFG_REG=0x%x\n", regval);
  117. regval = iproc_i2c_reg_read(bus_id, SMB_TIMGCFG_REG);
  118. INFO("SMB_TIMGCFG_REG=0x%x\n", regval);
  119. regval = iproc_i2c_reg_read(bus_id, SMB_ADDR_REG);
  120. INFO("SMB_ADDR_REG=0x%x\n", regval);
  121. regval = iproc_i2c_reg_read(bus_id, SMB_MSTRFIFOCTL_REG);
  122. INFO("SMB_MSTRFIFOCTL_REG=0x%x\n", regval);
  123. regval = iproc_i2c_reg_read(bus_id, SMB_SLVFIFOCTL_REG);
  124. INFO("SMB_SLVFIFOCTL_REG=0x%x\n", regval);
  125. regval = iproc_i2c_reg_read(bus_id, SMB_BITBANGCTL_REG);
  126. INFO("SMB_BITBANGCTL_REG=0x%x\n", regval);
  127. regval = iproc_i2c_reg_read(bus_id, SMB_MSTRCMD_REG);
  128. INFO("SMB_MSTRCMD_REG=0x%x\n", regval);
  129. regval = iproc_i2c_reg_read(bus_id, SMB_SLVCMD_REG);
  130. INFO("SMB_SLVCMD_REG=0x%x\n", regval);
  131. regval = iproc_i2c_reg_read(bus_id, SMB_EVTEN_REG);
  132. INFO("SMB_EVTEN_REG=0x%x\n", regval);
  133. regval = iproc_i2c_reg_read(bus_id, SMB_EVTSTS_REG);
  134. INFO("SMB_EVTSTS_REG=0x%x\n", regval);
  135. regval = iproc_i2c_reg_read(bus_id, SMB_MSTRDATAWR_REG);
  136. INFO("SMB_MSTRDATAWR_REG=0x%x\n", regval);
  137. regval = iproc_i2c_reg_read(bus_id, SMB_MSTRDATARD_REG);
  138. INFO("SMB_MSTRDATARD_REG=0x%x\n", regval);
  139. regval = iproc_i2c_reg_read(bus_id, SMB_SLVDATAWR_REG);
  140. INFO("SMB_SLVDATAWR_REG=0x%x\n", regval);
  141. regval = iproc_i2c_reg_read(bus_id, SMB_SLVDATARD_REG);
  142. INFO("SMB_SLVDATARD_REG=0x%x\n", regval);
  143. INFO("----------------------------------------------\n");
  144. return 0;
  145. }
  146. #endif
  147. /*
  148. * Function to ensure that the previous transaction was completed before
  149. * initiating a new transaction. It can also be used in polling mode to
  150. * check status of completion of a command
  151. */
  152. static int iproc_i2c_startbusy_wait(uint32_t bus_id)
  153. {
  154. uint32_t regval;
  155. uint32_t retry = 0U;
  156. /*
  157. * Check if an operation is in progress. During probe it won't be.
  158. * Want to make sure that the transaction in progress is completed.
  159. */
  160. do {
  161. udelay(1U);
  162. regval = iproc_i2c_reg_read(bus_id, SMB_MSTRCMD_REG);
  163. regval &= SMB_MSTRSTARTBUSYCMD_MASK;
  164. if (retry++ > BUS_BUSY_COUNT) {
  165. ERROR("%s: START_BUSY bit didn't clear, exiting\n",
  166. __func__);
  167. return -1;
  168. }
  169. } while (regval != 0U);
  170. return 0;
  171. }
  172. /*
  173. * This function copies data to SMBus's Tx FIFO. Valid for write transactions
  174. * info: Data to copy in to Tx FIFO. For read commands, the size should be
  175. * set to zero by the caller
  176. */
  177. static void iproc_i2c_write_trans_data(struct iproc_xact_info *info)
  178. {
  179. uint32_t regval;
  180. uint8_t devaddr;
  181. uint32_t i;
  182. uint32_t num_data_bytes = 0U;
  183. #ifdef BCM_I2C_DEBUG
  184. INFO("%s:dev_addr=0x%x,cmd_valid=%d, cmd=0x%x, size=%u proto=%d\n",
  185. __func__, info->devaddr, info->cmd_valid, info->command,
  186. info->size, info->smb_proto);
  187. #endif
  188. /* Shift devaddr by 1 bit since SMBus uses the low bit[0] for R/W_n */
  189. devaddr = (info->devaddr << 1);
  190. /*
  191. * Depending on the SMBus protocol, we need to write additional
  192. * transaction data in to Tx FIFO. Refer to section 5.5 of SMBus spec
  193. * for sequence for a transaction
  194. */
  195. switch (info->smb_proto) {
  196. case SMBUS_PROT_RECV_BYTE:
  197. /* No additional data to be written */
  198. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  199. devaddr | 0x1U | SMB_MSTRWRSTS_MASK);
  200. break;
  201. case SMBUS_PROT_SEND_BYTE:
  202. num_data_bytes = info->size;
  203. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  204. devaddr);
  205. break;
  206. case SMBUS_PROT_RD_BYTE:
  207. case SMBUS_PROT_RD_WORD:
  208. case SMBUS_PROT_BLK_RD:
  209. /* Write slave address with R/W~ set (bit #0) */
  210. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  211. devaddr | 0x1U);
  212. break;
  213. case SMBUS_PROT_BLK_WR_BLK_RD_PROC_CALL:
  214. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  215. devaddr | 0x1U | SMB_MSTRWRSTS_MASK);
  216. break;
  217. case SMBUS_PROT_WR_BYTE:
  218. case SMBUS_PROT_WR_WORD:
  219. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  220. devaddr);
  221. /*
  222. * No additional bytes to be written. Data portion is written
  223. * in the 'for' loop below
  224. */
  225. num_data_bytes = info->size;
  226. break;
  227. case SMBUS_PROT_BLK_WR:
  228. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  229. devaddr);
  230. /* 3rd byte is byte count */
  231. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  232. info->size);
  233. num_data_bytes = info->size;
  234. break;
  235. default:
  236. return;
  237. }
  238. /* If the protocol needs command code, copy it */
  239. if (info->cmd_valid) {
  240. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  241. info->command);
  242. }
  243. /*
  244. * Copy actual data from caller. In general, for reads,
  245. * no data is copied.
  246. */
  247. for (i = 0U; num_data_bytes; --num_data_bytes, i++) {
  248. /* For the last byte, set MASTER_WR_STATUS bit */
  249. regval = (num_data_bytes == 1U) ?
  250. info->data[i] | SMB_MSTRWRSTS_MASK : info->data[i];
  251. iproc_i2c_reg_write(info->bus_id, SMB_MSTRDATAWR_REG,
  252. regval);
  253. }
  254. }
  255. /*
  256. * This function writes to the master command register and
  257. * then polls for completion
  258. */
  259. static int iproc_i2c_write_master_command(uint32_t mastercmd,
  260. struct iproc_xact_info *info)
  261. {
  262. uint32_t retry = 0U;
  263. uint32_t regval;
  264. iproc_i2c_reg_write(info->bus_id, SMB_MSTRCMD_REG, mastercmd);
  265. /* Check for Master Busy status */
  266. regval = iproc_i2c_reg_read(info->bus_id, SMB_MSTRCMD_REG);
  267. while ((regval & SMB_MSTRSTARTBUSYCMD_MASK) != 0U) {
  268. udelay(1U);
  269. if (retry++ > BUS_BUSY_COUNT) {
  270. ERROR("%s: START_BUSY bit didn't clear, exiting\n",
  271. __func__);
  272. return -1;
  273. }
  274. regval = iproc_i2c_reg_read(info->bus_id, SMB_MSTRCMD_REG);
  275. }
  276. /* If start_busy bit cleared, check if there are any errors */
  277. if (!(regval & SMB_MSTRSTARTBUSYCMD_MASK)) {
  278. /* start_busy bit cleared, check master_status field now */
  279. regval &= SMB_MSTRSTS_MASK;
  280. regval >>= SMB_MSTRSTS_SHIFT;
  281. if (regval != MSTR_STS_XACT_SUCCESS) {
  282. /* Error We can flush Tx FIFO here */
  283. ERROR("%s: ERROR: %u exiting\n", __func__, regval);
  284. return -1;
  285. }
  286. }
  287. return 0;
  288. }
  289. /* Function to initiate data send and verify completion status */
  290. static int iproc_i2c_data_send(struct iproc_xact_info *info)
  291. {
  292. int rc;
  293. uint32_t mastercmd;
  294. /* Make sure the previous transaction completed */
  295. rc = iproc_i2c_startbusy_wait(info->bus_id);
  296. if (rc < 0) {
  297. WARN("%s: Send: bus is busy, exiting\n", __func__);
  298. return rc;
  299. }
  300. /* Write transaction bytes to Tx FIFO */
  301. iproc_i2c_write_trans_data(info);
  302. /*
  303. * Program master command register (0x30) with protocol type and set
  304. * start_busy_command bit to initiate the write transaction
  305. */
  306. mastercmd = (info->smb_proto << SMB_MSTRSMBUSPROTO_SHIFT) |
  307. SMB_MSTRSTARTBUSYCMD_MASK;
  308. if (iproc_i2c_write_master_command(mastercmd, info)) {
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. /*
  314. * Function to initiate data receive, verify completion status,
  315. * and read from SMBUS Read FIFO
  316. */
  317. static int iproc_i2c_data_recv(struct iproc_xact_info *info,
  318. uint32_t *num_bytes_read)
  319. {
  320. int rc;
  321. uint32_t mastercmd;
  322. uint32_t regval;
  323. /* Make sure the previous transaction completed */
  324. rc = iproc_i2c_startbusy_wait(info->bus_id);
  325. if (rc < 0) {
  326. WARN("%s: Receive: Bus is busy, exiting\n", __func__);
  327. return rc;
  328. }
  329. /* Program all transaction bytes into master Tx FIFO */
  330. iproc_i2c_write_trans_data(info);
  331. /*
  332. * Program master command register (0x30) with protocol type and set
  333. * start_busy_command bit to initiate the write transaction
  334. */
  335. mastercmd = (info->smb_proto << SMB_MSTRSMBUSPROTO_SHIFT) |
  336. SMB_MSTRSTARTBUSYCMD_MASK | info->size;
  337. if (iproc_i2c_write_master_command(mastercmd, info)) {
  338. return -1;
  339. }
  340. /* Read received byte(s), after TX out address etc */
  341. regval = iproc_i2c_reg_read(info->bus_id, SMB_MSTRDATARD_REG);
  342. /* For block read, protocol (hw) returns byte count,as the first byte */
  343. if (info->smb_proto == SMBUS_PROT_BLK_RD) {
  344. uint32_t i;
  345. *num_bytes_read = regval & SMB_MSTRRDDATA_MASK;
  346. /*
  347. * Limit to reading a max of 32 bytes only; just a safeguard.
  348. * If # bytes read is a number > 32, check transaction set up,
  349. * and contact hw engg.
  350. * Assumption: PEC is disabled
  351. */
  352. for (i = 0U; (i < *num_bytes_read) &&
  353. (i < I2C_SMBUS_BLOCK_MAX); i++) {
  354. /* Read Rx FIFO for data bytes */
  355. regval = iproc_i2c_reg_read(info->bus_id,
  356. SMB_MSTRDATARD_REG);
  357. info->data[i] = regval & SMB_MSTRRDDATA_MASK;
  358. }
  359. } else {
  360. /* 1 Byte data */
  361. *info->data = regval & SMB_MSTRRDDATA_MASK;
  362. *num_bytes_read = 1U;
  363. }
  364. return 0;
  365. }
  366. /*
  367. * This function set clock frequency for SMBus block. As per hardware
  368. * engineering, the clock frequency can be changed dynamically.
  369. */
  370. static int iproc_i2c_set_clk_freq(uint32_t bus_id, smb_clk_freq_t freq)
  371. {
  372. uint32_t val;
  373. switch (freq) {
  374. case IPROC_SMB_SPEED_100KHz:
  375. val = 0U;
  376. break;
  377. case IPROC_SMB_SPEED_400KHz:
  378. val = 1U;
  379. break;
  380. default:
  381. return -1;
  382. }
  383. iproc_i2c_reg_clearset(bus_id, SMB_TIMGCFG_REG,
  384. SMB_TIMGCFG_MODE400_MASK,
  385. val << SMB_TIMGCFG_MODE400_SHIFT);
  386. return 0;
  387. }
  388. /* Helper function to fill the iproc_xact_info structure */
  389. static void iproc_i2c_fill_info(struct iproc_xact_info *info, uint32_t bus_id,
  390. uint8_t devaddr, uint8_t cmd, uint8_t *value,
  391. uint8_t smb_proto, uint32_t cmd_valid)
  392. {
  393. info->bus_id = bus_id;
  394. info->devaddr = devaddr;
  395. info->command = (uint8_t)cmd;
  396. info->smb_proto = smb_proto;
  397. info->data = value;
  398. info->size = 1U;
  399. info->flags = 0U;
  400. info->cmd_valid = cmd_valid;
  401. }
  402. /* This function initializes the SMBUS */
  403. static void iproc_i2c_init(uint32_t bus_id, int speed)
  404. {
  405. uint32_t regval;
  406. #ifdef BCM_I2C_DEBUG
  407. INFO("%s: Enter Init\n", __func__);
  408. #endif
  409. /* Put controller in reset */
  410. regval = iproc_i2c_reg_read(bus_id, SMB_CFG_REG);
  411. regval |= BIT(SMB_CFG_RST_SHIFT);
  412. regval &= ~(BIT(SMB_CFG_SMBEN_SHIFT));
  413. iproc_i2c_reg_write(bus_id, SMB_CFG_REG, regval);
  414. /* Wait 100 usec per spec */
  415. udelay(100U);
  416. /* Bring controller out of reset */
  417. regval &= ~(BIT(SMB_CFG_RST_SHIFT));
  418. iproc_i2c_reg_write(bus_id, SMB_CFG_REG, regval);
  419. /*
  420. * Flush Tx, Rx FIFOs. Note we are setting the Rx FIFO threshold to 0.
  421. * May be OK since we are setting RX_EVENT and RX_FIFO_FULL interrupts
  422. */
  423. regval = SMB_MSTRRXFIFOFLSH_MASK | SMB_MSTRTXFIFOFLSH_MASK;
  424. iproc_i2c_reg_write(bus_id, SMB_MSTRFIFOCTL_REG, regval);
  425. /*
  426. * Enable SMbus block. Note, we are setting MASTER_RETRY_COUNT to zero
  427. * since there will be only one master
  428. */
  429. regval = iproc_i2c_reg_read(bus_id, SMB_CFG_REG);
  430. regval |= SMB_CFG_SMBEN_MASK;
  431. iproc_i2c_reg_write(bus_id, SMB_CFG_REG, regval);
  432. /* Wait a minimum of 50 Usec, as per SMB hw doc. But we wait longer */
  433. mdelay(10U);
  434. /* If error then set default speed */
  435. if (i2c_set_bus_speed(bus_id, speed)) {
  436. i2c_set_bus_speed(bus_id, I2C_SPEED_DEFAULT);
  437. }
  438. /* Disable intrs */
  439. regval = 0x0U;
  440. iproc_i2c_reg_write(bus_id, SMB_EVTEN_REG, regval);
  441. /* Clear intrs (W1TC) */
  442. regval = iproc_i2c_reg_read(bus_id, SMB_EVTSTS_REG);
  443. iproc_i2c_reg_write(bus_id, SMB_EVTSTS_REG, regval);
  444. #ifdef BCM_I2C_DEBUG
  445. iproc_dump_i2c_regs(bus_id);
  446. INFO("%s: Exit Init Successfully\n", __func__);
  447. #endif
  448. }
  449. /*
  450. * Function Name: i2c_init
  451. *
  452. * Description:
  453. * This function initializes the SMBUS.
  454. *
  455. * Parameters:
  456. * bus_id - I2C bus ID
  457. * speed - I2C bus speed in Hz
  458. *
  459. * Return:
  460. * 0 on success, or -1 on failure.
  461. */
  462. int i2c_init(uint32_t bus_id, int speed)
  463. {
  464. if (bus_id > MAX_I2C) {
  465. WARN("%s: Invalid Bus %u\n", __func__, bus_id);
  466. return -1;
  467. }
  468. iproc_i2c_init(bus_id, speed);
  469. return 0U;
  470. }
  471. /*
  472. * Function Name: i2c_probe
  473. *
  474. * Description:
  475. * This function probes the I2C bus for the existence of the specified
  476. * device.
  477. *
  478. * Parameters:
  479. * bus_id - I2C bus ID
  480. * devaddr - Device Address
  481. *
  482. * Return:
  483. * 0 on success, or -1 on failure.
  484. */
  485. int i2c_probe(uint32_t bus_id, uint8_t devaddr)
  486. {
  487. uint32_t regval;
  488. int rc;
  489. /*
  490. * i2c_init() Initializes internal regs, disable intrs (and then clear intrs),
  491. * set fifo thresholds, etc.
  492. * Shift devaddr by 1 bit since SMBus uses the low bit[0] for R/W_n
  493. */
  494. regval = (devaddr << 1U);
  495. iproc_i2c_reg_write(bus_id, SMB_MSTRDATAWR_REG, regval);
  496. regval = ((SMBUS_PROT_QUICK_CMD << SMB_MSTRSMBUSPROTO_SHIFT) |
  497. SMB_MSTRSTARTBUSYCMD_MASK);
  498. iproc_i2c_reg_write(bus_id, SMB_MSTRCMD_REG, regval);
  499. rc = iproc_i2c_startbusy_wait(bus_id);
  500. if (rc < 0) {
  501. WARN("%s: Probe: bus is busy, exiting\n", __func__);
  502. return rc;
  503. }
  504. regval = iproc_i2c_reg_read(bus_id, SMB_MSTRCMD_REG);
  505. if (((regval & SMB_MSTRSTS_MASK) >> SMB_MSTRSTS_SHIFT) == 0)
  506. VERBOSE("i2c device address: 0x%x\n", devaddr);
  507. else
  508. return -1;
  509. #ifdef BCM_I2C_DEBUG
  510. iproc_dump_i2c_regs(bus_id);
  511. #endif
  512. return 0;
  513. }
  514. /*
  515. * Function Name: i2c_recv_byte
  516. *
  517. * Description:
  518. * This function reads I2C data from a device without specifying
  519. * a command register.
  520. *
  521. * Parameters:
  522. * bus_id - I2C bus ID
  523. * devaddr - Device Address
  524. * value - Data Read
  525. *
  526. * Return:
  527. * 0 on success, or -1 on failure.
  528. */
  529. int i2c_recv_byte(uint32_t bus_id, uint8_t devaddr, uint8_t *value)
  530. {
  531. int rc;
  532. struct iproc_xact_info info;
  533. uint32_t num_bytes_read = 0;
  534. iproc_i2c_fill_info(&info, bus_id, devaddr, 0U, value,
  535. SMBUS_PROT_RECV_BYTE, 0U);
  536. /* Refer to i2c_smbus_read_byte for params passed. */
  537. rc = iproc_i2c_data_recv(&info, &num_bytes_read);
  538. if (rc < 0) {
  539. printf("%s: %s error accessing device 0x%x\n",
  540. __func__, "Read", devaddr);
  541. }
  542. return rc;
  543. }
  544. /*
  545. * Function Name: i2c_send_byte
  546. *
  547. * Description:
  548. * This function send I2C data to a device without specifying
  549. * a command register.
  550. *
  551. * Parameters:
  552. * bus_id - I2C bus ID
  553. * devaddr - Device Address
  554. * value - Data Send
  555. *
  556. * Return:
  557. * 0 on success, or -1 on failure.
  558. */
  559. int i2c_send_byte(uint32_t bus_id, uint8_t devaddr, uint8_t value)
  560. {
  561. int rc;
  562. struct iproc_xact_info info;
  563. iproc_i2c_fill_info(&info, bus_id, devaddr, 0U, &value,
  564. SMBUS_PROT_SEND_BYTE, 0U);
  565. /* Refer to i2c_smbus_write_byte params passed. */
  566. rc = iproc_i2c_data_send(&info);
  567. if (rc < 0) {
  568. ERROR("%s: %s error accessing device 0x%x\n",
  569. __func__, "Write", devaddr);
  570. }
  571. return rc;
  572. }
  573. /* Helper function to read a single byte */
  574. static int i2c_read_byte(uint32_t bus_id,
  575. uint8_t devaddr,
  576. uint8_t regoffset,
  577. uint8_t *value)
  578. {
  579. int rc;
  580. struct iproc_xact_info info;
  581. uint32_t num_bytes_read = 0U;
  582. iproc_i2c_fill_info(&info, bus_id, devaddr, regoffset, value,
  583. SMBUS_PROT_RD_BYTE, 1U);
  584. /* Refer to i2c_smbus_read_byte for params passed. */
  585. rc = iproc_i2c_data_recv(&info, &num_bytes_read);
  586. if (rc < 0) {
  587. ERROR("%s: %s error accessing device 0x%x\n",
  588. __func__, "Read", devaddr);
  589. }
  590. return rc;
  591. }
  592. /*
  593. * Function Name: i2c_read
  594. *
  595. * Description:
  596. * This function reads I2C data from a device with a designated
  597. * command register
  598. *
  599. * Parameters:
  600. * bus_id - I2C bus ID
  601. * devaddr - Device Address
  602. * addr - Register Offset
  603. * alen - Address Length, 1 for byte, 2 for word (not supported)
  604. * buffer - Data Buffer
  605. * len - Data Length in bytes
  606. *
  607. * Return:
  608. * 0 on success, or -1 on failure.
  609. */
  610. int i2c_read(uint32_t bus_id,
  611. uint8_t devaddr,
  612. uint32_t addr,
  613. int alen,
  614. uint8_t *buffer,
  615. int len)
  616. {
  617. uint32_t i;
  618. if (alen > 1) {
  619. WARN("I2C read: addr len %d not supported\n", alen);
  620. return -1;
  621. }
  622. if (addr + len > 256) {
  623. WARN("I2C read: address out of range\n");
  624. return -1;
  625. }
  626. for (i = 0U; i < len; i++) {
  627. if (i2c_read_byte(bus_id, devaddr, addr + i, &buffer[i])) {
  628. ERROR("I2C read: I/O error\n");
  629. iproc_i2c_init(bus_id, i2c_get_bus_speed(bus_id));
  630. return -1;
  631. }
  632. }
  633. return 0;
  634. }
  635. /* Helper function to write a single byte */
  636. static int i2c_write_byte(uint32_t bus_id,
  637. uint8_t devaddr,
  638. uint8_t regoffset,
  639. uint8_t value)
  640. {
  641. int rc;
  642. struct iproc_xact_info info;
  643. iproc_i2c_fill_info(&info, bus_id, devaddr, regoffset, &value,
  644. SMBUS_PROT_WR_BYTE, 1U);
  645. /* Refer to i2c_smbus_write_byte params passed. */
  646. rc = iproc_i2c_data_send(&info);
  647. if (rc < 0) {
  648. ERROR("%s: %s error accessing device 0x%x\n",
  649. __func__, "Write", devaddr);
  650. return -1;
  651. }
  652. return 0;
  653. }
  654. /*
  655. * Function Name: i2c_write
  656. *
  657. * Description:
  658. * This function write I2C data to a device with a designated
  659. * command register
  660. *
  661. * Parameters:
  662. * bus_id - I2C bus ID
  663. * devaddr - Device Address
  664. * addr - Register Offset
  665. * alen - Address Length, 1 for byte, 2 for word (not supported)
  666. * buffer - Data Buffer
  667. * len - Data Length in bytes
  668. *
  669. * Return:
  670. * 0 on success, or -1 on failure.
  671. */
  672. int i2c_write(uint32_t bus_id,
  673. uint8_t devaddr,
  674. uint32_t addr,
  675. int alen,
  676. uint8_t *buffer,
  677. int len)
  678. {
  679. uint32_t i;
  680. if (alen > 1) {
  681. WARN("I2C write: addr len %d not supported\n", alen);
  682. return -1;
  683. }
  684. if (addr + len > 256U) {
  685. WARN("I2C write: address out of range\n");
  686. return -1;
  687. }
  688. for (i = 0U; i < len; i++) {
  689. if (i2c_write_byte(bus_id, devaddr, addr + i, buffer[i])) {
  690. ERROR("I2C write: I/O error\n");
  691. iproc_i2c_init(bus_id, i2c_get_bus_speed(bus_id));
  692. return -1;
  693. }
  694. }
  695. return 0;
  696. }
  697. /*
  698. * Function Name: i2c_set_bus_speed
  699. *
  700. * Description:
  701. * This function configures the SMBUS speed
  702. *
  703. * Parameters:
  704. * bus_id - I2C bus ID
  705. * speed - I2C bus speed in Hz
  706. *
  707. * Return:
  708. * 0 on success, or -1 on failure.
  709. */
  710. int i2c_set_bus_speed(uint32_t bus_id, uint32_t speed)
  711. {
  712. switch (speed) {
  713. case I2C_SPEED_100KHz:
  714. iproc_i2c_set_clk_freq(bus_id, IPROC_SMB_SPEED_100KHz);
  715. break;
  716. case I2C_SPEED_400KHz:
  717. iproc_i2c_set_clk_freq(bus_id, IPROC_SMB_SPEED_400KHz);
  718. break;
  719. default:
  720. return -1;
  721. }
  722. return 0;
  723. }
  724. /*
  725. * Function Name: i2c_get_bus_speed
  726. *
  727. * Description:
  728. * This function returns the SMBUS speed.
  729. *
  730. * Parameters:
  731. * bus_id - I2C bus ID
  732. *
  733. * Return:
  734. * Bus speed in Hz, 0 on failure
  735. */
  736. uint32_t i2c_get_bus_speed(uint32_t bus_id)
  737. {
  738. uint32_t regval;
  739. uint32_t retval = 0U;
  740. regval = iproc_i2c_reg_read(bus_id, SMB_TIMGCFG_REG);
  741. regval &= SMB_TIMGCFG_MODE400_MASK;
  742. regval >>= SMB_TIMGCFG_MODE400_SHIFT;
  743. switch (regval) {
  744. case IPROC_SMB_SPEED_100KHz:
  745. retval = I2C_SPEED_100KHz;
  746. break;
  747. case IPROC_SMB_SPEED_400KHz:
  748. retval = I2C_SPEED_400KHz;
  749. break;
  750. default:
  751. break;
  752. }
  753. return retval;
  754. }