pf0100.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014-2016, Toradex AG
  4. */
  5. /*
  6. * Helpers for Freescale PMIC PF0100
  7. */
  8. #include <common.h>
  9. #include <i2c.h>
  10. #include <asm/arch/imx-regs.h>
  11. #include <asm/arch/iomux.h>
  12. #include <asm/arch/mx6-pins.h>
  13. #include <asm/gpio.h>
  14. #include <asm/mach-imx/iomux-v3.h>
  15. #include "pf0100_otp.inc"
  16. #include "pf0100.h"
  17. /* define for PMIC register dump */
  18. /*#define DEBUG */
  19. /* use GPIO: EXT_IO1 to switch on VPGM, ON: 1 */
  20. static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
  21. MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
  22. # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
  23. };
  24. unsigned pmic_init(void)
  25. {
  26. unsigned programmed = 0;
  27. uchar bus = 1;
  28. uchar devid, revid, val;
  29. puts("PMIC: ");
  30. if (!((0 == i2c_set_bus_num(bus)) &&
  31. (0 == i2c_probe(PFUZE100_I2C_ADDR)))) {
  32. puts("i2c bus failed\n");
  33. return 0;
  34. }
  35. /* get device ident */
  36. if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_DEVICEID, 1, &devid, 1) < 0) {
  37. puts("i2c pmic devid read failed\n");
  38. return 0;
  39. }
  40. if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_REVID, 1, &revid, 1) < 0) {
  41. puts("i2c pmic revid read failed\n");
  42. return 0;
  43. }
  44. printf("device id: 0x%.2x, revision id: 0x%.2x\n", devid, revid);
  45. #ifdef DEBUG
  46. {
  47. unsigned i, j;
  48. for (i = 0; i < 16; i++)
  49. printf("\t%x", i);
  50. for (j = 0; j < 0x80; ) {
  51. printf("\n%2x", j);
  52. for (i = 0; i < 16; i++) {
  53. i2c_read(PFUZE100_I2C_ADDR, j+i, 1, &val, 1);
  54. printf("\t%2x", val);
  55. }
  56. j += 0x10;
  57. }
  58. printf("\nEXT Page 1");
  59. val = PFUZE100_PAGE_REGISTER_PAGE1;
  60. if (i2c_write(PFUZE100_I2C_ADDR, PFUZE100_PAGE_REGISTER, 1,
  61. &val, 1)) {
  62. puts("i2c write failed\n");
  63. return 0;
  64. }
  65. for (j = 0x80; j < 0x100; ) {
  66. printf("\n%2x", j);
  67. for (i = 0; i < 16; i++) {
  68. i2c_read(PFUZE100_I2C_ADDR, j+i, 1, &val, 1);
  69. printf("\t%2x", val);
  70. }
  71. j += 0x10;
  72. }
  73. printf("\nEXT Page 2");
  74. val = PFUZE100_PAGE_REGISTER_PAGE2;
  75. if (i2c_write(PFUZE100_I2C_ADDR, PFUZE100_PAGE_REGISTER, 1,
  76. &val, 1)) {
  77. puts("i2c write failed\n");
  78. return 0;
  79. }
  80. for (j = 0x80; j < 0x100; ) {
  81. printf("\n%2x", j);
  82. for (i = 0; i < 16; i++) {
  83. i2c_read(PFUZE100_I2C_ADDR, j+i, 1, &val, 1);
  84. printf("\t%2x", val);
  85. }
  86. j += 0x10;
  87. }
  88. printf("\n");
  89. }
  90. #endif
  91. /* get device programmed state */
  92. val = PFUZE100_PAGE_REGISTER_PAGE1;
  93. if (i2c_write(PFUZE100_I2C_ADDR, PFUZE100_PAGE_REGISTER, 1, &val, 1)) {
  94. puts("i2c write failed\n");
  95. return 0;
  96. }
  97. if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_FUSE_POR1, 1, &val, 1) < 0) {
  98. puts("i2c fuse_por read failed\n");
  99. return 0;
  100. }
  101. if (val & PFUZE100_FUSE_POR_M)
  102. programmed++;
  103. if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_FUSE_POR2, 1, &val, 1) < 0) {
  104. puts("i2c fuse_por read failed\n");
  105. return programmed;
  106. }
  107. if (val & PFUZE100_FUSE_POR_M)
  108. programmed++;
  109. if (i2c_read(PFUZE100_I2C_ADDR, PFUZE100_FUSE_POR3, 1, &val, 1) < 0) {
  110. puts("i2c fuse_por read failed\n");
  111. return programmed;
  112. }
  113. if (val & PFUZE100_FUSE_POR_M)
  114. programmed++;
  115. switch (programmed) {
  116. case 0:
  117. printf("PMIC: not programmed\n");
  118. break;
  119. case 3:
  120. printf("PMIC: programmed\n");
  121. break;
  122. default:
  123. printf("PMIC: undefined programming state\n");
  124. break;
  125. }
  126. return programmed;
  127. }
  128. #ifndef CONFIG_SPL_BUILD
  129. static int pf0100_prog(void)
  130. {
  131. unsigned char bus = 1;
  132. unsigned char val;
  133. unsigned int i;
  134. if (pmic_init() == 3) {
  135. puts("PMIC already programmed, exiting\n");
  136. return CMD_RET_FAILURE;
  137. }
  138. /* set up gpio to manipulate vprog, initially off */
  139. imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
  140. ARRAY_SIZE(pmic_prog_pads));
  141. gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
  142. if (!((0 == i2c_set_bus_num(bus)) &&
  143. (0 == i2c_probe(PFUZE100_I2C_ADDR)))) {
  144. puts("i2c bus failed\n");
  145. return CMD_RET_FAILURE;
  146. }
  147. for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
  148. switch (pmic_otp_prog[i].cmd) {
  149. case pmic_i2c:
  150. val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
  151. if (i2c_write(PFUZE100_I2C_ADDR, pmic_otp_prog[i].reg,
  152. 1, &val, 1)) {
  153. printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
  154. pmic_otp_prog[i].reg, val);
  155. return CMD_RET_FAILURE;
  156. }
  157. break;
  158. case pmic_delay:
  159. udelay(pmic_otp_prog[i].value * 1000);
  160. break;
  161. case pmic_vpgm:
  162. gpio_direction_output(PMIC_PROG_VOLTAGE,
  163. pmic_otp_prog[i].value);
  164. break;
  165. case pmic_pwr:
  166. /* TODO */
  167. break;
  168. }
  169. }
  170. return CMD_RET_SUCCESS;
  171. }
  172. static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc,
  173. char * const argv[])
  174. {
  175. int ret;
  176. puts("Programming PMIC OTP...");
  177. ret = pf0100_prog();
  178. if (ret == CMD_RET_SUCCESS)
  179. puts("done.\n");
  180. else
  181. puts("failed.\n");
  182. return ret;
  183. }
  184. U_BOOT_CMD(
  185. pf0100_otp_prog, 1, 0, do_pf0100_prog,
  186. "Program the OTP fuses on the PMIC PF0100",
  187. ""
  188. );
  189. #endif