pf0100.c 5.1 KB

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