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)
310 set_current_state(TASK_UNINTERRUPTIBLE);
311 add_wait_queue(&chip->wq, &wait);
312 mutex_unlock(&chip->mutex);
314 remove_wait_queue(&chip->wq, &wait);
315 mutex_lock(&chip->mutex);
320 static void put_chip(struct map_info *map, struct flchip *chip)
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);
340 shared->erasing = NULL;
341 shared->writing = NULL;
342 } else if (shared->erasing == chip && shared->writing != chip) {
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.
350 mutex_unlock(&shared->lock);
354 mutex_unlock(&shared->lock);
357 switch (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;
369 printk(KERN_ERR "%s: put_chip() called with oldstate %d!\n",
370 map->name, chip->oldstate);
375 static int do_write_buffer(struct map_info *map, struct flchip *chip,
376 unsigned long adr, const struct kvec **pvec,
377 unsigned long *pvec_seek, int len)
379 struct lpddr_private *lpddr = map->fldrv_priv;
381 int ret, wbufsize, word_gap, words;
382 const struct kvec *vec;
383 unsigned long vec_seek;
384 unsigned long prog_buf_ofs;
386 wbufsize = 1 << lpddr->qinfo->BufSizeShift;
388 mutex_lock(&chip->mutex);
389 ret = get_chip(map, chip, FL_WRITING);
391 mutex_unlock(&chip->mutex);
394 /* Figure out the number of words to write */
395 word_gap = (-adr & (map_bankwidth(map)-1));
396 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
400 word_gap = map_bankwidth(map) - word_gap;
402 datum = map_word_ff(map);
405 /* Get the program buffer offset from PFOW register data first*/
406 prog_buf_ofs = map->pfow_base + CMDVAL(map_read(map,
407 map->pfow_base + PFOW_PROGRAM_BUFFER_OFFSET));
409 vec_seek = *pvec_seek;
411 int n = map_bankwidth(map) - word_gap;
413 if (n > vec->iov_len - vec_seek)
414 n = vec->iov_len - vec_seek;
418 if (!word_gap && (len < map_bankwidth(map)))
419 datum = map_word_ff(map);
421 datum = map_word_load_partial(map, datum,
422 vec->iov_base + vec_seek, word_gap, n);
426 if (!len || word_gap == map_bankwidth(map)) {
427 map_write(map, datum, prog_buf_ofs);
428 prog_buf_ofs += map_bankwidth(map);
433 if (vec_seek == vec->iov_len) {
439 *pvec_seek = vec_seek;
442 send_pfow_command(map, LPDDR_BUFF_PROGRAM, adr, wbufsize, NULL);
443 chip->state = FL_WRITING;
444 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->ProgBufferTime));
446 printk(KERN_WARNING"%s Buffer program error: %d at %lx; \n",
447 map->name, ret, adr);
451 out: put_chip(map, chip);
452 mutex_unlock(&chip->mutex);
456 static int do_erase_oneblock(struct mtd_info *mtd, loff_t adr)
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];
464 mutex_lock(&chip->mutex);
465 ret = get_chip(map, chip, FL_ERASING);
467 mutex_unlock(&chip->mutex);
470 send_pfow_command(map, LPDDR_BLOCK_ERASE, adr, 0, NULL);
471 chip->state = FL_ERASING;
472 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->BlockEraseTime)*1000);
474 printk(KERN_WARNING"%s Erase block error %d at : %llx\n",
475 map->name, ret, adr);
478 out: put_chip(map, chip);
479 mutex_unlock(&chip->mutex);
483 static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
484 size_t *retlen, u_char *buf)
486 struct map_info *map = mtd->priv;
487 struct lpddr_private *lpddr = map->fldrv_priv;
488 int chipnum = adr >> lpddr->chipshift;
489 struct flchip *chip = &lpddr->chips[chipnum];
492 mutex_lock(&chip->mutex);
493 ret = get_chip(map, chip, FL_READY);
495 mutex_unlock(&chip->mutex);
499 map_copy_from(map, buf, adr, len);
503 mutex_unlock(&chip->mutex);
507 static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
508 size_t *retlen, void **mtdbuf, resource_size_t *phys)
510 struct map_info *map = mtd->priv;
511 struct lpddr_private *lpddr = map->fldrv_priv;
512 int chipnum = adr >> lpddr->chipshift;
513 unsigned long ofs, last_end = 0;
514 struct flchip *chip = &lpddr->chips[chipnum];
520 /* ofs: offset within the first chip that the first read should start */
521 ofs = adr - (chipnum << lpddr->chipshift);
522 *mtdbuf = (void *)map->virt + chip->start + ofs;
525 unsigned long thislen;
527 if (chipnum >= lpddr->numchips)
530 /* We cannot point across chips that are virtually disjoint */
532 last_end = chip->start;
533 else if (chip->start != last_end)
536 if ((len + ofs - 1) >> lpddr->chipshift)
537 thislen = (1<<lpddr->chipshift) - ofs;
541 mutex_lock(&chip->mutex);
542 ret = get_chip(map, chip, FL_POINT);
543 mutex_unlock(&chip->mutex);
547 chip->state = FL_POINT;
548 chip->ref_point_counter++;
553 last_end += 1 << lpddr->chipshift;
555 chip = &lpddr->chips[chipnum];
560 static int lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
562 struct map_info *map = mtd->priv;
563 struct lpddr_private *lpddr = map->fldrv_priv;
564 int chipnum = adr >> lpddr->chipshift, err = 0;
567 /* ofs: offset within the first chip that the first read should start */
568 ofs = adr - (chipnum << lpddr->chipshift);
571 unsigned long thislen;
574 chip = &lpddr->chips[chipnum];
575 if (chipnum >= lpddr->numchips)
578 if ((len + ofs - 1) >> lpddr->chipshift)
579 thislen = (1<<lpddr->chipshift) - ofs;
583 mutex_lock(&chip->mutex);
584 if (chip->state == FL_POINT) {
585 chip->ref_point_counter--;
586 if (chip->ref_point_counter == 0)
587 chip->state = FL_READY;
589 printk(KERN_WARNING "%s: Warning: unpoint called on non"
590 "pointed region\n", map->name);
595 mutex_unlock(&chip->mutex);
605 static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
606 size_t *retlen, const u_char *buf)
610 vec.iov_base = (void *) buf;
613 return lpddr_writev(mtd, &vec, 1, to, retlen);
617 static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
618 unsigned long count, loff_t to, size_t *retlen)
620 struct map_info *map = mtd->priv;
621 struct lpddr_private *lpddr = map->fldrv_priv;
624 unsigned long ofs, vec_seek, i;
625 int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
628 for (i = 0; i < count; i++)
629 len += vecs[i].iov_len;
634 chipnum = to >> lpddr->chipshift;
640 /* We must not cross write block boundaries */
641 int size = wbufsize - (ofs & (wbufsize-1));
646 ret = do_write_buffer(map, &lpddr->chips[chipnum],
647 ofs, &vecs, &vec_seek, size);
655 /* Be nice and reschedule with the chip in a usable
656 * state for other processes */
664 static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
666 unsigned long ofs, len;
668 struct map_info *map = mtd->priv;
669 struct lpddr_private *lpddr = map->fldrv_priv;
670 int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
676 ret = do_erase_oneblock(mtd, ofs);
686 #define DO_XXLOCK_LOCK 1
687 #define DO_XXLOCK_UNLOCK 2
688 static int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
691 struct map_info *map = mtd->priv;
692 struct lpddr_private *lpddr = map->fldrv_priv;
693 int chipnum = adr >> lpddr->chipshift;
694 struct flchip *chip = &lpddr->chips[chipnum];
696 mutex_lock(&chip->mutex);
697 ret = get_chip(map, chip, FL_LOCKING);
699 mutex_unlock(&chip->mutex);
703 if (thunk == DO_XXLOCK_LOCK) {
704 send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
705 chip->state = FL_LOCKING;
706 } else if (thunk == DO_XXLOCK_UNLOCK) {
707 send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
708 chip->state = FL_UNLOCKING;
712 ret = wait_for_ready(map, chip, 1);
714 printk(KERN_ERR "%s: block unlock error status %d \n",
718 out: put_chip(map, chip);
719 mutex_unlock(&chip->mutex);
723 static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
725 return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
728 static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
730 return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
733 MODULE_LICENSE("GPL");
735 MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");