1/*      $NetBSD: lubbock_pcic.c,v 1.8 2023/12/20 13:55:18 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by IWAMOTO Toshihiro.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: lubbock_pcic.c,v 1.8 2023/12/20 13:55:18 thorpej Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/types.h>
38#include <sys/conf.h>
39#include <sys/file.h>
40#include <sys/device.h>
41#include <sys/kernel.h>
42#include <sys/kthread.h>
43
44#include <sys/bus.h>
45
46#include <dev/pcmcia/pcmciachip.h>
47#include <dev/pcmcia/pcmciavar.h>
48#include <arm/sa11x0/sa11x0_reg.h>
49#include <arm/sa11x0/sa11x0_var.h>
50#include <arm/sa11x0/sa1111_reg.h>
51#include <arm/sa11x0/sa1111_var.h>
52#include <arm/sa11x0/sa11x1_pcicreg.h>
53#include <arm/sa11x0/sa11xx_pcicvar.h>
54#include <arm/sa11x0/sa11x1_pcicvar.h>
55
56#include <evbarm/lubbock/lubbock_reg.h>
57#include <evbarm/lubbock/lubbock_var.h>
58
59static	int	sacpcic_match(device_t, cfdata_t, void *);
60static	void	sacpcic_attach(device_t, device_t, void *);
61static	void	lubbock_set_power(struct sapcic_socket *so, int arg);
62static	void	lubbock_socket_setup(struct sapcic_socket *sp);
63
64static struct sapcic_tag lubbock_sacpcic_functions = {
65	sacpcic_read,
66	sacpcic_write,
67	lubbock_set_power,
68	sacpcic_clear_intr,
69	sacpcic_intr_establish,
70	sacpcic_intr_disestablish
71};
72
73CFATTACH_DECL_NEW(sacpcic, sizeof(struct sacpcic_softc),
74    sacpcic_match, sacpcic_attach, NULL, NULL);
75
76static int
77sacpcic_match(device_t parent, cfdata_t cf, void *aux)
78{
79	return (1);
80}
81
82static void
83lubbock_socket_setup(struct sapcic_socket *sp)
84{
85	sp->power_capability = SAPCIC_POWER_5V | SAPCIC_POWER_3V;
86	sp->pcictag = &lubbock_sacpcic_functions;
87}
88
89static void
90sacpcic_attach(device_t parent, device_t self, void *aux)
91{
92	struct sacpcic_softc *sc = device_private(self);
93
94	sc->sc_pc.sc_dev = self;
95	sacpcic_attach_common(device_private(parent),
96	    sc, aux, lubbock_socket_setup);
97}
98
99
100static void
101lubbock_set_power(struct sapcic_socket *so, int arg)
102{
103	struct sacc_softc *sc = so->pcictag_cookie;
104	struct obio_softc *bsc = device_private(device_parent(sc->sc_dev));
105	int s;
106	uint16_t tmp;
107
108	static const uint8_t vval_socket0[] = {
109		/* for socket0 (pcmcia) */
110		0x00,		/* OFF */
111		0x08,		/* 3.3V */
112		0x05,		/* 5V */
113	};
114	static const uint16_t vval_socket1[] = {
115		/* for socket1 (CF) */
116		0x0000,		/* OFF */
117		0x8000,		/* 3.3V */
118		0x4000,		/* 5V */
119	};
120
121	if( arg < 0 || SAPCIC_POWER_5V < arg )
122		panic("sacpcic_set_power: bogus arg\n");
123
124	switch( so->socket ){
125	case 0:
126		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCGPIOA_DVR,
127				  vval_socket0[arg]);
128		break;
129	case 1:
130		s = splhigh();
131		tmp = bus_space_read_2(bsc->sc_iot, bsc->sc_obioreg_ioh,
132		    LUBBOCK_MISCWR);
133		bus_space_write_2(bsc->sc_iot, bsc->sc_obioreg_ioh,
134		    LUBBOCK_MISCWR, (tmp & 0x3fff) | vval_socket1[arg] );
135		splx(s);
136		break;
137	default:
138		aprint_normal("unknown socket number: %d\n", so->socket);
139	}
140}
141