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