blob: 0eed3459734b0a0711eb2f17f975b871447df80b [file] [log] [blame]
Patrick Delaunay0c4656b2018-05-17 15:24:06 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
6#include <common.h>
7#include <command.h>
Patrick Delaunayf81ddc12019-06-21 15:26:50 +02008#include <fuse.h>
Patrick Delaunay0c4656b2018-05-17 15:24:06 +02009#include <misc.h>
10#include <errno.h>
11#include <dm/device.h>
12#include <dm/uclass.h>
Patrick Delaunayd592d132019-02-04 11:26:22 +010013#include <power/stpmic1.h>
Patrick Delaunay0c4656b2018-05-17 15:24:06 +020014
15#define STM32MP_OTP_BANK 0
Patrick Delaunayd592d132019-02-04 11:26:22 +010016#define STM32MP_NVM_BANK 1
Patrick Delaunay0c4656b2018-05-17 15:24:06 +020017
18/*
19 * The 'fuse' command API
20 */
21int fuse_read(u32 bank, u32 word, u32 *val)
22{
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +020023 int ret;
Patrick Delaunay0c4656b2018-05-17 15:24:06 +020024 struct udevice *dev;
25
26 switch (bank) {
27 case STM32MP_OTP_BANK:
28 ret = uclass_get_device_by_driver(UCLASS_MISC,
29 DM_GET_DRIVER(stm32mp_bsec),
30 &dev);
31 if (ret)
32 return ret;
33 ret = misc_read(dev, word * 4 + STM32_BSEC_SHADOW_OFFSET,
34 val, 4);
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +020035 if (ret != 4)
36 ret = -EINVAL;
37 else
38 ret = 0;
Patrick Delaunay0c4656b2018-05-17 15:24:06 +020039 break;
40
Patrick Delaunayd592d132019-02-04 11:26:22 +010041#ifdef CONFIG_PMIC_STPMIC1
42 case STM32MP_NVM_BANK:
Patrick Delaunayc9a5d392019-08-02 13:08:03 +020043 ret = uclass_get_device_by_driver(UCLASS_MISC,
44 DM_GET_DRIVER(stpmic1_nvm),
45 &dev);
46 if (ret)
47 return ret;
Patrick Delaunayd592d132019-02-04 11:26:22 +010048 *val = 0;
Patrick Delaunayc9a5d392019-08-02 13:08:03 +020049 ret = misc_read(dev, -word, val, 1);
50 if (ret != 1)
51 ret = -EINVAL;
52 else
53 ret = 0;
Patrick Delaunayd592d132019-02-04 11:26:22 +010054 break;
55#endif /* CONFIG_PMIC_STPMIC1 */
56
Patrick Delaunay0c4656b2018-05-17 15:24:06 +020057 default:
58 printf("stm32mp %s: wrong value for bank %i\n", __func__, bank);
59 ret = -EINVAL;
60 break;
61 }
62
63 return ret;
64}
65
66int fuse_prog(u32 bank, u32 word, u32 val)
67{
68 struct udevice *dev;
69 int ret;
70
71 switch (bank) {
72 case STM32MP_OTP_BANK:
73 ret = uclass_get_device_by_driver(UCLASS_MISC,
74 DM_GET_DRIVER(stm32mp_bsec),
75 &dev);
76 if (ret)
77 return ret;
78 ret = misc_write(dev, word * 4 + STM32_BSEC_OTP_OFFSET,
79 &val, 4);
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +020080 if (ret != 4)
81 ret = -EINVAL;
82 else
83 ret = 0;
Patrick Delaunay0c4656b2018-05-17 15:24:06 +020084 break;
Patrick Delaunayd592d132019-02-04 11:26:22 +010085
86#ifdef CONFIG_PMIC_STPMIC1
87 case STM32MP_NVM_BANK:
Patrick Delaunayc9a5d392019-08-02 13:08:03 +020088 ret = uclass_get_device_by_driver(UCLASS_MISC,
89 DM_GET_DRIVER(stpmic1_nvm),
90 &dev);
91 if (ret)
92 return ret;
93 ret = misc_write(dev, word, &val, 1);
94 if (ret != 1)
95 ret = -EINVAL;
96 else
97 ret = 0;
Patrick Delaunayd592d132019-02-04 11:26:22 +010098 break;
99#endif /* CONFIG_PMIC_STPMIC1 */
Patrick Delaunay0c4656b2018-05-17 15:24:06 +0200100
101 default:
102 printf("stm32mp %s: wrong value for bank %i\n", __func__, bank);
103 ret = -EINVAL;
104 break;
105 }
106
107 return ret;
108}
109
110int fuse_sense(u32 bank, u32 word, u32 *val)
111{
112 struct udevice *dev;
113 int ret;
114
115 switch (bank) {
116 case STM32MP_OTP_BANK:
117 ret = uclass_get_device_by_driver(UCLASS_MISC,
118 DM_GET_DRIVER(stm32mp_bsec),
119 &dev);
120 if (ret)
121 return ret;
122 ret = misc_read(dev, word * 4 + STM32_BSEC_OTP_OFFSET, val, 4);
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200123 if (ret != 4)
124 ret = -EINVAL;
125 else
126 ret = 0;
Patrick Delaunay0c4656b2018-05-17 15:24:06 +0200127 break;
128
Patrick Delaunayd592d132019-02-04 11:26:22 +0100129#ifdef CONFIG_PMIC_STPMIC1
130 case STM32MP_NVM_BANK:
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200131 ret = uclass_get_device_by_driver(UCLASS_MISC,
132 DM_GET_DRIVER(stpmic1_nvm),
133 &dev);
134 if (ret)
135 return ret;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100136 *val = 0;
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200137 ret = misc_read(dev, word, val, 1);
138 if (ret != 1)
139 ret = -EINVAL;
140 else
141 ret = 0;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100142 break;
143#endif /* CONFIG_PMIC_STPMIC1 */
144
Patrick Delaunay0c4656b2018-05-17 15:24:06 +0200145 default:
146 printf("stm32mp %s: wrong value for bank %i\n", __func__, bank);
147 ret = -EINVAL;
148 break;
149 }
150
151 return ret;
152}
153
154int fuse_override(u32 bank, u32 word, u32 val)
155{
156 struct udevice *dev;
157 int ret;
158
159 switch (bank) {
160 case STM32MP_OTP_BANK:
161 ret = uclass_get_device_by_driver(UCLASS_MISC,
162 DM_GET_DRIVER(stm32mp_bsec),
163 &dev);
164 if (ret)
165 return ret;
166 ret = misc_write(dev, word * 4 + STM32_BSEC_SHADOW_OFFSET,
167 &val, 4);
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200168 if (ret != 4)
169 ret = -EINVAL;
170 else
171 ret = 0;
Patrick Delaunay0c4656b2018-05-17 15:24:06 +0200172 break;
173
Patrick Delaunayd592d132019-02-04 11:26:22 +0100174#ifdef CONFIG_PMIC_STPMIC1
175 case STM32MP_NVM_BANK:
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200176 ret = uclass_get_device_by_driver(UCLASS_MISC,
177 DM_GET_DRIVER(stpmic1_nvm),
178 &dev);
179 if (ret)
180 return ret;
181 ret = misc_write(dev, -word, &val, 1);
182 if (ret != 1)
183 ret = -EINVAL;
184 else
185 ret = 0;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100186 break;
187#endif /* CONFIG_PMIC_STPMIC1 */
188
Patrick Delaunay0c4656b2018-05-17 15:24:06 +0200189 default:
190 printf("stm32mp %s: wrong value for bank %i\n",
191 __func__, bank);
192 ret = -EINVAL;
193 break;
194 }
195
196 return ret;
197}