Lines Matching +full:flash +full:- +full:mode
2 * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
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 */
90 /* flash opcodes */
93 /* Flash Device Ids maintenance section */
95 /* data structure to maintain flash ids from different vendors */
157 * struct spear_smi - Structure for SMI Device
165 * @cmd_complete: queue to wait for command completion of NOR-flash.
167 * @flash: separate structure for each Serial NOR-flash attached to SMI.
178 struct spear_snor_flash *flash[MAX_NUM_FLASH_CHIP]; member
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
214 * spear_smi_read_sr - Read status register of flash through SMI
216 * @bank: bank to which flash is connected
218 * This routine will return the status register of the flash chip present at the
226 mutex_lock(&dev->lock); in spear_smi_read_sr()
227 dev->status = 0; /* Will be set in interrupt handler */ in spear_smi_read_sr()
229 ctrlreg1 = readl(dev->io_base + SMI_CR1); in spear_smi_read_sr()
230 /* program smi in hw mode */ in spear_smi_read_sr()
231 writel(ctrlreg1 & ~(SW_MODE | WB_MODE), dev->io_base + SMI_CR1); in spear_smi_read_sr()
233 /* performing a rsr instruction in hw mode */ in spear_smi_read_sr()
235 dev->io_base + SMI_CR2); in spear_smi_read_sr()
238 ret = wait_event_interruptible_timeout(dev->cmd_complete, in spear_smi_read_sr()
239 dev->status & TFF, SMI_CMD_TIMEOUT); in spear_smi_read_sr()
241 /* copy dev->status (lower 16 bits) in order to release lock */ in spear_smi_read_sr()
243 ret = dev->status & 0xffff; in spear_smi_read_sr()
245 ret = -ETIMEDOUT; in spear_smi_read_sr()
248 writel(ctrlreg1, dev->io_base + SMI_CR1); in spear_smi_read_sr()
249 writel(0, dev->io_base + SMI_CR2); in spear_smi_read_sr()
250 mutex_unlock(&dev->lock); in spear_smi_read_sr()
256 * spear_smi_wait_till_ready - wait till flash is ready
258 * @bank: flash corresponding to this bank
262 * If successful the routine returns 0 else -EBUSY
274 if (status == -ETIMEDOUT) in spear_smi_wait_till_ready()
284 dev_err(&dev->pdev->dev, "smi controller is busy, timeout\n"); in spear_smi_wait_till_ready()
285 return -EBUSY; in spear_smi_wait_till_ready()
289 * spear_smi_int_handler - SMI Interrupt Handler.
294 * dev->status which is used by the driver later.
301 status = readl(dev->io_base + SMI_SR); in spear_smi_int_handler()
307 writel(0, dev->io_base + SMI_SR); in spear_smi_int_handler()
309 /* copy the status register in dev->status */ in spear_smi_int_handler()
310 dev->status |= status; in spear_smi_int_handler()
313 wake_up_interruptible(&dev->cmd_complete); in spear_smi_int_handler()
319 * spear_smi_hw_init - initializes the smi controller.
330 rate = clk_get_rate(dev->clk); in spear_smi_hw_init()
333 prescale = DIV_ROUND_UP(rate, dev->clk_rate); in spear_smi_hw_init()
336 * setting the standard values, fast mode, prescaler for in spear_smi_hw_init()
341 mutex_lock(&dev->lock); in spear_smi_hw_init()
343 writel(0, dev->io_base + SMI_SR); in spear_smi_hw_init()
345 writel(val, dev->io_base + SMI_CR1); in spear_smi_hw_init()
346 mutex_unlock(&dev->lock); in spear_smi_hw_init()
350 * get_flash_index - match chip id from a flash list.
351 * @flash_id: a valid nor flash chip id obtained from board.
354 * returns negative. In case of success returns index in to the flash devices
361 /* Matches chip-id to entire list of 'serial-nor flash' ids */ in get_flash_index()
368 return -ENODEV; in get_flash_index()
372 * spear_smi_write_enable - Enable the flash to do write operation
374 * @bank: enable write for flash connected to this bank
384 mutex_lock(&dev->lock); in spear_smi_write_enable()
385 dev->status = 0; /* Will be set in interrupt handler */ in spear_smi_write_enable()
387 ctrlreg1 = readl(dev->io_base + SMI_CR1); in spear_smi_write_enable()
388 /* program smi in h/w mode */ in spear_smi_write_enable()
389 writel(ctrlreg1 & ~SW_MODE, dev->io_base + SMI_CR1); in spear_smi_write_enable()
391 /* give the flash, write enable command */ in spear_smi_write_enable()
392 writel((bank << BANK_SHIFT) | WE | TFIE, dev->io_base + SMI_CR2); in spear_smi_write_enable()
394 ret = wait_event_interruptible_timeout(dev->cmd_complete, in spear_smi_write_enable()
395 dev->status & TFF, SMI_CMD_TIMEOUT); in spear_smi_write_enable()
398 writel(ctrlreg1, dev->io_base + SMI_CR1); in spear_smi_write_enable()
399 writel(0, dev->io_base + SMI_CR2); in spear_smi_write_enable()
402 ret = -EIO; in spear_smi_write_enable()
403 dev_err(&dev->pdev->dev, in spear_smi_write_enable()
406 /* check whether write mode status is set for required bank */ in spear_smi_write_enable()
407 if (dev->status & (1 << (bank + WM_SHIFT))) in spear_smi_write_enable()
410 dev_err(&dev->pdev->dev, "couldn't enable write\n"); in spear_smi_write_enable()
411 ret = -EIO; in spear_smi_write_enable()
415 mutex_unlock(&dev->lock); in spear_smi_write_enable()
420 get_sector_erase_cmd(struct spear_snor_flash *flash, u32 offset) in get_sector_erase_cmd() argument
425 x[0] = flash->erase_cmd; in get_sector_erase_cmd()
434 * spear_smi_erase_sector - erase one sector of flash
440 * Erase one sector of flash memory at offset ``offset'' which is any
442 * Returns 0 if successful, non-zero otherwise.
458 mutex_lock(&dev->lock); in spear_smi_erase_sector()
460 ctrlreg1 = readl(dev->io_base + SMI_CR1); in spear_smi_erase_sector()
461 writel((ctrlreg1 | SW_MODE) & ~WB_MODE, dev->io_base + SMI_CR1); in spear_smi_erase_sector()
463 /* send command in sw mode */ in spear_smi_erase_sector()
464 writel(command, dev->io_base + SMI_TR); in spear_smi_erase_sector()
467 dev->io_base + SMI_CR2); in spear_smi_erase_sector()
469 ret = wait_event_interruptible_timeout(dev->cmd_complete, in spear_smi_erase_sector()
470 dev->status & TFF, SMI_CMD_TIMEOUT); in spear_smi_erase_sector()
473 ret = -EIO; in spear_smi_erase_sector()
474 dev_err(&dev->pdev->dev, "sector erase failed\n"); in spear_smi_erase_sector()
479 writel(ctrlreg1, dev->io_base + SMI_CR1); in spear_smi_erase_sector()
480 writel(0, dev->io_base + SMI_CR2); in spear_smi_erase_sector()
482 mutex_unlock(&dev->lock); in spear_smi_erase_sector()
487 * spear_mtd_erase - perform flash erase operation as requested by user
491 * Erase an address range on the flash chip. The address range may extend
496 struct spear_snor_flash *flash = get_flash_data(mtd); in spear_mtd_erase() local
497 struct spear_smi *dev = mtd->priv; in spear_mtd_erase()
501 if (!flash || !dev) in spear_mtd_erase()
502 return -ENODEV; in spear_mtd_erase()
504 bank = flash->bank; in spear_mtd_erase()
505 if (bank > dev->num_flashes - 1) { in spear_mtd_erase()
506 dev_err(&dev->pdev->dev, "Invalid Bank Num"); in spear_mtd_erase()
507 return -EINVAL; in spear_mtd_erase()
510 addr = e_info->addr; in spear_mtd_erase()
511 len = e_info->len; in spear_mtd_erase()
513 mutex_lock(&flash->lock); in spear_mtd_erase()
517 command = get_sector_erase_cmd(flash, addr); in spear_mtd_erase()
518 /* preparing the command for flash */ in spear_mtd_erase()
521 mutex_unlock(&flash->lock); in spear_mtd_erase()
524 addr += mtd->erasesize; in spear_mtd_erase()
525 len -= mtd->erasesize; in spear_mtd_erase()
528 mutex_unlock(&flash->lock); in spear_mtd_erase()
534 * spear_mtd_read - performs flash read operation as requested by the user
541 * Read an address range from the flash chip. The address range
548 struct spear_snor_flash *flash = get_flash_data(mtd); in spear_mtd_read() local
549 struct spear_smi *dev = mtd->priv; in spear_mtd_read()
554 if (!flash || !dev) in spear_mtd_read()
555 return -ENODEV; in spear_mtd_read()
557 if (flash->bank > dev->num_flashes - 1) { in spear_mtd_read()
558 dev_err(&dev->pdev->dev, "Invalid Bank Num"); in spear_mtd_read()
559 return -EINVAL; in spear_mtd_read()
563 src = flash->base_addr + from; in spear_mtd_read()
565 mutex_lock(&flash->lock); in spear_mtd_read()
568 ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT); in spear_mtd_read()
570 mutex_unlock(&flash->lock); in spear_mtd_read()
574 mutex_lock(&dev->lock); in spear_mtd_read()
575 /* put smi in hw mode not wbt mode */ in spear_mtd_read()
576 ctrlreg1 = val = readl(dev->io_base + SMI_CR1); in spear_mtd_read()
578 if (flash->fast_mode) in spear_mtd_read()
581 writel(val, dev->io_base + SMI_CR1); in spear_mtd_read()
586 writel(ctrlreg1, dev->io_base + SMI_CR1); in spear_mtd_read()
587 mutex_unlock(&dev->lock); in spear_mtd_read()
590 mutex_unlock(&flash->lock); in spear_mtd_read()
608 len--; in spear_smi_memcpy_toio_b()
631 /* put smi in hw, write burst mode */ in spear_smi_cpy_toio()
632 mutex_lock(&dev->lock); in spear_smi_cpy_toio()
634 ctrlreg1 = readl(dev->io_base + SMI_CR1); in spear_smi_cpy_toio()
635 writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1); in spear_smi_cpy_toio()
638 * In Write Burst mode (WB_MODE), the specs states that writes must be: in spear_smi_cpy_toio()
639 * - incremental in spear_smi_cpy_toio()
640 * - of the same size in spear_smi_cpy_toio()
642 * I/O by using as much 4-byte writes as possible, surrounded by in spear_smi_cpy_toio()
643 * 2-byte/1-byte access if: in spear_smi_cpy_toio()
644 * - the destination is not 4-byte aligned in spear_smi_cpy_toio()
645 * - the length is not a multiple of 4-byte. in spear_smi_cpy_toio()
655 writel(ctrlreg1, dev->io_base + SMI_CR1); in spear_smi_cpy_toio()
657 mutex_unlock(&dev->lock); in spear_smi_cpy_toio()
662 * spear_mtd_write - performs write operation as requested by the user.
669 * Write an address range to the flash chip. Data must be written in
677 struct spear_snor_flash *flash = get_flash_data(mtd); in spear_mtd_write() local
678 struct spear_smi *dev = mtd->priv; in spear_mtd_write()
683 if (!flash || !dev) in spear_mtd_write()
684 return -ENODEV; in spear_mtd_write()
686 if (flash->bank > dev->num_flashes - 1) { in spear_mtd_write()
687 dev_err(&dev->pdev->dev, "Invalid Bank Num"); in spear_mtd_write()
688 return -EINVAL; in spear_mtd_write()
692 dest = flash->base_addr + to; in spear_mtd_write()
693 mutex_lock(&flash->lock); in spear_mtd_write()
695 page_offset = (u32)to % flash->page_size; in spear_mtd_write()
698 if (page_offset + len <= flash->page_size) { in spear_mtd_write()
699 ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len); in spear_mtd_write()
706 page_size = flash->page_size - page_offset; in spear_mtd_write()
708 ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, in spear_mtd_write()
717 page_size = len - i; in spear_mtd_write()
718 if (page_size > flash->page_size) in spear_mtd_write()
719 page_size = flash->page_size; in spear_mtd_write()
721 ret = spear_smi_cpy_toio(dev, flash->bank, dest + i, in spear_mtd_write()
731 mutex_unlock(&flash->lock); in spear_mtd_write()
737 * spear_smi_probe_flash - Detects the NOR Flash chip.
739 * @bank: bank on which flash must be probed
741 * This routine will check whether there exists a flash chip on a given memory
743 * Return index of the probed flash in flash devices structure
754 mutex_lock(&dev->lock); in spear_smi_probe_flash()
756 dev->status = 0; /* Will be set in interrupt handler */ in spear_smi_probe_flash()
757 /* put smi in sw mode */ in spear_smi_probe_flash()
758 val = readl(dev->io_base + SMI_CR1); in spear_smi_probe_flash()
759 writel(val | SW_MODE, dev->io_base + SMI_CR1); in spear_smi_probe_flash()
761 /* send readid command in sw mode */ in spear_smi_probe_flash()
762 writel(OPCODE_RDID, dev->io_base + SMI_TR); in spear_smi_probe_flash()
766 writel(val, dev->io_base + SMI_CR2); in spear_smi_probe_flash()
769 ret = wait_event_interruptible_timeout(dev->cmd_complete, in spear_smi_probe_flash()
770 dev->status & TFF, SMI_CMD_TIMEOUT); in spear_smi_probe_flash()
772 ret = -ENODEV; in spear_smi_probe_flash()
777 val = readl(dev->io_base + SMI_RR); in spear_smi_probe_flash()
782 /* clear sw mode */ in spear_smi_probe_flash()
783 val = readl(dev->io_base + SMI_CR1); in spear_smi_probe_flash()
784 writel(val & ~SW_MODE, dev->io_base + SMI_CR1); in spear_smi_probe_flash()
786 mutex_unlock(&dev->lock); in spear_smi_probe_flash()
795 struct spear_smi_plat_data *pdata = dev_get_platdata(&pdev->dev); in spear_smi_probe_config_dt()
803 return -ENODEV; in spear_smi_probe_config_dt()
805 of_property_read_u32(np, "clock-rate", &val); in spear_smi_probe_config_dt()
806 pdata->clk_rate = val; in spear_smi_probe_config_dt()
808 pdata->board_flash_info = devm_kzalloc(&pdev->dev, in spear_smi_probe_config_dt()
809 sizeof(*pdata->board_flash_info), in spear_smi_probe_config_dt()
811 if (!pdata->board_flash_info) in spear_smi_probe_config_dt()
812 return -ENOMEM; in spear_smi_probe_config_dt()
814 /* Fill structs for each subnode (flash device) */ in spear_smi_probe_config_dt()
816 pdata->np[i] = pp; in spear_smi_probe_config_dt()
818 /* Read base-addr and size from DT */ in spear_smi_probe_config_dt()
820 pdata->board_flash_info->mem_base = be32_to_cpup(&addr[0]); in spear_smi_probe_config_dt()
821 pdata->board_flash_info->size = be32_to_cpup(&addr[1]); in spear_smi_probe_config_dt()
823 pdata->board_flash_info->fast_mode = in spear_smi_probe_config_dt()
824 of_property_read_bool(pp, "st,smi-fast-mode"); in spear_smi_probe_config_dt()
829 pdata->num_flashes = i; in spear_smi_probe_config_dt()
837 return -ENOSYS; in spear_smi_probe_config_dt()
847 struct spear_snor_flash *flash; in spear_smi_setup_banks() local
853 pdata = dev_get_platdata(&pdev->dev); in spear_smi_setup_banks()
854 if (bank > pdata->num_flashes - 1) in spear_smi_setup_banks()
855 return -EINVAL; in spear_smi_setup_banks()
857 flash_info = &pdata->board_flash_info[bank]; in spear_smi_setup_banks()
859 return -ENODEV; in spear_smi_setup_banks()
861 flash = devm_kzalloc(&pdev->dev, sizeof(*flash), GFP_ATOMIC); in spear_smi_setup_banks()
862 if (!flash) in spear_smi_setup_banks()
863 return -ENOMEM; in spear_smi_setup_banks()
864 flash->bank = bank; in spear_smi_setup_banks()
865 flash->fast_mode = flash_info->fast_mode ? 1 : 0; in spear_smi_setup_banks()
866 mutex_init(&flash->lock); in spear_smi_setup_banks()
868 /* verify whether nor flash is really present on board */ in spear_smi_setup_banks()
871 dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank); in spear_smi_setup_banks()
874 /* map the memory for nor flash chip */ in spear_smi_setup_banks()
875 flash->base_addr = devm_ioremap(&pdev->dev, flash_info->mem_base, in spear_smi_setup_banks()
876 flash_info->size); in spear_smi_setup_banks()
877 if (!flash->base_addr) in spear_smi_setup_banks()
878 return -EIO; in spear_smi_setup_banks()
880 dev->flash[bank] = flash; in spear_smi_setup_banks()
881 flash->mtd.priv = dev; in spear_smi_setup_banks()
883 if (flash_info->name) in spear_smi_setup_banks()
884 flash->mtd.name = flash_info->name; in spear_smi_setup_banks()
886 flash->mtd.name = flash_devices[flash_index].name; in spear_smi_setup_banks()
888 flash->mtd.dev.parent = &pdev->dev; in spear_smi_setup_banks()
889 mtd_set_of_node(&flash->mtd, np); in spear_smi_setup_banks()
890 flash->mtd.type = MTD_NORFLASH; in spear_smi_setup_banks()
891 flash->mtd.writesize = 1; in spear_smi_setup_banks()
892 flash->mtd.flags = MTD_CAP_NORFLASH; in spear_smi_setup_banks()
893 flash->mtd.size = flash_info->size; in spear_smi_setup_banks()
894 flash->mtd.erasesize = flash_devices[flash_index].sectorsize; in spear_smi_setup_banks()
895 flash->page_size = flash_devices[flash_index].pagesize; in spear_smi_setup_banks()
896 flash->mtd.writebufsize = flash->page_size; in spear_smi_setup_banks()
897 flash->erase_cmd = flash_devices[flash_index].erase_cmd; in spear_smi_setup_banks()
898 flash->mtd._erase = spear_mtd_erase; in spear_smi_setup_banks()
899 flash->mtd._read = spear_mtd_read; in spear_smi_setup_banks()
900 flash->mtd._write = spear_mtd_write; in spear_smi_setup_banks()
901 flash->dev_id = flash_devices[flash_index].device_id; in spear_smi_setup_banks()
903 dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n", in spear_smi_setup_banks()
904 flash->mtd.name, flash->mtd.size, in spear_smi_setup_banks()
905 flash->mtd.size / (1024 * 1024)); in spear_smi_setup_banks()
907 dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n", in spear_smi_setup_banks()
908 flash->mtd.erasesize, flash->mtd.erasesize / 1024); in spear_smi_setup_banks()
911 if (flash_info->partitions) { in spear_smi_setup_banks()
912 parts = flash_info->partitions; in spear_smi_setup_banks()
913 count = flash_info->nr_partitions; in spear_smi_setup_banks()
917 ret = mtd_device_register(&flash->mtd, parts, count); in spear_smi_setup_banks()
919 dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret); in spear_smi_setup_banks()
927 * spear_smi_probe - Entry routine
937 struct device_node *np = pdev->dev.of_node; in spear_smi_probe()
944 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); in spear_smi_probe()
946 ret = -ENOMEM; in spear_smi_probe()
949 pdev->dev.platform_data = pdata; in spear_smi_probe()
952 ret = -ENODEV; in spear_smi_probe()
953 dev_err(&pdev->dev, "no platform data\n"); in spear_smi_probe()
957 pdata = dev_get_platdata(&pdev->dev); in spear_smi_probe()
959 ret = -ENODEV; in spear_smi_probe()
960 dev_err(&pdev->dev, "no platform data\n"); in spear_smi_probe()
967 ret = -ENODEV; in spear_smi_probe()
971 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); in spear_smi_probe()
973 ret = -ENOMEM; in spear_smi_probe()
977 dev->io_base = devm_platform_ioremap_resource(pdev, 0); in spear_smi_probe()
978 if (IS_ERR(dev->io_base)) { in spear_smi_probe()
979 ret = PTR_ERR(dev->io_base); in spear_smi_probe()
983 dev->pdev = pdev; in spear_smi_probe()
984 dev->clk_rate = pdata->clk_rate; in spear_smi_probe()
986 if (dev->clk_rate > SMI_MAX_CLOCK_FREQ) in spear_smi_probe()
987 dev->clk_rate = SMI_MAX_CLOCK_FREQ; in spear_smi_probe()
989 dev->num_flashes = pdata->num_flashes; in spear_smi_probe()
991 if (dev->num_flashes > MAX_NUM_FLASH_CHIP) { in spear_smi_probe()
992 dev_err(&pdev->dev, "exceeding max number of flashes\n"); in spear_smi_probe()
993 dev->num_flashes = MAX_NUM_FLASH_CHIP; in spear_smi_probe()
996 dev->clk = devm_clk_get_enabled(&pdev->dev, NULL); in spear_smi_probe()
997 if (IS_ERR(dev->clk)) { in spear_smi_probe()
998 ret = PTR_ERR(dev->clk); in spear_smi_probe()
1002 ret = devm_request_irq(&pdev->dev, irq, spear_smi_int_handler, 0, in spear_smi_probe()
1003 pdev->name, dev); in spear_smi_probe()
1005 dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n"); in spear_smi_probe()
1009 mutex_init(&dev->lock); in spear_smi_probe()
1010 init_waitqueue_head(&dev->cmd_complete); in spear_smi_probe()
1014 /* loop for each serial nor-flash which is connected to smi */ in spear_smi_probe()
1015 for (i = 0; i < dev->num_flashes; i++) { in spear_smi_probe()
1016 ret = spear_smi_setup_banks(pdev, i, pdata->np[i]); in spear_smi_probe()
1018 dev_err(&dev->pdev->dev, "bank setup failed\n"); in spear_smi_probe()
1029 * spear_smi_remove - Exit routine
1037 struct spear_snor_flash *flash; in spear_smi_remove() local
1042 /* clean up for all nor flash */ in spear_smi_remove()
1043 for (i = 0; i < dev->num_flashes; i++) { in spear_smi_remove()
1044 flash = dev->flash[i]; in spear_smi_remove()
1045 if (!flash) in spear_smi_remove()
1049 WARN_ON(mtd_device_unregister(&flash->mtd)); in spear_smi_remove()
1058 if (sdev && sdev->clk) in spear_smi_suspend()
1059 clk_disable_unprepare(sdev->clk); in spear_smi_suspend()
1067 int ret = -EPERM; in spear_smi_resume()
1069 if (sdev && sdev->clk) in spear_smi_resume()
1070 ret = clk_prepare_enable(sdev->clk); in spear_smi_resume()
1082 { .compatible = "st,spear600-smi" },
1102 MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");