blob: 3ac657fd79f658470e7ff6160c30cea123d81b48 [file] [log] [blame]
Bikas Gurungd1aa5902010-10-01 23:45:33 -07001/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
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 Code Aurora Forum, Inc. 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.
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>
32#include <reg.h>
Amol Jadic52c8a32011-07-12 11:27:04 -070033#include <sys/types.h>
Bikas Gurungd1aa5902010-10-01 23:45:33 -070034#include <platform/iomap.h>
35#include <platform/irqs.h>
36#include <platform/interrupts.h>
Amol Jadic52c8a32011-07-12 11:27:04 -070037#include <platform/clock.h>
38#include <platform/gpio.h>
39#include <uart_dm.h>
40#include <gsbi.h>
Bikas Gurungd1aa5902010-10-01 23:45:33 -070041
Greg Griscod2471ef2011-07-14 13:00:42 -070042
Bikas Gurungd1aa5902010-10-01 23:45:33 -070043#ifndef NULL
44#define NULL 0
45#endif
46
Shashank Mittalaf406672011-11-23 19:44:52 -080047extern void dsb(void);
48
Shashank Mittal88df8052011-12-01 16:29:27 -080049static int uart_init_flag = 0;
50
Bikas Gurungd1aa5902010-10-01 23:45:33 -070051/* Note:
52 * This is a basic implementation of UART_DM protocol. More focus has been
53 * given on simplicity than efficiency. Few of the things to be noted are:
54 * - RX path may not be suitable for multi-threaded scenaraio because of the
55 * use of static variables. TX path shouldn't have any problem though. If
56 * multi-threaded support is required, a simple data-structure can
57 * be maintained for each thread.
58 * - Right now we are using polling method than interrupt based.
59 * - We are using legacy UART protocol without Data Mover.
60 * - Not all interrupts and error events are handled.
61 * - While waiting Watchdog hasn't been taken into consideration.
62 */
63
64
65#define PACK_CHARS_INTO_WORDS(a, cnt, word) { \
66 word = 0; \
67 for(int j=0; j < (int)cnt; j++) \
68 { \
69 word |= (a[j] & 0xff) \
70 << (j * 8); \
71 } \
72 }
73
74
75/* Static Function Prototype Declarations */
Amol Jadic52c8a32011-07-12 11:27:04 -070076static unsigned int msm_boot_uart_dm_gsbi_init(uint8_t id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -070077static unsigned int msm_boot_uart_replace_lr_with_cr(char* data_in,
78 int num_of_chars,
79 char *data_out,
80 int *num_of_chars_out);
Amol Jadic52c8a32011-07-12 11:27:04 -070081static unsigned int msm_boot_uart_dm_init(uint8_t id);
82static unsigned int msm_boot_uart_dm_read(uint8_t id, unsigned int* data,
Bikas Gurungd1aa5902010-10-01 23:45:33 -070083 int wait);
Amol Jadic52c8a32011-07-12 11:27:04 -070084static unsigned int msm_boot_uart_dm_write(uint8_t id, char* data,
Bikas Gurungd1aa5902010-10-01 23:45:33 -070085 unsigned int num_of_chars);
Amol Jadic52c8a32011-07-12 11:27:04 -070086static unsigned int msm_boot_uart_dm_init_rx_transfer(uint8_t id);
87static unsigned int msm_boot_uart_dm_reset(uint8_t id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -070088
Amol Jadic52c8a32011-07-12 11:27:04 -070089/* Keep track of gsbi vs port mapping.
90 */
91static uint8_t gsbi_lookup[4];
Bikas Gurungd1aa5902010-10-01 23:45:33 -070092
93/* Extern functions */
Bikas Gurungd1aa5902010-10-01 23:45:33 -070094void udelay(unsigned usecs);
95
96
97/*
98 * Helper function to replace Line Feed char "\n" with
99 * Carriage Return "\r\n".
100 * Currently keeping it simple than efficient
101 */
102static unsigned int msm_boot_uart_replace_lr_with_cr(char* data_in,
103 int num_of_chars,
104 char *data_out,
105 int *num_of_chars_out )
106{
107 int i = 0, j = 0;
108
109 if ((data_in == NULL) || (data_out == NULL) || (num_of_chars < 0))
110 {
111 return MSM_BOOT_UART_DM_E_INVAL;
112 }
113
114 for (i=0, j=0; i < num_of_chars; i++, j++)
115 {
116 if ( data_in[i] == '\n' )
117 {
118 data_out[j++] = '\r';
119 }
120
121 data_out[j] = data_in[i];
122 }
123
124 *num_of_chars_out = j;
125
126 return MSM_BOOT_UART_DM_E_SUCCESS;
127}
128
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700129/*
130 * Initialize and configure GSBI for operation
131 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700132static unsigned int msm_boot_uart_dm_gsbi_init(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700133{
Amol Jadic52c8a32011-07-12 11:27:04 -0700134 /* Configure the uart clock */
135 clock_config_uart_dm(id);
Shashank Mittalaf406672011-11-23 19:44:52 -0800136 dsb();
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700137
Amol Jadic52c8a32011-07-12 11:27:04 -0700138 /* Configure GPIO to provide connectivity between GSBI
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700139 product ports and chip pads */
Amol Jadic52c8a32011-07-12 11:27:04 -0700140 gpio_config_uart_dm(id);
Shashank Mittalaf406672011-11-23 19:44:52 -0800141 dsb();
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700142
143 /* Configure Data Mover for GSBI operation.
144 * Currently not supported. */
145
146 /* Configure GSBI for UART_DM protocol.
147 * I2C on 2 ports, UART (without HS flow control) on the other 2. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700148 writel(GSBI_PROTOCOL_CODE_I2C_UART << GSBI_CTRL_REG_PROTOCOL_CODE_S,
149 GSBI_CTRL_REG(id));
Shashank Mittalaf406672011-11-23 19:44:52 -0800150 dsb();
Amol Jadic52c8a32011-07-12 11:27:04 -0700151
Shashank Mittalaf406672011-11-23 19:44:52 -0800152 /* Configure clock selection register for tx and rx rates.
153 * Selecting 115.2k for both RX and TX.
154 */
155 writel(UART_DM_CLK_RX_TX_BIT_RATE, MSM_BOOT_UART_DM_CSR(id));
156 dsb();
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700157
158 return MSM_BOOT_UART_DM_E_SUCCESS;
159}
160
161/*
162 * Reset the UART
163 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700164static unsigned int msm_boot_uart_dm_reset(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700165{
Amol Jadic52c8a32011-07-12 11:27:04 -0700166 writel(MSM_BOOT_UART_DM_CMD_RESET_RX, MSM_BOOT_UART_DM_CR(id));
167 writel(MSM_BOOT_UART_DM_CMD_RESET_TX, MSM_BOOT_UART_DM_CR(id));
168 writel(MSM_BOOT_UART_DM_CMD_RESET_ERR_STAT, MSM_BOOT_UART_DM_CR(id));
169 writel(MSM_BOOT_UART_DM_CMD_RES_TX_ERR, MSM_BOOT_UART_DM_CR(id));
170 writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700171
172 return MSM_BOOT_UART_DM_E_SUCCESS;
173}
174
175
176/*
177 * Initialize UART_DM - configure clock and required registers.
178 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700179static unsigned int msm_boot_uart_dm_init(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700180{
Amol Jadic52c8a32011-07-12 11:27:04 -0700181 /* Configure GSBI for uart dm */
182 msm_boot_uart_dm_gsbi_init(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700183
184 /* Configure UART mode registers MR1 and MR2 */
185 /* Hardware flow control isn't supported */
Amol Jadic52c8a32011-07-12 11:27:04 -0700186 writel(0x0, MSM_BOOT_UART_DM_MR1(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700187
188 /* 8-N-1 configuration: 8 data bits - No parity - 1 stop bit */
Amol Jadic52c8a32011-07-12 11:27:04 -0700189 writel(MSM_BOOT_UART_DM_8_N_1_MODE, MSM_BOOT_UART_DM_MR2(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700190
191 /* Configure Interrupt Mask register IMR */
Amol Jadic52c8a32011-07-12 11:27:04 -0700192 writel(MSM_BOOT_UART_DM_IMR_ENABLED, MSM_BOOT_UART_DM_IMR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700193
194 /* Configure Tx and Rx watermarks configuration registers */
195 /* TX watermark value is set to 0 - interrupt is generated when
196 * FIFO level is less than or equal to 0 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700197 writel(MSM_BOOT_UART_DM_TFW_VALUE, MSM_BOOT_UART_DM_TFWR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700198
199 /* RX watermark value*/
Amol Jadic52c8a32011-07-12 11:27:04 -0700200 writel(MSM_BOOT_UART_DM_RFW_VALUE, MSM_BOOT_UART_DM_RFWR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700201
202 /* Configure Interrupt Programming Register*/
203 /* Set initial Stale timeout value*/
Amol Jadic52c8a32011-07-12 11:27:04 -0700204 writel(MSM_BOOT_UART_DM_STALE_TIMEOUT_LSB, MSM_BOOT_UART_DM_IPR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700205
206 /* Configure IRDA if required */
207 /* Disabling IRDA mode */
Amol Jadic52c8a32011-07-12 11:27:04 -0700208 writel(0x0, MSM_BOOT_UART_DM_IRDA(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700209
210 /* Configure and enable sim interface if required */
211
212 /* Configure hunt character value in HCR register */
213 /* Keep it in reset state */
Amol Jadic52c8a32011-07-12 11:27:04 -0700214 writel(0x0, MSM_BOOT_UART_DM_HCR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700215
216 /* Configure Rx FIFO base address */
217 /* Both TX/RX shares same SRAM and default is half-n-half.
218 * Sticking with default value now.
219 * As such RAM size is (2^RAM_ADDR_WIDTH, 32-bit entries).
220 * We have found RAM_ADDR_WIDTH = 0x7f */
221
222 /* Issue soft reset command */
Amol Jadic52c8a32011-07-12 11:27:04 -0700223 msm_boot_uart_dm_reset(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700224
225 /* Enable/Disable Rx/Tx DM interfaces */
226 /* Data Mover not currently utilized. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700227 writel(0x0, MSM_BOOT_UART_DM_DMEN(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700228
229
230 /* Enable transmitter and receiver */
Amol Jadic52c8a32011-07-12 11:27:04 -0700231 writel(MSM_BOOT_UART_DM_CR_RX_ENABLE, MSM_BOOT_UART_DM_CR(id));
232 writel(MSM_BOOT_UART_DM_CR_TX_ENABLE, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700233
234 /* Initialize Receive Path */
Amol Jadic52c8a32011-07-12 11:27:04 -0700235 msm_boot_uart_dm_init_rx_transfer(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700236
237 return MSM_BOOT_UART_DM_E_SUCCESS;
238}
239
240
241/*
242 * Initialize Receive Path
243 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700244static unsigned int msm_boot_uart_dm_init_rx_transfer(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700245{
Amol Jadic52c8a32011-07-12 11:27:04 -0700246 writel(MSM_BOOT_UART_DM_GCMD_DIS_STALE_EVT, MSM_BOOT_UART_DM_CR(id));
247 writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(id));
248 writel(MSM_BOOT_UART_DM_DMRX_DEF_VALUE, MSM_BOOT_UART_DM_DMRX(id));
249 writel(MSM_BOOT_UART_DM_GCMD_ENA_STALE_EVT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700250
251 return MSM_BOOT_UART_DM_E_SUCCESS;
252}
253
254/*
255 * UART Receive operation
256 * Reads a word from the RX FIFO.
257 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700258static unsigned int msm_boot_uart_dm_read(uint8_t id, unsigned int* data, int wait)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700259{
260 static int rx_last_snap_count = 0;
261 static int rx_chars_read_since_last_xfer = 0;
262
263 if (data == NULL)
264 {
265 return MSM_BOOT_UART_DM_E_INVAL;
266 }
267
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700268 /* We will be polling RXRDY status bit */
Amol Jadic52c8a32011-07-12 11:27:04 -0700269 while (!(readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_RXRDY))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700270 {
271 /* if this is not a blocking call, we'll just return */
272 if (!wait)
273 {
274 return MSM_BOOT_UART_DM_E_RX_NOT_READY;
275 }
276 }
277
278 /* Check for Overrun error. We'll just reset Error Status */
Amol Jadic52c8a32011-07-12 11:27:04 -0700279 if (readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_UART_OVERRUN)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700280 {
Amol Jadic52c8a32011-07-12 11:27:04 -0700281 writel(MSM_BOOT_UART_DM_CMD_RESET_ERR_STAT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700282 }
283
284 /* RX FIFO is ready; read a word. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700285 *data = readl(MSM_BOOT_UART_DM_RF(id, 0));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700286
287 /* increment the total count of chars we've read so far */
288 rx_chars_read_since_last_xfer += 4;
Shashank Mittaled177732011-05-06 19:12:59 -0700289
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700290 /* Rx transfer ends when one of the conditions is met:
291 * - The number of characters received since the end of the previous xfer
292 * equals the value written to DMRX at Transfer Initialization
293 * - A stale event occurred
294 */
295
296 /* If RX transfer has not ended yet */
297 if (rx_last_snap_count == 0)
298 {
299 /* Check if we've received stale event */
Amol Jadic52c8a32011-07-12 11:27:04 -0700300 if (readl(MSM_BOOT_UART_DM_MISR(id)) & MSM_BOOT_UART_DM_RXSTALE)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700301 {
302 /* Send command to reset stale interrupt */
Amol Jadic52c8a32011-07-12 11:27:04 -0700303 writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700304 }
305
306 /* Check if we haven't read more than DMRX value */
307 else if ((unsigned int)rx_chars_read_since_last_xfer <
Amol Jadic52c8a32011-07-12 11:27:04 -0700308 readl(MSM_BOOT_UART_DM_DMRX(id)))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700309 {
310 /* We can still continue reading before initializing RX transfer */
311 return MSM_BOOT_UART_DM_E_SUCCESS;
312 }
313
314 /* If we've reached here it means RX xfer end conditions been met */
315
316 /* Read UART_DM_RX_TOTAL_SNAP register to know how many valid chars
317 * we've read so far since last transfer */
Amol Jadic52c8a32011-07-12 11:27:04 -0700318 rx_last_snap_count = readl(MSM_BOOT_UART_DM_RX_TOTAL_SNAP(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700319
320 }
321
322 /* If there are still data left in FIFO we'll read them before
323 * initializing RX Transfer again */
324 if ((rx_last_snap_count - rx_chars_read_since_last_xfer) >= 0 )
325 {
326 return MSM_BOOT_UART_DM_E_SUCCESS;
327 }
328
Amol Jadic52c8a32011-07-12 11:27:04 -0700329 msm_boot_uart_dm_init_rx_transfer(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700330 rx_last_snap_count = 0;
331 rx_chars_read_since_last_xfer = 0;
332
333 return MSM_BOOT_UART_DM_E_SUCCESS;
334}
335
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700336/*
337 * UART transmit operation
338 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700339static unsigned int msm_boot_uart_dm_write(uint8_t id, char* data,
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700340 unsigned int num_of_chars)
341{
342 unsigned int tx_word_count = 0;
343 unsigned int tx_char_left = 0, tx_char = 0;
344 unsigned int tx_word = 0;
345 int i = 0;
346 char* tx_data = NULL;
347 char new_data[1024];
348
349 if ((data == NULL) || (num_of_chars <= 0))
350 {
351 return MSM_BOOT_UART_DM_E_INVAL;
352 }
353
354 /* Replace line-feed (/n) with carriage-return + line-feed (/r/n) */
355
356 msm_boot_uart_replace_lr_with_cr(data, num_of_chars, new_data, &i);
357
358 tx_data = new_data;
359 num_of_chars = i;
360
361 /* Write to NO_CHARS_FOR_TX register number of characters
362 * to be transmitted. However, before writing TX_FIFO must
363 * be empty as indicated by TX_READY interrupt in IMR register
364 */
365
366 /* Check if transmit FIFO is empty.
367 * If not we'll wait for TX_READY interrupt. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700368 if (!(readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_TXEMT))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700369 {
Amol Jadic52c8a32011-07-12 11:27:04 -0700370 while (!(readl(MSM_BOOT_UART_DM_ISR(id)) & MSM_BOOT_UART_DM_TX_READY))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700371 {
372 udelay(1);
373 /* Kick watchdog? */
374 }
375 }
376
377 /* We are here. FIFO is ready to be written. */
378 /* Write number of characters to be written */
Amol Jadic52c8a32011-07-12 11:27:04 -0700379 writel(num_of_chars, MSM_BOOT_UART_DM_NO_CHARS_FOR_TX(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700380
381 /* Clear TX_READY interrupt */
Amol Jadic52c8a32011-07-12 11:27:04 -0700382 writel(MSM_BOOT_UART_DM_GCMD_RES_TX_RDY_INT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700383
384 /* We use four-character word FIFO. So we need to divide data into
385 * four characters and write in UART_DM_TF register */
386 tx_word_count = (num_of_chars % 4)? ((num_of_chars / 4) + 1) :
387 (num_of_chars / 4);
388 tx_char_left = num_of_chars;
389
390 for (i = 0; i < (int)tx_word_count; i++)
391 {
392 tx_char = (tx_char_left < 4)? tx_char_left : 4;
393 PACK_CHARS_INTO_WORDS(tx_data, tx_char, tx_word);
394
395 /* Wait till TX FIFO has space */
Amol Jadic52c8a32011-07-12 11:27:04 -0700396 while (!(readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_TXRDY))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700397 {
398 udelay(1);
399 }
400
401 /* TX FIFO has space. Write the chars */
Amol Jadic52c8a32011-07-12 11:27:04 -0700402 writel(tx_word, MSM_BOOT_UART_DM_TF(id, 0));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700403 tx_char_left = num_of_chars - (i+1)*4;
404 tx_data = tx_data + 4;
405 }
406
407 return MSM_BOOT_UART_DM_E_SUCCESS;
408}
409
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700410/* Defining functions that's exposed to outside world and in coformance to
411 * existing uart implemention. These functions are being called to initialize
Amol Jadic52c8a32011-07-12 11:27:04 -0700412 * UART and print debug messages in bootloader.
413 */
414void uart_init(uint8_t gsbi_id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700415{
Amol Jadic52c8a32011-07-12 11:27:04 -0700416 static uint8_t port = 0;
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700417 char *data = "Android Bootloader - UART_DM Initialized!!!\n";
418
Amol Jadic52c8a32011-07-12 11:27:04 -0700419 msm_boot_uart_dm_init(gsbi_id);
420 msm_boot_uart_dm_write(gsbi_id, data, 44);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700421
Amol Jadic52c8a32011-07-12 11:27:04 -0700422 ASSERT(port < ARRAY_SIZE(gsbi_lookup));
423 gsbi_lookup[port++] = gsbi_id;
Shashank Mittal88df8052011-12-01 16:29:27 -0800424
425 /* Set UART init flag */
426 uart_init_flag = 1;
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700427}
428
Amol Jadic52c8a32011-07-12 11:27:04 -0700429
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700430/* UART_DM uses four character word FIFO where as UART core
431 * uses a character FIFO. so it's really inefficient to try
432 * to write single character. But that's how dprintf has been
433 * implemented.
434 */
435int uart_putc(int port, char c)
436{
Amol Jadic52c8a32011-07-12 11:27:04 -0700437 uint8_t gsbi_id = gsbi_lookup[port];
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700438
Shashank Mittal88df8052011-12-01 16:29:27 -0800439 /* Don't do anything if UART is not initialized */
440 if(!uart_init_flag)
441 return;
442
Amol Jadic52c8a32011-07-12 11:27:04 -0700443 msm_boot_uart_dm_write(gsbi_id, &c, 1);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700444
445 return 0;
446}
447
448/* UART_DM uses four character word FIFO whereas uart_getc
449 * is supposed to read only one character. So we need to
450 * read a word and keep track of each character in the word.
451 */
452int uart_getc(int port, bool wait)
453{
454 int byte;
455 static unsigned int word = 0;
Amol Jadic52c8a32011-07-12 11:27:04 -0700456 uint8_t gsbi_id = gsbi_lookup[port];
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700457
Shashank Mittal88df8052011-12-01 16:29:27 -0800458 /* Don't do anything if UART is not initialized */
459 if(!uart_init_flag)
460 return;
461
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700462 if (!word)
463 {
464 /* Read from FIFO only if it's a first read or all the four
465 * characters out of a word have been read */
Amol Jadic52c8a32011-07-12 11:27:04 -0700466 if (msm_boot_uart_dm_read(gsbi_id, &word, wait) !=
467 MSM_BOOT_UART_DM_E_SUCCESS)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700468 {
469 return -1;
470 }
471
472 }
473
474 byte = (int) word & 0xff;
475 word = word >> 8;
476
477 return byte;
478}
479