uart.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2013 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. /*
  15. * Implementation of UART gxio calls.
  16. */
  17. #include <linux/io.h>
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <gxio/uart.h>
  21. #include <gxio/iorpc_globals.h>
  22. #include <gxio/iorpc_uart.h>
  23. #include <gxio/kiorpc.h>
  24. int gxio_uart_init(gxio_uart_context_t *context, int uart_index)
  25. {
  26. char file[32];
  27. int fd;
  28. snprintf(file, sizeof(file), "uart/%d/iorpc", uart_index);
  29. fd = hv_dev_open((HV_VirtAddr) file, 0);
  30. if (fd < 0) {
  31. if (fd >= GXIO_ERR_MIN && fd <= GXIO_ERR_MAX)
  32. return fd;
  33. else
  34. return -ENODEV;
  35. }
  36. context->fd = fd;
  37. /* Map in the MMIO space. */
  38. context->mmio_base = (void __force *)
  39. iorpc_ioremap(fd, HV_UART_MMIO_OFFSET, HV_UART_MMIO_SIZE);
  40. if (context->mmio_base == NULL) {
  41. hv_dev_close(context->fd);
  42. context->fd = -1;
  43. return -ENODEV;
  44. }
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(gxio_uart_init);
  48. int gxio_uart_destroy(gxio_uart_context_t *context)
  49. {
  50. iounmap((void __force __iomem *)(context->mmio_base));
  51. hv_dev_close(context->fd);
  52. context->mmio_base = NULL;
  53. context->fd = -1;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(gxio_uart_destroy);
  57. /* UART register write wrapper. */
  58. void gxio_uart_write(gxio_uart_context_t *context, uint64_t offset,
  59. uint64_t word)
  60. {
  61. __gxio_mmio_write(context->mmio_base + offset, word);
  62. }
  63. EXPORT_SYMBOL_GPL(gxio_uart_write);
  64. /* UART register read wrapper. */
  65. uint64_t gxio_uart_read(gxio_uart_context_t *context, uint64_t offset)
  66. {
  67. return __gxio_mmio_read(context->mmio_base + offset);
  68. }
  69. EXPORT_SYMBOL_GPL(gxio_uart_read);