lpddr_cmds.c 19 KB

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