lpddr_cmds.c 20 KB

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