Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / powerpc / sysdev / qe_lib / qe_io.c
blobe53ea4d374a03f52f57c6e9c5e036275fb207c0b
1 /*
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>
24 #include <asm/io.h>
25 #include <asm/prom.h>
26 #include <sysdev/fsl_soc.h>
28 #undef DEBUG
30 #define NUM_OF_PINS 32
32 struct port_regs {
33 __be32 cpodr; /* Open drain register */
34 __be32 cpdata; /* Data register */
35 __be32 cpdir1; /* Direction register */
36 __be32 cpdir2; /* Direction register */
37 __be32 cppar1; /* Pin assignment register */
38 __be32 cppar2; /* Pin assignment register */
39 #ifdef CONFIG_PPC_85xx
40 u8 pad[8];
41 #endif
44 static struct port_regs *par_io = NULL;
45 static int num_par_io_ports = 0;
47 int par_io_init(struct device_node *np)
49 struct resource res;
50 int ret;
51 const u32 *num_ports;
53 /* Map Parallel I/O ports registers */
54 ret = of_address_to_resource(np, 0, &res);
55 if (ret)
56 return ret;
57 par_io = ioremap(res.start, res.end - res.start + 1);
59 num_ports = of_get_property(np, "num-ports", NULL);
60 if (num_ports)
61 num_par_io_ports = *num_ports;
63 return 0;
66 int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
67 int assignment, int has_irq)
69 u32 pin_mask1bit, pin_mask2bits, new_mask2bits, tmp_val;
71 if (!par_io)
72 return -1;
74 /* calculate pin location for single and 2 bits information */
75 pin_mask1bit = (u32) (1 << (NUM_OF_PINS - (pin + 1)));
77 /* Set open drain, if required */
78 tmp_val = in_be32(&par_io[port].cpodr);
79 if (open_drain)
80 out_be32(&par_io[port].cpodr, pin_mask1bit | tmp_val);
81 else
82 out_be32(&par_io[port].cpodr, ~pin_mask1bit & tmp_val);
84 /* define direction */
85 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
86 in_be32(&par_io[port].cpdir2) :
87 in_be32(&par_io[port].cpdir1);
89 /* get all bits mask for 2 bit per port */
90 pin_mask2bits = (u32) (0x3 << (NUM_OF_PINS -
91 (pin % (NUM_OF_PINS / 2) + 1) * 2));
93 /* Get the final mask we need for the right definition */
94 new_mask2bits = (u32) (dir << (NUM_OF_PINS -
95 (pin % (NUM_OF_PINS / 2) + 1) * 2));
97 /* clear and set 2 bits mask */
98 if (pin > (NUM_OF_PINS / 2) - 1) {
99 out_be32(&par_io[port].cpdir2,
100 ~pin_mask2bits & tmp_val);
101 tmp_val &= ~pin_mask2bits;
102 out_be32(&par_io[port].cpdir2, new_mask2bits | tmp_val);
103 } else {
104 out_be32(&par_io[port].cpdir1,
105 ~pin_mask2bits & tmp_val);
106 tmp_val &= ~pin_mask2bits;
107 out_be32(&par_io[port].cpdir1, new_mask2bits | tmp_val);
109 /* define pin assignment */
110 tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
111 in_be32(&par_io[port].cppar2) :
112 in_be32(&par_io[port].cppar1);
114 new_mask2bits = (u32) (assignment << (NUM_OF_PINS -
115 (pin % (NUM_OF_PINS / 2) + 1) * 2));
116 /* clear and set 2 bits mask */
117 if (pin > (NUM_OF_PINS / 2) - 1) {
118 out_be32(&par_io[port].cppar2,
119 ~pin_mask2bits & tmp_val);
120 tmp_val &= ~pin_mask2bits;
121 out_be32(&par_io[port].cppar2, new_mask2bits | tmp_val);
122 } else {
123 out_be32(&par_io[port].cppar1,
124 ~pin_mask2bits & tmp_val);
125 tmp_val &= ~pin_mask2bits;
126 out_be32(&par_io[port].cppar1, new_mask2bits | tmp_val);
129 return 0;
131 EXPORT_SYMBOL(par_io_config_pin);
133 int par_io_data_set(u8 port, u8 pin, u8 val)
135 u32 pin_mask, tmp_val;
137 if (port >= num_par_io_ports)
138 return -EINVAL;
139 if (pin >= NUM_OF_PINS)
140 return -EINVAL;
141 /* calculate pin location */
142 pin_mask = (u32) (1 << (NUM_OF_PINS - 1 - pin));
144 tmp_val = in_be32(&par_io[port].cpdata);
146 if (val == 0) /* clear */
147 out_be32(&par_io[port].cpdata, ~pin_mask & tmp_val);
148 else /* set */
149 out_be32(&par_io[port].cpdata, pin_mask | tmp_val);
151 return 0;
153 EXPORT_SYMBOL(par_io_data_set);
155 int par_io_of_config(struct device_node *np)
157 struct device_node *pio;
158 const phandle *ph;
159 int pio_map_len;
160 const unsigned int *pio_map;
162 if (par_io == NULL) {
163 printk(KERN_ERR "par_io not initialized \n");
164 return -1;
167 ph = of_get_property(np, "pio-handle", NULL);
168 if (ph == 0) {
169 printk(KERN_ERR "pio-handle not available \n");
170 return -1;
173 pio = of_find_node_by_phandle(*ph);
175 pio_map = of_get_property(pio, "pio-map", &pio_map_len);
176 if (pio_map == NULL) {
177 printk(KERN_ERR "pio-map is not set! \n");
178 return -1;
180 pio_map_len /= sizeof(unsigned int);
181 if ((pio_map_len % 6) != 0) {
182 printk(KERN_ERR "pio-map format wrong! \n");
183 return -1;
186 while (pio_map_len > 0) {
187 par_io_config_pin((u8) pio_map[0], (u8) pio_map[1],
188 (int) pio_map[2], (int) pio_map[3],
189 (int) pio_map[4], (int) pio_map[5]);
190 pio_map += 6;
191 pio_map_len -= 6;
193 of_node_put(pio);
194 return 0;
196 EXPORT_SYMBOL(par_io_of_config);
198 #ifdef DEBUG
199 static void dump_par_io(void)
201 unsigned int i;
203 printk(KERN_INFO "%s: par_io=%p\n", __FUNCTION__, par_io);
204 for (i = 0; i < num_par_io_ports; i++) {
205 printk(KERN_INFO " cpodr[%u]=%08x\n", i,
206 in_be32(&par_io[i].cpodr));
207 printk(KERN_INFO " cpdata[%u]=%08x\n", i,
208 in_be32(&par_io[i].cpdata));
209 printk(KERN_INFO " cpdir1[%u]=%08x\n", i,
210 in_be32(&par_io[i].cpdir1));
211 printk(KERN_INFO " cpdir2[%u]=%08x\n", i,
212 in_be32(&par_io[i].cpdir2));
213 printk(KERN_INFO " cppar1[%u]=%08x\n", i,
214 in_be32(&par_io[i].cppar1));
215 printk(KERN_INFO " cppar2[%u]=%08x\n", i,
216 in_be32(&par_io[i].cppar2));
220 EXPORT_SYMBOL(dump_par_io);
221 #endif /* DEBUG */