blob: 4d32516b005541c5dcd00d5e505660173b1fc5b0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
J. German Rivera43e4ae32015-01-06 13:19:02 -08002/*
3 * Freescale Layerscape MC I/O wrapper
4 *
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07005 * Copyright (C) 2013-2015 Freescale Semiconductor, Inc.
J. German Rivera43e4ae32015-01-06 13:19:02 -08006 * Author: German Rivera <German.Rivera@freescale.com>
J. German Rivera43e4ae32015-01-06 13:19:02 -08007 */
8
9#include <fsl-mc/fsl_mc_sys.h>
10#include <fsl-mc/fsl_mc_cmd.h>
11#include <common.h>
12#include <errno.h>
13#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080015
Ioana Ciornei1c3769d2023-05-31 19:04:36 +030016static u16 mc_cmd_hdr_read_cmdid(struct mc_command *cmd)
17{
18 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
19 u16 cmd_id = le16_to_cpu(hdr->cmd_id);
20
21 return cmd_id;
22}
J. German Rivera43e4ae32015-01-06 13:19:02 -080023
24/**
25 * mc_send_command - Send MC command and wait for response
26 *
27 * @mc_io: Pointer to MC I/O object to be used
28 * @cmd: MC command buffer. On input, it contains the command to send to the MC.
29 * On output, it contains the response from the MC if any.
30 *
31 * Depending on the sharing option specified when creating the MC portal
32 * wrapper, this function will use a spinlock or mutex to ensure exclusive
33 * access to the MC portal from the point when the command is sent until a
34 * response is received from the MC.
35 */
36int mc_send_command(struct fsl_mc_io *mc_io,
37 struct mc_command *cmd)
38{
39 enum mc_cmd_status status;
Prabhakar Kushwaha5940e4a2015-11-04 12:25:57 +053040 int timeout = 12000;
J. German Rivera43e4ae32015-01-06 13:19:02 -080041
42 mc_write_command(mc_io->mmio_regs, cmd);
43
44 for ( ; ; ) {
45 status = mc_read_response(mc_io->mmio_regs, cmd);
46 if (status != MC_CMD_STATUS_READY)
47 break;
48
49 if (--timeout == 0) {
50 printf("Error: Timeout waiting for MC response\n");
51 return -ETIMEDOUT;
52 }
53
54 udelay(500);
55 }
56
57 if (status != MC_CMD_STATUS_OK) {
58 printf("Error: MC command failed (portal: %p, obj handle: %#x, command: %#x, status: %#x)\n",
59 mc_io->mmio_regs,
Ioana Ciornei1c3769d2023-05-31 19:04:36 +030060 (unsigned int)mc_cmd_hdr_read_token(cmd),
61 (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
J. German Rivera43e4ae32015-01-06 13:19:02 -080062 (unsigned int)status);
63
64 return -EIO;
65 }
66
67 return 0;
68}