aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/chips
diff options
context:
space:
mode:
authorIra W. Snyder <iws@ovro.caltech.edu>2012-01-06 14:29:19 -0500
committerDavid Woodhouse <David.Woodhouse@intel.com>2012-03-26 19:11:03 -0400
commit30ec5a2cb17d78482de0cf9e38721410d48c086d (patch)
tree6f3f92b8bd41ec220b9dd59b095ef45259aa81a3 /drivers/mtd/chips
parent192cfd58774b4d17b2fe8bdc77d89c2ef4e0591d (diff)
mtd: cfi: AMD/Fujitsu compatibles: add panic write support
This allows the mtdoops driver to work on flash chips using the AMD/Fujitsu compatible command set. As the code comments note, the locks used throughout the normal code paths in the driver are ignored, so that the chance of writing out the kernel's last messages are maximized. Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Diffstat (limited to 'drivers/mtd/chips')
-rw-r--r--drivers/mtd/chips/cfi_cmdset_0002.c240
1 files changed, 240 insertions, 0 deletions
diff --git a/drivers/mtd/chips/cfi_cmdset_0002.c b/drivers/mtd/chips/cfi_cmdset_0002.c
index 8d70895a58d..e2d94bb1d7c 100644
--- a/drivers/mtd/chips/cfi_cmdset_0002.c
+++ b/drivers/mtd/chips/cfi_cmdset_0002.c
@@ -59,6 +59,9 @@ static void cfi_amdstd_resume (struct mtd_info *);
59static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *); 59static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
60static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 60static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
61 61
62static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
63 size_t *retlen, const u_char *buf);
64
62static void cfi_amdstd_destroy(struct mtd_info *); 65static void cfi_amdstd_destroy(struct mtd_info *);
63 66
64struct mtd_info *cfi_cmdset_0002(struct map_info *, int); 67struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
@@ -443,6 +446,7 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
443 pr_debug("MTD %s(): write buffer size %d\n", __func__, 446 pr_debug("MTD %s(): write buffer size %d\n", __func__,
444 mtd->writebufsize); 447 mtd->writebufsize);
445 448
449 mtd->panic_write = cfi_amdstd_panic_write;
446 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot; 450 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
447 451
448 if (cfi->cfi_mode==CFI_MODE_CFI){ 452 if (cfi->cfi_mode==CFI_MODE_CFI){
@@ -1562,6 +1566,242 @@ static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1562 return 0; 1566 return 0;
1563} 1567}
1564 1568
1569/*
1570 * Wait for the flash chip to become ready to write data
1571 *
1572 * This is only called during the panic_write() path. When panic_write()
1573 * is called, the kernel is in the process of a panic, and will soon be
1574 * dead. Therefore we don't take any locks, and attempt to get access
1575 * to the chip as soon as possible.
1576 */
1577static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
1578 unsigned long adr)
1579{
1580 struct cfi_private *cfi = map->fldrv_priv;
1581 int retries = 10;
1582 int i;
1583
1584 /*
1585 * If the driver thinks the chip is idle, and no toggle bits
1586 * are changing, then the chip is actually idle for sure.
1587 */
1588 if (chip->state == FL_READY && chip_ready(map, adr))
1589 return 0;
1590
1591 /*
1592 * Try several times to reset the chip and then wait for it
1593 * to become idle. The upper limit of a few milliseconds of
1594 * delay isn't a big problem: the kernel is dying anyway. It
1595 * is more important to save the messages.
1596 */
1597 while (retries > 0) {
1598 const unsigned long timeo = (HZ / 1000) + 1;
1599
1600 /* send the reset command */
1601 map_write(map, CMD(0xF0), chip->start);
1602
1603 /* wait for the chip to become ready */
1604 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
1605 if (chip_ready(map, adr))
1606 return 0;
1607
1608 udelay(1);
1609 }
1610 }
1611
1612 /* the chip never became ready */
1613 return -EBUSY;
1614}
1615
1616/*
1617 * Write out one word of data to a single flash chip during a kernel panic
1618 *
1619 * This is only called during the panic_write() path. When panic_write()
1620 * is called, the kernel is in the process of a panic, and will soon be
1621 * dead. Therefore we don't take any locks, and attempt to get access
1622 * to the chip as soon as possible.
1623 *
1624 * The implementation of this routine is intentionally similar to
1625 * do_write_oneword(), in order to ease code maintenance.
1626 */
1627static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
1628 unsigned long adr, map_word datum)
1629{
1630 const unsigned long uWriteTimeout = (HZ / 1000) + 1;
1631 struct cfi_private *cfi = map->fldrv_priv;
1632 int retry_cnt = 0;
1633 map_word oldd;
1634 int ret = 0;
1635 int i;
1636
1637 adr += chip->start;
1638
1639 ret = cfi_amdstd_panic_wait(map, chip, adr);
1640 if (ret)
1641 return ret;
1642
1643 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
1644 __func__, adr, datum.x[0]);
1645
1646 /*
1647 * Check for a NOP for the case when the datum to write is already
1648 * present - it saves time and works around buggy chips that corrupt
1649 * data at other locations when 0xff is written to a location that
1650 * already contains 0xff.
1651 */
1652 oldd = map_read(map, adr);
1653 if (map_word_equal(map, oldd, datum)) {
1654 pr_debug("MTD %s(): NOP\n", __func__);
1655 goto op_done;
1656 }
1657
1658 ENABLE_VPP(map);
1659
1660retry:
1661 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1662 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1663 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1664 map_write(map, datum, adr);
1665
1666 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
1667 if (chip_ready(map, adr))
1668 break;
1669
1670 udelay(1);
1671 }
1672
1673 if (!chip_good(map, adr, datum)) {
1674 /* reset on all failures. */
1675 map_write(map, CMD(0xF0), chip->start);
1676 /* FIXME - should have reset delay before continuing */
1677
1678 if (++retry_cnt <= MAX_WORD_RETRIES)
1679 goto retry;
1680
1681 ret = -EIO;
1682 }
1683
1684op_done:
1685 DISABLE_VPP(map);
1686 return ret;
1687}
1688
1689/*
1690 * Write out some data during a kernel panic
1691 *
1692 * This is used by the mtdoops driver to save the dying messages from a
1693 * kernel which has panic'd.
1694 *
1695 * This routine ignores all of the locking used throughout the rest of the
1696 * driver, in order to ensure that the data gets written out no matter what
1697 * state this driver (and the flash chip itself) was in when the kernel crashed.
1698 *
1699 * The implementation of this routine is intentionally similar to
1700 * cfi_amdstd_write_words(), in order to ease code maintenance.
1701 */
1702static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1703 size_t *retlen, const u_char *buf)
1704{
1705 struct map_info *map = mtd->priv;
1706 struct cfi_private *cfi = map->fldrv_priv;
1707 unsigned long ofs, chipstart;
1708 int ret = 0;
1709 int chipnum;
1710
1711 *retlen = 0;
1712 if (!len)
1713 return 0;
1714
1715 chipnum = to >> cfi->chipshift;
1716 ofs = to - (chipnum << cfi->chipshift);
1717 chipstart = cfi->chips[chipnum].start;
1718
1719 /* If it's not bus aligned, do the first byte write */
1720 if (ofs & (map_bankwidth(map) - 1)) {
1721 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
1722 int i = ofs - bus_ofs;
1723 int n = 0;
1724 map_word tmp_buf;
1725
1726 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
1727 if (ret)
1728 return ret;
1729
1730 /* Load 'tmp_buf' with old contents of flash */
1731 tmp_buf = map_read(map, bus_ofs + chipstart);
1732
1733 /* Number of bytes to copy from buffer */
1734 n = min_t(int, len, map_bankwidth(map) - i);
1735
1736 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1737
1738 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1739 bus_ofs, tmp_buf);
1740 if (ret)
1741 return ret;
1742
1743 ofs += n;
1744 buf += n;
1745 (*retlen) += n;
1746 len -= n;
1747
1748 if (ofs >> cfi->chipshift) {
1749 chipnum++;
1750 ofs = 0;
1751 if (chipnum == cfi->numchips)
1752 return 0;
1753 }
1754 }
1755
1756 /* We are now aligned, write as much as possible */
1757 while (len >= map_bankwidth(map)) {
1758 map_word datum;
1759
1760 datum = map_word_load(map, buf);
1761
1762 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1763 ofs, datum);
1764 if (ret)
1765 return ret;
1766
1767 ofs += map_bankwidth(map);
1768 buf += map_bankwidth(map);
1769 (*retlen) += map_bankwidth(map);
1770 len -= map_bankwidth(map);
1771
1772 if (ofs >> cfi->chipshift) {
1773 chipnum++;
1774 ofs = 0;
1775 if (chipnum == cfi->numchips)
1776 return 0;
1777
1778 chipstart = cfi->chips[chipnum].start;
1779 }
1780 }
1781
1782 /* Write the trailing bytes if any */
1783 if (len & (map_bankwidth(map) - 1)) {
1784 map_word tmp_buf;
1785
1786 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
1787 if (ret)
1788 return ret;
1789
1790 tmp_buf = map_read(map, ofs + chipstart);
1791
1792 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1793
1794 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1795 ofs, tmp_buf);
1796 if (ret)
1797 return ret;
1798
1799 (*retlen) += len;
1800 }
1801
1802 return 0;
1803}
1804
1565 1805
1566/* 1806/*
1567 * Handle devices with one erase region, that only implement 1807 * Handle devices with one erase region, that only implement