169d5fd8fSJohnny Huang /* 269d5fd8fSJohnny Huang * This program is distributed in the hope that it will be useful, 369d5fd8fSJohnny Huang * but WITHOUT ANY WARRANTY; without even the implied warranty of 469d5fd8fSJohnny Huang * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 569d5fd8fSJohnny Huang * GNU General Public License for more details. 669d5fd8fSJohnny Huang * 769d5fd8fSJohnny Huang * You should have received a copy of the GNU General Public License 869d5fd8fSJohnny Huang * along with this program; if not, write to the Free Software 969d5fd8fSJohnny Huang * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1069d5fd8fSJohnny Huang */ 114c1c9b35SJohnny Huang #include <stdlib.h> 1269d5fd8fSJohnny Huang #include <common.h> 1369d5fd8fSJohnny Huang #include <console.h> 1469d5fd8fSJohnny Huang #include <bootretry.h> 1569d5fd8fSJohnny Huang #include <cli.h> 1669d5fd8fSJohnny Huang #include <command.h> 1769d5fd8fSJohnny Huang #include <console.h> 184c1c9b35SJohnny Huang #include <malloc.h> 1969d5fd8fSJohnny Huang #include <inttypes.h> 2069d5fd8fSJohnny Huang #include <mapmem.h> 2169d5fd8fSJohnny Huang #include <asm/io.h> 2269d5fd8fSJohnny Huang #include <linux/compiler.h> 23696656c6SJohnny Huang #include <u-boot/sha256.h> 240cee9a95SJohnny Huang #include "otp_info.h" 2569d5fd8fSJohnny Huang 2669d5fd8fSJohnny Huang DECLARE_GLOBAL_DATA_PTR; 2769d5fd8fSJohnny Huang 28*f67375f7SJohnny Huang #define OTP_VER "1.0.0" 29*f67375f7SJohnny Huang 3069d5fd8fSJohnny Huang #define OTP_PASSWD 0x349fe38a 3169d5fd8fSJohnny Huang #define RETRY 3 327332532cSJohnny Huang #define OTP_REGION_STRAP BIT(0) 337332532cSJohnny Huang #define OTP_REGION_CONF BIT(1) 347332532cSJohnny Huang #define OTP_REGION_DATA BIT(2) 3569d5fd8fSJohnny Huang 362a856b9aSJohnny Huang #define OTP_USAGE -1 372a856b9aSJohnny Huang #define OTP_FAILURE -2 382a856b9aSJohnny Huang #define OTP_SUCCESS 0 392a856b9aSJohnny Huang 40a6af4a17SJohnny Huang #define OTP_PROG_SKIP 1 41a6af4a17SJohnny Huang 429a4fe690SJohnny Huang #define OTP_KEY_TYPE_RSA 1 439a4fe690SJohnny Huang #define OTP_KEY_TYPE_AES 2 449a4fe690SJohnny Huang #define OTP_KEY_TYPE_VAULT 3 459a4fe690SJohnny Huang #define OTP_KEY_TYPE_HMAC 4 469a4fe690SJohnny Huang 474c1c9b35SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||" 484c1c9b35SJohnny Huang #define PBWIDTH 60 494c1c9b35SJohnny Huang 503d3688adSJohnny Huang #define OTP_BASE 0x1e6f2000 513d3688adSJohnny Huang #define OTP_PROTECT_KEY OTP_BASE 523d3688adSJohnny Huang #define OTP_COMMAND OTP_BASE + 0x4 533d3688adSJohnny Huang #define OTP_TIMING OTP_BASE + 0x8 543d3688adSJohnny Huang #define OTP_ADDR OTP_BASE + 0x10 553d3688adSJohnny Huang #define OTP_STATUS OTP_BASE + 0x14 563d3688adSJohnny Huang #define OTP_COMPARE_1 OTP_BASE + 0x20 573d3688adSJohnny Huang #define OTP_COMPARE_2 OTP_BASE + 0x24 583d3688adSJohnny Huang #define OTP_COMPARE_3 OTP_BASE + 0x28 593d3688adSJohnny Huang #define OTP_COMPARE_4 OTP_BASE + 0x2c 603d3688adSJohnny Huang 61696656c6SJohnny Huang #define OTP_MAGIC "SOCOTP" 62696656c6SJohnny Huang #define CHECKSUM_LEN 32 63696656c6SJohnny Huang #define OTP_INC_DATA (1 << 31) 64696656c6SJohnny Huang #define OTP_INC_CONFIG (1 << 30) 65696656c6SJohnny Huang #define OTP_INC_STRAP (1 << 29) 66696656c6SJohnny Huang #define OTP_ECC_EN (1 << 28) 67696656c6SJohnny Huang #define OTP_REGION_SIZE(info) ((info >> 16) & 0xffff) 68696656c6SJohnny Huang #define OTP_REGION_OFFSET(info) (info & 0xffff) 69696656c6SJohnny Huang #define OTP_IMAGE_SIZE(info) (info & 0xffff) 70696656c6SJohnny Huang 71696656c6SJohnny Huang #define OTP_AST2600A0 0 72696656c6SJohnny Huang #define OTP_AST2600A1 1 735fdde29fSJohnny Huang #define OTP_AST2600A2 7 // fpga 74696656c6SJohnny Huang 75696656c6SJohnny Huang struct otp_header { 76696656c6SJohnny Huang u8 otp_magic[8]; 77696656c6SJohnny Huang u8 otp_version[8]; 78696656c6SJohnny Huang u32 image_info; 79696656c6SJohnny Huang u32 data_info; 80696656c6SJohnny Huang u32 config_info; 81696656c6SJohnny Huang u32 strap_info; 82696656c6SJohnny Huang u32 checksum_offset; 83696656c6SJohnny Huang } __attribute__((packed)); 84696656c6SJohnny Huang 8566f2f8e5SJohnny Huang struct otpstrap_status { 8669d5fd8fSJohnny Huang int value; 8769d5fd8fSJohnny Huang int option_array[7]; 8869d5fd8fSJohnny Huang int remain_times; 8969d5fd8fSJohnny Huang int writeable_option; 905010032bSJohnny Huang int reg_protected; 9169d5fd8fSJohnny Huang int protected; 9269d5fd8fSJohnny Huang }; 9369d5fd8fSJohnny Huang 9466f2f8e5SJohnny Huang struct otpconf_parse { 9566f2f8e5SJohnny Huang int dw_offset; 9666f2f8e5SJohnny Huang int bit; 9766f2f8e5SJohnny Huang int length; 9866f2f8e5SJohnny Huang int value; 99696656c6SJohnny Huang int ignore; 10066f2f8e5SJohnny Huang char status[80]; 10166f2f8e5SJohnny Huang }; 10266f2f8e5SJohnny Huang 1039a4fe690SJohnny Huang struct otpkey_type { 1049a4fe690SJohnny Huang int value; 1059a4fe690SJohnny Huang int key_type; 1069a4fe690SJohnny Huang int need_id; 1079a4fe690SJohnny Huang char information[110]; 1089a4fe690SJohnny Huang }; 1099a4fe690SJohnny Huang 1109a4fe690SJohnny Huang struct otp_info_cb { 1119a4fe690SJohnny Huang int version; 11279e42a59SJoel Stanley const struct otpstrap_info *strap_info; 1139a4fe690SJohnny Huang int strap_info_len; 11479e42a59SJoel Stanley const struct otpconf_info *conf_info; 1159a4fe690SJohnny Huang int conf_info_len; 11679e42a59SJoel Stanley const struct otpkey_type *key_info; 1179a4fe690SJohnny Huang int key_info_len; 1185010032bSJohnny Huang 1199a4fe690SJohnny Huang }; 1209a4fe690SJohnny Huang 121696656c6SJohnny Huang struct otp_image_layout { 1225010032bSJohnny Huang int data_length; 1235010032bSJohnny Huang int conf_length; 1245010032bSJohnny Huang int strap_length; 125696656c6SJohnny Huang uint8_t *data; 126696656c6SJohnny Huang uint8_t *data_ignore; 127696656c6SJohnny Huang uint8_t *conf; 128696656c6SJohnny Huang uint8_t *conf_ignore; 129696656c6SJohnny Huang uint8_t *strap; 130696656c6SJohnny Huang uint8_t *strap_reg_pro; 131696656c6SJohnny Huang uint8_t *strap_pro; 132696656c6SJohnny Huang uint8_t *strap_ignore; 133696656c6SJohnny Huang }; 134696656c6SJohnny Huang 1359a4fe690SJohnny Huang static struct otp_info_cb info_cb; 1369a4fe690SJohnny Huang 13779e42a59SJoel Stanley static const struct otpkey_type a0_key_type[] = { 1389a4fe690SJohnny Huang {0, OTP_KEY_TYPE_AES, 0, "AES-256 as OEM platform key for image encryption/decryption"}, 1399a4fe690SJohnny Huang {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 1409a4fe690SJohnny Huang {4, OTP_KEY_TYPE_HMAC, 1, "HMAC as encrypted OEM HMAC keys in Mode 1"}, 1419a4fe690SJohnny Huang {8, OTP_KEY_TYPE_RSA, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 1429a4fe690SJohnny Huang {9, OTP_KEY_TYPE_RSA, 0, "RSA-public as SOC public key"}, 1439a4fe690SJohnny Huang {10, OTP_KEY_TYPE_RSA, 0, "RSA-public as AES key decryption key"}, 1449a4fe690SJohnny Huang {13, OTP_KEY_TYPE_RSA, 0, "RSA-private as SOC private key"}, 1459a4fe690SJohnny Huang {14, OTP_KEY_TYPE_RSA, 0, "RSA-private as AES key decryption key"}, 1469a4fe690SJohnny Huang }; 1479a4fe690SJohnny Huang 14879e42a59SJoel Stanley static const struct otpkey_type a1_key_type[] = { 1499a4fe690SJohnny Huang {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 1509a4fe690SJohnny Huang {2, OTP_KEY_TYPE_AES, 1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"}, 1519a4fe690SJohnny Huang {8, OTP_KEY_TYPE_RSA, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 1529a4fe690SJohnny Huang {10, OTP_KEY_TYPE_RSA, 0, "RSA-public as AES key decryption key"}, 1539a4fe690SJohnny Huang {14, OTP_KEY_TYPE_RSA, 0, "RSA-private as AES key decryption key"}, 1549a4fe690SJohnny Huang }; 1559a4fe690SJohnny Huang 1565fdde29fSJohnny Huang static const struct otpkey_type a2_key_type[] = { 1575fdde29fSJohnny Huang {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 1585fdde29fSJohnny Huang {2, OTP_KEY_TYPE_AES, 1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"}, 1595fdde29fSJohnny Huang {8, OTP_KEY_TYPE_RSA, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 1605fdde29fSJohnny Huang {10, OTP_KEY_TYPE_RSA, 0, "RSA-public as AES key decryption key"}, 1615fdde29fSJohnny Huang {14, OTP_KEY_TYPE_RSA, 0, "RSA-private as AES key decryption key"}, 1625fdde29fSJohnny Huang }; 1635fdde29fSJohnny Huang 1649a4fe690SJohnny Huang static uint32_t chip_version(void) 1659a4fe690SJohnny Huang { 1669a4fe690SJohnny Huang uint32_t rev_id; 1679a4fe690SJohnny Huang 1685fdde29fSJohnny Huang rev_id = readl(0x1e6e2014); 1699a4fe690SJohnny Huang 170cfa54635SJohnny Huang if (((rev_id >> 24) & 0xf) != 0x5) 1715fdde29fSJohnny Huang return -1; 1725fdde29fSJohnny Huang 1735fdde29fSJohnny Huang return (rev_id >> 16) & 0xf; 1749a4fe690SJohnny Huang } 1759a4fe690SJohnny Huang 1763d3688adSJohnny Huang static void wait_complete(void) 1773d3688adSJohnny Huang { 1783d3688adSJohnny Huang int reg; 1793d3688adSJohnny Huang 1803d3688adSJohnny Huang do { 1813d3688adSJohnny Huang reg = readl(OTP_STATUS); 1823d3688adSJohnny Huang } while ((reg & 0x6) != 0x6); 1833d3688adSJohnny Huang } 1843d3688adSJohnny Huang 1852a856b9aSJohnny Huang static void otp_read_data(uint32_t offset, uint32_t *data) 18669d5fd8fSJohnny Huang { 1873d3688adSJohnny Huang writel(offset, OTP_ADDR); //Read address 1883d3688adSJohnny Huang writel(0x23b1e361, OTP_COMMAND); //trigger read 1893d3688adSJohnny Huang wait_complete(); 1903d3688adSJohnny Huang data[0] = readl(OTP_COMPARE_1); 1913d3688adSJohnny Huang data[1] = readl(OTP_COMPARE_2); 19269d5fd8fSJohnny Huang } 19369d5fd8fSJohnny Huang 1942a856b9aSJohnny Huang static void otp_read_config(uint32_t offset, uint32_t *data) 19569d5fd8fSJohnny Huang { 19669d5fd8fSJohnny Huang int config_offset; 19769d5fd8fSJohnny Huang 19869d5fd8fSJohnny Huang config_offset = 0x800; 19969d5fd8fSJohnny Huang config_offset |= (offset / 8) * 0x200; 20069d5fd8fSJohnny Huang config_offset |= (offset % 8) * 0x2; 20169d5fd8fSJohnny Huang 2023d3688adSJohnny Huang writel(config_offset, OTP_ADDR); //Read address 2033d3688adSJohnny Huang writel(0x23b1e361, OTP_COMMAND); //trigger read 2043d3688adSJohnny Huang wait_complete(); 2053d3688adSJohnny Huang data[0] = readl(OTP_COMPARE_1); 20669d5fd8fSJohnny Huang } 20769d5fd8fSJohnny Huang 20869d5fd8fSJohnny Huang static int otp_print_config(uint32_t offset, int dw_count) 20969d5fd8fSJohnny Huang { 21069d5fd8fSJohnny Huang int i; 21169d5fd8fSJohnny Huang uint32_t ret[1]; 21269d5fd8fSJohnny Huang 21369d5fd8fSJohnny Huang if (offset + dw_count > 32) 2142a856b9aSJohnny Huang return OTP_USAGE; 21569d5fd8fSJohnny Huang for (i = offset; i < offset + dw_count; i ++) { 21669d5fd8fSJohnny Huang otp_read_config(i, ret); 217a6af4a17SJohnny Huang printf("OTPCFG%X: %08X\n", i, ret[0]); 21869d5fd8fSJohnny Huang } 21969d5fd8fSJohnny Huang printf("\n"); 2202a856b9aSJohnny Huang return OTP_SUCCESS; 22169d5fd8fSJohnny Huang } 22269d5fd8fSJohnny Huang 22369d5fd8fSJohnny Huang static int otp_print_data(uint32_t offset, int dw_count) 22469d5fd8fSJohnny Huang { 22569d5fd8fSJohnny Huang int i; 22669d5fd8fSJohnny Huang uint32_t ret[2]; 22769d5fd8fSJohnny Huang 22869d5fd8fSJohnny Huang if (offset + dw_count > 2048 || offset % 4 != 0) 2292a856b9aSJohnny Huang return OTP_USAGE; 23069d5fd8fSJohnny Huang for (i = offset; i < offset + dw_count; i += 2) { 23169d5fd8fSJohnny Huang otp_read_data(i, ret); 23269d5fd8fSJohnny Huang if (i % 4 == 0) 23369d5fd8fSJohnny Huang printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]); 23469d5fd8fSJohnny Huang else 23569d5fd8fSJohnny Huang printf("%08X %08X\n", ret[0], ret[1]); 23669d5fd8fSJohnny Huang 23769d5fd8fSJohnny Huang } 23869d5fd8fSJohnny Huang printf("\n"); 2392a856b9aSJohnny Huang return OTP_SUCCESS; 24069d5fd8fSJohnny Huang } 24169d5fd8fSJohnny Huang 24269d5fd8fSJohnny Huang static int otp_compare(uint32_t otp_addr, uint32_t addr) 24369d5fd8fSJohnny Huang { 24469d5fd8fSJohnny Huang uint32_t ret; 24569d5fd8fSJohnny Huang uint32_t *buf; 24669d5fd8fSJohnny Huang 24769d5fd8fSJohnny Huang buf = map_physmem(addr, 16, MAP_WRBACK); 24869d5fd8fSJohnny Huang printf("%08X\n", buf[0]); 24969d5fd8fSJohnny Huang printf("%08X\n", buf[1]); 25069d5fd8fSJohnny Huang printf("%08X\n", buf[2]); 25169d5fd8fSJohnny Huang printf("%08X\n", buf[3]); 2523d3688adSJohnny Huang writel(otp_addr, OTP_ADDR); //Compare address 2533d3688adSJohnny Huang writel(buf[0], OTP_COMPARE_1); //Compare data 1 2543d3688adSJohnny Huang writel(buf[1], OTP_COMPARE_2); //Compare data 2 2553d3688adSJohnny Huang writel(buf[2], OTP_COMPARE_3); //Compare data 3 2563d3688adSJohnny Huang writel(buf[3], OTP_COMPARE_4); //Compare data 4 2573d3688adSJohnny Huang writel(0x23b1e363, OTP_COMMAND); //Compare command 2583d3688adSJohnny Huang wait_complete(); 2593d3688adSJohnny Huang ret = readl(OTP_STATUS); //Compare command 26069d5fd8fSJohnny Huang if (ret & 0x1) 26169d5fd8fSJohnny Huang return 0; 26269d5fd8fSJohnny Huang else 26369d5fd8fSJohnny Huang return -1; 26469d5fd8fSJohnny Huang } 26569d5fd8fSJohnny Huang 26669d5fd8fSJohnny Huang static void otp_write(uint32_t otp_addr, uint32_t data) 26769d5fd8fSJohnny Huang { 2683d3688adSJohnny Huang writel(otp_addr, OTP_ADDR); //write address 2693d3688adSJohnny Huang writel(data, OTP_COMPARE_1); //write data 2703d3688adSJohnny Huang writel(0x23b1e362, OTP_COMMAND); //write command 2713d3688adSJohnny Huang wait_complete(); 27269d5fd8fSJohnny Huang } 27369d5fd8fSJohnny Huang 274a6d0d645SJohnny Huang static int verify_bit(uint32_t otp_addr, int bit_offset, int value) 27569d5fd8fSJohnny Huang { 27630a8c590SJohnny Huang uint32_t ret[2]; 27769d5fd8fSJohnny Huang 27830a8c590SJohnny Huang if (otp_addr % 2 == 0) 2793d3688adSJohnny Huang writel(otp_addr, OTP_ADDR); //Read address 28030a8c590SJohnny Huang else 2813d3688adSJohnny Huang writel(otp_addr - 1, OTP_ADDR); //Read address 28230a8c590SJohnny Huang 2833d3688adSJohnny Huang writel(0x23b1e361, OTP_COMMAND); //trigger read 2843d3688adSJohnny Huang wait_complete(); 2853d3688adSJohnny Huang ret[0] = readl(OTP_COMPARE_1); 2863d3688adSJohnny Huang ret[1] = readl(OTP_COMPARE_2); 28783655e91SJohnny Huang 28830a8c590SJohnny Huang if (otp_addr % 2 == 0) { 28930a8c590SJohnny Huang if (((ret[0] >> bit_offset) & 1) == value) 29069d5fd8fSJohnny Huang return 0; 29169d5fd8fSJohnny Huang else 29269d5fd8fSJohnny Huang return -1; 29330a8c590SJohnny Huang } else { 29430a8c590SJohnny Huang if (((ret[1] >> bit_offset) & 1) == value) 29530a8c590SJohnny Huang return 0; 29630a8c590SJohnny Huang else 29730a8c590SJohnny Huang return -1; 29830a8c590SJohnny Huang } 29930a8c590SJohnny Huang 30069d5fd8fSJohnny Huang } 30169d5fd8fSJohnny Huang 302696656c6SJohnny Huang static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *ignore, uint32_t *compare, int size) 3034c1c9b35SJohnny Huang { 3044c1c9b35SJohnny Huang uint32_t ret[2]; 3054c1c9b35SJohnny Huang 3064c1c9b35SJohnny Huang otp_addr &= ~(1 << 15); 3074c1c9b35SJohnny Huang 3084c1c9b35SJohnny Huang if (otp_addr % 2 == 0) 3093d3688adSJohnny Huang writel(otp_addr, OTP_ADDR); //Read address 3104c1c9b35SJohnny Huang else 3113d3688adSJohnny Huang writel(otp_addr - 1, OTP_ADDR); //Read address 3123d3688adSJohnny Huang writel(0x23b1e361, OTP_COMMAND); //trigger read 3133d3688adSJohnny Huang wait_complete(); 3143d3688adSJohnny Huang ret[0] = readl(OTP_COMPARE_1); 3153d3688adSJohnny Huang ret[1] = readl(OTP_COMPARE_2); 3164c1c9b35SJohnny Huang if (size == 1) { 3174c1c9b35SJohnny Huang if (otp_addr % 2 == 0) { 3184c1c9b35SJohnny Huang // printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]); 319696656c6SJohnny Huang if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) { 3204c1c9b35SJohnny Huang compare[0] = 0; 3214c1c9b35SJohnny Huang return 0; 3224c1c9b35SJohnny Huang } else { 3234c1c9b35SJohnny Huang compare[0] = value[0] ^ ret[0]; 3244c1c9b35SJohnny Huang return -1; 3254c1c9b35SJohnny Huang } 3264c1c9b35SJohnny Huang 3274c1c9b35SJohnny Huang } else { 3284c1c9b35SJohnny Huang // printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]); 329696656c6SJohnny Huang if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) { 3304c1c9b35SJohnny Huang compare[0] = ~0; 3314c1c9b35SJohnny Huang return 0; 3324c1c9b35SJohnny Huang } else { 333d90825e2SJohnny Huang compare[0] = ~(value[0] ^ ret[1]); 3344c1c9b35SJohnny Huang return -1; 3354c1c9b35SJohnny Huang } 3364c1c9b35SJohnny Huang } 3374c1c9b35SJohnny Huang } else if (size == 2) { 3384c1c9b35SJohnny Huang // otp_addr should be even 339696656c6SJohnny Huang if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) { 3404c1c9b35SJohnny Huang // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 3414c1c9b35SJohnny Huang // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 3424c1c9b35SJohnny Huang compare[0] = 0; 3434c1c9b35SJohnny Huang compare[1] = ~0; 3444c1c9b35SJohnny Huang return 0; 3454c1c9b35SJohnny Huang } else { 3464c1c9b35SJohnny Huang // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 3474c1c9b35SJohnny Huang // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 3484c1c9b35SJohnny Huang compare[0] = value[0] ^ ret[0]; 3494c1c9b35SJohnny Huang compare[1] = ~(value[1] ^ ret[1]); 3504c1c9b35SJohnny Huang return -1; 3514c1c9b35SJohnny Huang } 3524c1c9b35SJohnny Huang } else { 3534c1c9b35SJohnny Huang return -1; 3544c1c9b35SJohnny Huang } 3554c1c9b35SJohnny Huang } 3564c1c9b35SJohnny Huang 3577e22f42dSJohnny Huang static void otp_soak(int soak) 358d90825e2SJohnny Huang { 3599d734279SJohnny Huang if (info_cb.version == OTP_AST2600A2) { 3609d734279SJohnny Huang switch (soak) { 3619d734279SJohnny Huang case 0: //default 3629d734279SJohnny Huang otp_write(0x3000, 0x0210); // Write MRA 3639d734279SJohnny Huang otp_write(0x5000, 0x0); // Write MRB 3649d734279SJohnny Huang otp_write(0x1000, 0x0); // Write MR 3659d734279SJohnny Huang break; 3669d734279SJohnny Huang case 1: //normal program 3679d734279SJohnny Huang otp_write(0x3000, 0x1200); // Write MRA 3689d734279SJohnny Huang otp_write(0x5000, 0x100F); // Write MRB 3699d734279SJohnny Huang otp_write(0x1000, 0x1024); // Write MR 3709d734279SJohnny Huang writel(0x04190760, OTP_TIMING); 3719d734279SJohnny Huang break; 3729d734279SJohnny Huang case 2: //soak program 3739d734279SJohnny Huang otp_write(0x3000, 0x1220); // Write MRA 3749d734279SJohnny Huang otp_write(0x5000, 0x2004); // Write MRB 3759d734279SJohnny Huang otp_write(0x1000, 0x08a4); // Write MR 3769d734279SJohnny Huang writel(0x041930d4, OTP_TIMING); 3779d734279SJohnny Huang break; 3789d734279SJohnny Huang } 3799d734279SJohnny Huang } else { 380de6fbf1cSJohnny Huang switch (soak) { 381de6fbf1cSJohnny Huang case 0: //default 382de6fbf1cSJohnny Huang otp_write(0x3000, 0x0); // Write MRA 383de6fbf1cSJohnny Huang otp_write(0x5000, 0x0); // Write MRB 384de6fbf1cSJohnny Huang otp_write(0x1000, 0x0); // Write MR 385de6fbf1cSJohnny Huang break; 386de6fbf1cSJohnny Huang case 1: //normal program 387de6fbf1cSJohnny Huang otp_write(0x3000, 0x4021); // Write MRA 388de6fbf1cSJohnny Huang otp_write(0x5000, 0x302f); // Write MRB 389de6fbf1cSJohnny Huang otp_write(0x1000, 0x4020); // Write MR 390de6fbf1cSJohnny Huang writel(0x04190760, OTP_TIMING); 391de6fbf1cSJohnny Huang break; 392de6fbf1cSJohnny Huang case 2: //soak program 393d90825e2SJohnny Huang otp_write(0x3000, 0x4021); // Write MRA 394d90825e2SJohnny Huang otp_write(0x5000, 0x1027); // Write MRB 395d90825e2SJohnny Huang otp_write(0x1000, 0x4820); // Write MR 396de6fbf1cSJohnny Huang writel(0x041930d4, OTP_TIMING); 397de6fbf1cSJohnny Huang break; 398d90825e2SJohnny Huang } 3999d734279SJohnny Huang } 400de6fbf1cSJohnny Huang 4013d3688adSJohnny Huang wait_complete(); 402d90825e2SJohnny Huang } 403d90825e2SJohnny Huang 40483655e91SJohnny Huang static void otp_prog(uint32_t otp_addr, uint32_t prog_bit) 40583655e91SJohnny Huang { 40683655e91SJohnny Huang writel(otp_addr, OTP_ADDR); //write address 40783655e91SJohnny Huang writel(prog_bit, OTP_COMPARE_1); //write data 40883655e91SJohnny Huang writel(0x23b1e364, OTP_COMMAND); //write command 40983655e91SJohnny Huang wait_complete(); 41083655e91SJohnny Huang } 41183655e91SJohnny Huang 41283655e91SJohnny Huang static void _otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset) 41383655e91SJohnny Huang { 41483655e91SJohnny Huang int prog_bit; 41583655e91SJohnny Huang 41683655e91SJohnny Huang if (prog_address % 2 == 0) { 41783655e91SJohnny Huang if (value) 41883655e91SJohnny Huang prog_bit = ~(0x1 << bit_offset); 41983655e91SJohnny Huang else 42083655e91SJohnny Huang return; 42183655e91SJohnny Huang } else { 42283655e91SJohnny Huang prog_address |= 1 << 15; 42383655e91SJohnny Huang if (!value) 42483655e91SJohnny Huang prog_bit = 0x1 << bit_offset; 42583655e91SJohnny Huang else 42683655e91SJohnny Huang return; 42783655e91SJohnny Huang } 42883655e91SJohnny Huang otp_prog(prog_address, prog_bit); 42983655e91SJohnny Huang } 43083655e91SJohnny Huang 43183655e91SJohnny Huang static int otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset) 43283655e91SJohnny Huang { 43383655e91SJohnny Huang int pass; 43483655e91SJohnny Huang int i; 43583655e91SJohnny Huang 43683655e91SJohnny Huang otp_soak(1); 43783655e91SJohnny Huang _otp_prog_bit(value, prog_address, bit_offset); 43883655e91SJohnny Huang pass = 0; 43983655e91SJohnny Huang 44083655e91SJohnny Huang for (i = 0; i < RETRY; i++) { 44183655e91SJohnny Huang if (verify_bit(prog_address, bit_offset, value) != 0) { 44283655e91SJohnny Huang otp_soak(2); 44383655e91SJohnny Huang _otp_prog_bit(value, prog_address, bit_offset); 44483655e91SJohnny Huang if (verify_bit(prog_address, bit_offset, value) != 0) { 44583655e91SJohnny Huang otp_soak(1); 44683655e91SJohnny Huang } else { 44783655e91SJohnny Huang pass = 1; 44883655e91SJohnny Huang break; 44983655e91SJohnny Huang } 45083655e91SJohnny Huang } else { 45183655e91SJohnny Huang pass = 1; 45283655e91SJohnny Huang break; 45383655e91SJohnny Huang } 45483655e91SJohnny Huang } 45583655e91SJohnny Huang 45683655e91SJohnny Huang return pass; 45783655e91SJohnny Huang } 45883655e91SJohnny Huang 459696656c6SJohnny Huang static void otp_prog_dw(uint32_t value, uint32_t ignore, uint32_t prog_address) 460d90825e2SJohnny Huang { 461d90825e2SJohnny Huang int j, bit_value, prog_bit; 462d90825e2SJohnny Huang 463d90825e2SJohnny Huang for (j = 0; j < 32; j++) { 464696656c6SJohnny Huang if ((ignore >> j) & 0x1) 465d90825e2SJohnny Huang continue; 466d90825e2SJohnny Huang bit_value = (value >> j) & 0x1; 467d90825e2SJohnny Huang if (prog_address % 2 == 0) { 468d90825e2SJohnny Huang if (bit_value) 469d90825e2SJohnny Huang prog_bit = ~(0x1 << j); 470d90825e2SJohnny Huang else 471d90825e2SJohnny Huang continue; 472d90825e2SJohnny Huang } else { 473d90825e2SJohnny Huang prog_address |= 1 << 15; 474d90825e2SJohnny Huang if (bit_value) 475d90825e2SJohnny Huang continue; 476d90825e2SJohnny Huang else 477d90825e2SJohnny Huang prog_bit = 0x1 << j; 478d90825e2SJohnny Huang } 479d90825e2SJohnny Huang otp_prog(prog_address, prog_bit); 480d90825e2SJohnny Huang } 481d90825e2SJohnny Huang } 482d90825e2SJohnny Huang 48354552c69SJohnny Huang static int otp_prog_verify_2dw(uint32_t *data, uint32_t *buf, uint32_t *ignore_mask, uint32_t prog_address) 48454552c69SJohnny Huang { 48554552c69SJohnny Huang int pass; 48654552c69SJohnny Huang int i; 48754552c69SJohnny Huang uint32_t data0_masked; 48854552c69SJohnny Huang uint32_t data1_masked; 48954552c69SJohnny Huang uint32_t buf0_masked; 49054552c69SJohnny Huang uint32_t buf1_masked; 49154552c69SJohnny Huang uint32_t compare[2]; 49254552c69SJohnny Huang 49354552c69SJohnny Huang data0_masked = data[0] & ~ignore_mask[0]; 49454552c69SJohnny Huang buf0_masked = buf[0] & ~ignore_mask[0]; 49554552c69SJohnny Huang data1_masked = data[1] & ~ignore_mask[1]; 49654552c69SJohnny Huang buf1_masked = buf[1] & ~ignore_mask[1]; 49754552c69SJohnny Huang if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked)) 49854552c69SJohnny Huang return 0; 49954552c69SJohnny Huang 50054552c69SJohnny Huang otp_soak(1); 50154552c69SJohnny Huang if (data0_masked != buf0_masked) 50254552c69SJohnny Huang otp_prog_dw(buf[0], ignore_mask[0], prog_address); 50354552c69SJohnny Huang if (data1_masked != buf1_masked) 50454552c69SJohnny Huang otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1); 50554552c69SJohnny Huang 50654552c69SJohnny Huang pass = 0; 50754552c69SJohnny Huang for (i = 0; i < RETRY; i++) { 50854552c69SJohnny Huang if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) { 50954552c69SJohnny Huang otp_soak(2); 51054552c69SJohnny Huang if (compare[0] != 0) { 51154552c69SJohnny Huang otp_prog_dw(compare[0], ignore_mask[0], prog_address); 51254552c69SJohnny Huang } 51354552c69SJohnny Huang if (compare[1] != ~0) { 51454552c69SJohnny Huang otp_prog_dw(compare[1], ignore_mask[0], prog_address + 1); 51554552c69SJohnny Huang } 51654552c69SJohnny Huang if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) { 51754552c69SJohnny Huang otp_soak(1); 51854552c69SJohnny Huang } else { 51954552c69SJohnny Huang pass = 1; 52054552c69SJohnny Huang break; 52154552c69SJohnny Huang } 52254552c69SJohnny Huang } else { 52354552c69SJohnny Huang pass = 1; 52454552c69SJohnny Huang break; 52554552c69SJohnny Huang } 52654552c69SJohnny Huang } 52754552c69SJohnny Huang 52854552c69SJohnny Huang if (!pass) { 52954552c69SJohnny Huang otp_soak(0); 53054552c69SJohnny Huang return OTP_FAILURE; 53154552c69SJohnny Huang } 53254552c69SJohnny Huang return OTP_SUCCESS; 53354552c69SJohnny Huang } 53454552c69SJohnny Huang 535541eb887SJohnny Huang static void otp_strap_status(struct otpstrap_status *otpstrap) 53676d13988SJohnny Huang { 53776d13988SJohnny Huang uint32_t OTPSTRAP_RAW[2]; 5385010032bSJohnny Huang int strap_end; 53976d13988SJohnny Huang int i, j; 54076d13988SJohnny Huang 5415010032bSJohnny Huang if (info_cb.version == OTP_AST2600A0) { 54276d13988SJohnny Huang for (j = 0; j < 64; j++) { 54376d13988SJohnny Huang otpstrap[j].value = 0; 54476d13988SJohnny Huang otpstrap[j].remain_times = 7; 54576d13988SJohnny Huang otpstrap[j].writeable_option = -1; 54676d13988SJohnny Huang otpstrap[j].protected = 0; 54776d13988SJohnny Huang } 5485010032bSJohnny Huang strap_end = 30; 5495010032bSJohnny Huang } else { 5505010032bSJohnny Huang for (j = 0; j < 64; j++) { 5515010032bSJohnny Huang otpstrap[j].value = 0; 5525010032bSJohnny Huang otpstrap[j].remain_times = 6; 5535010032bSJohnny Huang otpstrap[j].writeable_option = -1; 5545010032bSJohnny Huang otpstrap[j].reg_protected = 0; 5555010032bSJohnny Huang otpstrap[j].protected = 0; 5565010032bSJohnny Huang } 5575010032bSJohnny Huang strap_end = 28; 5585010032bSJohnny Huang } 55976d13988SJohnny Huang 5605010032bSJohnny Huang for (i = 16; i < strap_end; i += 2) { 56176d13988SJohnny Huang int option = (i - 16) / 2; 56276d13988SJohnny Huang otp_read_config(i, &OTPSTRAP_RAW[0]); 56376d13988SJohnny Huang otp_read_config(i + 1, &OTPSTRAP_RAW[1]); 56476d13988SJohnny Huang for (j = 0; j < 32; j++) { 56576d13988SJohnny Huang char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1); 56676d13988SJohnny Huang if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) { 56776d13988SJohnny Huang otpstrap[j].writeable_option = option; 56876d13988SJohnny Huang } 56976d13988SJohnny Huang if (bit_value == 1) 57076d13988SJohnny Huang otpstrap[j].remain_times --; 57176d13988SJohnny Huang otpstrap[j].value ^= bit_value; 57276d13988SJohnny Huang otpstrap[j].option_array[option] = bit_value; 57376d13988SJohnny Huang } 57476d13988SJohnny Huang for (j = 32; j < 64; j++) { 57576d13988SJohnny Huang char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1); 57676d13988SJohnny Huang if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) { 57776d13988SJohnny Huang otpstrap[j].writeable_option = option; 57876d13988SJohnny Huang } 57976d13988SJohnny Huang if (bit_value == 1) 58076d13988SJohnny Huang otpstrap[j].remain_times --; 58176d13988SJohnny Huang otpstrap[j].value ^= bit_value; 58276d13988SJohnny Huang otpstrap[j].option_array[option] = bit_value; 58376d13988SJohnny Huang } 58476d13988SJohnny Huang } 5855010032bSJohnny Huang 5865010032bSJohnny Huang if (info_cb.version != OTP_AST2600A0) { 5875010032bSJohnny Huang otp_read_config(28, &OTPSTRAP_RAW[0]); 5885010032bSJohnny Huang otp_read_config(29, &OTPSTRAP_RAW[1]); 5895010032bSJohnny Huang for (j = 0; j < 32; j++) { 5905010032bSJohnny Huang if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1) 5915010032bSJohnny Huang otpstrap[j].reg_protected = 1; 5925010032bSJohnny Huang } 5935010032bSJohnny Huang for (j = 32; j < 64; j++) { 5945010032bSJohnny Huang if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1) 5955010032bSJohnny Huang otpstrap[j].reg_protected = 1; 5965010032bSJohnny Huang } 5975010032bSJohnny Huang 5985010032bSJohnny Huang } 5995010032bSJohnny Huang 60076d13988SJohnny Huang otp_read_config(30, &OTPSTRAP_RAW[0]); 60176d13988SJohnny Huang otp_read_config(31, &OTPSTRAP_RAW[1]); 60276d13988SJohnny Huang for (j = 0; j < 32; j++) { 60376d13988SJohnny Huang if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1) 60476d13988SJohnny Huang otpstrap[j].protected = 1; 60576d13988SJohnny Huang } 60676d13988SJohnny Huang for (j = 32; j < 64; j++) { 60776d13988SJohnny Huang if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1) 60876d13988SJohnny Huang otpstrap[j].protected = 1; 60976d13988SJohnny Huang } 61076d13988SJohnny Huang } 61176d13988SJohnny Huang 612696656c6SJohnny Huang static int otp_print_conf_image(struct otp_image_layout *image_layout) 61369d5fd8fSJohnny Huang { 61479e42a59SJoel Stanley const struct otpconf_info *conf_info = info_cb.conf_info; 615696656c6SJohnny Huang uint32_t *OTPCFG = (uint32_t *)image_layout->conf; 616696656c6SJohnny Huang uint32_t *OTPCFG_IGNORE = (uint32_t *)image_layout->conf_ignore; 617b458cd62SJohnny Huang uint32_t mask; 618b458cd62SJohnny Huang uint32_t dw_offset; 619b458cd62SJohnny Huang uint32_t bit_offset; 620b458cd62SJohnny Huang uint32_t otp_value; 621696656c6SJohnny Huang uint32_t otp_ignore; 622b458cd62SJohnny Huang int fail = 0; 62373f11549SJohnny Huang char valid_bit[20]; 62466f2f8e5SJohnny Huang int i; 62573f11549SJohnny Huang int j; 62666f2f8e5SJohnny Huang 627737ed20bSJohnny Huang printf("DW BIT Value Description\n"); 62866f2f8e5SJohnny Huang printf("__________________________________________________________________________\n"); 6293cb28812SJohnny Huang for (i = 0; i < info_cb.conf_info_len; i++) { 6303cb28812SJohnny Huang dw_offset = conf_info[i].dw_offset; 6313cb28812SJohnny Huang bit_offset = conf_info[i].bit_offset; 6323cb28812SJohnny Huang mask = BIT(conf_info[i].length) - 1; 633b458cd62SJohnny Huang otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask; 634696656c6SJohnny Huang otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask; 635b458cd62SJohnny Huang 636696656c6SJohnny Huang if (otp_ignore == mask) { 637b458cd62SJohnny Huang continue; 638696656c6SJohnny Huang } else if (otp_ignore != 0) { 639b458cd62SJohnny Huang fail = 1; 640b458cd62SJohnny Huang } 641b458cd62SJohnny Huang 6423cb28812SJohnny Huang if ((otp_value != conf_info[i].value) && 6433cb28812SJohnny Huang conf_info[i].value != OTP_REG_RESERVED && 6443cb28812SJohnny Huang conf_info[i].value != OTP_REG_VALUE && 6453cb28812SJohnny Huang conf_info[i].value != OTP_REG_VALID_BIT) 646b458cd62SJohnny Huang continue; 647b458cd62SJohnny Huang printf("0x%-4X", dw_offset); 648b458cd62SJohnny Huang 6493cb28812SJohnny Huang if (conf_info[i].length == 1) { 6503cb28812SJohnny Huang printf("0x%-9X", conf_info[i].bit_offset); 65166f2f8e5SJohnny Huang } else { 652b458cd62SJohnny Huang printf("0x%-2X:0x%-4X", 6533cb28812SJohnny Huang conf_info[i].bit_offset + conf_info[i].length - 1, 6543cb28812SJohnny Huang conf_info[i].bit_offset); 65566f2f8e5SJohnny Huang } 656b458cd62SJohnny Huang printf("0x%-10x", otp_value); 657b458cd62SJohnny Huang 658b458cd62SJohnny Huang if (fail) { 659696656c6SJohnny Huang printf("Ignore mask error\n"); 660b458cd62SJohnny Huang } else { 6613cb28812SJohnny Huang if (conf_info[i].value == OTP_REG_RESERVED) { 662b458cd62SJohnny Huang printf("Reserved\n"); 6633cb28812SJohnny Huang } else if (conf_info[i].value == OTP_REG_VALUE) { 6643cb28812SJohnny Huang printf(conf_info[i].information, otp_value); 665b458cd62SJohnny Huang printf("\n"); 6663cb28812SJohnny Huang } else if (conf_info[i].value == OTP_REG_VALID_BIT) { 667b458cd62SJohnny Huang if (otp_value != 0) { 66873f11549SJohnny Huang for (j = 0; j < 7; j++) { 66973f11549SJohnny Huang if (otp_value == (1 << j)) { 67073f11549SJohnny Huang valid_bit[j * 2] = '1'; 671b458cd62SJohnny Huang } else { 67273f11549SJohnny Huang valid_bit[j * 2] = '0'; 67373f11549SJohnny Huang } 67473f11549SJohnny Huang valid_bit[j * 2 + 1] = ' '; 67573f11549SJohnny Huang } 67673f11549SJohnny Huang valid_bit[15] = 0; 67773f11549SJohnny Huang } else { 67873f11549SJohnny Huang strcpy(valid_bit, "0 0 0 0 0 0 0 0\0"); 679b458cd62SJohnny Huang } 6803cb28812SJohnny Huang printf(conf_info[i].information, valid_bit); 681b458cd62SJohnny Huang printf("\n"); 682b458cd62SJohnny Huang } else { 6833cb28812SJohnny Huang printf("%s\n", conf_info[i].information); 684b458cd62SJohnny Huang } 685b458cd62SJohnny Huang } 686b458cd62SJohnny Huang } 687b458cd62SJohnny Huang 688b458cd62SJohnny Huang if (fail) 689b458cd62SJohnny Huang return OTP_FAILURE; 690b458cd62SJohnny Huang 69166f2f8e5SJohnny Huang return OTP_SUCCESS; 69266f2f8e5SJohnny Huang } 69366f2f8e5SJohnny Huang 6942d4b0742SJohnny Huang static int otp_print_conf_info(int input_offset) 69566f2f8e5SJohnny Huang { 69679e42a59SJoel Stanley const struct otpconf_info *conf_info = info_cb.conf_info; 697bb34a7bfSJohnny Huang uint32_t OTPCFG[16]; 698b458cd62SJohnny Huang uint32_t mask; 699b458cd62SJohnny Huang uint32_t dw_offset; 700b458cd62SJohnny Huang uint32_t bit_offset; 701b458cd62SJohnny Huang uint32_t otp_value; 70273f11549SJohnny Huang char valid_bit[20]; 70366f2f8e5SJohnny Huang int i; 70473f11549SJohnny Huang int j; 70566f2f8e5SJohnny Huang 706bb34a7bfSJohnny Huang for (i = 0; i < 16; i++) 70766f2f8e5SJohnny Huang otp_read_config(i, &OTPCFG[i]); 70866f2f8e5SJohnny Huang 70966f2f8e5SJohnny Huang 710b458cd62SJohnny Huang printf("DW BIT Value Description\n"); 711b458cd62SJohnny Huang printf("__________________________________________________________________________\n"); 7123cb28812SJohnny Huang for (i = 0; i < info_cb.conf_info_len; i++) { 7133cb28812SJohnny Huang if (input_offset != -1 && input_offset != conf_info[i].dw_offset) 7142d4b0742SJohnny Huang continue; 7153cb28812SJohnny Huang dw_offset = conf_info[i].dw_offset; 7163cb28812SJohnny Huang bit_offset = conf_info[i].bit_offset; 7173cb28812SJohnny Huang mask = BIT(conf_info[i].length) - 1; 718b458cd62SJohnny Huang otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask; 719b458cd62SJohnny Huang 7203cb28812SJohnny Huang if ((otp_value != conf_info[i].value) && 7213cb28812SJohnny Huang conf_info[i].value != OTP_REG_RESERVED && 7223cb28812SJohnny Huang conf_info[i].value != OTP_REG_VALUE && 7233cb28812SJohnny Huang conf_info[i].value != OTP_REG_VALID_BIT) 724b458cd62SJohnny Huang continue; 725b458cd62SJohnny Huang printf("0x%-4X", dw_offset); 726b458cd62SJohnny Huang 7273cb28812SJohnny Huang if (conf_info[i].length == 1) { 7283cb28812SJohnny Huang printf("0x%-9X", conf_info[i].bit_offset); 729b458cd62SJohnny Huang } else { 730b458cd62SJohnny Huang printf("0x%-2X:0x%-4X", 7313cb28812SJohnny Huang conf_info[i].bit_offset + conf_info[i].length - 1, 7323cb28812SJohnny Huang conf_info[i].bit_offset); 733b458cd62SJohnny Huang } 734b458cd62SJohnny Huang printf("0x%-10x", otp_value); 735b458cd62SJohnny Huang 7363cb28812SJohnny Huang if (conf_info[i].value == OTP_REG_RESERVED) { 737b458cd62SJohnny Huang printf("Reserved\n"); 7383cb28812SJohnny Huang } else if (conf_info[i].value == OTP_REG_VALUE) { 7393cb28812SJohnny Huang printf(conf_info[i].information, otp_value); 740b458cd62SJohnny Huang printf("\n"); 7413cb28812SJohnny Huang } else if (conf_info[i].value == OTP_REG_VALID_BIT) { 742b458cd62SJohnny Huang if (otp_value != 0) { 74373f11549SJohnny Huang for (j = 0; j < 7; j++) { 74473f11549SJohnny Huang if (otp_value == (1 << j)) { 74573f11549SJohnny Huang valid_bit[j * 2] = '1'; 746b458cd62SJohnny Huang } else { 74773f11549SJohnny Huang valid_bit[j * 2] = '0'; 74873f11549SJohnny Huang } 74973f11549SJohnny Huang valid_bit[j * 2 + 1] = ' '; 75073f11549SJohnny Huang } 75173f11549SJohnny Huang valid_bit[15] = 0; 75273f11549SJohnny Huang } else { 75373f11549SJohnny Huang strcpy(valid_bit, "0 0 0 0 0 0 0 0\0"); 754b458cd62SJohnny Huang } 7553cb28812SJohnny Huang printf(conf_info[i].information, valid_bit); 756b458cd62SJohnny Huang printf("\n"); 757b458cd62SJohnny Huang } else { 7583cb28812SJohnny Huang printf("%s\n", conf_info[i].information); 759b458cd62SJohnny Huang } 760b458cd62SJohnny Huang } 761b458cd62SJohnny Huang return OTP_SUCCESS; 76266f2f8e5SJohnny Huang } 76366f2f8e5SJohnny Huang 7645010032bSJohnny Huang static int otp_print_strap_image(struct otp_image_layout *image_layout) 76576d13988SJohnny Huang { 76679e42a59SJoel Stanley const struct otpstrap_info *strap_info = info_cb.strap_info; 767696656c6SJohnny Huang uint32_t *OTPSTRAP; 768696656c6SJohnny Huang uint32_t *OTPSTRAP_REG_PRO; 769696656c6SJohnny Huang uint32_t *OTPSTRAP_PRO; 770696656c6SJohnny Huang uint32_t *OTPSTRAP_IGNORE; 77176d13988SJohnny Huang int i; 772a8bd6d8cSJohnny Huang int fail = 0; 773a8bd6d8cSJohnny Huang uint32_t bit_offset; 774a8bd6d8cSJohnny Huang uint32_t dw_offset; 775a8bd6d8cSJohnny Huang uint32_t mask; 776a8bd6d8cSJohnny Huang uint32_t otp_value; 777696656c6SJohnny Huang uint32_t otp_reg_protect; 778a8bd6d8cSJohnny Huang uint32_t otp_protect; 779696656c6SJohnny Huang uint32_t otp_ignore; 78076d13988SJohnny Huang 781696656c6SJohnny Huang OTPSTRAP = (uint32_t *)image_layout->strap; 782696656c6SJohnny Huang OTPSTRAP_PRO = (uint32_t *)image_layout->strap_pro; 783696656c6SJohnny Huang OTPSTRAP_IGNORE = (uint32_t *)image_layout->strap_ignore; 7845010032bSJohnny Huang if (info_cb.version == OTP_AST2600A0) { 785696656c6SJohnny Huang OTPSTRAP_REG_PRO = NULL; 786a8bd6d8cSJohnny Huang printf("BIT(hex) Value Protect Description\n"); 787696656c6SJohnny Huang } else { 788696656c6SJohnny Huang OTPSTRAP_REG_PRO = (uint32_t *)image_layout->strap_reg_pro; 789de6b0cc4SJohnny Huang printf("BIT(hex) Value Reg_Protect Protect Description\n"); 790696656c6SJohnny Huang } 791de6b0cc4SJohnny Huang printf("__________________________________________________________________________________________\n"); 792b458cd62SJohnny Huang 7933cb28812SJohnny Huang for (i = 0; i < info_cb.strap_info_len; i++) { 794696656c6SJohnny Huang if (strap_info[i].bit_offset > 31) { 795a8bd6d8cSJohnny Huang dw_offset = 1; 7963cb28812SJohnny Huang bit_offset = strap_info[i].bit_offset - 32; 797a8bd6d8cSJohnny Huang } else { 798a8bd6d8cSJohnny Huang dw_offset = 0; 7993cb28812SJohnny Huang bit_offset = strap_info[i].bit_offset; 800a8bd6d8cSJohnny Huang } 80176d13988SJohnny Huang 8023cb28812SJohnny Huang mask = BIT(strap_info[i].length) - 1; 803a8bd6d8cSJohnny Huang otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask; 804a8bd6d8cSJohnny Huang otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask; 805696656c6SJohnny Huang otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask; 806a8bd6d8cSJohnny Huang 8075010032bSJohnny Huang if (info_cb.version != OTP_AST2600A0) 808696656c6SJohnny Huang otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask; 8095010032bSJohnny Huang else 8105010032bSJohnny Huang otp_reg_protect = 0; 811696656c6SJohnny Huang 812696656c6SJohnny Huang if (otp_ignore == mask) { 813a8bd6d8cSJohnny Huang continue; 814696656c6SJohnny Huang } else if (otp_ignore != 0) { 815a8bd6d8cSJohnny Huang fail = 1; 816a8bd6d8cSJohnny Huang } 817a8bd6d8cSJohnny Huang 8183cb28812SJohnny Huang if ((otp_value != strap_info[i].value) && 8193cb28812SJohnny Huang strap_info[i].value != OTP_REG_RESERVED) 820a8bd6d8cSJohnny Huang continue; 821a8bd6d8cSJohnny Huang 8223cb28812SJohnny Huang if (strap_info[i].length == 1) { 8233cb28812SJohnny Huang printf("0x%-9X", strap_info[i].bit_offset); 824a8bd6d8cSJohnny Huang } else { 825b458cd62SJohnny Huang printf("0x%-2X:0x%-4X", 8263cb28812SJohnny Huang strap_info[i].bit_offset + strap_info[i].length - 1, 8273cb28812SJohnny Huang strap_info[i].bit_offset); 828a8bd6d8cSJohnny Huang } 829a8bd6d8cSJohnny Huang printf("0x%-10x", otp_value); 8305010032bSJohnny Huang if (info_cb.version != OTP_AST2600A0) 831696656c6SJohnny Huang printf("0x%-10x", otp_reg_protect); 832a8bd6d8cSJohnny Huang printf("0x%-10x", otp_protect); 833a8bd6d8cSJohnny Huang 834a8bd6d8cSJohnny Huang if (fail) { 835696656c6SJohnny Huang printf("Ignore mask error\n"); 836a8bd6d8cSJohnny Huang } else { 8373cb28812SJohnny Huang if (strap_info[i].value != OTP_REG_RESERVED) 8383cb28812SJohnny Huang printf("%s\n", strap_info[i].information); 839a8bd6d8cSJohnny Huang else 840a8bd6d8cSJohnny Huang printf("Reserved\n"); 841a8bd6d8cSJohnny Huang } 842a8bd6d8cSJohnny Huang } 843a8bd6d8cSJohnny Huang 844a8bd6d8cSJohnny Huang if (fail) 84576d13988SJohnny Huang return OTP_FAILURE; 84676d13988SJohnny Huang 84776d13988SJohnny Huang return OTP_SUCCESS; 84876d13988SJohnny Huang } 84976d13988SJohnny Huang 850b458cd62SJohnny Huang static int otp_print_strap_info(int view) 85176d13988SJohnny Huang { 85279e42a59SJoel Stanley const struct otpstrap_info *strap_info = info_cb.strap_info; 85376d13988SJohnny Huang struct otpstrap_status strap_status[64]; 85407baa4e8SJohnny Huang int i, j; 855b458cd62SJohnny Huang int fail = 0; 856b458cd62SJohnny Huang uint32_t bit_offset; 857b458cd62SJohnny Huang uint32_t length; 858b458cd62SJohnny Huang uint32_t otp_value; 859b458cd62SJohnny Huang uint32_t otp_protect; 86076d13988SJohnny Huang 861541eb887SJohnny Huang otp_strap_status(strap_status); 86276d13988SJohnny Huang 863b458cd62SJohnny Huang if (view) { 86483655e91SJohnny Huang if (info_cb.version == OTP_AST2600A0) 86507baa4e8SJohnny Huang printf("BIT(hex) Value Remains Protect Description\n"); 86683655e91SJohnny Huang else 86783655e91SJohnny Huang printf("BIT(hex) Value Remains Reg_Protect Protect Description\n"); 86807baa4e8SJohnny Huang printf("___________________________________________________________________________________________________\n"); 869b458cd62SJohnny Huang } else { 870b458cd62SJohnny Huang printf("BIT(hex) Value Description\n"); 871b458cd62SJohnny Huang printf("________________________________________________________________________________\n"); 87276d13988SJohnny Huang } 8733cb28812SJohnny Huang for (i = 0; i < info_cb.strap_info_len; i++) { 874b458cd62SJohnny Huang otp_value = 0; 8753cb28812SJohnny Huang bit_offset = strap_info[i].bit_offset; 8763cb28812SJohnny Huang length = strap_info[i].length; 877b458cd62SJohnny Huang for (j = 0; j < length; j++) { 878c947ef08SJohnny Huang otp_value |= strap_status[bit_offset + j].value << j; 879c947ef08SJohnny Huang otp_protect |= strap_status[bit_offset + j].protected << j; 880b458cd62SJohnny Huang } 8813cb28812SJohnny Huang if ((otp_value != strap_info[i].value) && 8823cb28812SJohnny Huang strap_info[i].value != OTP_REG_RESERVED) 883b458cd62SJohnny Huang continue; 884b458cd62SJohnny Huang if (view) { 885b458cd62SJohnny Huang for (j = 0; j < length; j++) { 8863cb28812SJohnny Huang printf("0x%-7X", strap_info[i].bit_offset + j); 887b458cd62SJohnny Huang printf("0x%-5X", strap_status[bit_offset + j].value); 88807baa4e8SJohnny Huang printf("%-9d", strap_status[bit_offset + j].remain_times); 88983655e91SJohnny Huang if (info_cb.version != OTP_AST2600A0) 890e1a7245eSJohnny Huang printf("0x%-10X", strap_status[bit_offset + j].reg_protected); 891e1a7245eSJohnny Huang printf("0x%-7X", strap_status[bit_offset + j].protected); 8923cb28812SJohnny Huang if (strap_info[i].value == OTP_REG_RESERVED) { 893b458cd62SJohnny Huang printf(" Reserved\n"); 894b458cd62SJohnny Huang continue; 895b458cd62SJohnny Huang } 896b458cd62SJohnny Huang if (length == 1) { 8973cb28812SJohnny Huang printf(" %s\n", strap_info[i].information); 898b458cd62SJohnny Huang continue; 89976d13988SJohnny Huang } 90076d13988SJohnny Huang 901b458cd62SJohnny Huang if (j == 0) 9023cb28812SJohnny Huang printf("/%s\n", strap_info[i].information); 903b458cd62SJohnny Huang else if (j == length - 1) 904b458cd62SJohnny Huang printf("\\ \"\n"); 905b458cd62SJohnny Huang else 906b458cd62SJohnny Huang printf("| \"\n"); 90776d13988SJohnny Huang } 908b458cd62SJohnny Huang } else { 909c947ef08SJohnny Huang if (length == 1) { 9103cb28812SJohnny Huang printf("0x%-9X", strap_info[i].bit_offset); 911b458cd62SJohnny Huang } else { 912b458cd62SJohnny Huang printf("0x%-2X:0x%-4X", 913b458cd62SJohnny Huang bit_offset + length - 1, bit_offset); 914b458cd62SJohnny Huang } 915b458cd62SJohnny Huang 916b458cd62SJohnny Huang printf("0x%-10X", otp_value); 917b458cd62SJohnny Huang 9183cb28812SJohnny Huang if (strap_info[i].value != OTP_REG_RESERVED) 9193cb28812SJohnny Huang printf("%s\n", strap_info[i].information); 920b458cd62SJohnny Huang else 921b458cd62SJohnny Huang printf("Reserved\n"); 922b458cd62SJohnny Huang } 923b458cd62SJohnny Huang } 924b458cd62SJohnny Huang 925b458cd62SJohnny Huang if (fail) 926b458cd62SJohnny Huang return OTP_FAILURE; 927b458cd62SJohnny Huang 928b458cd62SJohnny Huang return OTP_SUCCESS; 929b458cd62SJohnny Huang } 930b458cd62SJohnny Huang 931696656c6SJohnny Huang static void buf_print(uint8_t *buf, int len) 93269d5fd8fSJohnny Huang { 93369d5fd8fSJohnny Huang int i; 93469d5fd8fSJohnny Huang printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); 93569d5fd8fSJohnny Huang for (i = 0; i < len; i++) { 93669d5fd8fSJohnny Huang if (i % 16 == 0) { 93769d5fd8fSJohnny Huang printf("%04X: ", i); 93869d5fd8fSJohnny Huang } 93969d5fd8fSJohnny Huang printf("%02X ", buf[i]); 94069d5fd8fSJohnny Huang if ((i + 1) % 16 == 0) { 94169d5fd8fSJohnny Huang printf("\n"); 94269d5fd8fSJohnny Huang } 94369d5fd8fSJohnny Huang } 94469d5fd8fSJohnny Huang } 94569d5fd8fSJohnny Huang 946696656c6SJohnny Huang static int otp_print_data_info(struct otp_image_layout *image_layout) 94769d5fd8fSJohnny Huang { 94869d5fd8fSJohnny Huang int key_id, key_offset, last, key_type, key_length, exp_length; 94979e42a59SJoel Stanley const struct otpkey_type *key_info_array = info_cb.key_info; 9509a4fe690SJohnny Huang struct otpkey_type key_info; 951696656c6SJohnny Huang uint32_t *buf; 952696656c6SJohnny Huang uint8_t *byte_buf; 9539d998018SJohnny Huang char empty = 1; 95469d5fd8fSJohnny Huang int i = 0, len = 0; 9559a4fe690SJohnny Huang int j; 95654552c69SJohnny Huang 957696656c6SJohnny Huang byte_buf = image_layout->data; 958696656c6SJohnny Huang buf = (uint32_t *)byte_buf; 9599d998018SJohnny Huang 9609d998018SJohnny Huang for (i = 0; i < 16; i++) { 9619d998018SJohnny Huang if (buf[i] != 0) { 9629d998018SJohnny Huang empty = 0; 9639d998018SJohnny Huang } 9649d998018SJohnny Huang } 9659d998018SJohnny Huang if (empty) 9669d998018SJohnny Huang return 0; 9679d998018SJohnny Huang 9689d998018SJohnny Huang i = 0; 96969d5fd8fSJohnny Huang while (1) { 97069d5fd8fSJohnny Huang key_id = buf[i] & 0x7; 97169d5fd8fSJohnny Huang key_offset = buf[i] & 0x1ff8; 97269d5fd8fSJohnny Huang last = (buf[i] >> 13) & 1; 97369d5fd8fSJohnny Huang key_type = (buf[i] >> 14) & 0xf; 97469d5fd8fSJohnny Huang key_length = (buf[i] >> 18) & 0x3; 97569d5fd8fSJohnny Huang exp_length = (buf[i] >> 20) & 0xfff; 9769a4fe690SJohnny Huang 9779a4fe690SJohnny Huang for (j = 0; j < info_cb.key_info_len; j++) { 9789a4fe690SJohnny Huang if (key_type == key_info_array[j].value) { 9799a4fe690SJohnny Huang key_info = key_info_array[j]; 9809a4fe690SJohnny Huang break; 9819a4fe690SJohnny Huang } 9829a4fe690SJohnny Huang } 9839a4fe690SJohnny Huang 9847f795e57SJohnny Huang printf("\nKey[%d]:\n", i); 98569d5fd8fSJohnny Huang printf("Key Type: "); 9869a4fe690SJohnny Huang printf("%s\n", key_info.information); 9879a4fe690SJohnny Huang 9889a4fe690SJohnny Huang if (key_info.key_type == OTP_KEY_TYPE_HMAC) { 98969d5fd8fSJohnny Huang printf("HMAC SHA Type: "); 99069d5fd8fSJohnny Huang switch (key_length) { 99169d5fd8fSJohnny Huang case 0: 99269d5fd8fSJohnny Huang printf("HMAC(SHA224)\n"); 99369d5fd8fSJohnny Huang break; 99469d5fd8fSJohnny Huang case 1: 99569d5fd8fSJohnny Huang printf("HMAC(SHA256)\n"); 99669d5fd8fSJohnny Huang break; 99769d5fd8fSJohnny Huang case 2: 99869d5fd8fSJohnny Huang printf("HMAC(SHA384)\n"); 99969d5fd8fSJohnny Huang break; 100069d5fd8fSJohnny Huang case 3: 100169d5fd8fSJohnny Huang printf("HMAC(SHA512)\n"); 100269d5fd8fSJohnny Huang break; 100369d5fd8fSJohnny Huang } 10049a4fe690SJohnny Huang } else if (key_info.key_type == OTP_KEY_TYPE_RSA) { 100569d5fd8fSJohnny Huang printf("RSA SHA Type: "); 100669d5fd8fSJohnny Huang switch (key_length) { 100769d5fd8fSJohnny Huang case 0: 100869d5fd8fSJohnny Huang printf("RSA1024\n"); 100969d5fd8fSJohnny Huang len = 0x100; 101069d5fd8fSJohnny Huang break; 101169d5fd8fSJohnny Huang case 1: 101269d5fd8fSJohnny Huang printf("RSA2048\n"); 101369d5fd8fSJohnny Huang len = 0x200; 101469d5fd8fSJohnny Huang break; 101569d5fd8fSJohnny Huang case 2: 101669d5fd8fSJohnny Huang printf("RSA3072\n"); 101769d5fd8fSJohnny Huang len = 0x300; 101869d5fd8fSJohnny Huang break; 101969d5fd8fSJohnny Huang case 3: 102069d5fd8fSJohnny Huang printf("RSA4096\n"); 102169d5fd8fSJohnny Huang len = 0x400; 102269d5fd8fSJohnny Huang break; 102369d5fd8fSJohnny Huang } 102469d5fd8fSJohnny Huang printf("RSA exponent bit length: %d\n", exp_length); 102569d5fd8fSJohnny Huang } 10269a4fe690SJohnny Huang if (key_info.need_id) 102769d5fd8fSJohnny Huang printf("Key Number ID: %d\n", key_id); 102869d5fd8fSJohnny Huang printf("Key Value:\n"); 10299a4fe690SJohnny Huang if (key_info.key_type == OTP_KEY_TYPE_HMAC) { 103069d5fd8fSJohnny Huang buf_print(&byte_buf[key_offset], 0x40); 10319a4fe690SJohnny Huang } else if (key_info.key_type == OTP_KEY_TYPE_AES) { 10329a4fe690SJohnny Huang printf("AES Key:\n"); 10339a4fe690SJohnny Huang buf_print(&byte_buf[key_offset], 0x20); 10345fdde29fSJohnny Huang if (info_cb.version == OTP_AST2600A0) { 10359a4fe690SJohnny Huang printf("AES IV:\n"); 10369a4fe690SJohnny Huang buf_print(&byte_buf[key_offset + 0x20], 0x10); 10379a4fe690SJohnny Huang } 10389a4fe690SJohnny Huang 10399a4fe690SJohnny Huang } else if (key_info.key_type == OTP_KEY_TYPE_VAULT) { 10405fdde29fSJohnny Huang if (info_cb.version == OTP_AST2600A0) { 104169d5fd8fSJohnny Huang printf("AES Key:\n"); 104269d5fd8fSJohnny Huang buf_print(&byte_buf[key_offset], 0x20); 104369d5fd8fSJohnny Huang printf("AES IV:\n"); 104469d5fd8fSJohnny Huang buf_print(&byte_buf[key_offset + 0x20], 0x10); 10455fdde29fSJohnny Huang } else { 10469a4fe690SJohnny Huang printf("AES Key 1:\n"); 10479a4fe690SJohnny Huang buf_print(&byte_buf[key_offset], 0x20); 10489a4fe690SJohnny Huang printf("AES Key 2:\n"); 10499a4fe690SJohnny Huang buf_print(&byte_buf[key_offset + 0x20], 0x20); 10509a4fe690SJohnny Huang } 105169d5fd8fSJohnny Huang 10529a4fe690SJohnny Huang } else if (key_info.key_type == OTP_KEY_TYPE_RSA) { 105369d5fd8fSJohnny Huang printf("RSA mod:\n"); 105469d5fd8fSJohnny Huang buf_print(&byte_buf[key_offset], len / 2); 105569d5fd8fSJohnny Huang printf("RSA exp:\n"); 105669d5fd8fSJohnny Huang buf_print(&byte_buf[key_offset + (len / 2)], len / 2); 105769d5fd8fSJohnny Huang } 105869d5fd8fSJohnny Huang if (last) 105969d5fd8fSJohnny Huang break; 106069d5fd8fSJohnny Huang i++; 106169d5fd8fSJohnny Huang } 106269d5fd8fSJohnny Huang return 0; 106369d5fd8fSJohnny Huang } 106469d5fd8fSJohnny Huang 10655010032bSJohnny Huang static int otp_prog_conf(struct otp_image_layout *image_layout) 106669d5fd8fSJohnny Huang { 1067a6d0d645SJohnny Huang int i, k; 1068d90825e2SJohnny Huang int pass = 0; 1069a6d0d645SJohnny Huang uint32_t prog_address; 1070bb34a7bfSJohnny Huang uint32_t data[16]; 1071a6d0d645SJohnny Huang uint32_t compare[2]; 10725010032bSJohnny Huang uint32_t *conf = (uint32_t *)image_layout->conf; 10735010032bSJohnny Huang uint32_t *conf_ignore = (uint32_t *)image_layout->conf_ignore; 1074d90825e2SJohnny Huang uint32_t data_masked; 1075d90825e2SJohnny Huang uint32_t buf_masked; 107669d5fd8fSJohnny Huang 1077a6d0d645SJohnny Huang printf("Read OTP Config Region:\n"); 1078a6d0d645SJohnny Huang 1079bb34a7bfSJohnny Huang for (i = 0; i < 16 ; i ++) { 108069d5fd8fSJohnny Huang prog_address = 0x800; 1081a6d0d645SJohnny Huang prog_address |= (i / 8) * 0x200; 1082a6d0d645SJohnny Huang prog_address |= (i % 8) * 0x2; 1083a6d0d645SJohnny Huang otp_read_data(prog_address, &data[i]); 1084a6d0d645SJohnny Huang } 1085a6d0d645SJohnny Huang 1086a6d0d645SJohnny Huang printf("Check writable...\n"); 1087bb34a7bfSJohnny Huang for (i = 0; i < 16; i++) { 10885010032bSJohnny Huang data_masked = data[i] & ~conf_ignore[i]; 10895010032bSJohnny Huang buf_masked = conf[i] & ~conf_ignore[i]; 1090d90825e2SJohnny Huang if (data_masked == buf_masked) 109169d5fd8fSJohnny Huang continue; 1092d90825e2SJohnny Huang if ((data_masked | buf_masked) == buf_masked) { 1093a6d0d645SJohnny Huang continue; 1094a6d0d645SJohnny Huang } else { 1095a6d0d645SJohnny Huang printf("Input image can't program into OTP, please check.\n"); 1096a6af4a17SJohnny Huang printf("OTPCFG[%X] = %x\n", i, data[i]); 10975010032bSJohnny Huang printf("Input [%X] = %x\n", i, conf[i]); 10985010032bSJohnny Huang printf("Mask [%X] = %x\n", i, ~conf_ignore[i]); 10992a856b9aSJohnny Huang return OTP_FAILURE; 1100a6d0d645SJohnny Huang } 1101a6d0d645SJohnny Huang } 1102a6d0d645SJohnny Huang 1103a6d0d645SJohnny Huang printf("Start Programing...\n"); 1104d90825e2SJohnny Huang otp_soak(0); 1105bb34a7bfSJohnny Huang for (i = 0; i < 16; i++) { 11065010032bSJohnny Huang data_masked = data[i] & ~conf_ignore[i]; 11075010032bSJohnny Huang buf_masked = conf[i] & ~conf_ignore[i]; 1108a6d0d645SJohnny Huang prog_address = 0x800; 1109a6d0d645SJohnny Huang prog_address |= (i / 8) * 0x200; 1110a6d0d645SJohnny Huang prog_address |= (i % 8) * 0x2; 1111bb34a7bfSJohnny Huang if (data_masked == buf_masked) { 1112bb34a7bfSJohnny Huang pass = 1; 1113a6d0d645SJohnny Huang continue; 1114bb34a7bfSJohnny Huang } 1115de6fbf1cSJohnny Huang 1116a6d0d645SJohnny Huang 1117de6fbf1cSJohnny Huang otp_soak(1); 11185010032bSJohnny Huang otp_prog_dw(conf[i], conf_ignore[i], prog_address); 1119a6d0d645SJohnny Huang 112069d5fd8fSJohnny Huang pass = 0; 112169d5fd8fSJohnny Huang for (k = 0; k < RETRY; k++) { 11225010032bSJohnny Huang if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) { 1123de6fbf1cSJohnny Huang otp_soak(2); 1124a6d0d645SJohnny Huang otp_prog_dw(compare[0], prog_address, 1); 11255010032bSJohnny Huang if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) { 1126de6fbf1cSJohnny Huang otp_soak(1); 1127de6fbf1cSJohnny Huang } else { 1128de6fbf1cSJohnny Huang pass = 1; 1129de6fbf1cSJohnny Huang break; 1130de6fbf1cSJohnny Huang } 1131a6d0d645SJohnny Huang } else { 113269d5fd8fSJohnny Huang pass = 1; 113369d5fd8fSJohnny Huang break; 113469d5fd8fSJohnny Huang } 113569d5fd8fSJohnny Huang } 1136bb34a7bfSJohnny Huang if (pass == 0) { 1137bb34a7bfSJohnny Huang printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n", 11385010032bSJohnny Huang i, data[i], conf[i], conf_ignore[i]); 1139bb34a7bfSJohnny Huang break; 1140bb34a7bfSJohnny Huang } 1141a6d0d645SJohnny Huang } 1142a6d0d645SJohnny Huang 1143de6fbf1cSJohnny Huang otp_soak(0); 114469d5fd8fSJohnny Huang if (!pass) 11452a856b9aSJohnny Huang return OTP_FAILURE; 1146a6d0d645SJohnny Huang 11472a856b9aSJohnny Huang return OTP_SUCCESS; 1148d90825e2SJohnny Huang 114969d5fd8fSJohnny Huang } 115069d5fd8fSJohnny Huang 1151eda10d61SJohnny Huang static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit) 1152eda10d61SJohnny Huang { 1153eda10d61SJohnny Huang if (ibit == 1) { 1154eda10d61SJohnny Huang return OTP_SUCCESS; 1155eda10d61SJohnny Huang } else { 1156eda10d61SJohnny Huang printf("OTPSTRAP[%X]:\n", offset); 1157eda10d61SJohnny Huang } 1158eda10d61SJohnny Huang if (bit == otpstrap->value) { 1159eda10d61SJohnny Huang printf(" The value is same as before, skip it.\n"); 1160eda10d61SJohnny Huang return OTP_PROG_SKIP; 1161eda10d61SJohnny Huang } 1162eda10d61SJohnny Huang if (otpstrap->protected == 1) { 1163eda10d61SJohnny Huang printf(" This bit is protected and is not writable\n"); 1164eda10d61SJohnny Huang return OTP_FAILURE; 1165eda10d61SJohnny Huang } 1166eda10d61SJohnny Huang if (otpstrap->remain_times == 0) { 1167eda10d61SJohnny Huang printf(" This bit is no remaining times to write.\n"); 1168eda10d61SJohnny Huang return OTP_FAILURE; 1169eda10d61SJohnny Huang } 1170eda10d61SJohnny Huang if (pbit == 1) { 1171eda10d61SJohnny Huang printf(" This bit will be protected and become non-writable.\n"); 1172eda10d61SJohnny Huang } 1173eda10d61SJohnny Huang if (rpbit == 1 && info_cb.version != OTP_AST2600A0) { 1174eda10d61SJohnny Huang printf(" The relative register will be protected.\n"); 1175eda10d61SJohnny Huang } 1176eda10d61SJohnny Huang printf(" Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", offset, otpstrap->writeable_option + 1, otpstrap->value, otpstrap->value ^ 1); 1177eda10d61SJohnny Huang return OTP_SUCCESS; 1178eda10d61SJohnny Huang } 1179eda10d61SJohnny Huang 11805010032bSJohnny Huang static int otp_strap_image_confirm(struct otp_image_layout *image_layout) 118169d5fd8fSJohnny Huang { 118269d5fd8fSJohnny Huang int i; 11835010032bSJohnny Huang uint32_t *strap; 11845010032bSJohnny Huang uint32_t *strap_ignore; 11855010032bSJohnny Huang uint32_t *strap_reg_protect; 11865010032bSJohnny Huang uint32_t *strap_pro; 1187eda10d61SJohnny Huang int bit, pbit, ibit, rpbit; 118869d5fd8fSJohnny Huang int fail = 0; 1189a6af4a17SJohnny Huang int skip = -1; 1190eda10d61SJohnny Huang int ret; 119166f2f8e5SJohnny Huang struct otpstrap_status otpstrap[64]; 119269d5fd8fSJohnny Huang 11935010032bSJohnny Huang strap = (uint32_t *)image_layout->strap; 11945010032bSJohnny Huang strap_pro = (uint32_t *)image_layout->strap_pro; 11955010032bSJohnny Huang strap_ignore = (uint32_t *)image_layout->strap_ignore; 11965010032bSJohnny Huang strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro; 11975010032bSJohnny Huang 1198541eb887SJohnny Huang otp_strap_status(otpstrap); 119969d5fd8fSJohnny Huang for (i = 0; i < 64; i++) { 120069d5fd8fSJohnny Huang if (i < 32) { 12015010032bSJohnny Huang bit = (strap[0] >> i) & 0x1; 1202eda10d61SJohnny Huang ibit = (strap_ignore[0] >> i) & 0x1; 12035010032bSJohnny Huang pbit = (strap_pro[0] >> i) & 0x1; 120469d5fd8fSJohnny Huang } else { 12055010032bSJohnny Huang bit = (strap[1] >> (i - 32)) & 0x1; 1206eda10d61SJohnny Huang ibit = (strap_ignore[1] >> (i - 32)) & 0x1; 12075010032bSJohnny Huang pbit = (strap_pro[1] >> (i - 32)) & 0x1; 12085010032bSJohnny Huang } 12095010032bSJohnny Huang 12105010032bSJohnny Huang if (info_cb.version != OTP_AST2600A0) { 12115010032bSJohnny Huang if (i < 32) { 12125010032bSJohnny Huang rpbit = (strap_reg_protect[0] >> i) & 0x1; 12135010032bSJohnny Huang } else { 12145010032bSJohnny Huang rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1; 12155010032bSJohnny Huang } 12165010032bSJohnny Huang } else { 12175010032bSJohnny Huang rpbit = 0; 121869d5fd8fSJohnny Huang } 1219eda10d61SJohnny Huang ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit); 1220eda10d61SJohnny Huang if (ret == OTP_PROG_SKIP) { 1221a6af4a17SJohnny Huang if (skip == -1) 1222a6af4a17SJohnny Huang skip = 1; 122369d5fd8fSJohnny Huang continue; 1224a6af4a17SJohnny Huang } else { 1225eda10d61SJohnny Huang skip = 1; 122669d5fd8fSJohnny Huang } 1227eda10d61SJohnny Huang 1228eda10d61SJohnny Huang if (ret == OTP_FAILURE) 122969d5fd8fSJohnny Huang fail = 1; 123069d5fd8fSJohnny Huang } 123169d5fd8fSJohnny Huang if (fail == 1) 1232a6af4a17SJohnny Huang return OTP_FAILURE; 1233a6af4a17SJohnny Huang else if (skip == 1) 1234a6af4a17SJohnny Huang return OTP_PROG_SKIP; 12357e22f42dSJohnny Huang 1236eda10d61SJohnny Huang return OTP_SUCCESS; 123769d5fd8fSJohnny Huang } 123869d5fd8fSJohnny Huang 12392a856b9aSJohnny Huang static int otp_print_strap(int start, int count) 124069d5fd8fSJohnny Huang { 124169d5fd8fSJohnny Huang int i, j; 1242de6b0cc4SJohnny Huang int remains; 124366f2f8e5SJohnny Huang struct otpstrap_status otpstrap[64]; 124469d5fd8fSJohnny Huang 12452a856b9aSJohnny Huang if (start < 0 || start > 64) 12462a856b9aSJohnny Huang return OTP_USAGE; 12472a856b9aSJohnny Huang 12482a856b9aSJohnny Huang if ((start + count) < 0 || (start + count) > 64) 12492a856b9aSJohnny Huang return OTP_USAGE; 12502a856b9aSJohnny Huang 1251541eb887SJohnny Huang otp_strap_status(otpstrap); 125269d5fd8fSJohnny Huang 1253de6b0cc4SJohnny Huang if (info_cb.version == OTP_AST2600A0) { 1254de6b0cc4SJohnny Huang remains = 7; 125507baa4e8SJohnny Huang printf("BIT(hex) Value Option Status\n"); 1256de6b0cc4SJohnny Huang } else { 1257de6b0cc4SJohnny Huang remains = 6; 1258de6b0cc4SJohnny Huang printf("BIT(hex) Value Option Reg_Protect Status\n"); 1259de6b0cc4SJohnny Huang } 1260de6b0cc4SJohnny Huang printf("______________________________________________________________________________\n"); 1261737ed20bSJohnny Huang 1262cd1610b4SJohnny Huang for (i = start; i < start + count; i++) { 126307baa4e8SJohnny Huang printf("0x%-8X", i); 1264737ed20bSJohnny Huang printf("%-7d", otpstrap[i].value); 1265de6b0cc4SJohnny Huang for (j = 0; j < remains; j++) 1266737ed20bSJohnny Huang printf("%d ", otpstrap[i].option_array[j]); 1267737ed20bSJohnny Huang printf(" "); 1268de6b0cc4SJohnny Huang if (info_cb.version != OTP_AST2600A0) { 1269de6b0cc4SJohnny Huang printf("%d ", otpstrap[i].reg_protected); 1270de6b0cc4SJohnny Huang } 127169d5fd8fSJohnny Huang if (otpstrap[i].protected == 1) { 1272737ed20bSJohnny Huang printf("protected and not writable"); 127369d5fd8fSJohnny Huang } else { 1274737ed20bSJohnny Huang printf("not protected "); 127569d5fd8fSJohnny Huang if (otpstrap[i].remain_times == 0) { 1276737ed20bSJohnny Huang printf("and no remaining times to write."); 127769d5fd8fSJohnny Huang } else { 1278737ed20bSJohnny Huang printf("and still can write %d times", otpstrap[i].remain_times); 127969d5fd8fSJohnny Huang } 128069d5fd8fSJohnny Huang } 1281737ed20bSJohnny Huang printf("\n"); 128269d5fd8fSJohnny Huang } 12832a856b9aSJohnny Huang 12842a856b9aSJohnny Huang return OTP_SUCCESS; 128569d5fd8fSJohnny Huang } 128669d5fd8fSJohnny Huang 12878848d5dcSJohnny Huang static int otp_prog_strap_bit(int bit_offset, int value) 12888848d5dcSJohnny Huang { 12898848d5dcSJohnny Huang struct otpstrap_status otpstrap[64]; 129083655e91SJohnny Huang uint32_t prog_address; 12918848d5dcSJohnny Huang int offset; 12928848d5dcSJohnny Huang int ret; 12938848d5dcSJohnny Huang 12948848d5dcSJohnny Huang 12958848d5dcSJohnny Huang otp_strap_status(otpstrap); 12968848d5dcSJohnny Huang 12978848d5dcSJohnny Huang ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0); 12988848d5dcSJohnny Huang 12998848d5dcSJohnny Huang if (ret != OTP_SUCCESS) { 13008848d5dcSJohnny Huang return ret; 13018848d5dcSJohnny Huang } 13028848d5dcSJohnny Huang 13038848d5dcSJohnny Huang prog_address = 0x800; 13048848d5dcSJohnny Huang if (bit_offset < 32) { 13058848d5dcSJohnny Huang offset = bit_offset; 13068848d5dcSJohnny Huang prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200; 13078848d5dcSJohnny Huang prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2; 13088848d5dcSJohnny Huang 13098848d5dcSJohnny Huang } else { 13108848d5dcSJohnny Huang offset = (bit_offset - 32); 13118848d5dcSJohnny Huang prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200; 13128848d5dcSJohnny Huang prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2; 13138848d5dcSJohnny Huang } 13148848d5dcSJohnny Huang 13158848d5dcSJohnny Huang 131683655e91SJohnny Huang return otp_prog_bit(1, prog_address, offset); 13178848d5dcSJohnny Huang } 13188848d5dcSJohnny Huang 13195010032bSJohnny Huang static int otp_prog_strap(struct otp_image_layout *image_layout) 132069d5fd8fSJohnny Huang { 13215010032bSJohnny Huang uint32_t *strap; 13225010032bSJohnny Huang uint32_t *strap_ignore; 13235010032bSJohnny Huang uint32_t *strap_pro; 13245010032bSJohnny Huang uint32_t *strap_reg_protect; 132583655e91SJohnny Huang uint32_t prog_address; 132683655e91SJohnny Huang int i; 1327eda10d61SJohnny Huang int bit, pbit, ibit, offset, rpbit; 132869d5fd8fSJohnny Huang int fail = 0; 132983655e91SJohnny Huang int ret; 133066f2f8e5SJohnny Huang struct otpstrap_status otpstrap[64]; 133169d5fd8fSJohnny Huang 13325010032bSJohnny Huang strap = (uint32_t *)image_layout->strap; 13335010032bSJohnny Huang strap_pro = (uint32_t *)image_layout->strap_pro; 13345010032bSJohnny Huang strap_ignore = (uint32_t *)image_layout->strap_ignore; 13355010032bSJohnny Huang strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro; 13365010032bSJohnny Huang 13377f795e57SJohnny Huang printf("Read OTP Strap Region:\n"); 1338541eb887SJohnny Huang otp_strap_status(otpstrap); 133969d5fd8fSJohnny Huang 13407f795e57SJohnny Huang printf("Check writable...\n"); 13415010032bSJohnny Huang if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) { 13427f795e57SJohnny Huang printf("Input image can't program into OTP, please check.\n"); 13437f795e57SJohnny Huang return OTP_FAILURE; 13447f795e57SJohnny Huang } 13457e22f42dSJohnny Huang 134669d5fd8fSJohnny Huang for (i = 0; i < 64; i++) { 134769d5fd8fSJohnny Huang prog_address = 0x800; 134869d5fd8fSJohnny Huang if (i < 32) { 134969d5fd8fSJohnny Huang offset = i; 13505010032bSJohnny Huang bit = (strap[0] >> offset) & 0x1; 1351eda10d61SJohnny Huang ibit = (strap_ignore[0] >> offset) & 0x1; 13525010032bSJohnny Huang pbit = (strap_pro[0] >> offset) & 0x1; 135369d5fd8fSJohnny Huang prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200; 135469d5fd8fSJohnny Huang prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2; 135569d5fd8fSJohnny Huang 135669d5fd8fSJohnny Huang } else { 135769d5fd8fSJohnny Huang offset = (i - 32); 13585010032bSJohnny Huang bit = (strap[1] >> offset) & 0x1; 1359eda10d61SJohnny Huang ibit = (strap_ignore[1] >> offset) & 0x1; 13605010032bSJohnny Huang pbit = (strap_pro[1] >> offset) & 0x1; 136169d5fd8fSJohnny Huang prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200; 136269d5fd8fSJohnny Huang prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2; 136369d5fd8fSJohnny Huang } 13645010032bSJohnny Huang if (info_cb.version != OTP_AST2600A0) { 13655010032bSJohnny Huang if (i < 32) { 13665010032bSJohnny Huang rpbit = (strap_reg_protect[0] >> i) & 0x1; 13675010032bSJohnny Huang } else { 13685010032bSJohnny Huang rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1; 13695010032bSJohnny Huang } 13705010032bSJohnny Huang } else { 13715010032bSJohnny Huang rpbit = 0; 13725010032bSJohnny Huang } 137369d5fd8fSJohnny Huang 1374eda10d61SJohnny Huang if (ibit == 1) { 137569d5fd8fSJohnny Huang continue; 137669d5fd8fSJohnny Huang } 137769d5fd8fSJohnny Huang if (bit == otpstrap[i].value) { 137869d5fd8fSJohnny Huang continue; 137969d5fd8fSJohnny Huang } 138069d5fd8fSJohnny Huang if (otpstrap[i].protected == 1) { 138169d5fd8fSJohnny Huang fail = 1; 138269d5fd8fSJohnny Huang continue; 138369d5fd8fSJohnny Huang } 138469d5fd8fSJohnny Huang if (otpstrap[i].remain_times == 0) { 138569d5fd8fSJohnny Huang fail = 1; 138669d5fd8fSJohnny Huang continue; 138769d5fd8fSJohnny Huang } 13887e22f42dSJohnny Huang 138983655e91SJohnny Huang ret = otp_prog_bit(1, prog_address, offset); 139083655e91SJohnny Huang if (!ret) 13912a856b9aSJohnny Huang return OTP_FAILURE; 139269d5fd8fSJohnny Huang 13935010032bSJohnny Huang if (rpbit == 1 && info_cb.version != OTP_AST2600A0) { 139469d5fd8fSJohnny Huang prog_address = 0x800; 139569d5fd8fSJohnny Huang if (i < 32) 13965010032bSJohnny Huang prog_address |= 0x608; 139769d5fd8fSJohnny Huang else 13985010032bSJohnny Huang prog_address |= 0x60a; 13997e22f42dSJohnny Huang 140083655e91SJohnny Huang ret = otp_prog_bit(1, prog_address, offset); 140183655e91SJohnny Huang if (!ret) 14022a856b9aSJohnny Huang return OTP_FAILURE; 14035010032bSJohnny Huang } 14045010032bSJohnny Huang 14055010032bSJohnny Huang if (pbit != 0) { 14065010032bSJohnny Huang prog_address = 0x800; 14075010032bSJohnny Huang if (i < 32) 14085010032bSJohnny Huang prog_address |= 0x60c; 14095010032bSJohnny Huang else 14105010032bSJohnny Huang prog_address |= 0x60e; 14115010032bSJohnny Huang 141283655e91SJohnny Huang ret = otp_prog_bit(1, prog_address, offset); 141383655e91SJohnny Huang if (!ret) 14145010032bSJohnny Huang return OTP_FAILURE; 14155010032bSJohnny Huang } 141669d5fd8fSJohnny Huang 141769d5fd8fSJohnny Huang } 1418de6fbf1cSJohnny Huang otp_soak(0); 141969d5fd8fSJohnny Huang if (fail == 1) 14202a856b9aSJohnny Huang return OTP_FAILURE; 142169d5fd8fSJohnny Huang else 14222a856b9aSJohnny Huang return OTP_SUCCESS; 142369d5fd8fSJohnny Huang 142469d5fd8fSJohnny Huang } 142569d5fd8fSJohnny Huang 14265010032bSJohnny Huang static int otp_prog_data(struct otp_image_layout *image_layout) 14274c1c9b35SJohnny Huang { 142854552c69SJohnny Huang int i; 142954552c69SJohnny Huang int ret; 14305010032bSJohnny Huang int data_dw; 1431d90825e2SJohnny Huang uint32_t data[2048]; 14325010032bSJohnny Huang uint32_t *buf; 14335010032bSJohnny Huang uint32_t *buf_ignore; 14344c1c9b35SJohnny Huang 143554552c69SJohnny Huang uint32_t data_masked; 143654552c69SJohnny Huang uint32_t buf_masked; 14374c1c9b35SJohnny Huang 14385010032bSJohnny Huang buf = (uint32_t *)image_layout->data; 14395010032bSJohnny Huang buf_ignore = (uint32_t *)image_layout->data_ignore; 14405010032bSJohnny Huang 14415010032bSJohnny Huang data_dw = image_layout->data_length / 4; 14425010032bSJohnny Huang 14434c1c9b35SJohnny Huang printf("Read OTP Data:\n"); 14444c1c9b35SJohnny Huang 14455010032bSJohnny Huang for (i = 0; i < data_dw - 2 ; i += 2) { 1446d90825e2SJohnny Huang otp_read_data(i, &data[i]); 14474c1c9b35SJohnny Huang } 1448d90825e2SJohnny Huang 14494c1c9b35SJohnny Huang printf("Check writable...\n"); 145054552c69SJohnny Huang // ignore last two dw, the last two dw is used for slt otp write check. 14515010032bSJohnny Huang for (i = 0; i < data_dw - 2; i++) { 1452696656c6SJohnny Huang data_masked = data[i] & ~buf_ignore[i]; 1453696656c6SJohnny Huang buf_masked = buf[i] & ~buf_ignore[i]; 145454552c69SJohnny Huang if (data_masked == buf_masked) 14554c1c9b35SJohnny Huang continue; 1456d90825e2SJohnny Huang if (i % 2 == 0) { 145754552c69SJohnny Huang if ((data_masked | buf_masked) == buf_masked) { 14584c1c9b35SJohnny Huang continue; 14594c1c9b35SJohnny Huang } else { 14604c1c9b35SJohnny Huang printf("Input image can't program into OTP, please check.\n"); 1461d90825e2SJohnny Huang printf("OTP_ADDR[%x] = %x\n", i, data[i]); 14624c1c9b35SJohnny Huang printf("Input [%x] = %x\n", i, buf[i]); 1463696656c6SJohnny Huang printf("Mask [%x] = %x\n", i, ~buf_ignore[i]); 14642a856b9aSJohnny Huang return OTP_FAILURE; 146569d5fd8fSJohnny Huang } 1466d90825e2SJohnny Huang } else { 146754552c69SJohnny Huang if ((data_masked & buf_masked) == buf_masked) { 1468d90825e2SJohnny Huang continue; 1469d90825e2SJohnny Huang } else { 1470d90825e2SJohnny Huang printf("Input image can't program into OTP, please check.\n"); 1471d90825e2SJohnny Huang printf("OTP_ADDR[%x] = %x\n", i, data[i]); 1472d90825e2SJohnny Huang printf("Input [%x] = %x\n", i, buf[i]); 1473696656c6SJohnny Huang printf("Mask [%x] = %x\n", i, ~buf_ignore[i]); 14742a856b9aSJohnny Huang return OTP_FAILURE; 1475d90825e2SJohnny Huang } 1476d90825e2SJohnny Huang } 1477d90825e2SJohnny Huang } 147869d5fd8fSJohnny Huang 1479d90825e2SJohnny Huang printf("Start Programing...\n"); 1480d90825e2SJohnny Huang 148154552c69SJohnny Huang // programing ecc region first 148254552c69SJohnny Huang for (i = 1792; i < 2046; i += 2) { 1483696656c6SJohnny Huang ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i); 148454552c69SJohnny Huang if (ret != OTP_SUCCESS) { 148554552c69SJohnny Huang printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n", 1486696656c6SJohnny Huang i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]); 148754552c69SJohnny Huang return ret; 1488d90825e2SJohnny Huang } 1489d90825e2SJohnny Huang } 1490d90825e2SJohnny Huang 149154552c69SJohnny Huang for (i = 0; i < 1792; i += 2) { 1492696656c6SJohnny Huang ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i); 149354552c69SJohnny Huang if (ret != OTP_SUCCESS) { 149454552c69SJohnny Huang printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n", 1495696656c6SJohnny Huang i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]); 149654552c69SJohnny Huang return ret; 1497d90825e2SJohnny Huang } 1498de6fbf1cSJohnny Huang } 1499de6fbf1cSJohnny Huang otp_soak(0); 15002a856b9aSJohnny Huang return OTP_SUCCESS; 1501d90825e2SJohnny Huang 1502d90825e2SJohnny Huang } 1503d90825e2SJohnny Huang 1504696656c6SJohnny Huang static int otp_image_verify(uint8_t *src_buf, uint32_t length, uint8_t *digest_buf) 1505696656c6SJohnny Huang { 1506696656c6SJohnny Huang sha256_context ctx; 1507696656c6SJohnny Huang u8 digest_ret[CHECKSUM_LEN]; 1508696656c6SJohnny Huang 1509696656c6SJohnny Huang sha256_starts(&ctx); 1510696656c6SJohnny Huang sha256_update(&ctx, src_buf, length); 1511696656c6SJohnny Huang sha256_finish(&ctx, digest_ret); 1512696656c6SJohnny Huang 1513696656c6SJohnny Huang if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN)) 1514696656c6SJohnny Huang return 0; 1515696656c6SJohnny Huang else 1516696656c6SJohnny Huang return -1; 1517696656c6SJohnny Huang 1518696656c6SJohnny Huang } 1519696656c6SJohnny Huang 1520de6b0cc4SJohnny Huang static int do_otp_prog(int addr, int nconfirm) 152169d5fd8fSJohnny Huang { 152269d5fd8fSJohnny Huang int ret; 15239a4fe690SJohnny Huang int image_version = 0; 1524696656c6SJohnny Huang struct otp_header *otp_header; 1525696656c6SJohnny Huang struct otp_image_layout image_layout; 1526696656c6SJohnny Huang int image_size; 1527696656c6SJohnny Huang uint8_t *buf; 1528696656c6SJohnny Huang uint8_t *checksum; 152969d5fd8fSJohnny Huang 1530696656c6SJohnny Huang otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK); 1531696656c6SJohnny Huang if (!otp_header) { 153269d5fd8fSJohnny Huang puts("Failed to map physical memory\n"); 15332a856b9aSJohnny Huang return OTP_FAILURE; 153469d5fd8fSJohnny Huang } 1535d90825e2SJohnny Huang 1536696656c6SJohnny Huang image_size = OTP_IMAGE_SIZE(otp_header->image_info); 1537696656c6SJohnny Huang unmap_physmem(otp_header, MAP_WRBACK); 1538696656c6SJohnny Huang 1539696656c6SJohnny Huang buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK); 1540696656c6SJohnny Huang 1541696656c6SJohnny Huang if (!buf) { 1542696656c6SJohnny Huang puts("Failed to map physical memory\n"); 1543696656c6SJohnny Huang return OTP_FAILURE; 1544696656c6SJohnny Huang } 1545696656c6SJohnny Huang otp_header = (struct otp_header *) buf; 1546696656c6SJohnny Huang checksum = buf + otp_header->checksum_offset; 1547696656c6SJohnny Huang 1548696656c6SJohnny Huang if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) { 1549696656c6SJohnny Huang puts("Image is invalid\n"); 1550696656c6SJohnny Huang return OTP_FAILURE; 1551696656c6SJohnny Huang } 1552696656c6SJohnny Huang 1553696656c6SJohnny Huang 15545010032bSJohnny Huang image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2); 15555010032bSJohnny Huang image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info); 15565010032bSJohnny Huang image_layout.data_ignore = image_layout.data + image_layout.data_length; 15575010032bSJohnny Huang 15585010032bSJohnny Huang image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2); 1559696656c6SJohnny Huang image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info); 15605010032bSJohnny Huang image_layout.conf_ignore = image_layout.conf + image_layout.conf_length; 1561696656c6SJohnny Huang 1562696656c6SJohnny Huang image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info); 1563696656c6SJohnny Huang 1564696656c6SJohnny Huang if (!strcmp("A0", (char *)otp_header->otp_version)) { 1565696656c6SJohnny Huang image_version = OTP_AST2600A0; 15665010032bSJohnny Huang image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3); 15675010032bSJohnny Huang image_layout.strap_pro = image_layout.strap + image_layout.strap_length; 15685010032bSJohnny Huang image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length; 1569696656c6SJohnny Huang } else if (!strcmp("A1", (char *)otp_header->otp_version)) { 1570696656c6SJohnny Huang image_version = OTP_AST2600A1; 15715010032bSJohnny Huang image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4); 15725010032bSJohnny Huang image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length; 15735010032bSJohnny Huang image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length; 15745010032bSJohnny Huang image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length; 15755fdde29fSJohnny Huang } else if (!strcmp("A2", (char *)otp_header->otp_version)) { 15765fdde29fSJohnny Huang image_version = OTP_AST2600A2; 15775fdde29fSJohnny Huang image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4); 15785fdde29fSJohnny Huang image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length; 15795fdde29fSJohnny Huang image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length; 15805fdde29fSJohnny Huang image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length; 1581696656c6SJohnny Huang } else { 1582696656c6SJohnny Huang puts("Version is not supported\n"); 1583696656c6SJohnny Huang return OTP_FAILURE; 1584696656c6SJohnny Huang } 1585696656c6SJohnny Huang 15869a4fe690SJohnny Huang if (image_version != info_cb.version) { 15879a4fe690SJohnny Huang puts("Version is not match\n"); 15889a4fe690SJohnny Huang return OTP_FAILURE; 15899a4fe690SJohnny Huang } 15909a4fe690SJohnny Huang 1591696656c6SJohnny Huang if (otp_image_verify(buf, image_size, checksum)) { 1592696656c6SJohnny Huang puts("checksum is invalid\n"); 1593696656c6SJohnny Huang return OTP_FAILURE; 1594d90825e2SJohnny Huang } 15957332532cSJohnny Huang 159669d5fd8fSJohnny Huang if (!nconfirm) { 1597696656c6SJohnny Huang if (otp_header->image_info & OTP_INC_DATA) { 15987f795e57SJohnny Huang printf("\nOTP data region :\n"); 1599696656c6SJohnny Huang if (otp_print_data_info(&image_layout) < 0) { 160069d5fd8fSJohnny Huang printf("OTP data error, please check.\n"); 16012a856b9aSJohnny Huang return OTP_FAILURE; 160269d5fd8fSJohnny Huang } 160369d5fd8fSJohnny Huang } 1604696656c6SJohnny Huang if (otp_header->image_info & OTP_INC_STRAP) { 16057332532cSJohnny Huang printf("\nOTP strap region :\n"); 16065010032bSJohnny Huang if (otp_print_strap_image(&image_layout) < 0) { 16077332532cSJohnny Huang printf("OTP strap error, please check.\n"); 16087332532cSJohnny Huang return OTP_FAILURE; 16097332532cSJohnny Huang } 16107332532cSJohnny Huang } 1611696656c6SJohnny Huang if (otp_header->image_info & OTP_INC_CONFIG) { 16127332532cSJohnny Huang printf("\nOTP configuration region :\n"); 1613696656c6SJohnny Huang if (otp_print_conf_image(&image_layout) < 0) { 16147332532cSJohnny Huang printf("OTP config error, please check.\n"); 16157332532cSJohnny Huang return OTP_FAILURE; 16167332532cSJohnny Huang } 16177332532cSJohnny Huang } 16187332532cSJohnny Huang 161969d5fd8fSJohnny Huang printf("type \"YES\" (no quotes) to continue:\n"); 162069d5fd8fSJohnny Huang if (!confirm_yesno()) { 162169d5fd8fSJohnny Huang printf(" Aborting\n"); 16222a856b9aSJohnny Huang return OTP_FAILURE; 162369d5fd8fSJohnny Huang } 162469d5fd8fSJohnny Huang } 16257332532cSJohnny Huang 16265010032bSJohnny Huang if (otp_header->image_info & OTP_INC_DATA) { 16275010032bSJohnny Huang printf("programing data region ...\n"); 16285010032bSJohnny Huang ret = otp_prog_data(&image_layout); 16295010032bSJohnny Huang if (ret != 0) { 16305010032bSJohnny Huang printf("Error\n"); 16315010032bSJohnny Huang return ret; 16325010032bSJohnny Huang } else { 16335010032bSJohnny Huang printf("Done\n"); 16345010032bSJohnny Huang } 16355010032bSJohnny Huang } 16365010032bSJohnny Huang if (otp_header->image_info & OTP_INC_STRAP) { 16375010032bSJohnny Huang printf("programing strap region ...\n"); 16385010032bSJohnny Huang ret = otp_prog_strap(&image_layout); 16395010032bSJohnny Huang if (ret != 0) { 16405010032bSJohnny Huang printf("Error\n"); 16415010032bSJohnny Huang return ret; 16425010032bSJohnny Huang } else { 16435010032bSJohnny Huang printf("Done\n"); 16445010032bSJohnny Huang } 16455010032bSJohnny Huang } 16465010032bSJohnny Huang if (otp_header->image_info & OTP_INC_CONFIG) { 16475010032bSJohnny Huang printf("programing configuration region ...\n"); 16485010032bSJohnny Huang ret = otp_prog_conf(&image_layout); 16495010032bSJohnny Huang if (ret != 0) { 16505010032bSJohnny Huang printf("Error\n"); 16515010032bSJohnny Huang return ret; 16525010032bSJohnny Huang } 16535010032bSJohnny Huang printf("Done\n"); 16545010032bSJohnny Huang } 1655cd1610b4SJohnny Huang 16567332532cSJohnny Huang return OTP_SUCCESS; 16572a856b9aSJohnny Huang } 16582a856b9aSJohnny Huang 16592a856b9aSJohnny Huang static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm) 1660cd1610b4SJohnny Huang { 1661a6af4a17SJohnny Huang uint32_t read[2]; 1662d90825e2SJohnny Huang uint32_t prog_address = 0; 166366f2f8e5SJohnny Huang struct otpstrap_status otpstrap[64]; 1664cd1610b4SJohnny Huang int otp_bit; 166583655e91SJohnny Huang int ret = 0; 1666cd1610b4SJohnny Huang 1667cd1610b4SJohnny Huang switch (mode) { 1668a6d0d645SJohnny Huang case OTP_REGION_CONF: 1669a6af4a17SJohnny Huang otp_read_config(otp_dw_offset, read); 1670cd1610b4SJohnny Huang prog_address = 0x800; 1671cd1610b4SJohnny Huang prog_address |= (otp_dw_offset / 8) * 0x200; 1672cd1610b4SJohnny Huang prog_address |= (otp_dw_offset % 8) * 0x2; 1673a6af4a17SJohnny Huang otp_bit = (read[0] >> bit_offset) & 0x1; 1674cd1610b4SJohnny Huang if (otp_bit == value) { 1675a6af4a17SJohnny Huang printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value); 1676cd1610b4SJohnny Huang printf("No need to program\n"); 16772a856b9aSJohnny Huang return OTP_SUCCESS; 1678cd1610b4SJohnny Huang } 1679cd1610b4SJohnny Huang if (otp_bit == 1 && value == 0) { 1680a6af4a17SJohnny Huang printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset); 1681cd1610b4SJohnny Huang printf("OTP is programed, which can't be clean\n"); 16822a856b9aSJohnny Huang return OTP_FAILURE; 1683cd1610b4SJohnny Huang } 1684a6af4a17SJohnny Huang printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset); 1685cd1610b4SJohnny Huang break; 1686a6d0d645SJohnny Huang case OTP_REGION_DATA: 1687cd1610b4SJohnny Huang prog_address = otp_dw_offset; 1688cd1610b4SJohnny Huang 1689cd1610b4SJohnny Huang if (otp_dw_offset % 2 == 0) { 1690a6af4a17SJohnny Huang otp_read_data(otp_dw_offset, read); 1691a6af4a17SJohnny Huang otp_bit = (read[0] >> bit_offset) & 0x1; 1692643b9cfdSJohnny Huang 1693643b9cfdSJohnny Huang if (otp_bit == 1 && value == 0) { 1694643b9cfdSJohnny Huang printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset); 1695643b9cfdSJohnny Huang printf("OTP is programed, which can't be cleaned\n"); 1696643b9cfdSJohnny Huang return OTP_FAILURE; 1697643b9cfdSJohnny Huang } 1698cd1610b4SJohnny Huang } else { 1699a6af4a17SJohnny Huang otp_read_data(otp_dw_offset - 1, read); 1700a6af4a17SJohnny Huang otp_bit = (read[1] >> bit_offset) & 0x1; 1701643b9cfdSJohnny Huang 1702643b9cfdSJohnny Huang if (otp_bit == 0 && value == 1) { 1703643b9cfdSJohnny Huang printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset); 1704643b9cfdSJohnny Huang printf("OTP is programed, which can't be writen\n"); 1705643b9cfdSJohnny Huang return OTP_FAILURE; 1706643b9cfdSJohnny Huang } 1707cd1610b4SJohnny Huang } 1708cd1610b4SJohnny Huang if (otp_bit == value) { 1709a6af4a17SJohnny Huang printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value); 1710cd1610b4SJohnny Huang printf("No need to program\n"); 17112a856b9aSJohnny Huang return OTP_SUCCESS; 1712cd1610b4SJohnny Huang } 1713643b9cfdSJohnny Huang 1714a6af4a17SJohnny Huang printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset); 1715cd1610b4SJohnny Huang break; 1716a6d0d645SJohnny Huang case OTP_REGION_STRAP: 17178848d5dcSJohnny Huang otp_strap_status(otpstrap); 17188848d5dcSJohnny Huang otp_print_strap(bit_offset, 1); 17198848d5dcSJohnny Huang ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0); 17208848d5dcSJohnny Huang if (ret == OTP_FAILURE) 17218848d5dcSJohnny Huang return OTP_FAILURE; 17228848d5dcSJohnny Huang else if (ret == OTP_PROG_SKIP) 17238848d5dcSJohnny Huang return OTP_SUCCESS; 1724a6af4a17SJohnny Huang 1725cd1610b4SJohnny Huang break; 1726cd1610b4SJohnny Huang } 1727cd1610b4SJohnny Huang 1728cd1610b4SJohnny Huang if (!nconfirm) { 1729cd1610b4SJohnny Huang printf("type \"YES\" (no quotes) to continue:\n"); 1730cd1610b4SJohnny Huang if (!confirm_yesno()) { 1731cd1610b4SJohnny Huang printf(" Aborting\n"); 17322a856b9aSJohnny Huang return OTP_FAILURE; 1733cd1610b4SJohnny Huang } 1734cd1610b4SJohnny Huang } 1735cd1610b4SJohnny Huang 1736cd1610b4SJohnny Huang switch (mode) { 1737a6d0d645SJohnny Huang case OTP_REGION_STRAP: 173883655e91SJohnny Huang ret = otp_prog_strap_bit(bit_offset, value); 173983655e91SJohnny Huang break; 1740a6d0d645SJohnny Huang case OTP_REGION_CONF: 1741a6d0d645SJohnny Huang case OTP_REGION_DATA: 174283655e91SJohnny Huang ret = otp_prog_bit(value, prog_address, bit_offset); 1743de6fbf1cSJohnny Huang break; 1744de6fbf1cSJohnny Huang } 1745de6fbf1cSJohnny Huang otp_soak(0); 174683655e91SJohnny Huang if (ret) { 17479009c25dSJohnny Huang printf("SUCCESS\n"); 17482a856b9aSJohnny Huang return OTP_SUCCESS; 17499009c25dSJohnny Huang } else { 17509009c25dSJohnny Huang printf("OTP cannot be programed\n"); 17519009c25dSJohnny Huang printf("FAILED\n"); 17529009c25dSJohnny Huang return OTP_FAILURE; 17539009c25dSJohnny Huang } 1754cd1610b4SJohnny Huang 17552a856b9aSJohnny Huang return OTP_USAGE; 1756cd1610b4SJohnny Huang } 1757cd1610b4SJohnny Huang 17582a856b9aSJohnny Huang static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 175969d5fd8fSJohnny Huang { 17602a856b9aSJohnny Huang uint32_t offset, count; 17612a856b9aSJohnny Huang int ret; 176269d5fd8fSJohnny Huang 17632a856b9aSJohnny Huang if (argc == 4) { 17642a856b9aSJohnny Huang offset = simple_strtoul(argv[2], NULL, 16); 17652a856b9aSJohnny Huang count = simple_strtoul(argv[3], NULL, 16); 17662a856b9aSJohnny Huang } else if (argc == 3) { 17672a856b9aSJohnny Huang offset = simple_strtoul(argv[2], NULL, 16); 17682a856b9aSJohnny Huang count = 1; 17692a856b9aSJohnny Huang } else { 177069d5fd8fSJohnny Huang return CMD_RET_USAGE; 177169d5fd8fSJohnny Huang } 177269d5fd8fSJohnny Huang 177369d5fd8fSJohnny Huang 17742a856b9aSJohnny Huang if (!strcmp(argv[1], "conf")) { 17753d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 17762a856b9aSJohnny Huang ret = otp_print_config(offset, count); 17772a856b9aSJohnny Huang } else if (!strcmp(argv[1], "data")) { 17783d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 17792a856b9aSJohnny Huang ret = otp_print_data(offset, count); 17802a856b9aSJohnny Huang } else if (!strcmp(argv[1], "strap")) { 17813d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 17822a856b9aSJohnny Huang ret = otp_print_strap(offset, count); 17832a856b9aSJohnny Huang } else { 17842a856b9aSJohnny Huang return CMD_RET_USAGE; 178569d5fd8fSJohnny Huang } 178669d5fd8fSJohnny Huang 17872a856b9aSJohnny Huang if (ret == OTP_SUCCESS) 17882a856b9aSJohnny Huang return CMD_RET_SUCCESS; 17892a856b9aSJohnny Huang else 17902a856b9aSJohnny Huang return CMD_RET_USAGE; 17912a856b9aSJohnny Huang 17922a856b9aSJohnny Huang } 17932a856b9aSJohnny Huang 17942a856b9aSJohnny Huang static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 17952a856b9aSJohnny Huang { 17962a856b9aSJohnny Huang phys_addr_t addr; 17972a856b9aSJohnny Huang int ret; 17982a856b9aSJohnny Huang 1799de6b0cc4SJohnny Huang if (argc == 3) { 1800ed071a2bSJohnny Huang if (strcmp(argv[1], "o")) 18012a856b9aSJohnny Huang return CMD_RET_USAGE; 18022a856b9aSJohnny Huang addr = simple_strtoul(argv[2], NULL, 16); 18033d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1804de6b0cc4SJohnny Huang ret = do_otp_prog(addr, 1); 1805de6b0cc4SJohnny Huang } else if (argc == 2) { 18062a856b9aSJohnny Huang addr = simple_strtoul(argv[1], NULL, 16); 18073d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1808de6b0cc4SJohnny Huang ret = do_otp_prog(addr, 0); 18092a856b9aSJohnny Huang } else { 18102a856b9aSJohnny Huang return CMD_RET_USAGE; 18112a856b9aSJohnny Huang } 18122a856b9aSJohnny Huang 18132a856b9aSJohnny Huang if (ret == OTP_SUCCESS) 18142a856b9aSJohnny Huang return CMD_RET_SUCCESS; 18152a856b9aSJohnny Huang else if (ret == OTP_FAILURE) 18162a856b9aSJohnny Huang return CMD_RET_FAILURE; 18172a856b9aSJohnny Huang else 18182a856b9aSJohnny Huang return CMD_RET_USAGE; 18192a856b9aSJohnny Huang } 18202a856b9aSJohnny Huang 18212a856b9aSJohnny Huang static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 18222a856b9aSJohnny Huang { 18232a856b9aSJohnny Huang int mode = 0; 18242a856b9aSJohnny Huang int nconfirm = 0; 18252a856b9aSJohnny Huang int otp_addr = 0; 18262a856b9aSJohnny Huang int bit_offset; 18272a856b9aSJohnny Huang int value; 18282a856b9aSJohnny Huang int ret; 18292a856b9aSJohnny Huang 18302a856b9aSJohnny Huang if (argc != 4 && argc != 5 && argc != 6) 18312a856b9aSJohnny Huang return CMD_RET_USAGE; 18322a856b9aSJohnny Huang 18332a856b9aSJohnny Huang /* Drop the pb cmd */ 18342a856b9aSJohnny Huang argc--; 18352a856b9aSJohnny Huang argv++; 18362a856b9aSJohnny Huang 18372a856b9aSJohnny Huang if (!strcmp(argv[0], "conf")) 1838a6d0d645SJohnny Huang mode = OTP_REGION_CONF; 18392a856b9aSJohnny Huang else if (!strcmp(argv[0], "strap")) 1840a6d0d645SJohnny Huang mode = OTP_REGION_STRAP; 18412a856b9aSJohnny Huang else if (!strcmp(argv[0], "data")) 1842a6d0d645SJohnny Huang mode = OTP_REGION_DATA; 1843cd1610b4SJohnny Huang else 18442a856b9aSJohnny Huang return CMD_RET_USAGE; 18452a856b9aSJohnny Huang 18462a856b9aSJohnny Huang /* Drop the region cmd */ 18472a856b9aSJohnny Huang argc--; 18482a856b9aSJohnny Huang argv++; 18492a856b9aSJohnny Huang 1850ed071a2bSJohnny Huang if (!strcmp(argv[0], "o")) { 1851cd1610b4SJohnny Huang nconfirm = 1; 18522a856b9aSJohnny Huang /* Drop the force option */ 18532a856b9aSJohnny Huang argc--; 18542a856b9aSJohnny Huang argv++; 18552a856b9aSJohnny Huang } 1856cd1610b4SJohnny Huang 1857a6d0d645SJohnny Huang if (mode == OTP_REGION_STRAP) { 18582a856b9aSJohnny Huang bit_offset = simple_strtoul(argv[0], NULL, 16); 18592a856b9aSJohnny Huang value = simple_strtoul(argv[1], NULL, 16); 18600808cc55SJohnny Huang if (bit_offset >= 64 || (value != 0 && value != 1)) 18612a856b9aSJohnny Huang return CMD_RET_USAGE; 1862cd1610b4SJohnny Huang } else { 18632a856b9aSJohnny Huang otp_addr = simple_strtoul(argv[0], NULL, 16); 18642a856b9aSJohnny Huang bit_offset = simple_strtoul(argv[1], NULL, 16); 18652a856b9aSJohnny Huang value = simple_strtoul(argv[2], NULL, 16); 18660808cc55SJohnny Huang if (bit_offset >= 32 || (value != 0 && value != 1)) 18672a856b9aSJohnny Huang return CMD_RET_USAGE; 18680808cc55SJohnny Huang if (mode == OTP_REGION_DATA) { 186978855207SJohnny Huang if (otp_addr >= 0x800) 18700808cc55SJohnny Huang return CMD_RET_USAGE; 18710808cc55SJohnny Huang } else { 187278855207SJohnny Huang if (otp_addr >= 0x20) 18730808cc55SJohnny Huang return CMD_RET_USAGE; 18740808cc55SJohnny Huang } 1875cd1610b4SJohnny Huang } 1876cd1610b4SJohnny Huang if (value != 0 && value != 1) 18772a856b9aSJohnny Huang return CMD_RET_USAGE; 1878cd1610b4SJohnny Huang 18793d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 18802a856b9aSJohnny Huang ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm); 18812a856b9aSJohnny Huang 18822a856b9aSJohnny Huang if (ret == OTP_SUCCESS) 18832a856b9aSJohnny Huang return CMD_RET_SUCCESS; 18842a856b9aSJohnny Huang else if (ret == OTP_FAILURE) 18852a856b9aSJohnny Huang return CMD_RET_FAILURE; 18862a856b9aSJohnny Huang else 18872a856b9aSJohnny Huang return CMD_RET_USAGE; 18882a856b9aSJohnny Huang } 18892a856b9aSJohnny Huang 18902a856b9aSJohnny Huang static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 18912a856b9aSJohnny Huang { 18922a856b9aSJohnny Huang phys_addr_t addr; 18932a856b9aSJohnny Huang int otp_addr = 0; 18942a856b9aSJohnny Huang 18952a856b9aSJohnny Huang if (argc != 3) 18962a856b9aSJohnny Huang return CMD_RET_USAGE; 18972a856b9aSJohnny Huang 18983d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 18992a856b9aSJohnny Huang addr = simple_strtoul(argv[1], NULL, 16); 19002a856b9aSJohnny Huang otp_addr = simple_strtoul(argv[2], NULL, 16); 19012a856b9aSJohnny Huang if (otp_compare(otp_addr, addr) == 0) { 190269d5fd8fSJohnny Huang printf("Compare pass\n"); 19032a856b9aSJohnny Huang return CMD_RET_SUCCESS; 190469d5fd8fSJohnny Huang } else { 190569d5fd8fSJohnny Huang printf("Compare fail\n"); 19062a856b9aSJohnny Huang return CMD_RET_FAILURE; 190769d5fd8fSJohnny Huang } 190869d5fd8fSJohnny Huang } 190969d5fd8fSJohnny Huang 191066f2f8e5SJohnny Huang static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 191166f2f8e5SJohnny Huang { 1912a8bd6d8cSJohnny Huang int view = 0; 19132d4b0742SJohnny Huang int input; 1914a8bd6d8cSJohnny Huang 1915a8bd6d8cSJohnny Huang if (argc != 2 && argc != 3) 191666f2f8e5SJohnny Huang return CMD_RET_USAGE; 191766f2f8e5SJohnny Huang 19182d4b0742SJohnny Huang if (!strcmp(argv[1], "conf")) { 191966f2f8e5SJohnny Huang 19203d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 19212d4b0742SJohnny Huang if (argc == 3) { 19222d4b0742SJohnny Huang input = simple_strtoul(argv[2], NULL, 16); 19232d4b0742SJohnny Huang otp_print_conf_info(input); 19242d4b0742SJohnny Huang } else { 19252d4b0742SJohnny Huang otp_print_conf_info(-1); 19262d4b0742SJohnny Huang } 19272d4b0742SJohnny Huang } else if (!strcmp(argv[1], "strap")) { 19282d4b0742SJohnny Huang if (!strcmp(argv[2], "v")) { 1929a8bd6d8cSJohnny Huang view = 1; 1930a8bd6d8cSJohnny Huang /* Drop the view option */ 1931a8bd6d8cSJohnny Huang argc--; 1932a8bd6d8cSJohnny Huang argv++; 1933a8bd6d8cSJohnny Huang } 19343d3688adSJohnny Huang writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1935b458cd62SJohnny Huang otp_print_strap_info(view); 193666f2f8e5SJohnny Huang } else { 193766f2f8e5SJohnny Huang return CMD_RET_USAGE; 193866f2f8e5SJohnny Huang } 19392d4b0742SJohnny Huang 194066f2f8e5SJohnny Huang return CMD_RET_SUCCESS; 194166f2f8e5SJohnny Huang } 194266f2f8e5SJohnny Huang 1943737ed20bSJohnny Huang static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1944737ed20bSJohnny Huang { 1945737ed20bSJohnny Huang int input; 1946737ed20bSJohnny Huang int bit_offset; 1947737ed20bSJohnny Huang int prog_address; 194883655e91SJohnny Huang int ret; 1949737ed20bSJohnny Huang if (argc != 3 && argc != 2) 1950737ed20bSJohnny Huang return CMD_RET_USAGE; 1951737ed20bSJohnny Huang 1952ed071a2bSJohnny Huang if (!strcmp(argv[0], "o")) { 1953737ed20bSJohnny Huang input = simple_strtoul(argv[2], NULL, 16); 1954737ed20bSJohnny Huang } else { 1955737ed20bSJohnny Huang input = simple_strtoul(argv[1], NULL, 16); 1956737ed20bSJohnny Huang printf("OTPSTRAP[%d] will be protected\n", input); 1957737ed20bSJohnny Huang printf("type \"YES\" (no quotes) to continue:\n"); 1958737ed20bSJohnny Huang if (!confirm_yesno()) { 1959737ed20bSJohnny Huang printf(" Aborting\n"); 1960737ed20bSJohnny Huang return CMD_RET_FAILURE; 1961737ed20bSJohnny Huang } 1962737ed20bSJohnny Huang } 1963737ed20bSJohnny Huang 1964737ed20bSJohnny Huang prog_address = 0x800; 1965737ed20bSJohnny Huang if (input < 32) { 1966737ed20bSJohnny Huang bit_offset = input; 1967737ed20bSJohnny Huang prog_address |= 0x60c; 1968737ed20bSJohnny Huang } else if (input < 64) { 1969737ed20bSJohnny Huang bit_offset = input - 32; 1970737ed20bSJohnny Huang prog_address |= 0x60e; 1971737ed20bSJohnny Huang } else { 1972737ed20bSJohnny Huang return CMD_RET_USAGE; 1973737ed20bSJohnny Huang } 1974737ed20bSJohnny Huang 1975737ed20bSJohnny Huang if (verify_bit(prog_address, bit_offset, 1) == 0) { 1976737ed20bSJohnny Huang printf("OTPSTRAP[%d] already protected\n", input); 1977737ed20bSJohnny Huang } 1978de6fbf1cSJohnny Huang 197983655e91SJohnny Huang ret = otp_prog_bit(1, prog_address, bit_offset); 1980de6fbf1cSJohnny Huang otp_soak(0); 198183655e91SJohnny Huang 198283655e91SJohnny Huang if (ret) { 1983737ed20bSJohnny Huang printf("OTPSTRAP[%d] is protected\n", input); 1984737ed20bSJohnny Huang return CMD_RET_SUCCESS; 1985737ed20bSJohnny Huang } 1986737ed20bSJohnny Huang 1987737ed20bSJohnny Huang printf("Protect OTPSTRAP[%d] fail\n", input); 1988737ed20bSJohnny Huang return CMD_RET_FAILURE; 1989737ed20bSJohnny Huang 1990737ed20bSJohnny Huang } 19919a4fe690SJohnny Huang 1992*f67375f7SJohnny Huang static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1993*f67375f7SJohnny Huang { 1994*f67375f7SJohnny Huang printf("OTP tool version: %s\n", OTP_VER); 1995*f67375f7SJohnny Huang printf("OTP info version: %s\n", OTP_INFO_VER); 1996*f67375f7SJohnny Huang 1997*f67375f7SJohnny Huang return CMD_RET_SUCCESS; 1998*f67375f7SJohnny Huang } 1999*f67375f7SJohnny Huang 20002a856b9aSJohnny Huang static cmd_tbl_t cmd_otp[] = { 2001*f67375f7SJohnny Huang U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""), 20022a856b9aSJohnny Huang U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""), 2003a8bd6d8cSJohnny Huang U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""), 2004de6b0cc4SJohnny Huang U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""), 20052a856b9aSJohnny Huang U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""), 2006737ed20bSJohnny Huang U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""), 20072a856b9aSJohnny Huang U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""), 20082a856b9aSJohnny Huang }; 20092a856b9aSJohnny Huang 20102a856b9aSJohnny Huang static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 20112a856b9aSJohnny Huang { 20122a856b9aSJohnny Huang cmd_tbl_t *cp; 20132a856b9aSJohnny Huang 20142a856b9aSJohnny Huang cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp)); 20152a856b9aSJohnny Huang 2016737ed20bSJohnny Huang /* Drop the otp command */ 20172a856b9aSJohnny Huang argc--; 20182a856b9aSJohnny Huang argv++; 20192a856b9aSJohnny Huang 20202a856b9aSJohnny Huang if (cp == NULL || argc > cp->maxargs) 20212a856b9aSJohnny Huang return CMD_RET_USAGE; 20222a856b9aSJohnny Huang if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) 20232a856b9aSJohnny Huang return CMD_RET_SUCCESS; 20242a856b9aSJohnny Huang 2025696656c6SJohnny Huang if (chip_version() == OTP_AST2600A0) { 2026696656c6SJohnny Huang info_cb.version = OTP_AST2600A0; 20279a4fe690SJohnny Huang info_cb.conf_info = a0_conf_info; 20289a4fe690SJohnny Huang info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info); 20299a4fe690SJohnny Huang info_cb.strap_info = a0_strap_info; 20309a4fe690SJohnny Huang info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info); 20319a4fe690SJohnny Huang info_cb.key_info = a0_key_type; 20329a4fe690SJohnny Huang info_cb.key_info_len = ARRAY_SIZE(a0_key_type); 2033696656c6SJohnny Huang } else if (chip_version() == OTP_AST2600A1) { 2034696656c6SJohnny Huang info_cb.version = OTP_AST2600A1; 20353cb28812SJohnny Huang info_cb.conf_info = a1_conf_info; 20363cb28812SJohnny Huang info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info); 20373cb28812SJohnny Huang info_cb.strap_info = a1_strap_info; 20383cb28812SJohnny Huang info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info); 20399a4fe690SJohnny Huang info_cb.key_info = a1_key_type; 20409a4fe690SJohnny Huang info_cb.key_info_len = ARRAY_SIZE(a1_key_type); 20415fdde29fSJohnny Huang } else if (chip_version() == OTP_AST2600A2) { 20425fdde29fSJohnny Huang info_cb.version = OTP_AST2600A2; 20435fdde29fSJohnny Huang info_cb.conf_info = a2_conf_info; 20445fdde29fSJohnny Huang info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info); 20455fdde29fSJohnny Huang info_cb.strap_info = a2_strap_info; 20465fdde29fSJohnny Huang info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info); 20475fdde29fSJohnny Huang info_cb.key_info = a2_key_type; 20485fdde29fSJohnny Huang info_cb.key_info_len = ARRAY_SIZE(a2_key_type); 20499a4fe690SJohnny Huang } 20509a4fe690SJohnny Huang 20512a856b9aSJohnny Huang return cp->cmd(cmdtp, flag, argc, argv); 205269d5fd8fSJohnny Huang } 205369d5fd8fSJohnny Huang 205469d5fd8fSJohnny Huang U_BOOT_CMD( 205569d5fd8fSJohnny Huang otp, 7, 0, do_ast_otp, 205669d5fd8fSJohnny Huang "ASPEED One-Time-Programmable sub-system", 2057*f67375f7SJohnny Huang "version\n" 2058*f67375f7SJohnny Huang "otp read conf|data <otp_dw_offset> <dw_count>\n" 20592a856b9aSJohnny Huang "otp read strap <strap_bit_offset> <bit_count>\n" 20602d4b0742SJohnny Huang "otp info strap [v]\n" 20612d4b0742SJohnny Huang "otp info conf [otp_dw_offset]\n" 2062de6b0cc4SJohnny Huang "otp prog [o] <addr>\n" 2063ed071a2bSJohnny Huang "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n" 2064ed071a2bSJohnny Huang "otp pb strap [o] <bit_offset> <value>\n" 2065ed071a2bSJohnny Huang "otp protect [o] <bit_offset>\n" 20662a856b9aSJohnny Huang "otp cmp <addr> <otp_dw_offset>\n" 206769d5fd8fSJohnny Huang ); 2068