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