atmel_pio4.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <clk.h>
  11. #include <dm.h>
  12. #include <fdtdec.h>
  13. #include <asm/arch/hardware.h>
  14. #include <asm/gpio.h>
  15. #include <mach/gpio.h>
  16. #include <mach/atmel_pio4.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static struct atmel_pio4_port *atmel_pio4_port_base(u32 port)
  19. {
  20. struct atmel_pio4_port *base = NULL;
  21. switch (port) {
  22. case AT91_PIO_PORTA:
  23. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA;
  24. break;
  25. case AT91_PIO_PORTB:
  26. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB;
  27. break;
  28. case AT91_PIO_PORTC:
  29. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC;
  30. break;
  31. case AT91_PIO_PORTD:
  32. base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD;
  33. break;
  34. default:
  35. printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n",
  36. port);
  37. break;
  38. }
  39. return base;
  40. }
  41. static int atmel_pio4_config_io_func(u32 port, u32 pin,
  42. u32 func, u32 use_pullup)
  43. {
  44. struct atmel_pio4_port *port_base;
  45. u32 reg, mask;
  46. if (pin >= ATMEL_PIO_NPINS_PER_BANK)
  47. return -EINVAL;
  48. port_base = atmel_pio4_port_base(port);
  49. if (!port_base)
  50. return -EINVAL;
  51. mask = 1 << pin;
  52. reg = func;
  53. reg |= use_pullup ? ATMEL_PIO_PUEN_MASK : 0;
  54. writel(mask, &port_base->mskr);
  55. writel(reg, &port_base->cfgr);
  56. return 0;
  57. }
  58. int atmel_pio4_set_gpio(u32 port, u32 pin, u32 use_pullup)
  59. {
  60. return atmel_pio4_config_io_func(port, pin,
  61. ATMEL_PIO_CFGR_FUNC_GPIO,
  62. use_pullup);
  63. }
  64. int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 use_pullup)
  65. {
  66. return atmel_pio4_config_io_func(port, pin,
  67. ATMEL_PIO_CFGR_FUNC_PERIPH_A,
  68. use_pullup);
  69. }
  70. int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 use_pullup)
  71. {
  72. return atmel_pio4_config_io_func(port, pin,
  73. ATMEL_PIO_CFGR_FUNC_PERIPH_B,
  74. use_pullup);
  75. }
  76. int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 use_pullup)
  77. {
  78. return atmel_pio4_config_io_func(port, pin,
  79. ATMEL_PIO_CFGR_FUNC_PERIPH_C,
  80. use_pullup);
  81. }
  82. int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 use_pullup)
  83. {
  84. return atmel_pio4_config_io_func(port, pin,
  85. ATMEL_PIO_CFGR_FUNC_PERIPH_D,
  86. use_pullup);
  87. }
  88. int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 use_pullup)
  89. {
  90. return atmel_pio4_config_io_func(port, pin,
  91. ATMEL_PIO_CFGR_FUNC_PERIPH_E,
  92. use_pullup);
  93. }
  94. int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 use_pullup)
  95. {
  96. return atmel_pio4_config_io_func(port, pin,
  97. ATMEL_PIO_CFGR_FUNC_PERIPH_F,
  98. use_pullup);
  99. }
  100. int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 use_pullup)
  101. {
  102. return atmel_pio4_config_io_func(port, pin,
  103. ATMEL_PIO_CFGR_FUNC_PERIPH_G,
  104. use_pullup);
  105. }
  106. int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value)
  107. {
  108. struct atmel_pio4_port *port_base;
  109. u32 reg, mask;
  110. if (pin >= ATMEL_PIO_NPINS_PER_BANK)
  111. return -EINVAL;
  112. port_base = atmel_pio4_port_base(port);
  113. if (!port_base)
  114. return -EINVAL;
  115. mask = 0x01 << pin;
  116. reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK;
  117. writel(mask, &port_base->mskr);
  118. writel(reg, &port_base->cfgr);
  119. if (value)
  120. writel(mask, &port_base->sodr);
  121. else
  122. writel(mask, &port_base->codr);
  123. return 0;
  124. }
  125. int atmel_pio4_get_pio_input(u32 port, u32 pin)
  126. {
  127. struct atmel_pio4_port *port_base;
  128. u32 reg, mask;
  129. if (pin >= ATMEL_PIO_NPINS_PER_BANK)
  130. return -EINVAL;
  131. port_base = atmel_pio4_port_base(port);
  132. if (!port_base)
  133. return -EINVAL;
  134. mask = 0x01 << pin;
  135. reg = ATMEL_PIO_CFGR_FUNC_GPIO;
  136. writel(mask, &port_base->mskr);
  137. writel(reg, &port_base->cfgr);
  138. return (readl(&port_base->pdsr) & mask) ? 1 : 0;
  139. }
  140. #ifdef CONFIG_DM_GPIO
  141. struct atmel_pioctrl_data {
  142. u32 nbanks;
  143. };
  144. struct atmel_pio4_platdata {
  145. struct atmel_pio4_port *reg_base;
  146. };
  147. static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev,
  148. u32 bank)
  149. {
  150. struct atmel_pio4_platdata *plat = dev_get_platdata(dev);
  151. struct atmel_pio4_port *port_base =
  152. (struct atmel_pio4_port *)((u32)plat->reg_base +
  153. ATMEL_PIO_BANK_OFFSET * bank);
  154. return port_base;
  155. }
  156. static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset)
  157. {
  158. u32 bank = ATMEL_PIO_BANK(offset);
  159. u32 line = ATMEL_PIO_LINE(offset);
  160. struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
  161. u32 mask = BIT(line);
  162. writel(mask, &port_base->mskr);
  163. clrbits_le32(&port_base->cfgr,
  164. ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK);
  165. return 0;
  166. }
  167. static int atmel_pio4_direction_output(struct udevice *dev,
  168. unsigned offset, int value)
  169. {
  170. u32 bank = ATMEL_PIO_BANK(offset);
  171. u32 line = ATMEL_PIO_LINE(offset);
  172. struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
  173. u32 mask = BIT(line);
  174. writel(mask, &port_base->mskr);
  175. clrsetbits_le32(&port_base->cfgr,
  176. ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK);
  177. if (value)
  178. writel(mask, &port_base->sodr);
  179. else
  180. writel(mask, &port_base->codr);
  181. return 0;
  182. }
  183. static int atmel_pio4_get_value(struct udevice *dev, unsigned offset)
  184. {
  185. u32 bank = ATMEL_PIO_BANK(offset);
  186. u32 line = ATMEL_PIO_LINE(offset);
  187. struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
  188. u32 mask = BIT(line);
  189. return (readl(&port_base->pdsr) & mask) ? 1 : 0;
  190. }
  191. static int atmel_pio4_set_value(struct udevice *dev,
  192. unsigned offset, int value)
  193. {
  194. u32 bank = ATMEL_PIO_BANK(offset);
  195. u32 line = ATMEL_PIO_LINE(offset);
  196. struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
  197. u32 mask = BIT(line);
  198. if (value)
  199. writel(mask, &port_base->sodr);
  200. else
  201. writel(mask, &port_base->codr);
  202. return 0;
  203. }
  204. static int atmel_pio4_get_function(struct udevice *dev, unsigned offset)
  205. {
  206. u32 bank = ATMEL_PIO_BANK(offset);
  207. u32 line = ATMEL_PIO_LINE(offset);
  208. struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank);
  209. u32 mask = BIT(line);
  210. writel(mask, &port_base->mskr);
  211. return (readl(&port_base->cfgr) &
  212. ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT;
  213. }
  214. static const struct dm_gpio_ops atmel_pio4_ops = {
  215. .direction_input = atmel_pio4_direction_input,
  216. .direction_output = atmel_pio4_direction_output,
  217. .get_value = atmel_pio4_get_value,
  218. .set_value = atmel_pio4_set_value,
  219. .get_function = atmel_pio4_get_function,
  220. };
  221. static int atmel_pio4_bind(struct udevice *dev)
  222. {
  223. return dm_scan_fdt_dev(dev);
  224. }
  225. static int atmel_pio4_probe(struct udevice *dev)
  226. {
  227. struct atmel_pio4_platdata *plat = dev_get_platdata(dev);
  228. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  229. struct atmel_pioctrl_data *pioctrl_data;
  230. struct clk clk;
  231. fdt_addr_t addr_base;
  232. u32 nbanks;
  233. int ret;
  234. ret = clk_get_by_index(dev, 0, &clk);
  235. if (ret)
  236. return ret;
  237. ret = clk_enable(&clk);
  238. if (ret)
  239. return ret;
  240. clk_free(&clk);
  241. addr_base = devfdt_get_addr(dev);
  242. if (addr_base == FDT_ADDR_T_NONE)
  243. return -EINVAL;
  244. plat->reg_base = (struct atmel_pio4_port *)addr_base;
  245. pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev);
  246. nbanks = pioctrl_data->nbanks;
  247. uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev),
  248. NULL);
  249. uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK;
  250. return 0;
  251. }
  252. /*
  253. * The number of banks can be different from a SoC to another one.
  254. * We can have up to 16 banks.
  255. */
  256. static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
  257. .nbanks = 4,
  258. };
  259. static const struct udevice_id atmel_pio4_ids[] = {
  260. {
  261. .compatible = "atmel,sama5d2-gpio",
  262. .data = (ulong)&atmel_sama5d2_pioctrl_data,
  263. },
  264. {}
  265. };
  266. U_BOOT_DRIVER(gpio_atmel_pio4) = {
  267. .name = "gpio_atmel_pio4",
  268. .id = UCLASS_GPIO,
  269. .ops = &atmel_pio4_ops,
  270. .probe = atmel_pio4_probe,
  271. .bind = atmel_pio4_bind,
  272. .of_match = atmel_pio4_ids,
  273. .platdata_auto_alloc_size = sizeof(struct atmel_pio4_platdata),
  274. };
  275. #endif