gpio-xtensa.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2013 TangoTec Ltd.
  3. * Author: Baruch Siach <baruch@tkos.co.il>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Driver for the Xtensa LX4 GPIO32 Option
  10. *
  11. * Documentation: Xtensa LX4 Microprocessor Data Book, Section 2.22
  12. *
  13. * GPIO32 is a standard optional extension to the Xtensa architecture core that
  14. * provides preconfigured output and input ports for intra SoC signaling. The
  15. * GPIO32 option is implemented as 32bit Tensilica Instruction Extension (TIE)
  16. * output state called EXPSTATE, and 32bit input wire called IMPWIRE. This
  17. * driver treats input and output states as two distinct devices.
  18. *
  19. * Access to GPIO32 specific instructions is controlled by the CPENABLE
  20. * (Coprocessor Enable Bits) register. By default Xtensa Linux startup code
  21. * disables access to all coprocessors. This driver sets the CPENABLE bit
  22. * corresponding to GPIO32 before any GPIO32 specific instruction, and restores
  23. * CPENABLE state after that.
  24. *
  25. * This driver is currently incompatible with SMP. The GPIO32 extension is not
  26. * guaranteed to be available in all cores. Moreover, each core controls a
  27. * different set of IO wires. A theoretical SMP aware version of this driver
  28. * would need to have a per core workqueue to do the actual GPIO manipulation.
  29. */
  30. #include <linux/err.h>
  31. #include <linux/module.h>
  32. #include <linux/gpio.h>
  33. #include <linux/bitops.h>
  34. #include <linux/platform_device.h>
  35. #include <asm/coprocessor.h> /* CPENABLE read/write macros */
  36. #ifndef XCHAL_CP_ID_XTIOP
  37. #error GPIO32 option is not enabled for your xtensa core variant
  38. #endif
  39. #if XCHAL_HAVE_CP
  40. static inline unsigned long enable_cp(unsigned long *cpenable)
  41. {
  42. unsigned long flags;
  43. local_irq_save(flags);
  44. RSR_CPENABLE(*cpenable);
  45. WSR_CPENABLE(*cpenable | BIT(XCHAL_CP_ID_XTIOP));
  46. return flags;
  47. }
  48. static inline void disable_cp(unsigned long flags, unsigned long cpenable)
  49. {
  50. WSR_CPENABLE(cpenable);
  51. local_irq_restore(flags);
  52. }
  53. #else
  54. static inline unsigned long enable_cp(unsigned long *cpenable)
  55. {
  56. *cpenable = 0; /* avoid uninitialized value warning */
  57. return 0;
  58. }
  59. static inline void disable_cp(unsigned long flags, unsigned long cpenable)
  60. {
  61. }
  62. #endif /* XCHAL_HAVE_CP */
  63. static int xtensa_impwire_get_direction(struct gpio_chip *gc, unsigned offset)
  64. {
  65. return 1; /* input only */
  66. }
  67. static int xtensa_impwire_get_value(struct gpio_chip *gc, unsigned offset)
  68. {
  69. unsigned long flags, saved_cpenable;
  70. u32 impwire;
  71. flags = enable_cp(&saved_cpenable);
  72. __asm__ __volatile__("read_impwire %0" : "=a" (impwire));
  73. disable_cp(flags, saved_cpenable);
  74. return !!(impwire & BIT(offset));
  75. }
  76. static void xtensa_impwire_set_value(struct gpio_chip *gc, unsigned offset,
  77. int value)
  78. {
  79. BUG(); /* output only; should never be called */
  80. }
  81. static int xtensa_expstate_get_direction(struct gpio_chip *gc, unsigned offset)
  82. {
  83. return 0; /* output only */
  84. }
  85. static int xtensa_expstate_get_value(struct gpio_chip *gc, unsigned offset)
  86. {
  87. unsigned long flags, saved_cpenable;
  88. u32 expstate;
  89. flags = enable_cp(&saved_cpenable);
  90. __asm__ __volatile__("rur.expstate %0" : "=a" (expstate));
  91. disable_cp(flags, saved_cpenable);
  92. return !!(expstate & BIT(offset));
  93. }
  94. static void xtensa_expstate_set_value(struct gpio_chip *gc, unsigned offset,
  95. int value)
  96. {
  97. unsigned long flags, saved_cpenable;
  98. u32 mask = BIT(offset);
  99. u32 val = value ? BIT(offset) : 0;
  100. flags = enable_cp(&saved_cpenable);
  101. __asm__ __volatile__("wrmsk_expstate %0, %1"
  102. :: "a" (val), "a" (mask));
  103. disable_cp(flags, saved_cpenable);
  104. }
  105. static struct gpio_chip impwire_chip = {
  106. .label = "impwire",
  107. .base = -1,
  108. .ngpio = 32,
  109. .get_direction = xtensa_impwire_get_direction,
  110. .get = xtensa_impwire_get_value,
  111. .set = xtensa_impwire_set_value,
  112. };
  113. static struct gpio_chip expstate_chip = {
  114. .label = "expstate",
  115. .base = -1,
  116. .ngpio = 32,
  117. .get_direction = xtensa_expstate_get_direction,
  118. .get = xtensa_expstate_get_value,
  119. .set = xtensa_expstate_set_value,
  120. };
  121. static int xtensa_gpio_probe(struct platform_device *pdev)
  122. {
  123. int ret;
  124. ret = gpiochip_add_data(&impwire_chip, NULL);
  125. if (ret)
  126. return ret;
  127. return gpiochip_add_data(&expstate_chip, NULL);
  128. }
  129. static struct platform_driver xtensa_gpio_driver = {
  130. .driver = {
  131. .name = "xtensa-gpio",
  132. },
  133. .probe = xtensa_gpio_probe,
  134. };
  135. static int __init xtensa_gpio_init(void)
  136. {
  137. struct platform_device *pdev;
  138. pdev = platform_device_register_simple("xtensa-gpio", 0, NULL, 0);
  139. if (IS_ERR(pdev))
  140. return PTR_ERR(pdev);
  141. return platform_driver_register(&xtensa_gpio_driver);
  142. }
  143. device_initcall(xtensa_gpio_init);
  144. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  145. MODULE_DESCRIPTION("Xtensa LX4 GPIO32 driver");
  146. MODULE_LICENSE("GPL");