18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Common CPM code 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Author: Scott Wood <scottwood@freescale.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright 2007-2008,2010 Freescale Semiconductor, Inc. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Some parts derived from commproc.c/cpm2_common.c, which is: 108c2ecf20Sopenharmony_ci * Copyright (c) 1997 Dan error_act (dmalek@jlc.net) 118c2ecf20Sopenharmony_ci * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com> 128c2ecf20Sopenharmony_ci * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com) 138c2ecf20Sopenharmony_ci * 2006 (c) MontaVista Software, Inc. 148c2ecf20Sopenharmony_ci * Vitaly Bordug <vbordug@ru.mvista.com> 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/init.h> 188c2ecf20Sopenharmony_ci#include <linux/of_device.h> 198c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 208c2ecf20Sopenharmony_ci#include <linux/export.h> 218c2ecf20Sopenharmony_ci#include <linux/of.h> 228c2ecf20Sopenharmony_ci#include <linux/of_address.h> 238c2ecf20Sopenharmony_ci#include <linux/slab.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include <asm/udbg.h> 268c2ecf20Sopenharmony_ci#include <asm/io.h> 278c2ecf20Sopenharmony_ci#include <asm/cpm.h> 288c2ecf20Sopenharmony_ci#include <asm/fixmap.h> 298c2ecf20Sopenharmony_ci#include <soc/fsl/qe/qe.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include <mm/mmu_decl.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO) 348c2ecf20Sopenharmony_ci#include <linux/of_gpio.h> 358c2ecf20Sopenharmony_ci#endif 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic int __init cpm_init(void) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci struct device_node *np; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci np = of_find_compatible_node(NULL, NULL, "fsl,cpm1"); 428c2ecf20Sopenharmony_ci if (!np) 438c2ecf20Sopenharmony_ci np = of_find_compatible_node(NULL, NULL, "fsl,cpm2"); 448c2ecf20Sopenharmony_ci if (!np) 458c2ecf20Sopenharmony_ci return -ENODEV; 468c2ecf20Sopenharmony_ci cpm_muram_init(); 478c2ecf20Sopenharmony_ci of_node_put(np); 488c2ecf20Sopenharmony_ci return 0; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_cisubsys_initcall(cpm_init); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_EARLY_DEBUG_CPM 538c2ecf20Sopenharmony_cistatic u32 __iomem *cpm_udbg_txdesc; 548c2ecf20Sopenharmony_cistatic u8 __iomem *cpm_udbg_txbuf; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic void udbg_putc_cpm(char c) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci if (c == '\n') 598c2ecf20Sopenharmony_ci udbg_putc_cpm('\r'); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000) 628c2ecf20Sopenharmony_ci ; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci out_8(cpm_udbg_txbuf, c); 658c2ecf20Sopenharmony_ci out_be32(&cpm_udbg_txdesc[0], 0xa0000001); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_civoid __init udbg_init_cpm(void) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_8xx 718c2ecf20Sopenharmony_ci mmu_mapin_immr(); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci cpm_udbg_txdesc = (u32 __iomem __force *) 748c2ecf20Sopenharmony_ci (CONFIG_PPC_EARLY_DEBUG_CPM_ADDR - PHYS_IMMR_BASE + 758c2ecf20Sopenharmony_ci VIRT_IMMR_BASE); 768c2ecf20Sopenharmony_ci cpm_udbg_txbuf = (u8 __iomem __force *) 778c2ecf20Sopenharmony_ci (in_be32(&cpm_udbg_txdesc[1]) - PHYS_IMMR_BASE + 788c2ecf20Sopenharmony_ci VIRT_IMMR_BASE); 798c2ecf20Sopenharmony_ci#else 808c2ecf20Sopenharmony_ci cpm_udbg_txdesc = (u32 __iomem __force *) 818c2ecf20Sopenharmony_ci CONFIG_PPC_EARLY_DEBUG_CPM_ADDR; 828c2ecf20Sopenharmony_ci cpm_udbg_txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]); 838c2ecf20Sopenharmony_ci#endif 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (cpm_udbg_txdesc) { 868c2ecf20Sopenharmony_ci#ifdef CONFIG_CPM2 878c2ecf20Sopenharmony_ci setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG); 888c2ecf20Sopenharmony_ci#endif 898c2ecf20Sopenharmony_ci udbg_putc = udbg_putc_cpm; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci#endif 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO) 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistruct cpm2_ioports { 978c2ecf20Sopenharmony_ci u32 dir, par, sor, odr, dat; 988c2ecf20Sopenharmony_ci u32 res[3]; 998c2ecf20Sopenharmony_ci}; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistruct cpm2_gpio32_chip { 1028c2ecf20Sopenharmony_ci struct of_mm_gpio_chip mm_gc; 1038c2ecf20Sopenharmony_ci spinlock_t lock; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* shadowed data register to clear/set bits safely */ 1068c2ecf20Sopenharmony_ci u32 cpdata; 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct cpm2_gpio32_chip *cpm2_gc = 1128c2ecf20Sopenharmony_ci container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc); 1138c2ecf20Sopenharmony_ci struct cpm2_ioports __iomem *iop = mm_gc->regs; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci cpm2_gc->cpdata = in_be32(&iop->dat); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); 1218c2ecf20Sopenharmony_ci struct cpm2_ioports __iomem *iop = mm_gc->regs; 1228c2ecf20Sopenharmony_ci u32 pin_mask; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci pin_mask = 1 << (31 - gpio); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci return !!(in_be32(&iop->dat) & pin_mask); 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask, 1308c2ecf20Sopenharmony_ci int value) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(&mm_gc->gc); 1338c2ecf20Sopenharmony_ci struct cpm2_ioports __iomem *iop = mm_gc->regs; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (value) 1368c2ecf20Sopenharmony_ci cpm2_gc->cpdata |= pin_mask; 1378c2ecf20Sopenharmony_ci else 1388c2ecf20Sopenharmony_ci cpm2_gc->cpdata &= ~pin_mask; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci out_be32(&iop->dat, cpm2_gc->cpdata); 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); 1468c2ecf20Sopenharmony_ci struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc); 1478c2ecf20Sopenharmony_ci unsigned long flags; 1488c2ecf20Sopenharmony_ci u32 pin_mask = 1 << (31 - gpio); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci spin_lock_irqsave(&cpm2_gc->lock, flags); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci __cpm2_gpio32_set(mm_gc, pin_mask, value); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&cpm2_gc->lock, flags); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); 1608c2ecf20Sopenharmony_ci struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc); 1618c2ecf20Sopenharmony_ci struct cpm2_ioports __iomem *iop = mm_gc->regs; 1628c2ecf20Sopenharmony_ci unsigned long flags; 1638c2ecf20Sopenharmony_ci u32 pin_mask = 1 << (31 - gpio); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci spin_lock_irqsave(&cpm2_gc->lock, flags); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci setbits32(&iop->dir, pin_mask); 1688c2ecf20Sopenharmony_ci __cpm2_gpio32_set(mm_gc, pin_mask, val); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&cpm2_gc->lock, flags); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci return 0; 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); 1788c2ecf20Sopenharmony_ci struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc); 1798c2ecf20Sopenharmony_ci struct cpm2_ioports __iomem *iop = mm_gc->regs; 1808c2ecf20Sopenharmony_ci unsigned long flags; 1818c2ecf20Sopenharmony_ci u32 pin_mask = 1 << (31 - gpio); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci spin_lock_irqsave(&cpm2_gc->lock, flags); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci clrbits32(&iop->dir, pin_mask); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&cpm2_gc->lock, flags); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return 0; 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ciint cpm2_gpiochip_add32(struct device *dev) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 1958c2ecf20Sopenharmony_ci struct cpm2_gpio32_chip *cpm2_gc; 1968c2ecf20Sopenharmony_ci struct of_mm_gpio_chip *mm_gc; 1978c2ecf20Sopenharmony_ci struct gpio_chip *gc; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL); 2008c2ecf20Sopenharmony_ci if (!cpm2_gc) 2018c2ecf20Sopenharmony_ci return -ENOMEM; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci spin_lock_init(&cpm2_gc->lock); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci mm_gc = &cpm2_gc->mm_gc; 2068c2ecf20Sopenharmony_ci gc = &mm_gc->gc; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci mm_gc->save_regs = cpm2_gpio32_save_regs; 2098c2ecf20Sopenharmony_ci gc->ngpio = 32; 2108c2ecf20Sopenharmony_ci gc->direction_input = cpm2_gpio32_dir_in; 2118c2ecf20Sopenharmony_ci gc->direction_output = cpm2_gpio32_dir_out; 2128c2ecf20Sopenharmony_ci gc->get = cpm2_gpio32_get; 2138c2ecf20Sopenharmony_ci gc->set = cpm2_gpio32_set; 2148c2ecf20Sopenharmony_ci gc->parent = dev; 2158c2ecf20Sopenharmony_ci gc->owner = THIS_MODULE; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc); 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci#endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */ 220