intel-lpss.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Intel Sunrisepoint LPSS core support.
  3. *
  4. * Copyright (C) 2015, Intel Corporation
  5. *
  6. * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. * Heikki Krogerus <heikki.krogerus@linux.intel.com>
  9. * Jarkko Nikula <jarkko.nikula@linux.intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/clkdev.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/idr.h>
  20. #include <linux/ioport.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/pm_qos.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/property.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/io-64-nonatomic-lo-hi.h>
  29. #include "intel-lpss.h"
  30. #define LPSS_DEV_OFFSET 0x000
  31. #define LPSS_DEV_SIZE 0x200
  32. #define LPSS_PRIV_OFFSET 0x200
  33. #define LPSS_PRIV_SIZE 0x100
  34. #define LPSS_PRIV_REG_COUNT (LPSS_PRIV_SIZE / 4)
  35. #define LPSS_IDMA64_OFFSET 0x800
  36. #define LPSS_IDMA64_SIZE 0x800
  37. /* Offsets from lpss->priv */
  38. #define LPSS_PRIV_RESETS 0x04
  39. #define LPSS_PRIV_RESETS_IDMA BIT(2)
  40. #define LPSS_PRIV_RESETS_FUNC 0x3
  41. #define LPSS_PRIV_ACTIVELTR 0x10
  42. #define LPSS_PRIV_IDLELTR 0x14
  43. #define LPSS_PRIV_LTR_REQ BIT(15)
  44. #define LPSS_PRIV_LTR_SCALE_MASK 0xc00
  45. #define LPSS_PRIV_LTR_SCALE_1US 0x800
  46. #define LPSS_PRIV_LTR_SCALE_32US 0xc00
  47. #define LPSS_PRIV_LTR_VALUE_MASK 0x3ff
  48. #define LPSS_PRIV_SSP_REG 0x20
  49. #define LPSS_PRIV_SSP_REG_DIS_DMA_FIN BIT(0)
  50. #define LPSS_PRIV_REMAP_ADDR 0x40
  51. #define LPSS_PRIV_CAPS 0xfc
  52. #define LPSS_PRIV_CAPS_NO_IDMA BIT(8)
  53. #define LPSS_PRIV_CAPS_TYPE_SHIFT 4
  54. #define LPSS_PRIV_CAPS_TYPE_MASK (0xf << LPSS_PRIV_CAPS_TYPE_SHIFT)
  55. /* This matches the type field in CAPS register */
  56. enum intel_lpss_dev_type {
  57. LPSS_DEV_I2C = 0,
  58. LPSS_DEV_UART,
  59. LPSS_DEV_SPI,
  60. };
  61. struct intel_lpss {
  62. const struct intel_lpss_platform_info *info;
  63. enum intel_lpss_dev_type type;
  64. struct clk *clk;
  65. struct clk_lookup *clock;
  66. struct mfd_cell *cell;
  67. struct device *dev;
  68. void __iomem *priv;
  69. u32 priv_ctx[LPSS_PRIV_REG_COUNT];
  70. int devid;
  71. u32 caps;
  72. u32 active_ltr;
  73. u32 idle_ltr;
  74. struct dentry *debugfs;
  75. };
  76. static const struct resource intel_lpss_dev_resources[] = {
  77. DEFINE_RES_MEM_NAMED(LPSS_DEV_OFFSET, LPSS_DEV_SIZE, "lpss_dev"),
  78. DEFINE_RES_MEM_NAMED(LPSS_PRIV_OFFSET, LPSS_PRIV_SIZE, "lpss_priv"),
  79. DEFINE_RES_IRQ(0),
  80. };
  81. static const struct resource intel_lpss_idma64_resources[] = {
  82. DEFINE_RES_MEM(LPSS_IDMA64_OFFSET, LPSS_IDMA64_SIZE),
  83. DEFINE_RES_IRQ(0),
  84. };
  85. #define LPSS_IDMA64_DRIVER_NAME "idma64"
  86. /*
  87. * Cells needs to be ordered so that the iDMA is created first. This is
  88. * because we need to be sure the DMA is available when the host controller
  89. * driver is probed.
  90. */
  91. static const struct mfd_cell intel_lpss_idma64_cell = {
  92. .name = LPSS_IDMA64_DRIVER_NAME,
  93. .num_resources = ARRAY_SIZE(intel_lpss_idma64_resources),
  94. .resources = intel_lpss_idma64_resources,
  95. };
  96. static const struct mfd_cell intel_lpss_i2c_cell = {
  97. .name = "i2c_designware",
  98. .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
  99. .resources = intel_lpss_dev_resources,
  100. };
  101. static const struct mfd_cell intel_lpss_uart_cell = {
  102. .name = "dw-apb-uart",
  103. .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
  104. .resources = intel_lpss_dev_resources,
  105. };
  106. static const struct mfd_cell intel_lpss_spi_cell = {
  107. .name = "pxa2xx-spi",
  108. .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
  109. .resources = intel_lpss_dev_resources,
  110. };
  111. static DEFINE_IDA(intel_lpss_devid_ida);
  112. static struct dentry *intel_lpss_debugfs;
  113. static int intel_lpss_request_dma_module(const char *name)
  114. {
  115. static bool intel_lpss_dma_requested;
  116. if (intel_lpss_dma_requested)
  117. return 0;
  118. intel_lpss_dma_requested = true;
  119. return request_module("%s", name);
  120. }
  121. static void intel_lpss_cache_ltr(struct intel_lpss *lpss)
  122. {
  123. lpss->active_ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
  124. lpss->idle_ltr = readl(lpss->priv + LPSS_PRIV_IDLELTR);
  125. }
  126. static int intel_lpss_debugfs_add(struct intel_lpss *lpss)
  127. {
  128. struct dentry *dir;
  129. dir = debugfs_create_dir(dev_name(lpss->dev), intel_lpss_debugfs);
  130. if (IS_ERR(dir))
  131. return PTR_ERR(dir);
  132. /* Cache the values into lpss structure */
  133. intel_lpss_cache_ltr(lpss);
  134. debugfs_create_x32("capabilities", S_IRUGO, dir, &lpss->caps);
  135. debugfs_create_x32("active_ltr", S_IRUGO, dir, &lpss->active_ltr);
  136. debugfs_create_x32("idle_ltr", S_IRUGO, dir, &lpss->idle_ltr);
  137. lpss->debugfs = dir;
  138. return 0;
  139. }
  140. static void intel_lpss_debugfs_remove(struct intel_lpss *lpss)
  141. {
  142. debugfs_remove_recursive(lpss->debugfs);
  143. }
  144. static void intel_lpss_ltr_set(struct device *dev, s32 val)
  145. {
  146. struct intel_lpss *lpss = dev_get_drvdata(dev);
  147. u32 ltr;
  148. /*
  149. * Program latency tolerance (LTR) accordingly what has been asked
  150. * by the PM QoS layer or disable it in case we were passed
  151. * negative value or PM_QOS_LATENCY_ANY.
  152. */
  153. ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
  154. if (val == PM_QOS_LATENCY_ANY || val < 0) {
  155. ltr &= ~LPSS_PRIV_LTR_REQ;
  156. } else {
  157. ltr |= LPSS_PRIV_LTR_REQ;
  158. ltr &= ~LPSS_PRIV_LTR_SCALE_MASK;
  159. ltr &= ~LPSS_PRIV_LTR_VALUE_MASK;
  160. if (val > LPSS_PRIV_LTR_VALUE_MASK)
  161. ltr |= LPSS_PRIV_LTR_SCALE_32US | val >> 5;
  162. else
  163. ltr |= LPSS_PRIV_LTR_SCALE_1US | val;
  164. }
  165. if (ltr == lpss->active_ltr)
  166. return;
  167. writel(ltr, lpss->priv + LPSS_PRIV_ACTIVELTR);
  168. writel(ltr, lpss->priv + LPSS_PRIV_IDLELTR);
  169. /* Cache the values into lpss structure */
  170. intel_lpss_cache_ltr(lpss);
  171. }
  172. static void intel_lpss_ltr_expose(struct intel_lpss *lpss)
  173. {
  174. lpss->dev->power.set_latency_tolerance = intel_lpss_ltr_set;
  175. dev_pm_qos_expose_latency_tolerance(lpss->dev);
  176. }
  177. static void intel_lpss_ltr_hide(struct intel_lpss *lpss)
  178. {
  179. dev_pm_qos_hide_latency_tolerance(lpss->dev);
  180. lpss->dev->power.set_latency_tolerance = NULL;
  181. }
  182. static int intel_lpss_assign_devs(struct intel_lpss *lpss)
  183. {
  184. const struct mfd_cell *cell;
  185. unsigned int type;
  186. type = lpss->caps & LPSS_PRIV_CAPS_TYPE_MASK;
  187. type >>= LPSS_PRIV_CAPS_TYPE_SHIFT;
  188. switch (type) {
  189. case LPSS_DEV_I2C:
  190. cell = &intel_lpss_i2c_cell;
  191. break;
  192. case LPSS_DEV_UART:
  193. cell = &intel_lpss_uart_cell;
  194. break;
  195. case LPSS_DEV_SPI:
  196. cell = &intel_lpss_spi_cell;
  197. break;
  198. default:
  199. return -ENODEV;
  200. }
  201. lpss->cell = devm_kmemdup(lpss->dev, cell, sizeof(*cell), GFP_KERNEL);
  202. if (!lpss->cell)
  203. return -ENOMEM;
  204. lpss->type = type;
  205. return 0;
  206. }
  207. static bool intel_lpss_has_idma(const struct intel_lpss *lpss)
  208. {
  209. return (lpss->caps & LPSS_PRIV_CAPS_NO_IDMA) == 0;
  210. }
  211. static void intel_lpss_set_remap_addr(const struct intel_lpss *lpss)
  212. {
  213. resource_size_t addr = lpss->info->mem->start;
  214. lo_hi_writeq(addr, lpss->priv + LPSS_PRIV_REMAP_ADDR);
  215. }
  216. static void intel_lpss_deassert_reset(const struct intel_lpss *lpss)
  217. {
  218. u32 value = LPSS_PRIV_RESETS_FUNC | LPSS_PRIV_RESETS_IDMA;
  219. /* Bring out the device from reset */
  220. writel(value, lpss->priv + LPSS_PRIV_RESETS);
  221. }
  222. static void intel_lpss_init_dev(const struct intel_lpss *lpss)
  223. {
  224. u32 value = LPSS_PRIV_SSP_REG_DIS_DMA_FIN;
  225. /* Set the device in reset state */
  226. writel(0, lpss->priv + LPSS_PRIV_RESETS);
  227. intel_lpss_deassert_reset(lpss);
  228. intel_lpss_set_remap_addr(lpss);
  229. if (!intel_lpss_has_idma(lpss))
  230. return;
  231. /* Make sure that SPI multiblock DMA transfers are re-enabled */
  232. if (lpss->type == LPSS_DEV_SPI)
  233. writel(value, lpss->priv + LPSS_PRIV_SSP_REG);
  234. }
  235. static void intel_lpss_unregister_clock_tree(struct clk *clk)
  236. {
  237. struct clk *parent;
  238. while (clk) {
  239. parent = clk_get_parent(clk);
  240. clk_unregister(clk);
  241. clk = parent;
  242. }
  243. }
  244. static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
  245. const char *devname,
  246. struct clk **clk)
  247. {
  248. char name[32];
  249. struct clk *tmp = *clk;
  250. snprintf(name, sizeof(name), "%s-enable", devname);
  251. tmp = clk_register_gate(NULL, name, __clk_get_name(tmp), 0,
  252. lpss->priv, 0, 0, NULL);
  253. if (IS_ERR(tmp))
  254. return PTR_ERR(tmp);
  255. snprintf(name, sizeof(name), "%s-div", devname);
  256. tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
  257. 0, lpss->priv, 1, 15, 16, 15, 0,
  258. NULL);
  259. if (IS_ERR(tmp))
  260. return PTR_ERR(tmp);
  261. *clk = tmp;
  262. snprintf(name, sizeof(name), "%s-update", devname);
  263. tmp = clk_register_gate(NULL, name, __clk_get_name(tmp),
  264. CLK_SET_RATE_PARENT, lpss->priv, 31, 0, NULL);
  265. if (IS_ERR(tmp))
  266. return PTR_ERR(tmp);
  267. *clk = tmp;
  268. return 0;
  269. }
  270. static int intel_lpss_register_clock(struct intel_lpss *lpss)
  271. {
  272. const struct mfd_cell *cell = lpss->cell;
  273. struct clk *clk;
  274. char devname[24];
  275. int ret;
  276. if (!lpss->info->clk_rate)
  277. return 0;
  278. /* Root clock */
  279. clk = clk_register_fixed_rate(NULL, dev_name(lpss->dev), NULL, 0,
  280. lpss->info->clk_rate);
  281. if (IS_ERR(clk))
  282. return PTR_ERR(clk);
  283. snprintf(devname, sizeof(devname), "%s.%d", cell->name, lpss->devid);
  284. /*
  285. * Support for clock divider only if it has some preset value.
  286. * Otherwise we assume that the divider is not used.
  287. */
  288. if (lpss->type != LPSS_DEV_I2C) {
  289. ret = intel_lpss_register_clock_divider(lpss, devname, &clk);
  290. if (ret)
  291. goto err_clk_register;
  292. }
  293. ret = -ENOMEM;
  294. /* Clock for the host controller */
  295. lpss->clock = clkdev_create(clk, lpss->info->clk_con_id, "%s", devname);
  296. if (!lpss->clock)
  297. goto err_clk_register;
  298. lpss->clk = clk;
  299. return 0;
  300. err_clk_register:
  301. intel_lpss_unregister_clock_tree(clk);
  302. return ret;
  303. }
  304. static void intel_lpss_unregister_clock(struct intel_lpss *lpss)
  305. {
  306. if (IS_ERR_OR_NULL(lpss->clk))
  307. return;
  308. clkdev_drop(lpss->clock);
  309. intel_lpss_unregister_clock_tree(lpss->clk);
  310. }
  311. int intel_lpss_probe(struct device *dev,
  312. const struct intel_lpss_platform_info *info)
  313. {
  314. struct intel_lpss *lpss;
  315. int ret;
  316. if (!info || !info->mem || info->irq <= 0)
  317. return -EINVAL;
  318. lpss = devm_kzalloc(dev, sizeof(*lpss), GFP_KERNEL);
  319. if (!lpss)
  320. return -ENOMEM;
  321. lpss->priv = devm_ioremap(dev, info->mem->start + LPSS_PRIV_OFFSET,
  322. LPSS_PRIV_SIZE);
  323. if (!lpss->priv)
  324. return -ENOMEM;
  325. lpss->info = info;
  326. lpss->dev = dev;
  327. lpss->caps = readl(lpss->priv + LPSS_PRIV_CAPS);
  328. dev_set_drvdata(dev, lpss);
  329. ret = intel_lpss_assign_devs(lpss);
  330. if (ret)
  331. return ret;
  332. lpss->cell->properties = info->properties;
  333. intel_lpss_init_dev(lpss);
  334. lpss->devid = ida_simple_get(&intel_lpss_devid_ida, 0, 0, GFP_KERNEL);
  335. if (lpss->devid < 0)
  336. return lpss->devid;
  337. ret = intel_lpss_register_clock(lpss);
  338. if (ret)
  339. goto err_clk_register;
  340. intel_lpss_ltr_expose(lpss);
  341. ret = intel_lpss_debugfs_add(lpss);
  342. if (ret)
  343. dev_warn(dev, "Failed to create debugfs entries\n");
  344. if (intel_lpss_has_idma(lpss)) {
  345. /*
  346. * Ensure the DMA driver is loaded before the host
  347. * controller device appears, so that the host controller
  348. * driver can request its DMA channels as early as
  349. * possible.
  350. *
  351. * If the DMA module is not there that's OK as well.
  352. */
  353. intel_lpss_request_dma_module(LPSS_IDMA64_DRIVER_NAME);
  354. ret = mfd_add_devices(dev, lpss->devid, &intel_lpss_idma64_cell,
  355. 1, info->mem, info->irq, NULL);
  356. if (ret)
  357. dev_warn(dev, "Failed to add %s, fallback to PIO\n",
  358. LPSS_IDMA64_DRIVER_NAME);
  359. }
  360. ret = mfd_add_devices(dev, lpss->devid, lpss->cell,
  361. 1, info->mem, info->irq, NULL);
  362. if (ret)
  363. goto err_remove_ltr;
  364. dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
  365. return 0;
  366. err_remove_ltr:
  367. intel_lpss_debugfs_remove(lpss);
  368. intel_lpss_ltr_hide(lpss);
  369. intel_lpss_unregister_clock(lpss);
  370. err_clk_register:
  371. ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL_GPL(intel_lpss_probe);
  375. void intel_lpss_remove(struct device *dev)
  376. {
  377. struct intel_lpss *lpss = dev_get_drvdata(dev);
  378. mfd_remove_devices(dev);
  379. intel_lpss_debugfs_remove(lpss);
  380. intel_lpss_ltr_hide(lpss);
  381. intel_lpss_unregister_clock(lpss);
  382. ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
  383. }
  384. EXPORT_SYMBOL_GPL(intel_lpss_remove);
  385. static int resume_lpss_device(struct device *dev, void *data)
  386. {
  387. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND))
  388. pm_runtime_resume(dev);
  389. return 0;
  390. }
  391. int intel_lpss_prepare(struct device *dev)
  392. {
  393. /*
  394. * Resume both child devices before entering system sleep. This
  395. * ensures that they are in proper state before they get suspended.
  396. */
  397. device_for_each_child_reverse(dev, NULL, resume_lpss_device);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(intel_lpss_prepare);
  401. int intel_lpss_suspend(struct device *dev)
  402. {
  403. struct intel_lpss *lpss = dev_get_drvdata(dev);
  404. unsigned int i;
  405. /* Save device context */
  406. for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
  407. lpss->priv_ctx[i] = readl(lpss->priv + i * 4);
  408. /*
  409. * If the device type is not UART, then put the controller into
  410. * reset. UART cannot be put into reset since S3/S0ix fail when
  411. * no_console_suspend flag is enabled.
  412. */
  413. if (lpss->type != LPSS_DEV_UART)
  414. writel(0, lpss->priv + LPSS_PRIV_RESETS);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(intel_lpss_suspend);
  418. int intel_lpss_resume(struct device *dev)
  419. {
  420. struct intel_lpss *lpss = dev_get_drvdata(dev);
  421. unsigned int i;
  422. intel_lpss_deassert_reset(lpss);
  423. /* Restore device context */
  424. for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
  425. writel(lpss->priv_ctx[i], lpss->priv + i * 4);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(intel_lpss_resume);
  429. static int __init intel_lpss_init(void)
  430. {
  431. intel_lpss_debugfs = debugfs_create_dir("intel_lpss", NULL);
  432. return 0;
  433. }
  434. module_init(intel_lpss_init);
  435. static void __exit intel_lpss_exit(void)
  436. {
  437. ida_destroy(&intel_lpss_devid_ida);
  438. debugfs_remove(intel_lpss_debugfs);
  439. }
  440. module_exit(intel_lpss_exit);
  441. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  442. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  443. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  444. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
  445. MODULE_DESCRIPTION("Intel LPSS core driver");
  446. MODULE_LICENSE("GPL v2");