1 /* $NetBSD: tx39spi.c,v 1.3.6.1 2005/11/10 13:56:27 skrll Exp $ */
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
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
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>
46 struct tx39spi_softc
{
48 tx_chipset_tag_t sc_tc
;
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
*,
56 static int tx39spi_print(void *, const char *);
58 static int tx39spi_intr(void *);
61 CFATTACH_DECL(tx39spi
, sizeof(struct tx39spi_softc
),
62 tx39spi_match
, tx39spi_attach
, NULL
, NULL
);
65 tx39spi_match(struct device
*parent
, struct cfdata
*cf
, void *aux
)
67 return (ATTACH_NORMAL
);
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
;
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
);
87 tx_intr_establish(tc
, MAKEINTR(5, TX39_INTRSTATUS5_SPI
),
88 IST_EDGE
, IPL_TTY
, tx39spi_intr
, sc
);
92 config_search_ia(tx39spi_search
, self
, "txspiif", tx39spi_print
);
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");
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
);
120 tx39spi_print(void *aux
, const char *pnp
)
122 struct txspi_attach_args
*sa
= aux
;
124 aprint_normal(" slot %d", sa
->sa_slot
);
138 tx39spi_is_empty(struct tx39spi_softc
*sc
)
140 return tx_conf_read(sc
->sc_tc
, TX39_SPICTRL_REG
) & (TX39_SPICTRL_EMPTY
);
144 tx39spi_put_word(struct tx39spi_softc
*sc
, int w
)
146 tx_chipset_tag_t tc
= sc
->sc_tc
;
148 while(!(tx_conf_read(tc
, TX39_INTRSTATUS5_REG
) & TX39_INTRSTATUS5_SPIBUFAVAILINT
))
150 tx_conf_write(tc
, TX39_INTRCLEAR5_REG
, TX39_INTRSTATUS5_SPIBUFAVAILINT
);
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
;
160 while(!(tx_conf_read(tc
, TX39_INTRSTATUS5_REG
) & TX39_INTRSTATUS5_SPIRCVINT
))
162 tx_conf_write(tc
, TX39_INTRCLEAR5_REG
, TX39_INTRSTATUS5_SPIRCVINT
);
164 return tx_conf_read(tc
, TX39_SPIRXHOLD_REG
) & 0xffff;
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
);
173 reg
|= (TX39_SPICTRL_ENSPI
);
175 reg
&= ~(TX39_SPICTRL_ENSPI
);
176 tx_conf_write(tc
, TX39_SPICTRL_REG
, reg
);
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
));
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
));
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
);
201 reg
|= (TX39_SPICTRL_WORD
);
203 reg
&= ~(TX39_SPICTRL_WORD
);
204 tx_conf_write(tc
, TX39_SPICTRL_REG
, reg
);
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
);
213 reg
|= (TX39_SPICTRL_PHAPOL
);
215 reg
&= ~(TX39_SPICTRL_PHAPOL
);
216 tx_conf_write(tc
, TX39_SPICTRL_REG
, reg
);
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
);
225 reg
|= (TX39_SPICTRL_CLKPOL
);
227 reg
&= ~(TX39_SPICTRL_CLKPOL
);
228 tx_conf_write(tc
, TX39_SPICTRL_REG
, reg
);
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
);
237 reg
|= (TX39_SPICTRL_LSB
);
239 reg
&= ~(TX39_SPICTRL_LSB
);
240 tx_conf_write(tc
, TX39_SPICTRL_REG
, reg
);