xilinx_gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 - 2018 Xilinx, Michal Simek
  4. */
  5. #include <common.h>
  6. #include <errno.h>
  7. #include <malloc.h>
  8. #include <linux/list.h>
  9. #include <asm/io.h>
  10. #include <asm/gpio.h>
  11. #include <dm.h>
  12. static LIST_HEAD(gpio_list);
  13. enum gpio_direction {
  14. GPIO_DIRECTION_OUT = 0,
  15. GPIO_DIRECTION_IN = 1,
  16. };
  17. /* Gpio simple map */
  18. struct gpio_regs {
  19. u32 gpiodata;
  20. u32 gpiodir;
  21. };
  22. #if !defined(CONFIG_DM_GPIO)
  23. #define GPIO_NAME_SIZE 10
  24. struct gpio_names {
  25. char name[GPIO_NAME_SIZE];
  26. };
  27. /* Initialized, rxbd_current, rx_first_buf must be 0 after init */
  28. struct xilinx_gpio_priv {
  29. struct gpio_regs *regs;
  30. u32 gpio_min;
  31. u32 gpio_max;
  32. u32 gpiodata_store;
  33. char name[GPIO_NAME_SIZE];
  34. struct list_head list;
  35. struct gpio_names *gpio_name;
  36. };
  37. /* Store number of allocated gpio pins */
  38. static u32 xilinx_gpio_max;
  39. /* Get associated gpio controller */
  40. static struct xilinx_gpio_priv *gpio_get_controller(unsigned gpio)
  41. {
  42. struct list_head *entry;
  43. struct xilinx_gpio_priv *priv = NULL;
  44. list_for_each(entry, &gpio_list) {
  45. priv = list_entry(entry, struct xilinx_gpio_priv, list);
  46. if (gpio >= priv->gpio_min && gpio <= priv->gpio_max) {
  47. debug("%s: reg: %x, min-max: %d-%d\n", __func__,
  48. (u32)priv->regs, priv->gpio_min, priv->gpio_max);
  49. return priv;
  50. }
  51. }
  52. puts("!!!Can't get gpio controller!!!\n");
  53. return NULL;
  54. }
  55. /* Get gpio pin name if used/setup */
  56. static char *get_name(unsigned gpio)
  57. {
  58. u32 gpio_priv;
  59. struct xilinx_gpio_priv *priv;
  60. debug("%s\n", __func__);
  61. priv = gpio_get_controller(gpio);
  62. if (priv) {
  63. gpio_priv = gpio - priv->gpio_min;
  64. return *priv->gpio_name[gpio_priv].name ?
  65. priv->gpio_name[gpio_priv].name : "UNKNOWN";
  66. }
  67. return "UNKNOWN";
  68. }
  69. /* Get output value */
  70. static int gpio_get_output_value(unsigned gpio)
  71. {
  72. u32 val, gpio_priv;
  73. struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
  74. if (priv) {
  75. gpio_priv = gpio - priv->gpio_min;
  76. val = !!(priv->gpiodata_store & (1 << gpio_priv));
  77. debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
  78. (u32)priv->regs, gpio_priv, val);
  79. return val;
  80. }
  81. return -1;
  82. }
  83. /* Get input value */
  84. static int gpio_get_input_value(unsigned gpio)
  85. {
  86. u32 val, gpio_priv;
  87. struct gpio_regs *regs;
  88. struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
  89. if (priv) {
  90. regs = priv->regs;
  91. gpio_priv = gpio - priv->gpio_min;
  92. val = readl(&regs->gpiodata);
  93. val = !!(val & (1 << gpio_priv));
  94. debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
  95. (u32)priv->regs, gpio_priv, val);
  96. return val;
  97. }
  98. return -1;
  99. }
  100. /* Set gpio direction */
  101. static int gpio_set_direction(unsigned gpio, enum gpio_direction direction)
  102. {
  103. u32 val, gpio_priv;
  104. struct gpio_regs *regs;
  105. struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
  106. if (priv) {
  107. regs = priv->regs;
  108. val = readl(&regs->gpiodir);
  109. gpio_priv = gpio - priv->gpio_min;
  110. if (direction == GPIO_DIRECTION_OUT)
  111. val &= ~(1 << gpio_priv);
  112. else
  113. val |= 1 << gpio_priv;
  114. writel(val, &regs->gpiodir);
  115. debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
  116. (u32)priv->regs, gpio_priv, val);
  117. return 0;
  118. }
  119. return -1;
  120. }
  121. /* Get gpio direction */
  122. static int gpio_get_direction(unsigned gpio)
  123. {
  124. u32 val, gpio_priv;
  125. struct gpio_regs *regs;
  126. struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
  127. if (priv) {
  128. regs = priv->regs;
  129. gpio_priv = gpio - priv->gpio_min;
  130. val = readl(&regs->gpiodir);
  131. val = !!(val & (1 << gpio_priv));
  132. debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
  133. (u32)priv->regs, gpio_priv, val);
  134. return val;
  135. }
  136. return -1;
  137. }
  138. /*
  139. * Get input value
  140. * for example gpio setup to output only can't get input value
  141. * which is breaking gpio toggle command
  142. */
  143. int gpio_get_value(unsigned gpio)
  144. {
  145. u32 val;
  146. if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
  147. val = gpio_get_output_value(gpio);
  148. else
  149. val = gpio_get_input_value(gpio);
  150. return val;
  151. }
  152. /* Set output value */
  153. static int gpio_set_output_value(unsigned gpio, int value)
  154. {
  155. u32 val, gpio_priv;
  156. struct gpio_regs *regs;
  157. struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
  158. if (priv) {
  159. regs = priv->regs;
  160. gpio_priv = gpio - priv->gpio_min;
  161. val = priv->gpiodata_store;
  162. if (value)
  163. val |= 1 << gpio_priv;
  164. else
  165. val &= ~(1 << gpio_priv);
  166. writel(val, &regs->gpiodata);
  167. debug("%s: reg: %x, gpio_no: %d, output_val: %d\n", __func__,
  168. (u32)priv->regs, gpio_priv, val);
  169. priv->gpiodata_store = val;
  170. return 0;
  171. }
  172. return -1;
  173. }
  174. int gpio_set_value(unsigned gpio, int value)
  175. {
  176. if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
  177. return gpio_set_output_value(gpio, value);
  178. return -1;
  179. }
  180. /* Set GPIO as input */
  181. int gpio_direction_input(unsigned gpio)
  182. {
  183. debug("%s\n", __func__);
  184. return gpio_set_direction(gpio, GPIO_DIRECTION_IN);
  185. }
  186. /* Setup GPIO as output and set output value */
  187. int gpio_direction_output(unsigned gpio, int value)
  188. {
  189. int ret = gpio_set_direction(gpio, GPIO_DIRECTION_OUT);
  190. debug("%s\n", __func__);
  191. if (ret < 0)
  192. return ret;
  193. return gpio_set_output_value(gpio, value);
  194. }
  195. /* Show gpio status */
  196. void gpio_info(void)
  197. {
  198. unsigned gpio;
  199. struct list_head *entry;
  200. struct xilinx_gpio_priv *priv = NULL;
  201. list_for_each(entry, &gpio_list) {
  202. priv = list_entry(entry, struct xilinx_gpio_priv, list);
  203. printf("\n%s: %s/%x (%d-%d)\n", __func__, priv->name,
  204. (u32)priv->regs, priv->gpio_min, priv->gpio_max);
  205. for (gpio = priv->gpio_min; gpio <= priv->gpio_max; gpio++) {
  206. printf("GPIO_%d:\t%s is an ", gpio, get_name(gpio));
  207. if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
  208. printf("OUTPUT value = %d\n",
  209. gpio_get_output_value(gpio));
  210. else
  211. printf("INPUT value = %d\n",
  212. gpio_get_input_value(gpio));
  213. }
  214. }
  215. }
  216. int gpio_request(unsigned gpio, const char *label)
  217. {
  218. u32 gpio_priv;
  219. struct xilinx_gpio_priv *priv;
  220. if (gpio >= xilinx_gpio_max)
  221. return -EINVAL;
  222. priv = gpio_get_controller(gpio);
  223. if (priv) {
  224. gpio_priv = gpio - priv->gpio_min;
  225. if (label != NULL) {
  226. strncpy(priv->gpio_name[gpio_priv].name, label,
  227. GPIO_NAME_SIZE);
  228. priv->gpio_name[gpio_priv].name[GPIO_NAME_SIZE - 1] =
  229. '\0';
  230. }
  231. return 0;
  232. }
  233. return -1;
  234. }
  235. int gpio_free(unsigned gpio)
  236. {
  237. u32 gpio_priv;
  238. struct xilinx_gpio_priv *priv;
  239. if (gpio >= xilinx_gpio_max)
  240. return -EINVAL;
  241. priv = gpio_get_controller(gpio);
  242. if (priv) {
  243. gpio_priv = gpio - priv->gpio_min;
  244. priv->gpio_name[gpio_priv].name[0] = '\0';
  245. /* Do nothing here */
  246. return 0;
  247. }
  248. return -1;
  249. }
  250. int gpio_alloc(u32 baseaddr, const char *name, u32 gpio_no)
  251. {
  252. struct xilinx_gpio_priv *priv;
  253. priv = calloc(1, sizeof(struct xilinx_gpio_priv));
  254. /* Setup gpio name */
  255. if (name != NULL) {
  256. strncpy(priv->name, name, GPIO_NAME_SIZE);
  257. priv->name[GPIO_NAME_SIZE - 1] = '\0';
  258. }
  259. priv->regs = (struct gpio_regs *)baseaddr;
  260. priv->gpio_min = xilinx_gpio_max;
  261. xilinx_gpio_max = priv->gpio_min + gpio_no;
  262. priv->gpio_max = xilinx_gpio_max - 1;
  263. priv->gpio_name = calloc(gpio_no, sizeof(struct gpio_names));
  264. INIT_LIST_HEAD(&priv->list);
  265. list_add_tail(&priv->list, &gpio_list);
  266. printf("%s: Add %s (%d-%d)\n", __func__, name,
  267. priv->gpio_min, priv->gpio_max);
  268. /* Return the first gpio allocated for this device */
  269. return priv->gpio_min;
  270. }
  271. /* Dual channel gpio is one IP with two independent channels */
  272. int gpio_alloc_dual(u32 baseaddr, const char *name, u32 gpio_no0, u32 gpio_no1)
  273. {
  274. int ret;
  275. ret = gpio_alloc(baseaddr, name, gpio_no0);
  276. gpio_alloc(baseaddr + 8, strcat((char *)name, "_1"), gpio_no1);
  277. /* Return the first gpio allocated for this device */
  278. return ret;
  279. }
  280. #else
  281. #include <dt-bindings/gpio/gpio.h>
  282. #define XILINX_GPIO_MAX_BANK 2
  283. struct xilinx_gpio_platdata {
  284. struct gpio_regs *regs;
  285. int bank_max[XILINX_GPIO_MAX_BANK];
  286. int bank_input[XILINX_GPIO_MAX_BANK];
  287. int bank_output[XILINX_GPIO_MAX_BANK];
  288. };
  289. static int xilinx_gpio_get_bank_pin(unsigned offset, u32 *bank_num,
  290. u32 *bank_pin_num, struct udevice *dev)
  291. {
  292. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  293. u32 bank, max_pins;
  294. /* the first gpio is 0 not 1 */
  295. u32 pin_num = offset;
  296. for (bank = 0; bank < XILINX_GPIO_MAX_BANK; bank++) {
  297. max_pins = platdata->bank_max[bank];
  298. if (pin_num < max_pins) {
  299. debug("%s: found at bank 0x%x pin 0x%x\n", __func__,
  300. bank, pin_num);
  301. *bank_num = bank;
  302. *bank_pin_num = pin_num;
  303. return 0;
  304. }
  305. pin_num -= max_pins;
  306. }
  307. return -EINVAL;
  308. }
  309. static int xilinx_gpio_set_value(struct udevice *dev, unsigned offset,
  310. int value)
  311. {
  312. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  313. int val, ret;
  314. u32 bank, pin;
  315. ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
  316. if (ret)
  317. return ret;
  318. debug("%s: regs: %lx, value: %x, gpio: %x, bank %x, pin %x\n",
  319. __func__, (ulong)platdata->regs, value, offset, bank, pin);
  320. if (value) {
  321. val = readl(&platdata->regs->gpiodata + bank * 2);
  322. val = val | (1 << pin);
  323. writel(val, &platdata->regs->gpiodata + bank * 2);
  324. } else {
  325. val = readl(&platdata->regs->gpiodata + bank * 2);
  326. val = val & ~(1 << pin);
  327. writel(val, &platdata->regs->gpiodata + bank * 2);
  328. }
  329. return val;
  330. };
  331. static int xilinx_gpio_get_value(struct udevice *dev, unsigned offset)
  332. {
  333. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  334. int val, ret;
  335. u32 bank, pin;
  336. ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
  337. if (ret)
  338. return ret;
  339. debug("%s: regs: %lx, gpio: %x, bank %x, pin %x\n", __func__,
  340. (ulong)platdata->regs, offset, bank, pin);
  341. val = readl(&platdata->regs->gpiodata + bank * 2);
  342. val = !!(val & (1 << pin));
  343. return val;
  344. };
  345. static int xilinx_gpio_get_function(struct udevice *dev, unsigned offset)
  346. {
  347. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  348. int val, ret;
  349. u32 bank, pin;
  350. /* Check if all pins are inputs */
  351. if (platdata->bank_input[bank])
  352. return GPIOF_INPUT;
  353. /* Check if all pins are outputs */
  354. if (platdata->bank_output[bank])
  355. return GPIOF_OUTPUT;
  356. ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
  357. if (ret)
  358. return ret;
  359. /* FIXME test on dual */
  360. val = readl(&platdata->regs->gpiodir + bank * 2);
  361. val = !(val & (1 << pin));
  362. /* input is 1 in reg but GPIOF_INPUT is 0 */
  363. /* output is 0 in reg but GPIOF_OUTPUT is 1 */
  364. return val;
  365. }
  366. static int xilinx_gpio_direction_output(struct udevice *dev, unsigned offset,
  367. int value)
  368. {
  369. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  370. int val, ret;
  371. u32 bank, pin;
  372. ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
  373. if (ret)
  374. return ret;
  375. /* can't change it if all is input by default */
  376. if (platdata->bank_input[bank])
  377. return -EINVAL;
  378. if (!platdata->bank_output[bank]) {
  379. val = readl(&platdata->regs->gpiodir + bank * 2);
  380. val = val & ~(1 << pin);
  381. writel(val, &platdata->regs->gpiodir + bank * 2);
  382. }
  383. xilinx_gpio_set_value(dev, offset, value);
  384. return 0;
  385. }
  386. static int xilinx_gpio_direction_input(struct udevice *dev, unsigned offset)
  387. {
  388. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  389. int val, ret;
  390. u32 bank, pin;
  391. ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev);
  392. if (ret)
  393. return ret;
  394. /* Already input */
  395. if (platdata->bank_input[bank])
  396. return 0;
  397. /* can't change it if all is output by default */
  398. if (platdata->bank_output[bank])
  399. return -EINVAL;
  400. val = readl(&platdata->regs->gpiodir + bank * 2);
  401. val = val | (1 << pin);
  402. writel(val, &platdata->regs->gpiodir + bank * 2);
  403. return 0;
  404. }
  405. static int xilinx_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
  406. struct ofnode_phandle_args *args)
  407. {
  408. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  409. desc->offset = args->args[0];
  410. debug("%s: argc: %x, [0]: %x, [1]: %x, [2]: %x\n", __func__,
  411. args->args_count, args->args[0], args->args[1], args->args[2]);
  412. /*
  413. * The second cell is channel offset:
  414. * 0 is first channel, 8 is second channel
  415. *
  416. * U-Boot driver just combine channels together that's why simply
  417. * add amount of pins in second channel if present.
  418. */
  419. if (args->args[1]) {
  420. if (!platdata->bank_max[1]) {
  421. printf("%s: %s has no second channel\n",
  422. __func__, dev->name);
  423. return -EINVAL;
  424. }
  425. desc->offset += platdata->bank_max[0];
  426. }
  427. /* The third cell is optional */
  428. if (args->args_count > 2)
  429. desc->flags = (args->args[2] &
  430. GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0);
  431. debug("%s: offset %x, flags %lx\n",
  432. __func__, desc->offset, desc->flags);
  433. return 0;
  434. }
  435. static const struct dm_gpio_ops xilinx_gpio_ops = {
  436. .direction_input = xilinx_gpio_direction_input,
  437. .direction_output = xilinx_gpio_direction_output,
  438. .get_value = xilinx_gpio_get_value,
  439. .set_value = xilinx_gpio_set_value,
  440. .get_function = xilinx_gpio_get_function,
  441. .xlate = xilinx_gpio_xlate,
  442. };
  443. static int xilinx_gpio_probe(struct udevice *dev)
  444. {
  445. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  446. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  447. uc_priv->bank_name = dev->name;
  448. uc_priv->gpio_count = platdata->bank_max[0] + platdata->bank_max[1];
  449. return 0;
  450. }
  451. static int xilinx_gpio_ofdata_to_platdata(struct udevice *dev)
  452. {
  453. struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev);
  454. int is_dual;
  455. platdata->regs = (struct gpio_regs *)dev_read_addr(dev);
  456. platdata->bank_max[0] = dev_read_u32_default(dev,
  457. "xlnx,gpio-width", 0);
  458. platdata->bank_input[0] = dev_read_u32_default(dev,
  459. "xlnx,all-inputs", 0);
  460. platdata->bank_output[0] = dev_read_u32_default(dev,
  461. "xlnx,all-outputs", 0);
  462. is_dual = dev_read_u32_default(dev, "xlnx,is-dual", 0);
  463. if (is_dual) {
  464. platdata->bank_max[1] = dev_read_u32_default(dev,
  465. "xlnx,gpio2-width", 0);
  466. platdata->bank_input[1] = dev_read_u32_default(dev,
  467. "xlnx,all-inputs-2", 0);
  468. platdata->bank_output[1] = dev_read_u32_default(dev,
  469. "xlnx,all-outputs-2", 0);
  470. }
  471. return 0;
  472. }
  473. static const struct udevice_id xilinx_gpio_ids[] = {
  474. { .compatible = "xlnx,xps-gpio-1.00.a",},
  475. { }
  476. };
  477. U_BOOT_DRIVER(xilinx_gpio) = {
  478. .name = "xlnx_gpio",
  479. .id = UCLASS_GPIO,
  480. .ops = &xilinx_gpio_ops,
  481. .of_match = xilinx_gpio_ids,
  482. .ofdata_to_platdata = xilinx_gpio_ofdata_to_platdata,
  483. .probe = xilinx_gpio_probe,
  484. .platdata_auto_alloc_size = sizeof(struct xilinx_gpio_platdata),
  485. };
  486. #endif