pinmux.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file pinmux.c
  18. * @brief source file for the pinmux
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #include <stdint.h>
  23. #include "pinmux.h"
  24. #include "pin_name.h"
  25. #define readl(addr) \
  26. ({ unsigned int __v = (*(volatile unsigned int *) (addr)); __v; })
  27. #define writel(b,addr) (void)((*(volatile unsigned int *) (addr)) = (b))
  28. /*******************************************************************************
  29. * function: phobos_ioreuse_inital
  30. *
  31. * description:
  32. * initial phobos_pinmux
  33. *******************************************************************************/
  34. void phobos_ioreuse_initial(void)
  35. {
  36. unsigned int value;
  37. /* gpio data source select */
  38. value = readl(PHOBOS_GIPO0_PORTCTL_REG);
  39. value |= GPIO0_REUSE_EN;
  40. writel(value, PHOBOS_GIPO0_PORTCTL_REG);
  41. value = readl(PHOBOS_GIPO1_PORTCTL_REG);
  42. value |= GPIO1_REUSE_EN;
  43. writel(value, PHOBOS_GIPO1_PORTCTL_REG);
  44. /* reuse function select */
  45. value = readl(PHOBOS_IOMUX0L_REG);
  46. value |= IOMUX0L_FUNCTION_SEL;
  47. writel(value, PHOBOS_IOMUX0L_REG);
  48. value = readl(PHOBOS_IOMUX0H_REG);
  49. value |= IOMUX1L_FUNCTION_SEL;
  50. writel(value, PHOBOS_IOMUX0H_REG);
  51. value = readl(PHOBOS_IOMUX1L_REG);
  52. value |= IOMUX1L_FUNCTION_SEL;
  53. writel(value, PHOBOS_IOMUX1L_REG);
  54. }
  55. void phobos_pwm_ioreuse(void)
  56. {
  57. unsigned int value;
  58. /* gpio data source select */
  59. value = readl(PHOBOS_GIPO0_PORTCTL_REG);
  60. value |= PWM_GPIO0_REUSE_EN;
  61. writel(value, PHOBOS_GIPO0_PORTCTL_REG);
  62. /* reuse function select */
  63. value = readl(PHOBOS_IOMUX0L_REG);
  64. value |= PWM_IOMUX0L_FUNCTION_SEL;
  65. writel(value, PHOBOS_IOMUX0L_REG);
  66. }
  67. int32_t pin_mux(pin_name_t pin, uint16_t function)
  68. {
  69. unsigned int val = 0;
  70. unsigned int reg_val = 0;
  71. uint8_t offset;
  72. if (function > 3)
  73. {
  74. if (pin < PB0_ADC0_SDA0_PWM5_XX)
  75. {
  76. offset = pin;
  77. /* gpio data source select */
  78. val = readl(PHOBOS_GIPO0_PORTCTL_REG);
  79. val &= ~(1 << offset);
  80. writel(val, PHOBOS_GIPO0_PORTCTL_REG);
  81. return 0;
  82. }
  83. else if (pin >= PB0_ADC0_SDA0_PWM5_XX)
  84. {
  85. offset = pin - 32;
  86. /* gpio data source select */
  87. val = readl(PHOBOS_GIPO1_PORTCTL_REG);
  88. val &= ~(1 << offset);
  89. writel(val, PHOBOS_GIPO1_PORTCTL_REG);
  90. return 0;
  91. }
  92. else
  93. {
  94. return -1;
  95. }
  96. }
  97. if (pin >= PB0_ADC0_SDA0_PWM5_XX)
  98. {
  99. offset = pin - 32;
  100. /* gpio data source select */
  101. val = readl(PHOBOS_GIPO1_PORTCTL_REG);
  102. val |= (1 << offset);
  103. writel(val, PHOBOS_GIPO1_PORTCTL_REG);
  104. reg_val = (0x3 << (offset * 2));
  105. /* reuse function select */
  106. val = readl(PHOBOS_IOMUX1L_REG);
  107. val &= ~(reg_val);
  108. val |= (function << (2 * offset));
  109. writel(val, PHOBOS_IOMUX1L_REG);
  110. return 0;
  111. }
  112. offset = pin;
  113. /* gpio data source select */
  114. val = readl(PHOBOS_GIPO0_PORTCTL_REG);
  115. val |= (1 << offset);
  116. writel(val, PHOBOS_GIPO0_PORTCTL_REG);
  117. if (pin >= PA16_SPI1CLK_PWMTRIG1_XX_XX)
  118. {
  119. offset = pin - 16;
  120. reg_val = (0x3 << (offset * 2));
  121. /* reuse function select */
  122. val = readl(PHOBOS_IOMUX0H_REG);
  123. val &= ~(reg_val);
  124. val |= (function << (2 * offset));
  125. writel(val, PHOBOS_IOMUX0H_REG);
  126. return 0;
  127. }
  128. reg_val = (0x3 << (offset * 2));
  129. /* reuse function select */
  130. val = readl(PHOBOS_IOMUX0L_REG);
  131. val &= ~(reg_val);
  132. val |= (function << (2 * offset));
  133. writel(val, PHOBOS_IOMUX0L_REG);
  134. return 0;
  135. }