Figo Wang | 42cfd0b | 2014-03-31 15:50:37 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, 2014, The Linux Foundation. All rights reserved. |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 2 | * |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <debug.h> |
Figo Wang | 42cfd0b | 2014-03-31 15:50:37 +0800 | [diff] [blame] | 32 | #include <kernel/thread.h> |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 33 | #include <reg.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <platform/iomap.h> |
| 36 | #include <platform/irqs.h> |
| 37 | #include <platform/interrupts.h> |
| 38 | #include <platform/clock.h> |
| 39 | #include <platform/gpio.h> |
| 40 | #include <uart_dm.h> |
| 41 | #include <gsbi.h> |
| 42 | |
| 43 | #ifndef NULL |
| 44 | #define NULL 0 |
| 45 | #endif |
| 46 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 47 | |
| 48 | static int uart_init_flag = 0; |
| 49 | |
| 50 | /* Note: |
| 51 | * This is a basic implementation of UART_DM protocol. More focus has been |
| 52 | * given on simplicity than efficiency. Few of the things to be noted are: |
| 53 | * - RX path may not be suitable for multi-threaded scenaraio because of the |
| 54 | * use of static variables. TX path shouldn't have any problem though. If |
| 55 | * multi-threaded support is required, a simple data-structure can |
| 56 | * be maintained for each thread. |
| 57 | * - Right now we are using polling method than interrupt based. |
| 58 | * - We are using legacy UART protocol without Data Mover. |
| 59 | * - Not all interrupts and error events are handled. |
| 60 | * - While waiting Watchdog hasn't been taken into consideration. |
| 61 | */ |
| 62 | |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 63 | #define NON_PRINTABLE_ASCII_CHAR 128 |
| 64 | |
| 65 | static uint8_t pack_chars_into_words(uint8_t *buffer, uint8_t cnt, uint32_t *word) |
| 66 | { |
| 67 | uint8_t num_chars_writtten = 0; |
| 68 | |
| 69 | *word = 0; |
| 70 | |
| 71 | for(int j=0; j < cnt; j++) |
| 72 | { |
| 73 | if (buffer[num_chars_writtten] == '\n') |
| 74 | { |
| 75 | /* replace '\n' by the NON_PRINTABLE_ASCII_CHAR and print '\r'. |
| 76 | * While printing the NON_PRINTABLE_ASCII_CHAR, we will print '\n'. |
| 77 | * Thus successfully replacing '\n' by '\r' '\n'. |
| 78 | */ |
| 79 | *word |= ('\r' & 0xff) << (j * 8); |
| 80 | buffer[num_chars_writtten] = NON_PRINTABLE_ASCII_CHAR; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | if (buffer[num_chars_writtten] == NON_PRINTABLE_ASCII_CHAR) |
| 85 | { |
| 86 | buffer[num_chars_writtten] = '\n'; |
| 87 | } |
| 88 | |
| 89 | *word |= (buffer[num_chars_writtten] & 0xff) << (j * 8); |
| 90 | |
| 91 | num_chars_writtten++; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return num_chars_writtten; |
| 96 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 97 | |
| 98 | /* Static Function Prototype Declarations */ |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 99 | static unsigned int msm_boot_uart_calculate_num_chars_to_write(char *data_in, |
| 100 | uint32_t *num_of_chars); |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 101 | static unsigned int msm_boot_uart_dm_init(uint32_t base); |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 102 | static unsigned int msm_boot_uart_dm_read(uint32_t base, |
| 103 | unsigned int *data, int wait); |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 104 | static unsigned int msm_boot_uart_dm_write(uint32_t base, char *data, |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 105 | unsigned int num_of_chars); |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 106 | static unsigned int msm_boot_uart_dm_init_rx_transfer(uint32_t base); |
| 107 | static unsigned int msm_boot_uart_dm_reset(uint32_t base); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 108 | |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 109 | /* Keep track of uart block vs port mapping. |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 110 | */ |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 111 | static uint32_t port_lookup[4]; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 112 | |
| 113 | /* Extern functions */ |
| 114 | void udelay(unsigned usecs); |
| 115 | |
| 116 | /* |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 117 | * Helper function to keep track of Line Feed char "\n" with |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 118 | * Carriage Return "\r\n". |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 119 | */ |
| 120 | static unsigned int |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 121 | msm_boot_uart_calculate_num_chars_to_write(char *data_in, |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 122 | uint32_t *num_of_chars) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 123 | { |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 124 | uint32_t i = 0, j = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 125 | |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 126 | if ((data_in == NULL)) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 127 | return MSM_BOOT_UART_DM_E_INVAL; |
| 128 | } |
| 129 | |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 130 | for (i = 0, j = 0; i < *num_of_chars; i++, j++) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 131 | if (data_in[i] == '\n') { |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 132 | j++; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 137 | *num_of_chars = j; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 138 | |
| 139 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | /* |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 143 | * Reset the UART |
| 144 | */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 145 | static unsigned int msm_boot_uart_dm_reset(uint32_t base) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 146 | { |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 147 | writel(MSM_BOOT_UART_DM_CMD_RESET_RX, MSM_BOOT_UART_DM_CR(base)); |
| 148 | writel(MSM_BOOT_UART_DM_CMD_RESET_TX, MSM_BOOT_UART_DM_CR(base)); |
| 149 | writel(MSM_BOOT_UART_DM_CMD_RESET_ERR_STAT, MSM_BOOT_UART_DM_CR(base)); |
| 150 | writel(MSM_BOOT_UART_DM_CMD_RES_TX_ERR, MSM_BOOT_UART_DM_CR(base)); |
| 151 | writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 152 | |
| 153 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Initialize UART_DM - configure clock and required registers. |
| 158 | */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 159 | static unsigned int msm_boot_uart_dm_init(uint32_t uart_dm_base) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 160 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 161 | /* Configure UART mode registers MR1 and MR2 */ |
| 162 | /* Hardware flow control isn't supported */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 163 | writel(0x0, MSM_BOOT_UART_DM_MR1(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 164 | |
| 165 | /* 8-N-1 configuration: 8 data bits - No parity - 1 stop bit */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 166 | writel(MSM_BOOT_UART_DM_8_N_1_MODE, MSM_BOOT_UART_DM_MR2(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 167 | |
| 168 | /* Configure Interrupt Mask register IMR */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 169 | writel(MSM_BOOT_UART_DM_IMR_ENABLED, MSM_BOOT_UART_DM_IMR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 170 | |
| 171 | /* Configure Tx and Rx watermarks configuration registers */ |
| 172 | /* TX watermark value is set to 0 - interrupt is generated when |
| 173 | * FIFO level is less than or equal to 0 */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 174 | writel(MSM_BOOT_UART_DM_TFW_VALUE, MSM_BOOT_UART_DM_TFWR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 175 | |
| 176 | /* RX watermark value */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 177 | writel(MSM_BOOT_UART_DM_RFW_VALUE, MSM_BOOT_UART_DM_RFWR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 178 | |
| 179 | /* Configure Interrupt Programming Register */ |
| 180 | /* Set initial Stale timeout value */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 181 | writel(MSM_BOOT_UART_DM_STALE_TIMEOUT_LSB, MSM_BOOT_UART_DM_IPR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 182 | |
| 183 | /* Configure IRDA if required */ |
| 184 | /* Disabling IRDA mode */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 185 | writel(0x0, MSM_BOOT_UART_DM_IRDA(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 186 | |
| 187 | /* Configure and enable sim interface if required */ |
| 188 | |
| 189 | /* Configure hunt character value in HCR register */ |
| 190 | /* Keep it in reset state */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 191 | writel(0x0, MSM_BOOT_UART_DM_HCR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 192 | |
| 193 | /* Configure Rx FIFO base address */ |
| 194 | /* Both TX/RX shares same SRAM and default is half-n-half. |
| 195 | * Sticking with default value now. |
| 196 | * As such RAM size is (2^RAM_ADDR_WIDTH, 32-bit entries). |
| 197 | * We have found RAM_ADDR_WIDTH = 0x7f */ |
| 198 | |
| 199 | /* Issue soft reset command */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 200 | msm_boot_uart_dm_reset(uart_dm_base); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 201 | |
| 202 | /* Enable/Disable Rx/Tx DM interfaces */ |
| 203 | /* Data Mover not currently utilized. */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 204 | writel(0x0, MSM_BOOT_UART_DM_DMEN(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 205 | |
| 206 | /* Enable transmitter and receiver */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 207 | writel(MSM_BOOT_UART_DM_CR_RX_ENABLE, MSM_BOOT_UART_DM_CR(uart_dm_base)); |
| 208 | writel(MSM_BOOT_UART_DM_CR_TX_ENABLE, MSM_BOOT_UART_DM_CR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 209 | |
| 210 | /* Initialize Receive Path */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 211 | msm_boot_uart_dm_init_rx_transfer(uart_dm_base); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 212 | |
| 213 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Initialize Receive Path |
| 218 | */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 219 | static unsigned int msm_boot_uart_dm_init_rx_transfer(uint32_t uart_dm_base) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 220 | { |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 221 | writel(MSM_BOOT_UART_DM_GCMD_DIS_STALE_EVT, MSM_BOOT_UART_DM_CR(uart_dm_base)); |
| 222 | writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(uart_dm_base)); |
| 223 | writel(MSM_BOOT_UART_DM_DMRX_DEF_VALUE, MSM_BOOT_UART_DM_DMRX(uart_dm_base)); |
| 224 | writel(MSM_BOOT_UART_DM_GCMD_ENA_STALE_EVT, MSM_BOOT_UART_DM_CR(uart_dm_base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 225 | |
| 226 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * UART Receive operation |
| 231 | * Reads a word from the RX FIFO. |
| 232 | */ |
| 233 | static unsigned int |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 234 | msm_boot_uart_dm_read(uint32_t base, unsigned int *data, int wait) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 235 | { |
| 236 | static int rx_last_snap_count = 0; |
| 237 | static int rx_chars_read_since_last_xfer = 0; |
| 238 | |
| 239 | if (data == NULL) { |
| 240 | return MSM_BOOT_UART_DM_E_INVAL; |
| 241 | } |
| 242 | |
| 243 | /* We will be polling RXRDY status bit */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 244 | while (!(readl(MSM_BOOT_UART_DM_SR(base)) & MSM_BOOT_UART_DM_SR_RXRDY)) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 245 | /* if this is not a blocking call, we'll just return */ |
| 246 | if (!wait) { |
| 247 | return MSM_BOOT_UART_DM_E_RX_NOT_READY; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* Check for Overrun error. We'll just reset Error Status */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 252 | if (readl(MSM_BOOT_UART_DM_SR(base)) & MSM_BOOT_UART_DM_SR_UART_OVERRUN) { |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 253 | writel(MSM_BOOT_UART_DM_CMD_RESET_ERR_STAT, MSM_BOOT_UART_DM_CR(base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* RX FIFO is ready; read a word. */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 257 | *data = readl(MSM_BOOT_UART_DM_RF(base, 0)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 258 | |
| 259 | /* increment the total count of chars we've read so far */ |
| 260 | rx_chars_read_since_last_xfer += 4; |
| 261 | |
| 262 | /* Rx transfer ends when one of the conditions is met: |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 263 | * - The number of characters received since the end of the previous |
| 264 | * xfer equals the value written to DMRX at Transfer Initialization |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 265 | * - A stale event occurred |
| 266 | */ |
| 267 | |
| 268 | /* If RX transfer has not ended yet */ |
| 269 | if (rx_last_snap_count == 0) { |
| 270 | /* Check if we've received stale event */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 271 | if (readl(MSM_BOOT_UART_DM_MISR(base)) & MSM_BOOT_UART_DM_RXSTALE) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 272 | /* Send command to reset stale interrupt */ |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 273 | writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* Check if we haven't read more than DMRX value */ |
| 277 | else if ((unsigned int)rx_chars_read_since_last_xfer < |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 278 | readl(MSM_BOOT_UART_DM_DMRX(base))) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 279 | /* We can still continue reading before initializing RX transfer */ |
| 280 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 281 | } |
| 282 | |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 283 | /* If we've reached here it means RX |
| 284 | * xfer end conditions been met |
| 285 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 286 | |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 287 | /* Read UART_DM_RX_TOTAL_SNAP register |
| 288 | * to know how many valid chars |
| 289 | * we've read so far since last transfer |
| 290 | */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 291 | rx_last_snap_count = readl(MSM_BOOT_UART_DM_RX_TOTAL_SNAP(base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 292 | |
| 293 | } |
| 294 | |
| 295 | /* If there are still data left in FIFO we'll read them before |
| 296 | * initializing RX Transfer again */ |
| 297 | if ((rx_last_snap_count - rx_chars_read_since_last_xfer) >= 0) { |
| 298 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 299 | } |
| 300 | |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 301 | msm_boot_uart_dm_init_rx_transfer(base); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 302 | rx_last_snap_count = 0; |
| 303 | rx_chars_read_since_last_xfer = 0; |
| 304 | |
| 305 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * UART transmit operation |
| 310 | */ |
| 311 | static unsigned int |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 312 | msm_boot_uart_dm_write(uint32_t base, char *data, unsigned int num_of_chars) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 313 | { |
| 314 | unsigned int tx_word_count = 0; |
| 315 | unsigned int tx_char_left = 0, tx_char = 0; |
| 316 | unsigned int tx_word = 0; |
| 317 | int i = 0; |
| 318 | char *tx_data = NULL; |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 319 | uint8_t num_chars_written; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 320 | |
| 321 | if ((data == NULL) || (num_of_chars <= 0)) { |
| 322 | return MSM_BOOT_UART_DM_E_INVAL; |
| 323 | } |
| 324 | |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 325 | msm_boot_uart_calculate_num_chars_to_write(data, &num_of_chars); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 326 | |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 327 | tx_data = data; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 328 | |
| 329 | /* Write to NO_CHARS_FOR_TX register number of characters |
| 330 | * to be transmitted. However, before writing TX_FIFO must |
| 331 | * be empty as indicated by TX_READY interrupt in IMR register |
| 332 | */ |
| 333 | |
| 334 | /* Check if transmit FIFO is empty. |
| 335 | * If not we'll wait for TX_READY interrupt. */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 336 | if (!(readl(MSM_BOOT_UART_DM_SR(base)) & MSM_BOOT_UART_DM_SR_TXEMT)) { |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 337 | while (!(readl(MSM_BOOT_UART_DM_ISR(base)) & MSM_BOOT_UART_DM_TX_READY)) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 338 | udelay(1); |
| 339 | /* Kick watchdog? */ |
| 340 | } |
| 341 | } |
| 342 | |
Figo Wang | 42cfd0b | 2014-03-31 15:50:37 +0800 | [diff] [blame] | 343 | //We need to make sure the DM_NO_CHARS_FOR_TX&DM_TF are are programmed atmoically. |
| 344 | enter_critical_section(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 345 | /* We are here. FIFO is ready to be written. */ |
| 346 | /* Write number of characters to be written */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 347 | writel(num_of_chars, MSM_BOOT_UART_DM_NO_CHARS_FOR_TX(base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 348 | |
| 349 | /* Clear TX_READY interrupt */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 350 | writel(MSM_BOOT_UART_DM_GCMD_RES_TX_RDY_INT, MSM_BOOT_UART_DM_CR(base)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 351 | |
| 352 | /* We use four-character word FIFO. So we need to divide data into |
| 353 | * four characters and write in UART_DM_TF register */ |
| 354 | tx_word_count = (num_of_chars % 4) ? ((num_of_chars / 4) + 1) : |
| 355 | (num_of_chars / 4); |
| 356 | tx_char_left = num_of_chars; |
| 357 | |
| 358 | for (i = 0; i < (int)tx_word_count; i++) { |
| 359 | tx_char = (tx_char_left < 4) ? tx_char_left : 4; |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 360 | num_chars_written = pack_chars_into_words((uint8_t *)tx_data, tx_char, &tx_word); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 361 | |
| 362 | /* Wait till TX FIFO has space */ |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 363 | while (!(readl(MSM_BOOT_UART_DM_SR(base)) & MSM_BOOT_UART_DM_SR_TXRDY)) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 364 | udelay(1); |
| 365 | } |
| 366 | |
| 367 | /* TX FIFO has space. Write the chars */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 368 | writel(tx_word, MSM_BOOT_UART_DM_TF(base, 0)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 369 | tx_char_left = num_of_chars - (i + 1) * 4; |
Deepa Dinamani | 9965d1e | 2012-12-14 13:56:13 -0800 | [diff] [blame] | 370 | tx_data = tx_data + num_chars_written; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 371 | } |
Figo Wang | 42cfd0b | 2014-03-31 15:50:37 +0800 | [diff] [blame] | 372 | exit_critical_section(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 373 | |
| 374 | return MSM_BOOT_UART_DM_E_SUCCESS; |
| 375 | } |
| 376 | |
| 377 | /* Defining functions that's exposed to outside world and in coformance to |
| 378 | * existing uart implemention. These functions are being called to initialize |
| 379 | * UART and print debug messages in bootloader. |
| 380 | */ |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 381 | void uart_dm_init(uint8_t id, uint32_t gsbi_base, uint32_t uart_dm_base) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 382 | { |
| 383 | static uint8_t port = 0; |
| 384 | char *data = "Android Bootloader - UART_DM Initialized!!!\n"; |
| 385 | |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 386 | /* Configure the uart clock */ |
| 387 | clock_config_uart_dm(id); |
| 388 | dsb(); |
| 389 | |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 390 | /* Configure GPIO to provide connectivity between UART block |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 391 | product ports and chip pads */ |
| 392 | gpio_config_uart_dm(id); |
| 393 | dsb(); |
| 394 | |
| 395 | /* Configure GSBI for UART_DM protocol. |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 396 | * I2C on 2 ports, UART (without HS flow control) on the other 2. |
| 397 | * This is only on chips that have GSBI block |
| 398 | */ |
| 399 | if(gsbi_base) |
| 400 | writel(GSBI_PROTOCOL_CODE_I2C_UART << |
| 401 | GSBI_CTRL_REG_PROTOCOL_CODE_S, |
| 402 | GSBI_CTRL_REG(gsbi_base)); |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 403 | dsb(); |
| 404 | |
| 405 | /* Configure clock selection register for tx and rx rates. |
| 406 | * Selecting 115.2k for both RX and TX. |
| 407 | */ |
| 408 | writel(UART_DM_CLK_RX_TX_BIT_RATE, MSM_BOOT_UART_DM_CSR(uart_dm_base)); |
| 409 | dsb(); |
| 410 | |
| 411 | /* Intialize UART_DM */ |
| 412 | msm_boot_uart_dm_init(uart_dm_base); |
| 413 | |
| 414 | msm_boot_uart_dm_write(uart_dm_base, data, 44); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 415 | |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 416 | ASSERT(port < ARRAY_SIZE(port_lookup)); |
| 417 | port_lookup[port++] = uart_dm_base; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 418 | |
| 419 | /* Set UART init flag */ |
| 420 | uart_init_flag = 1; |
| 421 | } |
| 422 | |
| 423 | /* UART_DM uses four character word FIFO where as UART core |
| 424 | * uses a character FIFO. so it's really inefficient to try |
| 425 | * to write single character. But that's how dprintf has been |
| 426 | * implemented. |
| 427 | */ |
| 428 | int uart_putc(int port, char c) |
| 429 | { |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 430 | uint32_t uart_base = port_lookup[port]; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 431 | |
| 432 | /* Don't do anything if UART is not initialized */ |
| 433 | if (!uart_init_flag) |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 434 | return -1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 435 | |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 436 | msm_boot_uart_dm_write(uart_base, &c, 1); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* UART_DM uses four character word FIFO whereas uart_getc |
| 442 | * is supposed to read only one character. So we need to |
| 443 | * read a word and keep track of each character in the word. |
| 444 | */ |
| 445 | int uart_getc(int port, bool wait) |
| 446 | { |
| 447 | int byte; |
| 448 | static unsigned int word = 0; |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 449 | uint32_t uart_base = port_lookup[port]; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 450 | |
| 451 | /* Don't do anything if UART is not initialized */ |
| 452 | if (!uart_init_flag) |
Amol Jadi | a63aaff | 2012-02-01 15:51:50 -0800 | [diff] [blame] | 453 | return -1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 454 | |
| 455 | if (!word) { |
| 456 | /* Read from FIFO only if it's a first read or all the four |
| 457 | * characters out of a word have been read */ |
Deepa Dinamani | bce9d9e | 2012-05-17 13:53:17 -0700 | [diff] [blame] | 458 | if (msm_boot_uart_dm_read(uart_base, &word, wait) != MSM_BOOT_UART_DM_E_SUCCESS) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | } |
| 463 | |
| 464 | byte = (int)word & 0xff; |
| 465 | word = word >> 8; |
| 466 | |
| 467 | return byte; |
| 468 | } |