Lines Matching +full:smi +full:- +full:based
2 * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
4 * The serial nor interface is largely based on m25p80.c, however the SPI
5 * interface has been replaced by SMI.
40 /* SMI clock rate */
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 */
157 * struct spear_smi - Structure for SMI Device
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.
165 * @cmd_complete: queue to wait for command completion of NOR-flash.
167 * @flash: separate structure for each Serial NOR-flash attached to SMI.
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.
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.
214 * spear_smi_read_sr - Read status register of flash through SMI
215 * @dev: structure of SMI information.
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()
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
257 * @dev: structure of SMI information.
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.
291 * @dev_id: structure of SMI device, embedded in dev_id.
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.
320 * @dev: structure of smi device
322 * this routine initializes the smi controller wit the default values
330 rate = clk_get_rate(dev->clk); in spear_smi_hw_init()
332 /* functional clock of smi */ in spear_smi_hw_init()
333 prescale = DIV_ROUND_UP(rate, dev->clk_rate); 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.
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
373 * @dev: structure of SMI device
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()
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()
404 "smi controller failed on write enable\n"); 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()
425 x[0] = flash->erase_cmd; in get_sector_erase_cmd()
434 * spear_smi_erase_sector - erase one sector of flash
435 * @dev: structure of SMI information
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()
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
497 struct spear_smi *dev = mtd->priv; 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()
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
549 struct spear_smi *dev = mtd->priv; 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()
626 /* put smi in write enable */ in spear_smi_cpy_toio()
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()
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.
678 struct spear_smi *dev = mtd->priv; 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.
738 * @dev: structure of SMI information.
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()
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()
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()
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()
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()
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()
871 dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank); 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
1043 for (i = 0; i < dev->num_flashes; i++) { in spear_smi_remove()
1044 flash = dev->flash[i]; in spear_smi_remove()
1049 WARN_ON(mtd_device_unregister(&flash->mtd)); in spear_smi_remove()
1060 if (sdev && sdev->clk) in spear_smi_suspend()
1061 clk_disable_unprepare(sdev->clk); in spear_smi_suspend()
1069 int ret = -EPERM; in spear_smi_resume()
1071 if (sdev && sdev->clk) in spear_smi_resume()
1072 ret = clk_prepare_enable(sdev->clk); in spear_smi_resume()
1084 { .compatible = "st,spear600-smi" },
1092 .name = "smi",
1104 MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");