xref: /OK3568_Linux_fs/kernel/drivers/mtd/maps/physmap-ixp4xx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Intel IXP4xx OF physmap add-on
4*4882a593Smuzhiyun  * Copyright (C) 2019 Linus Walleij <linus.walleij@linaro.org>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Based on the ixp4xx.c map driver, originally written by:
7*4882a593Smuzhiyun  * Intel Corporation
8*4882a593Smuzhiyun  * Deepak Saxena <dsaxena@mvista.com>
9*4882a593Smuzhiyun  * Copyright (C) 2002 Intel Corporation
10*4882a593Smuzhiyun  * Copyright (C) 2003-2004 MontaVista Software, Inc.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun #include <linux/export.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/of_device.h>
15*4882a593Smuzhiyun #include <linux/mtd/map.h>
16*4882a593Smuzhiyun #include <linux/mtd/xip.h>
17*4882a593Smuzhiyun #include "physmap-ixp4xx.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Read/write a 16 bit word from flash address 'addr'.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * When the cpu is in little-endian mode it swizzles the address lines
23*4882a593Smuzhiyun  * ('address coherency') so we need to undo the swizzling to ensure commands
24*4882a593Smuzhiyun  * and the like end up on the correct flash address.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * To further complicate matters, due to the way the expansion bus controller
27*4882a593Smuzhiyun  * handles 32 bit reads, the byte stream ABCD is stored on the flash as:
28*4882a593Smuzhiyun  *     D15    D0
29*4882a593Smuzhiyun  *     +---+---+
30*4882a593Smuzhiyun  *     | A | B | 0
31*4882a593Smuzhiyun  *     +---+---+
32*4882a593Smuzhiyun  *     | C | D | 2
33*4882a593Smuzhiyun  *     +---+---+
34*4882a593Smuzhiyun  * This means that on LE systems each 16 bit word must be swapped. Note that
35*4882a593Smuzhiyun  * this requires CONFIG_MTD_CFI_BE_BYTE_SWAP to be enabled to 'unswap' the CFI
36*4882a593Smuzhiyun  * data and other flash commands which are always in D7-D0.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #ifndef CONFIG_CPU_BIG_ENDIAN
39*4882a593Smuzhiyun 
flash_read16(void __iomem * addr)40*4882a593Smuzhiyun static inline u16 flash_read16(void __iomem *addr)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	return be16_to_cpu(__raw_readw((void __iomem *)((unsigned long)addr ^ 0x2)));
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
flash_write16(u16 d,void __iomem * addr)45*4882a593Smuzhiyun static inline void flash_write16(u16 d, void __iomem *addr)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	__raw_writew(cpu_to_be16(d), (void __iomem *)((unsigned long)addr ^ 0x2));
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define	BYTE0(h)	((h) & 0xFF)
51*4882a593Smuzhiyun #define	BYTE1(h)	(((h) >> 8) & 0xFF)
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #else
54*4882a593Smuzhiyun 
flash_read16(const void __iomem * addr)55*4882a593Smuzhiyun static inline u16 flash_read16(const void __iomem *addr)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	return __raw_readw(addr);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
flash_write16(u16 d,void __iomem * addr)60*4882a593Smuzhiyun static inline void flash_write16(u16 d, void __iomem *addr)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	__raw_writew(d, addr);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #define	BYTE0(h)	(((h) >> 8) & 0xFF)
66*4882a593Smuzhiyun #define	BYTE1(h)	((h) & 0xFF)
67*4882a593Smuzhiyun #endif
68*4882a593Smuzhiyun 
ixp4xx_read16(struct map_info * map,unsigned long ofs)69*4882a593Smuzhiyun static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	map_word val;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	val.x[0] = flash_read16(map->virt + ofs);
74*4882a593Smuzhiyun 	return val;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun  * The IXP4xx expansion bus only allows 16-bit wide acceses
79*4882a593Smuzhiyun  * when attached to a 16-bit wide device (such as the 28F128J3A),
80*4882a593Smuzhiyun  * so we can't just memcpy_fromio().
81*4882a593Smuzhiyun  */
ixp4xx_copy_from(struct map_info * map,void * to,unsigned long from,ssize_t len)82*4882a593Smuzhiyun static void ixp4xx_copy_from(struct map_info *map, void *to,
83*4882a593Smuzhiyun 			     unsigned long from, ssize_t len)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	u8 *dest = (u8 *) to;
86*4882a593Smuzhiyun 	void __iomem *src = map->virt + from;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	if (len <= 0)
89*4882a593Smuzhiyun 		return;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	if (from & 1) {
92*4882a593Smuzhiyun 		*dest++ = BYTE1(flash_read16(src-1));
93*4882a593Smuzhiyun 		src++;
94*4882a593Smuzhiyun 		--len;
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	while (len >= 2) {
98*4882a593Smuzhiyun 		u16 data = flash_read16(src);
99*4882a593Smuzhiyun 		*dest++ = BYTE0(data);
100*4882a593Smuzhiyun 		*dest++ = BYTE1(data);
101*4882a593Smuzhiyun 		src += 2;
102*4882a593Smuzhiyun 		len -= 2;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (len > 0)
106*4882a593Smuzhiyun 		*dest++ = BYTE0(flash_read16(src));
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
ixp4xx_write16(struct map_info * map,map_word d,unsigned long adr)109*4882a593Smuzhiyun static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	flash_write16(d.x[0], map->virt + adr);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
of_flash_probe_ixp4xx(struct platform_device * pdev,struct device_node * np,struct map_info * map)114*4882a593Smuzhiyun int of_flash_probe_ixp4xx(struct platform_device *pdev,
115*4882a593Smuzhiyun 			  struct device_node *np,
116*4882a593Smuzhiyun 			  struct map_info *map)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/* Multiplatform guard */
121*4882a593Smuzhiyun 	if (!of_device_is_compatible(np, "intel,ixp4xx-flash"))
122*4882a593Smuzhiyun 		return 0;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	map->read = ixp4xx_read16;
125*4882a593Smuzhiyun 	map->write = ixp4xx_write16;
126*4882a593Smuzhiyun 	map->copy_from = ixp4xx_copy_from;
127*4882a593Smuzhiyun 	map->copy_to = NULL;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	dev_info(dev, "initialized Intel IXP4xx-specific physmap control\n");
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return 0;
132*4882a593Smuzhiyun }
133