uart-spi-switch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/gpio.h>
  24. #include <asm/arch/pinmux.h>
  25. #include <asm/arch/uart-spi-switch.h>
  26. #include <asm/arch/tegra.h>
  27. #include <asm/arch-tegra/tegra_spi.h>
  28. #include <asm/arch-tegra/board.h>
  29. /* position of the UART/SPI select switch */
  30. enum spi_uart_switch {
  31. SWITCH_UNKNOWN,
  32. SWITCH_SPI,
  33. SWITCH_UART,
  34. SWITCH_BOTH
  35. };
  36. /* Information about the spi/uart switch */
  37. struct spi_uart {
  38. int gpio; /* GPIO to control switch */
  39. u32 port; /* Port number of UART affected */
  40. };
  41. static struct spi_uart local;
  42. static enum spi_uart_switch switch_pos; /* Current switch position */
  43. static void get_config(struct spi_uart *config)
  44. {
  45. #if defined CONFIG_SPI_CORRUPTS_UART
  46. config->gpio = CONFIG_UART_DISABLE_GPIO;
  47. config->port = CONFIG_SPI_CORRUPTS_UART_NR;
  48. #else
  49. config->gpio = -1;
  50. #endif
  51. }
  52. /*
  53. * Init the UART / SPI switch. This can be called before relocation so we must
  54. * not access BSS.
  55. */
  56. void gpio_early_init_uart(void)
  57. {
  58. struct spi_uart config;
  59. get_config(&config);
  60. if (config.gpio != -1) {
  61. /* Cannot provide a label prior to relocation */
  62. gpio_request(config.gpio, NULL);
  63. gpio_direction_output(config.gpio, 0);
  64. }
  65. }
  66. /*
  67. * Configure the UART / SPI switch.
  68. */
  69. void gpio_config_uart(void)
  70. {
  71. get_config(&local);
  72. if (local.gpio != -1) {
  73. gpio_direction_output(local.gpio, 0);
  74. switch_pos = SWITCH_UART;
  75. } else {
  76. /*
  77. * If we're here we don't have a SPI switch; go ahead and
  78. * enable the SPI now. We didn't in spi_init() so we wouldn't
  79. * kill the UART.
  80. */
  81. pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
  82. switch_pos = SWITCH_BOTH;
  83. }
  84. }
  85. static void spi_uart_switch(struct spi_uart *config,
  86. enum spi_uart_switch new_pos)
  87. {
  88. if (switch_pos == SWITCH_BOTH || new_pos == switch_pos)
  89. return;
  90. /* pre-delay, allow SPI/UART to settle, FIFO to empty, etc. */
  91. udelay(CONFIG_SPI_CORRUPTS_UART_DLY);
  92. /* We need to dynamically change the pinmux, shared w/UART RXD/CTS */
  93. pinmux_set_func(PINGRP_GMC, new_pos == SWITCH_SPI ?
  94. PMUX_FUNC_SFLASH : PMUX_FUNC_UARTD);
  95. /*
  96. * On Seaboard, MOSI/MISO are shared w/UART.
  97. * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity.
  98. * Enable UART later (cs_deactivate) so we can use it for U-Boot comms.
  99. */
  100. gpio_direction_output(config->gpio, new_pos == SWITCH_SPI);
  101. switch_pos = new_pos;
  102. }
  103. void pinmux_select_uart(void)
  104. {
  105. spi_uart_switch(&local, SWITCH_UART);
  106. }
  107. void pinmux_select_spi(void)
  108. {
  109. spi_uart_switch(&local, SWITCH_SPI);
  110. }