Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ACPI support for Intel Lynxpoint LPSS. |
| 3 | * |
| 4 | * Copyright (C) 2013, Intel Corporation |
| 5 | * Authors: Mika Westerberg <mika.westerberg@linux.intel.com> |
| 6 | * Rafael J. Wysocki <rafael.j.wysocki@intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/acpi.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/clkdev.h> |
| 16 | #include <linux/clk-provider.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/platform_data/clk-lpss.h> |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 22 | #include <linux/delay.h> |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 23 | |
| 24 | #include "internal.h" |
| 25 | |
| 26 | ACPI_MODULE_NAME("acpi_lpss"); |
| 27 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 28 | #define LPSS_CLK_SIZE 0x04 |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 29 | #define LPSS_LTR_SIZE 0x18 |
| 30 | |
| 31 | /* Offsets relative to LPSS_PRIVATE_OFFSET */ |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 32 | #define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16)) |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 33 | #define LPSS_GENERAL 0x08 |
| 34 | #define LPSS_GENERAL_LTR_MODE_SW BIT(2) |
Heikki Krogerus | 088f1fd | 2013-10-09 09:49:20 +0300 | [diff] [blame] | 35 | #define LPSS_GENERAL_UART_RTS_OVRD BIT(3) |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 36 | #define LPSS_SW_LTR 0x10 |
| 37 | #define LPSS_AUTO_LTR 0x14 |
Rafael J. Wysocki | 1a8f835 | 2014-02-11 00:35:53 +0100 | [diff] [blame] | 38 | #define LPSS_LTR_SNOOP_REQ BIT(15) |
| 39 | #define LPSS_LTR_SNOOP_MASK 0x0000FFFF |
| 40 | #define LPSS_LTR_SNOOP_LAT_1US 0x800 |
| 41 | #define LPSS_LTR_SNOOP_LAT_32US 0xC00 |
| 42 | #define LPSS_LTR_SNOOP_LAT_SHIFT 5 |
| 43 | #define LPSS_LTR_SNOOP_LAT_CUTOFF 3000 |
| 44 | #define LPSS_LTR_MAX_VAL 0x3FF |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 45 | #define LPSS_TX_INT 0x20 |
| 46 | #define LPSS_TX_INT_MASK BIT(1) |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 47 | |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 48 | #define LPSS_PRV_REG_COUNT 9 |
| 49 | |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 50 | struct lpss_shared_clock { |
| 51 | const char *name; |
| 52 | unsigned long rate; |
| 53 | struct clk *clk; |
| 54 | }; |
| 55 | |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 56 | struct lpss_private_data; |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 57 | |
| 58 | struct lpss_device_desc { |
| 59 | bool clk_required; |
Rafael J. Wysocki | b59cc20 | 2013-05-08 11:55:49 +0300 | [diff] [blame] | 60 | const char *clkdev_name; |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 61 | bool ltr_required; |
| 62 | unsigned int prv_offset; |
Mika Westerberg | 958c4eb | 2013-06-18 16:51:35 +0300 | [diff] [blame] | 63 | size_t prv_size_override; |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 64 | bool clk_divider; |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 65 | bool clk_gate; |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 66 | bool save_ctx; |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 67 | struct lpss_shared_clock *shared_clock; |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 68 | void (*setup)(struct lpss_private_data *pdata); |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
Rafael J. Wysocki | b59cc20 | 2013-05-08 11:55:49 +0300 | [diff] [blame] | 71 | static struct lpss_device_desc lpss_dma_desc = { |
| 72 | .clk_required = true, |
| 73 | .clkdev_name = "hclk", |
| 74 | }; |
| 75 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 76 | struct lpss_private_data { |
| 77 | void __iomem *mmio_base; |
| 78 | resource_size_t mmio_size; |
| 79 | struct clk *clk; |
| 80 | const struct lpss_device_desc *dev_desc; |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 81 | u32 prv_reg_ctx[LPSS_PRV_REG_COUNT]; |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 84 | static void lpss_uart_setup(struct lpss_private_data *pdata) |
| 85 | { |
Heikki Krogerus | 088f1fd | 2013-10-09 09:49:20 +0300 | [diff] [blame] | 86 | unsigned int offset; |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 87 | u32 reg; |
| 88 | |
Heikki Krogerus | 088f1fd | 2013-10-09 09:49:20 +0300 | [diff] [blame] | 89 | offset = pdata->dev_desc->prv_offset + LPSS_TX_INT; |
| 90 | reg = readl(pdata->mmio_base + offset); |
| 91 | writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + offset); |
| 92 | |
| 93 | offset = pdata->dev_desc->prv_offset + LPSS_GENERAL; |
| 94 | reg = readl(pdata->mmio_base + offset); |
| 95 | writel(reg | LPSS_GENERAL_UART_RTS_OVRD, pdata->mmio_base + offset); |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 96 | } |
| 97 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 98 | static struct lpss_device_desc lpt_dev_desc = { |
| 99 | .clk_required = true, |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 100 | .prv_offset = 0x800, |
| 101 | .ltr_required = true, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 102 | .clk_divider = true, |
| 103 | .clk_gate = true, |
| 104 | }; |
| 105 | |
| 106 | static struct lpss_device_desc lpt_i2c_dev_desc = { |
| 107 | .clk_required = true, |
| 108 | .prv_offset = 0x800, |
| 109 | .ltr_required = true, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 110 | .clk_gate = true, |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 113 | static struct lpss_device_desc lpt_uart_dev_desc = { |
| 114 | .clk_required = true, |
| 115 | .prv_offset = 0x800, |
| 116 | .ltr_required = true, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 117 | .clk_divider = true, |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 118 | .clk_gate = true, |
| 119 | .setup = lpss_uart_setup, |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static struct lpss_device_desc lpt_sdio_dev_desc = { |
| 123 | .prv_offset = 0x1000, |
Mika Westerberg | 958c4eb | 2013-06-18 16:51:35 +0300 | [diff] [blame] | 124 | .prv_size_override = 0x1018, |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 125 | .ltr_required = true, |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Chew, Chiau Ee | e1c7481 | 2014-02-19 02:24:29 +0800 | [diff] [blame] | 128 | static struct lpss_shared_clock pwm_clock = { |
| 129 | .name = "pwm_clk", |
| 130 | .rate = 25000000, |
| 131 | }; |
| 132 | |
| 133 | static struct lpss_device_desc byt_pwm_dev_desc = { |
| 134 | .clk_required = true, |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 135 | .save_ctx = true, |
Chew, Chiau Ee | e1c7481 | 2014-02-19 02:24:29 +0800 | [diff] [blame] | 136 | .shared_clock = &pwm_clock, |
| 137 | }; |
| 138 | |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 139 | static struct lpss_device_desc byt_uart_dev_desc = { |
| 140 | .clk_required = true, |
| 141 | .prv_offset = 0x800, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 142 | .clk_divider = true, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 143 | .clk_gate = true, |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 144 | .save_ctx = true, |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 145 | .setup = lpss_uart_setup, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 148 | static struct lpss_device_desc byt_spi_dev_desc = { |
| 149 | .clk_required = true, |
| 150 | .prv_offset = 0x400, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 151 | .clk_divider = true, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 152 | .clk_gate = true, |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 153 | .save_ctx = true, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static struct lpss_device_desc byt_sdio_dev_desc = { |
| 157 | .clk_required = true, |
| 158 | }; |
| 159 | |
| 160 | static struct lpss_shared_clock i2c_clock = { |
| 161 | .name = "i2c_clk", |
| 162 | .rate = 100000000, |
| 163 | }; |
| 164 | |
| 165 | static struct lpss_device_desc byt_i2c_dev_desc = { |
| 166 | .clk_required = true, |
| 167 | .prv_offset = 0x800, |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 168 | .save_ctx = true, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 169 | .shared_clock = &i2c_clock, |
| 170 | }; |
| 171 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 172 | static const struct acpi_device_id acpi_lpss_device_ids[] = { |
Rafael J. Wysocki | b59cc20 | 2013-05-08 11:55:49 +0300 | [diff] [blame] | 173 | /* Generic LPSS devices */ |
| 174 | { "INTL9C60", (unsigned long)&lpss_dma_desc }, |
| 175 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 176 | /* Lynxpoint LPSS devices */ |
| 177 | { "INT33C0", (unsigned long)&lpt_dev_desc }, |
| 178 | { "INT33C1", (unsigned long)&lpt_dev_desc }, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 179 | { "INT33C2", (unsigned long)&lpt_i2c_dev_desc }, |
| 180 | { "INT33C3", (unsigned long)&lpt_i2c_dev_desc }, |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 181 | { "INT33C4", (unsigned long)&lpt_uart_dev_desc }, |
| 182 | { "INT33C5", (unsigned long)&lpt_uart_dev_desc }, |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 183 | { "INT33C6", (unsigned long)&lpt_sdio_dev_desc }, |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 184 | { "INT33C7", }, |
| 185 | |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 186 | /* BayTrail LPSS devices */ |
Chew, Chiau Ee | e1c7481 | 2014-02-19 02:24:29 +0800 | [diff] [blame] | 187 | { "80860F09", (unsigned long)&byt_pwm_dev_desc }, |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 188 | { "80860F0A", (unsigned long)&byt_uart_dev_desc }, |
| 189 | { "80860F0E", (unsigned long)&byt_spi_dev_desc }, |
| 190 | { "80860F14", (unsigned long)&byt_sdio_dev_desc }, |
| 191 | { "80860F41", (unsigned long)&byt_i2c_dev_desc }, |
| 192 | { "INT33B2", }, |
| 193 | |
Mika Westerberg | a4d9753 | 2013-11-12 11:48:19 +0200 | [diff] [blame] | 194 | { "INT3430", (unsigned long)&lpt_dev_desc }, |
| 195 | { "INT3431", (unsigned long)&lpt_dev_desc }, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 196 | { "INT3432", (unsigned long)&lpt_i2c_dev_desc }, |
| 197 | { "INT3433", (unsigned long)&lpt_i2c_dev_desc }, |
Mika Westerberg | a4d9753 | 2013-11-12 11:48:19 +0200 | [diff] [blame] | 198 | { "INT3434", (unsigned long)&lpt_uart_dev_desc }, |
| 199 | { "INT3435", (unsigned long)&lpt_uart_dev_desc }, |
| 200 | { "INT3436", (unsigned long)&lpt_sdio_dev_desc }, |
| 201 | { "INT3437", }, |
| 202 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 203 | { } |
| 204 | }; |
| 205 | |
| 206 | static int is_memory(struct acpi_resource *res, void *not_used) |
| 207 | { |
| 208 | struct resource r; |
| 209 | return !acpi_dev_resource_memory(res, &r); |
| 210 | } |
| 211 | |
| 212 | /* LPSS main clock device. */ |
| 213 | static struct platform_device *lpss_clk_dev; |
| 214 | |
| 215 | static inline void lpt_register_clock_device(void) |
| 216 | { |
| 217 | lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0); |
| 218 | } |
| 219 | |
| 220 | static int register_device_clock(struct acpi_device *adev, |
| 221 | struct lpss_private_data *pdata) |
| 222 | { |
| 223 | const struct lpss_device_desc *dev_desc = pdata->dev_desc; |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 224 | struct lpss_shared_clock *shared_clock = dev_desc->shared_clock; |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 225 | const char *devname = dev_name(&adev->dev); |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 226 | struct clk *clk = ERR_PTR(-ENODEV); |
Rafael J. Wysocki | b59cc20 | 2013-05-08 11:55:49 +0300 | [diff] [blame] | 227 | struct lpss_clk_data *clk_data; |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 228 | const char *parent, *clk_name; |
| 229 | void __iomem *prv_base; |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 230 | |
| 231 | if (!lpss_clk_dev) |
| 232 | lpt_register_clock_device(); |
| 233 | |
Rafael J. Wysocki | b59cc20 | 2013-05-08 11:55:49 +0300 | [diff] [blame] | 234 | clk_data = platform_get_drvdata(lpss_clk_dev); |
| 235 | if (!clk_data) |
| 236 | return -ENODEV; |
| 237 | |
| 238 | if (dev_desc->clkdev_name) { |
| 239 | clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name, |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 240 | devname); |
Rafael J. Wysocki | b59cc20 | 2013-05-08 11:55:49 +0300 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | if (!pdata->mmio_base |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 245 | || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE) |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 246 | return -ENODATA; |
| 247 | |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 248 | parent = clk_data->name; |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 249 | prv_base = pdata->mmio_base + dev_desc->prv_offset; |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 250 | |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 251 | if (shared_clock) { |
| 252 | clk = shared_clock->clk; |
| 253 | if (!clk) { |
| 254 | clk = clk_register_fixed_rate(NULL, shared_clock->name, |
| 255 | "lpss_clk", 0, |
| 256 | shared_clock->rate); |
| 257 | shared_clock->clk = clk; |
| 258 | } |
| 259 | parent = shared_clock->name; |
| 260 | } |
| 261 | |
| 262 | if (dev_desc->clk_gate) { |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 263 | clk = clk_register_gate(NULL, devname, parent, 0, |
| 264 | prv_base, 0, 0, NULL); |
| 265 | parent = devname; |
| 266 | } |
| 267 | |
| 268 | if (dev_desc->clk_divider) { |
| 269 | /* Prevent division by zero */ |
| 270 | if (!readl(prv_base)) |
| 271 | writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base); |
| 272 | |
| 273 | clk_name = kasprintf(GFP_KERNEL, "%s-div", devname); |
| 274 | if (!clk_name) |
| 275 | return -ENOMEM; |
| 276 | clk = clk_register_fractional_divider(NULL, clk_name, parent, |
| 277 | 0, prv_base, |
| 278 | 1, 15, 16, 15, 0, NULL); |
| 279 | parent = clk_name; |
| 280 | |
| 281 | clk_name = kasprintf(GFP_KERNEL, "%s-update", devname); |
| 282 | if (!clk_name) { |
| 283 | kfree(parent); |
| 284 | return -ENOMEM; |
| 285 | } |
| 286 | clk = clk_register_gate(NULL, clk_name, parent, |
| 287 | CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE, |
| 288 | prv_base, 31, 0, NULL); |
| 289 | kfree(parent); |
| 290 | kfree(clk_name); |
Mika Westerberg | f627217 | 2013-05-13 12:42:44 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (IS_ERR(clk)) |
| 294 | return PTR_ERR(clk); |
| 295 | |
Heikki Krogerus | ed3a872 | 2014-05-19 14:42:07 +0300 | [diff] [blame^] | 296 | pdata->clk = clk; |
| 297 | clk_register_clkdev(clk, NULL, devname); |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int acpi_lpss_create_device(struct acpi_device *adev, |
| 302 | const struct acpi_device_id *id) |
| 303 | { |
| 304 | struct lpss_device_desc *dev_desc; |
| 305 | struct lpss_private_data *pdata; |
| 306 | struct resource_list_entry *rentry; |
| 307 | struct list_head resource_list; |
| 308 | int ret; |
| 309 | |
| 310 | dev_desc = (struct lpss_device_desc *)id->driver_data; |
| 311 | if (!dev_desc) |
| 312 | return acpi_create_platform_device(adev, id); |
| 313 | |
| 314 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 315 | if (!pdata) |
| 316 | return -ENOMEM; |
| 317 | |
| 318 | INIT_LIST_HEAD(&resource_list); |
| 319 | ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL); |
| 320 | if (ret < 0) |
| 321 | goto err_out; |
| 322 | |
| 323 | list_for_each_entry(rentry, &resource_list, node) |
| 324 | if (resource_type(&rentry->res) == IORESOURCE_MEM) { |
Mika Westerberg | 958c4eb | 2013-06-18 16:51:35 +0300 | [diff] [blame] | 325 | if (dev_desc->prv_size_override) |
| 326 | pdata->mmio_size = dev_desc->prv_size_override; |
| 327 | else |
| 328 | pdata->mmio_size = resource_size(&rentry->res); |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 329 | pdata->mmio_base = ioremap(rentry->res.start, |
| 330 | pdata->mmio_size); |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | |
| 334 | acpi_dev_free_resource_list(&resource_list); |
| 335 | |
Mika Westerberg | af65cfe | 2013-09-02 13:30:25 +0300 | [diff] [blame] | 336 | pdata->dev_desc = dev_desc; |
| 337 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 338 | if (dev_desc->clk_required) { |
| 339 | ret = register_device_clock(adev, pdata); |
| 340 | if (ret) { |
Rafael J. Wysocki | b9e95fc | 2013-06-19 00:45:34 +0200 | [diff] [blame] | 341 | /* Skip the device, but continue the namespace scan. */ |
| 342 | ret = 0; |
| 343 | goto err_out; |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Rafael J. Wysocki | b9e95fc | 2013-06-19 00:45:34 +0200 | [diff] [blame] | 347 | /* |
| 348 | * This works around a known issue in ACPI tables where LPSS devices |
| 349 | * have _PS0 and _PS3 without _PSC (and no power resources), so |
| 350 | * acpi_bus_init_power() will assume that the BIOS has put them into D0. |
| 351 | */ |
| 352 | ret = acpi_device_fix_up_power(adev); |
| 353 | if (ret) { |
| 354 | /* Skip the device, but continue the namespace scan. */ |
| 355 | ret = 0; |
| 356 | goto err_out; |
| 357 | } |
| 358 | |
Heikki Krogerus | 06d8641 | 2013-06-17 13:25:46 +0300 | [diff] [blame] | 359 | if (dev_desc->setup) |
| 360 | dev_desc->setup(pdata); |
| 361 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 362 | adev->driver_data = pdata; |
| 363 | ret = acpi_create_platform_device(adev, id); |
| 364 | if (ret > 0) |
| 365 | return ret; |
| 366 | |
| 367 | adev->driver_data = NULL; |
| 368 | |
| 369 | err_out: |
| 370 | kfree(pdata); |
| 371 | return ret; |
| 372 | } |
| 373 | |
Rafael J. Wysocki | 1a8f835 | 2014-02-11 00:35:53 +0100 | [diff] [blame] | 374 | static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg) |
| 375 | { |
| 376 | return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg); |
| 377 | } |
| 378 | |
| 379 | static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata, |
| 380 | unsigned int reg) |
| 381 | { |
| 382 | writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg); |
| 383 | } |
| 384 | |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 385 | static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val) |
| 386 | { |
| 387 | struct acpi_device *adev; |
| 388 | struct lpss_private_data *pdata; |
| 389 | unsigned long flags; |
| 390 | int ret; |
| 391 | |
| 392 | ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev); |
| 393 | if (WARN_ON(ret)) |
| 394 | return ret; |
| 395 | |
| 396 | spin_lock_irqsave(&dev->power.lock, flags); |
| 397 | if (pm_runtime_suspended(dev)) { |
| 398 | ret = -EAGAIN; |
| 399 | goto out; |
| 400 | } |
| 401 | pdata = acpi_driver_data(adev); |
| 402 | if (WARN_ON(!pdata || !pdata->mmio_base)) { |
| 403 | ret = -ENODEV; |
| 404 | goto out; |
| 405 | } |
Rafael J. Wysocki | 1a8f835 | 2014-02-11 00:35:53 +0100 | [diff] [blame] | 406 | *val = __lpss_reg_read(pdata, reg); |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 407 | |
| 408 | out: |
| 409 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr, |
| 414 | char *buf) |
| 415 | { |
| 416 | u32 ltr_value = 0; |
| 417 | unsigned int reg; |
| 418 | int ret; |
| 419 | |
| 420 | reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR; |
| 421 | ret = lpss_reg_read(dev, reg, <r_value); |
| 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value); |
| 426 | } |
| 427 | |
| 428 | static ssize_t lpss_ltr_mode_show(struct device *dev, |
| 429 | struct device_attribute *attr, char *buf) |
| 430 | { |
| 431 | u32 ltr_mode = 0; |
| 432 | char *outstr; |
| 433 | int ret; |
| 434 | |
| 435 | ret = lpss_reg_read(dev, LPSS_GENERAL, <r_mode); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | |
| 439 | outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto"; |
| 440 | return sprintf(buf, "%s\n", outstr); |
| 441 | } |
| 442 | |
| 443 | static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL); |
| 444 | static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL); |
| 445 | static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL); |
| 446 | |
| 447 | static struct attribute *lpss_attrs[] = { |
| 448 | &dev_attr_auto_ltr.attr, |
| 449 | &dev_attr_sw_ltr.attr, |
| 450 | &dev_attr_ltr_mode.attr, |
| 451 | NULL, |
| 452 | }; |
| 453 | |
| 454 | static struct attribute_group lpss_attr_group = { |
| 455 | .attrs = lpss_attrs, |
| 456 | .name = "lpss_ltr", |
| 457 | }; |
| 458 | |
Rafael J. Wysocki | 1a8f835 | 2014-02-11 00:35:53 +0100 | [diff] [blame] | 459 | static void acpi_lpss_set_ltr(struct device *dev, s32 val) |
| 460 | { |
| 461 | struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev)); |
| 462 | u32 ltr_mode, ltr_val; |
| 463 | |
| 464 | ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL); |
| 465 | if (val < 0) { |
| 466 | if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) { |
| 467 | ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW; |
| 468 | __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL); |
| 469 | } |
| 470 | return; |
| 471 | } |
| 472 | ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK; |
| 473 | if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) { |
| 474 | ltr_val |= LPSS_LTR_SNOOP_LAT_32US; |
| 475 | val = LPSS_LTR_MAX_VAL; |
| 476 | } else if (val > LPSS_LTR_MAX_VAL) { |
| 477 | ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ; |
| 478 | val >>= LPSS_LTR_SNOOP_LAT_SHIFT; |
| 479 | } else { |
| 480 | ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ; |
| 481 | } |
| 482 | ltr_val |= val; |
| 483 | __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR); |
| 484 | if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) { |
| 485 | ltr_mode |= LPSS_GENERAL_LTR_MODE_SW; |
| 486 | __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL); |
| 487 | } |
| 488 | } |
| 489 | |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 490 | #ifdef CONFIG_PM |
| 491 | /** |
| 492 | * acpi_lpss_save_ctx() - Save the private registers of LPSS device |
| 493 | * @dev: LPSS device |
| 494 | * |
| 495 | * Most LPSS devices have private registers which may loose their context when |
| 496 | * the device is powered down. acpi_lpss_save_ctx() saves those registers into |
| 497 | * prv_reg_ctx array. |
| 498 | */ |
| 499 | static void acpi_lpss_save_ctx(struct device *dev) |
| 500 | { |
| 501 | struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev)); |
| 502 | unsigned int i; |
| 503 | |
| 504 | for (i = 0; i < LPSS_PRV_REG_COUNT; i++) { |
| 505 | unsigned long offset = i * sizeof(u32); |
| 506 | |
| 507 | pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset); |
| 508 | dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n", |
| 509 | pdata->prv_reg_ctx[i], offset); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device |
| 515 | * @dev: LPSS device |
| 516 | * |
| 517 | * Restores the registers that were previously stored with acpi_lpss_save_ctx(). |
| 518 | */ |
| 519 | static void acpi_lpss_restore_ctx(struct device *dev) |
| 520 | { |
| 521 | struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev)); |
| 522 | unsigned int i; |
| 523 | |
| 524 | /* |
| 525 | * The following delay is needed or the subsequent write operations may |
| 526 | * fail. The LPSS devices are actually PCI devices and the PCI spec |
| 527 | * expects 10ms delay before the device can be accessed after D3 to D0 |
| 528 | * transition. |
| 529 | */ |
| 530 | msleep(10); |
| 531 | |
| 532 | for (i = 0; i < LPSS_PRV_REG_COUNT; i++) { |
| 533 | unsigned long offset = i * sizeof(u32); |
| 534 | |
| 535 | __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset); |
| 536 | dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n", |
| 537 | pdata->prv_reg_ctx[i], offset); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | #ifdef CONFIG_PM_SLEEP |
| 542 | static int acpi_lpss_suspend_late(struct device *dev) |
| 543 | { |
| 544 | int ret = pm_generic_suspend_late(dev); |
| 545 | |
| 546 | if (ret) |
| 547 | return ret; |
| 548 | |
| 549 | acpi_lpss_save_ctx(dev); |
| 550 | return acpi_dev_suspend_late(dev); |
| 551 | } |
| 552 | |
| 553 | static int acpi_lpss_restore_early(struct device *dev) |
| 554 | { |
| 555 | int ret = acpi_dev_resume_early(dev); |
| 556 | |
| 557 | if (ret) |
| 558 | return ret; |
| 559 | |
| 560 | acpi_lpss_restore_ctx(dev); |
| 561 | return pm_generic_resume_early(dev); |
| 562 | } |
| 563 | #endif /* CONFIG_PM_SLEEP */ |
| 564 | |
| 565 | #ifdef CONFIG_PM_RUNTIME |
| 566 | static int acpi_lpss_runtime_suspend(struct device *dev) |
| 567 | { |
| 568 | int ret = pm_generic_runtime_suspend(dev); |
| 569 | |
| 570 | if (ret) |
| 571 | return ret; |
| 572 | |
| 573 | acpi_lpss_save_ctx(dev); |
| 574 | return acpi_dev_runtime_suspend(dev); |
| 575 | } |
| 576 | |
| 577 | static int acpi_lpss_runtime_resume(struct device *dev) |
| 578 | { |
| 579 | int ret = acpi_dev_runtime_resume(dev); |
| 580 | |
| 581 | if (ret) |
| 582 | return ret; |
| 583 | |
| 584 | acpi_lpss_restore_ctx(dev); |
| 585 | return pm_generic_runtime_resume(dev); |
| 586 | } |
| 587 | #endif /* CONFIG_PM_RUNTIME */ |
| 588 | #endif /* CONFIG_PM */ |
| 589 | |
| 590 | static struct dev_pm_domain acpi_lpss_pm_domain = { |
| 591 | .ops = { |
| 592 | #ifdef CONFIG_PM_SLEEP |
| 593 | .suspend_late = acpi_lpss_suspend_late, |
| 594 | .restore_early = acpi_lpss_restore_early, |
| 595 | .prepare = acpi_subsys_prepare, |
| 596 | .complete = acpi_subsys_complete, |
| 597 | .suspend = acpi_subsys_suspend, |
| 598 | .resume_early = acpi_subsys_resume_early, |
| 599 | .freeze = acpi_subsys_freeze, |
| 600 | .poweroff = acpi_subsys_suspend, |
| 601 | .poweroff_late = acpi_subsys_suspend_late, |
| 602 | #endif |
| 603 | #ifdef CONFIG_PM_RUNTIME |
| 604 | .runtime_suspend = acpi_lpss_runtime_suspend, |
| 605 | .runtime_resume = acpi_lpss_runtime_resume, |
| 606 | #endif |
| 607 | }, |
| 608 | }; |
| 609 | |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 610 | static int acpi_lpss_platform_notify(struct notifier_block *nb, |
| 611 | unsigned long action, void *data) |
| 612 | { |
| 613 | struct platform_device *pdev = to_platform_device(data); |
| 614 | struct lpss_private_data *pdata; |
| 615 | struct acpi_device *adev; |
| 616 | const struct acpi_device_id *id; |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 617 | |
| 618 | id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev); |
| 619 | if (!id || !id->driver_data) |
| 620 | return 0; |
| 621 | |
| 622 | if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev)) |
| 623 | return 0; |
| 624 | |
| 625 | pdata = acpi_driver_data(adev); |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 626 | if (!pdata || !pdata->mmio_base) |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 627 | return 0; |
| 628 | |
| 629 | if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) { |
| 630 | dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n"); |
| 631 | return 0; |
| 632 | } |
| 633 | |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 634 | switch (action) { |
| 635 | case BUS_NOTIFY_BOUND_DRIVER: |
| 636 | if (pdata->dev_desc->save_ctx) |
| 637 | pdev->dev.pm_domain = &acpi_lpss_pm_domain; |
| 638 | break; |
| 639 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 640 | if (pdata->dev_desc->save_ctx) |
| 641 | pdev->dev.pm_domain = NULL; |
| 642 | break; |
| 643 | case BUS_NOTIFY_ADD_DEVICE: |
| 644 | if (pdata->dev_desc->ltr_required) |
| 645 | return sysfs_create_group(&pdev->dev.kobj, |
| 646 | &lpss_attr_group); |
| 647 | case BUS_NOTIFY_DEL_DEVICE: |
| 648 | if (pdata->dev_desc->ltr_required) |
| 649 | sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group); |
| 650 | default: |
| 651 | break; |
| 652 | } |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 653 | |
Heikki Krogerus | c78b083 | 2014-05-23 16:15:09 +0300 | [diff] [blame] | 654 | return 0; |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static struct notifier_block acpi_lpss_nb = { |
| 658 | .notifier_call = acpi_lpss_platform_notify, |
| 659 | }; |
| 660 | |
Rafael J. Wysocki | 1a8f835 | 2014-02-11 00:35:53 +0100 | [diff] [blame] | 661 | static void acpi_lpss_bind(struct device *dev) |
| 662 | { |
| 663 | struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev)); |
| 664 | |
| 665 | if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required) |
| 666 | return; |
| 667 | |
| 668 | if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) |
| 669 | dev->power.set_latency_tolerance = acpi_lpss_set_ltr; |
| 670 | else |
| 671 | dev_err(dev, "MMIO size insufficient to access LTR\n"); |
| 672 | } |
| 673 | |
| 674 | static void acpi_lpss_unbind(struct device *dev) |
| 675 | { |
| 676 | dev->power.set_latency_tolerance = NULL; |
| 677 | } |
| 678 | |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 679 | static struct acpi_scan_handler lpss_handler = { |
| 680 | .ids = acpi_lpss_device_ids, |
| 681 | .attach = acpi_lpss_create_device, |
Rafael J. Wysocki | 1a8f835 | 2014-02-11 00:35:53 +0100 | [diff] [blame] | 682 | .bind = acpi_lpss_bind, |
| 683 | .unbind = acpi_lpss_unbind, |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | void __init acpi_lpss_init(void) |
| 687 | { |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 688 | if (!lpt_clk_init()) { |
| 689 | bus_register_notifier(&platform_bus_type, &acpi_lpss_nb); |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 690 | acpi_scan_add_handler(&lpss_handler); |
Rafael J. Wysocki | 2e0f882 | 2013-03-06 23:46:28 +0100 | [diff] [blame] | 691 | } |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 692 | } |