1/*	$NetBSD: gxmci.c,v 1.3 2011/07/01 20:39:34 dyoung Exp $	*/
2
3/*-
4 * Copyright (C) 2007 NONAKA Kimihiro <nonaka@netbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: gxmci.c,v 1.3 2011/07/01 20:39:34 dyoung Exp $");
30
31#include <sys/param.h>
32#include <sys/device.h>
33#include <sys/systm.h>
34
35#include <sys/bus.h>
36#include <machine/intr.h>
37
38#include <arm/xscale/pxa2x0cpu.h>
39#include <arm/xscale/pxa2x0reg.h>
40#include <arm/xscale/pxa2x0var.h>
41#include <arm/xscale/pxa2x0_gpio.h>
42#include <arm/xscale/pxa2x0_mci.h>
43
44#include <dev/sdmmc/sdmmcreg.h>
45
46#if defined(PXAMCI_DEBUG)
47#define	DPRINTF(s)	printf s
48#else
49#define	DPRINTF(s)
50#endif
51
52struct gxmci_softc {
53	struct pxamci_softc sc;
54};
55
56static int pxamci_match(device_t, cfdata_t, void *);
57static void pxamci_attach(device_t, device_t, void *);
58
59CFATTACH_DECL_NEW(gxmci, sizeof(struct gxmci_softc),
60    pxamci_match, pxamci_attach, NULL, NULL);
61
62static uint32_t gxmci_get_ocr(void *);
63static int gxmci_set_power(void *, uint32_t);
64static int gxmci_card_detect(void *);
65static int gxmci_write_protect(void *);
66
67static int
68pxamci_match(device_t parent, cfdata_t match, void *aux)
69{
70	struct pxaip_attach_args *pxa = aux;
71	static struct pxa2x0_gpioconf pxa25x_gxmci_gpioconf[] = {
72		{  8, GPIO_ALT_FN_1_OUT },	/* MMCCS0 */
73		{ 53, GPIO_ALT_FN_1_OUT },	/* MMCLK */
74		{ -1 }
75	};
76	struct pxa2x0_gpioconf *gpioconf;
77	u_int reg;
78	int i;
79
80	if (strcmp(pxa->pxa_name, match->cf_name) != 0)
81		return 0;
82
83	/*
84	 * Check GPIO configuration.  If you use these, it is sure already
85	 * to have been set by gxio.
86	 */
87	gpioconf =
88	    CPU_IS_PXA250 ? pxa25x_gxmci_gpioconf : pxa27x_pxamci_gpioconf;
89	for (i = 0; gpioconf[i].pin != -1; i++) {
90		reg = pxa2x0_gpio_get_function(gpioconf[i].pin);
91		if (GPIO_FN(reg) != GPIO_FN(gpioconf[i].value) ||
92		    GPIO_FN_IS_OUT(reg) != GPIO_FN_IS_OUT(gpioconf[i].value))
93		return 0;
94	}
95	pxa->pxa_size = PXA2X0_MMC_SIZE;
96
97	return 1;
98}
99
100static void
101pxamci_attach(device_t parent, device_t self, void *aux)
102{
103	struct gxmci_softc *sc = device_private(self);
104	struct pxaip_attach_args *pxa = aux;
105
106	sc->sc.sc_tag.cookie = sc;
107	sc->sc.sc_tag.get_ocr = gxmci_get_ocr;
108	sc->sc.sc_tag.set_power = gxmci_set_power;
109	sc->sc.sc_tag.card_detect = gxmci_card_detect;
110	sc->sc.sc_tag.write_protect = gxmci_write_protect;
111	sc->sc.sc_caps = 0;
112
113	if (pxamci_attach_sub(self, pxa)) {
114		aprint_error_dev(self, "unable to attach MMC controller\n");
115	}
116
117	if (!pmf_device_register(self, NULL, NULL)) {
118		aprint_error_dev(self, "couldn't establish power handler\n");
119	}
120}
121
122static uint32_t
123gxmci_get_ocr(void *arg)
124{
125
126	return MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V;
127}
128
129static int
130gxmci_set_power(void *arg, uint32_t ocr)
131{
132
133	return 0;
134}
135
136/*
137 * Return non-zero if the card is currently inserted.
138 */
139static int
140gxmci_card_detect(void *arg)
141{
142
143	return 1;
144}
145
146/*
147 * Return non-zero if the card is currently write-protected.
148 */
149static int
150gxmci_write_protect(void *arg)
151{
152
153	return 0;
154}
155