blob: 729ca5fb53edd65a69500d12752ef126af8df137 [file] [log] [blame]
Rafał Miłecki5fe42d52012-09-17 11:50:49 +02001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/slab.h>
4#include <linux/mtd/mtd.h>
5#include <linux/platform_device.h>
6#include <linux/bcma/bcma.h>
7
Rafał Miłeckia2f74a72013-01-06 21:28:50 +01008#include "bcm47xxsflash.h"
9
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020010MODULE_LICENSE("GPL");
11MODULE_DESCRIPTION("Serial flash driver for BCMA bus");
12
13static const char *probes[] = { "bcm47xxpart", NULL };
14
15static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
16 size_t *retlen, u_char *buf)
17{
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010018 struct bcm47xxsflash *b47s = mtd->priv;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020019
20 /* Check address range */
21 if ((from + len) > mtd->size)
22 return -EINVAL;
23
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010024 memcpy_fromio(buf, (void __iomem *)KSEG0ADDR(b47s->window + from),
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020025 len);
Hauke Mehrtens60aca062013-01-24 17:39:54 +010026 *retlen = len;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020027
28 return len;
29}
30
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010031static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s)
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020032{
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010033 struct mtd_info *mtd = &b47s->mtd;
34
35 mtd->priv = b47s;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020036 mtd->name = "bcm47xxsflash";
37 mtd->owner = THIS_MODULE;
38 mtd->type = MTD_ROM;
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010039 mtd->size = b47s->size;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020040 mtd->_read = bcm47xxsflash_read;
41
42 /* TODO: implement writing support and verify/change following code */
43 mtd->flags = MTD_CAP_ROM;
44 mtd->writebufsize = mtd->writesize = 1;
45}
46
47static int bcm47xxsflash_probe(struct platform_device *pdev)
48{
49 struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010050 struct bcm47xxsflash *b47s;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020051 int err;
52
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010053 b47s = kzalloc(sizeof(*b47s), GFP_KERNEL);
54 if (!b47s) {
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020055 err = -ENOMEM;
56 goto out;
57 }
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010058 sflash->priv = b47s;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020059
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010060 b47s->window = sflash->window;
61 b47s->blocksize = sflash->blocksize;
62 b47s->numblocks = sflash->numblocks;
63 b47s->size = sflash->size;
64 bcm47xxsflash_fill_mtd(b47s);
65
66 err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0);
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020067 if (err) {
68 pr_err("Failed to register MTD device: %d\n", err);
69 goto err_dev_reg;
70 }
71
72 return 0;
73
74err_dev_reg:
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010075 kfree(&b47s->mtd);
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020076out:
77 return err;
78}
79
Bill Pemberton810b7e02012-11-19 13:26:04 -050080static int bcm47xxsflash_remove(struct platform_device *pdev)
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020081{
82 struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev);
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010083 struct bcm47xxsflash *b47s = sflash->priv;
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020084
Rafał Miłeckia2f74a72013-01-06 21:28:50 +010085 mtd_device_unregister(&b47s->mtd);
86 kfree(b47s);
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020087
88 return 0;
89}
90
91static struct platform_driver bcma_sflash_driver = {
Hauke Mehrtens2d13dc32013-01-24 17:39:55 +010092 .probe = bcm47xxsflash_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -050093 .remove = bcm47xxsflash_remove,
Rafał Miłecki5fe42d52012-09-17 11:50:49 +020094 .driver = {
95 .name = "bcma_sflash",
96 .owner = THIS_MODULE,
97 },
98};
99
100static int __init bcm47xxsflash_init(void)
101{
102 int err;
103
Hauke Mehrtens2d13dc32013-01-24 17:39:55 +0100104 err = platform_driver_register(&bcma_sflash_driver);
Rafał Miłecki5fe42d52012-09-17 11:50:49 +0200105 if (err)
106 pr_err("Failed to register BCMA serial flash driver: %d\n",
107 err);
108
109 return err;
110}
111
112static void __exit bcm47xxsflash_exit(void)
113{
114 platform_driver_unregister(&bcma_sflash_driver);
115}
116
117module_init(bcm47xxsflash_init);
118module_exit(bcm47xxsflash_exit);