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> |
Rafał Miłecki | bddcb5e | 2013-03-24 21:51:31 +0100 | [diff] [blame] | 4 | #include <linux/delay.h> |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 5 | #include <linux/mtd/mtd.h> |
| 6 | #include <linux/platform_device.h> |
| 7 | #include <linux/bcma/bcma.h> |
| 8 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 9 | #include "bcm47xxsflash.h" |
| 10 | |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 11 | MODULE_LICENSE("GPL"); |
| 12 | MODULE_DESCRIPTION("Serial flash driver for BCMA bus"); |
| 13 | |
Artem Bityutskiy | afffeec | 2013-03-12 10:50:07 +0200 | [diff] [blame] | 14 | static const char * const probes[] = { "bcm47xxpart", NULL }; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 15 | |
Rafał Miłecki | bddcb5e | 2013-03-24 21:51:31 +0100 | [diff] [blame] | 16 | /************************************************** |
| 17 | * Various helpers |
| 18 | **************************************************/ |
| 19 | |
| 20 | static void bcm47xxsflash_cmd(struct bcm47xxsflash *b47s, u32 opcode) |
| 21 | { |
| 22 | int i; |
| 23 | |
| 24 | b47s->cc_write(b47s, BCMA_CC_FLASHCTL, BCMA_CC_FLASHCTL_START | opcode); |
| 25 | for (i = 0; i < 1000; i++) { |
| 26 | if (!(b47s->cc_read(b47s, BCMA_CC_FLASHCTL) & |
| 27 | BCMA_CC_FLASHCTL_BUSY)) |
| 28 | return; |
| 29 | cpu_relax(); |
| 30 | } |
| 31 | pr_err("Control command failed (timeout)!\n"); |
| 32 | } |
| 33 | |
| 34 | static int bcm47xxsflash_poll(struct bcm47xxsflash *b47s, int timeout) |
| 35 | { |
| 36 | unsigned long deadline = jiffies + timeout; |
| 37 | |
| 38 | do { |
| 39 | switch (b47s->type) { |
| 40 | case BCM47XXSFLASH_TYPE_ST: |
| 41 | bcm47xxsflash_cmd(b47s, OPCODE_ST_RDSR); |
| 42 | if (!(b47s->cc_read(b47s, BCMA_CC_FLASHDATA) & |
| 43 | SR_ST_WIP)) |
| 44 | return 0; |
| 45 | break; |
| 46 | case BCM47XXSFLASH_TYPE_ATMEL: |
| 47 | bcm47xxsflash_cmd(b47s, OPCODE_AT_STATUS); |
| 48 | if (b47s->cc_read(b47s, BCMA_CC_FLASHDATA) & |
| 49 | SR_AT_READY) |
| 50 | return 0; |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | cpu_relax(); |
| 55 | udelay(1); |
| 56 | } while (!time_after_eq(jiffies, deadline)); |
| 57 | |
| 58 | pr_err("Timeout waiting for flash to be ready!\n"); |
| 59 | |
| 60 | return -EBUSY; |
| 61 | } |
| 62 | |
| 63 | /************************************************** |
| 64 | * MTD ops |
| 65 | **************************************************/ |
| 66 | |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 67 | static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 68 | size_t *retlen, u_char *buf) |
| 69 | { |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 70 | struct bcm47xxsflash *b47s = mtd->priv; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 71 | |
| 72 | /* Check address range */ |
| 73 | if ((from + len) > mtd->size) |
| 74 | return -EINVAL; |
| 75 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 76 | memcpy_fromio(buf, (void __iomem *)KSEG0ADDR(b47s->window + from), |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 77 | len); |
Hauke Mehrtens | 60aca06 | 2013-01-24 17:39:54 +0100 | [diff] [blame] | 78 | *retlen = len; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 79 | |
| 80 | return len; |
| 81 | } |
| 82 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 83 | static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s) |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 84 | { |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 85 | struct mtd_info *mtd = &b47s->mtd; |
| 86 | |
| 87 | mtd->priv = b47s; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 88 | mtd->name = "bcm47xxsflash"; |
| 89 | mtd->owner = THIS_MODULE; |
| 90 | mtd->type = MTD_ROM; |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 91 | mtd->size = b47s->size; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 92 | mtd->_read = bcm47xxsflash_read; |
| 93 | |
| 94 | /* TODO: implement writing support and verify/change following code */ |
| 95 | mtd->flags = MTD_CAP_ROM; |
| 96 | mtd->writebufsize = mtd->writesize = 1; |
| 97 | } |
| 98 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame] | 99 | /************************************************** |
| 100 | * BCMA |
| 101 | **************************************************/ |
| 102 | |
Rafał Miłecki | 265dfbd | 2013-03-24 21:53:24 +0100 | [diff] [blame] | 103 | static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash *b47s, u16 offset) |
| 104 | { |
| 105 | return bcma_cc_read32(b47s->bcma_cc, offset); |
| 106 | } |
| 107 | |
| 108 | static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash *b47s, u16 offset, |
| 109 | u32 value) |
| 110 | { |
| 111 | bcma_cc_write32(b47s->bcma_cc, offset, value); |
| 112 | } |
| 113 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame] | 114 | static int bcm47xxsflash_bcma_probe(struct platform_device *pdev) |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 115 | { |
| 116 | struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev); |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 117 | struct bcm47xxsflash *b47s; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 118 | int err; |
| 119 | |
Libo Chen | d2b1bd1 | 2013-05-30 10:22:12 +0800 | [diff] [blame^] | 120 | b47s = devm_kzalloc(&pdev->dev, sizeof(*b47s), GFP_KERNEL); |
| 121 | if (!b47s) |
| 122 | return -ENOMEM; |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 123 | sflash->priv = b47s; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 124 | |
Rafał Miłecki | 41c8153 | 2013-03-06 12:33:17 +0100 | [diff] [blame] | 125 | b47s->bcma_cc = container_of(sflash, struct bcma_drv_cc, sflash); |
Rafał Miłecki | 265dfbd | 2013-03-24 21:53:24 +0100 | [diff] [blame] | 126 | b47s->cc_read = bcm47xxsflash_bcma_cc_read; |
| 127 | b47s->cc_write = bcm47xxsflash_bcma_cc_write; |
Rafał Miłecki | 41c8153 | 2013-03-06 12:33:17 +0100 | [diff] [blame] | 128 | |
Rafał Miłecki | 1f816bc | 2013-03-06 12:34:19 +0100 | [diff] [blame] | 129 | switch (b47s->bcma_cc->capabilities & BCMA_CC_CAP_FLASHT) { |
| 130 | case BCMA_CC_FLASHT_STSER: |
| 131 | b47s->type = BCM47XXSFLASH_TYPE_ST; |
| 132 | break; |
| 133 | case BCMA_CC_FLASHT_ATSER: |
| 134 | b47s->type = BCM47XXSFLASH_TYPE_ATMEL; |
| 135 | break; |
| 136 | } |
| 137 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 138 | b47s->window = sflash->window; |
| 139 | b47s->blocksize = sflash->blocksize; |
| 140 | b47s->numblocks = sflash->numblocks; |
| 141 | b47s->size = sflash->size; |
| 142 | bcm47xxsflash_fill_mtd(b47s); |
| 143 | |
| 144 | err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 145 | if (err) { |
| 146 | pr_err("Failed to register MTD device: %d\n", err); |
Libo Chen | d2b1bd1 | 2013-05-30 10:22:12 +0800 | [diff] [blame^] | 147 | return err; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Rafał Miłecki | bddcb5e | 2013-03-24 21:51:31 +0100 | [diff] [blame] | 150 | if (bcm47xxsflash_poll(b47s, HZ / 10)) |
| 151 | pr_warn("Serial flash busy\n"); |
| 152 | |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 153 | return 0; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame] | 156 | static int bcm47xxsflash_bcma_remove(struct platform_device *pdev) |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 157 | { |
| 158 | struct bcma_sflash *sflash = dev_get_platdata(&pdev->dev); |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 159 | struct bcm47xxsflash *b47s = sflash->priv; |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 160 | |
Rafał Miłecki | a2f74a7 | 2013-01-06 21:28:50 +0100 | [diff] [blame] | 161 | mtd_device_unregister(&b47s->mtd); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static struct platform_driver bcma_sflash_driver = { |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame] | 167 | .probe = bcm47xxsflash_bcma_probe, |
| 168 | .remove = bcm47xxsflash_bcma_remove, |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 169 | .driver = { |
| 170 | .name = "bcma_sflash", |
| 171 | .owner = THIS_MODULE, |
| 172 | }, |
| 173 | }; |
| 174 | |
Rafał Miłecki | f1a7c9d | 2013-02-04 08:23:08 +0100 | [diff] [blame] | 175 | /************************************************** |
| 176 | * Init |
| 177 | **************************************************/ |
| 178 | |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 179 | static int __init bcm47xxsflash_init(void) |
| 180 | { |
| 181 | int err; |
| 182 | |
Hauke Mehrtens | 2d13dc3 | 2013-01-24 17:39:55 +0100 | [diff] [blame] | 183 | err = platform_driver_register(&bcma_sflash_driver); |
Rafał Miłecki | 5fe42d5 | 2012-09-17 11:50:49 +0200 | [diff] [blame] | 184 | if (err) |
| 185 | pr_err("Failed to register BCMA serial flash driver: %d\n", |
| 186 | err); |
| 187 | |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | static void __exit bcm47xxsflash_exit(void) |
| 192 | { |
| 193 | platform_driver_unregister(&bcma_sflash_driver); |
| 194 | } |
| 195 | |
| 196 | module_init(bcm47xxsflash_init); |
| 197 | module_exit(bcm47xxsflash_exit); |