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