1 /* Copyright 2013-2014 Freescale Semiconductor Inc.
3 * I/O services to send MC commands to the MC hardware
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the above-listed copyright holders nor the
13 * names of any contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
17 * ALTERNATIVELY, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") as published by the Free Software
19 * Foundation, either version 2 of that License or (at your option) any
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include "../include/mc-sys.h"
36 #include "../include/mc-cmd.h"
37 #include <linux/delay.h>
38 #include <linux/slab.h>
39 #include <linux/ioport.h>
40 #include <linux/device.h>
43 * Timeout in jiffies to wait for the completion of an MC command
45 #define MC_CMD_COMPLETION_TIMEOUT_JIFFIES (HZ / 2) /* 500 ms */
48 * usleep_range() min and max values used to throttle down polling
49 * iterations while waiting for MC command completion
51 #define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
52 #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
54 #define MC_CMD_HDR_READ_CMDID(_hdr) \
55 ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S))
58 * Creates an MC I/O object
60 * @dev: device to be associated with the MC I/O object
61 * @mc_portal_phys_addr: physical address of the MC portal to use
62 * @mc_portal_size: size in bytes of the MC portal
63 * @resource: Pointer to MC bus object allocator resource associated
64 * with this MC I/O object or NULL if none.
65 * @flags: flags for the new MC I/O object
66 * @new_mc_io: Area to return pointer to newly created MC I/O object
68 * Returns '0' on Success; Error code otherwise.
70 int __must_check fsl_create_mc_io(struct device *dev,
71 phys_addr_t mc_portal_phys_addr,
72 uint32_t mc_portal_size,
73 struct fsl_mc_resource *resource,
74 uint32_t flags, struct fsl_mc_io **new_mc_io)
76 struct fsl_mc_io *mc_io;
77 void __iomem *mc_portal_virt_addr;
80 mc_io = devm_kzalloc(dev, sizeof(*mc_io), GFP_KERNEL);
86 mc_io->portal_phys_addr = mc_portal_phys_addr;
87 mc_io->portal_size = mc_portal_size;
88 mc_io->resource = resource;
89 res = devm_request_mem_region(dev,
95 "devm_request_mem_region failed for MC portal %#llx\n",
100 mc_portal_virt_addr = devm_ioremap_nocache(dev,
103 if (!mc_portal_virt_addr) {
105 "devm_ioremap_nocache failed for MC portal %#llx\n",
106 mc_portal_phys_addr);
110 mc_io->portal_virt_addr = mc_portal_virt_addr;
114 EXPORT_SYMBOL_GPL(fsl_create_mc_io);
117 * Destroys an MC I/O object
119 * @mc_io: MC I/O object to destroy
121 void fsl_destroy_mc_io(struct fsl_mc_io *mc_io)
123 devm_iounmap(mc_io->dev, mc_io->portal_virt_addr);
124 devm_release_mem_region(mc_io->dev,
125 mc_io->portal_phys_addr,
128 mc_io->portal_virt_addr = NULL;
129 devm_kfree(mc_io->dev, mc_io);
131 EXPORT_SYMBOL_GPL(fsl_destroy_mc_io);
133 static int mc_status_to_error(enum mc_cmd_status status)
135 static const int mc_status_to_error_map[] = {
136 [MC_CMD_STATUS_OK] = 0,
137 [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
138 [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
139 [MC_CMD_STATUS_DMA_ERR] = -EIO,
140 [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
141 [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
142 [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
143 [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
144 [MC_CMD_STATUS_BUSY] = -EBUSY,
145 [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
146 [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
149 if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map)))
152 return mc_status_to_error_map[status];
155 static const char *mc_status_to_string(enum mc_cmd_status status)
157 static const char *const status_strings[] = {
158 [MC_CMD_STATUS_OK] = "Command completed successfully",
159 [MC_CMD_STATUS_READY] = "Command ready to be processed",
160 [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
161 [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
162 [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
163 [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
164 [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
165 [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
166 [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
167 [MC_CMD_STATUS_BUSY] = "Device is busy",
168 [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
169 [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
172 if ((unsigned int)status >= ARRAY_SIZE(status_strings))
173 return "Unknown MC error";
175 return status_strings[status];
179 * mc_write_command - writes a command to a Management Complex (MC) portal
181 * @portal: pointer to an MC portal
182 * @cmd: pointer to a filled command
184 static inline void mc_write_command(struct mc_command __iomem *portal,
185 struct mc_command *cmd)
189 /* copy command parameters into the portal */
190 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
191 writeq(cmd->params[i], &portal->params[i]);
193 /* submit the command by writing the header */
194 writeq(cmd->header, &portal->header);
198 * mc_read_response - reads the response for the last MC command from a
199 * Management Complex (MC) portal
201 * @portal: pointer to an MC portal
202 * @resp: pointer to command response buffer
204 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
206 static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem *
208 struct mc_command *resp)
211 enum mc_cmd_status status;
213 /* Copy command response header from MC portal: */
214 resp->header = readq(&portal->header);
215 status = MC_CMD_HDR_READ_STATUS(resp->header);
216 if (status != MC_CMD_STATUS_OK)
219 /* Copy command response data from MC portal: */
220 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
221 resp->params[i] = readq(&portal->params[i]);
227 * Sends an command to the MC device using the given MC I/O object
229 * @mc_io: MC I/O object to be used
230 * @cmd: command to be sent
232 * Returns '0' on Success; Error code otherwise.
234 * NOTE: This function cannot be invoked from from atomic contexts.
236 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
238 enum mc_cmd_status status;
239 unsigned long jiffies_until_timeout =
240 jiffies + MC_CMD_COMPLETION_TIMEOUT_JIFFIES;
243 * Send command to the MC hardware:
245 mc_write_command(mc_io->portal_virt_addr, cmd);
248 * Wait for response from the MC hardware:
251 status = mc_read_response(mc_io->portal_virt_addr, cmd);
252 if (status != MC_CMD_STATUS_READY)
256 * TODO: When MC command completion interrupts are supported
257 * call wait function here instead of usleep_range()
259 usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
260 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
262 if (time_after_eq(jiffies, jiffies_until_timeout)) {
263 pr_debug("MC command timed out (portal: %#llx, obj handle: %#x, command: %#x)\n",
264 mc_io->portal_phys_addr,
266 MC_CMD_HDR_READ_TOKEN(cmd->header),
268 MC_CMD_HDR_READ_CMDID(cmd->header));
274 if (status != MC_CMD_STATUS_OK) {
275 pr_debug("MC command failed: portal: %#llx, obj handle: %#x, command: %#x, status: %s (%#x)\n",
276 mc_io->portal_phys_addr,
277 (unsigned int)MC_CMD_HDR_READ_TOKEN(cmd->header),
278 (unsigned int)MC_CMD_HDR_READ_CMDID(cmd->header),
279 mc_status_to_string(status),
280 (unsigned int)status);
282 return mc_status_to_error(status);
287 EXPORT_SYMBOL(mc_send_command);