Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 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 | |
| 23 | #include <common.h> |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 24 | #include <asm/gpio.h> |
| 25 | #include <asm/arch/pinmux.h> |
| 26 | #include <asm/arch/uart-spi-switch.h> |
Tom Warren | ab37196 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 27 | #include <asm/arch/tegra.h> |
| 28 | #include <asm/arch-tegra/tegra_spi.h> |
Lucas Stach | e80f7ca | 2012-09-29 10:02:08 +0000 | [diff] [blame] | 29 | #include <asm/arch-tegra/board.h> |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 30 | |
| 31 | /* position of the UART/SPI select switch */ |
| 32 | enum spi_uart_switch { |
| 33 | SWITCH_UNKNOWN, |
| 34 | SWITCH_SPI, |
| 35 | SWITCH_UART, |
| 36 | SWITCH_BOTH |
| 37 | }; |
| 38 | |
| 39 | /* Information about the spi/uart switch */ |
| 40 | struct spi_uart { |
| 41 | int gpio; /* GPIO to control switch */ |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 42 | u32 port; /* Port number of UART affected */ |
| 43 | }; |
| 44 | |
| 45 | static struct spi_uart local; |
| 46 | static enum spi_uart_switch switch_pos; /* Current switch position */ |
| 47 | |
| 48 | |
| 49 | static void get_config(struct spi_uart *config) |
| 50 | { |
| 51 | #if defined CONFIG_SPI_CORRUPTS_UART |
| 52 | config->gpio = CONFIG_UART_DISABLE_GPIO; |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 53 | 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 | */ |
| 63 | void 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 | */ |
| 78 | void 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 | |
| 95 | static 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 Warren | c309b46 | 2012-05-15 14:32:40 -0700 | [diff] [blame] | 101 | /* pre-delay, allow SPI/UART to settle, FIFO to empty, etc. */ |
| 102 | udelay(CONFIG_SPI_CORRUPTS_UART_DLY); |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 103 | |
| 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 Warren | c309b46 | 2012-05-15 14:32:40 -0700 | [diff] [blame] | 109 | * 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 Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 113 | gpio_direction_output(config->gpio, new_pos == SWITCH_SPI); |
| 114 | switch_pos = new_pos; |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Tom Warren | c309b46 | 2012-05-15 14:32:40 -0700 | [diff] [blame] | 117 | void pinmux_select_uart(void) |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 118 | { |
Simon Glass | 92d8f1f | 2011-11-05 04:46:49 +0000 | [diff] [blame] | 119 | spi_uart_switch(&local, SWITCH_UART); |
| 120 | } |
| 121 | |
| 122 | void pinmux_select_spi(void) |
| 123 | { |
| 124 | spi_uart_switch(&local, SWITCH_SPI); |
| 125 | } |