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