acpi_lpss.c 25 KB

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