1 /* $NetBSD: zs.c,v 1.2 2008/03/23 17:19:57 tsutsui Exp $ */
4 * Copyright (c) 2008 Izumi Tsutsui. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * optional Z85C30 serial support for Qube 2700
32 #include <lib/libsa/stand.h>
33 #include <lib/libkern/libkern.h>
35 #include <dev/ic/z8530reg.h>
37 #include <machine/cpu.h>
42 #define ZSCLOCK 11059200 /* 19200 * 576 */
44 #define ZS_DELAY() delay(2)
46 static uint8_t zs_read(void *, uint8_t);
47 static void zs_write(void *, uint8_t, uint8_t);
48 static void zs_write_reg(void *, uint8_t, uint8_t);
49 static void zs_reset(void *);
52 zs_read(void *dev
, uint8_t reg
)
54 volatile uint8_t *zs
= dev
;
57 val
= *(volatile uint8_t *)(zs
+ reg
);
64 zs_write(void *dev
, uint8_t reg
, uint8_t val
)
66 volatile uint8_t *zs
= dev
;
68 *(volatile uint8_t *)(zs
+ reg
) = val
;
73 zs_write_reg(void *dev
, uint8_t reg
, uint8_t val
)
76 zs_write(dev
, ZS_CSR
, reg
);
77 zs_write(dev
, ZS_CSR
, val
);
85 zs_write_reg(dev
, 9, 0);
87 zs_write_reg(dev
, 9, ZSWR9_HARD_RESET
);
90 /* disable all inerttupts */
91 zs_write_reg(dev
, 1, 0);
93 /* set TX/RX misc parameters and modes */
94 zs_write_reg(dev
, 4, ZSWR4_CLK_X16
| ZSWR4_ONESB
| ZSWR4_EVENP
);
95 zs_write_reg(dev
, 10, ZSWR10_NRZ
);
96 zs_write_reg(dev
, 3, ZSWR3_RX_8
);
97 zs_write_reg(dev
, 5, ZSWR5_TX_8
| ZSWR5_DTR
| ZSWR5_RTS
);
99 /* sync registers unused */
100 zs_write_reg(dev
, 6, 0);
101 zs_write_reg(dev
, 7, 0);
103 /* set baud rate generator mode */
104 zs_write_reg(dev
, 14, ZSWR14_BAUD_FROM_PCLK
);
106 zs_write_reg(dev
, 11, ZSWR11_RXCLK_BAUD
| ZSWR11_TXCLK_BAUD
);
107 /* set baud rate constant */
108 zs_write_reg(dev
, 12, BPS_TO_TCONST(ZSCLOCK
/ 16, ZSSPEED
));
109 zs_write_reg(dev
, 13, 0);
111 /* enable baud rate generator */
112 zs_write_reg(dev
, 14, ZSWR14_BAUD_FROM_PCLK
| ZSWR14_BAUD_ENA
);
113 /* disable all external interrupts */
114 zs_write_reg(dev
, 15, 0);
116 /* reset external status twice (see src/sys/dev/ic/z8530sc.c) */
117 zs_write(dev
, ZS_CSR
, ZSWR0_RESET_STATUS
);
118 zs_write(dev
, ZS_CSR
, ZSWR0_RESET_STATUS
);
120 /* enable TX and RX */
121 zs_write_reg(dev
, 3, ZSWR3_RX_8
| ZSWR3_RX_ENABLE
);
123 ZSWR5_TX_8
| ZSWR5_DTR
| ZSWR5_RTS
| ZSWR5_TX_ENABLE
);
127 zs_init(int addr
, int speed
)
131 zs
= (void *)MIPS_PHYS_TO_KSEG1(ZS_BASE
+ addr
);
138 zs_putc(void *dev
, int c
)
143 csr
= zs_read(dev
, ZS_CSR
);
144 } while ((csr
& ZSRR0_TX_READY
) == 0);
146 zs_write(dev
, ZS_DATA
, c
);
155 csr
= zs_read(dev
, ZS_CSR
);
156 } while ((csr
& ZSRR0_RX_READY
) == 0);
158 data
= zs_read(dev
, ZS_DATA
);
167 csr
= zs_read(dev
, ZS_CSR
);
168 if ((csr
& ZSRR0_RX_READY
) == 0)
171 data
= zs_read(dev
, ZS_DATA
);