atmel_pio4.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Atmel PIO4 device driver
  3. *
  4. * Copyright (C) 2015 Atmel Corporation
  5. * Wenyou.Yang <wenyou.yang@atmel.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <asm/arch/hardware.h>
  12. #include <mach/gpio.h>
  13. #include <mach/atmel_pio4.h>
  14. static struct atmel_pio4_port *atmel_pio4_port_base(u32 port)
  15. {
  16. struct atmel_pio4_port *base = NULL;
  17. switch (port) {
  18. case AT91_PIO_PORTA:
  19. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA;
  20. break;
  21. case AT91_PIO_PORTB:
  22. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB;
  23. break;
  24. case AT91_PIO_PORTC:
  25. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC;
  26. break;
  27. case AT91_PIO_PORTD:
  28. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD;
  29. break;
  30. default:
  31. printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
  32. port);
  33. break;
  34. }
  35. return base;
  36. }
  37. static int atmel_pio4_config_io_func(u32 port, u32 pin,
  38. u32 func, u32 use_pullup)
  39. {
  40. struct atmel_pio4_port *port_base;
  41. u32 reg, mask;
  42. if (pin >= ATMEL_PIO_NPINS_PER_BANK)
  43. return -ENODEV;
  44. port_base = atmel_pio4_port_base(port);
  45. if (!port_base)
  46. return -ENODEV;
  47. mask = 1 << pin;
  48. reg = func;
  49. reg |= use_pullup ? ATMEL_PIO_PUEN_MASK : 0;
  50. writel(mask, &port_base->mskr);
  51. writel(reg, &port_base->cfgr);
  52. return 0;
  53. }
  54. int atmel_pio4_set_gpio(u32 port, u32 pin, u32 use_pullup)
  55. {
  56. return atmel_pio4_config_io_func(port, pin,
  57. ATMEL_PIO_CFGR_FUNC_GPIO,
  58. use_pullup);
  59. }
  60. int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 use_pullup)
  61. {
  62. return atmel_pio4_config_io_func(port, pin,
  63. ATMEL_PIO_CFGR_FUNC_PERIPH_A,
  64. use_pullup);
  65. }
  66. int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 use_pullup)
  67. {
  68. return atmel_pio4_config_io_func(port, pin,
  69. ATMEL_PIO_CFGR_FUNC_PERIPH_B,
  70. use_pullup);
  71. }
  72. int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 use_pullup)
  73. {
  74. return atmel_pio4_config_io_func(port, pin,
  75. ATMEL_PIO_CFGR_FUNC_PERIPH_C,
  76. use_pullup);
  77. }
  78. int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 use_pullup)
  79. {
  80. return atmel_pio4_config_io_func(port, pin,
  81. ATMEL_PIO_CFGR_FUNC_PERIPH_D,
  82. use_pullup);
  83. }
  84. int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 use_pullup)
  85. {
  86. return atmel_pio4_config_io_func(port, pin,
  87. ATMEL_PIO_CFGR_FUNC_PERIPH_E,
  88. use_pullup);
  89. }
  90. int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 use_pullup)
  91. {
  92. return atmel_pio4_config_io_func(port, pin,
  93. ATMEL_PIO_CFGR_FUNC_PERIPH_F,
  94. use_pullup);
  95. }
  96. int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 use_pullup)
  97. {
  98. return atmel_pio4_config_io_func(port, pin,
  99. ATMEL_PIO_CFGR_FUNC_PERIPH_G,
  100. use_pullup);
  101. }
  102. int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
  103. {
  104. struct atmel_pio4_port *port_base;
  105. u32 reg, mask;
  106. if (pin >= ATMEL_PIO_NPINS_PER_BANK)
  107. return -ENODEV;
  108. port_base = atmel_pio4_port_base(port);
  109. if (!port_base)
  110. return -ENODEV;
  111. mask = 0x01 << pin;
  112. reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
  113. writel(mask, &port_base->mskr);
  114. writel(reg, &port_base->cfgr);
  115. if (value)
  116. writel(mask, &port_base->sodr);
  117. else
  118. writel(mask, &port_base->codr);
  119. return 0;
  120. }
  121. int atmel_pio4_get_pio_input(u32 port, u32 pin)
  122. {
  123. struct atmel_pio4_port *port_base;
  124. u32 reg, mask;
  125. if (pin >= ATMEL_PIO_NPINS_PER_BANK)
  126. return -ENODEV;
  127. port_base = atmel_pio4_port_base(port);
  128. if (!port_base)
  129. return -ENODEV;
  130. mask = 0x01 << pin;
  131. reg = ATMEL_PIO_CFGR_FUNC_GPIO;
  132. writel(mask, &port_base->mskr);
  133. writel(reg, &port_base->cfgr);
  134. return (readl(&port_base->pdsr) & mask) ? 1 : 0;
  135. }
  136. #ifdef CONFIG_DM_GPIO
  137. static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
  138. {
  139. struct at91_port_platdata *plat = dev_get_platdata(dev);
  140. struct atmel_pio4_port *port_base = (atmel_pio4_port *)plat->base_addr;
  141. u32 mask = 0x01 << offset;
  142. u32 reg = ATMEL_PIO_CFGR_FUNC_GPIO;
  143. writel(mask, &port_base->mskr);
  144. writel(reg, &port_base->cfgr);
  145. return 0;
  146. }
  147. static int atmel_pio4_direction_output(struct udevice *dev,
  148. unsigned offset, int value)
  149. {
  150. struct at91_port_platdata *plat = dev_get_platdata(dev);
  151. struct atmel_pio4_port *port_base = (atmel_pio4_port *)plat->base_addr;
  152. u32 mask = 0x01 << offset;
  153. u32 reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
  154. writel(mask, &port_base->mskr);
  155. writel(reg, &port_base->cfgr);
  156. if (value)
  157. writel(mask, &port_base->sodr);
  158. else
  159. writel(mask, &port_base->codr);
  160. return 0;
  161. }
  162. static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
  163. {
  164. struct at91_port_platdata *plat = dev_get_platdata(dev);
  165. struct atmel_pio4_port *port_base = (atmel_pio4_port *)plat->base_addr;
  166. u32 mask = 0x01 << offset;
  167. return (readl(&port_base->pdsr) & mask) ? 1 : 0;
  168. }
  169. static int atmel_pio4_set_value(struct udevice *dev,
  170. unsigned offset, int value)
  171. {
  172. struct at91_port_platdata *plat = dev_get_platdata(dev);
  173. struct atmel_pio4_port *port_base = (atmel_pio4_port *)plat->base_addr;
  174. u32 mask = 0x01 << offset;
  175. if (value)
  176. writel(mask, &port_base->sodr);
  177. else
  178. writel(mask, &port_base->codr);
  179. return 0;
  180. }
  181. static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
  182. {
  183. struct at91_port_platdata *plat = dev_get_platdata(dev);
  184. struct atmel_pio4_port *port_base = (atmel_pio4_port *)plat->base_addr;
  185. u32 mask = 0x01 << offset;
  186. writel(mask, &port_base->mskr);
  187. return (readl(&port_base->cfgr) &
  188. ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
  189. }
  190. static const struct dm_gpio_ops atmel_pio4_ops = {
  191. .direction_input = atmel_pio4_direction_input,
  192. .direction_output = atmel_pio4_direction_output,
  193. .get_value = atmel_pio4_get_value,
  194. .set_value = atmel_pio4_set_value,
  195. .get_function = atmel_pio4_get_function,
  196. };
  197. static int atmel_pio4_probe(struct udevice *dev)
  198. {
  199. struct at91_port_platdata *plat = dev_get_platdata(dev);
  200. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  201. uc_priv->bank_name = plat->bank_name;
  202. uc_priv->gpio_count = ATMEL_PIO_NPINS_PER_BANK;
  203. return 0;
  204. }
  205. U_BOOT_DRIVER(gpio_atmel_pio4) = {
  206. .name = "gpio_atmel_pio4",
  207. .id = UCLASS_GPIO,
  208. .ops = &atmel_pio4_ops,
  209. .probe = atmel_pio4_probe,
  210. };
  211. #endif