| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright © 2008 Ilya Yanok, Emcraft Systems |
|---|
| 3 | | - * |
|---|
| 4 | | - * |
|---|
| 5 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 7 | | - * published by the Free Software Foundation. |
|---|
| 8 | | - * |
|---|
| 9 | 4 | */ |
|---|
| 10 | 5 | |
|---|
| 11 | 6 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 27 | 22 | #define FPGA_NAND_DATA_SHIFT 16 |
|---|
| 28 | 23 | |
|---|
| 29 | 24 | struct socrates_nand_host { |
|---|
| 25 | + struct nand_controller controller; |
|---|
| 30 | 26 | struct nand_chip nand_chip; |
|---|
| 31 | 27 | void __iomem *io_base; |
|---|
| 32 | 28 | struct device *dev; |
|---|
| .. | .. |
|---|
| 34 | 30 | |
|---|
| 35 | 31 | /** |
|---|
| 36 | 32 | * socrates_nand_write_buf - write buffer to chip |
|---|
| 37 | | - * @mtd: MTD device structure |
|---|
| 33 | + * @this: NAND chip object |
|---|
| 38 | 34 | * @buf: data buffer |
|---|
| 39 | 35 | * @len: number of bytes to write |
|---|
| 40 | 36 | */ |
|---|
| 41 | | -static void socrates_nand_write_buf(struct mtd_info *mtd, |
|---|
| 42 | | - const uint8_t *buf, int len) |
|---|
| 37 | +static void socrates_nand_write_buf(struct nand_chip *this, const uint8_t *buf, |
|---|
| 38 | + int len) |
|---|
| 43 | 39 | { |
|---|
| 44 | 40 | int i; |
|---|
| 45 | | - struct nand_chip *this = mtd_to_nand(mtd); |
|---|
| 46 | 41 | struct socrates_nand_host *host = nand_get_controller_data(this); |
|---|
| 47 | 42 | |
|---|
| 48 | 43 | for (i = 0; i < len; i++) { |
|---|
| .. | .. |
|---|
| 54 | 49 | |
|---|
| 55 | 50 | /** |
|---|
| 56 | 51 | * socrates_nand_read_buf - read chip data into buffer |
|---|
| 57 | | - * @mtd: MTD device structure |
|---|
| 52 | + * @this: NAND chip object |
|---|
| 58 | 53 | * @buf: buffer to store date |
|---|
| 59 | 54 | * @len: number of bytes to read |
|---|
| 60 | 55 | */ |
|---|
| 61 | | -static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
|---|
| 56 | +static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf, |
|---|
| 57 | + int len) |
|---|
| 62 | 58 | { |
|---|
| 63 | 59 | int i; |
|---|
| 64 | | - struct nand_chip *this = mtd_to_nand(mtd); |
|---|
| 65 | 60 | struct socrates_nand_host *host = nand_get_controller_data(this); |
|---|
| 66 | 61 | uint32_t val; |
|---|
| 67 | 62 | |
|---|
| .. | .. |
|---|
| 78 | 73 | * socrates_nand_read_byte - read one byte from the chip |
|---|
| 79 | 74 | * @mtd: MTD device structure |
|---|
| 80 | 75 | */ |
|---|
| 81 | | -static uint8_t socrates_nand_read_byte(struct mtd_info *mtd) |
|---|
| 76 | +static uint8_t socrates_nand_read_byte(struct nand_chip *this) |
|---|
| 82 | 77 | { |
|---|
| 83 | 78 | uint8_t byte; |
|---|
| 84 | | - socrates_nand_read_buf(mtd, &byte, sizeof(byte)); |
|---|
| 79 | + socrates_nand_read_buf(this, &byte, sizeof(byte)); |
|---|
| 85 | 80 | return byte; |
|---|
| 86 | | -} |
|---|
| 87 | | - |
|---|
| 88 | | -/** |
|---|
| 89 | | - * socrates_nand_read_word - read one word from the chip |
|---|
| 90 | | - * @mtd: MTD device structure |
|---|
| 91 | | - */ |
|---|
| 92 | | -static uint16_t socrates_nand_read_word(struct mtd_info *mtd) |
|---|
| 93 | | -{ |
|---|
| 94 | | - uint16_t word; |
|---|
| 95 | | - socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word)); |
|---|
| 96 | | - return word; |
|---|
| 97 | 81 | } |
|---|
| 98 | 82 | |
|---|
| 99 | 83 | /* |
|---|
| 100 | 84 | * Hardware specific access to control-lines |
|---|
| 101 | 85 | */ |
|---|
| 102 | | -static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, |
|---|
| 103 | | - unsigned int ctrl) |
|---|
| 86 | +static void socrates_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd, |
|---|
| 87 | + unsigned int ctrl) |
|---|
| 104 | 88 | { |
|---|
| 105 | | - struct nand_chip *nand_chip = mtd_to_nand(mtd); |
|---|
| 106 | 89 | struct socrates_nand_host *host = nand_get_controller_data(nand_chip); |
|---|
| 107 | 90 | uint32_t val; |
|---|
| 108 | 91 | |
|---|
| .. | .. |
|---|
| 125 | 108 | /* |
|---|
| 126 | 109 | * Read the Device Ready pin. |
|---|
| 127 | 110 | */ |
|---|
| 128 | | -static int socrates_nand_device_ready(struct mtd_info *mtd) |
|---|
| 111 | +static int socrates_nand_device_ready(struct nand_chip *nand_chip) |
|---|
| 129 | 112 | { |
|---|
| 130 | | - struct nand_chip *nand_chip = mtd_to_nand(mtd); |
|---|
| 131 | 113 | struct socrates_nand_host *host = nand_get_controller_data(nand_chip); |
|---|
| 132 | 114 | |
|---|
| 133 | 115 | if (in_be32(host->io_base) & FPGA_NAND_BUSY) |
|---|
| 134 | 116 | return 0; /* busy */ |
|---|
| 135 | 117 | return 1; |
|---|
| 136 | 118 | } |
|---|
| 119 | + |
|---|
| 120 | +static int socrates_attach_chip(struct nand_chip *chip) |
|---|
| 121 | +{ |
|---|
| 122 | + if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT && |
|---|
| 123 | + chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN) |
|---|
| 124 | + chip->ecc.algo = NAND_ECC_ALGO_HAMMING; |
|---|
| 125 | + |
|---|
| 126 | + return 0; |
|---|
| 127 | +} |
|---|
| 128 | + |
|---|
| 129 | +static const struct nand_controller_ops socrates_ops = { |
|---|
| 130 | + .attach_chip = socrates_attach_chip, |
|---|
| 131 | +}; |
|---|
| 137 | 132 | |
|---|
| 138 | 133 | /* |
|---|
| 139 | 134 | * Probe for the NAND device. |
|---|
| .. | .. |
|---|
| 160 | 155 | mtd = nand_to_mtd(nand_chip); |
|---|
| 161 | 156 | host->dev = &ofdev->dev; |
|---|
| 162 | 157 | |
|---|
| 158 | + nand_controller_init(&host->controller); |
|---|
| 159 | + host->controller.ops = &socrates_ops; |
|---|
| 160 | + nand_chip->controller = &host->controller; |
|---|
| 161 | + |
|---|
| 163 | 162 | /* link the private data structures */ |
|---|
| 164 | 163 | nand_set_controller_data(nand_chip, host); |
|---|
| 165 | 164 | nand_set_flash_node(nand_chip, ofdev->dev.of_node); |
|---|
| 166 | 165 | mtd->name = "socrates_nand"; |
|---|
| 167 | 166 | mtd->dev.parent = &ofdev->dev; |
|---|
| 168 | 167 | |
|---|
| 169 | | - /*should never be accessed directly */ |
|---|
| 170 | | - nand_chip->IO_ADDR_R = (void *)0xdeadbeef; |
|---|
| 171 | | - nand_chip->IO_ADDR_W = (void *)0xdeadbeef; |
|---|
| 172 | | - |
|---|
| 173 | | - nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl; |
|---|
| 174 | | - nand_chip->read_byte = socrates_nand_read_byte; |
|---|
| 175 | | - nand_chip->read_word = socrates_nand_read_word; |
|---|
| 176 | | - nand_chip->write_buf = socrates_nand_write_buf; |
|---|
| 177 | | - nand_chip->read_buf = socrates_nand_read_buf; |
|---|
| 178 | | - nand_chip->dev_ready = socrates_nand_device_ready; |
|---|
| 179 | | - |
|---|
| 180 | | - nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */ |
|---|
| 181 | | - nand_chip->ecc.algo = NAND_ECC_HAMMING; |
|---|
| 168 | + nand_chip->legacy.cmd_ctrl = socrates_nand_cmd_ctrl; |
|---|
| 169 | + nand_chip->legacy.read_byte = socrates_nand_read_byte; |
|---|
| 170 | + nand_chip->legacy.write_buf = socrates_nand_write_buf; |
|---|
| 171 | + nand_chip->legacy.read_buf = socrates_nand_read_buf; |
|---|
| 172 | + nand_chip->legacy.dev_ready = socrates_nand_device_ready; |
|---|
| 182 | 173 | |
|---|
| 183 | 174 | /* TODO: I have no idea what real delay is. */ |
|---|
| 184 | | - nand_chip->chip_delay = 20; /* 20us command delay time */ |
|---|
| 175 | + nand_chip->legacy.chip_delay = 20; /* 20us command delay time */ |
|---|
| 176 | + |
|---|
| 177 | + /* |
|---|
| 178 | + * This driver assumes that the default ECC engine should be TYPE_SOFT. |
|---|
| 179 | + * Set ->engine_type before registering the NAND devices in order to |
|---|
| 180 | + * provide a driver specific default value. |
|---|
| 181 | + */ |
|---|
| 182 | + nand_chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT; |
|---|
| 185 | 183 | |
|---|
| 186 | 184 | dev_set_drvdata(&ofdev->dev, host); |
|---|
| 187 | 185 | |
|---|
| .. | .. |
|---|
| 206 | 204 | static int socrates_nand_remove(struct platform_device *ofdev) |
|---|
| 207 | 205 | { |
|---|
| 208 | 206 | struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev); |
|---|
| 207 | + struct nand_chip *chip = &host->nand_chip; |
|---|
| 208 | + int ret; |
|---|
| 209 | 209 | |
|---|
| 210 | | - nand_release(&host->nand_chip); |
|---|
| 210 | + ret = mtd_device_unregister(nand_to_mtd(chip)); |
|---|
| 211 | + WARN_ON(ret); |
|---|
| 212 | + nand_cleanup(chip); |
|---|
| 211 | 213 | |
|---|
| 212 | 214 | iounmap(host->io_base); |
|---|
| 213 | 215 | |
|---|