1244557Sbrooks/* SPDX-License-Identifier: GPL-2.0+ */
2244557Sbrooks/*
3244557Sbrooks * Test-related constants for sandbox
4244557Sbrooks *
5244557Sbrooks * Copyright (c) 2014 Google, Inc
6244557Sbrooks */
7244557Sbrooks
8244557Sbrooks#ifndef __ASM_TEST_H
9244557Sbrooks#define __ASM_TEST_H
10244557Sbrooks
11244557Sbrooks#include <pci_ids.h>
12244557Sbrooks
13244557Sbrooksstruct unit_test_state;
14244557Sbrooks
15244557Sbrooks/* The sandbox driver always permits an I2C device with this address */
16244557Sbrooks#define SANDBOX_I2C_TEST_ADDR		0x59
17244557Sbrooks
18244557Sbrooks#define SANDBOX_PCI_VENDOR_ID		0x1234
19244557Sbrooks#define SANDBOX_PCI_SWAP_CASE_EMUL_ID	0x5678
20244557Sbrooks#define SANDBOX_PCI_PMC_EMUL_ID		0x5677
21244557Sbrooks#define SANDBOX_PCI_P2SB_EMUL_ID	0x5676
22244557Sbrooks#define SANDBOX_PCI_CLASS_CODE		(PCI_CLASS_COMMUNICATION_SERIAL >> 8)
23244557Sbrooks#define SANDBOX_PCI_CLASS_SUB_CODE	(PCI_CLASS_COMMUNICATION_SERIAL & 0xff)
24244557Sbrooks
25244557Sbrooks#define PCI_CAP_ID_PM_OFFSET		0x50
26244557Sbrooks#define PCI_CAP_ID_EXP_OFFSET		0x60
27244557Sbrooks#define PCI_CAP_ID_MSIX_OFFSET		0x70
28244557Sbrooks#define PCI_CAP_ID_EA_OFFSET		0x80
29244557Sbrooks
30244557Sbrooks#define PCI_EXT_CAP_ID_ERR_OFFSET	0x100
31244557Sbrooks#define PCI_EXT_CAP_ID_VC_OFFSET	0x200
32244557Sbrooks#define PCI_EXT_CAP_ID_DSN_OFFSET	0x300
33244557Sbrooks
34244557Sbrooks/* Useful for PCI_VDEVICE() macro */
35244557Sbrooks#define PCI_VENDOR_ID_SANDBOX		SANDBOX_PCI_VENDOR_ID
36244557Sbrooks#define SWAP_CASE_DRV_DATA		0x55aa
37244557Sbrooks
38244557Sbrooks#define SANDBOX_CLK_RATE		32768
39244557Sbrooks
40244557Sbrooks/* Macros used to test PCI EA capability structure */
41244557Sbrooks#define PCI_CAP_EA_BASE_LO0		0x00100000
42#define PCI_CAP_EA_BASE_LO1		0x00110000
43#define PCI_CAP_EA_BASE_LO2		0x00120000
44#define PCI_CAP_EA_BASE_LO4		0x00140000
45#define PCI_CAP_EA_BASE_HI2		0x00020000ULL
46#define PCI_CAP_EA_BASE_HI4		0x00040000ULL
47#define PCI_CAP_EA_SIZE_LO		0x0000ffff
48#define PCI_CAP_EA_SIZE_HI		0x00000010ULL
49#define PCI_EA_BAR2_MAGIC		0x72727272
50#define PCI_EA_BAR4_MAGIC		0x74747474
51
52enum {
53	SANDBOX_IRQN_PEND = 1,	/* Interrupt number for 'pending' test */
54};
55
56/* System controller driver data */
57enum {
58	SYSCON0		= 32,
59	SYSCON1,
60
61	SYSCON_COUNT
62};
63
64/**
65 */
66enum cros_ec_test_t {
67	CROSECT_BREAK_HELLO	= BIT(1),
68	CROSECT_LID_OPEN	= BIT(2),
69};
70
71/**
72 * sandbox_i2c_set_test_mode() - set test mode for running unit tests
73 *
74 * See sandbox_i2c_xfer() for the behaviour changes.
75 *
76 * @bus:	sandbox I2C bus to adjust
77 * @test_mode:	true to select test mode, false to run normally
78 */
79void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode);
80
81enum sandbox_i2c_eeprom_test_mode {
82	SIE_TEST_MODE_NONE,
83	/* Permits read/write of only one byte per I2C transaction */
84	SIE_TEST_MODE_SINGLE_BYTE,
85};
86
87void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
88				      enum sandbox_i2c_eeprom_test_mode mode);
89
90void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len);
91
92void sandbox_i2c_eeprom_set_chip_addr_offset_mask(struct udevice *dev,
93						  uint mask);
94
95uint sanbox_i2c_eeprom_get_prev_addr(struct udevice *dev);
96
97uint sanbox_i2c_eeprom_get_prev_offset(struct udevice *dev);
98
99/**
100 * sandbox_i2c_rtc_set_offset() - set the time offset from system/base time
101 *
102 * @dev:		RTC device to adjust
103 * @use_system_time:	true to use system time, false to use @base_time
104 * @offset:		RTC offset from current system/base time (-1 for no
105 *			change)
106 * Return: old value of RTC offset
107 */
108long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
109				int offset);
110
111/**
112 * sandbox_i2c_rtc_get_set_base_time() - get and set the base time
113 *
114 * @dev:		RTC device to adjust
115 * @base_time:		New base system time (set to -1 for no change)
116 * Return: old base time
117 */
118long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time);
119
120int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str);
121
122/**
123 * sandbox_osd_get_mem() - get the internal memory of a sandbox OSD
124 *
125 * @dev:	OSD device for which to access the internal memory for
126 * @buf:	pointer to buffer to receive the OSD memory data
127 * @buflen:	length of buffer in bytes
128 */
129int sandbox_osd_get_mem(struct udevice *dev, u8 *buf, size_t buflen);
130
131/**
132 * sandbox_pwm_get_config() - get the PWM config for a channel
133 *
134 * @dev: Device to check
135 * @channel: Channel number to check
136 * @period_ns: Period of the PWM in nanoseconds
137 * @duty_ns: Current duty cycle of the PWM in nanoseconds
138 * @enable: true if the PWM is enabled
139 * @polarity: true if the PWM polarity is active high
140 * Return: 0 if OK, -ENOSPC if the PWM number is invalid
141 */
142int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp,
143			   uint *duty_nsp, bool *enablep, bool *polarityp);
144
145/**
146 * sandbox_sf_set_block_protect() - Set the BP bits of the status register
147 *
148 * @dev: Device to update
149 * @bp_mask: BP bits to set (bits 2:0, so a value of 0 to 7)
150 */
151void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask);
152
153/**
154 * sandbox_get_codec_params() - Read back codec parameters
155 *
156 * This reads back the parameters set by audio_codec_set_params() for the
157 * sandbox audio driver. Arguments are as for that function.
158 */
159void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
160			      int *mclk_freqp, int *bits_per_samplep,
161			      uint *channelsp);
162
163/**
164 * sandbox_get_i2s_sum() - Read back the sum of the audio data so far
165 *
166 * This data is provided to the sandbox driver by the I2S tx_data() method.
167 *
168 * @dev: Device to check
169 * Return: sum of audio data
170 */
171int sandbox_get_i2s_sum(struct udevice *dev);
172
173/**
174 * sandbox_get_setup_called() - Returns the number of times setup(*) was called
175 *
176 * This is used in the sound test
177 *
178 * @dev: Device to check
179 * Return: call count for the setup() method
180 */
181int sandbox_get_setup_called(struct udevice *dev);
182
183/**
184 * sandbox_get_sound_active() - Returns whether sound play is in progress
185 *
186 * Return: true if active, false if not
187 */
188int sandbox_get_sound_active(struct udevice *dev);
189
190/**
191 * sandbox_get_sound_count() - Read back the count of the sound data so far
192 *
193 * This data is provided to the sandbox driver by the sound play() method.
194 *
195 * @dev: Device to check
196 * Return: count of audio data
197 */
198int sandbox_get_sound_count(struct udevice *dev);
199
200/**
201 * sandbox_get_sound_sum() - Read back the sum of the sound data so far
202 *
203 * This data is provided to the sandbox driver by the sound play() method.
204 *
205 * @dev: Device to check
206 * Return: sum of audio data
207 */
208int sandbox_get_sound_sum(struct udevice *dev);
209
210/**
211 * sandbox_set_allow_beep() - Set whether the 'beep' interface is supported
212 *
213 * @dev: Device to update
214 * @allow: true to allow the start_beep() method, false to disallow it
215 */
216void sandbox_set_allow_beep(struct udevice *dev, bool allow);
217
218/**
219 * sandbox_get_beep_frequency() - Get the frequency of the current beep
220 *
221 * @dev: Device to check
222 * Return: frequency of beep, if there is an active beep, else 0
223 */
224int sandbox_get_beep_frequency(struct udevice *dev);
225
226/**
227 * sandbox_spi_get_speed() - Get current speed setting of a sandbox spi bus
228 *
229 * @dev: Device to check
230 * Return: current bus speed
231 */
232uint sandbox_spi_get_speed(struct udevice *dev);
233
234/**
235 * sandbox_spi_get_mode() - Get current mode setting of a sandbox spi bus
236 *
237 * @dev: Device to check
238 * Return: current mode
239 */
240uint sandbox_spi_get_mode(struct udevice *dev);
241
242/**
243 * sandbox_get_pch_spi_protect() - Get the PCI SPI protection status
244 *
245 * @dev: Device to check
246 * Return: 0 if not protected, 1 if protected
247 */
248int sandbox_get_pch_spi_protect(struct udevice *dev);
249
250/**
251 * sandbox_get_pci_ep_irq_count() - Get the PCI EP IRQ count
252 *
253 * @dev: Device to check
254 * Return: irq count
255 */
256int sandbox_get_pci_ep_irq_count(struct udevice *dev);
257
258/**
259 * sandbox_pci_read_bar() - Read the BAR value for a read_config operation
260 *
261 * This is used in PCI emulators to read a base address reset. This has special
262 * rules because when the register is set to 0xffffffff it can be used to
263 * discover the type and size of the BAR.
264 *
265 * @barval: Current value of the BAR
266 * @type: Type of BAR (PCI_BASE_ADDRESS_SPACE_IO or
267 *		PCI_BASE_ADDRESS_MEM_TYPE_32)
268 * @size: Size of BAR in bytes
269 * Return: BAR value to return from emulator
270 */
271uint sandbox_pci_read_bar(u32 barval, int type, uint size);
272
273/**
274 * sandbox_set_enable_memio() - Enable readl/writel() for sandbox
275 *
276 * Normally these I/O functions do nothing with sandbox. Certain tests need them
277 * to work as for other architectures, so this function can be used to enable
278 * them.
279 *
280 * @enable: true to enable, false to disable
281 */
282void sandbox_set_enable_memio(bool enable);
283
284/**
285 * sandbox_cros_ec_set_test_flags() - Set behaviour for testing purposes
286 *
287 * @dev: Device to check
288 * @flags: Flags to control behaviour (CROSECT_...)
289 */
290void sandbox_cros_ec_set_test_flags(struct udevice *dev, uint flags);
291
292/**
293 * sandbox_cros_ec_get_pwm_duty() - Get EC PWM config for testing purposes
294 *
295 * @dev: Device to check
296 * @index: PWM channel index
297 * @duty: Current duty cycle in 0..EC_PWM_MAX_DUTY range.
298 * Return: 0 if OK, -ENOSPC if the PWM number is invalid
299 */
300int sandbox_cros_ec_get_pwm_duty(struct udevice *dev, uint index, uint *duty);
301
302/**
303 * sandbox_set_fake_efi_mgr_dev() - Control EFI bootmgr producing valid bootflow
304 *
305 * This is only used for testing.
306 *
307 * @dev: efi_mgr bootmeth device
308 * @fake_dev: true to produce a valid bootflow when requested, false to produce
309 * an error
310 */
311void sandbox_set_fake_efi_mgr_dev(struct udevice *dev, bool fake_dev);
312
313/**
314 * sandbox_load_other_fdt() - load the 'other' FDT into the test state
315 *
316 * This copies the other.dtb file into the test state, so that a fresh version
317 * can be used for a test that is about to run.
318 *
319 * If @uts->other_fdt is NULL, as it is when first set up, this allocates a
320 * buffer for the other FDT and sets @uts->other_fdt_size to its size.
321 *
322 * In any case, the other FDT is copied from the sandbox state into
323 * @uts->other_fdt ready for use.
324 *
325 * @uts: Unit test state
326 * @return 0 if OK, -ve on error
327 */
328int sandbox_load_other_fdt(void **fdtp, int *sizep);
329
330/**
331 * sandbox_set_eth_enable() - Enable / disable Ethernet
332 *
333 * Allows control of whether Ethernet packets are actually send/received
334 *
335 * @enable: true to enable Ethernet, false to disable
336 */
337void sandbox_set_eth_enable(bool enable);
338
339/**
340 * sandbox_eth_enabled() - Check if Ethernet is enabled
341 *
342 * Returns: true if Ethernet is enabled on sandbox, False if not
343 */
344bool sandbox_eth_enabled(void);
345
346/**
347 * sandbox_sf_bootdev_enabled() - Check if SPI flash bootdevs should be bound
348 *
349 * Returns: true if sandbox should bind bootdevs for SPI flash, false if not
350 */
351bool sandbox_sf_bootdev_enabled(void);
352
353/**
354 * sandbox_sf_set_enable_bootdevs() - Enable / disable the SPI flash bootdevs
355 *
356 * @enable: true to bind the SPI flash bootdevs, false to skip
357 */
358void sandbox_sf_set_enable_bootdevs(bool enable);
359
360#endif
361