xref: /linux/drivers/soc/fsl/qe/qe_io.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27aa1aa6eSZhao Qiang /*
37aa1aa6eSZhao Qiang  * arch/powerpc/sysdev/qe_lib/qe_io.c
47aa1aa6eSZhao Qiang  *
57aa1aa6eSZhao Qiang  * QE Parallel I/O ports configuration routines
67aa1aa6eSZhao Qiang  *
77aa1aa6eSZhao Qiang  * Copyright 2006 Freescale Semiconductor, Inc. All rights reserved.
87aa1aa6eSZhao Qiang  *
97aa1aa6eSZhao Qiang  * Author: Li Yang <LeoLi@freescale.com>
107aa1aa6eSZhao Qiang  * Based on code from Shlomi Gridish <gridish@freescale.com>
117aa1aa6eSZhao Qiang  */
127aa1aa6eSZhao Qiang 
137aa1aa6eSZhao Qiang #include <linux/stddef.h>
147aa1aa6eSZhao Qiang #include <linux/kernel.h>
157aa1aa6eSZhao Qiang #include <linux/errno.h>
167aa1aa6eSZhao Qiang #include <linux/module.h>
177aa1aa6eSZhao Qiang #include <linux/ioport.h>
187aa1aa6eSZhao Qiang 
197aa1aa6eSZhao Qiang #include <asm/io.h>
207aa1aa6eSZhao Qiang #include <soc/fsl/qe/qe.h>
217aa1aa6eSZhao Qiang 
227aa1aa6eSZhao Qiang #undef DEBUG
237aa1aa6eSZhao Qiang 
247aa1aa6eSZhao Qiang static struct qe_pio_regs __iomem *par_io;
257aa1aa6eSZhao Qiang static int num_par_io_ports = 0;
267aa1aa6eSZhao Qiang 
par_io_init(struct device_node * np)277aa1aa6eSZhao Qiang int par_io_init(struct device_node *np)
287aa1aa6eSZhao Qiang {
297aa1aa6eSZhao Qiang 	struct resource res;
307aa1aa6eSZhao Qiang 	int ret;
31b3f4ff62SRasmus Villemoes 	u32 num_ports;
327aa1aa6eSZhao Qiang 
337aa1aa6eSZhao Qiang 	/* Map Parallel I/O ports registers */
347aa1aa6eSZhao Qiang 	ret = of_address_to_resource(np, 0, &res);
357aa1aa6eSZhao Qiang 	if (ret)
367aa1aa6eSZhao Qiang 		return ret;
377aa1aa6eSZhao Qiang 	par_io = ioremap(res.start, resource_size(&res));
38*a222fd85SJiasheng Jiang 	if (!par_io)
39*a222fd85SJiasheng Jiang 		return -ENOMEM;
407aa1aa6eSZhao Qiang 
41b3f4ff62SRasmus Villemoes 	if (!of_property_read_u32(np, "num-ports", &num_ports))
42b3f4ff62SRasmus Villemoes 		num_par_io_ports = num_ports;
437aa1aa6eSZhao Qiang 
447aa1aa6eSZhao Qiang 	return 0;
457aa1aa6eSZhao Qiang }
467aa1aa6eSZhao Qiang 
__par_io_config_pin(struct qe_pio_regs __iomem * par_io,u8 pin,int dir,int open_drain,int assignment,int has_irq)477aa1aa6eSZhao Qiang void __par_io_config_pin(struct qe_pio_regs __iomem *par_io, u8 pin, int dir,
487aa1aa6eSZhao Qiang 			 int open_drain, int assignment, int has_irq)
497aa1aa6eSZhao Qiang {
507aa1aa6eSZhao Qiang 	u32 pin_mask1bit;
517aa1aa6eSZhao Qiang 	u32 pin_mask2bits;
527aa1aa6eSZhao Qiang 	u32 new_mask2bits;
537aa1aa6eSZhao Qiang 	u32 tmp_val;
547aa1aa6eSZhao Qiang 
557aa1aa6eSZhao Qiang 	/* calculate pin location for single and 2 bits information */
567aa1aa6eSZhao Qiang 	pin_mask1bit = (u32) (1 << (QE_PIO_PINS - (pin + 1)));
577aa1aa6eSZhao Qiang 
587aa1aa6eSZhao Qiang 	/* Set open drain, if required */
593f39f38eSChristophe Leroy 	tmp_val = ioread32be(&par_io->cpodr);
607aa1aa6eSZhao Qiang 	if (open_drain)
613f39f38eSChristophe Leroy 		iowrite32be(pin_mask1bit | tmp_val, &par_io->cpodr);
627aa1aa6eSZhao Qiang 	else
633f39f38eSChristophe Leroy 		iowrite32be(~pin_mask1bit & tmp_val, &par_io->cpodr);
647aa1aa6eSZhao Qiang 
657aa1aa6eSZhao Qiang 	/* define direction */
667aa1aa6eSZhao Qiang 	tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
673f39f38eSChristophe Leroy 		ioread32be(&par_io->cpdir2) :
683f39f38eSChristophe Leroy 		ioread32be(&par_io->cpdir1);
697aa1aa6eSZhao Qiang 
707aa1aa6eSZhao Qiang 	/* get all bits mask for 2 bit per port */
717aa1aa6eSZhao Qiang 	pin_mask2bits = (u32) (0x3 << (QE_PIO_PINS -
727aa1aa6eSZhao Qiang 				(pin % (QE_PIO_PINS / 2) + 1) * 2));
737aa1aa6eSZhao Qiang 
747aa1aa6eSZhao Qiang 	/* Get the final mask we need for the right definition */
757aa1aa6eSZhao Qiang 	new_mask2bits = (u32) (dir << (QE_PIO_PINS -
767aa1aa6eSZhao Qiang 				(pin % (QE_PIO_PINS / 2) + 1) * 2));
777aa1aa6eSZhao Qiang 
787aa1aa6eSZhao Qiang 	/* clear and set 2 bits mask */
797aa1aa6eSZhao Qiang 	if (pin > (QE_PIO_PINS / 2) - 1) {
803f39f38eSChristophe Leroy 		iowrite32be(~pin_mask2bits & tmp_val, &par_io->cpdir2);
817aa1aa6eSZhao Qiang 		tmp_val &= ~pin_mask2bits;
823f39f38eSChristophe Leroy 		iowrite32be(new_mask2bits | tmp_val, &par_io->cpdir2);
837aa1aa6eSZhao Qiang 	} else {
843f39f38eSChristophe Leroy 		iowrite32be(~pin_mask2bits & tmp_val, &par_io->cpdir1);
857aa1aa6eSZhao Qiang 		tmp_val &= ~pin_mask2bits;
863f39f38eSChristophe Leroy 		iowrite32be(new_mask2bits | tmp_val, &par_io->cpdir1);
877aa1aa6eSZhao Qiang 	}
887aa1aa6eSZhao Qiang 	/* define pin assignment */
897aa1aa6eSZhao Qiang 	tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
903f39f38eSChristophe Leroy 		ioread32be(&par_io->cppar2) :
913f39f38eSChristophe Leroy 		ioread32be(&par_io->cppar1);
927aa1aa6eSZhao Qiang 
937aa1aa6eSZhao Qiang 	new_mask2bits = (u32) (assignment << (QE_PIO_PINS -
947aa1aa6eSZhao Qiang 			(pin % (QE_PIO_PINS / 2) + 1) * 2));
957aa1aa6eSZhao Qiang 	/* clear and set 2 bits mask */
967aa1aa6eSZhao Qiang 	if (pin > (QE_PIO_PINS / 2) - 1) {
973f39f38eSChristophe Leroy 		iowrite32be(~pin_mask2bits & tmp_val, &par_io->cppar2);
987aa1aa6eSZhao Qiang 		tmp_val &= ~pin_mask2bits;
993f39f38eSChristophe Leroy 		iowrite32be(new_mask2bits | tmp_val, &par_io->cppar2);
1007aa1aa6eSZhao Qiang 	} else {
1013f39f38eSChristophe Leroy 		iowrite32be(~pin_mask2bits & tmp_val, &par_io->cppar1);
1027aa1aa6eSZhao Qiang 		tmp_val &= ~pin_mask2bits;
1033f39f38eSChristophe Leroy 		iowrite32be(new_mask2bits | tmp_val, &par_io->cppar1);
1047aa1aa6eSZhao Qiang 	}
1057aa1aa6eSZhao Qiang }
1067aa1aa6eSZhao Qiang EXPORT_SYMBOL(__par_io_config_pin);
1077aa1aa6eSZhao Qiang 
par_io_config_pin(u8 port,u8 pin,int dir,int open_drain,int assignment,int has_irq)1087aa1aa6eSZhao Qiang int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
1097aa1aa6eSZhao Qiang 		      int assignment, int has_irq)
1107aa1aa6eSZhao Qiang {
1117aa1aa6eSZhao Qiang 	if (!par_io || port >= num_par_io_ports)
1127aa1aa6eSZhao Qiang 		return -EINVAL;
1137aa1aa6eSZhao Qiang 
1147aa1aa6eSZhao Qiang 	__par_io_config_pin(&par_io[port], pin, dir, open_drain, assignment,
1157aa1aa6eSZhao Qiang 			    has_irq);
1167aa1aa6eSZhao Qiang 	return 0;
1177aa1aa6eSZhao Qiang }
1187aa1aa6eSZhao Qiang EXPORT_SYMBOL(par_io_config_pin);
1197aa1aa6eSZhao Qiang 
par_io_data_set(u8 port,u8 pin,u8 val)1207aa1aa6eSZhao Qiang int par_io_data_set(u8 port, u8 pin, u8 val)
1217aa1aa6eSZhao Qiang {
1227aa1aa6eSZhao Qiang 	u32 pin_mask, tmp_val;
1237aa1aa6eSZhao Qiang 
1247aa1aa6eSZhao Qiang 	if (port >= num_par_io_ports)
1257aa1aa6eSZhao Qiang 		return -EINVAL;
1267aa1aa6eSZhao Qiang 	if (pin >= QE_PIO_PINS)
1277aa1aa6eSZhao Qiang 		return -EINVAL;
1287aa1aa6eSZhao Qiang 	/* calculate pin location */
1297aa1aa6eSZhao Qiang 	pin_mask = (u32) (1 << (QE_PIO_PINS - 1 - pin));
1307aa1aa6eSZhao Qiang 
1313f39f38eSChristophe Leroy 	tmp_val = ioread32be(&par_io[port].cpdata);
1327aa1aa6eSZhao Qiang 
1337aa1aa6eSZhao Qiang 	if (val == 0)		/* clear */
1343f39f38eSChristophe Leroy 		iowrite32be(~pin_mask & tmp_val, &par_io[port].cpdata);
1357aa1aa6eSZhao Qiang 	else			/* set */
1363f39f38eSChristophe Leroy 		iowrite32be(pin_mask | tmp_val, &par_io[port].cpdata);
1377aa1aa6eSZhao Qiang 
1387aa1aa6eSZhao Qiang 	return 0;
1397aa1aa6eSZhao Qiang }
1407aa1aa6eSZhao Qiang EXPORT_SYMBOL(par_io_data_set);
1417aa1aa6eSZhao Qiang 
par_io_of_config(struct device_node * np)1427aa1aa6eSZhao Qiang int par_io_of_config(struct device_node *np)
1437aa1aa6eSZhao Qiang {
1447aa1aa6eSZhao Qiang 	struct device_node *pio;
1457aa1aa6eSZhao Qiang 	int pio_map_len;
1466aef5123SRasmus Villemoes 	const __be32 *pio_map;
1477aa1aa6eSZhao Qiang 
1487aa1aa6eSZhao Qiang 	if (par_io == NULL) {
1497aa1aa6eSZhao Qiang 		printk(KERN_ERR "par_io not initialized\n");
1507aa1aa6eSZhao Qiang 		return -1;
1517aa1aa6eSZhao Qiang 	}
1527aa1aa6eSZhao Qiang 
153abc6311bSRasmus Villemoes 	pio = of_parse_phandle(np, "pio-handle", 0);
154abc6311bSRasmus Villemoes 	if (pio == NULL) {
1557aa1aa6eSZhao Qiang 		printk(KERN_ERR "pio-handle not available\n");
1567aa1aa6eSZhao Qiang 		return -1;
1577aa1aa6eSZhao Qiang 	}
1587aa1aa6eSZhao Qiang 
1597aa1aa6eSZhao Qiang 	pio_map = of_get_property(pio, "pio-map", &pio_map_len);
1607aa1aa6eSZhao Qiang 	if (pio_map == NULL) {
1617aa1aa6eSZhao Qiang 		printk(KERN_ERR "pio-map is not set!\n");
1627aa1aa6eSZhao Qiang 		return -1;
1637aa1aa6eSZhao Qiang 	}
1647aa1aa6eSZhao Qiang 	pio_map_len /= sizeof(unsigned int);
1657aa1aa6eSZhao Qiang 	if ((pio_map_len % 6) != 0) {
1667aa1aa6eSZhao Qiang 		printk(KERN_ERR "pio-map format wrong!\n");
1677aa1aa6eSZhao Qiang 		return -1;
1687aa1aa6eSZhao Qiang 	}
1697aa1aa6eSZhao Qiang 
1707aa1aa6eSZhao Qiang 	while (pio_map_len > 0) {
1716aef5123SRasmus Villemoes 		u8 port        = be32_to_cpu(pio_map[0]);
1726aef5123SRasmus Villemoes 		u8 pin         = be32_to_cpu(pio_map[1]);
1736aef5123SRasmus Villemoes 		int dir        = be32_to_cpu(pio_map[2]);
1746aef5123SRasmus Villemoes 		int open_drain = be32_to_cpu(pio_map[3]);
1756aef5123SRasmus Villemoes 		int assignment = be32_to_cpu(pio_map[4]);
1766aef5123SRasmus Villemoes 		int has_irq    = be32_to_cpu(pio_map[5]);
1776aef5123SRasmus Villemoes 
1786aef5123SRasmus Villemoes 		par_io_config_pin(port, pin, dir, open_drain,
1796aef5123SRasmus Villemoes 				  assignment, has_irq);
1807aa1aa6eSZhao Qiang 		pio_map += 6;
1817aa1aa6eSZhao Qiang 		pio_map_len -= 6;
1827aa1aa6eSZhao Qiang 	}
1837aa1aa6eSZhao Qiang 	of_node_put(pio);
1847aa1aa6eSZhao Qiang 	return 0;
1857aa1aa6eSZhao Qiang }
1867aa1aa6eSZhao Qiang EXPORT_SYMBOL(par_io_of_config);
187