18 #include <linux/device.h>
20 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/wait.h>
41 #define SMI_MAX_CLOCK_FREQ 50000000
44 #define SMI_PROBE_TIMEOUT (HZ / 10)
45 #define SMI_MAX_TIME_OUT (3 * HZ)
48 #define SMI_CMD_TIMEOUT (HZ / 10)
58 #define BANK_EN (0xF << 0)
59 #define DSEL_TIME (0x6 << 4)
60 #define SW_MODE (0x1 << 28)
61 #define WB_MODE (0x1 << 29)
62 #define FAST_MODE (0x1 << 15)
63 #define HOLD1 (0x1 << 16)
66 #define SEND (0x1 << 7)
67 #define TFIE (0x1 << 8)
68 #define WCIE (0x1 << 9)
69 #define RD_STATUS_REG (0x1 << 10)
70 #define WE (0x1 << 11)
72 #define TX_LEN_SHIFT 0
73 #define RX_LEN_SHIFT 4
91 #define OPCODE_RDID 0x9f
105 #define FLASH_ID(n, es, id, psize, ssize, size) \
111 .sectorsize = ssize, \
112 .size_in_bytes = size \
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),
243 ret = dev->
status & 0xffff;
264 static int spear_smi_wait_till_ready(
struct spear_smi *dev,
u32 bank,
265 unsigned long timeout)
267 unsigned long finish;
272 status = spear_smi_read_sr(dev, bank);
277 }
else if (!(status &
SR_WIP)) {
284 dev_err(&dev->
pdev->dev,
"smi controller is busy, timeout\n");
324 static void spear_smi_hw_init(
struct spear_smi *dev)
326 unsigned long rate = 0;
357 static int get_flash_index(
u32 flash_id)
362 for (index = 0; index <
ARRAY_SIZE(flash_devices); index++) {
363 if (flash_devices[index].
device_id == flash_id)
379 static int spear_smi_write_enable(
struct spear_smi *dev,
u32 bank)
404 "smi controller failed on write enable\n");
405 }
else if (ret > 0) {
410 dev_err(&dev->
pdev->dev,
"couldn't enable write\n");
444 static int spear_smi_erase_sector(
struct spear_smi *dev,
454 ret = spear_smi_write_enable(dev, bank);
517 command = get_sector_erase_cmd(flash, addr);
519 ret = spear_smi_erase_sector(dev, bank, command, 4);
548 static int spear_mtd_read(
struct mtd_info *mtd, loff_t
from,
size_t len,
549 size_t *retlen,
u8 *
buf)
598 static inline int spear_smi_cpy_toio(
struct spear_smi *dev,
u32 bank,
599 void *
dest,
const void *src,
size_t len)
610 ret = spear_smi_write_enable(dev, bank);
641 static int spear_mtd_write(
struct mtd_info *mtd, loff_t to,
size_t len,
642 size_t *retlen,
const u8 *buf)
665 if (page_offset + len <= flash->page_size) {
666 ret = spear_smi_cpy_toio(dev, flash->
bank, dest, buf, len);
675 ret = spear_smi_cpy_toio(dev, flash->
bank, dest, buf,
683 for (i = page_size; i < len; i +=
page_size) {
688 ret = spear_smi_cpy_toio(dev, flash->
bank, dest + i,
712 static int spear_smi_probe_flash(
struct spear_smi *dev,
u32 bank)
746 ret = get_flash_index(val);
772 of_property_read_u32(np,
"clock-rate", &val);
812 struct spear_smi *dev = platform_get_drvdata(pdev);
822 pdata = dev_get_platdata(&pdev->
dev);
838 flash_index = spear_smi_probe_flash(dev, bank);
839 if (flash_index < 0) {
840 dev_info(&dev->
pdev->dev,
"smi-nor%d not found\n", bank);
852 if (flash_info->
name)
853 flash->
mtd.name = flash_info->
name;
855 flash->
mtd.name = flash_devices[flash_index].
name;
858 flash->
mtd.writesize = 1;
860 flash->
mtd.size = flash_info->
size;
861 flash->
mtd.erasesize = flash_devices[flash_index].
sectorsize;
865 flash->
mtd._erase = spear_mtd_erase;
866 flash->
mtd._read = spear_mtd_read;
867 flash->
mtd._write = spear_mtd_write;
870 dev_info(&dev->
pdev->dev,
"mtd .name=%s .size=%llx(%lluM)\n",
871 flash->
mtd.name, flash->
mtd.size,
872 flash->
mtd.size / (1024 * 1024));
875 flash->
mtd.erasesize, flash->
mtd.erasesize / 1024);
888 dev_err(&dev->
pdev->dev,
"Err MTD partition=%d\n", ret);
916 pr_err(
"%s: ERROR: no memory", __func__);
921 ret = spear_smi_probe_config_dt(pdev, np);
928 pdata = dev_get_platdata(&pdev->
dev);
955 dev_err(&pdev->
dev,
"devm_request_and_ioremap fail\n");
968 dev_err(&pdev->
dev,
"exceeding max number of flashes\n");
973 if (IS_ERR(dev->
clk)) {
974 ret = PTR_ERR(dev->
clk);
978 ret = clk_prepare_enable(dev->
clk);
982 ret = devm_request_irq(&pdev->
dev, irq, spear_smi_int_handler, 0,
985 dev_err(&dev->
pdev->dev,
"SMI IRQ allocation failed\n");
991 spear_smi_hw_init(dev);
992 platform_set_drvdata(pdev, dev);
996 ret = spear_smi_setup_banks(pdev, i, pdata->
np[i]);
1006 platform_set_drvdata(pdev,
NULL);
1008 clk_disable_unprepare(dev->
clk);
1025 dev = platform_get_drvdata(pdev);
1043 clk_disable_unprepare(dev->
clk);
1044 platform_set_drvdata(pdev,
NULL);
1050 static int spear_smi_suspend(
struct device *dev)
1054 if (sdev && sdev->
clk)
1055 clk_disable_unprepare(sdev->
clk);
1060 static int spear_smi_resume(
struct device *dev)
1065 if (sdev && sdev->
clk)
1066 ret = clk_prepare_enable(sdev->
clk);
1069 spear_smi_hw_init(sdev);
1073 static SIMPLE_DEV_PM_OPS(spear_smi_pm_ops, spear_smi_suspend, spear_smi_resume);
1077 static const struct of_device_id spear_smi_id_table[] = {
1091 .pm = &spear_smi_pm_ops,
1094 .probe = spear_smi_probe,
1098 static int spear_smi_init(
void)
1104 static void spear_smi_exit(
void)