gpio.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Control GPIO pins on the fly
  3. *
  4. * Copyright (c) 2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <asm/blackfin.h>
  11. int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  12. {
  13. if (argc != 3) {
  14. show_usage:
  15. printf("Usage:\n%s\n", cmdtp->usage);
  16. return 1;
  17. }
  18. /* parse the behavior */
  19. ulong port_cmd = 0;
  20. switch (argv[1][0]) {
  21. case 'i': break;
  22. case 's': port_cmd = (PORTFIO_SET - PORTFIO); break;
  23. case 'c': port_cmd = (PORTFIO_CLEAR - PORTFIO); break;
  24. case 't': port_cmd = (PORTFIO_TOGGLE - PORTFIO); break;
  25. default: goto show_usage;
  26. }
  27. /* parse the pin with format: [p]<fgh><#> */
  28. const char *str_pin = argv[2];
  29. /* grab the [p]<fgh> portion */
  30. ulong port_base;
  31. if (*str_pin == 'p') ++str_pin;
  32. switch (*str_pin) {
  33. case 'f': port_base = PORTFIO; break;
  34. case 'g': port_base = PORTGIO; break;
  35. case 'h': port_base = PORTHIO; break;
  36. default: goto show_usage;
  37. }
  38. /* grab the <#> portion */
  39. ulong pin = simple_strtoul(str_pin+1, NULL, 10);
  40. ulong pin_mask = (1 << pin);
  41. if (pin > 15)
  42. goto show_usage;
  43. /* finally, let's do it: set direction and exec command */
  44. switch (*str_pin) {
  45. case 'f': bfin_write_PORTF_FER(bfin_read_PORTF_FER() & ~pin_mask); break;
  46. case 'g': bfin_write_PORTG_FER(bfin_read_PORTG_FER() & ~pin_mask); break;
  47. case 'h': bfin_write_PORTH_FER(bfin_read_PORTH_FER() & ~pin_mask); break;
  48. }
  49. ulong port_dir = port_base + (PORTFIO_DIR - PORTFIO);
  50. if (argv[1][0] == 'i')
  51. bfin_write16(port_dir, bfin_read16(port_dir) & ~pin_mask);
  52. else {
  53. bfin_write16(port_dir, bfin_read16(port_dir) | pin_mask);
  54. bfin_write16(port_base + port_cmd, pin_mask);
  55. }
  56. printf("gpio: pin %li on port %c has been %c\n", pin, *str_pin, argv[1][0]);
  57. return 0;
  58. }
  59. U_BOOT_CMD(gpio, 3, 0, do_gpio,
  60. "gpio - set/clear/toggle gpio output pins\n",
  61. "<s|c|t> <port><pin>\n"
  62. " - set/clear/toggle the specified pin\n");