sunxi_nand.c revision 1.4 1 1.4 jmcneill /* $NetBSD: sunxi_nand.c,v 1.4 2017/11/13 17:37:02 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_nand.c,v 1.4 2017/11/13 17:37:02 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/fdt/fdtvar.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/nand/nand.h>
41 1.1 jmcneill #include <dev/nand/onfi.h>
42 1.1 jmcneill
43 1.4 jmcneill #include <arm/bootconfig.h>
44 1.4 jmcneill
45 1.1 jmcneill #define NDFC_CTL 0x00
46 1.1 jmcneill #define NDFC_CTL_CE_SEL __BITS(27,24)
47 1.1 jmcneill #define NDFC_CTL_PAGE_SIZE __BITS(11,8)
48 1.1 jmcneill #define NDFC_CTL_RB_SEL __BITS(4,3)
49 1.1 jmcneill #define NDFC_CTL_BUS_WIDTH __BIT(2)
50 1.1 jmcneill #define NDFC_CTL_RESET __BIT(1)
51 1.1 jmcneill #define NDFC_CTL_EN __BIT(0)
52 1.1 jmcneill #define NDFC_ST 0x04
53 1.1 jmcneill #define NDFC_ST_RB_STATE(n) __BIT(8 + (n))
54 1.1 jmcneill #define NDFC_ST_CMD_FIFO_STATUS __BIT(3)
55 1.1 jmcneill #define NDFC_ST_CMD_INT_FLAG __BIT(1)
56 1.1 jmcneill #define NDFC_INT 0x08
57 1.1 jmcneill #define NDFC_TIMING_CTL 0x0c
58 1.1 jmcneill #define NDFC_TIMING_CFG 0x10
59 1.1 jmcneill #define NDFC_ADDR_LOW 0x14
60 1.1 jmcneill #define NDFC_ADDR_HIGH 0x18
61 1.1 jmcneill #define NDFC_BLOCK_NUM 0x1c
62 1.1 jmcneill #define NDFC_CNT 0x20
63 1.1 jmcneill #define NDFC_CNT_DATA_CNT __BITS(9,0)
64 1.1 jmcneill #define NDFC_CMD 0x24
65 1.1 jmcneill #define NDFC_CMD_DATA_METHOD __BIT(26)
66 1.1 jmcneill #define NDFC_CMD_SEND_FIRST_CMD __BIT(22)
67 1.1 jmcneill #define NDFC_CMD_DATA_TRANS __BIT(21)
68 1.1 jmcneill #define NDFC_CMD_ACCESS_DIR __BIT(20)
69 1.1 jmcneill #define NDFC_CMD_SEND_ADR __BIT(19)
70 1.1 jmcneill #define NDFC_CMD_ADR_NUM __BITS(18,16)
71 1.1 jmcneill #define NDFC_RCMD_SET 0x28
72 1.1 jmcneill #define NDFC_WCMD_SET 0x2c
73 1.1 jmcneill #define NDFC_IO_DATA 0x30
74 1.1 jmcneill #define NDFC_ECC_CTL 0x34
75 1.1 jmcneill #define NDFC_ECC_ST 0x38
76 1.1 jmcneill #define NDFC_EFR 0x3c
77 1.1 jmcneill #define NDFC_ERR_CNT0 0x40
78 1.1 jmcneill #define NDFC_ERR_CNT1 0x44
79 1.1 jmcneill #define NDFC_USER_DATA(n) (0x50 + 4 * (n))
80 1.1 jmcneill #define NDFC_EFNAND_STA 0x90
81 1.1 jmcneill #define NDFC_SPARE_AREA 0xa0
82 1.1 jmcneill #define NDFC_PAT_ID 0xa4
83 1.1 jmcneill #define NDFC_RDATA_STA_CTL 0xa8
84 1.1 jmcneill #define NDFC_RDATA_STA_0 0xac
85 1.1 jmcneill #define NDFC_RDATA_STA_1 0xb0
86 1.1 jmcneill #define NDFC_MDMA_ADDR 0xc0
87 1.1 jmcneill #define NDFC_MDMA_CNT 0xc4
88 1.1 jmcneill #define NDFC_RAM0_BASE 0x400
89 1.1 jmcneill #define NDFC_RAM1_BASE 0x800
90 1.1 jmcneill
91 1.1 jmcneill #define NDFC_RAM_SIZE 1024
92 1.1 jmcneill
93 1.1 jmcneill static const char * compatible[] = {
94 1.1 jmcneill "allwinner,sun4i-a10-nand",
95 1.1 jmcneill NULL
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill struct sunxi_nand_softc;
99 1.1 jmcneill
100 1.1 jmcneill enum sunxi_nand_eccmode {
101 1.1 jmcneill ECC_MODE_UNKNOWN,
102 1.1 jmcneill ECC_MODE_HW,
103 1.1 jmcneill ECC_MODE_HW_SYNDROME,
104 1.1 jmcneill ECC_MODE_SOFT,
105 1.1 jmcneill ECC_MODE_SOFT_BCH,
106 1.1 jmcneill ECC_MODE_NONE
107 1.1 jmcneill };
108 1.1 jmcneill
109 1.1 jmcneill struct sunxi_nand_softc;
110 1.1 jmcneill
111 1.1 jmcneill struct sunxi_nand_chip {
112 1.1 jmcneill struct sunxi_nand_softc *chip_sc;
113 1.1 jmcneill int chip_phandle;
114 1.1 jmcneill device_t chip_dev;
115 1.1 jmcneill
116 1.1 jmcneill u_int chip_cs;
117 1.1 jmcneill enum sunxi_nand_eccmode chip_eccmode;
118 1.1 jmcneill u_int chip_rb;
119 1.1 jmcneill struct fdtbus_gpio_pin *chip_rb_pin;
120 1.1 jmcneill
121 1.1 jmcneill struct nand_interface chip_nand;
122 1.1 jmcneill
123 1.1 jmcneill bool chip_addr_pending;
124 1.1 jmcneill u_int chip_addr_count;
125 1.1 jmcneill uint32_t chip_addr[2];
126 1.1 jmcneill };
127 1.1 jmcneill
128 1.1 jmcneill struct sunxi_nand_softc {
129 1.1 jmcneill device_t sc_dev;
130 1.1 jmcneill int sc_phandle;
131 1.1 jmcneill bus_space_tag_t sc_bst;
132 1.1 jmcneill bus_space_handle_t sc_bsh;
133 1.1 jmcneill
134 1.1 jmcneill struct clk *sc_clk_mod;
135 1.1 jmcneill struct clk *sc_clk_ahb;
136 1.1 jmcneill struct fdtbus_reset *sc_rst_ahb;
137 1.1 jmcneill
138 1.1 jmcneill struct sunxi_nand_chip sc_chip;
139 1.1 jmcneill };
140 1.1 jmcneill
141 1.1 jmcneill #define NAND_READ(sc, reg) \
142 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
143 1.1 jmcneill #define NAND_WRITE(sc, reg, val) \
144 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
145 1.1 jmcneill
146 1.1 jmcneill static int
147 1.1 jmcneill sunxi_nand_rb_state(struct sunxi_nand_softc *sc, struct sunxi_nand_chip *chip)
148 1.1 jmcneill {
149 1.1 jmcneill if (chip->chip_rb_pin != NULL)
150 1.1 jmcneill return fdtbus_gpio_read(chip->chip_rb_pin);
151 1.1 jmcneill
152 1.1 jmcneill const uint32_t status = NAND_READ(sc, NDFC_ST);
153 1.1 jmcneill return __SHIFTOUT(status, NDFC_ST_RB_STATE(chip->chip_rb));
154 1.1 jmcneill }
155 1.1 jmcneill
156 1.1 jmcneill static int
157 1.1 jmcneill sunxi_nand_wait_status(struct sunxi_nand_softc *sc, uint32_t mask, uint32_t val)
158 1.1 jmcneill {
159 1.1 jmcneill uint32_t status;
160 1.1 jmcneill int retry;
161 1.1 jmcneill
162 1.1 jmcneill for (retry = 1000000; retry > 0; retry--) {
163 1.1 jmcneill status = NAND_READ(sc, NDFC_ST);
164 1.1 jmcneill if ((status & mask) == val)
165 1.1 jmcneill break;
166 1.1 jmcneill delay(1);
167 1.1 jmcneill }
168 1.1 jmcneill if (retry == 0) {
169 1.1 jmcneill #ifdef SUNXI_NAND_DEBUG
170 1.1 jmcneill device_printf(sc->sc_dev,
171 1.1 jmcneill "device timeout; status=%x mask=%x val=%x\n",
172 1.1 jmcneill status, mask, val);
173 1.1 jmcneill #endif
174 1.1 jmcneill return ETIMEDOUT;
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.1 jmcneill if (mask == NDFC_ST_CMD_INT_FLAG)
178 1.1 jmcneill NAND_WRITE(sc, NDFC_ST, NDFC_ST_CMD_INT_FLAG);
179 1.1 jmcneill
180 1.1 jmcneill return 0;
181 1.1 jmcneill }
182 1.1 jmcneill
183 1.1 jmcneill static void
184 1.1 jmcneill sunxi_nand_send_address(struct sunxi_nand_softc *sc,
185 1.1 jmcneill struct sunxi_nand_chip *chip)
186 1.1 jmcneill {
187 1.1 jmcneill if (chip->chip_addr_count == 0)
188 1.1 jmcneill return;
189 1.1 jmcneill
190 1.1 jmcneill /* Wait for space in the command FIFO */
191 1.1 jmcneill sunxi_nand_wait_status(sc, NDFC_ST_CMD_FIFO_STATUS, 0);
192 1.1 jmcneill
193 1.1 jmcneill NAND_WRITE(sc, NDFC_ADDR_LOW, chip->chip_addr[0]);
194 1.1 jmcneill NAND_WRITE(sc, NDFC_ADDR_HIGH, chip->chip_addr[1]);
195 1.1 jmcneill NAND_WRITE(sc, NDFC_CMD,
196 1.1 jmcneill NDFC_CMD_SEND_ADR |
197 1.1 jmcneill __SHIFTIN(chip->chip_addr_count - 1, NDFC_CMD_ADR_NUM));
198 1.1 jmcneill
199 1.1 jmcneill /* Wait for the command to finish */
200 1.1 jmcneill sunxi_nand_wait_status(sc, NDFC_ST_CMD_INT_FLAG, NDFC_ST_CMD_INT_FLAG);
201 1.1 jmcneill
202 1.1 jmcneill chip->chip_addr[0] = 0;
203 1.1 jmcneill chip->chip_addr[1] = 0;
204 1.1 jmcneill chip->chip_addr_count = 0;
205 1.1 jmcneill }
206 1.1 jmcneill
207 1.1 jmcneill static int
208 1.1 jmcneill sunxi_nand_read_buf_n(device_t dev, void *data, size_t len, int n)
209 1.1 jmcneill {
210 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(dev);
211 1.1 jmcneill struct sunxi_nand_chip * const chip = &sc->sc_chip;
212 1.1 jmcneill uint8_t *xfer_buf = data;
213 1.1 jmcneill size_t resid = len;
214 1.1 jmcneill int error;
215 1.1 jmcneill
216 1.1 jmcneill KASSERT(n == 1 || n == 2);
217 1.1 jmcneill KASSERT(len % n == 0);
218 1.1 jmcneill
219 1.1 jmcneill sunxi_nand_send_address(sc, chip);
220 1.1 jmcneill
221 1.1 jmcneill while (resid > 0) {
222 1.1 jmcneill const size_t xfer = min(resid, NDFC_RAM_SIZE);
223 1.1 jmcneill
224 1.1 jmcneill /* Wait for space in the command FIFO */
225 1.1 jmcneill error = sunxi_nand_wait_status(sc, NDFC_ST_CMD_FIFO_STATUS, 0);
226 1.1 jmcneill if (error != 0)
227 1.1 jmcneill return error;
228 1.1 jmcneill
229 1.1 jmcneill /* Start the transfer. */
230 1.1 jmcneill NAND_WRITE(sc, NDFC_CNT, xfer);
231 1.1 jmcneill NAND_WRITE(sc, NDFC_CMD,
232 1.1 jmcneill NDFC_CMD_DATA_TRANS | NDFC_CMD_DATA_METHOD);
233 1.1 jmcneill
234 1.1 jmcneill /* Wait for the command to finish */
235 1.1 jmcneill error = sunxi_nand_wait_status(sc, NDFC_ST_CMD_INT_FLAG,
236 1.1 jmcneill NDFC_ST_CMD_INT_FLAG);
237 1.1 jmcneill if (error != 0)
238 1.1 jmcneill return error;
239 1.1 jmcneill
240 1.1 jmcneill /* Transfer data from FIFO RAM to buffer */
241 1.1 jmcneill const int count = xfer / n;
242 1.1 jmcneill if (n == 1) {
243 1.1 jmcneill bus_space_read_region_1(sc->sc_bst, sc->sc_bsh,
244 1.1 jmcneill NDFC_RAM0_BASE, (uint8_t *)xfer_buf, count);
245 1.1 jmcneill } else if (n == 2) {
246 1.1 jmcneill bus_space_read_region_2(sc->sc_bst, sc->sc_bsh,
247 1.1 jmcneill NDFC_RAM0_BASE, (uint16_t *)xfer_buf, count);
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.1 jmcneill resid -= xfer;
251 1.1 jmcneill xfer_buf += xfer;
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill return 0;
255 1.1 jmcneill }
256 1.1 jmcneill
257 1.1 jmcneill static int
258 1.1 jmcneill sunxi_nand_write_buf_n(device_t dev, const void *data, size_t len, int n)
259 1.1 jmcneill {
260 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(dev);
261 1.1 jmcneill struct sunxi_nand_chip * const chip = &sc->sc_chip;
262 1.1 jmcneill const uint8_t *xfer_buf = data;
263 1.1 jmcneill size_t resid = len;
264 1.1 jmcneill int error;
265 1.1 jmcneill
266 1.1 jmcneill KASSERT(n == 1 || n == 2);
267 1.1 jmcneill KASSERT(len % n == 0);
268 1.1 jmcneill
269 1.1 jmcneill sunxi_nand_send_address(sc, chip);
270 1.1 jmcneill
271 1.1 jmcneill while (resid > 0) {
272 1.1 jmcneill const size_t xfer = min(resid, NDFC_RAM_SIZE);
273 1.1 jmcneill
274 1.1 jmcneill /* Wait for space in the command FIFO */
275 1.1 jmcneill error = sunxi_nand_wait_status(sc, NDFC_ST_CMD_FIFO_STATUS, 0);
276 1.1 jmcneill if (error != 0)
277 1.1 jmcneill return error;
278 1.1 jmcneill
279 1.1 jmcneill /* Transfer data from buffer to FIFO RAM */
280 1.1 jmcneill const int count = xfer / n;
281 1.1 jmcneill if (n == 1) {
282 1.1 jmcneill bus_space_write_region_1(sc->sc_bst, sc->sc_bsh,
283 1.1 jmcneill NDFC_RAM0_BASE, (const uint8_t *)xfer_buf, count);
284 1.1 jmcneill } else if (n == 2) {
285 1.1 jmcneill bus_space_write_region_2(sc->sc_bst, sc->sc_bsh,
286 1.1 jmcneill NDFC_RAM0_BASE, (const uint16_t *)xfer_buf, count);
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill /* Start the transfer. */
290 1.1 jmcneill NAND_WRITE(sc, NDFC_CNT, xfer);
291 1.1 jmcneill NAND_WRITE(sc, NDFC_CMD,
292 1.1 jmcneill NDFC_CMD_DATA_TRANS | NDFC_CMD_DATA_METHOD |
293 1.1 jmcneill NDFC_CMD_ACCESS_DIR);
294 1.1 jmcneill
295 1.1 jmcneill /* Wait for the command to finish */
296 1.1 jmcneill error = sunxi_nand_wait_status(sc, NDFC_ST_CMD_INT_FLAG,
297 1.1 jmcneill NDFC_ST_CMD_INT_FLAG);
298 1.1 jmcneill if (error != 0)
299 1.1 jmcneill return error;
300 1.1 jmcneill
301 1.1 jmcneill resid -= xfer;
302 1.1 jmcneill xfer_buf += xfer;
303 1.1 jmcneill }
304 1.1 jmcneill
305 1.1 jmcneill return 0;
306 1.1 jmcneill }
307 1.1 jmcneill
308 1.1 jmcneill /*
309 1.1 jmcneill * NAND interface
310 1.1 jmcneill */
311 1.1 jmcneill
312 1.1 jmcneill static void
313 1.1 jmcneill sunxi_nand_select(device_t dev, bool enable)
314 1.1 jmcneill {
315 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(dev);
316 1.1 jmcneill struct sunxi_nand_chip * const chip = &sc->sc_chip;
317 1.1 jmcneill struct nand_softc * const nand_sc = device_private(chip->chip_dev);
318 1.1 jmcneill struct nand_chip * const nc = nand_sc ? &nand_sc->sc_chip : NULL;
319 1.1 jmcneill uint32_t ctl;
320 1.1 jmcneill
321 1.1 jmcneill ctl = NAND_READ(sc, NDFC_CTL);
322 1.1 jmcneill if (enable) {
323 1.1 jmcneill ctl &= ~NDFC_CTL_CE_SEL;
324 1.1 jmcneill ctl |= __SHIFTIN(chip->chip_cs, NDFC_CTL_CE_SEL);
325 1.1 jmcneill ctl &= ~NDFC_CTL_RB_SEL;
326 1.1 jmcneill ctl |= __SHIFTIN(chip->chip_rb, NDFC_CTL_RB_SEL);
327 1.1 jmcneill ctl &= ~NDFC_CTL_PAGE_SIZE;
328 1.1 jmcneill ctl &= ~NDFC_CTL_BUS_WIDTH;
329 1.1 jmcneill ctl |= NDFC_CTL_EN;
330 1.1 jmcneill
331 1.1 jmcneill if (nc) {
332 1.1 jmcneill ctl |= __SHIFTIN(__builtin_ffs(nc->nc_page_size) - 11,
333 1.1 jmcneill NDFC_CTL_PAGE_SIZE);
334 1.1 jmcneill if (ISSET(nc->nc_flags, NC_BUSWIDTH_16))
335 1.1 jmcneill ctl |= NDFC_CTL_BUS_WIDTH;
336 1.1 jmcneill
337 1.1 jmcneill NAND_WRITE(sc, NDFC_SPARE_AREA, nc->nc_page_size);
338 1.1 jmcneill }
339 1.1 jmcneill }
340 1.1 jmcneill NAND_WRITE(sc, NDFC_CTL, ctl);
341 1.1 jmcneill }
342 1.1 jmcneill
343 1.1 jmcneill static void
344 1.1 jmcneill sunxi_nand_command(device_t dev, uint8_t command)
345 1.1 jmcneill {
346 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(dev);
347 1.1 jmcneill struct sunxi_nand_chip * const chip = &sc->sc_chip;
348 1.1 jmcneill
349 1.1 jmcneill sunxi_nand_send_address(sc, chip);
350 1.1 jmcneill
351 1.1 jmcneill /* Wait for space in the command FIFO */
352 1.1 jmcneill sunxi_nand_wait_status(sc, NDFC_ST_CMD_FIFO_STATUS, 0);
353 1.1 jmcneill
354 1.1 jmcneill NAND_WRITE(sc, NDFC_CMD, NDFC_CMD_SEND_FIRST_CMD | command);
355 1.1 jmcneill
356 1.1 jmcneill /* Wait for the command to finish */
357 1.1 jmcneill sunxi_nand_wait_status(sc, NDFC_ST_CMD_INT_FLAG, NDFC_ST_CMD_INT_FLAG);
358 1.1 jmcneill }
359 1.1 jmcneill
360 1.1 jmcneill static void
361 1.1 jmcneill sunxi_nand_address(device_t dev, uint8_t address)
362 1.1 jmcneill {
363 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(dev);
364 1.1 jmcneill struct sunxi_nand_chip * const chip = &sc->sc_chip;
365 1.1 jmcneill const u_int index = chip->chip_addr_count / 4;
366 1.1 jmcneill const u_int shift = (chip->chip_addr_count & 3) * 8;
367 1.1 jmcneill
368 1.1 jmcneill KASSERT(index < 2);
369 1.1 jmcneill
370 1.1 jmcneill chip->chip_addr[index] |= ((uint32_t)address << shift);
371 1.1 jmcneill chip->chip_addr_count++;
372 1.1 jmcneill }
373 1.1 jmcneill
374 1.1 jmcneill static void
375 1.1 jmcneill sunxi_nand_busy(device_t dev)
376 1.1 jmcneill {
377 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(dev);
378 1.1 jmcneill struct sunxi_nand_chip * const chip = &sc->sc_chip;
379 1.1 jmcneill
380 1.1 jmcneill while (!sunxi_nand_rb_state(sc, chip))
381 1.1 jmcneill delay(1);
382 1.1 jmcneill }
383 1.1 jmcneill
384 1.1 jmcneill static void
385 1.1 jmcneill sunxi_nand_read_1(device_t dev, uint8_t *data)
386 1.1 jmcneill {
387 1.1 jmcneill sunxi_nand_read_buf_n(dev, data, 1, 1);
388 1.1 jmcneill }
389 1.1 jmcneill
390 1.1 jmcneill static void
391 1.1 jmcneill sunxi_nand_read_2(device_t dev, uint16_t *data)
392 1.1 jmcneill {
393 1.1 jmcneill sunxi_nand_read_buf_n(dev, data, 2, 2);
394 1.1 jmcneill }
395 1.1 jmcneill
396 1.1 jmcneill static void
397 1.1 jmcneill sunxi_nand_read_buf_1(device_t dev, void *data, size_t len)
398 1.1 jmcneill {
399 1.1 jmcneill sunxi_nand_read_buf_n(dev, data, len, 1);
400 1.1 jmcneill }
401 1.1 jmcneill
402 1.1 jmcneill static void
403 1.1 jmcneill sunxi_nand_read_buf_2(device_t dev, void *data, size_t len)
404 1.1 jmcneill {
405 1.1 jmcneill sunxi_nand_read_buf_n(dev, data, len, 2);
406 1.1 jmcneill }
407 1.1 jmcneill
408 1.1 jmcneill static void
409 1.1 jmcneill sunxi_nand_write_1(device_t dev, uint8_t data)
410 1.1 jmcneill {
411 1.1 jmcneill sunxi_nand_write_buf_n(dev, &data, 1, 1);
412 1.1 jmcneill }
413 1.1 jmcneill
414 1.1 jmcneill static void
415 1.1 jmcneill sunxi_nand_write_2(device_t dev, uint16_t data)
416 1.1 jmcneill {
417 1.1 jmcneill sunxi_nand_write_buf_n(dev, &data, 2, 2);
418 1.1 jmcneill }
419 1.1 jmcneill
420 1.1 jmcneill static void
421 1.1 jmcneill sunxi_nand_write_buf_1(device_t dev, const void *data, size_t len)
422 1.1 jmcneill {
423 1.1 jmcneill sunxi_nand_write_buf_n(dev, data, len, 1);
424 1.1 jmcneill }
425 1.1 jmcneill
426 1.1 jmcneill static void
427 1.1 jmcneill sunxi_nand_write_buf_2(device_t dev, const void *data, size_t len)
428 1.1 jmcneill {
429 1.1 jmcneill sunxi_nand_write_buf_n(dev, data, len, 2);
430 1.1 jmcneill }
431 1.1 jmcneill
432 1.1 jmcneill static void
433 1.1 jmcneill sunxi_nand_attach_chip(struct sunxi_nand_softc *sc,
434 1.1 jmcneill struct sunxi_nand_chip *chip, int phandle)
435 1.1 jmcneill {
436 1.1 jmcneill struct nand_interface *nand = &chip->chip_nand;
437 1.4 jmcneill const char *ecc_mode, *mtdparts;
438 1.1 jmcneill
439 1.1 jmcneill chip->chip_sc = sc;
440 1.1 jmcneill chip->chip_phandle = phandle;
441 1.1 jmcneill
442 1.1 jmcneill if (of_getprop_uint32(phandle, "reg", &chip->chip_cs) != 0) {
443 1.1 jmcneill aprint_error_dev(sc->sc_dev,
444 1.1 jmcneill "missing 'reg' property on chip node\n");
445 1.1 jmcneill return;
446 1.1 jmcneill }
447 1.1 jmcneill
448 1.1 jmcneill if (of_getprop_uint32(phandle, "allwinner,rb", &chip->chip_rb) != 0) {
449 1.1 jmcneill aprint_error_dev(sc->sc_dev,
450 1.1 jmcneill "chip #%u: missing 'allwinner,rb' property\n",
451 1.1 jmcneill chip->chip_cs);
452 1.1 jmcneill return;
453 1.1 jmcneill }
454 1.1 jmcneill
455 1.1 jmcneill ecc_mode = fdtbus_get_string(phandle, "nand-ecc-mode");
456 1.1 jmcneill if (ecc_mode == NULL)
457 1.1 jmcneill ecc_mode = "none";
458 1.1 jmcneill
459 1.1 jmcneill if (strcmp(ecc_mode, "none") == 0)
460 1.1 jmcneill chip->chip_eccmode = ECC_MODE_NONE;
461 1.1 jmcneill else if (strcmp(ecc_mode, "hw") == 0)
462 1.1 jmcneill chip->chip_eccmode = ECC_MODE_HW;
463 1.1 jmcneill else if (strcmp(ecc_mode, "hw_syndrome") == 0)
464 1.1 jmcneill chip->chip_eccmode = ECC_MODE_HW_SYNDROME;
465 1.1 jmcneill else if (strcmp(ecc_mode, "soft") == 0)
466 1.1 jmcneill chip->chip_eccmode = ECC_MODE_SOFT;
467 1.1 jmcneill else if (strcmp(ecc_mode, "soft_bch") == 0)
468 1.1 jmcneill chip->chip_eccmode = ECC_MODE_SOFT_BCH;
469 1.1 jmcneill else
470 1.1 jmcneill chip->chip_eccmode = ECC_MODE_UNKNOWN;
471 1.1 jmcneill
472 1.1 jmcneill /* Only HW mode is supported for now */
473 1.1 jmcneill switch (chip->chip_eccmode) {
474 1.1 jmcneill case ECC_MODE_HW:
475 1.1 jmcneill break;
476 1.1 jmcneill default:
477 1.1 jmcneill return;
478 1.1 jmcneill }
479 1.1 jmcneill
480 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "chip #%u: RB %u, ECC mode '%s'\n",
481 1.1 jmcneill chip->chip_cs, chip->chip_rb, ecc_mode);
482 1.1 jmcneill
483 1.1 jmcneill nand_init_interface(nand);
484 1.1 jmcneill nand->select = sunxi_nand_select;
485 1.1 jmcneill nand->command = sunxi_nand_command;
486 1.1 jmcneill nand->address = sunxi_nand_address;
487 1.1 jmcneill nand->read_buf_1 = sunxi_nand_read_buf_1;
488 1.1 jmcneill nand->read_buf_2 = sunxi_nand_read_buf_2;
489 1.1 jmcneill nand->read_1 = sunxi_nand_read_1;
490 1.1 jmcneill nand->read_2 = sunxi_nand_read_2;
491 1.1 jmcneill nand->write_buf_1 = sunxi_nand_write_buf_1;
492 1.1 jmcneill nand->write_buf_2 = sunxi_nand_write_buf_2;
493 1.1 jmcneill nand->write_1 = sunxi_nand_write_1;
494 1.1 jmcneill nand->write_2 = sunxi_nand_write_2;
495 1.1 jmcneill nand->busy = sunxi_nand_busy;
496 1.1 jmcneill
497 1.1 jmcneill #if notyet
498 1.1 jmcneill switch (chip->chip_eccmode) {
499 1.1 jmcneill case ECC_MODE_HW:
500 1.1 jmcneill nand->ecc_compute = sunxi_nand_ecc_compute;
501 1.1 jmcneill nand->ecc_correct = sunxi_nand_ecc_correct;
502 1.1 jmcneill nand->ecc_prepare = sunxi_nand_ecc_prepare;
503 1.1 jmcneill nand->ecc.necc_code_size = 3;
504 1.1 jmcneill nand->ecc.necc_block_size = 512;
505 1.1 jmcneill nand->ecc.necc_type = NAND_ECC_TYPE_HW;
506 1.1 jmcneill break;
507 1.1 jmcneill default:
508 1.1 jmcneill aprint_error_dev(sc->sc_dev,
509 1.1 jmcneill "chip #%u: ECC mode not supported by driver\n",
510 1.1 jmcneill chip->chip_cs);
511 1.1 jmcneill return;
512 1.1 jmcneill }
513 1.1 jmcneill #else
514 1.1 jmcneill nand->ecc.necc_code_size = 3;
515 1.3 jmcneill nand->ecc.necc_block_size = 256;
516 1.1 jmcneill #endif
517 1.1 jmcneill
518 1.1 jmcneill chip->chip_dev = nand_attach_mi(nand, sc->sc_dev);
519 1.4 jmcneill if (chip->chip_dev == NULL)
520 1.4 jmcneill return;
521 1.4 jmcneill
522 1.4 jmcneill mtdparts = get_bootconf_string(boot_args, "mtdparts");
523 1.4 jmcneill if (mtdparts != NULL) {
524 1.4 jmcneill char mtd_id[strlen("sunxi-nand.X") + 1];
525 1.4 jmcneill snprintf(mtd_id, sizeof(mtd_id), "sunxi-nand.%u",
526 1.4 jmcneill device_unit(sc->sc_dev));
527 1.4 jmcneill
528 1.4 jmcneill nand_attach_mtdparts(chip->chip_dev, mtd_id, mtdparts);
529 1.4 jmcneill }
530 1.1 jmcneill }
531 1.1 jmcneill
532 1.1 jmcneill static int
533 1.1 jmcneill sunxi_nand_init_resources(struct sunxi_nand_softc *sc)
534 1.1 jmcneill {
535 1.1 jmcneill int error;
536 1.1 jmcneill
537 1.1 jmcneill /* Both "mod" and "ahb" clocks are required */
538 1.1 jmcneill sc->sc_clk_mod = fdtbus_clock_get(sc->sc_phandle, "mod");
539 1.1 jmcneill sc->sc_clk_ahb = fdtbus_clock_get(sc->sc_phandle, "ahb");
540 1.1 jmcneill if (sc->sc_clk_mod == NULL || sc->sc_clk_ahb == NULL)
541 1.1 jmcneill return ENXIO;
542 1.1 jmcneill
543 1.1 jmcneill if ((error = clk_enable(sc->sc_clk_ahb)) != 0)
544 1.1 jmcneill return error;
545 1.1 jmcneill if ((error = clk_enable(sc->sc_clk_mod)) != 0)
546 1.1 jmcneill return error;
547 1.1 jmcneill
548 1.1 jmcneill /* Reset is optional */
549 1.1 jmcneill sc->sc_rst_ahb = fdtbus_reset_get(sc->sc_phandle, "ahb");
550 1.1 jmcneill if (sc->sc_rst_ahb != NULL) {
551 1.1 jmcneill if ((error = fdtbus_reset_deassert(sc->sc_rst_ahb)) != 0)
552 1.1 jmcneill return error;
553 1.1 jmcneill }
554 1.1 jmcneill
555 1.1 jmcneill return 0;
556 1.1 jmcneill }
557 1.1 jmcneill
558 1.1 jmcneill static int
559 1.1 jmcneill sunxi_nand_match(device_t parent, cfdata_t cf, void *aux)
560 1.1 jmcneill {
561 1.1 jmcneill struct fdt_attach_args * const faa = aux;
562 1.1 jmcneill
563 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
564 1.1 jmcneill }
565 1.1 jmcneill
566 1.1 jmcneill static void
567 1.1 jmcneill sunxi_nand_attach(device_t parent, device_t self, void *aux)
568 1.1 jmcneill {
569 1.1 jmcneill struct sunxi_nand_softc * const sc = device_private(self);
570 1.1 jmcneill struct fdt_attach_args * const faa = aux;
571 1.1 jmcneill const int phandle = faa->faa_phandle;
572 1.1 jmcneill bus_addr_t addr;
573 1.1 jmcneill bus_size_t size;
574 1.1 jmcneill int child;
575 1.1 jmcneill
576 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
577 1.1 jmcneill aprint_error(": couldn't get registers\n");
578 1.1 jmcneill return;
579 1.1 jmcneill }
580 1.1 jmcneill
581 1.1 jmcneill sc->sc_dev = self;
582 1.1 jmcneill sc->sc_phandle = phandle;
583 1.1 jmcneill sc->sc_bst = faa->faa_bst;
584 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
585 1.1 jmcneill aprint_error(": couldn't map registers\n");
586 1.1 jmcneill return;
587 1.1 jmcneill }
588 1.1 jmcneill
589 1.1 jmcneill aprint_naive("\n");
590 1.1 jmcneill aprint_normal(": NAND Flash Controller\n");
591 1.1 jmcneill
592 1.1 jmcneill if (sunxi_nand_init_resources(sc) != 0) {
593 1.1 jmcneill aprint_error_dev(self, "couldn't initialize resources\n");
594 1.1 jmcneill return;
595 1.1 jmcneill }
596 1.1 jmcneill
597 1.1 jmcneill /* DT bindings allow for multiple chips but we only use the first */
598 1.1 jmcneill child = OF_child(phandle);
599 1.1 jmcneill if (!child)
600 1.1 jmcneill return;
601 1.1 jmcneill
602 1.1 jmcneill sunxi_nand_attach_chip(sc, &sc->sc_chip, child);
603 1.1 jmcneill }
604 1.1 jmcneill
605 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_nand, sizeof(struct sunxi_nand_softc),
606 1.1 jmcneill sunxi_nand_match, sunxi_nand_attach, NULL, NULL);
607 1.1 jmcneill
608