sc520cdp.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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