17ddf24b3SIan Lepore /*-
27ddf24b3SIan Lepore * Copyright (c) 2014 Ian Lepore
37ddf24b3SIan Lepore * All rights reserved.
47ddf24b3SIan Lepore *
57ddf24b3SIan Lepore * Redistribution and use in source and binary forms, with or without
67ddf24b3SIan Lepore * modification, are permitted provided that the following conditions
77ddf24b3SIan Lepore * are met:
87ddf24b3SIan Lepore * 1. Redistributions of source code must retain the above copyright
97ddf24b3SIan Lepore * notice, this list of conditions and the following disclaimer.
107ddf24b3SIan Lepore * 2. Redistributions in binary form must reproduce the above copyright
117ddf24b3SIan Lepore * notice, this list of conditions and the following disclaimer in the
127ddf24b3SIan Lepore * documentation and/or other materials provided with the distribution.
137ddf24b3SIan Lepore *
147ddf24b3SIan Lepore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157ddf24b3SIan Lepore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167ddf24b3SIan Lepore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177ddf24b3SIan Lepore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187ddf24b3SIan Lepore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197ddf24b3SIan Lepore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207ddf24b3SIan Lepore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217ddf24b3SIan Lepore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227ddf24b3SIan Lepore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237ddf24b3SIan Lepore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247ddf24b3SIan Lepore * SUCH DAMAGE.
257ddf24b3SIan Lepore */
267ddf24b3SIan Lepore
2720250ee1SIan Lepore /*
2820250ee1SIan Lepore * Pin mux and pad control driver for imx5 and imx6.
2920250ee1SIan Lepore *
3020250ee1SIan Lepore * This driver implements the fdt_pinctrl interface for configuring the gpio and
3120250ee1SIan Lepore * peripheral pins based on fdt configuration data.
3220250ee1SIan Lepore *
3320250ee1SIan Lepore * When the driver attaches, it walks the entire fdt tree and automatically
3420250ee1SIan Lepore * configures the pins for each device which has a pinctrl-0 property and whose
3520250ee1SIan Lepore * status is "okay". In addition it implements the fdt_pinctrl_configure()
3620250ee1SIan Lepore * method which any other driver can call at any time to reconfigure its pins.
3720250ee1SIan Lepore *
3820250ee1SIan Lepore * The nature of the fsl,pins property in fdt data makes this driver's job very
3920250ee1SIan Lepore * easy. Instead of representing each pin and pad configuration using symbolic
4020250ee1SIan Lepore * properties such as pullup-enable="true" and so on, the data simply contains
4120250ee1SIan Lepore * the addresses of the registers that control the pins, and the raw values to
4220250ee1SIan Lepore * store in those registers.
4320250ee1SIan Lepore *
4420250ee1SIan Lepore * The imx5 and imx6 SoCs also have a small number of "general purpose
4520250ee1SIan Lepore * registers" in the iomuxc device which are used to control an assortment
4620250ee1SIan Lepore * of completely unrelated aspects of SoC behavior. This driver provides other
4720250ee1SIan Lepore * drivers with direct access to those registers via simple accessor functions.
4820250ee1SIan Lepore */
4920250ee1SIan Lepore
507ddf24b3SIan Lepore #include <sys/param.h>
517ddf24b3SIan Lepore #include <sys/systm.h>
527ddf24b3SIan Lepore #include <sys/bus.h>
537ddf24b3SIan Lepore #include <sys/kernel.h>
547ddf24b3SIan Lepore #include <sys/module.h>
557ddf24b3SIan Lepore #include <sys/malloc.h>
567ddf24b3SIan Lepore #include <sys/rman.h>
577ddf24b3SIan Lepore
587ddf24b3SIan Lepore #include <machine/bus.h>
597ddf24b3SIan Lepore
607ddf24b3SIan Lepore #include <dev/ofw/openfirm.h>
617ddf24b3SIan Lepore #include <dev/ofw/ofw_bus.h>
627ddf24b3SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
63222102cfSAndrew Turner #include <dev/fdt/fdt_pinctrl.h>
647ddf24b3SIan Lepore
657ddf24b3SIan Lepore #include <arm/freescale/imx/imx_iomuxvar.h>
667ddf24b3SIan Lepore #include <arm/freescale/imx/imx_machdep.h>
677ddf24b3SIan Lepore
687ddf24b3SIan Lepore struct iomux_softc {
697ddf24b3SIan Lepore device_t dev;
707ddf24b3SIan Lepore struct resource *mem_res;
7128926d45SIan Lepore u_int last_gpregaddr;
727ddf24b3SIan Lepore };
737ddf24b3SIan Lepore
747ddf24b3SIan Lepore static struct iomux_softc *iomux_sc;
757ddf24b3SIan Lepore
767ddf24b3SIan Lepore static struct ofw_compat_data compat_data[] = {
7794bc2117SOleksandr Tymoshenko {"fsl,imx8mq-iomuxc", true},
787ddf24b3SIan Lepore {"fsl,imx6dl-iomuxc", true},
797ddf24b3SIan Lepore {"fsl,imx6q-iomuxc", true},
807ddf24b3SIan Lepore {"fsl,imx6sl-iomuxc", true},
810c076be2SIan Lepore {"fsl,imx6ul-iomuxc", true},
827ddf24b3SIan Lepore {"fsl,imx6sx-iomuxc", true},
837ddf24b3SIan Lepore {"fsl,imx53-iomuxc", true},
847ddf24b3SIan Lepore {"fsl,imx51-iomuxc", true},
857ddf24b3SIan Lepore {NULL, false},
867ddf24b3SIan Lepore };
877ddf24b3SIan Lepore
887ddf24b3SIan Lepore /*
897ddf24b3SIan Lepore * Each tuple in an fsl,pins property contains these fields.
907ddf24b3SIan Lepore */
917ddf24b3SIan Lepore struct pincfg {
927ddf24b3SIan Lepore uint32_t mux_reg;
937ddf24b3SIan Lepore uint32_t padconf_reg;
947ddf24b3SIan Lepore uint32_t input_reg;
957ddf24b3SIan Lepore uint32_t mux_val;
967ddf24b3SIan Lepore uint32_t input_val;
977ddf24b3SIan Lepore uint32_t padconf_val;
987ddf24b3SIan Lepore };
997ddf24b3SIan Lepore
1003a8af166SIan Lepore #define PADCONF_NONE (1U << 31) /* Do not configure pad. */
1013a8af166SIan Lepore #define PADCONF_SION (1U << 30) /* Force SION bit in mux register. */
1023a8af166SIan Lepore #define PADMUX_SION (1U << 4) /* The SION bit in the mux register. */
1033a8af166SIan Lepore
1047ddf24b3SIan Lepore static inline uint32_t
RD4(struct iomux_softc * sc,bus_size_t off)1057ddf24b3SIan Lepore RD4(struct iomux_softc *sc, bus_size_t off)
1067ddf24b3SIan Lepore {
1077ddf24b3SIan Lepore
1087ddf24b3SIan Lepore return (bus_read_4(sc->mem_res, off));
1097ddf24b3SIan Lepore }
1107ddf24b3SIan Lepore
1117ddf24b3SIan Lepore static inline void
WR4(struct iomux_softc * sc,bus_size_t off,uint32_t val)1127ddf24b3SIan Lepore WR4(struct iomux_softc *sc, bus_size_t off, uint32_t val)
1137ddf24b3SIan Lepore {
1147ddf24b3SIan Lepore
1157ddf24b3SIan Lepore bus_write_4(sc->mem_res, off, val);
1167ddf24b3SIan Lepore }
1177ddf24b3SIan Lepore
1189e8ab8f7SIan Lepore static void
iomux_configure_input(struct iomux_softc * sc,uint32_t reg,uint32_t val)1199e8ab8f7SIan Lepore iomux_configure_input(struct iomux_softc *sc, uint32_t reg, uint32_t val)
1209e8ab8f7SIan Lepore {
1219e8ab8f7SIan Lepore u_int select, mask, shift, width;
1229e8ab8f7SIan Lepore
1239e8ab8f7SIan Lepore /* If register and value are zero, there is nothing to configure. */
1249e8ab8f7SIan Lepore if (reg == 0 && val == 0)
1259e8ab8f7SIan Lepore return;
1269e8ab8f7SIan Lepore
1279e8ab8f7SIan Lepore /*
1289e8ab8f7SIan Lepore * If the config value has 0xff in the high byte it is encoded:
1299e8ab8f7SIan Lepore * 31 23 15 7 0
1309e8ab8f7SIan Lepore * | 0xff | shift | width | select |
1319e8ab8f7SIan Lepore * We need to mask out the old select value and OR in the new, using a
1329e8ab8f7SIan Lepore * mask of the given width and shifting the values up by shift.
1339e8ab8f7SIan Lepore */
1349e8ab8f7SIan Lepore if ((val & 0xff000000) == 0xff000000) {
1359e8ab8f7SIan Lepore select = val & 0x000000ff;
1369e8ab8f7SIan Lepore width = (val & 0x0000ff00) >> 8;
1379e8ab8f7SIan Lepore shift = (val & 0x00ff0000) >> 16;
1389e8ab8f7SIan Lepore mask = ((1u << width) - 1) << shift;
1399e8ab8f7SIan Lepore val = (RD4(sc, reg) & ~mask) | (select << shift);
1409e8ab8f7SIan Lepore }
1419e8ab8f7SIan Lepore WR4(sc, reg, val);
1429e8ab8f7SIan Lepore }
1439e8ab8f7SIan Lepore
1447ddf24b3SIan Lepore static int
iomux_configure_pins(device_t dev,phandle_t cfgxref)1457ddf24b3SIan Lepore iomux_configure_pins(device_t dev, phandle_t cfgxref)
1467ddf24b3SIan Lepore {
1477ddf24b3SIan Lepore struct iomux_softc *sc;
1487ddf24b3SIan Lepore struct pincfg *cfgtuples, *cfg;
1497ddf24b3SIan Lepore phandle_t cfgnode;
1507ddf24b3SIan Lepore int i, ntuples;
1513a8af166SIan Lepore uint32_t sion;
1527ddf24b3SIan Lepore
1537ddf24b3SIan Lepore sc = device_get_softc(dev);
1547ddf24b3SIan Lepore cfgnode = OF_node_from_xref(cfgxref);
155f7604b1bSOleksandr Tymoshenko ntuples = OF_getencprop_alloc_multi(cfgnode, "fsl,pins",
156f7604b1bSOleksandr Tymoshenko sizeof(*cfgtuples), (void **)&cfgtuples);
1577ddf24b3SIan Lepore if (ntuples < 0)
1587ddf24b3SIan Lepore return (ENOENT);
1597ddf24b3SIan Lepore if (ntuples == 0)
1607ddf24b3SIan Lepore return (0); /* Empty property is not an error. */
1617ddf24b3SIan Lepore for (i = 0, cfg = cfgtuples; i < ntuples; i++, cfg++) {
1623a8af166SIan Lepore sion = (cfg->padconf_val & PADCONF_SION) ? PADMUX_SION : 0;
1633a8af166SIan Lepore WR4(sc, cfg->mux_reg, cfg->mux_val | sion);
1649e8ab8f7SIan Lepore iomux_configure_input(sc, cfg->input_reg, cfg->input_val);
1656eef1a33SIan Lepore if ((cfg->padconf_val & PADCONF_NONE) == 0)
1667ddf24b3SIan Lepore WR4(sc, cfg->padconf_reg, cfg->padconf_val);
1673a8af166SIan Lepore if (bootverbose) {
1683a8af166SIan Lepore char name[32];
1693a8af166SIan Lepore OF_getprop(cfgnode, "name", &name, sizeof(name));
1703a8af166SIan Lepore printf("%16s: muxreg 0x%04x muxval 0x%02x "
1713a8af166SIan Lepore "inpreg 0x%04x inpval 0x%02x "
1723a8af166SIan Lepore "padreg 0x%04x padval 0x%08x\n",
1733a8af166SIan Lepore name, cfg->mux_reg, cfg->mux_val | sion,
1743a8af166SIan Lepore cfg->input_reg, cfg->input_val,
1753a8af166SIan Lepore cfg->padconf_reg, cfg->padconf_val);
1763a8af166SIan Lepore }
1777ddf24b3SIan Lepore }
178bb307f24SOleksandr Tymoshenko OF_prop_free(cfgtuples);
1797ddf24b3SIan Lepore return (0);
1807ddf24b3SIan Lepore }
1817ddf24b3SIan Lepore
1827ddf24b3SIan Lepore static int
iomux_probe(device_t dev)1837ddf24b3SIan Lepore iomux_probe(device_t dev)
1847ddf24b3SIan Lepore {
1857ddf24b3SIan Lepore
1867ddf24b3SIan Lepore if (!ofw_bus_status_okay(dev))
1877ddf24b3SIan Lepore return (ENXIO);
1887ddf24b3SIan Lepore
1897ddf24b3SIan Lepore if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
1907ddf24b3SIan Lepore return (ENXIO);
1917ddf24b3SIan Lepore
1927ddf24b3SIan Lepore device_set_desc(dev, "Freescale i.MX pin configuration");
1937ddf24b3SIan Lepore return (BUS_PROBE_DEFAULT);
1947ddf24b3SIan Lepore }
1957ddf24b3SIan Lepore
1967ddf24b3SIan Lepore static int
iomux_detach(device_t dev)1977ddf24b3SIan Lepore iomux_detach(device_t dev)
1987ddf24b3SIan Lepore {
1997ddf24b3SIan Lepore
2007ddf24b3SIan Lepore /* This device is always present. */
2017ddf24b3SIan Lepore return (EBUSY);
2027ddf24b3SIan Lepore }
2037ddf24b3SIan Lepore
2047ddf24b3SIan Lepore static int
iomux_attach(device_t dev)2057ddf24b3SIan Lepore iomux_attach(device_t dev)
2067ddf24b3SIan Lepore {
2077ddf24b3SIan Lepore struct iomux_softc * sc;
2087ddf24b3SIan Lepore int rid;
2097ddf24b3SIan Lepore
2107ddf24b3SIan Lepore sc = device_get_softc(dev);
2117ddf24b3SIan Lepore sc->dev = dev;
2127ddf24b3SIan Lepore
2137ddf24b3SIan Lepore switch (imx_soc_type()) {
2147ddf24b3SIan Lepore case IMXSOC_51:
21528926d45SIan Lepore sc->last_gpregaddr = 1 * sizeof(uint32_t);
2167ddf24b3SIan Lepore break;
2177ddf24b3SIan Lepore case IMXSOC_53:
21828926d45SIan Lepore sc->last_gpregaddr = 2 * sizeof(uint32_t);
2197ddf24b3SIan Lepore break;
2207ddf24b3SIan Lepore case IMXSOC_6DL:
2217ddf24b3SIan Lepore case IMXSOC_6S:
2227ddf24b3SIan Lepore case IMXSOC_6SL:
2237ddf24b3SIan Lepore case IMXSOC_6Q:
22428926d45SIan Lepore sc->last_gpregaddr = 13 * sizeof(uint32_t);
2257ddf24b3SIan Lepore break;
2260c076be2SIan Lepore case IMXSOC_6UL:
22728926d45SIan Lepore sc->last_gpregaddr = 14 * sizeof(uint32_t);
2280c076be2SIan Lepore break;
2297ddf24b3SIan Lepore default:
2307ddf24b3SIan Lepore device_printf(dev, "Unknown SoC type\n");
2317ddf24b3SIan Lepore return (ENXIO);
2327ddf24b3SIan Lepore }
2337ddf24b3SIan Lepore
2347ddf24b3SIan Lepore rid = 0;
2357ddf24b3SIan Lepore sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2367ddf24b3SIan Lepore RF_ACTIVE);
2377ddf24b3SIan Lepore if (sc->mem_res == NULL) {
2387ddf24b3SIan Lepore device_printf(dev, "Cannot allocate memory resources\n");
2397ddf24b3SIan Lepore return (ENXIO);
2407ddf24b3SIan Lepore }
2417ddf24b3SIan Lepore
2427ddf24b3SIan Lepore iomux_sc = sc;
2437ddf24b3SIan Lepore
2447ddf24b3SIan Lepore /*
2457ddf24b3SIan Lepore * Register as a pinctrl device, and call the convenience function that
2467ddf24b3SIan Lepore * walks the entire device tree invoking FDT_PINCTRL_CONFIGURE() on any
2477ddf24b3SIan Lepore * pinctrl-0 property cells whose xref phandle refers to a configuration
2487ddf24b3SIan Lepore * that is a child node of our node in the tree.
2497ddf24b3SIan Lepore *
2507ddf24b3SIan Lepore * The pinctrl bindings documentation specifically mentions that the
2517ddf24b3SIan Lepore * pinctrl device itself may have a pinctrl-0 property which contains
2527ddf24b3SIan Lepore * static configuration to be applied at device init time. The tree
2537ddf24b3SIan Lepore * walk will automatically handle this for us when it passes through our
2547ddf24b3SIan Lepore * node in the tree.
2557ddf24b3SIan Lepore */
2567ddf24b3SIan Lepore fdt_pinctrl_register(dev, "fsl,pins");
2577ddf24b3SIan Lepore fdt_pinctrl_configure_tree(dev);
2587ddf24b3SIan Lepore
2597ddf24b3SIan Lepore return (0);
2607ddf24b3SIan Lepore }
2617ddf24b3SIan Lepore
2627ddf24b3SIan Lepore uint32_t
imx_iomux_gpr_get(u_int regaddr)26328926d45SIan Lepore imx_iomux_gpr_get(u_int regaddr)
2647ddf24b3SIan Lepore {
26569c595edSJohn Baldwin struct iomux_softc *sc __diagused;
2667ddf24b3SIan Lepore
2677ddf24b3SIan Lepore sc = iomux_sc;
2687ddf24b3SIan Lepore KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));
26928926d45SIan Lepore KASSERT(regaddr >= 0 && regaddr <= sc->last_gpregaddr,
27028926d45SIan Lepore ("%s bad regaddr %u, max %u", __FUNCTION__, regaddr,
27128926d45SIan Lepore sc->last_gpregaddr));
2727ddf24b3SIan Lepore
27328926d45SIan Lepore return (RD4(iomux_sc, regaddr));
2747ddf24b3SIan Lepore }
2757ddf24b3SIan Lepore
2767ddf24b3SIan Lepore void
imx_iomux_gpr_set(u_int regaddr,uint32_t val)27728926d45SIan Lepore imx_iomux_gpr_set(u_int regaddr, uint32_t val)
2787ddf24b3SIan Lepore {
27969c595edSJohn Baldwin struct iomux_softc *sc __diagused;
2807ddf24b3SIan Lepore
2817ddf24b3SIan Lepore sc = iomux_sc;
2827ddf24b3SIan Lepore KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));
28328926d45SIan Lepore KASSERT(regaddr >= 0 && regaddr <= sc->last_gpregaddr,
28428926d45SIan Lepore ("%s bad regaddr %u, max %u", __FUNCTION__, regaddr,
28528926d45SIan Lepore sc->last_gpregaddr));
2867ddf24b3SIan Lepore
28728926d45SIan Lepore WR4(iomux_sc, regaddr, val);
2887ddf24b3SIan Lepore }
2897ddf24b3SIan Lepore
2907ddf24b3SIan Lepore void
imx_iomux_gpr_set_masked(u_int regaddr,uint32_t clrbits,uint32_t setbits)29128926d45SIan Lepore imx_iomux_gpr_set_masked(u_int regaddr, uint32_t clrbits, uint32_t setbits)
2927ddf24b3SIan Lepore {
29369c595edSJohn Baldwin struct iomux_softc *sc __diagused;
2947ddf24b3SIan Lepore uint32_t val;
2957ddf24b3SIan Lepore
2967ddf24b3SIan Lepore sc = iomux_sc;
2977ddf24b3SIan Lepore KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));
29828926d45SIan Lepore KASSERT(regaddr >= 0 && regaddr <= sc->last_gpregaddr,
29928926d45SIan Lepore ("%s bad regaddr %u, max %u", __FUNCTION__, regaddr,
30028926d45SIan Lepore sc->last_gpregaddr));
3017ddf24b3SIan Lepore
30228926d45SIan Lepore val = RD4(iomux_sc, regaddr * 4);
3037ddf24b3SIan Lepore val = (val & ~clrbits) | setbits;
30428926d45SIan Lepore WR4(iomux_sc, regaddr, val);
3057ddf24b3SIan Lepore }
3067ddf24b3SIan Lepore
3077ddf24b3SIan Lepore static device_method_t imx_iomux_methods[] = {
3087ddf24b3SIan Lepore /* Device interface */
3097ddf24b3SIan Lepore DEVMETHOD(device_probe, iomux_probe),
3107ddf24b3SIan Lepore DEVMETHOD(device_attach, iomux_attach),
3117ddf24b3SIan Lepore DEVMETHOD(device_detach, iomux_detach),
3127ddf24b3SIan Lepore
3137ddf24b3SIan Lepore /* fdt_pinctrl interface */
3147ddf24b3SIan Lepore DEVMETHOD(fdt_pinctrl_configure,iomux_configure_pins),
3157ddf24b3SIan Lepore
3167ddf24b3SIan Lepore DEVMETHOD_END
3177ddf24b3SIan Lepore };
3187ddf24b3SIan Lepore
3197ddf24b3SIan Lepore static driver_t imx_iomux_driver = {
3207ddf24b3SIan Lepore "imx_iomux",
3217ddf24b3SIan Lepore imx_iomux_methods,
3227ddf24b3SIan Lepore sizeof(struct iomux_softc),
3237ddf24b3SIan Lepore };
3247ddf24b3SIan Lepore
325*ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx_iomux, simplebus, imx_iomux_driver, 0, 0,
326*ea538dabSJohn Baldwin BUS_PASS_CPU + BUS_PASS_ORDER_LATE);
327