blob: 4ba78a97e0ebab8460a5fe45b289ca39ae34e027 [file] [log] [blame]
Stefan Reinauerc566d202015-08-25 09:52:42 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Stefan Reinauerc566d202015-08-25 09:52:42 -070012 */
13
14#ifndef __EM100_H__
15#define __EM100_H__
16
17#include <libusb.h>
Stefan Reinauered59fc82020-11-21 16:11:40 -080018#include <signal.h>
Stefan Reinauerc566d202015-08-25 09:52:42 -070019
Stefan Reinauer6a5ed5b2019-11-19 14:52:16 -080020#define __unused __attribute__((unused))
21#define __packed __attribute__((packed))
22
Stefan Reinauerc566d202015-08-25 09:52:42 -070023struct em100 {
24 libusb_device_handle *dev;
25 libusb_context *ctx;
26 uint16_t mcu;
27 uint16_t fpga;
28 uint32_t serialno;
Stefan Reinauer98eb7532019-11-13 18:42:07 -080029 uint8_t hwversion;
Stefan Reinauerc566d202015-08-25 09:52:42 -070030};
31
Stefan Reinauerdb126f42019-11-18 18:25:45 -080032#define NUM_INIT_ENTRIES 212
33#define BYTES_PER_INIT_ENTRY 4
34typedef struct {
35 const char *vendor;
36 const char *name;
37 unsigned int size;
38 uint8_t init[NUM_INIT_ENTRIES][BYTES_PER_INIT_ENTRY];
39 int init_len;
40} chipdesc;
41
Stefan Reinauerfc22de32019-11-13 19:14:12 -080042/* Hardware versions */
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -080043#define HWVERSION_EM100PRO_EARLY 0xff
44#define HWVERSION_EM100PRO 0x04
45#define HWVERSION_EM100PRO_G2 0x06
Stefan Reinauerfc22de32019-11-13 19:14:12 -080046
Stefan Reinauerc566d202015-08-25 09:52:42 -070047#define BULK_SEND_TIMEOUT 5000 /* sentinel value */
48
49/* usb.c */
50int send_cmd(libusb_device_handle *dev, void *data);
51int get_response(libusb_device_handle *dev, void *data, int length);
52
Stefan Reinauera2b67d32015-09-01 16:30:43 -070053/* firmware.c */
Martin Rothb54d6ba2015-09-29 14:49:37 -060054int firmware_dump(struct em100 *em100, const char *filename,
55 int firmware_is_dpfw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -070056int firmware_update(struct em100 *em100, const char *filename, int verify);
57
Stefan Reinauerc566d202015-08-25 09:52:42 -070058/* fpga.c */
59int reconfig_fpga(struct em100 *em100);
60int check_fpga_status(struct em100 *em100);
61int read_fpga_register(struct em100 *em100, int reg, uint16_t *val);
62int write_fpga_register(struct em100 *em100, int reg, int val);
Simon Glass98cd62c2018-12-26 14:31:20 -070063int fpga_set_voltage(struct em100 *em100, int voltage_code);
64int fpga_get_voltage(struct em100 *em100, int *voltage_codep);
65int fpga_reconfigure(struct em100 *em100);
Stefan Reinauerc566d202015-08-25 09:52:42 -070066
Patrick Rudolph8b557a92019-10-10 10:15:14 +020067/* Guessed register names */
68#define FPGA_REG_DEVID 0x40
69#define FPGA_REG_VENDID 0x42
70
Stefan Reinauer14d35122015-09-03 16:02:55 -070071/* hexdump.c */
72void hexdump(const void *memory, size_t length);
73
Stefan Reinauerc566d202015-08-25 09:52:42 -070074/* sdram.c */
75int read_sdram(struct em100 *em100, void *data, int address, int length);
76int write_sdram(struct em100 *em100, unsigned char *data, int address,
77 int length);
78
79/* spi.c */
Stefan Reinauerf70ff252019-11-13 18:58:16 -080080uint32_t get_spi_flash_id(struct em100 *em100);
Stefan Reinauerc566d202015-08-25 09:52:42 -070081int erase_spi_flash(struct em100 *em100);
82int poll_spi_flash_status(struct em100 *em100);
Stefan Reinauer79533882019-11-22 00:57:29 -080083int read_spi_flash_page(struct em100 *em100, int address, unsigned char *blk);
Stefan Reinauer65559f12015-08-28 16:28:51 -070084int write_spi_flash_page(struct em100 *em100, int address, unsigned char *data);
Stefan Reinauer40cba332015-08-28 20:03:05 -070085int unlock_spi_flash(struct em100 *em100);
86int erase_spi_flash_sector(struct em100 *em100, unsigned int sector);
Stefan Reinauerc566d202015-08-25 09:52:42 -070087int read_ht_register(struct em100 *em100, int reg, uint8_t *val);
88int write_ht_register(struct em100 *em100, int reg, uint8_t val);
Stefan Reinauer79533882019-11-22 00:57:29 -080089int write_dfifo(struct em100 *em100, size_t length, unsigned int timeout,
Stefan Reinauer42dfb822015-08-28 16:59:42 -070090 unsigned char *blk);
Stefan Reinauer79533882019-11-22 00:57:29 -080091int read_ufifo(struct em100 *em100, size_t length, unsigned int timeout,
Stefan Reinauer42dfb822015-08-28 16:59:42 -070092 unsigned char *blk);
Stefan Reinauerc566d202015-08-25 09:52:42 -070093
94/* system.c */
95typedef enum {
96 out_trigger_vcc = 0,
97 out_reset_vcc = 1,
98 out_ref_plus = 2,
99 out_ref_minus = 3,
100 out_buffer_vcc = 4
101} set_voltage_channel_t;
102
103typedef enum {
104 in_v1_2 = 0,
105 in_e_vcc = 1,
106 in_ref_plus = 2,
107 in_ref_minus = 3,
108 in_buffer_vcc = 4,
109 in_trigger_vcc = 5,
110 in_reset_vcc = 6,
111 in_v3_3 = 7,
112 in_buffer_v3_3 = 8,
113 in_v5 = 9
114} get_voltage_channel_t;
115
116typedef enum {
117 both_off = 0,
118 green_on = 1,
119 red_on = 2,
120 both_on = 3
121} led_state_t;
122
123int get_version(struct em100 *em100);
124int set_voltage(struct em100 *em100, set_voltage_channel_t channel, int mV);
125int get_voltage(struct em100 *em100, get_voltage_channel_t channel);
126int set_led(struct em100 *em100, led_state_t led_state);
127
128/* trace.c */
Stefan Reinauer146eeba2020-12-17 19:26:02 -0800129extern int trace_brief;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600130#define EM100_SPECIFIC_CMD 0x11
131#define EM100_MSG_SIGNATURE 0x47364440
132
133typedef enum {
134 status_reg = 0,
135 dfifo_bytes_reg = 1,
136 ufifo_bytes_reg = 2,
137 em100_id_reg = 3,
138 ufifo_data_fmt_reg = 4,
139 timestamp_reg = 5
140} ht_register_t;
141
142/* Status register bits */
143#define UFIFO_OVERFLOW (1 << 0)
144#define BIT8_UFIFO_BYTES (1 << 3)
145#define START_SPI_EMULATION (1 << 4)
146#define PAUSE_SPI_EMULATION (0 << 4)
147#define UFIFO_EMPTY (1 << 5)
148#define DFIFO_EMPTY (1 << 6)
149
150struct em100_msg_header{
151 uint32_t signature;
152 uint8_t data_type;
153 uint8_t data_length;
154} __attribute__ ((packed));
155
156struct em100_msg{
157 struct em100_msg_header header;
158 uint8_t data[255];
159} __attribute__ ((packed));
160
161typedef enum {
162 ht_checkpoint_1byte = 0x01,
163 ht_checkpoint_2bytes = 0x02,
164 ht_checkpoint_4bytes = 0x03,
165 ht_hexadecimal_data = 0x04,
166 ht_ascii_data = 0x05,
167 ht_timestamp_data = 0x06,
168 ht_lookup_table = 0x07
169} ht_msg_type_t;
170
Stefan Reinauerc566d202015-08-25 09:52:42 -0700171int reset_spi_trace(struct em100 *em100);
Martin Roth712262a2015-09-18 14:01:18 -0600172int read_spi_trace(struct em100 *em100, int display_terminal,
173 unsigned long addr_offset);
Martin Rothdfdff3e2015-09-18 09:42:03 -0600174int read_spi_terminal(struct em100 *em100, int print_counter);
175int init_spi_terminal(struct em100 *em100);
Stefan Reinauercafd1512020-12-11 17:36:28 -0800176int read_spi_trace_console(struct em100 *em100, unsigned long addr_offset,
177 unsigned long addr_len);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700178
Stefan Reinauerdb126f42019-11-18 18:25:45 -0800179/* Archive handling */
180typedef struct {
181 unsigned char *address;
182 size_t length;
183 int alloc;
184} TFILE;
Stefan Reinauer79533882019-11-22 00:57:29 -0800185TFILE *tar_find(TFILE *tfile, const char *name, int casesensitive);
Stefan Reinauerdb126f42019-11-18 18:25:45 -0800186TFILE *tar_load_compressed(char *filename);
187int tar_for_each(TFILE *tfile, int (*run)(char *, TFILE *, void *, int), void *data);
188int tar_close(TFILE *tfile);
189int tar_ls(TFILE *tfile);
190int test_tar(void);
191
Stefan Reinauerdfd556c2019-11-20 17:57:40 -0800192/* Network */
193void download(const char *name, const char *id);
194int update_all_files(void);
195
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800196/* Misc. */
197
198#define MB * 1024 * 1024
Stefan Reinauerdb126f42019-11-18 18:25:45 -0800199#define FILENAME_BUFFER_SIZE 1024
Stefan Reinauer3b921082019-11-26 16:56:26 -0800200#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Stefan Reinauer79533882019-11-22 00:57:29 -0800201char *get_em100_file(const char *name);
Stefan Reinauerb20610e2019-12-06 11:14:33 -0800202extern int debug;
Stefan Reinauerdfb107d2020-12-10 19:22:40 -0800203extern int address_mode;
Stefan Reinauered59fc82020-11-21 16:11:40 -0800204extern volatile sig_atomic_t exit_requested;
Stefan Reinauerdb126f42019-11-18 18:25:45 -0800205
206/* Chips */
207int parse_dcfg(chipdesc *chip, TFILE *dcfg);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800208
Stefan Reinauer3b921082019-11-26 16:56:26 -0800209/* Images */
210int autocorrect_image(struct em100 *em100, char *image, size_t size);
211
Stefan Reinauerc566d202015-08-25 09:52:42 -0700212#endif