17b3bd9a7SJ. German Rivera /*
27b3bd9a7SJ. German Rivera * Freescale Layerscape MC I/O wrapper
37b3bd9a7SJ. German Rivera *
4a2a55e51SPrabhakar Kushwaha * Copyright (C) 2013-2015 Freescale Semiconductor, Inc.
57b3bd9a7SJ. German Rivera * Author: German Rivera <German.Rivera@freescale.com>
67b3bd9a7SJ. German Rivera *
77b3bd9a7SJ. German Rivera * SPDX-License-Identifier: GPL-2.0+
87b3bd9a7SJ. German Rivera */
97b3bd9a7SJ. German Rivera
107b3bd9a7SJ. German Rivera #include <fsl-mc/fsl_mc_sys.h>
117b3bd9a7SJ. German Rivera #include <fsl-mc/fsl_mc_cmd.h>
127b3bd9a7SJ. German Rivera #include <common.h>
137b3bd9a7SJ. German Rivera #include <errno.h>
147b3bd9a7SJ. German Rivera #include <asm/io.h>
157b3bd9a7SJ. German Rivera
167b3bd9a7SJ. German Rivera #define MC_CMD_HDR_READ_CMDID(_hdr) \
1787457d11SPrabhakar Kushwaha ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S))
187b3bd9a7SJ. German Rivera
197b3bd9a7SJ. German Rivera /**
207b3bd9a7SJ. German Rivera * mc_send_command - Send MC command and wait for response
217b3bd9a7SJ. German Rivera *
227b3bd9a7SJ. German Rivera * @mc_io: Pointer to MC I/O object to be used
237b3bd9a7SJ. German Rivera * @cmd: MC command buffer. On input, it contains the command to send to the MC.
247b3bd9a7SJ. German Rivera * On output, it contains the response from the MC if any.
257b3bd9a7SJ. German Rivera *
267b3bd9a7SJ. German Rivera * Depending on the sharing option specified when creating the MC portal
277b3bd9a7SJ. German Rivera * wrapper, this function will use a spinlock or mutex to ensure exclusive
287b3bd9a7SJ. German Rivera * access to the MC portal from the point when the command is sent until a
297b3bd9a7SJ. German Rivera * response is received from the MC.
307b3bd9a7SJ. German Rivera */
mc_send_command(struct fsl_mc_io * mc_io,struct mc_command * cmd)317b3bd9a7SJ. German Rivera int mc_send_command(struct fsl_mc_io *mc_io,
327b3bd9a7SJ. German Rivera struct mc_command *cmd)
337b3bd9a7SJ. German Rivera {
347b3bd9a7SJ. German Rivera enum mc_cmd_status status;
35*553d2751SPrabhakar Kushwaha int timeout = 12000;
367b3bd9a7SJ. German Rivera
377b3bd9a7SJ. German Rivera mc_write_command(mc_io->mmio_regs, cmd);
387b3bd9a7SJ. German Rivera
397b3bd9a7SJ. German Rivera for ( ; ; ) {
407b3bd9a7SJ. German Rivera status = mc_read_response(mc_io->mmio_regs, cmd);
417b3bd9a7SJ. German Rivera if (status != MC_CMD_STATUS_READY)
427b3bd9a7SJ. German Rivera break;
437b3bd9a7SJ. German Rivera
447b3bd9a7SJ. German Rivera if (--timeout == 0) {
457b3bd9a7SJ. German Rivera printf("Error: Timeout waiting for MC response\n");
467b3bd9a7SJ. German Rivera return -ETIMEDOUT;
477b3bd9a7SJ. German Rivera }
487b3bd9a7SJ. German Rivera
497b3bd9a7SJ. German Rivera udelay(500);
507b3bd9a7SJ. German Rivera }
517b3bd9a7SJ. German Rivera
527b3bd9a7SJ. German Rivera if (status != MC_CMD_STATUS_OK) {
537b3bd9a7SJ. German Rivera printf("Error: MC command failed (portal: %p, obj handle: %#x, command: %#x, status: %#x)\n",
547b3bd9a7SJ. German Rivera mc_io->mmio_regs,
55a2a55e51SPrabhakar Kushwaha (unsigned int)MC_CMD_HDR_READ_TOKEN(cmd->header),
567b3bd9a7SJ. German Rivera (unsigned int)MC_CMD_HDR_READ_CMDID(cmd->header),
577b3bd9a7SJ. German Rivera (unsigned int)status);
587b3bd9a7SJ. German Rivera
597b3bd9a7SJ. German Rivera return -EIO;
607b3bd9a7SJ. German Rivera }
617b3bd9a7SJ. German Rivera
627b3bd9a7SJ. German Rivera return 0;
637b3bd9a7SJ. German Rivera }
64