1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Freescale GPMI NAND Flash Driver
4  *
5  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
6  * Copyright (C) 2008 Embedded Alley Solutions, Inc.
7  */
8 #ifndef __DRIVERS_MTD_NAND_GPMI_NAND_H
9 #define __DRIVERS_MTD_NAND_GPMI_NAND_H
10 
11 #include <linux/mtd/rawnand.h>
12 #include <linux/platform_device.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmaengine.h>
15 
16 #define GPMI_CLK_MAX 5 /* MX6Q needs five clocks */
17 struct resources {
18 	void __iomem  *gpmi_regs;
19 	void __iomem  *bch_regs;
20 	unsigned int  dma_low_channel;
21 	unsigned int  dma_high_channel;
22 	struct clk    *clock[GPMI_CLK_MAX];
23 };
24 
25 /**
26  * struct bch_geometry - BCH geometry description.
27  * @gf_len:                   The length of Galois Field. (e.g., 13 or 14)
28  * @ecc_strength:             A number that describes the strength of the ECC
29  *                            algorithm.
30  * @page_size:                The size, in bytes, of a physical page, including
31  *                            both data and OOB.
32  * @metadata_size:            The size, in bytes, of the metadata.
33  * @ecc_chunk_size:           The size, in bytes, of a single ECC chunk. Note
34  *                            the first chunk in the page includes both data and
35  *                            metadata, so it's a bit larger than this value.
36  * @ecc_chunk_count:          The number of ECC chunks in the page,
37  * @payload_size:             The size, in bytes, of the payload buffer.
38  * @auxiliary_size:           The size, in bytes, of the auxiliary buffer.
39  * @auxiliary_status_offset:  The offset into the auxiliary buffer at which
40  *                            the ECC status appears.
41  * @block_mark_byte_offset:   The byte offset in the ECC-based page view at
42  *                            which the underlying physical block mark appears.
43  * @block_mark_bit_offset:    The bit offset into the ECC-based page view at
44  *                            which the underlying physical block mark appears.
45  */
46 struct bch_geometry {
47 	unsigned int  gf_len;
48 	unsigned int  ecc_strength;
49 	unsigned int  page_size;
50 	unsigned int  metadata_size;
51 	unsigned int  ecc_chunk_size;
52 	unsigned int  ecc_chunk_count;
53 	unsigned int  payload_size;
54 	unsigned int  auxiliary_size;
55 	unsigned int  auxiliary_status_offset;
56 	unsigned int  block_mark_byte_offset;
57 	unsigned int  block_mark_bit_offset;
58 };
59 
60 /**
61  * struct boot_rom_geometry - Boot ROM geometry description.
62  * @stride_size_in_pages:        The size of a boot block stride, in pages.
63  * @search_area_stride_exponent: The logarithm to base 2 of the size of a
64  *                               search area in boot block strides.
65  */
66 struct boot_rom_geometry {
67 	unsigned int  stride_size_in_pages;
68 	unsigned int  search_area_stride_exponent;
69 };
70 
71 enum gpmi_type {
72 	IS_MX23,
73 	IS_MX28,
74 	IS_MX6Q,
75 	IS_MX6SX,
76 	IS_MX7D,
77 };
78 
79 struct gpmi_devdata {
80 	enum gpmi_type type;
81 	int bch_max_ecc_strength;
82 	int max_chain_delay; /* See the async EDO mode */
83 	const char * const *clks;
84 	const int clks_count;
85 };
86 
87 /**
88  * struct gpmi_nfc_hardware_timing - GPMI hardware timing parameters.
89  * @must_apply_timings:        Whether controller timings have already been
90  *                             applied or not (useful only while there is
91  *                             support for only one chip select)
92  * @clk_rate:                  The clock rate that must be used to derive the
93  *                             following parameters
94  * @timing0:                   HW_GPMI_TIMING0 register
95  * @timing1:                   HW_GPMI_TIMING1 register
96  * @ctrl1n:                    HW_GPMI_CTRL1n register
97  */
98 struct gpmi_nfc_hardware_timing {
99 	bool must_apply_timings;
100 	unsigned long int clk_rate;
101 	u32 timing0;
102 	u32 timing1;
103 	u32 ctrl1n;
104 };
105 
106 struct gpmi_nand_data {
107 	/* Devdata */
108 	const struct gpmi_devdata *devdata;
109 
110 	/* System Interface */
111 	struct device		*dev;
112 	struct platform_device	*pdev;
113 
114 	/* Resources */
115 	struct resources	resources;
116 
117 	/* Flash Hardware */
118 	struct gpmi_nfc_hardware_timing hw;
119 
120 	/* BCH */
121 	struct bch_geometry	bch_geometry;
122 	struct completion	bch_done;
123 
124 	/* NAND Boot issue */
125 	bool			swap_block_mark;
126 	struct boot_rom_geometry rom_geometry;
127 
128 	/* MTD / NAND */
129 	struct nand_chip	nand;
130 
131 	/* General-use Variables */
132 	int			current_chip;
133 	unsigned int		command_length;
134 
135 	struct scatterlist	cmd_sgl;
136 	char			*cmd_buffer;
137 
138 	struct scatterlist	data_sgl;
139 	char			*data_buffer_dma;
140 
141 	void			*page_buffer_virt;
142 	dma_addr_t		page_buffer_phys;
143 	unsigned int		page_buffer_size;
144 
145 	void			*payload_virt;
146 	dma_addr_t		payload_phys;
147 
148 	void			*auxiliary_virt;
149 	dma_addr_t		auxiliary_phys;
150 
151 	void			*raw_buffer;
152 
153 	/* DMA channels */
154 #define DMA_CHANS		8
155 	struct dma_chan		*dma_chans[DMA_CHANS];
156 	struct completion	dma_done;
157 
158 	/* private */
159 	void			*private;
160 };
161 
162 /* Common Services */
163 int common_nfc_set_geometry(struct gpmi_nand_data *);
164 struct dma_chan *get_dma_chan(struct gpmi_nand_data *);
165 bool prepare_data_dma(struct gpmi_nand_data *, const void *buf, int len,
166 		      enum dma_data_direction dr);
167 int start_dma_without_bch_irq(struct gpmi_nand_data *,
168 			      struct dma_async_tx_descriptor *);
169 int start_dma_with_bch_irq(struct gpmi_nand_data *,
170 			   struct dma_async_tx_descriptor *);
171 
172 /* GPMI-NAND helper function library */
173 int gpmi_init(struct gpmi_nand_data *);
174 void gpmi_clear_bch(struct gpmi_nand_data *);
175 void gpmi_dump_info(struct gpmi_nand_data *);
176 int bch_set_geometry(struct gpmi_nand_data *);
177 int gpmi_is_ready(struct gpmi_nand_data *, unsigned chip);
178 int gpmi_send_command(struct gpmi_nand_data *);
179 int gpmi_enable_clk(struct gpmi_nand_data *this);
180 int gpmi_disable_clk(struct gpmi_nand_data *this);
181 int gpmi_setup_data_interface(struct mtd_info *mtd, int chipnr,
182 			      const struct nand_data_interface *conf);
183 void gpmi_nfc_apply_timings(struct gpmi_nand_data *this);
184 int gpmi_read_data(struct gpmi_nand_data *, void *buf, int len);
185 int gpmi_send_data(struct gpmi_nand_data *, const void *buf, int len);
186 
187 int gpmi_send_page(struct gpmi_nand_data *,
188 		   dma_addr_t payload, dma_addr_t auxiliary);
189 int gpmi_read_page(struct gpmi_nand_data *,
190 		   dma_addr_t payload, dma_addr_t auxiliary);
191 
192 void gpmi_copy_bits(u8 *dst, size_t dst_bit_off,
193 		    const u8 *src, size_t src_bit_off,
194 		    size_t nbits);
195 
196 /* BCH : Status Block Completion Codes */
197 #define STATUS_GOOD		0x00
198 #define STATUS_ERASED		0xff
199 #define STATUS_UNCORRECTABLE	0xfe
200 
201 /* Use the devdata to distinguish different Archs. */
202 #define GPMI_IS_MX23(x)		((x)->devdata->type == IS_MX23)
203 #define GPMI_IS_MX28(x)		((x)->devdata->type == IS_MX28)
204 #define GPMI_IS_MX6Q(x)		((x)->devdata->type == IS_MX6Q)
205 #define GPMI_IS_MX6SX(x)	((x)->devdata->type == IS_MX6SX)
206 #define GPMI_IS_MX7D(x)		((x)->devdata->type == IS_MX7D)
207 
208 #define GPMI_IS_MX6(x)		(GPMI_IS_MX6Q(x) || GPMI_IS_MX6SX(x) || \
209 				 GPMI_IS_MX7D(x))
210 #endif
211