blob: fed1bdd58d30a63a20784aeff01809fa7b92e2cf [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001
2#ifndef __SPI_DRV_HAL_H__
3/* !ignore me! */
4#define __SPI_DRV_HAL_H__
5
6#include "spi.h"
7#include "drv_features.h"
8#include "kal_general_types.h"
9
10/* !ignore me! */
11#if defined(DRV_SPI_SUPPORT) && defined(DRV_SPI_HAL)
12
13/* SPI ports number. */
14#define SPI_MAX_PORT_NUM 2
15
16/* SPI Handle.
17 SPI handle can be retrieved from spi_open(). It is used for most SPI APIs.
18*/
19typedef kal_uint32 SPI_HANDLE;
20
21/* <GROUP CallBackFunctions>
22 FUNCTION
23 SPI_CALLBACK
24 DESCRIPTION
25 SPI callback function
26 SPI callback function is called when SPI interrupt has arrived. It indicates SPI has finished the last data transfer.
27 SPI callback function is called in the context of HISR.
28*/
29typedef void (*SPI_CALLBACK)(void);
30
31/* SPI result enum.
32*/
33typedef enum
34{
35 /* Indicates no error occurs in the last operation. */
36 SPI_RESULT_OK,
37
38 /* Indicates some errors have occured in the last operation. */
39 SPI_RESULT_ERROR,
40
41 /* Indicates some errors have occured in the last operation. */
42 SPI_RESULT_INVALID_ARGUMENT,
43
44 /* Indicates the function is not supported on the current platform. */
45 SPI_RESULT_NOT_SUPPORTED
46} SPI_RESULT;
47
48/*************************************************************************************************************************************
49 * SPI IOCTL code definition.
50 * For detail description and usage of SPI control codes, please refer to <xref target="SPI HAL IOCTL Code" text="SPI IOCTL Code" />.
51 *************************************************************************************************************************************/
52typedef enum
53{
54 /****************************************************************************************************************
55 * Get the current SPI configuration parameters.
56 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_GET_PARAM" />.
57 ****************************************************************************************************************/
58 SPI_IOCTL_GET_PARAM,
59 /****************************************************************************************************************
60 * Get the capability of current SPI hardware.
61 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_QUERY_CAPABILITY" />.
62 ****************************************************************************************************************/
63 SPI_IOCTL_QUERY_CAPABILITY,
64 /*************************************************************************************************************************
65 * Query supported SPI modes.
66 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_QUERY_MODE_SUPPORT" />.
67 *************************************************************************************************************************/
68 SPI_IOCTL_QUERY_MODE_SUPPORT,
69 /******************************************************************************************************************
70 * Query SPI base clock.
71 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_QUERY_CLOCK" />.
72 ******************************************************************************************************************/
73 SPI_IOCTL_QUERY_CLOCK,
74 /***************************************************************************************************************
75 * Set and configure SPI mode.
76 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_SET_MODE" />.
77 ***************************************************************************************************************/
78 SPI_IOCTL_SET_MODE,
79 /***************************************************************************************************************
80 * Get SPI mode settings.
81 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_GET_MODE" />.
82 ***************************************************************************************************************/
83 SPI_IOCTL_GET_MODE,
84 /***************************************************************************************************************
85 * Get SPI driving current.
86 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_GET_DRIVING_CURRENT" />.
87 ***************************************************************************************************************/
88 SPI_IOCTL_GET_DRIVING_CURRENT,
89 /***************************************************************************************************************
90 * Set SPI driving current.
91 * For detail description and usage of this control code, please refer to <xref target="SPI_IOCTL_SET_DRIVING_CURRENT" />.
92 ***************************************************************************************************************/
93 SPI_IOCTL_SET_DRIVING_CURRENT
94} SPI_IOCTL_CODE;
95
96/* SPI mode. Including special modes provided by SPI controller.
97*/
98typedef enum
99{
100 /* Deassert mode. SPI CS pin will be pulled low and high for each byte during transmission. */
101 SPI_MODE_DEASSERT,
102 /* Pause mode. SPI CS pin is pulled low and keeps until specific amount of transfers have been finished. */
103 SPI_MODE_PAUSE,
104 /* Ultra high mode. Raise DMA priority during DMA transmission. */
105 SPI_MODE_ULTRA_HIGH,
106 /* Slow down mode. Slow down SPI DMA speed during DMA transmission. */
107 SPI_MODE_SLOW_DOWN,
108 /* Get tick delay mode. This mode is used to tuning SPI timing. */
109 SPI_MODE_GET_TICK
110} SPI_HAL_MODE;
111
112/*********************************
113 * SPI get tick delay mode enums.
114 *********************************/
115typedef enum
116{
117 /* Delay SCK for one clock. */
118 SPI_GET_TICK_DELAY1 = 1,
119 /* Delay SCK for two clocks. */
120 SPI_GET_TICK_DELAY2 = 2
121} SPI_GET_TICK_E;
122
123/* SPI transimssion bit order definition. */
124typedef enum
125{
126 /* Transfer LSB first. */
127 SPI_MSBF_LSB = 0,
128 /* Transfer MSB first. */
129 SPI_MSBF_MSB = 1
130} SPI_MSBF_E;
131
132/* SPI clock polarity definition. */
133typedef enum
134{
135 /* CPOL = 0. */
136 SPI_CPOL_B0 = 0,
137 /* CPOL = 1. */
138 SPI_CPOL_B1 = 1
139} SPI_CPOL_E;
140
141/* SPI clock format definition. */
142typedef enum
143{
144 /* CPHA = 0. */
145 SPI_CPHA_B0 = 0,
146 /* CPHA = 1. */
147 SPI_CPHA_B1 = 1
148} SPI_CPHA_E;
149
150/* SPI data transfer byte order definition. */
151typedef enum
152{
153 /* Use little endian. */
154 SPI_ENDIAN_LITTLE = 0,
155 /* Use big endian. */
156 SPI_ENDIAN_BIG = 1
157} SPI_ENDIAN_E;
158
159/************************************
160 * SPI configuration parameters.
161 *
162 * Remarks
163 * <img name="spi_timing_diagram" />
164 ************************************/
165typedef struct
166{
167 /* CS setup time. Unit in count of SPI base clock. Range(0-255).
168 The chip select setup time = (cs_setup_time+1)*CLK_PERIOD, where CLK_PERIOD is the cycle time of the clock the SPI engine adopts. */
169 kal_uint32 cs_setup_time;
170 /* CS hold time. Unit in count of SPI base clock. Range(0-255).
171 The chip select hold time = (cs_hold_time+1)*CLK_PERIOD. */
172 kal_uint32 cs_hold_time;
173 /* CS idle time. Unit in count of SPI base clock. Range(0-255).
174 The chip select idle time between consecutive transaction = (cs_idle_time+1)*CLK_PERIOD. */
175 kal_uint32 cs_idle_time;
176 /* SCK clock low time. Unit in count of SPI base clock. Range(0-255).
177 The SCK clock low time = (clk_low_time+1)*CLK_PERIOD. */
178 kal_uint32 clk_low_time;
179 /* SCK clock high time. Unit in count of SPI base clock. Range(0-255).
180 The SCK clock high time = (clk_high_time+1)*CLK_PERIOD. */
181 kal_uint32 clk_high_time;
182 /* Bit order setting for SPI output. */
183 SPI_MSBF_E tx_msbf;
184 /* Bit order setting for SPI input. */
185 SPI_MSBF_E rx_msbf;
186 /* Byte order setting for SPI output. */
187 SPI_ENDIAN_E tx_endian;
188 /* Byte order setting for SPI input. */
189 SPI_ENDIAN_E rx_endian;
190 /* SPI clock polarity. */
191 SPI_CPOL_E clk_polarity;
192 /* SPI clock format. */
193 SPI_CPHA_E clk_fmt;
194} SPI_CONFIG_PARAM_T;
195
196/* This structure is used as the parameter of SPI_IOCTL_QUERY_CAPABILITY.
197*/
198typedef struct
199{
200 /* Minimum value for cs setup time. */
201 kal_uint32 cs_setup_time_min;
202 /* Maximum value for cs setup time. */
203 kal_uint32 cs_setup_time_max;
204 /* Minimum value for cs hold time. */
205 kal_uint32 cs_hold_time_min;
206 /* Maximum value for cs hold time. */
207 kal_uint32 cs_hold_time_max;
208 /* Minimum value for cs idle time. */
209 kal_uint32 cs_idle_time_min;
210 /* Maximum value for cs idle time. */
211 kal_uint32 cs_idle_time_max;
212 /* Minimum value for clock low time. */
213 kal_uint32 clk_low_time_min;
214 /* Maximum value for clock low time. */
215 kal_uint32 clk_low_time_max;
216 /* Minimum value for clock high time. */
217 kal_uint32 clk_high_time_min;
218 /* Maximum value for clock high time. */
219 kal_uint32 clk_high_time_max;
220 /* Minimum value for transfer length. */
221 kal_uint32 transfer_length_min;
222 /* Maximum value for transfer length. */
223 kal_uint32 transfer_length_max;
224 /* Minimum value for transfer count. */
225 kal_uint32 transfer_count_min;
226 /* Maximum value for transfer count. */
227 kal_uint32 transfer_count_max;
228 /* Minimum value for slow down threshold. */
229 kal_uint32 slow_down_thresh_min;
230 /* Maximum value for slow down threshold. */
231 kal_uint32 slow_down_thresh_max;
232 /* Minimum value for ultra high threshold. */
233 kal_uint32 ultra_high_thresh_min;
234 /* Maximum value for ultra high threshold. */
235 kal_uint32 ultra_high_thresh_max;
236 /* Minimum value for driving current. */
237 kal_uint32 driving_current_min;
238 /* Maximum value for driving current. */
239 kal_uint32 driving_current_max;
240} SPI_CAPABILITY_T;
241
242/* This structure is used as the parameter of SPI_IOCTL_QUERY_MODE_SUPPORT.
243*/
244typedef struct
245{
246 /* [IN] Specify a SPI mode. */
247 SPI_HAL_MODE mode;
248 /* [OUT] Return whether the specific mode is supported. */
249 kal_bool bSupport;
250} SPI_QUERY_MODE_T;
251
252/* This structure is used as the parameter of SPI_IOCTL_QUERY_CLOCK.
253 It contains the clock frequency which is used to calculate SPI timing parameters.
254*/
255typedef struct
256{
257 /* [OUT] SPI base clock. Unit in MHz. */
258 kal_uint32 clock;
259} SPI_QUERY_CLOCK_T;
260
261/********************************************************************
262 * This structure is used as the parameter of SPI_IOCTL_SET_MODE and SPI_IOCTL_GET_MODE.
263 ********************************************************************/
264typedef struct
265{
266 /* [IN] Specify a SPI mode. */
267 SPI_HAL_MODE mode;
268 /* [IN/OUT] Specify whether the mode is enabled.
269 For SPI_IOCTL_SET_MODE, it is an input parameter.
270 For SPI_IOCTL_GET_MODE, it is an output parameter.
271 */
272 kal_bool bEnable;
273 /* [IN/OUT] The parameter for the specific mode.
274 The meaning of this parameter depends on the mode to be set/get.
275 */
276 kal_uint32 Param;
277} SPI_MODE_T;
278
279/* This structure is used as the parameter of SPI_MODE_T for SPI_MODE_SLOW_DOWN.
280 If user uses SPI_IOCTL_SET_MODE or SPI_IOCTL_GET_MODE to set/get SPI DMA slow down mode, the Param in SPI_MODE_T is a pointer to this structure.
281*/
282typedef struct
283{
284 /* This parameter decides whether to enable splitting burst one 4-beat read/write access into four single read/write accesses.*/
285 kal_bool split_burst_enable;
286 /* This parameter is a counter to configure how many time units to send one request to GMC.*/
287 kal_uint32 slow_down_thresh;
288} SPI_SLOWDOWN_PARAM_T;
289
290/* This structure is used as the parameter of SPI_IOCTL_GET_DRIVING_CURRENT and SPI_IOCTL_SET_DRIVING_CURRENT.
291*/
292typedef struct
293{
294 /* Indicates whether slew rate control is enabled. */
295 kal_bool bEnableSlewRate;
296 /* Driving current value.
297 <b> <color Red>Note: This variable just indicates the value that is set in the hardware configuration register. It does not stands fot the actual driving current.
298 Refer to related hardware spec for detail information.</b></color>
299 */
300 kal_uint32 driving_current;
301} SPI_DRIVING_CURRENT_T;
302
303/**************************************************************************************************************************************
304 * Function
305 * spi_open
306 * Description
307 * Open and get SPI handle.
308 * Parameters
309 * port : [IN] SPI port. Zero based port index.
310 * For <color Blue>MT6238</color>/<color Blue>MT6268</color>/<color Blue>MT6236</color>, only one port is supported.
311 * Returns
312 * Return a SPI handle.
313 * Example
314 * <code>
315 * SPI_HANDLE handle;
316 * handle = spi_open(0);
317 * </code>
318 **************************************************************************************************************************************/
319SPI_HANDLE spi_open(kal_uint32 port);
320
321/********************************************************
322 * Function
323 * spi_close
324 * Description
325 * Release a SPI handle.
326 * Parameters
327 * handle : [IN] SPI handle. Retrieved from spi_open().
328 * Returns
329 * Return a SPI result code.
330 * Example
331 * <code>
332 * SPI_HANDLE handle;
333 * handle = spi_open(0);
334 * // SPI operations.
335 * spi_close(handle);
336 * </code>
337 ********************************************************/
338SPI_RESULT spi_close(SPI_HANDLE handle);
339
340/***********************************************************************
341 * Function
342 * spi_configure
343 * Description
344 * Configure SPI parameters including timing parameters and waveform parameters.
345 * SPI parameters must be set before any SPI transmissions.
346 * Parameters
347 * handle : [IN] SPI handle. Retrieved from spi_open().
348 * pConfigParam : [IN] SPI parameters. Refer to SPI_CONFIG_PARAM_T for details.
349 * Returns
350 * Return a SPI result code.
351 * Example
352 * <code>
353 * SPI_HANDLE handle;
354 * SPI_CONFIG_PARAM_T param;
355 * handle = spi_open(0);
356 * param.cs_setup_time = 5;
357 * param.cs_hold_time = 5;
358 * param.cs_idle_time = 5;
359 * param.clk_low_time = 5;
360 * param.clk_high_time = 5;
361 * param.tx_mlsb = SPI_LSB;
362 * param.rx_mlsb = SPI_LSB;
363 * param.clk_polarity = SPI_CPOL_0;
364 * param.clk_fmt = SPI_CPHA_0;
365 * spi_configure(handle, &amp;param);
366 * // ...
367 * spi_close(handle);
368 * </code>
369 ***********************************************************************/
370SPI_RESULT spi_configure(SPI_HANDLE handle, SPI_CONFIG_PARAM_T* pConfigParam);
371
372/*******************************************************************************
373 * Function
374 * spi_write
375 * Description
376 * Fire a SPI transmission for output.
377 * Parameters
378 * handle : [IN] SPI handle. Retrieved from spi_open().
379 * pBuffer : [IN] Data buffer for output.
380 * length : [IN] Data length for one package. Unit in bytes. <color Red><b>If count &gt; 1, length must be 4X.</b></color>
381 * count : [IN] Package count.<b> <color Red>If count &gt; 1, length must be 4X.</b></color>
382 * fCB : [IN] Specifies the callback function when the transmission completes.
383 * If fCB is specified, SPI driver uses interrupt mode. This API is asynchronous.
384 * If fCB is NULL, SPI drvier uses polling mode. This API becomes synchronous.
385 * Returns
386 * Return a SPI result code.
387 * Example
388 * <code>
389 * SPI_HANDLE handle;
390 * SPI_CONFIG_PARAM_T param;
391 * handle = spi_open(0);
392 * // ...
393 * spi_configure(handle, &amp;param);
394 *
395 * // Synchronous call.
396 * spi_write(handle, spi_test_buffer1, 1024, 1, NULL);
397 *
398 * // Asynchronous call.
399 * spi_write(handle, spi_test_buffer1, 1024, 1, spi_test_cb);
400 * // Wait for a event which is set in callback function.
401 * // ...
402 * spi_close(handle);
403 * </code>
404 *******************************************************************************/
405SPI_RESULT spi_write(SPI_HANDLE handle, void* pBuffer, kal_uint32 length, kal_uint32 count, SPI_CALLBACK fCB);
406
407/*******************************************************************************
408 * Function
409 * spi_read
410 * Description
411 * Fire a SPI transmission for input.
412 * Parameters
413 * handle : [IN] SPI handle. Retrieved from spi_open().
414 * pBuffer : [OUT] Data buffer for input.
415 * length : [IN] Data length for one package. Unit in bytes. <color Red><b>If count &gt; 1, length must be 4X.</b></color>
416 * count : [IN] Package count. <color Red><b>If count &gt; 1, length must be 4X.</b></color>
417 * fCB : [IN] Specifies the callback function when the transmission completes.
418 * If fCB is specified, SPI driver uses interrupt mode. This API is asynchronous.
419 * If fCB is NULL, SPI drvier uses polling mode. This API becomes synchronous.
420 * Returns
421 * Return a SPI result code.
422 * Example
423 * <code>
424 * SPI_HANDLE handle;
425 * SPI_CONFIG_PARAM_T param;
426 * handle = spi_open(0);
427 * // ...
428 * spi_configure(handle, &amp;param);
429 *
430 * // Synchronous call.
431 * spi_read(handle, spi_test_buffer2, 1024, 1, NULL);
432 *
433 * // Asynchronous call.
434 * spi_read(handle, spi_test_buffer2, 1024, 1, spi_test_cb);
435 * // Wait for a event which is set in callback function.
436 * // ...
437 * spi_close(handle);
438 * </code>
439 *******************************************************************************/
440SPI_RESULT spi_read(SPI_HANDLE handle, void* pBuffer, kal_uint32 length, kal_uint32 count, SPI_CALLBACK fCB);
441
442/***********************************************************************************
443 * Function
444 * spi_readwrite
445 * Description
446 * Fire a SPI transmission for input and output. This transaction uses
447 * duplex mode of SPI.
448 * Parameters
449 * handle : [IN] SPI handle. Retrieved from spi_open().
450 * pOutBuffer : [IN] Data buffer for output.
451 * pInBuffer : [OUT] Data buffer for input.
452 * length : [IN] Data length for one package. Unit in bytes. <color Red><b>If count &gt; 1, length must be 4X.</b></color>
453 * count : [IN] Package count. <color Red><b>If count &gt; 1, length must be 4X.</b></color>
454 * fCB : [IN] Specifies the callback function when the transmission completes.
455 * If fCB is specified, SPI driver uses interrupt mode. This API is asynchronous.
456 * If fCB is NULL, SPI drvier uses polling mode. This API becomes synchronous.
457 * Returns
458 * Return a SPI result code.
459 * Example
460 * <code>
461 * SPI_HANDLE handle;
462 * SPI_CONFIG_PARAM_T param;
463 * handle = spi_open(0);
464 * // ...
465 * spi_configure(handle, &amp;param);
466 *
467 * // Synchronous call.
468 * spi_readwrite(handle, spi_test_buffer1, spi_test_buffer2, 1024, 1, NULL);
469 *
470 * // Asynchronous call.
471 * spi_readwrite(handle, spi_test_buffer1, spi_test_buffer2, 1024, 1, spi_test_cb);
472 * // Wait for a event which is set in callback function.
473 * // ...
474 * spi_close(handle);
475 * </code>
476 ***********************************************************************************/
477SPI_RESULT spi_readwrite(SPI_HANDLE handle, void* pOutBuffer, void* pInBuffer, kal_uint32 length, kal_uint32 count, SPI_CALLBACK fCB);
478
479/*****************************************************************************
480 * Function
481 * spi_power_ctrl
482 * Description
483 * Enable or disable SPI Controller power.
484 * <textattr color="Red">This API is currently not implemented. SPI power will be turned on when calling spi_open() and turned off when calling</textattr> spi_close().
485 * Parameters
486 * handle : [IN] SPI handle. Retrieved from spi_open().
487 * bPowerOn : [IN] Set KAL_TRUE to power on SPI Controller. Set KAL_FALSE to power off SPI Controller.
488 * Returns
489 * Return a SPI result code.
490 * Example
491 * <code>
492 * SPI_HANDLE handle;
493 * handle = spi_open(0);
494 * spi_power_ctrl(handle, KAL_TRUE);
495 * // SPI operations.
496 * spi_power_ctrl(handle, KAL_FALSE);
497 * spi_close(handle);
498 * </code>
499 *****************************************************************************/
500SPI_RESULT spi_power_ctrl(SPI_HANDLE handle, kal_bool bPowerOn);
501
502/*****************************************************************************
503 * Function
504 * spi_ioctl
505 * Description
506 * IO Control for SPI driver.
507 * A series of SPI control code is defined for extension usage. Refer to SPI_IOCTL_CODE.
508 * Parameters
509 * handle : [IN] SPI handle. Retrieved from spi_open().
510 * code : [IN] SPI control code. Refer to SPI_IOCTL_CODE.
511 * pParam : [IN/OUT] Parameter for SPI IO Control. The definition depends on the specified control code. Refer to SPI_IOCTL_CODE.
512 * Returns
513 * Return a SPI result code.
514 * The return value is SPI_RESULT_NOT_SUPPORTED if the current platform does not support the sepcific control code.
515 * Example
516 * <code>
517 * SPI_HANDLE handle;
518 * SPI_MODE_T SetMode;
519 * handle = spi_open(0);
520 * // Test for deassert mode.
521 * SetMode.mode = SPI_MODE_DEASSERT;
522 * SetMode.bEnable = KAL_TRUE;
523 * spi_ioctl(handle, SPI_IOCTL_SET_MODE, &amp;SetMode);
524 * spi_readwrite(handle, spi_test_buffer1, spi_test_buffer2, 1024, 1, NULL);
525 * SetMode.bEnable = KAL_FALSE;
526 * spi_ioctl(handle, SPI_IOCTL_SET_MODE, &amp;SetMode);
527 * spi_close(handle);
528 * </code>
529 *****************************************************************************/
530SPI_RESULT spi_ioctl(SPI_HANDLE handle, SPI_IOCTL_CODE code, void* pParam);
531
532#endif
533#endif