#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pxa2x0_pcic.c,v 1.15 2022/09/27 06:36:43 skrll Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <uvm/uvm.h>
#include <sys/bus.h>
#include <machine/intr.h>
#include <dev/pcmcia/pcmciareg.h>
#include <dev/pcmcia/pcmciavar.h>
#include <dev/pcmcia/pcmciachip.h>
#include <arm/xscale/pxa2x0cpu.h>
#include <arm/xscale/pxa2x0reg.h>
#include <arm/xscale/pxa2x0var.h>
#include <arm/xscale/pxa2x0_gpio.h>
#include <arm/xscale/pxa2x0_pcic.h>
static int pxapcic_print(void *, const char *);
static void pxapcic_doattach(device_t);
static void pxapcic_event_thread(void *);
static void pxapcic_event_process(struct pxapcic_socket *);
static void pxapcic_attach_card(struct pxapcic_socket *);
static void pxapcic_detach_card(struct pxapcic_socket *, int);
static int pxapcic_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
struct pcmcia_mem_handle *);
static void pxapcic_mem_free(pcmcia_chipset_handle_t,
struct pcmcia_mem_handle *);
static int pxapcic_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
bus_size_t, struct pcmcia_mem_handle *, bus_size_t *, int *);
static void pxapcic_mem_unmap(pcmcia_chipset_handle_t, int);
static int pxapcic_io_alloc(pcmcia_chipset_handle_t, bus_addr_t,
bus_size_t, bus_size_t, struct pcmcia_io_handle *);
static void pxapcic_io_free(pcmcia_chipset_handle_t,
struct pcmcia_io_handle *);
static int pxapcic_io_map(pcmcia_chipset_handle_t, int,
bus_addr_t, bus_size_t, struct pcmcia_io_handle *, int *);
static void pxapcic_io_unmap(pcmcia_chipset_handle_t, int);
static void *pxapcic_intr_establish(pcmcia_chipset_handle_t,
struct pcmcia_function *, int, int (*)(void *), void *);
static void pxapcic_intr_disestablish(pcmcia_chipset_handle_t, void *);
static void pxapcic_socket_enable(pcmcia_chipset_handle_t);
static void pxapcic_socket_disable(pcmcia_chipset_handle_t);
static void pxapcic_socket_settype(pcmcia_chipset_handle_t, int);
static struct pcmcia_chip_functions pxapcic_pcmcia_functions = {
pxapcic_mem_alloc,
pxapcic_mem_free,
pxapcic_mem_map,
pxapcic_mem_unmap,
pxapcic_io_alloc,
pxapcic_io_free,
pxapcic_io_map,
pxapcic_io_unmap,
pxapcic_intr_establish,
pxapcic_intr_disestablish,
pxapcic_socket_enable,
pxapcic_socket_disable,
pxapcic_socket_settype,
};
#define PXAPCIC_ATTR_OFFSET 0x08000000
#define PXAPCIC_COMMON_OFFSET 0x0C000000
static int
pxapcic_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
struct pcmcia_mem_handle *pmh)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
memset(pmh, 0, sizeof(*pmh));
pmh->memt = so->sc->sc_iot;
return 0;
}
static void
pxapcic_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
{
}
static int
pxapcic_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t card_addr,
bus_size_t size, struct pcmcia_mem_handle *pmh, bus_size_t *offsetp,
int *windowp)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
int error;
bus_addr_t pa;
pa = trunc_page(card_addr);
*offsetp = card_addr - pa;
size = round_page(card_addr + size) - pa;
pmh->realsize = size;
pa += PXA2X0_PCIC_SOCKET_BASE;
pa += PXA2X0_PCIC_SOCKET_OFFSET * so->socket;
switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
case PCMCIA_MEM_ATTR:
pa += PXAPCIC_ATTR_OFFSET;
break;
case PCMCIA_MEM_COMMON:
pa += PXAPCIC_COMMON_OFFSET;
break;
default:
panic("pxapcic_mem_map: bogus kind");
}
error = bus_space_map(so->sc->sc_iot, pa, size, 0, &pmh->memh);
if (error)
return error;
*windowp = (int)pmh->memh;
return 0;
}
static void
pxapcic_mem_unmap(pcmcia_chipset_handle_t pch, int window)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
bus_space_unmap(so->sc->sc_iot, (bus_addr_t)window, 4096);
}
static int
pxapcic_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pih)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
bus_addr_t pa;
int error;
memset(pih, 0, sizeof(*pih));
pih->iot = so->sc->sc_iot;
pih->addr = start;
pih->size = size;
pa = pih->addr;
pa += PXA2X0_PCIC_SOCKET_BASE;
pa += PXA2X0_PCIC_SOCKET_OFFSET * so->socket;
error = bus_space_map(so->sc->sc_iot, pa, size, 0, &pih->ioh);
return error;
}
static void
pxapcic_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
bus_space_unmap(so->sc->sc_iot, pih->ioh, pih->size);
}
static int
pxapcic_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
{
return 0;
}
static void
pxapcic_io_unmap(pcmcia_chipset_handle_t pch, int window)
{
}
static void *
pxapcic_intr_establish(pcmcia_chipset_handle_t pch,
struct pcmcia_function *pf, int ipl, int (*fct)(void *), void *arg)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
return (*so->pcictag->intr_establish)(so, ipl, fct, arg);
}
static void
pxapcic_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
(*so->pcictag->intr_disestablish)(so, ih);
}
static void
pxapcic_socket_enable(pcmcia_chipset_handle_t pch)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
int i;
(*so->pcictag->write)(so, PXAPCIC_CARD_POWER, PXAPCIC_POWER_OFF);
(*so->pcictag->set_power)(so, PXAPCIC_POWER_OFF);
delay((300 + 100) * 1000);
if (so->power_capability & PXAPCIC_POWER_5V) {
(*so->pcictag->set_power)(so, PXAPCIC_POWER_5V);
(*so->pcictag->write)(so, PXAPCIC_CARD_POWER,
PXAPCIC_POWER_5V);
} else {
(*so->pcictag->set_power)(so, PXAPCIC_POWER_3V);
(*so->pcictag->write)(so, PXAPCIC_CARD_POWER,
PXAPCIC_POWER_3V);
}
delay((100 + 20 + 200) * 1000);
(*so->pcictag->write)(so, PXAPCIC_CARD_RESET, 1);
delay(10);
delay(3000);
(*so->pcictag->write)(so, PXAPCIC_CARD_RESET, 0);
delay(20 * 1000);
for (i = 0; i < 10000; i++) {
if ((*so->pcictag->read)(so, PXAPCIC_CARD_READY))
break;
delay(500);
}
}
static void
pxapcic_socket_disable(pcmcia_chipset_handle_t pch)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)pch;
#ifdef PCICDEBUG
printf("pxapcic_socket_disable: socket %d\n", so->socket);
#endif
(*so->pcictag->write)(so, PXAPCIC_CARD_POWER, PXAPCIC_POWER_OFF);
(*so->pcictag->set_power)(so, PXAPCIC_POWER_OFF);
}
static void
pxapcic_socket_settype(pcmcia_chipset_handle_t pch, int type)
{
#ifdef PCICDEBUG
printf("pxapcic_socket_settype: type=%d",type);
switch (type) {
case PCMCIA_IFTYPE_MEMORY:
printf("(Memory)\n");
break;
case PCMCIA_IFTYPE_IO:
printf("(I/O)\n");
break;
default:
printf("(unknown)\n");
break;
}
#endif
}
static int
pxapcic_print(void *aux, const char *name)
{
return UNCONF;
}
void
pxapcic_attach_common(struct pxapcic_softc *sc,
void (*socket_setup_hook)(struct pxapcic_socket *))
{
struct pcmciabus_attach_args paa;
struct pxapcic_socket *so;
int s[PXAPCIC_NSLOT];
int i;
printf(": %d slot%s\n", sc->sc_nslots, sc->sc_nslots < 2 ? "" : "s");
if (sc->sc_nslots == 0) {
aprint_error_dev(sc->sc_dev, "can't attach\n");
return;
}
if (bus_space_map(sc->sc_iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE,
0, &sc->sc_memctl_ioh)) {
aprint_error_dev(sc->sc_dev, "failed to map MEMCTL\n");
return;
}
bus_space_write_4(sc->sc_iot, sc->sc_memctl_ioh, MEMCTL_MECR,
(sc->sc_nslots == 2) ? MECR_NOS : 0);
if (sc->sc_flags & PPF_REVERSE_ORDER) {
for (i = 0; i < sc->sc_nslots; i++) {
s[i] = sc->sc_nslots - 1 - i;
}
} else {
for (i = 0; i < sc->sc_nslots; i++) {
s[i] = i;
}
}
for (i = 0; i < sc->sc_nslots; i++) {
so = &sc->sc_socket[s[i]];
so->sc = sc;
so->socket = s[i];
so->flags = 0;
(*socket_setup_hook)(so);
paa.paa_busname = "pcmcia";
paa.pct = (pcmcia_chipset_tag_t)&pxapcic_pcmcia_functions;
paa.pch = (pcmcia_chipset_handle_t)so;
so->pcmcia =
config_found(sc->sc_dev, &paa, pxapcic_print, CFARGS_NONE);
pxa2x0_gpio_set_function(sc->sc_irqpin[s[i]], GPIO_IN);
pxa2x0_gpio_set_function(sc->sc_irqcfpin[s[i]], GPIO_IN);
so->irq = pxa2x0_gpio_intr_establish(sc->sc_irqcfpin[s[i]],
IST_EDGE_BOTH, IPL_BIO , pxapcic_intr, so);
so->irqpin = sc->sc_irqpin[s[i]];
}
config_interrupts(sc->sc_dev, pxapcic_doattach);
}
void
pxapcic_doattach(device_t self)
{
struct pxapcic_softc *sc = device_private(self);
struct pxapcic_socket *sock;
int s[PXAPCIC_NSLOT];
int i;
u_int cs;
if (sc->sc_flags & PPF_REVERSE_ORDER) {
for (i = 0; i < sc->sc_nslots; i++) {
s[i] = sc->sc_nslots - 1 - i;
}
} else {
for (i = 0; i < sc->sc_nslots; i++) {
s[i] = i;
}
}
for (i = 0; i < sc->sc_nslots; i++) {
sock = &sc->sc_socket[s[i]];
config_pending_incr(self);
cs = (*sock->pcictag->read)(sock, PXAPCIC_CARD_STATUS);
if (cs == PXAPCIC_CARD_VALID)
pxapcic_attach_card(sock);
if (kthread_create(PRI_NONE, 0, NULL, pxapcic_event_thread,
sock, &sock->event_thread, "%s,%d",
device_xname(sc->sc_dev), sock->socket)) {
aprint_error_dev(sc->sc_dev,
"unable to create event thread for %d\n",
sock->socket);
}
}
}
int
pxapcic_intr(void *arg)
{
struct pxapcic_socket *so = (struct pxapcic_socket *)arg;
(*so->pcictag->clear_intr)(so);
wakeup(so);
return 1;
}
static void
pxapcic_event_thread(void *arg)
{
struct pxapcic_socket *sock = (struct pxapcic_socket *)arg;
u_int cs;
int present;
config_pending_decr(sock->sc->sc_dev);
while (sock->sc->sc_shutdown == 0) {
(void) tsleep(sock, PWAIT, "pxapcicev", 0);
(void) tsleep((void *)sock, PWAIT, "pxapcicss", hz/4);
cs = (*sock->pcictag->read)(sock, PXAPCIC_CARD_STATUS);
present = sock->flags & PXAPCIC_FLAG_CARDP;
if ((cs == PXAPCIC_CARD_VALID) == (present == 1)) {
continue;
}
pxapcic_event_process(sock);
}
sock->event_thread = NULL;
wakeup(sock->sc);
kthread_exit(0);
}
static void
pxapcic_event_process(struct pxapcic_socket *sock)
{
u_int cs;
cs = (*sock->pcictag->read)(sock, PXAPCIC_CARD_STATUS);
if (cs == PXAPCIC_CARD_VALID) {
if (!(sock->flags & PXAPCIC_FLAG_CARDP)) {
pxapcic_attach_card(sock);
}
} else {
if ((sock->flags & PXAPCIC_FLAG_CARDP)) {
pxapcic_detach_card(sock, DETACH_FORCE);
}
}
}
static void
pxapcic_attach_card(struct pxapcic_socket *h)
{
struct pxapcic_softc *sc = h->sc;
uint32_t reg;
if (h->flags & PXAPCIC_FLAG_CARDP)
panic("pcic_attach_card: already attached");
h->flags |= PXAPCIC_FLAG_CARDP;
reg = bus_space_read_4(sc->sc_iot, sc->sc_memctl_ioh, MEMCTL_MECR);
bus_space_write_4(sc->sc_iot, sc->sc_memctl_ioh, MEMCTL_MECR,
reg | MECR_CIT);
pcmcia_card_attach(h->pcmcia);
}
static void
pxapcic_detach_card(struct pxapcic_socket *h, int flags)
{
struct pxapcic_softc *sc = h->sc;
uint32_t reg;
int i;
if (h->flags & PXAPCIC_FLAG_CARDP) {
h->flags &= ~PXAPCIC_FLAG_CARDP;
pcmcia_card_detach(h->pcmcia, flags);
}
for (i = 0; i < sc->sc_nslots; i++) {
if (sc->sc_socket[i].flags & PXAPCIC_FLAG_CARDP) {
return;
}
}
reg = bus_space_read_4(sc->sc_iot, sc->sc_memctl_ioh, MEMCTL_MECR);
bus_space_write_4(sc->sc_iot, sc->sc_memctl_ioh, MEMCTL_MECR,
reg & ~MECR_CIT);
}