Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2// Copyright (C) 2016 Broadcom
  3
  4#include <linux/acpi.h>
  5#include <linux/delay.h>
  6#include <linux/device.h>
  7#include <linux/io.h>
  8#include <linux/module.h>
  9#include <linux/nvmem-provider.h>
 10#include <linux/of.h>
 
 11#include <linux/platform_device.h>
 12
 13/*
 14 * # of tries for OTP Status. The time to execute a command varies. The slowest
 15 * commands are writes which also vary based on the # of bits turned on. Writing
 16 * 0xffffffff takes ~3800 us.
 17 */
 18#define OTPC_RETRIES                 5000
 19
 20/* Sequence to enable OTP program */
 21#define OTPC_PROG_EN_SEQ             { 0xf, 0x4, 0x8, 0xd }
 22
 23/* OTPC Commands */
 24#define OTPC_CMD_READ                0x0
 25#define OTPC_CMD_OTP_PROG_ENABLE     0x2
 26#define OTPC_CMD_OTP_PROG_DISABLE    0x3
 27#define OTPC_CMD_PROGRAM             0x8
 28
 29/* OTPC Status Bits */
 30#define OTPC_STAT_CMD_DONE           BIT(1)
 31#define OTPC_STAT_PROG_OK            BIT(2)
 32
 33/* OTPC register definition */
 34#define OTPC_MODE_REG_OFFSET         0x0
 35#define OTPC_MODE_REG_OTPC_MODE      0
 36#define OTPC_COMMAND_OFFSET          0x4
 37#define OTPC_COMMAND_COMMAND_WIDTH   6
 38#define OTPC_CMD_START_OFFSET        0x8
 39#define OTPC_CMD_START_START         0
 40#define OTPC_CPU_STATUS_OFFSET       0xc
 41#define OTPC_CPUADDR_REG_OFFSET      0x28
 42#define OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH 16
 43#define OTPC_CPU_WRITE_REG_OFFSET    0x2c
 44
 45#define OTPC_CMD_MASK  (BIT(OTPC_COMMAND_COMMAND_WIDTH) - 1)
 46#define OTPC_ADDR_MASK (BIT(OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH) - 1)
 47
 48
 49struct otpc_map {
 50	/* in words. */
 51	u32 otpc_row_size;
 52	/* 128 bit row / 4 words support. */
 53	u16 data_r_offset[4];
 54	/* 128 bit row / 4 words support. */
 55	u16 data_w_offset[4];
 56};
 57
 58static struct otpc_map otp_map = {
 59	.otpc_row_size = 1,
 60	.data_r_offset = {0x10},
 61	.data_w_offset = {0x2c},
 62};
 63
 64static struct otpc_map otp_map_v2 = {
 65	.otpc_row_size = 2,
 66	.data_r_offset = {0x10, 0x5c},
 67	.data_w_offset = {0x2c, 0x64},
 68};
 69
 70struct otpc_priv {
 71	struct device *dev;
 72	void __iomem *base;
 73	const struct otpc_map *map;
 74	struct nvmem_config *config;
 75};
 76
 77static inline void set_command(void __iomem *base, u32 command)
 78{
 79	writel(command & OTPC_CMD_MASK, base + OTPC_COMMAND_OFFSET);
 80}
 81
 82static inline void set_cpu_address(void __iomem *base, u32 addr)
 83{
 84	writel(addr & OTPC_ADDR_MASK, base + OTPC_CPUADDR_REG_OFFSET);
 85}
 86
 87static inline void set_start_bit(void __iomem *base)
 88{
 89	writel(1 << OTPC_CMD_START_START, base + OTPC_CMD_START_OFFSET);
 90}
 91
 92static inline void reset_start_bit(void __iomem *base)
 93{
 94	writel(0, base + OTPC_CMD_START_OFFSET);
 95}
 96
 97static inline void write_cpu_data(void __iomem *base, u32 value)
 98{
 99	writel(value, base + OTPC_CPU_WRITE_REG_OFFSET);
100}
101
102static int poll_cpu_status(void __iomem *base, u32 value)
103{
104	u32 status;
105	u32 retries;
106
107	for (retries = 0; retries < OTPC_RETRIES; retries++) {
108		status = readl(base + OTPC_CPU_STATUS_OFFSET);
109		if (status & value)
110			break;
111		udelay(1);
112	}
113	if (retries == OTPC_RETRIES)
114		return -EAGAIN;
115
116	return 0;
117}
118
119static int enable_ocotp_program(void __iomem *base)
120{
121	static const u32 vals[] = OTPC_PROG_EN_SEQ;
122	int i;
123	int ret;
124
125	/* Write the magic sequence to enable programming */
126	set_command(base, OTPC_CMD_OTP_PROG_ENABLE);
127	for (i = 0; i < ARRAY_SIZE(vals); i++) {
128		write_cpu_data(base, vals[i]);
129		set_start_bit(base);
130		ret = poll_cpu_status(base, OTPC_STAT_CMD_DONE);
131		reset_start_bit(base);
132		if (ret)
133			return ret;
134	}
135
136	return poll_cpu_status(base, OTPC_STAT_PROG_OK);
137}
138
139static int disable_ocotp_program(void __iomem *base)
140{
141	int ret;
142
143	set_command(base, OTPC_CMD_OTP_PROG_DISABLE);
144	set_start_bit(base);
145	ret = poll_cpu_status(base, OTPC_STAT_PROG_OK);
146	reset_start_bit(base);
147
148	return ret;
149}
150
151static int bcm_otpc_read(void *context, unsigned int offset, void *val,
152	size_t bytes)
153{
154	struct otpc_priv *priv = context;
155	u32 *buf = val;
156	u32 bytes_read;
157	u32 address = offset / priv->config->word_size;
158	int i, ret;
159
160	for (bytes_read = 0; bytes_read < bytes;) {
161		set_command(priv->base, OTPC_CMD_READ);
162		set_cpu_address(priv->base, address++);
163		set_start_bit(priv->base);
164		ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
165		if (ret) {
166			dev_err(priv->dev, "otp read error: 0x%x", ret);
167			return -EIO;
168		}
169
170		for (i = 0; i < priv->map->otpc_row_size; i++) {
171			*buf++ = readl(priv->base +
172					priv->map->data_r_offset[i]);
173			bytes_read += sizeof(*buf);
174		}
175
176		reset_start_bit(priv->base);
177	}
178
179	return 0;
180}
181
182static int bcm_otpc_write(void *context, unsigned int offset, void *val,
183	size_t bytes)
184{
185	struct otpc_priv *priv = context;
186	u32 *buf = val;
187	u32 bytes_written;
188	u32 address = offset / priv->config->word_size;
189	int i, ret;
190
191	if (offset % priv->config->word_size)
192		return -EINVAL;
193
194	ret = enable_ocotp_program(priv->base);
195	if (ret)
196		return -EIO;
197
198	for (bytes_written = 0; bytes_written < bytes;) {
199		set_command(priv->base, OTPC_CMD_PROGRAM);
200		set_cpu_address(priv->base, address++);
201		for (i = 0; i < priv->map->otpc_row_size; i++) {
202			writel(*buf, priv->base + priv->map->data_w_offset[i]);
203			buf++;
204			bytes_written += sizeof(*buf);
205		}
206		set_start_bit(priv->base);
207		ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
208		reset_start_bit(priv->base);
209		if (ret) {
210			dev_err(priv->dev, "otp write error: 0x%x", ret);
211			return -EIO;
212		}
213	}
214
215	disable_ocotp_program(priv->base);
216
217	return 0;
218}
219
220static struct nvmem_config bcm_otpc_nvmem_config = {
221	.name = "bcm-ocotp",
222	.read_only = false,
223	.word_size = 4,
224	.stride = 4,
225	.reg_read = bcm_otpc_read,
226	.reg_write = bcm_otpc_write,
227};
228
229static const struct of_device_id bcm_otpc_dt_ids[] = {
230	{ .compatible = "brcm,ocotp", .data = &otp_map },
231	{ .compatible = "brcm,ocotp-v2", .data = &otp_map_v2 },
232	{ },
233};
234MODULE_DEVICE_TABLE(of, bcm_otpc_dt_ids);
235
236static const struct acpi_device_id bcm_otpc_acpi_ids[] __maybe_unused = {
237	{ .id = "BRCM0700", .driver_data = (kernel_ulong_t)&otp_map },
238	{ .id = "BRCM0701", .driver_data = (kernel_ulong_t)&otp_map_v2 },
239	{ /* sentinel */ }
240};
241MODULE_DEVICE_TABLE(acpi, bcm_otpc_acpi_ids);
242
243static int bcm_otpc_probe(struct platform_device *pdev)
244{
245	struct device *dev = &pdev->dev;
 
246	struct otpc_priv *priv;
247	struct nvmem_device *nvmem;
248	int err;
249	u32 num_words;
250
251	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
252	if (!priv)
253		return -ENOMEM;
254
255	priv->map = device_get_match_data(dev);
256	if (!priv->map)
257		return -ENODEV;
258
259	/* Get OTP base address register. */
260	priv->base = devm_platform_ioremap_resource(pdev, 0);
 
261	if (IS_ERR(priv->base)) {
262		dev_err(dev, "unable to map I/O memory\n");
263		return PTR_ERR(priv->base);
264	}
265
266	/* Enable CPU access to OTPC. */
267	writel(readl(priv->base + OTPC_MODE_REG_OFFSET) |
268		BIT(OTPC_MODE_REG_OTPC_MODE),
269		priv->base + OTPC_MODE_REG_OFFSET);
270	reset_start_bit(priv->base);
271
272	/* Read size of memory in words. */
273	err = device_property_read_u32(dev, "brcm,ocotp-size", &num_words);
274	if (err) {
275		dev_err(dev, "size parameter not specified\n");
276		return -EINVAL;
277	} else if (num_words == 0) {
278		dev_err(dev, "size must be > 0\n");
279		return -EINVAL;
280	}
281
282	bcm_otpc_nvmem_config.size = 4 * num_words;
283	bcm_otpc_nvmem_config.dev = dev;
284	bcm_otpc_nvmem_config.priv = priv;
285
286	if (priv->map == &otp_map_v2) {
287		bcm_otpc_nvmem_config.word_size = 8;
288		bcm_otpc_nvmem_config.stride = 8;
289	}
290
291	priv->config = &bcm_otpc_nvmem_config;
292
293	nvmem = devm_nvmem_register(dev, &bcm_otpc_nvmem_config);
294	if (IS_ERR(nvmem)) {
295		dev_err(dev, "error registering nvmem config\n");
296		return PTR_ERR(nvmem);
297	}
298
299	return 0;
300}
301
302static struct platform_driver bcm_otpc_driver = {
303	.probe	= bcm_otpc_probe,
304	.driver = {
305		.name	= "brcm-otpc",
306		.of_match_table = bcm_otpc_dt_ids,
307		.acpi_match_table = ACPI_PTR(bcm_otpc_acpi_ids),
308	},
309};
310module_platform_driver(bcm_otpc_driver);
311
312MODULE_DESCRIPTION("Broadcom OTPC driver");
313MODULE_LICENSE("GPL v2");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2// Copyright (C) 2016 Broadcom
  3
  4#include <linux/acpi.h>
  5#include <linux/delay.h>
  6#include <linux/device.h>
  7#include <linux/io.h>
  8#include <linux/module.h>
  9#include <linux/nvmem-provider.h>
 10#include <linux/of.h>
 11#include <linux/of_device.h>
 12#include <linux/platform_device.h>
 13
 14/*
 15 * # of tries for OTP Status. The time to execute a command varies. The slowest
 16 * commands are writes which also vary based on the # of bits turned on. Writing
 17 * 0xffffffff takes ~3800 us.
 18 */
 19#define OTPC_RETRIES                 5000
 20
 21/* Sequence to enable OTP program */
 22#define OTPC_PROG_EN_SEQ             { 0xf, 0x4, 0x8, 0xd }
 23
 24/* OTPC Commands */
 25#define OTPC_CMD_READ                0x0
 26#define OTPC_CMD_OTP_PROG_ENABLE     0x2
 27#define OTPC_CMD_OTP_PROG_DISABLE    0x3
 28#define OTPC_CMD_PROGRAM             0x8
 29
 30/* OTPC Status Bits */
 31#define OTPC_STAT_CMD_DONE           BIT(1)
 32#define OTPC_STAT_PROG_OK            BIT(2)
 33
 34/* OTPC register definition */
 35#define OTPC_MODE_REG_OFFSET         0x0
 36#define OTPC_MODE_REG_OTPC_MODE      0
 37#define OTPC_COMMAND_OFFSET          0x4
 38#define OTPC_COMMAND_COMMAND_WIDTH   6
 39#define OTPC_CMD_START_OFFSET        0x8
 40#define OTPC_CMD_START_START         0
 41#define OTPC_CPU_STATUS_OFFSET       0xc
 42#define OTPC_CPUADDR_REG_OFFSET      0x28
 43#define OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH 16
 44#define OTPC_CPU_WRITE_REG_OFFSET    0x2c
 45
 46#define OTPC_CMD_MASK  (BIT(OTPC_COMMAND_COMMAND_WIDTH) - 1)
 47#define OTPC_ADDR_MASK (BIT(OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH) - 1)
 48
 49
 50struct otpc_map {
 51	/* in words. */
 52	u32 otpc_row_size;
 53	/* 128 bit row / 4 words support. */
 54	u16 data_r_offset[4];
 55	/* 128 bit row / 4 words support. */
 56	u16 data_w_offset[4];
 57};
 58
 59static struct otpc_map otp_map = {
 60	.otpc_row_size = 1,
 61	.data_r_offset = {0x10},
 62	.data_w_offset = {0x2c},
 63};
 64
 65static struct otpc_map otp_map_v2 = {
 66	.otpc_row_size = 2,
 67	.data_r_offset = {0x10, 0x5c},
 68	.data_w_offset = {0x2c, 0x64},
 69};
 70
 71struct otpc_priv {
 72	struct device *dev;
 73	void __iomem *base;
 74	const struct otpc_map *map;
 75	struct nvmem_config *config;
 76};
 77
 78static inline void set_command(void __iomem *base, u32 command)
 79{
 80	writel(command & OTPC_CMD_MASK, base + OTPC_COMMAND_OFFSET);
 81}
 82
 83static inline void set_cpu_address(void __iomem *base, u32 addr)
 84{
 85	writel(addr & OTPC_ADDR_MASK, base + OTPC_CPUADDR_REG_OFFSET);
 86}
 87
 88static inline void set_start_bit(void __iomem *base)
 89{
 90	writel(1 << OTPC_CMD_START_START, base + OTPC_CMD_START_OFFSET);
 91}
 92
 93static inline void reset_start_bit(void __iomem *base)
 94{
 95	writel(0, base + OTPC_CMD_START_OFFSET);
 96}
 97
 98static inline void write_cpu_data(void __iomem *base, u32 value)
 99{
100	writel(value, base + OTPC_CPU_WRITE_REG_OFFSET);
101}
102
103static int poll_cpu_status(void __iomem *base, u32 value)
104{
105	u32 status;
106	u32 retries;
107
108	for (retries = 0; retries < OTPC_RETRIES; retries++) {
109		status = readl(base + OTPC_CPU_STATUS_OFFSET);
110		if (status & value)
111			break;
112		udelay(1);
113	}
114	if (retries == OTPC_RETRIES)
115		return -EAGAIN;
116
117	return 0;
118}
119
120static int enable_ocotp_program(void __iomem *base)
121{
122	static const u32 vals[] = OTPC_PROG_EN_SEQ;
123	int i;
124	int ret;
125
126	/* Write the magic sequence to enable programming */
127	set_command(base, OTPC_CMD_OTP_PROG_ENABLE);
128	for (i = 0; i < ARRAY_SIZE(vals); i++) {
129		write_cpu_data(base, vals[i]);
130		set_start_bit(base);
131		ret = poll_cpu_status(base, OTPC_STAT_CMD_DONE);
132		reset_start_bit(base);
133		if (ret)
134			return ret;
135	}
136
137	return poll_cpu_status(base, OTPC_STAT_PROG_OK);
138}
139
140static int disable_ocotp_program(void __iomem *base)
141{
142	int ret;
143
144	set_command(base, OTPC_CMD_OTP_PROG_DISABLE);
145	set_start_bit(base);
146	ret = poll_cpu_status(base, OTPC_STAT_PROG_OK);
147	reset_start_bit(base);
148
149	return ret;
150}
151
152static int bcm_otpc_read(void *context, unsigned int offset, void *val,
153	size_t bytes)
154{
155	struct otpc_priv *priv = context;
156	u32 *buf = val;
157	u32 bytes_read;
158	u32 address = offset / priv->config->word_size;
159	int i, ret;
160
161	for (bytes_read = 0; bytes_read < bytes;) {
162		set_command(priv->base, OTPC_CMD_READ);
163		set_cpu_address(priv->base, address++);
164		set_start_bit(priv->base);
165		ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
166		if (ret) {
167			dev_err(priv->dev, "otp read error: 0x%x", ret);
168			return -EIO;
169		}
170
171		for (i = 0; i < priv->map->otpc_row_size; i++) {
172			*buf++ = readl(priv->base +
173					priv->map->data_r_offset[i]);
174			bytes_read += sizeof(*buf);
175		}
176
177		reset_start_bit(priv->base);
178	}
179
180	return 0;
181}
182
183static int bcm_otpc_write(void *context, unsigned int offset, void *val,
184	size_t bytes)
185{
186	struct otpc_priv *priv = context;
187	u32 *buf = val;
188	u32 bytes_written;
189	u32 address = offset / priv->config->word_size;
190	int i, ret;
191
192	if (offset % priv->config->word_size)
193		return -EINVAL;
194
195	ret = enable_ocotp_program(priv->base);
196	if (ret)
197		return -EIO;
198
199	for (bytes_written = 0; bytes_written < bytes;) {
200		set_command(priv->base, OTPC_CMD_PROGRAM);
201		set_cpu_address(priv->base, address++);
202		for (i = 0; i < priv->map->otpc_row_size; i++) {
203			writel(*buf, priv->base + priv->map->data_w_offset[i]);
204			buf++;
205			bytes_written += sizeof(*buf);
206		}
207		set_start_bit(priv->base);
208		ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
209		reset_start_bit(priv->base);
210		if (ret) {
211			dev_err(priv->dev, "otp write error: 0x%x", ret);
212			return -EIO;
213		}
214	}
215
216	disable_ocotp_program(priv->base);
217
218	return 0;
219}
220
221static struct nvmem_config bcm_otpc_nvmem_config = {
222	.name = "bcm-ocotp",
223	.read_only = false,
224	.word_size = 4,
225	.stride = 4,
226	.reg_read = bcm_otpc_read,
227	.reg_write = bcm_otpc_write,
228};
229
230static const struct of_device_id bcm_otpc_dt_ids[] = {
231	{ .compatible = "brcm,ocotp", .data = &otp_map },
232	{ .compatible = "brcm,ocotp-v2", .data = &otp_map_v2 },
233	{ },
234};
235MODULE_DEVICE_TABLE(of, bcm_otpc_dt_ids);
236
237static const struct acpi_device_id bcm_otpc_acpi_ids[] __maybe_unused = {
238	{ .id = "BRCM0700", .driver_data = (kernel_ulong_t)&otp_map },
239	{ .id = "BRCM0701", .driver_data = (kernel_ulong_t)&otp_map_v2 },
240	{ /* sentinel */ }
241};
242MODULE_DEVICE_TABLE(acpi, bcm_otpc_acpi_ids);
243
244static int bcm_otpc_probe(struct platform_device *pdev)
245{
246	struct device *dev = &pdev->dev;
247	struct resource *res;
248	struct otpc_priv *priv;
249	struct nvmem_device *nvmem;
250	int err;
251	u32 num_words;
252
253	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
254	if (!priv)
255		return -ENOMEM;
256
257	priv->map = device_get_match_data(dev);
258	if (!priv->map)
259		return -ENODEV;
260
261	/* Get OTP base address register. */
262	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
263	priv->base = devm_ioremap_resource(dev, res);
264	if (IS_ERR(priv->base)) {
265		dev_err(dev, "unable to map I/O memory\n");
266		return PTR_ERR(priv->base);
267	}
268
269	/* Enable CPU access to OTPC. */
270	writel(readl(priv->base + OTPC_MODE_REG_OFFSET) |
271		BIT(OTPC_MODE_REG_OTPC_MODE),
272		priv->base + OTPC_MODE_REG_OFFSET);
273	reset_start_bit(priv->base);
274
275	/* Read size of memory in words. */
276	err = device_property_read_u32(dev, "brcm,ocotp-size", &num_words);
277	if (err) {
278		dev_err(dev, "size parameter not specified\n");
279		return -EINVAL;
280	} else if (num_words == 0) {
281		dev_err(dev, "size must be > 0\n");
282		return -EINVAL;
283	}
284
285	bcm_otpc_nvmem_config.size = 4 * num_words;
286	bcm_otpc_nvmem_config.dev = dev;
287	bcm_otpc_nvmem_config.priv = priv;
288
289	if (priv->map == &otp_map_v2) {
290		bcm_otpc_nvmem_config.word_size = 8;
291		bcm_otpc_nvmem_config.stride = 8;
292	}
293
294	priv->config = &bcm_otpc_nvmem_config;
295
296	nvmem = devm_nvmem_register(dev, &bcm_otpc_nvmem_config);
297	if (IS_ERR(nvmem)) {
298		dev_err(dev, "error registering nvmem config\n");
299		return PTR_ERR(nvmem);
300	}
301
302	return 0;
303}
304
305static struct platform_driver bcm_otpc_driver = {
306	.probe	= bcm_otpc_probe,
307	.driver = {
308		.name	= "brcm-otpc",
309		.of_match_table = bcm_otpc_dt_ids,
310		.acpi_match_table = ACPI_PTR(bcm_otpc_acpi_ids),
311	},
312};
313module_platform_driver(bcm_otpc_driver);
314
315MODULE_DESCRIPTION("Broadcom OTPC driver");
316MODULE_LICENSE("GPL v2");