Path: blob/master/arch/powerpc/sysdev/cpm_common.c
10817 views
/*1* Common CPM code2*3* Author: Scott Wood <[email protected]>4*5* Copyright 2007 Freescale Semiconductor, Inc.6*7* Some parts derived from commproc.c/cpm2_common.c, which is:8* Copyright (c) 1997 Dan error_act ([email protected])9* Copyright (c) 1999-2001 Dan Malek <[email protected]>10* Copyright (c) 2000 MontaVista Software, Inc ([email protected])11* 2006 (c) MontaVista Software, Inc.12* Vitaly Bordug <[email protected]>13*14* This program is free software; you can redistribute it and/or modify15* it under the terms of version 2 of the GNU General Public License as16* published by the Free Software Foundation.17*/1819#include <linux/init.h>20#include <linux/of_device.h>21#include <linux/spinlock.h>22#include <linux/of.h>23#include <linux/slab.h>2425#include <asm/udbg.h>26#include <asm/io.h>27#include <asm/system.h>28#include <asm/rheap.h>29#include <asm/cpm.h>3031#include <mm/mmu_decl.h>3233#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)34#include <linux/of_gpio.h>35#endif3637#ifdef CONFIG_PPC_EARLY_DEBUG_CPM38static u32 __iomem *cpm_udbg_txdesc =39(u32 __iomem __force *)CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;4041static void udbg_putc_cpm(char c)42{43u8 __iomem *txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);4445if (c == '\n')46udbg_putc_cpm('\r');4748while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)49;5051out_8(txbuf, c);52out_be32(&cpm_udbg_txdesc[0], 0xa0000001);53}5455void __init udbg_init_cpm(void)56{57if (cpm_udbg_txdesc) {58#ifdef CONFIG_CPM259setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG);60#endif61udbg_putc = udbg_putc_cpm;62}63}64#endif6566static spinlock_t cpm_muram_lock;67static rh_block_t cpm_boot_muram_rh_block[16];68static rh_info_t cpm_muram_info;69static u8 __iomem *muram_vbase;70static phys_addr_t muram_pbase;7172/* Max address size we deal with */73#define OF_MAX_ADDR_CELLS 47475int cpm_muram_init(void)76{77struct device_node *np;78struct resource r;79u32 zero[OF_MAX_ADDR_CELLS] = {};80resource_size_t max = 0;81int i = 0;82int ret = 0;8384if (muram_pbase)85return 0;8687spin_lock_init(&cpm_muram_lock);88/* initialize the info header */89rh_init(&cpm_muram_info, 1,90sizeof(cpm_boot_muram_rh_block) /91sizeof(cpm_boot_muram_rh_block[0]),92cpm_boot_muram_rh_block);9394np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");95if (!np) {96/* try legacy bindings */97np = of_find_node_by_name(NULL, "data-only");98if (!np) {99printk(KERN_ERR "Cannot find CPM muram data node");100ret = -ENODEV;101goto out;102}103}104105muram_pbase = of_translate_address(np, zero);106if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {107printk(KERN_ERR "Cannot translate zero through CPM muram node");108ret = -ENODEV;109goto out;110}111112while (of_address_to_resource(np, i++, &r) == 0) {113if (r.end > max)114max = r.end;115116rh_attach_region(&cpm_muram_info, r.start - muram_pbase,117r.end - r.start + 1);118}119120muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);121if (!muram_vbase) {122printk(KERN_ERR "Cannot map CPM muram");123ret = -ENOMEM;124}125126out:127of_node_put(np);128return ret;129}130131/**132* cpm_muram_alloc - allocate the requested size worth of multi-user ram133* @size: number of bytes to allocate134* @align: requested alignment, in bytes135*136* This function returns an offset into the muram area.137* Use cpm_dpram_addr() to get the virtual address of the area.138* Use cpm_muram_free() to free the allocation.139*/140unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)141{142unsigned long start;143unsigned long flags;144145spin_lock_irqsave(&cpm_muram_lock, flags);146cpm_muram_info.alignment = align;147start = rh_alloc(&cpm_muram_info, size, "commproc");148spin_unlock_irqrestore(&cpm_muram_lock, flags);149150return start;151}152EXPORT_SYMBOL(cpm_muram_alloc);153154/**155* cpm_muram_free - free a chunk of multi-user ram156* @offset: The beginning of the chunk as returned by cpm_muram_alloc().157*/158int cpm_muram_free(unsigned long offset)159{160int ret;161unsigned long flags;162163spin_lock_irqsave(&cpm_muram_lock, flags);164ret = rh_free(&cpm_muram_info, offset);165spin_unlock_irqrestore(&cpm_muram_lock, flags);166167return ret;168}169EXPORT_SYMBOL(cpm_muram_free);170171/**172* cpm_muram_alloc_fixed - reserve a specific region of multi-user ram173* @offset: the offset into the muram area to reserve174* @size: the number of bytes to reserve175*176* This function returns "start" on success, -ENOMEM on failure.177* Use cpm_dpram_addr() to get the virtual address of the area.178* Use cpm_muram_free() to free the allocation.179*/180unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)181{182unsigned long start;183unsigned long flags;184185spin_lock_irqsave(&cpm_muram_lock, flags);186cpm_muram_info.alignment = 1;187start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");188spin_unlock_irqrestore(&cpm_muram_lock, flags);189190return start;191}192EXPORT_SYMBOL(cpm_muram_alloc_fixed);193194/**195* cpm_muram_addr - turn a muram offset into a virtual address196* @offset: muram offset to convert197*/198void __iomem *cpm_muram_addr(unsigned long offset)199{200return muram_vbase + offset;201}202EXPORT_SYMBOL(cpm_muram_addr);203204unsigned long cpm_muram_offset(void __iomem *addr)205{206return addr - (void __iomem *)muram_vbase;207}208EXPORT_SYMBOL(cpm_muram_offset);209210/**211* cpm_muram_dma - turn a muram virtual address into a DMA address212* @offset: virtual address from cpm_muram_addr() to convert213*/214dma_addr_t cpm_muram_dma(void __iomem *addr)215{216return muram_pbase + ((u8 __iomem *)addr - muram_vbase);217}218EXPORT_SYMBOL(cpm_muram_dma);219220#if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)221222struct cpm2_ioports {223u32 dir, par, sor, odr, dat;224u32 res[3];225};226227struct cpm2_gpio32_chip {228struct of_mm_gpio_chip mm_gc;229spinlock_t lock;230231/* shadowed data register to clear/set bits safely */232u32 cpdata;233};234235static inline struct cpm2_gpio32_chip *236to_cpm2_gpio32_chip(struct of_mm_gpio_chip *mm_gc)237{238return container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc);239}240241static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)242{243struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);244struct cpm2_ioports __iomem *iop = mm_gc->regs;245246cpm2_gc->cpdata = in_be32(&iop->dat);247}248249static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)250{251struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);252struct cpm2_ioports __iomem *iop = mm_gc->regs;253u32 pin_mask;254255pin_mask = 1 << (31 - gpio);256257return !!(in_be32(&iop->dat) & pin_mask);258}259260static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,261int value)262{263struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);264struct cpm2_ioports __iomem *iop = mm_gc->regs;265266if (value)267cpm2_gc->cpdata |= pin_mask;268else269cpm2_gc->cpdata &= ~pin_mask;270271out_be32(&iop->dat, cpm2_gc->cpdata);272}273274static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)275{276struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);277struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);278unsigned long flags;279u32 pin_mask = 1 << (31 - gpio);280281spin_lock_irqsave(&cpm2_gc->lock, flags);282283__cpm2_gpio32_set(mm_gc, pin_mask, value);284285spin_unlock_irqrestore(&cpm2_gc->lock, flags);286}287288static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)289{290struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);291struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);292struct cpm2_ioports __iomem *iop = mm_gc->regs;293unsigned long flags;294u32 pin_mask = 1 << (31 - gpio);295296spin_lock_irqsave(&cpm2_gc->lock, flags);297298setbits32(&iop->dir, pin_mask);299__cpm2_gpio32_set(mm_gc, pin_mask, val);300301spin_unlock_irqrestore(&cpm2_gc->lock, flags);302303return 0;304}305306static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)307{308struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);309struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);310struct cpm2_ioports __iomem *iop = mm_gc->regs;311unsigned long flags;312u32 pin_mask = 1 << (31 - gpio);313314spin_lock_irqsave(&cpm2_gc->lock, flags);315316clrbits32(&iop->dir, pin_mask);317318spin_unlock_irqrestore(&cpm2_gc->lock, flags);319320return 0;321}322323int cpm2_gpiochip_add32(struct device_node *np)324{325struct cpm2_gpio32_chip *cpm2_gc;326struct of_mm_gpio_chip *mm_gc;327struct gpio_chip *gc;328329cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL);330if (!cpm2_gc)331return -ENOMEM;332333spin_lock_init(&cpm2_gc->lock);334335mm_gc = &cpm2_gc->mm_gc;336gc = &mm_gc->gc;337338mm_gc->save_regs = cpm2_gpio32_save_regs;339gc->ngpio = 32;340gc->direction_input = cpm2_gpio32_dir_in;341gc->direction_output = cpm2_gpio32_dir_out;342gc->get = cpm2_gpio32_get;343gc->set = cpm2_gpio32_set;344345return of_mm_gpiochip_add(np, mm_gc);346}347#endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */348349350