Home | History | Annotate | Line # | Download | only in tx
      1 /*	$NetBSD: tx39spi.c,v 1.7 2021/08/07 16:18:54 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.7 2021/08/07 16:18:54 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 	    CFARGS(.search = tx39spi_search));
     92 }
     93 
     94 int
     95 tx39spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
     96 {
     97 	struct tx39spi_softc *sc = device_private(parent);
     98 	struct txspi_attach_args sa;
     99 
    100 	sa.sa_tc = sc->sc_tc;
    101 	sa.sa_slot = cf->cf_loc[TXSPIIFCF_SLOT];
    102 
    103 	if (sa.sa_slot == TXSPIIFCF_SLOT_DEFAULT) {
    104 		printf("tx39spi_search: wildcarded slot, skipping\n");
    105 		return 0;
    106 	}
    107 
    108 	if (!(sc->sc_attached & (1 << sa.sa_slot)) && /* not attached slot */
    109 	    config_probe(parent, cf, &sa)) {
    110 		config_attach(parent, cf, &sa, tx39spi_print, CFARGS_NONE);
    111 		sc->sc_attached |= (1 << sa.sa_slot);
    112 	}
    113 
    114 	return 0;
    115 }
    116 
    117 int
    118 tx39spi_print(void *aux, const char *pnp)
    119 {
    120 	struct txspi_attach_args *sa = aux;
    121 
    122 	aprint_normal(" slot %d", sa->sa_slot);
    123 
    124 	return (QUIET);
    125 }
    126 
    127 #ifndef USE_POLL
    128 int
    129 tx39spi_intr(void *)
    130 {
    131 	return 0;
    132 }
    133 #endif
    134 
    135 int
    136 tx39spi_is_empty(struct tx39spi_softc *sc)
    137 {
    138 	return tx_conf_read(sc->sc_tc, TX39_SPICTRL_REG) & (TX39_SPICTRL_EMPTY);
    139 }
    140 
    141 void
    142 tx39spi_put_word(struct tx39spi_softc *sc, int w)
    143 {
    144 	tx_chipset_tag_t tc = sc->sc_tc;
    145 #ifdef USE_POLL
    146 	while(!(tx_conf_read(tc, TX39_INTRSTATUS5_REG) & TX39_INTRSTATUS5_SPIBUFAVAILINT))
    147 		;
    148 	tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIBUFAVAILINT);
    149 #endif
    150 	tx_conf_write(tc, TX39_SPITXHOLD_REG , w & 0xffff);
    151 }
    152 
    153 int
    154 tx39spi_get_word(struct tx39spi_softc *sc)
    155 {
    156 	tx_chipset_tag_t tc = sc->sc_tc;
    157 #ifdef USE_POLL
    158 	while(!(tx_conf_read(tc, TX39_INTRSTATUS5_REG) & TX39_INTRSTATUS5_SPIRCVINT))
    159 		;
    160 	tx_conf_write(tc, TX39_INTRCLEAR5_REG, TX39_INTRSTATUS5_SPIRCVINT);
    161 #endif
    162 	return tx_conf_read(tc, TX39_SPIRXHOLD_REG) & 0xffff;
    163 }
    164 
    165 void
    166 tx39spi_enable(struct tx39spi_softc *sc, int n)
    167 {
    168 	tx_chipset_tag_t tc = sc->sc_tc;
    169 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    170 	if (n)
    171 		reg |= (TX39_SPICTRL_ENSPI);
    172 	else
    173 		reg &= ~(TX39_SPICTRL_ENSPI);
    174 	tx_conf_write(tc, TX39_SPICTRL_REG, reg);
    175 }
    176 
    177 void
    178 tx39spi_delayval(struct tx39spi_softc *sc, int n)
    179 {
    180 	tx_chipset_tag_t tc = sc->sc_tc;
    181 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    182 	tx_conf_write(tc, TX39_SPICTRL_REG, TX39_SPICTRL_DELAYVAL_SET(reg, n));
    183 }
    184 
    185 void
    186 tx39spi_baudrate(struct tx39spi_softc *sc, int n)
    187 {
    188 	tx_chipset_tag_t tc = sc->sc_tc;
    189 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    190 	tx_conf_write(tc, TX39_SPICTRL_REG, TX39_SPICTRL_BAUDRATE_SET(reg, n));
    191 }
    192 
    193 void
    194 tx39spi_word(struct tx39spi_softc *sc, int n)
    195 {
    196 	tx_chipset_tag_t tc = sc->sc_tc;
    197 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    198 	if (n)
    199 		reg |= (TX39_SPICTRL_WORD);
    200 	else
    201 		reg &= ~(TX39_SPICTRL_WORD);
    202 	tx_conf_write(tc, TX39_SPICTRL_REG, reg);
    203 }
    204 
    205 void
    206 tx39spi_phapol(struct tx39spi_softc *sc, int n)
    207 {
    208 	tx_chipset_tag_t tc = sc->sc_tc;
    209 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    210 	if (n)
    211 		reg |= (TX39_SPICTRL_PHAPOL);
    212 	else
    213 		reg &= ~(TX39_SPICTRL_PHAPOL);
    214 	tx_conf_write(tc, TX39_SPICTRL_REG, reg);
    215 }
    216 
    217 void
    218 tx39spi_clkpol(struct tx39spi_softc *sc, int n)
    219 {
    220 	tx_chipset_tag_t tc = sc->sc_tc;
    221 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    222 	if (n)
    223 		reg |= (TX39_SPICTRL_CLKPOL);
    224 	else
    225 		reg &= ~(TX39_SPICTRL_CLKPOL);
    226 	tx_conf_write(tc, TX39_SPICTRL_REG, reg);
    227 }
    228 
    229 void
    230 tx39spi_lsb(struct tx39spi_softc *sc, int n)
    231 {
    232 	tx_chipset_tag_t tc = sc->sc_tc;
    233 	txreg_t reg = tx_conf_read(tc, TX39_SPICTRL_REG);
    234 	if (n)
    235 		reg |= (TX39_SPICTRL_LSB);
    236 	else
    237 		reg &= ~(TX39_SPICTRL_LSB);
    238 	tx_conf_write(tc, TX39_SPICTRL_REG, reg);
    239 }
    240