1/*
2 * 8250_mid.c - Driver for UART on Intel Penwell and various other Intel SOCs
3 *
4 * Copyright (C) 2015 Intel Corporation
5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/rational.h>
13#include <linux/module.h>
14#include <linux/pci.h>
15
16#include <linux/dma/hsu.h>
17#include <linux/8250_pci.h>
18
19#include "8250.h"
20
21#define PCI_DEVICE_ID_INTEL_PNW_UART1	0x081b
22#define PCI_DEVICE_ID_INTEL_PNW_UART2	0x081c
23#define PCI_DEVICE_ID_INTEL_PNW_UART3	0x081d
24#define PCI_DEVICE_ID_INTEL_TNG_UART	0x1191
25#define PCI_DEVICE_ID_INTEL_DNV_UART	0x19d8
26
27/* Intel MID Specific registers */
28#define INTEL_MID_UART_DNV_FISR		0x08
29#define INTEL_MID_UART_PS		0x30
30#define INTEL_MID_UART_MUL		0x34
31#define INTEL_MID_UART_DIV		0x38
32
33struct mid8250;
34
35struct mid8250_board {
36	unsigned int flags;
37	unsigned long freq;
38	unsigned int base_baud;
39	int (*setup)(struct mid8250 *, struct uart_port *p);
40	void (*exit)(struct mid8250 *);
41};
42
43struct mid8250 {
44	int line;
45	int dma_index;
46	struct pci_dev *dma_dev;
47	struct uart_8250_dma dma;
48	struct mid8250_board *board;
49	struct hsu_dma_chip dma_chip;
50};
51
52/*****************************************************************************/
53
54static int pnw_setup(struct mid8250 *mid, struct uart_port *p)
55{
56	struct pci_dev *pdev = to_pci_dev(p->dev);
57
58	switch (pdev->device) {
59	case PCI_DEVICE_ID_INTEL_PNW_UART1:
60		mid->dma_index = 0;
61		break;
62	case PCI_DEVICE_ID_INTEL_PNW_UART2:
63		mid->dma_index = 1;
64		break;
65	case PCI_DEVICE_ID_INTEL_PNW_UART3:
66		mid->dma_index = 2;
67		break;
68	default:
69		return -EINVAL;
70	}
71
72	mid->dma_dev = pci_get_slot(pdev->bus,
73				    PCI_DEVFN(PCI_SLOT(pdev->devfn), 3));
74	return 0;
75}
76
77static int tng_setup(struct mid8250 *mid, struct uart_port *p)
78{
79	struct pci_dev *pdev = to_pci_dev(p->dev);
80	int index = PCI_FUNC(pdev->devfn);
81
82	/* Currently no support for HSU port0 */
83	if (index-- == 0)
84		return -ENODEV;
85
86	mid->dma_index = index;
87	mid->dma_dev = pci_get_slot(pdev->bus, PCI_DEVFN(5, 0));
88	return 0;
89}
90
91static int dnv_handle_irq(struct uart_port *p)
92{
93	struct mid8250 *mid = p->private_data;
94	unsigned int fisr = serial_port_in(p, INTEL_MID_UART_DNV_FISR);
95	int ret = IRQ_NONE;
96
97	if (fisr & BIT(2))
98		ret |= hsu_dma_irq(&mid->dma_chip, 1);
99	if (fisr & BIT(1))
100		ret |= hsu_dma_irq(&mid->dma_chip, 0);
101	if (fisr & BIT(0))
102		ret |= serial8250_handle_irq(p, serial_port_in(p, UART_IIR));
103	return ret;
104}
105
106#define DNV_DMA_CHAN_OFFSET 0x80
107
108static int dnv_setup(struct mid8250 *mid, struct uart_port *p)
109{
110	struct hsu_dma_chip *chip = &mid->dma_chip;
111	struct pci_dev *pdev = to_pci_dev(p->dev);
112	unsigned int bar = FL_GET_BASE(mid->board->flags);
113	int ret;
114
115	chip->dev = &pdev->dev;
116	chip->irq = pdev->irq;
117	chip->regs = p->membase;
118	chip->length = pci_resource_len(pdev, bar);
119	chip->offset = DNV_DMA_CHAN_OFFSET;
120
121	/* Falling back to PIO mode if DMA probing fails */
122	ret = hsu_dma_probe(chip);
123	if (ret)
124		return 0;
125
126	mid->dma_dev = pdev;
127
128	p->handle_irq = dnv_handle_irq;
129	return 0;
130}
131
132static void dnv_exit(struct mid8250 *mid)
133{
134	if (!mid->dma_dev)
135		return;
136	hsu_dma_remove(&mid->dma_chip);
137}
138
139/*****************************************************************************/
140
141static void mid8250_set_termios(struct uart_port *p,
142				struct ktermios *termios,
143				struct ktermios *old)
144{
145	unsigned int baud = tty_termios_baud_rate(termios);
146	struct mid8250 *mid = p->private_data;
147	unsigned short ps = 16;
148	unsigned long fuart = baud * ps;
149	unsigned long w = BIT(24) - 1;
150	unsigned long mul, div;
151
152	if (mid->board->freq < fuart) {
153		/* Find prescaler value that satisfies Fuart < Fref */
154		if (mid->board->freq > baud)
155			ps = mid->board->freq / baud;	/* baud rate too high */
156		else
157			ps = 1;				/* PLL case */
158		fuart = baud * ps;
159	} else {
160		/* Get Fuart closer to Fref */
161		fuart *= rounddown_pow_of_two(mid->board->freq / fuart);
162	}
163
164	rational_best_approximation(fuart, mid->board->freq, w, w, &mul, &div);
165	p->uartclk = fuart * 16 / ps;		/* core uses ps = 16 always */
166
167	writel(ps, p->membase + INTEL_MID_UART_PS);		/* set PS */
168	writel(mul, p->membase + INTEL_MID_UART_MUL);		/* set MUL */
169	writel(div, p->membase + INTEL_MID_UART_DIV);
170
171	serial8250_do_set_termios(p, termios, old);
172}
173
174static bool mid8250_dma_filter(struct dma_chan *chan, void *param)
175{
176	struct hsu_dma_slave *s = param;
177
178	if (s->dma_dev != chan->device->dev || s->chan_id != chan->chan_id)
179		return false;
180
181	chan->private = s;
182	return true;
183}
184
185static int mid8250_dma_setup(struct mid8250 *mid, struct uart_8250_port *port)
186{
187	struct uart_8250_dma *dma = &mid->dma;
188	struct device *dev = port->port.dev;
189	struct hsu_dma_slave *rx_param;
190	struct hsu_dma_slave *tx_param;
191
192	if (!mid->dma_dev)
193		return 0;
194
195	rx_param = devm_kzalloc(dev, sizeof(*rx_param), GFP_KERNEL);
196	if (!rx_param)
197		return -ENOMEM;
198
199	tx_param = devm_kzalloc(dev, sizeof(*tx_param), GFP_KERNEL);
200	if (!tx_param)
201		return -ENOMEM;
202
203	rx_param->chan_id = mid->dma_index * 2 + 1;
204	tx_param->chan_id = mid->dma_index * 2;
205
206	dma->rxconf.src_maxburst = 64;
207	dma->txconf.dst_maxburst = 64;
208
209	rx_param->dma_dev = &mid->dma_dev->dev;
210	tx_param->dma_dev = &mid->dma_dev->dev;
211
212	dma->fn = mid8250_dma_filter;
213	dma->rx_param = rx_param;
214	dma->tx_param = tx_param;
215
216	port->dma = dma;
217	return 0;
218}
219
220static int mid8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
221{
222	struct uart_8250_port uart;
223	struct mid8250 *mid;
224	unsigned int bar;
225	int ret;
226
227	ret = pcim_enable_device(pdev);
228	if (ret)
229		return ret;
230
231	pci_set_master(pdev);
232
233	mid = devm_kzalloc(&pdev->dev, sizeof(*mid), GFP_KERNEL);
234	if (!mid)
235		return -ENOMEM;
236
237	mid->board = (struct mid8250_board *)id->driver_data;
238	bar = FL_GET_BASE(mid->board->flags);
239
240	memset(&uart, 0, sizeof(struct uart_8250_port));
241
242	uart.port.dev = &pdev->dev;
243	uart.port.irq = pdev->irq;
244	uart.port.private_data = mid;
245	uart.port.type = PORT_16750;
246	uart.port.iotype = UPIO_MEM;
247	uart.port.uartclk = mid->board->base_baud * 16;
248	uart.port.flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE;
249	uart.port.set_termios = mid8250_set_termios;
250
251	uart.port.mapbase = pci_resource_start(pdev, bar);
252	uart.port.membase = pcim_iomap(pdev, bar, 0);
253	if (!uart.port.membase)
254		return -ENOMEM;
255
256	if (mid->board->setup) {
257		ret = mid->board->setup(mid, &uart.port);
258		if (ret)
259			return ret;
260	}
261
262	ret = mid8250_dma_setup(mid, &uart);
263	if (ret)
264		goto err;
265
266	ret = serial8250_register_8250_port(&uart);
267	if (ret < 0)
268		goto err;
269
270	mid->line = ret;
271
272	pci_set_drvdata(pdev, mid);
273	return 0;
274err:
275	if (mid->board->exit)
276		mid->board->exit(mid);
277	return ret;
278}
279
280static void mid8250_remove(struct pci_dev *pdev)
281{
282	struct mid8250 *mid = pci_get_drvdata(pdev);
283
284	if (mid->board->exit)
285		mid->board->exit(mid);
286
287	serial8250_unregister_port(mid->line);
288}
289
290static const struct mid8250_board pnw_board = {
291	.flags = FL_BASE0,
292	.freq = 50000000,
293	.base_baud = 115200,
294	.setup = pnw_setup,
295};
296
297static const struct mid8250_board tng_board = {
298	.flags = FL_BASE0,
299	.freq = 38400000,
300	.base_baud = 1843200,
301	.setup = tng_setup,
302};
303
304static const struct mid8250_board dnv_board = {
305	.flags = FL_BASE1,
306	.freq = 133333333,
307	.base_baud = 115200,
308	.setup = dnv_setup,
309	.exit = dnv_exit,
310};
311
312#define MID_DEVICE(id, board) { PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&board }
313
314static const struct pci_device_id pci_ids[] = {
315	MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART1, pnw_board),
316	MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART2, pnw_board),
317	MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART3, pnw_board),
318	MID_DEVICE(PCI_DEVICE_ID_INTEL_TNG_UART, tng_board),
319	MID_DEVICE(PCI_DEVICE_ID_INTEL_DNV_UART, dnv_board),
320	{ },
321};
322MODULE_DEVICE_TABLE(pci, pci_ids);
323
324static struct pci_driver mid8250_pci_driver = {
325	.name           = "8250_mid",
326	.id_table       = pci_ids,
327	.probe          = mid8250_probe,
328	.remove         = mid8250_remove,
329};
330
331module_pci_driver(mid8250_pci_driver);
332
333MODULE_AUTHOR("Intel Corporation");
334MODULE_LICENSE("GPL v2");
335MODULE_DESCRIPTION("Intel MID UART driver");
336