blob: 88b9812c854fdc2de978871012eeb3c507717569 [file] [log] [blame]
Scott Woodc374e002007-07-16 11:43:43 -05001/*
2 * Common CPM code
3 *
4 * Author: Scott Wood <scottwood@freescale.com>
5 *
6 * Copyright 2007 Freescale Semiconductor, Inc.
7 *
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>
23#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Scott Wood15f8c602007-09-28 14:06:16 -050025
Scott Woodc374e002007-07-16 11:43:43 -050026#include <asm/udbg.h>
27#include <asm/io.h>
28#include <asm/system.h>
Scott Wood15f8c602007-09-28 14:06:16 -050029#include <asm/rheap.h>
30#include <asm/cpm.h>
31
Scott Woodc374e002007-07-16 11:43:43 -050032#include <mm/mmu_decl.h>
33
Laurent Pincharte1933252008-07-28 10:43:22 +020034#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
35#include <linux/of_gpio.h>
36#endif
37
Scott Woodc374e002007-07-16 11:43:43 -050038#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
39static u32 __iomem *cpm_udbg_txdesc =
40 (u32 __iomem __force *)CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
41
42static void udbg_putc_cpm(char c)
43{
44 u8 __iomem *txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
45
46 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
52 out_8(txbuf, c);
53 out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
54}
55
56void __init udbg_init_cpm(void)
57{
58 if (cpm_udbg_txdesc) {
59#ifdef CONFIG_CPM2
Benjamin Herrenschmidt8d1cf342009-03-19 19:34:08 +000060 setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG);
Scott Woodc374e002007-07-16 11:43:43 -050061#endif
62 udbg_putc = udbg_putc_cpm;
63 }
64}
65#endif
Scott Wood15f8c602007-09-28 14:06:16 -050066
Scott Wood15f8c602007-09-28 14:06:16 -050067static spinlock_t cpm_muram_lock;
68static rh_block_t cpm_boot_muram_rh_block[16];
69static rh_info_t cpm_muram_info;
70static u8 __iomem *muram_vbase;
71static phys_addr_t muram_pbase;
72
73/* Max address size we deal with */
74#define OF_MAX_ADDR_CELLS 4
75
Anton Vorontsov0c7b87b2009-09-16 01:43:52 +040076int cpm_muram_init(void)
Scott Wood15f8c602007-09-28 14:06:16 -050077{
78 struct device_node *np;
79 struct resource r;
80 u32 zero[OF_MAX_ADDR_CELLS] = {};
81 resource_size_t max = 0;
82 int i = 0;
83 int ret = 0;
84
Anton Vorontsov0c7b87b2009-09-16 01:43:52 +040085 if (muram_pbase)
86 return 0;
87
Scott Wood15f8c602007-09-28 14:06:16 -050088 spin_lock_init(&cpm_muram_lock);
89 /* initialize the info header */
90 rh_init(&cpm_muram_info, 1,
91 sizeof(cpm_boot_muram_rh_block) /
92 sizeof(cpm_boot_muram_rh_block[0]),
93 cpm_boot_muram_rh_block);
94
95 np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
96 if (!np) {
Anton Vorontsov5093bb92008-05-23 20:39:06 +040097 /* try legacy bindings */
98 np = of_find_node_by_name(NULL, "data-only");
99 if (!np) {
100 printk(KERN_ERR "Cannot find CPM muram data node");
101 ret = -ENODEV;
102 goto out;
103 }
Scott Wood15f8c602007-09-28 14:06:16 -0500104 }
105
106 muram_pbase = of_translate_address(np, zero);
107 if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
108 printk(KERN_ERR "Cannot translate zero through CPM muram node");
109 ret = -ENODEV;
110 goto out;
111 }
112
113 while (of_address_to_resource(np, i++, &r) == 0) {
114 if (r.end > max)
115 max = r.end;
116
117 rh_attach_region(&cpm_muram_info, r.start - muram_pbase,
118 r.end - r.start + 1);
119 }
120
121 muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
122 if (!muram_vbase) {
123 printk(KERN_ERR "Cannot map CPM muram");
124 ret = -ENOMEM;
125 }
126
127out:
128 of_node_put(np);
129 return ret;
130}
131
132/**
133 * cpm_muram_alloc - allocate the requested size worth of multi-user ram
134 * @size: number of bytes to allocate
135 * @align: requested alignment, in bytes
136 *
137 * This function returns an offset into the muram area.
138 * Use cpm_dpram_addr() to get the virtual address of the area.
139 * Use cpm_muram_free() to free the allocation.
140 */
141unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)
142{
143 unsigned long start;
144 unsigned long flags;
145
146 spin_lock_irqsave(&cpm_muram_lock, flags);
147 cpm_muram_info.alignment = align;
148 start = rh_alloc(&cpm_muram_info, size, "commproc");
149 spin_unlock_irqrestore(&cpm_muram_lock, flags);
150
151 return start;
152}
153EXPORT_SYMBOL(cpm_muram_alloc);
154
155/**
156 * cpm_muram_free - free a chunk of multi-user ram
157 * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
158 */
159int cpm_muram_free(unsigned long offset)
160{
161 int ret;
162 unsigned long flags;
163
164 spin_lock_irqsave(&cpm_muram_lock, flags);
165 ret = rh_free(&cpm_muram_info, offset);
166 spin_unlock_irqrestore(&cpm_muram_lock, flags);
167
168 return ret;
169}
170EXPORT_SYMBOL(cpm_muram_free);
171
172/**
173 * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
174 * @offset: the offset into the muram area to reserve
175 * @size: the number of bytes to reserve
176 *
177 * This function returns "start" on success, -ENOMEM on failure.
178 * Use cpm_dpram_addr() to get the virtual address of the area.
179 * Use cpm_muram_free() to free the allocation.
180 */
181unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
182{
183 unsigned long start;
184 unsigned long flags;
185
186 spin_lock_irqsave(&cpm_muram_lock, flags);
187 cpm_muram_info.alignment = 1;
188 start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");
189 spin_unlock_irqrestore(&cpm_muram_lock, flags);
190
191 return start;
192}
193EXPORT_SYMBOL(cpm_muram_alloc_fixed);
194
195/**
196 * cpm_muram_addr - turn a muram offset into a virtual address
197 * @offset: muram offset to convert
198 */
199void __iomem *cpm_muram_addr(unsigned long offset)
200{
201 return muram_vbase + offset;
202}
203EXPORT_SYMBOL(cpm_muram_addr);
204
Anton Vorontsov5093bb92008-05-23 20:39:06 +0400205unsigned long cpm_muram_offset(void __iomem *addr)
206{
207 return addr - (void __iomem *)muram_vbase;
208}
209EXPORT_SYMBOL(cpm_muram_offset);
210
Scott Wood15f8c602007-09-28 14:06:16 -0500211/**
Scott Wood4c011b1fb2007-10-12 15:19:11 -0500212 * cpm_muram_dma - turn a muram virtual address into a DMA address
Scott Wood15f8c602007-09-28 14:06:16 -0500213 * @offset: virtual address from cpm_muram_addr() to convert
214 */
215dma_addr_t cpm_muram_dma(void __iomem *addr)
216{
217 return muram_pbase + ((u8 __iomem *)addr - muram_vbase);
218}
219EXPORT_SYMBOL(cpm_muram_dma);
Laurent Pincharte1933252008-07-28 10:43:22 +0200220
221#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
222
223struct cpm2_ioports {
224 u32 dir, par, sor, odr, dat;
225 u32 res[3];
226};
227
228struct cpm2_gpio32_chip {
229 struct of_mm_gpio_chip mm_gc;
230 spinlock_t lock;
231
232 /* shadowed data register to clear/set bits safely */
233 u32 cpdata;
234};
235
236static inline struct cpm2_gpio32_chip *
237to_cpm2_gpio32_chip(struct of_mm_gpio_chip *mm_gc)
238{
239 return container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc);
240}
241
242static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
243{
244 struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
245 struct cpm2_ioports __iomem *iop = mm_gc->regs;
246
247 cpm2_gc->cpdata = in_be32(&iop->dat);
248}
249
250static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
251{
252 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
253 struct cpm2_ioports __iomem *iop = mm_gc->regs;
254 u32 pin_mask;
255
256 pin_mask = 1 << (31 - gpio);
257
258 return !!(in_be32(&iop->dat) & pin_mask);
259}
260
Laurent Pinchart639d6442008-08-19 14:20:23 +0200261static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
262 int value)
263{
264 struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
265 struct cpm2_ioports __iomem *iop = mm_gc->regs;
266
267 if (value)
268 cpm2_gc->cpdata |= pin_mask;
269 else
270 cpm2_gc->cpdata &= ~pin_mask;
271
272 out_be32(&iop->dat, cpm2_gc->cpdata);
273}
274
Laurent Pincharte1933252008-07-28 10:43:22 +0200275static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
276{
277 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
278 struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
Laurent Pinchart639d6442008-08-19 14:20:23 +0200279 unsigned long flags;
280 u32 pin_mask = 1 << (31 - gpio);
281
282 spin_lock_irqsave(&cpm2_gc->lock, flags);
283
284 __cpm2_gpio32_set(mm_gc, pin_mask, value);
285
286 spin_unlock_irqrestore(&cpm2_gc->lock, flags);
287}
288
289static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
290{
291 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
292 struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
Laurent Pincharte1933252008-07-28 10:43:22 +0200293 struct cpm2_ioports __iomem *iop = mm_gc->regs;
294 unsigned long flags;
295 u32 pin_mask = 1 << (31 - gpio);
296
297 spin_lock_irqsave(&cpm2_gc->lock, flags);
298
Laurent Pinchart639d6442008-08-19 14:20:23 +0200299 setbits32(&iop->dir, pin_mask);
300 __cpm2_gpio32_set(mm_gc, pin_mask, val);
Laurent Pincharte1933252008-07-28 10:43:22 +0200301
302 spin_unlock_irqrestore(&cpm2_gc->lock, flags);
Laurent Pincharte1933252008-07-28 10:43:22 +0200303
304 return 0;
305}
306
307static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
308{
309 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Laurent Pinchart639d6442008-08-19 14:20:23 +0200310 struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
Laurent Pincharte1933252008-07-28 10:43:22 +0200311 struct cpm2_ioports __iomem *iop = mm_gc->regs;
Laurent Pinchart639d6442008-08-19 14:20:23 +0200312 unsigned long flags;
313 u32 pin_mask = 1 << (31 - gpio);
Laurent Pincharte1933252008-07-28 10:43:22 +0200314
Laurent Pinchart639d6442008-08-19 14:20:23 +0200315 spin_lock_irqsave(&cpm2_gc->lock, flags);
Laurent Pincharte1933252008-07-28 10:43:22 +0200316
317 clrbits32(&iop->dir, pin_mask);
318
Laurent Pinchart639d6442008-08-19 14:20:23 +0200319 spin_unlock_irqrestore(&cpm2_gc->lock, flags);
320
Laurent Pincharte1933252008-07-28 10:43:22 +0200321 return 0;
322}
323
324int cpm2_gpiochip_add32(struct device_node *np)
325{
326 struct cpm2_gpio32_chip *cpm2_gc;
327 struct of_mm_gpio_chip *mm_gc;
328 struct of_gpio_chip *of_gc;
329 struct gpio_chip *gc;
330
331 cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL);
332 if (!cpm2_gc)
333 return -ENOMEM;
334
335 spin_lock_init(&cpm2_gc->lock);
336
337 mm_gc = &cpm2_gc->mm_gc;
338 of_gc = &mm_gc->of_gc;
339 gc = &of_gc->gc;
340
341 mm_gc->save_regs = cpm2_gpio32_save_regs;
342 of_gc->gpio_cells = 2;
343 gc->ngpio = 32;
344 gc->direction_input = cpm2_gpio32_dir_in;
345 gc->direction_output = cpm2_gpio32_dir_out;
346 gc->get = cpm2_gpio32_get;
347 gc->set = cpm2_gpio32_set;
348
349 return of_mm_gpiochip_add(np, mm_gc);
350}
351#endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */