Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux...
[pandora-kernel.git] / arch / powerpc / sysdev / qe_lib / qe_io.c
1 /*
2  * arch/powerpc/sysdev/qe_lib/qe_io.c
3  *
4  * QE Parallel I/O ports configuration routines
5  *
6  * Copyright (C) Freescale Semicondutor, Inc. 2006. All rights reserved.
7  *
8  * Author: Li Yang <LeoLi@freescale.com>
9  * Based on code from Shlomi Gridish <gridish@freescale.com>
10  *
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.
15  */
16
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>
23
24 #include <asm/io.h>
25 #include <asm/qe.h>
26 #include <asm/prom.h>
27 #include <sysdev/fsl_soc.h>
28
29 #undef DEBUG
30
31 #define NUM_OF_PINS     32
32
33 struct port_regs {
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
41         u8      pad[8];
42 #endif
43 };
44
45 static struct port_regs __iomem *par_io;
46 static int num_par_io_ports = 0;
47
48 int par_io_init(struct device_node *np)
49 {
50         struct resource res;
51         int ret;
52         const u32 *num_ports;
53
54         /* Map Parallel I/O ports registers */
55         ret = of_address_to_resource(np, 0, &res);
56         if (ret)
57                 return ret;
58         par_io = ioremap(res.start, res.end - res.start + 1);
59
60         num_ports = of_get_property(np, "num-ports", NULL);
61         if (num_ports)
62                 num_par_io_ports = *num_ports;
63
64         return 0;
65 }
66
67 int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
68                       int assignment, int has_irq)
69 {
70         u32 pin_mask1bit, pin_mask2bits, new_mask2bits, tmp_val;
71
72         if (!par_io)
73                 return -1;
74
75         /* calculate pin location for single and 2 bits information */
76         pin_mask1bit = (u32) (1 << (NUM_OF_PINS - (pin + 1)));
77
78         /* Set open drain, if required */
79         tmp_val = in_be32(&par_io[port].cpodr);
80         if (open_drain)
81                 out_be32(&par_io[port].cpodr, pin_mask1bit | tmp_val);
82         else
83                 out_be32(&par_io[port].cpodr, ~pin_mask1bit & tmp_val);
84
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);
89
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));
93
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));
97
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);
104         } else {
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);
109         }
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);
114
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);
123         } else {
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);
128         }
129
130         return 0;
131 }
132 EXPORT_SYMBOL(par_io_config_pin);
133
134 int par_io_data_set(u8 port, u8 pin, u8 val)
135 {
136         u32 pin_mask, tmp_val;
137
138         if (port >= num_par_io_ports)
139                 return -EINVAL;
140         if (pin >= NUM_OF_PINS)
141                 return -EINVAL;
142         /* calculate pin location */
143         pin_mask = (u32) (1 << (NUM_OF_PINS - 1 - pin));
144
145         tmp_val = in_be32(&par_io[port].cpdata);
146
147         if (val == 0)           /* clear */
148                 out_be32(&par_io[port].cpdata, ~pin_mask & tmp_val);
149         else                    /* set */
150                 out_be32(&par_io[port].cpdata, pin_mask | tmp_val);
151
152         return 0;
153 }
154 EXPORT_SYMBOL(par_io_data_set);
155
156 int par_io_of_config(struct device_node *np)
157 {
158         struct device_node *pio;
159         const phandle *ph;
160         int pio_map_len;
161         const unsigned int *pio_map;
162
163         if (par_io == NULL) {
164                 printk(KERN_ERR "par_io not initialized \n");
165                 return -1;
166         }
167
168         ph = of_get_property(np, "pio-handle", NULL);
169         if (ph == NULL) {
170                 printk(KERN_ERR "pio-handle not available \n");
171                 return -1;
172         }
173
174         pio = of_find_node_by_phandle(*ph);
175
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");
179                 return -1;
180         }
181         pio_map_len /= sizeof(unsigned int);
182         if ((pio_map_len % 6) != 0) {
183                 printk(KERN_ERR "pio-map format wrong! \n");
184                 return -1;
185         }
186
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]);
191                 pio_map += 6;
192                 pio_map_len -= 6;
193         }
194         of_node_put(pio);
195         return 0;
196 }
197 EXPORT_SYMBOL(par_io_of_config);
198
199 #ifdef DEBUG
200 static void dump_par_io(void)
201 {
202         unsigned int i;
203
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));
218         }
219
220 }
221 EXPORT_SYMBOL(dump_par_io);
222 #endif /* DEBUG */