Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * Copyright (C) 2018 Exceet Electronics GmbH
0004  * Copyright (C) 2018 Bootlin
0005  *
0006  * Author:
0007  *  Peter Pan <peterpandong@micron.com>
0008  *  Boris Brezillon <boris.brezillon@bootlin.com>
0009  */
0010 
0011 #ifndef __LINUX_SPI_MEM_H
0012 #define __LINUX_SPI_MEM_H
0013 
0014 #include <linux/spi/spi.h>
0015 
0016 #define SPI_MEM_OP_CMD(__opcode, __buswidth)            \
0017     {                           \
0018         .buswidth = __buswidth,             \
0019         .opcode = __opcode,             \
0020         .nbytes = 1,                    \
0021     }
0022 
0023 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth)        \
0024     {                           \
0025         .nbytes = __nbytes,             \
0026         .val = __val,                   \
0027         .buswidth = __buswidth,             \
0028     }
0029 
0030 #define SPI_MEM_OP_NO_ADDR  { }
0031 
0032 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth)          \
0033     {                           \
0034         .nbytes = __nbytes,             \
0035         .buswidth = __buswidth,             \
0036     }
0037 
0038 #define SPI_MEM_OP_NO_DUMMY { }
0039 
0040 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth)     \
0041     {                           \
0042         .dir = SPI_MEM_DATA_IN,             \
0043         .nbytes = __nbytes,             \
0044         .buf.in = __buf,                \
0045         .buswidth = __buswidth,             \
0046     }
0047 
0048 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth)    \
0049     {                           \
0050         .dir = SPI_MEM_DATA_OUT,            \
0051         .nbytes = __nbytes,             \
0052         .buf.out = __buf,               \
0053         .buswidth = __buswidth,             \
0054     }
0055 
0056 #define SPI_MEM_OP_NO_DATA  { }
0057 
0058 /**
0059  * enum spi_mem_data_dir - describes the direction of a SPI memory data
0060  *             transfer from the controller perspective
0061  * @SPI_MEM_NO_DATA: no data transferred
0062  * @SPI_MEM_DATA_IN: data coming from the SPI memory
0063  * @SPI_MEM_DATA_OUT: data sent to the SPI memory
0064  */
0065 enum spi_mem_data_dir {
0066     SPI_MEM_NO_DATA,
0067     SPI_MEM_DATA_IN,
0068     SPI_MEM_DATA_OUT,
0069 };
0070 
0071 /**
0072  * struct spi_mem_op - describes a SPI memory operation
0073  * @cmd.nbytes: number of opcode bytes (only 1 or 2 are valid). The opcode is
0074  *      sent MSB-first.
0075  * @cmd.buswidth: number of IO lines used to transmit the command
0076  * @cmd.opcode: operation opcode
0077  * @cmd.dtr: whether the command opcode should be sent in DTR mode or not
0078  * @addr.nbytes: number of address bytes to send. Can be zero if the operation
0079  *       does not need to send an address
0080  * @addr.buswidth: number of IO lines used to transmit the address cycles
0081  * @addr.dtr: whether the address should be sent in DTR mode or not
0082  * @addr.val: address value. This value is always sent MSB first on the bus.
0083  *        Note that only @addr.nbytes are taken into account in this
0084  *        address value, so users should make sure the value fits in the
0085  *        assigned number of bytes.
0086  * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
0087  *        be zero if the operation does not require dummy bytes
0088  * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
0089  * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not
0090  * @data.buswidth: number of IO lanes used to send/receive the data
0091  * @data.dtr: whether the data should be sent in DTR mode or not
0092  * @data.ecc: whether error correction is required or not
0093  * @data.dir: direction of the transfer
0094  * @data.nbytes: number of data bytes to send/receive. Can be zero if the
0095  *       operation does not involve transferring data
0096  * @data.buf.in: input buffer (must be DMA-able)
0097  * @data.buf.out: output buffer (must be DMA-able)
0098  */
0099 struct spi_mem_op {
0100     struct {
0101         u8 nbytes;
0102         u8 buswidth;
0103         u8 dtr : 1;
0104         u16 opcode;
0105     } cmd;
0106 
0107     struct {
0108         u8 nbytes;
0109         u8 buswidth;
0110         u8 dtr : 1;
0111         u64 val;
0112     } addr;
0113 
0114     struct {
0115         u8 nbytes;
0116         u8 buswidth;
0117         u8 dtr : 1;
0118     } dummy;
0119 
0120     struct {
0121         u8 buswidth;
0122         u8 dtr : 1;
0123         u8 ecc : 1;
0124         enum spi_mem_data_dir dir;
0125         unsigned int nbytes;
0126         union {
0127             void *in;
0128             const void *out;
0129         } buf;
0130     } data;
0131 };
0132 
0133 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data)      \
0134     {                           \
0135         .cmd = __cmd,                   \
0136         .addr = __addr,                 \
0137         .dummy = __dummy,               \
0138         .data = __data,                 \
0139     }
0140 
0141 /**
0142  * struct spi_mem_dirmap_info - Direct mapping information
0143  * @op_tmpl: operation template that should be used by the direct mapping when
0144  *       the memory device is accessed
0145  * @offset: absolute offset this direct mapping is pointing to
0146  * @length: length in byte of this direct mapping
0147  *
0148  * These information are used by the controller specific implementation to know
0149  * the portion of memory that is directly mapped and the spi_mem_op that should
0150  * be used to access the device.
0151  * A direct mapping is only valid for one direction (read or write) and this
0152  * direction is directly encoded in the ->op_tmpl.data.dir field.
0153  */
0154 struct spi_mem_dirmap_info {
0155     struct spi_mem_op op_tmpl;
0156     u64 offset;
0157     u64 length;
0158 };
0159 
0160 /**
0161  * struct spi_mem_dirmap_desc - Direct mapping descriptor
0162  * @mem: the SPI memory device this direct mapping is attached to
0163  * @info: information passed at direct mapping creation time
0164  * @nodirmap: set to 1 if the SPI controller does not implement
0165  *        ->mem_ops->dirmap_create() or when this function returned an
0166  *        error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
0167  *        calls will use spi_mem_exec_op() to access the memory. This is a
0168  *        degraded mode that allows spi_mem drivers to use the same code
0169  *        no matter whether the controller supports direct mapping or not
0170  * @priv: field pointing to controller specific data
0171  *
0172  * Common part of a direct mapping descriptor. This object is created by
0173  * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
0174  * can create/attach direct mapping resources to the descriptor in the ->priv
0175  * field.
0176  */
0177 struct spi_mem_dirmap_desc {
0178     struct spi_mem *mem;
0179     struct spi_mem_dirmap_info info;
0180     unsigned int nodirmap;
0181     void *priv;
0182 };
0183 
0184 /**
0185  * struct spi_mem - describes a SPI memory device
0186  * @spi: the underlying SPI device
0187  * @drvpriv: spi_mem_driver private data
0188  * @name: name of the SPI memory device
0189  *
0190  * Extra information that describe the SPI memory device and may be needed by
0191  * the controller to properly handle this device should be placed here.
0192  *
0193  * One example would be the device size since some controller expose their SPI
0194  * mem devices through a io-mapped region.
0195  */
0196 struct spi_mem {
0197     struct spi_device *spi;
0198     void *drvpriv;
0199     const char *name;
0200 };
0201 
0202 /**
0203  * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
0204  *                device
0205  * @mem: memory device
0206  * @data: data to attach to the memory device
0207  */
0208 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
0209 {
0210     mem->drvpriv = data;
0211 }
0212 
0213 /**
0214  * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
0215  *                device
0216  * @mem: memory device
0217  *
0218  * Return: the data attached to the mem device.
0219  */
0220 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
0221 {
0222     return mem->drvpriv;
0223 }
0224 
0225 /**
0226  * struct spi_controller_mem_ops - SPI memory operations
0227  * @adjust_op_size: shrink the data xfer of an operation to match controller's
0228  *          limitations (can be alignment of max RX/TX size
0229  *          limitations)
0230  * @supports_op: check if an operation is supported by the controller
0231  * @exec_op: execute a SPI memory operation
0232  * @get_name: get a custom name for the SPI mem device from the controller.
0233  *        This might be needed if the controller driver has been ported
0234  *        to use the SPI mem layer and a custom name is used to keep
0235  *        mtdparts compatible.
0236  *        Note that if the implementation of this function allocates memory
0237  *        dynamically, then it should do so with devm_xxx(), as we don't
0238  *        have a ->free_name() function.
0239  * @dirmap_create: create a direct mapping descriptor that can later be used to
0240  *         access the memory device. This method is optional
0241  * @dirmap_destroy: destroy a memory descriptor previous created by
0242  *          ->dirmap_create()
0243  * @dirmap_read: read data from the memory device using the direct mapping
0244  *       created by ->dirmap_create(). The function can return less
0245  *       data than requested (for example when the request is crossing
0246  *       the currently mapped area), and the caller of
0247  *       spi_mem_dirmap_read() is responsible for calling it again in
0248  *       this case.
0249  * @dirmap_write: write data to the memory device using the direct mapping
0250  *        created by ->dirmap_create(). The function can return less
0251  *        data than requested (for example when the request is crossing
0252  *        the currently mapped area), and the caller of
0253  *        spi_mem_dirmap_write() is responsible for calling it again in
0254  *        this case.
0255  * @poll_status: poll memory device status until (status & mask) == match or
0256  *               when the timeout has expired. It fills the data buffer with
0257  *               the last status value.
0258  *
0259  * This interface should be implemented by SPI controllers providing an
0260  * high-level interface to execute SPI memory operation, which is usually the
0261  * case for QSPI controllers.
0262  *
0263  * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
0264  * mapping from the CPU because doing that can stall the CPU waiting for the
0265  * SPI mem transaction to finish, and this will make real-time maintainers
0266  * unhappy and might make your system less reactive. Instead, drivers should
0267  * use DMA to access this direct mapping.
0268  */
0269 struct spi_controller_mem_ops {
0270     int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
0271     bool (*supports_op)(struct spi_mem *mem,
0272                 const struct spi_mem_op *op);
0273     int (*exec_op)(struct spi_mem *mem,
0274                const struct spi_mem_op *op);
0275     const char *(*get_name)(struct spi_mem *mem);
0276     int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
0277     void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
0278     ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
0279                    u64 offs, size_t len, void *buf);
0280     ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
0281                 u64 offs, size_t len, const void *buf);
0282     int (*poll_status)(struct spi_mem *mem,
0283                const struct spi_mem_op *op,
0284                u16 mask, u16 match,
0285                unsigned long initial_delay_us,
0286                unsigned long polling_rate_us,
0287                unsigned long timeout_ms);
0288 };
0289 
0290 /**
0291  * struct spi_controller_mem_caps - SPI memory controller capabilities
0292  * @dtr: Supports DTR operations
0293  * @ecc: Supports operations with error correction
0294  */
0295 struct spi_controller_mem_caps {
0296     bool dtr;
0297     bool ecc;
0298 };
0299 
0300 #define spi_mem_controller_is_capable(ctlr, cap)    \
0301     ((ctlr)->mem_caps && (ctlr)->mem_caps->cap)
0302 
0303 /**
0304  * struct spi_mem_driver - SPI memory driver
0305  * @spidrv: inherit from a SPI driver
0306  * @probe: probe a SPI memory. Usually where detection/initialization takes
0307  *     place
0308  * @remove: remove a SPI memory
0309  * @shutdown: take appropriate action when the system is shutdown
0310  *
0311  * This is just a thin wrapper around a spi_driver. The core takes care of
0312  * allocating the spi_mem object and forwarding the probe/remove/shutdown
0313  * request to the spi_mem_driver. The reason we use this wrapper is because
0314  * we might have to stuff more information into the spi_mem struct to let
0315  * SPI controllers know more about the SPI memory they interact with, and
0316  * having this intermediate layer allows us to do that without adding more
0317  * useless fields to the spi_device object.
0318  */
0319 struct spi_mem_driver {
0320     struct spi_driver spidrv;
0321     int (*probe)(struct spi_mem *mem);
0322     int (*remove)(struct spi_mem *mem);
0323     void (*shutdown)(struct spi_mem *mem);
0324 };
0325 
0326 #if IS_ENABLED(CONFIG_SPI_MEM)
0327 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
0328                        const struct spi_mem_op *op,
0329                        struct sg_table *sg);
0330 
0331 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
0332                       const struct spi_mem_op *op,
0333                       struct sg_table *sg);
0334 
0335 bool spi_mem_default_supports_op(struct spi_mem *mem,
0336                  const struct spi_mem_op *op);
0337 #else
0338 static inline int
0339 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
0340                    const struct spi_mem_op *op,
0341                    struct sg_table *sg)
0342 {
0343     return -ENOTSUPP;
0344 }
0345 
0346 static inline void
0347 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
0348                      const struct spi_mem_op *op,
0349                      struct sg_table *sg)
0350 {
0351 }
0352 
0353 static inline
0354 bool spi_mem_default_supports_op(struct spi_mem *mem,
0355                  const struct spi_mem_op *op)
0356 {
0357     return false;
0358 }
0359 #endif /* CONFIG_SPI_MEM */
0360 
0361 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
0362 
0363 bool spi_mem_supports_op(struct spi_mem *mem,
0364              const struct spi_mem_op *op);
0365 
0366 int spi_mem_exec_op(struct spi_mem *mem,
0367             const struct spi_mem_op *op);
0368 
0369 const char *spi_mem_get_name(struct spi_mem *mem);
0370 
0371 struct spi_mem_dirmap_desc *
0372 spi_mem_dirmap_create(struct spi_mem *mem,
0373               const struct spi_mem_dirmap_info *info);
0374 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
0375 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
0376                 u64 offs, size_t len, void *buf);
0377 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
0378                  u64 offs, size_t len, const void *buf);
0379 struct spi_mem_dirmap_desc *
0380 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
0381                const struct spi_mem_dirmap_info *info);
0382 void devm_spi_mem_dirmap_destroy(struct device *dev,
0383                  struct spi_mem_dirmap_desc *desc);
0384 
0385 int spi_mem_poll_status(struct spi_mem *mem,
0386             const struct spi_mem_op *op,
0387             u16 mask, u16 match,
0388             unsigned long initial_delay_us,
0389             unsigned long polling_delay_us,
0390             u16 timeout_ms);
0391 
0392 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
0393                        struct module *owner);
0394 
0395 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
0396 
0397 #define spi_mem_driver_register(__drv)                                  \
0398     spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
0399 
0400 #define module_spi_mem_driver(__drv)                                    \
0401     module_driver(__drv, spi_mem_driver_register,                   \
0402               spi_mem_driver_unregister)
0403 
0404 #endif /* __LINUX_SPI_MEM_H */