dilnetpc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* dilnetpc.c -- MTD map driver for SSV DIL/Net PC Boards "DNP" and "ADNP"
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  16. *
  17. * The DIL/Net PC is a tiny embedded PC board made by SSV Embedded Systems
  18. * featuring the AMD Elan SC410 processor. There are two variants of this
  19. * board: DNP/1486 and ADNP/1486. The DNP version has 2 megs of flash
  20. * ROM (Intel 28F016S3) and 8 megs of DRAM, the ADNP version has 4 megs
  21. * flash and 16 megs of RAM.
  22. * For details, see http://www.ssv-embedded.de/ssv/pc104/p169.htm
  23. * and http://www.ssv-embedded.de/ssv/pc104/p170.htm
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/string.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/map.h>
  32. #include <linux/mtd/partitions.h>
  33. #include <linux/mtd/concat.h>
  34. #include <asm/io.h>
  35. /*
  36. ** The DIL/NetPC keeps its BIOS in two distinct flash blocks.
  37. ** Destroying any of these blocks transforms the DNPC into
  38. ** a paperweight (albeit not a very useful one, considering
  39. ** it only weighs a few grams).
  40. **
  41. ** Therefore, the BIOS blocks must never be erased or written to
  42. ** except by people who know exactly what they are doing (e.g.
  43. ** to install a BIOS update). These partitions are marked read-only
  44. ** by default, but can be made read/write by undefining
  45. ** DNPC_BIOS_BLOCKS_WRITEPROTECTED:
  46. */
  47. #define DNPC_BIOS_BLOCKS_WRITEPROTECTED
  48. /*
  49. ** The ID string (in ROM) is checked to determine whether we
  50. ** are running on a DNP/1486 or ADNP/1486
  51. */
  52. #define BIOSID_BASE 0x000fe100
  53. #define ID_DNPC "DNP1486"
  54. #define ID_ADNP "ADNP1486"
  55. /*
  56. ** Address where the flash should appear in CPU space
  57. */
  58. #define FLASH_BASE 0x2000000
  59. /*
  60. ** Chip Setup and Control (CSC) indexed register space
  61. */
  62. #define CSC_INDEX 0x22
  63. #define CSC_DATA 0x23
  64. #define CSC_MMSWAR 0x30 /* MMS window C-F attributes register */
  65. #define CSC_MMSWDSR 0x31 /* MMS window C-F device select register */
  66. #define CSC_RBWR 0xa7 /* GPIO Read-Back/Write Register B */
  67. #define CSC_CR 0xd0 /* internal I/O device disable/Echo */
  68. /* Z-bus/configuration register */
  69. #define CSC_PCCMDCR 0xf1 /* PC card mode and DMA control register */
  70. /*
  71. ** PC Card indexed register space:
  72. */
  73. #define PCC_INDEX 0x3e0
  74. #define PCC_DATA 0x3e1
  75. #define PCC_AWER_B 0x46 /* Socket B Address Window enable register */
  76. #define PCC_MWSAR_1_Lo 0x58 /* memory window 1 start address low register */
  77. #define PCC_MWSAR_1_Hi 0x59 /* memory window 1 start address high register */
  78. #define PCC_MWEAR_1_Lo 0x5A /* memory window 1 stop address low register */
  79. #define PCC_MWEAR_1_Hi 0x5B /* memory window 1 stop address high register */
  80. #define PCC_MWAOR_1_Lo 0x5C /* memory window 1 address offset low register */
  81. #define PCC_MWAOR_1_Hi 0x5D /* memory window 1 address offset high register */
  82. /*
  83. ** Access to SC4x0's Chip Setup and Control (CSC)
  84. ** and PC Card (PCC) indexed registers:
  85. */
  86. static inline void setcsc(int reg, unsigned char data)
  87. {
  88. outb(reg, CSC_INDEX);
  89. outb(data, CSC_DATA);
  90. }
  91. static inline unsigned char getcsc(int reg)
  92. {
  93. outb(reg, CSC_INDEX);
  94. return(inb(CSC_DATA));
  95. }
  96. static inline void setpcc(int reg, unsigned char data)
  97. {
  98. outb(reg, PCC_INDEX);
  99. outb(data, PCC_DATA);
  100. }
  101. static inline unsigned char getpcc(int reg)
  102. {
  103. outb(reg, PCC_INDEX);
  104. return(inb(PCC_DATA));
  105. }
  106. /*
  107. ************************************************************
  108. ** Enable access to DIL/NetPC's flash by mapping it into
  109. ** the SC4x0's MMS Window C.
  110. ************************************************************
  111. */
  112. static void dnpc_map_flash(unsigned long flash_base, unsigned long flash_size)
  113. {
  114. unsigned long flash_end = flash_base + flash_size - 1;
  115. /*
  116. ** enable setup of MMS windows C-F:
  117. */
  118. /* - enable PC Card indexed register space */
  119. setcsc(CSC_CR, getcsc(CSC_CR) | 0x2);
  120. /* - set PC Card controller to operate in standard mode */
  121. setcsc(CSC_PCCMDCR, getcsc(CSC_PCCMDCR) & ~1);
  122. /*
  123. ** Program base address and end address of window
  124. ** where the flash ROM should appear in CPU address space
  125. */
  126. setpcc(PCC_MWSAR_1_Lo, (flash_base >> 12) & 0xff);
  127. setpcc(PCC_MWSAR_1_Hi, (flash_base >> 20) & 0x3f);
  128. setpcc(PCC_MWEAR_1_Lo, (flash_end >> 12) & 0xff);
  129. setpcc(PCC_MWEAR_1_Hi, (flash_end >> 20) & 0x3f);
  130. /* program offset of first flash location to appear in this window (0) */
  131. setpcc(PCC_MWAOR_1_Lo, ((0 - flash_base) >> 12) & 0xff);
  132. setpcc(PCC_MWAOR_1_Hi, ((0 - flash_base)>> 20) & 0x3f);
  133. /* set attributes for MMS window C: non-cacheable, write-enabled */
  134. setcsc(CSC_MMSWAR, getcsc(CSC_MMSWAR) & ~0x11);
  135. /* select physical device ROMCS0 (i.e. flash) for MMS Window C */
  136. setcsc(CSC_MMSWDSR, getcsc(CSC_MMSWDSR) & ~0x03);
  137. /* enable memory window 1 */
  138. setpcc(PCC_AWER_B, getpcc(PCC_AWER_B) | 0x02);
  139. /* now disable PC Card indexed register space again */
  140. setcsc(CSC_CR, getcsc(CSC_CR) & ~0x2);
  141. }
  142. /*
  143. ************************************************************
  144. ** Disable access to DIL/NetPC's flash by mapping it into
  145. ** the SC4x0's MMS Window C.
  146. ************************************************************
  147. */
  148. static void dnpc_unmap_flash(void)
  149. {
  150. /* - enable PC Card indexed register space */
  151. setcsc(CSC_CR, getcsc(CSC_CR) | 0x2);
  152. /* disable memory window 1 */
  153. setpcc(PCC_AWER_B, getpcc(PCC_AWER_B) & ~0x02);
  154. /* now disable PC Card indexed register space again */
  155. setcsc(CSC_CR, getcsc(CSC_CR) & ~0x2);
  156. }
  157. /*
  158. ************************************************************
  159. ** Enable/Disable VPP to write to flash
  160. ************************************************************
  161. */
  162. static DEFINE_SPINLOCK(dnpc_spin);
  163. static int vpp_counter = 0;
  164. /*
  165. ** This is what has to be done for the DNP board ..
  166. */
  167. static void dnp_set_vpp(struct map_info *not_used, int on)
  168. {
  169. spin_lock_irq(&dnpc_spin);
  170. if (on)
  171. {
  172. if(++vpp_counter == 1)
  173. setcsc(CSC_RBWR, getcsc(CSC_RBWR) & ~0x4);
  174. }
  175. else
  176. {
  177. if(--vpp_counter == 0)
  178. setcsc(CSC_RBWR, getcsc(CSC_RBWR) | 0x4);
  179. else
  180. BUG_ON(vpp_counter < 0);
  181. }
  182. spin_unlock_irq(&dnpc_spin);
  183. }
  184. /*
  185. ** .. and this the ADNP version:
  186. */
  187. static void adnp_set_vpp(struct map_info *not_used, int on)
  188. {
  189. spin_lock_irq(&dnpc_spin);
  190. if (on)
  191. {
  192. if(++vpp_counter == 1)
  193. setcsc(CSC_RBWR, getcsc(CSC_RBWR) & ~0x8);
  194. }
  195. else
  196. {
  197. if(--vpp_counter == 0)
  198. setcsc(CSC_RBWR, getcsc(CSC_RBWR) | 0x8);
  199. else
  200. BUG_ON(vpp_counter < 0);
  201. }
  202. spin_unlock_irq(&dnpc_spin);
  203. }
  204. #define DNP_WINDOW_SIZE 0x00200000 /* DNP flash size is 2MiB */
  205. #define ADNP_WINDOW_SIZE 0x00400000 /* ADNP flash size is 4MiB */
  206. #define WINDOW_ADDR FLASH_BASE
  207. static struct map_info dnpc_map = {
  208. .name = "ADNP Flash Bank",
  209. .size = ADNP_WINDOW_SIZE,
  210. .bankwidth = 1,
  211. .set_vpp = adnp_set_vpp,
  212. .phys = WINDOW_ADDR
  213. };
  214. /*
  215. ** The layout of the flash is somewhat "strange":
  216. **
  217. ** 1. 960 KiB (15 blocks) : Space for ROM Bootloader and user data
  218. ** 2. 64 KiB (1 block) : System BIOS
  219. ** 3. 960 KiB (15 blocks) : User Data (DNP model) or
  220. ** 3. 3008 KiB (47 blocks) : User Data (ADNP model)
  221. ** 4. 64 KiB (1 block) : System BIOS Entry
  222. */
  223. static struct mtd_partition partition_info[]=
  224. {
  225. {
  226. .name = "ADNP boot",
  227. .offset = 0,
  228. .size = 0xf0000,
  229. },
  230. {
  231. .name = "ADNP system BIOS",
  232. .offset = MTDPART_OFS_NXTBLK,
  233. .size = 0x10000,
  234. #ifdef DNPC_BIOS_BLOCKS_WRITEPROTECTED
  235. .mask_flags = MTD_WRITEABLE,
  236. #endif
  237. },
  238. {
  239. .name = "ADNP file system",
  240. .offset = MTDPART_OFS_NXTBLK,
  241. .size = 0x2f0000,
  242. },
  243. {
  244. .name = "ADNP system BIOS entry",
  245. .offset = MTDPART_OFS_NXTBLK,
  246. .size = MTDPART_SIZ_FULL,
  247. #ifdef DNPC_BIOS_BLOCKS_WRITEPROTECTED
  248. .mask_flags = MTD_WRITEABLE,
  249. #endif
  250. },
  251. };
  252. #define NUM_PARTITIONS ARRAY_SIZE(partition_info)
  253. static struct mtd_info *mymtd;
  254. static struct mtd_info *lowlvl_parts[NUM_PARTITIONS];
  255. static struct mtd_info *merged_mtd;
  256. /*
  257. ** "Highlevel" partition info:
  258. **
  259. ** Using the MTD concat layer, we can re-arrange partitions to our
  260. ** liking: we construct a virtual MTD device by concatenating the
  261. ** partitions, specifying the sequence such that the boot block
  262. ** is immediately followed by the filesystem block (i.e. the stupid
  263. ** system BIOS block is mapped to a different place). When re-partitioning
  264. ** this concatenated MTD device, we can set the boot block size to
  265. ** an arbitrary (though erase block aligned) value i.e. not one that
  266. ** is dictated by the flash's physical layout. We can thus set the
  267. ** boot block to be e.g. 64 KB (which is fully sufficient if we want
  268. ** to boot an etherboot image) or to -say- 1.5 MB if we want to boot
  269. ** a large kernel image. In all cases, the remainder of the flash
  270. ** is available as file system space.
  271. */
  272. static struct mtd_partition higlvl_partition_info[]=
  273. {
  274. {
  275. .name = "ADNP boot block",
  276. .offset = 0,
  277. .size = CONFIG_MTD_DILNETPC_BOOTSIZE,
  278. },
  279. {
  280. .name = "ADNP file system space",
  281. .offset = MTDPART_OFS_NXTBLK,
  282. .size = ADNP_WINDOW_SIZE-CONFIG_MTD_DILNETPC_BOOTSIZE-0x20000,
  283. },
  284. {
  285. .name = "ADNP system BIOS + BIOS Entry",
  286. .offset = MTDPART_OFS_NXTBLK,
  287. .size = MTDPART_SIZ_FULL,
  288. #ifdef DNPC_BIOS_BLOCKS_WRITEPROTECTED
  289. .mask_flags = MTD_WRITEABLE,
  290. #endif
  291. },
  292. };
  293. #define NUM_HIGHLVL_PARTITIONS ARRAY_SIZE(higlvl_partition_info)
  294. static int dnp_adnp_probe(void)
  295. {
  296. char *biosid, rc = -1;
  297. biosid = (char*)ioremap(BIOSID_BASE, 16);
  298. if(biosid)
  299. {
  300. if(!strcmp(biosid, ID_DNPC))
  301. rc = 1; /* this is a DNPC */
  302. else if(!strcmp(biosid, ID_ADNP))
  303. rc = 0; /* this is a ADNPC */
  304. }
  305. iounmap((void *)biosid);
  306. return(rc);
  307. }
  308. static int __init init_dnpc(void)
  309. {
  310. int is_dnp;
  311. /*
  312. ** determine hardware (DNP/ADNP/invalid)
  313. */
  314. if((is_dnp = dnp_adnp_probe()) < 0)
  315. return -ENXIO;
  316. /*
  317. ** Things are set up for ADNP by default
  318. ** -> modify all that needs to be different for DNP
  319. */
  320. if(is_dnp)
  321. { /*
  322. ** Adjust window size, select correct set_vpp function.
  323. ** The partitioning scheme is identical on both DNP
  324. ** and ADNP except for the size of the third partition.
  325. */
  326. int i;
  327. dnpc_map.size = DNP_WINDOW_SIZE;
  328. dnpc_map.set_vpp = dnp_set_vpp;
  329. partition_info[2].size = 0xf0000;
  330. /*
  331. ** increment all string pointers so the leading 'A' gets skipped,
  332. ** thus turning all occurrences of "ADNP ..." into "DNP ..."
  333. */
  334. ++dnpc_map.name;
  335. for(i = 0; i < NUM_PARTITIONS; i++)
  336. ++partition_info[i].name;
  337. higlvl_partition_info[1].size = DNP_WINDOW_SIZE -
  338. CONFIG_MTD_DILNETPC_BOOTSIZE - 0x20000;
  339. for(i = 0; i < NUM_HIGHLVL_PARTITIONS; i++)
  340. ++higlvl_partition_info[i].name;
  341. }
  342. printk(KERN_NOTICE "DIL/Net %s flash: 0x%lx at 0x%llx\n",
  343. is_dnp ? "DNPC" : "ADNP", dnpc_map.size, (unsigned long long)dnpc_map.phys);
  344. dnpc_map.virt = ioremap_nocache(dnpc_map.phys, dnpc_map.size);
  345. dnpc_map_flash(dnpc_map.phys, dnpc_map.size);
  346. if (!dnpc_map.virt) {
  347. printk("Failed to ioremap_nocache\n");
  348. return -EIO;
  349. }
  350. simple_map_init(&dnpc_map);
  351. printk("FLASH virtual address: 0x%p\n", dnpc_map.virt);
  352. mymtd = do_map_probe("jedec_probe", &dnpc_map);
  353. if (!mymtd)
  354. mymtd = do_map_probe("cfi_probe", &dnpc_map);
  355. /*
  356. ** If flash probes fail, try to make flashes accessible
  357. ** at least as ROM. Ajust erasesize in this case since
  358. ** the default one (128M) will break our partitioning
  359. */
  360. if (!mymtd)
  361. if((mymtd = do_map_probe("map_rom", &dnpc_map)))
  362. mymtd->erasesize = 0x10000;
  363. if (!mymtd) {
  364. iounmap(dnpc_map.virt);
  365. return -ENXIO;
  366. }
  367. mymtd->owner = THIS_MODULE;
  368. /*
  369. ** Supply pointers to lowlvl_parts[] array to add_mtd_partitions()
  370. ** -> add_mtd_partitions() will _not_ register MTD devices for
  371. ** the partitions, but will instead store pointers to the MTD
  372. ** objects it creates into our lowlvl_parts[] array.
  373. ** NOTE: we arrange the pointers such that the sequence of the
  374. ** partitions gets re-arranged: partition #2 follows
  375. ** partition #0.
  376. */
  377. partition_info[0].mtdp = &lowlvl_parts[0];
  378. partition_info[1].mtdp = &lowlvl_parts[2];
  379. partition_info[2].mtdp = &lowlvl_parts[1];
  380. partition_info[3].mtdp = &lowlvl_parts[3];
  381. mtd_device_register(mymtd, partition_info, NUM_PARTITIONS);
  382. /*
  383. ** now create a virtual MTD device by concatenating the for partitions
  384. ** (in the sequence given by the lowlvl_parts[] array.
  385. */
  386. merged_mtd = mtd_concat_create(lowlvl_parts, NUM_PARTITIONS, "(A)DNP Flash Concatenated");
  387. if(merged_mtd)
  388. { /*
  389. ** now partition the new device the way we want it. This time,
  390. ** we do not supply mtd pointers in higlvl_partition_info, so
  391. ** add_mtd_partitions() will register the devices.
  392. */
  393. mtd_device_register(merged_mtd, higlvl_partition_info,
  394. NUM_HIGHLVL_PARTITIONS);
  395. }
  396. return 0;
  397. }
  398. static void __exit cleanup_dnpc(void)
  399. {
  400. if(merged_mtd) {
  401. mtd_device_unregister(merged_mtd);
  402. mtd_concat_destroy(merged_mtd);
  403. }
  404. if (mymtd) {
  405. mtd_device_unregister(mymtd);
  406. map_destroy(mymtd);
  407. }
  408. if (dnpc_map.virt) {
  409. iounmap(dnpc_map.virt);
  410. dnpc_unmap_flash();
  411. dnpc_map.virt = NULL;
  412. }
  413. }
  414. module_init(init_dnpc);
  415. module_exit(cleanup_dnpc);
  416. MODULE_LICENSE("GPL");
  417. MODULE_AUTHOR("Sysgo Real-Time Solutions GmbH");
  418. MODULE_DESCRIPTION("MTD map driver for SSV DIL/NetPC DNP & ADNP");