1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2019, STMicroelectronics - All Rights Reserved 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #ifndef DRIVERS_SPI_MEM_H 8*91f16700Schasinglulu #define DRIVERS_SPI_MEM_H 9*91f16700Schasinglulu 10*91f16700Schasinglulu #include <errno.h> 11*91f16700Schasinglulu #include <stdbool.h> 12*91f16700Schasinglulu #include <stdint.h> 13*91f16700Schasinglulu 14*91f16700Schasinglulu #define SPI_MEM_BUSWIDTH_1_LINE 1U 15*91f16700Schasinglulu #define SPI_MEM_BUSWIDTH_2_LINE 2U 16*91f16700Schasinglulu #define SPI_MEM_BUSWIDTH_4_LINE 4U 17*91f16700Schasinglulu 18*91f16700Schasinglulu /* 19*91f16700Schasinglulu * enum spi_mem_data_dir - Describes the direction of a SPI memory data 20*91f16700Schasinglulu * transfer from the controller perspective. 21*91f16700Schasinglulu * @SPI_MEM_DATA_IN: data coming from the SPI memory. 22*91f16700Schasinglulu * @SPI_MEM_DATA_OUT: data sent to the SPI memory. 23*91f16700Schasinglulu */ 24*91f16700Schasinglulu enum spi_mem_data_dir { 25*91f16700Schasinglulu SPI_MEM_DATA_IN, 26*91f16700Schasinglulu SPI_MEM_DATA_OUT, 27*91f16700Schasinglulu }; 28*91f16700Schasinglulu 29*91f16700Schasinglulu /* 30*91f16700Schasinglulu * struct spi_mem_op - Describes a SPI memory operation. 31*91f16700Schasinglulu * 32*91f16700Schasinglulu * @cmd.buswidth: Number of IO lines used to transmit the command. 33*91f16700Schasinglulu * @cmd.opcode: Operation opcode. 34*91f16700Schasinglulu * @addr.nbytes: Number of address bytes to send. Can be zero if the operation 35*91f16700Schasinglulu * does not need to send an address. 36*91f16700Schasinglulu * @addr.buswidth: Number of IO lines used to transmit the address. 37*91f16700Schasinglulu * @addr.val: Address value. This value is always sent MSB first on the bus. 38*91f16700Schasinglulu * Note that only @addr.nbytes are taken into account in this 39*91f16700Schasinglulu * address value, so users should make sure the value fits in the 40*91f16700Schasinglulu * assigned number of bytes. 41*91f16700Schasinglulu * @dummy.nbytes: Number of dummy bytes to send after an opcode or address. Can 42*91f16700Schasinglulu * be zero if the operation does not require dummy bytes. 43*91f16700Schasinglulu * @dummy.buswidth: Number of IO lines used to transmit the dummy bytes. 44*91f16700Schasinglulu * @data.buswidth: Number of IO lines used to send/receive the data. 45*91f16700Schasinglulu * @data.dir: Direction of the transfer. 46*91f16700Schasinglulu * @data.nbytes: Number of data bytes to transfer. 47*91f16700Schasinglulu * @data.buf: Input or output data buffer depending on data::dir. 48*91f16700Schasinglulu */ 49*91f16700Schasinglulu struct spi_mem_op { 50*91f16700Schasinglulu struct { 51*91f16700Schasinglulu uint8_t buswidth; 52*91f16700Schasinglulu uint8_t opcode; 53*91f16700Schasinglulu } cmd; 54*91f16700Schasinglulu 55*91f16700Schasinglulu struct { 56*91f16700Schasinglulu uint8_t nbytes; 57*91f16700Schasinglulu uint8_t buswidth; 58*91f16700Schasinglulu uint64_t val; 59*91f16700Schasinglulu } addr; 60*91f16700Schasinglulu 61*91f16700Schasinglulu struct { 62*91f16700Schasinglulu uint8_t nbytes; 63*91f16700Schasinglulu uint8_t buswidth; 64*91f16700Schasinglulu } dummy; 65*91f16700Schasinglulu 66*91f16700Schasinglulu struct { 67*91f16700Schasinglulu uint8_t buswidth; 68*91f16700Schasinglulu enum spi_mem_data_dir dir; 69*91f16700Schasinglulu unsigned int nbytes; 70*91f16700Schasinglulu void *buf; 71*91f16700Schasinglulu } data; 72*91f16700Schasinglulu }; 73*91f16700Schasinglulu 74*91f16700Schasinglulu /* SPI mode flags */ 75*91f16700Schasinglulu #define SPI_CPHA BIT(0) /* clock phase */ 76*91f16700Schasinglulu #define SPI_CPOL BIT(1) /* clock polarity */ 77*91f16700Schasinglulu #define SPI_CS_HIGH BIT(2) /* CS active high */ 78*91f16700Schasinglulu #define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */ 79*91f16700Schasinglulu #define SPI_3WIRE BIT(4) /* SI/SO signals shared */ 80*91f16700Schasinglulu #define SPI_PREAMBLE BIT(5) /* Skip preamble bytes */ 81*91f16700Schasinglulu #define SPI_TX_DUAL BIT(6) /* transmit with 2 wires */ 82*91f16700Schasinglulu #define SPI_TX_QUAD BIT(7) /* transmit with 4 wires */ 83*91f16700Schasinglulu #define SPI_RX_DUAL BIT(8) /* receive with 2 wires */ 84*91f16700Schasinglulu #define SPI_RX_QUAD BIT(9) /* receive with 4 wires */ 85*91f16700Schasinglulu 86*91f16700Schasinglulu struct spi_bus_ops { 87*91f16700Schasinglulu /* 88*91f16700Schasinglulu * Claim the bus and prepare it for communication. 89*91f16700Schasinglulu * 90*91f16700Schasinglulu * @cs: The chip select. 91*91f16700Schasinglulu * Returns: 0 if the bus was claimed successfully, or a negative value 92*91f16700Schasinglulu * if it wasn't. 93*91f16700Schasinglulu */ 94*91f16700Schasinglulu int (*claim_bus)(unsigned int cs); 95*91f16700Schasinglulu 96*91f16700Schasinglulu /* 97*91f16700Schasinglulu * Release the SPI bus. 98*91f16700Schasinglulu */ 99*91f16700Schasinglulu void (*release_bus)(void); 100*91f16700Schasinglulu 101*91f16700Schasinglulu /* 102*91f16700Schasinglulu * Set transfer speed. 103*91f16700Schasinglulu * 104*91f16700Schasinglulu * @hz: The transfer speed in Hertz. 105*91f16700Schasinglulu * Returns: 0 on success, a negative error code otherwise. 106*91f16700Schasinglulu */ 107*91f16700Schasinglulu int (*set_speed)(unsigned int hz); 108*91f16700Schasinglulu 109*91f16700Schasinglulu /* 110*91f16700Schasinglulu * Set the SPI mode/flags. 111*91f16700Schasinglulu * 112*91f16700Schasinglulu * @mode: Requested SPI mode (SPI_... flags). 113*91f16700Schasinglulu * Returns: 0 on success, a negative error code otherwise. 114*91f16700Schasinglulu */ 115*91f16700Schasinglulu int (*set_mode)(unsigned int mode); 116*91f16700Schasinglulu 117*91f16700Schasinglulu /* 118*91f16700Schasinglulu * Execute a SPI memory operation. 119*91f16700Schasinglulu * 120*91f16700Schasinglulu * @op: The memory operation to execute. 121*91f16700Schasinglulu * Returns: 0 on success, a negative error code otherwise. 122*91f16700Schasinglulu */ 123*91f16700Schasinglulu int (*exec_op)(const struct spi_mem_op *op); 124*91f16700Schasinglulu }; 125*91f16700Schasinglulu 126*91f16700Schasinglulu int spi_mem_exec_op(const struct spi_mem_op *op); 127*91f16700Schasinglulu int spi_mem_init_slave(void *fdt, int bus_node, 128*91f16700Schasinglulu const struct spi_bus_ops *ops); 129*91f16700Schasinglulu 130*91f16700Schasinglulu #endif /* DRIVERS_SPI_MEM_H */ 131