2 * Copyright (c) 2011 The Chromium OS Authors.
4 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <asm/arch/pinmux.h>
27 #include <asm/arch/uart-spi-switch.h>
28 #include <asm/arch/tegra2.h>
29 #include <asm/arch/tegra2_spi.h>
32 /* position of the UART/SPI select switch */
33 enum spi_uart_switch {
40 /* Information about the spi/uart switch */
42 int gpio; /* GPIO to control switch */
43 NS16550_t regs; /* Address of UART affected */
44 u32 port; /* Port number of UART affected */
47 static struct spi_uart local;
48 static enum spi_uart_switch switch_pos; /* Current switch position */
51 static void get_config(struct spi_uart *config)
53 #if defined CONFIG_SPI_CORRUPTS_UART
54 config->gpio = CONFIG_UART_DISABLE_GPIO;
55 config->regs = (NS16550_t)CONFIG_SPI_CORRUPTS_UART;
56 config->port = CONFIG_SPI_CORRUPTS_UART_NR;
63 * Init the UART / SPI switch. This can be called before relocation so we must
66 void gpio_early_init_uart(void)
68 struct spi_uart config;
71 if (config.gpio != -1) {
72 /* Cannot provide a label prior to relocation */
73 gpio_request(config.gpio, NULL);
74 gpio_direction_output(config.gpio, 0);
79 * Configure the UART / SPI switch.
81 void gpio_config_uart(void)
84 if (local.gpio != -1) {
85 gpio_direction_output(local.gpio, 0);
86 switch_pos = SWITCH_UART;
89 * If we're here we don't have a SPI switch; go ahead and
90 * enable the SPI now. We didn't in spi_init() so we wouldn't
93 pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
94 switch_pos = SWITCH_BOTH;
98 static void spi_uart_switch(struct spi_uart *config,
99 enum spi_uart_switch new_pos)
101 if (switch_pos == SWITCH_BOTH || new_pos == switch_pos)
104 /* if the UART was selected, allow it to drain */
105 if (switch_pos == SWITCH_UART)
106 NS16550_drain(config->regs, config->port);
108 /* We need to dynamically change the pinmux, shared w/UART RXD/CTS */
109 pinmux_set_func(PINGRP_GMC, new_pos == SWITCH_SPI ?
110 PMUX_FUNC_SFLASH : PMUX_FUNC_UARTD);
113 * On Seaboard, MOSI/MISO are shared w/UART.
114 * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity.
115 * Enable UART later (cs_deactivate) so we can use it for U-Boot comms.
117 gpio_direction_output(config->gpio, new_pos == SWITCH_SPI);
118 switch_pos = new_pos;
120 /* if the SPI was selected, clear any junk bytes in the UART */
121 if (switch_pos == SWITCH_UART) {
122 /* TODO: What if it is part-way through clocking in junk? */
124 NS16550_clear(config->regs, config->port);
128 void pinmux_select_uart(NS16550_t regs)
130 /* Also prevents calling spi_uart_switch() before relocation */
131 if (regs == local.regs)
132 spi_uart_switch(&local, SWITCH_UART);
135 void pinmux_select_spi(void)
137 spi_uart_switch(&local, SWITCH_SPI);