staging: fsl-mc: convert mc command build/parse to use C structs
[cascardo/linux.git] / drivers / staging / fsl-mc / include / mc-cmd.h
1 /* Copyright 2013-2015 Freescale Semiconductor Inc.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5  * * Redistributions of source code must retain the above copyright
6  * notice, this list of conditions and the following disclaimer.
7  * * Redistributions in binary form must reproduce the above copyright
8  * notice, this list of conditions and the following disclaimer in the
9  * documentation and/or other materials provided with the distribution.
10  * * Neither the name of the above-listed copyright holders nor the
11  * names of any contributors may be used to endorse or promote products
12  * derived from this software without specific prior written permission.
13  *
14  *
15  * ALTERNATIVELY, this software may be distributed under the terms of the
16  * GNU General Public License ("GPL") as published by the Free Software
17  * Foundation, either version 2 of that License or (at your option) any
18  * later version.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __FSL_MC_CMD_H
33 #define __FSL_MC_CMD_H
34
35 #define MC_CMD_NUM_OF_PARAMS    7
36
37 struct mc_cmd_header {
38         u8 src_id;
39         u8 flags_hw;
40         u8 status;
41         u8 flags_sw;
42         __le16 token;
43         __le16 cmd_id;
44 };
45
46 struct mc_command {
47         u64 header;
48         u64 params[MC_CMD_NUM_OF_PARAMS];
49 };
50
51 enum mc_cmd_status {
52         MC_CMD_STATUS_OK = 0x0, /* Completed successfully */
53         MC_CMD_STATUS_READY = 0x1, /* Ready to be processed */
54         MC_CMD_STATUS_AUTH_ERR = 0x3, /* Authentication error */
55         MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /* No privilege */
56         MC_CMD_STATUS_DMA_ERR = 0x5, /* DMA or I/O error */
57         MC_CMD_STATUS_CONFIG_ERR = 0x6, /* Configuration error */
58         MC_CMD_STATUS_TIMEOUT = 0x7, /* Operation timed out */
59         MC_CMD_STATUS_NO_RESOURCE = 0x8, /* No resources */
60         MC_CMD_STATUS_NO_MEMORY = 0x9, /* No memory available */
61         MC_CMD_STATUS_BUSY = 0xA, /* Device is busy */
62         MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /* Unsupported operation */
63         MC_CMD_STATUS_INVALID_STATE = 0xC /* Invalid state */
64 };
65
66 /*
67  * MC command flags
68  */
69
70 /* High priority flag */
71 #define MC_CMD_FLAG_PRI         0x80
72 /* Command completion flag */
73 #define MC_CMD_FLAG_INTR_DIS    0x01
74
75 #define MC_CMD_HDR_CMDID_MASK           0xFFF0
76 #define MC_CMD_HDR_CMDID_SHIFT          4
77 #define MC_CMD_HDR_TOKEN_MASK           0xFFC0
78 #define MC_CMD_HDR_TOKEN_SHIFT          6
79
80 static inline u64 mc_encode_cmd_header(u16 cmd_id,
81                                        u32 cmd_flags,
82                                        u16 token)
83 {
84         u64 header = 0;
85         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&header;
86
87         hdr->cmd_id = cpu_to_le16((cmd_id << MC_CMD_HDR_CMDID_SHIFT) &
88                                   MC_CMD_HDR_CMDID_MASK);
89         hdr->token = cpu_to_le16((token << MC_CMD_HDR_TOKEN_SHIFT) &
90                                  MC_CMD_HDR_TOKEN_MASK);
91         hdr->status = MC_CMD_STATUS_READY;
92         if (cmd_flags & MC_CMD_FLAG_PRI)
93                 hdr->flags_hw = MC_CMD_FLAG_PRI;
94         if (cmd_flags & MC_CMD_FLAG_INTR_DIS)
95                 hdr->flags_sw = MC_CMD_FLAG_INTR_DIS;
96
97         return header;
98 }
99
100 static inline u16 mc_cmd_hdr_read_token(struct mc_command *cmd)
101 {
102         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
103         u16 token = le16_to_cpu(hdr->token);
104
105         return (token & MC_CMD_HDR_TOKEN_MASK) >> MC_CMD_HDR_TOKEN_SHIFT;
106 }
107
108 #endif /* __FSL_MC_CMD_H */