1 /* 2 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #ifndef _MISC_H_ 8 #define _MISC_H_ 9 10 #include <asm-generic/ioctl.h> 11 12 /* 13 * Request command to be sent for misc_ioctl(). 14 */ 15 #define IOCTL_REQ_START _IO('m', 0x01) 16 #define IOCTL_REQ_STOP _IO('m', 0x02) 17 #define IOCTL_REQ_POLL _IO('m', 0x03) 18 #define IOCTL_REQ_CAPABILITY _IO('m', 0x04) 19 #define IOCTL_REQ_DATA_SIZE _IO('m', 0x05) 20 #define IOCTL_REQ_KEYLAD_INIT _IO('m', 0x06) 21 #define IOCTL_REQ_KEYLAD_DEINIT _IO('m', 0x07) 22 23 enum misc_mode { 24 DECOM_LZ4 = BIT(0), 25 DECOM_GZIP = BIT(1), 26 DECOM_ZLIB = BIT(2), 27 OTP_S = BIT(3), 28 OTP_NS = BIT(4), 29 }; 30 31 /* 32 * Read the device to buffer, optional. 33 * 34 * @dev: the device 35 * @offset: offset to read the device 36 * @buf: pointer to data buffer 37 * @size: data size in bytes to read the device 38 * @return: 0 if OK, -ve on error 39 */ 40 int misc_read(struct udevice *dev, int offset, void *buf, int size); 41 /* 42 * Write buffer to the device, optional. 43 * 44 * @dev: the device 45 * @offset: offset to write the device 46 * @buf: pointer to data buffer 47 * @size: data size in bytes to write the device 48 * @return: 0 if OK, -ve on error 49 */ 50 int misc_write(struct udevice *dev, int offset, void *buf, int size); 51 /* 52 * Assert command to the device, optional. 53 * 54 * @dev: the device 55 * @request: command to be sent to the device 56 * @buf: pointer to buffer related to the request 57 * @return: 0 if OK, -ve on error 58 */ 59 int misc_ioctl(struct udevice *dev, unsigned long request, void *buf); 60 61 /* 62 * Send a message to the device and wait for a response. 63 * 64 * The caller provides the message type/ID and payload to be sent. 65 * The callee constructs any message header required, transmits it to the 66 * target, waits for a response, checks any error code in the response, 67 * strips any message header from the response, and returns the error code 68 * (or a parsed version of it) and the response message payload. 69 * 70 * @dev: the device. 71 * @msgid: the message ID/number to send. 72 * tx_msg: the request/transmit message payload. 73 * tx_size: the size of the buffer pointed at by tx_msg. 74 * rx_msg: the buffer to receive the response message payload. May be NULL if 75 * the caller only cares about the error code. 76 * rx_size: the size of the buffer pointed at by rx_msg. 77 * @return the response message size if OK, -ve on error 78 */ 79 int misc_call(struct udevice *dev, int msgid, void *tx_msg, int tx_size, 80 void *rx_msg, int rx_size); 81 82 /* 83 * Get a misc device by capability 84 * 85 * The caller can get a misc device according to capability request, the driver 86 * must implement the IOCTL_REQ_CAPABILITY callback. 87 * 88 * @capability: the value of enum misc_mode. 89 * @return the require device if OK, NULL on error 90 */ 91 struct udevice *misc_get_device_by_capability(u32 capability); 92 93 /* 94 * struct misc_ops - Driver model Misc operations 95 * 96 * The uclass interface is implemented by all miscellaneous devices which 97 * use driver model. 98 */ 99 struct misc_ops { 100 /* 101 * Read the device to buffer, optional. 102 * 103 * @dev: the device 104 * @offset: offset to read the device 105 * @buf: pointer to data buffer 106 * @size: data size in bytes to read the device 107 * @return: 0 if OK, -ve on error 108 */ 109 int (*read)(struct udevice *dev, int offset, void *buf, int size); 110 /* 111 * Write buffer to the device, optional. 112 * 113 * @dev: the device 114 * @offset: offset to write the device 115 * @buf: pointer to data buffer 116 * @size: data size in bytes to write the device 117 * @return: 0 if OK, -ve on error 118 */ 119 int (*write)(struct udevice *dev, int offset, const void *buf, 120 int size); 121 /* 122 * Assert command to the device, optional. 123 * 124 * @dev: the device 125 * @request: command to be sent to the device 126 * @buf: pointer to buffer related to the request 127 * @return: 0 if OK, -ve on error 128 */ 129 int (*ioctl)(struct udevice *dev, unsigned long request, void *buf); 130 /* 131 * Send a message to the device and wait for a response. 132 * 133 * @dev: the device 134 * @msgid: the message ID/number to send 135 * tx_msg: the request/transmit message payload 136 * tx_size: the size of the buffer pointed at by tx_msg 137 * rx_msg: the buffer to receive the response message payload. May be 138 * NULL if the caller only cares about the error code. 139 * rx_size: the size of the buffer pointed at by rx_msg 140 * @return the response message size if OK, -ve on error 141 */ 142 int (*call)(struct udevice *dev, int msgid, void *tx_msg, int tx_size, 143 void *rx_msg, int rx_size); 144 }; 145 146 /* generic layer for otp */ 147 struct udevice *misc_otp_get_device(u32 capability); 148 int misc_otp_read(struct udevice *dev, int offset, void *buf, int size); 149 int misc_otp_write(struct udevice *dev, int offset, const void *buf, int size); 150 int misc_otp_ioctl(struct udevice *dev, unsigned long request, void *buf); 151 int misc_otp_write_verify(struct udevice *dev, int offset, const uint8_t *write_buf, int size); 152 153 /* generic layer for decompress */ 154 struct decom_param { 155 unsigned long addr_src; 156 unsigned long addr_dst; 157 u64 size_src; /* compressed */ 158 u64 size_dst; /* decompressed, to be filled for output */ 159 enum misc_mode mode; 160 u32 flags; 161 }; 162 163 /* function flags for decompress */ 164 #define DCOMP_FLG_IRQ_ONESHOT BIT(0) 165 166 void misc_decompress_async(u8 comp); 167 void misc_decompress_sync(u8 comp); 168 int misc_decompress_cleanup(void); 169 int misc_decompress_process(unsigned long dst, unsigned long src, 170 unsigned long src_len, u32 cap, bool sync, 171 u64 *size, u32 flags); 172 #endif /* _MISC_H_ */ 173