altera_pio.c 3.0 KB

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