uart-spi-switch.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <ns16550.h>
  24. #include <asm/gpio.h>
  25. #include <asm/arch/pinmux.h>
  26. #include <asm/arch/uart-spi-switch.h>
  27. #include <asm/arch/tegra2.h>
  28. #include <asm/arch/tegra2_spi.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. NS16550_t regs; /* Address of UART affected */
  40. u32 port; /* Port number of UART affected */
  41. };
  42. static struct spi_uart local;
  43. static enum spi_uart_switch switch_pos; /* Current switch position */
  44. static void get_config(struct spi_uart *config)
  45. {
  46. #if defined CONFIG_SPI_CORRUPTS_UART
  47. config->gpio = CONFIG_UART_DISABLE_GPIO;
  48. config->regs = (NS16550_t)CONFIG_SPI_CORRUPTS_UART;
  49. config->port = CONFIG_SPI_CORRUPTS_UART_NR;
  50. #else
  51. config->gpio = -1;
  52. #endif
  53. }
  54. /*
  55. * Init the UART / SPI switch. This can be called before relocation so we must
  56. * not access BSS.
  57. */
  58. void gpio_early_init_uart(void)
  59. {
  60. struct spi_uart config;
  61. get_config(&config);
  62. if (config.gpio != -1) {
  63. /* Cannot provide a label prior to relocation */
  64. gpio_request(config.gpio, NULL);
  65. gpio_direction_output(config.gpio, 0);
  66. }
  67. }
  68. /*
  69. * Configure the UART / SPI switch.
  70. */
  71. void gpio_config_uart(void)
  72. {
  73. get_config(&local);
  74. if (local.gpio != -1) {
  75. gpio_direction_output(local.gpio, 0);
  76. switch_pos = SWITCH_UART;
  77. } else {
  78. /*
  79. * If we're here we don't have a SPI switch; go ahead and
  80. * enable the SPI now. We didn't in spi_init() so we wouldn't
  81. * kill the UART.
  82. */
  83. pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
  84. switch_pos = SWITCH_BOTH;
  85. }
  86. }
  87. static void spi_uart_switch(struct spi_uart *config,
  88. enum spi_uart_switch new_pos)
  89. {
  90. if (switch_pos == SWITCH_BOTH || new_pos == switch_pos)
  91. return;
  92. /* if the UART was selected, allow it to drain */
  93. if (switch_pos == SWITCH_UART)
  94. NS16550_drain(config->regs, config->port);
  95. /* We need to dynamically change the pinmux, shared w/UART RXD/CTS */
  96. pinmux_set_func(PINGRP_GMC, new_pos == SWITCH_SPI ?
  97. PMUX_FUNC_SFLASH : PMUX_FUNC_UARTD);
  98. /*
  99. * On Seaboard, MOSI/MISO are shared w/UART.
  100. * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity.
  101. * Enable UART later (cs_deactivate) so we can use it for U-Boot comms.
  102. */
  103. gpio_direction_output(config->gpio, new_pos == SWITCH_SPI);
  104. switch_pos = new_pos;
  105. /* if the SPI was selected, clear any junk bytes in the UART */
  106. if (switch_pos == SWITCH_UART) {
  107. /* TODO: What if it is part-way through clocking in junk? */
  108. udelay(100);
  109. NS16550_clear(config->regs, config->port);
  110. }
  111. }
  112. void pinmux_select_uart(NS16550_t regs)
  113. {
  114. /* Also prevents calling spi_uart_switch() before relocation */
  115. if (regs == local.regs)
  116. spi_uart_switch(&local, SWITCH_UART);
  117. }
  118. void pinmux_select_spi(void)
  119. {
  120. spi_uart_switch(&local, SWITCH_SPI);
  121. }