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