pinctrl_broadwell.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <fdtdec.h>
  9. #include <pch.h>
  10. #include <pci.h>
  11. #include <asm/cpu.h>
  12. #include <asm/gpio.h>
  13. #include <asm/io.h>
  14. #include <asm/pci.h>
  15. #include <asm/arch/gpio.h>
  16. #include <dt-bindings/gpio/x86-gpio.h>
  17. #include <dm/pinctrl.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. enum {
  20. MAX_GPIOS = 95,
  21. };
  22. #define PIRQ_SHIFT 16
  23. #define CONF_MASK 0xffff
  24. struct pin_info {
  25. int node;
  26. int phandle;
  27. bool mode_gpio;
  28. bool dir_input;
  29. bool invert;
  30. bool trigger_level;
  31. bool output_high;
  32. bool sense_disable;
  33. bool owner_gpio;
  34. bool route_smi;
  35. bool irq_enable;
  36. bool reset_rsmrst;
  37. bool pirq_apic_route;
  38. };
  39. static int broadwell_pinctrl_read_configs(struct udevice *dev,
  40. struct pin_info *conf, int max_pins)
  41. {
  42. const void *blob = gd->fdt_blob;
  43. int count = 0;
  44. int node;
  45. debug("%s: starting\n", __func__);
  46. for (node = fdt_first_subnode(blob, dev_of_offset(dev));
  47. node > 0;
  48. node = fdt_next_subnode(blob, node)) {
  49. int phandle = fdt_get_phandle(blob, node);
  50. if (!phandle)
  51. continue;
  52. if (count == max_pins)
  53. return -ENOSPC;
  54. /* We've found a new configuration */
  55. memset(conf, '\0', sizeof(*conf));
  56. conf->node = node;
  57. conf->phandle = phandle;
  58. conf->mode_gpio = fdtdec_get_bool(blob, node, "mode-gpio");
  59. if (fdtdec_get_int(blob, node, "direction", -1) == PIN_INPUT)
  60. conf->dir_input = true;
  61. conf->invert = fdtdec_get_bool(blob, node, "invert");
  62. if (fdtdec_get_int(blob, node, "trigger", -1) == TRIGGER_LEVEL)
  63. conf->trigger_level = true;
  64. if (fdtdec_get_int(blob, node, "output-value", -1) == 1)
  65. conf->output_high = true;
  66. conf->sense_disable = fdtdec_get_bool(blob, node,
  67. "sense-disable");
  68. if (fdtdec_get_int(blob, node, "owner", -1) == OWNER_GPIO)
  69. conf->owner_gpio = true;
  70. if (fdtdec_get_int(blob, node, "route", -1) == ROUTE_SMI)
  71. conf->route_smi = true;
  72. conf->irq_enable = fdtdec_get_bool(blob, node, "irq-enable");
  73. conf->reset_rsmrst = fdtdec_get_bool(blob, node,
  74. "reset-rsmrst");
  75. if (fdtdec_get_int(blob, node, "pirq-apic", -1) ==
  76. PIRQ_APIC_ROUTE)
  77. conf->pirq_apic_route = true;
  78. debug("config: phandle=%d\n", phandle);
  79. count++;
  80. conf++;
  81. }
  82. debug("%s: Found %d configurations\n", __func__, count);
  83. return count;
  84. }
  85. static int broadwell_pinctrl_lookup_phandle(struct pin_info *conf,
  86. int conf_count, int phandle)
  87. {
  88. int i;
  89. for (i = 0; i < conf_count; i++) {
  90. if (conf[i].phandle == phandle)
  91. return i;
  92. }
  93. return -ENOENT;
  94. }
  95. static int broadwell_pinctrl_read_pins(struct udevice *dev,
  96. struct pin_info *conf, int conf_count, int gpio_conf[],
  97. int num_gpios)
  98. {
  99. const void *blob = gd->fdt_blob;
  100. int count = 0;
  101. int node;
  102. for (node = fdt_first_subnode(blob, dev_of_offset(dev));
  103. node > 0;
  104. node = fdt_next_subnode(blob, node)) {
  105. int len, i;
  106. const u32 *prop = fdt_getprop(blob, node, "config", &len);
  107. if (!prop)
  108. continue;
  109. /* There are three cells per pin */
  110. count = len / (sizeof(u32) * 3);
  111. debug("Found %d GPIOs to configure\n", count);
  112. for (i = 0; i < count; i++) {
  113. uint gpio = fdt32_to_cpu(prop[i * 3]);
  114. uint phandle = fdt32_to_cpu(prop[i * 3 + 1]);
  115. int val;
  116. if (gpio >= num_gpios) {
  117. debug("%s: GPIO %d out of range\n", __func__,
  118. gpio);
  119. return -EDOM;
  120. }
  121. val = broadwell_pinctrl_lookup_phandle(conf, conf_count,
  122. phandle);
  123. if (val < 0) {
  124. debug("%s: Cannot find phandle %d\n", __func__,
  125. phandle);
  126. return -EINVAL;
  127. }
  128. gpio_conf[gpio] = val |
  129. fdt32_to_cpu(prop[i * 3 + 2]) << PIRQ_SHIFT;
  130. }
  131. }
  132. return 0;
  133. }
  134. static void broadwell_pinctrl_commit(struct pch_lp_gpio_regs *regs,
  135. struct pin_info *pin_info,
  136. int gpio_conf[], int count)
  137. {
  138. u32 owner_gpio[GPIO_BANKS] = {0};
  139. u32 route_smi[GPIO_BANKS] = {0};
  140. u32 irq_enable[GPIO_BANKS] = {0};
  141. u32 reset_rsmrst[GPIO_BANKS] = {0};
  142. u32 pirq2apic = 0;
  143. int set, bit, gpio = 0;
  144. for (gpio = 0; gpio < MAX_GPIOS; gpio++) {
  145. int confnum = gpio_conf[gpio] & CONF_MASK;
  146. struct pin_info *pin = &pin_info[confnum];
  147. u32 val;
  148. val = pin->mode_gpio << CONFA_MODE_SHIFT |
  149. pin->dir_input << CONFA_DIR_SHIFT |
  150. pin->invert << CONFA_INVERT_SHIFT |
  151. pin->trigger_level << CONFA_TRIGGER_SHIFT |
  152. pin->output_high << CONFA_OUTPUT_SHIFT;
  153. outl(val, &regs->config[gpio].conf_a);
  154. outl(pin->sense_disable << CONFB_SENSE_SHIFT,
  155. &regs->config[gpio].conf_b);
  156. /* Determine set and bit based on GPIO number */
  157. set = gpio / GPIO_PER_BANK;
  158. bit = gpio % GPIO_PER_BANK;
  159. /* Apply settings to set specific bits */
  160. owner_gpio[set] |= pin->owner_gpio << bit;
  161. route_smi[set] |= pin->route_smi << bit;
  162. irq_enable[set] |= pin->irq_enable << bit;
  163. reset_rsmrst[set] |= pin->reset_rsmrst << bit;
  164. /* PIRQ to IO-APIC map */
  165. if (pin->pirq_apic_route)
  166. pirq2apic |= gpio_conf[gpio] >> PIRQ_SHIFT;
  167. debug("gpio %d: conf %d, mode_gpio %d, dir_input %d, output_high %d\n",
  168. gpio, confnum, pin->mode_gpio, pin->dir_input,
  169. pin->output_high);
  170. }
  171. for (set = 0; set < GPIO_BANKS; set++) {
  172. outl(owner_gpio[set], &regs->own[set]);
  173. outl(route_smi[set], &regs->gpi_route[set]);
  174. outl(irq_enable[set], &regs->gpi_ie[set]);
  175. outl(reset_rsmrst[set], &regs->rst_sel[set]);
  176. }
  177. outl(pirq2apic, &regs->pirq_to_ioxapic);
  178. }
  179. static int broadwell_pinctrl_probe(struct udevice *dev)
  180. {
  181. struct pch_lp_gpio_regs *regs;
  182. struct pin_info conf[12];
  183. int gpio_conf[MAX_GPIOS];
  184. struct udevice *pch;
  185. int conf_count;
  186. u32 gpiobase;
  187. int ret;
  188. ret = uclass_first_device(UCLASS_PCH, &pch);
  189. if (ret)
  190. return ret;
  191. if (!pch)
  192. return -ENODEV;
  193. debug("%s: start\n", __func__);
  194. /* Only init once, before relocation */
  195. if (gd->flags & GD_FLG_RELOC)
  196. return 0;
  197. /*
  198. * Get the memory/io base address to configure every pins.
  199. * IOBASE is used to configure the mode/pads
  200. * GPIOBASE is used to configure the direction and default value
  201. */
  202. ret = pch_get_gpio_base(pch, &gpiobase);
  203. if (ret) {
  204. debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
  205. gpiobase);
  206. return -EINVAL;
  207. }
  208. conf_count = broadwell_pinctrl_read_configs(dev, conf,
  209. ARRAY_SIZE(conf));
  210. if (conf_count < 0) {
  211. debug("%s: Cannot read configs: err=%d\n", __func__, ret);
  212. return conf_count;
  213. }
  214. /*
  215. * Assume that pin settings are provided for every pin. Pins not
  216. * mentioned will get the first config mentioned in the list.
  217. */
  218. ret = broadwell_pinctrl_read_pins(dev, conf, conf_count, gpio_conf,
  219. MAX_GPIOS);
  220. if (ret) {
  221. debug("%s: Cannot read pin settings: err=%d\n", __func__, ret);
  222. return ret;
  223. }
  224. regs = (struct pch_lp_gpio_regs *)gpiobase;
  225. broadwell_pinctrl_commit(regs, conf, gpio_conf, ARRAY_SIZE(conf));
  226. debug("%s: done\n", __func__);
  227. return 0;
  228. }
  229. static const struct udevice_id broadwell_pinctrl_match[] = {
  230. { .compatible = "intel,x86-broadwell-pinctrl",
  231. .data = X86_SYSCON_PINCONF },
  232. { /* sentinel */ }
  233. };
  234. U_BOOT_DRIVER(broadwell_pinctrl) = {
  235. .name = "broadwell_pinctrl",
  236. .id = UCLASS_SYSCON,
  237. .of_match = broadwell_pinctrl_match,
  238. .probe = broadwell_pinctrl_probe,
  239. };