Skip to content

Commit 9ab5465

Browse files
Kumaravel Thiagarajangregkh
authored andcommitted
misc: microchip: pci1xxxx: Add support to read and write into PCI1XXXX EEPROM via NVMEM sysfs
Microchip's pci1xxxx is an unmanaged PCIe3.1a switch for consumer, industrial, and automotive applications. This switch integrates OTP and EEPROM to enable customization of the part in the field. This patch adds support to read and write into PCI1XXXX EEPROM via NVMEM sysfs. Signed-off-by: Kumaravel Thiagarajan <kumaravel.thiagarajan@microchip.com> Co-developed-by: Tharun Kumar P <tharunkumar.pasumarthi@microchip.com> Signed-off-by: Tharun Kumar P <tharunkumar.pasumarthi@microchip.com> Co-developed-by: Vaibhaav Ram T.L <vaibhaavram.tl@microchip.com> Signed-off-by: Vaibhaav Ram T.L <vaibhaavram.tl@microchip.com> Link: https://lore.kernel.org/r/20230620143520.858-3-vaibhaavram.tl@microchip.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0969001 commit 9ab5465

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

drivers/misc/mchp_pci1xxxx/mchp_pci1xxxx_otpe2p.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,30 @@
1111
#include "mchp_pci1xxxx_gp.h"
1212

1313
#define AUX_DRIVER_NAME "PCI1xxxxOTPE2P"
14+
#define EEPROM_NAME "pci1xxxx_eeprom"
1415
#define OTP_NAME "pci1xxxx_otp"
1516

1617
#define PERI_PF3_SYSTEM_REG_ADDR_BASE 0x2000
1718
#define PERI_PF3_SYSTEM_REG_LENGTH 0x4000
1819

20+
#define EEPROM_SIZE_BYTES 8192
1921
#define OTP_SIZE_BYTES 8192
2022

2123
#define CONFIG_REG_ADDR_BASE 0
24+
#define EEPROM_REG_ADDR_BASE 0x0E00
2225
#define OTP_REG_ADDR_BASE 0x1000
2326

2427
#define MMAP_OTP_OFFSET(x) (OTP_REG_ADDR_BASE + (x))
28+
#define MMAP_EEPROM_OFFSET(x) (EEPROM_REG_ADDR_BASE + (x))
2529
#define MMAP_CFG_OFFSET(x) (CONFIG_REG_ADDR_BASE + (x))
2630

31+
#define EEPROM_CMD_REG 0x00
32+
#define EEPROM_DATA_REG 0x04
33+
34+
#define EEPROM_CMD_EPC_WRITE (BIT(29) | BIT(28))
35+
#define EEPROM_CMD_EPC_TIMEOUT_BIT BIT(17)
36+
#define EEPROM_CMD_EPC_BUSY_BIT BIT(31)
37+
2738
#define STATUS_READ_DELAY_US 1
2839
#define STATUS_READ_TIMEOUT_US 20000
2940

@@ -56,6 +67,8 @@
5667
struct pci1xxxx_otp_eeprom_device {
5768
struct auxiliary_device *pdev;
5869
void __iomem *reg_base;
70+
struct nvmem_config nvmem_config_eeprom;
71+
struct nvmem_device *nvmem_eeprom;
5972
struct nvmem_config nvmem_config_otp;
6073
struct nvmem_device *nvmem_otp;
6174
};
@@ -81,6 +94,115 @@ static void release_sys_lock(struct pci1xxxx_otp_eeprom_device *priv)
8194
writel(0, sys_lock);
8295
}
8396

97+
static bool is_eeprom_responsive(struct pci1xxxx_otp_eeprom_device *priv)
98+
{
99+
void __iomem *rb = priv->reg_base;
100+
u32 regval;
101+
int ret;
102+
103+
writel(EEPROM_CMD_EPC_TIMEOUT_BIT,
104+
rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
105+
writel(EEPROM_CMD_EPC_BUSY_BIT,
106+
rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
107+
108+
/* Wait for the EPC_BUSY bit to get cleared or timeout bit to get set*/
109+
ret = read_poll_timeout(readl, regval, !(regval & EEPROM_CMD_EPC_BUSY_BIT),
110+
STATUS_READ_DELAY_US, STATUS_READ_TIMEOUT_US,
111+
true, rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
112+
113+
/* Return failure if either of software or hardware timeouts happen */
114+
if (ret < 0 || (!ret && (regval & EEPROM_CMD_EPC_TIMEOUT_BIT)))
115+
return false;
116+
117+
return true;
118+
}
119+
120+
static int pci1xxxx_eeprom_read(void *priv_t, unsigned int off,
121+
void *buf_t, size_t count)
122+
{
123+
struct pci1xxxx_otp_eeprom_device *priv = priv_t;
124+
void __iomem *rb = priv->reg_base;
125+
char *buf = buf_t;
126+
u32 regval;
127+
u32 byte;
128+
int ret;
129+
130+
if (off >= priv->nvmem_config_eeprom.size)
131+
return -EFAULT;
132+
133+
if ((off + count) > priv->nvmem_config_eeprom.size)
134+
count = priv->nvmem_config_eeprom.size - off;
135+
136+
ret = set_sys_lock(priv);
137+
if (ret)
138+
return ret;
139+
140+
for (byte = 0; byte < count; byte++) {
141+
writel(EEPROM_CMD_EPC_BUSY_BIT | (off + byte), rb +
142+
MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
143+
144+
ret = read_poll_timeout(readl, regval,
145+
!(regval & EEPROM_CMD_EPC_BUSY_BIT),
146+
STATUS_READ_DELAY_US,
147+
STATUS_READ_TIMEOUT_US, true,
148+
rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
149+
if (ret < 0 || (!ret && (regval & EEPROM_CMD_EPC_TIMEOUT_BIT))) {
150+
ret = -EIO;
151+
goto error;
152+
}
153+
154+
buf[byte] = readl(rb + MMAP_EEPROM_OFFSET(EEPROM_DATA_REG));
155+
}
156+
ret = byte;
157+
error:
158+
release_sys_lock(priv);
159+
return ret;
160+
}
161+
162+
static int pci1xxxx_eeprom_write(void *priv_t, unsigned int off,
163+
void *value_t, size_t count)
164+
{
165+
struct pci1xxxx_otp_eeprom_device *priv = priv_t;
166+
void __iomem *rb = priv->reg_base;
167+
char *value = value_t;
168+
u32 regval;
169+
u32 byte;
170+
int ret;
171+
172+
if (off >= priv->nvmem_config_eeprom.size)
173+
return -EFAULT;
174+
175+
if ((off + count) > priv->nvmem_config_eeprom.size)
176+
count = priv->nvmem_config_eeprom.size - off;
177+
178+
ret = set_sys_lock(priv);
179+
if (ret)
180+
return ret;
181+
182+
for (byte = 0; byte < count; byte++) {
183+
writel(*(value + byte), rb + MMAP_EEPROM_OFFSET(EEPROM_DATA_REG));
184+
regval = EEPROM_CMD_EPC_TIMEOUT_BIT | EEPROM_CMD_EPC_WRITE |
185+
(off + byte);
186+
writel(regval, rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
187+
writel(EEPROM_CMD_EPC_BUSY_BIT | regval,
188+
rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
189+
190+
ret = read_poll_timeout(readl, regval,
191+
!(regval & EEPROM_CMD_EPC_BUSY_BIT),
192+
STATUS_READ_DELAY_US,
193+
STATUS_READ_TIMEOUT_US, true,
194+
rb + MMAP_EEPROM_OFFSET(EEPROM_CMD_REG));
195+
if (ret < 0 || (!ret && (regval & EEPROM_CMD_EPC_TIMEOUT_BIT))) {
196+
ret = -EIO;
197+
goto error;
198+
}
199+
}
200+
ret = byte;
201+
error:
202+
release_sys_lock(priv);
203+
return ret;
204+
}
205+
84206
static void otp_device_set_address(struct pci1xxxx_otp_eeprom_device *priv,
85207
u16 address)
86208
{
@@ -243,6 +365,24 @@ static int pci1xxxx_otp_eeprom_probe(struct auxiliary_device *aux_dev,
243365

244366
dev_set_drvdata(&aux_dev->dev, priv);
245367

368+
if (is_eeprom_responsive(priv)) {
369+
priv->nvmem_config_eeprom.type = NVMEM_TYPE_EEPROM;
370+
priv->nvmem_config_eeprom.name = EEPROM_NAME;
371+
priv->nvmem_config_eeprom.dev = &aux_dev->dev;
372+
priv->nvmem_config_eeprom.owner = THIS_MODULE;
373+
priv->nvmem_config_eeprom.reg_read = pci1xxxx_eeprom_read;
374+
priv->nvmem_config_eeprom.reg_write = pci1xxxx_eeprom_write;
375+
priv->nvmem_config_eeprom.priv = priv;
376+
priv->nvmem_config_eeprom.stride = 1;
377+
priv->nvmem_config_eeprom.word_size = 1;
378+
priv->nvmem_config_eeprom.size = EEPROM_SIZE_BYTES;
379+
380+
priv->nvmem_eeprom = devm_nvmem_register(&aux_dev->dev,
381+
&priv->nvmem_config_eeprom);
382+
if (!priv->nvmem_eeprom)
383+
return -ENOMEM;
384+
}
385+
246386
release_sys_lock(priv);
247387

248388
priv->nvmem_config_otp.type = NVMEM_TYPE_OTP;

0 commit comments

Comments
 (0)