lpddr2_nvm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LPDDR2-NVM MTD driver. This module provides read, write, erase, lock/unlock
  4. * support for LPDDR2-NVM PCM memories
  5. *
  6. * Copyright © 2012 Micron Technology, Inc.
  7. *
  8. * Vincenzo Aliberti <[email protected]>
  9. * Domenico Manna <[email protected]>
  10. * Many thanks to Andrea Vigilante for initial enabling
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mtd/map.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/ioport.h>
  23. #include <linux/err.h>
  24. /* Parameters */
  25. #define ERASE_BLOCKSIZE (0x00020000/2) /* in Word */
  26. #define WRITE_BUFFSIZE (0x00000400/2) /* in Word */
  27. #define OW_BASE_ADDRESS 0x00000000 /* OW offset */
  28. #define BUS_WIDTH 0x00000020 /* x32 devices */
  29. /* PFOW symbols address offset */
  30. #define PFOW_QUERY_STRING_P (0x0000/2) /* in Word */
  31. #define PFOW_QUERY_STRING_F (0x0002/2) /* in Word */
  32. #define PFOW_QUERY_STRING_O (0x0004/2) /* in Word */
  33. #define PFOW_QUERY_STRING_W (0x0006/2) /* in Word */
  34. /* OW registers address */
  35. #define CMD_CODE_OFS (0x0080/2) /* in Word */
  36. #define CMD_DATA_OFS (0x0084/2) /* in Word */
  37. #define CMD_ADD_L_OFS (0x0088/2) /* in Word */
  38. #define CMD_ADD_H_OFS (0x008A/2) /* in Word */
  39. #define MPR_L_OFS (0x0090/2) /* in Word */
  40. #define MPR_H_OFS (0x0092/2) /* in Word */
  41. #define CMD_EXEC_OFS (0x00C0/2) /* in Word */
  42. #define STATUS_REG_OFS (0x00CC/2) /* in Word */
  43. #define PRG_BUFFER_OFS (0x0010/2) /* in Word */
  44. /* Datamask */
  45. #define MR_CFGMASK 0x8000
  46. #define SR_OK_DATAMASK 0x0080
  47. /* LPDDR2-NVM Commands */
  48. #define LPDDR2_NVM_LOCK 0x0061
  49. #define LPDDR2_NVM_UNLOCK 0x0062
  50. #define LPDDR2_NVM_SW_PROGRAM 0x0041
  51. #define LPDDR2_NVM_SW_OVERWRITE 0x0042
  52. #define LPDDR2_NVM_BUF_PROGRAM 0x00E9
  53. #define LPDDR2_NVM_BUF_OVERWRITE 0x00EA
  54. #define LPDDR2_NVM_ERASE 0x0020
  55. /* LPDDR2-NVM Registers offset */
  56. #define LPDDR2_MODE_REG_DATA 0x0040
  57. #define LPDDR2_MODE_REG_CFG 0x0050
  58. /*
  59. * Internal Type Definitions
  60. * pcm_int_data contains memory controller details:
  61. * @reg_data : LPDDR2_MODE_REG_DATA register address after remapping
  62. * @reg_cfg : LPDDR2_MODE_REG_CFG register address after remapping
  63. * &bus_width: memory bus-width (eg: x16 2 Bytes, x32 4 Bytes)
  64. */
  65. struct pcm_int_data {
  66. void __iomem *ctl_regs;
  67. int bus_width;
  68. };
  69. static DEFINE_MUTEX(lpdd2_nvm_mutex);
  70. /*
  71. * Build a map_word starting from an u_long
  72. */
  73. static inline map_word build_map_word(u_long myword)
  74. {
  75. map_word val = { {0} };
  76. val.x[0] = myword;
  77. return val;
  78. }
  79. /*
  80. * Build Mode Register Configuration DataMask based on device bus-width
  81. */
  82. static inline u_int build_mr_cfgmask(u_int bus_width)
  83. {
  84. u_int val = MR_CFGMASK;
  85. if (bus_width == 0x0004) /* x32 device */
  86. val = val << 16;
  87. return val;
  88. }
  89. /*
  90. * Build Status Register OK DataMask based on device bus-width
  91. */
  92. static inline u_int build_sr_ok_datamask(u_int bus_width)
  93. {
  94. u_int val = SR_OK_DATAMASK;
  95. if (bus_width == 0x0004) /* x32 device */
  96. val = (val << 16)+val;
  97. return val;
  98. }
  99. /*
  100. * Evaluates Overlay Window Control Registers address
  101. */
  102. static inline u_long ow_reg_add(struct map_info *map, u_long offset)
  103. {
  104. u_long val = 0;
  105. struct pcm_int_data *pcm_data = map->fldrv_priv;
  106. val = map->pfow_base + offset*pcm_data->bus_width;
  107. return val;
  108. }
  109. /*
  110. * Enable lpddr2-nvm Overlay Window
  111. * Overlay Window is a memory mapped area containing all LPDDR2-NVM registers
  112. * used by device commands as well as uservisible resources like Device Status
  113. * Register, Device ID, etc
  114. */
  115. static inline void ow_enable(struct map_info *map)
  116. {
  117. struct pcm_int_data *pcm_data = map->fldrv_priv;
  118. writel_relaxed(build_mr_cfgmask(pcm_data->bus_width) | 0x18,
  119. pcm_data->ctl_regs + LPDDR2_MODE_REG_CFG);
  120. writel_relaxed(0x01, pcm_data->ctl_regs + LPDDR2_MODE_REG_DATA);
  121. }
  122. /*
  123. * Disable lpddr2-nvm Overlay Window
  124. * Overlay Window is a memory mapped area containing all LPDDR2-NVM registers
  125. * used by device commands as well as uservisible resources like Device Status
  126. * Register, Device ID, etc
  127. */
  128. static inline void ow_disable(struct map_info *map)
  129. {
  130. struct pcm_int_data *pcm_data = map->fldrv_priv;
  131. writel_relaxed(build_mr_cfgmask(pcm_data->bus_width) | 0x18,
  132. pcm_data->ctl_regs + LPDDR2_MODE_REG_CFG);
  133. writel_relaxed(0x02, pcm_data->ctl_regs + LPDDR2_MODE_REG_DATA);
  134. }
  135. /*
  136. * Execute lpddr2-nvm operations
  137. */
  138. static int lpddr2_nvm_do_op(struct map_info *map, u_long cmd_code,
  139. u_long cmd_data, u_long cmd_add, u_long cmd_mpr, u_char *buf)
  140. {
  141. map_word add_l = { {0} }, add_h = { {0} }, mpr_l = { {0} },
  142. mpr_h = { {0} }, data_l = { {0} }, cmd = { {0} },
  143. exec_cmd = { {0} }, sr;
  144. map_word data_h = { {0} }; /* only for 2x x16 devices stacked */
  145. u_long i, status_reg, prg_buff_ofs;
  146. struct pcm_int_data *pcm_data = map->fldrv_priv;
  147. u_int sr_ok_datamask = build_sr_ok_datamask(pcm_data->bus_width);
  148. /* Builds low and high words for OW Control Registers */
  149. add_l.x[0] = cmd_add & 0x0000FFFF;
  150. add_h.x[0] = (cmd_add >> 16) & 0x0000FFFF;
  151. mpr_l.x[0] = cmd_mpr & 0x0000FFFF;
  152. mpr_h.x[0] = (cmd_mpr >> 16) & 0x0000FFFF;
  153. cmd.x[0] = cmd_code & 0x0000FFFF;
  154. exec_cmd.x[0] = 0x0001;
  155. data_l.x[0] = cmd_data & 0x0000FFFF;
  156. data_h.x[0] = (cmd_data >> 16) & 0x0000FFFF; /* only for 2x x16 */
  157. /* Set Overlay Window Control Registers */
  158. map_write(map, cmd, ow_reg_add(map, CMD_CODE_OFS));
  159. map_write(map, data_l, ow_reg_add(map, CMD_DATA_OFS));
  160. map_write(map, add_l, ow_reg_add(map, CMD_ADD_L_OFS));
  161. map_write(map, add_h, ow_reg_add(map, CMD_ADD_H_OFS));
  162. map_write(map, mpr_l, ow_reg_add(map, MPR_L_OFS));
  163. map_write(map, mpr_h, ow_reg_add(map, MPR_H_OFS));
  164. if (pcm_data->bus_width == 0x0004) { /* 2x16 devices stacked */
  165. map_write(map, cmd, ow_reg_add(map, CMD_CODE_OFS) + 2);
  166. map_write(map, data_h, ow_reg_add(map, CMD_DATA_OFS) + 2);
  167. map_write(map, add_l, ow_reg_add(map, CMD_ADD_L_OFS) + 2);
  168. map_write(map, add_h, ow_reg_add(map, CMD_ADD_H_OFS) + 2);
  169. map_write(map, mpr_l, ow_reg_add(map, MPR_L_OFS) + 2);
  170. map_write(map, mpr_h, ow_reg_add(map, MPR_H_OFS) + 2);
  171. }
  172. /* Fill Program Buffer */
  173. if ((cmd_code == LPDDR2_NVM_BUF_PROGRAM) ||
  174. (cmd_code == LPDDR2_NVM_BUF_OVERWRITE)) {
  175. prg_buff_ofs = (map_read(map,
  176. ow_reg_add(map, PRG_BUFFER_OFS))).x[0];
  177. for (i = 0; i < cmd_mpr; i++) {
  178. map_write(map, build_map_word(buf[i]), map->pfow_base +
  179. prg_buff_ofs + i);
  180. }
  181. }
  182. /* Command Execute */
  183. map_write(map, exec_cmd, ow_reg_add(map, CMD_EXEC_OFS));
  184. if (pcm_data->bus_width == 0x0004) /* 2x16 devices stacked */
  185. map_write(map, exec_cmd, ow_reg_add(map, CMD_EXEC_OFS) + 2);
  186. /* Status Register Check */
  187. do {
  188. sr = map_read(map, ow_reg_add(map, STATUS_REG_OFS));
  189. status_reg = sr.x[0];
  190. if (pcm_data->bus_width == 0x0004) {/* 2x16 devices stacked */
  191. sr = map_read(map, ow_reg_add(map,
  192. STATUS_REG_OFS) + 2);
  193. status_reg += sr.x[0] << 16;
  194. }
  195. } while ((status_reg & sr_ok_datamask) != sr_ok_datamask);
  196. return (((status_reg & sr_ok_datamask) == sr_ok_datamask) ? 0 : -EIO);
  197. }
  198. /*
  199. * Execute lpddr2-nvm operations @ block level
  200. */
  201. static int lpddr2_nvm_do_block_op(struct mtd_info *mtd, loff_t start_add,
  202. uint64_t len, u_char block_op)
  203. {
  204. struct map_info *map = mtd->priv;
  205. u_long add, end_add;
  206. int ret = 0;
  207. mutex_lock(&lpdd2_nvm_mutex);
  208. ow_enable(map);
  209. add = start_add;
  210. end_add = add + len;
  211. do {
  212. ret = lpddr2_nvm_do_op(map, block_op, 0x00, add, add, NULL);
  213. if (ret)
  214. goto out;
  215. add += mtd->erasesize;
  216. } while (add < end_add);
  217. out:
  218. ow_disable(map);
  219. mutex_unlock(&lpdd2_nvm_mutex);
  220. return ret;
  221. }
  222. /*
  223. * verify presence of PFOW string
  224. */
  225. static int lpddr2_nvm_pfow_present(struct map_info *map)
  226. {
  227. map_word pfow_val[4];
  228. unsigned int found = 1;
  229. mutex_lock(&lpdd2_nvm_mutex);
  230. ow_enable(map);
  231. /* Load string from array */
  232. pfow_val[0] = map_read(map, ow_reg_add(map, PFOW_QUERY_STRING_P));
  233. pfow_val[1] = map_read(map, ow_reg_add(map, PFOW_QUERY_STRING_F));
  234. pfow_val[2] = map_read(map, ow_reg_add(map, PFOW_QUERY_STRING_O));
  235. pfow_val[3] = map_read(map, ow_reg_add(map, PFOW_QUERY_STRING_W));
  236. /* Verify the string loaded vs expected */
  237. if (!map_word_equal(map, build_map_word('P'), pfow_val[0]))
  238. found = 0;
  239. if (!map_word_equal(map, build_map_word('F'), pfow_val[1]))
  240. found = 0;
  241. if (!map_word_equal(map, build_map_word('O'), pfow_val[2]))
  242. found = 0;
  243. if (!map_word_equal(map, build_map_word('W'), pfow_val[3]))
  244. found = 0;
  245. ow_disable(map);
  246. mutex_unlock(&lpdd2_nvm_mutex);
  247. return found;
  248. }
  249. /*
  250. * lpddr2_nvm driver read method
  251. */
  252. static int lpddr2_nvm_read(struct mtd_info *mtd, loff_t start_add,
  253. size_t len, size_t *retlen, u_char *buf)
  254. {
  255. struct map_info *map = mtd->priv;
  256. mutex_lock(&lpdd2_nvm_mutex);
  257. *retlen = len;
  258. map_copy_from(map, buf, start_add, *retlen);
  259. mutex_unlock(&lpdd2_nvm_mutex);
  260. return 0;
  261. }
  262. /*
  263. * lpddr2_nvm driver write method
  264. */
  265. static int lpddr2_nvm_write(struct mtd_info *mtd, loff_t start_add,
  266. size_t len, size_t *retlen, const u_char *buf)
  267. {
  268. struct map_info *map = mtd->priv;
  269. struct pcm_int_data *pcm_data = map->fldrv_priv;
  270. u_long add, current_len, tot_len, target_len, my_data;
  271. u_char *write_buf = (u_char *)buf;
  272. int ret = 0;
  273. mutex_lock(&lpdd2_nvm_mutex);
  274. ow_enable(map);
  275. /* Set start value for the variables */
  276. add = start_add;
  277. target_len = len;
  278. tot_len = 0;
  279. while (tot_len < target_len) {
  280. if (!(IS_ALIGNED(add, mtd->writesize))) { /* do sw program */
  281. my_data = write_buf[tot_len];
  282. my_data += (write_buf[tot_len+1]) << 8;
  283. if (pcm_data->bus_width == 0x0004) {/* 2x16 devices */
  284. my_data += (write_buf[tot_len+2]) << 16;
  285. my_data += (write_buf[tot_len+3]) << 24;
  286. }
  287. ret = lpddr2_nvm_do_op(map, LPDDR2_NVM_SW_OVERWRITE,
  288. my_data, add, 0x00, NULL);
  289. if (ret)
  290. goto out;
  291. add += pcm_data->bus_width;
  292. tot_len += pcm_data->bus_width;
  293. } else { /* do buffer program */
  294. current_len = min(target_len - tot_len,
  295. (u_long) mtd->writesize);
  296. ret = lpddr2_nvm_do_op(map, LPDDR2_NVM_BUF_OVERWRITE,
  297. 0x00, add, current_len, write_buf + tot_len);
  298. if (ret)
  299. goto out;
  300. add += current_len;
  301. tot_len += current_len;
  302. }
  303. }
  304. out:
  305. *retlen = tot_len;
  306. ow_disable(map);
  307. mutex_unlock(&lpdd2_nvm_mutex);
  308. return ret;
  309. }
  310. /*
  311. * lpddr2_nvm driver erase method
  312. */
  313. static int lpddr2_nvm_erase(struct mtd_info *mtd, struct erase_info *instr)
  314. {
  315. return lpddr2_nvm_do_block_op(mtd, instr->addr, instr->len,
  316. LPDDR2_NVM_ERASE);
  317. }
  318. /*
  319. * lpddr2_nvm driver unlock method
  320. */
  321. static int lpddr2_nvm_unlock(struct mtd_info *mtd, loff_t start_add,
  322. uint64_t len)
  323. {
  324. return lpddr2_nvm_do_block_op(mtd, start_add, len, LPDDR2_NVM_UNLOCK);
  325. }
  326. /*
  327. * lpddr2_nvm driver lock method
  328. */
  329. static int lpddr2_nvm_lock(struct mtd_info *mtd, loff_t start_add,
  330. uint64_t len)
  331. {
  332. return lpddr2_nvm_do_block_op(mtd, start_add, len, LPDDR2_NVM_LOCK);
  333. }
  334. static const struct mtd_info lpddr2_nvm_mtd_info = {
  335. .type = MTD_RAM,
  336. .writesize = 1,
  337. .flags = (MTD_CAP_NVRAM | MTD_POWERUP_LOCK),
  338. ._read = lpddr2_nvm_read,
  339. ._write = lpddr2_nvm_write,
  340. ._erase = lpddr2_nvm_erase,
  341. ._unlock = lpddr2_nvm_unlock,
  342. ._lock = lpddr2_nvm_lock,
  343. };
  344. /*
  345. * lpddr2_nvm driver probe method
  346. */
  347. static int lpddr2_nvm_probe(struct platform_device *pdev)
  348. {
  349. struct map_info *map;
  350. struct mtd_info *mtd;
  351. struct resource *add_range;
  352. struct resource *control_regs;
  353. struct pcm_int_data *pcm_data;
  354. /* Allocate memory control_regs data structures */
  355. pcm_data = devm_kzalloc(&pdev->dev, sizeof(*pcm_data), GFP_KERNEL);
  356. if (!pcm_data)
  357. return -ENOMEM;
  358. pcm_data->bus_width = BUS_WIDTH;
  359. /* Allocate memory for map_info & mtd_info data structures */
  360. map = devm_kzalloc(&pdev->dev, sizeof(*map), GFP_KERNEL);
  361. if (!map)
  362. return -ENOMEM;
  363. mtd = devm_kzalloc(&pdev->dev, sizeof(*mtd), GFP_KERNEL);
  364. if (!mtd)
  365. return -ENOMEM;
  366. /* lpddr2_nvm address range */
  367. add_range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  368. if (!add_range)
  369. return -ENODEV;
  370. /* Populate map_info data structure */
  371. *map = (struct map_info) {
  372. .virt = devm_ioremap_resource(&pdev->dev, add_range),
  373. .name = pdev->dev.init_name,
  374. .phys = add_range->start,
  375. .size = resource_size(add_range),
  376. .bankwidth = pcm_data->bus_width / 2,
  377. .pfow_base = OW_BASE_ADDRESS,
  378. .fldrv_priv = pcm_data,
  379. };
  380. if (IS_ERR(map->virt))
  381. return PTR_ERR(map->virt);
  382. simple_map_init(map); /* fill with default methods */
  383. control_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  384. pcm_data->ctl_regs = devm_ioremap_resource(&pdev->dev, control_regs);
  385. if (IS_ERR(pcm_data->ctl_regs))
  386. return PTR_ERR(pcm_data->ctl_regs);
  387. /* Populate mtd_info data structure */
  388. *mtd = lpddr2_nvm_mtd_info;
  389. mtd->dev.parent = &pdev->dev;
  390. mtd->name = pdev->dev.init_name;
  391. mtd->priv = map;
  392. mtd->size = resource_size(add_range);
  393. mtd->erasesize = ERASE_BLOCKSIZE * pcm_data->bus_width;
  394. mtd->writebufsize = WRITE_BUFFSIZE * pcm_data->bus_width;
  395. /* Verify the presence of the device looking for PFOW string */
  396. if (!lpddr2_nvm_pfow_present(map)) {
  397. pr_err("device not recognized\n");
  398. return -EINVAL;
  399. }
  400. /* Parse partitions and register the MTD device */
  401. return mtd_device_register(mtd, NULL, 0);
  402. }
  403. /*
  404. * lpddr2_nvm driver remove method
  405. */
  406. static int lpddr2_nvm_remove(struct platform_device *pdev)
  407. {
  408. WARN_ON(mtd_device_unregister(dev_get_drvdata(&pdev->dev)));
  409. return 0;
  410. }
  411. /* Initialize platform_driver data structure for lpddr2_nvm */
  412. static struct platform_driver lpddr2_nvm_drv = {
  413. .driver = {
  414. .name = "lpddr2_nvm",
  415. },
  416. .probe = lpddr2_nvm_probe,
  417. .remove = lpddr2_nvm_remove,
  418. };
  419. module_platform_driver(lpddr2_nvm_drv);
  420. MODULE_LICENSE("GPL");
  421. MODULE_AUTHOR("Vincenzo Aliberti <[email protected]>");
  422. MODULE_DESCRIPTION("MTD driver for LPDDR2-NVM PCM memories");