rs5c316_mainbus.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: rs5c316_mainbus.c,v 1.1.4.2 2010/05/30 05:16:45 rmind Exp $ */
2 1.1.4.2 rmind
3 1.1.4.2 rmind /*-
4 1.1.4.2 rmind * Copyright (c) 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1.4.2 rmind * All rights reserved.
6 1.1.4.2 rmind *
7 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 rmind * modification, are permitted provided that the following conditions
9 1.1.4.2 rmind * are met:
10 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
15 1.1.4.2 rmind *
16 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1.4.2 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.4.2 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.4.2 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1.4.2 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.4.2 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.4.2 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.4.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.4.2 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.4.2 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.4.2 rmind * SUCH DAMAGE.
27 1.1.4.2 rmind */
28 1.1.4.2 rmind
29 1.1.4.2 rmind #include <sys/cdefs.h>
30 1.1.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: rs5c316_mainbus.c,v 1.1.4.2 2010/05/30 05:16:45 rmind Exp $");
31 1.1.4.2 rmind
32 1.1.4.2 rmind #include <sys/param.h>
33 1.1.4.2 rmind #include <sys/systm.h>
34 1.1.4.2 rmind #include <sys/device.h>
35 1.1.4.2 rmind #include <sys/kernel.h>
36 1.1.4.2 rmind
37 1.1.4.2 rmind #include <dev/clock_subr.h>
38 1.1.4.2 rmind #include <dev/ic/rs5c313var.h>
39 1.1.4.2 rmind
40 1.1.4.2 rmind #include <machine/autoconf.h>
41 1.1.4.2 rmind
42 1.1.4.2 rmind #include <sh3/devreg.h>
43 1.1.4.2 rmind #include <sh3/pfcreg.h>
44 1.1.4.2 rmind
45 1.1.4.2 rmind #include <evbsh3/ap_ms104_sh4/ap_ms104_sh4reg.h>
46 1.1.4.2 rmind #include <evbsh3/ap_ms104_sh4/ap_ms104_sh4var.h>
47 1.1.4.2 rmind
48 1.1.4.2 rmind /* chip access methods */
49 1.1.4.2 rmind static void rtc_begin(struct rs5c313_softc *);
50 1.1.4.2 rmind static void rtc_ce(struct rs5c313_softc *, int);
51 1.1.4.2 rmind static void rtc_dir(struct rs5c313_softc *, int);
52 1.1.4.2 rmind static void rtc_clk(struct rs5c313_softc *, int);
53 1.1.4.2 rmind static int rtc_read(struct rs5c313_softc *);
54 1.1.4.2 rmind static void rtc_write(struct rs5c313_softc *, int);
55 1.1.4.2 rmind
56 1.1.4.2 rmind static struct rs5c313_ops rs5c316_mainbus_ops = {
57 1.1.4.2 rmind .rs5c313_op_begin = rtc_begin,
58 1.1.4.2 rmind .rs5c313_op_ce = rtc_ce,
59 1.1.4.2 rmind .rs5c313_op_clk = rtc_clk,
60 1.1.4.2 rmind .rs5c313_op_dir = rtc_dir,
61 1.1.4.2 rmind .rs5c313_op_read = rtc_read,
62 1.1.4.2 rmind .rs5c313_op_write = rtc_write,
63 1.1.4.2 rmind };
64 1.1.4.2 rmind
65 1.1.4.2 rmind /* autoconf glue */
66 1.1.4.2 rmind static int rs5c316_mainbus_match(device_t, cfdata_t, void *);
67 1.1.4.2 rmind static void rs5c316_mainbus_attach(device_t, device_t, void *);
68 1.1.4.2 rmind
69 1.1.4.2 rmind CFATTACH_DECL_NEW(rs5c313_mainbus, sizeof(struct rs5c313_softc),
70 1.1.4.2 rmind rs5c316_mainbus_match, rs5c316_mainbus_attach, NULL, NULL);
71 1.1.4.2 rmind
72 1.1.4.2 rmind #define ndelay(x) delay(((x) + 999) / 1000)
73 1.1.4.2 rmind
74 1.1.4.2 rmind static int
75 1.1.4.2 rmind rs5c316_mainbus_match(device_t parent, cfdata_t cf, void *aux)
76 1.1.4.2 rmind {
77 1.1.4.2 rmind struct mainbus_attach_args *maa = (struct mainbus_attach_args *)aux;
78 1.1.4.2 rmind
79 1.1.4.2 rmind if (strcmp(maa->ma_name, "rs5c313rtc") != 0)
80 1.1.4.2 rmind return 0;
81 1.1.4.2 rmind return 1;
82 1.1.4.2 rmind }
83 1.1.4.2 rmind
84 1.1.4.2 rmind
85 1.1.4.2 rmind static void
86 1.1.4.2 rmind rs5c316_mainbus_attach(device_t parent, device_t self, void *aux)
87 1.1.4.2 rmind {
88 1.1.4.2 rmind struct rs5c313_softc *sc = device_private(self);
89 1.1.4.2 rmind uint32_t reg;
90 1.1.4.2 rmind
91 1.1.4.2 rmind sc->sc_dev = self;
92 1.1.4.2 rmind sc->sc_model = MODEL_5C316;
93 1.1.4.2 rmind sc->sc_ops = &rs5c316_mainbus_ops;
94 1.1.4.2 rmind
95 1.1.4.2 rmind /* setup gpio pin */
96 1.1.4.2 rmind reg = _reg_read_4(SH4_PCTRA);
97 1.1.4.2 rmind reg &= ~(3 << (GPIO_PIN_RTC_CE * 2));
98 1.1.4.2 rmind reg |= (1 << (GPIO_PIN_RTC_CE * 2)); /* output */
99 1.1.4.2 rmind reg &= ~(3 << (GPIO_PIN_RTC_SCLK * 2));
100 1.1.4.2 rmind reg |= (1 << (GPIO_PIN_RTC_SCLK * 2)); /* output */
101 1.1.4.2 rmind reg &= ~(3 << (GPIO_PIN_RTC_SIO * 2));
102 1.1.4.2 rmind reg |= (1 << (GPIO_PIN_RTC_SIO * 2)); /* output */
103 1.1.4.2 rmind _reg_write_4(SH4_PCTRA, reg);
104 1.1.4.2 rmind
105 1.1.4.2 rmind rs5c313_attach(sc);
106 1.1.4.2 rmind }
107 1.1.4.2 rmind
108 1.1.4.2 rmind static void
109 1.1.4.2 rmind rtc_begin(struct rs5c313_softc *sc)
110 1.1.4.2 rmind {
111 1.1.4.2 rmind
112 1.1.4.2 rmind /* nothing to do */
113 1.1.4.2 rmind }
114 1.1.4.2 rmind
115 1.1.4.2 rmind static void
116 1.1.4.2 rmind rtc_ce(struct rs5c313_softc *sc, int onoff)
117 1.1.4.2 rmind {
118 1.1.4.2 rmind uint16_t
119 1.1.4.2 rmind
120 1.1.4.2 rmind reg = _reg_read_2(SH4_PDTRA);
121 1.1.4.2 rmind if (onoff) {
122 1.1.4.2 rmind reg |= (1 << GPIO_PIN_RTC_CE);
123 1.1.4.2 rmind } else {
124 1.1.4.2 rmind reg &= ~(1 << GPIO_PIN_RTC_CE);
125 1.1.4.2 rmind }
126 1.1.4.2 rmind _reg_write_2(SH4_PDTRA, reg);
127 1.1.4.2 rmind ndelay(600);
128 1.1.4.2 rmind }
129 1.1.4.2 rmind
130 1.1.4.2 rmind static void
131 1.1.4.2 rmind rtc_clk(struct rs5c313_softc *sc, int onoff)
132 1.1.4.2 rmind {
133 1.1.4.2 rmind uint16_t reg;
134 1.1.4.2 rmind
135 1.1.4.2 rmind reg = _reg_read_2(SH4_PDTRA);
136 1.1.4.2 rmind if (onoff) {
137 1.1.4.2 rmind reg |= (1 << GPIO_PIN_RTC_SCLK);
138 1.1.4.2 rmind } else {
139 1.1.4.2 rmind reg &= ~(1 << GPIO_PIN_RTC_SCLK);
140 1.1.4.2 rmind }
141 1.1.4.2 rmind _reg_write_2(SH4_PDTRA, reg);
142 1.1.4.2 rmind }
143 1.1.4.2 rmind
144 1.1.4.2 rmind static void
145 1.1.4.2 rmind rtc_dir(struct rs5c313_softc *sc, int output)
146 1.1.4.2 rmind {
147 1.1.4.2 rmind uint32_t reg;
148 1.1.4.2 rmind
149 1.1.4.2 rmind reg = _reg_read_4(SH4_PCTRA);
150 1.1.4.2 rmind reg &= ~(3 << (GPIO_PIN_RTC_SIO * 2)); /* input */
151 1.1.4.2 rmind if (output) {
152 1.1.4.2 rmind reg |= (1 << (GPIO_PIN_RTC_SIO * 2)); /* output */
153 1.1.4.2 rmind }
154 1.1.4.2 rmind _reg_write_4(SH4_PCTRA, reg);
155 1.1.4.2 rmind }
156 1.1.4.2 rmind
157 1.1.4.2 rmind static int
158 1.1.4.2 rmind rtc_read(struct rs5c313_softc *sc)
159 1.1.4.2 rmind {
160 1.1.4.2 rmind int bit;
161 1.1.4.2 rmind
162 1.1.4.2 rmind ndelay(300);
163 1.1.4.2 rmind
164 1.1.4.2 rmind bit = (_reg_read_2(SH4_PDTRA) & (1 << GPIO_PIN_RTC_SIO)) ? 1 : 0;
165 1.1.4.2 rmind
166 1.1.4.2 rmind rtc_clk(sc, 0);
167 1.1.4.2 rmind ndelay(300);
168 1.1.4.2 rmind rtc_clk(sc, 1);
169 1.1.4.2 rmind
170 1.1.4.2 rmind return bit;
171 1.1.4.2 rmind }
172 1.1.4.2 rmind
173 1.1.4.2 rmind static void
174 1.1.4.2 rmind rtc_write(struct rs5c313_softc *sc, int bit)
175 1.1.4.2 rmind {
176 1.1.4.2 rmind uint16_t reg;
177 1.1.4.2 rmind
178 1.1.4.2 rmind reg = _reg_read_2(SH4_PDTRA);
179 1.1.4.2 rmind if (bit)
180 1.1.4.2 rmind reg |= (1 << GPIO_PIN_RTC_SIO);
181 1.1.4.2 rmind else
182 1.1.4.2 rmind reg &= ~(1 << GPIO_PIN_RTC_SIO);
183 1.1.4.2 rmind _reg_write_2(SH4_PDTRA, reg);
184 1.1.4.2 rmind
185 1.1.4.2 rmind ndelay(300);
186 1.1.4.2 rmind
187 1.1.4.2 rmind rtc_clk(sc, 0);
188 1.1.4.2 rmind ndelay(300);
189 1.1.4.2 rmind rtc_clk(sc, 1);
190 1.1.4.2 rmind }
191