1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _STM32_BL_H_
18#define _STM32_BL_H_
19
20/*
21 * cmd_erase, cmd_read_memory, cmd_write_memory - byte to send for cmd
22 * no_extra_sync - uart erase requires no extra sync between cnt and sector
23 *
24 * write_data - write length bytes of data
25 *   (function must checksum data. space reserved for checksum in buffer)
26 * write_cmd - write cmd
27 * read_data - read length bytes of data
28 * read_ack - read ack
29 * functions return results of command
30 */
31typedef struct handle
32{
33    uint8_t cmd_erase;
34    uint8_t cmd_read_memory;
35    uint8_t cmd_write_memory;
36    uint8_t no_extra_sync;
37
38    uint8_t (*write_data)(struct handle *, uint8_t *buffer, int length);
39    uint8_t (*write_cmd)(struct handle *, uint8_t cmd);
40    uint8_t (*read_data)(struct handle *, uint8_t *buffer, int length);
41    uint8_t (*read_ack)(struct handle *);
42} handle_t;
43
44uint8_t checksum(handle_t *handle, uint8_t *bytes, int length);
45uint8_t erase_sector(handle_t *handle, uint16_t sector);
46uint8_t read_memory(handle_t *handle, uint32_t addr, uint32_t length, uint8_t *buffer);
47uint8_t write_memory(handle_t *handle, uint32_t addr, uint32_t length, uint8_t *buffer);
48
49/*
50 * Bootloader commands
51 * _NS versions are no-stretch.
52 * will return CMD_BUSY instead of stretching the clock
53 */
54
55#define CMD_GET				0x00
56#define CMD_GET_VERSION			0x01
57#define CMD_GET_ID			0x02
58#define CMD_READ_MEMORY			0x11
59#define CMD_NACK			0x1F
60#define CMD_GO				0x21
61#define CMD_WRITE_MEMORY		0x31
62#define CMD_WRITE_MEMORY_NS		0x32
63#define CMD_ERASE			0x44
64#define CMD_ERASE_NS			0x45
65#define CMD_SOF				0x5A
66#define CMD_WRITE_PROTECT		0x63
67#define CMD_WRITE_PROTECT_NS		0x64
68#define CMD_WRITE_UNPROTECT		0x73
69#define CMD_WRITE_UNPROTECT_NS		0x74
70#define CMD_BUSY			0x76
71#define CMD_ACK				0x79
72#define CMD_UART_ENABLE			0x7F
73#define CMD_READOUT_PROTECT		0x82
74#define CMD_READOUT_PROTECT_NS		0x83
75#define CMD_READOUT_UNPROTECT		0x92
76#define CMD_READOUT_UNPROTECT_NS	0x93
77#define CMD_SOF_ACK			0xA5
78
79#endif /* _STM32_BL_H_ */
80