1/*
2 * Copyright 2013 Tilera Corporation. All Rights Reserved.
3 *
4 *   This program is free software; you can redistribute it and/or
5 *   modify it under the terms of the GNU General Public License
6 *   as published by the Free Software Foundation, version 2.
7 *
8 *   This program is distributed in the hope that it will be useful, but
9 *   WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 *   NON INFRINGEMENT.  See the GNU General Public License for
12 *   more details.
13 */
14
15/*
16 * Implementation of UART gxio calls.
17 */
18
19#include <linux/io.h>
20#include <linux/errno.h>
21#include <linux/module.h>
22
23#include <gxio/uart.h>
24#include <gxio/iorpc_globals.h>
25#include <gxio/iorpc_uart.h>
26#include <gxio/kiorpc.h>
27
28int gxio_uart_init(gxio_uart_context_t *context, int uart_index)
29{
30	char file[32];
31	int fd;
32
33	snprintf(file, sizeof(file), "uart/%d/iorpc", uart_index);
34	fd = hv_dev_open((HV_VirtAddr) file, 0);
35	if (fd < 0) {
36		if (fd >= GXIO_ERR_MIN && fd <= GXIO_ERR_MAX)
37			return fd;
38		else
39			return -ENODEV;
40	}
41
42	context->fd = fd;
43
44	/* Map in the MMIO space. */
45	context->mmio_base = (void __force *)
46		iorpc_ioremap(fd, HV_UART_MMIO_OFFSET, HV_UART_MMIO_SIZE);
47
48	if (context->mmio_base == NULL) {
49		hv_dev_close(context->fd);
50		context->fd = -1;
51		return -ENODEV;
52	}
53
54	return 0;
55}
56
57EXPORT_SYMBOL_GPL(gxio_uart_init);
58
59int gxio_uart_destroy(gxio_uart_context_t *context)
60{
61	iounmap((void __force __iomem *)(context->mmio_base));
62	hv_dev_close(context->fd);
63
64	context->mmio_base = NULL;
65	context->fd = -1;
66
67	return 0;
68}
69
70EXPORT_SYMBOL_GPL(gxio_uart_destroy);
71
72/* UART register write wrapper. */
73void gxio_uart_write(gxio_uart_context_t *context, uint64_t offset,
74		     uint64_t word)
75{
76	__gxio_mmio_write(context->mmio_base + offset, word);
77}
78
79EXPORT_SYMBOL_GPL(gxio_uart_write);
80
81/* UART register read wrapper. */
82uint64_t gxio_uart_read(gxio_uart_context_t *context, uint64_t offset)
83{
84	return __gxio_mmio_read(context->mmio_base + offset);
85}
86
87EXPORT_SYMBOL_GPL(gxio_uart_read);
88