2 * LPDDR flash memory device operations. This module provides read, write,
3 * erase, lock/unlock support for LPDDR flash memories
6 * Many thanks to Roman Borisov for initial enabling
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.
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.
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
23 * Implement VPP management
24 * Implement XIP support
25 * Implement OTP support
27 #include <linux/mtd/pfow.h>
28 #include <linux/mtd/qinfo.h>
29 #include <linux/slab.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 void 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);
47 struct mtd_info *lpddr_cmdset(struct map_info *map)
49 struct lpddr_private *lpddr = map->fldrv_priv;
50 struct flchip_shared *shared;
56 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
58 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
62 mtd->type = MTD_NORFLASH;
64 /* Fill in the default mtd operations */
65 mtd->read = lpddr_read;
66 mtd->type = MTD_NORFLASH;
67 mtd->flags = MTD_CAP_NORFLASH;
68 mtd->flags &= ~MTD_BIT_WRITEABLE;
69 mtd->erase = lpddr_erase;
70 mtd->write = lpddr_write_buffers;
71 mtd->writev = lpddr_writev;
73 mtd->write_oob = NULL;
75 mtd->lock = lpddr_lock;
76 mtd->unlock = lpddr_unlock;
79 if (map_is_linear(map)) {
80 mtd->point = lpddr_point;
81 mtd->unpoint = lpddr_unpoint;
83 mtd->block_isbad = NULL;
84 mtd->block_markbad = NULL;
85 mtd->size = 1 << lpddr->qinfo->DevSizeShift;
86 mtd->erasesize = 1 << lpddr->qinfo->UniformBlockSizeShift;
87 mtd->writesize = 1 << lpddr->qinfo->BufSizeShift;
89 shared = kmalloc(sizeof(struct flchip_shared) * lpddr->numchips,
97 chip = &lpddr->chips[0];
98 numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
99 for (i = 0; i < numchips; i++) {
100 shared[i].writing = shared[i].erasing = NULL;
101 mutex_init(&shared[i].lock);
102 for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
103 *chip = lpddr->chips[i];
104 chip->start += j << lpddr->chipshift;
105 chip->oldstate = chip->state = FL_READY;
106 chip->priv = &shared[i];
107 /* those should be reset too since
108 they create memory references. */
109 init_waitqueue_head(&chip->wq);
110 mutex_init(&chip->mutex);
117 EXPORT_SYMBOL(lpddr_cmdset);
119 static int wait_for_ready(struct map_info *map, struct flchip *chip,
120 unsigned int chip_op_time)
122 unsigned int timeo, reset_timeo, sleep_time;
124 flstate_t chip_state = chip->state;
127 /* set our timeout to 8 times the expected delay */
128 timeo = chip_op_time * 8;
132 sleep_time = chip_op_time / 2;
135 dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
136 if (dsr & DSR_READY_STATUS)
139 printk(KERN_ERR "%s: Flash timeout error state %d \n",
140 map->name, chip_state);
145 /* OK Still waiting. Drop the lock, wait a while and retry. */
146 mutex_unlock(&chip->mutex);
147 if (sleep_time >= 1000000/HZ) {
149 * Half of the normal delay still remaining
150 * can be performed with a sleeping delay instead
153 msleep(sleep_time/1000);
155 sleep_time = 1000000/HZ;
161 mutex_lock(&chip->mutex);
163 while (chip->state != chip_state) {
164 /* Someone's suspended the operation: sleep */
165 DECLARE_WAITQUEUE(wait, current);
166 set_current_state(TASK_UNINTERRUPTIBLE);
167 add_wait_queue(&chip->wq, &wait);
168 mutex_unlock(&chip->mutex);
170 remove_wait_queue(&chip->wq, &wait);
171 mutex_lock(&chip->mutex);
173 if (chip->erase_suspended || chip->write_suspended) {
174 /* Suspend has occurred while sleep: reset timeout */
176 chip->erase_suspended = chip->write_suspended = 0;
179 /* check status for errors */
182 map_write(map, CMD(~(DSR_ERR)), map->pfow_base + PFOW_DSR);
183 printk(KERN_WARNING"%s: Bad status on wait: 0x%x \n",
185 print_drs_error(dsr);
188 chip->state = FL_READY;
192 static int get_chip(struct map_info *map, struct flchip *chip, int mode)
195 DECLARE_WAITQUEUE(wait, current);
198 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)
199 && chip->state != FL_SYNCING) {
201 * OK. We have possibility for contension on the write/erase
202 * operations which are global to the real chip and not per
203 * partition. So let's fight it over in the partition which
204 * currently has authority on the operation.
206 * The rules are as follows:
208 * - any write operation must own shared->writing.
210 * - any erase operation must own _both_ shared->writing and
213 * - contension arbitration is handled in the owner's context.
215 * The 'shared' struct can be read and/or written only when
218 struct flchip_shared *shared = chip->priv;
219 struct flchip *contender;
220 mutex_lock(&shared->lock);
221 contender = shared->writing;
222 if (contender && contender != chip) {
224 * The engine to perform desired operation on this
225 * partition is already in use by someone else.
226 * Let's fight over it in the context of the chip
227 * currently using it. If it is possible to suspend,
228 * that other partition will do just that, otherwise
229 * it'll happily send us to sleep. In any case, when
230 * get_chip returns success we're clear to go ahead.
232 ret = mutex_trylock(&contender->mutex);
233 mutex_unlock(&shared->lock);
236 mutex_unlock(&chip->mutex);
237 ret = chip_ready(map, contender, mode);
238 mutex_lock(&chip->mutex);
240 if (ret == -EAGAIN) {
241 mutex_unlock(&contender->mutex);
245 mutex_unlock(&contender->mutex);
248 mutex_lock(&shared->lock);
250 /* We should not own chip if it is already in FL_SYNCING
251 * state. Put contender and retry. */
252 if (chip->state == FL_SYNCING) {
253 put_chip(map, contender);
254 mutex_unlock(&contender->mutex);
257 mutex_unlock(&contender->mutex);
260 /* Check if we have suspended erase on this chip.
261 Must sleep in such a case. */
262 if (mode == FL_ERASING && shared->erasing
263 && shared->erasing->oldstate == FL_ERASING) {
264 mutex_unlock(&shared->lock);
265 set_current_state(TASK_UNINTERRUPTIBLE);
266 add_wait_queue(&chip->wq, &wait);
267 mutex_unlock(&chip->mutex);
269 remove_wait_queue(&chip->wq, &wait);
270 mutex_lock(&chip->mutex);
275 shared->writing = chip;
276 if (mode == FL_ERASING)
277 shared->erasing = chip;
278 mutex_unlock(&shared->lock);
281 ret = chip_ready(map, chip, mode);
288 static int chip_ready(struct map_info *map, struct flchip *chip, int mode)
290 struct lpddr_private *lpddr = map->fldrv_priv;
292 DECLARE_WAITQUEUE(wait, current);
294 /* Prevent setting state FL_SYNCING for chip in suspended state. */
295 if (FL_SYNCING == mode && FL_READY != chip->oldstate)
298 switch (chip->state) {
304 if (!lpddr->qinfo->SuspEraseSupp ||
305 !(mode == FL_READY || mode == FL_POINT))
308 map_write(map, CMD(LPDDR_SUSPEND),
309 map->pfow_base + PFOW_PROGRAM_ERASE_SUSPEND);
310 chip->oldstate = FL_ERASING;
311 chip->state = FL_ERASE_SUSPENDING;
312 ret = wait_for_ready(map, chip, 0);
314 /* Oops. something got wrong. */
315 /* Resume and pretend we weren't here. */
317 printk(KERN_ERR "%s: suspend operation failed."
318 "State may be wrong \n", map->name);
321 chip->erase_suspended = 1;
322 chip->state = FL_READY;
326 /* Only if there's no operation suspended... */
327 if (mode == FL_READY && chip->oldstate == FL_READY)
332 set_current_state(TASK_UNINTERRUPTIBLE);
333 add_wait_queue(&chip->wq, &wait);
334 mutex_unlock(&chip->mutex);
336 remove_wait_queue(&chip->wq, &wait);
337 mutex_lock(&chip->mutex);
342 static void put_chip(struct map_info *map, struct flchip *chip)
345 struct flchip_shared *shared = chip->priv;
346 mutex_lock(&shared->lock);
347 if (shared->writing == chip && chip->oldstate == FL_READY) {
348 /* We own the ability to write, but we're done */
349 shared->writing = shared->erasing;
350 if (shared->writing && shared->writing != chip) {
351 /* give back the ownership */
352 struct flchip *loaner = shared->writing;
353 mutex_lock(&loaner->mutex);
354 mutex_unlock(&shared->lock);
355 mutex_unlock(&chip->mutex);
356 put_chip(map, loaner);
357 mutex_lock(&chip->mutex);
358 mutex_unlock(&loaner->mutex);
362 shared->erasing = NULL;
363 shared->writing = NULL;
364 } else if (shared->erasing == chip && shared->writing != chip) {
366 * We own the ability to erase without the ability
367 * to write, which means the erase was suspended
368 * and some other partition is currently writing.
369 * Don't let the switch below mess things up since
370 * we don't have ownership to resume anything.
372 mutex_unlock(&shared->lock);
376 mutex_unlock(&shared->lock);
379 switch (chip->oldstate) {
381 map_write(map, CMD(LPDDR_RESUME),
382 map->pfow_base + PFOW_COMMAND_CODE);
383 map_write(map, CMD(LPDDR_START_EXECUTION),
384 map->pfow_base + PFOW_COMMAND_EXECUTE);
385 chip->oldstate = FL_READY;
386 chip->state = FL_ERASING;
391 printk(KERN_ERR "%s: put_chip() called with oldstate %d!\n",
392 map->name, chip->oldstate);
397 int do_write_buffer(struct map_info *map, struct flchip *chip,
398 unsigned long adr, const struct kvec **pvec,
399 unsigned long *pvec_seek, int len)
401 struct lpddr_private *lpddr = map->fldrv_priv;
403 int ret, wbufsize, word_gap, words;
404 const struct kvec *vec;
405 unsigned long vec_seek;
406 unsigned long prog_buf_ofs;
408 wbufsize = 1 << lpddr->qinfo->BufSizeShift;
410 mutex_lock(&chip->mutex);
411 ret = get_chip(map, chip, FL_WRITING);
413 mutex_unlock(&chip->mutex);
416 /* Figure out the number of words to write */
417 word_gap = (-adr & (map_bankwidth(map)-1));
418 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
422 word_gap = map_bankwidth(map) - word_gap;
424 datum = map_word_ff(map);
427 /* Get the program buffer offset from PFOW register data first*/
428 prog_buf_ofs = map->pfow_base + CMDVAL(map_read(map,
429 map->pfow_base + PFOW_PROGRAM_BUFFER_OFFSET));
431 vec_seek = *pvec_seek;
433 int n = map_bankwidth(map) - word_gap;
435 if (n > vec->iov_len - vec_seek)
436 n = vec->iov_len - vec_seek;
440 if (!word_gap && (len < map_bankwidth(map)))
441 datum = map_word_ff(map);
443 datum = map_word_load_partial(map, datum,
444 vec->iov_base + vec_seek, word_gap, n);
448 if (!len || word_gap == map_bankwidth(map)) {
449 map_write(map, datum, prog_buf_ofs);
450 prog_buf_ofs += map_bankwidth(map);
455 if (vec_seek == vec->iov_len) {
461 *pvec_seek = vec_seek;
464 send_pfow_command(map, LPDDR_BUFF_PROGRAM, adr, wbufsize, NULL);
465 chip->state = FL_WRITING;
466 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->ProgBufferTime));
468 printk(KERN_WARNING"%s Buffer program error: %d at %lx; \n",
469 map->name, ret, adr);
473 out: put_chip(map, chip);
474 mutex_unlock(&chip->mutex);
478 int do_erase_oneblock(struct mtd_info *mtd, loff_t adr)
480 struct map_info *map = mtd->priv;
481 struct lpddr_private *lpddr = map->fldrv_priv;
482 int chipnum = adr >> lpddr->chipshift;
483 struct flchip *chip = &lpddr->chips[chipnum];
486 mutex_lock(&chip->mutex);
487 ret = get_chip(map, chip, FL_ERASING);
489 mutex_unlock(&chip->mutex);
492 send_pfow_command(map, LPDDR_BLOCK_ERASE, adr, 0, NULL);
493 chip->state = FL_ERASING;
494 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->BlockEraseTime)*1000);
496 printk(KERN_WARNING"%s Erase block error %d at : %llx\n",
497 map->name, ret, adr);
500 out: put_chip(map, chip);
501 mutex_unlock(&chip->mutex);
505 static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
506 size_t *retlen, u_char *buf)
508 struct map_info *map = mtd->priv;
509 struct lpddr_private *lpddr = map->fldrv_priv;
510 int chipnum = adr >> lpddr->chipshift;
511 struct flchip *chip = &lpddr->chips[chipnum];
514 mutex_lock(&chip->mutex);
515 ret = get_chip(map, chip, FL_READY);
517 mutex_unlock(&chip->mutex);
521 map_copy_from(map, buf, adr, len);
525 mutex_unlock(&chip->mutex);
529 static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
530 size_t *retlen, void **mtdbuf, resource_size_t *phys)
532 struct map_info *map = mtd->priv;
533 struct lpddr_private *lpddr = map->fldrv_priv;
534 int chipnum = adr >> lpddr->chipshift;
535 unsigned long ofs, last_end = 0;
536 struct flchip *chip = &lpddr->chips[chipnum];
539 if (!map->virt || (adr + len > mtd->size))
542 /* ofs: offset within the first chip that the first read should start */
543 ofs = adr - (chipnum << lpddr->chipshift);
545 *mtdbuf = (void *)map->virt + chip->start + ofs;
549 unsigned long thislen;
551 if (chipnum >= lpddr->numchips)
554 /* We cannot point across chips that are virtually disjoint */
556 last_end = chip->start;
557 else if (chip->start != last_end)
560 if ((len + ofs - 1) >> lpddr->chipshift)
561 thislen = (1<<lpddr->chipshift) - ofs;
565 mutex_lock(&chip->mutex);
566 ret = get_chip(map, chip, FL_POINT);
567 mutex_unlock(&chip->mutex);
571 chip->state = FL_POINT;
572 chip->ref_point_counter++;
577 last_end += 1 << lpddr->chipshift;
579 chip = &lpddr->chips[chipnum];
584 static void lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
586 struct map_info *map = mtd->priv;
587 struct lpddr_private *lpddr = map->fldrv_priv;
588 int chipnum = adr >> lpddr->chipshift;
591 /* ofs: offset within the first chip that the first read should start */
592 ofs = adr - (chipnum << lpddr->chipshift);
595 unsigned long thislen;
598 chip = &lpddr->chips[chipnum];
599 if (chipnum >= lpddr->numchips)
602 if ((len + ofs - 1) >> lpddr->chipshift)
603 thislen = (1<<lpddr->chipshift) - ofs;
607 mutex_lock(&chip->mutex);
608 if (chip->state == FL_POINT) {
609 chip->ref_point_counter--;
610 if (chip->ref_point_counter == 0)
611 chip->state = FL_READY;
613 printk(KERN_WARNING "%s: Warning: unpoint called on non"
614 "pointed region\n", map->name);
617 mutex_unlock(&chip->mutex);
625 static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
626 size_t *retlen, const u_char *buf)
630 vec.iov_base = (void *) buf;
633 return lpddr_writev(mtd, &vec, 1, to, retlen);
637 static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
638 unsigned long count, loff_t to, size_t *retlen)
640 struct map_info *map = mtd->priv;
641 struct lpddr_private *lpddr = map->fldrv_priv;
644 unsigned long ofs, vec_seek, i;
645 int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
649 for (i = 0; i < count; i++)
650 len += vecs[i].iov_len;
656 chipnum = to >> lpddr->chipshift;
662 /* We must not cross write block boundaries */
663 int size = wbufsize - (ofs & (wbufsize-1));
668 ret = do_write_buffer(map, &lpddr->chips[chipnum],
669 ofs, &vecs, &vec_seek, size);
677 /* Be nice and reschedule with the chip in a usable
678 * state for other processes */
686 static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
688 unsigned long ofs, len;
690 struct map_info *map = mtd->priv;
691 struct lpddr_private *lpddr = map->fldrv_priv;
692 int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
697 if (ofs > mtd->size || (len + ofs) > mtd->size)
701 ret = do_erase_oneblock(mtd, ofs);
707 instr->state = MTD_ERASE_DONE;
708 mtd_erase_callback(instr);
713 #define DO_XXLOCK_LOCK 1
714 #define DO_XXLOCK_UNLOCK 2
715 int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
718 struct map_info *map = mtd->priv;
719 struct lpddr_private *lpddr = map->fldrv_priv;
720 int chipnum = adr >> lpddr->chipshift;
721 struct flchip *chip = &lpddr->chips[chipnum];
723 mutex_lock(&chip->mutex);
724 ret = get_chip(map, chip, FL_LOCKING);
726 mutex_unlock(&chip->mutex);
730 if (thunk == DO_XXLOCK_LOCK) {
731 send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
732 chip->state = FL_LOCKING;
733 } else if (thunk == DO_XXLOCK_UNLOCK) {
734 send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
735 chip->state = FL_UNLOCKING;
739 ret = wait_for_ready(map, chip, 1);
741 printk(KERN_ERR "%s: block unlock error status %d \n",
745 out: put_chip(map, chip);
746 mutex_unlock(&chip->mutex);
750 static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
752 return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
755 static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
757 return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
760 int word_program(struct map_info *map, loff_t adr, uint32_t curval)
763 struct lpddr_private *lpddr = map->fldrv_priv;
764 int chipnum = adr >> lpddr->chipshift;
765 struct flchip *chip = &lpddr->chips[chipnum];
767 mutex_lock(&chip->mutex);
768 ret = get_chip(map, chip, FL_WRITING);
770 mutex_unlock(&chip->mutex);
774 send_pfow_command(map, LPDDR_WORD_PROGRAM, adr, 0x00, (map_word *)&curval);
776 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->SingleWordProgTime));
778 printk(KERN_WARNING"%s word_program error at: %llx; val: %x\n",
779 map->name, adr, curval);
783 out: put_chip(map, chip);
784 mutex_unlock(&chip->mutex);
788 MODULE_LICENSE("GPL");
790 MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");