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 int tng_setup(struct mid8250 *mid, struct uart_port *p)
80 struct pci_dev *pdev = to_pci_dev(p->dev);
81 int index = PCI_FUNC(pdev->devfn);
84 * Device 0000:00:04.0 is not a real HSU port. It provides a global
85 * register set for all HSU ports, although it has the same PCI ID.
91 mid->dma_index = index;
92 mid->dma_dev = pci_get_slot(pdev->bus, PCI_DEVFN(5, 0));
96 static int dnv_handle_irq(struct uart_port *p)
98 struct mid8250 *mid = p->private_data;
99 struct uart_8250_port *up = up_to_u8250p(p);
100 unsigned int fisr = serial_port_in(p, INTEL_MID_UART_DNV_FISR);
106 err = hsu_dma_get_status(&mid->dma_chip, 1, &status);
108 serial8250_rx_dma_flush(up);
111 ret |= hsu_dma_do_irq(&mid->dma_chip, 1, status);
114 err = hsu_dma_get_status(&mid->dma_chip, 0, &status);
118 ret |= hsu_dma_do_irq(&mid->dma_chip, 0, status);
121 ret |= serial8250_handle_irq(p, serial_port_in(p, UART_IIR));
122 return IRQ_RETVAL(ret);
125 #define DNV_DMA_CHAN_OFFSET 0x80
127 static int dnv_setup(struct mid8250 *mid, struct uart_port *p)
129 struct hsu_dma_chip *chip = &mid->dma_chip;
130 struct pci_dev *pdev = to_pci_dev(p->dev);
131 unsigned int bar = FL_GET_BASE(mid->board->flags);
134 chip->dev = &pdev->dev;
135 chip->irq = pdev->irq;
136 chip->regs = p->membase;
137 chip->length = pci_resource_len(pdev, bar);
138 chip->offset = DNV_DMA_CHAN_OFFSET;
140 /* Falling back to PIO mode if DMA probing fails */
141 ret = hsu_dma_probe(chip);
147 p->handle_irq = dnv_handle_irq;
151 static void dnv_exit(struct mid8250 *mid)
155 hsu_dma_remove(&mid->dma_chip);
158 /*****************************************************************************/
160 static void mid8250_set_termios(struct uart_port *p,
161 struct ktermios *termios,
162 struct ktermios *old)
164 unsigned int baud = tty_termios_baud_rate(termios);
165 struct mid8250 *mid = p->private_data;
166 unsigned short ps = 16;
167 unsigned long fuart = baud * ps;
168 unsigned long w = BIT(24) - 1;
169 unsigned long mul, div;
171 /* Gracefully handle the B0 case: fall back to B9600 */
172 fuart = fuart ? fuart : 9600 * 16;
174 if (mid->board->freq < fuart) {
175 /* Find prescaler value that satisfies Fuart < Fref */
176 if (mid->board->freq > baud)
177 ps = mid->board->freq / baud; /* baud rate too high */
179 ps = 1; /* PLL case */
182 /* Get Fuart closer to Fref */
183 fuart *= rounddown_pow_of_two(mid->board->freq / fuart);
186 rational_best_approximation(fuart, mid->board->freq, w, w, &mul, &div);
187 p->uartclk = fuart * 16 / ps; /* core uses ps = 16 always */
189 writel(ps, p->membase + INTEL_MID_UART_PS); /* set PS */
190 writel(mul, p->membase + INTEL_MID_UART_MUL); /* set MUL */
191 writel(div, p->membase + INTEL_MID_UART_DIV);
193 serial8250_do_set_termios(p, termios, old);
196 static bool mid8250_dma_filter(struct dma_chan *chan, void *param)
198 struct hsu_dma_slave *s = param;
200 if (s->dma_dev != chan->device->dev || s->chan_id != chan->chan_id)
207 static int mid8250_dma_setup(struct mid8250 *mid, struct uart_8250_port *port)
209 struct uart_8250_dma *dma = &mid->dma;
210 struct device *dev = port->port.dev;
211 struct hsu_dma_slave *rx_param;
212 struct hsu_dma_slave *tx_param;
217 rx_param = devm_kzalloc(dev, sizeof(*rx_param), GFP_KERNEL);
221 tx_param = devm_kzalloc(dev, sizeof(*tx_param), GFP_KERNEL);
225 rx_param->chan_id = mid->dma_index * 2 + 1;
226 tx_param->chan_id = mid->dma_index * 2;
228 dma->rxconf.src_maxburst = 64;
229 dma->txconf.dst_maxburst = 64;
231 rx_param->dma_dev = &mid->dma_dev->dev;
232 tx_param->dma_dev = &mid->dma_dev->dev;
234 dma->fn = mid8250_dma_filter;
235 dma->rx_param = rx_param;
236 dma->tx_param = tx_param;
242 static int mid8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
244 struct uart_8250_port uart;
249 ret = pcim_enable_device(pdev);
253 pci_set_master(pdev);
255 mid = devm_kzalloc(&pdev->dev, sizeof(*mid), GFP_KERNEL);
259 mid->board = (struct mid8250_board *)id->driver_data;
260 bar = FL_GET_BASE(mid->board->flags);
262 memset(&uart, 0, sizeof(struct uart_8250_port));
264 uart.port.dev = &pdev->dev;
265 uart.port.irq = pdev->irq;
266 uart.port.private_data = mid;
267 uart.port.type = PORT_16750;
268 uart.port.iotype = UPIO_MEM;
269 uart.port.uartclk = mid->board->base_baud * 16;
270 uart.port.flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE;
271 uart.port.set_termios = mid8250_set_termios;
273 uart.port.mapbase = pci_resource_start(pdev, bar);
274 uart.port.membase = pcim_iomap(pdev, bar, 0);
275 if (!uart.port.membase)
278 if (mid->board->setup) {
279 ret = mid->board->setup(mid, &uart.port);
284 ret = mid8250_dma_setup(mid, &uart);
288 ret = serial8250_register_8250_port(&uart);
294 pci_set_drvdata(pdev, mid);
297 if (mid->board->exit)
298 mid->board->exit(mid);
302 static void mid8250_remove(struct pci_dev *pdev)
304 struct mid8250 *mid = pci_get_drvdata(pdev);
306 if (mid->board->exit)
307 mid->board->exit(mid);
309 serial8250_unregister_port(mid->line);
312 static const struct mid8250_board pnw_board = {
319 static const struct mid8250_board tng_board = {
322 .base_baud = 1843200,
326 static const struct mid8250_board dnv_board = {
334 #define MID_DEVICE(id, board) { PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&board }
336 static const struct pci_device_id pci_ids[] = {
337 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART1, pnw_board),
338 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART2, pnw_board),
339 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART3, pnw_board),
340 MID_DEVICE(PCI_DEVICE_ID_INTEL_TNG_UART, tng_board),
341 MID_DEVICE(PCI_DEVICE_ID_INTEL_DNV_UART, dnv_board),
344 MODULE_DEVICE_TABLE(pci, pci_ids);
346 static struct pci_driver mid8250_pci_driver = {
349 .probe = mid8250_probe,
350 .remove = mid8250_remove,
353 module_pci_driver(mid8250_pci_driver);
355 MODULE_AUTHOR("Intel Corporation");
356 MODULE_LICENSE("GPL v2");
357 MODULE_DESCRIPTION("Intel MID UART driver");