GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / soc / fsl / qe / 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 2006 Freescale Semiconductor, Inc. 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/errno.h>
20 #include <linux/module.h>
21 #include <linux/ioport.h>
22
23 #include <asm/io.h>
24 #include <soc/fsl/qe/qe.h>
25 #include <asm/prom.h>
26 #include <sysdev/fsl_soc.h>
27
28 #undef DEBUG
29
30 static struct qe_pio_regs __iomem *par_io;
31 static int num_par_io_ports = 0;
32
33 int par_io_init(struct device_node *np)
34 {
35         struct resource res;
36         int ret;
37         const u32 *num_ports;
38
39         /* Map Parallel I/O ports registers */
40         ret = of_address_to_resource(np, 0, &res);
41         if (ret)
42                 return ret;
43         par_io = ioremap(res.start, resource_size(&res));
44         if (!par_io)
45                 return -ENOMEM;
46
47         num_ports = of_get_property(np, "num-ports", NULL);
48         if (num_ports)
49                 num_par_io_ports = *num_ports;
50
51         return 0;
52 }
53
54 void __par_io_config_pin(struct qe_pio_regs __iomem *par_io, u8 pin, int dir,
55                          int open_drain, int assignment, int has_irq)
56 {
57         u32 pin_mask1bit;
58         u32 pin_mask2bits;
59         u32 new_mask2bits;
60         u32 tmp_val;
61
62         /* calculate pin location for single and 2 bits information */
63         pin_mask1bit = (u32) (1 << (QE_PIO_PINS - (pin + 1)));
64
65         /* Set open drain, if required */
66         tmp_val = in_be32(&par_io->cpodr);
67         if (open_drain)
68                 out_be32(&par_io->cpodr, pin_mask1bit | tmp_val);
69         else
70                 out_be32(&par_io->cpodr, ~pin_mask1bit & tmp_val);
71
72         /* define direction */
73         tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
74                 in_be32(&par_io->cpdir2) :
75                 in_be32(&par_io->cpdir1);
76
77         /* get all bits mask for 2 bit per port */
78         pin_mask2bits = (u32) (0x3 << (QE_PIO_PINS -
79                                 (pin % (QE_PIO_PINS / 2) + 1) * 2));
80
81         /* Get the final mask we need for the right definition */
82         new_mask2bits = (u32) (dir << (QE_PIO_PINS -
83                                 (pin % (QE_PIO_PINS / 2) + 1) * 2));
84
85         /* clear and set 2 bits mask */
86         if (pin > (QE_PIO_PINS / 2) - 1) {
87                 out_be32(&par_io->cpdir2,
88                          ~pin_mask2bits & tmp_val);
89                 tmp_val &= ~pin_mask2bits;
90                 out_be32(&par_io->cpdir2, new_mask2bits | tmp_val);
91         } else {
92                 out_be32(&par_io->cpdir1,
93                          ~pin_mask2bits & tmp_val);
94                 tmp_val &= ~pin_mask2bits;
95                 out_be32(&par_io->cpdir1, new_mask2bits | tmp_val);
96         }
97         /* define pin assignment */
98         tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
99                 in_be32(&par_io->cppar2) :
100                 in_be32(&par_io->cppar1);
101
102         new_mask2bits = (u32) (assignment << (QE_PIO_PINS -
103                         (pin % (QE_PIO_PINS / 2) + 1) * 2));
104         /* clear and set 2 bits mask */
105         if (pin > (QE_PIO_PINS / 2) - 1) {
106                 out_be32(&par_io->cppar2,
107                          ~pin_mask2bits & tmp_val);
108                 tmp_val &= ~pin_mask2bits;
109                 out_be32(&par_io->cppar2, new_mask2bits | tmp_val);
110         } else {
111                 out_be32(&par_io->cppar1,
112                          ~pin_mask2bits & tmp_val);
113                 tmp_val &= ~pin_mask2bits;
114                 out_be32(&par_io->cppar1, new_mask2bits | tmp_val);
115         }
116 }
117 EXPORT_SYMBOL(__par_io_config_pin);
118
119 int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
120                       int assignment, int has_irq)
121 {
122         if (!par_io || port >= num_par_io_ports)
123                 return -EINVAL;
124
125         __par_io_config_pin(&par_io[port], pin, dir, open_drain, assignment,
126                             has_irq);
127         return 0;
128 }
129 EXPORT_SYMBOL(par_io_config_pin);
130
131 int par_io_data_set(u8 port, u8 pin, u8 val)
132 {
133         u32 pin_mask, tmp_val;
134
135         if (port >= num_par_io_ports)
136                 return -EINVAL;
137         if (pin >= QE_PIO_PINS)
138                 return -EINVAL;
139         /* calculate pin location */
140         pin_mask = (u32) (1 << (QE_PIO_PINS - 1 - pin));
141
142         tmp_val = in_be32(&par_io[port].cpdata);
143
144         if (val == 0)           /* clear */
145                 out_be32(&par_io[port].cpdata, ~pin_mask & tmp_val);
146         else                    /* set */
147                 out_be32(&par_io[port].cpdata, pin_mask | tmp_val);
148
149         return 0;
150 }
151 EXPORT_SYMBOL(par_io_data_set);
152
153 int par_io_of_config(struct device_node *np)
154 {
155         struct device_node *pio;
156         const phandle *ph;
157         int pio_map_len;
158         const unsigned int *pio_map;
159
160         if (par_io == NULL) {
161                 printk(KERN_ERR "par_io not initialized\n");
162                 return -1;
163         }
164
165         ph = of_get_property(np, "pio-handle", NULL);
166         if (ph == NULL) {
167                 printk(KERN_ERR "pio-handle not available\n");
168                 return -1;
169         }
170
171         pio = of_find_node_by_phandle(*ph);
172
173         pio_map = of_get_property(pio, "pio-map", &pio_map_len);
174         if (pio_map == NULL) {
175                 printk(KERN_ERR "pio-map is not set!\n");
176                 return -1;
177         }
178         pio_map_len /= sizeof(unsigned int);
179         if ((pio_map_len % 6) != 0) {
180                 printk(KERN_ERR "pio-map format wrong!\n");
181                 return -1;
182         }
183
184         while (pio_map_len > 0) {
185                 par_io_config_pin((u8) pio_map[0], (u8) pio_map[1],
186                                 (int) pio_map[2], (int) pio_map[3],
187                                 (int) pio_map[4], (int) pio_map[5]);
188                 pio_map += 6;
189                 pio_map_len -= 6;
190         }
191         of_node_put(pio);
192         return 0;
193 }
194 EXPORT_SYMBOL(par_io_of_config);