ufs: Active Power Mode - configuring bActiveICCLevel
[pandora-kernel.git] / drivers / scsi / ufs / ufs.h
1 /*
2  * Universal Flash Storage Host controller driver
3  *
4  * This code is based on drivers/scsi/ufs/ufs.h
5  * Copyright (C) 2011-2013 Samsung India Software Operations
6  *
7  * Authors:
8  *      Santosh Yaraganavi <santosh.sy@samsung.com>
9  *      Vinayak Holikatti <h.vinayak@samsung.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * See the COPYING file in the top-level directory or visit
16  * <http://www.gnu.org/licenses/gpl-2.0.html>
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * This program is provided "AS IS" and "WITH ALL FAULTS" and
24  * without warranty of any kind. You are solely responsible for
25  * determining the appropriateness of using and distributing
26  * the program and assume all risks associated with your exercise
27  * of rights with respect to the program, including but not limited
28  * to infringement of third party rights, the risks and costs of
29  * program errors, damage to or loss of data, programs or equipment,
30  * and unavailability or interruption of operations. Under no
31  * circumstances will the contributor of this Program be liable for
32  * any damages of any kind arising from your use or distribution of
33  * this program.
34  */
35
36 #ifndef _UFS_H
37 #define _UFS_H
38
39 #include <linux/mutex.h>
40 #include <linux/types.h>
41
42 #define MAX_CDB_SIZE    16
43 #define GENERAL_UPIU_REQUEST_SIZE 32
44 #define QUERY_DESC_MAX_SIZE       255
45 #define QUERY_DESC_MIN_SIZE       2
46 #define QUERY_OSF_SIZE            (GENERAL_UPIU_REQUEST_SIZE - \
47                                         (sizeof(struct utp_upiu_header)))
48
49 #define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\
50                         cpu_to_be32((byte3 << 24) | (byte2 << 16) |\
51                          (byte1 << 8) | (byte0))
52
53 #define UFS_UPIU_MAX_GENERAL_LUN        8
54
55 /*
56  * UFS Protocol Information Unit related definitions
57  */
58
59 /* Task management functions */
60 enum {
61         UFS_ABORT_TASK          = 0x01,
62         UFS_ABORT_TASK_SET      = 0x02,
63         UFS_CLEAR_TASK_SET      = 0x04,
64         UFS_LOGICAL_RESET       = 0x08,
65         UFS_QUERY_TASK          = 0x80,
66         UFS_QUERY_TASK_SET      = 0x81,
67 };
68
69 /* UTP UPIU Transaction Codes Initiator to Target */
70 enum {
71         UPIU_TRANSACTION_NOP_OUT        = 0x00,
72         UPIU_TRANSACTION_COMMAND        = 0x01,
73         UPIU_TRANSACTION_DATA_OUT       = 0x02,
74         UPIU_TRANSACTION_TASK_REQ       = 0x04,
75         UPIU_TRANSACTION_QUERY_REQ      = 0x16,
76 };
77
78 /* UTP UPIU Transaction Codes Target to Initiator */
79 enum {
80         UPIU_TRANSACTION_NOP_IN         = 0x20,
81         UPIU_TRANSACTION_RESPONSE       = 0x21,
82         UPIU_TRANSACTION_DATA_IN        = 0x22,
83         UPIU_TRANSACTION_TASK_RSP       = 0x24,
84         UPIU_TRANSACTION_READY_XFER     = 0x31,
85         UPIU_TRANSACTION_QUERY_RSP      = 0x36,
86         UPIU_TRANSACTION_REJECT_UPIU    = 0x3F,
87 };
88
89 /* UPIU Read/Write flags */
90 enum {
91         UPIU_CMD_FLAGS_NONE     = 0x00,
92         UPIU_CMD_FLAGS_WRITE    = 0x20,
93         UPIU_CMD_FLAGS_READ     = 0x40,
94 };
95
96 /* UPIU Task Attributes */
97 enum {
98         UPIU_TASK_ATTR_SIMPLE   = 0x00,
99         UPIU_TASK_ATTR_ORDERED  = 0x01,
100         UPIU_TASK_ATTR_HEADQ    = 0x02,
101         UPIU_TASK_ATTR_ACA      = 0x03,
102 };
103
104 /* UPIU Query request function */
105 enum {
106         UPIU_QUERY_FUNC_STANDARD_READ_REQUEST           = 0x01,
107         UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST          = 0x81,
108 };
109
110 /* Flag idn for Query Requests*/
111 enum flag_idn {
112         QUERY_FLAG_IDN_FDEVICEINIT      = 0x01,
113         QUERY_FLAG_IDN_BKOPS_EN         = 0x04,
114 };
115
116 /* Attribute idn for Query requests */
117 enum attr_idn {
118         QUERY_ATTR_IDN_ACTIVE_ICC_LVL   = 0x03,
119         QUERY_ATTR_IDN_BKOPS_STATUS     = 0x05,
120         QUERY_ATTR_IDN_EE_CONTROL       = 0x0D,
121         QUERY_ATTR_IDN_EE_STATUS        = 0x0E,
122 };
123
124 /* Descriptor idn for Query requests */
125 enum desc_idn {
126         QUERY_DESC_IDN_DEVICE           = 0x0,
127         QUERY_DESC_IDN_CONFIGURAION     = 0x1,
128         QUERY_DESC_IDN_UNIT             = 0x2,
129         QUERY_DESC_IDN_RFU_0            = 0x3,
130         QUERY_DESC_IDN_INTERCONNECT     = 0x4,
131         QUERY_DESC_IDN_STRING           = 0x5,
132         QUERY_DESC_IDN_RFU_1            = 0x6,
133         QUERY_DESC_IDN_GEOMETRY         = 0x7,
134         QUERY_DESC_IDN_POWER            = 0x8,
135         QUERY_DESC_IDN_MAX,
136 };
137
138 enum desc_header_offset {
139         QUERY_DESC_LENGTH_OFFSET        = 0x00,
140         QUERY_DESC_DESC_TYPE_OFFSET     = 0x01,
141 };
142
143 enum ufs_desc_max_size {
144         QUERY_DESC_DEVICE_MAX_SIZE              = 0x1F,
145         QUERY_DESC_CONFIGURAION_MAX_SIZE        = 0x90,
146         QUERY_DESC_UNIT_MAX_SIZE                = 0x23,
147         QUERY_DESC_INTERCONNECT_MAX_SIZE        = 0x06,
148         /*
149          * Max. 126 UNICODE characters (2 bytes per character) plus 2 bytes
150          * of descriptor header.
151          */
152         QUERY_DESC_STRING_MAX_SIZE              = 0xFE,
153         QUERY_DESC_GEOMETRY_MAZ_SIZE            = 0x44,
154         QUERY_DESC_POWER_MAX_SIZE               = 0x62,
155         QUERY_DESC_RFU_MAX_SIZE                 = 0x00,
156 };
157
158 /* Unit descriptor parameters offsets in bytes*/
159 enum unit_desc_param {
160         UNIT_DESC_PARAM_LEN                     = 0x0,
161         UNIT_DESC_PARAM_TYPE                    = 0x1,
162         UNIT_DESC_PARAM_UNIT_INDEX              = 0x2,
163         UNIT_DESC_PARAM_LU_ENABLE               = 0x3,
164         UNIT_DESC_PARAM_BOOT_LUN_ID             = 0x4,
165         UNIT_DESC_PARAM_LU_WR_PROTECT           = 0x5,
166         UNIT_DESC_PARAM_LU_Q_DEPTH              = 0x6,
167         UNIT_DESC_PARAM_MEM_TYPE                = 0x8,
168         UNIT_DESC_PARAM_DATA_RELIABILITY        = 0x9,
169         UNIT_DESC_PARAM_LOGICAL_BLK_SIZE        = 0xA,
170         UNIT_DESC_PARAM_LOGICAL_BLK_COUNT       = 0xB,
171         UNIT_DESC_PARAM_ERASE_BLK_SIZE          = 0x13,
172         UNIT_DESC_PARAM_PROVISIONING_TYPE       = 0x17,
173         UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT        = 0x18,
174         UNIT_DESC_PARAM_CTX_CAPABILITIES        = 0x20,
175         UNIT_DESC_PARAM_LARGE_UNIT_SIZE_M1      = 0x22,
176 };
177
178 /* bActiveICCLevel parameter current units */
179 enum {
180         UFSHCD_NANO_AMP         = 0,
181         UFSHCD_MICRO_AMP        = 1,
182         UFSHCD_MILI_AMP         = 2,
183         UFSHCD_AMP              = 3,
184 };
185
186 #define POWER_DESC_MAX_SIZE                     0x62
187 #define POWER_DESC_MAX_ACTV_ICC_LVLS            16
188
189 /* Attribute  bActiveICCLevel parameter bit masks definitions */
190 #define ATTR_ICC_LVL_UNIT_OFFSET        14
191 #define ATTR_ICC_LVL_UNIT_MASK          (0x3 << ATTR_ICC_LVL_UNIT_OFFSET)
192 #define ATTR_ICC_LVL_VALUE_MASK         0x3FF
193
194 /* Power descriptor parameters offsets in bytes */
195 enum power_desc_param_offset {
196         PWR_DESC_LEN                    = 0x0,
197         PWR_DESC_TYPE                   = 0x1,
198         PWR_DESC_ACTIVE_LVLS_VCC_0      = 0x2,
199         PWR_DESC_ACTIVE_LVLS_VCCQ_0     = 0x22,
200         PWR_DESC_ACTIVE_LVLS_VCCQ2_0    = 0x42,
201 };
202
203 /* Exception event mask values */
204 enum {
205         MASK_EE_STATUS          = 0xFFFF,
206         MASK_EE_URGENT_BKOPS    = (1 << 2),
207 };
208
209 /* Background operation status */
210 enum {
211         BKOPS_STATUS_NO_OP               = 0x0,
212         BKOPS_STATUS_NON_CRITICAL        = 0x1,
213         BKOPS_STATUS_PERF_IMPACT         = 0x2,
214         BKOPS_STATUS_CRITICAL            = 0x3,
215 };
216
217 /* UTP QUERY Transaction Specific Fields OpCode */
218 enum query_opcode {
219         UPIU_QUERY_OPCODE_NOP           = 0x0,
220         UPIU_QUERY_OPCODE_READ_DESC     = 0x1,
221         UPIU_QUERY_OPCODE_WRITE_DESC    = 0x2,
222         UPIU_QUERY_OPCODE_READ_ATTR     = 0x3,
223         UPIU_QUERY_OPCODE_WRITE_ATTR    = 0x4,
224         UPIU_QUERY_OPCODE_READ_FLAG     = 0x5,
225         UPIU_QUERY_OPCODE_SET_FLAG      = 0x6,
226         UPIU_QUERY_OPCODE_CLEAR_FLAG    = 0x7,
227         UPIU_QUERY_OPCODE_TOGGLE_FLAG   = 0x8,
228 };
229
230 /* Query response result code */
231 enum {
232         QUERY_RESULT_SUCCESS                    = 0x00,
233         QUERY_RESULT_NOT_READABLE               = 0xF6,
234         QUERY_RESULT_NOT_WRITEABLE              = 0xF7,
235         QUERY_RESULT_ALREADY_WRITTEN            = 0xF8,
236         QUERY_RESULT_INVALID_LENGTH             = 0xF9,
237         QUERY_RESULT_INVALID_VALUE              = 0xFA,
238         QUERY_RESULT_INVALID_SELECTOR           = 0xFB,
239         QUERY_RESULT_INVALID_INDEX              = 0xFC,
240         QUERY_RESULT_INVALID_IDN                = 0xFD,
241         QUERY_RESULT_INVALID_OPCODE             = 0xFE,
242         QUERY_RESULT_GENERAL_FAILURE            = 0xFF,
243 };
244
245 /* UTP Transfer Request Command Type (CT) */
246 enum {
247         UPIU_COMMAND_SET_TYPE_SCSI      = 0x0,
248         UPIU_COMMAND_SET_TYPE_UFS       = 0x1,
249         UPIU_COMMAND_SET_TYPE_QUERY     = 0x2,
250 };
251
252 /* UTP Transfer Request Command Offset */
253 #define UPIU_COMMAND_TYPE_OFFSET        28
254
255 /* Offset of the response code in the UPIU header */
256 #define UPIU_RSP_CODE_OFFSET            8
257
258 enum {
259         MASK_SCSI_STATUS                = 0xFF,
260         MASK_TASK_RESPONSE              = 0xFF00,
261         MASK_RSP_UPIU_RESULT            = 0xFFFF,
262         MASK_QUERY_DATA_SEG_LEN         = 0xFFFF,
263         MASK_RSP_UPIU_DATA_SEG_LEN      = 0xFFFF,
264         MASK_RSP_EXCEPTION_EVENT        = 0x10000,
265 };
266
267 /* Task management service response */
268 enum {
269         UPIU_TASK_MANAGEMENT_FUNC_COMPL         = 0x00,
270         UPIU_TASK_MANAGEMENT_FUNC_NOT_SUPPORTED = 0x04,
271         UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED     = 0x08,
272         UPIU_TASK_MANAGEMENT_FUNC_FAILED        = 0x05,
273         UPIU_INCORRECT_LOGICAL_UNIT_NO          = 0x09,
274 };
275 /**
276  * struct utp_upiu_header - UPIU header structure
277  * @dword_0: UPIU header DW-0
278  * @dword_1: UPIU header DW-1
279  * @dword_2: UPIU header DW-2
280  */
281 struct utp_upiu_header {
282         __be32 dword_0;
283         __be32 dword_1;
284         __be32 dword_2;
285 };
286
287 /**
288  * struct utp_upiu_cmd - Command UPIU structure
289  * @data_transfer_len: Data Transfer Length DW-3
290  * @cdb: Command Descriptor Block CDB DW-4 to DW-7
291  */
292 struct utp_upiu_cmd {
293         __be32 exp_data_transfer_len;
294         u8 cdb[MAX_CDB_SIZE];
295 };
296
297 /**
298  * struct utp_upiu_query - upiu request buffer structure for
299  * query request.
300  * @opcode: command to perform B-0
301  * @idn: a value that indicates the particular type of data B-1
302  * @index: Index to further identify data B-2
303  * @selector: Index to further identify data B-3
304  * @reserved_osf: spec reserved field B-4,5
305  * @length: number of descriptor bytes to read/write B-6,7
306  * @value: Attribute value to be written DW-5
307  * @reserved: spec reserved DW-6,7
308  */
309 struct utp_upiu_query {
310         u8 opcode;
311         u8 idn;
312         u8 index;
313         u8 selector;
314         __be16 reserved_osf;
315         __be16 length;
316         __be32 value;
317         __be32 reserved[2];
318 };
319
320 /**
321  * struct utp_upiu_req - general upiu request structure
322  * @header:UPIU header structure DW-0 to DW-2
323  * @sc: fields structure for scsi command DW-3 to DW-7
324  * @qr: fields structure for query request DW-3 to DW-7
325  */
326 struct utp_upiu_req {
327         struct utp_upiu_header header;
328         union {
329                 struct utp_upiu_cmd sc;
330                 struct utp_upiu_query qr;
331         };
332 };
333
334 /**
335  * struct utp_cmd_rsp - Response UPIU structure
336  * @residual_transfer_count: Residual transfer count DW-3
337  * @reserved: Reserved double words DW-4 to DW-7
338  * @sense_data_len: Sense data length DW-8 U16
339  * @sense_data: Sense data field DW-8 to DW-12
340  */
341 struct utp_cmd_rsp {
342         __be32 residual_transfer_count;
343         __be32 reserved[4];
344         __be16 sense_data_len;
345         u8 sense_data[18];
346 };
347
348 /**
349  * struct utp_upiu_rsp - general upiu response structure
350  * @header: UPIU header structure DW-0 to DW-2
351  * @sr: fields structure for scsi command DW-3 to DW-12
352  * @qr: fields structure for query request DW-3 to DW-7
353  */
354 struct utp_upiu_rsp {
355         struct utp_upiu_header header;
356         union {
357                 struct utp_cmd_rsp sr;
358                 struct utp_upiu_query qr;
359         };
360 };
361
362 /**
363  * struct utp_upiu_task_req - Task request UPIU structure
364  * @header - UPIU header structure DW0 to DW-2
365  * @input_param1: Input parameter 1 DW-3
366  * @input_param2: Input parameter 2 DW-4
367  * @input_param3: Input parameter 3 DW-5
368  * @reserved: Reserved double words DW-6 to DW-7
369  */
370 struct utp_upiu_task_req {
371         struct utp_upiu_header header;
372         __be32 input_param1;
373         __be32 input_param2;
374         __be32 input_param3;
375         __be32 reserved[2];
376 };
377
378 /**
379  * struct utp_upiu_task_rsp - Task Management Response UPIU structure
380  * @header: UPIU header structure DW0-DW-2
381  * @output_param1: Ouput parameter 1 DW3
382  * @output_param2: Output parameter 2 DW4
383  * @reserved: Reserved double words DW-5 to DW-7
384  */
385 struct utp_upiu_task_rsp {
386         struct utp_upiu_header header;
387         __be32 output_param1;
388         __be32 output_param2;
389         __be32 reserved[3];
390 };
391
392 /**
393  * struct ufs_query_req - parameters for building a query request
394  * @query_func: UPIU header query function
395  * @upiu_req: the query request data
396  */
397 struct ufs_query_req {
398         u8 query_func;
399         struct utp_upiu_query upiu_req;
400 };
401
402 /**
403  * struct ufs_query_resp - UPIU QUERY
404  * @response: device response code
405  * @upiu_res: query response data
406  */
407 struct ufs_query_res {
408         u8 response;
409         struct utp_upiu_query upiu_res;
410 };
411
412 #define UFS_VREG_VCC_MIN_UV        2700000 /* uV */
413 #define UFS_VREG_VCC_MAX_UV        3600000 /* uV */
414 #define UFS_VREG_VCC_1P8_MIN_UV    1700000 /* uV */
415 #define UFS_VREG_VCC_1P8_MAX_UV    1950000 /* uV */
416 #define UFS_VREG_VCCQ_MIN_UV       1100000 /* uV */
417 #define UFS_VREG_VCCQ_MAX_UV       1300000 /* uV */
418 #define UFS_VREG_VCCQ2_MIN_UV      1650000 /* uV */
419 #define UFS_VREG_VCCQ2_MAX_UV      1950000 /* uV */
420
421 struct ufs_vreg {
422         struct regulator *reg;
423         const char *name;
424         bool enabled;
425         int min_uV;
426         int max_uV;
427         int min_uA;
428         int max_uA;
429 };
430
431 struct ufs_vreg_info {
432         struct ufs_vreg *vcc;
433         struct ufs_vreg *vccq;
434         struct ufs_vreg *vccq2;
435         struct ufs_vreg *vdd_hba;
436 };
437
438 #endif /* End of Header */