1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
2 Subject: [RFC][PATCH] mtd: bcm47xxsflash: writing support
3 Date: Wed, 22 May 2013 14:39:02 +0200
6 drivers/mtd/devices/bcm47xxsflash.c | 130 +++++++++++++++++++++++++++++++++--
7 1 file changed, 126 insertions(+), 4 deletions(-)
9 --- a/drivers/mtd/devices/bcm47xxsflash.c
10 +++ b/drivers/mtd/devices/bcm47xxsflash.c
11 @@ -116,6 +116,127 @@ static int bcm47xxsflash_read(struct mtd
15 +static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
18 + struct bcm47xxsflash *b47s = mtd->priv;
22 + bcm47xxsflash_cmd(b47s, OPCODE_ST_WREN);
24 + /* Write first byte */
25 + b47s->cc_write(b47s, BCMA_CC_FLASHADDR, offset);
26 + b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++);
29 + if (b47s->bcma_cc->core->id.rev < 20) {
30 + bcm47xxsflash_cmd(b47s, OPCODE_ST_PP);
31 + return 1; /* 1B written */
34 + /* Program page and set CSA (on newer chips we can continue writing) */
35 + bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | OPCODE_ST_PP);
41 + /* Page boundary, another function call is needed */
42 + if ((offset & 0xFF) == 0)
45 + bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | *buf++);
51 + /* All done, drop CSA & poll */
52 + b47s->cc_write(b47s, BCMA_CC_FLASHCTL, 0);
54 + if (bcm47xxsflash_poll(b47s, HZ / 10))
55 + pr_err("Flash rejected dropping CSA\n");
60 +static int bcm47xxsflash_write_at(struct mtd_info *mtd, u32 offset, size_t len,
63 + struct bcm47xxsflash *b47s = mtd->priv;
64 + u32 mask = b47s->blocksize - 1;
65 + u32 page = (offset & ~mask) << 1;
66 + u32 byte = offset & mask;
69 + /* If we don't overwrite whole page, read it to the buffer first */
70 + if (byte || (len < b47s->blocksize)) {
73 + b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page);
74 + bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_LOAD);
75 + /* 250 us for AT45DB321B */
76 + err = bcm47xxsflash_poll(b47s, HZ / 1000);
78 + pr_err("Timeout reading page 0x%X info buffer\n", page);
83 + /* Change buffer content with our data */
85 + /* Page boundary, another function call is needed */
86 + if (byte == b47s->blocksize)
89 + b47s->cc_write(b47s, BCMA_CC_FLASHADDR, byte++);
90 + b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++);
91 + bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_WRITE);
96 + /* Program page with the buffer content */
97 + b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page);
98 + bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_PROGRAM);
103 +static int bcm47xxsflash_write(struct mtd_info *mtd, loff_t to, size_t len,
104 + size_t *retlen, const u_char *buf)
106 + struct bcm47xxsflash *b47s = mtd->priv;
109 + /* Writing functions can return without writing all passed data, for
110 + * example when the hardware is too old or when we git page boundary.
113 + switch (b47s->type) {
114 + case BCM47XXSFLASH_TYPE_ST:
115 + written = bcm47xxsflash_write_st(mtd, to, len, buf);
117 + case BCM47XXSFLASH_TYPE_ATMEL:
118 + written = bcm47xxsflash_write_at(mtd, to, len, buf);
124 + pr_err("Error writing at offset 0x%llX\n", to);
127 + to += (loff_t)written;
129 + *retlen += written;
136 static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s)
138 struct mtd_info *mtd = &b47s->mtd;
139 @@ -123,16 +244,17 @@ static void bcm47xxsflash_fill_mtd(struc
141 mtd->name = "bcm47xxsflash";
142 mtd->owner = THIS_MODULE;
143 - mtd->type = MTD_ROM;
145 - /* TODO: implement writing support and verify/change following code */
146 - mtd->flags = MTD_CAP_ROM;
147 + mtd->type = MTD_NORFLASH;
148 + mtd->flags = MTD_CAP_NORFLASH;
149 mtd->size = b47s->size;
150 mtd->erasesize = b47s->blocksize;
151 - mtd->writebufsize = mtd->writesize = 1;
152 + mtd->writesize = 1;
153 + mtd->writebufsize = 1;
155 mtd->_erase = bcm47xxsflash_erase;
156 mtd->_read = bcm47xxsflash_read;
157 + mtd->_write = bcm47xxsflash_write;
160 /**************************************************