Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2410_spi.c revision 1.7
      1 /* $NetBSD: s3c2410_spi.c,v 1.7 2012/10/27 17:17:40 chs Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004  Genetec Corporation.  All rights reserved.
      5  * Written by Hiroyuki Bessho for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of Genetec Corporation may not be used to endorse or
     16  *    promote products derived from this software without specific prior
     17  *    written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Support S3C2410's SPI dirver.
     34  * Real works are done by drivers attached to SPI ports.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: s3c2410_spi.c,v 1.7 2012/10/27 17:17:40 chs Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/conf.h>
     43 
     44 #include <sys/bus.h>
     45 #include <machine/cpu.h>
     46 
     47 #include <arm/s3c2xx0/s3c24x0var.h>
     48 #include <arm/s3c2xx0/s3c24x0reg.h>
     49 #include <arm/s3c2xx0/s3c2410reg.h>
     50 
     51 #include <arm/s3c2xx0/s3c24x0_spi.h>
     52 
     53 #include "locators.h"
     54 
     55 struct ssspi_softc {
     56 	bus_space_tag_t    iot;
     57 	bus_space_handle_t ioh;
     58 	short	index;
     59 };
     60 
     61 
     62 /* prototypes */
     63 static int	ssspi_match(device_t, cfdata_t, void *);
     64 static void	ssspi_attach(device_t, device_t, void *);
     65 static int 	ssspi_search(device_t, cfdata_t, const int *, void *);
     66 static int	ssspi_print(void *, const char *);
     67 
     68 /* attach structures */
     69 CFATTACH_DECL_NEW(ssspi, sizeof(struct ssspi_softc), ssspi_match, ssspi_attach,
     70     NULL, NULL);
     71 
     72 
     73 static int
     74 ssspi_print(void *aux, const char *name)
     75 {
     76 	struct ssspi_attach_args *spia = aux;
     77 
     78 	if (spia->spia_aux_intr != SSSPICF_INTR_DEFAULT)
     79 		printf(" intr %d", spia->spia_aux_intr);
     80         return (UNCONF);
     81 }
     82 
     83 int
     84 ssspi_match(device_t parent, cfdata_t match, void *aux)
     85 {
     86 	struct s3c2xx0_attach_args *sa = aux;
     87 
     88 	/* S3C2410 have only two SPIs */
     89 	switch (sa->sa_index) {
     90 	case 0:
     91 	case 1:
     92 		break;
     93 	default:
     94 		return 0;
     95 	}
     96 
     97 	return 1;
     98 }
     99 
    100 void
    101 ssspi_attach(device_t parent, device_t self, void *aux)
    102 {
    103 	struct ssspi_softc *sc = device_private(self);
    104 	struct s3c2xx0_attach_args *sa = aux;
    105 	bus_space_tag_t iot = sa->sa_iot;
    106 
    107 	static bus_space_handle_t spi_ioh = 0;
    108 
    109 	/* we map all registers for SPI0 and SPI1 at once, then
    110 	   use subregions */
    111 	if (spi_ioh == 0) {
    112 		if (bus_space_map(iot, S3C2410_SPI0_BASE,
    113 				  2 * S3C24X0_SPI_SIZE,
    114 				  0, &spi_ioh)) {
    115 			aprint_error(": can't map registers\n");
    116 			return;
    117 		}
    118 	}
    119 
    120 	aprint_normal("\n");
    121 
    122 	sc->index = sa->sa_index;
    123 	sc->iot = iot;
    124 
    125 	bus_space_subregion(iot, spi_ioh, sc->index == 0 ? 0 : S3C24X0_SPI_SIZE,
    126 	    S3C24X0_SPI_SIZE, &sc->ioh);
    127 
    128 	/*
    129 	 *  Attach child devices
    130 	 */
    131 	config_search_ia(ssspi_search, self, "ssspi", NULL);
    132 }
    133 
    134 int
    135 ssspi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    136 {
    137 	struct ssspi_softc *sc = device_private(parent);
    138 	struct ssspi_attach_args spia;
    139 	static const unsigned char intr[] = { S3C24X0_INT_SPI0,
    140 					      S3C2410_INT_SPI1 };
    141 
    142 	KASSERT(sc->index == 0 || sc->index == 1);
    143 
    144 	spia.spia_iot = sc->iot;
    145 	spia.spia_ioh = sc->ioh;
    146 	spia.spia_gpioh = s3c2xx0_softc->sc_gpio_ioh;
    147 	spia.spia_index = sc->index;
    148 	spia.spia_intr = intr[sc->index];
    149 	spia.spia_aux_intr = cf->cf_loc[SSSPICF_INTR];
    150 	spia.spia_dmat = s3c2xx0_softc->sc_dmat;
    151 
    152         if (config_match(parent, cf, &spia))
    153                 config_attach(parent, cf, &spia, ssspi_print);
    154 
    155         return 0;
    156 }
    157 
    158 /*
    159  * Intiialze SPI port. called by child devices.
    160  */
    161 int
    162 s3c24x0_spi_setup(struct ssspi_softc *sc, uint32_t mode, int bps, int use_ss)
    163 {
    164 	int pclk = s3c2xx0_softc->sc_pclk;
    165 	int prescaler;
    166 	uint32_t pgcon, pecon;
    167 	bus_space_handle_t gpioh = s3c2xx0_softc->sc_gpio_ioh;
    168 	bus_space_tag_t iot = sc->iot;
    169 
    170 	if (bps > 1) {
    171 		prescaler = pclk / 2 / bps - 1;
    172 
    173 		if (prescaler <= 0 || 0xff < prescaler)
    174 			return -1;
    175 		bus_space_write_1(sc->iot, sc->ioh, SPI_SPPRE, prescaler);
    176 	}
    177 
    178 
    179 	if (sc->index == 0) {
    180 		pecon = bus_space_read_4(iot, gpioh, GPIO_PECON);
    181 
    182 		if (use_ss) {
    183 			pgcon = bus_space_read_4(iot, gpioh, GPIO_PGCON);
    184 			pgcon = GPIO_SET_FUNC(pgcon, 2, PCON_ALTFUN2);
    185 			bus_space_write_4(iot, gpioh, GPIO_PGCON, pgcon);
    186 		}
    187 
    188 		pecon = GPIO_SET_FUNC(pecon, 11, PCON_ALTFUN2); /* SPIMISO0 */
    189 		pecon = GPIO_SET_FUNC(pecon, 12, PCON_ALTFUN2); /* SPIMOSI0 */
    190 		pecon = GPIO_SET_FUNC(pecon, 13, PCON_ALTFUN2); /* SPICL0 */
    191 
    192 		bus_space_write_4(iot, gpioh, GPIO_PECON, pecon);
    193 	}
    194 	else {
    195 		pgcon = bus_space_read_4(iot, gpioh, GPIO_PGCON);
    196 
    197 		if (use_ss)
    198 			pgcon = GPIO_SET_FUNC(pgcon, 3, PCON_ALTFUN2);
    199 
    200 		pgcon = GPIO_SET_FUNC(pgcon, 5, PCON_ALTFUN2); /* SPIMISO1 */
    201 		pgcon = GPIO_SET_FUNC(pgcon, 6, PCON_ALTFUN2); /* SPIMOSI1 */
    202 		pgcon = GPIO_SET_FUNC(pgcon, 7, PCON_ALTFUN2); /* SPICLK1 */
    203 
    204 		bus_space_write_4(iot, gpioh, GPIO_PGCON, pgcon);
    205 	}
    206 
    207 	bus_space_write_4(iot, sc->ioh, SPI_SPCON, mode);
    208 
    209 	return 0;
    210 }
    211