mca_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Written by Martin Kolinek, February 1996
  3. *
  4. * Changes:
  5. *
  6. * Chris Beauregard July 28th, 1996
  7. * - Fixed up integrated SCSI detection
  8. *
  9. * Chris Beauregard August 3rd, 1996
  10. * - Made mca_info local
  11. * - Made integrated registers accessible through standard function calls
  12. * - Added name field
  13. * - More sanity checking
  14. *
  15. * Chris Beauregard August 9th, 1996
  16. * - Rewrote /proc/mca
  17. *
  18. * Chris Beauregard January 7th, 1997
  19. * - Added basic NMI-processing
  20. * - Added more information to mca_info structure
  21. *
  22. * David Weinehall October 12th, 1998
  23. * - Made a lot of cleaning up in the source
  24. * - Added use of save_flags / restore_flags
  25. * - Added the 'driver_loaded' flag in MCA_adapter
  26. * - Added an alternative implemention of ZP Gu's mca_find_unused_adapter
  27. *
  28. * David Weinehall March 24th, 1999
  29. * - Fixed the output of 'Driver Installed' in /proc/mca/pos
  30. * - Made the Integrated Video & SCSI show up even if they have id 0000
  31. *
  32. * Alexander Viro November 9th, 1999
  33. * - Switched to regular procfs methods
  34. *
  35. * Alfred Arnold & David Weinehall August 23rd, 2000
  36. * - Added support for Planar POS-registers
  37. */
  38. #include <linux/module.h>
  39. #include <linux/types.h>
  40. #include <linux/errno.h>
  41. #include <linux/kernel.h>
  42. #include <linux/mca.h>
  43. #include <linux/kprobes.h>
  44. #include <linux/slab.h>
  45. #include <asm/io.h>
  46. #include <linux/proc_fs.h>
  47. #include <linux/mman.h>
  48. #include <linux/mm.h>
  49. #include <linux/pagemap.h>
  50. #include <linux/ioport.h>
  51. #include <asm/uaccess.h>
  52. #include <linux/init.h>
  53. static unsigned char which_scsi;
  54. int MCA_bus;
  55. EXPORT_SYMBOL(MCA_bus);
  56. /*
  57. * Motherboard register spinlock. Untested on SMP at the moment, but
  58. * are there any MCA SMP boxes?
  59. *
  60. * Yes - Alan
  61. */
  62. static DEFINE_SPINLOCK(mca_lock);
  63. /* Build the status info for the adapter */
  64. static void mca_configure_adapter_status(struct mca_device *mca_dev)
  65. {
  66. mca_dev->status = MCA_ADAPTER_NONE;
  67. mca_dev->pos_id = mca_dev->pos[0]
  68. + (mca_dev->pos[1] << 8);
  69. if (!mca_dev->pos_id && mca_dev->slot < MCA_MAX_SLOT_NR) {
  70. /*
  71. * id = 0x0000 usually indicates hardware failure,
  72. * however, ZP Gu (zpg@castle.net> reports that his 9556
  73. * has 0x0000 as id and everything still works. There
  74. * also seem to be an adapter with id = 0x0000; the
  75. * NCR Parallel Bus Memory Card. Until this is confirmed,
  76. * however, this code will stay.
  77. */
  78. mca_dev->status = MCA_ADAPTER_ERROR;
  79. return;
  80. } else if (mca_dev->pos_id != 0xffff) {
  81. /*
  82. * 0xffff usually indicates that there's no adapter,
  83. * however, some integrated adapters may have 0xffff as
  84. * their id and still be valid. Examples are on-board
  85. * VGA of the 55sx, the integrated SCSI of the 56 & 57,
  86. * and possibly also the 95 ULTIMEDIA.
  87. */
  88. mca_dev->status = MCA_ADAPTER_NORMAL;
  89. }
  90. if ((mca_dev->pos_id == 0xffff ||
  91. mca_dev->pos_id == 0x0000) && mca_dev->slot >= MCA_MAX_SLOT_NR) {
  92. int j;
  93. for (j = 2; j < 8; j++) {
  94. if (mca_dev->pos[j] != 0xff) {
  95. mca_dev->status = MCA_ADAPTER_NORMAL;
  96. break;
  97. }
  98. }
  99. }
  100. if (!(mca_dev->pos[2] & MCA_ENABLED)) {
  101. /* enabled bit is in POS 2 */
  102. mca_dev->status = MCA_ADAPTER_DISABLED;
  103. }
  104. } /* mca_configure_adapter_status */
  105. /*--------------------------------------------------------------------*/
  106. static struct resource mca_standard_resources[] = {
  107. { .start = 0x60, .end = 0x60, .name = "system control port B (MCA)" },
  108. { .start = 0x90, .end = 0x90, .name = "arbitration (MCA)" },
  109. { .start = 0x91, .end = 0x91, .name = "card Select Feedback (MCA)" },
  110. { .start = 0x92, .end = 0x92, .name = "system Control port A (MCA)" },
  111. { .start = 0x94, .end = 0x94, .name = "system board setup (MCA)" },
  112. { .start = 0x96, .end = 0x97, .name = "POS (MCA)" },
  113. { .start = 0x100, .end = 0x107, .name = "POS (MCA)" }
  114. };
  115. #define MCA_STANDARD_RESOURCES ARRAY_SIZE(mca_standard_resources)
  116. /*
  117. * mca_read_and_store_pos - read the POS registers into a memory buffer
  118. * @pos: a char pointer to 8 bytes, contains the POS register value on
  119. * successful return
  120. *
  121. * Returns 1 if a card actually exists (i.e. the pos isn't
  122. * all 0xff) or 0 otherwise
  123. */
  124. static int mca_read_and_store_pos(unsigned char *pos)
  125. {
  126. int j;
  127. int found = 0;
  128. for (j = 0; j < 8; j++) {
  129. pos[j] = inb_p(MCA_POS_REG(j));
  130. if (pos[j] != 0xff) {
  131. /* 0xff all across means no device. 0x00 means
  132. * something's broken, but a device is
  133. * probably there. However, if you get 0x00
  134. * from a motherboard register it won't matter
  135. * what we find. For the record, on the
  136. * 57SLC, the integrated SCSI adapter has
  137. * 0xffff for the adapter ID, but nonzero for
  138. * other registers. */
  139. found = 1;
  140. }
  141. }
  142. return found;
  143. }
  144. static unsigned char mca_pc_read_pos(struct mca_device *mca_dev, int reg)
  145. {
  146. unsigned char byte;
  147. unsigned long flags;
  148. if (reg < 0 || reg >= 8)
  149. return 0;
  150. spin_lock_irqsave(&mca_lock, flags);
  151. if (mca_dev->pos_register) {
  152. /* Disable adapter setup, enable motherboard setup */
  153. outb_p(0, MCA_ADAPTER_SETUP_REG);
  154. outb_p(mca_dev->pos_register, MCA_MOTHERBOARD_SETUP_REG);
  155. byte = inb_p(MCA_POS_REG(reg));
  156. outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
  157. } else {
  158. /* Make sure motherboard setup is off */
  159. outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
  160. /* Read the appropriate register */
  161. outb_p(0x8|(mca_dev->slot & 0xf), MCA_ADAPTER_SETUP_REG);
  162. byte = inb_p(MCA_POS_REG(reg));
  163. outb_p(0, MCA_ADAPTER_SETUP_REG);
  164. }
  165. spin_unlock_irqrestore(&mca_lock, flags);
  166. mca_dev->pos[reg] = byte;
  167. return byte;
  168. }
  169. static void mca_pc_write_pos(struct mca_device *mca_dev, int reg,
  170. unsigned char byte)
  171. {
  172. unsigned long flags;
  173. if (reg < 0 || reg >= 8)
  174. return;
  175. spin_lock_irqsave(&mca_lock, flags);
  176. /* Make sure motherboard setup is off */
  177. outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
  178. /* Read in the appropriate register */
  179. outb_p(0x8|(mca_dev->slot&0xf), MCA_ADAPTER_SETUP_REG);
  180. outb_p(byte, MCA_POS_REG(reg));
  181. outb_p(0, MCA_ADAPTER_SETUP_REG);
  182. spin_unlock_irqrestore(&mca_lock, flags);
  183. /* Update the global register list, while we have the byte */
  184. mca_dev->pos[reg] = byte;
  185. }
  186. /* for the primary MCA bus, we have identity transforms */
  187. static int mca_dummy_transform_irq(struct mca_device *mca_dev, int irq)
  188. {
  189. return irq;
  190. }
  191. static int mca_dummy_transform_ioport(struct mca_device *mca_dev, int port)
  192. {
  193. return port;
  194. }
  195. static void *mca_dummy_transform_memory(struct mca_device *mca_dev, void *mem)
  196. {
  197. return mem;
  198. }
  199. static int __init mca_init(void)
  200. {
  201. unsigned int i, j;
  202. struct mca_device *mca_dev;
  203. unsigned char pos[8];
  204. short mca_builtin_scsi_ports[] = {0xf7, 0xfd, 0x00};
  205. struct mca_bus *bus;
  206. /*
  207. * WARNING: Be careful when making changes here. Putting an adapter
  208. * and the motherboard simultaneously into setup mode may result in
  209. * damage to chips (according to The Indispensable PC Hardware Book
  210. * by Hans-Peter Messmer). Also, we disable system interrupts (so
  211. * that we are not disturbed in the middle of this).
  212. */
  213. /* Make sure the MCA bus is present */
  214. if (mca_system_init()) {
  215. printk(KERN_ERR "MCA bus system initialisation failed\n");
  216. return -ENODEV;
  217. }
  218. if (!MCA_bus)
  219. return -ENODEV;
  220. printk(KERN_INFO "Micro Channel bus detected.\n");
  221. /* All MCA systems have at least a primary bus */
  222. bus = mca_attach_bus(MCA_PRIMARY_BUS);
  223. if (!bus)
  224. goto out_nomem;
  225. bus->default_dma_mask = 0xffffffffLL;
  226. bus->f.mca_write_pos = mca_pc_write_pos;
  227. bus->f.mca_read_pos = mca_pc_read_pos;
  228. bus->f.mca_transform_irq = mca_dummy_transform_irq;
  229. bus->f.mca_transform_ioport = mca_dummy_transform_ioport;
  230. bus->f.mca_transform_memory = mca_dummy_transform_memory;
  231. /* get the motherboard device */
  232. mca_dev = kzalloc(sizeof(struct mca_device), GFP_KERNEL);
  233. if (unlikely(!mca_dev))
  234. goto out_nomem;
  235. /*
  236. * We do not expect many MCA interrupts during initialization,
  237. * but let us be safe:
  238. */
  239. spin_lock_irq(&mca_lock);
  240. /* Make sure adapter setup is off */
  241. outb_p(0, MCA_ADAPTER_SETUP_REG);
  242. /* Read motherboard POS registers */
  243. mca_dev->pos_register = 0x7f;
  244. outb_p(mca_dev->pos_register, MCA_MOTHERBOARD_SETUP_REG);
  245. mca_dev->name[0] = 0;
  246. mca_read_and_store_pos(mca_dev->pos);
  247. mca_configure_adapter_status(mca_dev);
  248. /* fake POS and slot for a motherboard */
  249. mca_dev->pos_id = MCA_MOTHERBOARD_POS;
  250. mca_dev->slot = MCA_MOTHERBOARD;
  251. mca_register_device(MCA_PRIMARY_BUS, mca_dev);
  252. mca_dev = kzalloc(sizeof(struct mca_device), GFP_ATOMIC);
  253. if (unlikely(!mca_dev))
  254. goto out_unlock_nomem;
  255. /* Put motherboard into video setup mode, read integrated video
  256. * POS registers, and turn motherboard setup off.
  257. */
  258. mca_dev->pos_register = 0xdf;
  259. outb_p(mca_dev->pos_register, MCA_MOTHERBOARD_SETUP_REG);
  260. mca_dev->name[0] = 0;
  261. mca_read_and_store_pos(mca_dev->pos);
  262. mca_configure_adapter_status(mca_dev);
  263. /* fake POS and slot for the integrated video */
  264. mca_dev->pos_id = MCA_INTEGVIDEO_POS;
  265. mca_dev->slot = MCA_INTEGVIDEO;
  266. mca_register_device(MCA_PRIMARY_BUS, mca_dev);
  267. /*
  268. * Put motherboard into scsi setup mode, read integrated scsi
  269. * POS registers, and turn motherboard setup off.
  270. *
  271. * It seems there are two possible SCSI registers. Martin says that
  272. * for the 56,57, 0xf7 is the one, but fails on the 76.
  273. * Alfredo (apena@vnet.ibm.com) says
  274. * 0xfd works on his machine. We'll try both of them. I figure it's
  275. * a good bet that only one could be valid at a time. This could
  276. * screw up though if one is used for something else on the other
  277. * machine.
  278. */
  279. for (i = 0; (which_scsi = mca_builtin_scsi_ports[i]) != 0; i++) {
  280. outb_p(which_scsi, MCA_MOTHERBOARD_SETUP_REG);
  281. if (mca_read_and_store_pos(pos))
  282. break;
  283. }
  284. if (which_scsi) {
  285. /* found a scsi card */
  286. mca_dev = kzalloc(sizeof(struct mca_device), GFP_ATOMIC);
  287. if (unlikely(!mca_dev))
  288. goto out_unlock_nomem;
  289. for (j = 0; j < 8; j++)
  290. mca_dev->pos[j] = pos[j];
  291. mca_configure_adapter_status(mca_dev);
  292. /* fake POS and slot for integrated SCSI controller */
  293. mca_dev->pos_id = MCA_INTEGSCSI_POS;
  294. mca_dev->slot = MCA_INTEGSCSI;
  295. mca_dev->pos_register = which_scsi;
  296. mca_register_device(MCA_PRIMARY_BUS, mca_dev);
  297. }
  298. /* Turn off motherboard setup */
  299. outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
  300. /*
  301. * Now loop over MCA slots: put each adapter into setup mode, and
  302. * read its POS registers. Then put adapter setup off.
  303. */
  304. for (i = 0; i < MCA_MAX_SLOT_NR; i++) {
  305. outb_p(0x8|(i&0xf), MCA_ADAPTER_SETUP_REG);
  306. if (!mca_read_and_store_pos(pos))
  307. continue;
  308. mca_dev = kzalloc(sizeof(struct mca_device), GFP_ATOMIC);
  309. if (unlikely(!mca_dev))
  310. goto out_unlock_nomem;
  311. for (j = 0; j < 8; j++)
  312. mca_dev->pos[j] = pos[j];
  313. mca_dev->driver_loaded = 0;
  314. mca_dev->slot = i;
  315. mca_dev->pos_register = 0;
  316. mca_configure_adapter_status(mca_dev);
  317. mca_register_device(MCA_PRIMARY_BUS, mca_dev);
  318. }
  319. outb_p(0, MCA_ADAPTER_SETUP_REG);
  320. /* Enable interrupts and return memory start */
  321. spin_unlock_irq(&mca_lock);
  322. for (i = 0; i < MCA_STANDARD_RESOURCES; i++)
  323. request_resource(&ioport_resource, mca_standard_resources + i);
  324. mca_do_proc_init();
  325. return 0;
  326. out_unlock_nomem:
  327. spin_unlock_irq(&mca_lock);
  328. out_nomem:
  329. printk(KERN_EMERG "Failed memory allocation in MCA setup!\n");
  330. return -ENOMEM;
  331. }
  332. subsys_initcall(mca_init);
  333. /*--------------------------------------------------------------------*/
  334. static __kprobes void
  335. mca_handle_nmi_device(struct mca_device *mca_dev, int check_flag)
  336. {
  337. int slot = mca_dev->slot;
  338. if (slot == MCA_INTEGSCSI) {
  339. printk(KERN_CRIT "NMI: caused by MCA integrated SCSI adapter (%s)\n",
  340. mca_dev->name);
  341. } else if (slot == MCA_INTEGVIDEO) {
  342. printk(KERN_CRIT "NMI: caused by MCA integrated video adapter (%s)\n",
  343. mca_dev->name);
  344. } else if (slot == MCA_MOTHERBOARD) {
  345. printk(KERN_CRIT "NMI: caused by motherboard (%s)\n",
  346. mca_dev->name);
  347. }
  348. /* More info available in POS 6 and 7? */
  349. if (check_flag) {
  350. unsigned char pos6, pos7;
  351. pos6 = mca_device_read_pos(mca_dev, 6);
  352. pos7 = mca_device_read_pos(mca_dev, 7);
  353. printk(KERN_CRIT "NMI: POS 6 = 0x%x, POS 7 = 0x%x\n", pos6, pos7);
  354. }
  355. } /* mca_handle_nmi_slot */
  356. /*--------------------------------------------------------------------*/
  357. static int __kprobes mca_handle_nmi_callback(struct device *dev, void *data)
  358. {
  359. struct mca_device *mca_dev = to_mca_device(dev);
  360. unsigned char pos5;
  361. pos5 = mca_device_read_pos(mca_dev, 5);
  362. if (!(pos5 & 0x80)) {
  363. /*
  364. * Bit 7 of POS 5 is reset when this adapter has a hardware
  365. * error. Bit 7 it reset if there's error information
  366. * available in POS 6 and 7.
  367. */
  368. mca_handle_nmi_device(mca_dev, !(pos5 & 0x40));
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. void __kprobes mca_handle_nmi(void)
  374. {
  375. /*
  376. * First try - scan the various adapters and see if a specific
  377. * adapter was responsible for the error.
  378. */
  379. bus_for_each_dev(&mca_bus_type, NULL, NULL, mca_handle_nmi_callback);
  380. }