blob: 52360703a7da2eeade3f2488f0535b86edfef10c [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 Yamadaadae2ec2016-09-21 11:28:53 +09009#include <common.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090010#include <linux/errno.h>
Masahiro Yamadaadae2ec2016-09-21 11:28:53 +090011#include <asm/io.h>
12#include <asm/immap_83xx.h>
Dave Liue732e9c2006-11-03 12:11:15 -060013
Dave Liue732e9c2006-11-03 12:11:15 -060014#define NUM_OF_PINS 32
Heiko Schocher3b07a132020-02-03 10:23:53 +010015
16/** qe_cfg_iopin configure one io pin setting
17 *
18 * @par_io: pointer to parallel I/O base
19 * @port: io pin port
20 * @pin: io pin number which get configured
21 * @dir: direction of io pin 2 bits valid
22 * 00 = pin disabled
23 * 01 = output
24 * 10 = input
25 * 11 = pin is I/O
26 * @open_drain: is pin open drain
27 * @assign: pin assignment registers select the function of the pin
28 */
29static void qe_cfg_iopin(qepio83xx_t *par_io, u8 port, u8 pin, int dir,
30 int open_drain, int assign)
Dave Liue732e9c2006-11-03 12:11:15 -060031{
Heiko Schocher3b07a132020-02-03 10:23:53 +010032 u32 dbit_mask;
33 u32 dbit_dir;
34 u32 dbit_asgn;
35 u32 bit_mask;
36 u32 tmp_val;
37 int offset;
Heiko Schocherbaf84a92020-05-25 07:27:26 +020038
Heiko Schocherbaf84a92020-05-25 07:27:26 +020039 offset = (NUM_OF_PINS - (pin % (NUM_OF_PINS / 2) + 1) * 2);
Dave Liue732e9c2006-11-03 12:11:15 -060040
Robert P. J. Daycbd618f2015-12-16 12:25:42 -050041 /* Calculate pin location and 2bit mask and dir */
Heiko Schocher3b07a132020-02-03 10:23:53 +010042 dbit_mask = (u32)(0x3 << offset);
43 dbit_dir = (u32)(dir << offset);
Dave Liue732e9c2006-11-03 12:11:15 -060044
45 /* Setup the direction */
Heiko Schocherbaf84a92020-05-25 07:27:26 +020046 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
Dave Liue732e9c2006-11-03 12:11:15 -060047 in_be32(&par_io->ioport[port].dir2) :
48 in_be32(&par_io->ioport[port].dir1);
49
Heiko Schocherbaf84a92020-05-25 07:27:26 +020050 if (pin > (NUM_OF_PINS / 2) - 1) {
Heiko Schocher3b07a132020-02-03 10:23:53 +010051 out_be32(&par_io->ioport[port].dir2, ~dbit_mask & tmp_val);
52 out_be32(&par_io->ioport[port].dir2, dbit_dir | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060053 } else {
Heiko Schocher3b07a132020-02-03 10:23:53 +010054 out_be32(&par_io->ioport[port].dir1, ~dbit_mask & tmp_val);
55 out_be32(&par_io->ioport[port].dir1, dbit_dir | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060056 }
57
58 /* Calculate pin location for 1bit mask */
Heiko Schocher3b07a132020-02-03 10:23:53 +010059 bit_mask = (u32)(1 << (NUM_OF_PINS - (pin + 1)));
Dave Liue732e9c2006-11-03 12:11:15 -060060
61 /* Setup the open drain */
62 tmp_val = in_be32(&par_io->ioport[port].podr);
Heiko Schocherbaf84a92020-05-25 07:27:26 +020063 if (open_drain)
Heiko Schocher3b07a132020-02-03 10:23:53 +010064 out_be32(&par_io->ioport[port].podr, bit_mask | tmp_val);
Heiko Schocherbaf84a92020-05-25 07:27:26 +020065 else
Heiko Schocher3b07a132020-02-03 10:23:53 +010066 out_be32(&par_io->ioport[port].podr, ~bit_mask & tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060067
68 /* Setup the assignment */
Heiko Schocherbaf84a92020-05-25 07:27:26 +020069 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
70 in_be32(&par_io->ioport[port].ppar2) :
Dave Liue732e9c2006-11-03 12:11:15 -060071 in_be32(&par_io->ioport[port].ppar1);
Heiko Schocher3b07a132020-02-03 10:23:53 +010072 dbit_asgn = (u32)(assign << offset);
Dave Liue732e9c2006-11-03 12:11:15 -060073
74 /* Clear and set 2 bits mask */
Heiko Schocherbaf84a92020-05-25 07:27:26 +020075 if (pin > (NUM_OF_PINS / 2) - 1) {
Heiko Schocher3b07a132020-02-03 10:23:53 +010076 out_be32(&par_io->ioport[port].ppar2, ~dbit_mask & tmp_val);
77 out_be32(&par_io->ioport[port].ppar2, dbit_asgn | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060078 } else {
Heiko Schocher3b07a132020-02-03 10:23:53 +010079 out_be32(&par_io->ioport[port].ppar1, ~dbit_mask & tmp_val);
80 out_be32(&par_io->ioport[port].ppar1, dbit_asgn | tmp_val);
Dave Liue732e9c2006-11-03 12:11:15 -060081 }
82}
Heiko Schocher3b07a132020-02-03 10:23:53 +010083
84#if !defined(CONFIG_PINCTRL)
85/** qe_config_iopin configure one io pin setting
86 *
87 * @port: io pin port
88 * @pin: io pin number which get configured
89 * @dir: direction of io pin 2 bits valid
90 * 00 = pin disabled
91 * 01 = output
92 * 10 = input
93 * 11 = pin is I/O
94 * @open_drain: is pin open drain
95 * @assign: pin assignment registers select the function of the pin
96 */
97void qe_config_iopin(u8 port, u8 pin, int dir, int open_drain, int assign)
98{
99 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
100 qepio83xx_t *par_io = (qepio83xx_t *)&im->qepio;
101
102 qe_cfg_iopin(par_io, port, pin, dir, open_drain, assign);
103}
104#endif