bcm47xxsflash.c 3.4 KB
Newer Older
1 2 3 4 5 6 7
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
#include <linux/platform_device.h>
#include <linux/bcma/bcma.h>

8 9
#include "bcm47xxsflash.h"

10 11 12
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Serial flash driver for BCMA bus");

13
static const char * const probes[] = { "bcm47xxpart", NULL };
14 15 16 17

static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
			      size_t *retlen, u_char *buf)
{
18
	struct bcm47xxsflash *b47s = mtd->priv;
19 20 21 22 23

	/* Check address range */
	if ((from + len) > mtd->size)
		return -EINVAL;

24
	memcpy_fromio(buf, (void __iomem *)KSEG0ADDR(b47s->window + from),
25
		      len);
26
	*retlen = len;
27 28 29 30

	return len;
}

31
static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s)
32
{
33 34 35
	struct mtd_info *mtd = &b47s->mtd;

	mtd->priv = b47s;
36 37 38
	mtd->name = "bcm47xxsflash";
	mtd->owner = THIS_MODULE;
	mtd->type = MTD_ROM;
39
	mtd->size = b47s->size;
40 41 42 43 44 45 46
	mtd->_read = bcm47xxsflash_read;

	/* TODO: implement writing support and verify/change following code */
	mtd->flags = MTD_CAP_ROM;
	mtd->writebufsize = mtd->writesize = 1;
}

47 48 49 50
/**************************************************
 * BCMA
 **************************************************/

51 52 53 54 55 56 57 58 59 60 61
static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash *b47s, u16 offset)
{
	return bcma_cc_read32(b47s->bcma_cc, offset);
}

static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash *b47s, u16 offset,
					u32 value)
{
	bcma_cc_write32(b47s->bcma_cc, offset, value);
}

62
static int bcm47xxsflash_bcma_probe(struct platform_device *pdev)
63 64
{
	struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
65
	struct bcm47xxsflash *b47s;
66 67
	int err;

68 69
	b47s = kzalloc(sizeof(*b47s), GFP_KERNEL);
	if (!b47s) {
70 71 72
		err = -ENOMEM;
		goto out;
	}
73 74
	sflash->priv = b47s;

75
	b47s->bcma_cc = container_of(sflash, struct bcma_drv_cc, sflash);
76 77
	b47s->cc_read = bcm47xxsflash_bcma_cc_read;
	b47s->cc_write = bcm47xxsflash_bcma_cc_write;
78

79 80 81 82 83 84 85 86 87
	switch (b47s->bcma_cc->capabilities & BCMA_CC_CAP_FLASHT) {
	case BCMA_CC_FLASHT_STSER:
		b47s->type = BCM47XXSFLASH_TYPE_ST;
		break;
	case BCMA_CC_FLASHT_ATSER:
		b47s->type = BCM47XXSFLASH_TYPE_ATMEL;
		break;
	}

88 89 90 91 92
	b47s->window = sflash->window;
	b47s->blocksize = sflash->blocksize;
	b47s->numblocks = sflash->numblocks;
	b47s->size = sflash->size;
	bcm47xxsflash_fill_mtd(b47s);
93

94
	err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0);
95 96 97 98 99 100 101 102
	if (err) {
		pr_err("Failed to register MTD device: %d\n", err);
		goto err_dev_reg;
	}

	return 0;

err_dev_reg:
103
	kfree(&b47s->mtd);
104 105 106 107
out:
	return err;
}

108
static int bcm47xxsflash_bcma_remove(struct platform_device *pdev)
109 110
{
	struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
111
	struct bcm47xxsflash *b47s = sflash->priv;
112

113 114
	mtd_device_unregister(&b47s->mtd);
	kfree(b47s);
115 116 117 118 119

	return 0;
}

static struct platform_driver bcma_sflash_driver = {
120 121
	.probe	= bcm47xxsflash_bcma_probe,
	.remove = bcm47xxsflash_bcma_remove,
122 123 124 125 126 127
	.driver = {
		.name = "bcma_sflash",
		.owner = THIS_MODULE,
	},
};

128 129 130 131
/**************************************************
 * Init
 **************************************************/

132 133 134 135
static int __init bcm47xxsflash_init(void)
{
	int err;

136
	err = platform_driver_register(&bcma_sflash_driver);
137 138 139 140 141 142 143 144 145 146 147 148 149 150
	if (err)
		pr_err("Failed to register BCMA serial flash driver: %d\n",
		       err);

	return err;
}

static void __exit bcm47xxsflash_exit(void)
{
	platform_driver_unregister(&bcma_sflash_driver);
}

module_init(bcm47xxsflash_init);
module_exit(bcm47xxsflash_exit);