mc_sys.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale Layerscape MC I/O wrapper
  4. *
  5. * Copyright (C) 2013-2015 Freescale Semiconductor, Inc.
  6. * Author: German Rivera <German.Rivera@freescale.com>
  7. */
  8. #include <fsl-mc/fsl_mc_sys.h>
  9. #include <fsl-mc/fsl_mc_cmd.h>
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <asm/io.h>
  13. #include <linux/delay.h>
  14. #define MC_CMD_HDR_READ_CMDID(_hdr) \
  15. ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S))
  16. /**
  17. * mc_send_command - Send MC command and wait for response
  18. *
  19. * @mc_io: Pointer to MC I/O object to be used
  20. * @cmd: MC command buffer. On input, it contains the command to send to the MC.
  21. * On output, it contains the response from the MC if any.
  22. *
  23. * Depending on the sharing option specified when creating the MC portal
  24. * wrapper, this function will use a spinlock or mutex to ensure exclusive
  25. * access to the MC portal from the point when the command is sent until a
  26. * response is received from the MC.
  27. */
  28. int mc_send_command(struct fsl_mc_io *mc_io,
  29. struct mc_command *cmd)
  30. {
  31. enum mc_cmd_status status;
  32. int timeout = 12000;
  33. mc_write_command(mc_io->mmio_regs, cmd);
  34. for ( ; ; ) {
  35. status = mc_read_response(mc_io->mmio_regs, cmd);
  36. if (status != MC_CMD_STATUS_READY)
  37. break;
  38. if (--timeout == 0) {
  39. printf("Error: Timeout waiting for MC response\n");
  40. return -ETIMEDOUT;
  41. }
  42. udelay(500);
  43. }
  44. if (status != MC_CMD_STATUS_OK) {
  45. printf("Error: MC command failed (portal: %p, obj handle: %#x, command: %#x, status: %#x)\n",
  46. mc_io->mmio_regs,
  47. (unsigned int)MC_CMD_HDR_READ_TOKEN(cmd->header),
  48. (unsigned int)MC_CMD_HDR_READ_CMDID(cmd->header),
  49. (unsigned int)status);
  50. return -EIO;
  51. }
  52. return 0;
  53. }