blob: 256dbfe8a4bb0b60b4909b55b8c8ddd23b9db7a9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dave Liue732e9c2006-11-03 12:11:15 -06002/*
3 * Copyright (C) 2006 Freescale Semiconductor, Inc.
4 *
5 * Dave Liu <daveliu@freescale.com>
6 * based on source code of Shlomi Gridish
Dave Liue732e9c2006-11-03 12:11:15 -06007 */
8
Masahiro Yamada56a931c2016-09-21 11:28:55 +09009#include <linux/errno.h>
Masahiro Yamadaadae2ec2016-09-21 11:28:53 +090010#include <asm/io.h>
11#include <asm/immap_83xx.h>
Dave Liue732e9c2006-11-03 12:11:15 -060012
Dave Liue732e9c2006-11-03 12:11:15 -060013#define NUM_OF_PINS 32
Heiko Schocher3b07a132020-02-03 10:23:53 +010014
15/** qe_cfg_iopin configure one io pin setting
16 *
17 * @par_io: pointer to parallel I/O base
18 * @port: io pin port
19 * @pin: io pin number which get configured
20 * @dir: direction of io pin 2 bits valid
21 * 00 = pin disabled
22 * 01 = output
23 * 10 = input
24 * 11 = pin is I/O
25 * @open_drain: is pin open drain
26 * @assign: pin assignment registers select the function of the pin
27 */
28static void qe_cfg_iopin(qepio83xx_t *par_io, u8 port, u8 pin, int dir,
29 int open_drain, int assign)
Dave Liue732e9c2006-11-03 12:11:15 -060030{
Heiko Schocher3b07a132020-02-03 10:23:53 +010031 u32 dbit_mask;
32 u32 dbit_dir;
33 u32 dbit_asgn;
34 u32 bit_mask;
35 u32 tmp_val;
36 int offset;
Heiko Schocherbaf84a92020-05-25 07:27:26 +020037
Heiko Schocherbaf84a92020-05-25 07:27:26 +020038 offset = (NUM_OF_PINS - (pin % (NUM_OF_PINS / 2) + 1) * 2);
Dave Liue732e9c2006-11-03 12:11:15 -060039
Robert P. J. Daycbd618f2015-12-16 12:25:42 -050040 /* Calculate pin location and 2bit mask and dir */
Heiko Schocher3b07a132020-02-03 10:23:53 +010041 dbit_mask = (u32)(0x3 << offset);
42 dbit_dir = (u32)(dir << offset);
Dave Liue732e9c2006-11-03 12:11:15 -060043
44 /* Setup the direction */
Heiko Schocherbaf84a92020-05-25 07:27:26 +020045 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
Dave Liue732e9c2006-11-03 12:11:15 -060046 in_be32(&par_io->ioport[port].dir2) :
47 in_be32(&par_io->ioport[port].dir1);
48
Heiko Schocherbaf84a92020-05-25 07:27:26 +020049 if (pin > (NUM_OF_PINS / 2) - 1) {
Heiko Schocher3b07a132020-02-03 10:23:53 +010050 out_be32(&par_io->ioport[port].dir2, ~dbit_mask & tmp_val);
51 out_be32(&par_io->ioport[port].dir2, dbit_dir | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060052 } else {
Heiko Schocher3b07a132020-02-03 10:23:53 +010053 out_be32(&par_io->ioport[port].dir1, ~dbit_mask & tmp_val);
54 out_be32(&par_io->ioport[port].dir1, dbit_dir | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060055 }
56
57 /* Calculate pin location for 1bit mask */
Heiko Schocher3b07a132020-02-03 10:23:53 +010058 bit_mask = (u32)(1 << (NUM_OF_PINS - (pin + 1)));
Dave Liue732e9c2006-11-03 12:11:15 -060059
60 /* Setup the open drain */
61 tmp_val = in_be32(&par_io->ioport[port].podr);
Heiko Schocherbaf84a92020-05-25 07:27:26 +020062 if (open_drain)
Heiko Schocher3b07a132020-02-03 10:23:53 +010063 out_be32(&par_io->ioport[port].podr, bit_mask | tmp_val);
Heiko Schocherbaf84a92020-05-25 07:27:26 +020064 else
Heiko Schocher3b07a132020-02-03 10:23:53 +010065 out_be32(&par_io->ioport[port].podr, ~bit_mask & tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060066
67 /* Setup the assignment */
Heiko Schocherbaf84a92020-05-25 07:27:26 +020068 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
69 in_be32(&par_io->ioport[port].ppar2) :
Dave Liue732e9c2006-11-03 12:11:15 -060070 in_be32(&par_io->ioport[port].ppar1);
Heiko Schocher3b07a132020-02-03 10:23:53 +010071 dbit_asgn = (u32)(assign << offset);
Dave Liue732e9c2006-11-03 12:11:15 -060072
73 /* Clear and set 2 bits mask */
Heiko Schocherbaf84a92020-05-25 07:27:26 +020074 if (pin > (NUM_OF_PINS / 2) - 1) {
Heiko Schocher3b07a132020-02-03 10:23:53 +010075 out_be32(&par_io->ioport[port].ppar2, ~dbit_mask & tmp_val);
76 out_be32(&par_io->ioport[port].ppar2, dbit_asgn | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060077 } else {
Heiko Schocher3b07a132020-02-03 10:23:53 +010078 out_be32(&par_io->ioport[port].ppar1, ~dbit_mask & tmp_val);
79 out_be32(&par_io->ioport[port].ppar1, dbit_asgn | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060080 }
81}
Heiko Schocher3b07a132020-02-03 10:23:53 +010082
83#if !defined(CONFIG_PINCTRL)
84/** qe_config_iopin configure one io pin setting
85 *
86 * @port: io pin port
87 * @pin: io pin number which get configured
88 * @dir: direction of io pin 2 bits valid
89 * 00 = pin disabled
90 * 01 = output
91 * 10 = input
92 * 11 = pin is I/O
93 * @open_drain: is pin open drain
94 * @assign: pin assignment registers select the function of the pin
95 */
96void qe_config_iopin(u8 port, u8 pin, int dir, int open_drain, int assign)
97{
98 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
99 qepio83xx_t *par_io = (qepio83xx_t *)&im->qepio;
100
101 qe_cfg_iopin(par_io, port, pin, dir, open_drain, assign);
102}
103#endif