nettel.c 12 KB

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