blob: f63397be2d6faa0ed1fe2e8ba58653fd287d0f2f [file] [log] [blame]
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +05301/*
2 * Copyright (C) 2007 Google, Inc.
Sahitya Tummaladd8caf42017-10-11 10:12:19 +05303 * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +05304 *
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 and
7 * only version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#ifndef __QPIC_NAND_H
17#define __QPIC_NAND_H
18
19#define pr_fmt(fmt) "%s: " fmt, __func__
20
21#include <linux/clk.h>
22#include <linux/pm_runtime.h>
23#include <linux/slab.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/nand.h>
28#include <linux/mtd/partitions.h>
29#include <linux/platform_device.h>
30#include <linux/dma-mapping.h>
31#include <linux/io.h>
32#include <linux/crc16.h>
33#include <linux/bitrev.h>
34#include <linux/mutex.h>
35#include <linux/of.h>
36#include <linux/ctype.h>
37#include <linux/msm-sps.h>
38#include <linux/msm-bus.h>
Sahitya Tummaladd8caf42017-10-11 10:12:19 +053039#include <linux/spinlock.h>
40#include <linux/ktime.h>
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +053041#include <soc/qcom/smem.h>
42
43#define PAGE_SIZE_2K 2048
44#define PAGE_SIZE_4K 4096
45
46#undef WRITE /* To avoid redefinition in above header files */
47#undef READ /* To avoid redefinition in above header files */
48#define WRITE 1
49#define READ 0
50
51#define MSM_NAND_IDLE_TIMEOUT 200 /* msecs */
52#define MSM_NAND_BUS_VOTE_MAX_RATE 100000000 /* Hz */
53
54/*
55 * The maximum no of descriptors per transfer (page read/write) won't be more
56 * than 64. For more details on what those commands are, please refer to the
57 * page read and page write functions in the driver.
58 */
59#define SPS_MAX_DESC_NUM 64
60#define SPS_DATA_CONS_PIPE_INDEX 0
61#define SPS_DATA_PROD_PIPE_INDEX 1
62#define SPS_CMD_CONS_PIPE_INDEX 2
63
64#define msm_virt_to_dma(chip, vaddr) \
65 ((chip)->dma_phys_addr + \
66 ((uint8_t *)(vaddr) - (chip)->dma_virt_addr))
67
68/*
69 * A single page read/write request would typically need DMA memory of about
70 * 1K memory approximately. So for a single request this memory is more than
71 * enough.
72 *
73 * But to accommodate multiple clients we allocate 8K of memory. Though only
74 * one client request can be submitted to NANDc at any time, other clients can
75 * still prepare the descriptors while waiting for current client request to
76 * be done. Thus for a total memory of 8K, the driver can currently support
77 * maximum clients up to 7 or 8 at a time. The client for which there is no
78 * free DMA memory shall wait on the wait queue until other clients free up
79 * the required memory.
80 */
81#define MSM_NAND_DMA_BUFFER_SIZE SZ_8K
82/*
83 * This defines the granularity at which the buffer management is done. The
84 * total number of slots is based on the size of the atomic_t variable
85 * dma_buffer_busy(number of bits) within the structure msm_nand_chip.
86 */
87#define MSM_NAND_DMA_BUFFER_SLOT_SZ \
88 (MSM_NAND_DMA_BUFFER_SIZE / (sizeof(((atomic_t *)0)->counter) * 8))
89
90/* ONFI(Open NAND Flash Interface) parameters */
91#define MSM_NAND_CFG0_RAW_ONFI_IDENTIFIER 0x88000800
92#define MSM_NAND_CFG0_RAW_ONFI_PARAM_INFO 0x88040000
93#define MSM_NAND_CFG1_RAW_ONFI_IDENTIFIER 0x0005045d
94#define MSM_NAND_CFG1_RAW_ONFI_PARAM_INFO 0x0005045d
95#define ONFI_PARAM_INFO_LENGTH 0x0200
96#define ONFI_PARAM_PAGE_LENGTH 0x0100
97#define ONFI_PARAMETER_PAGE_SIGNATURE 0x49464E4F
98#define FLASH_READ_ONFI_SIGNATURE_ADDRESS 0x20
99#define FLASH_READ_ONFI_PARAMETERS_ADDRESS 0x00
100#define FLASH_READ_DEVICE_ID_ADDRESS 0x00
101
102#define MSM_NAND_RESET_FLASH_STS 0x00000020
103#define MSM_NAND_RESET_READ_STS 0x000000C0
104
105/* QPIC NANDc (NAND Controller) Register Set */
106#define MSM_NAND_REG(info, off) (info->nand_phys + off)
107#define MSM_NAND_REG_ADJUSTED(info, off) (info->nand_phys_adjusted + off)
108#define MSM_NAND_QPIC_VERSION(info) MSM_NAND_REG_ADJUSTED(info, 0x20100)
109#define MSM_NAND_FLASH_CMD(info) MSM_NAND_REG(info, 0x30000)
110#define MSM_NAND_ADDR0(info) MSM_NAND_REG(info, 0x30004)
111#define MSM_NAND_ADDR1(info) MSM_NAND_REG(info, 0x30008)
112#define MSM_NAND_EXEC_CMD(info) MSM_NAND_REG(info, 0x30010)
113#define MSM_NAND_FLASH_STATUS(info) MSM_NAND_REG(info, 0x30014)
114#define FS_OP_ERR (1 << 4)
115#define FS_MPU_ERR (1 << 8)
116#define FS_DEVICE_STS_ERR (1 << 16)
117#define FS_DEVICE_WP (1 << 23)
118
119#define MSM_NAND_BUFFER_STATUS(info) MSM_NAND_REG(info, 0x30018)
120#define BS_UNCORRECTABLE_BIT (1 << 8)
121#define BS_CORRECTABLE_ERR_MSK 0x1F
122
123#define MSM_NAND_DEV0_CFG0(info) MSM_NAND_REG(info, 0x30020)
124#define DISABLE_STATUS_AFTER_WRITE 4
125#define CW_PER_PAGE 6
126#define UD_SIZE_BYTES 9
127#define SPARE_SIZE_BYTES 23
128#define NUM_ADDR_CYCLES 27
129
130#define MSM_NAND_DEV0_CFG1(info) MSM_NAND_REG(info, 0x30024)
131#define DEV0_CFG1_ECC_DISABLE 0
132#define WIDE_FLASH 1
133#define NAND_RECOVERY_CYCLES 2
134#define CS_ACTIVE_BSY 5
135#define BAD_BLOCK_BYTE_NUM 6
136#define BAD_BLOCK_IN_SPARE_AREA 16
137#define WR_RD_BSY_GAP 17
138#define ENABLE_BCH_ECC 27
139
140#define BYTES_512 512
141#define BYTES_516 516
142#define BYTES_517 517
143
144#define MSM_NAND_DEV0_ECC_CFG(info) MSM_NAND_REG(info, 0x30028)
145#define ECC_CFG_ECC_DISABLE 0
146#define ECC_SW_RESET 1
147#define ECC_MODE 4
148#define ECC_PARITY_SIZE_BYTES 8
149#define ECC_NUM_DATA_BYTES 16
150#define ECC_FORCE_CLK_OPEN 30
151
152#define MSM_NAND_READ_ID(info) MSM_NAND_REG(info, 0x30040)
153#define MSM_NAND_READ_STATUS(info) MSM_NAND_REG(info, 0x30044)
154#define MSM_NAND_READ_ID2(info) MSM_NAND_REG(info, 0x30048)
155#define EXTENDED_FETCH_ID BIT(19)
156#define MSM_NAND_DEV_CMD1(info) MSM_NAND_REG(info, 0x300A4)
157#define MSM_NAND_DEV_CMD_VLD(info) MSM_NAND_REG(info, 0x300AC)
158#define MSM_NAND_EBI2_ECC_BUF_CFG(info) MSM_NAND_REG(info, 0x300F0)
159
160#define MSM_NAND_ERASED_CW_DETECT_CFG(info) MSM_NAND_REG(info, 0x300E8)
161#define ERASED_CW_ECC_MASK 1
162#define AUTO_DETECT_RES 0
163#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
164#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
165#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
166#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
167#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
168
169#define MSM_NAND_ERASED_CW_DETECT_STATUS(info) MSM_NAND_REG(info, 0x300EC)
170#define PAGE_ALL_ERASED 7
171#define CODEWORD_ALL_ERASED 6
172#define PAGE_ERASED 5
173#define CODEWORD_ERASED 4
174#define ERASED_PAGE ((1 << PAGE_ALL_ERASED) | (1 << PAGE_ERASED))
175#define ERASED_CW ((1 << CODEWORD_ALL_ERASED) | (1 << CODEWORD_ERASED))
176
177#define MSM_NAND_CTRL(info) MSM_NAND_REG(info, 0x30F00)
178#define BAM_MODE_EN 0
179#define MSM_NAND_VERSION(info) MSM_NAND_REG_ADJUSTED(info, 0x30F08)
180#define MSM_NAND_READ_LOCATION_0(info) MSM_NAND_REG(info, 0x30F20)
181#define MSM_NAND_READ_LOCATION_1(info) MSM_NAND_REG(info, 0x30F24)
182
183/* device commands */
184#define MSM_NAND_CMD_PAGE_READ 0x32
185#define MSM_NAND_CMD_PAGE_READ_ECC 0x33
186#define MSM_NAND_CMD_PAGE_READ_ALL 0x34
187#define MSM_NAND_CMD_PAGE_READ_ONFI 0x35
188#define MSM_NAND_CMD_PRG_PAGE 0x36
189#define MSM_NAND_CMD_PRG_PAGE_ECC 0x37
190#define MSM_NAND_CMD_PRG_PAGE_ALL 0x39
191#define MSM_NAND_CMD_BLOCK_ERASE 0x3A
192#define MSM_NAND_CMD_FETCH_ID 0x0B
193
194/* Version Mask */
195#define MSM_NAND_VERSION_MAJOR_MASK 0xF0000000
196#define MSM_NAND_VERSION_MAJOR_SHIFT 28
197#define MSM_NAND_VERSION_MINOR_MASK 0x0FFF0000
198#define MSM_NAND_VERSION_MINOR_SHIFT 16
199
200#define CMD SPS_IOVEC_FLAG_CMD
201#define CMD_LCK (CMD | SPS_IOVEC_FLAG_LOCK)
202#define INT SPS_IOVEC_FLAG_INT
203#define INT_UNLCK (INT | SPS_IOVEC_FLAG_UNLOCK)
204#define CMD_INT_UNLCK (CMD | INT_UNLCK)
205#define NWD SPS_IOVEC_FLAG_NWD
206
207/* Structure that defines a NAND SPS command element */
208struct msm_nand_sps_cmd {
209 struct sps_command_element ce;
210 uint32_t flags;
211};
212
213struct msm_nand_cmd_setup_desc {
214 struct sps_command_element ce[11];
215 uint32_t flags;
216 uint32_t num_ce;
217};
218
219struct msm_nand_cmd_cw_desc {
220 struct sps_command_element ce[3];
221 uint32_t flags;
222 uint32_t num_ce;
223};
224
225struct msm_nand_rw_cmd_desc {
226 uint32_t count;
227 struct msm_nand_cmd_setup_desc setup_desc;
228 struct msm_nand_cmd_cw_desc cw_desc[];
229};
230
231/*
232 * Structure that defines the NAND controller properties as per the
233 * NAND flash device/chip that is attached.
234 */
235struct msm_nand_chip {
236 struct device *dev;
237 /*
238 * DMA memory will be allocated only once during probe and this memory
239 * will be used by all NAND clients. This wait queue is needed to
240 * make the applications wait for DMA memory to be free'd when the
241 * complete memory is exhausted.
242 */
243 wait_queue_head_t dma_wait_queue;
244 atomic_t dma_buffer_busy;
245 uint8_t *dma_virt_addr;
246 dma_addr_t dma_phys_addr;
247 uint32_t ecc_parity_bytes;
248 uint32_t bch_caps; /* Controller BCH ECC capabilities */
249#define MSM_NAND_CAP_4_BIT_BCH (1 << 0)
250#define MSM_NAND_CAP_8_BIT_BCH (1 << 1)
251 uint32_t cw_size;
252 /* NANDc register configurations */
253 uint32_t cfg0, cfg1, cfg0_raw, cfg1_raw;
254 uint32_t ecc_buf_cfg;
255 uint32_t ecc_bch_cfg;
256 uint32_t ecc_cfg_raw;
257};
258
259/* Structure that defines an SPS end point for a NANDc BAM pipe. */
260struct msm_nand_sps_endpt {
261 struct sps_pipe *handle;
262 struct sps_connect config;
263 struct sps_register_event event;
264 struct completion completion;
265 uint32_t index;
266};
267
268/*
269 * Structure that defines NANDc SPS data - BAM handle and an end point
270 * for each BAM pipe.
271 */
272struct msm_nand_sps_info {
273 unsigned long bam_handle;
274 struct msm_nand_sps_endpt data_prod;
275 struct msm_nand_sps_endpt data_cons;
276 struct msm_nand_sps_endpt cmd_pipe;
277};
278
279/*
280 * Structure that contains flash device information. This gets updated after
281 * the NAND flash device detection.
282 */
283struct flash_identification {
284 uint32_t flash_id;
285 uint64_t density;
286 uint32_t widebus;
287 uint32_t pagesize;
288 uint32_t blksize;
289 uint32_t oobsize;
290 uint32_t ecc_correctability;
291 uint32_t ecc_capability; /* Set based on the ECC capability selected. */
292};
293
294struct msm_nand_clk_data {
295 struct clk *qpic_clk;
296 struct msm_bus_scale_pdata *use_cases;
297 uint32_t client_handle;
298 atomic_t clk_enabled;
299 atomic_t curr_vote;
Sahitya Tummalaa2de55d2017-06-30 16:02:59 +0530300 bool rpmh_clk;
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +0530301};
302
Sahitya Tummaladd8caf42017-10-11 10:12:19 +0530303struct msm_nand_perf_stats {
304 u64 total_read_size;
305 u64 total_write_size;
306 u64 total_erase_blks;
307 ktime_t total_read_time;
308 ktime_t total_write_time;
309 ktime_t total_erase_time;
310 ktime_t min_read_time;
311 ktime_t min_write_time;
312 ktime_t min_erase_time;
313 ktime_t max_read_time;
314 ktime_t max_write_time;
315 ktime_t max_erase_time;
316 spinlock_t lock;
317};
318
319
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +0530320/* Structure that defines NANDc private data. */
321struct msm_nand_info {
322 struct mtd_info mtd;
323 struct msm_nand_chip nand_chip;
324 struct msm_nand_sps_info sps;
325 unsigned long bam_phys;
326 unsigned long nand_phys;
327 unsigned long nand_phys_adjusted;
328 void __iomem *bam_base;
329 int bam_irq;
330 /*
331 * This lock must be acquired before submitting any command or data
332 * descriptors to BAM pipes and must be held until all the submitted
333 * descriptors are processed.
334 *
335 * This is required to ensure that both command and descriptors are
336 * submitted atomically without interruption from other clients,
337 * when there are requests from more than client at any time.
338 * Othewise, data and command descriptors can be submitted out of
339 * order for a request which can cause data corruption.
340 */
341 struct mutex lock;
342 struct flash_identification flash_dev;
343 struct msm_nand_clk_data clk_data;
Sahitya Tummaladd8caf42017-10-11 10:12:19 +0530344 struct msm_nand_perf_stats perf;
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +0530345 u64 dma_mask;
346};
347
348/* Structure that defines an ONFI parameter page (512B) */
349struct onfi_param_page {
350 uint32_t parameter_page_signature;
351 uint16_t revision_number;
352 uint16_t features_supported;
353 uint16_t optional_commands_supported;
354 uint8_t reserved0[22];
355 uint8_t device_manufacturer[12];
356 uint8_t device_model[20];
357 uint8_t jedec_manufacturer_id;
358 uint16_t date_code;
359 uint8_t reserved1[13];
360 uint32_t number_of_data_bytes_per_page;
361 uint16_t number_of_spare_bytes_per_page;
362 uint32_t number_of_data_bytes_per_partial_page;
363 uint16_t number_of_spare_bytes_per_partial_page;
364 uint32_t number_of_pages_per_block;
365 uint32_t number_of_blocks_per_logical_unit;
366 uint8_t number_of_logical_units;
367 uint8_t number_of_address_cycles;
368 uint8_t number_of_bits_per_cell;
369 uint16_t maximum_bad_blocks_per_logical_unit;
370 uint16_t block_endurance;
371 uint8_t guaranteed_valid_begin_blocks;
372 uint16_t guaranteed_valid_begin_blocks_endurance;
373 uint8_t number_of_programs_per_page;
374 uint8_t partial_program_attributes;
375 uint8_t number_of_bits_ecc_correctability;
376 uint8_t number_of_interleaved_address_bits;
377 uint8_t interleaved_operation_attributes;
378 uint8_t reserved2[13];
379 uint8_t io_pin_capacitance;
380 uint16_t timing_mode_support;
381 uint16_t program_cache_timing_mode_support;
382 uint16_t maximum_page_programming_time;
383 uint16_t maximum_block_erase_time;
384 uint16_t maximum_page_read_time;
385 uint16_t maximum_change_column_setup_time;
386 uint8_t reserved3[23];
387 uint16_t vendor_specific_revision_number;
388 uint8_t vendor_specific[88];
389 uint16_t integrity_crc;
390} __attribute__((__packed__));
391
392#define FLASH_PART_MAGIC1 0x55EE73AA
393#define FLASH_PART_MAGIC2 0xE35EBDDB
394#define FLASH_PTABLE_V3 3
395#define FLASH_PTABLE_V4 4
396#define FLASH_PTABLE_MAX_PARTS_V3 16
397#define FLASH_PTABLE_MAX_PARTS_V4 32
398#define FLASH_PTABLE_HDR_LEN (4*sizeof(uint32_t))
399#define FLASH_PTABLE_ENTRY_NAME_SIZE 16
400
401struct flash_partition_entry {
402 char name[FLASH_PTABLE_ENTRY_NAME_SIZE];
403 u32 offset; /* Offset in blocks from beginning of device */
404 u32 length; /* Length of the partition in blocks */
405 u8 attr; /* Flags for this partition */
406};
407
408struct flash_partition_table {
409 u32 magic1;
410 u32 magic2;
411 u32 version;
412 u32 numparts;
413 struct flash_partition_entry part_entry[FLASH_PTABLE_MAX_PARTS_V4];
414};
415
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +0530416static struct flash_partition_table ptable;
Sahitya Tummala3f3f2f92017-06-05 09:03:42 +0530417
418static struct mtd_partition mtd_part[FLASH_PTABLE_MAX_PARTS_V4];
419
420static inline bool is_buffer_in_page(const void *buf, size_t len)
421{
422 return !(((unsigned long) buf & ~PAGE_MASK) + len > PAGE_SIZE);
423}
424#endif /* __QPIC_NAND_H */