mca.c 13 KB

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