1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * LPDDR flash memory device operations. This module provides read, write,
4 * erase, lock/unlock support for LPDDR flash memories
7 * Many thanks to Roman Borisov for initial enabling
10 * Implement VPP management
11 * Implement XIP support
12 * Implement OTP support
14 #include <linux/mtd/pfow.h>
15 #include <linux/mtd/qinfo.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
20 size_t *retlen, u_char *buf);
21 static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to,
22 size_t len, size_t *retlen, const u_char *buf);
23 static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
24 unsigned long count, loff_t to, size_t *retlen);
25 static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr);
26 static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
27 static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
28 static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
29 size_t *retlen, void **mtdbuf, resource_size_t *phys);
30 static int lpddr_unpoint(struct mtd_info *mtd, loff_t adr, size_t len);
31 static int get_chip(struct map_info *map, struct flchip *chip, int mode);
32 static int chip_ready(struct map_info *map, struct flchip *chip, int mode);
33 static void put_chip(struct map_info *map, struct flchip *chip);
35 struct mtd_info *lpddr_cmdset(struct map_info *map)
37 struct lpddr_private *lpddr = map->fldrv_priv;
38 struct flchip_shared *shared;
44 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
48 mtd->type = MTD_NORFLASH;
50 /* Fill in the default mtd operations */
51 mtd->_read = lpddr_read;
52 mtd->type = MTD_NORFLASH;
53 mtd->flags = MTD_CAP_NORFLASH;
54 mtd->flags &= ~MTD_BIT_WRITEABLE;
55 mtd->_erase = lpddr_erase;
56 mtd->_write = lpddr_write_buffers;
57 mtd->_writev = lpddr_writev;
58 mtd->_lock = lpddr_lock;
59 mtd->_unlock = lpddr_unlock;
60 if (map_is_linear(map)) {
61 mtd->_point = lpddr_point;
62 mtd->_unpoint = lpddr_unpoint;
64 mtd->size = 1 << lpddr->qinfo->DevSizeShift;
65 mtd->erasesize = 1 << lpddr->qinfo->UniformBlockSizeShift;
66 mtd->writesize = 1 << lpddr->qinfo->BufSizeShift;
68 shared = kmalloc_array(lpddr->numchips, sizeof(struct flchip_shared),
75 chip = &lpddr->chips[0];
76 numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
77 for (i = 0; i < numchips; i++) {
78 shared[i].writing = shared[i].erasing = NULL;
79 mutex_init(&shared[i].lock);
80 for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
81 *chip = lpddr->chips[i];
82 chip->start += j << lpddr->chipshift;
83 chip->oldstate = chip->state = FL_READY;
84 chip->priv = &shared[i];
85 /* those should be reset too since
86 they create memory references. */
87 init_waitqueue_head(&chip->wq);
88 mutex_init(&chip->mutex);
95 EXPORT_SYMBOL(lpddr_cmdset);
97 static int wait_for_ready(struct map_info *map, struct flchip *chip,
98 unsigned int chip_op_time)
100 unsigned int timeo, reset_timeo, sleep_time;
102 flstate_t chip_state = chip->state;
105 /* set our timeout to 8 times the expected delay */
106 timeo = chip_op_time * 8;
110 sleep_time = chip_op_time / 2;
113 dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
114 if (dsr & DSR_READY_STATUS)
117 printk(KERN_ERR "%s: Flash timeout error state %d \n",
118 map->name, chip_state);
123 /* OK Still waiting. Drop the lock, wait a while and retry. */
124 mutex_unlock(&chip->mutex);
125 if (sleep_time >= 1000000/HZ) {
127 * Half of the normal delay still remaining
128 * can be performed with a sleeping delay instead
131 msleep(sleep_time/1000);
133 sleep_time = 1000000/HZ;
139 mutex_lock(&chip->mutex);
141 while (chip->state != chip_state) {
142 /* Someone's suspended the operation: sleep */
143 DECLARE_WAITQUEUE(wait, current);
144 set_current_state(TASK_UNINTERRUPTIBLE);
145 add_wait_queue(&chip->wq, &wait);
146 mutex_unlock(&chip->mutex);
148 remove_wait_queue(&chip->wq, &wait);
149 mutex_lock(&chip->mutex);
151 if (chip->erase_suspended || chip->write_suspended) {
152 /* Suspend has occurred while sleep: reset timeout */
154 chip->erase_suspended = chip->write_suspended = 0;
157 /* check status for errors */
160 map_write(map, CMD(~(DSR_ERR)), map->pfow_base + PFOW_DSR);
161 printk(KERN_WARNING"%s: Bad status on wait: 0x%x \n",
163 print_drs_error(dsr);
166 chip->state = FL_READY;
170 static int get_chip(struct map_info *map, struct flchip *chip, int mode)
173 DECLARE_WAITQUEUE(wait, current);
176 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)
177 && chip->state != FL_SYNCING) {
179 * OK. We have possibility for contension on the write/erase
180 * operations which are global to the real chip and not per
181 * partition. So let's fight it over in the partition which
182 * currently has authority on the operation.
184 * The rules are as follows:
186 * - any write operation must own shared->writing.
188 * - any erase operation must own _both_ shared->writing and
191 * - contension arbitration is handled in the owner's context.
193 * The 'shared' struct can be read and/or written only when
196 struct flchip_shared *shared = chip->priv;
197 struct flchip *contender;
198 mutex_lock(&shared->lock);
199 contender = shared->writing;
200 if (contender && contender != chip) {
202 * The engine to perform desired operation on this
203 * partition is already in use by someone else.
204 * Let's fight over it in the context of the chip
205 * currently using it. If it is possible to suspend,
206 * that other partition will do just that, otherwise
207 * it'll happily send us to sleep. In any case, when
208 * get_chip returns success we're clear to go ahead.
210 ret = mutex_trylock(&contender->mutex);
211 mutex_unlock(&shared->lock);
214 mutex_unlock(&chip->mutex);
215 ret = chip_ready(map, contender, mode);
216 mutex_lock(&chip->mutex);
218 if (ret == -EAGAIN) {
219 mutex_unlock(&contender->mutex);
223 mutex_unlock(&contender->mutex);
226 mutex_lock(&shared->lock);
228 /* We should not own chip if it is already in FL_SYNCING
229 * state. Put contender and retry. */
230 if (chip->state == FL_SYNCING) {
231 put_chip(map, contender);
232 mutex_unlock(&contender->mutex);
235 mutex_unlock(&contender->mutex);
238 /* Check if we have suspended erase on this chip.
239 Must sleep in such a case. */
240 if (mode == FL_ERASING && shared->erasing
241 && shared->erasing->oldstate == FL_ERASING) {
242 mutex_unlock(&shared->lock);
243 set_current_state(TASK_UNINTERRUPTIBLE);
244 add_wait_queue(&chip->wq, &wait);
245 mutex_unlock(&chip->mutex);
247 remove_wait_queue(&chip->wq, &wait);
248 mutex_lock(&chip->mutex);
253 shared->writing = chip;
254 if (mode == FL_ERASING)
255 shared->erasing = chip;
256 mutex_unlock(&shared->lock);
259 ret = chip_ready(map, chip, mode);
266 static int chip_ready(struct map_info *map, struct flchip *chip, int mode)
268 struct lpddr_private *lpddr = map->fldrv_priv;
270 DECLARE_WAITQUEUE(wait, current);
272 /* Prevent setting state FL_SYNCING for chip in suspended state. */
273 if (FL_SYNCING == mode && FL_READY != chip->oldstate)
276 switch (chip->state) {
282 if (!lpddr->qinfo->SuspEraseSupp ||
283 !(mode == FL_READY || mode == FL_POINT))
286 map_write(map, CMD(LPDDR_SUSPEND),
287 map->pfow_base + PFOW_PROGRAM_ERASE_SUSPEND);
288 chip->oldstate = FL_ERASING;
289 chip->state = FL_ERASE_SUSPENDING;
290 ret = wait_for_ready(map, chip, 0);
292 /* Oops. something got wrong. */
293 /* Resume and pretend we weren't here. */
295 printk(KERN_ERR "%s: suspend operation failed."
296 "State may be wrong \n", map->name);
299 chip->erase_suspended = 1;
300 chip->state = FL_READY;
304 /* Only if there's no operation suspended... */
305 if (mode == FL_READY && chip->oldstate == FL_READY)
311 set_current_state(TASK_UNINTERRUPTIBLE);
312 add_wait_queue(&chip->wq, &wait);
313 mutex_unlock(&chip->mutex);
315 remove_wait_queue(&chip->wq, &wait);
316 mutex_lock(&chip->mutex);
321 static void put_chip(struct map_info *map, struct flchip *chip)
324 struct flchip_shared *shared = chip->priv;
325 mutex_lock(&shared->lock);
326 if (shared->writing == chip && chip->oldstate == FL_READY) {
327 /* We own the ability to write, but we're done */
328 shared->writing = shared->erasing;
329 if (shared->writing && shared->writing != chip) {
330 /* give back the ownership */
331 struct flchip *loaner = shared->writing;
332 mutex_lock(&loaner->mutex);
333 mutex_unlock(&shared->lock);
334 mutex_unlock(&chip->mutex);
335 put_chip(map, loaner);
336 mutex_lock(&chip->mutex);
337 mutex_unlock(&loaner->mutex);
341 shared->erasing = NULL;
342 shared->writing = NULL;
343 } else if (shared->erasing == chip && shared->writing != chip) {
345 * We own the ability to erase without the ability
346 * to write, which means the erase was suspended
347 * and some other partition is currently writing.
348 * Don't let the switch below mess things up since
349 * we don't have ownership to resume anything.
351 mutex_unlock(&shared->lock);
355 mutex_unlock(&shared->lock);
358 switch (chip->oldstate) {
360 map_write(map, CMD(LPDDR_RESUME),
361 map->pfow_base + PFOW_COMMAND_CODE);
362 map_write(map, CMD(LPDDR_START_EXECUTION),
363 map->pfow_base + PFOW_COMMAND_EXECUTE);
364 chip->oldstate = FL_READY;
365 chip->state = FL_ERASING;
370 printk(KERN_ERR "%s: put_chip() called with oldstate %d!\n",
371 map->name, chip->oldstate);
376 static int do_write_buffer(struct map_info *map, struct flchip *chip,
377 unsigned long adr, const struct kvec **pvec,
378 unsigned long *pvec_seek, int len)
380 struct lpddr_private *lpddr = map->fldrv_priv;
382 int ret, wbufsize, word_gap, words;
383 const struct kvec *vec;
384 unsigned long vec_seek;
385 unsigned long prog_buf_ofs;
387 wbufsize = 1 << lpddr->qinfo->BufSizeShift;
389 mutex_lock(&chip->mutex);
390 ret = get_chip(map, chip, FL_WRITING);
392 mutex_unlock(&chip->mutex);
395 /* Figure out the number of words to write */
396 word_gap = (-adr & (map_bankwidth(map)-1));
397 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
401 word_gap = map_bankwidth(map) - word_gap;
403 datum = map_word_ff(map);
406 /* Get the program buffer offset from PFOW register data first*/
407 prog_buf_ofs = map->pfow_base + CMDVAL(map_read(map,
408 map->pfow_base + PFOW_PROGRAM_BUFFER_OFFSET));
410 vec_seek = *pvec_seek;
412 int n = map_bankwidth(map) - word_gap;
414 if (n > vec->iov_len - vec_seek)
415 n = vec->iov_len - vec_seek;
419 if (!word_gap && (len < map_bankwidth(map)))
420 datum = map_word_ff(map);
422 datum = map_word_load_partial(map, datum,
423 vec->iov_base + vec_seek, word_gap, n);
427 if (!len || word_gap == map_bankwidth(map)) {
428 map_write(map, datum, prog_buf_ofs);
429 prog_buf_ofs += map_bankwidth(map);
434 if (vec_seek == vec->iov_len) {
440 *pvec_seek = vec_seek;
443 send_pfow_command(map, LPDDR_BUFF_PROGRAM, adr, wbufsize, NULL);
444 chip->state = FL_WRITING;
445 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->ProgBufferTime));
447 printk(KERN_WARNING"%s Buffer program error: %d at %lx; \n",
448 map->name, ret, adr);
452 out: put_chip(map, chip);
453 mutex_unlock(&chip->mutex);
457 static int do_erase_oneblock(struct mtd_info *mtd, loff_t adr)
459 struct map_info *map = mtd->priv;
460 struct lpddr_private *lpddr = map->fldrv_priv;
461 int chipnum = adr >> lpddr->chipshift;
462 struct flchip *chip = &lpddr->chips[chipnum];
465 mutex_lock(&chip->mutex);
466 ret = get_chip(map, chip, FL_ERASING);
468 mutex_unlock(&chip->mutex);
471 send_pfow_command(map, LPDDR_BLOCK_ERASE, adr, 0, NULL);
472 chip->state = FL_ERASING;
473 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->BlockEraseTime)*1000);
475 printk(KERN_WARNING"%s Erase block error %d at : %llx\n",
476 map->name, ret, adr);
479 out: put_chip(map, chip);
480 mutex_unlock(&chip->mutex);
484 static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
485 size_t *retlen, u_char *buf)
487 struct map_info *map = mtd->priv;
488 struct lpddr_private *lpddr = map->fldrv_priv;
489 int chipnum = adr >> lpddr->chipshift;
490 struct flchip *chip = &lpddr->chips[chipnum];
493 mutex_lock(&chip->mutex);
494 ret = get_chip(map, chip, FL_READY);
496 mutex_unlock(&chip->mutex);
500 map_copy_from(map, buf, adr, len);
504 mutex_unlock(&chip->mutex);
508 static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
509 size_t *retlen, void **mtdbuf, resource_size_t *phys)
511 struct map_info *map = mtd->priv;
512 struct lpddr_private *lpddr = map->fldrv_priv;
513 int chipnum = adr >> lpddr->chipshift;
514 unsigned long ofs, last_end = 0;
515 struct flchip *chip = &lpddr->chips[chipnum];
521 /* ofs: offset within the first chip that the first read should start */
522 ofs = adr - (chipnum << lpddr->chipshift);
523 *mtdbuf = (void *)map->virt + chip->start + ofs;
526 unsigned long thislen;
528 if (chipnum >= lpddr->numchips)
531 /* We cannot point across chips that are virtually disjoint */
533 last_end = chip->start;
534 else if (chip->start != last_end)
537 if ((len + ofs - 1) >> lpddr->chipshift)
538 thislen = (1<<lpddr->chipshift) - ofs;
542 mutex_lock(&chip->mutex);
543 ret = get_chip(map, chip, FL_POINT);
544 mutex_unlock(&chip->mutex);
548 chip->state = FL_POINT;
549 chip->ref_point_counter++;
554 last_end += 1 << lpddr->chipshift;
556 chip = &lpddr->chips[chipnum];
561 static int lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
563 struct map_info *map = mtd->priv;
564 struct lpddr_private *lpddr = map->fldrv_priv;
565 int chipnum = adr >> lpddr->chipshift, err = 0;
568 /* ofs: offset within the first chip that the first read should start */
569 ofs = adr - (chipnum << lpddr->chipshift);
572 unsigned long thislen;
575 chip = &lpddr->chips[chipnum];
576 if (chipnum >= lpddr->numchips)
579 if ((len + ofs - 1) >> lpddr->chipshift)
580 thislen = (1<<lpddr->chipshift) - ofs;
584 mutex_lock(&chip->mutex);
585 if (chip->state == FL_POINT) {
586 chip->ref_point_counter--;
587 if (chip->ref_point_counter == 0)
588 chip->state = FL_READY;
590 printk(KERN_WARNING "%s: Warning: unpoint called on non"
591 "pointed region\n", map->name);
596 mutex_unlock(&chip->mutex);
606 static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
607 size_t *retlen, const u_char *buf)
611 vec.iov_base = (void *) buf;
614 return lpddr_writev(mtd, &vec, 1, to, retlen);
618 static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
619 unsigned long count, loff_t to, size_t *retlen)
621 struct map_info *map = mtd->priv;
622 struct lpddr_private *lpddr = map->fldrv_priv;
625 unsigned long ofs, vec_seek, i;
626 int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
629 for (i = 0; i < count; i++)
630 len += vecs[i].iov_len;
635 chipnum = to >> lpddr->chipshift;
641 /* We must not cross write block boundaries */
642 int size = wbufsize - (ofs & (wbufsize-1));
647 ret = do_write_buffer(map, &lpddr->chips[chipnum],
648 ofs, &vecs, &vec_seek, size);
656 /* Be nice and reschedule with the chip in a usable
657 * state for other processes */
665 static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
667 unsigned long ofs, len;
669 struct map_info *map = mtd->priv;
670 struct lpddr_private *lpddr = map->fldrv_priv;
671 int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
677 ret = do_erase_oneblock(mtd, ofs);
687 #define DO_XXLOCK_LOCK 1
688 #define DO_XXLOCK_UNLOCK 2
689 static int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
692 struct map_info *map = mtd->priv;
693 struct lpddr_private *lpddr = map->fldrv_priv;
694 int chipnum = adr >> lpddr->chipshift;
695 struct flchip *chip = &lpddr->chips[chipnum];
697 mutex_lock(&chip->mutex);
698 ret = get_chip(map, chip, FL_LOCKING);
700 mutex_unlock(&chip->mutex);
704 if (thunk == DO_XXLOCK_LOCK) {
705 send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
706 chip->state = FL_LOCKING;
707 } else if (thunk == DO_XXLOCK_UNLOCK) {
708 send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
709 chip->state = FL_UNLOCKING;
713 ret = wait_for_ready(map, chip, 1);
715 printk(KERN_ERR "%s: block unlock error status %d \n",
719 out: put_chip(map, chip);
720 mutex_unlock(&chip->mutex);
724 static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
726 return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
729 static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
731 return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
734 MODULE_LICENSE("GPL");
736 MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");