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