blob: 0f1a28e4765d044693be6347cfbc5dd9aab66a31 [file] [log] [blame]
Philippe Reynes74ead742020-01-07 20:14:13 +01001// 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
14struct bcm68360_nand_soc {
15 struct brcmnand_soc soc;
16 void __iomem *base;
17};
18
19#define BCM68360_NAND_INT 0x00
20#define BCM68360_NAND_STATUS_SHIFT 0
21#define BCM68360_NAND_STATUS_MASK (0xfff << BCM68360_NAND_STATUS_SHIFT)
22
23#define BCM68360_NAND_INT_EN 0x04
24#define BCM68360_NAND_ENABLE_SHIFT 0
25#define BCM68360_NAND_ENABLE_MASK (0xffff << BCM68360_NAND_ENABLE_SHIFT)
26
27enum {
28 BCM68360_NP_READ = BIT(0),
29 BCM68360_BLOCK_ERASE = BIT(1),
30 BCM68360_COPY_BACK = BIT(2),
31 BCM68360_PAGE_PGM = BIT(3),
32 BCM68360_CTRL_READY = BIT(4),
33 BCM68360_DEV_RBPIN = BIT(5),
34 BCM68360_ECC_ERR_UNC = BIT(6),
35 BCM68360_ECC_ERR_CORR = BIT(7),
36};
37
38static bool bcm68360_nand_intc_ack(struct brcmnand_soc *soc)
39{
40 struct bcm68360_nand_soc *priv =
41 container_of(soc, struct bcm68360_nand_soc, soc);
42 void __iomem *mmio = priv->base + BCM68360_NAND_INT;
43 u32 val = brcmnand_readl(mmio);
44
45 if (val & (BCM68360_CTRL_READY << BCM68360_NAND_STATUS_SHIFT)) {
46 /* Ack interrupt */
47 val &= ~BCM68360_NAND_STATUS_MASK;
48 val |= BCM68360_CTRL_READY << BCM68360_NAND_STATUS_SHIFT;
49 brcmnand_writel(val, mmio);
50 return true;
51 }
52
53 return false;
54}
55
56static void bcm68360_nand_intc_set(struct brcmnand_soc *soc, bool en)
57{
58 struct bcm68360_nand_soc *priv =
59 container_of(soc, struct bcm68360_nand_soc, soc);
60 void __iomem *mmio = priv->base + BCM68360_NAND_INT_EN;
61 u32 val = brcmnand_readl(mmio);
62
63 /* Don't ack any interrupts */
64 val &= ~BCM68360_NAND_STATUS_MASK;
65
66 if (en)
67 val |= BCM68360_CTRL_READY << BCM68360_NAND_ENABLE_SHIFT;
68 else
69 val &= ~(BCM68360_CTRL_READY << BCM68360_NAND_ENABLE_SHIFT);
70
71 brcmnand_writel(val, mmio);
72}
73
74static int bcm68360_nand_probe(struct udevice *dev)
75{
76 struct udevice *pdev = dev;
77 struct bcm68360_nand_soc *priv = dev_get_priv(dev);
78 struct brcmnand_soc *soc;
79 struct resource res;
80
81 soc = &priv->soc;
82
83 dev_read_resource_byname(pdev, "nand-int-base", &res);
84 priv->base = devm_ioremap(dev, res.start, resource_size(&res));
85 if (IS_ERR(priv->base))
86 return PTR_ERR(priv->base);
87
88 soc->ctlrdy_ack = bcm68360_nand_intc_ack;
89 soc->ctlrdy_set_enabled = bcm68360_nand_intc_set;
90
91 /* Disable and ack all interrupts */
92 brcmnand_writel(0, priv->base + BCM68360_NAND_INT_EN);
93 brcmnand_writel(0, priv->base + BCM68360_NAND_INT);
94
95 return brcmnand_probe(pdev, soc);
96}
97
98static const struct udevice_id bcm68360_nand_dt_ids[] = {
99 {
100 .compatible = "brcm,nand-bcm68360",
101 },
102 { /* sentinel */ }
103};
104
105U_BOOT_DRIVER(bcm68360_nand) = {
106 .name = "bcm68360-nand",
107 .id = UCLASS_MTD,
108 .of_match = bcm68360_nand_dt_ids,
109 .probe = bcm68360_nand_probe,
110 .priv_auto_alloc_size = sizeof(struct bcm68360_nand_soc),
111};
112
113void board_nand_init(void)
114{
115 struct udevice *dev;
116 int ret;
117
118 ret = uclass_get_device_by_driver(UCLASS_MTD,
119 DM_GET_DRIVER(bcm68360_nand), &dev);
120 if (ret && ret != -ENODEV)
121 pr_err("Failed to initialize %s. (error %d)\n", dev->name,
122 ret);
123}