Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / hpcmips / tx / tx39spi.c
blob9e33a18d3d33b9517f3c3331d988c89cde3ca93b
1 /* $NetBSD: tx39spi.c,v 1.3.6.1 2005/11/10 13:56:27 skrll Exp $ */
3 /*-
4 * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
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.
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.
29 * Toshiba TX3912/3922 SPI module
32 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: tx39spi.c,v 1.3.6.1 2005/11/10 13:56:27 skrll Exp $");
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>
44 #include "locators.h"
46 struct tx39spi_softc {
47 struct device sc_dev;
48 tx_chipset_tag_t sc_tc;
49 int sc_attached;
52 static int tx39spi_match(struct device *, struct cfdata *, void *);
53 static void tx39spi_attach(struct device *, struct device *, void *);
54 static int tx39spi_search(struct device *, struct cfdata *,
55 const int *, void *);
56 static int tx39spi_print(void *, const char *);
57 #ifndef USE_POLL
58 static int tx39spi_intr(void *);
59 #endif
61 CFATTACH_DECL(tx39spi, sizeof(struct tx39spi_softc),
62 tx39spi_match, tx39spi_attach, NULL, NULL);
64 int
65 tx39spi_match(struct device *parent, struct cfdata *cf, void *aux)
67 return (ATTACH_NORMAL);
70 void
71 tx39spi_attach(struct device *parent, struct device *self, void *aux)
73 struct txsim_attach_args *ta = aux;
74 struct tx39spi_softc *sc = (void*)self;
75 tx_chipset_tag_t tc = sc->sc_tc = ta->ta_tc;
76 txreg_t reg;
78 reg = tx_conf_read(tc, TX39_SPICTRL_REG);
79 reg &= ~(TX39_SPICTRL_ENSPI);
80 tx_conf_write(tc, TX39_SPICTRL_REG, reg);
81 tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIBUFAVAILINT);
82 tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIERRINT);
83 tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIRCVINT);
84 tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIEMPTYINT);
86 #ifndef USE_POLL
87 tx_intr_establish(tc, MAKEINTR(5, TX39_INTRSTATUS5_SPI),
88 IST_EDGE, IPL_TTY, tx39spi_intr, sc);
89 #endif
90 printf("\n");
92 config_search_ia(tx39spi_search, self, "txspiif", tx39spi_print);
95 int
96 tx39spi_search(struct device *parent, struct cfdata *cf,
97 const int *ldesc, void *aux)
99 struct tx39spi_softc *sc = (void*)parent;
100 struct txspi_attach_args sa;
102 sa.sa_tc = sc->sc_tc;
103 sa.sa_slot = cf->cf_loc[TXSPIIFCF_SLOT];
105 if (sa.sa_slot == TXSPIIFCF_SLOT_DEFAULT) {
106 printf("tx39spi_search: wildcarded slot, skipping\n");
107 return 0;
110 if (!(sc->sc_attached & (1 << sa.sa_slot)) && /* not attached slot */
111 config_match(parent, cf, &sa)) {
112 config_attach(parent, cf, &sa, tx39spi_print);
113 sc->sc_attached |= (1 << sa.sa_slot);
116 return 0;
120 tx39spi_print(void *aux, const char *pnp)
122 struct txspi_attach_args *sa = aux;
124 aprint_normal(" slot %d", sa->sa_slot);
126 return (QUIET);
129 #ifndef USE_POLL
131 tx39spi_intr(void *)
133 return 0;
135 #endif
138 tx39spi_is_empty(struct tx39spi_softc *sc)
140 return tx_conf_read(sc->sc_tc, TX39_SPICTRL_REG) & (TX39_SPICTRL_EMPTY);
143 void
144 tx39spi_put_word(struct tx39spi_softc *sc, int w)
146 tx_chipset_tag_t tc = sc->sc_tc;
147 #ifdef USE_POLL
148 while(!(tx_conf_read(tc, TX39_INTRSTATUS5_REG) & TX39_INTRSTATUS5_SPIBUFAVAILINT))
150 tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIBUFAVAILINT);
151 #endif
152 tx_conf_write(tc, TX39_SPITXHOLD_REG , w & 0xffff);
156 tx39spi_get_word(struct tx39spi_softc *sc)
158 tx_chipset_tag_t tc = sc->sc_tc;
159 #ifdef USE_POLL
160 while(!(tx_conf_read(tc, TX39_INTRSTATUS5_REG) & TX39_INTRSTATUS5_SPIRCVINT))
162 tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIRCVINT);
163 #endif
164 return tx_conf_read(tc, TX39_SPIRXHOLD_REG) & 0xffff;
167 void
168 tx39spi_enable(struct tx39spi_softc *sc, int n)
170 tx_chipset_tag_t tc = sc->sc_tc;
171 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
172 if (n)
173 reg |= (TX39_SPICTRL_ENSPI);
174 else
175 reg &= ~(TX39_SPICTRL_ENSPI);
176 tx_conf_write(tc, TX39_SPICTRL_REG, reg);
179 void
180 tx39spi_delayval(struct tx39spi_softc *sc, int n)
182 tx_chipset_tag_t tc = sc->sc_tc;
183 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
184 tx_conf_write(tc, TX39_SPICTRL_REG, TX39_SPICTRL_DELAYVAL_SET(reg, n));
187 void
188 tx39spi_baudrate(struct tx39spi_softc *sc, int n)
190 tx_chipset_tag_t tc = sc->sc_tc;
191 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
192 tx_conf_write(tc, TX39_SPICTRL_REG, TX39_SPICTRL_BAUDRATE_SET(reg, n));
195 void
196 tx39spi_word(struct tx39spi_softc *sc, int n)
198 tx_chipset_tag_t tc = sc->sc_tc;
199 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
200 if (n)
201 reg |= (TX39_SPICTRL_WORD);
202 else
203 reg &= ~(TX39_SPICTRL_WORD);
204 tx_conf_write(tc, TX39_SPICTRL_REG, reg);
207 void
208 tx39spi_phapol(struct tx39spi_softc *sc, int n)
210 tx_chipset_tag_t tc = sc->sc_tc;
211 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
212 if (n)
213 reg |= (TX39_SPICTRL_PHAPOL);
214 else
215 reg &= ~(TX39_SPICTRL_PHAPOL);
216 tx_conf_write(tc, TX39_SPICTRL_REG, reg);
219 void
220 tx39spi_clkpol(struct tx39spi_softc *sc, int n)
222 tx_chipset_tag_t tc = sc->sc_tc;
223 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
224 if (n)
225 reg |= (TX39_SPICTRL_CLKPOL);
226 else
227 reg &= ~(TX39_SPICTRL_CLKPOL);
228 tx_conf_write(tc, TX39_SPICTRL_REG, reg);
231 void
232 tx39spi_lsb(struct tx39spi_softc *sc, int n)
234 tx_chipset_tag_t tc = sc->sc_tc;
235 txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
236 if (n)
237 reg |= (TX39_SPICTRL_LSB);
238 else
239 reg &= ~(TX39_SPICTRL_LSB);
240 tx_conf_write(tc, TX39_SPICTRL_REG, reg);