blob: a4bec3b9b2b39469247bfa5204f5a03006c725bd [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Juergen Beisertaa10abd2008-07-05 10:02:55 +02002/*
Uwe Kleine-König5e2e95f2010-02-15 09:42:59 +01003 * arch/arm/plat-mxc/iomux-v1.c
Juergen Beisertaa10abd2008-07-05 10:02:55 +02004 *
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +01005 * Copyright (C) 2004 Sascha Hauer, Synertronixx GmbH
6 * Copyright (C) 2009 Uwe Kleine-Koenig, Pengutronix
Juergen Beisertaa10abd2008-07-05 10:02:55 +02007 *
Uwe Kleine-König5e2e95f2010-02-15 09:42:59 +01008 * Common code for i.MX1, i.MX21 and i.MX27
Juergen Beisertaa10abd2008-07-05 10:02:55 +02009 */
10
11#include <linux/errno.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/string.h>
16#include <linux/gpio.h>
17
Juergen Beisertaa10abd2008-07-05 10:02:55 +020018#include <asm/mach/map.h>
Shawn Guo267dd342012-09-13 13:26:00 +080019
Shawn Guo50f2de62012-09-14 14:14:45 +080020#include "hardware.h"
Shawn Guo267dd342012-09-13 13:26:00 +080021#include "iomux-v1.h"
Juergen Beisertaa10abd2008-07-05 10:02:55 +020022
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +010023static void __iomem *imx_iomuxv1_baseaddr;
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +010024static unsigned imx_iomuxv1_numports;
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +010025
26static inline unsigned long imx_iomuxv1_readl(unsigned offset)
27{
Johannes Bergc5531382016-01-27 17:59:35 +010028 return imx_readl(imx_iomuxv1_baseaddr + offset);
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +010029}
30
31static inline void imx_iomuxv1_writel(unsigned long val, unsigned offset)
32{
Johannes Bergc5531382016-01-27 17:59:35 +010033 imx_writel(val, imx_iomuxv1_baseaddr + offset);
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +010034}
35
36static inline void imx_iomuxv1_rmwl(unsigned offset,
37 unsigned long mask, unsigned long value)
38{
39 unsigned long reg = imx_iomuxv1_readl(offset);
40
41 reg &= ~mask;
42 reg |= value;
43
44 imx_iomuxv1_writel(reg, offset);
45}
46
47static inline void imx_iomuxv1_set_puen(
48 unsigned int port, unsigned int pin, int on)
49{
50 unsigned long mask = 1 << pin;
51
52 imx_iomuxv1_rmwl(MXC_PUEN(port), mask, on ? mask : 0);
53}
54
55static inline void imx_iomuxv1_set_ddir(
56 unsigned int port, unsigned int pin, int out)
57{
58 unsigned long mask = 1 << pin;
59
60 imx_iomuxv1_rmwl(MXC_DDIR(port), mask, out ? mask : 0);
61}
62
63static inline void imx_iomuxv1_set_gpr(
64 unsigned int port, unsigned int pin, int af)
65{
66 unsigned long mask = 1 << pin;
67
68 imx_iomuxv1_rmwl(MXC_GPR(port), mask, af ? mask : 0);
69}
70
71static inline void imx_iomuxv1_set_gius(
72 unsigned int port, unsigned int pin, int inuse)
73{
74 unsigned long mask = 1 << pin;
75
76 imx_iomuxv1_rmwl(MXC_GIUS(port), mask, inuse ? mask : 0);
77}
78
79static inline void imx_iomuxv1_set_ocr(
80 unsigned int port, unsigned int pin, unsigned int ocr)
81{
82 unsigned long shift = (pin & 0xf) << 1;
83 unsigned long mask = 3 << shift;
84 unsigned long value = ocr << shift;
85 unsigned long offset = pin < 16 ? MXC_OCR1(port) : MXC_OCR2(port);
86
87 imx_iomuxv1_rmwl(offset, mask, value);
88}
89
90static inline void imx_iomuxv1_set_iconfa(
91 unsigned int port, unsigned int pin, unsigned int aout)
92{
93 unsigned long shift = (pin & 0xf) << 1;
94 unsigned long mask = 3 << shift;
95 unsigned long value = aout << shift;
96 unsigned long offset = pin < 16 ? MXC_ICONFA1(port) : MXC_ICONFA2(port);
97
98 imx_iomuxv1_rmwl(offset, mask, value);
99}
100
101static inline void imx_iomuxv1_set_iconfb(
102 unsigned int port, unsigned int pin, unsigned int bout)
103{
104 unsigned long shift = (pin & 0xf) << 1;
105 unsigned long mask = 3 << shift;
106 unsigned long value = bout << shift;
107 unsigned long offset = pin < 16 ? MXC_ICONFB1(port) : MXC_ICONFB2(port);
108
109 imx_iomuxv1_rmwl(offset, mask, value);
110}
111
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100112int mxc_gpio_mode(int gpio_mode)
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200113{
114 unsigned int pin = gpio_mode & GPIO_PIN_MASK;
115 unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
116 unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100117 unsigned int aout = (gpio_mode >> GPIO_AOUT_SHIFT) & 3;
118 unsigned int bout = (gpio_mode >> GPIO_BOUT_SHIFT) & 3;
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200119
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100120 if (port >= imx_iomuxv1_numports)
121 return -EINVAL;
122
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200123 /* Pullup enable */
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100124 imx_iomuxv1_set_puen(port, pin, gpio_mode & GPIO_PUEN);
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200125
126 /* Data direction */
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100127 imx_iomuxv1_set_ddir(port, pin, gpio_mode & GPIO_OUT);
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200128
129 /* Primary / alternate function */
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100130 imx_iomuxv1_set_gpr(port, pin, gpio_mode & GPIO_AF);
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200131
132 /* use as gpio? */
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100133 imx_iomuxv1_set_gius(port, pin, !(gpio_mode & (GPIO_PF | GPIO_AF)));
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200134
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100135 imx_iomuxv1_set_ocr(port, pin, ocr);
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200136
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100137 imx_iomuxv1_set_iconfa(port, pin, aout);
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200138
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100139 imx_iomuxv1_set_iconfb(port, pin, bout);
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100140
141 return 0;
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200142}
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200143
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100144static int imx_iomuxv1_setup_multiple(const int *list, unsigned count)
145{
146 size_t i;
Fabio Estevam6cecabb2011-06-22 11:31:00 -0300147 int ret = 0;
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100148
149 for (i = 0; i < count; ++i) {
150 ret = mxc_gpio_mode(list[i]);
151
152 if (ret)
153 return ret;
154 }
155
156 return ret;
157}
158
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200159int mxc_gpio_setup_multiple_pins(const int *pin_list, unsigned count,
Sascha Hauer7bd18222008-11-04 16:48:46 +0100160 const char *label)
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200161{
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100162 int ret;
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200163
Uwe Kleine-Königbac3fcf2010-02-15 09:47:55 +0100164 ret = imx_iomuxv1_setup_multiple(pin_list, count);
Sascha Hauer7bd18222008-11-04 16:48:46 +0100165 return ret;
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200166}
Juergen Beisertaa10abd2008-07-05 10:02:55 +0200167
Sascha Hauerff255fe2011-02-17 15:08:12 +0100168int __init imx_iomuxv1_init(void __iomem *base, int numports)
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100169{
Sascha Hauerff255fe2011-02-17 15:08:12 +0100170 imx_iomuxv1_baseaddr = base;
171 imx_iomuxv1_numports = numports;
Uwe Kleine-Königf021b5a2010-02-15 16:57:09 +0100172
173 return 0;
174}