1 // SPDX-License-Identifier: GPL-2.0-only
3 * TXx9 NAND flash memory controller driver
4 * Based on RBTX49xx patch from CELF patch archive.
6 * (C) Copyright TOSHIBA CORPORATION 2004-2007
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/rawnand.h>
17 #include <linux/mtd/nand_ecc.h>
18 #include <linux/mtd/partitions.h>
20 #include <linux/platform_data/txx9/ndfmc.h>
22 /* TXX9 NDFMC Registers */
23 #define TXX9_NDFDTR 0x00
24 #define TXX9_NDFMCR 0x04
25 #define TXX9_NDFSR 0x08
26 #define TXX9_NDFISR 0x0c
27 #define TXX9_NDFIMR 0x10
28 #define TXX9_NDFSPR 0x14
29 #define TXX9_NDFRSTR 0x18 /* not TX4939 */
31 /* NDFMCR : NDFMC Mode Control */
32 #define TXX9_NDFMCR_WE 0x80
33 #define TXX9_NDFMCR_ECC_ALL 0x60
34 #define TXX9_NDFMCR_ECC_RESET 0x60
35 #define TXX9_NDFMCR_ECC_READ 0x40
36 #define TXX9_NDFMCR_ECC_ON 0x20
37 #define TXX9_NDFMCR_ECC_OFF 0x00
38 #define TXX9_NDFMCR_CE 0x10
39 #define TXX9_NDFMCR_BSPRT 0x04 /* TX4925/TX4926 only */
40 #define TXX9_NDFMCR_ALE 0x02
41 #define TXX9_NDFMCR_CLE 0x01
43 #define TXX9_NDFMCR_X16 0x0400
44 #define TXX9_NDFMCR_DMAREQ_MASK 0x0300
45 #define TXX9_NDFMCR_DMAREQ_NODMA 0x0000
46 #define TXX9_NDFMCR_DMAREQ_128 0x0100
47 #define TXX9_NDFMCR_DMAREQ_256 0x0200
48 #define TXX9_NDFMCR_DMAREQ_512 0x0300
49 #define TXX9_NDFMCR_CS_MASK 0x0c
50 #define TXX9_NDFMCR_CS(ch) ((ch) << 2)
52 /* NDFMCR : NDFMC Status */
53 #define TXX9_NDFSR_BUSY 0x80
55 #define TXX9_NDFSR_DMARUN 0x40
57 /* NDFMCR : NDFMC Reset */
58 #define TXX9_NDFRSTR_RST 0x01
60 struct txx9ndfmc_priv {
61 struct platform_device *dev;
62 struct nand_chip chip;
67 #define MAX_TXX9NDFMC_DEV 4
68 struct txx9ndfmc_drvdata {
69 struct mtd_info *mtds[MAX_TXX9NDFMC_DEV];
71 unsigned char hold; /* in gbusclock */
72 unsigned char spw; /* in gbusclock */
73 struct nand_controller controller;
76 static struct platform_device *mtd_to_platdev(struct mtd_info *mtd)
78 struct nand_chip *chip = mtd_to_nand(mtd);
79 struct txx9ndfmc_priv *txx9_priv = nand_get_controller_data(chip);
80 return txx9_priv->dev;
83 static void __iomem *ndregaddr(struct platform_device *dev, unsigned int reg)
85 struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
86 struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
88 return drvdata->base + (reg << plat->shift);
91 static u32 txx9ndfmc_read(struct platform_device *dev, unsigned int reg)
93 return __raw_readl(ndregaddr(dev, reg));
96 static void txx9ndfmc_write(struct platform_device *dev,
97 u32 val, unsigned int reg)
99 __raw_writel(val, ndregaddr(dev, reg));
102 static uint8_t txx9ndfmc_read_byte(struct nand_chip *chip)
104 struct platform_device *dev = mtd_to_platdev(nand_to_mtd(chip));
106 return txx9ndfmc_read(dev, TXX9_NDFDTR);
109 static void txx9ndfmc_write_buf(struct nand_chip *chip, const uint8_t *buf,
112 struct platform_device *dev = mtd_to_platdev(nand_to_mtd(chip));
113 void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
114 u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
116 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_WE, TXX9_NDFMCR);
118 __raw_writel(*buf++, ndfdtr);
119 txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
122 static void txx9ndfmc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
124 struct platform_device *dev = mtd_to_platdev(nand_to_mtd(chip));
125 void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
128 *buf++ = __raw_readl(ndfdtr);
131 static void txx9ndfmc_cmd_ctrl(struct nand_chip *chip, int cmd,
134 struct txx9ndfmc_priv *txx9_priv = nand_get_controller_data(chip);
135 struct platform_device *dev = txx9_priv->dev;
136 struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
138 if (ctrl & NAND_CTRL_CHANGE) {
139 u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
141 mcr &= ~(TXX9_NDFMCR_CLE | TXX9_NDFMCR_ALE | TXX9_NDFMCR_CE);
142 mcr |= ctrl & NAND_CLE ? TXX9_NDFMCR_CLE : 0;
143 mcr |= ctrl & NAND_ALE ? TXX9_NDFMCR_ALE : 0;
144 /* TXX9_NDFMCR_CE bit is 0:high 1:low */
145 mcr |= ctrl & NAND_NCE ? TXX9_NDFMCR_CE : 0;
146 if (txx9_priv->cs >= 0 && (ctrl & NAND_NCE)) {
147 mcr &= ~TXX9_NDFMCR_CS_MASK;
148 mcr |= TXX9_NDFMCR_CS(txx9_priv->cs);
150 txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
152 if (cmd != NAND_CMD_NONE)
153 txx9ndfmc_write(dev, cmd & 0xff, TXX9_NDFDTR);
154 if (plat->flags & NDFMC_PLAT_FLAG_DUMMYWRITE) {
155 /* dummy write to update external latch */
156 if ((ctrl & NAND_CTRL_CHANGE) && cmd == NAND_CMD_NONE)
157 txx9ndfmc_write(dev, 0, TXX9_NDFDTR);
161 static int txx9ndfmc_dev_ready(struct nand_chip *chip)
163 struct platform_device *dev = mtd_to_platdev(nand_to_mtd(chip));
165 return !(txx9ndfmc_read(dev, TXX9_NDFSR) & TXX9_NDFSR_BUSY);
168 static int txx9ndfmc_calculate_ecc(struct nand_chip *chip, const uint8_t *dat,
171 struct platform_device *dev = mtd_to_platdev(nand_to_mtd(chip));
173 u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
175 mcr &= ~TXX9_NDFMCR_ECC_ALL;
176 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
177 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_READ, TXX9_NDFMCR);
178 for (eccbytes = chip->ecc.bytes; eccbytes > 0; eccbytes -= 3) {
179 ecc_code[1] = txx9ndfmc_read(dev, TXX9_NDFDTR);
180 ecc_code[0] = txx9ndfmc_read(dev, TXX9_NDFDTR);
181 ecc_code[2] = txx9ndfmc_read(dev, TXX9_NDFDTR);
184 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
188 static int txx9ndfmc_correct_data(struct nand_chip *chip, unsigned char *buf,
189 unsigned char *read_ecc,
190 unsigned char *calc_ecc)
196 for (eccsize = chip->ecc.size; eccsize > 0; eccsize -= 256) {
197 stat = __nand_correct_data(buf, read_ecc, calc_ecc, 256,
209 static void txx9ndfmc_enable_hwecc(struct nand_chip *chip, int mode)
211 struct platform_device *dev = mtd_to_platdev(nand_to_mtd(chip));
212 u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
214 mcr &= ~TXX9_NDFMCR_ECC_ALL;
215 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_RESET, TXX9_NDFMCR);
216 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
217 txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_ON, TXX9_NDFMCR);
220 static void txx9ndfmc_initialize(struct platform_device *dev)
222 struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
223 struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
226 if (plat->flags & NDFMC_PLAT_FLAG_NO_RSTR)
227 ; /* no NDFRSTR. Write to NDFSPR resets the NDFMC. */
231 txx9ndfmc_read(dev, TXX9_NDFRSTR) |
234 while (txx9ndfmc_read(dev, TXX9_NDFRSTR) & TXX9_NDFRSTR_RST) {
236 dev_err(&dev->dev, "reset failed.\n");
242 /* setup Hold Time, Strobe Pulse Width */
243 txx9ndfmc_write(dev, (drvdata->hold << 4) | drvdata->spw, TXX9_NDFSPR);
245 (plat->flags & NDFMC_PLAT_FLAG_USE_BSPRT) ?
246 TXX9_NDFMCR_BSPRT : 0, TXX9_NDFMCR);
249 #define TXX9NDFMC_NS_TO_CYC(gbusclk, ns) \
250 DIV_ROUND_UP((ns) * DIV_ROUND_UP(gbusclk, 1000), 1000000)
252 static int txx9ndfmc_attach_chip(struct nand_chip *chip)
254 struct mtd_info *mtd = nand_to_mtd(chip);
256 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
259 chip->ecc.strength = 1;
261 if (mtd->writesize >= 512) {
262 chip->ecc.size = 512;
265 chip->ecc.size = 256;
269 chip->ecc.calculate = txx9ndfmc_calculate_ecc;
270 chip->ecc.correct = txx9ndfmc_correct_data;
271 chip->ecc.hwctl = txx9ndfmc_enable_hwecc;
276 static const struct nand_controller_ops txx9ndfmc_controller_ops = {
277 .attach_chip = txx9ndfmc_attach_chip,
280 static int __init txx9ndfmc_probe(struct platform_device *dev)
282 struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
285 struct txx9ndfmc_drvdata *drvdata;
286 unsigned long gbusclk = plat->gbus_clock;
287 struct resource *res;
289 drvdata = devm_kzalloc(&dev->dev, sizeof(*drvdata), GFP_KERNEL);
292 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
293 drvdata->base = devm_ioremap_resource(&dev->dev, res);
294 if (IS_ERR(drvdata->base))
295 return PTR_ERR(drvdata->base);
297 hold = plat->hold ?: 20; /* tDH */
298 spw = plat->spw ?: 90; /* max(tREADID, tWP, tRP) */
300 hold = TXX9NDFMC_NS_TO_CYC(gbusclk, hold);
301 spw = TXX9NDFMC_NS_TO_CYC(gbusclk, spw);
302 if (plat->flags & NDFMC_PLAT_FLAG_HOLDADD)
303 hold -= 2; /* actual hold time : (HOLD + 2) BUSCLK */
304 spw -= 1; /* actual wait time : (SPW + 1) BUSCLK */
305 hold = clamp(hold, 1, 15);
306 drvdata->hold = hold;
307 spw = clamp(spw, 1, 15);
309 dev_info(&dev->dev, "CLK:%ldMHz HOLD:%d SPW:%d\n",
310 (gbusclk + 500000) / 1000000, hold, spw);
312 nand_controller_init(&drvdata->controller);
313 drvdata->controller.ops = &txx9ndfmc_controller_ops;
315 platform_set_drvdata(dev, drvdata);
316 txx9ndfmc_initialize(dev);
318 for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
319 struct txx9ndfmc_priv *txx9_priv;
320 struct nand_chip *chip;
321 struct mtd_info *mtd;
323 if (!(plat->ch_mask & (1 << i)))
325 txx9_priv = kzalloc(sizeof(struct txx9ndfmc_priv),
329 chip = &txx9_priv->chip;
330 mtd = nand_to_mtd(chip);
331 mtd->dev.parent = &dev->dev;
333 chip->legacy.read_byte = txx9ndfmc_read_byte;
334 chip->legacy.read_buf = txx9ndfmc_read_buf;
335 chip->legacy.write_buf = txx9ndfmc_write_buf;
336 chip->legacy.cmd_ctrl = txx9ndfmc_cmd_ctrl;
337 chip->legacy.dev_ready = txx9ndfmc_dev_ready;
338 chip->legacy.chip_delay = 100;
339 chip->controller = &drvdata->controller;
341 nand_set_controller_data(chip, txx9_priv);
342 txx9_priv->dev = dev;
344 if (plat->ch_mask != 1) {
346 txx9_priv->mtdname = kasprintf(GFP_KERNEL, "%s.%u",
347 dev_name(&dev->dev), i);
350 txx9_priv->mtdname = kstrdup(dev_name(&dev->dev),
353 if (!txx9_priv->mtdname) {
355 dev_err(&dev->dev, "Unable to allocate MTD name.\n");
358 if (plat->wide_mask & (1 << i))
359 chip->options |= NAND_BUSWIDTH_16;
361 if (nand_scan(chip, 1)) {
362 kfree(txx9_priv->mtdname);
366 mtd->name = txx9_priv->mtdname;
368 mtd_device_register(mtd, NULL, 0);
369 drvdata->mtds[i] = mtd;
375 static int __exit txx9ndfmc_remove(struct platform_device *dev)
377 struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
382 for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
383 struct mtd_info *mtd = drvdata->mtds[i];
384 struct nand_chip *chip;
385 struct txx9ndfmc_priv *txx9_priv;
389 chip = mtd_to_nand(mtd);
390 txx9_priv = nand_get_controller_data(chip);
392 ret = mtd_device_unregister(nand_to_mtd(chip));
395 kfree(txx9_priv->mtdname);
402 static int txx9ndfmc_resume(struct platform_device *dev)
404 if (platform_get_drvdata(dev))
405 txx9ndfmc_initialize(dev);
409 #define txx9ndfmc_resume NULL
412 static struct platform_driver txx9ndfmc_driver = {
413 .remove = __exit_p(txx9ndfmc_remove),
414 .resume = txx9ndfmc_resume,
420 module_platform_driver_probe(txx9ndfmc_driver, txx9ndfmc_probe);
422 MODULE_LICENSE("GPL");
423 MODULE_DESCRIPTION("TXx9 SoC NAND flash controller driver");
424 MODULE_ALIAS("platform:txx9ndfmc");