2 * Broadcom SiliconBackplane chipcommon serial flash interface
4 * Copyright 2004, Broadcom Corporation
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/ioport.h>
19 #include <linux/mtd/compatmac.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/errno.h>
23 #include <linux/pci.h>
36 #ifdef CONFIG_MTD_PARTITIONS
37 extern struct mtd_partition
* init_mtd_partitions(struct mtd_info
*mtd
, size_t size
);
42 struct semaphore lock
;
44 struct mtd_erase_region_info region
;
47 /* Private global state */
48 static struct sflash_mtd sflash
;
51 sflash_mtd_poll(struct sflash_mtd
*sflash
, unsigned int offset
, int timeout
)
57 if (!sflash_poll(sflash
->cc
, offset
)) {
61 if (time_after(jiffies
, now
+ timeout
)) {
62 printk(KERN_ERR
"sflash: timeout\n");
66 if (current
->need_resched
) {
67 set_current_state(TASK_UNINTERRUPTIBLE
);
68 schedule_timeout(timeout
/ 10);
77 sflash_mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
79 struct sflash_mtd
*sflash
= (struct sflash_mtd
*) mtd
->priv
;
82 /* Check address range */
85 if ((from
+ len
) > mtd
->size
)
92 if ((bytes
= sflash_read(sflash
->cc
, (uint
) from
, len
, buf
)) < 0) {
96 from
+= (loff_t
) bytes
;
108 sflash_mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
, const u_char
*buf
)
110 struct sflash_mtd
*sflash
= (struct sflash_mtd
*) mtd
->priv
;
113 /* Check address range */
116 if ((to
+ len
) > mtd
->size
)
123 if ((bytes
= sflash_write(sflash
->cc
, (uint
) to
, len
, buf
)) < 0) {
127 if ((ret
= sflash_mtd_poll(sflash
, (unsigned int) to
, HZ
/ 10)))
129 to
+= (loff_t
) bytes
;
141 sflash_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*erase
)
143 struct sflash_mtd
*sflash
= (struct sflash_mtd
*) mtd
->priv
;
145 unsigned int addr
, len
;
147 /* Check address range */
150 if ((erase
->addr
+ erase
->len
) > mtd
->size
)
158 /* Ensure that requested region is aligned */
159 for (i
= 0; i
< mtd
->numeraseregions
; i
++) {
160 for (j
= 0; j
< mtd
->eraseregions
[i
].numblocks
; j
++) {
161 if (addr
== mtd
->eraseregions
[i
].offset
+ mtd
->eraseregions
[i
].erasesize
* j
&&
162 len
>= mtd
->eraseregions
[i
].erasesize
) {
163 if ((ret
= sflash_erase(sflash
->cc
, addr
)) < 0)
165 if ((ret
= sflash_mtd_poll(sflash
, addr
, 10 * HZ
)))
167 addr
+= mtd
->eraseregions
[i
].erasesize
;
168 len
-= mtd
->eraseregions
[i
].erasesize
;
177 /* Set erase status */
179 erase
->state
= MTD_ERASE_FAILED
;
181 erase
->state
= MTD_ERASE_DONE
;
183 /* Call erase callback */
185 erase
->callback(erase
);
190 #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
191 #define sflash_mtd_init init_module
192 #define sflash_mtd_exit cleanup_module
196 sflash_mtd_init(void)
198 struct pci_dev
*pdev
;
202 #ifdef CONFIG_MTD_PARTITIONS
203 struct mtd_partition
*parts
;
206 if (!(pdev
= pci_find_device(VENDOR_BROADCOM
, SB_CC
, NULL
))) {
207 printk(KERN_ERR
"sflash: chipcommon not found\n");
211 memset(&sflash
, 0, sizeof(struct sflash_mtd
));
212 init_MUTEX(&sflash
.lock
);
214 /* Map registers and flash base */
215 if (!(sflash
.cc
= ioremap_nocache(pci_resource_start(pdev
, 0),
216 pci_resource_len(pdev
, 0)))) {
217 printk(KERN_ERR
"sflash: error mapping registers\n");
222 /* Initialize serial flash access */
223 info
= sflash_init(sflash
.cc
);
226 printk(KERN_ERR
"sflash: found no supported devices\n");
231 /* Setup region info */
232 sflash
.region
.offset
= 0;
233 sflash
.region
.erasesize
= info
->blocksize
;
234 sflash
.region
.numblocks
= info
->numblocks
;
235 if (sflash
.region
.erasesize
> sflash
.mtd
.erasesize
)
236 sflash
.mtd
.erasesize
= sflash
.region
.erasesize
;
237 sflash
.mtd
.size
= info
->size
;
238 sflash
.mtd
.numeraseregions
= 1;
240 /* Register with MTD */
241 sflash
.mtd
.name
= "sflash";
242 sflash
.mtd
.type
= MTD_NORFLASH
;
243 sflash
.mtd
.flags
= MTD_CAP_NORFLASH
;
244 sflash
.mtd
.eraseregions
= &sflash
.region
;
245 sflash
.mtd
.module
= THIS_MODULE
;
246 sflash
.mtd
.erase
= sflash_mtd_erase
;
247 sflash
.mtd
.read
= sflash_mtd_read
;
248 sflash
.mtd
.write
= sflash_mtd_write
;
249 sflash
.mtd
.priv
= &sflash
;
251 #ifdef CONFIG_MTD_PARTITIONS
252 parts
= init_mtd_partitions(&sflash
.mtd
, sflash
.mtd
.size
);
253 for (i
= 0; parts
[i
].name
; i
++);
254 ret
= add_mtd_partitions(&sflash
.mtd
, parts
, i
);
256 ret
= add_mtd_device(&sflash
.mtd
);
259 printk(KERN_ERR
"sflash: add_mtd failed\n");
267 iounmap((void *) sflash
.cc
);
272 sflash_mtd_exit(void)
274 #ifdef CONFIG_MTD_PARTITIONS
275 del_mtd_partitions(&sflash
.mtd
);
277 del_mtd_device(&sflash
.mtd
);
279 iounmap((void *) sflash
.cc
);
282 module_init(sflash_mtd_init
);
283 module_exit(sflash_mtd_exit
);