altera_pio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Driver for Altera's PIO ip core
  3. *
  4. * Copyright (C) 2011 Missing Link Electronics
  5. * Joachim Foerster <joachim@missinglinkelectronics.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. * To use this driver, in your board's config. header:
  10. * #define CONFIG_ALTERA_PIO
  11. * #define CONFIG_SYS_ALTERA_PIO_NUM <number-of-pio-cores>
  12. * #define CONFIG_SYS_ALTERA_PIO_GPIO_NUM <total-number-of-gpios>
  13. * And in your board's early setup routine:
  14. * altera_pio_init(<baseaddr>, <width>, 'i'|'o'|'t',
  15. * <reset-value>, <neg-mask>, "label");
  16. * - 'i'|'o'|'t': PIO is input-only/output-only/tri-state
  17. * - <reset-value>: for correct initial status display, output-only
  18. * - <neg-mask> is meant to be used to in cases of active-low
  19. * GPIOs, such as LEDs and buttons (on/pressed == 0). Each bit
  20. * which is 1 in <neg-mask> inverts the corresponding GPIO's value
  21. * before set/after get. So: gpio_set_value(gpio, 1) => LED on .
  22. *
  23. * Do NOT define CONFIG_SYS_GPIO_BASE !
  24. *
  25. * Optionally, in your board's config. header:
  26. * - To force a GPIO numbering scheme like in Linux ...
  27. * #define CONFIG_GPIO_DOWNTO_NUMBERING
  28. * ... starting with 255 (default)
  29. * #define CONFIG_GPIO_DOWNTO_MAX 255
  30. */
  31. #include <common.h>
  32. #include <asm/io.h>
  33. #include <asm/gpio.h>
  34. #ifdef CONFIG_GPIO_DOWNTO_NUMBERING
  35. #ifndef CONFIG_GPIO_DOWNTO_MAX
  36. #define CONFIG_GPIO_DOWNTO_MAX 255
  37. #endif
  38. #endif
  39. #define ALTERA_PIO_DATA 0x0
  40. #define ALTERA_PIO_DIR 0x4
  41. #define GPIO_LABEL_SIZE 9
  42. static struct altera_pio {
  43. u32 base;
  44. u8 width;
  45. char iot;
  46. u32 negmask;
  47. u32 sh_data;
  48. u32 sh_dir;
  49. int gidx;
  50. char label[GPIO_LABEL_SIZE];
  51. } pios[CONFIG_SYS_ALTERA_PIO_NUM];
  52. static int pio_num;
  53. static struct altera_pio_gpio {
  54. unsigned num;
  55. struct altera_pio *pio;
  56. char reqlabel[GPIO_LABEL_SIZE];
  57. } gpios[CONFIG_SYS_ALTERA_PIO_GPIO_NUM];
  58. static int pio_gpio_num;
  59. static int altera_pio_gidx(unsigned gpio)
  60. {
  61. int i;
  62. for (i = 0; i < pio_gpio_num; ++i) {
  63. if (gpio == gpios[i].num)
  64. break;
  65. }
  66. if (i >= pio_gpio_num)
  67. return -1;
  68. return i;
  69. }
  70. static struct altera_pio *altera_pio_get_and_mask(unsigned gpio, u32 *mask)
  71. {
  72. int gidx = altera_pio_gidx(gpio);
  73. if (gidx < 0)
  74. return NULL;
  75. if (mask)
  76. *mask = 1 << (gidx - gpios[gidx].pio->gidx);
  77. return gpios[gidx].pio;
  78. }
  79. #define altera_pio_use_gidx(_gidx, _reqlabel) \
  80. { strncpy(gpios[_gidx].reqlabel, _reqlabel, GPIO_LABEL_SIZE); }
  81. #define altera_pio_unuse_gidx(_gidx) { gpios[_gidx].reqlabel[0] = '\0'; }
  82. #define altera_pio_is_gidx_used(_gidx) (gpios[_gidx].reqlabel[0] != '\0')
  83. static int altera_pio_gpio_init(struct altera_pio *pio, u8 width)
  84. {
  85. u8 gidx = pio_gpio_num;
  86. int i;
  87. if (!width)
  88. return -1;
  89. if ((pio_gpio_num + width) > CONFIG_SYS_ALTERA_PIO_GPIO_NUM)
  90. return -1;
  91. for (i = 0; i < width; ++i) {
  92. #ifdef CONFIG_GPIO_DOWNTO_NUMBERING
  93. gpios[pio_gpio_num + i].num = \
  94. CONFIG_GPIO_DOWNTO_MAX + 1 - gidx - width + i;
  95. #else
  96. gpios[pio_gpio_num + i].num = pio_gpio_num + i;
  97. #endif
  98. gpios[pio_gpio_num + i].pio = pio;
  99. altera_pio_unuse_gidx(pio_gpio_num + i);
  100. }
  101. pio_gpio_num += width;
  102. return gidx;
  103. }
  104. int altera_pio_init(u32 base, u8 width, char iot, u32 rstval, u32 negmask,
  105. const char *label)
  106. {
  107. if (pio_num >= CONFIG_SYS_ALTERA_PIO_NUM)
  108. return -1;
  109. pios[pio_num].base = base;
  110. pios[pio_num].width = width;
  111. pios[pio_num].iot = iot;
  112. switch (iot) {
  113. case 'i':
  114. /* input only */
  115. pios[pio_num].sh_dir = 0;
  116. pios[pio_num].sh_data = readl(base + ALTERA_PIO_DATA);
  117. break;
  118. case 'o':
  119. /* output only */
  120. pios[pio_num].sh_dir = 0xffffffff & ((1 << width) - 1);
  121. pios[pio_num].sh_data = rstval;
  122. break;
  123. case 't':
  124. /* bidir, tri-state */
  125. pios[pio_num].sh_dir = readl(base + ALTERA_PIO_DIR);
  126. pios[pio_num].sh_data = readl(base + ALTERA_PIO_DATA);
  127. break;
  128. default:
  129. return -1;
  130. }
  131. pios[pio_num].negmask = negmask & ((1 << width) - 1);
  132. pios[pio_num].gidx = altera_pio_gpio_init(&pios[pio_num], width);
  133. if (pios[pio_num].gidx < 0)
  134. return -1;
  135. strncpy(pios[pio_num].label, label, GPIO_LABEL_SIZE);
  136. return pio_num++;
  137. }
  138. void altera_pio_info(void)
  139. {
  140. int i;
  141. int j;
  142. int gidx;
  143. u32 mask;
  144. for (i = 0; i < pio_num; ++i) {
  145. printf("Altera PIO % 2d, @0x%08x, "
  146. "width: %u, label: %s\n",
  147. i, pios[i].base, pios[i].width, pios[i].label);
  148. gidx = pios[i].gidx;
  149. for (j = gidx; j < (gidx + pios[i].width); ++j) {
  150. mask = 1 << (j - gidx);
  151. printf("\tGPIO % 4d: %s %s [%c] %s\n",
  152. gpios[j].num,
  153. gpios[j].pio->sh_dir & mask ? "out" : " in",
  154. gpio_get_value(gpios[j].num) ? "set" : "clr",
  155. altera_pio_is_gidx_used(j) ? 'x' : ' ',
  156. gpios[j].reqlabel);
  157. }
  158. }
  159. }
  160. int gpio_request(unsigned gpio, const char *label)
  161. {
  162. int gidx = altera_pio_gidx(gpio);
  163. if (gidx < 0)
  164. return gidx;
  165. if (altera_pio_is_gidx_used(gidx))
  166. return -1;
  167. altera_pio_use_gidx(gidx, label);
  168. return 0;
  169. }
  170. int gpio_free(unsigned gpio)
  171. {
  172. int gidx = altera_pio_gidx(gpio);
  173. if (gidx < 0)
  174. return gidx;
  175. if (!altera_pio_is_gidx_used(gidx))
  176. return -1;
  177. altera_pio_unuse_gidx(gidx);
  178. return 0;
  179. }
  180. int gpio_direction_input(unsigned gpio)
  181. {
  182. u32 mask;
  183. struct altera_pio *pio;
  184. pio = altera_pio_get_and_mask(gpio, &mask);
  185. if (!pio)
  186. return -1;
  187. if (pio->iot == 'o')
  188. return -1;
  189. writel(pio->sh_dir &= ~mask, pio->base + ALTERA_PIO_DIR);
  190. return 0;
  191. }
  192. int gpio_direction_output(unsigned gpio, int value)
  193. {
  194. u32 mask;
  195. struct altera_pio *pio;
  196. pio = altera_pio_get_and_mask(gpio, &mask);
  197. if (!pio)
  198. return -1;
  199. if (pio->iot == 'i')
  200. return -1;
  201. value = (pio->negmask & mask) ? !value : value;
  202. if (value)
  203. pio->sh_data |= mask;
  204. else
  205. pio->sh_data &= ~mask;
  206. writel(pio->sh_data, pio->base + ALTERA_PIO_DATA);
  207. writel(pio->sh_dir |= mask, pio->base + ALTERA_PIO_DIR);
  208. return 0;
  209. }
  210. int gpio_get_value(unsigned gpio)
  211. {
  212. u32 mask;
  213. struct altera_pio *pio;
  214. u32 val;
  215. pio = altera_pio_get_and_mask(gpio, &mask);
  216. if (!pio)
  217. return -1;
  218. if ((pio->sh_dir & mask) || (pio->iot == 'o'))
  219. val = pio->sh_data & mask;
  220. else
  221. val = readl(pio->base + ALTERA_PIO_DATA) & mask;
  222. return (pio->negmask & mask) ? !val : val;
  223. }
  224. void gpio_set_value(unsigned gpio, int value)
  225. {
  226. u32 mask;
  227. struct altera_pio *pio;
  228. pio = altera_pio_get_and_mask(gpio, &mask);
  229. if (!pio)
  230. return;
  231. if (pio->iot == 'i')
  232. return;
  233. value = (pio->negmask & mask) ? !value : value;
  234. if (value)
  235. pio->sh_data |= mask;
  236. else
  237. pio->sh_data &= ~mask;
  238. writel(pio->sh_data, pio->base + ALTERA_PIO_DATA);
  239. return;
  240. }
  241. int gpio_is_valid(int number)
  242. {
  243. int gidx = altera_pio_gidx(number);
  244. if (gidx < 0)
  245. return 1;
  246. return 0;
  247. }