mvsoc_sdhc.c revision 1.1 1 1.1 kiyohara /* $NetBSD: mvsoc_sdhc.c,v 1.1 2017/01/07 16:19:28 kiyohara Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2016 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara
28 1.1 kiyohara #include <sys/cdefs.h>
29 1.1 kiyohara __KERNEL_RCSID(0, "$NetBSD: mvsoc_sdhc.c,v 1.1 2017/01/07 16:19:28 kiyohara Exp $");
30 1.1 kiyohara
31 1.1 kiyohara #include <sys/param.h>
32 1.1 kiyohara #include <sys/bus.h>
33 1.1 kiyohara #include <sys/device.h>
34 1.1 kiyohara #include <sys/errno.h>
35 1.1 kiyohara #include <sys/malloc.h>
36 1.1 kiyohara #include <sys/systm.h>
37 1.1 kiyohara
38 1.1 kiyohara #include <dev/marvell/marvellreg.h>
39 1.1 kiyohara #include <dev/marvell/marvellvar.h>
40 1.1 kiyohara
41 1.1 kiyohara #include <dev/sdmmc/sdhcreg.h>
42 1.1 kiyohara #include <dev/sdmmc/sdhcvar.h>
43 1.1 kiyohara
44 1.1 kiyohara #define MVSOC_SDHC_SIZE 0x2000
45 1.1 kiyohara #define MVSOC_SDHC_REG_SIZE SDHC_CAPABILITIES2
46 1.1 kiyohara
47 1.1 kiyohara #define MVSOC_SDHC_MCR0 0x48
48 1.1 kiyohara #define MVSOC_SDHC_MCR1 0x4a
49 1.1 kiyohara #define MVSOC_SDHC_C1R 0x6a
50 1.1 kiyohara #define SDHC_C1R_CRC16_CHK_EN (1 << 2)
51 1.1 kiyohara
52 1.1 kiyohara static int mvsoc_sdhc_match(device_t, cfdata_t, void *);
53 1.1 kiyohara static void mvsoc_sdhc_attach(device_t, device_t, void *);
54 1.1 kiyohara
55 1.1 kiyohara CFATTACH_DECL_NEW(mvsoc_sdhc, sizeof(struct sdhc_softc),
56 1.1 kiyohara mvsoc_sdhc_match, mvsoc_sdhc_attach, NULL, NULL);
57 1.1 kiyohara
58 1.1 kiyohara static int
59 1.1 kiyohara mvsoc_sdhc_match(device_t parent, cfdata_t match, void *aux)
60 1.1 kiyohara {
61 1.1 kiyohara struct marvell_attach_args *mva = aux;
62 1.1 kiyohara
63 1.1 kiyohara if (strcmp(mva->mva_name, match->cf_name) != 0)
64 1.1 kiyohara return 0;
65 1.1 kiyohara mva->mva_size = MVSOC_SDHC_SIZE;
66 1.1 kiyohara return 1;
67 1.1 kiyohara }
68 1.1 kiyohara
69 1.1 kiyohara static void
70 1.1 kiyohara mvsoc_sdhc_attach(device_t parent, device_t self, void *aux)
71 1.1 kiyohara {
72 1.1 kiyohara struct sdhc_softc *sc = device_private(self);
73 1.1 kiyohara struct marvell_attach_args *mva = aux;
74 1.1 kiyohara bus_space_handle_t ioh;
75 1.1 kiyohara int error;
76 1.1 kiyohara
77 1.1 kiyohara sc->sc_dev = self;
78 1.1 kiyohara sc->sc_host = malloc(sizeof(*sc->sc_host), M_DEVBUF, M_WAITOK | M_ZERO);
79 1.1 kiyohara sc->sc_dmat = mva->mva_dmat;
80 1.1 kiyohara /* Must require the DMA. This sdhc can't 32bit access to SDHC_DATA. */
81 1.1 kiyohara sc->sc_flags = SDHC_FLAG_USE_DMA;
82 1.1 kiyohara sc->sc_flags |= SDHC_FLAG_EXTDMA_DMAEN;
83 1.1 kiyohara sc->sc_flags |= SDHC_FLAG_NO_AUTO_STOP;
84 1.1 kiyohara sc->sc_flags |= SDHC_FLAG_NO_BUSY_INTR;
85 1.1 kiyohara
86 1.1 kiyohara aprint_naive("\n");
87 1.1 kiyohara aprint_normal(": SDHC controller\n");
88 1.1 kiyohara
89 1.1 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
90 1.1 kiyohara mva->mva_offset, mva->mva_size, &ioh)) {
91 1.1 kiyohara aprint_error_dev(self, "can't map registers\n");
92 1.1 kiyohara return;
93 1.1 kiyohara }
94 1.1 kiyohara
95 1.1 kiyohara intr_establish(mva->mva_irq, IPL_VM, IST_LEVEL, sdhc_intr, sc);
96 1.1 kiyohara
97 1.1 kiyohara bus_space_write_2(mva->mva_iot, ioh, MVSOC_SDHC_C1R,
98 1.1 kiyohara bus_space_read_2(mva->mva_iot, ioh, MVSOC_SDHC_C1R) |
99 1.1 kiyohara SDHC_C1R_CRC16_CHK_EN);
100 1.1 kiyohara
101 1.1 kiyohara error = sdhc_host_found(sc, mva->mva_iot, ioh, MVSOC_SDHC_REG_SIZE);
102 1.1 kiyohara if (error != 0) {
103 1.1 kiyohara aprint_error_dev(self, "couldn't initialize host, error %d\n",
104 1.1 kiyohara error);
105 1.1 kiyohara return;
106 1.1 kiyohara }
107 1.1 kiyohara }
108