nettel.c 13 KB

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