cpm_uart_cpm2.c 12.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 *  linux/drivers/serial/cpm_uart_cpm2.c
 *
 *  Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
 *
6
 *  Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
L
Linus Torvalds 已提交
7
 *              Pantelis Antoniou (panto@intracom.gr) (CPM1)
K
Kumar Gala 已提交
8
 *
L
Linus Torvalds 已提交
9 10
 *  Copyright (C) 2004 Freescale Semiconductor, Inc.
 *            (C) 2004 Intracom, S.A.
11
 *            (C) 2006 MontaVista Software, Inc.
K
Kumar Gala 已提交
12
 *		Vitaly Bordug <vbordug@ru.mvista.com>
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/module.h>
#include <linux/tty.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/serial.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/device.h>
#include <linux/bootmem.h>
#include <linux/dma-mapping.h>

#include <asm/io.h>
#include <asm/irq.h>
43
#include <asm/fs_pd.h>
44 45 46
#ifdef CONFIG_PPC_CPM_NEW_BINDING
#include <asm/prom.h>
#endif
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54

#include <linux/serial_core.h>
#include <linux/kernel.h>

#include "cpm_uart.h"

/**************************************************************/

55 56 57
#ifdef CONFIG_PPC_CPM_NEW_BINDING
void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
{
58
	cpm_command(port->command, cmd);
59
}
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
				struct device_node *np)
{
	void __iomem *pram;
	unsigned long offset;
	struct resource res;
	unsigned long len;

	/* Don't remap parameter RAM if it has already been initialized
	 * during console setup.
	 */
	if (IS_SMC(port) && port->smcup)
		return port->smcup;
	else if (!IS_SMC(port) && port->sccup)
		return port->sccup;

	if (of_address_to_resource(np, 1, &res))
		return NULL;

	len = 1 + res.end - res.start;
	pram = ioremap(res.start, len);
	if (!pram)
		return NULL;

	if (!IS_SMC(port))
		return pram;

	if (len != 2) {
		printk(KERN_WARNING "cpm_uart[%d]: device tree references "
			"SMC pram, using boot loader/wrapper pram mapping. "
			"Please fix your device tree to reference the pram "
			"base register instead.\n",
			port->port.line);
		return pram;
	}

	offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
	out_be16(pram, offset);
	iounmap(pram);
	return cpm_muram_addr(offset);
}

void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
{
	if (!IS_SMC(port))
		iounmap(pram);
}

109 110
#else
void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
L
Linus Torvalds 已提交
111 112
{
	ulong val;
113
	int line = port - cpm_uart_ports;
114 115
	volatile cpm_cpm2_t *cp = cpm2_map(im_cpm);

L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	switch (line) {
	case UART_SMC1:
		val = mk_cr_cmd(CPM_CR_SMC1_PAGE, CPM_CR_SMC1_SBLOCK, 0,
				cmd) | CPM_CR_FLG;
		break;
	case UART_SMC2:
		val = mk_cr_cmd(CPM_CR_SMC2_PAGE, CPM_CR_SMC2_SBLOCK, 0,
				cmd) | CPM_CR_FLG;
		break;
	case UART_SCC1:
		val = mk_cr_cmd(CPM_CR_SCC1_PAGE, CPM_CR_SCC1_SBLOCK, 0,
				cmd) | CPM_CR_FLG;
		break;
	case UART_SCC2:
		val = mk_cr_cmd(CPM_CR_SCC2_PAGE, CPM_CR_SCC2_SBLOCK, 0,
				cmd) | CPM_CR_FLG;
		break;
	case UART_SCC3:
		val = mk_cr_cmd(CPM_CR_SCC3_PAGE, CPM_CR_SCC3_SBLOCK, 0,
				cmd) | CPM_CR_FLG;
		break;
	case UART_SCC4:
		val = mk_cr_cmd(CPM_CR_SCC4_PAGE, CPM_CR_SCC4_SBLOCK, 0,
				cmd) | CPM_CR_FLG;
		break;
	default:
		return;

	}
	cp->cp_cpcr = val;
	while (cp->cp_cpcr & CPM_CR_FLG) ;
148 149

	cpm2_unmap(cp);
L
Linus Torvalds 已提交
150 151 152 153
}

void smc1_lineif(struct uart_cpm_port *pinfo)
{
154 155
	volatile iop_cpm2_t *io = cpm2_map(im_ioport);
	volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163

	/* SMC1 is only on port D */
	io->iop_ppard |= 0x00c00000;
	io->iop_pdird |= 0x00400000;
	io->iop_pdird &= ~0x00800000;
	io->iop_psord &= ~0x00c00000;

	/* Wire BRG1 to SMC1 */
164
	cpmux->cmx_smr &= 0x0f;
L
Linus Torvalds 已提交
165
	pinfo->brg = 1;
166 167 168

	cpm2_unmap(cpmux);
	cpm2_unmap(io);
L
Linus Torvalds 已提交
169 170 171 172
}

void smc2_lineif(struct uart_cpm_port *pinfo)
{
173 174
	volatile iop_cpm2_t *io = cpm2_map(im_ioport);
	volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182

	/* SMC2 is only on port A */
	io->iop_ppara |= 0x00c00000;
	io->iop_pdira |= 0x00400000;
	io->iop_pdira &= ~0x00800000;
	io->iop_psora &= ~0x00c00000;

	/* Wire BRG2 to SMC2 */
183
	cpmux->cmx_smr &= 0xf0;
L
Linus Torvalds 已提交
184
	pinfo->brg = 2;
185 186 187

	cpm2_unmap(cpmux);
	cpm2_unmap(io);
L
Linus Torvalds 已提交
188 189 190 191
}

void scc1_lineif(struct uart_cpm_port *pinfo)
{
192 193
	volatile iop_cpm2_t *io = cpm2_map(im_ioport);
	volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202

	/* Use Port D for SCC1 instead of other functions.  */
	io->iop_ppard |= 0x00000003;
	io->iop_psord &= ~0x00000001;	/* Rx */
	io->iop_psord |= 0x00000002;	/* Tx */
	io->iop_pdird &= ~0x00000001;	/* Rx */
	io->iop_pdird |= 0x00000002;	/* Tx */

	/* Wire BRG1 to SCC1 */
203 204
	cpmux->cmx_scr &= 0x00ffffff;
	cpmux->cmx_scr |= 0x00000000;
L
Linus Torvalds 已提交
205
	pinfo->brg = 1;
206 207 208

	cpm2_unmap(cpmux);
	cpm2_unmap(io);
L
Linus Torvalds 已提交
209 210 211 212
}

void scc2_lineif(struct uart_cpm_port *pinfo)
{
213 214 215 216 217 218 219
	/*
	 * STx GP3 uses the SCC2 secondary option pin assignment
	 * which this driver doesn't account for in the static
	 * pin assignments. This kind of board specific info
	 * really has to get out of the driver so boards can
	 * be supported in a sane fashion.
	 */
220
	volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
221
#ifndef CONFIG_STX_GP3
222 223
	volatile iop_cpm2_t *io = cpm2_map(im_ioport);

L
Linus Torvalds 已提交
224 225 226 227 228
	io->iop_pparb |= 0x008b0000;
	io->iop_pdirb |= 0x00880000;
	io->iop_psorb |= 0x00880000;
	io->iop_pdirb &= ~0x00030000;
	io->iop_psorb &= ~0x00030000;
229
#endif
230 231
	cpmux->cmx_scr &= 0xff00ffff;
	cpmux->cmx_scr |= 0x00090000;
L
Linus Torvalds 已提交
232
	pinfo->brg = 2;
233 234 235

	cpm2_unmap(cpmux);
	cpm2_unmap(io);
L
Linus Torvalds 已提交
236 237 238 239
}

void scc3_lineif(struct uart_cpm_port *pinfo)
{
240 241 242
	volatile iop_cpm2_t *io = cpm2_map(im_ioport);
	volatile cpmux_t *cpmux = cpm2_map(im_cpmux);

L
Linus Torvalds 已提交
243 244 245 246 247
	io->iop_pparb |= 0x008b0000;
	io->iop_pdirb |= 0x00880000;
	io->iop_psorb |= 0x00880000;
	io->iop_pdirb &= ~0x00030000;
	io->iop_psorb &= ~0x00030000;
248 249
	cpmux->cmx_scr &= 0xffff00ff;
	cpmux->cmx_scr |= 0x00001200;
L
Linus Torvalds 已提交
250
	pinfo->brg = 3;
251 252 253

	cpm2_unmap(cpmux);
	cpm2_unmap(io);
L
Linus Torvalds 已提交
254 255 256 257
}

void scc4_lineif(struct uart_cpm_port *pinfo)
{
258 259
	volatile iop_cpm2_t *io = cpm2_map(im_ioport);
	volatile cpmux_t *cpmux = cpm2_map(im_cpmux);
L
Linus Torvalds 已提交
260 261 262 263 264 265

	io->iop_ppard |= 0x00000600;
	io->iop_psord &= ~0x00000600;	/* Tx/Rx */
	io->iop_pdird &= ~0x00000200;	/* Rx */
	io->iop_pdird |= 0x00000400;	/* Tx */

266 267
	cpmux->cmx_scr &= 0xffffff00;
	cpmux->cmx_scr |= 0x0000001b;
L
Linus Torvalds 已提交
268
	pinfo->brg = 4;
269 270 271

	cpm2_unmap(cpmux);
	cpm2_unmap(io);
L
Linus Torvalds 已提交
272
}
273
#endif
L
Linus Torvalds 已提交
274 275

/*
K
Kumar Gala 已提交
276
 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
L
Linus Torvalds 已提交
277 278 279 280 281 282 283
 * receive buffer descriptors from dual port ram, and a character
 * buffer area from host mem. If we are allocating for the console we need
 * to do it from bootmem
 */
int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
{
	int dpmemsz, memsz;
284
	u8 __iomem *dp_mem;
285
	unsigned long dp_offset;
L
Linus Torvalds 已提交
286 287 288 289 290 291 292
	u8 *mem_addr;
	dma_addr_t dma_addr = 0;

	pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);

	dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
	dp_offset = cpm_dpalloc(dpmemsz, 8);
293
	if (IS_ERR_VALUE(dp_offset)) {
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302
		printk(KERN_ERR
		       "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
		return -ENOMEM;
	}

	dp_mem = cpm_dpram_addr(dp_offset);

	memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
	    L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
303
	if (is_con) {
L
Linus Torvalds 已提交
304
		mem_addr = alloc_bootmem(memsz);
305
		dma_addr = virt_to_bus(mem_addr);
306
	}
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320
	else
		mem_addr = dma_alloc_coherent(NULL, memsz, &dma_addr,
					      GFP_KERNEL);

	if (mem_addr == NULL) {
		cpm_dpfree(dp_offset);
		printk(KERN_ERR
		       "cpm_uart_cpm.c: could not allocate coherent memory\n");
		return -ENOMEM;
	}

	pinfo->dp_addr = dp_offset;
	pinfo->mem_addr = mem_addr;
	pinfo->dma_addr = dma_addr;
321
	pinfo->mem_size = memsz;
L
Linus Torvalds 已提交
322 323 324 325 326

	pinfo->rx_buf = mem_addr;
	pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
						       * pinfo->rx_fifosize);

327
	pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335 336 337
	pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;

	return 0;
}

void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
{
	dma_free_coherent(NULL, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
					       pinfo->rx_fifosize) +
			  L1_CACHE_ALIGN(pinfo->tx_nrfifos *
S
Scott Wood 已提交
338
					 pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
L
Linus Torvalds 已提交
339 340 341 342 343
			  pinfo->dma_addr);

	cpm_dpfree(pinfo->dp_addr);
}

344
#ifndef CONFIG_PPC_CPM_NEW_BINDING
L
Linus Torvalds 已提交
345
/* Setup any dynamic params in the uart desc */
346
int cpm_uart_init_portdesc(void)
L
Linus Torvalds 已提交
347
{
348
#if defined(CONFIG_SERIAL_CPM_SMC1) || defined(CONFIG_SERIAL_CPM_SMC2)
349
	u16 *addr;
350
#endif
L
Linus Torvalds 已提交
351 352 353 354
	pr_debug("CPM uart[-]:init portdesc\n");

	cpm_uart_nr = 0;
#ifdef CONFIG_SERIAL_CPM_SMC1
355
	cpm_uart_ports[UART_SMC1].smcp = (smc_t *) cpm2_map(im_smc[0]);
L
Linus Torvalds 已提交
356
	cpm_uart_ports[UART_SMC1].port.mapbase =
357 358 359 360 361 362 363 364
	    (unsigned long)cpm_uart_ports[UART_SMC1].smcp;

	cpm_uart_ports[UART_SMC1].smcup =
	    (smc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SMC1], PROFF_SMC_SIZE);
	addr = (u16 *)cpm2_map_size(im_dprambase[PROFF_SMC1_BASE], 2);
	*addr = PROFF_SMC1;
	cpm2_unmap(addr);

L
Linus Torvalds 已提交
365 366
	cpm_uart_ports[UART_SMC1].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
	cpm_uart_ports[UART_SMC1].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
367
	cpm_uart_ports[UART_SMC1].port.uartclk = uart_clock();
L
Linus Torvalds 已提交
368 369 370 371
	cpm_uart_port_map[cpm_uart_nr++] = UART_SMC1;
#endif

#ifdef CONFIG_SERIAL_CPM_SMC2
372
	cpm_uart_ports[UART_SMC2].smcp = (smc_t *) cpm2_map(im_smc[1]);
L
Linus Torvalds 已提交
373
	cpm_uart_ports[UART_SMC2].port.mapbase =
374 375 376 377 378 379 380 381
	    (unsigned long)cpm_uart_ports[UART_SMC2].smcp;

	cpm_uart_ports[UART_SMC2].smcup =
	    (smc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SMC2], PROFF_SMC_SIZE);
	addr = (u16 *)cpm2_map_size(im_dprambase[PROFF_SMC2_BASE], 2);
	*addr = PROFF_SMC2;
	cpm2_unmap(addr);

L
Linus Torvalds 已提交
382 383
	cpm_uart_ports[UART_SMC2].smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
	cpm_uart_ports[UART_SMC2].smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
384
	cpm_uart_ports[UART_SMC2].port.uartclk = uart_clock();
L
Linus Torvalds 已提交
385 386 387 388
	cpm_uart_port_map[cpm_uart_nr++] = UART_SMC2;
#endif

#ifdef CONFIG_SERIAL_CPM_SCC1
389
	cpm_uart_ports[UART_SCC1].sccp = (scc_t *) cpm2_map(im_scc[0]);
L
Linus Torvalds 已提交
390
	cpm_uart_ports[UART_SCC1].port.mapbase =
391 392 393 394
	    (unsigned long)cpm_uart_ports[UART_SCC1].sccp;
	cpm_uart_ports[UART_SCC1].sccup =
	    (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC1], PROFF_SCC_SIZE);

L
Linus Torvalds 已提交
395 396 397 398
	cpm_uart_ports[UART_SCC1].sccp->scc_sccm &=
	    ~(UART_SCCM_TX | UART_SCCM_RX);
	cpm_uart_ports[UART_SCC1].sccp->scc_gsmrl &=
	    ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
399
	cpm_uart_ports[UART_SCC1].port.uartclk = uart_clock();
L
Linus Torvalds 已提交
400 401 402 403
	cpm_uart_port_map[cpm_uart_nr++] = UART_SCC1;
#endif

#ifdef CONFIG_SERIAL_CPM_SCC2
404
	cpm_uart_ports[UART_SCC2].sccp = (scc_t *) cpm2_map(im_scc[1]);
L
Linus Torvalds 已提交
405
	cpm_uart_ports[UART_SCC2].port.mapbase =
406 407 408 409
	    (unsigned long)cpm_uart_ports[UART_SCC2].sccp;
	cpm_uart_ports[UART_SCC2].sccup =
	    (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC2], PROFF_SCC_SIZE);

L
Linus Torvalds 已提交
410 411 412 413
	cpm_uart_ports[UART_SCC2].sccp->scc_sccm &=
	    ~(UART_SCCM_TX | UART_SCCM_RX);
	cpm_uart_ports[UART_SCC2].sccp->scc_gsmrl &=
	    ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
414
	cpm_uart_ports[UART_SCC2].port.uartclk = uart_clock();
L
Linus Torvalds 已提交
415 416 417 418
	cpm_uart_port_map[cpm_uart_nr++] = UART_SCC2;
#endif

#ifdef CONFIG_SERIAL_CPM_SCC3
419
	cpm_uart_ports[UART_SCC3].sccp = (scc_t *) cpm2_map(im_scc[2]);
L
Linus Torvalds 已提交
420
	cpm_uart_ports[UART_SCC3].port.mapbase =
421 422 423 424
	    (unsigned long)cpm_uart_ports[UART_SCC3].sccp;
	cpm_uart_ports[UART_SCC3].sccup =
	    (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC3], PROFF_SCC_SIZE);

L
Linus Torvalds 已提交
425 426 427 428
	cpm_uart_ports[UART_SCC3].sccp->scc_sccm &=
	    ~(UART_SCCM_TX | UART_SCCM_RX);
	cpm_uart_ports[UART_SCC3].sccp->scc_gsmrl &=
	    ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
429
	cpm_uart_ports[UART_SCC3].port.uartclk = uart_clock();
L
Linus Torvalds 已提交
430 431 432 433
	cpm_uart_port_map[cpm_uart_nr++] = UART_SCC3;
#endif

#ifdef CONFIG_SERIAL_CPM_SCC4
434
	cpm_uart_ports[UART_SCC4].sccp = (scc_t *) cpm2_map(im_scc[3]);
L
Linus Torvalds 已提交
435
	cpm_uart_ports[UART_SCC4].port.mapbase =
436 437 438 439
	    (unsigned long)cpm_uart_ports[UART_SCC4].sccp;
	cpm_uart_ports[UART_SCC4].sccup =
	    (scc_uart_t *) cpm2_map_size(im_dprambase[PROFF_SCC4], PROFF_SCC_SIZE);

L
Linus Torvalds 已提交
440 441 442 443
	cpm_uart_ports[UART_SCC4].sccp->scc_sccm &=
	    ~(UART_SCCM_TX | UART_SCCM_RX);
	cpm_uart_ports[UART_SCC4].sccp->scc_gsmrl &=
	    ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
444
	cpm_uart_ports[UART_SCC4].port.uartclk = uart_clock();
L
Linus Torvalds 已提交
445 446 447 448 449
	cpm_uart_port_map[cpm_uart_nr++] = UART_SCC4;
#endif

	return 0;
}
450
#endif