tx39spi.c revision 1.2 1 1.2 drochner /* $NetBSD: tx39spi.c,v 1.2 2005/06/30 17:03:53 drochner Exp $ */
2 1.1 hamajima
3 1.1 hamajima /*-
4 1.1 hamajima * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
5 1.1 hamajima *
6 1.1 hamajima * Redistribution and use in source and binary forms, with or without
7 1.1 hamajima * modification, are permitted provided that the following conditions
8 1.1 hamajima * are met:
9 1.1 hamajima * 1. Redistributions of source code must retain the above copyright
10 1.1 hamajima * notice, this list of conditions and the following disclaimer.
11 1.1 hamajima * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 hamajima * notice, this list of conditions and the following disclaimer in the
13 1.1 hamajima * documentation and/or other materials provided with the distribution.
14 1.1 hamajima *
15 1.1 hamajima * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 hamajima * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 hamajima * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 hamajima * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 hamajima * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 hamajima * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 hamajima * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 hamajima * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 hamajima * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 hamajima * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 hamajima * SUCH DAMAGE.
26 1.1 hamajima */
27 1.1 hamajima
28 1.1 hamajima /*
29 1.1 hamajima * Toshiba TX3912/3922 SPI module
30 1.1 hamajima */
31 1.1 hamajima
32 1.1 hamajima #include <sys/cdefs.h>
33 1.1 hamajima
34 1.2 drochner __KERNEL_RCSID(0, "$NetBSD: tx39spi.c,v 1.2 2005/06/30 17:03:53 drochner Exp $");
35 1.1 hamajima
36 1.1 hamajima #include <sys/param.h>
37 1.1 hamajima #include <sys/systm.h>
38 1.1 hamajima #include <sys/device.h>
39 1.1 hamajima #include <hpcmips/tx/tx39var.h>
40 1.1 hamajima #include <hpcmips/tx/tx39spivar.h>
41 1.1 hamajima #include <hpcmips/tx/tx39spireg.h>
42 1.1 hamajima #include <hpcmips/tx/tx39icureg.h>
43 1.1 hamajima
44 1.1 hamajima #include "locators.h"
45 1.1 hamajima
46 1.1 hamajima struct tx39spi_softc {
47 1.1 hamajima struct device sc_dev;
48 1.1 hamajima tx_chipset_tag_t sc_tc;
49 1.1 hamajima int sc_attached;
50 1.1 hamajima };
51 1.1 hamajima
52 1.1 hamajima static int tx39spi_match(struct device *, struct cfdata *, void *);
53 1.1 hamajima static void tx39spi_attach(struct device *, struct device *, void *);
54 1.2 drochner static int tx39spi_search(struct device *, struct cfdata *,
55 1.2 drochner const locdesc_t *, void *);
56 1.1 hamajima static int tx39spi_print(void *, const char *);
57 1.1 hamajima #ifndef USE_POLL
58 1.1 hamajima static int tx39spi_intr(void *);
59 1.1 hamajima #endif
60 1.1 hamajima
61 1.1 hamajima CFATTACH_DECL(tx39spi, sizeof(struct tx39spi_softc),
62 1.1 hamajima tx39spi_match, tx39spi_attach, NULL, NULL);
63 1.1 hamajima
64 1.1 hamajima int
65 1.1 hamajima tx39spi_match(struct device *parent, struct cfdata *cf, void *aux)
66 1.1 hamajima {
67 1.1 hamajima return (ATTACH_NORMAL);
68 1.1 hamajima }
69 1.1 hamajima
70 1.1 hamajima void
71 1.1 hamajima tx39spi_attach(struct device *parent, struct device *self, void *aux)
72 1.1 hamajima {
73 1.1 hamajima struct txsim_attach_args *ta = aux;
74 1.1 hamajima struct tx39spi_softc *sc = (void*)self;
75 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc = ta->ta_tc;
76 1.1 hamajima txreg_t reg;
77 1.1 hamajima
78 1.1 hamajima reg = tx_conf_read(tc, TX39_SPICTRL_REG);
79 1.1 hamajima reg &= ~(TX39_SPICTRL_ENSPI);
80 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, reg);
81 1.1 hamajima tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIBUFAVAILINT);
82 1.1 hamajima tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIERRINT);
83 1.1 hamajima tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIRCVINT);
84 1.1 hamajima tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIEMPTYINT);
85 1.1 hamajima
86 1.1 hamajima #ifndef USE_POLL
87 1.1 hamajima tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_SPI),
88 1.1 hamajima IST_EDGE, IPL_TTY, tx39spi_intr, sc);
89 1.1 hamajima #endif
90 1.1 hamajima printf("\n");
91 1.1 hamajima
92 1.2 drochner config_search_ia(tx39spi_search, self, "txspiif", tx39spi_print);
93 1.1 hamajima }
94 1.1 hamajima
95 1.1 hamajima int
96 1.2 drochner tx39spi_search(struct device *parent, struct cfdata *cf,
97 1.2 drochner const locdesc_t *ldesc, void *aux)
98 1.1 hamajima {
99 1.1 hamajima struct tx39spi_softc *sc = (void*)parent;
100 1.1 hamajima struct txspi_attach_args sa;
101 1.1 hamajima
102 1.1 hamajima sa.sa_tc = sc->sc_tc;
103 1.1 hamajima sa.sa_slot = cf->cf_loc[TXSPIIFCF_SLOT];
104 1.1 hamajima
105 1.1 hamajima if (sa.sa_slot == TXSPIIFCF_SLOT_DEFAULT) {
106 1.1 hamajima printf("tx39spi_search: wildcarded slot, skipping\n");
107 1.1 hamajima return 0;
108 1.1 hamajima }
109 1.1 hamajima
110 1.1 hamajima if (!(sc->sc_attached & (1 << sa.sa_slot)) && /* not attached slot */
111 1.1 hamajima config_match(parent, cf, &sa)) {
112 1.1 hamajima config_attach(parent, cf, &sa, tx39spi_print);
113 1.1 hamajima sc->sc_attached |= (1 << sa.sa_slot);
114 1.1 hamajima }
115 1.1 hamajima
116 1.1 hamajima return 0;
117 1.1 hamajima }
118 1.1 hamajima
119 1.1 hamajima int
120 1.1 hamajima tx39spi_print(void *aux, const char *pnp)
121 1.1 hamajima {
122 1.1 hamajima struct txspi_attach_args *sa = aux;
123 1.1 hamajima
124 1.1 hamajima aprint_normal(" slot %d", sa->sa_slot);
125 1.1 hamajima
126 1.1 hamajima return (QUIET);
127 1.1 hamajima }
128 1.1 hamajima
129 1.1 hamajima #ifndef USE_POLL
130 1.1 hamajima int
131 1.1 hamajima tx39spi_intr(void *)
132 1.1 hamajima {
133 1.1 hamajima return 0;
134 1.1 hamajima }
135 1.1 hamajima #endif
136 1.1 hamajima
137 1.1 hamajima int
138 1.1 hamajima tx39spi_is_empty(struct tx39spi_softc *sc)
139 1.1 hamajima {
140 1.1 hamajima return tx_conf_read(sc->sc_tc, TX39_SPICTRL_REG) & (TX39_SPICTRL_EMPTY);
141 1.1 hamajima }
142 1.1 hamajima
143 1.1 hamajima void
144 1.1 hamajima tx39spi_put_word(struct tx39spi_softc *sc, int w)
145 1.1 hamajima {
146 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
147 1.1 hamajima #ifdef USE_POLL
148 1.1 hamajima while(!(tx_conf_read(tc, TX39_INTRSTATUS5_REG) & TX39_INTRSTATUS5_SPIBUFAVAILINT))
149 1.1 hamajima ;
150 1.1 hamajima tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIBUFAVAILINT);
151 1.1 hamajima #endif
152 1.1 hamajima tx_conf_write(tc, TX39_SPITXHOLD_REG , w & 0xffff);
153 1.1 hamajima }
154 1.1 hamajima
155 1.1 hamajima int
156 1.1 hamajima tx39spi_get_word(struct tx39spi_softc *sc)
157 1.1 hamajima {
158 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
159 1.1 hamajima #ifdef USE_POLL
160 1.1 hamajima while(!(tx_conf_read(tc, TX39_INTRSTATUS5_REG) & TX39_INTRSTATUS5_SPIRCVINT))
161 1.1 hamajima ;
162 1.1 hamajima tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIRCVINT);
163 1.1 hamajima #endif
164 1.1 hamajima return tx_conf_read(tc, TX39_SPIRXHOLD_REG) & 0xffff;
165 1.1 hamajima }
166 1.1 hamajima
167 1.1 hamajima void
168 1.1 hamajima tx39spi_enable(struct tx39spi_softc *sc, int n)
169 1.1 hamajima {
170 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
171 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
172 1.1 hamajima if (n)
173 1.1 hamajima reg |= (TX39_SPICTRL_ENSPI);
174 1.1 hamajima else
175 1.1 hamajima reg &= ~(TX39_SPICTRL_ENSPI);
176 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, reg);
177 1.1 hamajima }
178 1.1 hamajima
179 1.1 hamajima void
180 1.1 hamajima tx39spi_delayval(struct tx39spi_softc *sc, int n)
181 1.1 hamajima {
182 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
183 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
184 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, TX39_SPICTRL_DELAYVAL_SET(reg, n));
185 1.1 hamajima }
186 1.1 hamajima
187 1.1 hamajima void
188 1.1 hamajima tx39spi_baudrate(struct tx39spi_softc *sc, int n)
189 1.1 hamajima {
190 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
191 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
192 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, TX39_SPICTRL_BAUDRATE_SET(reg, n));
193 1.1 hamajima }
194 1.1 hamajima
195 1.1 hamajima void
196 1.1 hamajima tx39spi_word(struct tx39spi_softc *sc, int n)
197 1.1 hamajima {
198 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
199 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
200 1.1 hamajima if (n)
201 1.1 hamajima reg |= (TX39_SPICTRL_WORD);
202 1.1 hamajima else
203 1.1 hamajima reg &= ~(TX39_SPICTRL_WORD);
204 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, reg);
205 1.1 hamajima }
206 1.1 hamajima
207 1.1 hamajima void
208 1.1 hamajima tx39spi_phapol(struct tx39spi_softc *sc, int n)
209 1.1 hamajima {
210 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
211 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
212 1.1 hamajima if (n)
213 1.1 hamajima reg |= (TX39_SPICTRL_PHAPOL);
214 1.1 hamajima else
215 1.1 hamajima reg &= ~(TX39_SPICTRL_PHAPOL);
216 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, reg);
217 1.1 hamajima }
218 1.1 hamajima
219 1.1 hamajima void
220 1.1 hamajima tx39spi_clkpol(struct tx39spi_softc *sc, int n)
221 1.1 hamajima {
222 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
223 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
224 1.1 hamajima if (n)
225 1.1 hamajima reg |= (TX39_SPICTRL_CLKPOL);
226 1.1 hamajima else
227 1.1 hamajima reg &= ~(TX39_SPICTRL_CLKPOL);
228 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, reg);
229 1.1 hamajima }
230 1.1 hamajima
231 1.1 hamajima void
232 1.1 hamajima tx39spi_lsb(struct tx39spi_softc *sc, int n)
233 1.1 hamajima {
234 1.1 hamajima tx_chipset_tag_t tc = sc->sc_tc;
235 1.1 hamajima txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
236 1.1 hamajima if (n)
237 1.1 hamajima reg |= (TX39_SPICTRL_LSB);
238 1.1 hamajima else
239 1.1 hamajima reg &= ~(TX39_SPICTRL_LSB);
240 1.1 hamajima tx_conf_write(tc, TX39_SPICTRL_REG, reg);
241 1.1 hamajima }
242