Simon Glass | cb93481 | 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> |
| 24 | #include <ns16550.h> |
| 25 | #include <asm/gpio.h> |
| 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> |
| 30 | |
| 31 | |
| 32 | /* position of the UART/SPI select switch */ |
| 33 | enum spi_uart_switch { |
| 34 | SWITCH_UNKNOWN, |
| 35 | SWITCH_SPI, |
| 36 | SWITCH_UART, |
| 37 | SWITCH_BOTH |
| 38 | }; |
| 39 | |
| 40 | /* Information about the spi/uart switch */ |
| 41 | struct spi_uart { |
| 42 | int gpio; /* GPIO to control switch */ |
| 43 | NS16550_t regs; /* Address of UART affected */ |
| 44 | u32 port; /* Port number of UART affected */ |
| 45 | }; |
| 46 | |
| 47 | static struct spi_uart local; |
| 48 | static enum spi_uart_switch switch_pos; /* Current switch position */ |
| 49 | |
| 50 | |
| 51 | static void get_config(struct spi_uart *config) |
| 52 | { |
| 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; |
| 57 | #else |
| 58 | config->gpio = -1; |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Init the UART / SPI switch. This can be called before relocation so we must |
| 64 | * not access BSS. |
| 65 | */ |
| 66 | void gpio_early_init_uart(void) |
| 67 | { |
| 68 | struct spi_uart config; |
| 69 | |
| 70 | get_config(&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); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Configure the UART / SPI switch. |
| 80 | */ |
| 81 | void gpio_config_uart(void) |
| 82 | { |
| 83 | get_config(&local); |
| 84 | if (local.gpio != -1) { |
| 85 | gpio_direction_output(local.gpio, 0); |
| 86 | switch_pos = SWITCH_UART; |
| 87 | } else { |
| 88 | /* |
| 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 |
| 91 | * kill the UART. |
| 92 | */ |
| 93 | pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); |
| 94 | switch_pos = SWITCH_BOTH; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void spi_uart_switch(struct spi_uart *config, |
| 99 | enum spi_uart_switch new_pos) |
| 100 | { |
| 101 | if (switch_pos == SWITCH_BOTH || new_pos == switch_pos) |
| 102 | return; |
| 103 | |
| 104 | /* if the UART was selected, allow it to drain */ |
| 105 | if (switch_pos == SWITCH_UART) |
| 106 | NS16550_drain(config->regs, config->port); |
| 107 | |
| 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); |
| 111 | |
| 112 | /* |
| 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. |
| 116 | */ |
| 117 | gpio_direction_output(config->gpio, new_pos == SWITCH_SPI); |
| 118 | switch_pos = new_pos; |
| 119 | |
| 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? */ |
| 123 | udelay(100); |
| 124 | NS16550_clear(config->regs, config->port); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void pinmux_select_uart(NS16550_t regs) |
| 129 | { |
| 130 | /* Also prevents calling spi_uart_switch() before relocation */ |
| 131 | if (regs == local.regs) |
| 132 | spi_uart_switch(&local, SWITCH_UART); |
| 133 | } |
| 134 | |
| 135 | void pinmux_select_spi(void) |
| 136 | { |
| 137 | spi_uart_switch(&local, SWITCH_SPI); |
| 138 | } |