sc520cdp.c 8.9 KB

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