hisi_lpc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Hisilicon Limited, All Rights Reserved.
  4. * Author: Zhichang Yuan <yuanzhichang@hisilicon.com>
  5. * Author: Zou Rongrong <zourongrong@huawei.com>
  6. * Author: John Garry <john.garry@huawei.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/console.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/logic_pio.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/pci.h>
  18. #include <linux/serial_8250.h>
  19. #include <linux/slab.h>
  20. #define DRV_NAME "hisi-lpc"
  21. /*
  22. * Setting this bit means each IO operation will target a different port
  23. * address; 0 means repeated IO operations will use the same port,
  24. * such as BT.
  25. */
  26. #define FG_INCRADDR_LPC 0x02
  27. struct lpc_cycle_para {
  28. unsigned int opflags;
  29. unsigned int csize; /* data length of each operation */
  30. };
  31. struct hisi_lpc_dev {
  32. spinlock_t cycle_lock;
  33. void __iomem *membase;
  34. struct logic_pio_hwaddr *io_host;
  35. };
  36. /* The max IO cycle counts supported is four per operation at maximum */
  37. #define LPC_MAX_DWIDTH 4
  38. #define LPC_REG_STARTUP_SIGNAL 0x00
  39. #define LPC_REG_STARTUP_SIGNAL_START BIT(0)
  40. #define LPC_REG_OP_STATUS 0x04
  41. #define LPC_REG_OP_STATUS_IDLE BIT(0)
  42. #define LPC_REG_OP_STATUS_FINISHED BIT(1)
  43. #define LPC_REG_OP_LEN 0x10 /* LPC cycles count per start */
  44. #define LPC_REG_CMD 0x14
  45. #define LPC_REG_CMD_OP BIT(0) /* 0: read, 1: write */
  46. #define LPC_REG_CMD_SAMEADDR BIT(3)
  47. #define LPC_REG_ADDR 0x20 /* target address */
  48. #define LPC_REG_WDATA 0x24 /* write FIFO */
  49. #define LPC_REG_RDATA 0x28 /* read FIFO */
  50. /* The minimal nanosecond interval for each query on LPC cycle status */
  51. #define LPC_NSEC_PERWAIT 100
  52. /*
  53. * The maximum waiting time is about 128us. It is specific for stream I/O,
  54. * such as ins.
  55. *
  56. * The fastest IO cycle time is about 390ns, but the worst case will wait
  57. * for extra 256 lpc clocks, so (256 + 13) * 30ns = 8 us. The maximum burst
  58. * cycles is 16. So, the maximum waiting time is about 128us under worst
  59. * case.
  60. *
  61. * Choose 1300 as the maximum.
  62. */
  63. #define LPC_MAX_WAITCNT 1300
  64. /* About 10us. This is specific for single IO operations, such as inb */
  65. #define LPC_PEROP_WAITCNT 100
  66. static int wait_lpc_idle(void __iomem *mbase, unsigned int waitcnt)
  67. {
  68. u32 status;
  69. do {
  70. status = readl(mbase + LPC_REG_OP_STATUS);
  71. if (status & LPC_REG_OP_STATUS_IDLE)
  72. return (status & LPC_REG_OP_STATUS_FINISHED) ? 0 : -EIO;
  73. ndelay(LPC_NSEC_PERWAIT);
  74. } while (--waitcnt);
  75. return -ETIME;
  76. }
  77. /*
  78. * hisi_lpc_target_in - trigger a series of LPC cycles for read operation
  79. * @lpcdev: pointer to hisi lpc device
  80. * @para: some parameters used to control the lpc I/O operations
  81. * @addr: the lpc I/O target port address
  82. * @buf: where the read back data is stored
  83. * @opcnt: how many I/O operations required, i.e. data width
  84. *
  85. * Returns 0 on success, non-zero on fail.
  86. */
  87. static int hisi_lpc_target_in(struct hisi_lpc_dev *lpcdev,
  88. struct lpc_cycle_para *para, unsigned long addr,
  89. unsigned char *buf, unsigned long opcnt)
  90. {
  91. unsigned int cmd_word;
  92. unsigned int waitcnt;
  93. unsigned long flags;
  94. int ret;
  95. if (!buf || !opcnt || !para || !para->csize || !lpcdev)
  96. return -EINVAL;
  97. cmd_word = 0; /* IO mode, Read */
  98. waitcnt = LPC_PEROP_WAITCNT;
  99. if (!(para->opflags & FG_INCRADDR_LPC)) {
  100. cmd_word |= LPC_REG_CMD_SAMEADDR;
  101. waitcnt = LPC_MAX_WAITCNT;
  102. }
  103. /* whole operation must be atomic */
  104. spin_lock_irqsave(&lpcdev->cycle_lock, flags);
  105. writel_relaxed(opcnt, lpcdev->membase + LPC_REG_OP_LEN);
  106. writel_relaxed(cmd_word, lpcdev->membase + LPC_REG_CMD);
  107. writel_relaxed(addr, lpcdev->membase + LPC_REG_ADDR);
  108. writel(LPC_REG_STARTUP_SIGNAL_START,
  109. lpcdev->membase + LPC_REG_STARTUP_SIGNAL);
  110. /* whether the operation is finished */
  111. ret = wait_lpc_idle(lpcdev->membase, waitcnt);
  112. if (ret) {
  113. spin_unlock_irqrestore(&lpcdev->cycle_lock, flags);
  114. return ret;
  115. }
  116. readsb(lpcdev->membase + LPC_REG_RDATA, buf, opcnt);
  117. spin_unlock_irqrestore(&lpcdev->cycle_lock, flags);
  118. return 0;
  119. }
  120. /*
  121. * hisi_lpc_target_out - trigger a series of LPC cycles for write operation
  122. * @lpcdev: pointer to hisi lpc device
  123. * @para: some parameters used to control the lpc I/O operations
  124. * @addr: the lpc I/O target port address
  125. * @buf: where the data to be written is stored
  126. * @opcnt: how many I/O operations required, i.e. data width
  127. *
  128. * Returns 0 on success, non-zero on fail.
  129. */
  130. static int hisi_lpc_target_out(struct hisi_lpc_dev *lpcdev,
  131. struct lpc_cycle_para *para, unsigned long addr,
  132. const unsigned char *buf, unsigned long opcnt)
  133. {
  134. unsigned int waitcnt;
  135. unsigned long flags;
  136. u32 cmd_word;
  137. int ret;
  138. if (!buf || !opcnt || !para || !lpcdev)
  139. return -EINVAL;
  140. /* default is increasing address */
  141. cmd_word = LPC_REG_CMD_OP; /* IO mode, write */
  142. waitcnt = LPC_PEROP_WAITCNT;
  143. if (!(para->opflags & FG_INCRADDR_LPC)) {
  144. cmd_word |= LPC_REG_CMD_SAMEADDR;
  145. waitcnt = LPC_MAX_WAITCNT;
  146. }
  147. spin_lock_irqsave(&lpcdev->cycle_lock, flags);
  148. writel_relaxed(opcnt, lpcdev->membase + LPC_REG_OP_LEN);
  149. writel_relaxed(cmd_word, lpcdev->membase + LPC_REG_CMD);
  150. writel_relaxed(addr, lpcdev->membase + LPC_REG_ADDR);
  151. writesb(lpcdev->membase + LPC_REG_WDATA, buf, opcnt);
  152. writel(LPC_REG_STARTUP_SIGNAL_START,
  153. lpcdev->membase + LPC_REG_STARTUP_SIGNAL);
  154. /* whether the operation is finished */
  155. ret = wait_lpc_idle(lpcdev->membase, waitcnt);
  156. spin_unlock_irqrestore(&lpcdev->cycle_lock, flags);
  157. return ret;
  158. }
  159. static unsigned long hisi_lpc_pio_to_addr(struct hisi_lpc_dev *lpcdev,
  160. unsigned long pio)
  161. {
  162. return pio - lpcdev->io_host->io_start + lpcdev->io_host->hw_start;
  163. }
  164. /*
  165. * hisi_lpc_comm_in - input the data in a single operation
  166. * @hostdata: pointer to the device information relevant to LPC controller
  167. * @pio: the target I/O port address
  168. * @dwidth: the data length required to read from the target I/O port
  169. *
  170. * When success, data is returned. Otherwise, ~0 is returned.
  171. */
  172. static u32 hisi_lpc_comm_in(void *hostdata, unsigned long pio, size_t dwidth)
  173. {
  174. struct hisi_lpc_dev *lpcdev = hostdata;
  175. struct lpc_cycle_para iopara;
  176. unsigned long addr;
  177. __le32 rd_data = 0;
  178. int ret;
  179. if (!lpcdev || !dwidth || dwidth > LPC_MAX_DWIDTH)
  180. return ~0;
  181. addr = hisi_lpc_pio_to_addr(lpcdev, pio);
  182. iopara.opflags = FG_INCRADDR_LPC;
  183. iopara.csize = dwidth;
  184. ret = hisi_lpc_target_in(lpcdev, &iopara, addr,
  185. (unsigned char *)&rd_data, dwidth);
  186. if (ret)
  187. return ~0;
  188. return le32_to_cpu(rd_data);
  189. }
  190. /*
  191. * hisi_lpc_comm_out - output the data in a single operation
  192. * @hostdata: pointer to the device information relevant to LPC controller
  193. * @pio: the target I/O port address
  194. * @val: a value to be output from caller, maximum is four bytes
  195. * @dwidth: the data width required writing to the target I/O port
  196. *
  197. * This function corresponds to out(b,w,l) only.
  198. */
  199. static void hisi_lpc_comm_out(void *hostdata, unsigned long pio,
  200. u32 val, size_t dwidth)
  201. {
  202. struct hisi_lpc_dev *lpcdev = hostdata;
  203. struct lpc_cycle_para iopara;
  204. const unsigned char *buf;
  205. unsigned long addr;
  206. __le32 _val = cpu_to_le32(val);
  207. if (!lpcdev || !dwidth || dwidth > LPC_MAX_DWIDTH)
  208. return;
  209. buf = (const unsigned char *)&_val;
  210. addr = hisi_lpc_pio_to_addr(lpcdev, pio);
  211. iopara.opflags = FG_INCRADDR_LPC;
  212. iopara.csize = dwidth;
  213. hisi_lpc_target_out(lpcdev, &iopara, addr, buf, dwidth);
  214. }
  215. /*
  216. * hisi_lpc_comm_ins - input the data in the buffer in multiple operations
  217. * @hostdata: pointer to the device information relevant to LPC controller
  218. * @pio: the target I/O port address
  219. * @buffer: a buffer where read/input data bytes are stored
  220. * @dwidth: the data width required writing to the target I/O port
  221. * @count: how many data units whose length is dwidth will be read
  222. *
  223. * When success, the data read back is stored in buffer pointed by buffer.
  224. * Returns 0 on success, -errno otherwise.
  225. */
  226. static u32 hisi_lpc_comm_ins(void *hostdata, unsigned long pio, void *buffer,
  227. size_t dwidth, unsigned int count)
  228. {
  229. struct hisi_lpc_dev *lpcdev = hostdata;
  230. unsigned char *buf = buffer;
  231. struct lpc_cycle_para iopara;
  232. unsigned long addr;
  233. if (!lpcdev || !buf || !count || !dwidth || dwidth > LPC_MAX_DWIDTH)
  234. return -EINVAL;
  235. iopara.opflags = 0;
  236. if (dwidth > 1)
  237. iopara.opflags |= FG_INCRADDR_LPC;
  238. iopara.csize = dwidth;
  239. addr = hisi_lpc_pio_to_addr(lpcdev, pio);
  240. do {
  241. int ret;
  242. ret = hisi_lpc_target_in(lpcdev, &iopara, addr, buf, dwidth);
  243. if (ret)
  244. return ret;
  245. buf += dwidth;
  246. } while (--count);
  247. return 0;
  248. }
  249. /*
  250. * hisi_lpc_comm_outs - output the data in the buffer in multiple operations
  251. * @hostdata: pointer to the device information relevant to LPC controller
  252. * @pio: the target I/O port address
  253. * @buffer: a buffer where write/output data bytes are stored
  254. * @dwidth: the data width required writing to the target I/O port
  255. * @count: how many data units whose length is dwidth will be written
  256. */
  257. static void hisi_lpc_comm_outs(void *hostdata, unsigned long pio,
  258. const void *buffer, size_t dwidth,
  259. unsigned int count)
  260. {
  261. struct hisi_lpc_dev *lpcdev = hostdata;
  262. struct lpc_cycle_para iopara;
  263. const unsigned char *buf = buffer;
  264. unsigned long addr;
  265. if (!lpcdev || !buf || !count || !dwidth || dwidth > LPC_MAX_DWIDTH)
  266. return;
  267. iopara.opflags = 0;
  268. if (dwidth > 1)
  269. iopara.opflags |= FG_INCRADDR_LPC;
  270. iopara.csize = dwidth;
  271. addr = hisi_lpc_pio_to_addr(lpcdev, pio);
  272. do {
  273. if (hisi_lpc_target_out(lpcdev, &iopara, addr, buf, dwidth))
  274. break;
  275. buf += dwidth;
  276. } while (--count);
  277. }
  278. static const struct logic_pio_host_ops hisi_lpc_ops = {
  279. .in = hisi_lpc_comm_in,
  280. .out = hisi_lpc_comm_out,
  281. .ins = hisi_lpc_comm_ins,
  282. .outs = hisi_lpc_comm_outs,
  283. };
  284. #ifdef CONFIG_ACPI
  285. static int hisi_lpc_acpi_xlat_io_res(struct acpi_device *adev,
  286. struct acpi_device *host,
  287. struct resource *res)
  288. {
  289. unsigned long sys_port;
  290. resource_size_t len = resource_size(res);
  291. sys_port = logic_pio_trans_hwaddr(&host->fwnode, res->start, len);
  292. if (sys_port == ~0UL)
  293. return -EFAULT;
  294. res->start = sys_port;
  295. res->end = sys_port + len;
  296. return 0;
  297. }
  298. /*
  299. * Released firmware describes the IO port max address as 0x3fff, which is
  300. * the max host bus address. Fixup to a proper range. This will probably
  301. * never be fixed in firmware.
  302. */
  303. static void hisi_lpc_acpi_fixup_child_resource(struct device *hostdev,
  304. struct resource *r)
  305. {
  306. if (r->end != 0x3fff)
  307. return;
  308. if (r->start == 0xe4)
  309. r->end = 0xe4 + 0x04 - 1;
  310. else if (r->start == 0x2f8)
  311. r->end = 0x2f8 + 0x08 - 1;
  312. else
  313. dev_warn(hostdev, "unrecognised resource %pR to fixup, ignoring\n",
  314. r);
  315. }
  316. /*
  317. * hisi_lpc_acpi_set_io_res - set the resources for a child
  318. * @child: the device node to be updated the I/O resource
  319. * @hostdev: the device node associated with host controller
  320. * @res: double pointer to be set to the address of translated resources
  321. * @num_res: pointer to variable to hold the number of translated resources
  322. *
  323. * Returns 0 when successful, and a negative value for failure.
  324. *
  325. * For a given host controller, each child device will have an associated
  326. * host-relative address resource. This function will return the translated
  327. * logical PIO addresses for each child devices resources.
  328. */
  329. static int hisi_lpc_acpi_set_io_res(struct device *child,
  330. struct device *hostdev,
  331. const struct resource **res, int *num_res)
  332. {
  333. struct acpi_device *adev;
  334. struct acpi_device *host;
  335. struct resource_entry *rentry;
  336. LIST_HEAD(resource_list);
  337. struct resource *resources;
  338. int count;
  339. int i;
  340. if (!child || !hostdev)
  341. return -EINVAL;
  342. host = to_acpi_device(hostdev);
  343. adev = to_acpi_device(child);
  344. if (!adev->status.present) {
  345. dev_dbg(child, "device is not present\n");
  346. return -EIO;
  347. }
  348. if (acpi_device_enumerated(adev)) {
  349. dev_dbg(child, "has been enumerated\n");
  350. return -EIO;
  351. }
  352. /*
  353. * The following code segment to retrieve the resources is common to
  354. * acpi_create_platform_device(), so consider a common helper function
  355. * in future.
  356. */
  357. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  358. if (count <= 0) {
  359. dev_dbg(child, "failed to get resources\n");
  360. return count ? count : -EIO;
  361. }
  362. resources = devm_kcalloc(hostdev, count, sizeof(*resources),
  363. GFP_KERNEL);
  364. if (!resources) {
  365. dev_warn(hostdev, "could not allocate memory for %d resources\n",
  366. count);
  367. acpi_dev_free_resource_list(&resource_list);
  368. return -ENOMEM;
  369. }
  370. count = 0;
  371. list_for_each_entry(rentry, &resource_list, node) {
  372. resources[count] = *rentry->res;
  373. hisi_lpc_acpi_fixup_child_resource(hostdev, &resources[count]);
  374. count++;
  375. }
  376. acpi_dev_free_resource_list(&resource_list);
  377. /* translate the I/O resources */
  378. for (i = 0; i < count; i++) {
  379. int ret;
  380. if (!(resources[i].flags & IORESOURCE_IO))
  381. continue;
  382. ret = hisi_lpc_acpi_xlat_io_res(adev, host, &resources[i]);
  383. if (ret) {
  384. dev_err(child, "translate IO range %pR failed (%d)\n",
  385. &resources[i], ret);
  386. return ret;
  387. }
  388. }
  389. *res = resources;
  390. *num_res = count;
  391. return 0;
  392. }
  393. static int hisi_lpc_acpi_remove_subdev(struct device *dev, void *unused)
  394. {
  395. platform_device_unregister(to_platform_device(dev));
  396. return 0;
  397. }
  398. struct hisi_lpc_acpi_cell {
  399. const char *hid;
  400. const char *name;
  401. void *pdata;
  402. size_t pdata_size;
  403. };
  404. static void hisi_lpc_acpi_remove(struct device *hostdev)
  405. {
  406. struct acpi_device *adev = ACPI_COMPANION(hostdev);
  407. struct acpi_device *child;
  408. device_for_each_child(hostdev, NULL, hisi_lpc_acpi_remove_subdev);
  409. list_for_each_entry(child, &adev->children, node)
  410. acpi_device_clear_enumerated(child);
  411. }
  412. /*
  413. * hisi_lpc_acpi_probe - probe children for ACPI FW
  414. * @hostdev: LPC host device pointer
  415. *
  416. * Returns 0 when successful, and a negative value for failure.
  417. *
  418. * Create a platform device per child, fixing up the resources
  419. * from bus addresses to Logical PIO addresses.
  420. *
  421. */
  422. static int hisi_lpc_acpi_probe(struct device *hostdev)
  423. {
  424. struct acpi_device *adev = ACPI_COMPANION(hostdev);
  425. struct acpi_device *child;
  426. int ret;
  427. /* Only consider the children of the host */
  428. list_for_each_entry(child, &adev->children, node) {
  429. const char *hid = acpi_device_hid(child);
  430. const struct hisi_lpc_acpi_cell *cell;
  431. struct platform_device *pdev;
  432. const struct resource *res;
  433. bool found = false;
  434. int num_res;
  435. ret = hisi_lpc_acpi_set_io_res(&child->dev, &adev->dev, &res,
  436. &num_res);
  437. if (ret) {
  438. dev_warn(hostdev, "set resource fail (%d)\n", ret);
  439. goto fail;
  440. }
  441. cell = (struct hisi_lpc_acpi_cell []){
  442. /* ipmi */
  443. {
  444. .hid = "IPI0001",
  445. .name = "hisi-lpc-ipmi",
  446. },
  447. /* 8250-compatible uart */
  448. {
  449. .hid = "HISI1031",
  450. .name = "serial8250",
  451. .pdata = (struct plat_serial8250_port []) {
  452. {
  453. .iobase = res->start,
  454. .uartclk = 1843200,
  455. .iotype = UPIO_PORT,
  456. .flags = UPF_BOOT_AUTOCONF,
  457. },
  458. {}
  459. },
  460. .pdata_size = 2 *
  461. sizeof(struct plat_serial8250_port),
  462. },
  463. {}
  464. };
  465. for (; cell && cell->name; cell++) {
  466. if (!strcmp(cell->hid, hid)) {
  467. found = true;
  468. break;
  469. }
  470. }
  471. if (!found) {
  472. dev_warn(hostdev,
  473. "could not find cell for child device (%s), discarding\n",
  474. hid);
  475. continue;
  476. }
  477. pdev = platform_device_alloc(cell->name, PLATFORM_DEVID_AUTO);
  478. if (!pdev) {
  479. ret = -ENOMEM;
  480. goto fail;
  481. }
  482. pdev->dev.parent = hostdev;
  483. ACPI_COMPANION_SET(&pdev->dev, child);
  484. ret = platform_device_add_resources(pdev, res, num_res);
  485. if (ret)
  486. goto fail;
  487. ret = platform_device_add_data(pdev, cell->pdata,
  488. cell->pdata_size);
  489. if (ret)
  490. goto fail;
  491. ret = platform_device_add(pdev);
  492. if (ret)
  493. goto fail;
  494. acpi_device_set_enumerated(child);
  495. }
  496. return 0;
  497. fail:
  498. hisi_lpc_acpi_remove(hostdev);
  499. return ret;
  500. }
  501. static const struct acpi_device_id hisi_lpc_acpi_match[] = {
  502. {"HISI0191"},
  503. {}
  504. };
  505. #else
  506. static int hisi_lpc_acpi_probe(struct device *dev)
  507. {
  508. return -ENODEV;
  509. }
  510. static void hisi_lpc_acpi_remove(struct device *hostdev)
  511. {
  512. }
  513. #endif // CONFIG_ACPI
  514. /*
  515. * hisi_lpc_probe - the probe callback function for hisi lpc host,
  516. * will finish all the initialization.
  517. * @pdev: the platform device corresponding to hisi lpc host
  518. *
  519. * Returns 0 on success, non-zero on fail.
  520. */
  521. static int hisi_lpc_probe(struct platform_device *pdev)
  522. {
  523. struct device *dev = &pdev->dev;
  524. struct acpi_device *acpi_device = ACPI_COMPANION(dev);
  525. struct logic_pio_hwaddr *range;
  526. struct hisi_lpc_dev *lpcdev;
  527. resource_size_t io_end;
  528. struct resource *res;
  529. int ret;
  530. lpcdev = devm_kzalloc(dev, sizeof(*lpcdev), GFP_KERNEL);
  531. if (!lpcdev)
  532. return -ENOMEM;
  533. spin_lock_init(&lpcdev->cycle_lock);
  534. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  535. lpcdev->membase = devm_ioremap_resource(dev, res);
  536. if (IS_ERR(lpcdev->membase))
  537. return PTR_ERR(lpcdev->membase);
  538. range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
  539. if (!range)
  540. return -ENOMEM;
  541. range->fwnode = dev->fwnode;
  542. range->flags = LOGIC_PIO_INDIRECT;
  543. range->size = PIO_INDIRECT_SIZE;
  544. range->hostdata = lpcdev;
  545. range->ops = &hisi_lpc_ops;
  546. lpcdev->io_host = range;
  547. ret = logic_pio_register_range(range);
  548. if (ret) {
  549. dev_err(dev, "register IO range failed (%d)!\n", ret);
  550. return ret;
  551. }
  552. /* register the LPC host PIO resources */
  553. if (acpi_device)
  554. ret = hisi_lpc_acpi_probe(dev);
  555. else
  556. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  557. if (ret) {
  558. logic_pio_unregister_range(range);
  559. return ret;
  560. }
  561. dev_set_drvdata(dev, lpcdev);
  562. io_end = lpcdev->io_host->io_start + lpcdev->io_host->size;
  563. dev_info(dev, "registered range [%pa - %pa]\n",
  564. &lpcdev->io_host->io_start, &io_end);
  565. return ret;
  566. }
  567. static int hisi_lpc_remove(struct platform_device *pdev)
  568. {
  569. struct device *dev = &pdev->dev;
  570. struct acpi_device *acpi_device = ACPI_COMPANION(dev);
  571. struct hisi_lpc_dev *lpcdev = dev_get_drvdata(dev);
  572. struct logic_pio_hwaddr *range = lpcdev->io_host;
  573. if (acpi_device)
  574. hisi_lpc_acpi_remove(dev);
  575. else
  576. of_platform_depopulate(dev);
  577. logic_pio_unregister_range(range);
  578. return 0;
  579. }
  580. static const struct of_device_id hisi_lpc_of_match[] = {
  581. { .compatible = "hisilicon,hip06-lpc", },
  582. { .compatible = "hisilicon,hip07-lpc", },
  583. {}
  584. };
  585. static struct platform_driver hisi_lpc_driver = {
  586. .driver = {
  587. .name = DRV_NAME,
  588. .of_match_table = hisi_lpc_of_match,
  589. .acpi_match_table = ACPI_PTR(hisi_lpc_acpi_match),
  590. },
  591. .probe = hisi_lpc_probe,
  592. .remove = hisi_lpc_remove,
  593. };
  594. builtin_platform_driver(hisi_lpc_driver);