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