1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * arch/powerpc/sysdev/qe_lib/qe_io.c
5 * QE Parallel I/O ports configuration routines
7 * Copyright 2006 Freescale Semiconductor, Inc. All rights reserved.
9 * Author: Li Yang <LeoLi@freescale.com>
10 * Based on code from Shlomi Gridish <gridish@freescale.com>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/ioport.h>
20 #include <soc/fsl/qe/qe.h>
24 static struct qe_pio_regs __iomem *par_io;
25 static int num_par_io_ports = 0;
27 int par_io_init(struct device_node *np)
33 /* Map Parallel I/O ports registers */
34 ret = of_address_to_resource(np, 0, &res);
37 par_io = ioremap(res.start, resource_size(&res));
39 num_ports = of_get_property(np, "num-ports", NULL);
41 num_par_io_ports = *num_ports;
46 void __par_io_config_pin(struct qe_pio_regs __iomem *par_io, u8 pin, int dir,
47 int open_drain, int assignment, int has_irq)
54 /* calculate pin location for single and 2 bits information */
55 pin_mask1bit = (u32) (1 << (QE_PIO_PINS - (pin + 1)));
57 /* Set open drain, if required */
58 tmp_val = qe_ioread32be(&par_io->cpodr);
60 qe_iowrite32be(pin_mask1bit | tmp_val, &par_io->cpodr);
62 qe_iowrite32be(~pin_mask1bit & tmp_val, &par_io->cpodr);
64 /* define direction */
65 tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
66 qe_ioread32be(&par_io->cpdir2) :
67 qe_ioread32be(&par_io->cpdir1);
69 /* get all bits mask for 2 bit per port */
70 pin_mask2bits = (u32) (0x3 << (QE_PIO_PINS -
71 (pin % (QE_PIO_PINS / 2) + 1) * 2));
73 /* Get the final mask we need for the right definition */
74 new_mask2bits = (u32) (dir << (QE_PIO_PINS -
75 (pin % (QE_PIO_PINS / 2) + 1) * 2));
77 /* clear and set 2 bits mask */
78 if (pin > (QE_PIO_PINS / 2) - 1) {
79 qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cpdir2);
80 tmp_val &= ~pin_mask2bits;
81 qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cpdir2);
83 qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cpdir1);
84 tmp_val &= ~pin_mask2bits;
85 qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cpdir1);
87 /* define pin assignment */
88 tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
89 qe_ioread32be(&par_io->cppar2) :
90 qe_ioread32be(&par_io->cppar1);
92 new_mask2bits = (u32) (assignment << (QE_PIO_PINS -
93 (pin % (QE_PIO_PINS / 2) + 1) * 2));
94 /* clear and set 2 bits mask */
95 if (pin > (QE_PIO_PINS / 2) - 1) {
96 qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cppar2);
97 tmp_val &= ~pin_mask2bits;
98 qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cppar2);
100 qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cppar1);
101 tmp_val &= ~pin_mask2bits;
102 qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cppar1);
105 EXPORT_SYMBOL(__par_io_config_pin);
107 int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
108 int assignment, int has_irq)
110 if (!par_io || port >= num_par_io_ports)
113 __par_io_config_pin(&par_io[port], pin, dir, open_drain, assignment,
117 EXPORT_SYMBOL(par_io_config_pin);
119 int par_io_data_set(u8 port, u8 pin, u8 val)
121 u32 pin_mask, tmp_val;
123 if (port >= num_par_io_ports)
125 if (pin >= QE_PIO_PINS)
127 /* calculate pin location */
128 pin_mask = (u32) (1 << (QE_PIO_PINS - 1 - pin));
130 tmp_val = qe_ioread32be(&par_io[port].cpdata);
132 if (val == 0) /* clear */
133 qe_iowrite32be(~pin_mask & tmp_val, &par_io[port].cpdata);
135 qe_iowrite32be(pin_mask | tmp_val, &par_io[port].cpdata);
139 EXPORT_SYMBOL(par_io_data_set);
141 int par_io_of_config(struct device_node *np)
143 struct device_node *pio;
145 const unsigned int *pio_map;
147 if (par_io == NULL) {
148 printk(KERN_ERR "par_io not initialized\n");
152 pio = of_parse_phandle(np, "pio-handle", 0);
154 printk(KERN_ERR "pio-handle not available\n");
158 pio_map = of_get_property(pio, "pio-map", &pio_map_len);
159 if (pio_map == NULL) {
160 printk(KERN_ERR "pio-map is not set!\n");
163 pio_map_len /= sizeof(unsigned int);
164 if ((pio_map_len % 6) != 0) {
165 printk(KERN_ERR "pio-map format wrong!\n");
169 while (pio_map_len > 0) {
170 par_io_config_pin((u8) pio_map[0], (u8) pio_map[1],
171 (int) pio_map[2], (int) pio_map[3],
172 (int) pio_map[4], (int) pio_map[5]);
179 EXPORT_SYMBOL(par_io_of_config);