nettel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /****************************************************************************/
  2. /*
  3. * nettel.c -- mappings for NETtel/SecureEdge/SnapGear (x86) boards.
  4. *
  5. * (C) Copyright 2000-2001, Greg Ungerer (gerg@snapgear.com)
  6. * (C) Copyright 2001-2002, SnapGear (www.snapgear.com)
  7. */
  8. /****************************************************************************/
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/map.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/mtd/cfi.h>
  17. #include <linux/reboot.h>
  18. #include <linux/err.h>
  19. #include <linux/kdev_t.h>
  20. #include <linux/root_dev.h>
  21. #include <asm/io.h>
  22. /****************************************************************************/
  23. #define INTEL_BUSWIDTH 1
  24. #define AMD_WINDOW_MAXSIZE 0x00200000
  25. #define AMD_BUSWIDTH 1
  26. /*
  27. * PAR masks and shifts, assuming 64K pages.
  28. */
  29. #define SC520_PAR_ADDR_MASK 0x00003fff
  30. #define SC520_PAR_ADDR_SHIFT 16
  31. #define SC520_PAR_TO_ADDR(par) \
  32. (((par)&SC520_PAR_ADDR_MASK) << SC520_PAR_ADDR_SHIFT)
  33. #define SC520_PAR_SIZE_MASK 0x01ffc000
  34. #define SC520_PAR_SIZE_SHIFT 2
  35. #define SC520_PAR_TO_SIZE(par) \
  36. ((((par)&SC520_PAR_SIZE_MASK) << SC520_PAR_SIZE_SHIFT) + (64*1024))
  37. #define SC520_PAR(cs, addr, size) \
  38. ((cs) | \
  39. ((((size)-(64*1024)) >> SC520_PAR_SIZE_SHIFT) & SC520_PAR_SIZE_MASK) | \
  40. (((addr) >> SC520_PAR_ADDR_SHIFT) & SC520_PAR_ADDR_MASK))
  41. #define SC520_PAR_BOOTCS 0x8a000000
  42. #define SC520_PAR_ROMCS1 0xaa000000
  43. #define SC520_PAR_ROMCS2 0xca000000 /* Cache disabled, 64K page */
  44. static void *nettel_mmcrp = NULL;
  45. #ifdef CONFIG_MTD_CFI_INTELEXT
  46. static struct mtd_info *intel_mtd;
  47. #endif
  48. static struct mtd_info *amd_mtd;
  49. /****************************************************************************/
  50. /****************************************************************************/
  51. #ifdef CONFIG_MTD_CFI_INTELEXT
  52. static struct map_info nettel_intel_map = {
  53. .name = "SnapGear Intel",
  54. .size = 0,
  55. .bankwidth = INTEL_BUSWIDTH,
  56. };
  57. static struct mtd_partition nettel_intel_partitions[] = {
  58. {
  59. .name = "SnapGear kernel",
  60. .offset = 0,
  61. .size = 0x000e0000
  62. },
  63. {
  64. .name = "SnapGear filesystem",
  65. .offset = 0x00100000,
  66. },
  67. {
  68. .name = "SnapGear config",
  69. .offset = 0x000e0000,
  70. .size = 0x00020000
  71. },
  72. {
  73. .name = "SnapGear Intel",
  74. .offset = 0
  75. },
  76. {
  77. .name = "SnapGear BIOS Config",
  78. .offset = 0x007e0000,
  79. .size = 0x00020000
  80. },
  81. {
  82. .name = "SnapGear BIOS",
  83. .offset = 0x007e0000,
  84. .size = 0x00020000
  85. },
  86. };
  87. #endif
  88. static struct map_info nettel_amd_map = {
  89. .name = "SnapGear AMD",
  90. .size = AMD_WINDOW_MAXSIZE,
  91. .bankwidth = AMD_BUSWIDTH,
  92. };
  93. static struct mtd_partition nettel_amd_partitions[] = {
  94. {
  95. .name = "SnapGear BIOS config",
  96. .offset = 0x000e0000,
  97. .size = 0x00010000
  98. },
  99. {
  100. .name = "SnapGear BIOS",
  101. .offset = 0x000f0000,
  102. .size = 0x00010000
  103. },
  104. {
  105. .name = "SnapGear AMD",
  106. .offset = 0
  107. },
  108. {
  109. .name = "SnapGear high BIOS",
  110. .offset = 0x001f0000,
  111. .size = 0x00010000
  112. }
  113. };
  114. #define NUM_AMD_PARTITIONS ARRAY_SIZE(nettel_amd_partitions)
  115. /****************************************************************************/
  116. #ifdef CONFIG_MTD_CFI_INTELEXT
  117. /*
  118. * Set the Intel flash back to read mode since some old boot
  119. * loaders don't.
  120. */
  121. static int nettel_reboot_notifier(struct notifier_block *nb, unsigned long val, void *v)
  122. {
  123. struct cfi_private *cfi = nettel_intel_map.fldrv_priv;
  124. unsigned long b;
  125. /* Make sure all FLASH chips are put back into read mode */
  126. for (b = 0; (b < nettel_intel_partitions[3].size); b += 0x100000) {
  127. cfi_send_gen_cmd(0xff, 0x55, b, &nettel_intel_map, cfi,
  128. cfi->device_type, NULL);
  129. }
  130. return(NOTIFY_OK);
  131. }
  132. static struct notifier_block nettel_notifier_block = {
  133. nettel_reboot_notifier, NULL, 0
  134. };
  135. #endif
  136. /****************************************************************************/
  137. static int __init nettel_init(void)
  138. {
  139. volatile unsigned long *amdpar;
  140. unsigned long amdaddr, maxsize;
  141. int num_amd_partitions=0;
  142. #ifdef CONFIG_MTD_CFI_INTELEXT
  143. volatile unsigned long *intel0par, *intel1par;
  144. unsigned long orig_bootcspar, orig_romcs1par;
  145. unsigned long intel0addr, intel0size;
  146. unsigned long intel1addr, intel1size;
  147. int intelboot, intel0cs, intel1cs;
  148. int num_intel_partitions;
  149. #endif
  150. int rc = 0;
  151. nettel_mmcrp = (void *) ioremap_nocache(0xfffef000, 4096);
  152. if (nettel_mmcrp == NULL) {
  153. printk("SNAPGEAR: failed to disable MMCR cache??\n");
  154. return(-EIO);
  155. }
  156. /* Set CPU clock to be 33.000MHz */
  157. *((unsigned char *) (nettel_mmcrp + 0xc64)) = 0x01;
  158. amdpar = (volatile unsigned long *) (nettel_mmcrp + 0xc4);
  159. #ifdef CONFIG_MTD_CFI_INTELEXT
  160. intelboot = 0;
  161. intel0cs = SC520_PAR_ROMCS1;
  162. intel0par = (volatile unsigned long *) (nettel_mmcrp + 0xc0);
  163. intel1cs = SC520_PAR_ROMCS2;
  164. intel1par = (volatile unsigned long *) (nettel_mmcrp + 0xbc);
  165. /*
  166. * Save the CS settings then ensure ROMCS1 and ROMCS2 are off,
  167. * otherwise they might clash with where we try to map BOOTCS.
  168. */
  169. orig_bootcspar = *amdpar;
  170. orig_romcs1par = *intel0par;
  171. *intel0par = 0;
  172. *intel1par = 0;
  173. #endif
  174. /*
  175. * The first thing to do is determine if we have a separate
  176. * boot FLASH device. Typically this is a small (1 to 2MB)
  177. * AMD FLASH part. It seems that device size is about the
  178. * only way to tell if this is the case...
  179. */
  180. amdaddr = 0x20000000;
  181. maxsize = AMD_WINDOW_MAXSIZE;
  182. *amdpar = SC520_PAR(SC520_PAR_BOOTCS, amdaddr, maxsize);
  183. __asm__ ("wbinvd");
  184. nettel_amd_map.phys = amdaddr;
  185. nettel_amd_map.virt = ioremap_nocache(amdaddr, maxsize);
  186. if (!nettel_amd_map.virt) {
  187. printk("SNAPGEAR: failed to ioremap() BOOTCS\n");
  188. iounmap(nettel_mmcrp);
  189. return(-EIO);
  190. }
  191. simple_map_init(&nettel_amd_map);
  192. if ((amd_mtd = do_map_probe("jedec_probe", &nettel_amd_map))) {
  193. printk(KERN_NOTICE "SNAPGEAR: AMD flash device size = %dK\n",
  194. (int)(amd_mtd->size>>10));
  195. amd_mtd->owner = THIS_MODULE;
  196. /* The high BIOS partition is only present for 2MB units */
  197. num_amd_partitions = NUM_AMD_PARTITIONS;
  198. if (amd_mtd->size < AMD_WINDOW_MAXSIZE)
  199. num_amd_partitions--;
  200. /* Don't add the partition until after the primary INTEL's */
  201. #ifdef CONFIG_MTD_CFI_INTELEXT
  202. /*
  203. * Map the Intel flash into memory after the AMD
  204. * It has to start on a multiple of maxsize.
  205. */
  206. maxsize = SC520_PAR_TO_SIZE(orig_romcs1par);
  207. if (maxsize < (32 * 1024 * 1024))
  208. maxsize = (32 * 1024 * 1024);
  209. intel0addr = amdaddr + maxsize;
  210. #endif
  211. } else {
  212. #ifdef CONFIG_MTD_CFI_INTELEXT
  213. /* INTEL boot FLASH */
  214. intelboot++;
  215. if (!orig_romcs1par) {
  216. intel0cs = SC520_PAR_BOOTCS;
  217. intel0par = (volatile unsigned long *)
  218. (nettel_mmcrp + 0xc4);
  219. intel1cs = SC520_PAR_ROMCS1;
  220. intel1par = (volatile unsigned long *)
  221. (nettel_mmcrp + 0xc0);
  222. intel0addr = SC520_PAR_TO_ADDR(orig_bootcspar);
  223. maxsize = SC520_PAR_TO_SIZE(orig_bootcspar);
  224. } else {
  225. /* Kernel base is on ROMCS1, not BOOTCS */
  226. intel0cs = SC520_PAR_ROMCS1;
  227. intel0par = (volatile unsigned long *)
  228. (nettel_mmcrp + 0xc0);
  229. intel1cs = SC520_PAR_BOOTCS;
  230. intel1par = (volatile unsigned long *)
  231. (nettel_mmcrp + 0xc4);
  232. intel0addr = SC520_PAR_TO_ADDR(orig_romcs1par);
  233. maxsize = SC520_PAR_TO_SIZE(orig_romcs1par);
  234. }
  235. /* Destroy useless AMD MTD mapping */
  236. amd_mtd = NULL;
  237. iounmap(nettel_amd_map.virt);
  238. nettel_amd_map.virt = NULL;
  239. #else
  240. /* Only AMD flash supported */
  241. rc = -ENXIO;
  242. goto out_unmap2;
  243. #endif
  244. }
  245. #ifdef CONFIG_MTD_CFI_INTELEXT
  246. /*
  247. * We have determined the INTEL FLASH configuration, so lets
  248. * go ahead and probe for them now.
  249. */
  250. /* Set PAR to the maximum size */
  251. if (maxsize < (32 * 1024 * 1024))
  252. maxsize = (32 * 1024 * 1024);
  253. *intel0par = SC520_PAR(intel0cs, intel0addr, maxsize);
  254. /* Turn other PAR off so the first probe doesn't find it */
  255. *intel1par = 0;
  256. /* Probe for the size of the first Intel flash */
  257. nettel_intel_map.size = maxsize;
  258. nettel_intel_map.phys = intel0addr;
  259. nettel_intel_map.virt = ioremap_nocache(intel0addr, maxsize);
  260. if (!nettel_intel_map.virt) {
  261. printk("SNAPGEAR: failed to ioremap() ROMCS1\n");
  262. rc = -EIO;
  263. goto out_unmap2;
  264. }
  265. simple_map_init(&nettel_intel_map);
  266. intel_mtd = do_map_probe("cfi_probe", &nettel_intel_map);
  267. if (!intel_mtd) {
  268. rc = -ENXIO;
  269. goto out_unmap1;
  270. }
  271. /* Set PAR to the detected size */
  272. intel0size = intel_mtd->size;
  273. *intel0par = SC520_PAR(intel0cs, intel0addr, intel0size);
  274. /*
  275. * Map second Intel FLASH right after first. Set its size to the
  276. * same maxsize used for the first Intel FLASH.
  277. */
  278. intel1addr = intel0addr + intel0size;
  279. *intel1par = SC520_PAR(intel1cs, intel1addr, maxsize);
  280. __asm__ ("wbinvd");
  281. maxsize += intel0size;
  282. /* Delete the old map and probe again to do both chips */
  283. map_destroy(intel_mtd);
  284. intel_mtd = NULL;
  285. iounmap(nettel_intel_map.virt);
  286. nettel_intel_map.size = maxsize;
  287. nettel_intel_map.virt = ioremap_nocache(intel0addr, maxsize);
  288. if (!nettel_intel_map.virt) {
  289. printk("SNAPGEAR: failed to ioremap() ROMCS1/2\n");
  290. rc = -EIO;
  291. goto out_unmap2;
  292. }
  293. intel_mtd = do_map_probe("cfi_probe", &nettel_intel_map);
  294. if (! intel_mtd) {
  295. rc = -ENXIO;
  296. goto out_unmap1;
  297. }
  298. intel1size = intel_mtd->size - intel0size;
  299. if (intel1size > 0) {
  300. *intel1par = SC520_PAR(intel1cs, intel1addr, intel1size);
  301. __asm__ ("wbinvd");
  302. } else {
  303. *intel1par = 0;
  304. }
  305. printk(KERN_NOTICE "SNAPGEAR: Intel flash device size = %lldKiB\n",
  306. (unsigned long long)(intel_mtd->size >> 10));
  307. intel_mtd->owner = THIS_MODULE;
  308. num_intel_partitions = ARRAY_SIZE(nettel_intel_partitions);
  309. if (intelboot) {
  310. /*
  311. * Adjust offset and size of last boot partition.
  312. * Must allow for BIOS region at end of FLASH.
  313. */
  314. nettel_intel_partitions[1].size = (intel0size + intel1size) -
  315. (1024*1024 + intel_mtd->erasesize);
  316. nettel_intel_partitions[3].size = intel0size + intel1size;
  317. nettel_intel_partitions[4].offset =
  318. (intel0size + intel1size) - intel_mtd->erasesize;
  319. nettel_intel_partitions[4].size = intel_mtd->erasesize;
  320. nettel_intel_partitions[5].offset =
  321. nettel_intel_partitions[4].offset;
  322. nettel_intel_partitions[5].size =
  323. nettel_intel_partitions[4].size;
  324. } else {
  325. /* No BIOS regions when AMD boot */
  326. num_intel_partitions -= 2;
  327. }
  328. rc = mtd_device_register(intel_mtd, nettel_intel_partitions,
  329. num_intel_partitions);
  330. if (rc)
  331. goto out_map_destroy;
  332. #endif
  333. if (amd_mtd) {
  334. rc = mtd_device_register(amd_mtd, nettel_amd_partitions,
  335. num_amd_partitions);
  336. if (rc)
  337. goto out_mtd_unreg;
  338. }
  339. #ifdef CONFIG_MTD_CFI_INTELEXT
  340. register_reboot_notifier(&nettel_notifier_block);
  341. #endif
  342. return rc;
  343. out_mtd_unreg:
  344. #ifdef CONFIG_MTD_CFI_INTELEXT
  345. mtd_device_unregister(intel_mtd);
  346. out_map_destroy:
  347. map_destroy(intel_mtd);
  348. out_unmap1:
  349. iounmap(nettel_intel_map.virt);
  350. #endif
  351. out_unmap2:
  352. iounmap(nettel_mmcrp);
  353. iounmap(nettel_amd_map.virt);
  354. return rc;
  355. }
  356. /****************************************************************************/
  357. static void __exit nettel_cleanup(void)
  358. {
  359. #ifdef CONFIG_MTD_CFI_INTELEXT
  360. unregister_reboot_notifier(&nettel_notifier_block);
  361. #endif
  362. if (amd_mtd) {
  363. mtd_device_unregister(amd_mtd);
  364. map_destroy(amd_mtd);
  365. }
  366. if (nettel_mmcrp) {
  367. iounmap(nettel_mmcrp);
  368. nettel_mmcrp = NULL;
  369. }
  370. if (nettel_amd_map.virt) {
  371. iounmap(nettel_amd_map.virt);
  372. nettel_amd_map.virt = NULL;
  373. }
  374. #ifdef CONFIG_MTD_CFI_INTELEXT
  375. if (intel_mtd) {
  376. mtd_device_unregister(intel_mtd);
  377. map_destroy(intel_mtd);
  378. }
  379. if (nettel_intel_map.virt) {
  380. iounmap(nettel_intel_map.virt);
  381. nettel_intel_map.virt = NULL;
  382. }
  383. #endif
  384. }
  385. /****************************************************************************/
  386. module_init(nettel_init);
  387. module_exit(nettel_cleanup);
  388. MODULE_LICENSE("GPL");
  389. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>");
  390. MODULE_DESCRIPTION("SnapGear/SecureEdge FLASH support");
  391. /****************************************************************************/