1/*	$NetBSD: zmci.c,v 1.4 2012/01/21 18:56:51 nonaka Exp $	*/
2
3/*-
4 * Copyright (C) 2006-2008 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: zmci.c,v 1.4 2012/01/21 18:56:51 nonaka Exp $");
30
31#include <sys/param.h>
32#include <sys/device.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/pmf.h>
36
37#include <machine/intr.h>
38
39#include <arm/xscale/pxa2x0cpu.h>
40#include <arm/xscale/pxa2x0reg.h>
41#include <arm/xscale/pxa2x0var.h>
42#include <arm/xscale/pxa2x0_gpio.h>
43#include <arm/xscale/pxa2x0_mci.h>
44
45#include <dev/sdmmc/sdmmcreg.h>
46
47#include <zaurus/dev/scoopreg.h>
48#include <zaurus/dev/scoopvar.h>
49
50#include <zaurus/zaurus/zaurus_reg.h>
51#include <zaurus/zaurus/zaurus_var.h>
52
53#if defined(PXAMCI_DEBUG)
54#define	DPRINTF(s)	do { printf s; } while (0)
55#else
56#define	DPRINTF(s)	do {} while (0)
57#endif
58
59struct zmci_softc {
60	struct pxamci_softc sc;
61
62	void *sc_detect_ih;
63	int sc_detect_pin;
64	int sc_wp_pin;
65	int sc_power_pin;
66};
67
68static int pxamci_match(device_t, cfdata_t, void *);
69static void pxamci_attach(device_t, device_t, void *);
70
71CFATTACH_DECL_NEW(zmci, sizeof(struct zmci_softc),
72    pxamci_match, pxamci_attach, NULL, NULL);
73
74static int zmci_intr(void *arg);
75
76static uint32_t	zmci_get_ocr(void *);
77static int zmci_set_power(void *, uint32_t);
78static int zmci_card_detect(void *);
79static int zmci_write_protect(void *);
80
81static int
82pxamci_match(device_t parent, cfdata_t cf, void *aux)
83{
84
85	return 1;
86}
87
88static void
89pxamci_attach(device_t parent, device_t self, void *aux)
90{
91	struct zmci_softc *sc = device_private(self);
92	struct pxaip_attach_args *pxa = aux;
93
94	sc->sc.sc_dev = self;
95
96	if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
97		sc->sc_detect_pin = C3000_GPIO_SD_DETECT_PIN;
98		sc->sc_wp_pin = C3000_GPIO_SD_WP_PIN;
99		pxa2x0_gpio_set_function(sc->sc_detect_pin, GPIO_IN);
100		pxa2x0_gpio_set_function(sc->sc_wp_pin, GPIO_IN);
101	} else {
102		/* C7x0/C860 */
103		sc->sc_detect_pin = C860_GPIO_SD_DETECT_PIN;
104		sc->sc_wp_pin = C860_GPIO_SD_WP_PIN;
105		sc->sc_power_pin = C860_GPIO_SD_POWER_PIN;
106		pxa2x0_gpio_set_function(sc->sc_detect_pin, GPIO_IN);
107		pxa2x0_gpio_set_function(sc->sc_wp_pin, GPIO_IN);
108		pxa2x0_gpio_set_function(sc->sc_power_pin, GPIO_OUT|GPIO_SET);
109	}
110
111	/* Establish SD detect interrupt */
112	sc->sc_detect_ih = pxa2x0_gpio_intr_establish(sc->sc_detect_pin,
113	    IST_EDGE_BOTH, IPL_BIO, zmci_intr, sc);
114	if (sc->sc_detect_ih == NULL) {
115		aprint_error_dev(self,
116		    "unable to establish nSD_DETECT interrupt\n");
117		return;
118	}
119
120	sc->sc.sc_tag.cookie = sc;
121	sc->sc.sc_tag.get_ocr = zmci_get_ocr;
122	sc->sc.sc_tag.set_power = zmci_set_power;
123	sc->sc.sc_tag.card_detect = zmci_card_detect;
124	sc->sc.sc_tag.write_protect = zmci_write_protect;
125	sc->sc.sc_caps = PMC_CAPS_4BIT;
126
127	if (pxamci_attach_sub(self, pxa)) {
128		aprint_error_dev(self, "unable to attach MMC controller\n");
129		goto free_intr;
130	}
131
132	if (!pmf_device_register(self, NULL, NULL)) {
133		aprint_error_dev(self, "couldn't establish power handler\n");
134	}
135
136	return;
137
138free_intr:
139	pxa2x0_gpio_intr_disestablish(sc->sc_detect_ih);
140	sc->sc_detect_ih = NULL;
141}
142
143static int
144zmci_intr(void *arg)
145{
146	struct zmci_softc *sc = (struct zmci_softc *)arg;
147
148	pxa2x0_gpio_clear_intr(sc->sc_detect_pin);
149
150	pxamci_card_detect_event(&sc->sc);
151
152	return 1;
153}
154
155static uint32_t
156zmci_get_ocr(void *arg)
157{
158
159	return MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V;
160}
161
162static int
163zmci_set_power(void *arg, uint32_t ocr)
164{
165	struct zmci_softc *sc = (struct zmci_softc *)arg;
166
167	if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
168		if (ZAURUS_ISC3000 || ZAURUS_ISC1000)
169			scoop_set_sdmmc_power(1);
170		else if (ZAURUS_ISC860)
171			pxa2x0_gpio_set_bit(sc->sc_power_pin);
172		return 0;
173	}
174
175	if (ocr != 0) {
176		printf("%s: zmci_set_power(): unsupported OCR (%#x)\n",
177		    device_xname(sc->sc.sc_dev), ocr);
178		return EINVAL;
179	}
180
181	/* power off */
182	if (ZAURUS_ISC3000 || ZAURUS_ISC1000)
183		scoop_set_sdmmc_power(0);
184	else if(ZAURUS_ISC860)
185		pxa2x0_gpio_clear_bit(sc->sc_power_pin);
186	return 0;
187}
188
189/*
190 * Return non-zero if the card is currently inserted.
191 */
192static int
193zmci_card_detect(void *arg)
194{
195	struct zmci_softc *sc = (struct zmci_softc *)arg;
196
197	if (!pxa2x0_gpio_get_bit(sc->sc_detect_pin))
198		return 1;
199	return 0;
200}
201
202/*
203 * Return non-zero if the card is currently write-protected.
204 */
205static int
206zmci_write_protect(void *arg)
207{
208	struct zmci_softc *sc = (struct zmci_softc *)arg;
209
210	if (pxa2x0_gpio_get_bit(sc->sc_wp_pin))
211		return 1;
212	return 0;
213}
214