lpddr2_nvm.c 14 KB

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