1/*
2 *  Driver for CPM (SCC/SMC) serial ports; CPM1 definitions
3 *
4 *  Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
5 *              Pantelis Antoniou (panto@intracom.gr) (CPM1)
6 *
7 *  Copyright (C) 2004 Freescale Semiconductor, Inc.
8 *            (C) 2004 Intracom, S.A.
9 *            (C) 2006 MontaVista Software, Inc.
10 *		Vitaly Bordug <vbordug@ru.mvista.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/tty.h>
30#include <linux/gfp.h>
31#include <linux/ioport.h>
32#include <linux/serial.h>
33#include <linux/console.h>
34#include <linux/sysrq.h>
35#include <linux/device.h>
36#include <linux/bootmem.h>
37#include <linux/dma-mapping.h>
38
39#include <asm/io.h>
40#include <asm/irq.h>
41#include <asm/fs_pd.h>
42
43#include <linux/serial_core.h>
44#include <linux/kernel.h>
45
46#include <linux/of.h>
47#include <linux/of_address.h>
48
49#include "cpm_uart.h"
50
51/**************************************************************/
52
53void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
54{
55	cpm_command(port->command, cmd);
56}
57
58void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
59				struct device_node *np)
60{
61	return of_iomap(np, 1);
62}
63
64void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
65{
66	iounmap(pram);
67}
68
69/*
70 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
71 * receive buffer descriptors from dual port ram, and a character
72 * buffer area from host mem. If we are allocating for the console we need
73 * to do it from bootmem
74 */
75int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
76{
77	int dpmemsz, memsz;
78	u8 *dp_mem;
79	unsigned long dp_offset;
80	u8 *mem_addr;
81	dma_addr_t dma_addr = 0;
82
83	pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
84
85	dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
86	dp_offset = cpm_dpalloc(dpmemsz, 8);
87	if (IS_ERR_VALUE(dp_offset)) {
88		printk(KERN_ERR
89		       "cpm_uart_cpm1.c: could not allocate buffer descriptors\n");
90		return -ENOMEM;
91	}
92	dp_mem = cpm_dpram_addr(dp_offset);
93
94	memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
95	    L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
96	if (is_con) {
97		/* was hostalloc but changed cause it blows away the */
98		/* large tlb mapping when pinning the kernel area    */
99		mem_addr = (u8 *) cpm_dpram_addr(cpm_dpalloc(memsz, 8));
100		dma_addr = (u32)cpm_dpram_phys(mem_addr);
101	} else
102		mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
103					      GFP_KERNEL);
104
105	if (mem_addr == NULL) {
106		cpm_dpfree(dp_offset);
107		printk(KERN_ERR
108		       "cpm_uart_cpm1.c: could not allocate coherent memory\n");
109		return -ENOMEM;
110	}
111
112	pinfo->dp_addr = dp_offset;
113	pinfo->mem_addr = mem_addr;             /*  virtual address*/
114	pinfo->dma_addr = dma_addr;             /*  physical address*/
115	pinfo->mem_size = memsz;
116
117	pinfo->rx_buf = mem_addr;
118	pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
119						       * pinfo->rx_fifosize);
120
121	pinfo->rx_bd_base = (cbd_t __iomem __force *)dp_mem;
122	pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
123
124	return 0;
125}
126
127void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
128{
129	dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
130							  pinfo->rx_fifosize) +
131			  L1_CACHE_ALIGN(pinfo->tx_nrfifos *
132					 pinfo->tx_fifosize), pinfo->mem_addr,
133			  pinfo->dma_addr);
134
135	cpm_dpfree(pinfo->dp_addr);
136}
137