mxs_gpio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale i.MX28 GPIO control code
  4. *
  5. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  6. * on behalf of DENX Software Engineering GmbH
  7. */
  8. #include <common.h>
  9. #include <linux/errno.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/iomux.h>
  12. #include <asm/arch/imx-regs.h>
  13. #if defined(CONFIG_MX23)
  14. #define PINCTRL_BANKS 3
  15. #define PINCTRL_DOUT(n) (0x0500 + ((n) * 0x10))
  16. #define PINCTRL_DIN(n) (0x0600 + ((n) * 0x10))
  17. #define PINCTRL_DOE(n) (0x0700 + ((n) * 0x10))
  18. #define PINCTRL_PIN2IRQ(n) (0x0800 + ((n) * 0x10))
  19. #define PINCTRL_IRQEN(n) (0x0900 + ((n) * 0x10))
  20. #define PINCTRL_IRQSTAT(n) (0x0c00 + ((n) * 0x10))
  21. #elif defined(CONFIG_MX28)
  22. #define PINCTRL_BANKS 5
  23. #define PINCTRL_DOUT(n) (0x0700 + ((n) * 0x10))
  24. #define PINCTRL_DIN(n) (0x0900 + ((n) * 0x10))
  25. #define PINCTRL_DOE(n) (0x0b00 + ((n) * 0x10))
  26. #define PINCTRL_PIN2IRQ(n) (0x1000 + ((n) * 0x10))
  27. #define PINCTRL_IRQEN(n) (0x1100 + ((n) * 0x10))
  28. #define PINCTRL_IRQSTAT(n) (0x1400 + ((n) * 0x10))
  29. #else
  30. #error "Please select CONFIG_MX23 or CONFIG_MX28"
  31. #endif
  32. #define GPIO_INT_FALL_EDGE 0x0
  33. #define GPIO_INT_LOW_LEV 0x1
  34. #define GPIO_INT_RISE_EDGE 0x2
  35. #define GPIO_INT_HIGH_LEV 0x3
  36. #define GPIO_INT_LEV_MASK (1 << 0)
  37. #define GPIO_INT_POL_MASK (1 << 1)
  38. void mxs_gpio_init(void)
  39. {
  40. int i;
  41. for (i = 0; i < PINCTRL_BANKS; i++) {
  42. writel(0, MXS_PINCTRL_BASE + PINCTRL_PIN2IRQ(i));
  43. writel(0, MXS_PINCTRL_BASE + PINCTRL_IRQEN(i));
  44. /* Use SCT address here to clear the IRQSTAT bits */
  45. writel(0xffffffff, MXS_PINCTRL_BASE + PINCTRL_IRQSTAT(i) + 8);
  46. }
  47. }
  48. int gpio_get_value(unsigned gpio)
  49. {
  50. uint32_t bank = PAD_BANK(gpio);
  51. uint32_t offset = PINCTRL_DIN(bank);
  52. struct mxs_register_32 *reg =
  53. (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
  54. return (readl(&reg->reg) >> PAD_PIN(gpio)) & 1;
  55. }
  56. void gpio_set_value(unsigned gpio, int value)
  57. {
  58. uint32_t bank = PAD_BANK(gpio);
  59. uint32_t offset = PINCTRL_DOUT(bank);
  60. struct mxs_register_32 *reg =
  61. (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
  62. if (value)
  63. writel(1 << PAD_PIN(gpio), &reg->reg_set);
  64. else
  65. writel(1 << PAD_PIN(gpio), &reg->reg_clr);
  66. }
  67. int gpio_direction_input(unsigned gpio)
  68. {
  69. uint32_t bank = PAD_BANK(gpio);
  70. uint32_t offset = PINCTRL_DOE(bank);
  71. struct mxs_register_32 *reg =
  72. (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
  73. writel(1 << PAD_PIN(gpio), &reg->reg_clr);
  74. return 0;
  75. }
  76. int gpio_direction_output(unsigned gpio, int value)
  77. {
  78. uint32_t bank = PAD_BANK(gpio);
  79. uint32_t offset = PINCTRL_DOE(bank);
  80. struct mxs_register_32 *reg =
  81. (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
  82. gpio_set_value(gpio, value);
  83. writel(1 << PAD_PIN(gpio), &reg->reg_set);
  84. return 0;
  85. }
  86. int gpio_request(unsigned gpio, const char *label)
  87. {
  88. if (PAD_BANK(gpio) >= PINCTRL_BANKS)
  89. return -1;
  90. return 0;
  91. }
  92. int gpio_free(unsigned gpio)
  93. {
  94. return 0;
  95. }
  96. int name_to_gpio(const char *name)
  97. {
  98. unsigned bank, pin;
  99. char *end;
  100. bank = simple_strtoul(name, &end, 10);
  101. if (!*end || *end != ':')
  102. return bank;
  103. pin = simple_strtoul(end + 1, NULL, 10);
  104. return (bank << MXS_PAD_BANK_SHIFT) | (pin << MXS_PAD_PIN_SHIFT);
  105. }