blob: 209170ed2b764511d6224ab8ddb8996059c26663 [file] [log] [blame]
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +02001/*
2 * drivers/mtd/nand/orion_nand.c
3 *
4 * NAND support for Marvell Orion SoC platforms
5 *
6 * Tzachi Perelstein <tzachi@marvell.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
Jamie Lentina0fabf722012-04-18 11:06:41 +010016#include <linux/of.h>
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020017#include <linux/mtd/mtd.h>
18#include <linux/mtd/nand.h>
19#include <linux/mtd/partitions.h>
Andrew Lunn9c2bd502012-02-19 11:01:22 +010020#include <linux/clk.h>
21#include <linux/err.h>
Michael Opdenackera0fa0b62014-10-16 06:58:35 +020022#include <linux/io.h>
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020023#include <asm/sizes.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020024#include <linux/platform_data/mtd-orion_nand.h>
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020025
Simon Baatz675b11d2017-03-27 20:02:07 +020026struct orion_nand_info {
27 struct nand_chip chip;
28 struct clk *clk;
29};
30
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020031static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
32{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010033 struct nand_chip *nc = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010034 struct orion_nand_data *board = nand_get_controller_data(nc);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020035 u32 offs;
36
37 if (cmd == NAND_CMD_NONE)
38 return;
39
40 if (ctrl & NAND_CLE)
41 offs = (1 << board->cle);
42 else if (ctrl & NAND_ALE)
43 offs = (1 << board->ale);
44 else
45 return;
46
47 if (nc->options & NAND_BUSWIDTH_16)
48 offs <<= 1;
49
50 writeb(cmd, nc->IO_ADDR_W + offs);
51}
52
Nicolas Pitrebfee1a42009-05-31 22:25:40 -040053static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
54{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010055 struct nand_chip *chip = mtd_to_nand(mtd);
Nicolas Pitrebfee1a42009-05-31 22:25:40 -040056 void __iomem *io_base = chip->IO_ADDR_R;
57 uint64_t *buf64;
58 int i = 0;
59
60 while (len && (unsigned long)buf & 7) {
61 *buf++ = readb(io_base);
62 len--;
63 }
64 buf64 = (uint64_t *)buf;
65 while (i < len/8) {
Paulius Zaleckasa88a2b82010-04-23 13:17:47 -040066 /*
67 * Since GCC has no proper constraint (PR 43518)
68 * force x variable to r2/r3 registers as ldrd instruction
69 * requires first register to be even.
70 */
71 register uint64_t x asm ("r2");
72
Simon Kagstrom94da2102009-08-20 09:19:53 +020073 asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
Nicolas Pitrebfee1a42009-05-31 22:25:40 -040074 buf64[i++] = x;
75 }
76 i *= 8;
77 while (i < len)
78 buf[i++] = readb(io_base);
79}
80
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020081static int __init orion_nand_probe(struct platform_device *pdev)
82{
Simon Baatz675b11d2017-03-27 20:02:07 +020083 struct orion_nand_info *info;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020084 struct mtd_info *mtd;
85 struct nand_chip *nc;
86 struct orion_nand_data *board;
H Hartley Sweetene9903062009-12-14 16:48:34 -050087 struct resource *res;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020088 void __iomem *io_base;
89 int ret = 0;
Jamie Lentina0fabf722012-04-18 11:06:41 +010090 u32 val = 0;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020091
Simon Baatz675b11d2017-03-27 20:02:07 +020092 info = devm_kzalloc(&pdev->dev,
93 sizeof(struct orion_nand_info),
Michael Opdenackera0fa0b62014-10-16 06:58:35 +020094 GFP_KERNEL);
Simon Baatz675b11d2017-03-27 20:02:07 +020095 if (!info)
Michael Opdenackera0fa0b62014-10-16 06:58:35 +020096 return -ENOMEM;
Simon Baatz675b11d2017-03-27 20:02:07 +020097 nc = &info->chip;
Boris BREZILLON53cd2682015-12-10 09:00:17 +010098 mtd = nand_to_mtd(nc);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +020099
H Hartley Sweetene9903062009-12-14 16:48:34 -0500100 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Michael Opdenackera0fa0b62014-10-16 06:58:35 +0200101 io_base = devm_ioremap_resource(&pdev->dev, res);
H Hartley Sweetene9903062009-12-14 16:48:34 -0500102
Michael Opdenackera0fa0b62014-10-16 06:58:35 +0200103 if (IS_ERR(io_base))
104 return PTR_ERR(io_base);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200105
Jamie Lentina0fabf722012-04-18 11:06:41 +0100106 if (pdev->dev.of_node) {
107 board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
108 GFP_KERNEL);
Michael Opdenackera0fa0b62014-10-16 06:58:35 +0200109 if (!board)
110 return -ENOMEM;
Jamie Lentina0fabf722012-04-18 11:06:41 +0100111 if (!of_property_read_u32(pdev->dev.of_node, "cle", &val))
112 board->cle = (u8)val;
113 else
114 board->cle = 0;
115 if (!of_property_read_u32(pdev->dev.of_node, "ale", &val))
116 board->ale = (u8)val;
117 else
118 board->ale = 1;
119 if (!of_property_read_u32(pdev->dev.of_node,
120 "bank-width", &val))
121 board->width = (u8)val * 8;
122 else
123 board->width = 8;
124 if (!of_property_read_u32(pdev->dev.of_node,
125 "chip-delay", &val))
126 board->chip_delay = (u8)val;
Jingoo Han453810b2013-07-30 17:18:33 +0900127 } else {
128 board = dev_get_platdata(&pdev->dev);
129 }
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200130
Frans Klaver84630992015-06-10 22:38:58 +0200131 mtd->dev.parent = &pdev->dev;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200132
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100133 nand_set_controller_data(nc, board);
Brian Norrisa61ae812015-10-30 20:33:25 -0700134 nand_set_flash_node(nc, pdev->dev.of_node);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200135 nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
136 nc->cmd_ctrl = orion_nand_cmd_ctrl;
Nicolas Pitrebfee1a42009-05-31 22:25:40 -0400137 nc->read_buf = orion_nand_read_buf;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200138 nc->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckiac7efcb2016-04-08 12:23:48 +0200139 nc->ecc.algo = NAND_ECC_HAMMING;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200140
Saeed Bisharaf4db56f2008-05-04 19:25:52 -1100141 if (board->chip_delay)
142 nc->chip_delay = board->chip_delay;
143
Jamie Lentina0fabf722012-04-18 11:06:41 +0100144 WARN(board->width > 16,
145 "%d bit bus width out of range",
146 board->width);
147
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200148 if (board->width == 16)
149 nc->options |= NAND_BUSWIDTH_16;
150
Ben Dookseedfea22010-04-20 10:26:18 +0100151 if (board->dev_ready)
152 nc->dev_ready = board->dev_ready;
153
Simon Baatz675b11d2017-03-27 20:02:07 +0200154 platform_set_drvdata(pdev, info);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200155
Andrew Lunn9c2bd502012-02-19 11:01:22 +0100156 /* Not all platforms can gate the clock, so it is not
157 an error if the clock does not exists. */
Simon Baatz675b11d2017-03-27 20:02:07 +0200158 info->clk = devm_clk_get(&pdev->dev, NULL);
Simon Baatzef980cf82017-03-27 20:02:08 +0200159 if (IS_ERR(info->clk)) {
160 ret = PTR_ERR(info->clk);
161 if (ret == -ENOENT) {
162 info->clk = NULL;
163 } else {
164 dev_err(&pdev->dev, "failed to get clock!\n");
165 return ret;
166 }
167 }
168
Arvind Yadav3762a332017-06-01 16:28:15 +0530169 ret = clk_prepare_enable(info->clk);
170 if (ret) {
171 dev_err(&pdev->dev, "failed to prepare clock!\n");
172 return ret;
173 }
Andrew Lunn9c2bd502012-02-19 11:01:22 +0100174
Masahiro Yamada6c34ad72016-11-04 19:42:55 +0900175 ret = nand_scan(mtd, 1);
176 if (ret)
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200177 goto no_dev;
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200178
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200179 mtd->name = "orion_nand";
Brian Norrisa61ae812015-10-30 20:33:25 -0700180 ret = mtd_device_register(mtd, board->parts, board->nr_parts);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200181 if (ret) {
182 nand_release(mtd);
183 goto no_dev;
184 }
185
186 return 0;
187
188no_dev:
Simon Baatzef980cf82017-03-27 20:02:08 +0200189 clk_disable_unprepare(info->clk);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200190 return ret;
191}
192
Bill Pemberton810b7e02012-11-19 13:26:04 -0500193static int orion_nand_remove(struct platform_device *pdev)
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200194{
Simon Baatz675b11d2017-03-27 20:02:07 +0200195 struct orion_nand_info *info = platform_get_drvdata(pdev);
196 struct nand_chip *chip = &info->chip;
197 struct mtd_info *mtd = nand_to_mtd(chip);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200198
199 nand_release(mtd);
200
Simon Baatzef980cf82017-03-27 20:02:08 +0200201 clk_disable_unprepare(info->clk);
Andrew Lunn9c2bd502012-02-19 11:01:22 +0100202
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200203 return 0;
204}
205
Jamie Lentina0fabf722012-04-18 11:06:41 +0100206#ifdef CONFIG_OF
Jingoo Hancb3346a2014-05-07 17:50:23 +0900207static const struct of_device_id orion_nand_of_match_table[] = {
Andrew Lunn77843502012-07-18 19:22:54 +0200208 { .compatible = "marvell,orion-nand", },
Jamie Lentina0fabf722012-04-18 11:06:41 +0100209 {},
210};
Luis de Bethencourt98d1a5e2015-09-18 00:14:02 +0200211MODULE_DEVICE_TABLE(of, orion_nand_of_match_table);
Jamie Lentina0fabf722012-04-18 11:06:41 +0100212#endif
213
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200214static struct platform_driver orion_nand_driver = {
Bill Pemberton5153b882012-11-19 13:21:24 -0500215 .remove = orion_nand_remove,
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200216 .driver = {
217 .name = "orion_nand",
Jamie Lentina0fabf722012-04-18 11:06:41 +0100218 .of_match_table = of_match_ptr(orion_nand_of_match_table),
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200219 },
220};
221
Jingoo Hand9ba3102013-03-05 13:29:20 +0900222module_platform_driver_probe(orion_nand_driver, orion_nand_probe);
Tzachi Perelstein2a1dba22007-10-17 01:10:40 +0200223
224MODULE_LICENSE("GPL");
225MODULE_AUTHOR("Tzachi Perelstein");
226MODULE_DESCRIPTION("NAND glue for Orion platforms");
Kay Sievers1ff18422008-04-18 13:44:27 -0700227MODULE_ALIAS("platform:orion_nand");