1/*
2 *  Driver for CPM (SCC/SMC) serial ports; CPM2 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/ioport.h>
31#include <linux/slab.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#include <asm/prom.h>
43
44#include <linux/serial_core.h>
45#include <linux/kernel.h>
46
47#include "cpm_uart.h"
48
49/**************************************************************/
50
51void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
52{
53	cpm_command(port->command, cmd);
54}
55
56void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
57				struct device_node *np)
58{
59	void __iomem *pram;
60	unsigned long offset;
61	struct resource res;
62	resource_size_t len;
63
64	/* Don't remap parameter RAM if it has already been initialized
65	 * during console setup.
66	 */
67	if (IS_SMC(port) && port->smcup)
68		return port->smcup;
69	else if (!IS_SMC(port) && port->sccup)
70		return port->sccup;
71
72	if (of_address_to_resource(np, 1, &res))
73		return NULL;
74
75	len = resource_size(&res);
76	pram = ioremap(res.start, len);
77	if (!pram)
78		return NULL;
79
80	if (!IS_SMC(port))
81		return pram;
82
83	if (len != 2) {
84		printk(KERN_WARNING "cpm_uart[%d]: device tree references "
85			"SMC pram, using boot loader/wrapper pram mapping. "
86			"Please fix your device tree to reference the pram "
87			"base register instead.\n",
88			port->port.line);
89		return pram;
90	}
91
92	offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
93	out_be16(pram, offset);
94	iounmap(pram);
95	return cpm_muram_addr(offset);
96}
97
98void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
99{
100	if (!IS_SMC(port))
101		iounmap(pram);
102}
103
104/*
105 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
106 * receive buffer descriptors from dual port ram, and a character
107 * buffer area from host mem. If we are allocating for the console we need
108 * to do it from bootmem
109 */
110int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
111{
112	int dpmemsz, memsz;
113	u8 __iomem *dp_mem;
114	unsigned long dp_offset;
115	u8 *mem_addr;
116	dma_addr_t dma_addr = 0;
117
118	pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
119
120	dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
121	dp_offset = cpm_dpalloc(dpmemsz, 8);
122	if (IS_ERR_VALUE(dp_offset)) {
123		printk(KERN_ERR
124		       "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
125		return -ENOMEM;
126	}
127
128	dp_mem = cpm_dpram_addr(dp_offset);
129
130	memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
131	    L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
132	if (is_con) {
133		mem_addr = kzalloc(memsz, GFP_NOWAIT);
134		dma_addr = virt_to_bus(mem_addr);
135	}
136	else
137		mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
138					      GFP_KERNEL);
139
140	if (mem_addr == NULL) {
141		cpm_dpfree(dp_offset);
142		printk(KERN_ERR
143		       "cpm_uart_cpm.c: could not allocate coherent memory\n");
144		return -ENOMEM;
145	}
146
147	pinfo->dp_addr = dp_offset;
148	pinfo->mem_addr = mem_addr;
149	pinfo->dma_addr = dma_addr;
150	pinfo->mem_size = memsz;
151
152	pinfo->rx_buf = mem_addr;
153	pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
154						       * pinfo->rx_fifosize);
155
156	pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
157	pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
158
159	return 0;
160}
161
162void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
163{
164	dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
165							  pinfo->rx_fifosize) +
166			  L1_CACHE_ALIGN(pinfo->tx_nrfifos *
167					 pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
168			  pinfo->dma_addr);
169
170	cpm_dpfree(pinfo->dp_addr);
171}
172