intel-lpss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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_FUNC BIT(2)
  40. #define LPSS_PRIV_RESETS_IDMA 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. intel_lpss_deassert_reset(lpss);
  226. intel_lpss_set_remap_addr(lpss);
  227. if (!intel_lpss_has_idma(lpss))
  228. return;
  229. /* Make sure that SPI multiblock DMA transfers are re-enabled */
  230. if (lpss->type == LPSS_DEV_SPI)
  231. writel(value, lpss->priv + LPSS_PRIV_SSP_REG);
  232. }
  233. static void intel_lpss_unregister_clock_tree(struct clk *clk)
  234. {
  235. struct clk *parent;
  236. while (clk) {
  237. parent = clk_get_parent(clk);
  238. clk_unregister(clk);
  239. clk = parent;
  240. }
  241. }
  242. static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
  243. const char *devname,
  244. struct clk **clk)
  245. {
  246. char name[32];
  247. struct clk *tmp = *clk;
  248. snprintf(name, sizeof(name), "%s-enable", devname);
  249. tmp = clk_register_gate(NULL, name, __clk_get_name(tmp), 0,
  250. lpss->priv, 0, 0, NULL);
  251. if (IS_ERR(tmp))
  252. return PTR_ERR(tmp);
  253. snprintf(name, sizeof(name), "%s-div", devname);
  254. tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
  255. 0, lpss->priv, 1, 15, 16, 15, 0,
  256. NULL);
  257. if (IS_ERR(tmp))
  258. return PTR_ERR(tmp);
  259. *clk = tmp;
  260. snprintf(name, sizeof(name), "%s-update", devname);
  261. tmp = clk_register_gate(NULL, name, __clk_get_name(tmp),
  262. CLK_SET_RATE_PARENT, lpss->priv, 31, 0, NULL);
  263. if (IS_ERR(tmp))
  264. return PTR_ERR(tmp);
  265. *clk = tmp;
  266. return 0;
  267. }
  268. static int intel_lpss_register_clock(struct intel_lpss *lpss)
  269. {
  270. const struct mfd_cell *cell = lpss->cell;
  271. struct clk *clk;
  272. char devname[24];
  273. int ret;
  274. if (!lpss->info->clk_rate)
  275. return 0;
  276. /* Root clock */
  277. clk = clk_register_fixed_rate(NULL, dev_name(lpss->dev), NULL, 0,
  278. lpss->info->clk_rate);
  279. if (IS_ERR(clk))
  280. return PTR_ERR(clk);
  281. snprintf(devname, sizeof(devname), "%s.%d", cell->name, lpss->devid);
  282. /*
  283. * Support for clock divider only if it has some preset value.
  284. * Otherwise we assume that the divider is not used.
  285. */
  286. if (lpss->type != LPSS_DEV_I2C) {
  287. ret = intel_lpss_register_clock_divider(lpss, devname, &clk);
  288. if (ret)
  289. goto err_clk_register;
  290. }
  291. ret = -ENOMEM;
  292. /* Clock for the host controller */
  293. lpss->clock = clkdev_create(clk, lpss->info->clk_con_id, "%s", devname);
  294. if (!lpss->clock)
  295. goto err_clk_register;
  296. lpss->clk = clk;
  297. return 0;
  298. err_clk_register:
  299. intel_lpss_unregister_clock_tree(clk);
  300. return ret;
  301. }
  302. static void intel_lpss_unregister_clock(struct intel_lpss *lpss)
  303. {
  304. if (IS_ERR_OR_NULL(lpss->clk))
  305. return;
  306. clkdev_drop(lpss->clock);
  307. intel_lpss_unregister_clock_tree(lpss->clk);
  308. }
  309. int intel_lpss_probe(struct device *dev,
  310. const struct intel_lpss_platform_info *info)
  311. {
  312. struct intel_lpss *lpss;
  313. int ret;
  314. if (!info || !info->mem || info->irq <= 0)
  315. return -EINVAL;
  316. lpss = devm_kzalloc(dev, sizeof(*lpss), GFP_KERNEL);
  317. if (!lpss)
  318. return -ENOMEM;
  319. lpss->priv = devm_ioremap(dev, info->mem->start + LPSS_PRIV_OFFSET,
  320. LPSS_PRIV_SIZE);
  321. if (!lpss->priv)
  322. return -ENOMEM;
  323. lpss->info = info;
  324. lpss->dev = dev;
  325. lpss->caps = readl(lpss->priv + LPSS_PRIV_CAPS);
  326. dev_set_drvdata(dev, lpss);
  327. ret = intel_lpss_assign_devs(lpss);
  328. if (ret)
  329. return ret;
  330. lpss->cell->properties = info->properties;
  331. intel_lpss_init_dev(lpss);
  332. lpss->devid = ida_simple_get(&intel_lpss_devid_ida, 0, 0, GFP_KERNEL);
  333. if (lpss->devid < 0)
  334. return lpss->devid;
  335. ret = intel_lpss_register_clock(lpss);
  336. if (ret)
  337. goto err_clk_register;
  338. intel_lpss_ltr_expose(lpss);
  339. ret = intel_lpss_debugfs_add(lpss);
  340. if (ret)
  341. dev_warn(dev, "Failed to create debugfs entries\n");
  342. if (intel_lpss_has_idma(lpss)) {
  343. /*
  344. * Ensure the DMA driver is loaded before the host
  345. * controller device appears, so that the host controller
  346. * driver can request its DMA channels as early as
  347. * possible.
  348. *
  349. * If the DMA module is not there that's OK as well.
  350. */
  351. intel_lpss_request_dma_module(LPSS_IDMA64_DRIVER_NAME);
  352. ret = mfd_add_devices(dev, lpss->devid, &intel_lpss_idma64_cell,
  353. 1, info->mem, info->irq, NULL);
  354. if (ret)
  355. dev_warn(dev, "Failed to add %s, fallback to PIO\n",
  356. LPSS_IDMA64_DRIVER_NAME);
  357. }
  358. ret = mfd_add_devices(dev, lpss->devid, lpss->cell,
  359. 1, info->mem, info->irq, NULL);
  360. if (ret)
  361. goto err_remove_ltr;
  362. return 0;
  363. err_remove_ltr:
  364. intel_lpss_debugfs_remove(lpss);
  365. intel_lpss_ltr_hide(lpss);
  366. intel_lpss_unregister_clock(lpss);
  367. err_clk_register:
  368. ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
  369. return ret;
  370. }
  371. EXPORT_SYMBOL_GPL(intel_lpss_probe);
  372. void intel_lpss_remove(struct device *dev)
  373. {
  374. struct intel_lpss *lpss = dev_get_drvdata(dev);
  375. mfd_remove_devices(dev);
  376. intel_lpss_debugfs_remove(lpss);
  377. intel_lpss_ltr_hide(lpss);
  378. intel_lpss_unregister_clock(lpss);
  379. ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
  380. }
  381. EXPORT_SYMBOL_GPL(intel_lpss_remove);
  382. static int resume_lpss_device(struct device *dev, void *data)
  383. {
  384. pm_runtime_resume(dev);
  385. return 0;
  386. }
  387. int intel_lpss_prepare(struct device *dev)
  388. {
  389. /*
  390. * Resume both child devices before entering system sleep. This
  391. * ensures that they are in proper state before they get suspended.
  392. */
  393. device_for_each_child_reverse(dev, NULL, resume_lpss_device);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL_GPL(intel_lpss_prepare);
  397. int intel_lpss_suspend(struct device *dev)
  398. {
  399. struct intel_lpss *lpss = dev_get_drvdata(dev);
  400. unsigned int i;
  401. /* Save device context */
  402. for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
  403. lpss->priv_ctx[i] = readl(lpss->priv + i * 4);
  404. return 0;
  405. }
  406. EXPORT_SYMBOL_GPL(intel_lpss_suspend);
  407. int intel_lpss_resume(struct device *dev)
  408. {
  409. struct intel_lpss *lpss = dev_get_drvdata(dev);
  410. unsigned int i;
  411. intel_lpss_deassert_reset(lpss);
  412. /* Restore device context */
  413. for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
  414. writel(lpss->priv_ctx[i], lpss->priv + i * 4);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(intel_lpss_resume);
  418. static int __init intel_lpss_init(void)
  419. {
  420. intel_lpss_debugfs = debugfs_create_dir("intel_lpss", NULL);
  421. return 0;
  422. }
  423. module_init(intel_lpss_init);
  424. static void __exit intel_lpss_exit(void)
  425. {
  426. debugfs_remove(intel_lpss_debugfs);
  427. }
  428. module_exit(intel_lpss_exit);
  429. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  430. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  431. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  432. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
  433. MODULE_DESCRIPTION("Intel LPSS core driver");
  434. MODULE_LICENSE("GPL v2");