aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2012-04-26 20:14:02 +0200
committerHarald Welte <laforge@gnumonks.org>2012-04-26 20:14:02 +0200
commit9398532f5220002050eb4ac0e8b7bedf85f538a5 (patch)
tree4cc4255f2beea8aa5d926117aeed7fe709b5a28b /src
parentc1a989f886183949f2dab8a19ab9e11783e74566 (diff)
add unfinished UART driver
Diffstat (limited to 'src')
-rw-r--r--src/cc32/uart_sc16is740.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/cc32/uart_sc16is740.c b/src/cc32/uart_sc16is740.c
new file mode 100644
index 0000000..12fee26
--- /dev/null
+++ b/src/cc32/uart_sc16is740.c
@@ -0,0 +1,88 @@
+/*
+ * NXP SC16IS740 SPI UART driver
+ *
+ * Copyright (C) 2012 Harald Welte <laforge@gnumonks.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define RHR 0x00
+#define THR 0x00
+#define IER 0x01
+#define FCR 0x02
+#define IIR 0x02
+#define LCR 0x03
+#define MCR 0x04
+#define LSR 0x05
+#define MSR 0x06
+#define SPR 0x07
+
+static void reg_write(uint8_t reg, uint8_t val)
+{
+ uint8_t cmd = (reg << 3);
+
+ cc32_spi_xcv_byte(cmd);
+ cc32_spi_xcv_byte(val);
+}
+
+static int reg_read(uint8_t reg)
+{
+ uint8_t tx = (reg << 3) | 0x80;
+
+ cc32_spi_xcv_byte(tx);
+
+ return cc32_spi_xcv_byte(0);
+}
+
+static void fifo_write(uint8_t *buf, uint8_t len)
+{
+ uint8_t cmd = (0 << 3);
+
+ cc32_spi_xcv_byte(cmd);
+
+ while (len--)
+ cc32_spi_xcv_byte(buf++);
+}
+
+static void fifo_read(uint8_t *buf, uint8_t len)
+{
+ uint8_t cmd = (reg << 3) | 0x80;
+
+ cc32_spi_xcv_byte(cmd);
+
+ while (len--)
+ *(buf++) = cc32_spi_xcv_byte(0);
+}
+
+
+void uart_sc16is740_init()
+{
+ /* set 8N1 */
+ reg_write(LCR, (3 << 0) | (0 << 2);
+ /* set LCR[7] = 1 (divisor latch) */
+ reg_write(LCR, reg_read(LCR) |= 0x80);
+ /* baud rate generator for 115200 */
+ reg_write(DLL, 1);
+ reg_write(DLH, 0);
+ /* clear LCR[7] = 0 (divisor latch) */
+ reg_write(LCR, reg_read(LCR) &= ~0x80);
+}
+
+void uart_sc16is740_putchar(uint8_t ch)
+{
+ /* wait until THR empty */
+ while (!(reg_read(LSR) & (1 << 5))) { }
+
+ reg_write(THR, ch);
+}