xref: /openbmc/u-boot/arch/powerpc/cpu/mpc83xx/qe_io.c (revision ec48b6c9)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2006 Freescale Semiconductor, Inc.
4  *
5  * Dave Liu <daveliu@freescale.com>
6  * based on source code of Shlomi Gridish
7  */
8 
9 #include <common.h>
10 #include <linux/errno.h>
11 #include <asm/io.h>
12 #include <asm/immap_83xx.h>
13 
14 #define	NUM_OF_PINS	32
15 void qe_config_iopin(u8 port, u8 pin, int dir, int open_drain, int assign)
16 {
17 	u32			pin_2bit_mask;
18 	u32			pin_2bit_dir;
19 	u32			pin_2bit_assign;
20 	u32			pin_1bit_mask;
21 	u32			tmp_val;
22 	volatile immap_t	*im = (volatile immap_t *)CONFIG_SYS_IMMR;
23 	volatile qepio83xx_t	*par_io = (volatile qepio83xx_t *)&im->qepio;
24 
25 	/* Calculate pin location and 2bit mask and dir */
26 	pin_2bit_mask = (u32)(0x3 << (NUM_OF_PINS-(pin%(NUM_OF_PINS/2)+1)*2));
27 	pin_2bit_dir = (u32)(dir << (NUM_OF_PINS-(pin%(NUM_OF_PINS/2)+1)*2));
28 
29 	/* Setup the direction */
30 	tmp_val = (pin > (NUM_OF_PINS/2) - 1) ? \
31 		in_be32(&par_io->ioport[port].dir2) :
32 		in_be32(&par_io->ioport[port].dir1);
33 
34 	if (pin > (NUM_OF_PINS/2) -1) {
35 		out_be32(&par_io->ioport[port].dir2, ~pin_2bit_mask & tmp_val);
36 		out_be32(&par_io->ioport[port].dir2, pin_2bit_dir | tmp_val);
37 	} else {
38 		out_be32(&par_io->ioport[port].dir1, ~pin_2bit_mask & tmp_val);
39 		out_be32(&par_io->ioport[port].dir1, pin_2bit_dir | tmp_val);
40 	}
41 
42 	/* Calculate pin location for 1bit mask */
43 	pin_1bit_mask = (u32)(1 << (NUM_OF_PINS - (pin+1)));
44 
45 	/* Setup the open drain */
46 	tmp_val = in_be32(&par_io->ioport[port].podr);
47 	if (open_drain) {
48 		out_be32(&par_io->ioport[port].podr, pin_1bit_mask | tmp_val);
49 	} else {
50 		out_be32(&par_io->ioport[port].podr, ~pin_1bit_mask & tmp_val);
51 	}
52 
53 	/* Setup the assignment */
54 	tmp_val = (pin > (NUM_OF_PINS/2) - 1) ?
55 		in_be32(&par_io->ioport[port].ppar2):
56 		in_be32(&par_io->ioport[port].ppar1);
57 	pin_2bit_assign = (u32)(assign
58 				<< (NUM_OF_PINS - (pin%(NUM_OF_PINS/2)+1)*2));
59 
60 	/* Clear and set 2 bits mask */
61 	if (pin > (NUM_OF_PINS/2) - 1) {
62 		out_be32(&par_io->ioport[port].ppar2, ~pin_2bit_mask & tmp_val);
63 		out_be32(&par_io->ioport[port].ppar2, pin_2bit_assign | tmp_val);
64 	} else {
65 		out_be32(&par_io->ioport[port].ppar1, ~pin_2bit_mask & tmp_val);
66 		out_be32(&par_io->ioport[port].ppar1, pin_2bit_assign | tmp_val);
67 	}
68 }
69