Path: blob/main/sys/arm/freescale/imx/imx_iomux.c
109658 views
/*-1* Copyright (c) 2014 Ian Lepore2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Pin mux and pad control driver for imx5 and imx6.28*29* This driver implements the fdt_pinctrl interface for configuring the gpio and30* peripheral pins based on fdt configuration data.31*32* When the driver attaches, it walks the entire fdt tree and automatically33* configures the pins for each device which has a pinctrl-0 property and whose34* status is "okay". In addition it implements the fdt_pinctrl_configure()35* method which any other driver can call at any time to reconfigure its pins.36*37* The nature of the fsl,pins property in fdt data makes this driver's job very38* easy. Instead of representing each pin and pad configuration using symbolic39* properties such as pullup-enable="true" and so on, the data simply contains40* the addresses of the registers that control the pins, and the raw values to41* store in those registers.42*43* The imx5 and imx6 SoCs also have a small number of "general purpose44* registers" in the iomuxc device which are used to control an assortment45* of completely unrelated aspects of SoC behavior. This driver provides other46* drivers with direct access to those registers via simple accessor functions.47*/4849#include <sys/param.h>50#include <sys/systm.h>51#include <sys/bus.h>52#include <sys/kernel.h>53#include <sys/module.h>54#include <sys/malloc.h>55#include <sys/rman.h>5657#include <machine/bus.h>5859#include <dev/ofw/openfirm.h>60#include <dev/ofw/ofw_bus.h>61#include <dev/ofw/ofw_bus_subr.h>62#include <dev/fdt/fdt_pinctrl.h>6364#include <arm/freescale/imx/imx_iomuxvar.h>65#include <arm/freescale/imx/imx_machdep.h>6667struct iomux_softc {68device_t dev;69struct resource *mem_res;70u_int last_gpregaddr;71};7273static struct iomux_softc *iomux_sc;7475static struct ofw_compat_data compat_data[] = {76{"fsl,imx8mq-iomuxc", true},77{"fsl,imx6dl-iomuxc", true},78{"fsl,imx6q-iomuxc", true},79{"fsl,imx6sl-iomuxc", true},80{"fsl,imx6ul-iomuxc", true},81{"fsl,imx6sx-iomuxc", true},82{"fsl,imx53-iomuxc", true},83{"fsl,imx51-iomuxc", true},84{NULL, false},85};8687/*88* Each tuple in an fsl,pins property contains these fields.89*/90struct pincfg {91uint32_t mux_reg;92uint32_t padconf_reg;93uint32_t input_reg;94uint32_t mux_val;95uint32_t input_val;96uint32_t padconf_val;97};9899#define PADCONF_NONE (1U << 31) /* Do not configure pad. */100#define PADCONF_SION (1U << 30) /* Force SION bit in mux register. */101#define PADMUX_SION (1U << 4) /* The SION bit in the mux register. */102103static inline uint32_t104RD4(struct iomux_softc *sc, bus_size_t off)105{106107return (bus_read_4(sc->mem_res, off));108}109110static inline void111WR4(struct iomux_softc *sc, bus_size_t off, uint32_t val)112{113114bus_write_4(sc->mem_res, off, val);115}116117static void118iomux_configure_input(struct iomux_softc *sc, uint32_t reg, uint32_t val)119{120u_int select, mask, shift, width;121122/* If register and value are zero, there is nothing to configure. */123if (reg == 0 && val == 0)124return;125126/*127* If the config value has 0xff in the high byte it is encoded:128* 31 23 15 7 0129* | 0xff | shift | width | select |130* We need to mask out the old select value and OR in the new, using a131* mask of the given width and shifting the values up by shift.132*/133if ((val & 0xff000000) == 0xff000000) {134select = val & 0x000000ff;135width = (val & 0x0000ff00) >> 8;136shift = (val & 0x00ff0000) >> 16;137mask = ((1u << width) - 1) << shift;138val = (RD4(sc, reg) & ~mask) | (select << shift);139}140WR4(sc, reg, val);141}142143static int144iomux_configure_pins(device_t dev, phandle_t cfgxref)145{146struct iomux_softc *sc;147struct pincfg *cfgtuples, *cfg;148phandle_t cfgnode;149int i, ntuples;150uint32_t sion;151152sc = device_get_softc(dev);153cfgnode = OF_node_from_xref(cfgxref);154ntuples = OF_getencprop_alloc_multi(cfgnode, "fsl,pins",155sizeof(*cfgtuples), (void **)&cfgtuples);156if (ntuples < 0)157return (ENOENT);158if (ntuples == 0)159return (0); /* Empty property is not an error. */160for (i = 0, cfg = cfgtuples; i < ntuples; i++, cfg++) {161sion = (cfg->padconf_val & PADCONF_SION) ? PADMUX_SION : 0;162WR4(sc, cfg->mux_reg, cfg->mux_val | sion);163iomux_configure_input(sc, cfg->input_reg, cfg->input_val);164if ((cfg->padconf_val & PADCONF_NONE) == 0)165WR4(sc, cfg->padconf_reg, cfg->padconf_val);166if (bootverbose) {167char name[32];168OF_getprop(cfgnode, "name", &name, sizeof(name));169printf("%16s: muxreg 0x%04x muxval 0x%02x "170"inpreg 0x%04x inpval 0x%02x "171"padreg 0x%04x padval 0x%08x\n",172name, cfg->mux_reg, cfg->mux_val | sion,173cfg->input_reg, cfg->input_val,174cfg->padconf_reg, cfg->padconf_val);175}176}177OF_prop_free(cfgtuples);178return (0);179}180181static int182iomux_probe(device_t dev)183{184185if (!ofw_bus_status_okay(dev))186return (ENXIO);187188if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)189return (ENXIO);190191device_set_desc(dev, "Freescale i.MX pin configuration");192return (BUS_PROBE_DEFAULT);193}194195static int196iomux_detach(device_t dev)197{198199/* This device is always present. */200return (EBUSY);201}202203static int204iomux_attach(device_t dev)205{206struct iomux_softc * sc;207int rid;208209sc = device_get_softc(dev);210sc->dev = dev;211212switch (imx_soc_type()) {213case IMXSOC_51:214sc->last_gpregaddr = 1 * sizeof(uint32_t);215break;216case IMXSOC_53:217sc->last_gpregaddr = 2 * sizeof(uint32_t);218break;219case IMXSOC_6DL:220case IMXSOC_6S:221case IMXSOC_6SL:222case IMXSOC_6Q:223sc->last_gpregaddr = 13 * sizeof(uint32_t);224break;225case IMXSOC_6UL:226sc->last_gpregaddr = 14 * sizeof(uint32_t);227break;228default:229device_printf(dev, "Unknown SoC type\n");230return (ENXIO);231}232233rid = 0;234sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,235RF_ACTIVE);236if (sc->mem_res == NULL) {237device_printf(dev, "Cannot allocate memory resources\n");238return (ENXIO);239}240241iomux_sc = sc;242243/*244* Register as a pinctrl device, and call the convenience function that245* walks the entire device tree invoking FDT_PINCTRL_CONFIGURE() on any246* pinctrl-0 property cells whose xref phandle refers to a configuration247* that is a child node of our node in the tree.248*249* The pinctrl bindings documentation specifically mentions that the250* pinctrl device itself may have a pinctrl-0 property which contains251* static configuration to be applied at device init time. The tree252* walk will automatically handle this for us when it passes through our253* node in the tree.254*/255fdt_pinctrl_register(dev, "fsl,pins");256fdt_pinctrl_configure_tree(dev);257258return (0);259}260261uint32_t262imx_iomux_gpr_get(u_int regaddr)263{264struct iomux_softc *sc __diagused;265266sc = iomux_sc;267KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));268KASSERT(regaddr >= 0 && regaddr <= sc->last_gpregaddr,269("%s bad regaddr %u, max %u", __FUNCTION__, regaddr,270sc->last_gpregaddr));271272return (RD4(iomux_sc, regaddr));273}274275void276imx_iomux_gpr_set(u_int regaddr, uint32_t val)277{278struct iomux_softc *sc __diagused;279280sc = iomux_sc;281KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));282KASSERT(regaddr >= 0 && regaddr <= sc->last_gpregaddr,283("%s bad regaddr %u, max %u", __FUNCTION__, regaddr,284sc->last_gpregaddr));285286WR4(iomux_sc, regaddr, val);287}288289void290imx_iomux_gpr_set_masked(u_int regaddr, uint32_t clrbits, uint32_t setbits)291{292struct iomux_softc *sc __diagused;293uint32_t val;294295sc = iomux_sc;296KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));297KASSERT(regaddr >= 0 && regaddr <= sc->last_gpregaddr,298("%s bad regaddr %u, max %u", __FUNCTION__, regaddr,299sc->last_gpregaddr));300301val = RD4(iomux_sc, regaddr * 4);302val = (val & ~clrbits) | setbits;303WR4(iomux_sc, regaddr, val);304}305306static device_method_t imx_iomux_methods[] = {307/* Device interface */308DEVMETHOD(device_probe, iomux_probe),309DEVMETHOD(device_attach, iomux_attach),310DEVMETHOD(device_detach, iomux_detach),311312/* fdt_pinctrl interface */313DEVMETHOD(fdt_pinctrl_configure,iomux_configure_pins),314315DEVMETHOD_END316};317318static driver_t imx_iomux_driver = {319"imx_iomux",320imx_iomux_methods,321sizeof(struct iomux_softc),322};323324EARLY_DRIVER_MODULE(imx_iomux, simplebus, imx_iomux_driver, 0, 0,325BUS_PASS_CPU + BUS_PASS_ORDER_LATE);326327328