Álvaro Fernández Rojas | d9f9bfc | 2019-08-28 19:12:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <asm/io.h> |
| 5 | #include <memalign.h> |
| 6 | #include <nand.h> |
| 7 | #include <linux/errno.h> |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/ioport.h> |
| 10 | #include <dm.h> |
| 11 | |
| 12 | #include "brcmnand.h" |
| 13 | |
| 14 | struct bcm6368_nand_soc { |
| 15 | struct brcmnand_soc soc; |
| 16 | void __iomem *base; |
| 17 | }; |
| 18 | |
| 19 | #define soc_to_priv(_soc) container_of(_soc, struct bcm6368_nand_soc, soc) |
| 20 | |
| 21 | #define BCM6368_NAND_INT 0x00 |
| 22 | #define BCM6368_NAND_STATUS_SHIFT 0 |
| 23 | #define BCM6368_NAND_STATUS_MASK (0xfff << BCM6368_NAND_STATUS_SHIFT) |
| 24 | #define BCM6368_NAND_ENABLE_SHIFT 16 |
| 25 | #define BCM6368_NAND_ENABLE_MASK (0xffff << BCM6368_NAND_ENABLE_SHIFT) |
| 26 | |
| 27 | enum { |
| 28 | BCM6368_NP_READ = BIT(0), |
| 29 | BCM6368_BLOCK_ERASE = BIT(1), |
| 30 | BCM6368_COPY_BACK = BIT(2), |
| 31 | BCM6368_PAGE_PGM = BIT(3), |
| 32 | BCM6368_CTRL_READY = BIT(4), |
| 33 | BCM6368_DEV_RBPIN = BIT(5), |
| 34 | BCM6368_ECC_ERR_UNC = BIT(6), |
| 35 | BCM6368_ECC_ERR_CORR = BIT(7), |
| 36 | }; |
| 37 | |
| 38 | static bool bcm6368_nand_intc_ack(struct brcmnand_soc *soc) |
| 39 | { |
| 40 | struct bcm6368_nand_soc *priv = soc_to_priv(soc); |
| 41 | void __iomem *mmio = priv->base + BCM6368_NAND_INT; |
| 42 | u32 val = brcmnand_readl(mmio); |
| 43 | |
| 44 | if (val & (BCM6368_CTRL_READY << BCM6368_NAND_STATUS_SHIFT)) { |
| 45 | /* Ack interrupt */ |
| 46 | val &= ~BCM6368_NAND_STATUS_MASK; |
| 47 | val |= BCM6368_CTRL_READY << BCM6368_NAND_STATUS_SHIFT; |
| 48 | brcmnand_writel(val, mmio); |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | static void bcm6368_nand_intc_set(struct brcmnand_soc *soc, bool en) |
| 56 | { |
| 57 | struct bcm6368_nand_soc *priv = soc_to_priv(soc); |
| 58 | void __iomem *mmio = priv->base + BCM6368_NAND_INT; |
| 59 | u32 val = brcmnand_readl(mmio); |
| 60 | |
| 61 | /* Don't ack any interrupts */ |
| 62 | val &= ~BCM6368_NAND_STATUS_MASK; |
| 63 | |
| 64 | if (en) |
| 65 | val |= BCM6368_CTRL_READY << BCM6368_NAND_ENABLE_SHIFT; |
| 66 | else |
| 67 | val &= ~(BCM6368_CTRL_READY << BCM6368_NAND_ENABLE_SHIFT); |
| 68 | |
| 69 | brcmnand_writel(val, mmio); |
| 70 | } |
| 71 | |
| 72 | static int bcm6368_nand_probe(struct udevice *dev) |
| 73 | { |
| 74 | struct bcm6368_nand_soc *priv = dev_get_priv(dev); |
| 75 | struct brcmnand_soc *soc = &priv->soc; |
| 76 | |
| 77 | priv->base = dev_remap_addr_name(dev, "nand-int-base"); |
| 78 | if (!priv->base) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | soc->ctlrdy_ack = bcm6368_nand_intc_ack; |
| 82 | soc->ctlrdy_set_enabled = bcm6368_nand_intc_set; |
| 83 | |
| 84 | /* Disable and ack all interrupts */ |
| 85 | brcmnand_writel(0, priv->base + BCM6368_NAND_INT); |
| 86 | brcmnand_writel(BCM6368_NAND_STATUS_MASK, |
| 87 | priv->base + BCM6368_NAND_INT); |
| 88 | |
| 89 | return brcmnand_probe(dev, soc); |
| 90 | } |
| 91 | |
| 92 | static const struct udevice_id bcm6368_nand_dt_ids[] = { |
| 93 | { |
| 94 | .compatible = "brcm,nand-bcm6368", |
| 95 | }, |
| 96 | { /* sentinel */ } |
| 97 | }; |
| 98 | |
| 99 | U_BOOT_DRIVER(bcm6368_nand) = { |
| 100 | .name = "bcm6368-nand", |
| 101 | .id = UCLASS_MTD, |
| 102 | .of_match = bcm6368_nand_dt_ids, |
| 103 | .probe = bcm6368_nand_probe, |
| 104 | .priv_auto_alloc_size = sizeof(struct bcm6368_nand_soc), |
| 105 | }; |
| 106 | |
| 107 | void board_nand_init(void) |
| 108 | { |
| 109 | struct udevice *dev; |
| 110 | int ret; |
| 111 | |
| 112 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
| 113 | DM_GET_DRIVER(bcm6368_nand), &dev); |
| 114 | if (ret && ret != -ENODEV) |
| 115 | pr_err("Failed to initialize %s. (error %d)\n", dev->name, |
| 116 | ret); |
| 117 | } |