18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Sonics Silicon Backplane
38c2ecf20Sopenharmony_ci * ChipCommon serial flash interface
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Licensed under the GNU/GPL. See COPYING for details.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "ssb_private.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/ssb/ssb.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic struct resource ssb_sflash_resource = {
138c2ecf20Sopenharmony_ci	.name	= "ssb_sflash",
148c2ecf20Sopenharmony_ci	.start	= SSB_FLASH2,
158c2ecf20Sopenharmony_ci	.end	= 0,
168c2ecf20Sopenharmony_ci	.flags  = IORESOURCE_MEM | IORESOURCE_READONLY,
178c2ecf20Sopenharmony_ci};
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct platform_device ssb_sflash_dev = {
208c2ecf20Sopenharmony_ci	.name		= "ssb_sflash",
218c2ecf20Sopenharmony_ci	.resource	= &ssb_sflash_resource,
228c2ecf20Sopenharmony_ci	.num_resources	= 1,
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct ssb_sflash_tbl_e {
268c2ecf20Sopenharmony_ci	char *name;
278c2ecf20Sopenharmony_ci	u32 id;
288c2ecf20Sopenharmony_ci	u32 blocksize;
298c2ecf20Sopenharmony_ci	u16 numblocks;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const struct ssb_sflash_tbl_e ssb_sflash_st_tbl[] = {
338c2ecf20Sopenharmony_ci	{ "M25P20", 0x11, 0x10000, 4, },
348c2ecf20Sopenharmony_ci	{ "M25P40", 0x12, 0x10000, 8, },
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	{ "M25P16", 0x14, 0x10000, 32, },
378c2ecf20Sopenharmony_ci	{ "M25P32", 0x15, 0x10000, 64, },
388c2ecf20Sopenharmony_ci	{ "M25P64", 0x16, 0x10000, 128, },
398c2ecf20Sopenharmony_ci	{ "M25FL128", 0x17, 0x10000, 256, },
408c2ecf20Sopenharmony_ci	{ NULL },
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic const struct ssb_sflash_tbl_e ssb_sflash_sst_tbl[] = {
448c2ecf20Sopenharmony_ci	{ "SST25WF512", 1, 0x1000, 16, },
458c2ecf20Sopenharmony_ci	{ "SST25VF512", 0x48, 0x1000, 16, },
468c2ecf20Sopenharmony_ci	{ "SST25WF010", 2, 0x1000, 32, },
478c2ecf20Sopenharmony_ci	{ "SST25VF010", 0x49, 0x1000, 32, },
488c2ecf20Sopenharmony_ci	{ "SST25WF020", 3, 0x1000, 64, },
498c2ecf20Sopenharmony_ci	{ "SST25VF020", 0x43, 0x1000, 64, },
508c2ecf20Sopenharmony_ci	{ "SST25WF040", 4, 0x1000, 128, },
518c2ecf20Sopenharmony_ci	{ "SST25VF040", 0x44, 0x1000, 128, },
528c2ecf20Sopenharmony_ci	{ "SST25VF040B", 0x8d, 0x1000, 128, },
538c2ecf20Sopenharmony_ci	{ "SST25WF080", 5, 0x1000, 256, },
548c2ecf20Sopenharmony_ci	{ "SST25VF080B", 0x8e, 0x1000, 256, },
558c2ecf20Sopenharmony_ci	{ "SST25VF016", 0x41, 0x1000, 512, },
568c2ecf20Sopenharmony_ci	{ "SST25VF032", 0x4a, 0x1000, 1024, },
578c2ecf20Sopenharmony_ci	{ "SST25VF064", 0x4b, 0x1000, 2048, },
588c2ecf20Sopenharmony_ci	{ NULL },
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const struct ssb_sflash_tbl_e ssb_sflash_at_tbl[] = {
628c2ecf20Sopenharmony_ci	{ "AT45DB011", 0xc, 256, 512, },
638c2ecf20Sopenharmony_ci	{ "AT45DB021", 0x14, 256, 1024, },
648c2ecf20Sopenharmony_ci	{ "AT45DB041", 0x1c, 256, 2048, },
658c2ecf20Sopenharmony_ci	{ "AT45DB081", 0x24, 256, 4096, },
668c2ecf20Sopenharmony_ci	{ "AT45DB161", 0x2c, 512, 4096, },
678c2ecf20Sopenharmony_ci	{ "AT45DB321", 0x34, 512, 8192, },
688c2ecf20Sopenharmony_ci	{ "AT45DB642", 0x3c, 1024, 8192, },
698c2ecf20Sopenharmony_ci	{ NULL },
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void ssb_sflash_cmd(struct ssb_chipcommon *cc, u32 opcode)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int i;
758c2ecf20Sopenharmony_ci	chipco_write32(cc, SSB_CHIPCO_FLASHCTL,
768c2ecf20Sopenharmony_ci		       SSB_CHIPCO_FLASHCTL_START | opcode);
778c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
788c2ecf20Sopenharmony_ci		if (!(chipco_read32(cc, SSB_CHIPCO_FLASHCTL) &
798c2ecf20Sopenharmony_ci		      SSB_CHIPCO_FLASHCTL_BUSY))
808c2ecf20Sopenharmony_ci			return;
818c2ecf20Sopenharmony_ci		cpu_relax();
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci	dev_err(cc->dev->dev, "SFLASH control command failed (timeout)!\n");
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* Initialize serial flash access */
878c2ecf20Sopenharmony_ciint ssb_sflash_init(struct ssb_chipcommon *cc)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct ssb_sflash *sflash = &cc->dev->bus->mipscore.sflash;
908c2ecf20Sopenharmony_ci	const struct ssb_sflash_tbl_e *e;
918c2ecf20Sopenharmony_ci	u32 id, id2;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	switch (cc->capabilities & SSB_CHIPCO_CAP_FLASHT) {
948c2ecf20Sopenharmony_ci	case SSB_CHIPCO_FLASHT_STSER:
958c2ecf20Sopenharmony_ci		ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_DP);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		chipco_write32(cc, SSB_CHIPCO_FLASHADDR, 0);
988c2ecf20Sopenharmony_ci		ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_RES);
998c2ecf20Sopenharmony_ci		id = chipco_read32(cc, SSB_CHIPCO_FLASHDATA);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		chipco_write32(cc, SSB_CHIPCO_FLASHADDR, 1);
1028c2ecf20Sopenharmony_ci		ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_RES);
1038c2ecf20Sopenharmony_ci		id2 = chipco_read32(cc, SSB_CHIPCO_FLASHDATA);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		switch (id) {
1068c2ecf20Sopenharmony_ci		case 0xbf:
1078c2ecf20Sopenharmony_ci			for (e = ssb_sflash_sst_tbl; e->name; e++) {
1088c2ecf20Sopenharmony_ci				if (e->id == id2)
1098c2ecf20Sopenharmony_ci					break;
1108c2ecf20Sopenharmony_ci			}
1118c2ecf20Sopenharmony_ci			break;
1128c2ecf20Sopenharmony_ci		case 0x13:
1138c2ecf20Sopenharmony_ci			return -ENOTSUPP;
1148c2ecf20Sopenharmony_ci		default:
1158c2ecf20Sopenharmony_ci			for (e = ssb_sflash_st_tbl; e->name; e++) {
1168c2ecf20Sopenharmony_ci				if (e->id == id)
1178c2ecf20Sopenharmony_ci					break;
1188c2ecf20Sopenharmony_ci			}
1198c2ecf20Sopenharmony_ci			break;
1208c2ecf20Sopenharmony_ci		}
1218c2ecf20Sopenharmony_ci		if (!e->name) {
1228c2ecf20Sopenharmony_ci			pr_err("Unsupported ST serial flash (id: 0x%X, id2: 0x%X)\n",
1238c2ecf20Sopenharmony_ci			       id, id2);
1248c2ecf20Sopenharmony_ci			return -ENOTSUPP;
1258c2ecf20Sopenharmony_ci		}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci		break;
1288c2ecf20Sopenharmony_ci	case SSB_CHIPCO_FLASHT_ATSER:
1298c2ecf20Sopenharmony_ci		ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_AT_STATUS);
1308c2ecf20Sopenharmony_ci		id = chipco_read32(cc, SSB_CHIPCO_FLASHDATA) & 0x3c;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		for (e = ssb_sflash_at_tbl; e->name; e++) {
1338c2ecf20Sopenharmony_ci			if (e->id == id)
1348c2ecf20Sopenharmony_ci				break;
1358c2ecf20Sopenharmony_ci		}
1368c2ecf20Sopenharmony_ci		if (!e->name) {
1378c2ecf20Sopenharmony_ci			pr_err("Unsupported Atmel serial flash (id: 0x%X)\n",
1388c2ecf20Sopenharmony_ci			       id);
1398c2ecf20Sopenharmony_ci			return -ENOTSUPP;
1408c2ecf20Sopenharmony_ci		}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci		break;
1438c2ecf20Sopenharmony_ci	default:
1448c2ecf20Sopenharmony_ci		pr_err("Unsupported flash type\n");
1458c2ecf20Sopenharmony_ci		return -ENOTSUPP;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	sflash->window = SSB_FLASH2;
1498c2ecf20Sopenharmony_ci	sflash->blocksize = e->blocksize;
1508c2ecf20Sopenharmony_ci	sflash->numblocks = e->numblocks;
1518c2ecf20Sopenharmony_ci	sflash->size = sflash->blocksize * sflash->numblocks;
1528c2ecf20Sopenharmony_ci	sflash->present = true;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	pr_info("Found %s serial flash (size: %dKiB, blocksize: 0x%X, blocks: %d)\n",
1558c2ecf20Sopenharmony_ci		e->name, sflash->size / 1024, e->blocksize, e->numblocks);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* Prepare platform device, but don't register it yet. It's too early,
1588c2ecf20Sopenharmony_ci	 * malloc (required by device_private_init) is not available yet. */
1598c2ecf20Sopenharmony_ci	ssb_sflash_dev.resource[0].end = ssb_sflash_dev.resource[0].start +
1608c2ecf20Sopenharmony_ci					 sflash->size;
1618c2ecf20Sopenharmony_ci	ssb_sflash_dev.dev.platform_data = sflash;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
165