sc520cdp.c 8.4 KB

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