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