sc520cdp.c 8.9 KB

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