2 * Copyright (C) ST-Ericsson SA 2010
3 * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson.
4 * Author: Jonas Linde <jonas.linde@stericsson.com> for ST-Ericsson.
5 * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
6 * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
7 * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
8 * License terms: GNU General Public License (GPL) version 2
14 #include <linux/completion.h>
15 #include <linux/dmaengine.h>
16 #include <linux/klist.h>
17 #include <linux/mutex.h>
19 #define DEV_DBG_NAME "crypX crypX:"
21 /* CRYP enable/disable */
23 CRYP_CRYPEN_DISABLE = 0,
24 CRYP_CRYPEN_ENABLE = 1
27 /* CRYP Start Computation enable/disable */
29 CRYP_START_DISABLE = 0,
33 /* CRYP Init Signal enable/disable */
35 CRYP_INIT_DISABLE = 0,
39 /* Cryp State enable/disable */
41 CRYP_STATE_DISABLE = 0,
45 /* Key preparation bit enable */
51 /* Key size for AES */
52 #define CRYP_KEY_SIZE_128 (0)
53 #define CRYP_KEY_SIZE_192 (1)
54 #define CRYP_KEY_SIZE_256 (2)
68 /* Cryp Encryption or Decryption */
69 enum cryp_algorithm_dir {
70 CRYP_ALGORITHM_ENCRYPT,
71 CRYP_ALGORITHM_DECRYPT
74 /* Hardware access method */
82 * struct cryp_config -
83 * @keysize: Key size for AES
84 * @algomode: AES modes
85 * @algodir: Cryp Encryption or Decryption
87 * CRYP configuration structure to be passed to set configuration
91 enum cryp_algo_mode algomode;
92 enum cryp_algorithm_dir algodir;
96 * struct cryp_protection_config -
97 * @privilege_access: Privileged cryp state enable/disable
98 * @secure_access: Secure cryp state enable/disable
100 * Protection configuration structure for setting privilage access
102 struct cryp_protection_config {
103 enum cryp_state privilege_access;
104 enum cryp_state secure_access;
108 enum cryp_status_id {
109 CRYP_STATUS_BUSY = 0x10,
110 CRYP_STATUS_OUTPUT_FIFO_FULL = 0x08,
111 CRYP_STATUS_OUTPUT_FIFO_NOT_EMPTY = 0x04,
112 CRYP_STATUS_INPUT_FIFO_NOT_FULL = 0x02,
113 CRYP_STATUS_INPUT_FIFO_EMPTY = 0x01
116 /* Cryp DMA interface */
117 #define CRYP_DMA_TX_FIFO 0x08
118 #define CRYP_DMA_RX_FIFO 0x10
120 enum cryp_dma_req_type {
121 CRYP_DMA_DISABLE_BOTH,
122 CRYP_DMA_ENABLE_IN_DATA,
123 CRYP_DMA_ENABLE_OUT_DATA,
124 CRYP_DMA_ENABLE_BOTH_DIRECTIONS
127 enum cryp_dma_channel {
133 enum cryp_key_reg_index {
140 /* Key register left and right */
141 struct cryp_key_value {
146 /* Cryp Initialization structure */
147 enum cryp_init_vector_index {
148 CRYP_INIT_VECTOR_INDEX_0,
149 CRYP_INIT_VECTOR_INDEX_1
152 /* struct cryp_init_vector_value -
156 struct cryp_init_vector_value {
158 u32 init_value_right;
162 * struct cryp_device_context - structure for a cryp context.
163 * @cr: control register
164 * @dmacr: DMA control register
165 * @imsc: Interrupt mask set/clear register
166 * @key_1_l: Key 1l register
167 * @key_1_r: Key 1r register
168 * @key_2_l: Key 2l register
169 * @key_2_r: Key 2r register
170 * @key_3_l: Key 3l register
171 * @key_3_r: Key 3r register
172 * @key_4_l: Key 4l register
173 * @key_4_r: Key 4r register
174 * @init_vect_0_l: Initialization vector 0l register
175 * @init_vect_0_r: Initialization vector 0r register
176 * @init_vect_1_l: Initialization vector 1l register
177 * @init_vect_1_r: Initialization vector 0r register
178 * @din: Data in register
179 * @dout: Data out register
181 * CRYP power management specifc structure.
183 struct cryp_device_context {
208 struct completion cryp_dma_complete;
209 struct dma_chan *chan_cryp2mem;
210 struct dma_chan *chan_mem2cryp;
211 struct stedma40_chan_cfg *cfg_cryp2mem;
212 struct stedma40_chan_cfg *cfg_mem2cryp;
215 struct scatterlist *sg_src;
216 struct scatterlist *sg_dst;
222 * struct cryp_device_data - structure for a cryp device.
223 * @base: Pointer to virtual base address of the cryp device.
224 * @phybase: Pointer to physical memory location of the cryp device.
225 * @dev: Pointer to the devices dev structure.
226 * @clk: Pointer to the device's clock control.
227 * @pwr_regulator: Pointer to the device's power control.
228 * @power_status: Current status of the power.
229 * @ctx_lock: Lock for current_ctx.
230 * @current_ctx: Pointer to the currently allocated context.
231 * @list_node: For inclusion into a klist.
232 * @dma: The dma structure holding channel configuration.
233 * @power_state: TRUE = power state on, FALSE = power state off.
234 * @power_state_spinlock: Spinlock for power_state.
235 * @restore_dev_ctx: TRUE = saved ctx, FALSE = no saved ctx.
237 struct cryp_device_data {
238 struct cryp_register __iomem *base;
242 struct regulator *pwr_regulator;
244 struct spinlock ctx_lock;
245 struct cryp_ctx *current_ctx;
246 struct klist_node list_node;
249 struct spinlock power_state_spinlock;
250 bool restore_dev_ctx;
253 void cryp_wait_until_done(struct cryp_device_data *device_data);
255 /* Initialization functions */
257 int cryp_check(struct cryp_device_data *device_data);
259 void cryp_activity(struct cryp_device_data *device_data,
260 enum cryp_crypen cryp_crypen);
262 void cryp_flush_inoutfifo(struct cryp_device_data *device_data);
264 int cryp_set_configuration(struct cryp_device_data *device_data,
265 struct cryp_config *cryp_config,
266 u32 *control_register);
268 void cryp_configure_for_dma(struct cryp_device_data *device_data,
269 enum cryp_dma_req_type dma_req);
271 int cryp_configure_key_values(struct cryp_device_data *device_data,
272 enum cryp_key_reg_index key_reg_index,
273 struct cryp_key_value key_value);
275 int cryp_configure_init_vector(struct cryp_device_data *device_data,
276 enum cryp_init_vector_index
278 struct cryp_init_vector_value
281 int cryp_configure_protection(struct cryp_device_data *device_data,
282 struct cryp_protection_config *p_protect_config);
284 /* Power management funtions */
285 void cryp_save_device_context(struct cryp_device_data *device_data,
286 struct cryp_device_context *ctx,
289 void cryp_restore_device_context(struct cryp_device_data *device_data,
290 struct cryp_device_context *ctx);
292 /* Data transfer and status bits. */
293 int cryp_is_logic_busy(struct cryp_device_data *device_data);
295 int cryp_get_status(struct cryp_device_data *device_data);
298 * cryp_write_indata - This routine writes 32 bit data into the data input
299 * register of the cryptography IP.
300 * @device_data: Pointer to the device data struct for base address.
301 * @write_data: Data to write.
303 int cryp_write_indata(struct cryp_device_data *device_data, u32 write_data);
306 * cryp_read_outdata - This routine reads the data from the data output
307 * register of the CRYP logic
308 * @device_data: Pointer to the device data struct for base address.
309 * @read_data: Read the data from the output FIFO.
311 int cryp_read_outdata(struct cryp_device_data *device_data, u32 *read_data);
313 #endif /* _CRYP_H_ */