18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Broadcom specific AMBA
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 "bcma_private.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
118c2ecf20Sopenharmony_ci#include <linux/bcma/bcma.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic struct resource bcma_sflash_resource = {
148c2ecf20Sopenharmony_ci	.name	= "bcma_sflash",
158c2ecf20Sopenharmony_ci	.start	= BCMA_SOC_FLASH2,
168c2ecf20Sopenharmony_ci	.end	= 0,
178c2ecf20Sopenharmony_ci	.flags  = IORESOURCE_MEM | IORESOURCE_READONLY,
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct platform_device bcma_sflash_dev = {
218c2ecf20Sopenharmony_ci	.name		= "bcma_sflash",
228c2ecf20Sopenharmony_ci	.resource	= &bcma_sflash_resource,
238c2ecf20Sopenharmony_ci	.num_resources	= 1,
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct bcma_sflash_tbl_e {
278c2ecf20Sopenharmony_ci	char *name;
288c2ecf20Sopenharmony_ci	u32 id;
298c2ecf20Sopenharmony_ci	u32 blocksize;
308c2ecf20Sopenharmony_ci	u16 numblocks;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic const struct bcma_sflash_tbl_e bcma_sflash_st_tbl[] = {
348c2ecf20Sopenharmony_ci	{ "M25P20", 0x11, 0x10000, 4, },
358c2ecf20Sopenharmony_ci	{ "M25P40", 0x12, 0x10000, 8, },
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	{ "M25P16", 0x14, 0x10000, 32, },
388c2ecf20Sopenharmony_ci	{ "M25P32", 0x15, 0x10000, 64, },
398c2ecf20Sopenharmony_ci	{ "M25P64", 0x16, 0x10000, 128, },
408c2ecf20Sopenharmony_ci	{ "M25FL128", 0x17, 0x10000, 256, },
418c2ecf20Sopenharmony_ci	{ "MX25L25635F", 0x18, 0x10000, 512, },
428c2ecf20Sopenharmony_ci	{ NULL },
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic const struct bcma_sflash_tbl_e bcma_sflash_sst_tbl[] = {
468c2ecf20Sopenharmony_ci	{ "SST25WF512", 1, 0x1000, 16, },
478c2ecf20Sopenharmony_ci	{ "SST25VF512", 0x48, 0x1000, 16, },
488c2ecf20Sopenharmony_ci	{ "SST25WF010", 2, 0x1000, 32, },
498c2ecf20Sopenharmony_ci	{ "SST25VF010", 0x49, 0x1000, 32, },
508c2ecf20Sopenharmony_ci	{ "SST25WF020", 3, 0x1000, 64, },
518c2ecf20Sopenharmony_ci	{ "SST25VF020", 0x43, 0x1000, 64, },
528c2ecf20Sopenharmony_ci	{ "SST25WF040", 4, 0x1000, 128, },
538c2ecf20Sopenharmony_ci	{ "SST25VF040", 0x44, 0x1000, 128, },
548c2ecf20Sopenharmony_ci	{ "SST25VF040B", 0x8d, 0x1000, 128, },
558c2ecf20Sopenharmony_ci	{ "SST25WF080", 5, 0x1000, 256, },
568c2ecf20Sopenharmony_ci	{ "SST25VF080B", 0x8e, 0x1000, 256, },
578c2ecf20Sopenharmony_ci	{ "SST25VF016", 0x41, 0x1000, 512, },
588c2ecf20Sopenharmony_ci	{ "SST25VF032", 0x4a, 0x1000, 1024, },
598c2ecf20Sopenharmony_ci	{ "SST25VF064", 0x4b, 0x1000, 2048, },
608c2ecf20Sopenharmony_ci	{ NULL },
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct bcma_sflash_tbl_e bcma_sflash_at_tbl[] = {
648c2ecf20Sopenharmony_ci	{ "AT45DB011", 0xc, 256, 512, },
658c2ecf20Sopenharmony_ci	{ "AT45DB021", 0x14, 256, 1024, },
668c2ecf20Sopenharmony_ci	{ "AT45DB041", 0x1c, 256, 2048, },
678c2ecf20Sopenharmony_ci	{ "AT45DB081", 0x24, 256, 4096, },
688c2ecf20Sopenharmony_ci	{ "AT45DB161", 0x2c, 512, 4096, },
698c2ecf20Sopenharmony_ci	{ "AT45DB321", 0x34, 512, 8192, },
708c2ecf20Sopenharmony_ci	{ "AT45DB642", 0x3c, 1024, 8192, },
718c2ecf20Sopenharmony_ci	{ NULL },
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void bcma_sflash_cmd(struct bcma_drv_cc *cc, u32 opcode)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	int i;
778c2ecf20Sopenharmony_ci	bcma_cc_write32(cc, BCMA_CC_FLASHCTL,
788c2ecf20Sopenharmony_ci			BCMA_CC_FLASHCTL_START | opcode);
798c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
808c2ecf20Sopenharmony_ci		if (!(bcma_cc_read32(cc, BCMA_CC_FLASHCTL) &
818c2ecf20Sopenharmony_ci		      BCMA_CC_FLASHCTL_BUSY))
828c2ecf20Sopenharmony_ci			return;
838c2ecf20Sopenharmony_ci		cpu_relax();
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	bcma_err(cc->core->bus, "SFLASH control command failed (timeout)!\n");
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Initialize serial flash access */
898c2ecf20Sopenharmony_ciint bcma_sflash_init(struct bcma_drv_cc *cc)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct bcma_bus *bus = cc->core->bus;
928c2ecf20Sopenharmony_ci	struct bcma_sflash *sflash = &cc->sflash;
938c2ecf20Sopenharmony_ci	const struct bcma_sflash_tbl_e *e;
948c2ecf20Sopenharmony_ci	u32 id, id2;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
978c2ecf20Sopenharmony_ci	case BCMA_CC_FLASHT_STSER:
988c2ecf20Sopenharmony_ci		bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_DP);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		bcma_cc_write32(cc, BCMA_CC_FLASHADDR, 0);
1018c2ecf20Sopenharmony_ci		bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_RES);
1028c2ecf20Sopenharmony_ci		id = bcma_cc_read32(cc, BCMA_CC_FLASHDATA);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		bcma_cc_write32(cc, BCMA_CC_FLASHADDR, 1);
1058c2ecf20Sopenharmony_ci		bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_RES);
1068c2ecf20Sopenharmony_ci		id2 = bcma_cc_read32(cc, BCMA_CC_FLASHDATA);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		switch (id) {
1098c2ecf20Sopenharmony_ci		case 0xbf:
1108c2ecf20Sopenharmony_ci			for (e = bcma_sflash_sst_tbl; e->name; e++) {
1118c2ecf20Sopenharmony_ci				if (e->id == id2)
1128c2ecf20Sopenharmony_ci					break;
1138c2ecf20Sopenharmony_ci			}
1148c2ecf20Sopenharmony_ci			break;
1158c2ecf20Sopenharmony_ci		case 0x13:
1168c2ecf20Sopenharmony_ci			return -ENOTSUPP;
1178c2ecf20Sopenharmony_ci		default:
1188c2ecf20Sopenharmony_ci			for (e = bcma_sflash_st_tbl; e->name; e++) {
1198c2ecf20Sopenharmony_ci				if (e->id == id)
1208c2ecf20Sopenharmony_ci					break;
1218c2ecf20Sopenharmony_ci			}
1228c2ecf20Sopenharmony_ci			break;
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci		if (!e->name) {
1258c2ecf20Sopenharmony_ci			bcma_err(bus, "Unsupported ST serial flash (id: 0x%X, id2: 0x%X)\n", id, id2);
1268c2ecf20Sopenharmony_ci			return -ENOTSUPP;
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		break;
1308c2ecf20Sopenharmony_ci	case BCMA_CC_FLASHT_ATSER:
1318c2ecf20Sopenharmony_ci		bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_STATUS);
1328c2ecf20Sopenharmony_ci		id = bcma_cc_read32(cc, BCMA_CC_FLASHDATA) & 0x3c;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		for (e = bcma_sflash_at_tbl; e->name; e++) {
1358c2ecf20Sopenharmony_ci			if (e->id == id)
1368c2ecf20Sopenharmony_ci				break;
1378c2ecf20Sopenharmony_ci		}
1388c2ecf20Sopenharmony_ci		if (!e->name) {
1398c2ecf20Sopenharmony_ci			bcma_err(bus, "Unsupported Atmel serial flash (id: 0x%X)\n", id);
1408c2ecf20Sopenharmony_ci			return -ENOTSUPP;
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		break;
1448c2ecf20Sopenharmony_ci	default:
1458c2ecf20Sopenharmony_ci		bcma_err(bus, "Unsupported flash type\n");
1468c2ecf20Sopenharmony_ci		return -ENOTSUPP;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
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	bcma_info(bus, "Found %s serial flash (size: %dKiB, blocksize: 0x%X, blocks: %d)\n",
1558c2ecf20Sopenharmony_ci		  e->name, sflash->size / 1024, sflash->blocksize,
1568c2ecf20Sopenharmony_ci		  sflash->numblocks);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/* Prepare platform device, but don't register it yet. It's too early,
1598c2ecf20Sopenharmony_ci	 * malloc (required by device_private_init) is not available yet. */
1608c2ecf20Sopenharmony_ci	bcma_sflash_dev.resource[0].end = bcma_sflash_dev.resource[0].start +
1618c2ecf20Sopenharmony_ci					  sflash->size;
1628c2ecf20Sopenharmony_ci	bcma_sflash_dev.dev.platform_data = sflash;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	return 0;
1658c2ecf20Sopenharmony_ci}
166