lpddr_cmds.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LPDDR flash memory device operations. This module provides read, write,
  4. * erase, lock/unlock support for LPDDR flash memories
  5. * (C) 2008 Korolev Alexey <akorolev@infradead.org>
  6. * (C) 2008 Vasiliy Leonenko <vasiliy.leonenko@gmail.com>
  7. * Many thanks to Roman Borisov for initial enabling
  8. *
  9. * TODO:
  10. * Implement VPP management
  11. * Implement XIP support
  12. * Implement OTP support
  13. */
  14. #include <linux/mtd/pfow.h>
  15. #include <linux/mtd/qinfo.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
  19. size_t *retlen, u_char *buf);
  20. static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to,
  21. size_t len, size_t *retlen, const u_char *buf);
  22. static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
  23. unsigned long count, loff_t to, size_t *retlen);
  24. static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr);
  25. static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  26. static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  27. static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
  28. size_t *retlen, void **mtdbuf, resource_size_t *phys);
  29. static int lpddr_unpoint(struct mtd_info *mtd, loff_t adr, size_t len);
  30. static int get_chip(struct map_info *map, struct flchip *chip, int mode);
  31. static int chip_ready(struct map_info *map, struct flchip *chip, int mode);
  32. static void put_chip(struct map_info *map, struct flchip *chip);
  33. struct mtd_info *lpddr_cmdset(struct map_info *map)
  34. {
  35. struct lpddr_private *lpddr = map->fldrv_priv;
  36. struct flchip_shared *shared;
  37. struct flchip *chip;
  38. struct mtd_info *mtd;
  39. int numchips;
  40. int i, j;
  41. mtd = kzalloc_obj(*mtd);
  42. if (!mtd)
  43. return NULL;
  44. mtd->priv = map;
  45. mtd->type = MTD_NORFLASH;
  46. /* Fill in the default mtd operations */
  47. mtd->_read = lpddr_read;
  48. mtd->type = MTD_NORFLASH;
  49. mtd->flags = MTD_CAP_NORFLASH;
  50. mtd->flags &= ~MTD_BIT_WRITEABLE;
  51. mtd->_erase = lpddr_erase;
  52. mtd->_write = lpddr_write_buffers;
  53. mtd->_writev = lpddr_writev;
  54. mtd->_lock = lpddr_lock;
  55. mtd->_unlock = lpddr_unlock;
  56. if (map_is_linear(map)) {
  57. mtd->_point = lpddr_point;
  58. mtd->_unpoint = lpddr_unpoint;
  59. }
  60. mtd->size = 1ULL << lpddr->qinfo->DevSizeShift;
  61. mtd->erasesize = 1 << lpddr->qinfo->UniformBlockSizeShift;
  62. mtd->writesize = 1 << lpddr->qinfo->BufSizeShift;
  63. shared = kmalloc_objs(struct flchip_shared, lpddr->numchips);
  64. if (!shared) {
  65. kfree(mtd);
  66. return NULL;
  67. }
  68. chip = &lpddr->chips[0];
  69. numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
  70. for (i = 0; i < numchips; i++) {
  71. shared[i].writing = shared[i].erasing = NULL;
  72. mutex_init(&shared[i].lock);
  73. for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
  74. *chip = lpddr->chips[i];
  75. chip->start += (unsigned long)j << lpddr->chipshift;
  76. chip->oldstate = chip->state = FL_READY;
  77. chip->priv = &shared[i];
  78. /* those should be reset too since
  79. they create memory references. */
  80. init_waitqueue_head(&chip->wq);
  81. mutex_init(&chip->mutex);
  82. chip++;
  83. }
  84. }
  85. return mtd;
  86. }
  87. EXPORT_SYMBOL(lpddr_cmdset);
  88. static void print_drs_error(unsigned int dsr)
  89. {
  90. int prog_status = (dsr & DSR_RPS) >> 8;
  91. if (!(dsr & DSR_AVAILABLE))
  92. pr_notice("DSR.15: (0) Device not Available\n");
  93. if ((prog_status & 0x03) == 0x03)
  94. pr_notice("DSR.9,8: (11) Attempt to program invalid half with 41h command\n");
  95. else if (prog_status & 0x02)
  96. pr_notice("DSR.9,8: (10) Object Mode Program attempt in region with Control Mode data\n");
  97. else if (prog_status & 0x01)
  98. pr_notice("DSR.9,8: (01) Program attempt in region with Object Mode data\n");
  99. if (!(dsr & DSR_READY_STATUS))
  100. pr_notice("DSR.7: (0) Device is Busy\n");
  101. if (dsr & DSR_ESS)
  102. pr_notice("DSR.6: (1) Erase Suspended\n");
  103. if (dsr & DSR_ERASE_STATUS)
  104. pr_notice("DSR.5: (1) Erase/Blank check error\n");
  105. if (dsr & DSR_PROGRAM_STATUS)
  106. pr_notice("DSR.4: (1) Program Error\n");
  107. if (dsr & DSR_VPPS)
  108. pr_notice("DSR.3: (1) Vpp low detect, operation aborted\n");
  109. if (dsr & DSR_PSS)
  110. pr_notice("DSR.2: (1) Program suspended\n");
  111. if (dsr & DSR_DPS)
  112. pr_notice("DSR.1: (1) Aborted Erase/Program attempt on locked block\n");
  113. }
  114. static int wait_for_ready(struct map_info *map, struct flchip *chip,
  115. unsigned int chip_op_time)
  116. {
  117. unsigned int timeo, reset_timeo, sleep_time;
  118. unsigned int dsr;
  119. flstate_t chip_state = chip->state;
  120. int ret = 0;
  121. /* set our timeout to 8 times the expected delay */
  122. timeo = chip_op_time * 8;
  123. if (!timeo)
  124. timeo = 500000;
  125. reset_timeo = timeo;
  126. sleep_time = chip_op_time / 2;
  127. for (;;) {
  128. dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
  129. if (dsr & DSR_READY_STATUS)
  130. break;
  131. if (!timeo) {
  132. printk(KERN_ERR "%s: Flash timeout error state %d\n",
  133. map->name, chip_state);
  134. ret = -ETIME;
  135. break;
  136. }
  137. /* OK Still waiting. Drop the lock, wait a while and retry. */
  138. mutex_unlock(&chip->mutex);
  139. if (sleep_time >= 1000000/HZ) {
  140. /*
  141. * Half of the normal delay still remaining
  142. * can be performed with a sleeping delay instead
  143. * of busy waiting.
  144. */
  145. msleep(sleep_time/1000);
  146. timeo -= sleep_time;
  147. sleep_time = 1000000/HZ;
  148. } else {
  149. udelay(1);
  150. cond_resched();
  151. timeo--;
  152. }
  153. mutex_lock(&chip->mutex);
  154. while (chip->state != chip_state) {
  155. /* Someone's suspended the operation: sleep */
  156. DECLARE_WAITQUEUE(wait, current);
  157. set_current_state(TASK_UNINTERRUPTIBLE);
  158. add_wait_queue(&chip->wq, &wait);
  159. mutex_unlock(&chip->mutex);
  160. schedule();
  161. remove_wait_queue(&chip->wq, &wait);
  162. mutex_lock(&chip->mutex);
  163. }
  164. if (chip->erase_suspended || chip->write_suspended) {
  165. /* Suspend has occurred while sleep: reset timeout */
  166. timeo = reset_timeo;
  167. chip->erase_suspended = chip->write_suspended = 0;
  168. }
  169. }
  170. /* check status for errors */
  171. if (dsr & DSR_ERR) {
  172. /* Clear DSR*/
  173. map_write(map, CMD(~(DSR_ERR)), map->pfow_base + PFOW_DSR);
  174. printk(KERN_WARNING"%s: Bad status on wait: 0x%x\n",
  175. map->name, dsr);
  176. print_drs_error(dsr);
  177. ret = -EIO;
  178. }
  179. chip->state = FL_READY;
  180. return ret;
  181. }
  182. static int get_chip(struct map_info *map, struct flchip *chip, int mode)
  183. {
  184. int ret;
  185. DECLARE_WAITQUEUE(wait, current);
  186. retry:
  187. if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)
  188. && chip->state != FL_SYNCING) {
  189. /*
  190. * OK. We have possibility for contension on the write/erase
  191. * operations which are global to the real chip and not per
  192. * partition. So let's fight it over in the partition which
  193. * currently has authority on the operation.
  194. *
  195. * The rules are as follows:
  196. *
  197. * - any write operation must own shared->writing.
  198. *
  199. * - any erase operation must own _both_ shared->writing and
  200. * shared->erasing.
  201. *
  202. * - contension arbitration is handled in the owner's context.
  203. *
  204. * The 'shared' struct can be read and/or written only when
  205. * its lock is taken.
  206. */
  207. struct flchip_shared *shared = chip->priv;
  208. struct flchip *contender;
  209. mutex_lock(&shared->lock);
  210. contender = shared->writing;
  211. if (contender && contender != chip) {
  212. /*
  213. * The engine to perform desired operation on this
  214. * partition is already in use by someone else.
  215. * Let's fight over it in the context of the chip
  216. * currently using it. If it is possible to suspend,
  217. * that other partition will do just that, otherwise
  218. * it'll happily send us to sleep. In any case, when
  219. * get_chip returns success we're clear to go ahead.
  220. */
  221. ret = mutex_trylock(&contender->mutex);
  222. mutex_unlock(&shared->lock);
  223. if (!ret)
  224. goto retry;
  225. mutex_unlock(&chip->mutex);
  226. ret = chip_ready(map, contender, mode);
  227. mutex_lock(&chip->mutex);
  228. if (ret == -EAGAIN) {
  229. mutex_unlock(&contender->mutex);
  230. goto retry;
  231. }
  232. if (ret) {
  233. mutex_unlock(&contender->mutex);
  234. return ret;
  235. }
  236. mutex_lock(&shared->lock);
  237. /* We should not own chip if it is already in FL_SYNCING
  238. * state. Put contender and retry. */
  239. if (chip->state == FL_SYNCING) {
  240. put_chip(map, contender);
  241. mutex_unlock(&contender->mutex);
  242. goto retry;
  243. }
  244. mutex_unlock(&contender->mutex);
  245. }
  246. /* Check if we have suspended erase on this chip.
  247. Must sleep in such a case. */
  248. if (mode == FL_ERASING && shared->erasing
  249. && shared->erasing->oldstate == FL_ERASING) {
  250. mutex_unlock(&shared->lock);
  251. set_current_state(TASK_UNINTERRUPTIBLE);
  252. add_wait_queue(&chip->wq, &wait);
  253. mutex_unlock(&chip->mutex);
  254. schedule();
  255. remove_wait_queue(&chip->wq, &wait);
  256. mutex_lock(&chip->mutex);
  257. goto retry;
  258. }
  259. /* We now own it */
  260. shared->writing = chip;
  261. if (mode == FL_ERASING)
  262. shared->erasing = chip;
  263. mutex_unlock(&shared->lock);
  264. }
  265. ret = chip_ready(map, chip, mode);
  266. if (ret == -EAGAIN)
  267. goto retry;
  268. return ret;
  269. }
  270. static int chip_ready(struct map_info *map, struct flchip *chip, int mode)
  271. {
  272. struct lpddr_private *lpddr = map->fldrv_priv;
  273. int ret = 0;
  274. DECLARE_WAITQUEUE(wait, current);
  275. /* Prevent setting state FL_SYNCING for chip in suspended state. */
  276. if (FL_SYNCING == mode && FL_READY != chip->oldstate)
  277. goto sleep;
  278. switch (chip->state) {
  279. case FL_READY:
  280. case FL_JEDEC_QUERY:
  281. return 0;
  282. case FL_ERASING:
  283. if (!lpddr->qinfo->SuspEraseSupp ||
  284. !(mode == FL_READY || mode == FL_POINT))
  285. goto sleep;
  286. map_write(map, CMD(LPDDR_SUSPEND),
  287. map->pfow_base + PFOW_PROGRAM_ERASE_SUSPEND);
  288. chip->oldstate = FL_ERASING;
  289. chip->state = FL_ERASE_SUSPENDING;
  290. ret = wait_for_ready(map, chip, 0);
  291. if (ret) {
  292. /* Oops. something got wrong. */
  293. /* Resume and pretend we weren't here. */
  294. put_chip(map, chip);
  295. printk(KERN_ERR "%s: suspend operation failed."
  296. "State may be wrong\n", map->name);
  297. return -EIO;
  298. }
  299. chip->erase_suspended = 1;
  300. chip->state = FL_READY;
  301. return 0;
  302. /* Erase suspend */
  303. case FL_POINT:
  304. /* Only if there's no operation suspended... */
  305. if (mode == FL_READY && chip->oldstate == FL_READY)
  306. return 0;
  307. fallthrough;
  308. default:
  309. sleep:
  310. set_current_state(TASK_UNINTERRUPTIBLE);
  311. add_wait_queue(&chip->wq, &wait);
  312. mutex_unlock(&chip->mutex);
  313. schedule();
  314. remove_wait_queue(&chip->wq, &wait);
  315. mutex_lock(&chip->mutex);
  316. return -EAGAIN;
  317. }
  318. }
  319. static void put_chip(struct map_info *map, struct flchip *chip)
  320. {
  321. if (chip->priv) {
  322. struct flchip_shared *shared = chip->priv;
  323. mutex_lock(&shared->lock);
  324. if (shared->writing == chip && chip->oldstate == FL_READY) {
  325. /* We own the ability to write, but we're done */
  326. shared->writing = shared->erasing;
  327. if (shared->writing && shared->writing != chip) {
  328. /* give back the ownership */
  329. struct flchip *loaner = shared->writing;
  330. mutex_lock(&loaner->mutex);
  331. mutex_unlock(&shared->lock);
  332. mutex_unlock(&chip->mutex);
  333. put_chip(map, loaner);
  334. mutex_lock(&chip->mutex);
  335. mutex_unlock(&loaner->mutex);
  336. wake_up(&chip->wq);
  337. return;
  338. }
  339. shared->erasing = NULL;
  340. shared->writing = NULL;
  341. } else if (shared->erasing == chip && shared->writing != chip) {
  342. /*
  343. * We own the ability to erase without the ability
  344. * to write, which means the erase was suspended
  345. * and some other partition is currently writing.
  346. * Don't let the switch below mess things up since
  347. * we don't have ownership to resume anything.
  348. */
  349. mutex_unlock(&shared->lock);
  350. wake_up(&chip->wq);
  351. return;
  352. }
  353. mutex_unlock(&shared->lock);
  354. }
  355. switch (chip->oldstate) {
  356. case FL_ERASING:
  357. map_write(map, CMD(LPDDR_RESUME),
  358. map->pfow_base + PFOW_COMMAND_CODE);
  359. map_write(map, CMD(LPDDR_START_EXECUTION),
  360. map->pfow_base + PFOW_COMMAND_EXECUTE);
  361. chip->oldstate = FL_READY;
  362. chip->state = FL_ERASING;
  363. break;
  364. case FL_READY:
  365. break;
  366. default:
  367. printk(KERN_ERR "%s: put_chip() called with oldstate %d!\n",
  368. map->name, chip->oldstate);
  369. }
  370. wake_up(&chip->wq);
  371. }
  372. static int do_write_buffer(struct map_info *map, struct flchip *chip,
  373. unsigned long adr, const struct kvec **pvec,
  374. unsigned long *pvec_seek, int len)
  375. {
  376. struct lpddr_private *lpddr = map->fldrv_priv;
  377. map_word datum;
  378. int ret, wbufsize, word_gap;
  379. const struct kvec *vec;
  380. unsigned long vec_seek;
  381. unsigned long prog_buf_ofs;
  382. wbufsize = 1 << lpddr->qinfo->BufSizeShift;
  383. mutex_lock(&chip->mutex);
  384. ret = get_chip(map, chip, FL_WRITING);
  385. if (ret) {
  386. mutex_unlock(&chip->mutex);
  387. return ret;
  388. }
  389. /* Figure out the number of words to write */
  390. word_gap = (-adr & (map_bankwidth(map)-1));
  391. if (word_gap) {
  392. word_gap = map_bankwidth(map) - word_gap;
  393. adr -= word_gap;
  394. datum = map_word_ff(map);
  395. }
  396. /* Write data */
  397. /* Get the program buffer offset from PFOW register data first*/
  398. prog_buf_ofs = map->pfow_base + CMDVAL(map_read(map,
  399. map->pfow_base + PFOW_PROGRAM_BUFFER_OFFSET));
  400. vec = *pvec;
  401. vec_seek = *pvec_seek;
  402. do {
  403. int n = map_bankwidth(map) - word_gap;
  404. if (n > vec->iov_len - vec_seek)
  405. n = vec->iov_len - vec_seek;
  406. if (n > len)
  407. n = len;
  408. if (!word_gap && (len < map_bankwidth(map)))
  409. datum = map_word_ff(map);
  410. datum = map_word_load_partial(map, datum,
  411. vec->iov_base + vec_seek, word_gap, n);
  412. len -= n;
  413. word_gap += n;
  414. if (!len || word_gap == map_bankwidth(map)) {
  415. map_write(map, datum, prog_buf_ofs);
  416. prog_buf_ofs += map_bankwidth(map);
  417. word_gap = 0;
  418. }
  419. vec_seek += n;
  420. if (vec_seek == vec->iov_len) {
  421. vec++;
  422. vec_seek = 0;
  423. }
  424. } while (len);
  425. *pvec = vec;
  426. *pvec_seek = vec_seek;
  427. /* GO GO GO */
  428. send_pfow_command(map, LPDDR_BUFF_PROGRAM, adr, wbufsize, NULL);
  429. chip->state = FL_WRITING;
  430. ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->ProgBufferTime));
  431. if (ret) {
  432. printk(KERN_WARNING"%s Buffer program error: %d at %lx\n",
  433. map->name, ret, adr);
  434. goto out;
  435. }
  436. out: put_chip(map, chip);
  437. mutex_unlock(&chip->mutex);
  438. return ret;
  439. }
  440. static int do_erase_oneblock(struct mtd_info *mtd, loff_t adr)
  441. {
  442. struct map_info *map = mtd->priv;
  443. struct lpddr_private *lpddr = map->fldrv_priv;
  444. int chipnum = adr >> lpddr->chipshift;
  445. struct flchip *chip = &lpddr->chips[chipnum];
  446. int ret;
  447. mutex_lock(&chip->mutex);
  448. ret = get_chip(map, chip, FL_ERASING);
  449. if (ret) {
  450. mutex_unlock(&chip->mutex);
  451. return ret;
  452. }
  453. send_pfow_command(map, LPDDR_BLOCK_ERASE, adr, 0, NULL);
  454. chip->state = FL_ERASING;
  455. ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->BlockEraseTime)*1000);
  456. if (ret) {
  457. printk(KERN_WARNING"%s Erase block error %d at : %llx\n",
  458. map->name, ret, adr);
  459. goto out;
  460. }
  461. out: put_chip(map, chip);
  462. mutex_unlock(&chip->mutex);
  463. return ret;
  464. }
  465. static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
  466. size_t *retlen, u_char *buf)
  467. {
  468. struct map_info *map = mtd->priv;
  469. struct lpddr_private *lpddr = map->fldrv_priv;
  470. int chipnum = adr >> lpddr->chipshift;
  471. struct flchip *chip = &lpddr->chips[chipnum];
  472. int ret = 0;
  473. mutex_lock(&chip->mutex);
  474. ret = get_chip(map, chip, FL_READY);
  475. if (ret) {
  476. mutex_unlock(&chip->mutex);
  477. return ret;
  478. }
  479. map_copy_from(map, buf, adr, len);
  480. *retlen = len;
  481. put_chip(map, chip);
  482. mutex_unlock(&chip->mutex);
  483. return ret;
  484. }
  485. static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
  486. size_t *retlen, void **mtdbuf, resource_size_t *phys)
  487. {
  488. struct map_info *map = mtd->priv;
  489. struct lpddr_private *lpddr = map->fldrv_priv;
  490. int chipnum = adr >> lpddr->chipshift;
  491. unsigned long ofs, last_end = 0;
  492. struct flchip *chip = &lpddr->chips[chipnum];
  493. int ret = 0;
  494. if (!map->virt)
  495. return -EINVAL;
  496. /* ofs: offset within the first chip that the first read should start */
  497. ofs = adr - (chipnum << lpddr->chipshift);
  498. *mtdbuf = (void *)map->virt + chip->start + ofs;
  499. while (len) {
  500. unsigned long thislen;
  501. if (chipnum >= lpddr->numchips)
  502. break;
  503. /* We cannot point across chips that are virtually disjoint */
  504. if (!last_end)
  505. last_end = chip->start;
  506. else if (chip->start != last_end)
  507. break;
  508. if ((len + ofs - 1) >> lpddr->chipshift)
  509. thislen = (1UL << lpddr->chipshift) - ofs;
  510. else
  511. thislen = len;
  512. /* get the chip */
  513. mutex_lock(&chip->mutex);
  514. ret = get_chip(map, chip, FL_POINT);
  515. mutex_unlock(&chip->mutex);
  516. if (ret)
  517. break;
  518. chip->state = FL_POINT;
  519. chip->ref_point_counter++;
  520. *retlen += thislen;
  521. len -= thislen;
  522. ofs = 0;
  523. last_end += 1UL << lpddr->chipshift;
  524. chipnum++;
  525. chip = &lpddr->chips[chipnum];
  526. }
  527. return 0;
  528. }
  529. static int lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
  530. {
  531. struct map_info *map = mtd->priv;
  532. struct lpddr_private *lpddr = map->fldrv_priv;
  533. int chipnum = adr >> lpddr->chipshift, err = 0;
  534. unsigned long ofs;
  535. /* ofs: offset within the first chip that the first read should start */
  536. ofs = adr - (chipnum << lpddr->chipshift);
  537. while (len) {
  538. unsigned long thislen;
  539. struct flchip *chip;
  540. chip = &lpddr->chips[chipnum];
  541. if (chipnum >= lpddr->numchips)
  542. break;
  543. if ((len + ofs - 1) >> lpddr->chipshift)
  544. thislen = (1UL << lpddr->chipshift) - ofs;
  545. else
  546. thislen = len;
  547. mutex_lock(&chip->mutex);
  548. if (chip->state == FL_POINT) {
  549. chip->ref_point_counter--;
  550. if (chip->ref_point_counter == 0)
  551. chip->state = FL_READY;
  552. } else {
  553. printk(KERN_WARNING "%s: Warning: unpoint called on non"
  554. "pointed region\n", map->name);
  555. err = -EINVAL;
  556. }
  557. put_chip(map, chip);
  558. mutex_unlock(&chip->mutex);
  559. len -= thislen;
  560. ofs = 0;
  561. chipnum++;
  562. }
  563. return err;
  564. }
  565. static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  566. size_t *retlen, const u_char *buf)
  567. {
  568. struct kvec vec;
  569. vec.iov_base = (void *) buf;
  570. vec.iov_len = len;
  571. return lpddr_writev(mtd, &vec, 1, to, retlen);
  572. }
  573. static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
  574. unsigned long count, loff_t to, size_t *retlen)
  575. {
  576. struct map_info *map = mtd->priv;
  577. struct lpddr_private *lpddr = map->fldrv_priv;
  578. int ret = 0;
  579. int chipnum;
  580. unsigned long ofs, vec_seek, i;
  581. int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
  582. size_t len = 0;
  583. for (i = 0; i < count; i++)
  584. len += vecs[i].iov_len;
  585. if (!len)
  586. return 0;
  587. chipnum = to >> lpddr->chipshift;
  588. ofs = to;
  589. vec_seek = 0;
  590. do {
  591. /* We must not cross write block boundaries */
  592. int size = wbufsize - (ofs & (wbufsize-1));
  593. if (size > len)
  594. size = len;
  595. ret = do_write_buffer(map, &lpddr->chips[chipnum],
  596. ofs, &vecs, &vec_seek, size);
  597. if (ret)
  598. return ret;
  599. ofs += size;
  600. (*retlen) += size;
  601. len -= size;
  602. /* Be nice and reschedule with the chip in a usable
  603. * state for other processes */
  604. cond_resched();
  605. } while (len);
  606. return 0;
  607. }
  608. static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
  609. {
  610. unsigned long ofs, len;
  611. int ret;
  612. struct map_info *map = mtd->priv;
  613. struct lpddr_private *lpddr = map->fldrv_priv;
  614. int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
  615. ofs = instr->addr;
  616. len = instr->len;
  617. while (len > 0) {
  618. ret = do_erase_oneblock(mtd, ofs);
  619. if (ret)
  620. return ret;
  621. ofs += size;
  622. len -= size;
  623. }
  624. return 0;
  625. }
  626. #define DO_XXLOCK_LOCK 1
  627. #define DO_XXLOCK_UNLOCK 2
  628. static int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
  629. {
  630. int ret = 0;
  631. struct map_info *map = mtd->priv;
  632. struct lpddr_private *lpddr = map->fldrv_priv;
  633. int chipnum = adr >> lpddr->chipshift;
  634. struct flchip *chip = &lpddr->chips[chipnum];
  635. mutex_lock(&chip->mutex);
  636. ret = get_chip(map, chip, FL_LOCKING);
  637. if (ret) {
  638. mutex_unlock(&chip->mutex);
  639. return ret;
  640. }
  641. if (thunk == DO_XXLOCK_LOCK) {
  642. send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
  643. chip->state = FL_LOCKING;
  644. } else if (thunk == DO_XXLOCK_UNLOCK) {
  645. send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
  646. chip->state = FL_UNLOCKING;
  647. } else
  648. BUG();
  649. ret = wait_for_ready(map, chip, 1);
  650. if (ret) {
  651. printk(KERN_ERR "%s: block unlock error status %d\n",
  652. map->name, ret);
  653. goto out;
  654. }
  655. out: put_chip(map, chip);
  656. mutex_unlock(&chip->mutex);
  657. return ret;
  658. }
  659. static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  660. {
  661. return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
  662. }
  663. static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  664. {
  665. return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
  666. }
  667. MODULE_LICENSE("GPL");
  668. MODULE_AUTHOR("Alexey Korolev <akorolev@infradead.org>");
  669. MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");