1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
4  *
5  *  Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
6  *              Pantelis Antoniou (panto@intracom.gr) (CPM1)
7  *
8  *  Copyright (C) 2004 Freescale Semiconductor, Inc.
9  *            (C) 2004 Intracom, S.A.
10  *            (C) 2006 MontaVista Software, Inc.
11  *		Vitaly Bordug <vbordug@ru.mvista.com>
12  */
13 
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/ioport.h>
17 #include <linux/slab.h>
18 #include <linux/serial.h>
19 #include <linux/console.h>
20 #include <linux/sysrq.h>
21 #include <linux/device.h>
22 #include <linux/memblock.h>
23 #include <linux/dma-mapping.h>
24 
25 #include <asm/io.h>
26 #include <asm/irq.h>
27 #include <asm/fs_pd.h>
28 
29 #include <linux/serial_core.h>
30 #include <linux/kernel.h>
31 
32 #include "cpm_uart.h"
33 
34 /**************************************************************/
35 
cpm_line_cr_cmd(struct uart_cpm_port * port,int cmd)36 void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
37 {
38 	cpm_command(port->command, cmd);
39 }
40 
cpm_uart_map_pram(struct uart_cpm_port * port,struct device_node * np)41 void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
42 				struct device_node *np)
43 {
44 	void __iomem *pram;
45 	unsigned long offset;
46 	struct resource res;
47 	resource_size_t len;
48 
49 	/* Don't remap parameter RAM if it has already been initialized
50 	 * during console setup.
51 	 */
52 	if (IS_SMC(port) && port->smcup)
53 		return port->smcup;
54 	else if (!IS_SMC(port) && port->sccup)
55 		return port->sccup;
56 
57 	if (of_address_to_resource(np, 1, &res))
58 		return NULL;
59 
60 	len = resource_size(&res);
61 	pram = ioremap(res.start, len);
62 	if (!pram)
63 		return NULL;
64 
65 	if (!IS_SMC(port))
66 		return pram;
67 
68 	if (len != 2) {
69 		printk(KERN_WARNING "cpm_uart[%d]: device tree references "
70 			"SMC pram, using boot loader/wrapper pram mapping. "
71 			"Please fix your device tree to reference the pram "
72 			"base register instead.\n",
73 			port->port.line);
74 		return pram;
75 	}
76 
77 	offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
78 	out_be16(pram, offset);
79 	iounmap(pram);
80 	return cpm_muram_addr(offset);
81 }
82 
cpm_uart_unmap_pram(struct uart_cpm_port * port,void __iomem * pram)83 void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
84 {
85 	if (!IS_SMC(port))
86 		iounmap(pram);
87 }
88 
89 /*
90  * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
91  * receive buffer descriptors from dual port ram, and a character
92  * buffer area from host mem. If we are allocating for the console we need
93  * to do it from bootmem
94  */
cpm_uart_allocbuf(struct uart_cpm_port * pinfo,unsigned int is_con)95 int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
96 {
97 	int dpmemsz, memsz;
98 	u8 __iomem *dp_mem;
99 	unsigned long dp_offset;
100 	u8 *mem_addr;
101 	dma_addr_t dma_addr = 0;
102 
103 	pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
104 
105 	dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
106 	dp_offset = cpm_dpalloc(dpmemsz, 8);
107 	if (IS_ERR_VALUE(dp_offset)) {
108 		printk(KERN_ERR
109 		       "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
110 		return -ENOMEM;
111 	}
112 
113 	dp_mem = cpm_dpram_addr(dp_offset);
114 
115 	memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
116 	    L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
117 	if (is_con) {
118 		mem_addr = kzalloc(memsz, GFP_NOWAIT);
119 		dma_addr = virt_to_bus(mem_addr);
120 	}
121 	else
122 		mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
123 					      GFP_KERNEL);
124 
125 	if (mem_addr == NULL) {
126 		cpm_dpfree(dp_offset);
127 		printk(KERN_ERR
128 		       "cpm_uart_cpm.c: could not allocate coherent memory\n");
129 		return -ENOMEM;
130 	}
131 
132 	pinfo->dp_addr = dp_offset;
133 	pinfo->mem_addr = mem_addr;
134 	pinfo->dma_addr = dma_addr;
135 	pinfo->mem_size = memsz;
136 
137 	pinfo->rx_buf = mem_addr;
138 	pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
139 						       * pinfo->rx_fifosize);
140 
141 	pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
142 	pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
143 
144 	return 0;
145 }
146 
cpm_uart_freebuf(struct uart_cpm_port * pinfo)147 void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
148 {
149 	dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
150 							  pinfo->rx_fifosize) +
151 			  L1_CACHE_ALIGN(pinfo->tx_nrfifos *
152 					 pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
153 			  pinfo->dma_addr);
154 
155 	cpm_dpfree(pinfo->dp_addr);
156 }
157