acpi_lpss.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. #include <linux/acpi.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/mutex.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/platform_data/clk-lpss.h>
  20. #include <linux/platform_data/x86/pmc_atom.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/pwm.h>
  24. #include <linux/delay.h>
  25. #include "internal.h"
  26. ACPI_MODULE_NAME("acpi_lpss");
  27. #ifdef CONFIG_X86_INTEL_LPSS
  28. #include <asm/cpu_device_id.h>
  29. #include <asm/intel-family.h>
  30. #include <asm/iosf_mbi.h>
  31. #define LPSS_ADDR(desc) ((unsigned long)&desc)
  32. #define LPSS_CLK_SIZE 0x04
  33. #define LPSS_LTR_SIZE 0x18
  34. /* Offsets relative to LPSS_PRIVATE_OFFSET */
  35. #define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16))
  36. #define LPSS_RESETS 0x04
  37. #define LPSS_RESETS_RESET_FUNC BIT(0)
  38. #define LPSS_RESETS_RESET_APB BIT(1)
  39. #define LPSS_GENERAL 0x08
  40. #define LPSS_GENERAL_LTR_MODE_SW BIT(2)
  41. #define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
  42. #define LPSS_SW_LTR 0x10
  43. #define LPSS_AUTO_LTR 0x14
  44. #define LPSS_LTR_SNOOP_REQ BIT(15)
  45. #define LPSS_LTR_SNOOP_MASK 0x0000FFFF
  46. #define LPSS_LTR_SNOOP_LAT_1US 0x800
  47. #define LPSS_LTR_SNOOP_LAT_32US 0xC00
  48. #define LPSS_LTR_SNOOP_LAT_SHIFT 5
  49. #define LPSS_LTR_SNOOP_LAT_CUTOFF 3000
  50. #define LPSS_LTR_MAX_VAL 0x3FF
  51. #define LPSS_TX_INT 0x20
  52. #define LPSS_TX_INT_MASK BIT(1)
  53. #define LPSS_PRV_REG_COUNT 9
  54. /* LPSS Flags */
  55. #define LPSS_CLK BIT(0)
  56. #define LPSS_CLK_GATE BIT(1)
  57. #define LPSS_CLK_DIVIDER BIT(2)
  58. #define LPSS_LTR BIT(3)
  59. #define LPSS_SAVE_CTX BIT(4)
  60. #define LPSS_NO_D3_DELAY BIT(5)
  61. /* Crystal Cove PMIC shares same ACPI ID between different platforms */
  62. #define BYT_CRC_HRV 2
  63. #define CHT_CRC_HRV 3
  64. struct lpss_private_data;
  65. struct lpss_device_desc {
  66. unsigned int flags;
  67. const char *clk_con_id;
  68. unsigned int prv_offset;
  69. size_t prv_size_override;
  70. struct property_entry *properties;
  71. void (*setup)(struct lpss_private_data *pdata);
  72. };
  73. static const struct lpss_device_desc lpss_dma_desc = {
  74. .flags = LPSS_CLK,
  75. };
  76. struct lpss_private_data {
  77. struct acpi_device *adev;
  78. void __iomem *mmio_base;
  79. resource_size_t mmio_size;
  80. unsigned int fixed_clk_rate;
  81. struct clk *clk;
  82. const struct lpss_device_desc *dev_desc;
  83. u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
  84. };
  85. /* Devices which need to be in D3 before lpss_iosf_enter_d3_state() proceeds */
  86. static u32 pmc_atom_d3_mask = 0xfe000ffe;
  87. /* LPSS run time quirks */
  88. static unsigned int lpss_quirks;
  89. /*
  90. * LPSS_QUIRK_ALWAYS_POWER_ON: override power state for LPSS DMA device.
  91. *
  92. * The LPSS DMA controller has neither _PS0 nor _PS3 method. Moreover
  93. * it can be powered off automatically whenever the last LPSS device goes down.
  94. * In case of no power any access to the DMA controller will hang the system.
  95. * The behaviour is reproduced on some HP laptops based on Intel BayTrail as
  96. * well as on ASuS T100TA transformer.
  97. *
  98. * This quirk overrides power state of entire LPSS island to keep DMA powered
  99. * on whenever we have at least one other device in use.
  100. */
  101. #define LPSS_QUIRK_ALWAYS_POWER_ON BIT(0)
  102. /* UART Component Parameter Register */
  103. #define LPSS_UART_CPR 0xF4
  104. #define LPSS_UART_CPR_AFCE BIT(4)
  105. static void lpss_uart_setup(struct lpss_private_data *pdata)
  106. {
  107. unsigned int offset;
  108. u32 val;
  109. offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
  110. val = readl(pdata->mmio_base + offset);
  111. writel(val | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
  112. val = readl(pdata->mmio_base + LPSS_UART_CPR);
  113. if (!(val & LPSS_UART_CPR_AFCE)) {
  114. offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
  115. val = readl(pdata->mmio_base + offset);
  116. val |= LPSS_GENERAL_UART_RTS_OVRD;
  117. writel(val, pdata->mmio_base + offset);
  118. }
  119. }
  120. static void lpss_deassert_reset(struct lpss_private_data *pdata)
  121. {
  122. unsigned int offset;
  123. u32 val;
  124. offset = pdata->dev_desc->prv_offset + LPSS_RESETS;
  125. val = readl(pdata->mmio_base + offset);
  126. val |= LPSS_RESETS_RESET_APB | LPSS_RESETS_RESET_FUNC;
  127. writel(val, pdata->mmio_base + offset);
  128. }
  129. /*
  130. * BYT PWM used for backlight control by the i915 driver on systems without
  131. * the Crystal Cove PMIC.
  132. */
  133. static struct pwm_lookup byt_pwm_lookup[] = {
  134. PWM_LOOKUP_WITH_MODULE("80860F09:00", 0, "0000:00:02.0",
  135. "pwm_backlight", 0, PWM_POLARITY_NORMAL,
  136. "pwm-lpss-platform"),
  137. };
  138. static void byt_pwm_setup(struct lpss_private_data *pdata)
  139. {
  140. struct acpi_device *adev = pdata->adev;
  141. /* Only call pwm_add_table for the first PWM controller */
  142. if (!adev->pnp.unique_id || strcmp(adev->pnp.unique_id, "1"))
  143. return;
  144. if (!acpi_dev_present("INT33FD", NULL, BYT_CRC_HRV))
  145. pwm_add_table(byt_pwm_lookup, ARRAY_SIZE(byt_pwm_lookup));
  146. }
  147. #define LPSS_I2C_ENABLE 0x6c
  148. static void byt_i2c_setup(struct lpss_private_data *pdata)
  149. {
  150. const char *uid_str = acpi_device_uid(pdata->adev);
  151. acpi_handle handle = pdata->adev->handle;
  152. unsigned long long shared_host = 0;
  153. acpi_status status;
  154. long uid = 0;
  155. /* Expected to always be true, but better safe then sorry */
  156. if (uid_str)
  157. uid = simple_strtol(uid_str, NULL, 10);
  158. /* Detect I2C bus shared with PUNIT and ignore its d3 status */
  159. status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
  160. if (ACPI_SUCCESS(status) && shared_host && uid)
  161. pmc_atom_d3_mask &= ~(BIT_LPSS2_F1_I2C1 << (uid - 1));
  162. lpss_deassert_reset(pdata);
  163. if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset))
  164. pdata->fixed_clk_rate = 133000000;
  165. writel(0, pdata->mmio_base + LPSS_I2C_ENABLE);
  166. }
  167. /* BSW PWM used for backlight control by the i915 driver */
  168. static struct pwm_lookup bsw_pwm_lookup[] = {
  169. PWM_LOOKUP_WITH_MODULE("80862288:00", 0, "0000:00:02.0",
  170. "pwm_backlight", 0, PWM_POLARITY_NORMAL,
  171. "pwm-lpss-platform"),
  172. };
  173. static void bsw_pwm_setup(struct lpss_private_data *pdata)
  174. {
  175. struct acpi_device *adev = pdata->adev;
  176. /* Only call pwm_add_table for the first PWM controller */
  177. if (!adev->pnp.unique_id || strcmp(adev->pnp.unique_id, "1"))
  178. return;
  179. pwm_add_table(bsw_pwm_lookup, ARRAY_SIZE(bsw_pwm_lookup));
  180. }
  181. static const struct lpss_device_desc lpt_dev_desc = {
  182. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
  183. .prv_offset = 0x800,
  184. };
  185. static const struct lpss_device_desc lpt_i2c_dev_desc = {
  186. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR,
  187. .prv_offset = 0x800,
  188. };
  189. static struct property_entry uart_properties[] = {
  190. PROPERTY_ENTRY_U32("reg-io-width", 4),
  191. PROPERTY_ENTRY_U32("reg-shift", 2),
  192. PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
  193. { },
  194. };
  195. static const struct lpss_device_desc lpt_uart_dev_desc = {
  196. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
  197. .clk_con_id = "baudclk",
  198. .prv_offset = 0x800,
  199. .setup = lpss_uart_setup,
  200. .properties = uart_properties,
  201. };
  202. static const struct lpss_device_desc lpt_sdio_dev_desc = {
  203. .flags = LPSS_LTR,
  204. .prv_offset = 0x1000,
  205. .prv_size_override = 0x1018,
  206. };
  207. static const struct lpss_device_desc byt_pwm_dev_desc = {
  208. .flags = LPSS_SAVE_CTX,
  209. .prv_offset = 0x800,
  210. .setup = byt_pwm_setup,
  211. };
  212. static const struct lpss_device_desc bsw_pwm_dev_desc = {
  213. .flags = LPSS_SAVE_CTX | LPSS_NO_D3_DELAY,
  214. .prv_offset = 0x800,
  215. .setup = bsw_pwm_setup,
  216. };
  217. static const struct lpss_device_desc byt_uart_dev_desc = {
  218. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
  219. .clk_con_id = "baudclk",
  220. .prv_offset = 0x800,
  221. .setup = lpss_uart_setup,
  222. .properties = uart_properties,
  223. };
  224. static const struct lpss_device_desc bsw_uart_dev_desc = {
  225. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX
  226. | LPSS_NO_D3_DELAY,
  227. .clk_con_id = "baudclk",
  228. .prv_offset = 0x800,
  229. .setup = lpss_uart_setup,
  230. .properties = uart_properties,
  231. };
  232. static const struct lpss_device_desc byt_spi_dev_desc = {
  233. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
  234. .prv_offset = 0x400,
  235. };
  236. static const struct lpss_device_desc byt_sdio_dev_desc = {
  237. .flags = LPSS_CLK,
  238. };
  239. static const struct lpss_device_desc byt_i2c_dev_desc = {
  240. .flags = LPSS_CLK | LPSS_SAVE_CTX,
  241. .prv_offset = 0x800,
  242. .setup = byt_i2c_setup,
  243. };
  244. static const struct lpss_device_desc bsw_i2c_dev_desc = {
  245. .flags = LPSS_CLK | LPSS_SAVE_CTX | LPSS_NO_D3_DELAY,
  246. .prv_offset = 0x800,
  247. .setup = byt_i2c_setup,
  248. };
  249. static const struct lpss_device_desc bsw_spi_dev_desc = {
  250. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX
  251. | LPSS_NO_D3_DELAY,
  252. .prv_offset = 0x400,
  253. .setup = lpss_deassert_reset,
  254. };
  255. #define ICPU(model) { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, }
  256. static const struct x86_cpu_id lpss_cpu_ids[] = {
  257. ICPU(INTEL_FAM6_ATOM_SILVERMONT), /* Valleyview, Bay Trail */
  258. ICPU(INTEL_FAM6_ATOM_AIRMONT), /* Braswell, Cherry Trail */
  259. {}
  260. };
  261. #else
  262. #define LPSS_ADDR(desc) (0UL)
  263. #endif /* CONFIG_X86_INTEL_LPSS */
  264. static const struct acpi_device_id acpi_lpss_device_ids[] = {
  265. /* Generic LPSS devices */
  266. { "INTL9C60", LPSS_ADDR(lpss_dma_desc) },
  267. /* Lynxpoint LPSS devices */
  268. { "INT33C0", LPSS_ADDR(lpt_dev_desc) },
  269. { "INT33C1", LPSS_ADDR(lpt_dev_desc) },
  270. { "INT33C2", LPSS_ADDR(lpt_i2c_dev_desc) },
  271. { "INT33C3", LPSS_ADDR(lpt_i2c_dev_desc) },
  272. { "INT33C4", LPSS_ADDR(lpt_uart_dev_desc) },
  273. { "INT33C5", LPSS_ADDR(lpt_uart_dev_desc) },
  274. { "INT33C6", LPSS_ADDR(lpt_sdio_dev_desc) },
  275. { "INT33C7", },
  276. /* BayTrail LPSS devices */
  277. { "80860F09", LPSS_ADDR(byt_pwm_dev_desc) },
  278. { "80860F0A", LPSS_ADDR(byt_uart_dev_desc) },
  279. { "80860F0E", LPSS_ADDR(byt_spi_dev_desc) },
  280. { "80860F14", LPSS_ADDR(byt_sdio_dev_desc) },
  281. { "80860F41", LPSS_ADDR(byt_i2c_dev_desc) },
  282. { "INT33B2", },
  283. { "INT33FC", },
  284. /* Braswell LPSS devices */
  285. { "80862286", LPSS_ADDR(lpss_dma_desc) },
  286. { "80862288", LPSS_ADDR(bsw_pwm_dev_desc) },
  287. { "8086228A", LPSS_ADDR(bsw_uart_dev_desc) },
  288. { "8086228E", LPSS_ADDR(bsw_spi_dev_desc) },
  289. { "808622C0", LPSS_ADDR(lpss_dma_desc) },
  290. { "808622C1", LPSS_ADDR(bsw_i2c_dev_desc) },
  291. /* Broadwell LPSS devices */
  292. { "INT3430", LPSS_ADDR(lpt_dev_desc) },
  293. { "INT3431", LPSS_ADDR(lpt_dev_desc) },
  294. { "INT3432", LPSS_ADDR(lpt_i2c_dev_desc) },
  295. { "INT3433", LPSS_ADDR(lpt_i2c_dev_desc) },
  296. { "INT3434", LPSS_ADDR(lpt_uart_dev_desc) },
  297. { "INT3435", LPSS_ADDR(lpt_uart_dev_desc) },
  298. { "INT3436", LPSS_ADDR(lpt_sdio_dev_desc) },
  299. { "INT3437", },
  300. /* Wildcat Point LPSS devices */
  301. { "INT3438", LPSS_ADDR(lpt_dev_desc) },
  302. { }
  303. };
  304. #ifdef CONFIG_X86_INTEL_LPSS
  305. static int is_memory(struct acpi_resource *res, void *not_used)
  306. {
  307. struct resource r;
  308. return !acpi_dev_resource_memory(res, &r);
  309. }
  310. /* LPSS main clock device. */
  311. static struct platform_device *lpss_clk_dev;
  312. static inline void lpt_register_clock_device(void)
  313. {
  314. lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
  315. }
  316. static int register_device_clock(struct acpi_device *adev,
  317. struct lpss_private_data *pdata)
  318. {
  319. const struct lpss_device_desc *dev_desc = pdata->dev_desc;
  320. const char *devname = dev_name(&adev->dev);
  321. struct clk *clk = ERR_PTR(-ENODEV);
  322. struct lpss_clk_data *clk_data;
  323. const char *parent, *clk_name;
  324. void __iomem *prv_base;
  325. if (!lpss_clk_dev)
  326. lpt_register_clock_device();
  327. clk_data = platform_get_drvdata(lpss_clk_dev);
  328. if (!clk_data)
  329. return -ENODEV;
  330. clk = clk_data->clk;
  331. if (!pdata->mmio_base
  332. || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
  333. return -ENODATA;
  334. parent = clk_data->name;
  335. prv_base = pdata->mmio_base + dev_desc->prv_offset;
  336. if (pdata->fixed_clk_rate) {
  337. clk = clk_register_fixed_rate(NULL, devname, parent, 0,
  338. pdata->fixed_clk_rate);
  339. goto out;
  340. }
  341. if (dev_desc->flags & LPSS_CLK_GATE) {
  342. clk = clk_register_gate(NULL, devname, parent, 0,
  343. prv_base, 0, 0, NULL);
  344. parent = devname;
  345. }
  346. if (dev_desc->flags & LPSS_CLK_DIVIDER) {
  347. /* Prevent division by zero */
  348. if (!readl(prv_base))
  349. writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
  350. clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
  351. if (!clk_name)
  352. return -ENOMEM;
  353. clk = clk_register_fractional_divider(NULL, clk_name, parent,
  354. 0, prv_base,
  355. 1, 15, 16, 15, 0, NULL);
  356. parent = clk_name;
  357. clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
  358. if (!clk_name) {
  359. kfree(parent);
  360. return -ENOMEM;
  361. }
  362. clk = clk_register_gate(NULL, clk_name, parent,
  363. CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
  364. prv_base, 31, 0, NULL);
  365. kfree(parent);
  366. kfree(clk_name);
  367. }
  368. out:
  369. if (IS_ERR(clk))
  370. return PTR_ERR(clk);
  371. pdata->clk = clk;
  372. clk_register_clkdev(clk, dev_desc->clk_con_id, devname);
  373. return 0;
  374. }
  375. static int acpi_lpss_create_device(struct acpi_device *adev,
  376. const struct acpi_device_id *id)
  377. {
  378. const struct lpss_device_desc *dev_desc;
  379. struct lpss_private_data *pdata;
  380. struct resource_entry *rentry;
  381. struct list_head resource_list;
  382. struct platform_device *pdev;
  383. int ret;
  384. dev_desc = (const struct lpss_device_desc *)id->driver_data;
  385. if (!dev_desc) {
  386. pdev = acpi_create_platform_device(adev, NULL);
  387. return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
  388. }
  389. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  390. if (!pdata)
  391. return -ENOMEM;
  392. INIT_LIST_HEAD(&resource_list);
  393. ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
  394. if (ret < 0)
  395. goto err_out;
  396. list_for_each_entry(rentry, &resource_list, node)
  397. if (resource_type(rentry->res) == IORESOURCE_MEM) {
  398. if (dev_desc->prv_size_override)
  399. pdata->mmio_size = dev_desc->prv_size_override;
  400. else
  401. pdata->mmio_size = resource_size(rentry->res);
  402. pdata->mmio_base = ioremap(rentry->res->start,
  403. pdata->mmio_size);
  404. break;
  405. }
  406. acpi_dev_free_resource_list(&resource_list);
  407. if (!pdata->mmio_base) {
  408. /* Avoid acpi_bus_attach() instantiating a pdev for this dev. */
  409. adev->pnp.type.platform_id = 0;
  410. /* Skip the device, but continue the namespace scan. */
  411. ret = 0;
  412. goto err_out;
  413. }
  414. pdata->adev = adev;
  415. pdata->dev_desc = dev_desc;
  416. if (dev_desc->setup)
  417. dev_desc->setup(pdata);
  418. if (dev_desc->flags & LPSS_CLK) {
  419. ret = register_device_clock(adev, pdata);
  420. if (ret) {
  421. /* Skip the device, but continue the namespace scan. */
  422. ret = 0;
  423. goto err_out;
  424. }
  425. }
  426. /*
  427. * This works around a known issue in ACPI tables where LPSS devices
  428. * have _PS0 and _PS3 without _PSC (and no power resources), so
  429. * acpi_bus_init_power() will assume that the BIOS has put them into D0.
  430. */
  431. acpi_device_fix_up_power(adev);
  432. adev->driver_data = pdata;
  433. pdev = acpi_create_platform_device(adev, dev_desc->properties);
  434. if (!IS_ERR_OR_NULL(pdev)) {
  435. return 1;
  436. }
  437. ret = PTR_ERR(pdev);
  438. adev->driver_data = NULL;
  439. err_out:
  440. kfree(pdata);
  441. return ret;
  442. }
  443. static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
  444. {
  445. return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
  446. }
  447. static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
  448. unsigned int reg)
  449. {
  450. writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
  451. }
  452. static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
  453. {
  454. struct acpi_device *adev;
  455. struct lpss_private_data *pdata;
  456. unsigned long flags;
  457. int ret;
  458. ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
  459. if (WARN_ON(ret))
  460. return ret;
  461. spin_lock_irqsave(&dev->power.lock, flags);
  462. if (pm_runtime_suspended(dev)) {
  463. ret = -EAGAIN;
  464. goto out;
  465. }
  466. pdata = acpi_driver_data(adev);
  467. if (WARN_ON(!pdata || !pdata->mmio_base)) {
  468. ret = -ENODEV;
  469. goto out;
  470. }
  471. *val = __lpss_reg_read(pdata, reg);
  472. out:
  473. spin_unlock_irqrestore(&dev->power.lock, flags);
  474. return ret;
  475. }
  476. static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
  477. char *buf)
  478. {
  479. u32 ltr_value = 0;
  480. unsigned int reg;
  481. int ret;
  482. reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
  483. ret = lpss_reg_read(dev, reg, &ltr_value);
  484. if (ret)
  485. return ret;
  486. return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
  487. }
  488. static ssize_t lpss_ltr_mode_show(struct device *dev,
  489. struct device_attribute *attr, char *buf)
  490. {
  491. u32 ltr_mode = 0;
  492. char *outstr;
  493. int ret;
  494. ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
  495. if (ret)
  496. return ret;
  497. outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
  498. return sprintf(buf, "%s\n", outstr);
  499. }
  500. static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
  501. static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
  502. static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
  503. static struct attribute *lpss_attrs[] = {
  504. &dev_attr_auto_ltr.attr,
  505. &dev_attr_sw_ltr.attr,
  506. &dev_attr_ltr_mode.attr,
  507. NULL,
  508. };
  509. static const struct attribute_group lpss_attr_group = {
  510. .attrs = lpss_attrs,
  511. .name = "lpss_ltr",
  512. };
  513. static void acpi_lpss_set_ltr(struct device *dev, s32 val)
  514. {
  515. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  516. u32 ltr_mode, ltr_val;
  517. ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
  518. if (val < 0) {
  519. if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
  520. ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
  521. __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
  522. }
  523. return;
  524. }
  525. ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
  526. if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
  527. ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
  528. val = LPSS_LTR_MAX_VAL;
  529. } else if (val > LPSS_LTR_MAX_VAL) {
  530. ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
  531. val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
  532. } else {
  533. ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
  534. }
  535. ltr_val |= val;
  536. __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
  537. if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
  538. ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
  539. __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
  540. }
  541. }
  542. #ifdef CONFIG_PM
  543. /**
  544. * acpi_lpss_save_ctx() - Save the private registers of LPSS device
  545. * @dev: LPSS device
  546. * @pdata: pointer to the private data of the LPSS device
  547. *
  548. * Most LPSS devices have private registers which may loose their context when
  549. * the device is powered down. acpi_lpss_save_ctx() saves those registers into
  550. * prv_reg_ctx array.
  551. */
  552. static void acpi_lpss_save_ctx(struct device *dev,
  553. struct lpss_private_data *pdata)
  554. {
  555. unsigned int i;
  556. for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
  557. unsigned long offset = i * sizeof(u32);
  558. pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
  559. dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
  560. pdata->prv_reg_ctx[i], offset);
  561. }
  562. }
  563. /**
  564. * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
  565. * @dev: LPSS device
  566. * @pdata: pointer to the private data of the LPSS device
  567. *
  568. * Restores the registers that were previously stored with acpi_lpss_save_ctx().
  569. */
  570. static void acpi_lpss_restore_ctx(struct device *dev,
  571. struct lpss_private_data *pdata)
  572. {
  573. unsigned int i;
  574. for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
  575. unsigned long offset = i * sizeof(u32);
  576. __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
  577. dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
  578. pdata->prv_reg_ctx[i], offset);
  579. }
  580. }
  581. static void acpi_lpss_d3_to_d0_delay(struct lpss_private_data *pdata)
  582. {
  583. /*
  584. * The following delay is needed or the subsequent write operations may
  585. * fail. The LPSS devices are actually PCI devices and the PCI spec
  586. * expects 10ms delay before the device can be accessed after D3 to D0
  587. * transition. However some platforms like BSW does not need this delay.
  588. */
  589. unsigned int delay = 10; /* default 10ms delay */
  590. if (pdata->dev_desc->flags & LPSS_NO_D3_DELAY)
  591. delay = 0;
  592. msleep(delay);
  593. }
  594. static int acpi_lpss_activate(struct device *dev)
  595. {
  596. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  597. int ret;
  598. ret = acpi_dev_runtime_resume(dev);
  599. if (ret)
  600. return ret;
  601. acpi_lpss_d3_to_d0_delay(pdata);
  602. /*
  603. * This is called only on ->probe() stage where a device is either in
  604. * known state defined by BIOS or most likely powered off. Due to this
  605. * we have to deassert reset line to be sure that ->probe() will
  606. * recognize the device.
  607. */
  608. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  609. lpss_deassert_reset(pdata);
  610. return 0;
  611. }
  612. static void acpi_lpss_dismiss(struct device *dev)
  613. {
  614. acpi_dev_runtime_suspend(dev);
  615. }
  616. #ifdef CONFIG_PM_SLEEP
  617. static int acpi_lpss_suspend_late(struct device *dev)
  618. {
  619. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  620. int ret;
  621. ret = pm_generic_suspend_late(dev);
  622. if (ret)
  623. return ret;
  624. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  625. acpi_lpss_save_ctx(dev, pdata);
  626. return acpi_dev_suspend_late(dev);
  627. }
  628. static int acpi_lpss_resume_early(struct device *dev)
  629. {
  630. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  631. int ret;
  632. ret = acpi_dev_resume_early(dev);
  633. if (ret)
  634. return ret;
  635. acpi_lpss_d3_to_d0_delay(pdata);
  636. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  637. acpi_lpss_restore_ctx(dev, pdata);
  638. return pm_generic_resume_early(dev);
  639. }
  640. #endif /* CONFIG_PM_SLEEP */
  641. /* IOSF SB for LPSS island */
  642. #define LPSS_IOSF_UNIT_LPIOEP 0xA0
  643. #define LPSS_IOSF_UNIT_LPIO1 0xAB
  644. #define LPSS_IOSF_UNIT_LPIO2 0xAC
  645. #define LPSS_IOSF_PMCSR 0x84
  646. #define LPSS_PMCSR_D0 0
  647. #define LPSS_PMCSR_D3hot 3
  648. #define LPSS_PMCSR_Dx_MASK GENMASK(1, 0)
  649. #define LPSS_IOSF_GPIODEF0 0x154
  650. #define LPSS_GPIODEF0_DMA1_D3 BIT(2)
  651. #define LPSS_GPIODEF0_DMA2_D3 BIT(3)
  652. #define LPSS_GPIODEF0_DMA_D3_MASK GENMASK(3, 2)
  653. #define LPSS_GPIODEF0_DMA_LLP BIT(13)
  654. static DEFINE_MUTEX(lpss_iosf_mutex);
  655. static void lpss_iosf_enter_d3_state(void)
  656. {
  657. u32 value1 = 0;
  658. u32 mask1 = LPSS_GPIODEF0_DMA_D3_MASK | LPSS_GPIODEF0_DMA_LLP;
  659. u32 value2 = LPSS_PMCSR_D3hot;
  660. u32 mask2 = LPSS_PMCSR_Dx_MASK;
  661. /*
  662. * PMC provides an information about actual status of the LPSS devices.
  663. * Here we read the values related to LPSS power island, i.e. LPSS
  664. * devices, excluding both LPSS DMA controllers, along with SCC domain.
  665. */
  666. u32 func_dis, d3_sts_0, pmc_status;
  667. int ret;
  668. ret = pmc_atom_read(PMC_FUNC_DIS, &func_dis);
  669. if (ret)
  670. return;
  671. mutex_lock(&lpss_iosf_mutex);
  672. ret = pmc_atom_read(PMC_D3_STS_0, &d3_sts_0);
  673. if (ret)
  674. goto exit;
  675. /*
  676. * Get the status of entire LPSS power island per device basis.
  677. * Shutdown both LPSS DMA controllers if and only if all other devices
  678. * are already in D3hot.
  679. */
  680. pmc_status = (~(d3_sts_0 | func_dis)) & pmc_atom_d3_mask;
  681. if (pmc_status)
  682. goto exit;
  683. iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO1, MBI_CFG_WRITE,
  684. LPSS_IOSF_PMCSR, value2, mask2);
  685. iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO2, MBI_CFG_WRITE,
  686. LPSS_IOSF_PMCSR, value2, mask2);
  687. iosf_mbi_modify(LPSS_IOSF_UNIT_LPIOEP, MBI_CR_WRITE,
  688. LPSS_IOSF_GPIODEF0, value1, mask1);
  689. exit:
  690. mutex_unlock(&lpss_iosf_mutex);
  691. }
  692. static void lpss_iosf_exit_d3_state(void)
  693. {
  694. u32 value1 = LPSS_GPIODEF0_DMA1_D3 | LPSS_GPIODEF0_DMA2_D3 |
  695. LPSS_GPIODEF0_DMA_LLP;
  696. u32 mask1 = LPSS_GPIODEF0_DMA_D3_MASK | LPSS_GPIODEF0_DMA_LLP;
  697. u32 value2 = LPSS_PMCSR_D0;
  698. u32 mask2 = LPSS_PMCSR_Dx_MASK;
  699. mutex_lock(&lpss_iosf_mutex);
  700. iosf_mbi_modify(LPSS_IOSF_UNIT_LPIOEP, MBI_CR_WRITE,
  701. LPSS_IOSF_GPIODEF0, value1, mask1);
  702. iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO2, MBI_CFG_WRITE,
  703. LPSS_IOSF_PMCSR, value2, mask2);
  704. iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO1, MBI_CFG_WRITE,
  705. LPSS_IOSF_PMCSR, value2, mask2);
  706. mutex_unlock(&lpss_iosf_mutex);
  707. }
  708. static int acpi_lpss_runtime_suspend(struct device *dev)
  709. {
  710. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  711. int ret;
  712. ret = pm_generic_runtime_suspend(dev);
  713. if (ret)
  714. return ret;
  715. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  716. acpi_lpss_save_ctx(dev, pdata);
  717. ret = acpi_dev_runtime_suspend(dev);
  718. /*
  719. * This call must be last in the sequence, otherwise PMC will return
  720. * wrong status for devices being about to be powered off. See
  721. * lpss_iosf_enter_d3_state() for further information.
  722. */
  723. if (lpss_quirks & LPSS_QUIRK_ALWAYS_POWER_ON && iosf_mbi_available())
  724. lpss_iosf_enter_d3_state();
  725. return ret;
  726. }
  727. static int acpi_lpss_runtime_resume(struct device *dev)
  728. {
  729. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  730. int ret;
  731. /*
  732. * This call is kept first to be in symmetry with
  733. * acpi_lpss_runtime_suspend() one.
  734. */
  735. if (lpss_quirks & LPSS_QUIRK_ALWAYS_POWER_ON && iosf_mbi_available())
  736. lpss_iosf_exit_d3_state();
  737. ret = acpi_dev_runtime_resume(dev);
  738. if (ret)
  739. return ret;
  740. acpi_lpss_d3_to_d0_delay(pdata);
  741. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  742. acpi_lpss_restore_ctx(dev, pdata);
  743. return pm_generic_runtime_resume(dev);
  744. }
  745. #endif /* CONFIG_PM */
  746. static struct dev_pm_domain acpi_lpss_pm_domain = {
  747. #ifdef CONFIG_PM
  748. .activate = acpi_lpss_activate,
  749. .dismiss = acpi_lpss_dismiss,
  750. #endif
  751. .ops = {
  752. #ifdef CONFIG_PM
  753. #ifdef CONFIG_PM_SLEEP
  754. .prepare = acpi_subsys_prepare,
  755. .complete = pm_complete_with_resume_check,
  756. .suspend = acpi_subsys_suspend,
  757. .suspend_late = acpi_lpss_suspend_late,
  758. .resume_early = acpi_lpss_resume_early,
  759. .freeze = acpi_subsys_freeze,
  760. .poweroff = acpi_subsys_suspend,
  761. .poweroff_late = acpi_lpss_suspend_late,
  762. .restore_early = acpi_lpss_resume_early,
  763. #endif
  764. .runtime_suspend = acpi_lpss_runtime_suspend,
  765. .runtime_resume = acpi_lpss_runtime_resume,
  766. #endif
  767. },
  768. };
  769. static int acpi_lpss_platform_notify(struct notifier_block *nb,
  770. unsigned long action, void *data)
  771. {
  772. struct platform_device *pdev = to_platform_device(data);
  773. struct lpss_private_data *pdata;
  774. struct acpi_device *adev;
  775. const struct acpi_device_id *id;
  776. id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
  777. if (!id || !id->driver_data)
  778. return 0;
  779. if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
  780. return 0;
  781. pdata = acpi_driver_data(adev);
  782. if (!pdata)
  783. return 0;
  784. if (pdata->mmio_base &&
  785. pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
  786. dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
  787. return 0;
  788. }
  789. switch (action) {
  790. case BUS_NOTIFY_BIND_DRIVER:
  791. dev_pm_domain_set(&pdev->dev, &acpi_lpss_pm_domain);
  792. break;
  793. case BUS_NOTIFY_DRIVER_NOT_BOUND:
  794. case BUS_NOTIFY_UNBOUND_DRIVER:
  795. dev_pm_domain_set(&pdev->dev, NULL);
  796. break;
  797. case BUS_NOTIFY_ADD_DEVICE:
  798. dev_pm_domain_set(&pdev->dev, &acpi_lpss_pm_domain);
  799. if (pdata->dev_desc->flags & LPSS_LTR)
  800. return sysfs_create_group(&pdev->dev.kobj,
  801. &lpss_attr_group);
  802. break;
  803. case BUS_NOTIFY_DEL_DEVICE:
  804. if (pdata->dev_desc->flags & LPSS_LTR)
  805. sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
  806. dev_pm_domain_set(&pdev->dev, NULL);
  807. break;
  808. default:
  809. break;
  810. }
  811. return 0;
  812. }
  813. static struct notifier_block acpi_lpss_nb = {
  814. .notifier_call = acpi_lpss_platform_notify,
  815. };
  816. static void acpi_lpss_bind(struct device *dev)
  817. {
  818. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  819. if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR))
  820. return;
  821. if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
  822. dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
  823. else
  824. dev_err(dev, "MMIO size insufficient to access LTR\n");
  825. }
  826. static void acpi_lpss_unbind(struct device *dev)
  827. {
  828. dev->power.set_latency_tolerance = NULL;
  829. }
  830. static struct acpi_scan_handler lpss_handler = {
  831. .ids = acpi_lpss_device_ids,
  832. .attach = acpi_lpss_create_device,
  833. .bind = acpi_lpss_bind,
  834. .unbind = acpi_lpss_unbind,
  835. };
  836. void __init acpi_lpss_init(void)
  837. {
  838. const struct x86_cpu_id *id;
  839. int ret;
  840. ret = lpt_clk_init();
  841. if (ret)
  842. return;
  843. id = x86_match_cpu(lpss_cpu_ids);
  844. if (id)
  845. lpss_quirks |= LPSS_QUIRK_ALWAYS_POWER_ON;
  846. bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
  847. acpi_scan_add_handler(&lpss_handler);
  848. }
  849. #else
  850. static struct acpi_scan_handler lpss_handler = {
  851. .ids = acpi_lpss_device_ids,
  852. };
  853. void __init acpi_lpss_init(void)
  854. {
  855. acpi_scan_add_handler(&lpss_handler);
  856. }
  857. #endif /* CONFIG_X86_INTEL_LPSS */