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