blob: 01cf5a47478ad07b8ae88cd006567e1af3e89600 [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
Bikas Gurungd1aa5902010-10-01 23:45:33 -070049/* Note:
50 * This is a basic implementation of UART_DM protocol. More focus has been
51 * given on simplicity than efficiency. Few of the things to be noted are:
52 * - RX path may not be suitable for multi-threaded scenaraio because of the
53 * use of static variables. TX path shouldn't have any problem though. If
54 * multi-threaded support is required, a simple data-structure can
55 * be maintained for each thread.
56 * - Right now we are using polling method than interrupt based.
57 * - We are using legacy UART protocol without Data Mover.
58 * - Not all interrupts and error events are handled.
59 * - While waiting Watchdog hasn't been taken into consideration.
60 */
61
62
63#define PACK_CHARS_INTO_WORDS(a, cnt, word) { \
64 word = 0; \
65 for(int j=0; j < (int)cnt; j++) \
66 { \
67 word |= (a[j] & 0xff) \
68 << (j * 8); \
69 } \
70 }
71
72
73/* Static Function Prototype Declarations */
Amol Jadic52c8a32011-07-12 11:27:04 -070074static unsigned int msm_boot_uart_dm_gsbi_init(uint8_t id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -070075static unsigned int msm_boot_uart_replace_lr_with_cr(char* data_in,
76 int num_of_chars,
77 char *data_out,
78 int *num_of_chars_out);
Amol Jadic52c8a32011-07-12 11:27:04 -070079static unsigned int msm_boot_uart_dm_init(uint8_t id);
80static unsigned int msm_boot_uart_dm_read(uint8_t id, unsigned int* data,
Bikas Gurungd1aa5902010-10-01 23:45:33 -070081 int wait);
Amol Jadic52c8a32011-07-12 11:27:04 -070082static unsigned int msm_boot_uart_dm_write(uint8_t id, char* data,
Bikas Gurungd1aa5902010-10-01 23:45:33 -070083 unsigned int num_of_chars);
Amol Jadic52c8a32011-07-12 11:27:04 -070084static unsigned int msm_boot_uart_dm_init_rx_transfer(uint8_t id);
85static unsigned int msm_boot_uart_dm_reset(uint8_t id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -070086
Amol Jadic52c8a32011-07-12 11:27:04 -070087/* Keep track of gsbi vs port mapping.
88 */
89static uint8_t gsbi_lookup[4];
Bikas Gurungd1aa5902010-10-01 23:45:33 -070090
91/* Extern functions */
Bikas Gurungd1aa5902010-10-01 23:45:33 -070092void udelay(unsigned usecs);
93
94
95/*
96 * Helper function to replace Line Feed char "\n" with
97 * Carriage Return "\r\n".
98 * Currently keeping it simple than efficient
99 */
100static unsigned int msm_boot_uart_replace_lr_with_cr(char* data_in,
101 int num_of_chars,
102 char *data_out,
103 int *num_of_chars_out )
104{
105 int i = 0, j = 0;
106
107 if ((data_in == NULL) || (data_out == NULL) || (num_of_chars < 0))
108 {
109 return MSM_BOOT_UART_DM_E_INVAL;
110 }
111
112 for (i=0, j=0; i < num_of_chars; i++, j++)
113 {
114 if ( data_in[i] == '\n' )
115 {
116 data_out[j++] = '\r';
117 }
118
119 data_out[j] = data_in[i];
120 }
121
122 *num_of_chars_out = j;
123
124 return MSM_BOOT_UART_DM_E_SUCCESS;
125}
126
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700127/*
128 * Initialize and configure GSBI for operation
129 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700130static unsigned int msm_boot_uart_dm_gsbi_init(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700131{
Amol Jadic52c8a32011-07-12 11:27:04 -0700132 /* Configure the uart clock */
133 clock_config_uart_dm(id);
Shashank Mittalaf406672011-11-23 19:44:52 -0800134 dsb();
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700135
Amol Jadic52c8a32011-07-12 11:27:04 -0700136 /* Configure GPIO to provide connectivity between GSBI
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700137 product ports and chip pads */
Amol Jadic52c8a32011-07-12 11:27:04 -0700138 gpio_config_uart_dm(id);
Shashank Mittalaf406672011-11-23 19:44:52 -0800139 dsb();
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700140
141 /* Configure Data Mover for GSBI operation.
142 * Currently not supported. */
143
144 /* Configure GSBI for UART_DM protocol.
145 * I2C on 2 ports, UART (without HS flow control) on the other 2. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700146 writel(GSBI_PROTOCOL_CODE_I2C_UART << GSBI_CTRL_REG_PROTOCOL_CODE_S,
147 GSBI_CTRL_REG(id));
Shashank Mittalaf406672011-11-23 19:44:52 -0800148 dsb();
Amol Jadic52c8a32011-07-12 11:27:04 -0700149
Shashank Mittalaf406672011-11-23 19:44:52 -0800150 /* Configure clock selection register for tx and rx rates.
151 * Selecting 115.2k for both RX and TX.
152 */
153 writel(UART_DM_CLK_RX_TX_BIT_RATE, MSM_BOOT_UART_DM_CSR(id));
154 dsb();
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700155
156 return MSM_BOOT_UART_DM_E_SUCCESS;
157}
158
159/*
160 * Reset the UART
161 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700162static unsigned int msm_boot_uart_dm_reset(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700163{
Amol Jadic52c8a32011-07-12 11:27:04 -0700164 writel(MSM_BOOT_UART_DM_CMD_RESET_RX, MSM_BOOT_UART_DM_CR(id));
165 writel(MSM_BOOT_UART_DM_CMD_RESET_TX, MSM_BOOT_UART_DM_CR(id));
166 writel(MSM_BOOT_UART_DM_CMD_RESET_ERR_STAT, MSM_BOOT_UART_DM_CR(id));
167 writel(MSM_BOOT_UART_DM_CMD_RES_TX_ERR, MSM_BOOT_UART_DM_CR(id));
168 writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700169
170 return MSM_BOOT_UART_DM_E_SUCCESS;
171}
172
173
174/*
175 * Initialize UART_DM - configure clock and required registers.
176 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700177static unsigned int msm_boot_uart_dm_init(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700178{
Amol Jadic52c8a32011-07-12 11:27:04 -0700179 /* Configure GSBI for uart dm */
180 msm_boot_uart_dm_gsbi_init(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700181
182 /* Configure UART mode registers MR1 and MR2 */
183 /* Hardware flow control isn't supported */
Amol Jadic52c8a32011-07-12 11:27:04 -0700184 writel(0x0, MSM_BOOT_UART_DM_MR1(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700185
186 /* 8-N-1 configuration: 8 data bits - No parity - 1 stop bit */
Amol Jadic52c8a32011-07-12 11:27:04 -0700187 writel(MSM_BOOT_UART_DM_8_N_1_MODE, MSM_BOOT_UART_DM_MR2(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700188
189 /* Configure Interrupt Mask register IMR */
Amol Jadic52c8a32011-07-12 11:27:04 -0700190 writel(MSM_BOOT_UART_DM_IMR_ENABLED, MSM_BOOT_UART_DM_IMR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700191
192 /* Configure Tx and Rx watermarks configuration registers */
193 /* TX watermark value is set to 0 - interrupt is generated when
194 * FIFO level is less than or equal to 0 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700195 writel(MSM_BOOT_UART_DM_TFW_VALUE, MSM_BOOT_UART_DM_TFWR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700196
197 /* RX watermark value*/
Amol Jadic52c8a32011-07-12 11:27:04 -0700198 writel(MSM_BOOT_UART_DM_RFW_VALUE, MSM_BOOT_UART_DM_RFWR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700199
200 /* Configure Interrupt Programming Register*/
201 /* Set initial Stale timeout value*/
Amol Jadic52c8a32011-07-12 11:27:04 -0700202 writel(MSM_BOOT_UART_DM_STALE_TIMEOUT_LSB, MSM_BOOT_UART_DM_IPR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700203
204 /* Configure IRDA if required */
205 /* Disabling IRDA mode */
Amol Jadic52c8a32011-07-12 11:27:04 -0700206 writel(0x0, MSM_BOOT_UART_DM_IRDA(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700207
208 /* Configure and enable sim interface if required */
209
210 /* Configure hunt character value in HCR register */
211 /* Keep it in reset state */
Amol Jadic52c8a32011-07-12 11:27:04 -0700212 writel(0x0, MSM_BOOT_UART_DM_HCR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700213
214 /* Configure Rx FIFO base address */
215 /* Both TX/RX shares same SRAM and default is half-n-half.
216 * Sticking with default value now.
217 * As such RAM size is (2^RAM_ADDR_WIDTH, 32-bit entries).
218 * We have found RAM_ADDR_WIDTH = 0x7f */
219
220 /* Issue soft reset command */
Amol Jadic52c8a32011-07-12 11:27:04 -0700221 msm_boot_uart_dm_reset(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700222
223 /* Enable/Disable Rx/Tx DM interfaces */
224 /* Data Mover not currently utilized. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700225 writel(0x0, MSM_BOOT_UART_DM_DMEN(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700226
227
228 /* Enable transmitter and receiver */
Amol Jadic52c8a32011-07-12 11:27:04 -0700229 writel(MSM_BOOT_UART_DM_CR_RX_ENABLE, MSM_BOOT_UART_DM_CR(id));
230 writel(MSM_BOOT_UART_DM_CR_TX_ENABLE, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700231
232 /* Initialize Receive Path */
Amol Jadic52c8a32011-07-12 11:27:04 -0700233 msm_boot_uart_dm_init_rx_transfer(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700234
235 return MSM_BOOT_UART_DM_E_SUCCESS;
236}
237
238
239/*
240 * Initialize Receive Path
241 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700242static unsigned int msm_boot_uart_dm_init_rx_transfer(uint8_t id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700243{
Amol Jadic52c8a32011-07-12 11:27:04 -0700244 writel(MSM_BOOT_UART_DM_GCMD_DIS_STALE_EVT, MSM_BOOT_UART_DM_CR(id));
245 writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(id));
246 writel(MSM_BOOT_UART_DM_DMRX_DEF_VALUE, MSM_BOOT_UART_DM_DMRX(id));
247 writel(MSM_BOOT_UART_DM_GCMD_ENA_STALE_EVT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700248
249 return MSM_BOOT_UART_DM_E_SUCCESS;
250}
251
252/*
253 * UART Receive operation
254 * Reads a word from the RX FIFO.
255 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700256static unsigned int msm_boot_uart_dm_read(uint8_t id, unsigned int* data, int wait)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700257{
258 static int rx_last_snap_count = 0;
259 static int rx_chars_read_since_last_xfer = 0;
260
261 if (data == NULL)
262 {
263 return MSM_BOOT_UART_DM_E_INVAL;
264 }
265
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700266 /* We will be polling RXRDY status bit */
Amol Jadic52c8a32011-07-12 11:27:04 -0700267 while (!(readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_RXRDY))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700268 {
269 /* if this is not a blocking call, we'll just return */
270 if (!wait)
271 {
272 return MSM_BOOT_UART_DM_E_RX_NOT_READY;
273 }
274 }
275
276 /* Check for Overrun error. We'll just reset Error Status */
Amol Jadic52c8a32011-07-12 11:27:04 -0700277 if (readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_UART_OVERRUN)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700278 {
Amol Jadic52c8a32011-07-12 11:27:04 -0700279 writel(MSM_BOOT_UART_DM_CMD_RESET_ERR_STAT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700280 }
281
282 /* RX FIFO is ready; read a word. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700283 *data = readl(MSM_BOOT_UART_DM_RF(id, 0));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700284
285 /* increment the total count of chars we've read so far */
286 rx_chars_read_since_last_xfer += 4;
Shashank Mittaled177732011-05-06 19:12:59 -0700287
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700288 /* Rx transfer ends when one of the conditions is met:
289 * - The number of characters received since the end of the previous xfer
290 * equals the value written to DMRX at Transfer Initialization
291 * - A stale event occurred
292 */
293
294 /* If RX transfer has not ended yet */
295 if (rx_last_snap_count == 0)
296 {
297 /* Check if we've received stale event */
Amol Jadic52c8a32011-07-12 11:27:04 -0700298 if (readl(MSM_BOOT_UART_DM_MISR(id)) & MSM_BOOT_UART_DM_RXSTALE)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700299 {
300 /* Send command to reset stale interrupt */
Amol Jadic52c8a32011-07-12 11:27:04 -0700301 writel(MSM_BOOT_UART_DM_CMD_RES_STALE_INT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700302 }
303
304 /* Check if we haven't read more than DMRX value */
305 else if ((unsigned int)rx_chars_read_since_last_xfer <
Amol Jadic52c8a32011-07-12 11:27:04 -0700306 readl(MSM_BOOT_UART_DM_DMRX(id)))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700307 {
308 /* We can still continue reading before initializing RX transfer */
309 return MSM_BOOT_UART_DM_E_SUCCESS;
310 }
311
312 /* If we've reached here it means RX xfer end conditions been met */
313
314 /* Read UART_DM_RX_TOTAL_SNAP register to know how many valid chars
315 * we've read so far since last transfer */
Amol Jadic52c8a32011-07-12 11:27:04 -0700316 rx_last_snap_count = readl(MSM_BOOT_UART_DM_RX_TOTAL_SNAP(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700317
318 }
319
320 /* If there are still data left in FIFO we'll read them before
321 * initializing RX Transfer again */
322 if ((rx_last_snap_count - rx_chars_read_since_last_xfer) >= 0 )
323 {
324 return MSM_BOOT_UART_DM_E_SUCCESS;
325 }
326
Amol Jadic52c8a32011-07-12 11:27:04 -0700327 msm_boot_uart_dm_init_rx_transfer(id);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700328 rx_last_snap_count = 0;
329 rx_chars_read_since_last_xfer = 0;
330
331 return MSM_BOOT_UART_DM_E_SUCCESS;
332}
333
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700334/*
335 * UART transmit operation
336 */
Amol Jadic52c8a32011-07-12 11:27:04 -0700337static unsigned int msm_boot_uart_dm_write(uint8_t id, char* data,
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700338 unsigned int num_of_chars)
339{
340 unsigned int tx_word_count = 0;
341 unsigned int tx_char_left = 0, tx_char = 0;
342 unsigned int tx_word = 0;
343 int i = 0;
344 char* tx_data = NULL;
345 char new_data[1024];
346
347 if ((data == NULL) || (num_of_chars <= 0))
348 {
349 return MSM_BOOT_UART_DM_E_INVAL;
350 }
351
352 /* Replace line-feed (/n) with carriage-return + line-feed (/r/n) */
353
354 msm_boot_uart_replace_lr_with_cr(data, num_of_chars, new_data, &i);
355
356 tx_data = new_data;
357 num_of_chars = i;
358
359 /* Write to NO_CHARS_FOR_TX register number of characters
360 * to be transmitted. However, before writing TX_FIFO must
361 * be empty as indicated by TX_READY interrupt in IMR register
362 */
363
364 /* Check if transmit FIFO is empty.
365 * If not we'll wait for TX_READY interrupt. */
Amol Jadic52c8a32011-07-12 11:27:04 -0700366 if (!(readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_TXEMT))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700367 {
Amol Jadic52c8a32011-07-12 11:27:04 -0700368 while (!(readl(MSM_BOOT_UART_DM_ISR(id)) & MSM_BOOT_UART_DM_TX_READY))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700369 {
370 udelay(1);
371 /* Kick watchdog? */
372 }
373 }
374
375 /* We are here. FIFO is ready to be written. */
376 /* Write number of characters to be written */
Amol Jadic52c8a32011-07-12 11:27:04 -0700377 writel(num_of_chars, MSM_BOOT_UART_DM_NO_CHARS_FOR_TX(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700378
379 /* Clear TX_READY interrupt */
Amol Jadic52c8a32011-07-12 11:27:04 -0700380 writel(MSM_BOOT_UART_DM_GCMD_RES_TX_RDY_INT, MSM_BOOT_UART_DM_CR(id));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700381
382 /* We use four-character word FIFO. So we need to divide data into
383 * four characters and write in UART_DM_TF register */
384 tx_word_count = (num_of_chars % 4)? ((num_of_chars / 4) + 1) :
385 (num_of_chars / 4);
386 tx_char_left = num_of_chars;
387
388 for (i = 0; i < (int)tx_word_count; i++)
389 {
390 tx_char = (tx_char_left < 4)? tx_char_left : 4;
391 PACK_CHARS_INTO_WORDS(tx_data, tx_char, tx_word);
392
393 /* Wait till TX FIFO has space */
Amol Jadic52c8a32011-07-12 11:27:04 -0700394 while (!(readl(MSM_BOOT_UART_DM_SR(id)) & MSM_BOOT_UART_DM_SR_TXRDY))
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700395 {
396 udelay(1);
397 }
398
399 /* TX FIFO has space. Write the chars */
Amol Jadic52c8a32011-07-12 11:27:04 -0700400 writel(tx_word, MSM_BOOT_UART_DM_TF(id, 0));
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700401 tx_char_left = num_of_chars - (i+1)*4;
402 tx_data = tx_data + 4;
403 }
404
405 return MSM_BOOT_UART_DM_E_SUCCESS;
406}
407
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700408/* Defining functions that's exposed to outside world and in coformance to
409 * existing uart implemention. These functions are being called to initialize
Amol Jadic52c8a32011-07-12 11:27:04 -0700410 * UART and print debug messages in bootloader.
411 */
412void uart_init(uint8_t gsbi_id)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700413{
Amol Jadic52c8a32011-07-12 11:27:04 -0700414 static uint8_t port = 0;
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700415 char *data = "Android Bootloader - UART_DM Initialized!!!\n";
416
Amol Jadic52c8a32011-07-12 11:27:04 -0700417 msm_boot_uart_dm_init(gsbi_id);
418 msm_boot_uart_dm_write(gsbi_id, data, 44);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700419
Amol Jadic52c8a32011-07-12 11:27:04 -0700420 ASSERT(port < ARRAY_SIZE(gsbi_lookup));
421 gsbi_lookup[port++] = gsbi_id;
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700422}
423
Amol Jadic52c8a32011-07-12 11:27:04 -0700424
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700425/* UART_DM uses four character word FIFO where as UART core
426 * uses a character FIFO. so it's really inefficient to try
427 * to write single character. But that's how dprintf has been
428 * implemented.
429 */
430int uart_putc(int port, char c)
431{
Amol Jadic52c8a32011-07-12 11:27:04 -0700432 uint8_t gsbi_id = gsbi_lookup[port];
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700433
Amol Jadic52c8a32011-07-12 11:27:04 -0700434 msm_boot_uart_dm_write(gsbi_id, &c, 1);
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700435
436 return 0;
437}
438
439/* UART_DM uses four character word FIFO whereas uart_getc
440 * is supposed to read only one character. So we need to
441 * read a word and keep track of each character in the word.
442 */
443int uart_getc(int port, bool wait)
444{
445 int byte;
446 static unsigned int word = 0;
Amol Jadic52c8a32011-07-12 11:27:04 -0700447 uint8_t gsbi_id = gsbi_lookup[port];
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700448
449 if (!word)
450 {
451 /* Read from FIFO only if it's a first read or all the four
452 * characters out of a word have been read */
Amol Jadic52c8a32011-07-12 11:27:04 -0700453 if (msm_boot_uart_dm_read(gsbi_id, &word, wait) !=
454 MSM_BOOT_UART_DM_E_SUCCESS)
Bikas Gurungd1aa5902010-10-01 23:45:33 -0700455 {
456 return -1;
457 }
458
459 }
460
461 byte = (int) word & 0xff;
462 word = word >> 8;
463
464 return byte;
465}
466