lpddr_cmds.c 21 KB

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