1 #ifndef PROGALGNVM_H
2 #define PROGALGNVM_H
3 
4 #include <stdint.h>
5 
6 #include <pdioverjtag.h>
7 
8 #define XNVM_PDI_LDS_INSTR    0x00 //!< LDS instruction.
9 #define XNVM_PDI_STS_INSTR    0x40 //!< STS instruction.
10 #define XNVM_PDI_LD_INSTR     0x20 //!< LD instruction.
11 #define XNVM_PDI_ST_INSTR     0x60 //!< ST instruction.
12 #define XNVM_PDI_LDCS_INSTR   0x80 //!< LDCS instruction.
13 #define XNVM_PDI_STCS_INSTR   0xC0 //!< STCS instruction.
14 #define XNVM_PDI_REPEAT_INSTR 0xA0 //!< REPEAT instruction.
15 #define XNVM_PDI_KEY_INSTR    0xE0 //!< KEY instruction.
16 
17 /** Byte size address mask for LDS and STS instruction */
18 #define XNVM_PDI_BYTE_ADDRESS_MASK 0x00
19 /** Word size address mask for LDS and STS instruction */
20 #define XNVM_PDI_WORD_ADDRESS_MASK 0x04
21 /** 3 bytes size address mask for LDS and STS instruction */
22 #define XNVM_PDI_3BYTES_ADDRESS_MASK 0x08
23 /** Long size address mask for LDS and STS instruction */
24 #define XNVM_PDI_LONG_ADDRESS_MASK 0x0C
25 /** Byte size data mask for LDS and STS instruction */
26 #define XNVM_PDI_BYTE_DATA_MASK 0x00
27 /** Word size data mask for LDS and STS instruction */
28 #define XNVM_PDI_WORD_DATA_MASK 0x01
29 /** 3 bytes size data mask for LDS and STS instruction */
30 #define XNVM_PDI_3BYTES_DATA_MASK 0x02
31 /** Long size data mask for LDS and STS instruction */
32 #define XNVM_PDI_LONG_DATA_MASK 0x03
33 /** Byte size address mask for LDS and STS instruction */
34 #define XNVM_PDI_LD_PTR_STAR_MASK 0x00
35 /** Word size address mask for LDS and STS instruction */
36 #define XNVM_PDI_LD_PTR_STAR_INC_MASK 0x04
37 /** 3 bytes size address mask for LDS and STS instruction */
38 #define XNVM_PDI_LD_PTR_ADDRESS_MASK 0x08
39 
40 #define XNVM_CMD_NOP                         0x00 //!< No Operation.
41 #define XNVM_CMD_CHIP_ERASE                  0x40 //!< Chip Erase.
42 #define XNVM_CMD_READ_NVM_PDI                0x43 //!< Read NVM PDI.
43 #define XNVM_CMD_LOAD_FLASH_PAGE_BUFFER      0x23 //!< Load Flash Page Buffer.
44 #define XNVM_CMD_ERASE_FLASH_PAGE_BUFFER     0x26 //!< Erase Flash Page Buffer.
45 #define XNVM_CMD_ERASE_FLASH_PAGE            0x2B //!< Erase Flash Page.
46 #define XNVM_CMD_WRITE_FLASH_PAGE            0x2E //!< Flash Page Write.
47 #define XNVM_CMD_ERASE_AND_WRITE_FLASH_PAGE  0x2F //!< Erase & Write Flash Page.
48 #define XNVM_CMD_CALC_CRC_ON_FLASH           0x78 //!< Flash CRC.
49 
50 #define XNVM_CMD_ERASE_APP_SECTION           0x20 //!< Erase Application Section.
51 #define XNVM_CMD_ERASE_APP_PAGE              0x22 //!< Erase Application Section.
52 #define XNVM_CMD_WRITE_APP_SECTION           0x24 //!< Write Application Section.
53 #define XNVM_CMD_ERASE_AND_WRITE_APP_SECTION 0x25 //!< Erase & Write Application Section Page.
54 #define XNVM_CMD_CALC_CRC_APP_SECTION        0x38 //!< Application Section CRC.
55 
56 #define XNVM_CMD_ERASE_BOOT_SECTION          0x68 //!< Erase Boot Section.
57 #define XNVM_CMD_ERASE_BOOT_PAGE             0x2A //!< Erase Boot Loader Section Page.
58 #define XNVM_CMD_WRITE_BOOT_PAGE             0x2C //!< Write Boot Loader Section Page.
59 #define XNVM_CMD_ERASE_AND_WRITE_BOOT_PAGE   0x2D //!< Erase & Write Boot Loader Section Page.
60 #define XNVM_CMD_CALC_CRC_BOOT_SECTION       0x39 //!< Boot Loader Section CRC.
61 
62 #define XNVM_CMD_READ_USER_SIGN              0x03 //!< Read User Signature Row.
63 #define XNVM_CMD_ERASE_USER_SIGN             0x18 //!< Erase User Signature Row.
64 #define XNVM_CMD_WRITE_USER_SIGN             0x1A //!< Write User Signature Row.
65 #define XNVM_CMD_READ_CALIB_ROW              0x02 //!< Read Calibration Row.
66 
67 #define XNVM_CMD_READ_FUSE                   0x07 //!< Read Fuse.
68 #define XNVM_CMD_WRITE_FUSE                  0x4C //!< Write Fuse.
69 #define XNVM_CMD_WRITE_LOCK_BITS             0x08 //!< Write Lock Bits.
70 
71 #define XNVM_CMD_LOAD_EEPROM_PAGE_BUFFER     0x33 //!< Load EEPROM Page Buffer.
72 #define XNVM_CMD_ERASE_EEPROM_PAGE_BUFFER    0x36 //!< Erase EEPROM Page Buffer.
73 
74 #define XNVM_CMD_ERASE_EEPROM                0x30 //!< Erase EEPROM.
75 #define XNVM_CMD_ERASE_EEPROM_PAGE           0x32 //!< Erase EEPROM Page.
76 #define XNVM_CMD_WRITE_EEPROM_PAGE           0x34 //!< Write EEPROM Page.
77 #define XNVM_CMD_ERASE_AND_WRITE_EEPROM      0x35 //!< Erase & Write EEPROM Page.
78 #define XNVM_CMD_READ_EEPROM                 0x06 //!< Read EEPROM.
79 
80 /**
81  * \brief Key used to enable the NVM interface.
82  */
83 #define NVM_KEY_BYTE0 0xFF
84 #define NVM_KEY_BYTE1 0x88
85 #define NVM_KEY_BYTE2 0xD8
86 #define NVM_KEY_BYTE3 0xCD
87 #define NVM_KEY_BYTE4 0x45
88 #define NVM_KEY_BYTE5 0xAB
89 #define NVM_KEY_BYTE6 0x89
90 #define NVM_KEY_BYTE7 0x12
91 
92 
93 class ProgAlgNVM
94 {
95 private:
96     PDIoverJTAG *prot;
97     int initialized;
98 
99     enum PDI_STATUS_CODE xnvm_read_pdi_status(uint8_t *status);
100     enum PDI_STATUS_CODE xnvm_wait_for_nvmen(uint32_t retries);
101     enum PDI_STATUS_CODE xnvm_ctrl_read_reg(uint16_t reg, uint8_t *value);
102     enum PDI_STATUS_CODE xnvm_ctrl_write_reg(uint16_t reg, uint8_t value);
103     enum PDI_STATUS_CODE xnvm_ctrl_cmd_write(uint8_t cmd_id);
104     enum PDI_STATUS_CODE xnvm_ctrl_read_status(uint8_t *value);
105     enum PDI_STATUS_CODE xnvm_erase_application_flash_page
106 	(uint32_t address, uint8_t *dat_buf, uint16_t length);
107     enum PDI_STATUS_CODE xnvm_erase_flash_buffer(uint32_t retries);
108     enum PDI_STATUS_CODE xnvm_load_flash_page_buffer
109 	(uint32_t addr, uint8_t *buf, uint16_t len);
110     enum PDI_STATUS_CODE xnvm_st_ptr(uint32_t address);
111     enum PDI_STATUS_CODE xnvm_st_star_ptr_postinc(uint8_t value);
112     enum PDI_STATUS_CODE xnvm_write_repeat(uint32_t count);
113     enum PDI_STATUS_CODE xnvm_ctrl_wait_nvmbusy(uint32_t retries);
114     enum PDI_STATUS_CODE xnvm_ctrl_cmdex_write(void);
115     enum PDI_STATUS_CODE xnvm_erase_eeprom_buffer(uint32_t retries);
116     enum PDI_STATUS_CODE xnvm_load_eeprom_page_buffer
117     (uint32_t addr, uint8_t *buf, uint16_t len);
118 
119 
120 public:
121     uint8_t cmd_buffer[20];
122     ProgAlgNVM(PDIoverJTAG *protocoll);
123     ~ProgAlgNVM(void);
124     enum PDI_STATUS_CODE xnvm_init (void);
125     enum PDI_STATUS_CODE xnvm_ioread_byte(uint16_t address, uint8_t *value);
126     enum PDI_STATUS_CODE xnvm_iowrite_byte(uint16_t address, uint8_t value);
127     enum PDI_STATUS_CODE xnvm_chip_erase(void);
128     enum PDI_STATUS_CODE xnvm_application_erase(void);
129     enum PDI_STATUS_CODE xnvm_boot_erase(uint32_t address);
130     enum PDI_STATUS_CODE xnvm_erase_eeprom(void);
131     uint16_t xnvm_read_memory(uint32_t address, uint8_t *data, uint32_t length);
132     enum PDI_STATUS_CODE xnvm_erase_program_flash_page
133 	(uint32_t address, uint8_t *dat_buf, uint16_t length);
134     enum PDI_STATUS_CODE xnvm_program_flash_page
135         (uint32_t address, uint8_t *dat_buf, uint16_t length);
136     enum PDI_STATUS_CODE xnvm_put_dev_in_reset (void);
137     enum PDI_STATUS_CODE xnvm_pull_dev_out_of_reset(void);
138     enum PDI_STATUS_CODE xnvm_erase_program_eeprom_page
139 	(uint32_t address, uint8_t *dat_buf, uint16_t length);
140     enum PDI_STATUS_CODE xnvm_erase_user_sign(void);
141     enum PDI_STATUS_CODE xnvm_erase_program_user_sign
142 	(uint32_t address, uint8_t *dat_buf, uint16_t length);
143     enum PDI_STATUS_CODE xnvm_write_fuse_byte
144 	(uint32_t address, uint8_t value);
145     enum PDI_STATUS_CODE xnvm_write_lock_byte(uint8_t value);
146 };
147 
148 #endif //PROGALGNVM_H
149