pf0100.c 5.1 KB

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