altera_pio.c 7.0 KB

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