2 * 8250_mid.c - Driver for UART on Intel Penwell and various other Intel SOCs
4 * Copyright (C) 2015 Intel Corporation
5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
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.
12 #include <linux/bitops.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/rational.h>
17 #include <linux/dma/hsu.h>
18 #include <linux/8250_pci.h>
22 #define PCI_DEVICE_ID_INTEL_PNW_UART1 0x081b
23 #define PCI_DEVICE_ID_INTEL_PNW_UART2 0x081c
24 #define PCI_DEVICE_ID_INTEL_PNW_UART3 0x081d
25 #define PCI_DEVICE_ID_INTEL_TNG_UART 0x1191
26 #define PCI_DEVICE_ID_INTEL_DNV_UART 0x19d8
28 /* Intel MID Specific registers */
29 #define INTEL_MID_UART_DNV_FISR 0x08
30 #define INTEL_MID_UART_PS 0x30
31 #define INTEL_MID_UART_MUL 0x34
32 #define INTEL_MID_UART_DIV 0x38
36 struct mid8250_board {
39 unsigned int base_baud;
40 int (*setup)(struct mid8250 *, struct uart_port *p);
41 void (*exit)(struct mid8250 *);
47 struct pci_dev *dma_dev;
48 struct uart_8250_dma dma;
49 struct mid8250_board *board;
50 struct hsu_dma_chip dma_chip;
53 /*****************************************************************************/
55 static int pnw_setup(struct mid8250 *mid, struct uart_port *p)
57 struct pci_dev *pdev = to_pci_dev(p->dev);
59 switch (pdev->device) {
60 case PCI_DEVICE_ID_INTEL_PNW_UART1:
63 case PCI_DEVICE_ID_INTEL_PNW_UART2:
66 case PCI_DEVICE_ID_INTEL_PNW_UART3:
73 mid->dma_dev = pci_get_slot(pdev->bus,
74 PCI_DEVFN(PCI_SLOT(pdev->devfn), 3));
78 static void pnw_exit(struct mid8250 *mid)
80 pci_dev_put(mid->dma_dev);
83 static int tng_handle_irq(struct uart_port *p)
85 struct mid8250 *mid = p->private_data;
86 struct uart_8250_port *up = up_to_u8250p(p);
87 struct hsu_dma_chip *chip;
92 chip = pci_get_drvdata(mid->dma_dev);
95 err = hsu_dma_get_status(chip, mid->dma_index * 2 + 1, &status);
97 serial8250_rx_dma_flush(up);
100 ret |= hsu_dma_do_irq(chip, mid->dma_index * 2 + 1, status);
103 err = hsu_dma_get_status(chip, mid->dma_index * 2, &status);
107 ret |= hsu_dma_do_irq(chip, mid->dma_index * 2, status);
110 ret |= serial8250_handle_irq(p, serial_port_in(p, UART_IIR));
111 return IRQ_RETVAL(ret);
114 static int tng_setup(struct mid8250 *mid, struct uart_port *p)
116 struct pci_dev *pdev = to_pci_dev(p->dev);
117 int index = PCI_FUNC(pdev->devfn);
120 * Device 0000:00:04.0 is not a real HSU port. It provides a global
121 * register set for all HSU ports, although it has the same PCI ID.
127 mid->dma_index = index;
128 mid->dma_dev = pci_get_slot(pdev->bus, PCI_DEVFN(5, 0));
130 p->handle_irq = tng_handle_irq;
134 static void tng_exit(struct mid8250 *mid)
136 pci_dev_put(mid->dma_dev);
139 static int dnv_handle_irq(struct uart_port *p)
141 struct mid8250 *mid = p->private_data;
142 struct uart_8250_port *up = up_to_u8250p(p);
143 unsigned int fisr = serial_port_in(p, INTEL_MID_UART_DNV_FISR);
149 err = hsu_dma_get_status(&mid->dma_chip, 1, &status);
151 serial8250_rx_dma_flush(up);
154 ret |= hsu_dma_do_irq(&mid->dma_chip, 1, status);
157 err = hsu_dma_get_status(&mid->dma_chip, 0, &status);
161 ret |= hsu_dma_do_irq(&mid->dma_chip, 0, status);
164 ret |= serial8250_handle_irq(p, serial_port_in(p, UART_IIR));
165 return IRQ_RETVAL(ret);
168 #define DNV_DMA_CHAN_OFFSET 0x80
170 static int dnv_setup(struct mid8250 *mid, struct uart_port *p)
172 struct hsu_dma_chip *chip = &mid->dma_chip;
173 struct pci_dev *pdev = to_pci_dev(p->dev);
174 unsigned int bar = FL_GET_BASE(mid->board->flags);
177 pci_set_master(pdev);
179 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
183 p->irq = pci_irq_vector(pdev, 0);
185 chip->dev = &pdev->dev;
186 chip->irq = pci_irq_vector(pdev, 0);
187 chip->regs = p->membase;
188 chip->length = pci_resource_len(pdev, bar);
189 chip->offset = DNV_DMA_CHAN_OFFSET;
191 /* Falling back to PIO mode if DMA probing fails */
192 ret = hsu_dma_probe(chip);
198 p->handle_irq = dnv_handle_irq;
202 static void dnv_exit(struct mid8250 *mid)
206 hsu_dma_remove(&mid->dma_chip);
209 /*****************************************************************************/
211 static void mid8250_set_termios(struct uart_port *p,
212 struct ktermios *termios,
213 struct ktermios *old)
215 unsigned int baud = tty_termios_baud_rate(termios);
216 struct mid8250 *mid = p->private_data;
217 unsigned short ps = 16;
218 unsigned long fuart = baud * ps;
219 unsigned long w = BIT(24) - 1;
220 unsigned long mul, div;
222 /* Gracefully handle the B0 case: fall back to B9600 */
223 fuart = fuart ? fuart : 9600 * 16;
225 if (mid->board->freq < fuart) {
226 /* Find prescaler value that satisfies Fuart < Fref */
227 if (mid->board->freq > baud)
228 ps = mid->board->freq / baud; /* baud rate too high */
230 ps = 1; /* PLL case */
233 /* Get Fuart closer to Fref */
234 fuart *= rounddown_pow_of_two(mid->board->freq / fuart);
237 rational_best_approximation(fuart, mid->board->freq, w, w, &mul, &div);
238 p->uartclk = fuart * 16 / ps; /* core uses ps = 16 always */
240 writel(ps, p->membase + INTEL_MID_UART_PS); /* set PS */
241 writel(mul, p->membase + INTEL_MID_UART_MUL); /* set MUL */
242 writel(div, p->membase + INTEL_MID_UART_DIV);
244 serial8250_do_set_termios(p, termios, old);
247 static bool mid8250_dma_filter(struct dma_chan *chan, void *param)
249 struct hsu_dma_slave *s = param;
251 if (s->dma_dev != chan->device->dev || s->chan_id != chan->chan_id)
258 static int mid8250_dma_setup(struct mid8250 *mid, struct uart_8250_port *port)
260 struct uart_8250_dma *dma = &mid->dma;
261 struct device *dev = port->port.dev;
262 struct hsu_dma_slave *rx_param;
263 struct hsu_dma_slave *tx_param;
268 rx_param = devm_kzalloc(dev, sizeof(*rx_param), GFP_KERNEL);
272 tx_param = devm_kzalloc(dev, sizeof(*tx_param), GFP_KERNEL);
276 rx_param->chan_id = mid->dma_index * 2 + 1;
277 tx_param->chan_id = mid->dma_index * 2;
279 dma->rxconf.src_maxburst = 64;
280 dma->txconf.dst_maxburst = 64;
282 rx_param->dma_dev = &mid->dma_dev->dev;
283 tx_param->dma_dev = &mid->dma_dev->dev;
285 dma->fn = mid8250_dma_filter;
286 dma->rx_param = rx_param;
287 dma->tx_param = tx_param;
293 static int mid8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
295 struct uart_8250_port uart;
300 ret = pcim_enable_device(pdev);
304 mid = devm_kzalloc(&pdev->dev, sizeof(*mid), GFP_KERNEL);
308 mid->board = (struct mid8250_board *)id->driver_data;
309 bar = FL_GET_BASE(mid->board->flags);
311 memset(&uart, 0, sizeof(struct uart_8250_port));
313 uart.port.dev = &pdev->dev;
314 uart.port.irq = pdev->irq;
315 uart.port.private_data = mid;
316 uart.port.type = PORT_16750;
317 uart.port.iotype = UPIO_MEM;
318 uart.port.uartclk = mid->board->base_baud * 16;
319 uart.port.flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE;
320 uart.port.set_termios = mid8250_set_termios;
322 uart.port.mapbase = pci_resource_start(pdev, bar);
323 uart.port.membase = pcim_iomap(pdev, bar, 0);
324 if (!uart.port.membase)
327 if (mid->board->setup) {
328 ret = mid->board->setup(mid, &uart.port);
333 ret = mid8250_dma_setup(mid, &uart);
337 ret = serial8250_register_8250_port(&uart);
343 pci_set_drvdata(pdev, mid);
347 mid->board->exit(mid);
351 static void mid8250_remove(struct pci_dev *pdev)
353 struct mid8250 *mid = pci_get_drvdata(pdev);
355 serial8250_unregister_port(mid->line);
357 mid->board->exit(mid);
360 static const struct mid8250_board pnw_board = {
368 static const struct mid8250_board tng_board = {
371 .base_baud = 1843200,
376 static const struct mid8250_board dnv_board = {
384 #define MID_DEVICE(id, board) { PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&board }
386 static const struct pci_device_id pci_ids[] = {
387 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART1, pnw_board),
388 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART2, pnw_board),
389 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART3, pnw_board),
390 MID_DEVICE(PCI_DEVICE_ID_INTEL_TNG_UART, tng_board),
391 MID_DEVICE(PCI_DEVICE_ID_INTEL_DNV_UART, dnv_board),
394 MODULE_DEVICE_TABLE(pci, pci_ids);
396 static struct pci_driver mid8250_pci_driver = {
399 .probe = mid8250_probe,
400 .remove = mid8250_remove,
403 module_pci_driver(mid8250_pci_driver);
405 MODULE_AUTHOR("Intel Corporation");
406 MODULE_LICENSE("GPL v2");
407 MODULE_DESCRIPTION("Intel MID UART driver");