s3c2440_gpio.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2012
  3. * Gabriel Huau <contact@huau-gabriel.fr>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/arch/s3c2440.h>
  9. #include <asm/gpio.h>
  10. #include <asm/io.h>
  11. #include <errno.h>
  12. #define GPIO_INPUT 0x0
  13. #define GPIO_OUTPUT 0x1
  14. #define S3C_GPIO_CON 0x0
  15. #define S3C_GPIO_DAT 0x4
  16. static uint32_t s3c_gpio_get_bank_addr(unsigned gpio)
  17. {
  18. /* There is up to 16 pins per bank, one bank is 0x10 big. */
  19. uint32_t addr = gpio & ~0xf;
  20. if (addr >= 0x80 && addr != 0xd0) { /* Wrong GPIO bank. */
  21. printf("Invalid GPIO bank (bank %02x)\n", addr);
  22. return 0xffffffff;
  23. }
  24. return addr | S3C24X0_GPIO_BASE;
  25. }
  26. int gpio_set_value(unsigned gpio, int value)
  27. {
  28. uint32_t addr = s3c_gpio_get_bank_addr(gpio);
  29. if (addr == 0xffffffff)
  30. return -EINVAL;
  31. if (value)
  32. setbits_le32(addr | S3C_GPIO_DAT, 1 << (gpio & 0xf));
  33. else
  34. clrbits_le32(addr | S3C_GPIO_DAT, 1 << (gpio & 0xf));
  35. return 0;
  36. }
  37. int gpio_get_value(unsigned gpio)
  38. {
  39. uint32_t addr = s3c_gpio_get_bank_addr(gpio);
  40. if (addr == 0xffffffff)
  41. return -EINVAL;
  42. return !!(readl(addr | S3C_GPIO_DAT) & (1 << (gpio & 0xf)));
  43. }
  44. int gpio_request(unsigned gpio, const char *label)
  45. {
  46. return 0;
  47. }
  48. int gpio_free(unsigned gpio)
  49. {
  50. return 0;
  51. }
  52. static int s3c_gpio_direction(unsigned gpio, uint8_t dir)
  53. {
  54. uint32_t addr = s3c_gpio_get_bank_addr(gpio);
  55. const uint32_t mask = 0x3 << ((gpio & 0xf) << 1);
  56. const uint32_t dirm = dir << ((gpio & 0xf) << 1);
  57. if (addr == 0xffffffff)
  58. return -EINVAL;
  59. clrsetbits_le32(addr | S3C_GPIO_CON, mask, dirm);
  60. return 0;
  61. }
  62. int gpio_direction_input(unsigned gpio)
  63. {
  64. return s3c_gpio_direction(gpio, GPIO_INPUT);
  65. }
  66. int gpio_direction_output(unsigned gpio, int value)
  67. {
  68. return s3c_gpio_direction(gpio, GPIO_OUTPUT);
  69. }