1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
8 #ifndef _MTK_SNAND_OS_H_
9 #define _MTK_SNAND_OS_H_
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/limits.h>
14 #include <linux/types.h>
15 #include <linux/bitops.h>
16 #include <linux/sizes.h>
17 #include <linux/iopoll.h>
18 #include <linux/hrtimer.h>
19 #include <linux/device.h>
20 #include <linux/dma-mapping.h>
22 #include <asm/div64.h>
24 struct mtk_snand_plat_dev
{
26 struct completion done
;
30 #define read16_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
31 readw_poll_timeout((addr), (val), (cond), (sleep_us), (timeout_us))
33 #define read32_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
34 readl_poll_timeout((addr), (val), (cond), (sleep_us), (timeout_us))
37 #define mtk_snand_time_t ktime_t
39 static inline mtk_snand_time_t
timer_get_ticks(void)
44 static inline mtk_snand_time_t
timer_time_to_tick(uint32_t timeout_us
)
46 return ktime_add_us(ktime_set(0, 0), timeout_us
);
49 static inline bool timer_is_timeout(mtk_snand_time_t start_tick
,
50 mtk_snand_time_t timeout_tick
)
52 ktime_t tmo
= ktime_add(start_tick
, timeout_tick
);
54 return ktime_compare(ktime_get(), tmo
) > 0;
58 static inline void *generic_mem_alloc(struct mtk_snand_plat_dev
*pdev
,
61 return devm_kzalloc(pdev
->dev
, size
, GFP_KERNEL
);
63 static inline void generic_mem_free(struct mtk_snand_plat_dev
*pdev
, void *ptr
)
65 devm_kfree(pdev
->dev
, ptr
);
68 static inline void *dma_mem_alloc(struct mtk_snand_plat_dev
*pdev
, size_t size
)
70 return kzalloc(size
, GFP_KERNEL
);
72 static inline void dma_mem_free(struct mtk_snand_plat_dev
*pdev
, void *ptr
)
77 static inline int dma_mem_map(struct mtk_snand_plat_dev
*pdev
, void *vaddr
,
78 uintptr_t *dma_addr
, size_t size
, bool to_device
)
83 addr
= dma_map_single(pdev
->dev
, vaddr
, size
,
84 to_device
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
85 ret
= dma_mapping_error(pdev
->dev
, addr
);
89 *dma_addr
= (uintptr_t)addr
;
94 static inline void dma_mem_unmap(struct mtk_snand_plat_dev
*pdev
,
95 uintptr_t dma_addr
, size_t size
,
98 dma_unmap_single(pdev
->dev
, dma_addr
, size
,
99 to_device
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
102 /* Interrupt helpers */
103 static inline void irq_completion_done(struct mtk_snand_plat_dev
*pdev
)
105 complete(&pdev
->done
);
108 static inline void irq_completion_init(struct mtk_snand_plat_dev
*pdev
)
110 init_completion(&pdev
->done
);
113 static inline int irq_completion_wait(struct mtk_snand_plat_dev
*pdev
,
114 void __iomem
*reg
, uint32_t bit
,
119 ret
= wait_for_completion_timeout(&pdev
->done
,
120 usecs_to_jiffies(timeout_us
));
127 #endif /* _MTK_SNAND_OS_H_ */