uart-spi-switch.c 3.2 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/tegra20.h>
  27. #include <asm/arch/tegra_spi.h>
  28. /* position of the UART/SPI select switch */
  29. enum spi_uart_switch {
  30. SWITCH_UNKNOWN,
  31. SWITCH_SPI,
  32. SWITCH_UART,
  33. SWITCH_BOTH
  34. };
  35. /* Information about the spi/uart switch */
  36. struct spi_uart {
  37. int gpio; /* GPIO to control switch */
  38. u32 port; /* Port number of UART affected */
  39. };
  40. static struct spi_uart local;
  41. static enum spi_uart_switch switch_pos; /* Current switch position */
  42. static void get_config(struct spi_uart *config)
  43. {
  44. #if defined CONFIG_SPI_CORRUPTS_UART
  45. config->gpio = CONFIG_UART_DISABLE_GPIO;
  46. config->port = CONFIG_SPI_CORRUPTS_UART_NR;
  47. #else
  48. config->gpio = -1;
  49. #endif
  50. }
  51. /*
  52. * Init the UART / SPI switch. This can be called before relocation so we must
  53. * not access BSS.
  54. */
  55. void gpio_early_init_uart(void)
  56. {
  57. struct spi_uart config;
  58. get_config(&config);
  59. if (config.gpio != -1) {
  60. /* Cannot provide a label prior to relocation */
  61. gpio_request(config.gpio, NULL);
  62. gpio_direction_output(config.gpio, 0);
  63. }
  64. }
  65. /*
  66. * Configure the UART / SPI switch.
  67. */
  68. void gpio_config_uart(void)
  69. {
  70. get_config(&local);
  71. if (local.gpio != -1) {
  72. gpio_direction_output(local.gpio, 0);
  73. switch_pos = SWITCH_UART;
  74. } else {
  75. /*
  76. * If we're here we don't have a SPI switch; go ahead and
  77. * enable the SPI now. We didn't in spi_init() so we wouldn't
  78. * kill the UART.
  79. */
  80. pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
  81. switch_pos = SWITCH_BOTH;
  82. }
  83. }
  84. static void spi_uart_switch(struct spi_uart *config,
  85. enum spi_uart_switch new_pos)
  86. {
  87. if (switch_pos == SWITCH_BOTH || new_pos == switch_pos)
  88. return;
  89. /* pre-delay, allow SPI/UART to settle, FIFO to empty, etc. */
  90. udelay(CONFIG_SPI_CORRUPTS_UART_DLY);
  91. /* We need to dynamically change the pinmux, shared w/UART RXD/CTS */
  92. pinmux_set_func(PINGRP_GMC, new_pos == SWITCH_SPI ?
  93. PMUX_FUNC_SFLASH : PMUX_FUNC_UARTD);
  94. /*
  95. * On Seaboard, MOSI/MISO are shared w/UART.
  96. * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity.
  97. * Enable UART later (cs_deactivate) so we can use it for U-Boot comms.
  98. */
  99. gpio_direction_output(config->gpio, new_pos == SWITCH_SPI);
  100. switch_pos = new_pos;
  101. }
  102. void pinmux_select_uart(void)
  103. {
  104. spi_uart_switch(&local, SWITCH_UART);
  105. }
  106. void pinmux_select_spi(void)
  107. {
  108. spi_uart_switch(&local, SWITCH_SPI);
  109. }