18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/powerpc/sysdev/qe_lib/qe_io.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * QE Parallel I/O ports configuration routines 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright 2006 Freescale Semiconductor, Inc. All rights reserved. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Author: Li Yang <LeoLi@freescale.com> 108c2ecf20Sopenharmony_ci * Based on code from Shlomi Gridish <gridish@freescale.com> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/stddef.h> 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/errno.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/ioport.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <asm/io.h> 208c2ecf20Sopenharmony_ci#include <soc/fsl/qe/qe.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#undef DEBUG 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic struct qe_pio_regs __iomem *par_io; 258c2ecf20Sopenharmony_cistatic int num_par_io_ports = 0; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ciint par_io_init(struct device_node *np) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci struct resource res; 308c2ecf20Sopenharmony_ci int ret; 318c2ecf20Sopenharmony_ci u32 num_ports; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci /* Map Parallel I/O ports registers */ 348c2ecf20Sopenharmony_ci ret = of_address_to_resource(np, 0, &res); 358c2ecf20Sopenharmony_ci if (ret) 368c2ecf20Sopenharmony_ci return ret; 378c2ecf20Sopenharmony_ci par_io = ioremap(res.start, resource_size(&res)); 388c2ecf20Sopenharmony_ci if (!par_io) 398c2ecf20Sopenharmony_ci return -ENOMEM; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci if (!of_property_read_u32(np, "num-ports", &num_ports)) 428c2ecf20Sopenharmony_ci num_par_io_ports = num_ports; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci return 0; 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_civoid __par_io_config_pin(struct qe_pio_regs __iomem *par_io, u8 pin, int dir, 488c2ecf20Sopenharmony_ci int open_drain, int assignment, int has_irq) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci u32 pin_mask1bit; 518c2ecf20Sopenharmony_ci u32 pin_mask2bits; 528c2ecf20Sopenharmony_ci u32 new_mask2bits; 538c2ecf20Sopenharmony_ci u32 tmp_val; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci /* calculate pin location for single and 2 bits information */ 568c2ecf20Sopenharmony_ci pin_mask1bit = (u32) (1 << (QE_PIO_PINS - (pin + 1))); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci /* Set open drain, if required */ 598c2ecf20Sopenharmony_ci tmp_val = qe_ioread32be(&par_io->cpodr); 608c2ecf20Sopenharmony_ci if (open_drain) 618c2ecf20Sopenharmony_ci qe_iowrite32be(pin_mask1bit | tmp_val, &par_io->cpodr); 628c2ecf20Sopenharmony_ci else 638c2ecf20Sopenharmony_ci qe_iowrite32be(~pin_mask1bit & tmp_val, &par_io->cpodr); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* define direction */ 668c2ecf20Sopenharmony_ci tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ? 678c2ecf20Sopenharmony_ci qe_ioread32be(&par_io->cpdir2) : 688c2ecf20Sopenharmony_ci qe_ioread32be(&par_io->cpdir1); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* get all bits mask for 2 bit per port */ 718c2ecf20Sopenharmony_ci pin_mask2bits = (u32) (0x3 << (QE_PIO_PINS - 728c2ecf20Sopenharmony_ci (pin % (QE_PIO_PINS / 2) + 1) * 2)); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci /* Get the final mask we need for the right definition */ 758c2ecf20Sopenharmony_ci new_mask2bits = (u32) (dir << (QE_PIO_PINS - 768c2ecf20Sopenharmony_ci (pin % (QE_PIO_PINS / 2) + 1) * 2)); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci /* clear and set 2 bits mask */ 798c2ecf20Sopenharmony_ci if (pin > (QE_PIO_PINS / 2) - 1) { 808c2ecf20Sopenharmony_ci qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cpdir2); 818c2ecf20Sopenharmony_ci tmp_val &= ~pin_mask2bits; 828c2ecf20Sopenharmony_ci qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cpdir2); 838c2ecf20Sopenharmony_ci } else { 848c2ecf20Sopenharmony_ci qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cpdir1); 858c2ecf20Sopenharmony_ci tmp_val &= ~pin_mask2bits; 868c2ecf20Sopenharmony_ci qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cpdir1); 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci /* define pin assignment */ 898c2ecf20Sopenharmony_ci tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ? 908c2ecf20Sopenharmony_ci qe_ioread32be(&par_io->cppar2) : 918c2ecf20Sopenharmony_ci qe_ioread32be(&par_io->cppar1); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci new_mask2bits = (u32) (assignment << (QE_PIO_PINS - 948c2ecf20Sopenharmony_ci (pin % (QE_PIO_PINS / 2) + 1) * 2)); 958c2ecf20Sopenharmony_ci /* clear and set 2 bits mask */ 968c2ecf20Sopenharmony_ci if (pin > (QE_PIO_PINS / 2) - 1) { 978c2ecf20Sopenharmony_ci qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cppar2); 988c2ecf20Sopenharmony_ci tmp_val &= ~pin_mask2bits; 998c2ecf20Sopenharmony_ci qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cppar2); 1008c2ecf20Sopenharmony_ci } else { 1018c2ecf20Sopenharmony_ci qe_iowrite32be(~pin_mask2bits & tmp_val, &par_io->cppar1); 1028c2ecf20Sopenharmony_ci tmp_val &= ~pin_mask2bits; 1038c2ecf20Sopenharmony_ci qe_iowrite32be(new_mask2bits | tmp_val, &par_io->cppar1); 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__par_io_config_pin); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ciint par_io_config_pin(u8 port, u8 pin, int dir, int open_drain, 1098c2ecf20Sopenharmony_ci int assignment, int has_irq) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci if (!par_io || port >= num_par_io_ports) 1128c2ecf20Sopenharmony_ci return -EINVAL; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci __par_io_config_pin(&par_io[port], pin, dir, open_drain, assignment, 1158c2ecf20Sopenharmony_ci has_irq); 1168c2ecf20Sopenharmony_ci return 0; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(par_io_config_pin); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciint par_io_data_set(u8 port, u8 pin, u8 val) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci u32 pin_mask, tmp_val; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if (port >= num_par_io_ports) 1258c2ecf20Sopenharmony_ci return -EINVAL; 1268c2ecf20Sopenharmony_ci if (pin >= QE_PIO_PINS) 1278c2ecf20Sopenharmony_ci return -EINVAL; 1288c2ecf20Sopenharmony_ci /* calculate pin location */ 1298c2ecf20Sopenharmony_ci pin_mask = (u32) (1 << (QE_PIO_PINS - 1 - pin)); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci tmp_val = qe_ioread32be(&par_io[port].cpdata); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci if (val == 0) /* clear */ 1348c2ecf20Sopenharmony_ci qe_iowrite32be(~pin_mask & tmp_val, &par_io[port].cpdata); 1358c2ecf20Sopenharmony_ci else /* set */ 1368c2ecf20Sopenharmony_ci qe_iowrite32be(pin_mask | tmp_val, &par_io[port].cpdata); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci return 0; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(par_io_data_set); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciint par_io_of_config(struct device_node *np) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci struct device_node *pio; 1458c2ecf20Sopenharmony_ci int pio_map_len; 1468c2ecf20Sopenharmony_ci const __be32 *pio_map; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (par_io == NULL) { 1498c2ecf20Sopenharmony_ci printk(KERN_ERR "par_io not initialized\n"); 1508c2ecf20Sopenharmony_ci return -1; 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci pio = of_parse_phandle(np, "pio-handle", 0); 1548c2ecf20Sopenharmony_ci if (pio == NULL) { 1558c2ecf20Sopenharmony_ci printk(KERN_ERR "pio-handle not available\n"); 1568c2ecf20Sopenharmony_ci return -1; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci pio_map = of_get_property(pio, "pio-map", &pio_map_len); 1608c2ecf20Sopenharmony_ci if (pio_map == NULL) { 1618c2ecf20Sopenharmony_ci printk(KERN_ERR "pio-map is not set!\n"); 1628c2ecf20Sopenharmony_ci return -1; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci pio_map_len /= sizeof(unsigned int); 1658c2ecf20Sopenharmony_ci if ((pio_map_len % 6) != 0) { 1668c2ecf20Sopenharmony_ci printk(KERN_ERR "pio-map format wrong!\n"); 1678c2ecf20Sopenharmony_ci return -1; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci while (pio_map_len > 0) { 1718c2ecf20Sopenharmony_ci u8 port = be32_to_cpu(pio_map[0]); 1728c2ecf20Sopenharmony_ci u8 pin = be32_to_cpu(pio_map[1]); 1738c2ecf20Sopenharmony_ci int dir = be32_to_cpu(pio_map[2]); 1748c2ecf20Sopenharmony_ci int open_drain = be32_to_cpu(pio_map[3]); 1758c2ecf20Sopenharmony_ci int assignment = be32_to_cpu(pio_map[4]); 1768c2ecf20Sopenharmony_ci int has_irq = be32_to_cpu(pio_map[5]); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci par_io_config_pin(port, pin, dir, open_drain, 1798c2ecf20Sopenharmony_ci assignment, has_irq); 1808c2ecf20Sopenharmony_ci pio_map += 6; 1818c2ecf20Sopenharmony_ci pio_map_len -= 6; 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci of_node_put(pio); 1848c2ecf20Sopenharmony_ci return 0; 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ciEXPORT_SYMBOL(par_io_of_config); 187