Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Common CPM code |
| 3 | * |
| 4 | * Author: Scott Wood <scottwood@freescale.com> |
| 5 | * |
Hongjun Chen | 1661e5b | 2010-03-26 16:43:46 +0800 | [diff] [blame] | 6 | * Copyright 2007-2008,2010 Freescale Semiconductor, Inc. |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 7 | * |
Scott Wood | 15f8c60 | 2007-09-28 14:06:16 -0500 | [diff] [blame] | 8 | * Some parts derived from commproc.c/cpm2_common.c, which is: |
| 9 | * Copyright (c) 1997 Dan error_act (dmalek@jlc.net) |
| 10 | * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com> |
| 11 | * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com) |
| 12 | * 2006 (c) MontaVista Software, Inc. |
| 13 | * Vitaly Bordug <vbordug@ru.mvista.com> |
| 14 | * |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of version 2 of the GNU General Public License as |
| 17 | * published by the Free Software Foundation. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
Scott Wood | 15f8c60 | 2007-09-28 14:06:16 -0500 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 24 | #include <linux/of.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 25 | #include <linux/of_address.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Scott Wood | 15f8c60 | 2007-09-28 14:06:16 -0500 | [diff] [blame] | 27 | |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 28 | #include <asm/udbg.h> |
| 29 | #include <asm/io.h> |
Scott Wood | 15f8c60 | 2007-09-28 14:06:16 -0500 | [diff] [blame] | 30 | #include <asm/cpm.h> |
Christophe Leroy | f86ef74 | 2016-05-17 09:02:43 +0200 | [diff] [blame] | 31 | #include <asm/fixmap.h> |
Zhao Qiang | 7aa1aa6 | 2015-11-30 10:48:57 +0800 | [diff] [blame] | 32 | #include <soc/fsl/qe/qe.h> |
Scott Wood | 15f8c60 | 2007-09-28 14:06:16 -0500 | [diff] [blame] | 33 | |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 34 | #include <mm/mmu_decl.h> |
| 35 | |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 36 | #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO) |
| 37 | #include <linux/of_gpio.h> |
| 38 | #endif |
| 39 | |
Christophe Leroy | 4d486e0 | 2016-08-16 08:26:20 +0200 | [diff] [blame] | 40 | static int __init cpm_init(void) |
| 41 | { |
| 42 | struct device_node *np; |
| 43 | |
| 44 | np = of_find_compatible_node(NULL, NULL, "fsl,cpm1"); |
| 45 | if (!np) |
| 46 | np = of_find_compatible_node(NULL, NULL, "fsl,cpm2"); |
| 47 | if (!np) |
| 48 | return -ENODEV; |
| 49 | cpm_muram_init(); |
| 50 | of_node_put(np); |
| 51 | return 0; |
| 52 | } |
| 53 | subsys_initcall(cpm_init); |
| 54 | |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 55 | #ifdef CONFIG_PPC_EARLY_DEBUG_CPM |
Christophe Leroy | f86ef74 | 2016-05-17 09:02:43 +0200 | [diff] [blame] | 56 | static u32 __iomem *cpm_udbg_txdesc; |
| 57 | static u8 __iomem *cpm_udbg_txbuf; |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 58 | |
| 59 | static void udbg_putc_cpm(char c) |
| 60 | { |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 61 | if (c == '\n') |
Nye Liu | 5e82eb3 | 2008-06-27 13:01:00 -0700 | [diff] [blame] | 62 | udbg_putc_cpm('\r'); |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 63 | |
| 64 | while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000) |
| 65 | ; |
| 66 | |
Christophe Leroy | f86ef74 | 2016-05-17 09:02:43 +0200 | [diff] [blame] | 67 | out_8(cpm_udbg_txbuf, c); |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 68 | out_be32(&cpm_udbg_txdesc[0], 0xa0000001); |
| 69 | } |
| 70 | |
| 71 | void __init udbg_init_cpm(void) |
| 72 | { |
Christophe Leroy | f86ef74 | 2016-05-17 09:02:43 +0200 | [diff] [blame] | 73 | #ifdef CONFIG_PPC_8xx |
| 74 | cpm_udbg_txdesc = (u32 __iomem __force *) |
| 75 | (CONFIG_PPC_EARLY_DEBUG_CPM_ADDR - PHYS_IMMR_BASE + |
| 76 | VIRT_IMMR_BASE); |
| 77 | cpm_udbg_txbuf = (u8 __iomem __force *) |
| 78 | (in_be32(&cpm_udbg_txdesc[1]) - PHYS_IMMR_BASE + |
| 79 | VIRT_IMMR_BASE); |
| 80 | #else |
| 81 | cpm_udbg_txdesc = (u32 __iomem __force *) |
| 82 | CONFIG_PPC_EARLY_DEBUG_CPM_ADDR; |
| 83 | cpm_udbg_txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]); |
| 84 | #endif |
| 85 | |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 86 | if (cpm_udbg_txdesc) { |
| 87 | #ifdef CONFIG_CPM2 |
Benjamin Herrenschmidt | 8d1cf34 | 2009-03-19 19:34:08 +0000 | [diff] [blame] | 88 | setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG); |
Scott Wood | c374e00 | 2007-07-16 11:43:43 -0500 | [diff] [blame] | 89 | #endif |
| 90 | udbg_putc = udbg_putc_cpm; |
| 91 | } |
| 92 | } |
| 93 | #endif |
Scott Wood | 15f8c60 | 2007-09-28 14:06:16 -0500 | [diff] [blame] | 94 | |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 95 | #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO) |
| 96 | |
| 97 | struct cpm2_ioports { |
| 98 | u32 dir, par, sor, odr, dat; |
| 99 | u32 res[3]; |
| 100 | }; |
| 101 | |
| 102 | struct cpm2_gpio32_chip { |
| 103 | struct of_mm_gpio_chip mm_gc; |
| 104 | spinlock_t lock; |
| 105 | |
| 106 | /* shadowed data register to clear/set bits safely */ |
| 107 | u32 cpdata; |
| 108 | }; |
| 109 | |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 110 | static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc) |
| 111 | { |
Christophe Leroy | 41017a7 | 2016-08-11 10:50:40 +0200 | [diff] [blame] | 112 | struct cpm2_gpio32_chip *cpm2_gc = |
| 113 | container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 114 | struct cpm2_ioports __iomem *iop = mm_gc->regs; |
| 115 | |
| 116 | cpm2_gc->cpdata = in_be32(&iop->dat); |
| 117 | } |
| 118 | |
| 119 | static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio) |
| 120 | { |
| 121 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
| 122 | struct cpm2_ioports __iomem *iop = mm_gc->regs; |
| 123 | u32 pin_mask; |
| 124 | |
| 125 | pin_mask = 1 << (31 - gpio); |
| 126 | |
| 127 | return !!(in_be32(&iop->dat) & pin_mask); |
| 128 | } |
| 129 | |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 130 | static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask, |
| 131 | int value) |
| 132 | { |
Linus Walleij | a14a2d4 | 2015-12-08 15:05:43 +0100 | [diff] [blame] | 133 | struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(&mm_gc->gc); |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 134 | struct cpm2_ioports __iomem *iop = mm_gc->regs; |
| 135 | |
| 136 | if (value) |
| 137 | cpm2_gc->cpdata |= pin_mask; |
| 138 | else |
| 139 | cpm2_gc->cpdata &= ~pin_mask; |
| 140 | |
| 141 | out_be32(&iop->dat, cpm2_gc->cpdata); |
| 142 | } |
| 143 | |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 144 | static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value) |
| 145 | { |
| 146 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | a14a2d4 | 2015-12-08 15:05:43 +0100 | [diff] [blame] | 147 | struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc); |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 148 | unsigned long flags; |
| 149 | u32 pin_mask = 1 << (31 - gpio); |
| 150 | |
| 151 | spin_lock_irqsave(&cpm2_gc->lock, flags); |
| 152 | |
| 153 | __cpm2_gpio32_set(mm_gc, pin_mask, value); |
| 154 | |
| 155 | spin_unlock_irqrestore(&cpm2_gc->lock, flags); |
| 156 | } |
| 157 | |
| 158 | static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 159 | { |
| 160 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | a14a2d4 | 2015-12-08 15:05:43 +0100 | [diff] [blame] | 161 | struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 162 | struct cpm2_ioports __iomem *iop = mm_gc->regs; |
| 163 | unsigned long flags; |
| 164 | u32 pin_mask = 1 << (31 - gpio); |
| 165 | |
| 166 | spin_lock_irqsave(&cpm2_gc->lock, flags); |
| 167 | |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 168 | setbits32(&iop->dir, pin_mask); |
| 169 | __cpm2_gpio32_set(mm_gc, pin_mask, val); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 170 | |
| 171 | spin_unlock_irqrestore(&cpm2_gc->lock, flags); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio) |
| 177 | { |
| 178 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | a14a2d4 | 2015-12-08 15:05:43 +0100 | [diff] [blame] | 179 | struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 180 | struct cpm2_ioports __iomem *iop = mm_gc->regs; |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 181 | unsigned long flags; |
| 182 | u32 pin_mask = 1 << (31 - gpio); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 183 | |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 184 | spin_lock_irqsave(&cpm2_gc->lock, flags); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 185 | |
| 186 | clrbits32(&iop->dir, pin_mask); |
| 187 | |
Laurent Pinchart | 639d644 | 2008-08-19 14:20:23 +0200 | [diff] [blame] | 188 | spin_unlock_irqrestore(&cpm2_gc->lock, flags); |
| 189 | |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Christophe Leroy | c095ff9 | 2017-12-13 12:26:23 +0100 | [diff] [blame] | 193 | int cpm2_gpiochip_add32(struct device *dev) |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 194 | { |
Christophe Leroy | c095ff9 | 2017-12-13 12:26:23 +0100 | [diff] [blame] | 195 | struct device_node *np = dev->of_node; |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 196 | struct cpm2_gpio32_chip *cpm2_gc; |
| 197 | struct of_mm_gpio_chip *mm_gc; |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 198 | struct gpio_chip *gc; |
| 199 | |
| 200 | cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL); |
| 201 | if (!cpm2_gc) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | spin_lock_init(&cpm2_gc->lock); |
| 205 | |
| 206 | mm_gc = &cpm2_gc->mm_gc; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 207 | gc = &mm_gc->gc; |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 208 | |
| 209 | mm_gc->save_regs = cpm2_gpio32_save_regs; |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 210 | gc->ngpio = 32; |
| 211 | gc->direction_input = cpm2_gpio32_dir_in; |
| 212 | gc->direction_output = cpm2_gpio32_dir_out; |
| 213 | gc->get = cpm2_gpio32_get; |
| 214 | gc->set = cpm2_gpio32_set; |
Christophe Leroy | c095ff9 | 2017-12-13 12:26:23 +0100 | [diff] [blame] | 215 | gc->parent = dev; |
| 216 | gc->owner = THIS_MODULE; |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 217 | |
Linus Walleij | a14a2d4 | 2015-12-08 15:05:43 +0100 | [diff] [blame] | 218 | return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc); |
Laurent Pinchart | e193325 | 2008-07-28 10:43:22 +0200 | [diff] [blame] | 219 | } |
| 220 | #endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */ |