2 * arch/powerpc/sysdev/qe_lib/qe_io.c
4 * QE Parallel I/O ports configuration routines
6 * Copyright (C) Freescale Semicondutor, Inc. 2006. All rights reserved.
8 * Author: Li Yang <LeoLi@freescale.com>
9 * Based on code from Shlomi Gridish <gridish@freescale.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/ioport.h>
27 #include <sysdev/fsl_soc.h>
31 #define NUM_OF_PINS 32
34 __be32 cpodr; /* Open drain register */
35 __be32 cpdata; /* Data register */
36 __be32 cpdir1; /* Direction register */
37 __be32 cpdir2; /* Direction register */
38 __be32 cppar1; /* Pin assignment register */
39 __be32 cppar2; /* Pin assignment register */
40 #ifdef CONFIG_PPC_85xx
45 static struct port_regs __iomem *par_io;
46 static int num_par_io_ports = 0;
48 int par_io_init(struct device_node *np)
54 /* Map Parallel I/O ports registers */
55 ret = of_address_to_resource(np, 0, &res);
58 par_io = ioremap(res.start, res.end - res.start + 1);
60 num_ports = of_get_property(np, "num-ports", NULL);
62 num_par_io_ports = *num_ports;
67 int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
68 int assignment, int has_irq)
70 u32 pin_mask1bit, pin_mask2bits, new_mask2bits, tmp_val;
75 /* calculate pin location for single and 2 bits information */
76 pin_mask1bit = (u32) (1 << (NUM_OF_PINS - (pin + 1)));
78 /* Set open drain, if required */
79 tmp_val = in_be32(&par_io[port].cpodr);
81 out_be32(&par_io[port].cpodr, pin_mask1bit | tmp_val);
83 out_be32(&par_io[port].cpodr, ~pin_mask1bit & tmp_val);
85 /* define direction */
86 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
87 in_be32(&par_io[port].cpdir2) :
88 in_be32(&par_io[port].cpdir1);
90 /* get all bits mask for 2 bit per port */
91 pin_mask2bits = (u32) (0x3 << (NUM_OF_PINS -
92 (pin % (NUM_OF_PINS / 2) + 1) * 2));
94 /* Get the final mask we need for the right definition */
95 new_mask2bits = (u32) (dir << (NUM_OF_PINS -
96 (pin % (NUM_OF_PINS / 2) + 1) * 2));
98 /* clear and set 2 bits mask */
99 if (pin > (NUM_OF_PINS / 2) - 1) {
100 out_be32(&par_io[port].cpdir2,
101 ~pin_mask2bits & tmp_val);
102 tmp_val &= ~pin_mask2bits;
103 out_be32(&par_io[port].cpdir2, new_mask2bits | tmp_val);
105 out_be32(&par_io[port].cpdir1,
106 ~pin_mask2bits & tmp_val);
107 tmp_val &= ~pin_mask2bits;
108 out_be32(&par_io[port].cpdir1, new_mask2bits | tmp_val);
110 /* define pin assignment */
111 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
112 in_be32(&par_io[port].cppar2) :
113 in_be32(&par_io[port].cppar1);
115 new_mask2bits = (u32) (assignment << (NUM_OF_PINS -
116 (pin % (NUM_OF_PINS / 2) + 1) * 2));
117 /* clear and set 2 bits mask */
118 if (pin > (NUM_OF_PINS / 2) - 1) {
119 out_be32(&par_io[port].cppar2,
120 ~pin_mask2bits & tmp_val);
121 tmp_val &= ~pin_mask2bits;
122 out_be32(&par_io[port].cppar2, new_mask2bits | tmp_val);
124 out_be32(&par_io[port].cppar1,
125 ~pin_mask2bits & tmp_val);
126 tmp_val &= ~pin_mask2bits;
127 out_be32(&par_io[port].cppar1, new_mask2bits | tmp_val);
132 EXPORT_SYMBOL(par_io_config_pin);
134 int par_io_data_set(u8 port, u8 pin, u8 val)
136 u32 pin_mask, tmp_val;
138 if (port >= num_par_io_ports)
140 if (pin >= NUM_OF_PINS)
142 /* calculate pin location */
143 pin_mask = (u32) (1 << (NUM_OF_PINS - 1 - pin));
145 tmp_val = in_be32(&par_io[port].cpdata);
147 if (val == 0) /* clear */
148 out_be32(&par_io[port].cpdata, ~pin_mask & tmp_val);
150 out_be32(&par_io[port].cpdata, pin_mask | tmp_val);
154 EXPORT_SYMBOL(par_io_data_set);
156 int par_io_of_config(struct device_node *np)
158 struct device_node *pio;
161 const unsigned int *pio_map;
163 if (par_io == NULL) {
164 printk(KERN_ERR "par_io not initialized \n");
168 ph = of_get_property(np, "pio-handle", NULL);
170 printk(KERN_ERR "pio-handle not available \n");
174 pio = of_find_node_by_phandle(*ph);
176 pio_map = of_get_property(pio, "pio-map", &pio_map_len);
177 if (pio_map == NULL) {
178 printk(KERN_ERR "pio-map is not set! \n");
181 pio_map_len /= sizeof(unsigned int);
182 if ((pio_map_len % 6) != 0) {
183 printk(KERN_ERR "pio-map format wrong! \n");
187 while (pio_map_len > 0) {
188 par_io_config_pin((u8) pio_map[0], (u8) pio_map[1],
189 (int) pio_map[2], (int) pio_map[3],
190 (int) pio_map[4], (int) pio_map[5]);
197 EXPORT_SYMBOL(par_io_of_config);
200 static void dump_par_io(void)
204 printk(KERN_INFO "%s: par_io=%p\n", __func__, par_io);
205 for (i = 0; i < num_par_io_ports; i++) {
206 printk(KERN_INFO " cpodr[%u]=%08x\n", i,
207 in_be32(&par_io[i].cpodr));
208 printk(KERN_INFO " cpdata[%u]=%08x\n", i,
209 in_be32(&par_io[i].cpdata));
210 printk(KERN_INFO " cpdir1[%u]=%08x\n", i,
211 in_be32(&par_io[i].cpdir1));
212 printk(KERN_INFO " cpdir2[%u]=%08x\n", i,
213 in_be32(&par_io[i].cpdir2));
214 printk(KERN_INFO " cppar1[%u]=%08x\n", i,
215 in_be32(&par_io[i].cppar1));
216 printk(KERN_INFO " cppar2[%u]=%08x\n", i,
217 in_be32(&par_io[i].cppar2));
221 EXPORT_SYMBOL(dump_par_io);