18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * PowerNV LPC bus handling.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2013 IBM Corp.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/of.h>
108c2ecf20Sopenharmony_ci#include <linux/bug.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm/machdep.h>
158c2ecf20Sopenharmony_ci#include <asm/firmware.h>
168c2ecf20Sopenharmony_ci#include <asm/opal.h>
178c2ecf20Sopenharmony_ci#include <asm/prom.h>
188c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
198c2ecf20Sopenharmony_ci#include <asm/debugfs.h>
208c2ecf20Sopenharmony_ci#include <asm/isa-bridge.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic int opal_lpc_chip_id = -1;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic u8 opal_lpc_inb(unsigned long port)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	int64_t rc;
278c2ecf20Sopenharmony_ci	__be32 data;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0 || port > 0xffff)
308c2ecf20Sopenharmony_ci		return 0xff;
318c2ecf20Sopenharmony_ci	rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 1);
328c2ecf20Sopenharmony_ci	return rc ? 0xff : be32_to_cpu(data);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic __le16 __opal_lpc_inw(unsigned long port)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	int64_t rc;
388c2ecf20Sopenharmony_ci	__be32 data;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0 || port > 0xfffe)
418c2ecf20Sopenharmony_ci		return 0xffff;
428c2ecf20Sopenharmony_ci	if (port & 1)
438c2ecf20Sopenharmony_ci		return (__le16)opal_lpc_inb(port) << 8 | opal_lpc_inb(port + 1);
448c2ecf20Sopenharmony_ci	rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 2);
458c2ecf20Sopenharmony_ci	return rc ? 0xffff : be32_to_cpu(data);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_cistatic u16 opal_lpc_inw(unsigned long port)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	return le16_to_cpu(__opal_lpc_inw(port));
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic __le32 __opal_lpc_inl(unsigned long port)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	int64_t rc;
558c2ecf20Sopenharmony_ci	__be32 data;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0 || port > 0xfffc)
588c2ecf20Sopenharmony_ci		return 0xffffffff;
598c2ecf20Sopenharmony_ci	if (port & 3)
608c2ecf20Sopenharmony_ci		return (__le32)opal_lpc_inb(port    ) << 24 |
618c2ecf20Sopenharmony_ci		       (__le32)opal_lpc_inb(port + 1) << 16 |
628c2ecf20Sopenharmony_ci		       (__le32)opal_lpc_inb(port + 2) <<  8 |
638c2ecf20Sopenharmony_ci			       opal_lpc_inb(port + 3);
648c2ecf20Sopenharmony_ci	rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 4);
658c2ecf20Sopenharmony_ci	return rc ? 0xffffffff : be32_to_cpu(data);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic u32 opal_lpc_inl(unsigned long port)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return le32_to_cpu(__opal_lpc_inl(port));
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void opal_lpc_outb(u8 val, unsigned long port)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0 || port > 0xffff)
768c2ecf20Sopenharmony_ci		return;
778c2ecf20Sopenharmony_ci	opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 1);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic void __opal_lpc_outw(__le16 val, unsigned long port)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0 || port > 0xfffe)
838c2ecf20Sopenharmony_ci		return;
848c2ecf20Sopenharmony_ci	if (port & 1) {
858c2ecf20Sopenharmony_ci		opal_lpc_outb(val >> 8, port);
868c2ecf20Sopenharmony_ci		opal_lpc_outb(val     , port + 1);
878c2ecf20Sopenharmony_ci		return;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci	opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 2);
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic void opal_lpc_outw(u16 val, unsigned long port)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	__opal_lpc_outw(cpu_to_le16(val), port);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic void __opal_lpc_outl(__le32 val, unsigned long port)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0 || port > 0xfffc)
1008c2ecf20Sopenharmony_ci		return;
1018c2ecf20Sopenharmony_ci	if (port & 3) {
1028c2ecf20Sopenharmony_ci		opal_lpc_outb(val >> 24, port);
1038c2ecf20Sopenharmony_ci		opal_lpc_outb(val >> 16, port + 1);
1048c2ecf20Sopenharmony_ci		opal_lpc_outb(val >>  8, port + 2);
1058c2ecf20Sopenharmony_ci		opal_lpc_outb(val      , port + 3);
1068c2ecf20Sopenharmony_ci		return;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 4);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void opal_lpc_outl(u32 val, unsigned long port)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	__opal_lpc_outl(cpu_to_le32(val), port);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void opal_lpc_insb(unsigned long p, void *b, unsigned long c)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	u8 *ptr = b;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	while(c--)
1218c2ecf20Sopenharmony_ci		*(ptr++) = opal_lpc_inb(p);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void opal_lpc_insw(unsigned long p, void *b, unsigned long c)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	__le16 *ptr = b;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	while(c--)
1298c2ecf20Sopenharmony_ci		*(ptr++) = __opal_lpc_inw(p);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic void opal_lpc_insl(unsigned long p, void *b, unsigned long c)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	__le32 *ptr = b;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	while(c--)
1378c2ecf20Sopenharmony_ci		*(ptr++) = __opal_lpc_inl(p);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic void opal_lpc_outsb(unsigned long p, const void *b, unsigned long c)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	const u8 *ptr = b;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	while(c--)
1458c2ecf20Sopenharmony_ci		opal_lpc_outb(*(ptr++), p);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic void opal_lpc_outsw(unsigned long p, const void *b, unsigned long c)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	const __le16 *ptr = b;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	while(c--)
1538c2ecf20Sopenharmony_ci		__opal_lpc_outw(*(ptr++), p);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic void opal_lpc_outsl(unsigned long p, const void *b, unsigned long c)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	const __le32 *ptr = b;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	while(c--)
1618c2ecf20Sopenharmony_ci		__opal_lpc_outl(*(ptr++), p);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic const struct ppc_pci_io opal_lpc_io = {
1658c2ecf20Sopenharmony_ci	.inb	= opal_lpc_inb,
1668c2ecf20Sopenharmony_ci	.inw	= opal_lpc_inw,
1678c2ecf20Sopenharmony_ci	.inl	= opal_lpc_inl,
1688c2ecf20Sopenharmony_ci	.outb	= opal_lpc_outb,
1698c2ecf20Sopenharmony_ci	.outw	= opal_lpc_outw,
1708c2ecf20Sopenharmony_ci	.outl	= opal_lpc_outl,
1718c2ecf20Sopenharmony_ci	.insb	= opal_lpc_insb,
1728c2ecf20Sopenharmony_ci	.insw	= opal_lpc_insw,
1738c2ecf20Sopenharmony_ci	.insl	= opal_lpc_insl,
1748c2ecf20Sopenharmony_ci	.outsb	= opal_lpc_outsb,
1758c2ecf20Sopenharmony_ci	.outsw	= opal_lpc_outsw,
1768c2ecf20Sopenharmony_ci	.outsl	= opal_lpc_outsl,
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
1808c2ecf20Sopenharmony_cistruct lpc_debugfs_entry {
1818c2ecf20Sopenharmony_ci	enum OpalLPCAddressType lpc_type;
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic ssize_t lpc_debug_read(struct file *filp, char __user *ubuf,
1858c2ecf20Sopenharmony_ci			      size_t count, loff_t *ppos)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct lpc_debugfs_entry *lpc = filp->private_data;
1888c2ecf20Sopenharmony_ci	u32 data, pos, len, todo;
1898c2ecf20Sopenharmony_ci	int rc;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (!access_ok(ubuf, count))
1928c2ecf20Sopenharmony_ci		return -EFAULT;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	todo = count;
1958c2ecf20Sopenharmony_ci	while (todo) {
1968c2ecf20Sopenharmony_ci		pos = *ppos;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		/*
1998c2ecf20Sopenharmony_ci		 * Select access size based on count and alignment and
2008c2ecf20Sopenharmony_ci		 * access type. IO and MEM only support byte acceses,
2018c2ecf20Sopenharmony_ci		 * FW supports all 3.
2028c2ecf20Sopenharmony_ci		 */
2038c2ecf20Sopenharmony_ci		len = 1;
2048c2ecf20Sopenharmony_ci		if (lpc->lpc_type == OPAL_LPC_FW) {
2058c2ecf20Sopenharmony_ci			if (todo > 3 && (pos & 3) == 0)
2068c2ecf20Sopenharmony_ci				len = 4;
2078c2ecf20Sopenharmony_ci			else if (todo > 1 && (pos & 1) == 0)
2088c2ecf20Sopenharmony_ci				len = 2;
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci		rc = opal_lpc_read(opal_lpc_chip_id, lpc->lpc_type, pos,
2118c2ecf20Sopenharmony_ci				   &data, len);
2128c2ecf20Sopenharmony_ci		if (rc)
2138c2ecf20Sopenharmony_ci			return -ENXIO;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		/*
2168c2ecf20Sopenharmony_ci		 * Now there is some trickery with the data returned by OPAL
2178c2ecf20Sopenharmony_ci		 * as it's the desired data right justified in a 32-bit BE
2188c2ecf20Sopenharmony_ci		 * word.
2198c2ecf20Sopenharmony_ci		 *
2208c2ecf20Sopenharmony_ci		 * This is a very bad interface and I'm to blame for it :-(
2218c2ecf20Sopenharmony_ci		 *
2228c2ecf20Sopenharmony_ci		 * So we can't just apply a 32-bit swap to what comes from OPAL,
2238c2ecf20Sopenharmony_ci		 * because user space expects the *bytes* to be in their proper
2248c2ecf20Sopenharmony_ci		 * respective positions (ie, LPC position).
2258c2ecf20Sopenharmony_ci		 *
2268c2ecf20Sopenharmony_ci		 * So what we really want to do here is to shift data right
2278c2ecf20Sopenharmony_ci		 * appropriately on a LE kernel.
2288c2ecf20Sopenharmony_ci		 *
2298c2ecf20Sopenharmony_ci		 * IE. If the LPC transaction has bytes B0, B1, B2 and B3 in that
2308c2ecf20Sopenharmony_ci		 * order, we have in memory written to by OPAL at the "data"
2318c2ecf20Sopenharmony_ci		 * pointer:
2328c2ecf20Sopenharmony_ci		 *
2338c2ecf20Sopenharmony_ci		 *               Bytes:      OPAL "data"   LE "data"
2348c2ecf20Sopenharmony_ci		 *   32-bit:   B0 B1 B2 B3   B0B1B2B3      B3B2B1B0
2358c2ecf20Sopenharmony_ci		 *   16-bit:   B0 B1         0000B0B1      B1B00000
2368c2ecf20Sopenharmony_ci		 *    8-bit:   B0            000000B0      B0000000
2378c2ecf20Sopenharmony_ci		 *
2388c2ecf20Sopenharmony_ci		 * So a BE kernel will have the leftmost of the above in the MSB
2398c2ecf20Sopenharmony_ci		 * and rightmost in the LSB and can just then "cast" the u32 "data"
2408c2ecf20Sopenharmony_ci		 * down to the appropriate quantity and write it.
2418c2ecf20Sopenharmony_ci		 *
2428c2ecf20Sopenharmony_ci		 * However, an LE kernel can't. It doesn't need to swap because a
2438c2ecf20Sopenharmony_ci		 * load from data followed by a store to user are going to preserve
2448c2ecf20Sopenharmony_ci		 * the byte ordering which is the wire byte order which is what the
2458c2ecf20Sopenharmony_ci		 * user wants, but in order to "crop" to the right size, we need to
2468c2ecf20Sopenharmony_ci		 * shift right first.
2478c2ecf20Sopenharmony_ci		 */
2488c2ecf20Sopenharmony_ci		switch(len) {
2498c2ecf20Sopenharmony_ci		case 4:
2508c2ecf20Sopenharmony_ci			rc = __put_user((u32)data, (u32 __user *)ubuf);
2518c2ecf20Sopenharmony_ci			break;
2528c2ecf20Sopenharmony_ci		case 2:
2538c2ecf20Sopenharmony_ci#ifdef __LITTLE_ENDIAN__
2548c2ecf20Sopenharmony_ci			data >>= 16;
2558c2ecf20Sopenharmony_ci#endif
2568c2ecf20Sopenharmony_ci			rc = __put_user((u16)data, (u16 __user *)ubuf);
2578c2ecf20Sopenharmony_ci			break;
2588c2ecf20Sopenharmony_ci		default:
2598c2ecf20Sopenharmony_ci#ifdef __LITTLE_ENDIAN__
2608c2ecf20Sopenharmony_ci			data >>= 24;
2618c2ecf20Sopenharmony_ci#endif
2628c2ecf20Sopenharmony_ci			rc = __put_user((u8)data, (u8 __user *)ubuf);
2638c2ecf20Sopenharmony_ci			break;
2648c2ecf20Sopenharmony_ci		}
2658c2ecf20Sopenharmony_ci		if (rc)
2668c2ecf20Sopenharmony_ci			return -EFAULT;
2678c2ecf20Sopenharmony_ci		*ppos += len;
2688c2ecf20Sopenharmony_ci		ubuf += len;
2698c2ecf20Sopenharmony_ci		todo -= len;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return count;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic ssize_t lpc_debug_write(struct file *filp, const char __user *ubuf,
2768c2ecf20Sopenharmony_ci			       size_t count, loff_t *ppos)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct lpc_debugfs_entry *lpc = filp->private_data;
2798c2ecf20Sopenharmony_ci	u32 data, pos, len, todo;
2808c2ecf20Sopenharmony_ci	int rc;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	if (!access_ok(ubuf, count))
2838c2ecf20Sopenharmony_ci		return -EFAULT;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	todo = count;
2868c2ecf20Sopenharmony_ci	while (todo) {
2878c2ecf20Sopenharmony_ci		pos = *ppos;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci		/*
2908c2ecf20Sopenharmony_ci		 * Select access size based on count and alignment and
2918c2ecf20Sopenharmony_ci		 * access type. IO and MEM only support byte acceses,
2928c2ecf20Sopenharmony_ci		 * FW supports all 3.
2938c2ecf20Sopenharmony_ci		 */
2948c2ecf20Sopenharmony_ci		len = 1;
2958c2ecf20Sopenharmony_ci		if (lpc->lpc_type == OPAL_LPC_FW) {
2968c2ecf20Sopenharmony_ci			if (todo > 3 && (pos & 3) == 0)
2978c2ecf20Sopenharmony_ci				len = 4;
2988c2ecf20Sopenharmony_ci			else if (todo > 1 && (pos & 1) == 0)
2998c2ecf20Sopenharmony_ci				len = 2;
3008c2ecf20Sopenharmony_ci		}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		/*
3038c2ecf20Sopenharmony_ci		 * Similarly to the read case, we have some trickery here but
3048c2ecf20Sopenharmony_ci		 * it's different to handle. We need to pass the value to OPAL in
3058c2ecf20Sopenharmony_ci		 * a register whose layout depends on the access size. We want
3068c2ecf20Sopenharmony_ci		 * to reproduce the memory layout of the user, however we aren't
3078c2ecf20Sopenharmony_ci		 * doing a load from user and a store to another memory location
3088c2ecf20Sopenharmony_ci		 * which would achieve that. Here we pass the value to OPAL via
3098c2ecf20Sopenharmony_ci		 * a register which is expected to contain the "BE" interpretation
3108c2ecf20Sopenharmony_ci		 * of the byte sequence. IE: for a 32-bit access, byte 0 should be
3118c2ecf20Sopenharmony_ci		 * in the MSB. So here we *do* need to byteswap on LE.
3128c2ecf20Sopenharmony_ci		 *
3138c2ecf20Sopenharmony_ci		 *           User bytes:    LE "data"  OPAL "data"
3148c2ecf20Sopenharmony_ci		 *  32-bit:  B0 B1 B2 B3    B3B2B1B0   B0B1B2B3
3158c2ecf20Sopenharmony_ci		 *  16-bit:  B0 B1          0000B1B0   0000B0B1
3168c2ecf20Sopenharmony_ci		 *   8-bit:  B0             000000B0   000000B0
3178c2ecf20Sopenharmony_ci		 */
3188c2ecf20Sopenharmony_ci		switch(len) {
3198c2ecf20Sopenharmony_ci		case 4:
3208c2ecf20Sopenharmony_ci			rc = __get_user(data, (u32 __user *)ubuf);
3218c2ecf20Sopenharmony_ci			data = cpu_to_be32(data);
3228c2ecf20Sopenharmony_ci			break;
3238c2ecf20Sopenharmony_ci		case 2:
3248c2ecf20Sopenharmony_ci			rc = __get_user(data, (u16 __user *)ubuf);
3258c2ecf20Sopenharmony_ci			data = cpu_to_be16(data);
3268c2ecf20Sopenharmony_ci			break;
3278c2ecf20Sopenharmony_ci		default:
3288c2ecf20Sopenharmony_ci			rc = __get_user(data, (u8 __user *)ubuf);
3298c2ecf20Sopenharmony_ci			break;
3308c2ecf20Sopenharmony_ci		}
3318c2ecf20Sopenharmony_ci		if (rc)
3328c2ecf20Sopenharmony_ci			return -EFAULT;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci		rc = opal_lpc_write(opal_lpc_chip_id, lpc->lpc_type, pos,
3358c2ecf20Sopenharmony_ci				    data, len);
3368c2ecf20Sopenharmony_ci		if (rc)
3378c2ecf20Sopenharmony_ci			return -ENXIO;
3388c2ecf20Sopenharmony_ci		*ppos += len;
3398c2ecf20Sopenharmony_ci		ubuf += len;
3408c2ecf20Sopenharmony_ci		todo -= len;
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	return count;
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic const struct file_operations lpc_fops = {
3478c2ecf20Sopenharmony_ci	.read =		lpc_debug_read,
3488c2ecf20Sopenharmony_ci	.write =	lpc_debug_write,
3498c2ecf20Sopenharmony_ci	.open =		simple_open,
3508c2ecf20Sopenharmony_ci	.llseek =	default_llseek,
3518c2ecf20Sopenharmony_ci};
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic int opal_lpc_debugfs_create_type(struct dentry *folder,
3548c2ecf20Sopenharmony_ci					const char *fname,
3558c2ecf20Sopenharmony_ci					enum OpalLPCAddressType type)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	struct lpc_debugfs_entry *entry;
3588c2ecf20Sopenharmony_ci	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3598c2ecf20Sopenharmony_ci	if (!entry)
3608c2ecf20Sopenharmony_ci		return -ENOMEM;
3618c2ecf20Sopenharmony_ci	entry->lpc_type = type;
3628c2ecf20Sopenharmony_ci	debugfs_create_file(fname, 0600, folder, entry, &lpc_fops);
3638c2ecf20Sopenharmony_ci	return 0;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic int opal_lpc_init_debugfs(void)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	struct dentry *root;
3698c2ecf20Sopenharmony_ci	int rc = 0;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0)
3728c2ecf20Sopenharmony_ci		return -ENODEV;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	root = debugfs_create_dir("lpc", powerpc_debugfs_root);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	rc |= opal_lpc_debugfs_create_type(root, "io", OPAL_LPC_IO);
3778c2ecf20Sopenharmony_ci	rc |= opal_lpc_debugfs_create_type(root, "mem", OPAL_LPC_MEM);
3788c2ecf20Sopenharmony_ci	rc |= opal_lpc_debugfs_create_type(root, "fw", OPAL_LPC_FW);
3798c2ecf20Sopenharmony_ci	return rc;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_cimachine_device_initcall(powernv, opal_lpc_init_debugfs);
3828c2ecf20Sopenharmony_ci#endif  /* CONFIG_DEBUG_FS */
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_civoid __init opal_lpc_init(void)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	struct device_node *np;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/*
3898c2ecf20Sopenharmony_ci	 * Look for a Power8 LPC bus tagged as "primary",
3908c2ecf20Sopenharmony_ci	 * we currently support only one though the OPAL APIs
3918c2ecf20Sopenharmony_ci	 * support any number.
3928c2ecf20Sopenharmony_ci	 */
3938c2ecf20Sopenharmony_ci	for_each_compatible_node(np, NULL, "ibm,power8-lpc") {
3948c2ecf20Sopenharmony_ci		if (!of_device_is_available(np))
3958c2ecf20Sopenharmony_ci			continue;
3968c2ecf20Sopenharmony_ci		if (!of_get_property(np, "primary", NULL))
3978c2ecf20Sopenharmony_ci			continue;
3988c2ecf20Sopenharmony_ci		opal_lpc_chip_id = of_get_ibm_chip_id(np);
3998c2ecf20Sopenharmony_ci		of_node_put(np);
4008c2ecf20Sopenharmony_ci		break;
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci	if (opal_lpc_chip_id < 0)
4038c2ecf20Sopenharmony_ci		return;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	/* Does it support direct mapping ? */
4068c2ecf20Sopenharmony_ci	if (of_get_property(np, "ranges", NULL)) {
4078c2ecf20Sopenharmony_ci		pr_info("OPAL: Found memory mapped LPC bus on chip %d\n",
4088c2ecf20Sopenharmony_ci			opal_lpc_chip_id);
4098c2ecf20Sopenharmony_ci		isa_bridge_init_non_pci(np);
4108c2ecf20Sopenharmony_ci	} else {
4118c2ecf20Sopenharmony_ci		pr_info("OPAL: Found non-mapped LPC bus on chip %d\n",
4128c2ecf20Sopenharmony_ci			opal_lpc_chip_id);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci		/* Setup special IO ops */
4158c2ecf20Sopenharmony_ci		ppc_pci_io = opal_lpc_io;
4168c2ecf20Sopenharmony_ci		isa_io_special = true;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci}
419