2 * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
4 * The serial nor interface is largely based on drivers/mtd/m25p80.c,
5 * however the SPI interface has been replaced by SMI.
7 * Copyright © 2010 STMicroelectronics.
9 * Shiraz Hashim <shiraz.hashim@st.com>
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.
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>
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>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/mtd/spear_smi.h>
33 #include <linux/mutex.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/wait.h>
38 #include <linux/of_address.h>
41 #define SMI_MAX_CLOCK_FREQ 50000000 /* 50 MHz */
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)
47 /* timeout for command completion */
48 #define SMI_CMD_TIMEOUT (HZ / 10)
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 */
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 */
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 */
72 #define TX_LEN_SHIFT 0
73 #define RX_LEN_SHIFT 4
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 */
91 #define OPCODE_RDID 0x9f /* Read JEDEC ID */
93 /* Flash Device Ids maintenance section */
95 /* data structure to maintain flash ids from different vendors */
101 unsigned long sectorsize;
102 unsigned long size_in_bytes;
105 #define FLASH_ID(n, es, id, psize, ssize, size) \
111 .sectorsize = ssize, \
112 .size_in_bytes = size \
115 static 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),
152 /* Define spear specific structures */
154 struct spear_snor_flash;
157 * struct spear_smi - Structure for SMI Device
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.
172 unsigned long clk_rate;
174 void __iomem *io_base;
175 struct platform_device *pdev;
176 wait_queue_head_t cmd_complete;
178 struct spear_snor_flash *flash[MAX_NUM_FLASH_CHIP];
182 * struct spear_snor_flash - Structure for Serial NOR Flash
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
195 struct spear_snor_flash {
201 struct mtd_partition *parts;
203 void __iomem *base_addr;
208 static inline struct spear_snor_flash *get_flash_data(struct mtd_info *mtd)
210 return container_of(mtd, struct spear_snor_flash, mtd);
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
218 * This routine will return the status register of the flash chip present at the
221 static int spear_smi_read_sr(struct spear_smi *dev, u32 bank)
226 mutex_lock(&dev->lock);
227 dev->status = 0; /* Will be set in interrupt handler */
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);
233 /* performing a rsr instruction in hw mode */
234 writel((bank << BANK_SHIFT) | RD_STATUS_REG | TFIE,
235 dev->io_base + SMI_CR2);
238 ret = wait_event_interruptible_timeout(dev->cmd_complete,
239 dev->status & TFF, SMI_CMD_TIMEOUT);
241 /* copy dev->status (lower 16 bits) in order to release lock */
243 ret = dev->status & 0xffff;
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);
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
261 * This routine checks for WIP (write in progress) bit in Status register
262 * If successful the routine returns 0 else -EBUSY
264 static int spear_smi_wait_till_ready(struct spear_smi *dev, u32 bank,
265 unsigned long timeout)
267 unsigned long finish;
270 finish = jiffies + timeout;
272 status = spear_smi_read_sr(dev, bank);
274 continue; /* try till timeout */
275 else if (!(status & SR_WIP))
279 } while (!time_after_eq(jiffies, finish));
281 dev_err(&dev->pdev->dev, "smi controller is busy, timeout\n");
286 * spear_smi_int_handler - SMI Interrupt Handler.
288 * @dev_id: structure of SMI device, embedded in dev_id.
290 * The handler clears all interrupt conditions and records the status in
291 * dev->status which is used by the driver later.
293 static irqreturn_t spear_smi_int_handler(int irq, void *dev_id)
296 struct spear_smi *dev = dev_id;
298 status = readl(dev->io_base + SMI_SR);
300 if (unlikely(!status))
303 /* clear all interrupt conditions */
304 writel(0, dev->io_base + SMI_SR);
306 /* copy the status register in dev->status */
307 dev->status |= status;
309 /* send the completion */
310 wake_up_interruptible(&dev->cmd_complete);
316 * spear_smi_hw_init - initializes the smi controller.
317 * @dev: structure of smi device
319 * this routine initializes the smi controller wit the default values
321 static void spear_smi_hw_init(struct spear_smi *dev)
323 unsigned long rate = 0;
327 rate = clk_get_rate(dev->clk);
329 /* functional clock of smi */
330 prescale = DIV_ROUND_UP(rate, dev->clk_rate);
333 * setting the standard values, fast mode, prescaler for
334 * SMI_MAX_CLOCK_FREQ (50MHz) operation and bank enable
336 val = HOLD1 | BANK_EN | DSEL_TIME | (prescale << 8);
338 mutex_lock(&dev->lock);
339 /* clear all interrupt conditions */
340 writel(0, dev->io_base + SMI_SR);
342 writel(val, dev->io_base + SMI_CR1);
343 mutex_unlock(&dev->lock);
347 * get_flash_index - match chip id from a flash list.
348 * @flash_id: a valid nor flash chip id obtained from board.
350 * try to validate the chip id by matching from a list, if not found then simply
351 * returns negative. In case of success returns index in to the flash devices
354 static int get_flash_index(u32 flash_id)
358 /* Matches chip-id to entire list of 'serial-nor flash' ids */
359 for (index = 0; index < ARRAY_SIZE(flash_devices); index++) {
360 if (flash_devices[index].device_id == flash_id)
364 /* Memory chip is not listed and not supported */
369 * spear_smi_write_enable - Enable the flash to do write operation
370 * @dev: structure of SMI device
371 * @bank: enable write for flash connected to this bank
373 * Set write enable latch with Write Enable command.
374 * Returns 0 on success.
376 static int spear_smi_write_enable(struct spear_smi *dev, u32 bank)
381 mutex_lock(&dev->lock);
382 dev->status = 0; /* Will be set in interrupt handler */
384 ctrlreg1 = readl(dev->io_base + SMI_CR1);
385 /* program smi in h/w mode */
386 writel(ctrlreg1 & ~SW_MODE, dev->io_base + SMI_CR1);
388 /* give the flash, write enable command */
389 writel((bank << BANK_SHIFT) | WE | TFIE, dev->io_base + SMI_CR2);
391 ret = wait_event_interruptible_timeout(dev->cmd_complete,
392 dev->status & TFF, SMI_CMD_TIMEOUT);
394 /* restore the ctrl regs state */
395 writel(ctrlreg1, dev->io_base + SMI_CR1);
396 writel(0, dev->io_base + SMI_CR2);
400 dev_err(&dev->pdev->dev,
401 "smi controller failed on write enable\n");
403 /* check whether write mode status is set for required bank */
404 if (dev->status & (1 << (bank + WM_SHIFT)))
407 dev_err(&dev->pdev->dev, "couldn't enable write\n");
412 mutex_unlock(&dev->lock);
417 get_sector_erase_cmd(struct spear_snor_flash *flash, u32 offset)
422 x[0] = flash->erase_cmd;
431 * spear_smi_erase_sector - erase one sector of flash
432 * @dev: structure of SMI information
433 * @command: erase command to be send
434 * @bank: bank to which this command needs to be send
435 * @bytes: size of command
437 * Erase one sector of flash memory at offset ``offset'' which is any
438 * address within the sector which should be erased.
439 * Returns 0 if successful, non-zero otherwise.
441 static int spear_smi_erase_sector(struct spear_smi *dev,
442 u32 bank, u32 command, u32 bytes)
447 ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
451 ret = spear_smi_write_enable(dev, bank);
455 mutex_lock(&dev->lock);
457 ctrlreg1 = readl(dev->io_base + SMI_CR1);
458 writel((ctrlreg1 | SW_MODE) & ~WB_MODE, dev->io_base + SMI_CR1);
460 /* send command in sw mode */
461 writel(command, dev->io_base + SMI_TR);
463 writel((bank << BANK_SHIFT) | SEND | TFIE | (bytes << TX_LEN_SHIFT),
464 dev->io_base + SMI_CR2);
466 ret = wait_event_interruptible_timeout(dev->cmd_complete,
467 dev->status & TFF, SMI_CMD_TIMEOUT);
471 dev_err(&dev->pdev->dev, "sector erase failed\n");
473 ret = 0; /* success */
475 /* restore ctrl regs */
476 writel(ctrlreg1, dev->io_base + SMI_CR1);
477 writel(0, dev->io_base + SMI_CR2);
479 mutex_unlock(&dev->lock);
484 * spear_mtd_erase - perform flash erase operation as requested by user
485 * @mtd: Provides the memory characteristics
486 * @e_info: Provides the erase information
488 * Erase an address range on the flash chip. The address range may extend
489 * one or more erase sectors. Return an error is there is a problem erasing.
491 static int spear_mtd_erase(struct mtd_info *mtd, struct erase_info *e_info)
493 struct spear_snor_flash *flash = get_flash_data(mtd);
494 struct spear_smi *dev = mtd->priv;
495 u32 addr, command, bank;
502 if (bank > dev->num_flashes - 1) {
503 dev_err(&dev->pdev->dev, "Invalid Bank Num");
510 mutex_lock(&flash->lock);
512 /* now erase sectors in loop */
514 command = get_sector_erase_cmd(flash, addr);
515 /* preparing the command for flash */
516 ret = spear_smi_erase_sector(dev, bank, command, 4);
518 e_info->state = MTD_ERASE_FAILED;
519 mutex_unlock(&flash->lock);
522 addr += mtd->erasesize;
523 len -= mtd->erasesize;
526 mutex_unlock(&flash->lock);
527 e_info->state = MTD_ERASE_DONE;
528 mtd_erase_callback(e_info);
534 * spear_mtd_read - performs flash read operation as requested by the user
535 * @mtd: MTD information of the memory bank
536 * @from: Address from which to start read
537 * @len: Number of bytes to be read
538 * @retlen: Fills the Number of bytes actually read
539 * @buf: Fills this after reading
541 * Read an address range from the flash chip. The address range
542 * may be any size provided it is within the physical boundaries.
543 * Returns 0 on success, non zero otherwise
545 static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
546 size_t *retlen, u8 *buf)
548 struct spear_snor_flash *flash = get_flash_data(mtd);
549 struct spear_smi *dev = mtd->priv;
557 if (flash->bank > dev->num_flashes - 1) {
558 dev_err(&dev->pdev->dev, "Invalid Bank Num");
562 /* select address as per bank number */
563 src = flash->base_addr + from;
565 mutex_lock(&flash->lock);
567 /* wait till previous write/erase is done. */
568 ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT);
570 mutex_unlock(&flash->lock);
574 mutex_lock(&dev->lock);
575 /* put smi in hw mode not wbt mode */
576 ctrlreg1 = val = readl(dev->io_base + SMI_CR1);
577 val &= ~(SW_MODE | WB_MODE);
578 if (flash->fast_mode)
581 writel(val, dev->io_base + SMI_CR1);
583 memcpy_fromio(buf, (u8 *)src, len);
585 /* restore ctrl reg1 */
586 writel(ctrlreg1, dev->io_base + SMI_CR1);
587 mutex_unlock(&dev->lock);
590 mutex_unlock(&flash->lock);
595 static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank,
596 void *dest, const void *src, size_t len)
601 /* wait until finished previous write command. */
602 ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
606 /* put smi in write enable */
607 ret = spear_smi_write_enable(dev, bank);
611 /* put smi in hw, write burst mode */
612 mutex_lock(&dev->lock);
614 ctrlreg1 = readl(dev->io_base + SMI_CR1);
615 writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1);
617 memcpy_toio(dest, src, len);
619 writel(ctrlreg1, dev->io_base + SMI_CR1);
621 mutex_unlock(&dev->lock);
626 * spear_mtd_write - performs write operation as requested by the user.
627 * @mtd: MTD information of the memory bank.
628 * @to: Address to write.
629 * @len: Number of bytes to be written.
630 * @retlen: Number of bytes actually wrote.
631 * @buf: Buffer from which the data to be taken.
633 * Write an address range to the flash chip. Data must be written in
634 * flash_page_size chunks. The address range may be any size provided
635 * it is within the physical boundaries.
636 * Returns 0 on success, non zero otherwise
638 static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
639 size_t *retlen, const u8 *buf)
641 struct spear_snor_flash *flash = get_flash_data(mtd);
642 struct spear_smi *dev = mtd->priv;
644 u32 page_offset, page_size;
650 if (flash->bank > dev->num_flashes - 1) {
651 dev_err(&dev->pdev->dev, "Invalid Bank Num");
655 /* select address as per bank number */
656 dest = flash->base_addr + to;
657 mutex_lock(&flash->lock);
659 page_offset = (u32)to % flash->page_size;
661 /* do if all the bytes fit onto one page */
662 if (page_offset + len <= flash->page_size) {
663 ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len);
669 /* the size of data remaining on the first page */
670 page_size = flash->page_size - page_offset;
672 ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf,
677 *retlen += page_size;
679 /* write everything in pagesize chunks */
680 for (i = page_size; i < len; i += page_size) {
682 if (page_size > flash->page_size)
683 page_size = flash->page_size;
685 ret = spear_smi_cpy_toio(dev, flash->bank, dest + i,
690 *retlen += page_size;
695 mutex_unlock(&flash->lock);
701 * spear_smi_probe_flash - Detects the NOR Flash chip.
702 * @dev: structure of SMI information.
703 * @bank: bank on which flash must be probed
705 * This routine will check whether there exists a flash chip on a given memory
707 * Return index of the probed flash in flash devices structure
709 static int spear_smi_probe_flash(struct spear_smi *dev, u32 bank)
714 ret = spear_smi_wait_till_ready(dev, bank, SMI_PROBE_TIMEOUT);
718 mutex_lock(&dev->lock);
720 dev->status = 0; /* Will be set in interrupt handler */
721 /* put smi in sw mode */
722 val = readl(dev->io_base + SMI_CR1);
723 writel(val | SW_MODE, dev->io_base + SMI_CR1);
725 /* send readid command in sw mode */
726 writel(OPCODE_RDID, dev->io_base + SMI_TR);
728 val = (bank << BANK_SHIFT) | SEND | (1 << TX_LEN_SHIFT) |
729 (3 << RX_LEN_SHIFT) | TFIE;
730 writel(val, dev->io_base + SMI_CR2);
733 ret = wait_event_interruptible_timeout(dev->cmd_complete,
734 dev->status & TFF, SMI_CMD_TIMEOUT);
740 /* get memory chip id */
741 val = readl(dev->io_base + SMI_RR);
743 ret = get_flash_index(val);
747 val = readl(dev->io_base + SMI_CR1);
748 writel(val & ~SW_MODE, dev->io_base + SMI_CR1);
750 mutex_unlock(&dev->lock);
756 static int __devinit spear_smi_probe_config_dt(struct platform_device *pdev,
757 struct device_node *np)
759 struct spear_smi_plat_data *pdata = dev_get_platdata(&pdev->dev);
760 struct device_node *pp = NULL;
769 of_property_read_u32(np, "clock-rate", &val);
770 pdata->clk_rate = val;
772 pdata->board_flash_info = devm_kzalloc(&pdev->dev,
773 sizeof(*pdata->board_flash_info),
776 /* Fill structs for each subnode (flash device) */
777 while ((pp = of_get_next_child(np, pp))) {
778 struct spear_smi_flash_info *flash_info;
780 flash_info = &pdata->board_flash_info[i];
783 /* Read base-addr and size from DT */
784 addr = of_get_property(pp, "reg", &len);
785 pdata->board_flash_info->mem_base = be32_to_cpup(&addr[0]);
786 pdata->board_flash_info->size = be32_to_cpup(&addr[1]);
788 if (of_get_property(pp, "st,smi-fast-mode", NULL))
789 pdata->board_flash_info->fast_mode = 1;
794 pdata->num_flashes = i;
799 static int __devinit spear_smi_probe_config_dt(struct platform_device *pdev,
800 struct device_node *np)
806 static int spear_smi_setup_banks(struct platform_device *pdev,
807 u32 bank, struct device_node *np)
809 struct spear_smi *dev = platform_get_drvdata(pdev);
810 struct mtd_part_parser_data ppdata = {};
811 struct spear_smi_flash_info *flash_info;
812 struct spear_smi_plat_data *pdata;
813 struct spear_snor_flash *flash;
814 struct mtd_partition *parts = NULL;
819 pdata = dev_get_platdata(&pdev->dev);
820 if (bank > pdata->num_flashes - 1)
823 flash_info = &pdata->board_flash_info[bank];
827 flash = kzalloc(sizeof(*flash), GFP_ATOMIC);
831 flash->fast_mode = flash_info->fast_mode ? 1 : 0;
832 mutex_init(&flash->lock);
834 /* verify whether nor flash is really present on board */
835 flash_index = spear_smi_probe_flash(dev, bank);
836 if (flash_index < 0) {
837 dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank);
841 /* map the memory for nor flash chip */
842 flash->base_addr = ioremap(flash_info->mem_base, flash_info->size);
843 if (!flash->base_addr) {
848 dev->flash[bank] = flash;
849 flash->mtd.priv = dev;
851 if (flash_info->name)
852 flash->mtd.name = flash_info->name;
854 flash->mtd.name = flash_devices[flash_index].name;
856 flash->mtd.type = MTD_NORFLASH;
857 flash->mtd.writesize = 1;
858 flash->mtd.flags = MTD_CAP_NORFLASH;
859 flash->mtd.size = flash_info->size;
860 flash->mtd.erasesize = flash_devices[flash_index].sectorsize;
861 flash->page_size = flash_devices[flash_index].pagesize;
862 flash->mtd.writebufsize = flash->page_size;
863 flash->erase_cmd = flash_devices[flash_index].erase_cmd;
864 flash->mtd._erase = spear_mtd_erase;
865 flash->mtd._read = spear_mtd_read;
866 flash->mtd._write = spear_mtd_write;
867 flash->dev_id = flash_devices[flash_index].device_id;
869 dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n",
870 flash->mtd.name, flash->mtd.size,
871 flash->mtd.size / (1024 * 1024));
873 dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n",
874 flash->mtd.erasesize, flash->mtd.erasesize / 1024);
877 if (flash_info->partitions) {
878 parts = flash_info->partitions;
879 count = flash_info->nr_partitions;
884 ret = mtd_device_parse_register(&flash->mtd, NULL, &ppdata, parts,
887 dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret);
894 iounmap(flash->base_addr);
902 * spear_smi_probe - Entry routine
903 * @pdev: platform device structure
905 * This is the first routine which gets invoked during booting and does all
906 * initialization/allocation work. The routine looks for available memory banks,
907 * and do proper init for any found one.
908 * Returns 0 on success, non zero otherwise
910 static int __devinit spear_smi_probe(struct platform_device *pdev)
912 struct device_node *np = pdev->dev.of_node;
913 struct spear_smi_plat_data *pdata = NULL;
914 struct spear_smi *dev;
915 struct resource *smi_base;
920 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
922 pr_err("%s: ERROR: no memory", __func__);
926 pdev->dev.platform_data = pdata;
927 ret = spear_smi_probe_config_dt(pdev, np);
930 dev_err(&pdev->dev, "no platform data\n");
934 pdata = dev_get_platdata(&pdev->dev);
937 dev_err(&pdev->dev, "no platform data\n");
942 smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
945 dev_err(&pdev->dev, "invalid smi base address\n");
949 irq = platform_get_irq(pdev, 0);
952 dev_err(&pdev->dev, "invalid smi irq\n");
956 dev = kzalloc(sizeof(*dev), GFP_ATOMIC);
959 dev_err(&pdev->dev, "mem alloc fail\n");
963 smi_base = request_mem_region(smi_base->start, resource_size(smi_base),
967 dev_err(&pdev->dev, "request mem region fail\n");
971 dev->io_base = ioremap(smi_base->start, resource_size(smi_base));
974 dev_err(&pdev->dev, "ioremap fail\n");
979 dev->clk_rate = pdata->clk_rate;
981 if (dev->clk_rate < 0 || dev->clk_rate > SMI_MAX_CLOCK_FREQ)
982 dev->clk_rate = SMI_MAX_CLOCK_FREQ;
984 dev->num_flashes = pdata->num_flashes;
986 if (dev->num_flashes > MAX_NUM_FLASH_CHIP) {
987 dev_err(&pdev->dev, "exceeding max number of flashes\n");
988 dev->num_flashes = MAX_NUM_FLASH_CHIP;
991 dev->clk = clk_get(&pdev->dev, NULL);
992 if (IS_ERR(dev->clk)) {
993 ret = PTR_ERR(dev->clk);
997 ret = clk_prepare_enable(dev->clk);
999 goto err_clk_prepare_enable;
1001 ret = request_irq(irq, spear_smi_int_handler, 0, pdev->name, dev);
1003 dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n");
1007 mutex_init(&dev->lock);
1008 init_waitqueue_head(&dev->cmd_complete);
1009 spear_smi_hw_init(dev);
1010 platform_set_drvdata(pdev, dev);
1012 /* loop for each serial nor-flash which is connected to smi */
1013 for (i = 0; i < dev->num_flashes; i++) {
1014 ret = spear_smi_setup_banks(pdev, i, pdata->np[i]);
1016 dev_err(&dev->pdev->dev, "bank setup failed\n");
1017 goto err_bank_setup;
1025 platform_set_drvdata(pdev, NULL);
1027 clk_disable_unprepare(dev->clk);
1028 err_clk_prepare_enable:
1031 iounmap(dev->io_base);
1033 release_mem_region(smi_base->start, resource_size(smi_base));
1041 * spear_smi_remove - Exit routine
1042 * @pdev: platform device structure
1044 * free all allocations and delete the partitions.
1046 static int __devexit spear_smi_remove(struct platform_device *pdev)
1048 struct spear_smi *dev;
1049 struct spear_smi_plat_data *pdata;
1050 struct spear_snor_flash *flash;
1051 struct resource *smi_base;
1055 dev = platform_get_drvdata(pdev);
1057 dev_err(&pdev->dev, "dev is null\n");
1061 pdata = dev_get_platdata(&pdev->dev);
1063 /* clean up for all nor flash */
1064 for (i = 0; i < dev->num_flashes; i++) {
1065 flash = dev->flash[i];
1069 /* clean up mtd stuff */
1070 ret = mtd_device_unregister(&flash->mtd);
1072 dev_err(&pdev->dev, "error removing mtd\n");
1074 iounmap(flash->base_addr);
1078 irq = platform_get_irq(pdev, 0);
1081 clk_disable_unprepare(dev->clk);
1083 iounmap(dev->io_base);
1086 smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1087 release_mem_region(smi_base->start, resource_size(smi_base));
1088 platform_set_drvdata(pdev, NULL);
1094 static int spear_smi_suspend(struct device *dev)
1096 struct spear_smi *sdev = dev_get_drvdata(dev);
1098 if (sdev && sdev->clk)
1099 clk_disable_unprepare(sdev->clk);
1104 static int spear_smi_resume(struct device *dev)
1106 struct spear_smi *sdev = dev_get_drvdata(dev);
1109 if (sdev && sdev->clk)
1110 ret = clk_prepare_enable(sdev->clk);
1113 spear_smi_hw_init(sdev);
1117 static SIMPLE_DEV_PM_OPS(spear_smi_pm_ops, spear_smi_suspend, spear_smi_resume);
1121 static const struct of_device_id spear_smi_id_table[] = {
1122 { .compatible = "st,spear600-smi" },
1125 MODULE_DEVICE_TABLE(of, spear_smi_id_table);
1128 static struct platform_driver spear_smi_driver = {
1131 .bus = &platform_bus_type,
1132 .owner = THIS_MODULE,
1133 .of_match_table = of_match_ptr(spear_smi_id_table),
1135 .pm = &spear_smi_pm_ops,
1138 .probe = spear_smi_probe,
1139 .remove = __devexit_p(spear_smi_remove),
1142 static int spear_smi_init(void)
1144 return platform_driver_register(&spear_smi_driver);
1146 module_init(spear_smi_init);
1148 static void spear_smi_exit(void)
1150 platform_driver_unregister(&spear_smi_driver);
1152 module_exit(spear_smi_exit);
1154 MODULE_LICENSE("GPL");
1155 MODULE_AUTHOR("Ashish Priyadarshi, Shiraz Hashim <shiraz.hashim@st.com>");
1156 MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");