pmc551.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * $Id: pmc551.c,v 1.32 2005/11/07 11:14:25 gleixner Exp $
  3. *
  4. * PMC551 PCI Mezzanine Ram Device
  5. *
  6. * Author:
  7. * Mark Ferrell <mferrell@mvista.com>
  8. * Copyright 1999,2000 Nortel Networks
  9. *
  10. * License:
  11. * As part of this driver was derived from the slram.c driver it
  12. * falls under the same license, which is GNU General Public
  13. * License v2
  14. *
  15. * Description:
  16. * This driver is intended to support the PMC551 PCI Ram device
  17. * from Ramix Inc. The PMC551 is a PMC Mezzanine module for
  18. * cPCI embedded systems. The device contains a single SROM
  19. * that initially programs the V370PDC chipset onboard the
  20. * device, and various banks of DRAM/SDRAM onboard. This driver
  21. * implements this PCI Ram device as an MTD (Memory Technology
  22. * Device) so that it can be used to hold a file system, or for
  23. * added swap space in embedded systems. Since the memory on
  24. * this board isn't as fast as main memory we do not try to hook
  25. * it into main memory as that would simply reduce performance
  26. * on the system. Using it as a block device allows us to use
  27. * it as high speed swap or for a high speed disk device of some
  28. * sort. Which becomes very useful on diskless systems in the
  29. * embedded market I might add.
  30. *
  31. * Notes:
  32. * Due to what I assume is more buggy SROM, the 64M PMC551 I
  33. * have available claims that all 4 of its DRAM banks have 64MiB
  34. * of ram configured (making a grand total of 256MiB onboard).
  35. * This is slightly annoying since the BAR0 size reflects the
  36. * aperture size, not the dram size, and the V370PDC supplies no
  37. * other method for memory size discovery. This problem is
  38. * mostly only relevant when compiled as a module, as the
  39. * unloading of the module with an aperture size smaller then
  40. * the ram will cause the driver to detect the onboard memory
  41. * size to be equal to the aperture size when the module is
  42. * reloaded. Soooo, to help, the module supports an msize
  43. * option to allow the specification of the onboard memory, and
  44. * an asize option, to allow the specification of the aperture
  45. * size. The aperture must be equal to or less then the memory
  46. * size, the driver will correct this if you screw it up. This
  47. * problem is not relevant for compiled in drivers as compiled
  48. * in drivers only init once.
  49. *
  50. * Credits:
  51. * Saeed Karamooz <saeed@ramix.com> of Ramix INC. for the
  52. * initial example code of how to initialize this device and for
  53. * help with questions I had concerning operation of the device.
  54. *
  55. * Most of the MTD code for this driver was originally written
  56. * for the slram.o module in the MTD drivers package which
  57. * allows the mapping of system memory into an MTD device.
  58. * Since the PMC551 memory module is accessed in the same
  59. * fashion as system memory, the slram.c code became a very nice
  60. * fit to the needs of this driver. All we added was PCI
  61. * detection/initialization to the driver and automatically figure
  62. * out the size via the PCI detection.o, later changes by Corey
  63. * Minyard set up the card to utilize a 1M sliding apature.
  64. *
  65. * Corey Minyard <minyard@nortelnetworks.com>
  66. * * Modified driver to utilize a sliding aperture instead of
  67. * mapping all memory into kernel space which turned out to
  68. * be very wasteful.
  69. * * Located a bug in the SROM's initialization sequence that
  70. * made the memory unusable, added a fix to code to touch up
  71. * the DRAM some.
  72. *
  73. * Bugs/FIXMEs:
  74. * * MUST fix the init function to not spin on a register
  75. * waiting for it to set .. this does not safely handle busted
  76. * devices that never reset the register correctly which will
  77. * cause the system to hang w/ a reboot being the only chance at
  78. * recover. [sort of fixed, could be better]
  79. * * Add I2C handling of the SROM so we can read the SROM's information
  80. * about the aperture size. This should always accurately reflect the
  81. * onboard memory size.
  82. * * Comb the init routine. It's still a bit cludgy on a few things.
  83. */
  84. #include <linux/kernel.h>
  85. #include <linux/module.h>
  86. #include <asm/uaccess.h>
  87. #include <linux/types.h>
  88. #include <linux/init.h>
  89. #include <linux/ptrace.h>
  90. #include <linux/slab.h>
  91. #include <linux/string.h>
  92. #include <linux/timer.h>
  93. #include <linux/major.h>
  94. #include <linux/fs.h>
  95. #include <linux/ioctl.h>
  96. #include <asm/io.h>
  97. #include <asm/system.h>
  98. #include <linux/pci.h>
  99. #include <linux/mtd/mtd.h>
  100. #include <linux/mtd/pmc551.h>
  101. #include <linux/mtd/compatmac.h>
  102. static struct mtd_info *pmc551list;
  103. static int pmc551_erase(struct mtd_info *mtd, struct erase_info *instr)
  104. {
  105. struct mypriv *priv = mtd->priv;
  106. u32 soff_hi, soff_lo; /* start address offset hi/lo */
  107. u32 eoff_hi, eoff_lo; /* end address offset hi/lo */
  108. unsigned long end;
  109. u_char *ptr;
  110. size_t retlen;
  111. #ifdef CONFIG_MTD_PMC551_DEBUG
  112. printk(KERN_DEBUG "pmc551_erase(pos:%ld, len:%ld)\n", (long)instr->addr,
  113. (long)instr->len);
  114. #endif
  115. end = instr->addr + instr->len - 1;
  116. /* Is it past the end? */
  117. if (end > mtd->size) {
  118. #ifdef CONFIG_MTD_PMC551_DEBUG
  119. printk(KERN_DEBUG "pmc551_erase() out of bounds (%ld > %ld)\n",
  120. (long)end, (long)mtd->size);
  121. #endif
  122. return -EINVAL;
  123. }
  124. eoff_hi = end & ~(priv->asize - 1);
  125. soff_hi = instr->addr & ~(priv->asize - 1);
  126. eoff_lo = end & (priv->asize - 1);
  127. soff_lo = instr->addr & (priv->asize - 1);
  128. pmc551_point(mtd, instr->addr, instr->len, &retlen,
  129. (void **)&ptr, NULL);
  130. if (soff_hi == eoff_hi || mtd->size == priv->asize) {
  131. /* The whole thing fits within one access, so just one shot
  132. will do it. */
  133. memset(ptr, 0xff, instr->len);
  134. } else {
  135. /* We have to do multiple writes to get all the data
  136. written. */
  137. while (soff_hi != eoff_hi) {
  138. #ifdef CONFIG_MTD_PMC551_DEBUG
  139. printk(KERN_DEBUG "pmc551_erase() soff_hi: %ld, "
  140. "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
  141. #endif
  142. memset(ptr, 0xff, priv->asize);
  143. if (soff_hi + priv->asize >= mtd->size) {
  144. goto out;
  145. }
  146. soff_hi += priv->asize;
  147. pmc551_point(mtd, (priv->base_map0 | soff_hi),
  148. priv->asize, &retlen,
  149. (void **)&ptr, NULL);
  150. }
  151. memset(ptr, 0xff, eoff_lo);
  152. }
  153. out:
  154. instr->state = MTD_ERASE_DONE;
  155. #ifdef CONFIG_MTD_PMC551_DEBUG
  156. printk(KERN_DEBUG "pmc551_erase() done\n");
  157. #endif
  158. mtd_erase_callback(instr);
  159. return 0;
  160. }
  161. static int pmc551_point(struct mtd_info *mtd, loff_t from, size_t len,
  162. size_t *retlen, void **virt, resource_size_t *phys)
  163. {
  164. struct mypriv *priv = mtd->priv;
  165. u32 soff_hi;
  166. u32 soff_lo;
  167. #ifdef CONFIG_MTD_PMC551_DEBUG
  168. printk(KERN_DEBUG "pmc551_point(%ld, %ld)\n", (long)from, (long)len);
  169. #endif
  170. if (from + len > mtd->size) {
  171. #ifdef CONFIG_MTD_PMC551_DEBUG
  172. printk(KERN_DEBUG "pmc551_point() out of bounds (%ld > %ld)\n",
  173. (long)from + len, (long)mtd->size);
  174. #endif
  175. return -EINVAL;
  176. }
  177. /* can we return a physical address with this driver? */
  178. if (phys)
  179. return -EINVAL;
  180. soff_hi = from & ~(priv->asize - 1);
  181. soff_lo = from & (priv->asize - 1);
  182. /* Cheap hack optimization */
  183. if (priv->curr_map0 != from) {
  184. pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
  185. (priv->base_map0 | soff_hi));
  186. priv->curr_map0 = soff_hi;
  187. }
  188. *virt = priv->start + soff_lo;
  189. *retlen = len;
  190. return 0;
  191. }
  192. static void pmc551_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  193. {
  194. #ifdef CONFIG_MTD_PMC551_DEBUG
  195. printk(KERN_DEBUG "pmc551_unpoint()\n");
  196. #endif
  197. }
  198. static int pmc551_read(struct mtd_info *mtd, loff_t from, size_t len,
  199. size_t * retlen, u_char * buf)
  200. {
  201. struct mypriv *priv = mtd->priv;
  202. u32 soff_hi, soff_lo; /* start address offset hi/lo */
  203. u32 eoff_hi, eoff_lo; /* end address offset hi/lo */
  204. unsigned long end;
  205. u_char *ptr;
  206. u_char *copyto = buf;
  207. #ifdef CONFIG_MTD_PMC551_DEBUG
  208. printk(KERN_DEBUG "pmc551_read(pos:%ld, len:%ld) asize: %ld\n",
  209. (long)from, (long)len, (long)priv->asize);
  210. #endif
  211. end = from + len - 1;
  212. /* Is it past the end? */
  213. if (end > mtd->size) {
  214. #ifdef CONFIG_MTD_PMC551_DEBUG
  215. printk(KERN_DEBUG "pmc551_read() out of bounds (%ld > %ld)\n",
  216. (long)end, (long)mtd->size);
  217. #endif
  218. return -EINVAL;
  219. }
  220. soff_hi = from & ~(priv->asize - 1);
  221. eoff_hi = end & ~(priv->asize - 1);
  222. soff_lo = from & (priv->asize - 1);
  223. eoff_lo = end & (priv->asize - 1);
  224. pmc551_point(mtd, from, len, retlen, (void **)&ptr, NULL);
  225. if (soff_hi == eoff_hi) {
  226. /* The whole thing fits within one access, so just one shot
  227. will do it. */
  228. memcpy(copyto, ptr, len);
  229. copyto += len;
  230. } else {
  231. /* We have to do multiple writes to get all the data
  232. written. */
  233. while (soff_hi != eoff_hi) {
  234. #ifdef CONFIG_MTD_PMC551_DEBUG
  235. printk(KERN_DEBUG "pmc551_read() soff_hi: %ld, "
  236. "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
  237. #endif
  238. memcpy(copyto, ptr, priv->asize);
  239. copyto += priv->asize;
  240. if (soff_hi + priv->asize >= mtd->size) {
  241. goto out;
  242. }
  243. soff_hi += priv->asize;
  244. pmc551_point(mtd, soff_hi, priv->asize, retlen,
  245. (void **)&ptr, NULL);
  246. }
  247. memcpy(copyto, ptr, eoff_lo);
  248. copyto += eoff_lo;
  249. }
  250. out:
  251. #ifdef CONFIG_MTD_PMC551_DEBUG
  252. printk(KERN_DEBUG "pmc551_read() done\n");
  253. #endif
  254. *retlen = copyto - buf;
  255. return 0;
  256. }
  257. static int pmc551_write(struct mtd_info *mtd, loff_t to, size_t len,
  258. size_t * retlen, const u_char * buf)
  259. {
  260. struct mypriv *priv = mtd->priv;
  261. u32 soff_hi, soff_lo; /* start address offset hi/lo */
  262. u32 eoff_hi, eoff_lo; /* end address offset hi/lo */
  263. unsigned long end;
  264. u_char *ptr;
  265. const u_char *copyfrom = buf;
  266. #ifdef CONFIG_MTD_PMC551_DEBUG
  267. printk(KERN_DEBUG "pmc551_write(pos:%ld, len:%ld) asize:%ld\n",
  268. (long)to, (long)len, (long)priv->asize);
  269. #endif
  270. end = to + len - 1;
  271. /* Is it past the end? or did the u32 wrap? */
  272. if (end > mtd->size) {
  273. #ifdef CONFIG_MTD_PMC551_DEBUG
  274. printk(KERN_DEBUG "pmc551_write() out of bounds (end: %ld, "
  275. "size: %ld, to: %ld)\n", (long)end, (long)mtd->size,
  276. (long)to);
  277. #endif
  278. return -EINVAL;
  279. }
  280. soff_hi = to & ~(priv->asize - 1);
  281. eoff_hi = end & ~(priv->asize - 1);
  282. soff_lo = to & (priv->asize - 1);
  283. eoff_lo = end & (priv->asize - 1);
  284. pmc551_point(mtd, to, len, retlen, (void **)&ptr, NULL);
  285. if (soff_hi == eoff_hi) {
  286. /* The whole thing fits within one access, so just one shot
  287. will do it. */
  288. memcpy(ptr, copyfrom, len);
  289. copyfrom += len;
  290. } else {
  291. /* We have to do multiple writes to get all the data
  292. written. */
  293. while (soff_hi != eoff_hi) {
  294. #ifdef CONFIG_MTD_PMC551_DEBUG
  295. printk(KERN_DEBUG "pmc551_write() soff_hi: %ld, "
  296. "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
  297. #endif
  298. memcpy(ptr, copyfrom, priv->asize);
  299. copyfrom += priv->asize;
  300. if (soff_hi >= mtd->size) {
  301. goto out;
  302. }
  303. soff_hi += priv->asize;
  304. pmc551_point(mtd, soff_hi, priv->asize, retlen,
  305. (void **)&ptr, NULL);
  306. }
  307. memcpy(ptr, copyfrom, eoff_lo);
  308. copyfrom += eoff_lo;
  309. }
  310. out:
  311. #ifdef CONFIG_MTD_PMC551_DEBUG
  312. printk(KERN_DEBUG "pmc551_write() done\n");
  313. #endif
  314. *retlen = copyfrom - buf;
  315. return 0;
  316. }
  317. /*
  318. * Fixup routines for the V370PDC
  319. * PCI device ID 0x020011b0
  320. *
  321. * This function basicly kick starts the DRAM oboard the card and gets it
  322. * ready to be used. Before this is done the device reads VERY erratic, so
  323. * much that it can crash the Linux 2.2.x series kernels when a user cat's
  324. * /proc/pci .. though that is mainly a kernel bug in handling the PCI DEVSEL
  325. * register. FIXME: stop spinning on registers .. must implement a timeout
  326. * mechanism
  327. * returns the size of the memory region found.
  328. */
  329. static u32 fixup_pmc551(struct pci_dev *dev)
  330. {
  331. #ifdef CONFIG_MTD_PMC551_BUGFIX
  332. u32 dram_data;
  333. #endif
  334. u32 size, dcmd, cfg, dtmp;
  335. u16 cmd, tmp, i;
  336. u8 bcmd, counter;
  337. /* Sanity Check */
  338. if (!dev) {
  339. return -ENODEV;
  340. }
  341. /*
  342. * Attempt to reset the card
  343. * FIXME: Stop Spinning registers
  344. */
  345. counter = 0;
  346. /* unlock registers */
  347. pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, 0xA5);
  348. /* read in old data */
  349. pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
  350. /* bang the reset line up and down for a few */
  351. for (i = 0; i < 10; i++) {
  352. counter = 0;
  353. bcmd &= ~0x80;
  354. while (counter++ < 100) {
  355. pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
  356. }
  357. counter = 0;
  358. bcmd |= 0x80;
  359. while (counter++ < 100) {
  360. pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
  361. }
  362. }
  363. bcmd |= (0x40 | 0x20);
  364. pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
  365. /*
  366. * Take care and turn off the memory on the device while we
  367. * tweak the configurations
  368. */
  369. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  370. tmp = cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  371. pci_write_config_word(dev, PCI_COMMAND, tmp);
  372. /*
  373. * Disable existing aperture before probing memory size
  374. */
  375. pci_read_config_dword(dev, PMC551_PCI_MEM_MAP0, &dcmd);
  376. dtmp = (dcmd | PMC551_PCI_MEM_MAP_ENABLE | PMC551_PCI_MEM_MAP_REG_EN);
  377. pci_write_config_dword(dev, PMC551_PCI_MEM_MAP0, dtmp);
  378. /*
  379. * Grab old BAR0 config so that we can figure out memory size
  380. * This is another bit of kludge going on. The reason for the
  381. * redundancy is I am hoping to retain the original configuration
  382. * previously assigned to the card by the BIOS or some previous
  383. * fixup routine in the kernel. So we read the old config into cfg,
  384. * then write all 1's to the memory space, read back the result into
  385. * "size", and then write back all the old config.
  386. */
  387. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &cfg);
  388. #ifndef CONFIG_MTD_PMC551_BUGFIX
  389. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, ~0);
  390. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &size);
  391. size = (size & PCI_BASE_ADDRESS_MEM_MASK);
  392. size &= ~(size - 1);
  393. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, cfg);
  394. #else
  395. /*
  396. * Get the size of the memory by reading all the DRAM size values
  397. * and adding them up.
  398. *
  399. * KLUDGE ALERT: the boards we are using have invalid column and
  400. * row mux values. We fix them here, but this will break other
  401. * memory configurations.
  402. */
  403. pci_read_config_dword(dev, PMC551_DRAM_BLK0, &dram_data);
  404. size = PMC551_DRAM_BLK_GET_SIZE(dram_data);
  405. dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  406. dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  407. pci_write_config_dword(dev, PMC551_DRAM_BLK0, dram_data);
  408. pci_read_config_dword(dev, PMC551_DRAM_BLK1, &dram_data);
  409. size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
  410. dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  411. dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  412. pci_write_config_dword(dev, PMC551_DRAM_BLK1, dram_data);
  413. pci_read_config_dword(dev, PMC551_DRAM_BLK2, &dram_data);
  414. size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
  415. dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  416. dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  417. pci_write_config_dword(dev, PMC551_DRAM_BLK2, dram_data);
  418. pci_read_config_dword(dev, PMC551_DRAM_BLK3, &dram_data);
  419. size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
  420. dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  421. dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  422. pci_write_config_dword(dev, PMC551_DRAM_BLK3, dram_data);
  423. /*
  424. * Oops .. something went wrong
  425. */
  426. if ((size &= PCI_BASE_ADDRESS_MEM_MASK) == 0) {
  427. return -ENODEV;
  428. }
  429. #endif /* CONFIG_MTD_PMC551_BUGFIX */
  430. if ((cfg & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
  431. return -ENODEV;
  432. }
  433. /*
  434. * Precharge Dram
  435. */
  436. pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0400);
  437. pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x00bf);
  438. /*
  439. * Wait until command has gone through
  440. * FIXME: register spinning issue
  441. */
  442. do {
  443. pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
  444. if (counter++ > 100)
  445. break;
  446. } while ((PCI_COMMAND_IO) & cmd);
  447. /*
  448. * Turn on auto refresh
  449. * The loop is taken directly from Ramix's example code. I assume that
  450. * this must be held high for some duration of time, but I can find no
  451. * documentation refrencing the reasons why.
  452. */
  453. for (i = 1; i <= 8; i++) {
  454. pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0df);
  455. /*
  456. * Make certain command has gone through
  457. * FIXME: register spinning issue
  458. */
  459. counter = 0;
  460. do {
  461. pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
  462. if (counter++ > 100)
  463. break;
  464. } while ((PCI_COMMAND_IO) & cmd);
  465. }
  466. pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0020);
  467. pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0ff);
  468. /*
  469. * Wait until command completes
  470. * FIXME: register spinning issue
  471. */
  472. counter = 0;
  473. do {
  474. pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
  475. if (counter++ > 100)
  476. break;
  477. } while ((PCI_COMMAND_IO) & cmd);
  478. pci_read_config_dword(dev, PMC551_DRAM_CFG, &dcmd);
  479. dcmd |= 0x02000000;
  480. pci_write_config_dword(dev, PMC551_DRAM_CFG, dcmd);
  481. /*
  482. * Check to make certain fast back-to-back, if not
  483. * then set it so
  484. */
  485. pci_read_config_word(dev, PCI_STATUS, &cmd);
  486. if ((cmd & PCI_COMMAND_FAST_BACK) == 0) {
  487. cmd |= PCI_COMMAND_FAST_BACK;
  488. pci_write_config_word(dev, PCI_STATUS, cmd);
  489. }
  490. /*
  491. * Check to make certain the DEVSEL is set correctly, this device
  492. * has a tendancy to assert DEVSEL and TRDY when a write is performed
  493. * to the memory when memory is read-only
  494. */
  495. if ((cmd & PCI_STATUS_DEVSEL_MASK) != 0x0) {
  496. cmd &= ~PCI_STATUS_DEVSEL_MASK;
  497. pci_write_config_word(dev, PCI_STATUS, cmd);
  498. }
  499. /*
  500. * Set to be prefetchable and put everything back based on old cfg.
  501. * it's possible that the reset of the V370PDC nuked the original
  502. * setup
  503. */
  504. /*
  505. cfg |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  506. pci_write_config_dword( dev, PCI_BASE_ADDRESS_0, cfg );
  507. */
  508. /*
  509. * Turn PCI memory and I/O bus access back on
  510. */
  511. pci_write_config_word(dev, PCI_COMMAND,
  512. PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
  513. #ifdef CONFIG_MTD_PMC551_DEBUG
  514. /*
  515. * Some screen fun
  516. */
  517. printk(KERN_DEBUG "pmc551: %d%sB (0x%x) of %sprefetchable memory at "
  518. "0x%llx\n", (size < 1024) ? size : (size < 1048576) ?
  519. size >> 10 : size >> 20,
  520. (size < 1024) ? "" : (size < 1048576) ? "Ki" : "Mi", size,
  521. ((dcmd & (0x1 << 3)) == 0) ? "non-" : "",
  522. (unsigned long long)pci_resource_start(dev, 0));
  523. /*
  524. * Check to see the state of the memory
  525. */
  526. pci_read_config_dword(dev, PMC551_DRAM_BLK0, &dcmd);
  527. printk(KERN_DEBUG "pmc551: DRAM_BLK0 Flags: %s,%s\n"
  528. "pmc551: DRAM_BLK0 Size: %d at %d\n"
  529. "pmc551: DRAM_BLK0 Row MUX: %d, Col MUX: %d\n",
  530. (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  531. (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  532. PMC551_DRAM_BLK_GET_SIZE(dcmd),
  533. ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  534. ((dcmd >> 9) & 0xF));
  535. pci_read_config_dword(dev, PMC551_DRAM_BLK1, &dcmd);
  536. printk(KERN_DEBUG "pmc551: DRAM_BLK1 Flags: %s,%s\n"
  537. "pmc551: DRAM_BLK1 Size: %d at %d\n"
  538. "pmc551: DRAM_BLK1 Row MUX: %d, Col MUX: %d\n",
  539. (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  540. (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  541. PMC551_DRAM_BLK_GET_SIZE(dcmd),
  542. ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  543. ((dcmd >> 9) & 0xF));
  544. pci_read_config_dword(dev, PMC551_DRAM_BLK2, &dcmd);
  545. printk(KERN_DEBUG "pmc551: DRAM_BLK2 Flags: %s,%s\n"
  546. "pmc551: DRAM_BLK2 Size: %d at %d\n"
  547. "pmc551: DRAM_BLK2 Row MUX: %d, Col MUX: %d\n",
  548. (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  549. (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  550. PMC551_DRAM_BLK_GET_SIZE(dcmd),
  551. ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  552. ((dcmd >> 9) & 0xF));
  553. pci_read_config_dword(dev, PMC551_DRAM_BLK3, &dcmd);
  554. printk(KERN_DEBUG "pmc551: DRAM_BLK3 Flags: %s,%s\n"
  555. "pmc551: DRAM_BLK3 Size: %d at %d\n"
  556. "pmc551: DRAM_BLK3 Row MUX: %d, Col MUX: %d\n",
  557. (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  558. (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  559. PMC551_DRAM_BLK_GET_SIZE(dcmd),
  560. ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  561. ((dcmd >> 9) & 0xF));
  562. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  563. printk(KERN_DEBUG "pmc551: Memory Access %s\n",
  564. (((0x1 << 1) & cmd) == 0) ? "off" : "on");
  565. printk(KERN_DEBUG "pmc551: I/O Access %s\n",
  566. (((0x1 << 0) & cmd) == 0) ? "off" : "on");
  567. pci_read_config_word(dev, PCI_STATUS, &cmd);
  568. printk(KERN_DEBUG "pmc551: Devsel %s\n",
  569. ((PCI_STATUS_DEVSEL_MASK & cmd) == 0x000) ? "Fast" :
  570. ((PCI_STATUS_DEVSEL_MASK & cmd) == 0x200) ? "Medium" :
  571. ((PCI_STATUS_DEVSEL_MASK & cmd) == 0x400) ? "Slow" : "Invalid");
  572. printk(KERN_DEBUG "pmc551: %sFast Back-to-Back\n",
  573. ((PCI_COMMAND_FAST_BACK & cmd) == 0) ? "Not " : "");
  574. pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
  575. printk(KERN_DEBUG "pmc551: EEPROM is under %s control\n"
  576. "pmc551: System Control Register is %slocked to PCI access\n"
  577. "pmc551: System Control Register is %slocked to EEPROM access\n",
  578. (bcmd & 0x1) ? "software" : "hardware",
  579. (bcmd & 0x20) ? "" : "un", (bcmd & 0x40) ? "" : "un");
  580. #endif
  581. return size;
  582. }
  583. /*
  584. * Kernel version specific module stuffages
  585. */
  586. MODULE_LICENSE("GPL");
  587. MODULE_AUTHOR("Mark Ferrell <mferrell@mvista.com>");
  588. MODULE_DESCRIPTION(PMC551_VERSION);
  589. /*
  590. * Stuff these outside the ifdef so as to not bust compiled in driver support
  591. */
  592. static int msize = 0;
  593. static int asize = 0;
  594. module_param(msize, int, 0);
  595. MODULE_PARM_DESC(msize, "memory size in MiB [1 - 1024]");
  596. module_param(asize, int, 0);
  597. MODULE_PARM_DESC(asize, "aperture size, must be <= memsize [1-1024]");
  598. /*
  599. * PMC551 Card Initialization
  600. */
  601. static int __init init_pmc551(void)
  602. {
  603. struct pci_dev *PCI_Device = NULL;
  604. struct mypriv *priv;
  605. int count, found = 0;
  606. struct mtd_info *mtd;
  607. u32 length = 0;
  608. if (msize) {
  609. msize = (1 << (ffs(msize) - 1)) << 20;
  610. if (msize > (1 << 30)) {
  611. printk(KERN_NOTICE "pmc551: Invalid memory size [%d]\n",
  612. msize);
  613. return -EINVAL;
  614. }
  615. }
  616. if (asize) {
  617. asize = (1 << (ffs(asize) - 1)) << 20;
  618. if (asize > (1 << 30)) {
  619. printk(KERN_NOTICE "pmc551: Invalid aperture size "
  620. "[%d]\n", asize);
  621. return -EINVAL;
  622. }
  623. }
  624. printk(KERN_INFO PMC551_VERSION);
  625. /*
  626. * PCU-bus chipset probe.
  627. */
  628. for (count = 0; count < MAX_MTD_DEVICES; count++) {
  629. if ((PCI_Device = pci_get_device(PCI_VENDOR_ID_V3_SEMI,
  630. PCI_DEVICE_ID_V3_SEMI_V370PDC,
  631. PCI_Device)) == NULL) {
  632. break;
  633. }
  634. printk(KERN_NOTICE "pmc551: Found PCI V370PDC at 0x%llx\n",
  635. (unsigned long long)pci_resource_start(PCI_Device, 0));
  636. /*
  637. * The PMC551 device acts VERY weird if you don't init it
  638. * first. i.e. it will not correctly report devsel. If for
  639. * some reason the sdram is in a wrote-protected state the
  640. * device will DEVSEL when it is written to causing problems
  641. * with the oldproc.c driver in
  642. * some kernels (2.2.*)
  643. */
  644. if ((length = fixup_pmc551(PCI_Device)) <= 0) {
  645. printk(KERN_NOTICE "pmc551: Cannot init SDRAM\n");
  646. break;
  647. }
  648. /*
  649. * This is needed until the driver is capable of reading the
  650. * onboard I2C SROM to discover the "real" memory size.
  651. */
  652. if (msize) {
  653. length = msize;
  654. printk(KERN_NOTICE "pmc551: Using specified memory "
  655. "size 0x%x\n", length);
  656. } else {
  657. msize = length;
  658. }
  659. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  660. if (!mtd) {
  661. printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
  662. "device.\n");
  663. break;
  664. }
  665. priv = kzalloc(sizeof(struct mypriv), GFP_KERNEL);
  666. if (!priv) {
  667. printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
  668. "device.\n");
  669. kfree(mtd);
  670. break;
  671. }
  672. mtd->priv = priv;
  673. priv->dev = PCI_Device;
  674. if (asize > length) {
  675. printk(KERN_NOTICE "pmc551: reducing aperture size to "
  676. "fit %dM\n", length >> 20);
  677. priv->asize = asize = length;
  678. } else if (asize == 0 || asize == length) {
  679. printk(KERN_NOTICE "pmc551: Using existing aperture "
  680. "size %dM\n", length >> 20);
  681. priv->asize = asize = length;
  682. } else {
  683. printk(KERN_NOTICE "pmc551: Using specified aperture "
  684. "size %dM\n", asize >> 20);
  685. priv->asize = asize;
  686. }
  687. priv->start = pci_iomap(PCI_Device, 0, priv->asize);
  688. if (!priv->start) {
  689. printk(KERN_NOTICE "pmc551: Unable to map IO space\n");
  690. kfree(mtd->priv);
  691. kfree(mtd);
  692. break;
  693. }
  694. #ifdef CONFIG_MTD_PMC551_DEBUG
  695. printk(KERN_DEBUG "pmc551: setting aperture to %d\n",
  696. ffs(priv->asize >> 20) - 1);
  697. #endif
  698. priv->base_map0 = (PMC551_PCI_MEM_MAP_REG_EN
  699. | PMC551_PCI_MEM_MAP_ENABLE
  700. | (ffs(priv->asize >> 20) - 1) << 4);
  701. priv->curr_map0 = priv->base_map0;
  702. pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
  703. priv->curr_map0);
  704. #ifdef CONFIG_MTD_PMC551_DEBUG
  705. printk(KERN_DEBUG "pmc551: aperture set to %d\n",
  706. (priv->base_map0 & 0xF0) >> 4);
  707. #endif
  708. mtd->size = msize;
  709. mtd->flags = MTD_CAP_RAM;
  710. mtd->erase = pmc551_erase;
  711. mtd->read = pmc551_read;
  712. mtd->write = pmc551_write;
  713. mtd->point = pmc551_point;
  714. mtd->unpoint = pmc551_unpoint;
  715. mtd->type = MTD_RAM;
  716. mtd->name = "PMC551 RAM board";
  717. mtd->erasesize = 0x10000;
  718. mtd->writesize = 1;
  719. mtd->owner = THIS_MODULE;
  720. if (add_mtd_device(mtd)) {
  721. printk(KERN_NOTICE "pmc551: Failed to register new device\n");
  722. pci_iounmap(PCI_Device, priv->start);
  723. kfree(mtd->priv);
  724. kfree(mtd);
  725. break;
  726. }
  727. /* Keep a reference as the add_mtd_device worked */
  728. pci_dev_get(PCI_Device);
  729. printk(KERN_NOTICE "Registered pmc551 memory device.\n");
  730. printk(KERN_NOTICE "Mapped %dMiB of memory from 0x%p to 0x%p\n",
  731. priv->asize >> 20,
  732. priv->start, priv->start + priv->asize);
  733. printk(KERN_NOTICE "Total memory is %d%sB\n",
  734. (length < 1024) ? length :
  735. (length < 1048576) ? length >> 10 : length >> 20,
  736. (length < 1024) ? "" : (length < 1048576) ? "Ki" : "Mi");
  737. priv->nextpmc551 = pmc551list;
  738. pmc551list = mtd;
  739. found++;
  740. }
  741. /* Exited early, reference left over */
  742. if (PCI_Device)
  743. pci_dev_put(PCI_Device);
  744. if (!pmc551list) {
  745. printk(KERN_NOTICE "pmc551: not detected\n");
  746. return -ENODEV;
  747. } else {
  748. printk(KERN_NOTICE "pmc551: %d pmc551 devices loaded\n", found);
  749. return 0;
  750. }
  751. }
  752. /*
  753. * PMC551 Card Cleanup
  754. */
  755. static void __exit cleanup_pmc551(void)
  756. {
  757. int found = 0;
  758. struct mtd_info *mtd;
  759. struct mypriv *priv;
  760. while ((mtd = pmc551list)) {
  761. priv = mtd->priv;
  762. pmc551list = priv->nextpmc551;
  763. if (priv->start) {
  764. printk(KERN_DEBUG "pmc551: unmapping %dMiB starting at "
  765. "0x%p\n", priv->asize >> 20, priv->start);
  766. pci_iounmap(priv->dev, priv->start);
  767. }
  768. pci_dev_put(priv->dev);
  769. kfree(mtd->priv);
  770. del_mtd_device(mtd);
  771. kfree(mtd);
  772. found++;
  773. }
  774. printk(KERN_NOTICE "pmc551: %d pmc551 devices unloaded\n", found);
  775. }
  776. module_init(init_pmc551);
  777. module_exit(cleanup_pmc551);