altera_pio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
  4. * Copyright (C) 2011 Missing Link Electronics
  5. * Joachim Foerster <joachim@missinglinkelectronics.com>
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <malloc.h>
  11. #include <fdtdec.h>
  12. #include <asm/io.h>
  13. #include <asm/gpio.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. struct altera_pio_regs {
  16. u32 data; /* Data register */
  17. u32 direction; /* Direction register */
  18. };
  19. struct altera_pio_plat {
  20. struct altera_pio_regs *regs;
  21. int gpio_count;
  22. const char *bank_name;
  23. };
  24. static int altera_pio_direction_input(struct udevice *dev, unsigned pin)
  25. {
  26. struct altera_pio_plat *plat = dev_get_plat(dev);
  27. struct altera_pio_regs *const regs = plat->regs;
  28. clrbits_le32(&regs->direction, 1 << pin);
  29. return 0;
  30. }
  31. static int altera_pio_direction_output(struct udevice *dev, unsigned pin,
  32. int val)
  33. {
  34. struct altera_pio_plat *plat = dev_get_plat(dev);
  35. struct altera_pio_regs *const regs = plat->regs;
  36. if (val)
  37. setbits_le32(&regs->data, 1 << pin);
  38. else
  39. clrbits_le32(&regs->data, 1 << pin);
  40. /* change the data first, then the direction. to avoid glitch */
  41. setbits_le32(&regs->direction, 1 << pin);
  42. return 0;
  43. }
  44. static int altera_pio_get_value(struct udevice *dev, unsigned pin)
  45. {
  46. struct altera_pio_plat *plat = dev_get_plat(dev);
  47. struct altera_pio_regs *const regs = plat->regs;
  48. return !!(readl(&regs->data) & (1 << pin));
  49. }
  50. static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
  51. {
  52. struct altera_pio_plat *plat = dev_get_plat(dev);
  53. struct altera_pio_regs *const regs = plat->regs;
  54. if (val)
  55. setbits_le32(&regs->data, 1 << pin);
  56. else
  57. clrbits_le32(&regs->data, 1 << pin);
  58. return 0;
  59. }
  60. static int altera_pio_probe(struct udevice *dev)
  61. {
  62. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  63. struct altera_pio_plat *plat = dev_get_plat(dev);
  64. uc_priv->gpio_count = plat->gpio_count;
  65. uc_priv->bank_name = plat->bank_name;
  66. return 0;
  67. }
  68. static int altera_pio_of_to_plat(struct udevice *dev)
  69. {
  70. struct altera_pio_plat *plat = dev_get_plat(dev);
  71. plat->regs = map_physmem(dev_read_addr(dev),
  72. sizeof(struct altera_pio_regs),
  73. MAP_NOCACHE);
  74. plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  75. "altr,gpio-bank-width", 32);
  76. plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  77. "gpio-bank-name", NULL);
  78. return 0;
  79. }
  80. static const struct dm_gpio_ops altera_pio_ops = {
  81. .direction_input = altera_pio_direction_input,
  82. .direction_output = altera_pio_direction_output,
  83. .get_value = altera_pio_get_value,
  84. .set_value = altera_pio_set_value,
  85. };
  86. static const struct udevice_id altera_pio_ids[] = {
  87. { .compatible = "altr,pio-1.0" },
  88. { }
  89. };
  90. U_BOOT_DRIVER(altera_pio) = {
  91. .name = "altera_pio",
  92. .id = UCLASS_GPIO,
  93. .of_match = altera_pio_ids,
  94. .ops = &altera_pio_ops,
  95. .of_to_plat = altera_pio_of_to_plat,
  96. .plat_auto = sizeof(struct altera_pio_plat),
  97. .probe = altera_pio_probe,
  98. };