blob: 947f42007734c1c22d5f6eb02ae72f110cd5f44b [file] [log] [blame]
Scott Woodc374e002007-07-16 11:43:43 -05001/*
2 * Common CPM code
3 *
4 * Author: Scott Wood <scottwood@freescale.com>
5 *
Hongjun Chen1661e5b2010-03-26 16:43:46 +08006 * Copyright 2007-2008,2010 Freescale Semiconductor, Inc.
Scott Woodc374e002007-07-16 11:43:43 -05007 *
Scott Wood15f8c602007-09-28 14:06:16 -05008 * 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 Woodc374e002007-07-16 11:43:43 -050015 * 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 Wood15f8c602007-09-28 14:06:16 -050021#include <linux/of_device.h>
Laurent Pincharte1933252008-07-28 10:43:22 +020022#include <linux/spinlock.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040023#include <linux/export.h>
Laurent Pincharte1933252008-07-28 10:43:22 +020024#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050025#include <linux/of_address.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Scott Wood15f8c602007-09-28 14:06:16 -050027
Scott Woodc374e002007-07-16 11:43:43 -050028#include <asm/udbg.h>
29#include <asm/io.h>
Scott Wood15f8c602007-09-28 14:06:16 -050030#include <asm/cpm.h>
Christophe Leroyf86ef742016-05-17 09:02:43 +020031#include <asm/fixmap.h>
Zhao Qiang7aa1aa62015-11-30 10:48:57 +080032#include <soc/fsl/qe/qe.h>
Scott Wood15f8c602007-09-28 14:06:16 -050033
Scott Woodc374e002007-07-16 11:43:43 -050034#include <mm/mmu_decl.h>
35
Laurent Pincharte1933252008-07-28 10:43:22 +020036#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
37#include <linux/of_gpio.h>
38#endif
39
Scott Woodc374e002007-07-16 11:43:43 -050040#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
Christophe Leroyf86ef742016-05-17 09:02:43 +020041static u32 __iomem *cpm_udbg_txdesc;
42static u8 __iomem *cpm_udbg_txbuf;
Scott Woodc374e002007-07-16 11:43:43 -050043
44static void udbg_putc_cpm(char c)
45{
Scott Woodc374e002007-07-16 11:43:43 -050046 if (c == '\n')
Nye Liu5e82eb32008-06-27 13:01:00 -070047 udbg_putc_cpm('\r');
Scott Woodc374e002007-07-16 11:43:43 -050048
49 while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
50 ;
51
Christophe Leroyf86ef742016-05-17 09:02:43 +020052 out_8(cpm_udbg_txbuf, c);
Scott Woodc374e002007-07-16 11:43:43 -050053 out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
54}
55
56void __init udbg_init_cpm(void)
57{
Christophe Leroyf86ef742016-05-17 09:02:43 +020058#ifdef CONFIG_PPC_8xx
59 cpm_udbg_txdesc = (u32 __iomem __force *)
60 (CONFIG_PPC_EARLY_DEBUG_CPM_ADDR - PHYS_IMMR_BASE +
61 VIRT_IMMR_BASE);
62 cpm_udbg_txbuf = (u8 __iomem __force *)
63 (in_be32(&cpm_udbg_txdesc[1]) - PHYS_IMMR_BASE +
64 VIRT_IMMR_BASE);
65#else
66 cpm_udbg_txdesc = (u32 __iomem __force *)
67 CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
68 cpm_udbg_txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
69#endif
70
Scott Woodc374e002007-07-16 11:43:43 -050071 if (cpm_udbg_txdesc) {
72#ifdef CONFIG_CPM2
Benjamin Herrenschmidt8d1cf342009-03-19 19:34:08 +000073 setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG);
Scott Woodc374e002007-07-16 11:43:43 -050074#endif
75 udbg_putc = udbg_putc_cpm;
76 }
77}
78#endif
Scott Wood15f8c602007-09-28 14:06:16 -050079
Laurent Pincharte1933252008-07-28 10:43:22 +020080#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
81
82struct cpm2_ioports {
83 u32 dir, par, sor, odr, dat;
84 u32 res[3];
85};
86
87struct cpm2_gpio32_chip {
88 struct of_mm_gpio_chip mm_gc;
89 spinlock_t lock;
90
91 /* shadowed data register to clear/set bits safely */
92 u32 cpdata;
93};
94
Laurent Pincharte1933252008-07-28 10:43:22 +020095static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
96{
Christophe Leroy41017a72016-08-11 10:50:40 +020097 struct cpm2_gpio32_chip *cpm2_gc =
98 container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc);
Laurent Pincharte1933252008-07-28 10:43:22 +020099 struct cpm2_ioports __iomem *iop = mm_gc->regs;
100
101 cpm2_gc->cpdata = in_be32(&iop->dat);
102}
103
104static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
105{
106 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
107 struct cpm2_ioports __iomem *iop = mm_gc->regs;
108 u32 pin_mask;
109
110 pin_mask = 1 << (31 - gpio);
111
112 return !!(in_be32(&iop->dat) & pin_mask);
113}
114
Laurent Pinchart639d6442008-08-19 14:20:23 +0200115static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
116 int value)
117{
Linus Walleija14a2d42015-12-08 15:05:43 +0100118 struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(&mm_gc->gc);
Laurent Pinchart639d6442008-08-19 14:20:23 +0200119 struct cpm2_ioports __iomem *iop = mm_gc->regs;
120
121 if (value)
122 cpm2_gc->cpdata |= pin_mask;
123 else
124 cpm2_gc->cpdata &= ~pin_mask;
125
126 out_be32(&iop->dat, cpm2_gc->cpdata);
127}
128
Laurent Pincharte1933252008-07-28 10:43:22 +0200129static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
130{
131 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Linus Walleija14a2d42015-12-08 15:05:43 +0100132 struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
Laurent Pinchart639d6442008-08-19 14:20:23 +0200133 unsigned long flags;
134 u32 pin_mask = 1 << (31 - gpio);
135
136 spin_lock_irqsave(&cpm2_gc->lock, flags);
137
138 __cpm2_gpio32_set(mm_gc, pin_mask, value);
139
140 spin_unlock_irqrestore(&cpm2_gc->lock, flags);
141}
142
143static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
144{
145 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Linus Walleija14a2d42015-12-08 15:05:43 +0100146 struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
Laurent Pincharte1933252008-07-28 10:43:22 +0200147 struct cpm2_ioports __iomem *iop = mm_gc->regs;
148 unsigned long flags;
149 u32 pin_mask = 1 << (31 - gpio);
150
151 spin_lock_irqsave(&cpm2_gc->lock, flags);
152
Laurent Pinchart639d6442008-08-19 14:20:23 +0200153 setbits32(&iop->dir, pin_mask);
154 __cpm2_gpio32_set(mm_gc, pin_mask, val);
Laurent Pincharte1933252008-07-28 10:43:22 +0200155
156 spin_unlock_irqrestore(&cpm2_gc->lock, flags);
Laurent Pincharte1933252008-07-28 10:43:22 +0200157
158 return 0;
159}
160
161static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
162{
163 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Linus Walleija14a2d42015-12-08 15:05:43 +0100164 struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
Laurent Pincharte1933252008-07-28 10:43:22 +0200165 struct cpm2_ioports __iomem *iop = mm_gc->regs;
Laurent Pinchart639d6442008-08-19 14:20:23 +0200166 unsigned long flags;
167 u32 pin_mask = 1 << (31 - gpio);
Laurent Pincharte1933252008-07-28 10:43:22 +0200168
Laurent Pinchart639d6442008-08-19 14:20:23 +0200169 spin_lock_irqsave(&cpm2_gc->lock, flags);
Laurent Pincharte1933252008-07-28 10:43:22 +0200170
171 clrbits32(&iop->dir, pin_mask);
172
Laurent Pinchart639d6442008-08-19 14:20:23 +0200173 spin_unlock_irqrestore(&cpm2_gc->lock, flags);
174
Laurent Pincharte1933252008-07-28 10:43:22 +0200175 return 0;
176}
177
178int cpm2_gpiochip_add32(struct device_node *np)
179{
180 struct cpm2_gpio32_chip *cpm2_gc;
181 struct of_mm_gpio_chip *mm_gc;
Laurent Pincharte1933252008-07-28 10:43:22 +0200182 struct gpio_chip *gc;
183
184 cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL);
185 if (!cpm2_gc)
186 return -ENOMEM;
187
188 spin_lock_init(&cpm2_gc->lock);
189
190 mm_gc = &cpm2_gc->mm_gc;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600191 gc = &mm_gc->gc;
Laurent Pincharte1933252008-07-28 10:43:22 +0200192
193 mm_gc->save_regs = cpm2_gpio32_save_regs;
Laurent Pincharte1933252008-07-28 10:43:22 +0200194 gc->ngpio = 32;
195 gc->direction_input = cpm2_gpio32_dir_in;
196 gc->direction_output = cpm2_gpio32_dir_out;
197 gc->get = cpm2_gpio32_get;
198 gc->set = cpm2_gpio32_set;
199
Linus Walleija14a2d42015-12-08 15:05:43 +0100200 return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc);
Laurent Pincharte1933252008-07-28 10:43:22 +0200201}
202#endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */