xref: /netbsd-src/sys/arch/evbarm/imx31/imx31lk_pcic.c (revision 08a4aba755ef4f463e005a94ee98c3f3ebc49090)
1*08a4aba7Sskrll /*	$Id: imx31lk_pcic.c,v 1.6 2012/11/12 18:00:39 skrll Exp $	*/
2*08a4aba7Sskrll /*	$NetBSD: imx31lk_pcic.c,v 1.6 2012/11/12 18:00:39 skrll Exp $	*/
3825088edSmatt /*	$OpenBSD: pxapcic.c,v 1.1 2005/07/01 23:51:55 uwe Exp $	*/
4825088edSmatt 
5825088edSmatt /*
6825088edSmatt  * Copyright (c) 2005 Uwe Stuehler <uwe@bsdx.de>
7825088edSmatt  *
8825088edSmatt  * Permission to use, copy, modify, and distribute this software for any
9825088edSmatt  * purpose with or without fee is hereby granted, provided that the above
10825088edSmatt  * copyright notice and this permission notice appear in all copies.
11825088edSmatt  *
12825088edSmatt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13825088edSmatt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14825088edSmatt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15825088edSmatt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16825088edSmatt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17825088edSmatt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18825088edSmatt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19825088edSmatt  */
20825088edSmatt 
21825088edSmatt #include <sys/cdefs.h>
22*08a4aba7Sskrll __KERNEL_RCSID(0, "$Id: imx31lk_pcic.c,v 1.6 2012/11/12 18:00:39 skrll Exp $");
23825088edSmatt 
24825088edSmatt #include <sys/param.h>
25825088edSmatt #include <sys/systm.h>
26825088edSmatt #include <sys/device.h>
27825088edSmatt 
28825088edSmatt #include <machine/intr.h>
29fea15f47Sdyoung #include <sys/bus.h>
30825088edSmatt 
31825088edSmatt #include <uvm/uvm.h>
32825088edSmatt 
33825088edSmatt #ifdef NOTYET
34825088edSmatt #include <arch/arm/imx/imx_emifs.h>
35825088edSmatt #include <arch/arm/imx/imx_gpio.h>
36825088edSmatt #endif
37825088edSmatt #include <arch/arm/imx/imx31var.h>
38825088edSmatt #include <arch/arm/imx/imx_pcic.h>
39825088edSmatt 
40825088edSmatt 
41cbab9cadSchs static int	imx31lk_pcic_match(device_t, cfdata_t, void *);
42cbab9cadSchs static void	imx31lk_pcic_attach(device_t, device_t, void *);
43825088edSmatt 
44cbab9cadSchs CFATTACH_DECL_NEW(imx31lk_pcic, sizeof(struct imx_pcic_softc),
45825088edSmatt 	imx31lk_pcic_match, imx31lk_pcic_attach, NULL, NULL);
46825088edSmatt 
47825088edSmatt static void	imx31lk_pcic_socket_setup(struct imx_pcic_socket *);
48825088edSmatt static u_int	imx31lk_pcic_read(struct imx_pcic_socket *, int);
49825088edSmatt static void	imx31lk_pcic_write(struct imx_pcic_socket *, int, u_int);
50825088edSmatt static void	imx31lk_pcic_set_power(struct imx_pcic_socket *, int);
51825088edSmatt static void	imx31lk_pcic_clear_intr(struct imx_pcic_socket *);
52825088edSmatt static void 	*imx31lk_pcic_intr_establish(struct imx_pcic_socket *, int,
53825088edSmatt 		    int (*)(void *), void *);
54825088edSmatt static void	imx31lk_pcic_intr_disestablish(struct imx_pcic_socket *, void *);
55825088edSmatt 
56825088edSmatt struct imx_pcic_tag imx31lk_pcic_functions = {
57825088edSmatt 	imx31lk_pcic_read,
58825088edSmatt 	imx31lk_pcic_write,
59825088edSmatt 	imx31lk_pcic_set_power,
60825088edSmatt 	imx31lk_pcic_clear_intr,
61825088edSmatt 	imx31lk_pcic_intr_establish,
62825088edSmatt 	imx31lk_pcic_intr_disestablish
63825088edSmatt };
64825088edSmatt 
65825088edSmatt static int
imx31lk_pcic_match(device_t parent,cfdata_t cf,void * aux)66cbab9cadSchs imx31lk_pcic_match(device_t parent, cfdata_t cf, void *aux)
67825088edSmatt {
68825088edSmatt 
69825088edSmatt 	return 1;	/* XXX */
70825088edSmatt }
71825088edSmatt 
72825088edSmatt static void
imx31lk_pcic_attach(device_t parent,device_t self,void * aux)73cbab9cadSchs imx31lk_pcic_attach(device_t parent, device_t self, void *aux)
74825088edSmatt {
75cbab9cadSchs 	struct imx_pcic_softc *sc = device_private(self);
76825088edSmatt 	struct aips_attach_args * const aipsa = aux;
77825088edSmatt 
78cbab9cadSchs 	sc->sc_dev = self;
79cbab9cadSchs 
80825088edSmatt printf("\n");
81825088edSmatt printf("imx_iot %p\n", aipsa->aipsa_memt);
82825088edSmatt printf("imx_addr %lx\n", aipsa->aipsa_addr);
83825088edSmatt printf("imx_size %lx\n", aipsa->aipsa_size);
84825088edSmatt printf("imx_intr %d\n", aipsa->aipsa_intr);
85825088edSmatt 
86825088edSmatt 	sc->sc_pa = aipsa->aipsa_addr;
87825088edSmatt 	sc->sc_iot = aipsa->aipsa_memt;
88825088edSmatt 
89825088edSmatt 	sc->sc_nslots = 1;
90825088edSmatt #ifdef NOTYET
91825088edSmatt 	sc->sc_irqpin[0] = aa->emifs_intr;	/* XXX */
92825088edSmatt 	sc->sc_irqcfpin[0] = -1;		/* XXX */
93825088edSmatt #endif
94825088edSmatt 
95825088edSmatt 	sc->sc_flags |= PPF_REVERSE_ORDER;
96825088edSmatt 
97825088edSmatt 	imx_pcic_attach_common(sc, &imx31lk_pcic_socket_setup);
98825088edSmatt }
99825088edSmatt 
100825088edSmatt static void
imx31lk_pcic_socket_setup(struct imx_pcic_socket * so)101825088edSmatt imx31lk_pcic_socket_setup(struct imx_pcic_socket *so)
102825088edSmatt {
103825088edSmatt 	struct imx_pcic_softc *sc;
104825088edSmatt 	bus_addr_t pa;
105825088edSmatt 	bus_size_t size = 0x2000;		/* XXX */
106825088edSmatt 	bus_space_tag_t iot;
107825088edSmatt 	bus_space_handle_t imx31lkh;
108825088edSmatt 	int error;
109825088edSmatt 
110825088edSmatt 	sc = so->sc;
111825088edSmatt 	iot = sc->sc_iot;
112825088edSmatt 
113825088edSmatt 	if (so->socket != 0)
114cbab9cadSchs 		panic("%s: CF slot %d not supported", device_xname(sc->sc_dev), so->socket);
115825088edSmatt 
116825088edSmatt 	pa = sc->sc_pa;
117825088edSmatt 
118825088edSmatt 	error = bus_space_map(iot, trunc_page(pa), round_page(size),
119825088edSmatt 	    0, &imx31lkh);
120825088edSmatt 	if (error) {
121825088edSmatt 		panic("%s: failed to map memory %x for imx31lk",
122cbab9cadSchs 		    device_xname(sc->sc_dev), (uint32_t)pa);
123825088edSmatt 	}
124825088edSmatt 	imx31lkh += pa - trunc_page(pa);
125825088edSmatt 
126825088edSmatt #ifdef NOTYET
127825088edSmatt 	/* setup */
128825088edSmatt #endif
129825088edSmatt 
130825088edSmatt #ifdef NOTYET
131825088edSmatt 	so->power_capability = PXAPCIC_POWER_3V;
132825088edSmatt 	if (so->socket == 0)
133825088edSmatt 		so->power_capability |= PXAPCIC_POWER_5V;
134825088edSmatt #endif
135825088edSmatt 
136825088edSmatt 	so->pcictag_cookie = (void *)imx31lkh;
137825088edSmatt 	so->pcictag = &imx31lk_pcic_functions;
138825088edSmatt }
139825088edSmatt 
140825088edSmatt static u_int
imx31lk_pcic_read(struct imx_pcic_socket * so,int reg)141825088edSmatt imx31lk_pcic_read(struct imx_pcic_socket *so, int reg)
142825088edSmatt {
143825088edSmatt #ifdef NOTYET
144825088edSmatt 	bus_space_tag_t iot = so->sc->sc_iot;
145825088edSmatt 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
146825088edSmatt 	uint16_t csr;
147825088edSmatt 
148825088edSmatt 	csr = bus_space_read_2(iot, ioh, SCOOP_CSR);
149825088edSmatt 
150825088edSmatt 	switch (reg) {
151825088edSmatt 	case PXAPCIC_CARD_STATUS:
152825088edSmatt 		if (csr & SCP_CSR_MISSING)
153825088edSmatt 			return (PXAPCIC_CARD_INVALID);
154825088edSmatt 		else
155825088edSmatt 			return (PXAPCIC_CARD_VALID);
156825088edSmatt 
157825088edSmatt 	case PXAPCIC_CARD_READY:
158825088edSmatt 		return ((bus_space_read_2(iot, ioh, SCOOP_CSR) &
159825088edSmatt 		    SCP_CSR_READY) != 0);
160825088edSmatt 
161825088edSmatt 	default:
162825088edSmatt 		panic("imx31lk_pcic_read: bogus register");
163825088edSmatt 	}
164825088edSmatt 	/*NOTREACHED*/
165825088edSmatt #else
166825088edSmatt 	panic("imx31lk_pcic_read");
167825088edSmatt #endif
168825088edSmatt }
169825088edSmatt 
170825088edSmatt static void
imx31lk_pcic_write(struct imx_pcic_socket * so,int reg,u_int val)171825088edSmatt imx31lk_pcic_write(struct imx_pcic_socket *so, int reg, u_int val)
172825088edSmatt {
173825088edSmatt #ifdef NOTYET
174825088edSmatt 	bus_space_tag_t iot = so->sc->sc_iot;
175825088edSmatt 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
176825088edSmatt 	uint16_t newval;
177825088edSmatt 	int s;
178825088edSmatt 
179825088edSmatt 	s = splhigh();
180825088edSmatt 
181825088edSmatt 	switch (reg) {
182825088edSmatt 	case PXAPCIC_CARD_POWER:
183825088edSmatt 		newval = bus_space_read_2(iot, ioh, SCOOP_CPR);
184825088edSmatt 		newval &= ~(SCP_CPR_PWR | SCP_CPR_3V | SCP_CPR_5V);
185825088edSmatt 
186825088edSmatt 		if (val == PXAPCIC_POWER_3V)
187825088edSmatt 			newval |= (SCP_CPR_PWR | SCP_CPR_3V);
188825088edSmatt 		else if (val == PXAPCIC_POWER_5V)
189825088edSmatt 			newval |= (SCP_CPR_PWR | SCP_CPR_5V);
190825088edSmatt 
191825088edSmatt 		bus_space_write_2(iot, ioh, SCOOP_CPR, newval);
192825088edSmatt 		break;
193825088edSmatt 
194825088edSmatt 	case PXAPCIC_CARD_RESET:
195825088edSmatt 		bus_space_write_2(iot, ioh, SCOOP_CCR,
196825088edSmatt 		    val ? SCP_CCR_RESET : 0);
197825088edSmatt 		break;
198825088edSmatt 
199825088edSmatt 	default:
200825088edSmatt 		panic("imx31lk_pcic_write: bogus register");
201825088edSmatt 	}
202825088edSmatt 
203825088edSmatt 	splx(s);
204825088edSmatt #else
205825088edSmatt 	panic("imx31lk_pcic_write");
206825088edSmatt #endif
207825088edSmatt }
208825088edSmatt 
209825088edSmatt static void
imx31lk_pcic_set_power(struct imx_pcic_socket * so,int pwr)210825088edSmatt imx31lk_pcic_set_power(struct imx_pcic_socket *so, int pwr)
211825088edSmatt {
212825088edSmatt #ifdef NOTYET
213825088edSmatt 	bus_space_tag_t iot = so->sc->sc_iot;
214825088edSmatt 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
215*08a4aba7Sskrll 	uint16_t reg;
216825088edSmatt 	int s;
217825088edSmatt 
218825088edSmatt 	s = splhigh();
219825088edSmatt 
220825088edSmatt 	switch (pwr) {
221825088edSmatt 	case PXAPCIC_POWER_OFF:
222825088edSmatt #if 0
223825088edSmatt 		/* XXX does this disable power to both sockets? */
224825088edSmatt 		reg = bus_space_read_2(iot, ioh, SCOOP_GPWR);
225825088edSmatt 		bus_space_write_2(iot, ioh, SCOOP_GPWR,
226825088edSmatt 		    reg & ~(1 << SCOOP0_CF_POWER_C3000));
227825088edSmatt #endif
228825088edSmatt 		break;
229825088edSmatt 
230825088edSmatt 	case PXAPCIC_POWER_3V:
231825088edSmatt 	case PXAPCIC_POWER_5V:
232825088edSmatt 		/* XXX */
233825088edSmatt 		if (so->socket == 0) {
234825088edSmatt 			reg = bus_space_read_2(iot, ioh, SCOOP_GPWR);
235825088edSmatt 			bus_space_write_2(iot, ioh, SCOOP_GPWR,
236825088edSmatt 			    reg | (1 << SCOOP0_CF_POWER_C3000));
237825088edSmatt 		}
238825088edSmatt 		break;
239825088edSmatt 
240825088edSmatt 	default:
241825088edSmatt 		splx(s);
242825088edSmatt 		panic("imx31lk_pcic_set_power: bogus power state");
243825088edSmatt 	}
244825088edSmatt 
245825088edSmatt 	splx(s);
246825088edSmatt #else
247825088edSmatt 	panic("imx31lk_pcic_set_power");
248825088edSmatt #endif
249825088edSmatt }
250825088edSmatt 
251825088edSmatt static void
imx31lk_pcic_clear_intr(struct imx_pcic_socket * so)252825088edSmatt imx31lk_pcic_clear_intr(struct imx_pcic_socket *so)
253825088edSmatt {
254825088edSmatt #ifdef NOTYET
255825088edSmatt 	bus_space_tag_t iot = so->sc->sc_iot;
256825088edSmatt 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
257825088edSmatt 
258825088edSmatt 	bus_space_write_2(iot, ioh, SCOOP_IRM, 0x00ff);
259825088edSmatt 	bus_space_write_2(iot, ioh, SCOOP_ISR, 0x0000);
260825088edSmatt 	bus_space_write_2(iot, ioh, SCOOP_IRM, 0x0000);
261825088edSmatt #else
262825088edSmatt 	panic("imx31lk_pcic_clear_intr");
263825088edSmatt #endif
264825088edSmatt }
265825088edSmatt 
266825088edSmatt static void *
imx31lk_pcic_intr_establish(struct imx_pcic_socket * so,int ipl,int (* func)(void *),void * arg)267825088edSmatt imx31lk_pcic_intr_establish(struct imx_pcic_socket *so, int ipl,
268825088edSmatt     int (*func)(void *), void *arg)
269825088edSmatt {
270825088edSmatt #ifdef NOTYET
27110cfc49bSperry printf("%s: irqpin %d\n", __func__, so->irqpin);
272825088edSmatt 	return (imx_gpio_intr_establish(so->irqpin, IST_EDGE_FALLING,
273825088edSmatt 	    ipl, "pcic", func, arg));
274825088edSmatt #else
275825088edSmatt 	return 0;		/* XXX */
276825088edSmatt #endif
277825088edSmatt }
278825088edSmatt 
279825088edSmatt static void
imx31lk_pcic_intr_disestablish(struct imx_pcic_socket * so,void * ih)280825088edSmatt imx31lk_pcic_intr_disestablish(struct imx_pcic_socket *so, void *ih)
281825088edSmatt {
282825088edSmatt 
283825088edSmatt #ifdef NOTYET
284825088edSmatt 	imx_gpio_intr_disestablish(ih);
285825088edSmatt #endif
286825088edSmatt }
287