Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 1 | #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łecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 8 | #include "bcm47xxsflash.h" |
| 9 | |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 10 | MODULE_LICENSE("GPL"); |
| 11 | MODULE_DESCRIPTION("Serial flash driver for BCMA bus"); |
| 12 | |
| 13 | static const char *probes[] = { "bcm47xxpart", NULL }; |
| 14 | |
| 15 | static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 16 | size_t *retlen, u_char *buf) |
| 17 | { |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 18 | struct bcm47xxsflash *b47s = mtd->priv; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 19 | |
| 20 | /* Check address range */ |
| 21 | if ((from + len) > mtd->size) |
| 22 | return -EINVAL; |
| 23 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 24 | memcpy_fromio(buf, (void __iomem *)KSEG0ADDR(b47s->window + from), |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 25 | len); |
Hauke Mehrtens | 60aca06 | 2013-01-24 17:39:54 +0100 | [diff] [blame] | 26 | *retlen = len; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 27 | |
| 28 | return len; |
| 29 | } |
| 30 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 31 | static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s) |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 32 | { |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 33 | struct mtd_info *mtd = &b47s->mtd; |
| 34 | |
| 35 | mtd->priv = b47s; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 36 | mtd->name = "bcm47xxsflash"; |
| 37 | mtd->owner = THIS_MODULE; |
| 38 | mtd->type = MTD_ROM; |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 39 | mtd->size = b47s->size; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 40 | 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 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame^] | 47 | /************************************************** |
| 48 | * BCMA |
| 49 | **************************************************/ |
| 50 | |
| 51 | static int bcm47xxsflash_bcma_probe(struct platform_device *pdev) |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 52 | { |
| 53 | struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev); |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 54 | struct bcm47xxsflash *b47s; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 55 | int err; |
| 56 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 57 | b47s = kzalloc(sizeof(*b47s), GFP_KERNEL); |
| 58 | if (!b47s) { |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 59 | err = -ENOMEM; |
| 60 | goto out; |
| 61 | } |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 62 | sflash->priv = b47s; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 63 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 64 | b47s->window = sflash->window; |
| 65 | b47s->blocksize = sflash->blocksize; |
| 66 | b47s->numblocks = sflash->numblocks; |
| 67 | b47s->size = sflash->size; |
| 68 | bcm47xxsflash_fill_mtd(b47s); |
| 69 | |
| 70 | err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 71 | if (err) { |
| 72 | pr_err("Failed to register MTD device: %d\n", err); |
| 73 | goto err_dev_reg; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | |
| 78 | err_dev_reg: |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 79 | kfree(&b47s->mtd); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 80 | out: |
| 81 | return err; |
| 82 | } |
| 83 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame^] | 84 | static int bcm47xxsflash_bcma_remove(struct platform_device *pdev) |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 85 | { |
| 86 | struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev); |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 87 | struct bcm47xxsflash *b47s = sflash->priv; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 88 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 89 | mtd_device_unregister(&b47s->mtd); |
| 90 | kfree(b47s); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static struct platform_driver bcma_sflash_driver = { |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame^] | 96 | .probe = bcm47xxsflash_bcma_probe, |
| 97 | .remove = bcm47xxsflash_bcma_remove, |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 98 | .driver = { |
| 99 | .name = "bcma_sflash", |
| 100 | .owner = THIS_MODULE, |
| 101 | }, |
| 102 | }; |
| 103 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame^] | 104 | /************************************************** |
| 105 | * Init |
| 106 | **************************************************/ |
| 107 | |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 108 | static int __init bcm47xxsflash_init(void) |
| 109 | { |
| 110 | int err; |
| 111 | |
Hauke Mehrtens | 2d13dc3 | 2013-01-24 17:39:55 +0100 | [diff] [blame] | 112 | err = platform_driver_register(&bcma_sflash_driver); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 113 | if (err) |
| 114 | pr_err("Failed to register BCMA serial flash driver: %d\n", |
| 115 | err); |
| 116 | |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | static void __exit bcm47xxsflash_exit(void) |
| 121 | { |
| 122 | platform_driver_unregister(&bcma_sflash_driver); |
| 123 | } |
| 124 | |
| 125 | module_init(bcm47xxsflash_init); |
| 126 | module_exit(bcm47xxsflash_exit); |