pwr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Intel MID Power Management Unit (PWRMU) device driver
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. *
  6. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * Intel MID Power Management Unit device driver handles the South Complex PCI
  13. * devices such as GPDMA, SPI, I2C, PWM, and so on. By default PCI core
  14. * modifies bits in PMCSR register in the PCI configuration space. This is not
  15. * enough on some SoCs like Intel Tangier. In such case PCI core sets a new
  16. * power state of the device in question through a PM hook registered in struct
  17. * pci_platform_pm_ops (see drivers/pci/pci-mid.c).
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/export.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pci.h>
  27. #include <asm/intel-mid.h>
  28. /* Registers */
  29. #define PM_STS 0x00
  30. #define PM_CMD 0x04
  31. #define PM_ICS 0x08
  32. #define PM_WKC(x) (0x10 + (x) * 4)
  33. #define PM_WKS(x) (0x18 + (x) * 4)
  34. #define PM_SSC(x) (0x20 + (x) * 4)
  35. #define PM_SSS(x) (0x30 + (x) * 4)
  36. /* Bits in PM_STS */
  37. #define PM_STS_BUSY (1 << 8)
  38. /* Bits in PM_CMD */
  39. #define PM_CMD_CMD(x) ((x) << 0)
  40. #define PM_CMD_IOC (1 << 8)
  41. #define PM_CMD_CM_NOP (0 << 9)
  42. #define PM_CMD_CM_IMMEDIATE (1 << 9)
  43. #define PM_CMD_CM_DELAY (2 << 9)
  44. #define PM_CMD_CM_TRIGGER (3 << 9)
  45. /* System states */
  46. #define PM_CMD_SYS_STATE_S5 (5 << 16)
  47. /* Trigger variants */
  48. #define PM_CMD_CFG_TRIGGER_NC (3 << 19)
  49. /* Message to wait for TRIGGER_NC case */
  50. #define TRIGGER_NC_MSG_2 (2 << 22)
  51. /* List of commands */
  52. #define CMD_SET_CFG 0x01
  53. /* Bits in PM_ICS */
  54. #define PM_ICS_INT_STATUS(x) ((x) & 0xff)
  55. #define PM_ICS_IE (1 << 8)
  56. #define PM_ICS_IP (1 << 9)
  57. #define PM_ICS_SW_INT_STS (1 << 10)
  58. /* List of interrupts */
  59. #define INT_INVALID 0
  60. #define INT_CMD_COMPLETE 1
  61. #define INT_CMD_ERR 2
  62. #define INT_WAKE_EVENT 3
  63. #define INT_LSS_POWER_ERR 4
  64. #define INT_S0iX_MSG_ERR 5
  65. #define INT_NO_C6 6
  66. #define INT_TRIGGER_ERR 7
  67. #define INT_INACTIVITY 8
  68. /* South Complex devices */
  69. #define LSS_MAX_SHARED_DEVS 4
  70. #define LSS_MAX_DEVS 64
  71. #define LSS_WS_BITS 1 /* wake state width */
  72. #define LSS_PWS_BITS 2 /* power state width */
  73. /* Supported device IDs */
  74. #define PCI_DEVICE_ID_PENWELL 0x0828
  75. #define PCI_DEVICE_ID_TANGIER 0x11a1
  76. struct mid_pwr_dev {
  77. struct pci_dev *pdev;
  78. pci_power_t state;
  79. };
  80. struct mid_pwr {
  81. struct device *dev;
  82. void __iomem *regs;
  83. int irq;
  84. bool available;
  85. struct mutex lock;
  86. struct mid_pwr_dev lss[LSS_MAX_DEVS][LSS_MAX_SHARED_DEVS];
  87. };
  88. static struct mid_pwr *midpwr;
  89. static u32 mid_pwr_get_state(struct mid_pwr *pwr, int reg)
  90. {
  91. return readl(pwr->regs + PM_SSS(reg));
  92. }
  93. static void mid_pwr_set_state(struct mid_pwr *pwr, int reg, u32 value)
  94. {
  95. writel(value, pwr->regs + PM_SSC(reg));
  96. }
  97. static void mid_pwr_set_wake(struct mid_pwr *pwr, int reg, u32 value)
  98. {
  99. writel(value, pwr->regs + PM_WKC(reg));
  100. }
  101. static void mid_pwr_interrupt_disable(struct mid_pwr *pwr)
  102. {
  103. writel(~PM_ICS_IE, pwr->regs + PM_ICS);
  104. }
  105. static bool mid_pwr_is_busy(struct mid_pwr *pwr)
  106. {
  107. return !!(readl(pwr->regs + PM_STS) & PM_STS_BUSY);
  108. }
  109. /* Wait 500ms that the latest PWRMU command finished */
  110. static int mid_pwr_wait(struct mid_pwr *pwr)
  111. {
  112. unsigned int count = 500000;
  113. bool busy;
  114. do {
  115. busy = mid_pwr_is_busy(pwr);
  116. if (!busy)
  117. return 0;
  118. udelay(1);
  119. } while (--count);
  120. return -EBUSY;
  121. }
  122. static int mid_pwr_wait_for_cmd(struct mid_pwr *pwr, u8 cmd)
  123. {
  124. writel(PM_CMD_CMD(cmd) | PM_CMD_CM_IMMEDIATE, pwr->regs + PM_CMD);
  125. return mid_pwr_wait(pwr);
  126. }
  127. static int __update_power_state(struct mid_pwr *pwr, int reg, int bit, int new)
  128. {
  129. int curstate;
  130. u32 power;
  131. int ret;
  132. /* Check if the device is already in desired state */
  133. power = mid_pwr_get_state(pwr, reg);
  134. curstate = (power >> bit) & 3;
  135. if (curstate == new)
  136. return 0;
  137. /* Update the power state */
  138. mid_pwr_set_state(pwr, reg, (power & ~(3 << bit)) | (new << bit));
  139. /* Send command to SCU */
  140. ret = mid_pwr_wait_for_cmd(pwr, CMD_SET_CFG);
  141. if (ret)
  142. return ret;
  143. /* Check if the device is already in desired state */
  144. power = mid_pwr_get_state(pwr, reg);
  145. curstate = (power >> bit) & 3;
  146. if (curstate != new)
  147. return -EAGAIN;
  148. return 0;
  149. }
  150. static pci_power_t __find_weakest_power_state(struct mid_pwr_dev *lss,
  151. struct pci_dev *pdev,
  152. pci_power_t state)
  153. {
  154. pci_power_t weakest = PCI_D3hot;
  155. unsigned int j;
  156. /* Find device in cache or first free cell */
  157. for (j = 0; j < LSS_MAX_SHARED_DEVS; j++) {
  158. if (lss[j].pdev == pdev || !lss[j].pdev)
  159. break;
  160. }
  161. /* Store the desired state in cache */
  162. if (j < LSS_MAX_SHARED_DEVS) {
  163. lss[j].pdev = pdev;
  164. lss[j].state = state;
  165. } else {
  166. dev_WARN(&pdev->dev, "No room for device in PWRMU LSS cache\n");
  167. weakest = state;
  168. }
  169. /* Find the power state we may use */
  170. for (j = 0; j < LSS_MAX_SHARED_DEVS; j++) {
  171. if (lss[j].state < weakest)
  172. weakest = lss[j].state;
  173. }
  174. return weakest;
  175. }
  176. static int __set_power_state(struct mid_pwr *pwr, struct pci_dev *pdev,
  177. pci_power_t state, int id, int reg, int bit)
  178. {
  179. const char *name;
  180. int ret;
  181. state = __find_weakest_power_state(pwr->lss[id], pdev, state);
  182. name = pci_power_name(state);
  183. ret = __update_power_state(pwr, reg, bit, (__force int)state);
  184. if (ret) {
  185. dev_warn(&pdev->dev, "Can't set power state %s: %d\n", name, ret);
  186. return ret;
  187. }
  188. dev_vdbg(&pdev->dev, "Set power state %s\n", name);
  189. return 0;
  190. }
  191. static int mid_pwr_set_power_state(struct mid_pwr *pwr, struct pci_dev *pdev,
  192. pci_power_t state)
  193. {
  194. int id, reg, bit;
  195. int ret;
  196. id = intel_mid_pwr_get_lss_id(pdev);
  197. if (id < 0)
  198. return id;
  199. reg = (id * LSS_PWS_BITS) / 32;
  200. bit = (id * LSS_PWS_BITS) % 32;
  201. /* We support states between PCI_D0 and PCI_D3hot */
  202. if (state < PCI_D0)
  203. state = PCI_D0;
  204. if (state > PCI_D3hot)
  205. state = PCI_D3hot;
  206. mutex_lock(&pwr->lock);
  207. ret = __set_power_state(pwr, pdev, state, id, reg, bit);
  208. mutex_unlock(&pwr->lock);
  209. return ret;
  210. }
  211. int intel_mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
  212. {
  213. struct mid_pwr *pwr = midpwr;
  214. int ret = 0;
  215. might_sleep();
  216. if (pwr && pwr->available)
  217. ret = mid_pwr_set_power_state(pwr, pdev, state);
  218. dev_vdbg(&pdev->dev, "set_power_state() returns %d\n", ret);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(intel_mid_pci_set_power_state);
  222. pci_power_t intel_mid_pci_get_power_state(struct pci_dev *pdev)
  223. {
  224. struct mid_pwr *pwr = midpwr;
  225. int id, reg, bit;
  226. u32 power;
  227. if (!pwr || !pwr->available)
  228. return PCI_UNKNOWN;
  229. id = intel_mid_pwr_get_lss_id(pdev);
  230. if (id < 0)
  231. return PCI_UNKNOWN;
  232. reg = (id * LSS_PWS_BITS) / 32;
  233. bit = (id * LSS_PWS_BITS) % 32;
  234. power = mid_pwr_get_state(pwr, reg);
  235. return (__force pci_power_t)((power >> bit) & 3);
  236. }
  237. void intel_mid_pwr_power_off(void)
  238. {
  239. struct mid_pwr *pwr = midpwr;
  240. u32 cmd = PM_CMD_SYS_STATE_S5 |
  241. PM_CMD_CMD(CMD_SET_CFG) |
  242. PM_CMD_CM_TRIGGER |
  243. PM_CMD_CFG_TRIGGER_NC |
  244. TRIGGER_NC_MSG_2;
  245. /* Send command to SCU */
  246. writel(cmd, pwr->regs + PM_CMD);
  247. mid_pwr_wait(pwr);
  248. }
  249. int intel_mid_pwr_get_lss_id(struct pci_dev *pdev)
  250. {
  251. int vndr;
  252. u8 id;
  253. /*
  254. * Mapping to PWRMU index is kept in the Logical SubSystem ID byte of
  255. * Vendor capability.
  256. */
  257. vndr = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
  258. if (!vndr)
  259. return -EINVAL;
  260. /* Read the Logical SubSystem ID byte */
  261. pci_read_config_byte(pdev, vndr + INTEL_MID_PWR_LSS_OFFSET, &id);
  262. if (!(id & INTEL_MID_PWR_LSS_TYPE))
  263. return -ENODEV;
  264. id &= ~INTEL_MID_PWR_LSS_TYPE;
  265. if (id >= LSS_MAX_DEVS)
  266. return -ERANGE;
  267. return id;
  268. }
  269. static irqreturn_t mid_pwr_irq_handler(int irq, void *dev_id)
  270. {
  271. struct mid_pwr *pwr = dev_id;
  272. u32 ics;
  273. ics = readl(pwr->regs + PM_ICS);
  274. if (!(ics & PM_ICS_IP))
  275. return IRQ_NONE;
  276. writel(ics | PM_ICS_IP, pwr->regs + PM_ICS);
  277. dev_warn(pwr->dev, "Unexpected IRQ: %#x\n", PM_ICS_INT_STATUS(ics));
  278. return IRQ_HANDLED;
  279. }
  280. struct mid_pwr_device_info {
  281. int (*set_initial_state)(struct mid_pwr *pwr);
  282. };
  283. static int mid_pwr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  284. {
  285. struct mid_pwr_device_info *info = (void *)id->driver_data;
  286. struct device *dev = &pdev->dev;
  287. struct mid_pwr *pwr;
  288. int ret;
  289. ret = pcim_enable_device(pdev);
  290. if (ret < 0) {
  291. dev_err(&pdev->dev, "error: could not enable device\n");
  292. return ret;
  293. }
  294. ret = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
  295. if (ret) {
  296. dev_err(&pdev->dev, "I/O memory remapping failed\n");
  297. return ret;
  298. }
  299. pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
  300. if (!pwr)
  301. return -ENOMEM;
  302. pwr->dev = dev;
  303. pwr->regs = pcim_iomap_table(pdev)[0];
  304. pwr->irq = pdev->irq;
  305. mutex_init(&pwr->lock);
  306. /* Disable interrupts */
  307. mid_pwr_interrupt_disable(pwr);
  308. if (info && info->set_initial_state) {
  309. ret = info->set_initial_state(pwr);
  310. if (ret)
  311. dev_warn(dev, "Can't set initial state: %d\n", ret);
  312. }
  313. ret = devm_request_irq(dev, pdev->irq, mid_pwr_irq_handler,
  314. IRQF_NO_SUSPEND, pci_name(pdev), pwr);
  315. if (ret)
  316. return ret;
  317. pwr->available = true;
  318. midpwr = pwr;
  319. pci_set_drvdata(pdev, pwr);
  320. return 0;
  321. }
  322. static int mid_set_initial_state(struct mid_pwr *pwr, const u32 *states)
  323. {
  324. unsigned int i, j;
  325. int ret;
  326. /*
  327. * Enable wake events.
  328. *
  329. * PWRMU supports up to 32 sources for wake up the system. Ungate them
  330. * all here.
  331. */
  332. mid_pwr_set_wake(pwr, 0, 0xffffffff);
  333. mid_pwr_set_wake(pwr, 1, 0xffffffff);
  334. /*
  335. * Power off South Complex devices.
  336. *
  337. * There is a map (see a note below) of 64 devices with 2 bits per each
  338. * on 32-bit HW registers. The following calls set all devices to one
  339. * known initial state, i.e. PCI_D3hot. This is done in conjunction
  340. * with PMCSR setting in arch/x86/pci/intel_mid_pci.c.
  341. *
  342. * NOTE: The actual device mapping is provided by a platform at run
  343. * time using vendor capability of PCI configuration space.
  344. */
  345. mid_pwr_set_state(pwr, 0, states[0]);
  346. mid_pwr_set_state(pwr, 1, states[1]);
  347. mid_pwr_set_state(pwr, 2, states[2]);
  348. mid_pwr_set_state(pwr, 3, states[3]);
  349. /* Send command to SCU */
  350. ret = mid_pwr_wait_for_cmd(pwr, CMD_SET_CFG);
  351. if (ret)
  352. return ret;
  353. for (i = 0; i < LSS_MAX_DEVS; i++) {
  354. for (j = 0; j < LSS_MAX_SHARED_DEVS; j++)
  355. pwr->lss[i][j].state = PCI_D3hot;
  356. }
  357. return 0;
  358. }
  359. static int pnw_set_initial_state(struct mid_pwr *pwr)
  360. {
  361. /* On Penwell SRAM must stay powered on */
  362. const u32 states[] = {
  363. 0xf00fffff, /* PM_SSC(0) */
  364. 0xffffffff, /* PM_SSC(1) */
  365. 0xffffffff, /* PM_SSC(2) */
  366. 0xffffffff, /* PM_SSC(3) */
  367. };
  368. return mid_set_initial_state(pwr, states);
  369. }
  370. static int tng_set_initial_state(struct mid_pwr *pwr)
  371. {
  372. const u32 states[] = {
  373. 0xffffffff, /* PM_SSC(0) */
  374. 0xffffffff, /* PM_SSC(1) */
  375. 0xffffffff, /* PM_SSC(2) */
  376. 0xffffffff, /* PM_SSC(3) */
  377. };
  378. return mid_set_initial_state(pwr, states);
  379. }
  380. static const struct mid_pwr_device_info pnw_info = {
  381. .set_initial_state = pnw_set_initial_state,
  382. };
  383. static const struct mid_pwr_device_info tng_info = {
  384. .set_initial_state = tng_set_initial_state,
  385. };
  386. /* This table should be in sync with the one in drivers/pci/pci-mid.c */
  387. static const struct pci_device_id mid_pwr_pci_ids[] = {
  388. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PENWELL), (kernel_ulong_t)&pnw_info },
  389. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_TANGIER), (kernel_ulong_t)&tng_info },
  390. {}
  391. };
  392. static struct pci_driver mid_pwr_pci_driver = {
  393. .name = "intel_mid_pwr",
  394. .probe = mid_pwr_probe,
  395. .id_table = mid_pwr_pci_ids,
  396. };
  397. builtin_pci_driver(mid_pwr_pci_driver);