xref: /netbsd-src/sys/arch/arm/imx/imx51_esdhc.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: imx51_esdhc.c,v 1.1 2012/04/19 09:53:53 bsh Exp $ */
2 
3 /*-
4  * Copyright (c) 2012  Genetec Corporation.  All rights reserved.
5  * Written by Hiroyuki Bessho for Genetec Corporation.
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
13  *    copyright notice, this list of conditions and the following
14  *    disclaimer in the documentation and/or other materials provided
15  *    with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: imx51_esdhc.c,v 1.1 2012/04/19 09:53:53 bsh Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/pmf.h>
40 
41 #include <machine/intr.h>
42 
43 #include <dev/sdmmc/sdhcvar.h>
44 #include <dev/sdmmc/sdmmcvar.h>
45 
46 #include <arm/imx/imx51reg.h>
47 #include <arm/imx/imx51var.h>
48 #include <arm/imx/imx51_ccmvar.h>
49 
50 struct sdhc_axi_softc {
51 	struct sdhc_softc  sc_sdhc;
52 	/* we have only one slot */
53 	struct sdhc_host *sc_hosts[1];
54 
55 	void *sc_ih;
56 };
57 
58 static int sdhc_match(device_t, cfdata_t, void *);
59 static void sdhc_attach(device_t, device_t, void *);
60 
61 CFATTACH_DECL_NEW(sdhc_axi, sizeof(struct sdhc_axi_softc),
62     sdhc_match, sdhc_attach, NULL, NULL);
63 
64 static int
65 sdhc_match(device_t parent, cfdata_t cf, void *aux)
66 {
67 
68 	struct axi_attach_args *aa = aux;
69 
70 	switch (aa->aa_addr) {
71 	case ESDHC1_BASE:
72 	case ESDHC2_BASE:
73 		return 1;
74 	}
75 
76 	return 0;
77 }
78 
79 static void
80 sdhc_attach(device_t parent, device_t self, void *aux)
81 {
82 	struct sdhc_axi_softc *sc = device_private(self);
83 	struct axi_attach_args *aa = aux;
84 	bus_space_tag_t iot = aa->aa_iot;
85 	bus_space_handle_t ioh;
86 	u_int perclk;
87 
88 	sc->sc_sdhc.sc_dev = self;
89 
90 	sc->sc_sdhc.sc_dmat = aa->aa_dmat;
91 
92 	if (bus_space_map(iot, aa->aa_addr, ESDHC_SIZE, 0, &ioh)) {
93 		aprint_error_dev(self, "can't map\n");
94 		return;
95 	}
96 
97 	aprint_normal(": SD/MMC host controller\n");
98 	aprint_naive("\n");
99 
100 
101 	sc->sc_sdhc.sc_host = sc->sc_hosts;
102 	/* base clock frequency in kHz */
103 	perclk = imx51_get_clock(IMX51CLK_PERCLK_ROOT);
104 	sc->sc_sdhc.sc_clkbase = perclk / 1000;
105 	sc->sc_sdhc.sc_flags |= SDHC_FLAG_HAVE_DVS |
106 		SDHC_FLAG_NO_PWR0 |
107 		SDHC_FLAG_32BIT_ACCESS |
108 		SDHC_FLAG_ENHANCED;
109 
110 	sc->sc_ih = intr_establish(aa->aa_irq, IPL_SDMMC, IST_LEVEL,
111 	    sdhc_intr, &sc->sc_sdhc);
112 
113 	if (sc->sc_ih == NULL) {
114 		aprint_error_dev(self, "can't establish interrupt\n");
115 		return;
116 	}
117 
118 	if (sdhc_host_found(&sc->sc_sdhc, iot, ioh, ESDHC_SIZE)) {
119 		aprint_error_dev(self, "can't initialize host\n");
120 		return;
121 	}
122 
123 	if (!pmf_device_register1(self, sdhc_suspend, sdhc_resume,
124 		sdhc_shutdown)) {
125 		aprint_error_dev(self,
126 		    "can't establish power hook\n");
127 	}
128 }
129