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