Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Synopsys DesignWare 8250 driver. |
| 3 | * |
| 4 | * Copyright 2011 Picochip, Jamie Iles. |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 5 | * Copyright 2013 Intel Corporation |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 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 as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the |
| 13 | * LCR is written whilst busy. If it is, then a busy detect interrupt is |
| 14 | * raised, the LCR needs to be rewritten and the uart status register read. |
| 15 | */ |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/serial_8250.h> |
| 21 | #include <linux/serial_core.h> |
| 22 | #include <linux/serial_reg.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_irq.h> |
| 25 | #include <linux/of_platform.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/slab.h> |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 28 | #include <linux/acpi.h> |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 29 | |
Heikki Krogerus | 7277b2a | 2013-01-10 11:25:12 +0200 | [diff] [blame^] | 30 | #include "8250.h" |
| 31 | |
Heikki Krogerus | 30046df | 2013-01-10 11:25:09 +0200 | [diff] [blame] | 32 | /* Offsets for the DesignWare specific registers */ |
| 33 | #define DW_UART_USR 0x1f /* UART Status Register */ |
| 34 | #define DW_UART_CPR 0xf4 /* Component Parameter Register */ |
| 35 | #define DW_UART_UCV 0xf8 /* UART Component Version */ |
| 36 | |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 37 | /* Intel Low Power Subsystem specific */ |
| 38 | #define LPSS_PRV_CLOCK_PARAMS 0x800 |
| 39 | |
Heikki Krogerus | 30046df | 2013-01-10 11:25:09 +0200 | [diff] [blame] | 40 | /* Component Parameter Register bits */ |
| 41 | #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0) |
| 42 | #define DW_UART_CPR_AFCE_MODE (1 << 4) |
| 43 | #define DW_UART_CPR_THRE_MODE (1 << 5) |
| 44 | #define DW_UART_CPR_SIR_MODE (1 << 6) |
| 45 | #define DW_UART_CPR_SIR_LP_MODE (1 << 7) |
| 46 | #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8) |
| 47 | #define DW_UART_CPR_FIFO_ACCESS (1 << 9) |
| 48 | #define DW_UART_CPR_FIFO_STAT (1 << 10) |
| 49 | #define DW_UART_CPR_SHADOW (1 << 11) |
| 50 | #define DW_UART_CPR_ENCODED_PARMS (1 << 12) |
| 51 | #define DW_UART_CPR_DMA_EXTRA (1 << 13) |
| 52 | #define DW_UART_CPR_FIFO_MODE (0xff << 16) |
| 53 | /* Helper for fifo size calculation */ |
| 54 | #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16) |
| 55 | |
| 56 | |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 57 | struct dw8250_data { |
| 58 | int last_lcr; |
| 59 | int line; |
| 60 | }; |
| 61 | |
| 62 | static void dw8250_serial_out(struct uart_port *p, int offset, int value) |
| 63 | { |
| 64 | struct dw8250_data *d = p->private_data; |
| 65 | |
| 66 | if (offset == UART_LCR) |
| 67 | d->last_lcr = value; |
| 68 | |
| 69 | offset <<= p->regshift; |
| 70 | writeb(value, p->membase + offset); |
| 71 | } |
| 72 | |
| 73 | static unsigned int dw8250_serial_in(struct uart_port *p, int offset) |
| 74 | { |
| 75 | offset <<= p->regshift; |
| 76 | |
| 77 | return readb(p->membase + offset); |
| 78 | } |
| 79 | |
| 80 | static void dw8250_serial_out32(struct uart_port *p, int offset, int value) |
| 81 | { |
| 82 | struct dw8250_data *d = p->private_data; |
| 83 | |
| 84 | if (offset == UART_LCR) |
| 85 | d->last_lcr = value; |
| 86 | |
| 87 | offset <<= p->regshift; |
| 88 | writel(value, p->membase + offset); |
| 89 | } |
| 90 | |
| 91 | static unsigned int dw8250_serial_in32(struct uart_port *p, int offset) |
| 92 | { |
| 93 | offset <<= p->regshift; |
| 94 | |
| 95 | return readl(p->membase + offset); |
| 96 | } |
| 97 | |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 98 | static int dw8250_handle_irq(struct uart_port *p) |
| 99 | { |
| 100 | struct dw8250_data *d = p->private_data; |
| 101 | unsigned int iir = p->serial_in(p, UART_IIR); |
| 102 | |
| 103 | if (serial8250_handle_irq(p, iir)) { |
| 104 | return 1; |
| 105 | } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) { |
| 106 | /* Clear the USR and write the LCR again. */ |
Heikki Krogerus | 30046df | 2013-01-10 11:25:09 +0200 | [diff] [blame] | 107 | (void)p->serial_in(p, DW_UART_USR); |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 108 | p->serial_out(p, d->last_lcr, UART_LCR); |
| 109 | |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 116 | static int dw8250_probe_of(struct uart_port *p) |
| 117 | { |
| 118 | struct device_node *np = p->dev->of_node; |
| 119 | u32 val; |
| 120 | |
| 121 | if (!of_property_read_u32(np, "reg-io-width", &val)) { |
| 122 | switch (val) { |
| 123 | case 1: |
| 124 | break; |
| 125 | case 4: |
| 126 | p->iotype = UPIO_MEM32; |
| 127 | p->serial_in = dw8250_serial_in32; |
| 128 | p->serial_out = dw8250_serial_out32; |
| 129 | break; |
| 130 | default: |
| 131 | dev_err(p->dev, "unsupported reg-io-width (%u)\n", val); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!of_property_read_u32(np, "reg-shift", &val)) |
| 137 | p->regshift = val; |
| 138 | |
| 139 | if (of_property_read_u32(np, "clock-frequency", &val)) { |
| 140 | dev_err(p->dev, "no clock-frequency property set\n"); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | p->uartclk = val; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Heikki Krogerus | 7277b2a | 2013-01-10 11:25:12 +0200 | [diff] [blame^] | 148 | static bool dw8250_acpi_dma_filter(struct dma_chan *chan, void *parm) |
| 149 | { |
| 150 | return chan->chan_id == *(int *)parm; |
| 151 | } |
| 152 | |
| 153 | static acpi_status |
| 154 | dw8250_acpi_walk_resource(struct acpi_resource *res, void *data) |
| 155 | { |
| 156 | struct uart_port *p = data; |
| 157 | struct uart_8250_port *port; |
| 158 | struct uart_8250_dma *dma; |
| 159 | struct acpi_resource_fixed_dma *fixed_dma; |
| 160 | struct dma_slave_config *slave; |
| 161 | |
| 162 | port = container_of(p, struct uart_8250_port, port); |
| 163 | |
| 164 | switch (res->type) { |
| 165 | case ACPI_RESOURCE_TYPE_FIXED_DMA: |
| 166 | fixed_dma = &res->data.fixed_dma; |
| 167 | |
| 168 | /* TX comes first */ |
| 169 | if (!port->dma) { |
| 170 | dma = devm_kzalloc(p->dev, sizeof(*dma), GFP_KERNEL); |
| 171 | if (!dma) |
| 172 | return AE_NO_MEMORY; |
| 173 | |
| 174 | port->dma = dma; |
| 175 | slave = &dma->txconf; |
| 176 | |
| 177 | slave->direction = DMA_MEM_TO_DEV; |
| 178 | slave->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 179 | slave->slave_id = fixed_dma->request_lines; |
| 180 | |
| 181 | dma->tx_chan_id = fixed_dma->channels; |
| 182 | dma->tx_param = &dma->tx_chan_id; |
| 183 | dma->fn = dw8250_acpi_dma_filter; |
| 184 | } else { |
| 185 | dma = port->dma; |
| 186 | slave = &dma->rxconf; |
| 187 | |
| 188 | slave->direction = DMA_DEV_TO_MEM; |
| 189 | slave->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 190 | slave->slave_id = fixed_dma->request_lines; |
| 191 | |
| 192 | dma->rx_chan_id = fixed_dma->channels; |
| 193 | dma->rx_param = &dma->rx_chan_id; |
| 194 | } |
| 195 | |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | return AE_OK; |
| 200 | } |
| 201 | |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 202 | static int dw8250_probe_acpi(struct uart_port *p) |
| 203 | { |
| 204 | const struct acpi_device_id *id; |
Heikki Krogerus | 7277b2a | 2013-01-10 11:25:12 +0200 | [diff] [blame^] | 205 | acpi_status status; |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 206 | u32 reg; |
| 207 | |
| 208 | id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev); |
| 209 | if (!id) |
| 210 | return -ENODEV; |
| 211 | |
| 212 | p->iotype = UPIO_MEM32; |
| 213 | p->serial_in = dw8250_serial_in32; |
| 214 | p->serial_out = dw8250_serial_out32; |
| 215 | p->regshift = 2; |
| 216 | p->uartclk = (unsigned int)id->driver_data; |
| 217 | |
Heikki Krogerus | 7277b2a | 2013-01-10 11:25:12 +0200 | [diff] [blame^] | 218 | status = acpi_walk_resources(ACPI_HANDLE(p->dev), METHOD_NAME__CRS, |
| 219 | dw8250_acpi_walk_resource, p); |
| 220 | if (ACPI_FAILURE(status)) { |
| 221 | dev_err_ratelimited(p->dev, "%s failed \"%s\"\n", __func__, |
| 222 | acpi_format_exception(status)); |
| 223 | return -ENODEV; |
| 224 | } |
| 225 | |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 226 | /* Fix Haswell issue where the clocks do not get enabled */ |
| 227 | if (!strcmp(id->id, "INT33C4") || !strcmp(id->id, "INT33C5")) { |
| 228 | reg = readl(p->membase + LPSS_PRV_CLOCK_PARAMS); |
| 229 | writel(reg | 1, p->membase + LPSS_PRV_CLOCK_PARAMS); |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Heikki Krogerus | 30046df | 2013-01-10 11:25:09 +0200 | [diff] [blame] | 235 | static void dw8250_setup_port(struct uart_8250_port *up) |
| 236 | { |
| 237 | struct uart_port *p = &up->port; |
| 238 | u32 reg = readl(p->membase + DW_UART_UCV); |
| 239 | |
| 240 | /* |
| 241 | * If the Component Version Register returns zero, we know that |
| 242 | * ADDITIONAL_FEATURES are not enabled. No need to go any further. |
| 243 | */ |
| 244 | if (!reg) |
| 245 | return; |
| 246 | |
| 247 | dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n", |
| 248 | (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff); |
| 249 | |
| 250 | reg = readl(p->membase + DW_UART_CPR); |
| 251 | if (!reg) |
| 252 | return; |
| 253 | |
| 254 | /* Select the type based on fifo */ |
| 255 | if (reg & DW_UART_CPR_FIFO_MODE) { |
| 256 | p->type = PORT_16550A; |
| 257 | p->flags |= UPF_FIXED_TYPE; |
| 258 | p->fifosize = DW_UART_CPR_FIFO_SIZE(reg); |
| 259 | up->tx_loadsz = p->fifosize; |
| 260 | } |
| 261 | } |
| 262 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 263 | static int dw8250_probe(struct platform_device *pdev) |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 264 | { |
Alan Cox | 2655a2c | 2012-07-12 12:59:50 +0100 | [diff] [blame] | 265 | struct uart_8250_port uart = {}; |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 266 | struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 267 | struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 268 | struct dw8250_data *data; |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 269 | int err; |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 270 | |
| 271 | if (!regs || !irq) { |
| 272 | dev_err(&pdev->dev, "no registers/irq defined\n"); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
Alan Cox | 2655a2c | 2012-07-12 12:59:50 +0100 | [diff] [blame] | 276 | spin_lock_init(&uart.port.lock); |
| 277 | uart.port.mapbase = regs->start; |
| 278 | uart.port.irq = irq->start; |
| 279 | uart.port.handle_irq = dw8250_handle_irq; |
| 280 | uart.port.type = PORT_8250; |
Heikki Krogerus | f93366f | 2013-01-10 11:25:07 +0200 | [diff] [blame] | 281 | uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT; |
Alan Cox | 2655a2c | 2012-07-12 12:59:50 +0100 | [diff] [blame] | 282 | uart.port.dev = &pdev->dev; |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 283 | |
Heikki Krogerus | f93366f | 2013-01-10 11:25:07 +0200 | [diff] [blame] | 284 | uart.port.membase = ioremap(regs->start, resource_size(regs)); |
| 285 | if (!uart.port.membase) |
| 286 | return -ENOMEM; |
| 287 | |
Alan Cox | 2655a2c | 2012-07-12 12:59:50 +0100 | [diff] [blame] | 288 | uart.port.iotype = UPIO_MEM; |
| 289 | uart.port.serial_in = dw8250_serial_in; |
| 290 | uart.port.serial_out = dw8250_serial_out; |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 291 | |
| 292 | if (pdev->dev.of_node) { |
| 293 | err = dw8250_probe_of(&uart.port); |
| 294 | if (err) |
| 295 | return err; |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 296 | } else if (ACPI_HANDLE(&pdev->dev)) { |
| 297 | err = dw8250_probe_acpi(&uart.port); |
| 298 | if (err) |
| 299 | return err; |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 300 | } else { |
| 301 | return -ENODEV; |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Heikki Krogerus | 30046df | 2013-01-10 11:25:09 +0200 | [diff] [blame] | 304 | dw8250_setup_port(&uart); |
| 305 | |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 306 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 307 | if (!data) |
| 308 | return -ENOMEM; |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 309 | |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 310 | uart.port.private_data = data; |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 311 | |
Alan Cox | 2655a2c | 2012-07-12 12:59:50 +0100 | [diff] [blame] | 312 | data->line = serial8250_register_8250_port(&uart); |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 313 | if (data->line < 0) |
| 314 | return data->line; |
| 315 | |
| 316 | platform_set_drvdata(pdev, data); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 321 | static int dw8250_remove(struct platform_device *pdev) |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 322 | { |
| 323 | struct dw8250_data *data = platform_get_drvdata(pdev); |
| 324 | |
| 325 | serial8250_unregister_port(data->line); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
James Hogan | b61c5ed | 2012-10-15 10:25:58 +0100 | [diff] [blame] | 330 | #ifdef CONFIG_PM |
| 331 | static int dw8250_suspend(struct platform_device *pdev, pm_message_t state) |
| 332 | { |
| 333 | struct dw8250_data *data = platform_get_drvdata(pdev); |
| 334 | |
| 335 | serial8250_suspend_port(data->line); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int dw8250_resume(struct platform_device *pdev) |
| 341 | { |
| 342 | struct dw8250_data *data = platform_get_drvdata(pdev); |
| 343 | |
| 344 | serial8250_resume_port(data->line); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | #else |
| 349 | #define dw8250_suspend NULL |
| 350 | #define dw8250_resume NULL |
| 351 | #endif /* CONFIG_PM */ |
| 352 | |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 353 | static const struct of_device_id dw8250_of_match[] = { |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 354 | { .compatible = "snps,dw-apb-uart" }, |
| 355 | { /* Sentinel */ } |
| 356 | }; |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 357 | MODULE_DEVICE_TABLE(of, dw8250_of_match); |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 358 | |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 359 | static const struct acpi_device_id dw8250_acpi_match[] = { |
| 360 | { "INT33C4", 100000000 }, |
| 361 | { "INT33C5", 100000000 }, |
| 362 | { }, |
| 363 | }; |
| 364 | MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match); |
| 365 | |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 366 | static struct platform_driver dw8250_platform_driver = { |
| 367 | .driver = { |
| 368 | .name = "dw-apb-uart", |
| 369 | .owner = THIS_MODULE, |
Heikki Krogerus | a7260c8 | 2013-01-10 11:25:08 +0200 | [diff] [blame] | 370 | .of_match_table = dw8250_of_match, |
Heikki Krogerus | 6a7320c | 2013-01-10 11:25:10 +0200 | [diff] [blame] | 371 | .acpi_match_table = ACPI_PTR(dw8250_acpi_match), |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 372 | }, |
| 373 | .probe = dw8250_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 374 | .remove = dw8250_remove, |
James Hogan | b61c5ed | 2012-10-15 10:25:58 +0100 | [diff] [blame] | 375 | .suspend = dw8250_suspend, |
| 376 | .resume = dw8250_resume, |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 377 | }; |
| 378 | |
Axel Lin | c8381c15 | 2011-11-28 19:22:15 +0800 | [diff] [blame] | 379 | module_platform_driver(dw8250_platform_driver); |
Jamie Iles | 7d4008e | 2011-08-26 19:04:50 +0100 | [diff] [blame] | 380 | |
| 381 | MODULE_AUTHOR("Jamie Iles"); |
| 382 | MODULE_LICENSE("GPL"); |
| 383 | MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver"); |