Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | * spi-uart debug routings |
| 3 | * Copyright (C) 2008, Feng Tang <feng.tang@intel.com> Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include "spi-uart.h" |
| 21 | |
| 22 | #define MRST_SPI_TIMEOUT 0x200000 |
Alek Du | c8496d1 | 2008-07-10 14:46:17 +0800 | [diff] [blame] | 23 | static int spi_inited = 0; |
Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 24 | static struct mrst_spi_reg *pspi = 0; |
| 25 | |
| 26 | static void spi_init() |
| 27 | { |
| 28 | u32 ctrlr0; |
| 29 | |
| 30 | pspi = (struct mrst_spi_reg *)MRST_REGBASE_SPI0; |
| 31 | |
| 32 | /* disable SPI controller first */ |
| 33 | pspi->ssienr = 0x0; |
| 34 | |
| 35 | /* set control param, 16 bits, transmit only mode */ |
| 36 | ctrlr0 = pspi->ctrlr0; |
| 37 | |
| 38 | ctrlr0 &= 0xfcc0; |
| 39 | ctrlr0 |= (0xf | (FRF_SPI << SPI_FRF_OFFSET) |
| 40 | | (TMOD_TO << SPI_TMOD_OFFSET)); |
| 41 | pspi->ctrlr0 = ctrlr0; |
| 42 | |
| 43 | /* set a default baud rate, 115200 */ |
| 44 | /* feng, need make sure SPIC and MAXIM3110 match */ |
| 45 | //spi_enable_clk(32); |
Alek Du | b7b3000 | 2008-08-21 09:49:20 +0800 | [diff] [blame] | 46 | pspi->baudr = 2; |
Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 47 | |
| 48 | /* need set the transmit threshhol? */ |
| 49 | pspi->txftlr = 0x3; |
| 50 | |
| 51 | /* disable all INT for early phase */ |
| 52 | pspi->imr &= 0xffffff00; |
| 53 | |
| 54 | /* select one slave SPI device */ |
Alek Du | 3f5527f | 2008-06-17 14:08:35 +0800 | [diff] [blame] | 55 | pspi->ser = 0x2; |
Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 56 | |
| 57 | /* enable the HW, this should be the last step for HW init */ |
| 58 | pspi->ssienr |= 0x1; |
| 59 | |
| 60 | spi_inited = 1; |
| 61 | } |
| 62 | |
| 63 | /* set the ratio rate, INT */ |
| 64 | static void max3110_write_config(void) |
| 65 | { |
| 66 | u16 config; |
| 67 | |
| 68 | /* 115200, TM not set, no parity, 8bit word */ |
Alek Du | b7b3000 | 2008-08-21 09:49:20 +0800 | [diff] [blame] | 69 | config = 0xc001; |
Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 70 | pspi->dr[0] = config; |
| 71 | } |
| 72 | |
| 73 | /* transfer char to a eligibal word and send to max3110 */ |
Alek Du | c8496d1 | 2008-07-10 14:46:17 +0800 | [diff] [blame] | 74 | static void max3110_write_data(char c) |
Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 75 | { |
| 76 | u16 data; |
Alek Du | 7eac544 | 2008-06-05 17:38:56 +0800 | [diff] [blame] | 77 | |
| 78 | data = 0x8000 | c; |
| 79 | pspi->dr[0] = data; |
| 80 | } |
| 81 | |
| 82 | /* slave select should be called in the read/write function */ |
| 83 | static int spi_max3110_putc(char c) |
| 84 | { |
| 85 | unsigned int timeout; |
| 86 | u32 sr; |
| 87 | u32 test; |
| 88 | |
| 89 | /* read RX FIFO out if there is any */ |
| 90 | while ((pspi->sr & SR_RF_NOT_EMPT) && pspi->rxflr ) { |
| 91 | timeout = MRST_SPI_TIMEOUT; |
| 92 | while (timeout--) { |
| 93 | if (!(pspi->sr & SR_BUSY)) |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | if (timeout == 0xffffffff) |
| 98 | return -1; |
| 99 | test = pspi->dr[0]; |
| 100 | } |
| 101 | |
| 102 | timeout = MRST_SPI_TIMEOUT; |
| 103 | /* early putc need make sure the TX FIFO is empty */ |
| 104 | while (timeout--) { |
| 105 | sr = pspi->sr; |
| 106 | if ( (sr & SR_BUSY) || !(sr & SR_TF_EMPT)) |
| 107 | continue; |
| 108 | else |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | if (timeout == 0xffffffff) |
| 113 | return -1; |
| 114 | |
| 115 | max3110_write_data(c); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void bs_spi_printk(const char *str) |
| 122 | { |
| 123 | if (!spi_inited) |
| 124 | spi_init(); |
| 125 | |
| 126 | if (!str) |
| 127 | return; |
| 128 | |
| 129 | /* |
| 130 | * here we assume only 1 write_config is enough, |
| 131 | * if not will call it for each putc |
| 132 | */ |
| 133 | max3110_write_config(); |
| 134 | |
| 135 | while (*str) { |
| 136 | if (*str == '\n') |
| 137 | spi_max3110_putc('\r'); |
| 138 | spi_max3110_putc(*str++); |
| 139 | } |
| 140 | } |