mxs_gpio.c 3.1 KB

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