xref: /linux/drivers/mtd/nand/raw/brcmnand/bcmbca_nand.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright © 2015 Broadcom Corporation
4  */
5 
6 #include <linux/device.h>
7 #include <linux/io.h>
8 #include <linux/ioport.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 
15 #include "brcmnand.h"
16 
17 struct bcmbca_nand_soc {
18 	struct brcmnand_soc soc;
19 	void __iomem *base;
20 };
21 
22 #define BCMBCA_NAND_INT_STATUS		0x00
23 #define BCMBCA_NAND_INT_EN			0x04
24 
25 enum {
26 	BCMBCA_CTLRDY		= BIT(4),
27 };
28 
29 #if defined(CONFIG_ARM64)
30 #define ALIGN_REQ		8
31 #else
32 #define ALIGN_REQ		4
33 #endif
34 
35 static inline bool bcmbca_nand_is_buf_aligned(void *flash_cache,  void *buffer)
36 {
37 	return IS_ALIGNED((uintptr_t)buffer, ALIGN_REQ) &&
38 				IS_ALIGNED((uintptr_t)flash_cache, ALIGN_REQ);
39 }
40 
41 static bool bcmbca_nand_intc_ack(struct brcmnand_soc *soc)
42 {
43 	struct bcmbca_nand_soc *priv =
44 			container_of(soc, struct bcmbca_nand_soc, soc);
45 	void __iomem *mmio = priv->base + BCMBCA_NAND_INT_STATUS;
46 	u32 val = brcmnand_readl(mmio);
47 
48 	if (val & BCMBCA_CTLRDY) {
49 		brcmnand_writel(val & ~BCMBCA_CTLRDY, mmio);
50 		return true;
51 	}
52 
53 	return false;
54 }
55 
56 static void bcmbca_nand_intc_set(struct brcmnand_soc *soc, bool en)
57 {
58 	struct bcmbca_nand_soc *priv =
59 			container_of(soc, struct bcmbca_nand_soc, soc);
60 	void __iomem *mmio = priv->base + BCMBCA_NAND_INT_EN;
61 	u32 val = brcmnand_readl(mmio);
62 
63 	if (en)
64 		val |= BCMBCA_CTLRDY;
65 	else
66 		val &= ~BCMBCA_CTLRDY;
67 
68 	brcmnand_writel(val, mmio);
69 }
70 
71 static void bcmbca_read_data_bus(struct brcmnand_soc *soc,
72 				 void __iomem *flash_cache,  u32 *buffer, int fc_words)
73 {
74 	/*
75 	 * memcpy can do unaligned aligned access depending on source
76 	 * and dest address, which is incompatible with nand cache. Fallback
77 	 * to the memcpy_fromio in such case
78 	 */
79 	if (bcmbca_nand_is_buf_aligned((void __force *)flash_cache, buffer))
80 		memcpy((void *)buffer, (void __force *)flash_cache, fc_words * 4);
81 	else
82 		memcpy_fromio((void *)buffer, flash_cache, fc_words * 4);
83 }
84 
85 static int bcmbca_nand_probe(struct platform_device *pdev)
86 {
87 	struct device *dev = &pdev->dev;
88 	struct bcmbca_nand_soc *priv;
89 	struct brcmnand_soc *soc;
90 
91 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
92 	if (!priv)
93 		return -ENOMEM;
94 	soc = &priv->soc;
95 
96 	priv->base = devm_platform_ioremap_resource_byname(pdev, "nand-int-base");
97 	if (IS_ERR(priv->base))
98 		return PTR_ERR(priv->base);
99 
100 	soc->ctlrdy_ack = bcmbca_nand_intc_ack;
101 	soc->ctlrdy_set_enabled = bcmbca_nand_intc_set;
102 	soc->read_data_bus = bcmbca_read_data_bus;
103 
104 	return brcmnand_probe(pdev, soc);
105 }
106 
107 static const struct of_device_id bcmbca_nand_of_match[] = {
108 	{ .compatible = "brcm,nand-bcm63138" },
109 	{},
110 };
111 MODULE_DEVICE_TABLE(of, bcmbca_nand_of_match);
112 
113 static struct platform_driver bcmbca_nand_driver = {
114 	.probe			= bcmbca_nand_probe,
115 	.remove_new		= brcmnand_remove,
116 	.driver = {
117 		.name		= "bcmbca_nand",
118 		.pm		= &brcmnand_pm_ops,
119 		.of_match_table	= bcmbca_nand_of_match,
120 	}
121 };
122 module_platform_driver(bcmbca_nand_driver);
123 
124 MODULE_LICENSE("GPL v2");
125 MODULE_AUTHOR("Brian Norris");
126 MODULE_DESCRIPTION("NAND driver for BCMBCA");
127