1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2025 Bojan Novković <bnovkov@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/param.h>
28 #include <sys/bus.h>
29 #include <sys/kernel.h>
30 #include <sys/types.h>
31 #include <sys/taskqueue.h>
32 #include <sys/module.h>
33
34 #include <machine/bus.h>
35
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
38 #include <dev/ofw/ofw_subr.h>
39 #include <dev/clk/clk.h>
40 #include <dev/clk/clk_fixed.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/syscon/syscon.h>
43
44 #include <dev/phy/phy.h>
45 #include <dev/mmc/bridge.h>
46 #include <dev/mmc/mmcbrvar.h>
47 #include <dev/mmc/mmcreg.h>
48
49 #include <dev/fdt/fdt_common.h>
50 #include <dev/mmc/mmc_fdt_helpers.h>
51
52 #include <dev/sdhci/sdhci.h>
53 #include <dev/sdhci/sdhci_fdt_gpio.h>
54 #include <dev/sdhci/sdhci_fdt.h>
55
56 #include "syscon_if.h"
57 #include "mmcbr_if.h"
58 #include "sdhci_if.h"
59
60 #include "opt_mmccam.h"
61 #include "opt_soc.h"
62
63 #define CV181X_SYSCTRL_SD_PWRSW_CTRL 0x1F4
64 #define SD_PWRSW_CTRL_RESET_MASK 0x9
65 #define CVI_CV181X_SDHCI_VENDOR_OFFSET 0x200
66 #define CVI_CV181X_SDHCI_EMMC_CTRL (CVI_CV181X_SDHCI_VENDOR_OFFSET + 0x0)
67 #define EMMC_CTRL_RESET_MASK 0x302
68 #define CVI_CV181X_SDHCI_PHY_TX_RX_DLY (CVI_CV181X_SDHCI_VENDOR_OFFSET + 0x40)
69 #define PHY_TX_RX_DLY_RESET_MASK 0x1000100
70 #define CVI_CV181X_SDHCI_PHY_CONFIG (CVI_CV181X_SDHCI_VENDOR_OFFSET + 0x4C)
71 #define PHY_CONFIG_RESET_MASK 0x1
72
73 #define SDHCI_FDT_CVITEK_CV181X_SD 1
74
75 static struct ofw_compat_data compat_data[] = {
76 { "cvitek,cv181x-sd", SDHCI_FDT_CVITEK_CV181X_SD },
77 { NULL, 0 }
78 };
79
80 static int
sdhci_fdt_cvitek_probe(device_t dev)81 sdhci_fdt_cvitek_probe(device_t dev)
82 {
83 if (!ofw_bus_status_okay(dev))
84 return (ENXIO);
85
86 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
87 return (ENXIO);
88
89 device_set_desc(dev, "Cvitek CV181x SDHCI controller");
90 return (BUS_PROBE_SPECIFIC);
91 }
92
93 static int
sdhci_fdt_cvitek_attach(device_t dev)94 sdhci_fdt_cvitek_attach(device_t dev)
95 {
96 int error;
97 uint32_t reg;
98 phandle_t node;
99 struct resource *res;
100 struct syscon *syscon;
101 struct sdhci_fdt_softc *sc = device_get_softc(dev);
102
103 if (sdhci_fdt_attach(dev))
104 return (ENXIO);
105
106 res = sc->mem_res[0];
107 node = ofw_bus_find_compatible(OF_finddevice("/"), "syscon");
108 error = syscon_get_by_ofw_node(dev, node, &syscon);
109 if (error != 0) {
110 device_printf(dev, "Couldn't get syscon handle\n");
111 return (error);
112 }
113
114 SYSCON_WRITE_4(syscon, CV181X_SYSCTRL_SD_PWRSW_CTRL,
115 SD_PWRSW_CTRL_RESET_MASK);
116 DELAY(1000);
117
118 reg = bus_read_4(res, CVI_CV181X_SDHCI_EMMC_CTRL);
119 reg |= EMMC_CTRL_RESET_MASK;
120 bus_write_4(res, CVI_CV181X_SDHCI_EMMC_CTRL, reg);
121 bus_write_4(res, CVI_CV181X_SDHCI_PHY_TX_RX_DLY,
122 PHY_TX_RX_DLY_RESET_MASK);
123 bus_write_4(res, CVI_CV181X_SDHCI_PHY_CONFIG,
124 PHY_CONFIG_RESET_MASK);
125
126 return (0);
127 }
128
129 static device_method_t sdhci_fdt_cvitek_methods[] = {
130 /* device_if */
131 DEVMETHOD(device_probe, sdhci_fdt_cvitek_probe),
132 DEVMETHOD(device_attach, sdhci_fdt_cvitek_attach),
133
134 DEVMETHOD_END
135 };
136 extern driver_t sdhci_fdt_driver;
137
138 DEFINE_CLASS_1(sdhci_cvitek, sdhci_fdt_cvitek_driver, sdhci_fdt_cvitek_methods,
139 sizeof(struct sdhci_fdt_softc), sdhci_fdt_driver);
140 DRIVER_MODULE(sdhci_cvitek, simplebus, sdhci_fdt_cvitek_driver, NULL, NULL);
141
142 #ifndef MMCCAM
143 MMC_DECLARE_BRIDGE(sdhci_fdt_cvitek);
144 #endif
145