Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Freescale Layerscape MC I/O wrapper |
| 4 | * |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 5 | * Copyright (C) 2013-2015 Freescale Semiconductor, Inc. |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 6 | * Author: German Rivera <German.Rivera@freescale.com> |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 7 | */ |
| 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 Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 15 | |
| 16 | #define MC_CMD_HDR_READ_CMDID(_hdr) \ |
Prabhakar Kushwaha | 9564df6 | 2015-07-07 15:40:06 +0530 | [diff] [blame] | 17 | ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S)) |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * mc_send_command - Send MC command and wait for response |
| 21 | * |
| 22 | * @mc_io: Pointer to MC I/O object to be used |
| 23 | * @cmd: MC command buffer. On input, it contains the command to send to the MC. |
| 24 | * On output, it contains the response from the MC if any. |
| 25 | * |
| 26 | * Depending on the sharing option specified when creating the MC portal |
| 27 | * wrapper, this function will use a spinlock or mutex to ensure exclusive |
| 28 | * access to the MC portal from the point when the command is sent until a |
| 29 | * response is received from the MC. |
| 30 | */ |
| 31 | int mc_send_command(struct fsl_mc_io *mc_io, |
| 32 | struct mc_command *cmd) |
| 33 | { |
| 34 | enum mc_cmd_status status; |
Prabhakar Kushwaha | 5940e4a | 2015-11-04 12:25:57 +0530 | [diff] [blame] | 35 | int timeout = 12000; |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 36 | |
| 37 | mc_write_command(mc_io->mmio_regs, cmd); |
| 38 | |
| 39 | for ( ; ; ) { |
| 40 | status = mc_read_response(mc_io->mmio_regs, cmd); |
| 41 | if (status != MC_CMD_STATUS_READY) |
| 42 | break; |
| 43 | |
| 44 | if (--timeout == 0) { |
| 45 | printf("Error: Timeout waiting for MC response\n"); |
| 46 | return -ETIMEDOUT; |
| 47 | } |
| 48 | |
| 49 | udelay(500); |
| 50 | } |
| 51 | |
| 52 | if (status != MC_CMD_STATUS_OK) { |
| 53 | printf("Error: MC command failed (portal: %p, obj handle: %#x, command: %#x, status: %#x)\n", |
| 54 | mc_io->mmio_regs, |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 55 | (unsigned int)MC_CMD_HDR_READ_TOKEN(cmd->header), |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 56 | (unsigned int)MC_CMD_HDR_READ_CMDID(cmd->header), |
| 57 | (unsigned int)status); |
| 58 | |
| 59 | return -EIO; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |