blob: 482fb0463d5db30b25cb6c099f3d7b1a1234f0e6 [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>
J. German Rivera43e4ae32015-01-06 13:19:02 -080011#include <errno.h>
12#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080014
Ioana Ciornei1c3769d2023-05-31 19:04:36 +030015static u16 mc_cmd_hdr_read_cmdid(struct mc_command *cmd)
16{
17 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
18 u16 cmd_id = le16_to_cpu(hdr->cmd_id);
19
20 return cmd_id;
21}
J. German Rivera43e4ae32015-01-06 13:19:02 -080022
23/**
24 * mc_send_command - Send MC command and wait for response
25 *
26 * @mc_io: Pointer to MC I/O object to be used
27 * @cmd: MC command buffer. On input, it contains the command to send to the MC.
28 * On output, it contains the response from the MC if any.
29 *
30 * Depending on the sharing option specified when creating the MC portal
31 * wrapper, this function will use a spinlock or mutex to ensure exclusive
32 * access to the MC portal from the point when the command is sent until a
33 * response is received from the MC.
34 */
35int mc_send_command(struct fsl_mc_io *mc_io,
36 struct mc_command *cmd)
37{
38 enum mc_cmd_status status;
Prabhakar Kushwaha5940e4a2015-11-04 12:25:57 +053039 int timeout = 12000;
J. German Rivera43e4ae32015-01-06 13:19:02 -080040
41 mc_write_command(mc_io->mmio_regs, cmd);
42
43 for ( ; ; ) {
44 status = mc_read_response(mc_io->mmio_regs, cmd);
45 if (status != MC_CMD_STATUS_READY)
46 break;
47
48 if (--timeout == 0) {
49 printf("Error: Timeout waiting for MC response\n");
50 return -ETIMEDOUT;
51 }
52
53 udelay(500);
54 }
55
56 if (status != MC_CMD_STATUS_OK) {
57 printf("Error: MC command failed (portal: %p, obj handle: %#x, command: %#x, status: %#x)\n",
58 mc_io->mmio_regs,
Ioana Ciornei1c3769d2023-05-31 19:04:36 +030059 (unsigned int)mc_cmd_hdr_read_token(cmd),
60 (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
J. German Rivera43e4ae32015-01-06 13:19:02 -080061 (unsigned int)status);
62
63 return -EIO;
64 }
65
66 return 0;
67}