blob: f7f34fd1a3e5ba6c955155f8cdc7eb685a14b102 [file] [log] [blame]
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +01001/*
2 * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
3 * SPEAr platform
4 * The serial nor interface is largely based on drivers/mtd/m25p80.c,
5 * however the SPI interface has been replaced by SMI.
6 *
7 * Copyright © 2010 STMicroelectronics.
8 * Ashish Priyadarshi
9 * Shiraz Hashim <shiraz.hashim@st.com>
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/device.h>
19#include <linux/err.h>
20#include <linux/errno.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/jiffies.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/param.h>
28#include <linux/platform_device.h>
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/partitions.h>
31#include <linux/mtd/spear_smi.h>
32#include <linux/mutex.h>
33#include <linux/sched.h>
34#include <linux/slab.h>
35#include <linux/wait.h>
36
37/* max possible slots for serial-nor flash chip in the SMI controller */
38#define MAX_NUM_FLASH_CHIP 4
39
40/* SMI clock rate */
41#define SMI_MAX_CLOCK_FREQ 50000000 /* 50 MHz */
42
43/* MAX time out to safely come out of a erase or write busy conditions */
44#define SMI_PROBE_TIMEOUT (HZ / 10)
45#define SMI_MAX_TIME_OUT (3 * HZ)
46
47/* timeout for command completion */
48#define SMI_CMD_TIMEOUT (HZ / 10)
49
50/* registers of smi */
51#define SMI_CR1 0x0 /* SMI control register 1 */
52#define SMI_CR2 0x4 /* SMI control register 2 */
53#define SMI_SR 0x8 /* SMI status register */
54#define SMI_TR 0xC /* SMI transmit register */
55#define SMI_RR 0x10 /* SMI receive register */
56
57/* defines for control_reg 1 */
58#define BANK_EN (0xF << 0) /* enables all banks */
59#define DSEL_TIME (0x6 << 4) /* Deselect time 6 + 1 SMI_CK periods */
60#define SW_MODE (0x1 << 28) /* enables SW Mode */
61#define WB_MODE (0x1 << 29) /* Write Burst Mode */
62#define FAST_MODE (0x1 << 15) /* Fast Mode */
63#define HOLD1 (0x1 << 16) /* Clock Hold period selection */
64
65/* defines for control_reg 2 */
66#define SEND (0x1 << 7) /* Send data */
67#define TFIE (0x1 << 8) /* Transmission Flag Interrupt Enable */
68#define WCIE (0x1 << 9) /* Write Complete Interrupt Enable */
69#define RD_STATUS_REG (0x1 << 10) /* reads status reg */
70#define WE (0x1 << 11) /* Write Enable */
71
72#define TX_LEN_SHIFT 0
73#define RX_LEN_SHIFT 4
74#define BANK_SHIFT 12
75
76/* defines for status register */
77#define SR_WIP 0x1 /* Write in progress */
78#define SR_WEL 0x2 /* Write enable latch */
79#define SR_BP0 0x4 /* Block protect 0 */
80#define SR_BP1 0x8 /* Block protect 1 */
81#define SR_BP2 0x10 /* Block protect 2 */
82#define SR_SRWD 0x80 /* SR write protect */
83#define TFF 0x100 /* Transfer Finished Flag */
84#define WCF 0x200 /* Transfer Finished Flag */
85#define ERF1 0x400 /* Forbidden Write Request */
86#define ERF2 0x800 /* Forbidden Access */
87
88#define WM_SHIFT 12
89
90/* flash opcodes */
91#define OPCODE_RDID 0x9f /* Read JEDEC ID */
92
93/* Flash Device Ids maintenance section */
94
95/* data structure to maintain flash ids from different vendors */
96struct flash_device {
97 char *name;
98 u8 erase_cmd;
99 u32 device_id;
100 u32 pagesize;
101 unsigned long sectorsize;
102 unsigned long size_in_bytes;
103};
104
105#define FLASH_ID(n, es, id, psize, ssize, size) \
106{ \
107 .name = n, \
108 .erase_cmd = es, \
109 .device_id = id, \
110 .pagesize = psize, \
111 .sectorsize = ssize, \
112 .size_in_bytes = size \
113}
114
115static struct flash_device flash_devices[] = {
116 FLASH_ID("st m25p16" , 0xd8, 0x00152020, 0x100, 0x10000, 0x200000),
117 FLASH_ID("st m25p32" , 0xd8, 0x00162020, 0x100, 0x10000, 0x400000),
118 FLASH_ID("st m25p64" , 0xd8, 0x00172020, 0x100, 0x10000, 0x800000),
119 FLASH_ID("st m25p128" , 0xd8, 0x00182020, 0x100, 0x40000, 0x1000000),
120 FLASH_ID("st m25p05" , 0xd8, 0x00102020, 0x80 , 0x8000 , 0x10000),
121 FLASH_ID("st m25p10" , 0xd8, 0x00112020, 0x80 , 0x8000 , 0x20000),
122 FLASH_ID("st m25p20" , 0xd8, 0x00122020, 0x100, 0x10000, 0x40000),
123 FLASH_ID("st m25p40" , 0xd8, 0x00132020, 0x100, 0x10000, 0x80000),
124 FLASH_ID("st m25p80" , 0xd8, 0x00142020, 0x100, 0x10000, 0x100000),
125 FLASH_ID("st m45pe10" , 0xd8, 0x00114020, 0x100, 0x10000, 0x20000),
126 FLASH_ID("st m45pe20" , 0xd8, 0x00124020, 0x100, 0x10000, 0x40000),
127 FLASH_ID("st m45pe40" , 0xd8, 0x00134020, 0x100, 0x10000, 0x80000),
128 FLASH_ID("st m45pe80" , 0xd8, 0x00144020, 0x100, 0x10000, 0x100000),
129 FLASH_ID("sp s25fl004" , 0xd8, 0x00120201, 0x100, 0x10000, 0x80000),
130 FLASH_ID("sp s25fl008" , 0xd8, 0x00130201, 0x100, 0x10000, 0x100000),
131 FLASH_ID("sp s25fl016" , 0xd8, 0x00140201, 0x100, 0x10000, 0x200000),
132 FLASH_ID("sp s25fl032" , 0xd8, 0x00150201, 0x100, 0x10000, 0x400000),
133 FLASH_ID("sp s25fl064" , 0xd8, 0x00160201, 0x100, 0x10000, 0x800000),
134 FLASH_ID("atmel 25f512" , 0x52, 0x0065001F, 0x80 , 0x8000 , 0x10000),
135 FLASH_ID("atmel 25f1024" , 0x52, 0x0060001F, 0x100, 0x8000 , 0x20000),
136 FLASH_ID("atmel 25f2048" , 0x52, 0x0063001F, 0x100, 0x10000, 0x40000),
137 FLASH_ID("atmel 25f4096" , 0x52, 0x0064001F, 0x100, 0x10000, 0x80000),
138 FLASH_ID("atmel 25fs040" , 0xd7, 0x0004661F, 0x100, 0x10000, 0x80000),
139 FLASH_ID("mac 25l512" , 0xd8, 0x001020C2, 0x010, 0x10000, 0x10000),
140 FLASH_ID("mac 25l1005" , 0xd8, 0x001120C2, 0x010, 0x10000, 0x20000),
141 FLASH_ID("mac 25l2005" , 0xd8, 0x001220C2, 0x010, 0x10000, 0x40000),
142 FLASH_ID("mac 25l4005" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
143 FLASH_ID("mac 25l4005a" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
144 FLASH_ID("mac 25l8005" , 0xd8, 0x001420C2, 0x010, 0x10000, 0x100000),
145 FLASH_ID("mac 25l1605" , 0xd8, 0x001520C2, 0x100, 0x10000, 0x200000),
146 FLASH_ID("mac 25l1605a" , 0xd8, 0x001520C2, 0x010, 0x10000, 0x200000),
147 FLASH_ID("mac 25l3205" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
148 FLASH_ID("mac 25l3205a" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
149 FLASH_ID("mac 25l6405" , 0xd8, 0x001720C2, 0x100, 0x10000, 0x800000),
150};
151
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100152/* Define spear specific structures */
153
154struct spear_snor_flash;
155
156/**
157 * struct spear_smi - Structure for SMI Device
158 *
159 * @clk: functional clock
160 * @status: current status register of SMI.
161 * @clk_rate: functional clock rate of SMI (default: SMI_MAX_CLOCK_FREQ)
162 * @lock: lock to prevent parallel access of SMI.
163 * @io_base: base address for registers of SMI.
164 * @pdev: platform device
165 * @cmd_complete: queue to wait for command completion of NOR-flash.
166 * @num_flashes: number of flashes actually present on board.
167 * @flash: separate structure for each Serial NOR-flash attached to SMI.
168 */
169struct spear_smi {
170 struct clk *clk;
171 u32 status;
172 unsigned long clk_rate;
173 struct mutex lock;
174 void __iomem *io_base;
175 struct platform_device *pdev;
176 wait_queue_head_t cmd_complete;
177 u32 num_flashes;
178 struct spear_snor_flash *flash[MAX_NUM_FLASH_CHIP];
179};
180
181/**
182 * struct spear_snor_flash - Structure for Serial NOR Flash
183 *
184 * @bank: Bank number(0, 1, 2, 3) for each NOR-flash.
185 * @dev_id: Device ID of NOR-flash.
186 * @lock: lock to manage flash read, write and erase operations
187 * @mtd: MTD info for each NOR-flash.
188 * @num_parts: Total number of partition in each bank of NOR-flash.
189 * @parts: Partition info for each bank of NOR-flash.
190 * @page_size: Page size of NOR-flash.
191 * @base_addr: Base address of NOR-flash.
192 * @erase_cmd: erase command may vary on different flash types
193 * @fast_mode: flash supports read in fast mode
194 */
195struct spear_snor_flash {
196 u32 bank;
197 u32 dev_id;
198 struct mutex lock;
199 struct mtd_info mtd;
200 u32 num_parts;
201 struct mtd_partition *parts;
202 u32 page_size;
203 void __iomem *base_addr;
204 u8 erase_cmd;
205 u8 fast_mode;
206};
207
208static inline struct spear_snor_flash *get_flash_data(struct mtd_info *mtd)
209{
210 return container_of(mtd, struct spear_snor_flash, mtd);
211}
212
213/**
214 * spear_smi_read_sr - Read status register of flash through SMI
215 * @dev: structure of SMI information.
216 * @bank: bank to which flash is connected
217 *
218 * This routine will return the status register of the flash chip present at the
219 * given bank.
220 */
221static int spear_smi_read_sr(struct spear_smi *dev, u32 bank)
222{
223 int ret;
224 u32 ctrlreg1;
225
226 mutex_lock(&dev->lock);
227 dev->status = 0; /* Will be set in interrupt handler */
228
229 ctrlreg1 = readl(dev->io_base + SMI_CR1);
230 /* program smi in hw mode */
231 writel(ctrlreg1 & ~(SW_MODE | WB_MODE), dev->io_base + SMI_CR1);
232
233 /* performing a rsr instruction in hw mode */
234 writel((bank << BANK_SHIFT) | RD_STATUS_REG | TFIE,
235 dev->io_base + SMI_CR2);
236
237 /* wait for tff */
238 ret = wait_event_interruptible_timeout(dev->cmd_complete,
239 dev->status & TFF, SMI_CMD_TIMEOUT);
240
241 /* copy dev->status (lower 16 bits) in order to release lock */
242 if (ret > 0)
243 ret = dev->status & 0xffff;
244 else
245 ret = -EIO;
246
247 /* restore the ctrl regs state */
248 writel(ctrlreg1, dev->io_base + SMI_CR1);
249 writel(0, dev->io_base + SMI_CR2);
250 mutex_unlock(&dev->lock);
251
252 return ret;
253}
254
255/**
256 * spear_smi_wait_till_ready - wait till flash is ready
257 * @dev: structure of SMI information.
258 * @bank: flash corresponding to this bank
259 * @timeout: timeout for busy wait condition
260 *
261 * This routine checks for WIP (write in progress) bit in Status register
262 * If successful the routine returns 0 else -EBUSY
263 */
264static int spear_smi_wait_till_ready(struct spear_smi *dev, u32 bank,
265 unsigned long timeout)
266{
267 unsigned long finish;
268 int status;
269
270 finish = jiffies + timeout;
271 do {
272 status = spear_smi_read_sr(dev, bank);
273 if (status < 0)
274 continue; /* try till timeout */
275 else if (!(status & SR_WIP))
276 return 0;
277
278 cond_resched();
279 } while (!time_after_eq(jiffies, finish));
280
281 dev_err(&dev->pdev->dev, "smi controller is busy, timeout\n");
282 return status;
283}
284
285/**
286 * spear_smi_int_handler - SMI Interrupt Handler.
287 * @irq: irq number
288 * @dev_id: structure of SMI device, embedded in dev_id.
289 *
290 * The handler clears all interrupt conditions and records the status in
291 * dev->status which is used by the driver later.
292 */
293static irqreturn_t spear_smi_int_handler(int irq, void *dev_id)
294{
295 u32 status = 0;
296 struct spear_smi *dev = dev_id;
297
298 status = readl(dev->io_base + SMI_SR);
299
300 if (unlikely(!status))
301 return IRQ_NONE;
302
303 /* clear all interrupt conditions */
304 writel(0, dev->io_base + SMI_SR);
305
306 /* copy the status register in dev->status */
307 dev->status |= status;
308
309 /* send the completion */
310 wake_up_interruptible(&dev->cmd_complete);
311
312 return IRQ_HANDLED;
313}
314
315/**
316 * spear_smi_hw_init - initializes the smi controller.
317 * @dev: structure of smi device
318 *
319 * this routine initializes the smi controller wit the default values
320 */
321static void spear_smi_hw_init(struct spear_smi *dev)
322{
323 unsigned long rate = 0;
324 u32 prescale = 0;
325 u32 val;
326
327 rate = clk_get_rate(dev->clk);
328
329 /* functional clock of smi */
330 prescale = DIV_ROUND_UP(rate, dev->clk_rate);
331
332 /*
333 * setting the standard values, fast mode, prescaler for
334 * SMI_MAX_CLOCK_FREQ (50MHz) operation and bank enable
335 */
336 val = HOLD1 | BANK_EN | DSEL_TIME | (prescale << 8);
337
338 mutex_lock(&dev->lock);
339 writel(val, dev->io_base + SMI_CR1);
340 mutex_unlock(&dev->lock);
341}
342
343/**
344 * get_flash_index - match chip id from a flash list.
345 * @flash_id: a valid nor flash chip id obtained from board.
346 *
347 * try to validate the chip id by matching from a list, if not found then simply
348 * returns negative. In case of success returns index in to the flash devices
349 * array.
350 */
351static int get_flash_index(u32 flash_id)
352{
353 int index;
354
355 /* Matches chip-id to entire list of 'serial-nor flash' ids */
356 for (index = 0; index < ARRAY_SIZE(flash_devices); index++) {
357 if (flash_devices[index].device_id == flash_id)
358 return index;
359 }
360
361 /* Memory chip is not listed and not supported */
362 return -ENODEV;
363}
364
365/**
366 * spear_smi_write_enable - Enable the flash to do write operation
367 * @dev: structure of SMI device
368 * @bank: enable write for flash connected to this bank
369 *
370 * Set write enable latch with Write Enable command.
371 * Returns 0 on success.
372 */
373static int spear_smi_write_enable(struct spear_smi *dev, u32 bank)
374{
375 int ret;
376 u32 ctrlreg1;
377
378 mutex_lock(&dev->lock);
379 dev->status = 0; /* Will be set in interrupt handler */
380
381 ctrlreg1 = readl(dev->io_base + SMI_CR1);
382 /* program smi in h/w mode */
383 writel(ctrlreg1 & ~SW_MODE, dev->io_base + SMI_CR1);
384
385 /* give the flash, write enable command */
386 writel((bank << BANK_SHIFT) | WE | TFIE, dev->io_base + SMI_CR2);
387
388 ret = wait_event_interruptible_timeout(dev->cmd_complete,
389 dev->status & TFF, SMI_CMD_TIMEOUT);
390
391 /* restore the ctrl regs state */
392 writel(ctrlreg1, dev->io_base + SMI_CR1);
393 writel(0, dev->io_base + SMI_CR2);
394
395 if (ret <= 0) {
396 ret = -EIO;
397 dev_err(&dev->pdev->dev,
398 "smi controller failed on write enable\n");
399 } else {
400 /* check whether write mode status is set for required bank */
401 if (dev->status & (1 << (bank + WM_SHIFT)))
402 ret = 0;
403 else {
404 dev_err(&dev->pdev->dev, "couldn't enable write\n");
405 ret = -EIO;
406 }
407 }
408
409 mutex_unlock(&dev->lock);
410 return ret;
411}
412
413static inline u32
414get_sector_erase_cmd(struct spear_snor_flash *flash, u32 offset)
415{
416 u32 cmd;
417 u8 *x = (u8 *)&cmd;
418
419 x[0] = flash->erase_cmd;
420 x[1] = offset >> 16;
421 x[2] = offset >> 8;
422 x[3] = offset;
423
424 return cmd;
425}
426
427/**
428 * spear_smi_erase_sector - erase one sector of flash
429 * @dev: structure of SMI information
430 * @command: erase command to be send
431 * @bank: bank to which this command needs to be send
432 * @bytes: size of command
433 *
434 * Erase one sector of flash memory at offset ``offset'' which is any
435 * address within the sector which should be erased.
436 * Returns 0 if successful, non-zero otherwise.
437 */
438static int spear_smi_erase_sector(struct spear_smi *dev,
439 u32 bank, u32 command, u32 bytes)
440{
441 u32 ctrlreg1 = 0;
442 int ret;
443
444 ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
445 if (ret)
446 return ret;
447
448 ret = spear_smi_write_enable(dev, bank);
449 if (ret)
450 return ret;
451
452 mutex_lock(&dev->lock);
453
454 ctrlreg1 = readl(dev->io_base + SMI_CR1);
455 writel((ctrlreg1 | SW_MODE) & ~WB_MODE, dev->io_base + SMI_CR1);
456
457 /* send command in sw mode */
458 writel(command, dev->io_base + SMI_TR);
459
460 writel((bank << BANK_SHIFT) | SEND | TFIE | (bytes << TX_LEN_SHIFT),
461 dev->io_base + SMI_CR2);
462
463 ret = wait_event_interruptible_timeout(dev->cmd_complete,
464 dev->status & TFF, SMI_CMD_TIMEOUT);
465
466 if (ret <= 0) {
467 ret = -EIO;
468 dev_err(&dev->pdev->dev, "sector erase failed\n");
469 } else
470 ret = 0; /* success */
471
472 /* restore ctrl regs */
473 writel(ctrlreg1, dev->io_base + SMI_CR1);
474 writel(0, dev->io_base + SMI_CR2);
475
476 mutex_unlock(&dev->lock);
477 return ret;
478}
479
480/**
481 * spear_mtd_erase - perform flash erase operation as requested by user
482 * @mtd: Provides the memory characteristics
483 * @e_info: Provides the erase information
484 *
485 * Erase an address range on the flash chip. The address range may extend
486 * one or more erase sectors. Return an error is there is a problem erasing.
487 */
488static int spear_mtd_erase(struct mtd_info *mtd, struct erase_info *e_info)
489{
490 struct spear_snor_flash *flash = get_flash_data(mtd);
491 struct spear_smi *dev = mtd->priv;
492 u32 addr, command, bank;
493 int len, ret;
494
495 if (!flash || !dev)
496 return -ENODEV;
497
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100498 bank = flash->bank;
499 if (bank > dev->num_flashes - 1) {
500 dev_err(&dev->pdev->dev, "Invalid Bank Num");
501 return -EINVAL;
502 }
503
504 addr = e_info->addr;
505 len = e_info->len;
506
507 mutex_lock(&flash->lock);
508
509 /* now erase sectors in loop */
510 while (len) {
511 command = get_sector_erase_cmd(flash, addr);
512 /* preparing the command for flash */
513 ret = spear_smi_erase_sector(dev, bank, command, 4);
514 if (ret) {
515 e_info->state = MTD_ERASE_FAILED;
516 mutex_unlock(&flash->lock);
517 return ret;
518 }
519 addr += mtd->erasesize;
520 len -= mtd->erasesize;
521 }
522
523 mutex_unlock(&flash->lock);
524 e_info->state = MTD_ERASE_DONE;
525 mtd_erase_callback(e_info);
526
527 return 0;
528}
529
530/**
531 * spear_mtd_read - performs flash read operation as requested by the user
532 * @mtd: MTD information of the memory bank
533 * @from: Address from which to start read
534 * @len: Number of bytes to be read
535 * @retlen: Fills the Number of bytes actually read
536 * @buf: Fills this after reading
537 *
538 * Read an address range from the flash chip. The address range
539 * may be any size provided it is within the physical boundaries.
540 * Returns 0 on success, non zero otherwise
541 */
542static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
543 size_t *retlen, u8 *buf)
544{
545 struct spear_snor_flash *flash = get_flash_data(mtd);
546 struct spear_smi *dev = mtd->priv;
547 void *src;
548 u32 ctrlreg1, val;
549 int ret;
550
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100551 if (!flash || !dev)
552 return -ENODEV;
553
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100554 if (flash->bank > dev->num_flashes - 1) {
555 dev_err(&dev->pdev->dev, "Invalid Bank Num");
556 return -EINVAL;
557 }
558
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100559 /* select address as per bank number */
560 src = flash->base_addr + from;
561
562 mutex_lock(&flash->lock);
563
564 /* wait till previous write/erase is done. */
565 ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT);
566 if (ret) {
567 mutex_unlock(&flash->lock);
568 return ret;
569 }
570
571 mutex_lock(&dev->lock);
572 /* put smi in hw mode not wbt mode */
573 ctrlreg1 = val = readl(dev->io_base + SMI_CR1);
574 val &= ~(SW_MODE | WB_MODE);
575 if (flash->fast_mode)
576 val |= FAST_MODE;
577
578 writel(val, dev->io_base + SMI_CR1);
579
580 memcpy_fromio(buf, (u8 *)src, len);
581
582 /* restore ctrl reg1 */
583 writel(ctrlreg1, dev->io_base + SMI_CR1);
584 mutex_unlock(&dev->lock);
585
586 *retlen = len;
587 mutex_unlock(&flash->lock);
588
589 return 0;
590}
591
592static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank,
593 void *dest, const void *src, size_t len)
594{
595 int ret;
596 u32 ctrlreg1;
597
598 /* wait until finished previous write command. */
599 ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
600 if (ret)
601 return ret;
602
603 /* put smi in write enable */
604 ret = spear_smi_write_enable(dev, bank);
605 if (ret)
606 return ret;
607
608 /* put smi in hw, write burst mode */
609 mutex_lock(&dev->lock);
610
611 ctrlreg1 = readl(dev->io_base + SMI_CR1);
612 writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1);
613
614 memcpy_toio(dest, src, len);
615
616 writel(ctrlreg1, dev->io_base + SMI_CR1);
617
618 mutex_unlock(&dev->lock);
619 return 0;
620}
621
622/**
623 * spear_mtd_write - performs write operation as requested by the user.
624 * @mtd: MTD information of the memory bank.
625 * @to: Address to write.
626 * @len: Number of bytes to be written.
627 * @retlen: Number of bytes actually wrote.
628 * @buf: Buffer from which the data to be taken.
629 *
630 * Write an address range to the flash chip. Data must be written in
631 * flash_page_size chunks. The address range may be any size provided
632 * it is within the physical boundaries.
633 * Returns 0 on success, non zero otherwise
634 */
635static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
636 size_t *retlen, const u8 *buf)
637{
638 struct spear_snor_flash *flash = get_flash_data(mtd);
639 struct spear_smi *dev = mtd->priv;
640 void *dest;
641 u32 page_offset, page_size;
642 int ret;
643
644 if (!flash || !dev)
645 return -ENODEV;
646
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100647 if (flash->bank > dev->num_flashes - 1) {
648 dev_err(&dev->pdev->dev, "Invalid Bank Num");
649 return -EINVAL;
650 }
651
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100652 /* select address as per bank number */
653 dest = flash->base_addr + to;
654 mutex_lock(&flash->lock);
655
656 page_offset = (u32)to % flash->page_size;
657
658 /* do if all the bytes fit onto one page */
659 if (page_offset + len <= flash->page_size) {
660 ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len);
661 if (!ret)
662 *retlen += len;
663 } else {
664 u32 i;
665
666 /* the size of data remaining on the first page */
667 page_size = flash->page_size - page_offset;
668
669 ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf,
670 page_size);
671 if (ret)
672 goto err_write;
673 else
674 *retlen += page_size;
675
676 /* write everything in pagesize chunks */
677 for (i = page_size; i < len; i += page_size) {
678 page_size = len - i;
679 if (page_size > flash->page_size)
680 page_size = flash->page_size;
681
682 ret = spear_smi_cpy_toio(dev, flash->bank, dest + i,
683 buf + i, page_size);
684 if (ret)
685 break;
686 else
687 *retlen += page_size;
688 }
689 }
690
691err_write:
692 mutex_unlock(&flash->lock);
693
694 return ret;
695}
696
697/**
698 * spear_smi_probe_flash - Detects the NOR Flash chip.
699 * @dev: structure of SMI information.
700 * @bank: bank on which flash must be probed
701 *
702 * This routine will check whether there exists a flash chip on a given memory
703 * bank ID.
704 * Return index of the probed flash in flash devices structure
705 */
706static int spear_smi_probe_flash(struct spear_smi *dev, u32 bank)
707{
708 int ret;
709 u32 val = 0;
710
711 ret = spear_smi_wait_till_ready(dev, bank, SMI_PROBE_TIMEOUT);
712 if (ret)
713 return ret;
714
715 mutex_lock(&dev->lock);
716
717 dev->status = 0; /* Will be set in interrupt handler */
718 /* put smi in sw mode */
719 val = readl(dev->io_base + SMI_CR1);
720 writel(val | SW_MODE, dev->io_base + SMI_CR1);
721
722 /* send readid command in sw mode */
723 writel(OPCODE_RDID, dev->io_base + SMI_TR);
724
725 val = (bank << BANK_SHIFT) | SEND | (1 << TX_LEN_SHIFT) |
726 (3 << RX_LEN_SHIFT) | TFIE;
727 writel(val, dev->io_base + SMI_CR2);
728
729 /* wait for TFF */
730 ret = wait_event_interruptible_timeout(dev->cmd_complete,
731 dev->status & TFF, SMI_CMD_TIMEOUT);
732 if (ret <= 0) {
733 ret = -ENODEV;
734 goto err_probe;
735 }
736
737 /* get memory chip id */
738 val = readl(dev->io_base + SMI_RR);
739 val &= 0x00ffffff;
740 ret = get_flash_index(val);
741
742err_probe:
743 /* clear sw mode */
744 val = readl(dev->io_base + SMI_CR1);
745 writel(val & ~SW_MODE, dev->io_base + SMI_CR1);
746
747 mutex_unlock(&dev->lock);
748 return ret;
749}
750
751static int spear_smi_setup_banks(struct platform_device *pdev, u32 bank)
752{
753 struct spear_smi *dev = platform_get_drvdata(pdev);
754 struct spear_smi_flash_info *flash_info;
755 struct spear_smi_plat_data *pdata;
756 struct spear_snor_flash *flash;
Stefan Roesef7e3dd82012-03-16 11:41:40 +0100757 struct mtd_partition *parts = NULL;
758 int count = 0;
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100759 int flash_index;
760 int ret = 0;
761
762 pdata = dev_get_platdata(&pdev->dev);
763 if (bank > pdata->num_flashes - 1)
764 return -EINVAL;
765
766 flash_info = &pdata->board_flash_info[bank];
767 if (!flash_info)
768 return -ENODEV;
769
770 flash = kzalloc(sizeof(*flash), GFP_ATOMIC);
771 if (!flash)
772 return -ENOMEM;
773 flash->bank = bank;
774 flash->fast_mode = flash_info->fast_mode ? 1 : 0;
775 mutex_init(&flash->lock);
776
777 /* verify whether nor flash is really present on board */
778 flash_index = spear_smi_probe_flash(dev, bank);
779 if (flash_index < 0) {
780 dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank);
781 ret = flash_index;
782 goto err_probe;
783 }
784 /* map the memory for nor flash chip */
785 flash->base_addr = ioremap(flash_info->mem_base, flash_info->size);
786 if (!flash->base_addr) {
787 ret = -EIO;
788 goto err_probe;
789 }
790
791 dev->flash[bank] = flash;
792 flash->mtd.priv = dev;
793
794 if (flash_info->name)
795 flash->mtd.name = flash_info->name;
796 else
797 flash->mtd.name = flash_devices[flash_index].name;
798
799 flash->mtd.type = MTD_NORFLASH;
800 flash->mtd.writesize = 1;
801 flash->mtd.flags = MTD_CAP_NORFLASH;
802 flash->mtd.size = flash_info->size;
803 flash->mtd.erasesize = flash_devices[flash_index].sectorsize;
804 flash->page_size = flash_devices[flash_index].pagesize;
Artem Bityutskiy81fefdf2012-02-03 10:14:12 +0200805 flash->mtd.writebufsize = flash->page_size;
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100806 flash->erase_cmd = flash_devices[flash_index].erase_cmd;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200807 flash->mtd._erase = spear_mtd_erase;
808 flash->mtd._read = spear_mtd_read;
809 flash->mtd._write = spear_mtd_write;
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100810 flash->dev_id = flash_devices[flash_index].device_id;
811
812 dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n",
813 flash->mtd.name, flash->mtd.size,
814 flash->mtd.size / (1024 * 1024));
815
816 dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n",
817 flash->mtd.erasesize, flash->mtd.erasesize / 1024);
818
819 if (flash_info->partitions) {
820 parts = flash_info->partitions;
821 count = flash_info->nr_partitions;
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100822 }
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200823 ret = mtd_device_parse_register(&flash->mtd, NULL, NULL, parts, count);
Stefan Roesef7e3dd82012-03-16 11:41:40 +0100824 if (ret) {
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100825 dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret);
Stefan Roesef7e3dd82012-03-16 11:41:40 +0100826 goto err_map;
827 }
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100828
Stefan Roesef7e3dd82012-03-16 11:41:40 +0100829 return 0;
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100830
831err_map:
832 iounmap(flash->base_addr);
833
834err_probe:
835 kfree(flash);
836 return ret;
837}
838
839/**
840 * spear_smi_probe - Entry routine
841 * @pdev: platform device structure
842 *
843 * This is the first routine which gets invoked during booting and does all
844 * initialization/allocation work. The routine looks for available memory banks,
845 * and do proper init for any found one.
846 * Returns 0 on success, non zero otherwise
847 */
848static int __devinit spear_smi_probe(struct platform_device *pdev)
849{
850 struct spear_smi_plat_data *pdata;
851 struct spear_smi *dev;
852 struct resource *smi_base;
853 int irq, ret = 0;
854 int i;
855
856 pdata = dev_get_platdata(&pdev->dev);
857 if (pdata < 0) {
858 ret = -ENODEV;
859 dev_err(&pdev->dev, "no platform data\n");
860 goto err;
861 }
862
863 smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
864 if (!smi_base) {
865 ret = -ENODEV;
866 dev_err(&pdev->dev, "invalid smi base address\n");
867 goto err;
868 }
869
870 irq = platform_get_irq(pdev, 0);
871 if (irq < 0) {
872 ret = -ENODEV;
873 dev_err(&pdev->dev, "invalid smi irq\n");
874 goto err;
875 }
876
877 dev = kzalloc(sizeof(*dev), GFP_ATOMIC);
878 if (!dev) {
879 ret = -ENOMEM;
880 dev_err(&pdev->dev, "mem alloc fail\n");
881 goto err;
882 }
883
884 smi_base = request_mem_region(smi_base->start, resource_size(smi_base),
885 pdev->name);
886 if (!smi_base) {
887 ret = -EBUSY;
888 dev_err(&pdev->dev, "request mem region fail\n");
889 goto err_mem;
890 }
891
892 dev->io_base = ioremap(smi_base->start, resource_size(smi_base));
893 if (!dev->io_base) {
894 ret = -EIO;
895 dev_err(&pdev->dev, "ioremap fail\n");
896 goto err_ioremap;
897 }
898
899 dev->pdev = pdev;
900 dev->clk_rate = pdata->clk_rate;
901
902 if (dev->clk_rate < 0 || dev->clk_rate > SMI_MAX_CLOCK_FREQ)
903 dev->clk_rate = SMI_MAX_CLOCK_FREQ;
904
905 dev->num_flashes = pdata->num_flashes;
906
907 if (dev->num_flashes > MAX_NUM_FLASH_CHIP) {
908 dev_err(&pdev->dev, "exceeding max number of flashes\n");
909 dev->num_flashes = MAX_NUM_FLASH_CHIP;
910 }
911
912 dev->clk = clk_get(&pdev->dev, NULL);
913 if (IS_ERR(dev->clk)) {
914 ret = PTR_ERR(dev->clk);
915 goto err_clk;
916 }
917
918 ret = clk_enable(dev->clk);
919 if (ret)
920 goto err_clk_enable;
921
922 ret = request_irq(irq, spear_smi_int_handler, 0, pdev->name, dev);
923 if (ret) {
924 dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n");
925 goto err_irq;
926 }
927
928 mutex_init(&dev->lock);
929 init_waitqueue_head(&dev->cmd_complete);
930 spear_smi_hw_init(dev);
931 platform_set_drvdata(pdev, dev);
932
933 /* loop for each serial nor-flash which is connected to smi */
934 for (i = 0; i < dev->num_flashes; i++) {
935 ret = spear_smi_setup_banks(pdev, i);
936 if (ret) {
937 dev_err(&dev->pdev->dev, "bank setup failed\n");
938 goto err_bank_setup;
939 }
940 }
941
942 return 0;
943
944err_bank_setup:
945 free_irq(irq, dev);
946 platform_set_drvdata(pdev, NULL);
947err_irq:
948 clk_disable(dev->clk);
949err_clk_enable:
950 clk_put(dev->clk);
951err_clk:
952 iounmap(dev->io_base);
953err_ioremap:
954 release_mem_region(smi_base->start, resource_size(smi_base));
955err_mem:
956 kfree(dev);
957err:
958 return ret;
959}
960
961/**
962 * spear_smi_remove - Exit routine
963 * @pdev: platform device structure
964 *
965 * free all allocations and delete the partitions.
966 */
967static int __devexit spear_smi_remove(struct platform_device *pdev)
968{
969 struct spear_smi *dev;
970 struct spear_snor_flash *flash;
Shiraz Hashim495c47d2012-01-20 11:35:19 +0100971 struct resource *smi_base;
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +0100972 int ret;
973 int i, irq;
974
975 dev = platform_get_drvdata(pdev);
976 if (!dev) {
977 dev_err(&pdev->dev, "dev is null\n");
978 return -ENODEV;
979 }
980
981 /* clean up for all nor flash */
982 for (i = 0; i < dev->num_flashes; i++) {
983 flash = dev->flash[i];
984 if (!flash)
985 continue;
986
987 /* clean up mtd stuff */
988 ret = mtd_device_unregister(&flash->mtd);
989 if (ret)
990 dev_err(&pdev->dev, "error removing mtd\n");
991
992 iounmap(flash->base_addr);
993 kfree(flash);
994 }
995
996 irq = platform_get_irq(pdev, 0);
997 free_irq(irq, dev);
998
999 clk_disable(dev->clk);
1000 clk_put(dev->clk);
1001 iounmap(dev->io_base);
1002 kfree(dev);
Shiraz Hashim495c47d2012-01-20 11:35:19 +01001003
1004 smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1005 release_mem_region(smi_base->start, resource_size(smi_base));
Shiraz Hashimf18dbbb2012-01-12 14:38:57 +01001006 platform_set_drvdata(pdev, NULL);
1007
1008 return 0;
1009}
1010
1011int spear_smi_suspend(struct platform_device *pdev, pm_message_t state)
1012{
1013 struct spear_smi *dev = platform_get_drvdata(pdev);
1014
1015 if (dev && dev->clk)
1016 clk_disable(dev->clk);
1017
1018 return 0;
1019}
1020
1021int spear_smi_resume(struct platform_device *pdev)
1022{
1023 struct spear_smi *dev = platform_get_drvdata(pdev);
1024 int ret = -EPERM;
1025
1026 if (dev && dev->clk)
1027 ret = clk_enable(dev->clk);
1028
1029 if (!ret)
1030 spear_smi_hw_init(dev);
1031 return ret;
1032}
1033
1034static struct platform_driver spear_smi_driver = {
1035 .driver = {
1036 .name = "smi",
1037 .bus = &platform_bus_type,
1038 .owner = THIS_MODULE,
1039 },
1040 .probe = spear_smi_probe,
1041 .remove = __devexit_p(spear_smi_remove),
1042 .suspend = spear_smi_suspend,
1043 .resume = spear_smi_resume,
1044};
1045
1046static int spear_smi_init(void)
1047{
1048 return platform_driver_register(&spear_smi_driver);
1049}
1050module_init(spear_smi_init);
1051
1052static void spear_smi_exit(void)
1053{
1054 platform_driver_unregister(&spear_smi_driver);
1055}
1056module_exit(spear_smi_exit);
1057
1058MODULE_LICENSE("GPL");
1059MODULE_AUTHOR("Ashish Priyadarshi, Shiraz Hashim <shiraz.hashim@st.com>");
1060MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");