acpi_lpss.c 25 KB

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