2021-08-07 19:18:40 +03:00
|
|
|
/* $NetBSD: s3c2410_spi.c,v 1.9 2021/08/07 16:18:45 thorpej Exp $ */
|
2004-02-14 10:19:51 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Genetec Corporation. All rights reserved.
|
|
|
|
* Written by Hiroyuki Bessho for Genetec Corporation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of Genetec Corporation may not be used to endorse or
|
|
|
|
* promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Support S3C2410's SPI dirver.
|
|
|
|
* Real works are done by drivers attached to SPI ports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2021-08-07 19:18:40 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: s3c2410_spi.c,v 1.9 2021/08/07 16:18:45 thorpej Exp $");
|
2004-02-14 10:19:51 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
|
2011-07-02 00:26:35 +04:00
|
|
|
#include <sys/bus.h>
|
2004-02-14 10:19:51 +03:00
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
|
|
|
#include <arm/s3c2xx0/s3c24x0var.h>
|
|
|
|
#include <arm/s3c2xx0/s3c24x0reg.h>
|
|
|
|
#include <arm/s3c2xx0/s3c2410reg.h>
|
|
|
|
|
|
|
|
#include <arm/s3c2xx0/s3c24x0_spi.h>
|
|
|
|
|
|
|
|
#include "locators.h"
|
|
|
|
|
|
|
|
struct ssspi_softc {
|
|
|
|
bus_space_tag_t iot;
|
|
|
|
bus_space_handle_t ioh;
|
|
|
|
short index;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* prototypes */
|
2012-10-27 21:17:22 +04:00
|
|
|
static int ssspi_match(device_t, cfdata_t, void *);
|
|
|
|
static void ssspi_attach(device_t, device_t, void *);
|
|
|
|
static int ssspi_search(device_t, cfdata_t, const int *, void *);
|
2004-02-14 10:19:51 +03:00
|
|
|
static int ssspi_print(void *, const char *);
|
|
|
|
|
|
|
|
/* attach structures */
|
2012-10-27 21:17:22 +04:00
|
|
|
CFATTACH_DECL_NEW(ssspi, sizeof(struct ssspi_softc), ssspi_match, ssspi_attach,
|
2004-02-14 10:19:51 +03:00
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
ssspi_print(void *aux, const char *name)
|
|
|
|
{
|
|
|
|
struct ssspi_attach_args *spia = aux;
|
|
|
|
|
|
|
|
if (spia->spia_aux_intr != SSSPICF_INTR_DEFAULT)
|
|
|
|
printf(" intr %d", spia->spia_aux_intr);
|
|
|
|
return (UNCONF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-10-27 21:17:22 +04:00
|
|
|
ssspi_match(device_t parent, cfdata_t match, void *aux)
|
2004-02-14 10:19:51 +03:00
|
|
|
{
|
|
|
|
struct s3c2xx0_attach_args *sa = aux;
|
|
|
|
|
|
|
|
/* S3C2410 have only two SPIs */
|
|
|
|
switch (sa->sa_index) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-27 21:17:22 +04:00
|
|
|
ssspi_attach(device_t parent, device_t self, void *aux)
|
2004-02-14 10:19:51 +03:00
|
|
|
{
|
2012-10-27 21:17:22 +04:00
|
|
|
struct ssspi_softc *sc = device_private(self);
|
|
|
|
struct s3c2xx0_attach_args *sa = aux;
|
2004-02-14 10:19:51 +03:00
|
|
|
bus_space_tag_t iot = sa->sa_iot;
|
|
|
|
|
|
|
|
static bus_space_handle_t spi_ioh = 0;
|
|
|
|
|
|
|
|
/* we map all registers for SPI0 and SPI1 at once, then
|
|
|
|
use subregions */
|
|
|
|
if (spi_ioh == 0) {
|
|
|
|
if (bus_space_map(iot, S3C2410_SPI0_BASE,
|
|
|
|
2 * S3C24X0_SPI_SIZE,
|
|
|
|
0, &spi_ioh)) {
|
|
|
|
aprint_error(": can't map registers\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aprint_normal("\n");
|
|
|
|
|
|
|
|
sc->index = sa->sa_index;
|
|
|
|
sc->iot = iot;
|
|
|
|
|
|
|
|
bus_space_subregion(iot, spi_ioh, sc->index == 0 ? 0 : S3C24X0_SPI_SIZE,
|
|
|
|
S3C24X0_SPI_SIZE, &sc->ioh);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach child devices
|
|
|
|
*/
|
Merge thorpej-cfargs branch:
Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.
Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)
Remove unnecessary or redundant interface attributes where they're not
needed.
There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)
...and a sentinel value CFARG_EOL.
Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.
Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
2021-04-25 02:36:23 +03:00
|
|
|
config_search(self, NULL,
|
2021-08-07 19:18:40 +03:00
|
|
|
CFARGS(.search = ssspi_search));
|
2004-02-14 10:19:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-10-27 21:17:22 +04:00
|
|
|
ssspi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
|
2004-02-14 10:19:51 +03:00
|
|
|
{
|
2012-10-27 21:17:22 +04:00
|
|
|
struct ssspi_softc *sc = device_private(parent);
|
2004-02-14 10:19:51 +03:00
|
|
|
struct ssspi_attach_args spia;
|
|
|
|
static const unsigned char intr[] = { S3C24X0_INT_SPI0,
|
|
|
|
S3C2410_INT_SPI1 };
|
|
|
|
|
|
|
|
KASSERT(sc->index == 0 || sc->index == 1);
|
|
|
|
|
|
|
|
spia.spia_iot = sc->iot;
|
|
|
|
spia.spia_ioh = sc->ioh;
|
|
|
|
spia.spia_gpioh = s3c2xx0_softc->sc_gpio_ioh;
|
|
|
|
spia.spia_index = sc->index;
|
|
|
|
spia.spia_intr = intr[sc->index];
|
|
|
|
spia.spia_aux_intr = cf->cf_loc[SSSPICF_INTR];
|
|
|
|
spia.spia_dmat = s3c2xx0_softc->sc_dmat;
|
|
|
|
|
Merge thorpej-cfargs branch:
Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.
Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)
Remove unnecessary or redundant interface attributes where they're not
needed.
There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)
...and a sentinel value CFARG_EOL.
Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.
Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
2021-04-25 02:36:23 +03:00
|
|
|
if (config_probe(parent, cf, &spia))
|
2021-08-07 19:18:40 +03:00
|
|
|
config_attach(parent, cf, &spia, ssspi_print, CFARGS_NONE);
|
2004-02-14 10:19:51 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Intiialze SPI port. called by child devices.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
s3c24x0_spi_setup(struct ssspi_softc *sc, uint32_t mode, int bps, int use_ss)
|
|
|
|
{
|
|
|
|
int pclk = s3c2xx0_softc->sc_pclk;
|
|
|
|
int prescaler;
|
|
|
|
uint32_t pgcon, pecon;
|
|
|
|
bus_space_handle_t gpioh = s3c2xx0_softc->sc_gpio_ioh;
|
|
|
|
bus_space_tag_t iot = sc->iot;
|
|
|
|
|
|
|
|
if (bps > 1) {
|
|
|
|
prescaler = pclk / 2 / bps - 1;
|
|
|
|
|
|
|
|
if (prescaler <= 0 || 0xff < prescaler)
|
|
|
|
return -1;
|
|
|
|
bus_space_write_1(sc->iot, sc->ioh, SPI_SPPRE, prescaler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (sc->index == 0) {
|
|
|
|
pecon = bus_space_read_4(iot, gpioh, GPIO_PECON);
|
|
|
|
|
|
|
|
if (use_ss) {
|
|
|
|
pgcon = bus_space_read_4(iot, gpioh, GPIO_PGCON);
|
|
|
|
pgcon = GPIO_SET_FUNC(pgcon, 2, PCON_ALTFUN2);
|
|
|
|
bus_space_write_4(iot, gpioh, GPIO_PGCON, pgcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
pecon = GPIO_SET_FUNC(pecon, 11, PCON_ALTFUN2); /* SPIMISO0 */
|
|
|
|
pecon = GPIO_SET_FUNC(pecon, 12, PCON_ALTFUN2); /* SPIMOSI0 */
|
|
|
|
pecon = GPIO_SET_FUNC(pecon, 13, PCON_ALTFUN2); /* SPICL0 */
|
|
|
|
|
|
|
|
bus_space_write_4(iot, gpioh, GPIO_PECON, pecon);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pgcon = bus_space_read_4(iot, gpioh, GPIO_PGCON);
|
|
|
|
|
|
|
|
if (use_ss)
|
|
|
|
pgcon = GPIO_SET_FUNC(pgcon, 3, PCON_ALTFUN2);
|
|
|
|
|
|
|
|
pgcon = GPIO_SET_FUNC(pgcon, 5, PCON_ALTFUN2); /* SPIMISO1 */
|
|
|
|
pgcon = GPIO_SET_FUNC(pgcon, 6, PCON_ALTFUN2); /* SPIMOSI1 */
|
|
|
|
pgcon = GPIO_SET_FUNC(pgcon, 7, PCON_ALTFUN2); /* SPICLK1 */
|
|
|
|
|
|
|
|
bus_space_write_4(iot, gpioh, GPIO_PGCON, pgcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
bus_space_write_4(iot, sc->ioh, SPI_SPCON, mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|