1/*	$NetBSD: tx39spi.c,v 1.3.6.1 2005/11/10 13:56:27 skrll 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.3.6.1 2005/11/10 13:56:27 skrll 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
46struct tx39spi_softc {
47	struct device sc_dev;
48	tx_chipset_tag_t sc_tc;
49	int sc_attached;
50};
51
52static int tx39spi_match(struct device *, struct cfdata *, void *);
53static void tx39spi_attach(struct device *, struct device *, void *);
54static int tx39spi_search(struct device *, struct cfdata *,
55			  const int *, void *);
56static int tx39spi_print(void *, const char *);
57#ifndef USE_POLL
58static int tx39spi_intr(void *);
59#endif
60
61CFATTACH_DECL(tx39spi, sizeof(struct tx39spi_softc),
62    tx39spi_match, tx39spi_attach, NULL, NULL);
63
64int
65tx39spi_match(struct device *parent, struct cfdata *cf, void *aux)
66{
67	return (ATTACH_NORMAL);
68}
69
70void
71tx39spi_attach(struct device *parent, struct device *self, void *aux)
72{
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;
77
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);
85
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");
91
92	config_search_ia(tx39spi_search, self, "txspiif", tx39spi_print);
93}
94
95int
96tx39spi_search(struct device *parent, struct cfdata *cf,
97	       const int *ldesc, void *aux)
98{
99	struct tx39spi_softc *sc = (void*)parent;
100	struct txspi_attach_args sa;
101
102	sa.sa_tc = sc->sc_tc;
103	sa.sa_slot = cf->cf_loc[TXSPIIFCF_SLOT];
104
105	if (sa.sa_slot == TXSPIIFCF_SLOT_DEFAULT) {
106		printf("tx39spi_search: wildcarded slot, skipping\n");
107		return 0;
108	}
109
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);
114	}
115
116	return 0;
117}
118
119int
120tx39spi_print(void *aux, const char *pnp)
121{
122	struct txspi_attach_args *sa = aux;
123
124	aprint_normal(" slot %d", sa->sa_slot);
125
126	return (QUIET);
127}
128
129#ifndef USE_POLL
130int
131tx39spi_intr(void *)
132{
133	return 0;
134}
135#endif
136
137int
138tx39spi_is_empty(struct tx39spi_softc *sc)
139{
140	return tx_conf_read(sc->sc_tc, TX39_SPICTRL_REG) & (TX39_SPICTRL_EMPTY);
141}
142
143void
144tx39spi_put_word(struct tx39spi_softc *sc, int w)
145{
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))
149		;
150	tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIBUFAVAILINT);
151#endif
152	tx_conf_write(tc, TX39_SPITXHOLD_REG , w & 0xffff);
153}
154
155int
156tx39spi_get_word(struct tx39spi_softc *sc)
157{
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))
161		;
162	tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIRCVINT);
163#endif
164	return tx_conf_read(tc, TX39_SPIRXHOLD_REG) & 0xffff;
165}
166
167void
168tx39spi_enable(struct tx39spi_softc *sc, int n)
169{
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);
177}
178
179void
180tx39spi_delayval(struct tx39spi_softc *sc, int n)
181{
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));
185}
186
187void
188tx39spi_baudrate(struct tx39spi_softc *sc, int n)
189{
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));
193}
194
195void
196tx39spi_word(struct tx39spi_softc *sc, int n)
197{
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);
205}
206
207void
208tx39spi_phapol(struct tx39spi_softc *sc, int n)
209{
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);
217}
218
219void
220tx39spi_clkpol(struct tx39spi_softc *sc, int n)
221{
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);
229}
230
231void
232tx39spi_lsb(struct tx39spi_softc *sc, int n)
233{
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);
241}
242