blob: 76a4c73e100e836fd2259ee0aa6b944d424e7252 [file] [log] [blame]
Alexey Korolevc6826472008-12-16 18:20:03 +00001/*
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>
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006 * Many thanks to Roman Borisov for initial enabling
Alexey Korolevc6826472008-12-16 18:20:03 +00007 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040030#include <linux/module.h>
Alexey Korolevc6826472008-12-16 18:20:03 +000031
32static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
33 size_t *retlen, u_char *buf);
34static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to,
35 size_t len, size_t *retlen, const u_char *buf);
36static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
37 unsigned long count, loff_t to, size_t *retlen);
38static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr);
39static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
40static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
41static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
42 size_t *retlen, void **mtdbuf, resource_size_t *phys);
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020043static int lpddr_unpoint(struct mtd_info *mtd, loff_t adr, size_t len);
Alexey Korolevc6826472008-12-16 18:20:03 +000044static int get_chip(struct map_info *map, struct flchip *chip, int mode);
45static int chip_ready(struct map_info *map, struct flchip *chip, int mode);
46static void put_chip(struct map_info *map, struct flchip *chip);
47
48struct mtd_info *lpddr_cmdset(struct map_info *map)
49{
50 struct lpddr_private *lpddr = map->fldrv_priv;
51 struct flchip_shared *shared;
52 struct flchip *chip;
53 struct mtd_info *mtd;
54 int numchips;
55 int i, j;
56
57 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Jingoo Han54f5a572014-02-06 15:14:33 +090058 if (!mtd)
Alexey Korolevc6826472008-12-16 18:20:03 +000059 return NULL;
Alexey Korolevc6826472008-12-16 18:20:03 +000060 mtd->priv = map;
61 mtd->type = MTD_NORFLASH;
62
63 /* Fill in the default mtd operations */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020064 mtd->_read = lpddr_read;
Alexey Korolevc6826472008-12-16 18:20:03 +000065 mtd->type = MTD_NORFLASH;
66 mtd->flags = MTD_CAP_NORFLASH;
67 mtd->flags &= ~MTD_BIT_WRITEABLE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020068 mtd->_erase = lpddr_erase;
69 mtd->_write = lpddr_write_buffers;
70 mtd->_writev = lpddr_writev;
71 mtd->_lock = lpddr_lock;
72 mtd->_unlock = lpddr_unlock;
Alexey Korolevc6826472008-12-16 18:20:03 +000073 if (map_is_linear(map)) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020074 mtd->_point = lpddr_point;
75 mtd->_unpoint = lpddr_unpoint;
Alexey Korolevc6826472008-12-16 18:20:03 +000076 }
Alexey Korolevc6826472008-12-16 18:20:03 +000077 mtd->size = 1 << lpddr->qinfo->DevSizeShift;
78 mtd->erasesize = 1 << lpddr->qinfo->UniformBlockSizeShift;
79 mtd->writesize = 1 << lpddr->qinfo->BufSizeShift;
80
Kees Cook6da2ec52018-06-12 13:55:00 -070081 shared = kmalloc_array(lpddr->numchips, sizeof(struct flchip_shared),
Alexey Korolevc6826472008-12-16 18:20:03 +000082 GFP_KERNEL);
83 if (!shared) {
84 kfree(lpddr);
85 kfree(mtd);
86 return NULL;
87 }
88
89 chip = &lpddr->chips[0];
90 numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
91 for (i = 0; i < numchips; i++) {
92 shared[i].writing = shared[i].erasing = NULL;
Stefani Seibold8ae66412010-08-05 09:19:26 +020093 mutex_init(&shared[i].lock);
Alexey Korolevc6826472008-12-16 18:20:03 +000094 for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
95 *chip = lpddr->chips[i];
96 chip->start += j << lpddr->chipshift;
97 chip->oldstate = chip->state = FL_READY;
98 chip->priv = &shared[i];
99 /* those should be reset too since
100 they create memory references. */
101 init_waitqueue_head(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200102 mutex_init(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000103 chip++;
104 }
105 }
106
107 return mtd;
108}
109EXPORT_SYMBOL(lpddr_cmdset);
110
111static 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
119 /* set our timeout to 8 times the expected delay */
120 timeo = chip_op_time * 8;
121 if (!timeo)
122 timeo = 500000;
123 reset_timeo = timeo;
124 sleep_time = chip_op_time / 2;
125
126 for (;;) {
127 dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
128 if (dsr & DSR_READY_STATUS)
129 break;
130 if (!timeo) {
131 printk(KERN_ERR "%s: Flash timeout error state %d \n",
132 map->name, chip_state);
133 ret = -ETIME;
134 break;
135 }
136
137 /* OK Still waiting. Drop the lock, wait a while and retry. */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200138 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000139 if (sleep_time >= 1000000/HZ) {
140 /*
141 * Half of the normal delay still remaining
142 * can be performed with a sleeping delay instead
143 * of busy waiting.
144 */
145 msleep(sleep_time/1000);
146 timeo -= sleep_time;
147 sleep_time = 1000000/HZ;
148 } else {
149 udelay(1);
150 cond_resched();
151 timeo--;
152 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200153 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000154
155 while (chip->state != chip_state) {
156 /* Someone's suspended the operation: sleep */
157 DECLARE_WAITQUEUE(wait, current);
158 set_current_state(TASK_UNINTERRUPTIBLE);
159 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200160 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000161 schedule();
162 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200163 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000164 }
165 if (chip->erase_suspended || chip->write_suspended) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166 /* Suspend has occurred while sleep: reset timeout */
Alexey Korolevc6826472008-12-16 18:20:03 +0000167 timeo = reset_timeo;
168 chip->erase_suspended = chip->write_suspended = 0;
169 }
170 }
171 /* check status for errors */
172 if (dsr & DSR_ERR) {
173 /* Clear DSR*/
174 map_write(map, CMD(~(DSR_ERR)), map->pfow_base + PFOW_DSR);
175 printk(KERN_WARNING"%s: Bad status on wait: 0x%x \n",
176 map->name, dsr);
177 print_drs_error(dsr);
178 ret = -EIO;
179 }
180 chip->state = FL_READY;
181 return ret;
182}
183
184static int get_chip(struct map_info *map, struct flchip *chip, int mode)
185{
186 int ret;
187 DECLARE_WAITQUEUE(wait, current);
188
189 retry:
190 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)
191 && chip->state != FL_SYNCING) {
192 /*
193 * OK. We have possibility for contension on the write/erase
194 * operations which are global to the real chip and not per
195 * partition. So let's fight it over in the partition which
196 * currently has authority on the operation.
197 *
198 * The rules are as follows:
199 *
200 * - any write operation must own shared->writing.
201 *
202 * - any erase operation must own _both_ shared->writing and
203 * shared->erasing.
204 *
205 * - contension arbitration is handled in the owner's context.
206 *
207 * The 'shared' struct can be read and/or written only when
208 * its lock is taken.
209 */
210 struct flchip_shared *shared = chip->priv;
211 struct flchip *contender;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200212 mutex_lock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000213 contender = shared->writing;
214 if (contender && contender != chip) {
215 /*
216 * The engine to perform desired operation on this
217 * partition is already in use by someone else.
218 * Let's fight over it in the context of the chip
219 * currently using it. If it is possible to suspend,
220 * that other partition will do just that, otherwise
221 * it'll happily send us to sleep. In any case, when
222 * get_chip returns success we're clear to go ahead.
223 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200224 ret = mutex_trylock(&contender->mutex);
Stefani Seibold8ae66412010-08-05 09:19:26 +0200225 mutex_unlock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000226 if (!ret)
227 goto retry;
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200228 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000229 ret = chip_ready(map, contender, mode);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200230 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000231
232 if (ret == -EAGAIN) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200233 mutex_unlock(&contender->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000234 goto retry;
235 }
236 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200237 mutex_unlock(&contender->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000238 return ret;
239 }
Stefani Seibold8ae66412010-08-05 09:19:26 +0200240 mutex_lock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000241
242 /* We should not own chip if it is already in FL_SYNCING
243 * state. Put contender and retry. */
244 if (chip->state == FL_SYNCING) {
245 put_chip(map, contender);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200246 mutex_unlock(&contender->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000247 goto retry;
248 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200249 mutex_unlock(&contender->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000250 }
251
252 /* Check if we have suspended erase on this chip.
253 Must sleep in such a case. */
254 if (mode == FL_ERASING && shared->erasing
255 && shared->erasing->oldstate == FL_ERASING) {
Stefani Seibold8ae66412010-08-05 09:19:26 +0200256 mutex_unlock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000257 set_current_state(TASK_UNINTERRUPTIBLE);
258 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200259 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000260 schedule();
261 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200262 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000263 goto retry;
264 }
265
266 /* We now own it */
267 shared->writing = chip;
268 if (mode == FL_ERASING)
269 shared->erasing = chip;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200270 mutex_unlock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000271 }
272
273 ret = chip_ready(map, chip, mode);
274 if (ret == -EAGAIN)
275 goto retry;
276
277 return ret;
278}
279
280static int chip_ready(struct map_info *map, struct flchip *chip, int mode)
281{
282 struct lpddr_private *lpddr = map->fldrv_priv;
283 int ret = 0;
284 DECLARE_WAITQUEUE(wait, current);
285
286 /* Prevent setting state FL_SYNCING for chip in suspended state. */
287 if (FL_SYNCING == mode && FL_READY != chip->oldstate)
288 goto sleep;
289
290 switch (chip->state) {
291 case FL_READY:
292 case FL_JEDEC_QUERY:
293 return 0;
294
295 case FL_ERASING:
296 if (!lpddr->qinfo->SuspEraseSupp ||
297 !(mode == FL_READY || mode == FL_POINT))
298 goto sleep;
299
300 map_write(map, CMD(LPDDR_SUSPEND),
301 map->pfow_base + PFOW_PROGRAM_ERASE_SUSPEND);
302 chip->oldstate = FL_ERASING;
303 chip->state = FL_ERASE_SUSPENDING;
304 ret = wait_for_ready(map, chip, 0);
305 if (ret) {
306 /* Oops. something got wrong. */
307 /* Resume and pretend we weren't here. */
Tadashi Abe100f2342011-05-19 15:58:15 +0900308 put_chip(map, chip);
Alexey Korolevc6826472008-12-16 18:20:03 +0000309 printk(KERN_ERR "%s: suspend operation failed."
310 "State may be wrong \n", map->name);
311 return -EIO;
312 }
313 chip->erase_suspended = 1;
314 chip->state = FL_READY;
315 return 0;
316 /* Erase suspend */
317 case FL_POINT:
318 /* Only if there's no operation suspended... */
319 if (mode == FL_READY && chip->oldstate == FL_READY)
320 return 0;
Gustavo A. R. Silvafae49732019-02-08 12:12:10 -0600321 /* fall through */
Alexey Korolevc6826472008-12-16 18:20:03 +0000322
323 default:
324sleep:
325 set_current_state(TASK_UNINTERRUPTIBLE);
326 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200327 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000328 schedule();
329 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200330 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000331 return -EAGAIN;
332 }
333}
334
335static void put_chip(struct map_info *map, struct flchip *chip)
336{
337 if (chip->priv) {
338 struct flchip_shared *shared = chip->priv;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200339 mutex_lock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000340 if (shared->writing == chip && chip->oldstate == FL_READY) {
341 /* We own the ability to write, but we're done */
342 shared->writing = shared->erasing;
343 if (shared->writing && shared->writing != chip) {
344 /* give back the ownership */
345 struct flchip *loaner = shared->writing;
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200346 mutex_lock(&loaner->mutex);
Stefani Seibold8ae66412010-08-05 09:19:26 +0200347 mutex_unlock(&shared->lock);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200348 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000349 put_chip(map, loaner);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200350 mutex_lock(&chip->mutex);
351 mutex_unlock(&loaner->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000352 wake_up(&chip->wq);
353 return;
354 }
355 shared->erasing = NULL;
356 shared->writing = NULL;
357 } else if (shared->erasing == chip && shared->writing != chip) {
358 /*
359 * We own the ability to erase without the ability
360 * to write, which means the erase was suspended
361 * and some other partition is currently writing.
362 * Don't let the switch below mess things up since
363 * we don't have ownership to resume anything.
364 */
Stefani Seibold8ae66412010-08-05 09:19:26 +0200365 mutex_unlock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000366 wake_up(&chip->wq);
367 return;
368 }
Stefani Seibold8ae66412010-08-05 09:19:26 +0200369 mutex_unlock(&shared->lock);
Alexey Korolevc6826472008-12-16 18:20:03 +0000370 }
371
372 switch (chip->oldstate) {
373 case FL_ERASING:
Alexey Korolevc6826472008-12-16 18:20:03 +0000374 map_write(map, CMD(LPDDR_RESUME),
375 map->pfow_base + PFOW_COMMAND_CODE);
376 map_write(map, CMD(LPDDR_START_EXECUTION),
377 map->pfow_base + PFOW_COMMAND_EXECUTE);
378 chip->oldstate = FL_READY;
379 chip->state = FL_ERASING;
380 break;
381 case FL_READY:
382 break;
383 default:
384 printk(KERN_ERR "%s: put_chip() called with oldstate %d!\n",
385 map->name, chip->oldstate);
386 }
387 wake_up(&chip->wq);
388}
389
Rashika Kheria176e4a22013-12-13 12:44:07 +0530390static int do_write_buffer(struct map_info *map, struct flchip *chip,
Alexey Korolevc6826472008-12-16 18:20:03 +0000391 unsigned long adr, const struct kvec **pvec,
392 unsigned long *pvec_seek, int len)
393{
394 struct lpddr_private *lpddr = map->fldrv_priv;
395 map_word datum;
396 int ret, wbufsize, word_gap, words;
397 const struct kvec *vec;
398 unsigned long vec_seek;
399 unsigned long prog_buf_ofs;
400
401 wbufsize = 1 << lpddr->qinfo->BufSizeShift;
402
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200403 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000404 ret = get_chip(map, chip, FL_WRITING);
405 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200406 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000407 return ret;
408 }
409 /* Figure out the number of words to write */
410 word_gap = (-adr & (map_bankwidth(map)-1));
411 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
412 if (!word_gap) {
413 words--;
414 } else {
415 word_gap = map_bankwidth(map) - word_gap;
416 adr -= word_gap;
417 datum = map_word_ff(map);
418 }
419 /* Write data */
420 /* Get the program buffer offset from PFOW register data first*/
421 prog_buf_ofs = map->pfow_base + CMDVAL(map_read(map,
422 map->pfow_base + PFOW_PROGRAM_BUFFER_OFFSET));
423 vec = *pvec;
424 vec_seek = *pvec_seek;
425 do {
426 int n = map_bankwidth(map) - word_gap;
427
428 if (n > vec->iov_len - vec_seek)
429 n = vec->iov_len - vec_seek;
430 if (n > len)
431 n = len;
432
433 if (!word_gap && (len < map_bankwidth(map)))
434 datum = map_word_ff(map);
435
436 datum = map_word_load_partial(map, datum,
437 vec->iov_base + vec_seek, word_gap, n);
438
439 len -= n;
440 word_gap += n;
441 if (!len || word_gap == map_bankwidth(map)) {
442 map_write(map, datum, prog_buf_ofs);
443 prog_buf_ofs += map_bankwidth(map);
444 word_gap = 0;
445 }
446
447 vec_seek += n;
448 if (vec_seek == vec->iov_len) {
449 vec++;
450 vec_seek = 0;
451 }
452 } while (len);
453 *pvec = vec;
454 *pvec_seek = vec_seek;
455
456 /* GO GO GO */
457 send_pfow_command(map, LPDDR_BUFF_PROGRAM, adr, wbufsize, NULL);
458 chip->state = FL_WRITING;
459 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->ProgBufferTime));
460 if (ret) {
461 printk(KERN_WARNING"%s Buffer program error: %d at %lx; \n",
462 map->name, ret, adr);
463 goto out;
464 }
465
466 out: put_chip(map, chip);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200467 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000468 return ret;
469}
470
Rashika Kheria176e4a22013-12-13 12:44:07 +0530471static int do_erase_oneblock(struct mtd_info *mtd, loff_t adr)
Alexey Korolevc6826472008-12-16 18:20:03 +0000472{
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;
478
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200479 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000480 ret = get_chip(map, chip, FL_ERASING);
481 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200482 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000483 return ret;
484 }
485 send_pfow_command(map, LPDDR_BLOCK_ERASE, adr, 0, NULL);
486 chip->state = FL_ERASING;
487 ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->BlockEraseTime)*1000);
488 if (ret) {
489 printk(KERN_WARNING"%s Erase block error %d at : %llx\n",
490 map->name, ret, adr);
491 goto out;
492 }
493 out: put_chip(map, chip);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200494 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000495 return ret;
496}
497
498static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
499 size_t *retlen, u_char *buf)
500{
501 struct map_info *map = mtd->priv;
502 struct lpddr_private *lpddr = map->fldrv_priv;
503 int chipnum = adr >> lpddr->chipshift;
504 struct flchip *chip = &lpddr->chips[chipnum];
505 int ret = 0;
506
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200507 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000508 ret = get_chip(map, chip, FL_READY);
509 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200510 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000511 return ret;
512 }
513
514 map_copy_from(map, buf, adr, len);
515 *retlen = len;
516
517 put_chip(map, chip);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200518 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000519 return ret;
520}
521
522static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
523 size_t *retlen, void **mtdbuf, resource_size_t *phys)
524{
525 struct map_info *map = mtd->priv;
526 struct lpddr_private *lpddr = map->fldrv_priv;
527 int chipnum = adr >> lpddr->chipshift;
528 unsigned long ofs, last_end = 0;
529 struct flchip *chip = &lpddr->chips[chipnum];
530 int ret = 0;
531
Artem Bityutskiy5def4892012-02-03 16:23:52 +0200532 if (!map->virt)
Alexey Korolevc6826472008-12-16 18:20:03 +0000533 return -EINVAL;
534
535 /* ofs: offset within the first chip that the first read should start */
536 ofs = adr - (chipnum << lpddr->chipshift);
Alexey Korolevc6826472008-12-16 18:20:03 +0000537 *mtdbuf = (void *)map->virt + chip->start + ofs;
Alexey Korolevc6826472008-12-16 18:20:03 +0000538
539 while (len) {
540 unsigned long thislen;
541
542 if (chipnum >= lpddr->numchips)
543 break;
544
545 /* We cannot point across chips that are virtually disjoint */
546 if (!last_end)
547 last_end = chip->start;
548 else if (chip->start != last_end)
549 break;
550
551 if ((len + ofs - 1) >> lpddr->chipshift)
552 thislen = (1<<lpddr->chipshift) - ofs;
553 else
554 thislen = len;
555 /* get the chip */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200556 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000557 ret = get_chip(map, chip, FL_POINT);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200558 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000559 if (ret)
560 break;
561
562 chip->state = FL_POINT;
563 chip->ref_point_counter++;
564 *retlen += thislen;
565 len -= thislen;
566
567 ofs = 0;
568 last_end += 1 << lpddr->chipshift;
569 chipnum++;
570 chip = &lpddr->chips[chipnum];
571 }
572 return 0;
573}
574
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200575static int lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
Alexey Korolevc6826472008-12-16 18:20:03 +0000576{
577 struct map_info *map = mtd->priv;
578 struct lpddr_private *lpddr = map->fldrv_priv;
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200579 int chipnum = adr >> lpddr->chipshift, err = 0;
Alexey Korolevc6826472008-12-16 18:20:03 +0000580 unsigned long ofs;
581
582 /* ofs: offset within the first chip that the first read should start */
583 ofs = adr - (chipnum << lpddr->chipshift);
584
585 while (len) {
586 unsigned long thislen;
587 struct flchip *chip;
588
589 chip = &lpddr->chips[chipnum];
590 if (chipnum >= lpddr->numchips)
591 break;
592
593 if ((len + ofs - 1) >> lpddr->chipshift)
594 thislen = (1<<lpddr->chipshift) - ofs;
595 else
596 thislen = len;
597
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200598 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000599 if (chip->state == FL_POINT) {
600 chip->ref_point_counter--;
601 if (chip->ref_point_counter == 0)
602 chip->state = FL_READY;
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200603 } else {
Alexey Korolevc6826472008-12-16 18:20:03 +0000604 printk(KERN_WARNING "%s: Warning: unpoint called on non"
605 "pointed region\n", map->name);
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200606 err = -EINVAL;
607 }
Alexey Korolevc6826472008-12-16 18:20:03 +0000608
609 put_chip(map, chip);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200610 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000611
612 len -= thislen;
613 ofs = 0;
614 chipnum++;
615 }
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200616
617 return err;
Alexey Korolevc6826472008-12-16 18:20:03 +0000618}
619
620static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
621 size_t *retlen, const u_char *buf)
622{
623 struct kvec vec;
624
625 vec.iov_base = (void *) buf;
626 vec.iov_len = len;
627
628 return lpddr_writev(mtd, &vec, 1, to, retlen);
629}
630
631
632static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
633 unsigned long count, loff_t to, size_t *retlen)
634{
635 struct map_info *map = mtd->priv;
636 struct lpddr_private *lpddr = map->fldrv_priv;
637 int ret = 0;
638 int chipnum;
639 unsigned long ofs, vec_seek, i;
640 int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
Alexey Korolevc6826472008-12-16 18:20:03 +0000641 size_t len = 0;
642
643 for (i = 0; i < count; i++)
644 len += vecs[i].iov_len;
645
Alexey Korolevc6826472008-12-16 18:20:03 +0000646 if (!len)
647 return 0;
648
649 chipnum = to >> lpddr->chipshift;
650
651 ofs = to;
652 vec_seek = 0;
653
654 do {
655 /* We must not cross write block boundaries */
656 int size = wbufsize - (ofs & (wbufsize-1));
657
658 if (size > len)
659 size = len;
660
661 ret = do_write_buffer(map, &lpddr->chips[chipnum],
662 ofs, &vecs, &vec_seek, size);
663 if (ret)
664 return ret;
665
666 ofs += size;
667 (*retlen) += size;
668 len -= size;
669
670 /* Be nice and reschedule with the chip in a usable
671 * state for other processes */
672 cond_resched();
673
674 } while (len);
675
676 return 0;
677}
678
679static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
680{
681 unsigned long ofs, len;
682 int ret;
683 struct map_info *map = mtd->priv;
684 struct lpddr_private *lpddr = map->fldrv_priv;
685 int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
686
687 ofs = instr->addr;
688 len = instr->len;
689
Alexey Korolevc6826472008-12-16 18:20:03 +0000690 while (len > 0) {
691 ret = do_erase_oneblock(mtd, ofs);
692 if (ret)
693 return ret;
694 ofs += size;
695 len -= size;
696 }
Alexey Korolevc6826472008-12-16 18:20:03 +0000697
698 return 0;
699}
700
701#define DO_XXLOCK_LOCK 1
702#define DO_XXLOCK_UNLOCK 2
Brian Norris535ab902013-08-28 11:19:05 -0700703static int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
Alexey Korolevc6826472008-12-16 18:20:03 +0000704{
705 int ret = 0;
706 struct map_info *map = mtd->priv;
707 struct lpddr_private *lpddr = map->fldrv_priv;
708 int chipnum = adr >> lpddr->chipshift;
709 struct flchip *chip = &lpddr->chips[chipnum];
710
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200711 mutex_lock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000712 ret = get_chip(map, chip, FL_LOCKING);
713 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200714 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000715 return ret;
716 }
717
718 if (thunk == DO_XXLOCK_LOCK) {
719 send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
720 chip->state = FL_LOCKING;
721 } else if (thunk == DO_XXLOCK_UNLOCK) {
722 send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
723 chip->state = FL_UNLOCKING;
724 } else
725 BUG();
726
727 ret = wait_for_ready(map, chip, 1);
728 if (ret) {
729 printk(KERN_ERR "%s: block unlock error status %d \n",
730 map->name, ret);
731 goto out;
732 }
733out: put_chip(map, chip);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200734 mutex_unlock(&chip->mutex);
Alexey Korolevc6826472008-12-16 18:20:03 +0000735 return ret;
736}
737
738static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
739{
740 return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
741}
742
743static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
744{
745 return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
746}
747
Alexey Korolevc6826472008-12-16 18:20:03 +0000748MODULE_LICENSE("GPL");
749MODULE_AUTHOR("Alexey Korolev <akorolev@infradead.org>");
750MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");