atmel_pio4.c 8.3 KB

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