806-v6.0-0001-nvmem-microchip-otpc-add-support.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. From 98830350d3fc824c1ff5c338140fe20f041a5916 Mon Sep 17 00:00:00 2001
  2. From: Claudiu Beznea <[email protected]>
  3. Date: Wed, 6 Jul 2022 11:06:22 +0100
  4. Subject: [PATCH] nvmem: microchip-otpc: add support
  5. Add support for Microchip OTP controller available on SAMA7G5. The OTPC
  6. controls the access to a non-volatile memory. The memory behind OTPC is
  7. organized into packets, packets are composed by a fixed length header
  8. (4 bytes long) and a variable length payload (payload length is available
  9. in the header). When software request the data at an offset in memory
  10. the OTPC will return (via header + data registers) the whole packet that
  11. has a word at that offset. For the OTP memory layout like below:
  12. offset OTP Memory layout
  13. . .
  14. . ... .
  15. . .
  16. 0x0E +-----------+ <--- packet X
  17. | header X |
  18. 0x12 +-----------+
  19. | payload X |
  20. 0x16 | |
  21. | |
  22. 0x1A | |
  23. +-----------+
  24. . .
  25. . ... .
  26. . .
  27. if user requests data at address 0x16 the data started at 0x0E will be
  28. returned by controller. User will be able to fetch the whole packet
  29. starting at 0x0E (or parts of the packet) via proper registers. The same
  30. packet will be returned if software request the data at offset 0x0E or
  31. 0x12 or 0x1A.
  32. The OTP will be populated by Microchip with at least 2 packets first one
  33. being boot configuration packet and the 2nd one being temperature
  34. calibration packet. The packet order will be preserved b/w different chip
  35. revisions but the packet sizes may change.
  36. For the above reasons and to keep the same software able to work on all
  37. chip variants the read function of the driver is working with a packet
  38. id instead of an offset in OTP memory.
  39. Signed-off-by: Claudiu Beznea <[email protected]>
  40. Signed-off-by: Srinivas Kandagatla <[email protected]>
  41. Link: https://lore.kernel.org/r/[email protected]
  42. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  43. ---
  44. MAINTAINERS | 8 +
  45. drivers/nvmem/Kconfig | 7 +
  46. drivers/nvmem/Makefile | 2 +
  47. drivers/nvmem/microchip-otpc.c | 288 +++++++++++++++++++++++++++++++++
  48. 4 files changed, 305 insertions(+)
  49. create mode 100644 drivers/nvmem/microchip-otpc.c
  50. --- a/MAINTAINERS
  51. +++ b/MAINTAINERS
  52. @@ -12364,6 +12364,14 @@ S: Supported
  53. F: Documentation/devicetree/bindings/mtd/atmel-nand.txt
  54. F: drivers/mtd/nand/raw/atmel/*
  55. +MICROCHIP OTPC DRIVER
  56. +M: Claudiu Beznea <[email protected]>
  57. +L: [email protected] (moderated for non-subscribers)
  58. +S: Supported
  59. +F: Documentation/devicetree/bindings/nvmem/microchip,sama7g5-otpc.yaml
  60. +F: drivers/nvmem/microchip-otpc.c
  61. +F: dt-bindings/nvmem/microchip,sama7g5-otpc.h
  62. +
  63. MICROCHIP PWM DRIVER
  64. M: Claudiu Beznea <[email protected]>
  65. L: [email protected] (moderated for non-subscribers)
  66. --- a/drivers/nvmem/Kconfig
  67. +++ b/drivers/nvmem/Kconfig
  68. @@ -107,6 +107,13 @@ config MTK_EFUSE
  69. This driver can also be built as a module. If so, the module
  70. will be called efuse-mtk.
  71. +config MICROCHIP_OTPC
  72. + tristate "Microchip OTPC support"
  73. + depends on ARCH_AT91 || COMPILE_TEST
  74. + help
  75. + This driver enable the OTP controller available on Microchip SAMA7G5
  76. + SoCs. It controlls the access to the OTP memory connected to it.
  77. +
  78. config NVMEM_NINTENDO_OTP
  79. tristate "Nintendo Wii and Wii U OTP Support"
  80. depends on WII || COMPILE_TEST
  81. --- a/drivers/nvmem/Makefile
  82. +++ b/drivers/nvmem/Makefile
  83. @@ -67,3 +67,5 @@ obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP) += nvm
  84. nvmem_sunplus_ocotp-y := sunplus-ocotp.o
  85. obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvmem-apple-efuses.o
  86. nvmem-apple-efuses-y := apple-efuses.o
  87. +obj-$(CONFIG_MICROCHIP_OTPC) += nvmem-microchip-otpc.o
  88. +nvmem-microchip-otpc-y := microchip-otpc.o
  89. --- /dev/null
  90. +++ b/drivers/nvmem/microchip-otpc.c
  91. @@ -0,0 +1,288 @@
  92. +// SPDX-License-Identifier: GPL-2.0
  93. +/*
  94. + * OTP Memory controller
  95. + *
  96. + * Copyright (C) 2022 Microchip Technology Inc. and its subsidiaries
  97. + *
  98. + * Author: Claudiu Beznea <[email protected]>
  99. + */
  100. +
  101. +#include <linux/bitfield.h>
  102. +#include <linux/iopoll.h>
  103. +#include <linux/module.h>
  104. +#include <linux/nvmem-provider.h>
  105. +#include <linux/of.h>
  106. +#include <linux/platform_device.h>
  107. +
  108. +#define MCHP_OTPC_CR (0x0)
  109. +#define MCHP_OTPC_CR_READ BIT(6)
  110. +#define MCHP_OTPC_MR (0x4)
  111. +#define MCHP_OTPC_MR_ADDR GENMASK(31, 16)
  112. +#define MCHP_OTPC_AR (0x8)
  113. +#define MCHP_OTPC_SR (0xc)
  114. +#define MCHP_OTPC_SR_READ BIT(6)
  115. +#define MCHP_OTPC_HR (0x20)
  116. +#define MCHP_OTPC_HR_SIZE GENMASK(15, 8)
  117. +#define MCHP_OTPC_DR (0x24)
  118. +
  119. +#define MCHP_OTPC_NAME "mchp-otpc"
  120. +#define MCHP_OTPC_SIZE (11 * 1024)
  121. +
  122. +/**
  123. + * struct mchp_otpc - OTPC private data structure
  124. + * @base: base address
  125. + * @dev: struct device pointer
  126. + * @packets: list of packets in OTP memory
  127. + * @npackets: number of packets in OTP memory
  128. + */
  129. +struct mchp_otpc {
  130. + void __iomem *base;
  131. + struct device *dev;
  132. + struct list_head packets;
  133. + u32 npackets;
  134. +};
  135. +
  136. +/**
  137. + * struct mchp_otpc_packet - OTPC packet data structure
  138. + * @list: list head
  139. + * @id: packet ID
  140. + * @offset: packet offset (in words) in OTP memory
  141. + */
  142. +struct mchp_otpc_packet {
  143. + struct list_head list;
  144. + u32 id;
  145. + u32 offset;
  146. +};
  147. +
  148. +static struct mchp_otpc_packet *mchp_otpc_id_to_packet(struct mchp_otpc *otpc,
  149. + u32 id)
  150. +{
  151. + struct mchp_otpc_packet *packet;
  152. +
  153. + if (id >= otpc->npackets)
  154. + return NULL;
  155. +
  156. + list_for_each_entry(packet, &otpc->packets, list) {
  157. + if (packet->id == id)
  158. + return packet;
  159. + }
  160. +
  161. + return NULL;
  162. +}
  163. +
  164. +static int mchp_otpc_prepare_read(struct mchp_otpc *otpc,
  165. + unsigned int offset)
  166. +{
  167. + u32 tmp;
  168. +
  169. + /* Set address. */
  170. + tmp = readl_relaxed(otpc->base + MCHP_OTPC_MR);
  171. + tmp &= ~MCHP_OTPC_MR_ADDR;
  172. + tmp |= FIELD_PREP(MCHP_OTPC_MR_ADDR, offset);
  173. + writel_relaxed(tmp, otpc->base + MCHP_OTPC_MR);
  174. +
  175. + /* Set read. */
  176. + tmp = readl_relaxed(otpc->base + MCHP_OTPC_CR);
  177. + tmp |= MCHP_OTPC_CR_READ;
  178. + writel_relaxed(tmp, otpc->base + MCHP_OTPC_CR);
  179. +
  180. + /* Wait for packet to be transferred into temporary buffers. */
  181. + return read_poll_timeout(readl_relaxed, tmp, !(tmp & MCHP_OTPC_SR_READ),
  182. + 10000, 2000, false, otpc->base + MCHP_OTPC_SR);
  183. +}
  184. +
  185. +/*
  186. + * OTPC memory is organized into packets. Each packets contains a header and
  187. + * a payload. Header is 4 bytes long and contains the size of the payload.
  188. + * Payload size varies. The memory footprint is something as follows:
  189. + *
  190. + * Memory offset Memory footprint Packet ID
  191. + * ------------- ---------------- ---------
  192. + *
  193. + * 0x0 +------------+ <-- packet 0
  194. + * | header 0 |
  195. + * 0x4 +------------+
  196. + * | payload 0 |
  197. + * . .
  198. + * . ... .
  199. + * . .
  200. + * offset1 +------------+ <-- packet 1
  201. + * | header 1 |
  202. + * offset1 + 0x4 +------------+
  203. + * | payload 1 |
  204. + * . .
  205. + * . ... .
  206. + * . .
  207. + * offset2 +------------+ <-- packet 2
  208. + * . .
  209. + * . ... .
  210. + * . .
  211. + * offsetN +------------+ <-- packet N
  212. + * | header N |
  213. + * offsetN + 0x4 +------------+
  214. + * | payload N |
  215. + * . .
  216. + * . ... .
  217. + * . .
  218. + * +------------+
  219. + *
  220. + * where offset1, offset2, offsetN depends on the size of payload 0, payload 1,
  221. + * payload N-1.
  222. + *
  223. + * The access to memory is done on a per packet basis: the control registers
  224. + * need to be updated with an offset address (within a packet range) and the
  225. + * data registers will be update by controller with information contained by
  226. + * that packet. E.g. if control registers are updated with any address within
  227. + * the range [offset1, offset2) the data registers are updated by controller
  228. + * with packet 1. Header data is accessible though MCHP_OTPC_HR register.
  229. + * Payload data is accessible though MCHP_OTPC_DR and MCHP_OTPC_AR registers.
  230. + * There is no direct mapping b/w the offset requested by software and the
  231. + * offset returned by hardware.
  232. + *
  233. + * For this, the read function will return the first requested bytes in the
  234. + * packet. The user will have to be aware of the memory footprint before doing
  235. + * the read request.
  236. + */
  237. +static int mchp_otpc_read(void *priv, unsigned int off, void *val,
  238. + size_t bytes)
  239. +{
  240. + struct mchp_otpc *otpc = priv;
  241. + struct mchp_otpc_packet *packet;
  242. + u32 *buf = val;
  243. + u32 offset;
  244. + size_t len = 0;
  245. + int ret, payload_size;
  246. +
  247. + /*
  248. + * We reach this point with off being multiple of stride = 4 to
  249. + * be able to cross the subsystem. Inside the driver we use continuous
  250. + * unsigned integer numbers for packet id, thus devide off by 4
  251. + * before passing it to mchp_otpc_id_to_packet().
  252. + */
  253. + packet = mchp_otpc_id_to_packet(otpc, off / 4);
  254. + if (!packet)
  255. + return -EINVAL;
  256. + offset = packet->offset;
  257. +
  258. + while (len < bytes) {
  259. + ret = mchp_otpc_prepare_read(otpc, offset);
  260. + if (ret)
  261. + return ret;
  262. +
  263. + /* Read and save header content. */
  264. + *buf++ = readl_relaxed(otpc->base + MCHP_OTPC_HR);
  265. + len += sizeof(*buf);
  266. + offset++;
  267. + if (len >= bytes)
  268. + break;
  269. +
  270. + /* Read and save payload content. */
  271. + payload_size = FIELD_GET(MCHP_OTPC_HR_SIZE, *(buf - 1));
  272. + writel_relaxed(0UL, otpc->base + MCHP_OTPC_AR);
  273. + do {
  274. + *buf++ = readl_relaxed(otpc->base + MCHP_OTPC_DR);
  275. + len += sizeof(*buf);
  276. + offset++;
  277. + payload_size--;
  278. + } while (payload_size >= 0 && len < bytes);
  279. + }
  280. +
  281. + return 0;
  282. +}
  283. +
  284. +static int mchp_otpc_init_packets_list(struct mchp_otpc *otpc, u32 *size)
  285. +{
  286. + struct mchp_otpc_packet *packet;
  287. + u32 word, word_pos = 0, id = 0, npackets = 0, payload_size;
  288. + int ret;
  289. +
  290. + INIT_LIST_HEAD(&otpc->packets);
  291. + *size = 0;
  292. +
  293. + while (*size < MCHP_OTPC_SIZE) {
  294. + ret = mchp_otpc_prepare_read(otpc, word_pos);
  295. + if (ret)
  296. + return ret;
  297. +
  298. + word = readl_relaxed(otpc->base + MCHP_OTPC_HR);
  299. + payload_size = FIELD_GET(MCHP_OTPC_HR_SIZE, word);
  300. + if (!payload_size)
  301. + break;
  302. +
  303. + packet = devm_kzalloc(otpc->dev, sizeof(*packet), GFP_KERNEL);
  304. + if (!packet)
  305. + return -ENOMEM;
  306. +
  307. + packet->id = id++;
  308. + packet->offset = word_pos;
  309. + INIT_LIST_HEAD(&packet->list);
  310. + list_add_tail(&packet->list, &otpc->packets);
  311. +
  312. + /* Count size by adding header and paload sizes. */
  313. + *size += 4 * (payload_size + 1);
  314. + /* Next word: this packet (header, payload) position + 1. */
  315. + word_pos += payload_size + 2;
  316. +
  317. + npackets++;
  318. + }
  319. +
  320. + otpc->npackets = npackets;
  321. +
  322. + return 0;
  323. +}
  324. +
  325. +static struct nvmem_config mchp_nvmem_config = {
  326. + .name = MCHP_OTPC_NAME,
  327. + .type = NVMEM_TYPE_OTP,
  328. + .read_only = true,
  329. + .word_size = 4,
  330. + .stride = 4,
  331. + .reg_read = mchp_otpc_read,
  332. +};
  333. +
  334. +static int mchp_otpc_probe(struct platform_device *pdev)
  335. +{
  336. + struct nvmem_device *nvmem;
  337. + struct mchp_otpc *otpc;
  338. + u32 size;
  339. + int ret;
  340. +
  341. + otpc = devm_kzalloc(&pdev->dev, sizeof(*otpc), GFP_KERNEL);
  342. + if (!otpc)
  343. + return -ENOMEM;
  344. +
  345. + otpc->base = devm_platform_ioremap_resource(pdev, 0);
  346. + if (IS_ERR(otpc->base))
  347. + return PTR_ERR(otpc->base);
  348. +
  349. + otpc->dev = &pdev->dev;
  350. + ret = mchp_otpc_init_packets_list(otpc, &size);
  351. + if (ret)
  352. + return ret;
  353. +
  354. + mchp_nvmem_config.dev = otpc->dev;
  355. + mchp_nvmem_config.size = size;
  356. + mchp_nvmem_config.priv = otpc;
  357. + nvmem = devm_nvmem_register(&pdev->dev, &mchp_nvmem_config);
  358. +
  359. + return PTR_ERR_OR_ZERO(nvmem);
  360. +}
  361. +
  362. +static const struct of_device_id __maybe_unused mchp_otpc_ids[] = {
  363. + { .compatible = "microchip,sama7g5-otpc", },
  364. + { },
  365. +};
  366. +MODULE_DEVICE_TABLE(of, mchp_otpc_ids);
  367. +
  368. +static struct platform_driver mchp_otpc_driver = {
  369. + .probe = mchp_otpc_probe,
  370. + .driver = {
  371. + .name = MCHP_OTPC_NAME,
  372. + .of_match_table = of_match_ptr(mchp_otpc_ids),
  373. + },
  374. +};
  375. +module_platform_driver(mchp_otpc_driver);
  376. +
  377. +MODULE_AUTHOR("Claudiu Beznea <[email protected]>");
  378. +MODULE_DESCRIPTION("Microchip SAMA7G5 OTPC driver");
  379. +MODULE_LICENSE("GPL");