pinctrl_broadwell.c 6.9 KB

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