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 20 enum misc_mode { 21 DECOM_LZ4 = BIT(0), 22 DECOM_GZIP = BIT(1), 23 DECOM_ZLIB = BIT(2), 24 OTP_S = BIT(3), 25 OTP_NS = BIT(4), 26 }; 27 28 /* 29 * Read the device to buffer, optional. 30 * 31 * @dev: the device 32 * @offset: offset to read the device 33 * @buf: pointer to data buffer 34 * @size: data size in bytes to read the device 35 * @return: 0 if OK, -ve on error 36 */ 37 int misc_read(struct udevice *dev, int offset, void *buf, int size); 38 /* 39 * Write buffer to the device, optional. 40 * 41 * @dev: the device 42 * @offset: offset to write the device 43 * @buf: pointer to data buffer 44 * @size: data size in bytes to write the device 45 * @return: 0 if OK, -ve on error 46 */ 47 int misc_write(struct udevice *dev, int offset, void *buf, int size); 48 /* 49 * Assert command to the device, optional. 50 * 51 * @dev: the device 52 * @request: command to be sent to the device 53 * @buf: pointer to buffer related to the request 54 * @return: 0 if OK, -ve on error 55 */ 56 int misc_ioctl(struct udevice *dev, unsigned long request, void *buf); 57 58 /* 59 * Send a message to the device and wait for a response. 60 * 61 * The caller provides the message type/ID and payload to be sent. 62 * The callee constructs any message header required, transmits it to the 63 * target, waits for a response, checks any error code in the response, 64 * strips any message header from the response, and returns the error code 65 * (or a parsed version of it) and the response message payload. 66 * 67 * @dev: the device. 68 * @msgid: the message ID/number to send. 69 * tx_msg: the request/transmit message payload. 70 * tx_size: the size of the buffer pointed at by tx_msg. 71 * rx_msg: the buffer to receive the response message payload. May be NULL if 72 * the caller only cares about the error code. 73 * rx_size: the size of the buffer pointed at by rx_msg. 74 * @return the response message size if OK, -ve on error 75 */ 76 int misc_call(struct udevice *dev, int msgid, void *tx_msg, int tx_size, 77 void *rx_msg, int rx_size); 78 79 /* 80 * Get a misc device by capability 81 * 82 * The caller can get a misc device according to capability request, the driver 83 * must implement the IOCTL_REQ_CAPABILITY callback. 84 * 85 * @capability: the value of enum misc_mode. 86 * @return the require device if OK, NULL on error 87 */ 88 struct udevice *misc_get_device_by_capability(u32 capability); 89 90 /* 91 * struct misc_ops - Driver model Misc operations 92 * 93 * The uclass interface is implemented by all miscellaneous devices which 94 * use driver model. 95 */ 96 struct misc_ops { 97 /* 98 * Read the device to buffer, optional. 99 * 100 * @dev: the device 101 * @offset: offset to read the device 102 * @buf: pointer to data buffer 103 * @size: data size in bytes to read the device 104 * @return: 0 if OK, -ve on error 105 */ 106 int (*read)(struct udevice *dev, int offset, void *buf, int size); 107 /* 108 * Write buffer to the device, optional. 109 * 110 * @dev: the device 111 * @offset: offset to write the device 112 * @buf: pointer to data buffer 113 * @size: data size in bytes to write the device 114 * @return: 0 if OK, -ve on error 115 */ 116 int (*write)(struct udevice *dev, int offset, const void *buf, 117 int size); 118 /* 119 * Assert command to the device, optional. 120 * 121 * @dev: the device 122 * @request: command to be sent to the device 123 * @buf: pointer to buffer related to the request 124 * @return: 0 if OK, -ve on error 125 */ 126 int (*ioctl)(struct udevice *dev, unsigned long request, void *buf); 127 /* 128 * Send a message to the device and wait for a response. 129 * 130 * @dev: the device 131 * @msgid: the message ID/number to send 132 * tx_msg: the request/transmit message payload 133 * tx_size: the size of the buffer pointed at by tx_msg 134 * rx_msg: the buffer to receive the response message payload. May be 135 * NULL if the caller only cares about the error code. 136 * rx_size: the size of the buffer pointed at by rx_msg 137 * @return the response message size if OK, -ve on error 138 */ 139 int (*call)(struct udevice *dev, int msgid, void *tx_msg, int tx_size, 140 void *rx_msg, int rx_size); 141 }; 142 143 /* generic layer for otp */ 144 struct udevice *misc_otp_get_device(u32 capability); 145 int misc_otp_read(struct udevice *dev, int offset, void *buf, int size); 146 int misc_otp_write(struct udevice *dev, int offset, const void *buf, int size); 147 148 /* generic layer for decompress */ 149 struct decom_param { 150 unsigned long addr_src; 151 unsigned long addr_dst; 152 unsigned long size; 153 enum misc_mode mode; 154 }; 155 156 struct udevice *misc_decompress_get_device(u32 capability); 157 int misc_decompress_start(struct udevice *dev, unsigned long src, 158 unsigned long dst, unsigned long size); 159 int misc_decompress_stop(struct udevice *dev); 160 int misc_decompress_is_complete(struct udevice *dev); 161 162 #endif /* _MISC_H_ */ 163