sandbox.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <fdtdec.h>
  8. #include <log.h>
  9. #include <malloc.h>
  10. #include <acpi/acpi_device.h>
  11. #include <asm/gpio.h>
  12. #include <dm/acpi.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/device_compat.h>
  15. #include <dm/lists.h>
  16. #include <dm/of.h>
  17. #include <dm/pinctrl.h>
  18. #include <dt-bindings/gpio/gpio.h>
  19. #include <dt-bindings/gpio/sandbox-gpio.h>
  20. struct gpio_state {
  21. const char *label; /* label given by requester */
  22. ulong flags; /* flags (GPIOD_...) */
  23. };
  24. /* Access routines for GPIO info */
  25. static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
  26. {
  27. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  28. struct gpio_state *state = dev_get_priv(dev);
  29. if (offset >= uc_priv->gpio_count) {
  30. printf("sandbox_gpio: error: invalid gpio %u\n", offset);
  31. return NULL;
  32. }
  33. return &state[offset];
  34. }
  35. /* Access routines for GPIO flags */
  36. static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
  37. {
  38. struct gpio_state *state = get_gpio_state(dev, offset);
  39. if (!state)
  40. return NULL;
  41. return &state->flags;
  42. }
  43. static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
  44. {
  45. return (*get_gpio_flags(dev, offset) & flag) != 0;
  46. }
  47. static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
  48. int value)
  49. {
  50. struct gpio_state *state = get_gpio_state(dev, offset);
  51. if (value)
  52. state->flags |= flag;
  53. else
  54. state->flags &= ~flag;
  55. return 0;
  56. }
  57. /*
  58. * Back-channel sandbox-internal-only access to GPIO state
  59. */
  60. int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
  61. {
  62. struct gpio_state *state = get_gpio_state(dev, offset);
  63. bool val;
  64. if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
  65. debug("sandbox_gpio: get_value on output gpio %u\n", offset);
  66. if (state->flags & GPIOD_EXT_DRIVEN) {
  67. val = state->flags & GPIOD_EXT_HIGH;
  68. } else {
  69. if (state->flags & GPIOD_EXT_PULL_UP)
  70. val = true;
  71. else if (state->flags & GPIOD_EXT_PULL_DOWN)
  72. val = false;
  73. else
  74. val = state->flags & GPIOD_PULL_UP;
  75. }
  76. return val;
  77. }
  78. int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  79. {
  80. set_gpio_flag(dev, offset, GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
  81. return 0;
  82. }
  83. int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
  84. {
  85. return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
  86. }
  87. int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
  88. {
  89. set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
  90. set_gpio_flag(dev, offset, GPIOD_IS_IN, !output);
  91. return 0;
  92. }
  93. ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
  94. {
  95. ulong flags = *get_gpio_flags(dev, offset);
  96. return flags & ~GPIOD_SANDBOX_MASK;
  97. }
  98. int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
  99. {
  100. struct gpio_state *state = get_gpio_state(dev, offset);
  101. state->flags = flags;
  102. return 0;
  103. }
  104. /*
  105. * These functions implement the public interface within U-Boot
  106. */
  107. /* set GPIO port 'offset' as an input */
  108. static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
  109. {
  110. debug("%s: offset:%u\n", __func__, offset);
  111. return sandbox_gpio_set_direction(dev, offset, 0);
  112. }
  113. /* set GPIO port 'offset' as an output, with polarity 'value' */
  114. static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
  115. int value)
  116. {
  117. int ret;
  118. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  119. ret = sandbox_gpio_set_direction(dev, offset, 1);
  120. if (ret)
  121. return ret;
  122. ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
  123. GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
  124. if (ret)
  125. return ret;
  126. return 0;
  127. }
  128. /* read GPIO IN value of port 'offset' */
  129. static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
  130. {
  131. debug("%s: offset:%u\n", __func__, offset);
  132. return sandbox_gpio_get_value(dev, offset);
  133. }
  134. /* write GPIO OUT value to port 'offset' */
  135. static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  136. {
  137. int ret;
  138. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  139. if (!sandbox_gpio_get_direction(dev, offset)) {
  140. printf("sandbox_gpio: error: set_value on input gpio %u\n",
  141. offset);
  142. return -1;
  143. }
  144. ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
  145. GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
  146. if (ret)
  147. return ret;
  148. return 0;
  149. }
  150. static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
  151. {
  152. if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
  153. return GPIOF_OUTPUT;
  154. if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
  155. return GPIOF_INPUT;
  156. return GPIOF_INPUT; /*GPIO is not configurated */
  157. }
  158. static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
  159. struct ofnode_phandle_args *args)
  160. {
  161. desc->offset = args->args[0];
  162. if (args->args_count < 2)
  163. return 0;
  164. /* treat generic binding with gpio uclass */
  165. gpio_xlate_offs_flags(dev, desc, args);
  166. /* sandbox test specific, not defined in gpio.h */
  167. if (args->args[1] & GPIO_IN)
  168. desc->flags |= GPIOD_IS_IN;
  169. if (args->args[1] & GPIO_OUT)
  170. desc->flags |= GPIOD_IS_OUT;
  171. if (args->args[1] & GPIO_OUT_ACTIVE)
  172. desc->flags |= GPIOD_IS_OUT_ACTIVE;
  173. return 0;
  174. }
  175. static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
  176. ulong flags)
  177. {
  178. debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
  179. struct gpio_state *state = get_gpio_state(dev, offset);
  180. if (flags & GPIOD_IS_OUT) {
  181. flags |= GPIOD_EXT_DRIVEN;
  182. if (flags & GPIOD_IS_OUT_ACTIVE)
  183. flags |= GPIOD_EXT_HIGH;
  184. else
  185. flags &= ~GPIOD_EXT_HIGH;
  186. } else {
  187. flags |= state->flags & GPIOD_SANDBOX_MASK;
  188. }
  189. state->flags = flags;
  190. return 0;
  191. }
  192. static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
  193. {
  194. debug("%s: offset:%u\n", __func__, offset);
  195. *flagsp = *get_gpio_flags(dev, offset) & ~GPIOD_SANDBOX_MASK;
  196. return 0;
  197. }
  198. #if CONFIG_IS_ENABLED(ACPIGEN)
  199. static int sb_gpio_get_acpi(const struct gpio_desc *desc,
  200. struct acpi_gpio *gpio)
  201. {
  202. int ret;
  203. /* Note that gpio_get_acpi() zeroes *gpio before calling here */
  204. gpio->pin_count = 1;
  205. gpio->pins[0] = desc->offset;
  206. ret = acpi_device_scope(desc->dev, gpio->resource,
  207. sizeof(gpio->resource));
  208. if (ret)
  209. return log_ret(ret);
  210. /* All of these values are just used for testing */
  211. if (desc->flags & GPIOD_ACTIVE_LOW) {
  212. gpio->pin0_addr = 0x80012 + desc->offset;
  213. gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
  214. gpio->pull = ACPI_GPIO_PULL_DOWN;
  215. gpio->interrupt_debounce_timeout = 4321;
  216. /* We use the GpioInt part */
  217. gpio->irq.pin = desc->offset;
  218. gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
  219. gpio->irq.shared = ACPI_IRQ_SHARED;
  220. gpio->irq.wake = ACPI_IRQ_WAKE;
  221. /* The GpioIo part is only used for testing */
  222. gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
  223. } else {
  224. gpio->pin0_addr = 0xc00dc + desc->offset;
  225. gpio->type = ACPI_GPIO_TYPE_IO;
  226. gpio->pull = ACPI_GPIO_PULL_UP;
  227. gpio->interrupt_debounce_timeout = 0;
  228. /* The GpioInt part is not used */
  229. /* We use the GpioIo part */
  230. gpio->output_drive_strength = 1234;
  231. gpio->io_shared = true;
  232. gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
  233. gpio->polarity = 0;
  234. }
  235. return 0;
  236. }
  237. static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
  238. {
  239. return acpi_copy_name(out_name, "GPIO");
  240. }
  241. struct acpi_ops gpio_sandbox_acpi_ops = {
  242. .get_name = sb_gpio_get_name,
  243. };
  244. #endif /* ACPIGEN */
  245. static const struct dm_gpio_ops gpio_sandbox_ops = {
  246. .direction_input = sb_gpio_direction_input,
  247. .direction_output = sb_gpio_direction_output,
  248. .get_value = sb_gpio_get_value,
  249. .set_value = sb_gpio_set_value,
  250. .get_function = sb_gpio_get_function,
  251. .xlate = sb_gpio_xlate,
  252. .set_flags = sb_gpio_set_flags,
  253. .get_flags = sb_gpio_get_flags,
  254. #if CONFIG_IS_ENABLED(ACPIGEN)
  255. .get_acpi = sb_gpio_get_acpi,
  256. #endif
  257. };
  258. static int sandbox_gpio_of_to_plat(struct udevice *dev)
  259. {
  260. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  261. uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
  262. 0);
  263. uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
  264. return 0;
  265. }
  266. static int gpio_sandbox_probe(struct udevice *dev)
  267. {
  268. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  269. if (!dev_has_ofnode(dev))
  270. /* Tell the uclass how many GPIOs we have */
  271. uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
  272. dev_set_priv(dev,
  273. calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
  274. return 0;
  275. }
  276. static int gpio_sandbox_remove(struct udevice *dev)
  277. {
  278. free(dev_get_priv(dev));
  279. return 0;
  280. }
  281. static const struct udevice_id sandbox_gpio_ids[] = {
  282. { .compatible = "sandbox,gpio" },
  283. { }
  284. };
  285. U_BOOT_DRIVER(sandbox_gpio) = {
  286. .name = "sandbox_gpio",
  287. .id = UCLASS_GPIO,
  288. .of_match = sandbox_gpio_ids,
  289. .of_to_plat = sandbox_gpio_of_to_plat,
  290. .probe = gpio_sandbox_probe,
  291. .remove = gpio_sandbox_remove,
  292. .ops = &gpio_sandbox_ops,
  293. ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
  294. };
  295. DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
  296. /* pincontrol: used only to check GPIO pin configuration (pinmux command) */
  297. struct sb_pinctrl_priv {
  298. int pinctrl_ngpios;
  299. struct list_head gpio_dev;
  300. };
  301. struct sb_gpio_bank {
  302. struct udevice *gpio_dev;
  303. struct list_head list;
  304. };
  305. static int sb_populate_gpio_dev_list(struct udevice *dev)
  306. {
  307. struct sb_pinctrl_priv *priv = dev_get_priv(dev);
  308. struct udevice *gpio_dev;
  309. struct udevice *child;
  310. struct sb_gpio_bank *gpio_bank;
  311. int ret;
  312. /*
  313. * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
  314. * a list with all gpio device reference which belongs to the
  315. * current pin-controller. This list is used to find pin_name and
  316. * pin muxing
  317. */
  318. list_for_each_entry(child, &dev->child_head, sibling_node) {
  319. ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
  320. &gpio_dev);
  321. if (ret < 0)
  322. continue;
  323. gpio_bank = malloc(sizeof(*gpio_bank));
  324. if (!gpio_bank) {
  325. dev_err(dev, "Not enough memory\n");
  326. return -ENOMEM;
  327. }
  328. gpio_bank->gpio_dev = gpio_dev;
  329. list_add_tail(&gpio_bank->list, &priv->gpio_dev);
  330. }
  331. return 0;
  332. }
  333. static int sb_pinctrl_get_pins_count(struct udevice *dev)
  334. {
  335. struct sb_pinctrl_priv *priv = dev_get_priv(dev);
  336. struct gpio_dev_priv *uc_priv;
  337. struct sb_gpio_bank *gpio_bank;
  338. /*
  339. * if get_pins_count has already been executed once on this
  340. * pin-controller, no need to run it again
  341. */
  342. if (priv->pinctrl_ngpios)
  343. return priv->pinctrl_ngpios;
  344. if (list_empty(&priv->gpio_dev))
  345. sb_populate_gpio_dev_list(dev);
  346. /*
  347. * walk through all banks to retrieve the pin-controller
  348. * pins number
  349. */
  350. list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
  351. uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
  352. priv->pinctrl_ngpios += uc_priv->gpio_count;
  353. }
  354. return priv->pinctrl_ngpios;
  355. }
  356. static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
  357. unsigned int selector,
  358. unsigned int *idx)
  359. {
  360. struct sb_pinctrl_priv *priv = dev_get_priv(dev);
  361. struct sb_gpio_bank *gpio_bank;
  362. struct gpio_dev_priv *uc_priv;
  363. int pin_count = 0;
  364. if (list_empty(&priv->gpio_dev))
  365. sb_populate_gpio_dev_list(dev);
  366. /* look up for the bank which owns the requested pin */
  367. list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
  368. uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
  369. if (selector < (pin_count + uc_priv->gpio_count)) {
  370. /*
  371. * we found the bank, convert pin selector to
  372. * gpio bank index
  373. */
  374. *idx = selector - pin_count;
  375. return gpio_bank->gpio_dev;
  376. }
  377. pin_count += uc_priv->gpio_count;
  378. }
  379. return NULL;
  380. }
  381. static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
  382. unsigned int selector)
  383. {
  384. struct gpio_dev_priv *uc_priv;
  385. struct udevice *gpio_dev;
  386. unsigned int gpio_idx;
  387. static char pin_name[PINNAME_SIZE];
  388. /* look up for the bank which owns the requested pin */
  389. gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
  390. if (!gpio_dev) {
  391. snprintf(pin_name, PINNAME_SIZE, "Error");
  392. } else {
  393. uc_priv = dev_get_uclass_priv(gpio_dev);
  394. snprintf(pin_name, PINNAME_SIZE, "%s%d",
  395. uc_priv->bank_name,
  396. gpio_idx);
  397. }
  398. return pin_name;
  399. }
  400. static char *get_flags_string(ulong flags)
  401. {
  402. if (flags & GPIOD_OPEN_DRAIN)
  403. return "drive-open-drain";
  404. if (flags & GPIOD_OPEN_SOURCE)
  405. return "drive-open-source";
  406. if (flags & GPIOD_PULL_UP)
  407. return "bias-pull-up";
  408. if (flags & GPIOD_PULL_DOWN)
  409. return "bias-pull-down";
  410. return ".";
  411. }
  412. static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
  413. unsigned int selector,
  414. char *buf, int size)
  415. {
  416. struct udevice *gpio_dev;
  417. unsigned int gpio_idx;
  418. ulong flags;
  419. int function;
  420. /* look up for the bank which owns the requested pin */
  421. gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
  422. if (!gpio_dev) {
  423. snprintf(buf, size, "Error");
  424. } else {
  425. function = sb_gpio_get_function(gpio_dev, gpio_idx);
  426. flags = *get_gpio_flags(gpio_dev, gpio_idx);
  427. snprintf(buf, size, "gpio %s %s",
  428. function == GPIOF_OUTPUT ? "output" : "input",
  429. get_flags_string(flags));
  430. }
  431. return 0;
  432. }
  433. #if CONFIG_IS_ENABLED(ACPIGEN)
  434. static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
  435. {
  436. return acpi_copy_name(out_name, "PINC");
  437. }
  438. #endif
  439. static int sandbox_pinctrl_probe(struct udevice *dev)
  440. {
  441. struct sb_pinctrl_priv *priv = dev_get_priv(dev);
  442. INIT_LIST_HEAD(&priv->gpio_dev);
  443. return 0;
  444. }
  445. static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
  446. .get_pin_name = sb_pinctrl_get_pin_name,
  447. .get_pins_count = sb_pinctrl_get_pins_count,
  448. .get_pin_muxing = sb_pinctrl_get_pin_muxing,
  449. };
  450. #if CONFIG_IS_ENABLED(ACPIGEN)
  451. struct acpi_ops pinctrl_sandbox_acpi_ops = {
  452. .get_name = sb_pinctrl_get_name,
  453. };
  454. #endif
  455. static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
  456. { .compatible = "sandbox,pinctrl-gpio" },
  457. { /* sentinel */ }
  458. };
  459. U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
  460. .name = "sandbox_pinctrl_gpio",
  461. .id = UCLASS_PINCTRL,
  462. .of_match = sandbox_pinctrl_gpio_match,
  463. .ops = &sandbox_pinctrl_gpio_ops,
  464. .bind = dm_scan_fdt_dev,
  465. .probe = sandbox_pinctrl_probe,
  466. .priv_auto = sizeof(struct sb_pinctrl_priv),
  467. ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)
  468. };