1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* sc520cdp.c -- MTD map driver for AMD SC520 Customer Development Platform
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2001 Sysgo Real-Time Solutions GmbH
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * The SC520CDP is an evaluation board for the Elan SC520 processor available
7*4882a593Smuzhiyun * from AMD. It has two banks of 32-bit Flash ROM, each 8 Megabytes in size,
8*4882a593Smuzhiyun * and up to 512 KiB of 8-bit DIL Flash ROM.
9*4882a593Smuzhiyun * For details see https://www.amd.com/products/epd/desiging/evalboards/18.elansc520/520_cdp_brief/index.html
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <asm/io.h>
17*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
18*4882a593Smuzhiyun #include <linux/mtd/map.h>
19*4882a593Smuzhiyun #include <linux/mtd/concat.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun ** The Embedded Systems BIOS decodes the first FLASH starting at
23*4882a593Smuzhiyun ** 0x8400000. This is a *terrible* place for it because accessing
24*4882a593Smuzhiyun ** the flash at this location causes the A22 address line to be high
25*4882a593Smuzhiyun ** (that's what 0x8400000 binary's ought to be). But this is the highest
26*4882a593Smuzhiyun ** order address line on the raw flash devices themselves!!
27*4882a593Smuzhiyun ** This causes the top HALF of the flash to be accessed first. Beyond
28*4882a593Smuzhiyun ** the physical limits of the flash, the flash chip aliases over (to
29*4882a593Smuzhiyun ** 0x880000 which causes the bottom half to be accessed. This splits the
30*4882a593Smuzhiyun ** flash into two and inverts it! If you then try to access this from another
31*4882a593Smuzhiyun ** program that does NOT do this insanity, then you *will* access the
32*4882a593Smuzhiyun ** first half of the flash, but not find what you expect there. That
33*4882a593Smuzhiyun ** stuff is in the *second* half! Similarly, the address used by the
34*4882a593Smuzhiyun ** BIOS for the second FLASH bank is also quite a bad choice.
35*4882a593Smuzhiyun ** If REPROGRAM_PAR is defined below (the default), then this driver will
36*4882a593Smuzhiyun ** choose more useful addresses for the FLASH banks by reprogramming the
37*4882a593Smuzhiyun ** responsible PARxx registers in the SC520's MMCR region. This will
38*4882a593Smuzhiyun ** cause the settings to be incompatible with the BIOS's settings, which
39*4882a593Smuzhiyun ** shouldn't be a problem since you are running Linux, (i.e. the BIOS is
40*4882a593Smuzhiyun ** not much use anyway). However, if you need to be compatible with
41*4882a593Smuzhiyun ** the BIOS for some reason, just undefine REPROGRAM_PAR.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun #define REPROGRAM_PAR
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #ifdef REPROGRAM_PAR
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /* These are the addresses we want.. */
50*4882a593Smuzhiyun #define WINDOW_ADDR_0 0x08800000
51*4882a593Smuzhiyun #define WINDOW_ADDR_1 0x09000000
52*4882a593Smuzhiyun #define WINDOW_ADDR_2 0x09800000
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* .. and these are the addresses the BIOS gives us */
55*4882a593Smuzhiyun #define WINDOW_ADDR_0_BIOS 0x08400000
56*4882a593Smuzhiyun #define WINDOW_ADDR_1_BIOS 0x08c00000
57*4882a593Smuzhiyun #define WINDOW_ADDR_2_BIOS 0x09400000
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #else
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define WINDOW_ADDR_0 0x08400000
62*4882a593Smuzhiyun #define WINDOW_ADDR_1 0x08C00000
63*4882a593Smuzhiyun #define WINDOW_ADDR_2 0x09400000
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #endif
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define WINDOW_SIZE_0 0x00800000
68*4882a593Smuzhiyun #define WINDOW_SIZE_1 0x00800000
69*4882a593Smuzhiyun #define WINDOW_SIZE_2 0x00080000
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun static struct map_info sc520cdp_map[] = {
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun .name = "SC520CDP Flash Bank #0",
75*4882a593Smuzhiyun .size = WINDOW_SIZE_0,
76*4882a593Smuzhiyun .bankwidth = 4,
77*4882a593Smuzhiyun .phys = WINDOW_ADDR_0
78*4882a593Smuzhiyun },
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun .name = "SC520CDP Flash Bank #1",
81*4882a593Smuzhiyun .size = WINDOW_SIZE_1,
82*4882a593Smuzhiyun .bankwidth = 4,
83*4882a593Smuzhiyun .phys = WINDOW_ADDR_1
84*4882a593Smuzhiyun },
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun .name = "SC520CDP DIL Flash",
87*4882a593Smuzhiyun .size = WINDOW_SIZE_2,
88*4882a593Smuzhiyun .bankwidth = 1,
89*4882a593Smuzhiyun .phys = WINDOW_ADDR_2
90*4882a593Smuzhiyun },
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun #define NUM_FLASH_BANKS ARRAY_SIZE(sc520cdp_map)
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static struct mtd_info *mymtd[NUM_FLASH_BANKS];
96*4882a593Smuzhiyun static struct mtd_info *merged_mtd;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun #ifdef REPROGRAM_PAR
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun ** The SC520 MMCR (memory mapped control register) region resides
102*4882a593Smuzhiyun ** at 0xFFFEF000. The 16 Programmable Address Region (PAR) registers
103*4882a593Smuzhiyun ** are at offset 0x88 in the MMCR:
104*4882a593Smuzhiyun */
105*4882a593Smuzhiyun #define SC520_MMCR_BASE 0xFFFEF000
106*4882a593Smuzhiyun #define SC520_MMCR_EXTENT 0x1000
107*4882a593Smuzhiyun #define SC520_PAR(x) ((0x88/sizeof(unsigned long)) + (x))
108*4882a593Smuzhiyun #define NUM_SC520_PAR 16 /* total number of PAR registers */
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun ** The highest three bits in a PAR register determine what target
112*4882a593Smuzhiyun ** device is controlled by this PAR. Here, only ROMCS? and BOOTCS
113*4882a593Smuzhiyun ** devices are of interest.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun #define SC520_PAR_BOOTCS (0x4<<29)
116*4882a593Smuzhiyun #define SC520_PAR_ROMCS0 (0x5<<29)
117*4882a593Smuzhiyun #define SC520_PAR_ROMCS1 (0x6<<29)
118*4882a593Smuzhiyun #define SC520_PAR_TRGDEV (0x7<<29)
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun ** Bits 28 thru 26 determine some attributes for the
122*4882a593Smuzhiyun ** region controlled by the PAR. (We only use non-cacheable)
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun #define SC520_PAR_WRPROT (1<<26) /* write protected */
125*4882a593Smuzhiyun #define SC520_PAR_NOCACHE (1<<27) /* non-cacheable */
126*4882a593Smuzhiyun #define SC520_PAR_NOEXEC (1<<28) /* code execution denied */
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun ** Bit 25 determines the granularity: 4K or 64K
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun #define SC520_PAR_PG_SIZ4 (0<<25)
133*4882a593Smuzhiyun #define SC520_PAR_PG_SIZ64 (1<<25)
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /*
136*4882a593Smuzhiyun ** Build a value to be written into a PAR register.
137*4882a593Smuzhiyun ** We only need ROM entries, 64K page size:
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun #define SC520_PAR_ENTRY(trgdev, address, size) \
140*4882a593Smuzhiyun ((trgdev) | SC520_PAR_NOCACHE | SC520_PAR_PG_SIZ64 | \
141*4882a593Smuzhiyun (address) >> 16 | (((size) >> 16) - 1) << 14)
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun struct sc520_par_table
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun unsigned long trgdev;
146*4882a593Smuzhiyun unsigned long new_par;
147*4882a593Smuzhiyun unsigned long default_address;
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun static const struct sc520_par_table par_table[NUM_FLASH_BANKS] =
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun { /* Flash Bank #0: selected by ROMCS0 */
153*4882a593Smuzhiyun SC520_PAR_ROMCS0,
154*4882a593Smuzhiyun SC520_PAR_ENTRY(SC520_PAR_ROMCS0, WINDOW_ADDR_0, WINDOW_SIZE_0),
155*4882a593Smuzhiyun WINDOW_ADDR_0_BIOS
156*4882a593Smuzhiyun },
157*4882a593Smuzhiyun { /* Flash Bank #1: selected by ROMCS1 */
158*4882a593Smuzhiyun SC520_PAR_ROMCS1,
159*4882a593Smuzhiyun SC520_PAR_ENTRY(SC520_PAR_ROMCS1, WINDOW_ADDR_1, WINDOW_SIZE_1),
160*4882a593Smuzhiyun WINDOW_ADDR_1_BIOS
161*4882a593Smuzhiyun },
162*4882a593Smuzhiyun { /* DIL (BIOS) Flash: selected by BOOTCS */
163*4882a593Smuzhiyun SC520_PAR_BOOTCS,
164*4882a593Smuzhiyun SC520_PAR_ENTRY(SC520_PAR_BOOTCS, WINDOW_ADDR_2, WINDOW_SIZE_2),
165*4882a593Smuzhiyun WINDOW_ADDR_2_BIOS
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun };
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun
sc520cdp_setup_par(void)170*4882a593Smuzhiyun static void sc520cdp_setup_par(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun unsigned long __iomem *mmcr;
173*4882a593Smuzhiyun unsigned long mmcr_val;
174*4882a593Smuzhiyun int i, j;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /* map in SC520's MMCR area */
177*4882a593Smuzhiyun mmcr = ioremap(SC520_MMCR_BASE, SC520_MMCR_EXTENT);
178*4882a593Smuzhiyun if(!mmcr) { /* ioremap failed: skip the PAR reprogramming */
179*4882a593Smuzhiyun /* force physical address fields to BIOS defaults: */
180*4882a593Smuzhiyun for(i = 0; i < NUM_FLASH_BANKS; i++)
181*4882a593Smuzhiyun sc520cdp_map[i].phys = par_table[i].default_address;
182*4882a593Smuzhiyun return;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun ** Find the PARxx registers that are responsible for activating
187*4882a593Smuzhiyun ** ROMCS0, ROMCS1 and BOOTCS. Reprogram each of these with a
188*4882a593Smuzhiyun ** new value from the table.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun for(i = 0; i < NUM_FLASH_BANKS; i++) { /* for each par_table entry */
191*4882a593Smuzhiyun for(j = 0; j < NUM_SC520_PAR; j++) { /* for each PAR register */
192*4882a593Smuzhiyun mmcr_val = readl(&mmcr[SC520_PAR(j)]);
193*4882a593Smuzhiyun /* if target device field matches, reprogram the PAR */
194*4882a593Smuzhiyun if((mmcr_val & SC520_PAR_TRGDEV) == par_table[i].trgdev)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun writel(par_table[i].new_par, &mmcr[SC520_PAR(j)]);
197*4882a593Smuzhiyun break;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun if(j == NUM_SC520_PAR)
201*4882a593Smuzhiyun { /* no matching PAR found: try default BIOS address */
202*4882a593Smuzhiyun printk(KERN_NOTICE "Could not find PAR responsible for %s\n",
203*4882a593Smuzhiyun sc520cdp_map[i].name);
204*4882a593Smuzhiyun printk(KERN_NOTICE "Trying default address 0x%lx\n",
205*4882a593Smuzhiyun par_table[i].default_address);
206*4882a593Smuzhiyun sc520cdp_map[i].phys = par_table[i].default_address;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun iounmap(mmcr);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun
init_sc520cdp(void)214*4882a593Smuzhiyun static int __init init_sc520cdp(void)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun int i, j, devices_found = 0;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #ifdef REPROGRAM_PAR
219*4882a593Smuzhiyun /* reprogram PAR registers so flash appears at the desired addresses */
220*4882a593Smuzhiyun sc520cdp_setup_par();
221*4882a593Smuzhiyun #endif
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun for (i = 0; i < NUM_FLASH_BANKS; i++) {
224*4882a593Smuzhiyun printk(KERN_NOTICE "SC520 CDP flash device: 0x%Lx at 0x%Lx\n",
225*4882a593Smuzhiyun (unsigned long long)sc520cdp_map[i].size,
226*4882a593Smuzhiyun (unsigned long long)sc520cdp_map[i].phys);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun sc520cdp_map[i].virt = ioremap(sc520cdp_map[i].phys, sc520cdp_map[i].size);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun if (!sc520cdp_map[i].virt) {
231*4882a593Smuzhiyun printk("Failed to ioremap\n");
232*4882a593Smuzhiyun for (j = 0; j < i; j++) {
233*4882a593Smuzhiyun if (mymtd[j]) {
234*4882a593Smuzhiyun map_destroy(mymtd[j]);
235*4882a593Smuzhiyun iounmap(sc520cdp_map[j].virt);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun return -EIO;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun simple_map_init(&sc520cdp_map[i]);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun mymtd[i] = do_map_probe("cfi_probe", &sc520cdp_map[i]);
244*4882a593Smuzhiyun if(!mymtd[i])
245*4882a593Smuzhiyun mymtd[i] = do_map_probe("jedec_probe", &sc520cdp_map[i]);
246*4882a593Smuzhiyun if(!mymtd[i])
247*4882a593Smuzhiyun mymtd[i] = do_map_probe("map_rom", &sc520cdp_map[i]);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (mymtd[i]) {
250*4882a593Smuzhiyun mymtd[i]->owner = THIS_MODULE;
251*4882a593Smuzhiyun ++devices_found;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun else {
254*4882a593Smuzhiyun iounmap(sc520cdp_map[i].virt);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun if(devices_found >= 2) {
258*4882a593Smuzhiyun /* Combine the two flash banks into a single MTD device & register it: */
259*4882a593Smuzhiyun merged_mtd = mtd_concat_create(mymtd, 2, "SC520CDP Flash Banks #0 and #1");
260*4882a593Smuzhiyun if(merged_mtd)
261*4882a593Smuzhiyun mtd_device_register(merged_mtd, NULL, 0);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun if(devices_found == 3) /* register the third (DIL-Flash) device */
264*4882a593Smuzhiyun mtd_device_register(mymtd[2], NULL, 0);
265*4882a593Smuzhiyun return(devices_found ? 0 : -ENXIO);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
cleanup_sc520cdp(void)268*4882a593Smuzhiyun static void __exit cleanup_sc520cdp(void)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun int i;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun if (merged_mtd) {
273*4882a593Smuzhiyun mtd_device_unregister(merged_mtd);
274*4882a593Smuzhiyun mtd_concat_destroy(merged_mtd);
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun if (mymtd[2])
277*4882a593Smuzhiyun mtd_device_unregister(mymtd[2]);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun for (i = 0; i < NUM_FLASH_BANKS; i++) {
280*4882a593Smuzhiyun if (mymtd[i])
281*4882a593Smuzhiyun map_destroy(mymtd[i]);
282*4882a593Smuzhiyun if (sc520cdp_map[i].virt) {
283*4882a593Smuzhiyun iounmap(sc520cdp_map[i].virt);
284*4882a593Smuzhiyun sc520cdp_map[i].virt = NULL;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun module_init(init_sc520cdp);
290*4882a593Smuzhiyun module_exit(cleanup_sc520cdp);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun MODULE_LICENSE("GPL");
293*4882a593Smuzhiyun MODULE_AUTHOR("Sysgo Real-Time Solutions GmbH");
294*4882a593Smuzhiyun MODULE_DESCRIPTION("MTD map driver for AMD SC520 Customer Development Platform");
295